diff --git a/components/esp_netif/include/esp_netif_ppp.h b/components/esp_netif/include/esp_netif_ppp.h index e8267ded481..45e02da2624 100644 --- a/components/esp_netif/include/esp_netif_ppp.h +++ b/components/esp_netif/include/esp_netif_ppp.h @@ -33,6 +33,7 @@ typedef struct esp_netif_ppp_config { esp_ip4_addr_t ppp_their_ip4_addr; /**< Set our preferred address, typically used when we're the PPP server */ esp_ip4_addr_t ppp_dns1_addr; /**< DNS to provide if peer asks for it, typically used when we're the PPP server */ esp_ip4_addr_t ppp_dns2_addr; /**< DNS to provide if peer asks for it, typically used when we're the PPP server */ + bool ppp_passive; /**< Try once to initiate connection, stay silent if it fails, typically used when we're the PPP server */ #endif // CONFIG_LWIP_PPP_SERVER_SUPPORT } esp_netif_ppp_config_t; diff --git a/components/esp_netif/include/esp_netif_types.h b/components/esp_netif/include/esp_netif_types.h index 84a06281773..49bf17413ac 100644 --- a/components/esp_netif/include/esp_netif_types.h +++ b/components/esp_netif/include/esp_netif_types.h @@ -105,6 +105,8 @@ typedef enum { IP_EVENT_PPP_GOT_IP, /*!< PPP interface got IP */ IP_EVENT_PPP_LOST_IP, /*!< PPP interface lost IP */ IP_EVENT_TX_RX, /*!< transmitting/receiving data packet */ + IP_EVENT_PPPD_GOT_IP, /*!< PPP server interface got IP */ + IP_EVENT_PPPD_LOST_IP, /*!< PPP server interface lost IP */ } ip_event_t; /** @brief IP event base declaration */ diff --git a/components/esp_netif/lwip/esp_netif_lwip_ppp.c b/components/esp_netif/lwip/esp_netif_lwip_ppp.c index 2741bbd6e93..5b6d2a8719d 100644 --- a/components/esp_netif/lwip/esp_netif_lwip_ppp.c +++ b/components/esp_netif/lwip/esp_netif_lwip_ppp.c @@ -39,6 +39,7 @@ typedef struct lwip_peer2peer_ctx { esp_ip4_addr_t ppp_their_ip4_addr; // their desired IP (IPADDR_ANY if no preference) esp_ip4_addr_t ppp_dns1_addr; // dns server 1 IP (IPADDR_ANY if no preference) esp_ip4_addr_t ppp_dns2_addr; // dns server 2 IP (IPADDR_ANY if no preference) + bool ppp_passive; // Set ppp_passive() and use ppp_listen() #endif ppp_pcb *ppp; } lwip_peer2peer_ctx_t; @@ -50,6 +51,7 @@ typedef struct lwip_peer2peer_ctx { static void on_ppp_status_changed(ppp_pcb *pcb, int err_code, void *ctx) { esp_netif_t *netif = ctx; + const char *name = netif->if_desc ? netif->if_desc : ""; ip_event_got_ip_t evt = { .esp_netif = netif, }; @@ -58,59 +60,59 @@ static void on_ppp_status_changed(ppp_pcb *pcb, int err_code, void *ctx) assert(obj->base.netif_type == PPP_LWIP_NETIF); switch (err_code) { case PPPERR_NONE: - ESP_LOGI(TAG, "Connected"); + ESP_LOGI(TAG, "%s: Connected", name); break; case PPPERR_PARAM: - ESP_LOGE(TAG, "Invalid parameter"); + ESP_LOGE(TAG, "%s: Invalid parameter", name); break; case PPPERR_OPEN: - ESP_LOGE(TAG, "Unable to open PPP session"); + ESP_LOGE(TAG, "%s: Unable to open PPP session", name); break; case PPPERR_DEVICE: - ESP_LOGE(TAG, "Invalid I/O device for PPP"); + ESP_LOGE(TAG, "%s: Invalid I/O device for PPP", name); break; case PPPERR_ALLOC: - ESP_LOGE(TAG, "Unable to allocate resources"); + ESP_LOGE(TAG, "%s: Unable to allocate resources", name); break; case PPPERR_USER: /* User interrupt */ - ESP_LOGI(TAG, "User interrupt"); + ESP_LOGI(TAG, "%s: User interrupt", name); break; case PPPERR_CONNECT: /* Connection lost */ - ESP_LOGI(TAG, "Connection lost"); + ESP_LOGI(TAG, "%s: Connection lost", name); esp_netif_update_default_netif(netif, ESP_NETIF_LOST_IP); err = esp_event_post(IP_EVENT, netif->lost_ip_event, &evt, sizeof(evt), 0); if (ESP_OK != err) { - ESP_LOGE(TAG, "esp_event_post failed with code %d", err); + ESP_LOGE(TAG, "%s: esp_event_post failed with code %d", name, err); } return; case PPPERR_AUTHFAIL: - ESP_LOGE(TAG, "Failed authentication challenge"); + ESP_LOGE(TAG, "%s: Failed authentication challenge", name); break; case PPPERR_PROTOCOL: - ESP_LOGE(TAG, "Failed to meet protocol"); + ESP_LOGE(TAG, "%s: Failed to meet protocol", name); break; case PPPERR_PEERDEAD: - ESP_LOGE(TAG, "Connection timeout"); + ESP_LOGE(TAG, "%s: Connection timeout", name); break; case PPPERR_IDLETIMEOUT: - ESP_LOGE(TAG, "Idle Timeout"); + ESP_LOGE(TAG, "%s: Idle Timeout", name); break; case PPPERR_CONNECTTIME: - ESP_LOGE(TAG, "Max connect time reached"); + ESP_LOGE(TAG, "%s: Max connect time reached", name); break; case PPPERR_LOOPBACK: - ESP_LOGE(TAG, "Loopback detected"); + ESP_LOGE(TAG, "%s: Loopback detected", name); break; default: - ESP_LOGE(TAG, "Unknown error code %d", err_code); + ESP_LOGE(TAG, "%s: Unknown error code %d", name, err_code); break; } if (obj->ppp_error_event_enabled) { err = esp_event_post(NETIF_PPP_STATUS, err_code, &netif, sizeof(netif), 0); if (err != ESP_OK) { - ESP_LOGE(TAG, "esp_event_post failed with code %d", err); + ESP_LOGE(TAG, "%s: esp_event_post failed with code %d", name, err); } } @@ -126,42 +128,44 @@ static void on_ppp_status_changed(ppp_pcb *pcb, int err_code, void *ctx) */ static void on_ppp_notify_phase(ppp_pcb *pcb, u8_t phase, void *ctx) { + esp_netif_t *netif = ctx; + const char *name = netif->if_desc ? netif->if_desc : ""; + switch (phase) { case PPP_PHASE_DEAD: - ESP_LOGD(TAG, "Phase Dead"); + ESP_LOGD(TAG, "%s: Phase Dead", name); break; case PPP_PHASE_INITIALIZE: - ESP_LOGD(TAG, "Phase Start"); + ESP_LOGD(TAG, "%s: Phase Start", name); break; case PPP_PHASE_ESTABLISH: - ESP_LOGD(TAG, "Phase Establish"); + ESP_LOGD(TAG, "%s: Phase Establish", name); break; case PPP_PHASE_AUTHENTICATE: - ESP_LOGD(TAG, "Phase Authenticate"); + ESP_LOGD(TAG, "%s: Phase Authenticate", name); break; case PPP_PHASE_NETWORK: - ESP_LOGD(TAG, "Phase Network"); + ESP_LOGD(TAG, "%s: Phase Network", name); break; case PPP_PHASE_RUNNING: - ESP_LOGD(TAG, "Phase Running"); + ESP_LOGD(TAG, "%s: Phase Running", name); break; case PPP_PHASE_TERMINATE: - ESP_LOGD(TAG, "Phase Terminate"); + ESP_LOGD(TAG, "%s: Phase Terminate", name); break; case PPP_PHASE_DISCONNECT: - ESP_LOGD(TAG, "Phase Disconnect"); + ESP_LOGD(TAG, "%s: Phase Disconnect", name); break; default: - ESP_LOGW(TAG, "Phase Unknown: %d", phase); + ESP_LOGW(TAG, "%s: Phase Unknown: %d", name, phase); break; } - esp_netif_t *netif = ctx; lwip_peer2peer_ctx_t *obj = (lwip_peer2peer_ctx_t *)netif->related_data; assert(obj->base.netif_type == PPP_LWIP_NETIF); if (obj && obj->ppp_phase_event_enabled) { esp_err_t err = esp_event_post(NETIF_PPP_STATUS, NETIF_PP_PHASE_OFFSET + phase, &netif, sizeof(netif), 0); if (err != ESP_OK) { - ESP_LOGE(TAG, "esp_event_post failed with code %d", err); + ESP_LOGE(TAG, "%s: esp_event_post failed with code %d", name, err); } } } @@ -281,6 +285,8 @@ esp_err_t esp_netif_start_ppp(esp_netif_t *esp_netif) // No need to request DNS servers from peer when providing DNS servers. ppp_set_usepeerdns(ppp_ctx->ppp, 0); } + + ppp_set_passive(ppp_ctx->ppp, ppp_ctx->ppp_passive); #endif // CONFIG_LWIP_PPP_SERVER_SUPPORT #if ESP_IPV6_AUTOCONFIG @@ -289,7 +295,12 @@ esp_err_t esp_netif_start_ppp(esp_netif_t *esp_netif) ESP_LOGD(TAG, "%s: Starting PPP connection: %p", __func__, ppp_ctx->ppp); #ifdef CONFIG_LWIP_PPP_SERVER_SUPPORT - esp_err_t err = ppp_listen(ppp_ctx->ppp); + esp_err_t err; + if (ppp_ctx->ppp_passive) { + err = ppp_listen(ppp_ctx->ppp); + } else { + err = ppp_connect(ppp_ctx->ppp, 0); + } #else err_t err = ppp_connect(ppp_ctx->ppp, 0); #endif @@ -353,6 +364,7 @@ esp_err_t esp_netif_ppp_set_params(esp_netif_t *netif, const esp_netif_ppp_confi obj->ppp_their_ip4_addr = config->ppp_their_ip4_addr; obj->ppp_dns1_addr = config->ppp_dns1_addr; obj->ppp_dns2_addr = config->ppp_dns2_addr; + obj->ppp_passive = config->ppp_passive; #endif return ESP_OK; } @@ -374,6 +386,7 @@ esp_err_t esp_netif_ppp_get_params(esp_netif_t *netif, esp_netif_ppp_config_t *c config->ppp_their_ip4_addr = obj->ppp_their_ip4_addr; config->ppp_dns1_addr = obj->ppp_dns1_addr; config->ppp_dns2_addr = obj->ppp_dns2_addr; + config->ppp_passive = obj->ppp_passive; #endif return ESP_OK;