From 52ca3a917d7bbf52381e0441e28aaac534edf099 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Thu, 21 Nov 2019 13:23:58 +0100 Subject: [PATCH] esp_netif: Support for PPPoS in esp_netif using lwip ppp client --- components/esp_common/src/esp_err_to_name.c | 6 + components/esp_netif/CMakeLists.txt | 1 + components/esp_netif/esp_netif_defaults.c | 9 + components/esp_netif/include/esp_netif.h | 22 ++- .../esp_netif/include/esp_netif_defaults.h | 20 ++ .../esp_netif/include/esp_netif_net_stack.h | 5 +- .../esp_netif/include/esp_netif_types.h | 17 +- components/esp_netif/lwip/esp_netif_lwip.c | 177 +++++++++++------- .../esp_netif/lwip/esp_netif_lwip_defaults.c | 33 +++- .../esp_netif/lwip/esp_netif_lwip_internal.h | 58 +++++- .../port/esp32/include/netif/ethernetif.h | 2 +- .../lwip/port/esp32/include/netif/wlanif.h | 2 +- components/lwip/port/esp32/netif/ethernetif.c | 3 +- components/lwip/port/esp32/netif/wlanif.c | 3 +- 14 files changed, 264 insertions(+), 94 deletions(-) diff --git a/components/esp_common/src/esp_err_to_name.c b/components/esp_common/src/esp_err_to_name.c index 24971ca490..ec97c0804e 100644 --- a/components/esp_common/src/esp_err_to_name.c +++ b/components/esp_common/src/esp_err_to_name.c @@ -488,6 +488,12 @@ static const esp_err_msg_t esp_err_msg_table[] = { # endif # ifdef ESP_ERR_ESP_NETIF_DRIVER_ATTACH_FAILED ERR_TBL_IT(ESP_ERR_ESP_NETIF_DRIVER_ATTACH_FAILED), /* 20488 0x5008 */ +# endif +# ifdef ESP_ERR_ESP_NETIF_INIT_FAILED + ERR_TBL_IT(ESP_ERR_ESP_NETIF_INIT_FAILED), /* 20489 0x5009 */ +# endif +# ifdef ESP_ERR_ESP_NETIF_DNS_NOT_CONFIGURED + ERR_TBL_IT(ESP_ERR_ESP_NETIF_DNS_NOT_CONFIGURED), /* 20490 0x500a */ # endif // components/esp_common/include/esp_err.h # ifdef ESP_ERR_FLASH_BASE diff --git a/components/esp_netif/CMakeLists.txt b/components/esp_netif/CMakeLists.txt index 6155f95795..b775ba3b17 100644 --- a/components/esp_netif/CMakeLists.txt +++ b/components/esp_netif/CMakeLists.txt @@ -2,6 +2,7 @@ idf_component_register(SRCS "esp_netif_handlers.c" "esp_netif_objects.c" "esp_netif_defaults.c" "lwip/esp_netif_lwip.c" + "lwip/esp_netif_lwip_ppp.c" "loopback/esp_netif_loopback.c" "lwip/esp_netif_lwip_defaults.c" "lwip/esp_netif_sta_list.c" diff --git a/components/esp_netif/esp_netif_defaults.c b/components/esp_netif/esp_netif_defaults.c index 5fc037f1db..39278857fb 100644 --- a/components/esp_netif/esp_netif_defaults.c +++ b/components/esp_netif/esp_netif_defaults.c @@ -67,3 +67,12 @@ const esp_netif_inherent_config_t _g_esp_netif_inherent_eth_config = { .if_desc = "eth", .route_prio = 50 }; + +const esp_netif_inherent_config_t _g_esp_netif_inherent_ppp_config = { + .flags = ESP_NETIF_FLAG_IS_PPP, + .lost_ip_event = IP_EVENT_PPP_LOST_IP, + .get_ip_event = IP_EVENT_PPP_GOT_IP, + .if_key = "PPP_DEF", + .if_desc = "ppp", + .route_prio = 128 +}; diff --git a/components/esp_netif/include/esp_netif.h b/components/esp_netif/include/esp_netif.h index 2f44160371..fc0cc2740c 100644 --- a/components/esp_netif/include/esp_netif.h +++ b/components/esp_netif/include/esp_netif.h @@ -26,10 +26,6 @@ #include "esp_eth_netif_glue.h" #endif -#ifdef __cplusplus -extern "C" { -#endif - // // Note: tcpip_adapter legacy API has to be included by default to provide full compatibility // for applications that used tcpip_adapter API without explicit inclusion of tcpip_adapter.h @@ -40,6 +36,10 @@ extern "C" { #undef _ESP_NETIF_SUPPRESS_LEGACY_WARNING_ #endif // CONFIG_ESP_NETIF_TCPIP_ADAPTER_COMPATIBLE_LAYER +#ifdef __cplusplus +extern "C" { +#endif + /** * @defgroup ESP_NETIF_INIT_API ESP-NETIF Initialization API * @brief Initialization and deinitialization of underlying TCP/IP stack and esp-netif instances @@ -101,7 +101,7 @@ void esp_netif_destroy(esp_netif_t *esp_netif); * */ esp_err_t esp_netif_set_driver_config(esp_netif_t *esp_netif, - const esp_netif_driver_ifconfig_t *driver_config); + const esp_netif_driver_ifconfig_t *driver_config); /** * @brief Attaches esp_netif instance to the io driver handle @@ -411,7 +411,9 @@ int esp_netif_get_netif_impl_index(esp_netif_t *esp_netif); * - ESP_ERR_ESP_NETIF_DHCP_ALREADY_STOPPED * - ESP_ERR_ESP_NETIF_DHCP_ALREADY_STARTED */ -esp_err_t esp_netif_dhcps_option(esp_netif_t *esp_netif, esp_netif_dhcp_option_mode_t opt_op, esp_netif_dhcp_option_id_t opt_id, void *opt_val, uint32_t opt_len); +esp_err_t +esp_netif_dhcps_option(esp_netif_t *esp_netif, esp_netif_dhcp_option_mode_t opt_op, esp_netif_dhcp_option_id_t opt_id, + void *opt_val, uint32_t opt_len); /** * @brief Set or Get DHCP client option @@ -428,7 +430,9 @@ esp_err_t esp_netif_dhcps_option(esp_netif_t *esp_netif, esp_netif_dhcp_option_m * - ESP_ERR_ESP_NETIF_DHCP_ALREADY_STOPPED * - ESP_ERR_ESP_NETIF_DHCP_ALREADY_STARTED */ -esp_err_t esp_netif_dhcpc_option(esp_netif_t *esp_netif, esp_netif_dhcp_option_mode_t opt_op, esp_netif_dhcp_option_id_t opt_id, void *opt_val, uint32_t opt_len); +esp_err_t +esp_netif_dhcpc_option(esp_netif_t *esp_netif, esp_netif_dhcp_option_mode_t opt_op, esp_netif_dhcp_option_id_t opt_id, + void *opt_val, uint32_t opt_len); /** * @brief Start DHCP client (only if enabled in interface object) @@ -630,7 +634,7 @@ void esp_netif_set_ip4_addr(esp_ip4_addr_t *addr, uint8_t a, uint8_t b, uint8_t * @return either pointer to buf which now holds the ASCII * representation of addr or NULL if buf was too small */ -char * esp_ip4addr_ntoa(const esp_ip4_addr_t *addr, char *buf, int buflen); +char *esp_ip4addr_ntoa(const esp_ip4_addr_t *addr, char *buf, int buflen); /** * @brief Ascii internet address interpretation routine @@ -731,7 +735,7 @@ int32_t esp_netif_get_event_id(esp_netif_t *esp_netif, esp_netif_ip_event_type_t * * @return First netif from the list if supplied parameter is NULL, next one otherwise */ -esp_netif_t* esp_netif_next(esp_netif_t* esp_netif); +esp_netif_t *esp_netif_next(esp_netif_t *esp_netif); /** * @brief Returns number of registered esp_netif objects diff --git a/components/esp_netif/include/esp_netif_defaults.h b/components/esp_netif/include/esp_netif_defaults.h index f2eea9d6ef..caa5a8a008 100644 --- a/components/esp_netif/include/esp_netif_defaults.h +++ b/components/esp_netif/include/esp_netif_defaults.h @@ -52,6 +52,16 @@ extern "C" { .stack = ESP_NETIF_NETSTACK_DEFAULT_WIFI_STA, \ .driver = NULL, \ } + +/** +* @brief Default configuration reference of PPP client +*/ +#define ESP_NETIF_DEFAULT_PPP() \ + { \ + .base = ESP_NETIF_BASE_DEFAULT_PPP, \ + .stack = ESP_NETIF_NETSTACK_DEFAULT_PPP, \ + .driver = NULL, \ + } /** * @brief Default base config (esp-netif inherent) of WIFI STA */ @@ -67,9 +77,17 @@ extern "C" { */ #define ESP_NETIF_BASE_DEFAULT_ETH &_g_esp_netif_inherent_eth_config +/** + * @brief Default base config (esp-netif inherent) of ppp interface + */ +#define ESP_NETIF_BASE_DEFAULT_PPP &_g_esp_netif_inherent_ppp_config + + #define ESP_NETIF_NETSTACK_DEFAULT_ETH _g_esp_netif_netstack_default_eth #define ESP_NETIF_NETSTACK_DEFAULT_WIFI_STA _g_esp_netif_netstack_default_wifi_sta #define ESP_NETIF_NETSTACK_DEFAULT_WIFI_AP _g_esp_netif_netstack_default_wifi_ap +#define ESP_NETIF_NETSTACK_DEFAULT_PPP _g_esp_netif_netstack_default_ppp + // // Include default network stacks configs // - Network stack configurations are provided in a specific network stack @@ -79,6 +97,7 @@ extern "C" { extern const esp_netif_netstack_config_t *_g_esp_netif_netstack_default_eth; extern const esp_netif_netstack_config_t *_g_esp_netif_netstack_default_wifi_sta; extern const esp_netif_netstack_config_t *_g_esp_netif_netstack_default_wifi_ap; +extern const esp_netif_netstack_config_t *_g_esp_netif_netstack_default_ppp; // // Include default common configs inherent to esp-netif @@ -88,6 +107,7 @@ extern const esp_netif_netstack_config_t *_g_esp_netif_netstack_default_wifi_ap; extern const esp_netif_inherent_config_t _g_esp_netif_inherent_sta_config; extern const esp_netif_inherent_config_t _g_esp_netif_inherent_ap_config; extern const esp_netif_inherent_config_t _g_esp_netif_inherent_eth_config; +extern const esp_netif_inherent_config_t _g_esp_netif_inherent_ppp_config; #ifdef __cplusplus } diff --git a/components/esp_netif/include/esp_netif_net_stack.h b/components/esp_netif/include/esp_netif_net_stack.h index 8b1b2eaef7..334183a119 100644 --- a/components/esp_netif/include/esp_netif_net_stack.h +++ b/components/esp_netif/include/esp_netif_net_stack.h @@ -37,7 +37,10 @@ extern "C" { esp_netif_t* esp_netif_get_handle_from_netif_impl(void *dev); /** - * @brief Returns network stack specific implementation handle + * @brief Returns network stack specific implementation handle (if supported) + * + * Note that it is not supported to acquire PPP netif impl pointer and + * this function will return NULL for esp_netif instances configured to PPP mode * * @param[in] esp_netif Handle to esp-netif instance * diff --git a/components/esp_netif/include/esp_netif_types.h b/components/esp_netif/include/esp_netif_types.h index 05373ba5b0..9c785ad883 100644 --- a/components/esp_netif/include/esp_netif_types.h +++ b/components/esp_netif/include/esp_netif_types.h @@ -31,6 +31,8 @@ extern "C" { #define ESP_ERR_ESP_NETIF_NO_MEM ESP_ERR_ESP_NETIF_BASE + 0x06 #define ESP_ERR_ESP_NETIF_DHCP_NOT_STOPPED ESP_ERR_ESP_NETIF_BASE + 0x07 #define ESP_ERR_ESP_NETIF_DRIVER_ATTACH_FAILED ESP_ERR_ESP_NETIF_BASE + 0x08 +#define ESP_ERR_ESP_NETIF_INIT_FAILED ESP_ERR_ESP_NETIF_BASE + 0x09 +#define ESP_ERR_ESP_NETIF_DNS_NOT_CONFIGURED ESP_ERR_ESP_NETIF_BASE + 0x0A /** @brief Type of esp_netif_object server */ @@ -80,11 +82,13 @@ typedef enum{ /** IP event declarations */ typedef enum { - IP_EVENT_STA_GOT_IP, /*!< ESP32 station got IP from connected AP */ - IP_EVENT_STA_LOST_IP, /*!< ESP32 station lost IP and the IP is reset to 0 */ - IP_EVENT_AP_STAIPASSIGNED, /*!< ESP32 soft-AP assign an IP to a connected station */ - IP_EVENT_GOT_IP6, /*!< ESP32 station or ap or ethernet interface v6IP addr is preferred */ - IP_EVENT_ETH_GOT_IP, /*!< ESP32 ethernet got IP from connected AP */ + IP_EVENT_STA_GOT_IP, /*!< station got IP from connected AP */ + IP_EVENT_STA_LOST_IP, /*!< station lost IP and the IP is reset to 0 */ + IP_EVENT_AP_STAIPASSIGNED, /*!< soft-AP assign an IP to a connected station */ + IP_EVENT_GOT_IP6, /*!< station or ap or ethernet interface v6IP addr is preferred */ + IP_EVENT_ETH_GOT_IP, /*!< ethernet got IP from connected AP */ + IP_EVENT_PPP_GOT_IP, /*!< PPP interface got IP */ + IP_EVENT_PPP_LOST_IP, /*!< PPP interface lost IP */ } ip_event_t; /** @brief IP event base declaration */ @@ -131,7 +135,8 @@ typedef enum esp_netif_flags { ESP_NETIF_DHCP_SERVER = 1 << 1, ESP_NETIF_FLAG_AUTOUP = 1 << 2, ESP_NETIF_FLAG_GARP = 1 << 3, - ESP_NETIF_FLAG_EVENT_IP_MODIFIED = 1 << 4 + ESP_NETIF_FLAG_EVENT_IP_MODIFIED = 1 << 4, + ESP_NETIF_FLAG_IS_PPP = 1 << 5 } esp_netif_flags_t; typedef enum esp_netif_ip_event_type { diff --git a/components/esp_netif/lwip/esp_netif_lwip.c b/components/esp_netif/lwip/esp_netif_lwip.c index c736577451..91e7b13fd3 100644 --- a/components/esp_netif/lwip/esp_netif_lwip.c +++ b/components/esp_netif/lwip/esp_netif_lwip.c @@ -13,6 +13,7 @@ // limitations under the License. #include +#include #include "esp_netif_lwip_internal.h" @@ -32,6 +33,7 @@ #include "lwip/dns.h" #endif +#include "esp_netif_lwip_ppp.h" #include "dhcpserver/dhcpserver.h" #include "dhcpserver/dhcpserver_options.h" @@ -45,10 +47,13 @@ #define ESP_NETIF_HOSTNAME_MAX_SIZE 32 /** - * @brief thread safe tcpip function utility macro + * @brief lwip thread safe tcpip function utility macro */ -#define _LWIP_TASK_IPC_CALL(function, netif, param) \ -{ \ +#define _RUN_IN_LWIP_TASK_IF_SUPPORTED(function, netif, param) \ +{ \ + if (netif->is_ppp_netif) { \ + return ESP_ERR_NOT_SUPPORTED; \ + } \ return esp_netif_lwip_ipc_call(function, netif, (void *)(param)); \ } @@ -61,42 +66,6 @@ typedef enum esp_netif_action { ESP_NETIF_STOPPED, } esp_netif_action_t; -/** - * @brief Main esp-netif container with interface related information - */ -struct esp_netif_obj { - // default interface addresses - uint8_t mac[NETIF_MAX_HWADDR_LEN]; - esp_netif_ip_info_t* ip_info; - esp_netif_ip_info_t* ip_info_old; - - // lwip netif related - struct netif* lwip_netif; - err_t (*lwip_init_fn)(struct netif*); - void (*lwip_input_fn)(struct netif *netif, void *buffer, size_t len, void *eb); - - // io driver related - void* driver_handle; - esp_err_t (*driver_transmit)(void *h, void *buffer, size_t len); - void (*driver_free_rx_buffer)(void *h, void* buffer); - - // dhcp related - esp_netif_dhcp_status_t dhcpc_status; - esp_netif_dhcp_status_t dhcps_status; - bool timer_running; - - // event translation - ip_event_t get_ip_event; - ip_event_t lost_ip_event; - - // misc flags, types, keys, priority - esp_netif_flags_t flags; - char * hostname; - char * if_key; - char * if_desc; - int route_prio; -}; - // // Internal variables for this module // @@ -167,6 +136,20 @@ static esp_netif_t* esp_netif_is_active(esp_netif_t *arg) return NULL; } +/** + * @brief This function sets default netif no matter which implementation used + * + * @param esp_netif handle to network interface + */ +static void esp_netif_set_default_netif(esp_netif_t *esp_netif) +{ + if (esp_netif->is_ppp_netif) { + esp_netif_ppp_set_default_netif(esp_netif->netif_handle); + } else { + netif_set_default(esp_netif->netif_handle); + } +} + /** * @brief This function sets default routing netif based on priorities of all interfaces which are up * @param esp_netif current interface which just updated state @@ -183,10 +166,10 @@ static void esp_netif_update_default_netif(esp_netif_t *esp_netif, esp_netif_act s_last_default_esp_netif = esp_netif_is_active(s_last_default_esp_netif); if (s_last_default_esp_netif && esp_netif_is_netif_up(s_last_default_esp_netif) && (s_last_default_esp_netif->route_prio > esp_netif->route_prio)) { - netif_set_default(s_last_default_esp_netif->lwip_netif); + esp_netif_set_default_netif(s_last_default_esp_netif); } else if (esp_netif_is_netif_up(esp_netif)) { s_last_default_esp_netif = esp_netif; - netif_set_default(s_last_default_esp_netif->lwip_netif); + esp_netif_set_default_netif(s_last_default_esp_netif); } } break; @@ -212,7 +195,7 @@ static void esp_netif_update_default_netif(esp_netif_t *esp_netif, esp_netif_act } esp_netif_list_unlock(); if (s_last_default_esp_netif && esp_netif_is_netif_up(s_last_default_esp_netif)) { - netif_set_default(s_last_default_esp_netif->lwip_netif); + esp_netif_set_default_netif(s_last_default_esp_netif); } } break; @@ -243,13 +226,15 @@ esp_netif_iodriver_handle esp_netif_get_io_driver(esp_netif_t *esp_netif) esp_netif_t* esp_netif_get_handle_from_netif_impl(void *dev) { + // ppp_pcb ptr would never get to app code, so this function only works with vanilla lwip impl struct netif *lwip_netif = dev; return lwip_netif->state; } void* esp_netif_get_netif_impl(esp_netif_t *esp_netif) { - if (esp_netif) { + // get impl ptr only for vanilla lwip impl (ppp_pcb not supported) + if (esp_netif && !esp_netif->is_ppp_netif) { return esp_netif->lwip_netif; } return NULL; @@ -333,11 +318,25 @@ static esp_err_t esp_netif_init_configuration(esp_netif_t *esp_netif, const esp_ // Install network stack functions -- connects netif and L3 stack const esp_netif_netstack_config_t *esp_netif_stack_config = cfg->stack; - if (esp_netif_stack_config->init_fn) { - esp_netif->lwip_init_fn = esp_netif_stack_config->init_fn; - } - if (esp_netif_stack_config->input_fn) { - esp_netif->lwip_input_fn = esp_netif_stack_config->input_fn; + if (cfg->base->flags & ESP_NETIF_FLAG_IS_PPP) { + esp_netif->lwip_ppp_ctx = esp_netif_new_ppp(esp_netif, esp_netif_stack_config); + if (esp_netif->lwip_ppp_ctx == NULL) { + return ESP_ERR_ESP_NETIF_INIT_FAILED; + } + esp_netif->lwip_input_fn = esp_netif_stack_config->lwip_ppp.input_fn; + esp_netif->is_ppp_netif = true; + // Make the netif handle (used for tcpip input function) the ppp_netif + esp_netif->netif_handle = esp_netif->lwip_ppp_ctx; + } else { + if (esp_netif_stack_config-> lwip.init_fn) { + esp_netif->lwip_init_fn = esp_netif_stack_config->lwip.init_fn; + } + if (esp_netif_stack_config->lwip.input_fn) { + esp_netif->lwip_input_fn = esp_netif_stack_config->lwip.input_fn; + } + // Make the netif handle (used for tcpip input function) the lwip_netif itself + esp_netif->netif_handle = esp_netif->lwip_netif; + } // Install IO functions only if provided -- connects driver and netif @@ -442,6 +441,14 @@ static esp_err_t esp_netif_lwip_add(esp_netif_t *esp_netif) return ESP_ERR_NO_MEM; } } + if (esp_netif->flags & ESP_NETIF_FLAG_IS_PPP) { + err_t err = esp_netif->lwip_init_fn(NULL); + if (err != ERR_OK) { + ESP_LOGE(TAG, "Init netif failed with %d", err); + return ESP_ERR_ESP_NETIF_INIT_FAILED; + } + } + if (NULL == netif_add(esp_netif->lwip_netif, (struct ip4_addr*)&esp_netif->ip_info->ip, (struct ip4_addr*)&esp_netif->ip_info->netmask, (struct ip4_addr*)&esp_netif->ip_info->gw, esp_netif, esp_netif->lwip_init_fn, tcpip_input)) { @@ -460,6 +467,9 @@ void esp_netif_destroy(esp_netif_t *esp_netif) free(esp_netif->if_key); free(esp_netif->if_desc); esp_netif_lwip_remove(esp_netif); + if (esp_netif->is_ppp_netif) { + esp_netif_destroy_ppp(esp_netif->netif_handle); + } free(esp_netif->lwip_netif); free(esp_netif->hostname); if (s_last_default_esp_netif == esp_netif) { @@ -510,6 +520,9 @@ esp_err_t esp_netif_set_mac(esp_netif_t *esp_netif, uint8_t mac[]) if (esp_netif == NULL || esp_netif->lwip_netif == NULL) { return ESP_ERR_ESP_NETIF_IF_NOT_READY; } + if (esp_netif->is_ppp_netif) { + return ESP_ERR_NOT_SUPPORTED; + } memcpy(esp_netif->mac, mac, NETIF_MAX_HWADDR_LEN); memcpy(esp_netif->lwip_netif->hwaddr, mac, NETIF_MAX_HWADDR_LEN); return ESP_OK; @@ -601,7 +614,18 @@ static esp_err_t esp_netif_start_api(esp_netif_api_msg_t *msg) return ESP_OK; } -esp_err_t esp_netif_start(esp_netif_t *esp_netif) _LWIP_TASK_IPC_CALL(esp_netif_start_api, esp_netif, NULL) +esp_err_t esp_netif_start(esp_netif_t *esp_netif) +{ + if (esp_netif->is_ppp_netif) { + // No need to start PPP interface in lwip thread + esp_err_t ret = esp_netif_start_ppp(esp_netif->lwip_ppp_ctx); + if (ret == ESP_OK) { + esp_netif_update_default_netif(esp_netif, ESP_NETIF_STARTED); + } + return ret; + } + return esp_netif_lwip_ipc_call(esp_netif_start_api, esp_netif, NULL); +} static esp_err_t esp_netif_stop_api(esp_netif_api_msg_t *msg) { @@ -639,7 +663,18 @@ static esp_err_t esp_netif_stop_api(esp_netif_api_msg_t *msg) return ESP_OK; } -esp_err_t esp_netif_stop(esp_netif_t *esp_netif) _LWIP_TASK_IPC_CALL(esp_netif_stop_api, esp_netif, NULL) +esp_err_t esp_netif_stop(esp_netif_t *esp_netif) +{ + if (esp_netif->is_ppp_netif) { + // No need to stop PPP interface in lwip thread + esp_err_t ret = esp_netif_stop_ppp(esp_netif->lwip_ppp_ctx); + if (ret == ESP_OK) { + esp_netif_update_default_netif(esp_netif, ESP_NETIF_STOPPED);; + } + return ret; + } + return esp_netif_lwip_ipc_call(esp_netif_stop_api, esp_netif, NULL); +} // // IO translate functions @@ -657,7 +692,7 @@ esp_err_t esp_netif_transmit(esp_netif_t *esp_netif, void* data, size_t len) esp_err_t esp_netif_receive(esp_netif_t *esp_netif, void *buffer, size_t len, void *eb) { - esp_netif->lwip_input_fn(esp_netif->lwip_netif, buffer, len, eb); + esp_netif->lwip_input_fn(esp_netif->netif_handle, buffer, len, eb); return ESP_OK; } @@ -817,7 +852,7 @@ static esp_err_t esp_netif_dhcpc_stop_api(esp_netif_api_msg_t *msg) return ESP_OK; } -esp_err_t esp_netif_dhcpc_stop(esp_netif_t *esp_netif) _LWIP_TASK_IPC_CALL(esp_netif_dhcpc_stop_api, esp_netif, NULL) +esp_err_t esp_netif_dhcpc_stop(esp_netif_t *esp_netif) _RUN_IN_LWIP_TASK_IF_SUPPORTED(esp_netif_dhcpc_stop_api, esp_netif, NULL) static esp_err_t esp_netif_dhcpc_start_api(esp_netif_api_msg_t *msg) { @@ -866,11 +901,11 @@ static esp_err_t esp_netif_dhcpc_start_api(esp_netif_api_msg_t *msg) } } -esp_err_t esp_netif_dhcpc_start(esp_netif_t *esp_netif) _LWIP_TASK_IPC_CALL(esp_netif_dhcpc_start_api, esp_netif, NULL) +esp_err_t esp_netif_dhcpc_start(esp_netif_t *esp_netif) _RUN_IN_LWIP_TASK_IF_SUPPORTED(esp_netif_dhcpc_start_api, esp_netif, NULL) esp_err_t esp_netif_dhcps_get_status(esp_netif_t *esp_netif, esp_netif_dhcp_status_t *status) { - if (!esp_netif || (esp_netif->flags & ESP_NETIF_DHCP_CLIENT)) { + if (!esp_netif || (esp_netif->flags & ESP_NETIF_DHCP_CLIENT) || esp_netif->is_ppp_netif) { return ESP_ERR_INVALID_ARG; } @@ -880,7 +915,7 @@ esp_err_t esp_netif_dhcps_get_status(esp_netif_t *esp_netif, esp_netif_dhcp_stat esp_err_t esp_netif_dhcpc_get_status(esp_netif_t *esp_netif, esp_netif_dhcp_status_t *status) { - if (!esp_netif || (esp_netif->flags & ESP_NETIF_DHCP_SERVER)) { + if (!esp_netif || (esp_netif->flags & ESP_NETIF_DHCP_SERVER) || esp_netif->is_ppp_netif) { return ESP_ERR_INVALID_ARG; } @@ -915,7 +950,7 @@ static esp_err_t esp_netif_dhcps_start_api(esp_netif_api_msg_t *msg) } } -esp_err_t esp_netif_dhcps_start(esp_netif_t *esp_netif) _LWIP_TASK_IPC_CALL(esp_netif_dhcps_start_api, esp_netif, NULL) +esp_err_t esp_netif_dhcps_start(esp_netif_t *esp_netif) _RUN_IN_LWIP_TASK_IF_SUPPORTED(esp_netif_dhcps_start_api, esp_netif, NULL) static esp_err_t esp_netif_dhcps_stop_api(esp_netif_api_msg_t *msg) { @@ -945,7 +980,7 @@ static esp_err_t esp_netif_dhcps_stop_api(esp_netif_api_msg_t *msg) return ESP_OK; } -esp_err_t esp_netif_dhcps_stop(esp_netif_t *esp_netif) _LWIP_TASK_IPC_CALL(esp_netif_dhcps_stop_api, esp_netif, NULL) +esp_err_t esp_netif_dhcps_stop(esp_netif_t *esp_netif) _RUN_IN_LWIP_TASK_IF_SUPPORTED(esp_netif_dhcps_stop_api, esp_netif, NULL) static esp_err_t esp_netif_set_hostname_api(esp_netif_api_msg_t *msg) { @@ -984,13 +1019,13 @@ static esp_err_t esp_netif_set_hostname_api(esp_netif_api_msg_t *msg) #endif } -esp_err_t esp_netif_set_hostname(esp_netif_t *esp_netif, const char *hostname) _LWIP_TASK_IPC_CALL(esp_netif_set_hostname_api, esp_netif, hostname) +esp_err_t esp_netif_set_hostname(esp_netif_t *esp_netif, const char *hostname) _RUN_IN_LWIP_TASK_IF_SUPPORTED(esp_netif_set_hostname_api, esp_netif, hostname) esp_err_t esp_netif_get_hostname(esp_netif_t *esp_netif, const char **hostname) { ESP_LOGD(TAG, "%s esp_netif:%p", __func__, esp_netif); - if (!esp_netif) { + if (!esp_netif || esp_netif->is_ppp_netif) { return ESP_ERR_INVALID_ARG; } @@ -1029,7 +1064,7 @@ static esp_err_t esp_netif_up_api(esp_netif_api_msg_t *msg) return ESP_OK; } -esp_err_t esp_netif_up(esp_netif_t *esp_netif) _LWIP_TASK_IPC_CALL(esp_netif_up_api, esp_netif, NULL) +esp_err_t esp_netif_up(esp_netif_t *esp_netif) _RUN_IN_LWIP_TASK_IF_SUPPORTED(esp_netif_up_api, esp_netif, NULL) static esp_err_t esp_netif_down_api(esp_netif_api_msg_t *msg) { @@ -1063,7 +1098,7 @@ static esp_err_t esp_netif_down_api(esp_netif_api_msg_t *msg) return ESP_OK; } -esp_err_t esp_netif_down(esp_netif_t *esp_netif) _LWIP_TASK_IPC_CALL(esp_netif_down_api, esp_netif, NULL) +esp_err_t esp_netif_down(esp_netif_t *esp_netif) _RUN_IN_LWIP_TASK_IF_SUPPORTED(esp_netif_down_api, esp_netif, NULL) bool esp_netif_is_netif_up(esp_netif_t *esp_netif) { @@ -1135,7 +1170,7 @@ static esp_err_t esp_netif_set_ip_old_info_api(esp_netif_api_msg_t *msg) return ESP_OK; } -esp_err_t esp_netif_set_old_ip_info(esp_netif_t *esp_netif, const esp_netif_ip_info_t *ip_info) _LWIP_TASK_IPC_CALL(esp_netif_set_ip_old_info_api, esp_netif, ip_info) +esp_err_t esp_netif_set_old_ip_info(esp_netif_t *esp_netif, const esp_netif_ip_info_t *ip_info) _RUN_IN_LWIP_TASK_IF_SUPPORTED(esp_netif_set_ip_old_info_api, esp_netif, ip_info) static esp_err_t esp_netif_set_ip_info_api(esp_netif_api_msg_t *msg) { @@ -1195,7 +1230,7 @@ static esp_err_t esp_netif_set_ip_info_api(esp_netif_api_msg_t *msg) return ESP_OK; } -esp_err_t esp_netif_set_ip_info(esp_netif_t *esp_netif, const esp_netif_ip_info_t *ip_info) _LWIP_TASK_IPC_CALL(esp_netif_set_ip_info_api, esp_netif, ip_info) +esp_err_t esp_netif_set_ip_info(esp_netif_t *esp_netif, const esp_netif_ip_info_t *ip_info) _RUN_IN_LWIP_TASK_IF_SUPPORTED(esp_netif_set_ip_info_api, esp_netif, ip_info) static esp_err_t esp_netif_set_dns_info_api(esp_netif_api_msg_t *msg) { @@ -1242,6 +1277,9 @@ static esp_err_t esp_netif_set_dns_info_api(esp_netif_api_msg_t *msg) esp_err_t esp_netif_set_dns_info(esp_netif_t *esp_netif, esp_netif_dns_type_t type, esp_netif_dns_info_t *dns) { + if (esp_netif->is_ppp_netif) { + return ESP_ERR_NOT_SUPPORTED; + } esp_netif_dns_param_t dns_param = { .dns_type = type, .dns_info = dns @@ -1279,6 +1317,15 @@ static esp_err_t esp_netif_get_dns_info_api(esp_netif_api_msg_t *msg) esp_err_t esp_netif_get_dns_info(esp_netif_t *esp_netif, esp_netif_dns_type_t type, esp_netif_dns_info_t *dns) { + if (esp_netif->is_ppp_netif) { + const ip_addr_t *dns_ip = dns_getserver(type); + if (dns_ip == IP_ADDR_ANY) { + return ESP_ERR_ESP_NETIF_DNS_NOT_CONFIGURED; + } + memcpy(&dns->ip.u_addr.ip4, &dns_ip->u_addr.ip4, sizeof(ip4_addr_t)); + return ESP_OK; + } + esp_netif_dns_param_t dns_param = { .dns_type = type, .dns_info = dns @@ -1330,13 +1377,13 @@ static esp_err_t esp_netif_create_ip6_linklocal_api(esp_netif_api_msg_t *msg) } } -esp_err_t esp_netif_create_ip6_linklocal(esp_netif_t *esp_netif) _LWIP_TASK_IPC_CALL(esp_netif_create_ip6_linklocal_api, esp_netif, NULL) +esp_err_t esp_netif_create_ip6_linklocal(esp_netif_t *esp_netif) _RUN_IN_LWIP_TASK_IF_SUPPORTED(esp_netif_create_ip6_linklocal_api, esp_netif, NULL) esp_err_t esp_netif_get_ip6_linklocal(esp_netif_t *esp_netif, esp_ip6_addr_t *if_ip6) { ESP_LOGD(TAG, "%s esp-netif:%p", __func__, esp_netif); - if (esp_netif == NULL || if_ip6 == NULL) { + if (esp_netif == NULL || if_ip6 == NULL || esp_netif->is_ppp_netif) { return ESP_ERR_ESP_NETIF_INVALID_PARAMS; } struct netif *p_netif = esp_netif->lwip_netif; diff --git a/components/esp_netif/lwip/esp_netif_lwip_defaults.c b/components/esp_netif/lwip/esp_netif_lwip_defaults.c index 6fc61088ac..955285024b 100644 --- a/components/esp_netif/lwip/esp_netif_lwip_defaults.c +++ b/components/esp_netif/lwip/esp_netif_lwip_defaults.c @@ -14,6 +14,7 @@ #include "esp_netif.h" #include "esp_netif_lwip_internal.h" +#include "esp_netif_lwip_ppp.h" #include "netif/wlanif.h" #include "netif/ethernetif.h" @@ -24,18 +25,36 @@ // static const struct esp_netif_netstack_config s_eth_netif_config = { - .init_fn = ethernetif_init, - .input_fn = ethernetif_input + .lwip = { + .init_fn = ethernetif_init, + .input_fn = ethernetif_input + } }; static const struct esp_netif_netstack_config s_wifi_netif_config_ap = { - .init_fn = wlanif_init_ap, - .input_fn = wlanif_input + .lwip = { + .init_fn = wlanif_init_ap, + .input_fn = wlanif_input + } + }; static const struct esp_netif_netstack_config s_wifi_netif_config_sta = { - .init_fn = wlanif_init_sta, - .input_fn = wlanif_input + .lwip = { + .init_fn = wlanif_init_sta, + .input_fn = wlanif_input + } +}; + +static const struct esp_netif_netstack_config s_netif_config_ppp = { + .lwip_ppp = { + .input_fn = esp_netif_lwip_ppp_input, + .ppp_events = { + .ppp_error_event_enabled = true, + .ppp_phase_event_enabled = false + } + } }; const esp_netif_netstack_config_t *_g_esp_netif_netstack_default_eth = &s_eth_netif_config; const esp_netif_netstack_config_t *_g_esp_netif_netstack_default_wifi_sta = &s_wifi_netif_config_sta; -const esp_netif_netstack_config_t *_g_esp_netif_netstack_default_wifi_ap = &s_wifi_netif_config_ap; \ No newline at end of file +const esp_netif_netstack_config_t *_g_esp_netif_netstack_default_wifi_ap = &s_wifi_netif_config_ap; +const esp_netif_netstack_config_t *_g_esp_netif_netstack_default_ppp = &s_netif_config_ppp; \ No newline at end of file diff --git a/components/esp_netif/lwip/esp_netif_lwip_internal.h b/components/esp_netif/lwip/esp_netif_lwip_internal.h index 56ac90d68b..853b0629e8 100644 --- a/components/esp_netif/lwip/esp_netif_lwip_internal.h +++ b/components/esp_netif/lwip/esp_netif_lwip_internal.h @@ -15,12 +15,25 @@ #pragma once #include "esp_netif.h" +#include "esp_netif_ppp.h" #include "lwip/netif.h" +struct esp_netif_netstack_lwip_vanilla_config { + err_t (*init_fn)(struct netif*); + void (*input_fn)(void *netif, void *buffer, size_t len, void *eb); +}; + +struct esp_netif_netstack_lwip_ppp_config { + void (*input_fn)(void *netif, void *buffer, size_t len, void *eb); + esp_netif_ppp_config_t ppp_events; +}; + // LWIP netif specific network stack configuration struct esp_netif_netstack_config { - err_t (*init_fn)(struct netif*); - void (*input_fn)(struct netif *netif, void *buffer, size_t len, void *eb); + union { + struct esp_netif_netstack_lwip_vanilla_config lwip; + struct esp_netif_netstack_lwip_ppp_config lwip_ppp; + }; }; struct esp_netif_api_msg_s; @@ -45,3 +58,44 @@ typedef struct esp_netif_ip_lost_timer_s { bool timer_running; } esp_netif_ip_lost_timer_t; +// Forward declare the ppp context +typedef struct lwip_ppp_ctx lwip_ppp_ctx_t; + +/** + * @brief Main esp-netif container with interface related information + */ +struct esp_netif_obj { + // default interface addresses + uint8_t mac[NETIF_MAX_HWADDR_LEN]; + esp_netif_ip_info_t* ip_info; + esp_netif_ip_info_t* ip_info_old; + + // lwip netif related + struct netif *lwip_netif; + lwip_ppp_ctx_t *lwip_ppp_ctx; + err_t (*lwip_init_fn)(struct netif*); + void (*lwip_input_fn)(void *input_netif_handle, void *buffer, size_t len, void *eb); + void * netif_handle; // netif impl context (either vanilla lwip-netif or ppp_pcb) + bool is_ppp_netif; + + // io driver related + void* driver_handle; + esp_err_t (*driver_transmit)(void *h, void *buffer, size_t len); + void (*driver_free_rx_buffer)(void *h, void* buffer); + + // dhcp related + esp_netif_dhcp_status_t dhcpc_status; + esp_netif_dhcp_status_t dhcps_status; + bool timer_running; + + // event translation + ip_event_t get_ip_event; + ip_event_t lost_ip_event; + + // misc flags, types, keys, priority + esp_netif_flags_t flags; + char * hostname; + char * if_key; + char * if_desc; + int route_prio; +}; diff --git a/components/lwip/port/esp32/include/netif/ethernetif.h b/components/lwip/port/esp32/include/netif/ethernetif.h index 9abd1e4ee9..d5d23b3d39 100644 --- a/components/lwip/port/esp32/include/netif/ethernetif.h +++ b/components/lwip/port/esp32/include/netif/ethernetif.h @@ -24,7 +24,7 @@ extern "C" { err_t ethernetif_init(struct netif *netif); -void ethernetif_input(struct netif *netif, void *buffer, size_t len, void *eb); +void ethernetif_input(void *h, void *buffer, size_t len, void *eb); void netif_reg_addr_change_cb(void* cb); diff --git a/components/lwip/port/esp32/include/netif/wlanif.h b/components/lwip/port/esp32/include/netif/wlanif.h index d3e39e2ab2..4535b6cb51 100644 --- a/components/lwip/port/esp32/include/netif/wlanif.h +++ b/components/lwip/port/esp32/include/netif/wlanif.h @@ -27,7 +27,7 @@ extern "C" { err_t wlanif_init_ap(struct netif *netif); err_t wlanif_init_sta(struct netif *netif); -void wlanif_input(struct netif *netif, void *buffer, size_t len, void* eb); +void wlanif_input(void *netif, void *buffer, size_t len, void* eb); err_t wlanif_init(struct netif *netif); wifi_interface_t wifi_get_interface(void *dev); diff --git a/components/lwip/port/esp32/netif/ethernetif.c b/components/lwip/port/esp32/netif/ethernetif.c index eac9823809..3c75bd1ebe 100644 --- a/components/lwip/port/esp32/netif/ethernetif.c +++ b/components/lwip/port/esp32/netif/ethernetif.c @@ -151,8 +151,9 @@ static err_t ethernet_low_level_output(struct netif *netif, struct pbuf *p) * @param buffer ethernet buffer * @param len length of buffer */ -void ethernetif_input(struct netif *netif, void *buffer, size_t len, void *eb) +void ethernetif_input(void *h, void *buffer, size_t len, void *eb) { + struct netif *netif = h; struct pbuf *p; if (buffer == NULL || !netif_is_up(netif)) { diff --git a/components/lwip/port/esp32/netif/wlanif.c b/components/lwip/port/esp32/netif/wlanif.c index 4b5c839d1c..e1d0bed6b9 100644 --- a/components/lwip/port/esp32/netif/wlanif.c +++ b/components/lwip/port/esp32/netif/wlanif.c @@ -154,8 +154,9 @@ low_level_output(struct netif *netif, struct pbuf *p) * @param netif the lwip network interface structure for this ethernetif */ void ESP_IRAM_ATTR -wlanif_input(struct netif *netif, void *buffer, size_t len, void* eb) +wlanif_input(void *h, void *buffer, size_t len, void* eb) { + struct netif * netif = h; esp_netif_t *esp_netif = esp_netif_get_handle_from_netif_impl(netif); struct pbuf *p;