From ec9f245dd35d3e8e7b19a8dec5e05e003dc21f39 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Tue, 20 Aug 2019 14:40:34 +0200 Subject: [PATCH 01/31] examples: removed ip4addr_ntoa and used prefered IP2STR for displaying IP addresses --- components/driver/test/esp32/test_adc2.c | 2 +- components/esp_wifi/test/esp32/test_wifi.c | 3 +-- components/mdns/test/test_mdns.c | 4 ++-- examples/mesh/internal_communication/main/mesh_main.c | 3 ++- examples/mesh/manual_networking/main/mesh_main.c | 2 +- examples/protocols/esp_local_ctrl/main/app_main.c | 3 +-- examples/provisioning/ble_prov/main/app_main.c | 3 +-- examples/provisioning/console_prov/main/app_main.c | 3 +-- examples/provisioning/custom_config/main/app_main.c | 3 +-- examples/provisioning/manager/main/app_main.c | 2 +- examples/provisioning/softap_prov/main/app_main.c | 3 +-- examples/wifi/fast_scan/main/fast_scan.c | 2 +- .../wifi/getting_started/station/main/station_example_main.c | 3 +-- examples/wifi/power_save/main/power_save.c | 2 +- examples/wifi/wps/main/wps.c | 2 +- 15 files changed, 17 insertions(+), 23 deletions(-) diff --git a/components/driver/test/esp32/test_adc2.c b/components/driver/test/esp32/test_adc2.c index 1438ae4d5c..fa45232280 100644 --- a/components/driver/test/esp32/test_adc2.c +++ b/components/driver/test/esp32/test_adc2.c @@ -46,7 +46,7 @@ static void ip_event_handler(void* arg, esp_event_base_t event_base, case IP_EVENT_STA_GOT_IP: event = (ip_event_got_ip_t*)event_data; ESP_LOGI(TAG, "IP_EVENT_STA_GOT_IP"); - ESP_LOGI(TAG, "got ip:%s\n", ip4addr_ntoa(&event->ip_info.ip)); + ESP_LOGI(TAG, "got ip:" IPSTR "\n", IP2STR(&event->ip_info.ip)); break; default: break; diff --git a/components/esp_wifi/test/esp32/test_wifi.c b/components/esp_wifi/test/esp32/test_wifi.c index 89b94ac5cb..0d1385f6bc 100644 --- a/components/esp_wifi/test/esp32/test_wifi.c +++ b/components/esp_wifi/test/esp32/test_wifi.c @@ -64,8 +64,7 @@ static void ip_event_handler(void* arg, esp_event_base_t event_base, case IP_EVENT_STA_GOT_IP: event = (ip_event_got_ip_t*)event_data; ESP_LOGI(TAG, "IP_EVENT_STA_GOT_IP"); - ESP_LOGI(TAG, "got ip:%s\n", - ip4addr_ntoa(&event->ip_info.ip)); + ESP_LOGI(TAG, "got ip:" IPSTR "\n", IP2STR(&event->ip_info.ip)); if (wifi_events) { xEventGroupSetBits(wifi_events, GOT_IP_EVENT); } diff --git a/components/mdns/test/test_mdns.c b/components/mdns/test/test_mdns.c index 6ae5e835ff..9e1e50dc40 100644 --- a/components/mdns/test/test_mdns.c +++ b/components/mdns/test/test_mdns.c @@ -73,8 +73,8 @@ TEST_CASE("mdns api return expected err-code and do not leak memory", "[mdns][le TEST_CASE("mdns query api return expected err-code and do not leak memory", "[leaks=64]") { mdns_result_t * results = NULL; - ip6_addr_t addr6; - ip4_addr_t addr4; + esp_ip6_addr_t addr6; + esp_ip4_addr_t addr4; test_case_uses_tcpip(); TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_create_default()); diff --git a/examples/mesh/internal_communication/main/mesh_main.c b/examples/mesh/internal_communication/main/mesh_main.c index 5b1c7d7e75..0bbff5165e 100644 --- a/examples/mesh/internal_communication/main/mesh_main.c +++ b/examples/mesh/internal_communication/main/mesh_main.c @@ -367,7 +367,8 @@ void ip_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data) { ip_event_got_ip_t *event = (ip_event_got_ip_t *) event_data; - ESP_LOGI(MESH_TAG, "IP:%s", ip4addr_ntoa(&event->ip_info.ip)); + ESP_LOGI(MESH_TAG, "IP:" IPSTR, IP2STR(&event->ip_info.ip)); + } void app_main(void) diff --git a/examples/mesh/manual_networking/main/mesh_main.c b/examples/mesh/manual_networking/main/mesh_main.c index 99ccfbe7f1..4135a72353 100644 --- a/examples/mesh/manual_networking/main/mesh_main.c +++ b/examples/mesh/manual_networking/main/mesh_main.c @@ -292,7 +292,7 @@ void ip_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data) { ip_event_got_ip_t *event = (ip_event_got_ip_t *) event_data; - ESP_LOGI(MESH_TAG, "IP:%s", ip4addr_ntoa(&event->ip_info.ip)); + ESP_LOGI(MESH_TAG, "IP:" IPSTR, IP2STR(&event->ip_info.ip)); } void app_main(void) diff --git a/examples/protocols/esp_local_ctrl/main/app_main.c b/examples/protocols/esp_local_ctrl/main/app_main.c index 8282b675b2..65651cc667 100644 --- a/examples/protocols/esp_local_ctrl/main/app_main.c +++ b/examples/protocols/esp_local_ctrl/main/app_main.c @@ -55,8 +55,7 @@ static void event_handler(void* arg, esp_event_base_t event_base, ESP_LOGI(TAG,"connect to the AP fail"); } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) { ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data; - ESP_LOGI(TAG, "got ip:%s", - ip4addr_ntoa(&event->ip_info.ip)); + ESP_LOGI(TAG, "got ip:" IPSTR, IP2STR(&event->ip_info.ip)); s_retry_num = 0; xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT); } diff --git a/examples/provisioning/ble_prov/main/app_main.c b/examples/provisioning/ble_prov/main/app_main.c index 1b96f5d125..04361574b1 100644 --- a/examples/provisioning/ble_prov/main/app_main.c +++ b/examples/provisioning/ble_prov/main/app_main.c @@ -69,8 +69,7 @@ static void event_handler(void* arg, esp_event_base_t event_base, } } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) { ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data; - ESP_LOGI(TAG, "got ip:%s", - ip4addr_ntoa(&event->ip_info.ip)); + ESP_LOGI(TAG, "got ip:" IPSTR, IP2STR(&event->ip_info.ip)); s_retry_num_ap_not_found = 0; s_retry_num_ap_auth_fail = 0; } diff --git a/examples/provisioning/console_prov/main/app_main.c b/examples/provisioning/console_prov/main/app_main.c index 6d5ef38836..366a0a49ab 100644 --- a/examples/provisioning/console_prov/main/app_main.c +++ b/examples/provisioning/console_prov/main/app_main.c @@ -41,8 +41,7 @@ static void event_handler(void* arg, esp_event_base_t event_base, ESP_LOGI(TAG,"connect to the AP fail"); } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) { ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data; - ESP_LOGI(TAG, "got ip:%s", - ip4addr_ntoa(&event->ip_info.ip)); + ESP_LOGI(TAG, "got ip:" IPSTR, IP2STR(&event->ip_info.ip)); s_retry_num = 0; } } diff --git a/examples/provisioning/custom_config/main/app_main.c b/examples/provisioning/custom_config/main/app_main.c index 072e7b5aaa..3eb53365f4 100644 --- a/examples/provisioning/custom_config/main/app_main.c +++ b/examples/provisioning/custom_config/main/app_main.c @@ -41,8 +41,7 @@ static void event_handler(void* arg, esp_event_base_t event_base, ESP_LOGI(TAG,"connect to the AP fail"); } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) { ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data; - ESP_LOGI(TAG, "got ip:%s", - ip4addr_ntoa(&event->ip_info.ip)); + ESP_LOGI(TAG, "got ip: " IPSTR, IP2STR(&event->ip_info.ip)); s_retry_num = 0; } } diff --git a/examples/provisioning/manager/main/app_main.c b/examples/provisioning/manager/main/app_main.c index 065b6f8fcf..799b0c634b 100644 --- a/examples/provisioning/manager/main/app_main.c +++ b/examples/provisioning/manager/main/app_main.c @@ -68,7 +68,7 @@ static void event_handler(void* arg, esp_event_base_t event_base, esp_wifi_connect(); } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) { ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data; - ESP_LOGI(TAG, "Connected with IP Address:%s", ip4addr_ntoa(&event->ip_info.ip)); + ESP_LOGI(TAG, "Connected with IP Address:" IPSTR, IP2STR(&event->ip_info.ip)); /* Signal main application to continue execution */ xEventGroupSetBits(wifi_event_group, WIFI_CONNECTED_EVENT); } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) { diff --git a/examples/provisioning/softap_prov/main/app_main.c b/examples/provisioning/softap_prov/main/app_main.c index 8ee7ae679c..676c85925d 100644 --- a/examples/provisioning/softap_prov/main/app_main.c +++ b/examples/provisioning/softap_prov/main/app_main.c @@ -41,8 +41,7 @@ static void event_handler(void* arg, esp_event_base_t event_base, ESP_LOGI(TAG,"connect to the AP fail"); } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) { ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data; - ESP_LOGI(TAG, "got ip:%s", - ip4addr_ntoa(&event->ip_info.ip)); + ESP_LOGI(TAG, "got ip:" IPSTR, IP2STR(&event->ip_info.ip)); s_retry_num = 0; } } diff --git a/examples/wifi/fast_scan/main/fast_scan.c b/examples/wifi/fast_scan/main/fast_scan.c index bc724126df..43df223e1c 100644 --- a/examples/wifi/fast_scan/main/fast_scan.c +++ b/examples/wifi/fast_scan/main/fast_scan.c @@ -77,7 +77,7 @@ static void event_handler(void* arg, esp_event_base_t event_base, esp_wifi_connect(); } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) { ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data; - ESP_LOGI(TAG, "got ip: %s", ip4addr_ntoa(&event->ip_info.ip)); + ESP_LOGI(TAG, "got ip:" IPSTR, IP2STR(&event->ip_info.ip)); } } diff --git a/examples/wifi/getting_started/station/main/station_example_main.c b/examples/wifi/getting_started/station/main/station_example_main.c index 869b614ce5..ce349e8395 100644 --- a/examples/wifi/getting_started/station/main/station_example_main.c +++ b/examples/wifi/getting_started/station/main/station_example_main.c @@ -54,8 +54,7 @@ static void event_handler(void* arg, esp_event_base_t event_base, ESP_LOGI(TAG,"connect to the AP fail"); } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) { ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data; - ESP_LOGI(TAG, "got ip:%s", - ip4addr_ntoa(&event->ip_info.ip)); + ESP_LOGI(TAG, "got ip:" IPSTR, IP2STR(&event->ip_info.ip)); s_retry_num = 0; xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT); } diff --git a/examples/wifi/power_save/main/power_save.c b/examples/wifi/power_save/main/power_save.c index 6eca7f48e2..21b7e7b51b 100644 --- a/examples/wifi/power_save/main/power_save.c +++ b/examples/wifi/power_save/main/power_save.c @@ -48,7 +48,7 @@ static void event_handler(void* arg, esp_event_base_t event_base, esp_wifi_connect(); } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) { ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data; - ESP_LOGI(TAG, "got ip: %s", ip4addr_ntoa(&event->ip_info.ip)); + ESP_LOGI(TAG, "got ip: " IPSTR, IP2STR(&event->ip_info.ip)); } } diff --git a/examples/wifi/wps/main/wps.c b/examples/wifi/wps/main/wps.c index 4e1af5d03d..640ad952c6 100644 --- a/examples/wifi/wps/main/wps.c +++ b/examples/wifi/wps/main/wps.c @@ -91,7 +91,7 @@ static void got_ip_event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) { ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data; - ESP_LOGI(TAG, "got ip: %s", ip4addr_ntoa(&event->ip_info.ip)); + ESP_LOGI(TAG, "got ip: " IPSTR, IP2STR(&event->ip_info.ip)); } /*init wifi as sta and start wps*/ From ffe043b1a81a0f9e1cc2cfa8873e21318ec89143 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Fri, 28 Jun 2019 16:47:34 +0200 Subject: [PATCH 02/31] esp_netif: Introduction of esp-netif component as a replacement of tcpip_adpter - provides object oriented access to network intefaces - not limited to default netifs - more generic abstraction to network input output functions - event handler registration removed from component responsibility - backward compatibility layer for legacy tcpip_apapter APIs Closes IDF-39 --- components/esp_eth/CMakeLists.txt | 2 +- components/esp_eth/include/esp_eth.h | 28 +- components/esp_eth/src/esp_eth.c | 105 +- components/esp_eth/test/test_emac.c | 2 +- components/esp_event/CMakeLists.txt | 2 +- components/esp_event/event_send_compat.inc | 14 +- .../esp_event/include/esp_event_legacy.h | 2 +- components/esp_netif/CMakeLists.txt | 8 + .../{tcpip_adapter => esp_netif}/Kconfig | 13 +- components/esp_netif/README.md | 81 + components/esp_netif/component.mk | 6 + components/esp_netif/esp_netif_defaults.c | 95 ++ components/esp_netif/esp_netif_handlers.c | 125 ++ components/esp_netif/esp_netif_objects.c | 102 ++ components/esp_netif/include/esp_netif.h | 641 ++++++++ .../esp_netif/include/esp_netif_defaults.h | 114 ++ .../esp_netif/include/esp_netif_ip_addr.h | 88 + .../esp_netif/include/esp_netif_net_stack.h | 41 + .../esp_netif/include/esp_netif_types.h | 223 +++ components/esp_netif/lwip/esp_netif_lwip.c | 1444 +++++++++++++++++ .../esp_netif/lwip/esp_netif_lwip_defaults.c | 44 + .../esp_netif/lwip/esp_netif_lwip_internal.h | 70 + .../private_include/esp_netif_private.h | 114 ++ components/esp_netif/test/CMakeLists.txt | 5 + components/esp_netif/test/component.mk | 5 + components/esp_netif/test/test_esp_netif.c | 56 + components/esp_wifi/CMakeLists.txt | 3 +- components/esp_wifi/include/esp_mesh.h | 1 + .../esp_wifi/include/esp_wifi_default.h | 28 + components/esp_wifi/src/wifi_default.c | 244 +++ components/esp_wifi/src/wifi_init.c | 4 + components/lwip/CMakeLists.txt | 3 +- components/lwip/apps/dhcpserver/dhcpserver.c | 38 +- components/lwip/lwip | 2 +- components/lwip/port/esp32/include/lwipopts.h | 2 +- .../port/esp32/include/netif/dhcp_state.h | 2 +- .../port/esp32/include/netif/ethernetif.h | 2 +- .../lwip/port/esp32/include/netif/nettestif.h | 33 - .../lwip/port/esp32/include/netif/wlanif.h | 5 +- components/lwip/port/esp32/netif/dhcp_state.c | 40 +- components/lwip/port/esp32/netif/ethernetif.c | 27 +- components/lwip/port/esp32/netif/nettestif.c | 105 -- components/lwip/port/esp32/netif/wlanif.c | 49 +- components/mdns/mdns.c | 2 +- components/tcpip_adapter/CMakeLists.txt | 4 +- components/tcpip_adapter/event_handlers.c | 312 ---- .../tcpip_adapter/include/tcpip_adapter.h | 770 +-------- .../tcpip_adapter_compat.h | 40 + .../include/tcpip_adapter_internal.h | 66 - components/tcpip_adapter/sdkconfig.rename | 5 - .../tcpip_adapter/tcpip_adapter_compat.c | 274 ++++ components/tcpip_adapter/tcpip_adapter_lwip.c | 1305 --------------- components/wifi_provisioning/src/handlers.c | 10 +- .../system/network_tests/main/CMakeLists.txt | 2 +- .../network_tests/main/lwip_test_netif.c | 125 ++ .../system/network_tests/main/net_suite.c | 40 +- 56 files changed, 4344 insertions(+), 2629 deletions(-) create mode 100644 components/esp_netif/CMakeLists.txt rename components/{tcpip_adapter => esp_netif}/Kconfig (67%) create mode 100644 components/esp_netif/README.md create mode 100644 components/esp_netif/component.mk create mode 100644 components/esp_netif/esp_netif_defaults.c create mode 100644 components/esp_netif/esp_netif_handlers.c create mode 100644 components/esp_netif/esp_netif_objects.c create mode 100644 components/esp_netif/include/esp_netif.h create mode 100644 components/esp_netif/include/esp_netif_defaults.h create mode 100644 components/esp_netif/include/esp_netif_ip_addr.h create mode 100644 components/esp_netif/include/esp_netif_net_stack.h create mode 100644 components/esp_netif/include/esp_netif_types.h create mode 100644 components/esp_netif/lwip/esp_netif_lwip.c create mode 100644 components/esp_netif/lwip/esp_netif_lwip_defaults.c create mode 100644 components/esp_netif/lwip/esp_netif_lwip_internal.h create mode 100644 components/esp_netif/private_include/esp_netif_private.h create mode 100644 components/esp_netif/test/CMakeLists.txt create mode 100644 components/esp_netif/test/component.mk create mode 100644 components/esp_netif/test/test_esp_netif.c create mode 100644 components/esp_wifi/src/wifi_default.c delete mode 100644 components/lwip/port/esp32/include/netif/nettestif.h delete mode 100644 components/lwip/port/esp32/netif/nettestif.c delete mode 100644 components/tcpip_adapter/event_handlers.c create mode 100644 components/tcpip_adapter/include/tcpip_adapter_compatible/tcpip_adapter_compat.h delete mode 100644 components/tcpip_adapter/include/tcpip_adapter_internal.h delete mode 100644 components/tcpip_adapter/sdkconfig.rename create mode 100644 components/tcpip_adapter/tcpip_adapter_compat.c delete mode 100644 components/tcpip_adapter/tcpip_adapter_lwip.c create mode 100644 examples/system/network_tests/main/lwip_test_netif.c diff --git a/components/esp_eth/CMakeLists.txt b/components/esp_eth/CMakeLists.txt index 73f559e5b7..894966d34e 100644 --- a/components/esp_eth/CMakeLists.txt +++ b/components/esp_eth/CMakeLists.txt @@ -30,5 +30,5 @@ idf_component_register(SRCS "${srcs}" INCLUDE_DIRS ${include} LDFRAGMENTS ${linker} REQUIRES "esp_event" - PRIV_REQUIRES "tcpip_adapter" "driver" "log") + PRIV_REQUIRES "esp_netif" "driver" "log") diff --git a/components/esp_eth/include/esp_eth.h b/components/esp_eth/include/esp_eth.h index 0d40d09562..39393e8235 100644 --- a/components/esp_eth/include/esp_eth.h +++ b/components/esp_eth/include/esp_eth.h @@ -85,6 +85,7 @@ typedef struct { * - ESP_FAIL: error occurred when processing extra lowlevel deinitialization */ esp_err_t (*on_lowlevel_deinit_done)(esp_eth_handle_t eth_handle); + } esp_eth_config_t; /** @@ -101,6 +102,15 @@ typedef struct { .on_lowlevel_deinit_done = NULL, \ } +/** +* @brief Default esp-netif driver related configuration +* +*/ +#define ESP_NETIF_DRIVER_DEFAULT_ETH _g_eth_driver_ifconfig + +struct esp_netif_driver_ifconfig; +extern const struct esp_netif_driver_ifconfig *_g_eth_driver_ifconfig; + /** * @brief Install Ethernet driver * @@ -139,7 +149,7 @@ esp_err_t esp_eth_driver_uninstall(esp_eth_handle_t hdl); * - ESP_ERR_INVALID_ARG: transmit frame buffer failed because of some invalid argument * - ESP_FAIL: transmit frame buffer failed because some other error occurred */ -esp_err_t esp_eth_transmit(esp_eth_handle_t hdl, uint8_t *buf, uint32_t length); +esp_err_t esp_eth_transmit(void* hdl, void *buf, uint32_t length); /** * @brief General Receive @@ -174,6 +184,22 @@ esp_err_t esp_eth_receive(esp_eth_handle_t hdl, uint8_t *buf, uint32_t *length); */ esp_err_t esp_eth_ioctl(esp_eth_handle_t hdl, esp_eth_io_cmd_t cmd, void *data); +/** + * @brief Register default ethernet handlers + * + * @param[in] esp_netif esp network interface handle created for this driver + * (note: appropriate ethernet handle not yet properly initialized when setting up + * default handlers) + */ +esp_err_t esp_eth_set_default_handlers(void* esp_netif); + +/** + * @brief Unregister default ethernet handlers + * + * @param[in] esp_netif esp network interface handle created for this driver + */ +esp_err_t esp_eth_clear_default_handlers(void* esp_netif); + #ifdef __cplusplus } #endif diff --git a/components/esp_eth/src/esp_eth.c b/components/esp_eth/src/esp_eth.c index adef9096e3..fcd5991f53 100644 --- a/components/esp_eth/src/esp_eth.c +++ b/components/esp_eth/src/esp_eth.c @@ -18,6 +18,8 @@ #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "freertos/timers.h" +#include "esp_netif.h" +#include "tcpip_adapter_compatible/tcpip_adapter_compat.h" static const char *TAG = "esp_eth"; #define ETH_CHECK(a, str, goto_tag, ret_value, ...) \ @@ -33,6 +35,15 @@ static const char *TAG = "esp_eth"; ESP_EVENT_DEFINE_BASE(ETH_EVENT); +// Default driver interface config, at the init moment doesn't include driver handle, as it is created +// in starup phase, netif is updated in event handler on eth_start event with valid ethernet driver handle +static const esp_netif_driver_ifconfig_t _s_eth_driver_ifconfig = { + .handle = NULL, + .transmit = esp_eth_transmit, +}; + +const esp_netif_driver_ifconfig_t *_g_eth_driver_ifconfig = &_s_eth_driver_ifconfig; + /** * @brief The Ethernet driver mainly consists of PHY, MAC and * the mediator who will handle the request/response from/to MAC, PHY and Users. @@ -44,6 +55,7 @@ ESP_EVENT_DEFINE_BASE(ETH_EVENT); * In the callback, user can do any low level operations (e.g. enable/disable crystal clock). */ typedef struct { + esp_netif_driver_base_t base; esp_eth_mediator_t mediator; esp_eth_phy_t *phy; esp_eth_mac_t *mac; @@ -83,7 +95,7 @@ static esp_err_t eth_stack_input(esp_eth_mediator_t *eth, uint8_t *buffer, uint3 { esp_eth_driver_t *eth_driver = __containerof(eth, esp_eth_driver_t, mediator); if (!eth_driver->stack_input) { - return tcpip_adapter_eth_input(buffer, length, NULL); + return esp_netif_receive(eth_driver->base.netif, buffer, length, NULL); } else { return eth_driver->stack_input((esp_eth_handle_t)eth_driver, buffer, length); } @@ -154,6 +166,37 @@ static void eth_check_link_timer_cb(TimerHandle_t xTimer) // It's helpful for us to support multiple Ethernet port on ESP32. ////////////////////////////////////////////////////////////////////////////////////////////// +static esp_err_t esp_eth_driver_start(esp_netif_t * esp_netif, void * args) +{ + esp_err_t ret = ESP_OK; + esp_eth_driver_t *eth_driver = args; + eth_driver->base.netif = esp_netif; + // Update esp-netif with already created ethernet handle + + esp_netif_driver_ifconfig_t driver_ifconfig = { + .handle = eth_driver, + .transmit = esp_eth_transmit, + }; + esp_netif_config_t cfg = { + .driver = &driver_ifconfig, + + }; + ESP_ERROR_CHECK(esp_netif_configure(esp_netif, &cfg)); + uint8_t eth_mac[6]; + esp_eth_ioctl(eth_driver, ETH_CMD_G_MAC_ADDR, eth_mac); + ESP_LOGI(TAG, "%x %x %x %x %x %x", eth_mac[0], eth_mac[1], eth_mac[2], eth_mac[3], eth_mac[4], eth_mac[5]); + + esp_netif_set_mac(esp_netif, eth_mac); + ESP_LOGI(TAG, "ETH netif started"); + + ETH_CHECK(esp_event_post(ETH_EVENT, ETHERNET_EVENT_START, ð_driver, sizeof(eth_driver), 0) == ESP_OK, + "send ETHERNET_EVENT_START event failed", err_event, ESP_FAIL); + return ret; +err_event: + esp_eth_driver_uninstall(eth_driver); + return ret; +} + esp_err_t esp_eth_driver_install(const esp_eth_config_t *config, esp_eth_handle_t *out_hdl) { esp_err_t ret = ESP_OK; @@ -184,12 +227,10 @@ esp_err_t esp_eth_driver_install(const esp_eth_config_t *config, esp_eth_handle_ eth_driver, eth_check_link_timer_cb); ETH_CHECK(eth_driver->check_link_timer, "create eth_link_timer failed", err_create_timer, ESP_FAIL); ETH_CHECK(xTimerStart(eth_driver->check_link_timer, 0) == pdPASS, "start eth_link_timer failed", err_start_timer, ESP_FAIL); - ETH_CHECK(esp_event_post(ETH_EVENT, ETHERNET_EVENT_START, ð_driver, sizeof(eth_driver), 0) == ESP_OK, - "send ETHERNET_EVENT_START event failed", err_event, ESP_FAIL); + eth_driver->base.post_attach = esp_eth_driver_start; *out_hdl = (esp_eth_handle_t)eth_driver; + tcpip_adapter_start_eth(eth_driver); return ESP_OK; -err_event: - xTimerStop(eth_driver->check_link_timer, 0); err_start_timer: xTimerDelete(eth_driver->check_link_timer, 0); err_create_timer: @@ -221,7 +262,7 @@ err: return ret; } -esp_err_t esp_eth_transmit(esp_eth_handle_t hdl, uint8_t *buf, uint32_t length) +esp_err_t esp_eth_transmit(void* hdl, void *buf, uint32_t length) { esp_err_t ret = ESP_OK; esp_eth_driver_t *eth_driver = (esp_eth_driver_t *)hdl; @@ -282,3 +323,55 @@ esp_err_t esp_eth_ioctl(esp_eth_handle_t hdl, esp_eth_io_cmd_t cmd, void *data) err: return ret; } + +esp_err_t esp_eth_clear_default_handlers(void* esp_netif) +{ + esp_err_t ret; + ETH_CHECK(esp_netif, "esp-netif handle can't be null", fail, ESP_ERR_INVALID_ARG); + + esp_event_handler_unregister(ETH_EVENT, ETHERNET_EVENT_START, esp_netif_action_start); + esp_event_handler_unregister(ETH_EVENT, ETHERNET_EVENT_STOP, esp_netif_action_stop); + esp_event_handler_unregister(ETH_EVENT, ETHERNET_EVENT_CONNECTED, esp_netif_action_connected); + esp_event_handler_unregister(ETH_EVENT, ETHERNET_EVENT_DISCONNECTED, esp_netif_action_disconnected); + esp_event_handler_unregister(IP_EVENT, IP_EVENT_ETH_GOT_IP, esp_netif_action_got_ip); + return ESP_OK; +fail: + return ret; +} + +esp_err_t esp_eth_set_default_handlers(void* esp_netif) +{ + esp_err_t ret; + ETH_CHECK(esp_netif, "esp-netif handle can't be null", fail, ESP_ERR_INVALID_ARG); + + ret = esp_event_handler_register(ETH_EVENT, ETHERNET_EVENT_START, esp_netif_action_start, esp_netif); + if (ret != ESP_OK) { + goto fail; + } + + ret = esp_event_handler_register(ETH_EVENT, ETHERNET_EVENT_STOP, esp_netif_action_stop, esp_netif); + if (ret != ESP_OK) { + goto fail; + } + + ret = esp_event_handler_register(ETH_EVENT, ETHERNET_EVENT_CONNECTED, esp_netif_action_connected, esp_netif); + if (ret != ESP_OK) { + goto fail; + } + + ret = esp_event_handler_register(ETH_EVENT, ETHERNET_EVENT_DISCONNECTED, esp_netif_action_disconnected, esp_netif); + if (ret != ESP_OK) { + goto fail; + } + + ret = esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, esp_netif_action_got_ip, esp_netif); + if (ret != ESP_OK) { + goto fail; + } + + return ESP_OK; + +fail: + esp_eth_clear_default_handlers(esp_netif); + return ret; +} diff --git a/components/esp_eth/test/test_emac.c b/components/esp_eth/test/test_emac.c index dc2e67128a..368494cbf7 100644 --- a/components/esp_eth/test/test_emac.c +++ b/components/esp_eth/test/test_emac.c @@ -62,7 +62,7 @@ static void got_ip_event_handler(void *arg, esp_event_base_t event_base, { EventGroupHandle_t eth_event_group = (EventGroupHandle_t)arg; ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data; - const tcpip_adapter_ip_info_t *ip_info = &event->ip_info; + const tcpip_adapter_ip_info_t *ip_info = (tcpip_adapter_ip_info_t *)&event->ip_info; ESP_LOGI(TAG, "Ethernet Got IP Address"); ESP_LOGI(TAG, "~~~~~~~~~~~"); diff --git a/components/esp_event/CMakeLists.txt b/components/esp_event/CMakeLists.txt index da463ff1fc..9c1d7da5f1 100644 --- a/components/esp_event/CMakeLists.txt +++ b/components/esp_event/CMakeLists.txt @@ -11,7 +11,7 @@ idf_component_register(SRCS "default_event_loop.c" "event_send.c" INCLUDE_DIRS "include" PRIV_INCLUDE_DIRS "private_include" - REQUIRES log tcpip_adapter + REQUIRES log esp_netif PRIV_REQUIRES ${priv_requires} LDFRAGMENTS linker.lf) diff --git a/components/esp_event/event_send_compat.inc b/components/esp_event/event_send_compat.inc index 721e8cd82b..7e1e59ef86 100644 --- a/components/esp_event/event_send_compat.inc +++ b/components/esp_event/event_send_compat.inc @@ -16,7 +16,7 @@ #include "esp_log.h" #include "esp_event_legacy.h" #include "esp_wifi_types.h" -#include "tcpip_adapter.h" +#include "esp_netif.h" #if CONFIG_ETH_ENABLED #include "esp_eth.h" #endif @@ -271,16 +271,8 @@ static void esp_system_event_debug(const system_event_t* event) break; } case SYSTEM_EVENT_GOT_IP6: { - const ip6_addr_t *addr = &event->event_info.got_ip6.ip6_info.ip; - ESP_LOGD(TAG, "SYSTEM_EVENT_AP_STA_GOT_IP6 address %04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x", - IP6_ADDR_BLOCK1(addr), - IP6_ADDR_BLOCK2(addr), - IP6_ADDR_BLOCK3(addr), - IP6_ADDR_BLOCK4(addr), - IP6_ADDR_BLOCK5(addr), - IP6_ADDR_BLOCK6(addr), - IP6_ADDR_BLOCK7(addr), - IP6_ADDR_BLOCK8(addr)); + const esp_ip6_addr_t *addr = &event->event_info.got_ip6.ip6_info.ip; + ESP_LOGD(TAG, "SYSTEM_EVENT_AP_STA_GOT_IP6 address %04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x", IPV62STR(*addr)); break; } #if CONFIG_IDF_TARGET_ESP32 diff --git a/components/esp_event/include/esp_event_legacy.h b/components/esp_event/include/esp_event_legacy.h index 139c81763d..00d402c435 100644 --- a/components/esp_event/include/esp_event_legacy.h +++ b/components/esp_event/include/esp_event_legacy.h @@ -19,7 +19,7 @@ #include "esp_err.h" #include "esp_wifi_types.h" -#include "tcpip_adapter.h" +#include "esp_netif.h" #ifdef __cplusplus extern "C" { diff --git a/components/esp_netif/CMakeLists.txt b/components/esp_netif/CMakeLists.txt new file mode 100644 index 0000000000..bdcdd80bb2 --- /dev/null +++ b/components/esp_netif/CMakeLists.txt @@ -0,0 +1,8 @@ +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_defaults.c" + INCLUDE_DIRS include + PRIV_INCLUDE_DIRS lwip private_include + REQUIRES lwip esp_eth tcpip_adapter) diff --git a/components/tcpip_adapter/Kconfig b/components/esp_netif/Kconfig similarity index 67% rename from components/tcpip_adapter/Kconfig rename to components/esp_netif/Kconfig index 6b409d38b7..fd693386f5 100644 --- a/components/tcpip_adapter/Kconfig +++ b/components/esp_netif/Kconfig @@ -1,6 +1,6 @@ -menu "TCP/IP Adapter" +menu "ESP NETIF Adapter" - config NETIF_IP_LOST_TIMER_INTERVAL + config ESP_NETIF_IP_LOST_TIMER_INTERVAL int "IP Address lost timer interval (seconds)" range 0 65535 default 120 @@ -13,7 +13,7 @@ menu "TCP/IP Adapter" the timer expires. The IP lost timer is stopped if the station get the IP again before the timer expires. - choice NETIF_USE_TCPIP_STACK_LIB + choice ESP_NETIF_USE_TCPIP_STACK_LIB prompt "TCP/IP Stack Library" default TCPIP_LWIP help @@ -24,4 +24,11 @@ menu "TCP/IP Adapter" lwIP is a small independent implementation of the TCP/IP protocol suite. endchoice + config ESP_NETIF_USE_TCPIP_ADAPTER_COMPATIBLE_LAYER + bool "Enable backward compatible tcpip_adapter interface" + default y + help + Backward compatible interface to tcpip_adapter is enabled by default to support + legacy tcpip stack initialisation code. Disable this option to use only esp-netif + interface. endmenu diff --git a/components/esp_netif/README.md b/components/esp_netif/README.md new file mode 100644 index 0000000000..4231a42721 --- /dev/null +++ b/components/esp_netif/README.md @@ -0,0 +1,81 @@ +# ESP-NETIF architecture + + | (A) USER CODE | + | | + .............| init settings events | + . +----------------------------------------+ + . . | * + . . V * + --------+ +===========================+ * +-----------------------+ + | | new/config get/set | * | | + | | |...*.....| init | + | |---------------------------| * | | + init | | |**** | | + start |********| event handler |*********| DHCP | + stop | | | | | + | |---------------------------| | | + | | | | NETIF | + +-----| | | +-----------------+ | + | glue|----<---| esp_netif_transmit |--<------| netif_output | | + | | | | | | | + | |---->---| esp_netif_receive |-->------| netif_input | | + | | | | + ----------------+ | + | |....<...| esp_netif_free_rx_buffer |...<.....| packet buffer | + +-----| | | | | + | | | | | + (B) | | | +-----------------------+ + --------+ +===========================+ + communication NETWORK STACK + DRIVER ESP-NETIF + + +## Components: + +### A) User code, boiler plate +Overall application interaction with communication media and network stack + + * initialization code + - create a new instance of ESP-NETIF + - configure the object with + 1) netif specific options (flags, behaviour, name) + 2) network stack options (netif init and input functions, not publicly available) + 3) IO driver specific options (transmit, tx_free functions, IO driver handle) + - setup event handlers + - use default handlers for common interfaces defined in IO drivers; or define a specific handlers + for customised behaviour/new interfaces + - register handlers for app related events (such as IP lost/acquired) + - interact with network interfaces using ESP-NETIF API + +### B) Communication driver, IO driver, media driver + * event handler + - define behaviour patterns of interaction with ESP-NETIF (example: ehternet link-up -> turn netif on) + * glue IO layer: adapt the input/output functions to use esp-netif transmit/input/free_rx + - install driver_transmit to appropriate ESP-NETIF object, so that outgoing packets from + network stack are passed to the IO driver + - calls esp_netif_receive to pass incoming data to network stack + +### C) ESP-NETIF, former tcpip_adapter +* init API (new, configure) +* IO API: for passing data between IO driver and network stack +* event/actiona API (esp-netif lifecycle management) + - building blocks for designing event handlers +* setters, getters +* network stack abstraction: enabling user interaction with TCP/IP stack + - netif up/down + - dhcp server, client + - dns api +* driver conversion utilities + +### D) Network stack: no public interaction with user code (wrtt interfaces) + + +## Data/event flow: + +* `........` Initialization line from user code to esp-netif and comm driver + +* `--<--->--` Data packets going from communication media to tcpip stack and back + +* `********` Events agregated in ESP-NETIP propagates to driver, user code and network stack + +* `|` User settings and runtime configuration + diff --git a/components/esp_netif/component.mk b/components/esp_netif/component.mk new file mode 100644 index 0000000000..c45836abad --- /dev/null +++ b/components/esp_netif/component.mk @@ -0,0 +1,6 @@ +# +# Component Makefile +# +COMPONENT_ADD_INCLUDEDIRS := include +COMPONENT_PRIV_INCLUDEDIRS := private_include lwip +COMPONENT_SRCDIRS := . lwip \ No newline at end of file diff --git a/components/esp_netif/esp_netif_defaults.c b/components/esp_netif/esp_netif_defaults.c new file mode 100644 index 0000000000..abbb76da47 --- /dev/null +++ b/components/esp_netif/esp_netif_defaults.c @@ -0,0 +1,95 @@ +// Copyright 2015-2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "esp_netif.h" +#include "esp_event.h" +#include "esp_wifi_default.h" +#include "esp_eth.h" + +// +// Purpose of this module is to provide +// - general esp-netif definitions of default objects for STA, AP, ETH +// - default init / create functions for basic default interfaces +// + +#define IP4TOUINT32(a,b,c,d) (((uint32_t)((a) & 0xffU) << 24) | \ + ((uint32_t)((b) & 0xffU) << 16) | \ + ((uint32_t)((c) & 0xffU) << 8) | \ + (uint32_t)((d) & 0xffU)) + +#define IP4TOADDR(a,b,c,d) esp_netif_htonl(IP4TOUINT32(a, b, c, d)) + + +// +// Default configuration of common interfaces, such as STA, AP, ETH +// +const esp_netif_inherent_config_t _g_esp_netif_inherent_sta_config = { + .flags = ESP_NETIF_DHCPC | ESP_NETIF_FLAG_GARP | ESP_NETIF_FLAG_EVENT_IP_MODIFIED, + .lost_ip_event = IP_EVENT_STA_LOST_IP, + .get_ip_event = IP_EVENT_STA_GOT_IP, + .if_key = "WIFI_STA_DEF", + .if_type = ESP_NETIF_TYPE_STA, + .route_prio = 100 +}; + +static const esp_netif_ip_info_t soft_ap_ip = { + .ip = { .addr = IP4TOADDR( 192, 168, 8, 1) }, + .gw = { .addr = IP4TOADDR( 192, 168, 8, 1) }, + .netmask = { .addr = IP4TOADDR( 255, 255, 255, 0) }, + +}; + +const esp_netif_inherent_config_t _g_esp_netif_inherent_ap_config = { + .flags = ESP_NETIF_DHCPS | ESP_NETIF_FLAG_AUTOUP, + .ip_info = (esp_netif_ip_info_t*)&soft_ap_ip, + .if_key = "WIFI_AP_DEF", + .if_type = ESP_NETIF_TYPE_AP, + .route_prio = 10 +}; + +const esp_netif_inherent_config_t _g_esp_netif_inherent_eth_config = { + .get_ip_event = IP_EVENT_ETH_GOT_IP, + .lost_ip_event = 0, + .flags = ESP_NETIF_DHCPC | ESP_NETIF_FLAG_GARP | ESP_NETIF_FLAG_EVENT_IP_MODIFIED, + .if_key = "ETH_DEF", + .if_type = ESP_NETIF_TYPE_ETH, + .route_prio = 50 +}; + +esp_netif_t* esp_netif_create_default_wifi_ap(void) +{ + esp_netif_config_t cfg = ESP_NETIF_DEFAULT_WIFI_AP(); + esp_netif_t *netif = esp_netif_new(&cfg); + assert(netif); + esp_wifi_set_default_wifi_ap_handlers(netif); + return netif; +} + +esp_netif_t* esp_netif_create_default_wifi_sta(void) +{ + esp_netif_config_t cfg = ESP_NETIF_DEFAULT_WIFI_STA(); + esp_netif_t *netif = esp_netif_new(&cfg); + assert(netif); + esp_wifi_set_default_wifi_sta_handlers(netif); + return netif; +} + +esp_netif_t* esp_netif_create_default_eth(void * eth_driver) +{ + esp_netif_config_t cfg = ESP_NETIF_DEFAULT_ETH(); + esp_netif_t* eth_netif = esp_netif_new(&cfg); + assert(eth_netif); + ESP_ERROR_CHECK(esp_netif_attach(eth_netif, eth_driver)); + return eth_netif; +} \ No newline at end of file diff --git a/components/esp_netif/esp_netif_handlers.c b/components/esp_netif/esp_netif_handlers.c new file mode 100644 index 0000000000..8aac2e79fa --- /dev/null +++ b/components/esp_netif/esp_netif_handlers.c @@ -0,0 +1,125 @@ +// Copyright 2018 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include "esp_netif.h" +#include "esp_event.h" +#include "esp_err.h" +#include "esp_log.h" +#include "esp_netif_private.h" + +// +// Purpose of this module is to define general event handler primitives +// enabling application code to them either directly or with minimal modification +// for example with a separate pre/post handler. +// This module has no dependency on a specific network stack (lwip) +// + +static const char *TAG = "esp_netif_handlers"; + +#define _STR(x) #x + +/** + * @brief This function converts interface type to string, which + * helps with backward compatibility of test infrastructure + * to check for standard message that specific interface (sta, ap, eth) + * obtained IP address + */ +static const char* get_netif_type(esp_netif_t* netif) { + const char* s_esp_netif_type_desc[] = { + _STR(ESP_NETIF_TYPE_UNKNOWN), + "sta", + "ap", + "eth", + _STR(ESP_NETIF_TYPE_OTHER) + }; + size_t type_nr = esp_netif_get_type(netif); + if (type_nr > sizeof(s_esp_netif_type_desc)/sizeof(s_esp_netif_type_desc[0])) { + type_nr = 0; + } + return s_esp_netif_type_desc[type_nr]; +} + + +void esp_netif_action_start(void *esp_netif, esp_event_base_t base, int32_t event_id, void *data) +{ + ESP_LOGD(TAG, "esp_netif action has started with netif%p from event_id=%d", esp_netif, event_id); + esp_netif_start(esp_netif); +} + +void esp_netif_action_stop(void *esp_netif, esp_event_base_t base, int32_t event_id, void *data) +{ + ESP_LOGD(TAG, "esp_netif action stopped with netif%p from event_id=%d", esp_netif, event_id); + esp_netif_stop(esp_netif); +} + +void esp_netif_action_connected(void *esp_netif, esp_event_base_t base, int32_t event_id, void *data) +{ + esp_netif_dhcp_status_t status; + + ESP_LOGD(TAG, "esp_netif action connected with netif%p from event_id=%d", esp_netif, event_id); + esp_netif_up(esp_netif); + + esp_netif_dhcpc_get_status(esp_netif, &status); + if (status == ESP_NETIF_DHCP_INIT) { + esp_netif_dhcpc_start(esp_netif); + } else if (status == ESP_NETIF_DHCP_STOPPED) { + // + esp_netif_ip_info_t ip; + esp_netif_ip_info_t old_ip; + + esp_netif_get_ip_info(esp_netif, &ip); + esp_netif_get_old_ip_info(esp_netif, &old_ip); + + if (esp_netif_is_valid_static_ip(&ip)) { + ip_event_got_ip_t evt = { + .esp_netif = esp_netif, + .if_index = -1, // to indicate ptr to if used + .ip_changed = false, + }; + + if (memcmp(&ip, &old_ip, sizeof(ip))) { + evt.ip_changed = true; + } + + memcpy(&evt.ip_info, &ip, sizeof(esp_netif_ip_info_t)); + esp_netif_set_old_ip_info(esp_netif, &ip); + + ESP_NETIF_CALL_CHECK("esp_event_send_internal in esp_netif_action_connected", + esp_event_send_internal(IP_EVENT, esp_netif_get_event_id(esp_netif, ESP_NETIF_IP_EVENT_GOT_IP) , + &evt, sizeof(evt), 0), ESP_OK); + ESP_LOGD(TAG, "static ip: ip changed=%d", evt.ip_changed); + } else { + ESP_LOGE(TAG, "invalid static ip"); + } + } +} + +void esp_netif_action_disconnected(void *esp_netif, esp_event_base_t base, int32_t event_id, void *data) +{ + ESP_LOGD(TAG, "esp_netif action connected with netif%p from event_id=%d", esp_netif, event_id); + esp_netif_down(esp_netif); + +} + +void esp_netif_action_got_ip(void *esp_netif, esp_event_base_t base, int32_t event_id, void *data) +{ + ESP_LOGD(TAG, "esp_netif action got_ip with netif%p from event_id=%d", esp_netif, event_id); + const ip_event_got_ip_t *event = (const ip_event_got_ip_t *) data; + ESP_LOGI(TAG, "%s ip: " IPSTR ", mask: " IPSTR ", gw: " IPSTR, get_netif_type(esp_netif), + IP2STR(&event->ip_info.ip), + IP2STR(&event->ip_info.netmask), + IP2STR(&event->ip_info.gw)); +} + diff --git a/components/esp_netif/esp_netif_objects.c b/components/esp_netif/esp_netif_objects.c new file mode 100644 index 0000000000..1eba1930e2 --- /dev/null +++ b/components/esp_netif/esp_netif_objects.c @@ -0,0 +1,102 @@ +// Copyright 2015-2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "esp_netif.h" +#include "sys/queue.h" +#include "esp_log.h" + +// +// Purpose of this module is to provide list of esp-netif structures +// - this module has no dependency on a specific network stack (lwip) +// + +static const char *TAG = "esp_netif_objects"; + +//#define ESP_NETIF_TYPE_DEFINE(id) const esp_netif_type_t id = #id + +typedef struct slist_netifs_s slist_netifs_t; +struct slist_netifs_s { + esp_netif_t *netif; + SLIST_ENTRY(slist_netifs_s) next; +}; + +SLIST_HEAD(slisthead, slist_netifs_s) s_head = { .slh_first = NULL, }; + +static size_t s_esp_netif_counter = 0; + +ESP_EVENT_DEFINE_BASE(IP_EVENT); + +//ESP_NETIF_TYPE_DEFINE(ESP_NETIF_TYPE_STA); +//ESP_NETIF_TYPE_DEFINE(ESP_NETIF_TYPE_AP); +//ESP_NETIF_TYPE_DEFINE(ESP_NETIF_TYPE_ETH); + + +// +// List manipulation functions +// +esp_err_t esp_netif_add_to_list(esp_netif_t *netif) +{ + struct slist_netifs_s *item = calloc(1, sizeof(struct slist_netifs_s)); + ESP_LOGD(TAG, "%s %p", __func__, netif); + if (item == NULL) { + return ESP_ERR_NO_MEM; + } + item->netif = netif; + + SLIST_INSERT_HEAD(&s_head, item, next); + ++s_esp_netif_counter; + ESP_LOGD(TAG, "%s netif added successfully (total netifs: %d)", __func__, s_esp_netif_counter); + return ESP_OK; +} + + +esp_err_t esp_netif_remove_from_list(esp_netif_t *netif) +{ + struct slist_netifs_s *item; + ESP_LOGV(TAG, "%s %p", __func__, netif); + SLIST_FOREACH(item, &s_head, next) { + if (item->netif == netif) { + SLIST_REMOVE(&s_head, item, slist_netifs_s, next); + assert(s_esp_netif_counter > 0); + --s_esp_netif_counter; + ESP_LOGD(TAG, "%s netif successfully removed (total netifs: %d)", __func__, s_esp_netif_counter); + free(item); + return ESP_OK; + } + } + return ESP_ERR_NOT_FOUND; +} + +size_t esp_netif_get_nr_of_ifs(void) +{ + return s_esp_netif_counter; +} + +esp_netif_t* esp_netif_next(esp_netif_t* netif) +{ + ESP_LOGV(TAG, "%s %p", __func__, netif); + struct slist_netifs_s *item; + if (netif == NULL) { + item = SLIST_FIRST(&s_head); + return (item == NULL) ? NULL : item->netif; + } else { + SLIST_FOREACH(item, &s_head, next) { + if (item->netif == netif) { + item = SLIST_NEXT(item, next); + return (item == NULL) ? NULL : item->netif; + } + } + } + return NULL; +} \ No newline at end of file diff --git a/components/esp_netif/include/esp_netif.h b/components/esp_netif/include/esp_netif.h new file mode 100644 index 0000000000..6cd8b13dde --- /dev/null +++ b/components/esp_netif/include/esp_netif.h @@ -0,0 +1,641 @@ +// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef _ESP_NETIF_H_ +#define _ESP_NETIF_H_ + +#include +#include "esp_wifi_types.h" +#include "sdkconfig.h" +#include "esp_netif_ip_addr.h" +#include "esp_netif_types.h" +#include "esp_netif_defaults.h" + +// +// 1) Initialization API: +// + +/** + * @brief Initialize the underlying TCP/IP stack + * + * @return + * - ESP_OK on success + * - ESP_FAIL if initializing failed + + * @note This function should be called exactly once from application code, when the application starts up. + */ +esp_err_t esp_netif_init(void); + +/** + * @brief Deinitialize the esp-netif component (and the underlying TCP/IP stack) + * + * Note: Deinitialization is not supported yet + * + * @return + * - ESP_ERR_INVALID_STATE if esp_netif not initialized + * - ESP_ERR_NOT_SUPPORTED otherwise + */ +esp_err_t esp_netif_deinit(void); + +/** + * @brief Creates an instance of new esp-netif object based on provided config + * + * @param[in] esp_netif_config pointer esp-netif configuration + * + * @return + * - pointer to esp-netif object on success + * - NULL otherwise + */ +esp_netif_t *esp_netif_new(const esp_netif_config_t *esp_netif_config); + +/** + * @brief Destroys the esp_netif object + * + * @param[in] pointer to the object to be deleted + */ +void esp_netif_destroy(esp_netif_t *esp_netif); + +/** + * @brief Configures the esp_netif object + * + * Note: if some of the configuration parameter is null, the corresponding config + * option is skipped. This enables calling this function multiple times to configure + * different parts related to for example network stack or io driver separately + * + * @param[inout] pointer to the object to be configured + * @param[in] esp_netif_config pointer esp-netif configuration + * @return + * - ESP_OK + * + */ +esp_err_t esp_netif_configure(esp_netif_t *esp_netif, + const esp_netif_config_t *esp_netif_config); + +esp_err_t esp_netif_attach(esp_netif_t *esp_netif, esp_netif_iodriver_handle driver_handle); + +// +// 2) Input - Output APIs +// + +/** + * @brief Outputs packets from the TCP/IP stack to the media to be transmitted + * + * This function gets called from network stack to output packets to IO driver. + * + * @note This function is installed automatically for default interfaces. + * Custom interface may need to install if the rx buffer passed as pointer and the IO driver has to + * deallocate the data in driver context + * + * @param[in] esp_netif Handle to esp-netif instance + * @param[in] void* buffer: rx buffer pointer + */ +esp_err_t esp_netif_transmit(esp_netif_t *esp_netif, void* data, size_t len); + +/** + * @brief Free the rx buffer allocated by the media driver + * + * This function gets called from network stack when the rx buffer to be freed in media driver context. + * + * @note This function is installed automatically for default interfaces. + * Custom interface may need to install if the rx buffer passed as pointer and the IO driver has to + * deallocate the data in driver context + * + * @param[in] esp_netif Handle to esp-netif instance + * @param[in] void* buffer: rx buffer pointer + */ +void esp_netif_free_rx_buffer(void *esp_netif, void* buffer); + +/** + * @brief Passes the raw packets from communication media to the appropriate TCP/IP stack + * + * This function is called from the configured (peripheral) driver layer. + * The data are then forwarded as frames to the TCP/IP stack. + * + * @note Installation happens automatically for default interfaces; custom IO drivers must install + * this function so that it is called on a reception of incoming packet + * + * @note Application code does not usually need to call this function directly. + * + * @param[in] esp_netif Handle to esp-netif instance + * @param[in] buffer Received data + * @param[in] len Length of the data frame + * @param[in] eb Pointer to internal buffer (used in Wi-Fi driver) + * + * @return + * - ESP_OK + */ +esp_err_t esp_netif_receive(esp_netif_t *esp_netif, void *buffer, size_t len, void *eb); + +// +// 3) ESP-NETIF lifecycle +// + + +/** + * @brief Default building block for network interface action upon IO driver start event + * Creates network interface, if AUTOUP enabled turns the interface on, + * if DHCPS enabled starts dhcp server + * + * @note This API can be directly used as event handler + * + * @param[in] esp_netif Handle to esp-netif instance + * @param base + * @param event_id + * @param data + */ +void esp_netif_action_start(void *esp_netif, esp_event_base_t base, int32_t event_id, void *data); + +/** + * @brief Default building block for network interface action upon IO driver stop event + * + * @note This API can be directly used as event handler + * + * @param[in] esp_netif Handle to esp-netif instance + * @param base + * @param event_id + * @param data + */ +void esp_netif_action_stop(void *esp_netif, esp_event_base_t base, int32_t event_id, void *data); + +/** + * @brief Default building block for network interface action upon IO driver connected event + * + * @note This API can be directly used as event handler + * + * @param[in] esp_netif Handle to esp-netif instance + * @param base + * @param event_id + * @param data + */ +void esp_netif_action_connected(void *esp_netif, esp_event_base_t base, int32_t event_id, void *data); + +/** + * @brief Default building block for network interface action upon IO driver disconnected event + * + * @note This API can be directly used as event handler + * + * @param[in] esp_netif Handle to esp-netif instance + * @param base + * @param event_id + * @param data + */ +void esp_netif_action_disconnected(void *esp_netif, esp_event_base_t base, int32_t event_id, void *data); + +/** + * @brief Default building block for network interface action upon network got IP event + * + * @note This API can be directly used as event handler + * + * @param[in] esp_netif Handle to esp-netif instance + * @param base + * @param event_id + * @param data + */ +void esp_netif_action_got_ip(void *esp_netif, esp_event_base_t base, int32_t event_id, void *data); + +// +/// 4) Configuration (getter - setters) +// +/** + * @brief Set the hostname of an interface + * + * @param[in] esp_netif Handle to esp-netif instance + * @param[in] hostname New hostname for the interface. Maximum length 32 bytes. + * + * @return + * - ESP_OK - success + * - ESP_ERR_ESP_NETIF_IF_NOT_READY - interface status error + * - ESP_ERR_ESP_NETIF_INVALID_PARAMS - parameter error + */ +esp_err_t esp_netif_set_hostname(esp_netif_t *esp_netif, const char *hostname); + +/** + * @brief Get interface hostname. + * + * @param[in] esp_netif Handle to esp-netif instance + * @param[out] hostname Returns a pointer to the hostname. May be NULL if no hostname is set. If set non-NULL, pointer remains valid (and string may change if the hostname changes). + * + * @return + * - ESP_OK - success + * - ESP_ERR_ESP_NETIF_IF_NOT_READY - interface status error + * - ESP_ERR_ESP_NETIF_INVALID_PARAMS - parameter error + */ +esp_err_t esp_netif_get_hostname(esp_netif_t *esp_netif, const char **hostname); + +/** + * @brief Test if supplied interface is up or down + * + * @param[in] esp_netif Handle to esp-netif instance + * + * @return + * - true - Interface is up + * - false - Interface is down + */ +bool esp_netif_is_netif_up(esp_netif_t *esp_netif); + +/** + * @brief Get interface's IP address information + * + * If the interface is up, IP information is read directly from the TCP/IP stack. + * If the interface is down, IP information is read from a copy kept in the ESP-NETIF instance + * + * @param[in] esp_netif Handle to esp-netif instance + * @param[out] ip_info If successful, IP information will be returned in this argument. + * + * @return + * - ESP_OK + * - ESP_ERR_ESP_NETIF_INVALID_PARAMS + */ +esp_err_t esp_netif_get_ip_info(esp_netif_t *esp_netif, esp_netif_ip_info_t *ip_info); + +/** + * @brief Get interface's old IP information + * + * Returns an "old" IP address previously stored for the interface when the valid IP changed. + * + * If the IP lost timer has expired (meaning the interface was down for longer than the configured interval) + * then the old IP information will be zero. + * + * @param[in] esp_netif Handle to esp-netif instance + * @param[out] ip_info If successful, IP information will be returned in this argument. + * + * @return + * - ESP_OK + * - ESP_ERR_ESP_NETIF_INVALID_PARAMS + */ +esp_err_t esp_netif_get_old_ip_info(esp_netif_t *esp_netif, esp_netif_ip_info_t *ip_info); + +/** + * @brief Set interface's IP address information + * + * This function is mainly used to set a static IP on an interface. + * + * If the interface is up, the new IP information is set directly in the TCP/IP stack. + * + * The copy of IP information kept in the ESP-NETIF instance is also updated (this + * copy is returned if the IP is queried while the interface is still down.) + * + * @note DHCP client/server must be stopped (if enabled for this interface) before setting new IP information. + * + * @note Calling this interface for may generate a SYSTEM_EVENT_STA_GOT_IP or SYSTEM_EVENT_ETH_GOT_IP event. + * + * @param[in] esp_netif Handle to esp-netif instance + * @param[in] ip_info IP information to set on the specified interface + * + * @return + * - ESP_OK + * - ESP_ERR_ESP_NETIF_INVALID_PARAMS + * - ESP_ERR_ESP_NETIF_DHCP_NOT_STOPPED If DHCP server or client is still running + */ +esp_err_t esp_netif_set_ip_info(esp_netif_t *esp_netif, const esp_netif_ip_info_t *ip_info); + +/** + * @brief Set interface old IP information + * + * This function is called from the DHCP client (if enabled), before a new IP is set. + * It is also called from the default handlers for the SYSTEM_EVENT_STA_CONNECTED and SYSTEM_EVENT_ETH_CONNECTED events. + * + * Calling this function stores the previously configured IP, which can be used to determine if the IP changes in the future. + * + * If the interface is disconnected or down for too long, the "IP lost timer" will expire (after the configured interval) and set the old IP information to zero. + * + * @param[in] esp_netif Handle to esp-netif instance + * @param[in] ip_info Store the old IP information for the specified interface + * + * @return + * - ESP_OK + * - ESP_ERR_ESP_NETIF_INVALID_PARAMS + */ +esp_err_t esp_netif_set_old_ip_info(esp_netif_t *esp_netif, const esp_netif_ip_info_t *ip_info); + + +// +// 5) Network stack related API +// + +// +// DHCP +// +/** + * @brief Set or Get DHCP server option + * + * @param[in] esp_netif Handle to esp-netif instance + * @param[in] opt_op ESP_NETIF_OP_SET to set an option, ESP_NETIF_OP_GET to get an option. + * @param[in] opt_id Option index to get or set, must be one of the supported enum values. + * @param[inout] opt_val Pointer to the option parameter. + * @param[in] opt_len Length of the option parameter. + * + * @return + * - ESP_OK + * - ESP_ERR_ESP_NETIF_INVALID_PARAMS + * - 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_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) + * + * @note The default event handlers for the SYSTEM_EVENT_STA_CONNECTED and SYSTEM_EVENT_ETH_CONNECTED events call this function. + * + * @param[in] esp_netif Handle to esp-netif instance + * + * @return + * - ESP_OK + * - ESP_ERR_ESP_NETIF_INVALID_PARAMS + * - ESP_ERR_ESP_NETIF_DHCP_ALREADY_STARTED + * - ESP_ERR_ESP_NETIF_DHCPC_START_FAILED + */ +esp_err_t esp_netif_dhcpc_start(esp_netif_t *esp_netif); + +/** + * @brief Stop DHCP client (only if enabled in interface object) + * + * @note Calling action_netif_stop() will also stop the DHCP Client if it is running. + * + * @param[in] esp_netif Handle to esp-netif instance + * + * @return + * - ESP_OK + * - ESP_ERR_ESP_NETIF_INVALID_PARAMS + * - ESP_ERR_ESP_NETIF_DHCP_ALREADY_STOPPED + * - ESP_ERR_ESP_NETIF_IF_NOT_READY + */ +esp_err_t esp_netif_dhcpc_stop(esp_netif_t *esp_netif); + +/** + * @brief Get DHCP client status + * + * @param[in] esp_netif Handle to esp-netif instance + * @param[out] status If successful, the status of DHCP client will be returned in this argument. + * + * @return + * - ESP_OK + */ +esp_err_t esp_netif_dhcpc_get_status(esp_netif_t *esp_netif, esp_netif_dhcp_status_t *status); + +/** + * @brief Get DHCP Server status + * + * @param[in] esp_netif Handle to esp-netif instance + * @param[out] status If successful, the status of the DHCP server will be returned in this argument. + * + * @return + * - ESP_OK + */ +esp_err_t esp_netif_dhcps_get_status(esp_netif_t *esp_netif, esp_netif_dhcp_status_t *status); + +/** + * @brief Start DHCP server (only if enabled in interface object) + * + * @param[in] esp_netif Handle to esp-netif instance + * + * @return + * - ESP_OK + * - ESP_ERR_ESP_NETIF_INVALID_PARAMS + * - ESP_ERR_ESP_NETIF_DHCP_ALREADY_STARTED + */ +esp_err_t esp_netif_dhcps_start(esp_netif_t *esp_netif); + +/** + * @brief Stop DHCP server (only if enabled in interface object) + * + * @param[in] esp_netif Handle to esp-netif instance + * + * @return + * - ESP_OK + * - ESP_ERR_ESP_NETIF_INVALID_PARAMS + * - ESP_ERR_ESP_NETIF_DHCP_ALREADY_STOPPED + * - ESP_ERR_ESP_NETIF_IF_NOT_READY + */ +esp_err_t esp_netif_dhcps_stop(esp_netif_t *esp_netif); + +// +// DNS: +// +/** + * @brief Set DNS Server information + * + * This function behaves differently if DHCP server or client is enabled + * + * If DHCP client is enabled, main and backup DNS servers will be updated automatically + * from the DHCP lease if the relevant DHCP options are set. Fallback DNS Server is never updated from the DHCP lease + * and is designed to be set via this API. + * If DHCP client is disabled, all DNS server types can be set via this API only. + * + * If DHCP server is enabled, the Main DNS Server setting is used by the DHCP server to provide a DNS Server option + * to DHCP clients (Wi-Fi stations). + * - The default Main DNS server is typically the IP of the Wi-Fi AP interface itself. + * - This function can override it by setting server type ESP_NETIF_DNS_MAIN. + * - Other DNS Server types are not supported for the Wi-Fi AP interface. + * + * @param[in] esp_netif Handle to esp-netif instance + * @param[in] type Type of DNS Server to set: ESP_NETIF_DNS_MAIN, ESP_NETIF_DNS_BACKUP, ESP_NETIF_DNS_FALLBACK + * @param[in] dns DNS Server address to set + * + * @return + * - ESP_OK on success + * - ESP_ERR_ESP_NETIF_INVALID_PARAMS invalid params + */ +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); + +/** + * @brief Get DNS Server information + * + * Return the currently configured DNS Server address for the specified interface and Server type. + * + * This may be result of a previous call to esp_netif_set_dns_info(). If the interface's DHCP client is enabled, + * the Main or Backup DNS Server may be set by the current DHCP lease. + * + * @param[in] esp_netif Handle to esp-netif instance + * @param[in] type Type of DNS Server to get: ESP_NETIF_DNS_MAIN, ESP_NETIF_DNS_BACKUP, ESP_NETIF_DNS_FALLBACK + * @param[out] dns DNS Server result is written here on success + * + * @return + * - ESP_OK on success + * - ESP_ERR_ESP_NETIF_INVALID_PARAMS invalid params + */ +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); +/** + * @brief Set the mac address for the interface instance + + * @param[in] esp_netif Handle to esp-netif instance + * @param[in] mac Desired mac address for the related network interface + * @return ESP_OK + */ +esp_err_t esp_netif_set_mac(esp_netif_t *esp_netif, uint8_t mac[]); + +/** + * @brief Create interface link-local IPv6 address + * + * Cause the TCP/IP stack to create a link-local IPv6 address for the specified interface. + * + * This function also registers a callback for the specified interface, so that if the link-local address becomes + * verified as the preferred address then a SYSTEM_EVENT_GOT_IP6 event will be sent. + * + * @param[in] esp_netif Handle to esp-netif instance + * + * @return + * - ESP_OK + * - ESP_ERR_ESP_NETIF_INVALID_PARAMS + */ +esp_err_t esp_netif_create_ip6_linklocal(esp_netif_t *esp_netif); + +/** + * @brief Get interface link-local IPv6 address + * + * If the specified interface is up and a preferred link-local IPv6 address + * has been created for the interface, return a copy of it. + * + * @param[in] esp_netif Handle to esp-netif instance + * @param[out] if_ip6 IPv6 information will be returned in this argument if successful. + * + * @return + * - ESP_OK + * - ESP_FAIL If interface is down, does not have a link-local IPv6 address, + * or the link-local IPv6 address is not a preferred address. + */ +esp_err_t esp_netif_get_ip6_linklocal(esp_netif_t *esp_netif, esp_ip6_addr_t *if_ip6); + +/** + * @brief Sets IPv4 address to the specified octets + * + * @param[out] addr IP address to be set + * @param a the first octet (127 for IP 127.0.0.1) + * @param b + * @param c + * @param d + */ +void esp_netif_set_ip4_addr(esp_ip4_addr_t *addr, uint8_t a, uint8_t b, uint8_t c, uint8_t d); + + +/** + * @brief Converts numeric IP address into decimal dotted ASCII representation. + * + * @param addr ip address in network order to convert + * @param buf target buffer where the string is stored + * @param buflen length of buf + * @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); + +// +// 6) Driver conversion utilities to related keys, flags, implementation handle, list of netifs +// + +/** + * @brief Gets media driver handle for this esp-netif instance + * + * @param[in] esp_netif Handle to esp-netif instance + * + * @return opaque pointer of related IO driver + */ +esp_netif_iodriver_handle esp_netif_get_io_driver(esp_netif_t *esp_netif); + +/** + * @brief Searches over a list of created objects to find an instance with supplied if key + * + * @param if_key Textual description of network interface + * + * @return Handle to esp-netif instance + */ +esp_netif_t *esp_netif_get_handle_from_ifkey(const char *if_key); + +/** + * @brief Returns configured flags for this interface + * + * @param[in] esp_netif Handle to esp-netif instance + * + * @return Configuration flags + */ +esp_netif_flags_t esp_netif_get_flags(esp_netif_t *esp_netif); + +/** + * @brief Returns configured interface key for this esp-netif instance + * + * @param[in] esp_netif Handle to esp-netif instance + * + * @return Textual description of related interface + */ +char *esp_netif_get_ifkey(esp_netif_t *esp_netif); + +/** + * @brief Returns configured interface type for this esp-netif instance + * + * @param[in] esp_netif Handle to esp-netif instance + * + * @return Enumerated type of this interface, such as station, AP, ethernet + */ +esp_netif_type_t esp_netif_get_type(esp_netif_t *esp_netif); + +/** + * @brief Returns configured event for this esp-netif instance and supplied event type + * + * @param[in] esp_netif Handle to esp-netif instance + * + * @param event_type (either get or lost IP) + * + * @return specific event id which is configured to be raised if the interface lost or acquired IP address + */ +uint32_t esp_netif_get_event_id(esp_netif_t *esp_netif, esp_netif_ip_event_type_t event_type); + +// +// 7) esp_netif list API +// +/** + * @brief Iterates over list of interfaces. Returns first netif if NULL given as parameter + * + * @param[in] esp_netif Handle to esp-netif instance + * + * @return First netif from the list if supplied parameter is NULL, next one otherwise + */ +esp_netif_t* esp_netif_next(esp_netif_t* netif); + +/** + * @brief Returns number of registered esp_netif objects + * + * @return Number of esp_netifs + */ +size_t esp_netif_get_nr_of_ifs(void); + +// +// 8) MISC: List of STAs +// + +/** + * @brief Get IP information for stations connected to the Wi-Fi AP interface + * + * @param[in] wifi_sta_list Wi-Fi station info list, returned from esp_wifi_ap_get_sta_list() + * @param[out] tcpip_sta_list IP layer station info list, corresponding to MAC addresses provided in wifi_sta_list + * + * @return + * - ESP_OK + * - ESP_ERR_ESP_NETIF_NO_MEM + * - ESP_ERR_ESP_NETIF_INVALID_PARAMS + */ +esp_err_t esp_netif_get_sta_list(const wifi_sta_list_t *wifi_sta_list, esp_netif_sta_list_t *tcpip_sta_list); + +int esp_netif_get_netif_index(esp_netif_t *esp_netif); + +#if CONFIG_ESP_NETIF_USE_TCPIP_ADAPTER_COMPATIBLE_LAYER +// +// 8) Compatibility layer +// +#include "tcpip_adapter.h" +#endif + +#endif /* _ESP_NETIF_H_ */ diff --git a/components/esp_netif/include/esp_netif_defaults.h b/components/esp_netif/include/esp_netif_defaults.h new file mode 100644 index 0000000000..19b7208e94 --- /dev/null +++ b/components/esp_netif/include/esp_netif_defaults.h @@ -0,0 +1,114 @@ +// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef _ESP_NETIF_DEFAULTS_H +#define _ESP_NETIF_DEFAULTS_H + +// +// Macros to assemble master configs with partial configs from netif, stack and driver +// + +/** + * @brief Default configuration reference of ethernet interface + */ +#define ESP_NETIF_DEFAULT_ETH() \ + { \ + .base = ESP_NETIF_BASE_DEFAULT_ETH, \ + .stack = ESP_NETIF_NETSTACK_DEFAULT_ETH, \ + .driver = ESP_NETIF_DRIVER_DEFAULT_ETH, \ + } + +/** + * @brief Default configuration reference of WIFI AP + */ +#define ESP_NETIF_DEFAULT_WIFI_AP() \ +{ \ + .base = ESP_NETIF_BASE_DEFAULT_WIFI_AP, \ + .stack = ESP_NETIF_NETSTACK_DEFAULT_WIFI_AP, \ + .driver = ESP_NETIF_DRIVER_DEFAULT_WIFI_AP, \ + } + +/** +* @brief Default configuration reference of WIFI STA +*/ +#define ESP_NETIF_DEFAULT_WIFI_STA() \ + { \ + .base = ESP_NETIF_BASE_DEFAULT_WIFI_STA, \ + .stack = ESP_NETIF_NETSTACK_DEFAULT_WIFI_STA, \ + .driver = ESP_NETIF_DRIVER_DEFAULT_WIFI_STA, \ + } +/** + * @brief Default base config (esp-netif inherent) of WIFI STA + */ +#define ESP_NETIF_BASE_DEFAULT_WIFI_STA &_g_esp_netif_inherent_sta_config + +/** + * @brief Default base config (esp-netif inherent) of WIFI AP + */ +#define ESP_NETIF_BASE_DEFAULT_WIFI_AP &_g_esp_netif_inherent_ap_config + +/** + * @brief Default base config (esp-netif inherent) of ethernet interface + */ +#define ESP_NETIF_BASE_DEFAULT_ETH &_g_esp_netif_inherent_eth_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 +// +// Include default network stacks configs +// - Network stack configurations are provided in a specific network stack +// implementation that is invisible to user API +// - Here referenced only as opaque pointers +// +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; + +// +// Include default common configs inherent to esp-netif +// - These inherent configs are defined in esp_netif_defaults.c and describe +// common behavioural patters for common interfaces such as STA, AP, ETH +// +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; + +// +// API for creating default interfaces +// + +/** + * @brief Creates default WIFI AP. In case of any init error this API aborts. + * + * @return pointer to esp-netif instance + */ +esp_netif_t* esp_netif_create_default_wifi_ap(void); + +/** + * @brief Creates default WIFI STA. In case of any init error this API aborts. + * + * @return pointer to esp-netif instance + */ +esp_netif_t* esp_netif_create_default_wifi_sta(void); + +/** + * @brief Creates default ethernet interface. In case of any init error this API aborts. + * + * @return pointer to esp-netif instance + */ +esp_netif_t* esp_netif_create_default_eth(void * eth_driver); + + +#endif //_ESP_NETIF_DEFAULTS_H diff --git a/components/esp_netif/include/esp_netif_ip_addr.h b/components/esp_netif/include/esp_netif_ip_addr.h new file mode 100644 index 0000000000..1680ac9764 --- /dev/null +++ b/components/esp_netif/include/esp_netif_ip_addr.h @@ -0,0 +1,88 @@ +// Copyright 2015-2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef _ESP_NETIF_IP_ADDR_H_ +#define _ESP_NETIF_IP_ADDR_H_ + +#include +#if BYTE_ORDER == BIG_ENDIAN +#define esp_netif_htonl(x) ((uint32_t)(x)) +#else +#define esp_netif_htonl(x) ((((x) & (uint32_t)0x000000ffUL) << 24) | \ + (((x) & (uint32_t)0x0000ff00UL) << 8) | \ + (((x) & (uint32_t)0x00ff0000UL) >> 8) | \ + (((x) & (uint32_t)0xff000000UL) >> 24)) +#endif +// Access address in 16-bit block +#define ESP_IP6_ADDR_BLOCK1(ip6addr) ((uint16_t)((esp_netif_htonl((ip6addr)->addr[0]) >> 16) & 0xffff)) +#define ESP_IP6_ADDR_BLOCK2(ip6addr) ((uint16_t)((esp_netif_htonl((ip6addr)->addr[0])) & 0xffff)) +#define ESP_IP6_ADDR_BLOCK3(ip6addr) ((uint16_t)((esp_netif_htonl((ip6addr)->addr[1]) >> 16) & 0xffff)) +#define ESP_IP6_ADDR_BLOCK4(ip6addr) ((uint16_t)((esp_netif_htonl((ip6addr)->addr[1])) & 0xffff)) +#define ESP_IP6_ADDR_BLOCK5(ip6addr) ((uint16_t)((esp_netif_htonl((ip6addr)->addr[2]) >> 16) & 0xffff)) +#define ESP_IP6_ADDR_BLOCK6(ip6addr) ((uint16_t)((esp_netif_htonl((ip6addr)->addr[2])) & 0xffff)) +#define ESP_IP6_ADDR_BLOCK7(ip6addr) ((uint16_t)((esp_netif_htonl((ip6addr)->addr[3]) >> 16) & 0xffff)) +#define ESP_IP6_ADDR_BLOCK8(ip6addr) ((uint16_t)((esp_netif_htonl((ip6addr)->addr[3])) & 0xffff)) + +#define IPSTR "%d.%d.%d.%d" +#define esp_ip4_addr_get_byte(ipaddr, idx) (((const uint8_t*)(&(ipaddr)->addr))[idx]) +#define esp_ip4_addr1(ipaddr) esp_ip4_addr_get_byte(ipaddr, 0) +#define esp_ip4_addr2(ipaddr) esp_ip4_addr_get_byte(ipaddr, 1) +#define esp_ip4_addr3(ipaddr) esp_ip4_addr_get_byte(ipaddr, 2) +#define esp_ip4_addr4(ipaddr) esp_ip4_addr_get_byte(ipaddr, 3) + + +#define esp_ip4_addr1_16(ipaddr) ((uint16_t)esp_ip4_addr1(ipaddr)) +#define esp_ip4_addr2_16(ipaddr) ((uint16_t)esp_ip4_addr2(ipaddr)) +#define esp_ip4_addr3_16(ipaddr) ((uint16_t)esp_ip4_addr3(ipaddr)) +#define esp_ip4_addr4_16(ipaddr) ((uint16_t)esp_ip4_addr4(ipaddr)) + +#define IP2STR(ipaddr) esp_ip4_addr1_16(ipaddr), \ + esp_ip4_addr2_16(ipaddr), \ + esp_ip4_addr3_16(ipaddr), \ + esp_ip4_addr4_16(ipaddr) + +#define IPV6STR "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x" + +#define IPV62STR(ipaddr) ESP_IP6_ADDR_BLOCK1(&(ipaddr)), \ + ESP_IP6_ADDR_BLOCK2(&(ipaddr)), \ + ESP_IP6_ADDR_BLOCK3(&(ipaddr)), \ + ESP_IP6_ADDR_BLOCK4(&(ipaddr)), \ + ESP_IP6_ADDR_BLOCK5(&(ipaddr)), \ + ESP_IP6_ADDR_BLOCK6(&(ipaddr)), \ + ESP_IP6_ADDR_BLOCK7(&(ipaddr)), \ + ESP_IP6_ADDR_BLOCK8(&(ipaddr)) + + +struct esp_ip6_addr { + uint32_t addr[4]; + uint8_t zone; +}; + +struct esp_ip4_addr { + uint32_t addr; +}; + +typedef struct esp_ip4_addr esp_ip4_addr_t; + +typedef struct esp_ip6_addr esp_ip6_addr_t; + +typedef struct _ip_addr { + union { + esp_ip6_addr_t ip6; + esp_ip4_addr_t ip4; + } u_addr; + uint8_t type; +} esp_ip_addr_t; + +#endif //_ESP_NETIF_IP_ADDR_H_ diff --git a/components/esp_netif/include/esp_netif_net_stack.h b/components/esp_netif/include/esp_netif_net_stack.h new file mode 100644 index 0000000000..bfef36fd04 --- /dev/null +++ b/components/esp_netif/include/esp_netif_net_stack.h @@ -0,0 +1,41 @@ +// Copyright 2015-2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef _ESP_NETIF_NET_STACK_H_ +#define _ESP_NETIF_NET_STACK_H_ + +// +// Network stack API: This ESP-NETIF API are supposed to be called only from internals of TCP/IP stack +// + +/** + * @brief Returns esp-netif handle + * + * @param[in] dev opaque ptr to network interface of specific TCP/IP stack + * + * @return handle to related esp-netif instance + */ +esp_netif_t* esp_netif_get_handle_from_netif_impl(void *dev); + + +/** + * @brief Returns network stack specific implementation handle + * + * @param[in] esp_netif Handle to esp-netif instance + * + * @return handle to related network stack netif handle + */ +void* esp_netif_get_netif_impl(esp_netif_t *esp_netif); + +#endif //_ESP_NETIF_NET_STACK_H_ diff --git a/components/esp_netif/include/esp_netif_types.h b/components/esp_netif/include/esp_netif_types.h new file mode 100644 index 0000000000..5ce7837c11 --- /dev/null +++ b/components/esp_netif/include/esp_netif_types.h @@ -0,0 +1,223 @@ +// Copyright 2015-2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef _ESP_NETIF_TYPES_H_ +#define _ESP_NETIF_TYPES_H_ + +/** + * @brief Definition of ESP-NETIF based errors + */ +#define ESP_ERR_ESP_NETIF_BASE 0x5000 +#define ESP_ERR_ESP_NETIF_INVALID_PARAMS ESP_ERR_ESP_NETIF_BASE + 0x01 +#define ESP_ERR_ESP_NETIF_IF_NOT_READY ESP_ERR_ESP_NETIF_BASE + 0x02 +#define ESP_ERR_ESP_NETIF_DHCPC_START_FAILED ESP_ERR_ESP_NETIF_BASE + 0x03 +#define ESP_ERR_ESP_NETIF_DHCP_ALREADY_STARTED ESP_ERR_ESP_NETIF_BASE + 0x04 +#define ESP_ERR_ESP_NETIF_DHCP_ALREADY_STOPPED ESP_ERR_ESP_NETIF_BASE + 0x05 +#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_ATACH_FAILED ESP_ERR_ESP_NETIF_BASE + 0x08 + + +/** @brief Type of esp_netif_object server */ +struct esp_netif_obj; + +typedef struct esp_netif_obj esp_netif_t; + + +/** @brief Type of DNS server */ +typedef enum { + ESP_NETIF_DNS_MAIN= 0, /**< DNS main server address*/ + ESP_NETIF_DNS_BACKUP, /**< DNS backup server address (Wi-Fi STA and Ethernet only) */ + ESP_NETIF_DNS_FALLBACK, /**< DNS fallback server address (Wi-Fi STA and Ethernet only) */ + ESP_NETIF_DNS_MAX +} esp_netif_dns_type_t; + +/** @brief DNS server info */ +typedef struct { + esp_ip_addr_t ip; /**< IPV4 address of DNS server */ +} esp_netif_dns_info_t; + +/** @brief Status of DHCP client or DHCP server */ +typedef enum { + ESP_NETIF_DHCP_INIT = 0, /**< DHCP client/server is in initial state (not yet started) */ + ESP_NETIF_DHCP_STARTED, /**< DHCP client/server has been started */ + ESP_NETIF_DHCP_STOPPED, /**< DHCP client/server has been stopped */ + ESP_NETIF_DHCP_STATUS_MAX +} esp_netif_dhcp_status_t; + + +/** @brief Mode for DHCP client or DHCP server option functions */ +typedef enum{ + ESP_NETIF_OP_START = 0, + ESP_NETIF_OP_SET, /**< Set option */ + ESP_NETIF_OP_GET, /**< Get option */ + ESP_NETIF_OP_MAX +} esp_netif_dhcp_option_mode_t; + +/** @brief Supported options for DHCP client or DHCP server */ +typedef enum{ + ESP_NETIF_DOMAIN_NAME_SERVER = 6, /**< Domain name server */ + ESP_NETIF_ROUTER_SOLICITATION_ADDRESS = 32, /**< Solicitation router address */ + ESP_NETIF_REQUESTED_IP_ADDRESS = 50, /**< Request specific IP address */ + ESP_NETIF_IP_ADDRESS_LEASE_TIME = 51, /**< Request IP address lease time */ + ESP_NETIF_IP_REQUEST_RETRY_TIME = 52, /**< Request IP address retry counter */ +} esp_netif_dhcp_option_id_t; + +/** 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_t; + +/** @brief IP event base declaration */ +ESP_EVENT_DECLARE_BASE(IP_EVENT); + +/** Event structure for IP_EVENT_STA_GOT_IP, IP_EVENT_ETH_GOT_IP events */ + +typedef struct { + esp_ip4_addr_t ip; /**< Interface IPV4 address */ + esp_ip4_addr_t netmask; /**< Interface IPV4 netmask */ + esp_ip4_addr_t gw; /**< Interface IPV4 gateway address */ +} esp_netif_ip_info_t; + +/** @brief IPV6 IP address information + */ +typedef struct { + esp_ip6_addr_t ip; /**< Interface IPV6 address */ +} esp_netif_ip6_info_t; + +typedef struct { + int if_index; /*!< Interface index for which the event is received (left for legacy compilation) */ + esp_netif_t *esp_netif; /*!< Pointer to corresponding esp-netif object */ + esp_netif_ip_info_t ip_info; /*!< IP address, netmask, gatway IP address */ + bool ip_changed; /*!< Whether the assigned IP has changed or not */ +} ip_event_got_ip_t; + +/** Event structure for IP_EVENT_GOT_IP6 event */ +typedef struct { + int if_index; /*!< Interface index for which the event is received (left for legacy compilation) */ + esp_netif_t *esp_netif; /*!< Pointer to corresponding esp-netif object */ + esp_netif_ip6_info_t ip6_info; /*!< IPv6 address of the interface */ +} ip_event_got_ip6_t; + +/** Event structure for IP_EVENT_AP_STAIPASSIGNED event */ +typedef struct { + esp_ip4_addr_t ip; /*!< IP address which was assigned to the station */ +} ip_event_ap_staipassigned_t; + + +typedef struct { + uint8_t mac[6]; /**< Station MAC address */ + esp_ip4_addr_t ip; /**< Station assigned IP address */ +} esp_netif_sta_info_t; + + +typedef struct { + esp_netif_sta_info_t sta[ESP_WIFI_MAX_CONN_NUM]; /**< Connected stations */ + int num; /**< Number of connected stations */ +} esp_netif_sta_list_t; + +typedef enum esp_netif_flags { + ESP_NETIF_DHCPC = 1 << 0, + ESP_NETIF_DHCPS = 1 << 1, + ESP_NETIF_FLAG_AUTOUP = 1 << 2, + ESP_NETIF_FLAG_GARP = 1 << 3, + ESP_NETIF_FLAG_EVENT_IP_MODIFIED = 1 << 4 +} esp_netif_flags_t; + +typedef enum esp_netif_type { + ESP_NETIF_TYPE_UNKNOWN, + ESP_NETIF_TYPE_STA, + ESP_NETIF_TYPE_AP, + ESP_NETIF_TYPE_ETH, + ESP_NETIF_TYPE_OTHER, + ESP_NETIF_TYPE_MAX +} esp_netif_type_t; + +typedef enum esp_netif_ip_event_type { + ESP_NETIF_IP_EVENT_GOT_IP = 1, + ESP_NETIF_IP_EVENT_LOST_IP = 2, +} esp_netif_ip_event_type_t; + + +// +// ESP-NETIF interface configuration: +// 1) general (behavioral) config (esp_netif_config_t) +// 2) (peripheral) driver specific config (esp_netif_driver_ifconfig_t) +// 3) network stack specific config (esp_netif_net_stack_ifconfig_t) -- no publicly available +// + +typedef struct esp_netif_inherent_config { + esp_netif_flags_t flags; /*!< flags that define esp-netif behavior */ + uint8_t mac[6]; /*!< initial mac address for this interface */ + esp_netif_ip_info_t* ip_info; /*!< initial ip address for this interface */ + uint32_t get_ip_event; /*!< event id to be raised when interface gets an IP */ + uint32_t lost_ip_event; /*!< event id to be raised when interface losts its IP */ + esp_netif_type_t if_type; /*!< enum type of the interface */ + const char * if_key; /*!< string identifier of the interface */ + int route_prio; /*!< numeric priority of this interface to become a default + routing if (if other netifs are up) */ +} esp_netif_inherent_config_t; + +typedef struct esp_netif_config esp_netif_config_t; + +/** + * @brief IO driver handle type + */ +typedef void * esp_netif_iodriver_handle; + +typedef struct esp_netif_driver_base_s { + esp_err_t (*post_attach)(esp_netif_t *netif, esp_netif_iodriver_handle h); + esp_netif_t *netif; +} esp_netif_driver_base_t; + +/** + * @brief Specific IO driver configuration + */ +struct esp_netif_driver_ifconfig { + esp_netif_iodriver_handle handle; + esp_err_t (*transmit)(void *h, void *buffer, size_t len); + void (*driver_free_rx_buffer)(void *h, void* buffer); +}; + +//typedef struct esp_netif_net_stack_ifconfig esp_netif_net_stack_ifconfig_t; +typedef struct esp_netif_driver_ifconfig esp_netif_driver_ifconfig_t; + +/** + * @brief Specific L3 network stack configuration + */ +typedef enum esp_netif_netstack_type { + ESP_NETIF_NETWORK_STACK_IS_LWIP = 0, + ESP_NETIF_NETWORK_STACK_MAX, +} esp_netif_netstack_type_t; + +typedef struct esp_netif_netstack_base_config { + esp_netif_netstack_type_t type; +} esp_netif_netstack_base_config_t; + +typedef struct esp_netif_netstack_config esp_netif_netstack_config_t; + +/** + * @brief Generic esp_netif configuration + */ +struct esp_netif_config { + const esp_netif_inherent_config_t *base; + const esp_netif_driver_ifconfig_t *driver; + const esp_netif_netstack_config_t *stack; +}; + +#endif // _ESP_NETIF_TYPES_H_ \ No newline at end of file diff --git a/components/esp_netif/lwip/esp_netif_lwip.c b/components/esp_netif/lwip/esp_netif_lwip.c new file mode 100644 index 0000000000..6ff408ec46 --- /dev/null +++ b/components/esp_netif/lwip/esp_netif_lwip.c @@ -0,0 +1,1444 @@ +// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include + +#include "esp_netif_lwip_internal.h" + +#include "esp_netif.h" +#include "esp_netif_private.h" +#include "esp_netif_net_stack.h" + +#if CONFIG_TCPIP_LWIP + +#include "lwip/inet.h" +#include "lwip/tcpip.h" +#include "lwip/dhcp.h" +#include "lwip/ip_addr.h" +#include "lwip/ip6_addr.h" +#include "lwip/nd6.h" +#include "lwip/priv/tcpip_priv.h" +#include "lwip/netif.h" +#if LWIP_DNS /* don't build if not configured for use in lwipopts.h */ +#include "lwip/dns.h" +#endif +#include "esp_netif_lwip_internal.h" + +#include "dhcpserver/dhcpserver.h" +#include "dhcpserver/dhcpserver_options.h" + +#include "esp_event.h" +#include "esp_log.h" +#include "lwip/ip_addr.h" + +// +// This is the main module implementing lwip interaction with esp-netif +// + +#define ESP_NETIF_HOSTNAME_MAX_SIZE 32 + +typedef enum esp_netif_action { + ESP_NETIF_UNDEF, + ESP_NETIF_STARTED, + ESP_NETIF_STOPPED, +} esp_netif_action_t; + +extern sys_thread_t g_lwip_task; + +static const char *TAG = "esp_netif_lwip"; + +static sys_sem_t api_sync_sem = NULL; +static sys_sem_t api_lock_sem = NULL; +static bool tcpip_initialized = false; +static esp_netif_t *s_last_default_esp_netif = NULL; + +static int esp_netif_ipc_check(esp_netif_api_msg_t *msg); + +/** + * @brief Main esp-netif container with interface related information + * + * + */ +struct esp_netif_obj { + // default interface addresses + uint8_t mac[6]; + 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; + esp_netif_type_t if_type; + int route_prio; +}; + +/** + * @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 + * @param action updating action (on-off) + * + * @note: This function must be called from lwip thread + * + */ +static void esp_netif_update_default_netif(esp_netif_t *esp_netif, esp_netif_action_t action) { + switch (action) { + case ESP_NETIF_STARTED: + 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); + } 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); + } + break; + default: + case ESP_NETIF_STOPPED: + { + esp_netif_t *netif = esp_netif_next(NULL); + s_last_default_esp_netif = NULL; + while (netif) { + if (esp_netif_is_netif_up(netif)) { + if (s_last_default_esp_netif && esp_netif_is_netif_up(s_last_default_esp_netif)) { + if (netif->route_prio > s_last_default_esp_netif->route_prio) { + s_last_default_esp_netif = netif; + } // else not needed, as the s_last_default_esp_netif is correct + } else { + // s_last_default is either not set or down, current netif is up + s_last_default_esp_netif = netif; + } + } + netif = esp_netif_next(netif); + } + 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); + } + } break; + } +} + +void esp_netif_set_ip4_addr(esp_ip4_addr_t *addr, uint8_t a, uint8_t b, uint8_t c, uint8_t d) +{ + ip4_addr_t *address = (ip4_addr_t*)addr; + IP4_ADDR(address, a, b, c, d); +} + +char * esp_ip4addr_ntoa(const esp_ip4_addr_t *addr, char *buf, int buflen) +{ + return ip4addr_ntoa_r((ip4_addr_t *)addr, buf, buflen); +} + +esp_netif_iodriver_handle esp_netif_get_io_driver(esp_netif_t *esp_netif) +{ + return esp_netif->driver_handle; +} + +esp_netif_t* esp_netif_get_handle_from_netif_impl(void *dev) +{ + struct netif *lwip_netif = dev; + return lwip_netif->state; +} + +void* esp_netif_get_netif_impl(esp_netif_t *esp_netif) +{ + return esp_netif->lwip_netif; +} + +esp_err_t esp_netif_init(void) +{ + if (tcpip_initialized == false) { + tcpip_initialized = true; + tcpip_init(NULL, NULL); + ESP_LOGD(TAG, "LwIP stack has been initialized"); + } + + if (!api_sync_sem) { + if (ERR_OK != sys_sem_new(&api_sync_sem, 0)) { + ESP_LOGE(TAG, "esp netif api sync sem init fail"); + return ESP_FAIL; + } + } + + if (!api_lock_sem) { + if (ERR_OK != sys_sem_new(&api_lock_sem, 1)) { + ESP_LOGE(TAG, "esp netif api lock sem init fail"); + return ESP_FAIL; + } + } + + ESP_LOGD(TAG, "esp-netif has been successfully initialized"); + return ESP_OK; +} + +esp_err_t esp_netif_deinit(void) +{ + if (tcpip_initialized == true) { + /* deinit of LwIP not supported: + * do not deinit semaphores and states, + * so init could be called multiple times + * + tcpip_initialized = false; + sys_sem_free(&api_sync_sem); + sys_sem_free(&api_lock_sem); + */ + return ESP_ERR_NOT_SUPPORTED; + + } + return ESP_ERR_INVALID_STATE; +} + +esp_netif_t *esp_netif_new(const esp_netif_config_t *esp_netif_config) +{ + // Create parent esp-netif object + esp_netif_t *esp_netif = calloc(1, sizeof(struct esp_netif_obj)); + if (!esp_netif) { + return NULL; + } + + // Create ip info + esp_netif_ip_info_t *ip_info = calloc(1, sizeof(esp_netif_ip_info_t)); + if (!ip_info) { + free(esp_netif); + return NULL; + } + esp_netif->ip_info = ip_info; + + // creating another ip info (to store old ip) + ip_info = calloc(1, sizeof(esp_netif_ip_info_t)); + if (!ip_info) { + free(esp_netif->ip_info); + free(esp_netif); + return NULL; + } + esp_netif->ip_info_old = ip_info; + + // Create underlying lwip netif + struct netif * lwip_netif = calloc(1, sizeof(struct netif)); + if (!lwip_netif) { + free(esp_netif->ip_info_old); + free(esp_netif->ip_info); + free(esp_netif); + return NULL; + } + + lwip_netif->state = esp_netif; + esp_netif->lwip_netif = lwip_netif; + + esp_netif_add_to_list(esp_netif); + + // Configure the created object with provided configuration + if (esp_netif_config) { + ESP_ERROR_CHECK(esp_netif_configure(esp_netif, esp_netif_config)); + } + + return esp_netif; +} + +void esp_netif_destroy(esp_netif_t *esp_netif) +{ + if (esp_netif) { + esp_netif_remove_from_list(esp_netif); + free(esp_netif->ip_info); + free(esp_netif->ip_info_old); + free(esp_netif->lwip_netif); + free(esp_netif->if_key); + free(esp_netif); + } +} + +esp_err_t esp_netif_attach(esp_netif_t *esp_netif, esp_netif_iodriver_handle driver_handle) +{ + esp_netif_driver_base_t *base_driver = driver_handle; + + esp_netif->driver_handle = driver_handle; + if (base_driver->post_attach) { + esp_err_t ret = base_driver->post_attach(esp_netif, driver_handle); + if (ret != ESP_OK) { + ESP_LOGE(TAG, "Post-attach callback of driver(%p) failed with %d", driver_handle, ret); + return ESP_ERR_ESP_NETIF_DRIVER_ATACH_FAILED; + } + } + return ESP_OK; +} + +esp_err_t esp_netif_configure(esp_netif_t *esp_netif, const esp_netif_config_t *cfg) + +{ + if (cfg == NULL) { + return ESP_ERR_ESP_NETIF_INVALID_PARAMS; + } +// Configure general esp-netif properties + if (cfg->base) { + memcpy(esp_netif->mac, cfg->base->mac, NETIF_MAX_HWADDR_LEN); + if (cfg->base->ip_info == NULL) { + ip4_addr_set_zero(&esp_netif->ip_info->ip); + ip4_addr_set_zero(&esp_netif->ip_info->gw); + ip4_addr_set_zero(&esp_netif->ip_info->netmask); + } else { + memcpy(esp_netif->ip_info, cfg->base->ip_info, sizeof(esp_netif_ip_info_t)); + } + memcpy(esp_netif->ip_info_old, esp_netif->ip_info, sizeof(esp_netif_ip_info_t)); + + // Setup main config parameters + esp_netif->lost_ip_event = cfg->base->lost_ip_event; + esp_netif->get_ip_event = cfg->base->get_ip_event; + esp_netif->flags = cfg->base->flags; + + if (cfg->base->if_key) { + esp_netif->if_key = strdup(cfg->base->if_key); + } + if (cfg->base->if_type) { + esp_netif->if_type = cfg->base->if_type; + } + if (cfg->base->route_prio) { + esp_netif->route_prio = cfg->base->route_prio; + } + } + +// Install network stack functions -- connects netif and L3 stack + if (cfg->stack) { + const esp_netif_netstack_config_t *esp_netif_stack_config = cfg->stack; + if (cfg->stack->base.type != ESP_NETIF_NETWORK_STACK_IS_LWIP) { + ESP_LOGE(TAG, "Failed to configure uknown network stack %d", cfg->stack->base.type); + return ESP_ERR_NOT_SUPPORTED; + } + 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; + } + } +// Install IO functions -- connects driver and netif + if (cfg->driver) { + const esp_netif_driver_ifconfig_t *esp_netif_driver_config = cfg->driver; + if (esp_netif_driver_config->handle) { + esp_netif->driver_handle = esp_netif_driver_config->handle; + } + if (esp_netif_driver_config->transmit) { + esp_netif->driver_transmit = esp_netif_driver_config->transmit; + } + if (esp_netif_driver_config->driver_free_rx_buffer) { + esp_netif->driver_free_rx_buffer = esp_netif_driver_config->driver_free_rx_buffer; + } + } + return ESP_OK; +} + +static esp_err_t esp_netif_reset_ip_info(esp_netif_t *esp_netif) +{ + ip4_addr_set_zero(&(esp_netif->ip_info->ip)); + ip4_addr_set_zero(&esp_netif->ip_info->gw); + ip4_addr_set_zero(&esp_netif->ip_info->netmask); + return ESP_OK; +} + +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; + } + memcpy(esp_netif->mac, mac, NETIF_MAX_HWADDR_LEN); + memcpy(esp_netif->lwip_netif->hwaddr, mac, NETIF_MAX_HWADDR_LEN); + return ESP_OK; +} + +esp_err_t esp_netif_start_api(esp_netif_api_msg_t *msg) +{ + return esp_netif_start(msg->esp_netif); +} + +static void esp_netif_dhcps_cb(u8_t client_ip[4]) +{ + ESP_LOGI(TAG, "softAP assign IP to station,IP is: %d.%d.%d.%d", + client_ip[0], client_ip[1], client_ip[2], client_ip[3]); + ip_event_ap_staipassigned_t evt; + + memset(&evt, 0, sizeof(ip_event_ap_staipassigned_t)); + memcpy((char *)&evt.ip.addr, (char *)client_ip, sizeof(evt.ip.addr)); + int ret = esp_event_send_internal(IP_EVENT, IP_EVENT_AP_STAIPASSIGNED, &evt, sizeof(evt), 0); + if (ESP_OK != ret) { + ESP_LOGE(TAG, "dhcps cb: failed to post IP_EVENT_AP_STAIPASSIGNED (%x)", ret); + } +} + + +esp_err_t esp_netif_start(esp_netif_t *esp_netif) +{ + ESP_NETIF_IPC_CALL(esp_netif, NULL, esp_netif_start_api); + + ESP_LOGD(TAG, "%s %p", __func__, esp_netif); + + 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); + + if (esp_netif->flags&ESP_NETIF_FLAG_GARP) { +#if ESP_GRATUITOUS_ARP + netif_set_garp_flag(esp_netif->lwip_netif); +#else + ESP_LOGW(TAG,"CONFIG_LWIP_ESP_GRATUITOUS_ARP not enabled, but esp-netif configured woth ESP_NETIF_FLAG_GARP") +#endif + } + struct netif *p_netif = esp_netif->lwip_netif; + if (esp_netif->flags&ESP_NETIF_FLAG_AUTOUP) { + ESP_LOGD(TAG, "%s Setting the lwip netif%p UP", __func__, p_netif); + netif_set_up(p_netif); + } + if (esp_netif->flags&ESP_NETIF_DHCPS) { + if (esp_netif->dhcps_status != ESP_NETIF_DHCP_STARTED) { + if (p_netif != NULL && netif_is_up(p_netif)) { + esp_netif_ip_info_t *default_ip = esp_netif->ip_info; + ip4_addr_t lwip_ip; + memcpy(&lwip_ip, &default_ip->ip, sizeof(struct ip4_addr)); + dhcps_set_new_lease_cb(esp_netif_dhcps_cb); + dhcps_start(p_netif, lwip_ip); + esp_netif->dhcps_status = ESP_NETIF_DHCP_STARTED; + ESP_LOGD(TAG, "DHCP server started successfully"); + esp_netif_update_default_netif(esp_netif, ESP_NETIF_STARTED); + return ESP_OK; + } else { + ESP_LOGD(TAG, "DHCP server re init"); + esp_netif->dhcps_status = ESP_NETIF_DHCP_INIT; + return ESP_OK; + } + } + ESP_LOGD(TAG, "DHCP server already started"); + return ESP_ERR_ESP_NETIF_DHCP_ALREADY_STARTED; + } + + esp_netif_update_default_netif(esp_netif, ESP_NETIF_STARTED); + + return ESP_OK; +} + +esp_err_t esp_netif_stop_api(esp_netif_api_msg_t *msg) +{ + return esp_netif_stop(msg->esp_netif); +} + +esp_err_t esp_netif_stop(esp_netif_t *esp_netif) +{ + ESP_NETIF_IPC_CALL(esp_netif, NULL, esp_netif_stop_api); + + struct netif *lwip_netif = esp_netif->lwip_netif; + if (lwip_netif == NULL) { + return ESP_ERR_ESP_NETIF_IF_NOT_READY; + } + + if (!netif_is_up(lwip_netif)) { + netif_remove(lwip_netif); + return ESP_ERR_ESP_NETIF_IF_NOT_READY; + } + + if (esp_netif->flags&ESP_NETIF_DHCPS) { + dhcps_stop(lwip_netif); // TODO: dhcps checks status by its self + if (ESP_NETIF_DHCP_STOPPED != esp_netif->dhcps_status) { + esp_netif->dhcps_status = ESP_NETIF_DHCP_INIT; + } + } else if (esp_netif->flags&ESP_NETIF_DHCPS) { + dhcp_release(lwip_netif); + dhcp_stop(lwip_netif); + dhcp_cleanup(lwip_netif); + + esp_netif->dhcpc_status = ESP_NETIF_DHCP_INIT; + + esp_netif_reset_ip_info(esp_netif); + } + + netif_set_down(lwip_netif); + netif_remove(lwip_netif); + esp_netif_update_default_netif(esp_netif, ESP_NETIF_STOPPED);; + + return ESP_OK; +} + +void esp_netif_free_rx_buffer(void *h, void* buffer) +{ + esp_netif_t *esp_netif = h; + esp_netif->driver_free_rx_buffer(esp_netif->driver_handle, buffer); +} + +esp_err_t esp_netif_transmit(esp_netif_t *esp_netif, void* data, size_t len) +{ + return (esp_netif->driver_transmit)(esp_netif->driver_handle, data, 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); + return ESP_OK; +} + + +static void esp_netif_api_cb(void *api_msg) +{ + esp_netif_api_msg_t *msg = (esp_netif_api_msg_t *)api_msg; + + if (!msg || !msg->api_fn) { + ESP_LOGD(TAG, "null msg/api_fn"); + return; + } + + msg->ret = msg->api_fn(msg); + ESP_LOGD(TAG, "call api in lwip: ret=0x%x, give sem", msg->ret); + sys_sem_signal(&api_sync_sem); + +} + +static int esp_netif_ipc_check(esp_netif_api_msg_t *msg) +{ +#if ESP_NETIF_TRHEAD_SAFE + xTaskHandle local_task = xTaskGetCurrentTaskHandle(); + + if (local_task == g_lwip_task) { + return ESP_NETIF_IPC_LOCAL; + } + + sys_arch_sem_wait(&api_lock_sem, 0); + tcpip_send_msg_wait_sem((tcpip_callback_fn)esp_netif_api_cb, msg, &api_sync_sem); + sys_sem_signal(&api_lock_sem); + + return ESP_NETIF_IPC_REMOTE; +#else + return ESP_NETIF_IPC_LOCAL; +#endif +} + +// +// DHCP: +// +static esp_err_t esp_netif_start_ip_lost_timer(esp_netif_t *esp_netif); + +static void esp_netif_dhcpc_cb(struct netif *netif) +{ + if (!netif) { + ESP_LOGD(TAG, "null netif=%p", netif); + return; + } + ESP_LOGD(TAG, "%s lwip-netif:%p", __func__, netif); + + esp_netif_t *esp_netif = esp_netif_get_handle_from_netif_impl(netif); + + esp_netif_ip_info_t *ip_info = esp_netif->ip_info; + esp_netif_ip_info_t *ip_info_old = esp_netif->ip_info_old; + + + if ( !ip4_addr_cmp(ip_2_ip4(&netif->ip_addr), IP4_ADDR_ANY4) ) { + + //check whether IP is changed + if ( !ip4_addr_cmp(ip_2_ip4(&netif->ip_addr), (&ip_info->ip)) || + !ip4_addr_cmp(ip_2_ip4(&netif->netmask), (&ip_info->netmask)) || + !ip4_addr_cmp(ip_2_ip4(&netif->gw), (&ip_info->gw)) ) { + ip_event_got_ip_t evt = { + .esp_netif = esp_netif, + .if_index = -1, // invalid index, handle used + .ip_changed = false, + }; + ip_event_t evt_id = esp_netif_get_event_id(esp_netif, ESP_NETIF_IP_EVENT_GOT_IP); + int ret; + + ip4_addr_set(&ip_info->ip, ip_2_ip4(&netif->ip_addr)); + ip4_addr_set(&ip_info->netmask, ip_2_ip4(&netif->netmask)); + ip4_addr_set(&ip_info->gw, ip_2_ip4(&netif->gw)); + + //notify event + if (memcmp(ip_info, ip_info_old, sizeof(esp_netif_ip_info_t))) { + evt.ip_changed = true; + } + + memcpy(&evt.ip_info, ip_info, sizeof(esp_netif_ip_info_t)); + memcpy(ip_info_old, ip_info, sizeof(esp_netif_ip_info_t)); + ESP_LOGD(TAG, "if%p ip changed=%d", esp_netif, evt.ip_changed); + ret = esp_event_send_internal(IP_EVENT, evt_id, &evt, sizeof(evt), 0); + if (ESP_OK != ret) { + ESP_LOGE(TAG, "dhcpc cb: failed to post got ip event (%x)", ret); + } + } else { + ESP_LOGD(TAG, "if%p ip unchanged", esp_netif); + } + } else { + if (!ip4_addr_cmp(&ip_info->ip, IP4_ADDR_ANY4)) { + esp_netif_start_ip_lost_timer(esp_netif); + } + } +} + +static void esp_netif_ip_lost_timer(void *arg) +{ + esp_netif_t *esp_netif = (esp_netif_t*)arg; + + ESP_LOGD(TAG, "%s esp_netif:%p", __func__, esp_netif); + + esp_netif->timer_running = false; + + struct netif *netif = esp_netif->lwip_netif; + + // @TODO: check if netif type is sta + + if ( (!netif) || (netif && ip4_addr_cmp(ip_2_ip4(&netif->ip_addr), IP4_ADDR_ANY4))) { + ip_event_got_ip_t evt = { + .esp_netif = esp_netif, + .if_index = -1, + }; + int ret; + + ESP_LOGD(TAG, "if%p ip lost tmr: raise ip lost event", esp_netif); + memset(&esp_netif->ip_info_old, 0, sizeof(esp_netif_ip_info_t)); + if (esp_netif->lost_ip_event) { + ret = esp_event_send_internal(IP_EVENT, esp_netif->lost_ip_event, + &evt, sizeof(evt), 0); + if (ESP_OK != ret) { + ESP_LOGE(TAG, "ip lost timer: failed to post lost ip event (%x)", ret); + } + } + } else { + ESP_LOGD(TAG, "if%p ip lost tmr: no need raise ip lost event", esp_netif); + } +} + + +static esp_err_t esp_netif_start_ip_lost_timer(esp_netif_t *esp_netif) +{ + esp_netif_ip_info_t *ip_info_old = (esp_netif_ip_info_t *)&esp_netif->ip_info; + struct netif *netif = esp_netif->lwip_netif; + + ESP_LOGD(TAG, "%s esp_netif:%p", __func__, esp_netif); + + if (esp_netif->timer_running) { + ESP_LOGD(TAG, "if%p start ip lost tmr: already started", esp_netif); + return ESP_OK; + } + + if ( netif && (CONFIG_ESP_NETIF_IP_LOST_TIMER_INTERVAL > 0) && !ip4_addr_isany_val(ip_info_old->ip)) { + esp_netif->timer_running = true; + sys_timeout(CONFIG_ESP_NETIF_IP_LOST_TIMER_INTERVAL * 1000, esp_netif_ip_lost_timer, (void *)esp_netif); + ESP_LOGD(TAG, "if%p start ip lost tmr: interval=%d", esp_netif, CONFIG_ESP_NETIF_IP_LOST_TIMER_INTERVAL); + return ESP_OK; + } + + ESP_LOGD(TAG, "if%p start ip lost tmr: no need start because netif=%p interval=%d ip=%x", + esp_netif, netif, CONFIG_ESP_NETIF_IP_LOST_TIMER_INTERVAL, ip_info_old->ip.addr); + + return ESP_OK; +} + +static esp_err_t esp_netif_dhcpc_stop_api(esp_netif_api_msg_t *msg) +{ + return esp_netif_dhcpc_stop(msg->esp_netif); +} + +esp_err_t esp_netif_dhcpc_stop(esp_netif_t *esp_netif) +{ + ESP_LOGD(TAG, "%s esp_netif:%p", __func__, esp_netif); + + ESP_NETIF_IPC_CALL(esp_netif, NULL, esp_netif_dhcpc_stop_api); + + if (esp_netif == NULL) { + ESP_LOGE(TAG, "dhcp client stop called with NULL api"); + return ESP_ERR_ESP_NETIF_INVALID_PARAMS; + } + + if (esp_netif->dhcpc_status == ESP_NETIF_DHCP_STARTED) { + struct netif *p_netif = esp_netif->lwip_netif; + + if (p_netif != NULL) { + dhcp_stop(p_netif); + esp_netif_reset_ip_info(esp_netif); + esp_netif_start_ip_lost_timer(esp_netif); + } else { + ESP_LOGD(TAG, "dhcp client if not ready"); + return ESP_ERR_ESP_NETIF_IF_NOT_READY; + } + } else if (esp_netif->dhcpc_status == ESP_NETIF_DHCP_STOPPED) { + ESP_LOGD(TAG, "dhcp client already stoped"); + return ESP_ERR_ESP_NETIF_DHCP_ALREADY_STOPPED; + } + + ESP_LOGD(TAG, "dhcp client stop successfully"); + esp_netif->dhcpc_status = ESP_NETIF_DHCP_STOPPED; + + LWIP_DHCP_IP_ADDR_ERASE(esp_netif); + + return ESP_OK; + +} + + +static esp_err_t esp_netif_dhcpc_start_api(esp_netif_api_msg_t *msg) +{ + return esp_netif_dhcpc_start(msg->esp_netif); +} + +esp_err_t esp_netif_dhcpc_start(esp_netif_t *esp_netif) +{ + ESP_LOGD(TAG, "%s esp_netif:%p", __func__, esp_netif); + + if (!esp_netif) { + return ESP_ERR_INVALID_ARG; + } + + ESP_NETIF_IPC_CALL(esp_netif, NULL, esp_netif_dhcpc_start_api); + + struct netif *p_netif = esp_netif->lwip_netif; + + esp_netif_reset_ip_info(esp_netif); + +#if LWIP_DNS + dns_clear_servers(true); +#endif + + if (p_netif != NULL) { + if (netif_is_up(p_netif)) { + ip_addr_set_zero(&p_netif->ip_addr); + ip_addr_set_zero(&p_netif->netmask); + ip_addr_set_zero(&p_netif->gw); + esp_netif_start_ip_lost_timer(esp_netif); + } else { + ESP_LOGD(TAG, "dhcp client re init"); + esp_netif->dhcpc_status = ESP_NETIF_DHCP_INIT; + return ESP_OK; + } + ESP_LOGD(TAG, "starting dhcp client"); + + if (dhcp_start(p_netif) != ERR_OK) { + ESP_LOGE(TAG, "dhcp client start failed"); + return ESP_ERR_ESP_NETIF_DHCPC_START_FAILED; + } + + dhcp_set_cb(p_netif, esp_netif_dhcpc_cb); + + esp_netif->dhcpc_status = ESP_NETIF_DHCP_STARTED; + return ESP_OK; + } else { + ESP_LOGD(TAG, "dhcp client re init"); + esp_netif->dhcpc_status = ESP_NETIF_DHCP_INIT; + return ESP_OK; + } +} + +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_DHCPC)) { + return ESP_ERR_INVALID_ARG; + } + + *status = esp_netif->dhcps_status; + return ESP_OK; +} + +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_DHCPS)) { + return ESP_ERR_INVALID_ARG; + } + + *status = esp_netif->dhcpc_status; + return ESP_OK; +} + +static esp_err_t esp_netif_dhcps_start_api(esp_netif_api_msg_t *msg) +{ + return esp_netif_dhcps_start(msg->esp_netif); +} + +esp_err_t esp_netif_dhcps_start(esp_netif_t *esp_netif) +{ + ESP_LOGD(TAG, "%s esp_netif:%p", __func__, esp_netif); + + if (!esp_netif || (!(esp_netif->flags&ESP_NETIF_DHCPS))) { + return ESP_ERR_INVALID_ARG; + } + + ESP_NETIF_IPC_CALL(esp_netif, NULL, esp_netif_dhcps_start_api); + + struct netif *p_netif = esp_netif->lwip_netif; + if (p_netif != NULL && netif_is_up(p_netif)) { + esp_netif_ip_info_t *default_ip = esp_netif->ip_info; + ip4_addr_t lwip_ip; + memcpy(&lwip_ip, &default_ip->ip, sizeof(struct ip4_addr)); + dhcps_set_new_lease_cb(esp_netif_dhcps_cb); + dhcps_start(p_netif, lwip_ip); + esp_netif->dhcps_status = ESP_NETIF_DHCP_STARTED; + ESP_LOGD(TAG, "DHCP server started successfully"); + return ESP_OK; + } else { + ESP_LOGD(TAG, "dhcp server re init"); + esp_netif->dhcps_status = ESP_NETIF_DHCP_INIT; + return ESP_OK; + } +} + +static esp_err_t esp_netif_dhcps_stop_api(esp_netif_api_msg_t *msg) +{ + return esp_netif_dhcps_stop(msg->esp_netif); +} + +esp_err_t esp_netif_dhcps_stop(esp_netif_t *esp_netif) +{ + ESP_LOGD(TAG, "%s esp_netif:%p", __func__, esp_netif); + + if (!esp_netif) { + return ESP_ERR_INVALID_ARG; + } + ESP_NETIF_IPC_CALL(esp_netif, NULL, esp_netif_dhcps_stop_api); + + struct netif *p_netif = esp_netif->lwip_netif; + if (esp_netif->dhcps_status == ESP_NETIF_DHCP_STARTED) { + if (p_netif != NULL) { + dhcps_stop(p_netif); + } else { + ESP_LOGD(TAG, "dhcp server if not ready"); + return ESP_ERR_ESP_NETIF_IF_NOT_READY; + } + } else if (esp_netif->dhcps_status == ESP_NETIF_DHCP_STOPPED) { + ESP_LOGD(TAG, "dhcp server already stoped"); + return ESP_ERR_ESP_NETIF_DHCP_ALREADY_STOPPED; + } + + ESP_LOGD(TAG, "dhcp server stop successfully"); + esp_netif->dhcps_status = ESP_NETIF_DHCP_STOPPED; + return ESP_OK; +} + +static esp_err_t esp_netif_set_hostname_api(esp_netif_api_msg_t *msg) +{ + return esp_netif_set_hostname(msg->esp_netif, msg->data); +} + +esp_err_t esp_netif_set_hostname(esp_netif_t *esp_netif, const char *hostname) +{ + ESP_LOGD(TAG, "%s esp_netif:%p hostname %s", __func__, esp_netif, hostname); + + if (!esp_netif) { + return ESP_ERR_INVALID_ARG; + } + +#if LWIP_NETIF_HOSTNAME + ESP_NETIF_IPC_CALL(esp_netif, hostname, esp_netif_set_hostname_api); + + struct netif *p_netif = esp_netif->lwip_netif; + if (esp_netif->hostname) { + free(esp_netif->hostname); + } + esp_netif->hostname = strdup(hostname); + if (esp_netif->hostname == NULL) { + return ESP_ERR_NO_MEM; + } + + if (strlen(hostname) > ESP_NETIF_HOSTNAME_MAX_SIZE) { + return ESP_ERR_ESP_NETIF_INVALID_PARAMS; + } + + if (p_netif != NULL) { + p_netif->hostname = esp_netif->hostname; + return ESP_OK; + } else { + return ESP_ERR_ESP_NETIF_IF_NOT_READY; + } +#else + return ESP_ERR_ESP_NETIF_IF_NOT_READY; +#endif +} + +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) { + return ESP_ERR_INVALID_ARG; + } + +#if LWIP_NETIF_HOSTNAME + struct netif *p_netif = esp_netif->lwip_netif; + + if (p_netif != NULL) { + *hostname = p_netif->hostname; + return ESP_OK; + } else { + return ESP_ERR_ESP_NETIF_INVALID_PARAMS; + } +#else + return ESP_ERR_ESP_NETIF_IF_NOT_READY; +#endif +} + +esp_err_t esp_netif_up_api(esp_netif_api_msg_t *msg) +{ + return esp_netif_up(msg->esp_netif); +} + +esp_err_t esp_netif_up(esp_netif_t *esp_netif) +{ + ESP_LOGD(TAG, "%s esp_netif:%p", __func__, esp_netif); + + if (!esp_netif) { + return ESP_ERR_INVALID_STATE; + } + ESP_NETIF_IPC_CALL(esp_netif, NULL, esp_netif_up_api); + + struct netif *lwip_netif = esp_netif->lwip_netif; + + /* use last obtained ip, or static ip */ + netif_set_addr(lwip_netif, (ip4_addr_t*)&esp_netif->ip_info->ip, (ip4_addr_t*)&esp_netif->ip_info->netmask, (ip4_addr_t*)&esp_netif->ip_info->gw); + netif_set_up(lwip_netif); + + esp_netif_update_default_netif(esp_netif, ESP_NETIF_STARTED); + + return ESP_OK; +} + +static esp_err_t esp_netif_down_api(esp_netif_api_msg_t *msg) +{ + return esp_netif_down(msg->esp_netif); +} + +esp_err_t esp_netif_down(esp_netif_t *esp_netif) +{ + ESP_LOGD(TAG, "%s esp_netif:%p", __func__, esp_netif); + + if (!esp_netif) { + return ESP_ERR_INVALID_STATE; + } + ESP_NETIF_IPC_CALL(esp_netif, NULL, &esp_netif_down_api); + + if (esp_netif->flags&ESP_NETIF_FLAG_AUTOUP) { + ESP_LOGE(TAG, "Interface if%p asked to go up, but configured to AUTO-UP flag (flags=%x)", esp_netif, esp_netif->flags); + return ESP_ERR_INVALID_STATE; + } + + struct netif *lwip_netif = esp_netif->lwip_netif; + + if (esp_netif->flags&ESP_NETIF_DHCPC && esp_netif->dhcpc_status == ESP_NETIF_DHCP_STARTED) { + dhcp_stop(esp_netif->lwip_netif); + + esp_netif->dhcpc_status = ESP_NETIF_DHCP_INIT; + + esp_netif_reset_ip_info(esp_netif); + } + + netif_set_addr(lwip_netif, IP4_ADDR_ANY4, IP4_ADDR_ANY4, IP4_ADDR_ANY4); + netif_set_down(lwip_netif); + + if (esp_netif->flags&ESP_NETIF_DHCPC) { + esp_netif_start_ip_lost_timer(esp_netif); + } + + esp_netif_update_default_netif(esp_netif, ESP_NETIF_STOPPED); + + return ESP_OK; +} + +bool esp_netif_is_netif_up(esp_netif_t *esp_netif) +{ + ESP_LOGV(TAG, "%s esp_netif:%p", __func__, esp_netif); + + if (esp_netif != NULL && netif_is_up(esp_netif->lwip_netif)) { + return true; + } else { + return false; + } +} + +esp_err_t esp_netif_get_old_ip_info(esp_netif_t *esp_netif, esp_netif_ip_info_t *ip_info) +{ + ESP_LOGD(TAG, "%s esp_netif:%p", __func__, esp_netif); + + if (esp_netif == NULL || ip_info == NULL) { + return ESP_ERR_INVALID_ARG; + } + memcpy(ip_info, esp_netif->ip_info_old, sizeof(esp_netif_ip_info_t)); + return ESP_OK; +} + +esp_err_t esp_netif_get_ip_info(esp_netif_t *esp_netif, esp_netif_ip_info_t *ip_info) +{ + ESP_LOGD(TAG, "%s esp_netif:%p", __func__, esp_netif); + + if (esp_netif == NULL || ip_info == NULL) { + return ESP_ERR_INVALID_ARG; + } + + struct netif *p_netif = esp_netif->lwip_netif; + + if (p_netif != NULL && netif_is_up(p_netif)) { + ip4_addr_set(&ip_info->ip, ip_2_ip4(&p_netif->ip_addr)); + ip4_addr_set(&ip_info->netmask, ip_2_ip4(&p_netif->netmask)); + ip4_addr_set(&ip_info->gw, ip_2_ip4(&p_netif->gw)); + + return ESP_OK; + } + + memcpy(ip_info, esp_netif->ip_info, sizeof(esp_netif_ip_info_t)); + + return ESP_OK; +} + + +bool esp_netif_is_valid_static_ip(esp_netif_ip_info_t *ip_info) +{ + if (!(ip4_addr_isany_val(ip_info->ip) || ip4_addr_isany_val(ip_info->netmask))) { + // let's assume valid ip_info is when none of ip and netmask is 'any' address (zeros) + return true; + } + return false; +} + +static esp_err_t esp_netif_set_ip_old_info_api(esp_netif_api_msg_t *msg) +{ + memcpy(msg->esp_netif->ip_info, msg->data, sizeof(esp_netif_ip_info_t)); + 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) +{ + ESP_LOGD(TAG, "%s esp_netif:%p", __func__, esp_netif); + + if (esp_netif == NULL || ip_info == NULL) { + return ESP_ERR_INVALID_STATE; + } + ESP_NETIF_IPC_CALL(esp_netif, ip_info, esp_netif_set_ip_old_info_api); + return ESP_OK; +} + +static esp_err_t esp_netif_set_ip_info_api(esp_netif_api_msg_t *msg) +{ + return esp_netif_set_ip_info(msg->esp_netif, msg->data); +} + +esp_err_t esp_netif_set_ip_info(esp_netif_t *esp_netif, const esp_netif_ip_info_t *ip_info) +{ + ESP_LOGD(TAG, "%s esp_netif:%p", __func__, esp_netif); + + if (esp_netif == NULL || ip_info == NULL) { + return ESP_ERR_INVALID_STATE; + } + ESP_NETIF_IPC_CALL(esp_netif, ip_info, esp_netif_set_ip_info_api); + + if (esp_netif->flags&ESP_NETIF_DHCPS) { + if (esp_netif->dhcps_status != ESP_NETIF_DHCP_STOPPED) { + return ESP_ERR_ESP_NETIF_DHCP_NOT_STOPPED; + } + } else if (esp_netif->flags&ESP_NETIF_DHCPC) { + if (esp_netif->dhcpc_status != ESP_NETIF_DHCP_STOPPED) { + return ESP_ERR_ESP_NETIF_DHCP_NOT_STOPPED; + } +#if LWIP_DNS /* don't build if not configured for use in lwipopts.h */ + dns_clear_servers(true); +#endif + } + + ip4_addr_copy(esp_netif->ip_info->ip, ip_info->ip); + ip4_addr_copy(esp_netif->ip_info->gw, ip_info->gw); + ip4_addr_copy(esp_netif->ip_info->netmask, ip_info->netmask); + + struct netif *p_netif = esp_netif->lwip_netif; + + if (p_netif != NULL && netif_is_up(p_netif)) { + netif_set_addr(p_netif, (ip4_addr_t*)&ip_info->ip, (ip4_addr_t*)&ip_info->netmask, (ip4_addr_t*)&ip_info->gw); + if (ESP_NETIF_FLAG_EVENT_IP_MODIFIED & esp_netif->flags) { + if (!(ip4_addr_isany_val(ip_info->ip) || ip4_addr_isany_val(ip_info->netmask) || ip4_addr_isany_val(ip_info->gw))) { + + ip_event_t evt_id = esp_netif->get_ip_event; + ip_event_got_ip_t evt = { .esp_netif = esp_netif, .if_index = -1, .ip_changed = false}; + int ret; + + if (memcmp(ip_info, &esp_netif->ip_info_old, sizeof(esp_netif_ip_info_t))) { + evt.ip_changed = true; + } + + memcpy(&evt.ip_info, ip_info, sizeof(esp_netif_ip_info_t)); + memcpy(&esp_netif->ip_info_old, ip_info, sizeof(esp_netif_ip_info_t)); + ret = esp_event_send_internal(IP_EVENT, evt_id, &evt, sizeof(evt), 0); + if (ESP_OK != ret) { + ESP_LOGE(TAG, "set ip info: failed to post got ip event (%x)", ret); + } + + ESP_LOGD(TAG, "if%p netif set static ip: ip changed=%d", esp_netif, evt.ip_changed); + + } + } + } + + return ESP_OK; +} + +static esp_err_t esp_netif_set_dns_info_api(esp_netif_api_msg_t *msg) +{ + esp_netif_dns_param_t *dns_param = (esp_netif_dns_param_t *)msg->data; + + return esp_netif_set_dns_info(msg->esp_netif, dns_param->dns_type, dns_param->dns_info); +} + +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) +{ + ESP_LOGD(TAG, "%s esp_netif:%p", __func__, esp_netif); + + esp_netif_dns_param_t dns_param; + + dns_param.dns_type = type; + dns_param.dns_info = dns; + + ESP_NETIF_IPC_CALL(esp_netif, &dns_param, esp_netif_set_dns_info_api); + + if (esp_netif == NULL) { + return ESP_ERR_ESP_NETIF_INVALID_PARAMS; + } + + if (!dns) { + ESP_LOGD(TAG, "set dns null dns"); + return ESP_ERR_ESP_NETIF_INVALID_PARAMS; + } + + if (ip4_addr_isany_val(dns->ip.u_addr.ip4)) { + ESP_LOGD(TAG, "set dns invalid dns"); + return ESP_ERR_ESP_NETIF_INVALID_PARAMS; + } + + ESP_LOGD(TAG, "set dns if=%p type=%d dns=%x", esp_netif, type, dns->ip.u_addr.ip4.addr); + + ip_addr_t *lwip_ip = (ip_addr_t*)&dns->ip; + lwip_ip->type = IPADDR_TYPE_V4; + + if (esp_netif->flags&ESP_NETIF_DHCPS) { + // if DHCP server configured to set DNS in dhcps API + if (type != ESP_NETIF_DNS_MAIN) { + ESP_LOGD(TAG, "set dns invalid type"); + return ESP_ERR_ESP_NETIF_INVALID_PARAMS; + } else { + dhcps_dns_setserver(lwip_ip); + } + } else { + dns_setserver(type, lwip_ip); + } + + return ESP_OK; +} + +static esp_err_t esp_netif_get_dns_info_api(esp_netif_api_msg_t *msg) +{ + esp_netif_dns_param_t *dns_param = (esp_netif_dns_param_t *)msg->data; + + return esp_netif_get_dns_info(msg->esp_netif, dns_param->dns_type, dns_param->dns_info); +} + +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) +{ + ESP_LOGD(TAG, "%s esp_netif:%p", __func__, esp_netif); + + esp_netif_dns_param_t dns_param; + + dns_param.dns_type = type; + dns_param.dns_info = dns; + + ESP_NETIF_IPC_CALL(esp_netif, &dns_param, esp_netif_get_dns_info_api); + if (!dns) { + ESP_LOGD(TAG, "get dns null dns"); + return ESP_ERR_ESP_NETIF_INVALID_PARAMS; + } + + if (esp_netif->flags&ESP_NETIF_DHCPS) { + ip4_addr_t dns_ip = dhcps_dns_getserver(); + memcpy(&dns->ip.u_addr.ip4, &dns_ip, sizeof(ip4_addr_t)); + } else { + const ip_addr_t* dns_ip = NULL; + dns_ip = dns_getserver(type); + if(dns_ip != NULL) { + memcpy(&dns->ip.u_addr, &dns_ip, sizeof(ip4_addr_t)); + } + } + + return ESP_OK; +} + +esp_err_t esp_netif_get_sta_list(const wifi_sta_list_t *wifi_sta_list, esp_netif_sta_list_t *netif_sta_list) +{ + ESP_LOGD(TAG, "%s entered", __func__); + + if ((wifi_sta_list == NULL) || (netif_sta_list == NULL)) { + return ESP_ERR_ESP_NETIF_INVALID_PARAMS; + } + + memset(netif_sta_list, 0, sizeof(esp_netif_sta_list_t)); + netif_sta_list->num = wifi_sta_list->num; + for (int i = 0; i < wifi_sta_list->num; i++) { + memcpy(netif_sta_list->sta[i].mac, wifi_sta_list->sta[i].mac, 6); + dhcp_search_ip_on_mac(netif_sta_list->sta[i].mac, (ip4_addr_t*)&netif_sta_list->sta[i].ip); + } + + return ESP_OK; +} + + +static void esp_netif_nd6_cb(struct netif *p_netif, uint8_t ip_idex) +{ + ESP_LOGD(TAG, "%s lwip-netif:%p", __func__, p_netif); + if (!p_netif) { + ESP_LOGD(TAG, "null p_netif=%p", p_netif); + return; + } + + esp_netif_ip6_info_t ip6_info; + ip6_addr_t lwip_ip6_info; + //notify event + ip_event_got_ip6_t evt = { .esp_netif = p_netif->state, .if_index = -1 }; + + ip6_addr_set(&lwip_ip6_info, ip_2_ip6(&p_netif->ip6_addr[ip_idex])); +#if LWIP_IPV6_SCOPES + memcpy(&ip6_info.ip, &lwip_ip6_info, sizeof(esp_ip6_addr_t)); +#else + memcpy(&ip6_info.ip, &lwip_ip6_info, sizeof(ip6_addr_t)); + ip6_info.ip.zone = 0; // zero out zone, as not used in lwip +#endif /* LWIP_IPV6_SCOPES */ + + memcpy(&evt.ip6_info, &ip6_info, sizeof(esp_netif_ip6_info_t)); + int ret = esp_event_send_internal(IP_EVENT, IP_EVENT_GOT_IP6, &evt, sizeof(evt), 0); + if (ESP_OK != ret) { + ESP_LOGE(TAG, "nd6 cb: failed to post IP_EVENT_GOT_IP6 (%x)", ret); + } + +} + + +static esp_err_t esp_netif_create_ip6_linklocal_api(esp_netif_api_msg_t *msg) +{ + return esp_netif_create_ip6_linklocal(msg->esp_netif); +} + +esp_err_t esp_netif_create_ip6_linklocal(esp_netif_t *esp_netif) +{ + ESP_LOGD(TAG, "%s esp-netif:%p", __func__, esp_netif); + + ESP_NETIF_IPC_CALL(esp_netif, NULL, esp_netif_create_ip6_linklocal_api); + struct netif *p_netif = esp_netif->lwip_netif; + if (p_netif != NULL && netif_is_up(p_netif)) { + netif_create_ip6_linklocal_address(p_netif, 1); + nd6_set_cb(p_netif, esp_netif_nd6_cb); + return ESP_OK; + } else { + return ESP_FAIL; + } +} + +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) { + return ESP_ERR_ESP_NETIF_INVALID_PARAMS; + } + struct netif *p_netif = esp_netif->lwip_netif; + + if (p_netif != NULL && netif_is_up(p_netif) && ip6_addr_ispreferred(netif_ip6_addr_state(p_netif, 0))) { + memcpy(if_ip6, &p_netif->ip6_addr[0], sizeof(ip6_addr_t)); + } else { + return ESP_FAIL; + } + return ESP_OK; +} + +esp_netif_flags_t esp_netif_get_flags(esp_netif_t *esp_netif) +{ + return esp_netif->flags; +} + +char *esp_netif_get_ifkey(esp_netif_t *esp_netif) +{ + return esp_netif->if_key; +} + +esp_netif_type_t esp_netif_get_type(esp_netif_t *esp_netif) +{ + return esp_netif->if_type; +} + +esp_netif_t *esp_netif_get_handle_from_ifkey(const char *if_key) +{ + esp_netif_t *esp_netif = esp_netif_next(NULL); + do { + if (esp_netif && strcmp(if_key, esp_netif->if_key)==0) { + return esp_netif; + } + } while (NULL != (esp_netif = esp_netif_next(esp_netif))); + return NULL; +} + +uint32_t esp_netif_get_event_id(esp_netif_t *esp_netif, esp_netif_ip_event_type_t event_type) +{ + switch(event_type) { + case ESP_NETIF_IP_EVENT_GOT_IP: + return esp_netif->get_ip_event; + case ESP_NETIF_IP_EVENT_LOST_IP: + return esp_netif->lost_ip_event; + } + return 0; +} + +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) +{ + void *opt_info = dhcps_option_info(opt_id, opt_len); + if (esp_netif == NULL) { + return ESP_ERR_ESP_NETIF_IF_NOT_READY; + } + + esp_netif_dhcp_status_t dhcps_status = esp_netif->dhcps_status; + if (opt_info == NULL || opt_val == NULL) { + return ESP_ERR_ESP_NETIF_INVALID_PARAMS; + } + + if (opt_op == ESP_NETIF_OP_GET) { + if (dhcps_status == ESP_NETIF_DHCP_STOPPED) { + return ESP_ERR_ESP_NETIF_DHCP_ALREADY_STOPPED; + } + + switch (opt_id) { + case IP_ADDRESS_LEASE_TIME: { + *(uint32_t *)opt_val = *(uint32_t *)opt_info; + break; + } + case REQUESTED_IP_ADDRESS: { + memcpy(opt_val, opt_info, opt_len); + break; + } + case ROUTER_SOLICITATION_ADDRESS: { + if ((*(uint8_t *)opt_info) & OFFER_ROUTER) { + *(uint8_t *)opt_val = 1; + } else { + *(uint8_t *)opt_val = 0; + } + break; + } + case DOMAIN_NAME_SERVER: { + if ((*(uint8_t *)opt_info) & OFFER_DNS) { + *(uint8_t *)opt_val = 1; + } else { + *(uint8_t *)opt_val = 0; + } + break; + } + default: + break; + } + } else if (opt_op == ESP_NETIF_OP_SET) { + if (dhcps_status == ESP_NETIF_DHCP_STARTED) { + return ESP_ERR_ESP_NETIF_DHCP_ALREADY_STARTED; + } + + switch (opt_id) { + case IP_ADDRESS_LEASE_TIME: { + if (*(uint32_t *)opt_val != 0) { + *(uint32_t *)opt_info = *(uint32_t *)opt_val; + } else { + *(uint32_t *)opt_info = DHCPS_LEASE_TIME_DEF; + } + break; + } + case REQUESTED_IP_ADDRESS: { + esp_netif_ip_info_t info; + uint32_t softap_ip = 0; + uint32_t start_ip = 0; + uint32_t end_ip = 0; + dhcps_lease_t *poll = opt_val; + + if (poll->enable) { + memset(&info, 0x00, sizeof(esp_netif_ip_info_t)); + esp_netif_get_ip_info(esp_netif, &info); + + softap_ip = htonl(info.ip.addr); + start_ip = htonl(poll->start_ip.addr); + end_ip = htonl(poll->end_ip.addr); + + /*config ip information can't contain local ip*/ + if ((start_ip <= softap_ip) && (softap_ip <= end_ip)) { + return ESP_ERR_ESP_NETIF_INVALID_PARAMS; + } + + /*config ip information must be in the same segment as the local ip*/ + softap_ip >>= 8; + if ((start_ip >> 8 != softap_ip) + || (end_ip >> 8 != softap_ip)) { + return ESP_ERR_ESP_NETIF_INVALID_PARAMS; + } + + if (end_ip - start_ip > DHCPS_MAX_LEASE) { + return ESP_ERR_ESP_NETIF_INVALID_PARAMS; + } + } + + memcpy(opt_info, opt_val, opt_len); + break; + } + case ROUTER_SOLICITATION_ADDRESS: { + if (*(uint8_t *)opt_val) { + *(uint8_t *)opt_info |= OFFER_ROUTER; + } else { + *(uint8_t *)opt_info &= ((~OFFER_ROUTER) & 0xFF); + } + break; + } + case DOMAIN_NAME_SERVER: { + if (*(uint8_t *)opt_val) { + *(uint8_t *)opt_info |= OFFER_DNS; + } else { + *(uint8_t *)opt_info &= ((~OFFER_DNS) & 0xFF); + } + break; + } + + default: + break; + } + dhcps_set_option_info(opt_id, opt_info, opt_len); + } else { + return ESP_ERR_ESP_NETIF_INVALID_PARAMS; + } + + return ESP_OK; +} + +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) +{ + // TODO: when dhcp request timeout,change the retry count + return ESP_ERR_NOT_SUPPORTED; +} + +int esp_netif_get_netif_index(esp_netif_t *esp_netif) +{ + if (esp_netif == NULL || esp_netif->lwip_netif == NULL) { + return -1; + } + return netif_get_index(esp_netif->lwip_netif); +} + +#endif /* CONFIG_TCPIP_LWIP */ diff --git a/components/esp_netif/lwip/esp_netif_lwip_defaults.c b/components/esp_netif/lwip/esp_netif_lwip_defaults.c new file mode 100644 index 0000000000..6fce8067ac --- /dev/null +++ b/components/esp_netif/lwip/esp_netif_lwip_defaults.c @@ -0,0 +1,44 @@ +// Copyright 2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "esp_netif.h" +#include "esp_netif_lwip_internal.h" + +#include "netif/wlanif.h" +#include "netif/ethernetif.h" + +// +// Purpose of this object is to define default network stack configuration +// of basic types of interfaces using lwip network stack +// + +static const struct esp_netif_netstack_config s_eth_netif_config = { + .base = { .type = ESP_NETIF_NETWORK_STACK_IS_LWIP }, + .init_fn = ethernetif_init, + .input_fn = ethernetif_input +}; +static const struct esp_netif_netstack_config s_wifi_netif_config_ap = { + .base = { .type = ESP_NETIF_NETWORK_STACK_IS_LWIP }, + .init_fn = wlanif_init_ap, + .input_fn = wlanif_input +}; +static const struct esp_netif_netstack_config s_wifi_netif_config_sta = { + .base = { .type = ESP_NETIF_NETWORK_STACK_IS_LWIP }, + .init_fn = wlanif_init_sta, + .input_fn = wlanif_input +}; + +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 diff --git a/components/esp_netif/lwip/esp_netif_lwip_internal.h b/components/esp_netif/lwip/esp_netif_lwip_internal.h new file mode 100644 index 0000000000..181c3bf17c --- /dev/null +++ b/components/esp_netif/lwip/esp_netif_lwip_internal.h @@ -0,0 +1,70 @@ +// Copyright 2015-2018 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#pragma once + +#include "esp_netif.h" +#include "lwip/netif.h" + +// LWIP netif specific network stack configuration +struct esp_netif_netstack_config { + esp_netif_netstack_base_config_t base; + err_t (*init_fn)(struct netif*); + void (*input_fn)(struct netif *netif, void *buffer, size_t len, void *eb); +}; + +struct esp_netif_api_msg_s; + +typedef int (*esp_netif_api_fn)(struct esp_netif_api_msg_s *msg); + +typedef struct esp_netif_api_msg_s { + int type; /**< The first field MUST be int */ + int ret; + esp_netif_api_fn api_fn; + esp_netif_t *esp_netif; + void *data; +} esp_netif_api_msg_t; + + +typedef struct esp_netif_dns_param_s { + esp_netif_dns_type_t dns_type; + esp_netif_dns_info_t *dns_info; +} esp_netif_dns_param_t; + +typedef struct esp_netif_ip_lost_timer_s { + bool timer_running; +} esp_netif_ip_lost_timer_t; + + +#define ESP_NETIF_TRHEAD_SAFE 1 +#define ESP_NETIF_IPC_LOCAL 0 +#define ESP_NETIF_IPC_REMOTE 1 + +#define ESP_NETIF_IPC_CALL(_if, _data, _fn) do {\ + esp_netif_api_msg_t msg;\ + if (tcpip_initialized == false) {\ + ESP_LOGE(TAG, "esp_netif is not initialized!");\ + abort();\ + }\ + memset(&msg, 0, sizeof(msg));\ + msg.esp_netif = (_if);\ + msg.data = (void*)(_data);\ + msg.api_fn = (_fn);\ + if (ESP_NETIF_IPC_REMOTE == esp_netif_ipc_check(&msg)) {\ + ESP_LOGD(TAG, "check: remote, if=%p fn=%p\n", (_if), (_fn));\ + return msg.ret;\ + } else {\ + ESP_LOGD(TAG, "check: local, if=%p fn=%p\n", (_if), (_fn));\ + }\ +} while(0) diff --git a/components/esp_netif/private_include/esp_netif_private.h b/components/esp_netif/private_include/esp_netif_private.h new file mode 100644 index 0000000000..0519c397d3 --- /dev/null +++ b/components/esp_netif/private_include/esp_netif_private.h @@ -0,0 +1,114 @@ +// Copyright 2015-2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef _ESP_NETIF_PRIVATE_H_ +#define _ESP_NETIF_PRIVATE_H_ + +#define ESP_NETIF_CALL_CHECK(info, api_call, ret) \ +do{\ + esp_err_t __err = (api_call);\ + if ((ret) != __err) {\ + ESP_LOGE(TAG, "%s %d %s ret=0x%X", __FUNCTION__, __LINE__, (info), __err);\ + return;\ + }\ +} while(0) + +/** + * @brief Cause the TCP/IP stack to start the ESP-NETIF instance interface + * + * @param[in] esp_netif Handle to esp-netif instance + * + * @return + * - ESP_OK + * - ESP_ERR_ESP_NETIF_INVALID_PARAMS + * - ESP_ERR_NO_MEM + */ +esp_err_t esp_netif_start(esp_netif_t *esp_netif); + +/** + * @brief Cause the TCP/IP stack to stop a network interface defined as ESP-NETIF instance + * + * Causes TCP/IP stack to clean up this interface. This includes stopping the DHCP server or client, if they are started. + * + * @note To stop an interface from application code, the media driver-specific API (esp_wifi_stop() or esp_eth_stop()) + * shall be called, the driver layer will then send a stop event and the event handler should call this API. + * + * @param[in] esp_netif Handle to esp-netif instance + * + * @return + * - ESP_OK + * - ESP_ERR_ESP_NETIF_INVALID_PARAMS + * - ESP_ERR_ESP_NETIF_IF_NOT_READY + */ +esp_err_t esp_netif_stop(esp_netif_t *esp_netif); + +/** + * @brief Cause the TCP/IP stack to bring up an interface + * This function is called automatically by default called from event handlers/actions + * + * @note This function is not normally used with Wi-Fi AP interface. If the AP interface is started, it is up. + * + * @param[in] esp_netif Handle to esp-netif instance + * + * @return + * - ESP_OK + * - ESP_ERR_ESP_NETIF_IF_NOT_READY + */ +esp_err_t esp_netif_up(esp_netif_t *esp_netif); + +/** + * @brief Cause the TCP/IP stack to shutdown an interface + * This function is called automatically by default called from event handlers/actions + * + * @note This function is not normally used with Wi-Fi AP interface. If the AP interface is stopped, it is down. + * + * @param[in] esp_netif Handle to esp-netif instance + * + * @return + * - ESP_OK + * - ESP_ERR_ESP_NETIF_INVALID_PARAMS - parameter error + */ +esp_err_t esp_netif_down(esp_netif_t *esp_netif); + +/** + * @brief Returns true if underlying TCP/IP stack finds the ip_info as valid static address + * + * @param[in] ip_info + * @return true if address assumed to be valid static IP address + */ +bool esp_netif_is_valid_static_ip(esp_netif_ip_info_t *ip_info); + +/** + * @brief Adds created interface to the list of netifs + * + * @param[in] esp_netif Handle to esp-netif instance + * + * @return + * - ESP_OK -- Success + * - ESP_ERR_NO_MEM -- Cannot be added due to memory allocation failure + */ +esp_err_t esp_netif_add_to_list(esp_netif_t* netif); + +/** + * @brief Removes interface to be destroyed from the list of netifs + * + * @param[in] esp_netif Handle to esp-netif instance + * + * @return + * - ESP_OK -- Success + * - ESP_ERR_NOT_FOUND -- This netif was not found in the netif list + */ +esp_err_t esp_netif_remove_from_list(esp_netif_t* netif); + +#endif //_ESP_NETIF_PRIVATE_H_ diff --git a/components/esp_netif/test/CMakeLists.txt b/components/esp_netif/test/CMakeLists.txt new file mode 100644 index 0000000000..9dfde6842b --- /dev/null +++ b/components/esp_netif/test/CMakeLists.txt @@ -0,0 +1,5 @@ +set(COMPONENT_SRCDIRS ".") +set(COMPONENT_PRIV_INCLUDEDIRS "../private_include" ".") +set(COMPONENT_PRIV_REQUIRES unity test_utils esp_netif) + +register_component() \ No newline at end of file diff --git a/components/esp_netif/test/component.mk b/components/esp_netif/test/component.mk new file mode 100644 index 0000000000..22e49eddde --- /dev/null +++ b/components/esp_netif/test/component.mk @@ -0,0 +1,5 @@ +# +#Component Makefile +# +COMPONENT_PRIV_INCLUDEDIRS := ../private_include . +COMPONENT_ADD_LDFLAGS = -Wl,--whole-archive -l$(COMPONENT_NAME) -Wl,--no-whole-archive \ No newline at end of file diff --git a/components/esp_netif/test/test_esp_netif.c b/components/esp_netif/test/test_esp_netif.c new file mode 100644 index 0000000000..3eedd10c38 --- /dev/null +++ b/components/esp_netif/test/test_esp_netif.c @@ -0,0 +1,56 @@ +#include "unity.h" +#include "esp_netif.h" +#include "esp_wifi.h" + +TEST_CASE("esp_netif: init and destroy", "[esp_netif][leaks=0]") +{ + esp_netif_config_t cfg = {}; + esp_netif_t *esp_netif = esp_netif_new(NULL); + + TEST_ASSERT_EQUAL(ESP_ERR_ESP_NETIF_INVALID_PARAMS, esp_netif_configure(esp_netif, NULL)); + TEST_ASSERT_EQUAL(ESP_OK, esp_netif_configure(esp_netif, &cfg)); + + esp_netif_destroy(esp_netif); +} + + +TEST_CASE("esp_netif: get from if_key", "[esp_netif][leaks=0]") +{ + // init default netif + esp_netif_config_t cfg = ESP_NETIF_DEFAULT_WIFI_STA(); + esp_netif_t *esp_netif = esp_netif_new(&cfg); + TEST_ASSERT_NOT_NULL(esp_netif); + + // check it's accessible by key + TEST_ASSERT_EQUAL(esp_netif, esp_netif_get_handle_from_ifkey("WIFI_STA_DEF")); + + // destroy it + esp_netif_destroy(esp_netif); + + // check it's also destroyed in list + TEST_ASSERT_EQUAL(NULL, esp_netif_get_handle_from_ifkey("WIFI_STA_DEF")); + +} + + +TEST_CASE("esp_netif: create and delete multiple netifs", "[esp_netif][leaks=0]") +{ + const int nr_of_netifs = 10; + esp_netif_t *netifs[nr_of_netifs]; + esp_netif_config_t cfg = ESP_NETIF_DEFAULT_WIFI_STA(); + + // create 10 wifi stations + for (int i=0; i CONFIG_ESP32_WIFI_DYNAMIC_RX_BUFFER_NUM) #error "WiFi configuration check: WARNING, WIFI_RX_BA_WIN should not be larger than WIFI_DYNAMIC_RX_BUFFER_NUM!" @@ -124,10 +126,12 @@ esp_err_t esp_wifi_init(const wifi_init_config_t *config) } } #endif +#if CONFIG_ESP_NETIF_USE_TCPIP_ADAPTER_COMPATIBLE_LAYER esp_err_t err = tcpip_adapter_set_default_wifi_handlers(); if (err != ESP_OK) { ESP_LOGW(TAG, "Failed to set default Wi-Fi event handlers (0x%x)", err); } +#endif esp_err_t result = esp_wifi_init_internal(config); if (result == ESP_OK) { esp_wifi_set_debug_log(); diff --git a/components/lwip/CMakeLists.txt b/components/lwip/CMakeLists.txt index 56187ca638..632a00e57d 100644 --- a/components/lwip/CMakeLists.txt +++ b/components/lwip/CMakeLists.txt @@ -89,7 +89,6 @@ set(srcs "port/esp32/debug/lwip_debug.c" "port/esp32/freertos/sys_arch.c" "port/esp32/netif/dhcp_state.c" - "port/esp32/netif/nettestif.c" "port/esp32/netif/wlanif.c") if(CONFIG_LWIP_PPP_SUPPORT) @@ -134,7 +133,7 @@ idf_component_register(SRCS "${srcs}" INCLUDE_DIRS "${include_dirs}" LDFRAGMENTS linker.lf REQUIRES vfs esp_wifi - PRIV_REQUIRES esp_eth tcpip_adapter nvs_flash) + PRIV_REQUIRES esp_eth esp_netif tcpip_adapter nvs_flash) # lots of LWIP source files evaluate macros that check address of stack variables target_compile_options(${COMPONENT_LIB} PRIVATE -Wno-address) diff --git a/components/lwip/apps/dhcpserver/dhcpserver.c b/components/lwip/apps/dhcpserver/dhcpserver.c index 48adc3b98d..5052b67414 100644 --- a/components/lwip/apps/dhcpserver/dhcpserver.c +++ b/components/lwip/apps/dhcpserver/dhcpserver.c @@ -20,7 +20,7 @@ #include "lwip/udp.h" #include "lwip/mem.h" #include "lwip/ip_addr.h" -#include "tcpip_adapter.h" +#include "esp_netif.h" #include "dhcpserver/dhcpserver.h" #include "dhcpserver/dhcpserver_options.h" @@ -81,7 +81,7 @@ typedef struct _list_node { static const u32_t magic_cookie = 0x63538263; -static struct udp_pcb *pcb_dhcps = NULL; +static struct netif *dhcps_netif = NULL; static ip4_addr_t broadcast_dhcps; static ip4_addr_t server_address; static ip4_addr_t dns_server = {0}; @@ -325,19 +325,19 @@ static u8_t *add_offer_options(u8_t *optptr) *optptr++ = ip4_addr4(&ipadd); if (dhcps_router_enabled(dhcps_offer)) { - tcpip_adapter_ip_info_t if_ip; - //bzero(&if_ip, sizeof(struct ip_info)); - memset(&if_ip , 0x00, sizeof(tcpip_adapter_ip_info_t)); + esp_netif_ip_info_t if_ip; + memset(&if_ip , 0x00, sizeof(esp_netif_ip_info_t)); + esp_netif_get_ip_info(dhcps_netif->state, &if_ip); - tcpip_adapter_get_ip_info(ESP_IF_WIFI_AP, &if_ip); + ip4_addr_t* gw_ip = (ip4_addr_t*)&if_ip.gw; - if (!ip4_addr_isany_val(if_ip.gw)) { + if (!ip4_addr_isany_val(*gw_ip)) { *optptr++ = DHCP_OPTION_ROUTER; *optptr++ = 4; - *optptr++ = ip4_addr1(&if_ip.gw); - *optptr++ = ip4_addr2(&if_ip.gw); - *optptr++ = ip4_addr3(&if_ip.gw); - *optptr++ = ip4_addr4(&if_ip.gw); + *optptr++ = ip4_addr1(gw_ip); + *optptr++ = ip4_addr2(gw_ip); + *optptr++ = ip4_addr3(gw_ip); + *optptr++ = ip4_addr4(gw_ip); } } @@ -525,6 +525,7 @@ static void send_offer(struct dhcps_msg *m, u16_t len) ip_addr_t ip_temp = IPADDR4_INIT(0x0); ip4_addr_set(ip_2_ip4(&ip_temp), &broadcast_dhcps); + struct udp_pcb *pcb_dhcps = dhcps_netif->dhcps_pcb; #if DHCPS_DEBUG SendOffer_err_t = udp_sendto(pcb_dhcps, p, &ip_temp, DHCPS_CLIENT_PORT); DHCPS_LOG("dhcps: send_offer>>udp_sendto result %x\n", SendOffer_err_t); @@ -602,6 +603,7 @@ static void send_nak(struct dhcps_msg *m, u16_t len) ip_addr_t ip_temp = IPADDR4_INIT(0x0); ip4_addr_set(ip_2_ip4(&ip_temp), &broadcast_dhcps); + struct udp_pcb *pcb_dhcps = dhcps_netif->dhcps_pcb; #if DHCPS_DEBUG SendNak_err_t = udp_sendto(pcb_dhcps, p, &ip_temp, DHCPS_CLIENT_PORT); DHCPS_LOG("dhcps: send_nak>>udp_sendto result %x\n", SendNak_err_t); @@ -678,6 +680,7 @@ static void send_ack(struct dhcps_msg *m, u16_t len) ip_addr_t ip_temp = IPADDR4_INIT(0x0); ip4_addr_set(ip_2_ip4(&ip_temp), &broadcast_dhcps); + struct udp_pcb *pcb_dhcps = dhcps_netif->dhcps_pcb; SendAck_err_t = udp_sendto(pcb_dhcps, p, &ip_temp, DHCPS_CLIENT_PORT); #if DHCPS_DEBUG DHCPS_LOG("dhcps: send_ack>>udp_sendto result %x\n", SendAck_err_t); @@ -1135,19 +1138,20 @@ void dhcps_set_new_lease_cb(dhcps_cb_t cb) *******************************************************************************/ void dhcps_start(struct netif *netif, ip4_addr_t ip) { - struct netif *apnetif = netif; + dhcps_netif = netif; - if (apnetif->dhcps_pcb != NULL) { - udp_remove(apnetif->dhcps_pcb); + if (dhcps_netif->dhcps_pcb != NULL) { + udp_remove(dhcps_netif->dhcps_pcb); } - pcb_dhcps = udp_new(); + dhcps_netif->dhcps_pcb = udp_new(); + struct udp_pcb *pcb_dhcps = dhcps_netif->dhcps_pcb; if (pcb_dhcps == NULL || ip4_addr_isany_val(ip)) { printf("dhcps_start(): could not obtain pcb\n"); } - apnetif->dhcps_pcb = pcb_dhcps; + dhcps_netif->dhcps_pcb = pcb_dhcps; IP4_ADDR(&broadcast_dhcps, 255, 255, 255, 255); @@ -1326,5 +1330,5 @@ dhcps_dns_getserver(void) { return dns_server; } -#endif +#endif // ESP_DHCP diff --git a/components/lwip/lwip b/components/lwip/lwip index f2bd195eed..c483f30ba2 160000 --- a/components/lwip/lwip +++ b/components/lwip/lwip @@ -1 +1 @@ -Subproject commit f2bd195eed8f0d6e5cb784bccdba6c9b2e487e75 +Subproject commit c483f30ba22ac3c56c113f2466c39e7d6f60ab89 diff --git a/components/lwip/port/esp32/include/lwipopts.h b/components/lwip/port/esp32/include/lwipopts.h index 89e1b32768..19be43367f 100644 --- a/components/lwip/port/esp32/include/lwipopts.h +++ b/components/lwip/port/esp32/include/lwipopts.h @@ -231,7 +231,7 @@ #define LWIP_DHCP_IP_ADDR_RESTORE() dhcp_ip_addr_restore(netif) #define LWIP_DHCP_IP_ADDR_STORE() dhcp_ip_addr_store(netif) -#define LWIP_DHCP_IP_ADDR_ERASE() dhcp_ip_addr_erase(esp_netif[tcpip_if]) +#define LWIP_DHCP_IP_ADDR_ERASE(esp_netif) dhcp_ip_addr_erase(esp_netif) #endif diff --git a/components/lwip/port/esp32/include/netif/dhcp_state.h b/components/lwip/port/esp32/include/netif/dhcp_state.h index ffea116401..b03dabbe74 100644 --- a/components/lwip/port/esp32/include/netif/dhcp_state.h +++ b/components/lwip/port/esp32/include/netif/dhcp_state.h @@ -24,7 +24,7 @@ bool dhcp_ip_addr_restore(void *netif); void dhcp_ip_addr_store(void *netif); -void dhcp_ip_addr_erase(void *netif); +void dhcp_ip_addr_erase(void *esp_netif); #ifdef __cplusplus } diff --git a/components/lwip/port/esp32/include/netif/ethernetif.h b/components/lwip/port/esp32/include/netif/ethernetif.h index 134e8eb5fe..9abd1e4ee9 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, u16_t len); +void ethernetif_input(struct netif *netif, void *buffer, size_t len, void *eb); void netif_reg_addr_change_cb(void* cb); diff --git a/components/lwip/port/esp32/include/netif/nettestif.h b/components/lwip/port/esp32/include/netif/nettestif.h deleted file mode 100644 index aa76ed8079..0000000000 --- a/components/lwip/port/esp32/include/netif/nettestif.h +++ /dev/null @@ -1,33 +0,0 @@ -// Copyright 2015-2019 Espressif Systems (Shanghai) PTE LTD -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at - -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - - -#ifndef _NETTEST_LWIP_IF_H_ -#define _NETTEST_LWIP_IF_H_ - -#include "lwip/err.h" - -#ifdef __cplusplus -extern "C" { -#endif - -err_t nettestif_init(struct netif *netif); - -void nettestif_input(void *buffer, u16_t len); - -#ifdef __cplusplus -} -#endif - -#endif /* _NETTEST_LWIP_IF_H_ */ \ No newline at end of file diff --git a/components/lwip/port/esp32/include/netif/wlanif.h b/components/lwip/port/esp32/include/netif/wlanif.h index 21fa91c345..d3e39e2ab2 100644 --- a/components/lwip/port/esp32/include/netif/wlanif.h +++ b/components/lwip/port/esp32/include/netif/wlanif.h @@ -18,8 +18,6 @@ #include "esp_wifi.h" -#include "esp_private/wifi.h" - #include "lwip/err.h" #ifdef __cplusplus @@ -29,7 +27,8 @@ 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, u16_t len, void* eb); +void wlanif_input(struct netif *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/dhcp_state.c b/components/lwip/port/esp32/netif/dhcp_state.c index 8e2e3d9f38..a442e12a0b 100644 --- a/components/lwip/port/esp32/netif/dhcp_state.c +++ b/components/lwip/port/esp32/netif/dhcp_state.c @@ -19,17 +19,14 @@ #include "lwip/dhcp.h" #include "lwip/netif.h" #include "esp_interface.h" -#include "tcpip_adapter.h" +#include "esp_netif.h" +#include "esp_netif_net_stack.h" #include "netif/dhcp_state.h" #define DHCP_NAMESPACE "dhcp_state" -#define VALID_NETIF_ID(id) ((id < ESP_IF_MAX) && (id != ESP_IF_WIFI_AP)) -static uint32_t restored_ip_addr[TCPIP_ADAPTER_IF_MAX]; -static const char *interface_key[] = {"IF_STA", "IF_AP", "IF_ETH", "IF_TEST"}; - -_Static_assert(sizeof(interface_key) / sizeof(char*) == TCPIP_ADAPTER_IF_MAX, - "Number interface keys differs from number of interfaces"); +// DHCP_Client has to be enabled for this netif +#define VALID_NETIF_ID(netif) (ESP_NETIF_DHCPC&esp_netif_get_flags(netif)) bool dhcp_ip_addr_restore(void *netif) { @@ -37,13 +34,12 @@ bool dhcp_ip_addr_restore(void *netif) bool err = false; struct netif *net = (struct netif *)netif; struct dhcp *dhcp = netif_dhcp_data(net); - esp_interface_t netif_id = tcpip_adapter_get_esp_if(net); + esp_netif_t *esp_netif = esp_netif_get_handle_from_netif_impl(netif); - if(VALID_NETIF_ID(netif_id)) { + if(VALID_NETIF_ID(esp_netif)) { uint32_t *ip_addr = &dhcp->offered_ip_addr.addr; if (nvs_open(DHCP_NAMESPACE, NVS_READONLY, &nvs) == ESP_OK) { - if (nvs_get_u32(nvs, interface_key[netif_id], ip_addr) == ESP_OK) { - restored_ip_addr[netif_id] = *ip_addr; + if (nvs_get_u32(nvs, esp_netif_get_ifkey(esp_netif), ip_addr) == ESP_OK) { err = true; } nvs_close(nvs); @@ -58,28 +54,24 @@ void dhcp_ip_addr_store(void *netif) struct netif *net = (struct netif *)netif; struct dhcp *dhcp = netif_dhcp_data(net); uint32_t ip_addr = dhcp->offered_ip_addr.addr; - esp_interface_t netif_id = tcpip_adapter_get_esp_if(net); + esp_netif_t *esp_netif = esp_netif_get_handle_from_netif_impl(netif); - if(VALID_NETIF_ID(netif_id)) { - if (restored_ip_addr[netif_id] != ip_addr) { - if (nvs_open(DHCP_NAMESPACE, NVS_READWRITE, &nvs) == ESP_OK) { - nvs_set_u32(nvs, interface_key[netif_id], ip_addr); - nvs_commit(nvs); - nvs_close(nvs); - } + if(VALID_NETIF_ID(esp_netif)) { + if (nvs_open(DHCP_NAMESPACE, NVS_READWRITE, &nvs) == ESP_OK) { + nvs_set_u32(nvs,esp_netif_get_ifkey(esp_netif), ip_addr); + nvs_commit(nvs); + nvs_close(nvs); } } } -void dhcp_ip_addr_erase(void *netif) +void dhcp_ip_addr_erase(void *esp_netif) { nvs_handle_t nvs; - struct netif *net = (struct netif *)netif; - esp_interface_t netif_id = tcpip_adapter_get_esp_if(net); - if(VALID_NETIF_ID(netif_id)) { + if(VALID_NETIF_ID(esp_netif)) { if (nvs_open(DHCP_NAMESPACE, NVS_READWRITE, &nvs) == ESP_OK) { - nvs_erase_key(nvs, interface_key[netif_id]); + nvs_erase_key(nvs, esp_netif_get_ifkey(esp_netif)); nvs_commit(nvs); nvs_close(nvs); } diff --git a/components/lwip/port/esp32/netif/ethernetif.c b/components/lwip/port/esp32/netif/ethernetif.c index fb758b902e..eac9823809 100644 --- a/components/lwip/port/esp32/netif/ethernetif.c +++ b/components/lwip/port/esp32/netif/ethernetif.c @@ -49,7 +49,8 @@ #include #include "esp_eth.h" -#include "tcpip_adapter.h" +#include "esp_netif.h" +#include "esp_netif_net_stack.h" /* Define those to better describe your network interface. */ #define IFNAME0 'e' @@ -61,7 +62,7 @@ * @param buf memory alloc in L2 layer * @note this function is also the callback when invoke pbuf_free */ -static void ethernet_free_rx_buf_l2(void *buf) +static void ethernet_free_rx_buf_l2(struct netif *netif, void *buf) { free(buf); } @@ -105,17 +106,15 @@ static void ethernet_low_level_init(struct netif *netif) static err_t ethernet_low_level_output(struct netif *netif, struct pbuf *p) { struct pbuf *q = p; - esp_interface_t eth_if = tcpip_adapter_get_esp_if(netif); + esp_netif_t *esp_netif = esp_netif_get_handle_from_netif_impl(netif); esp_err_t ret = ESP_FAIL; - esp_eth_handle_t eth_handle = (esp_eth_handle_t)netif->state; - - if (eth_if != ESP_IF_ETH) { - LWIP_DEBUGF(NETIF_DEBUG, ("eth_if=%d netif=%p pbuf=%p len=%d\n", eth_if, netif, p, p->len)); + if (!esp_netif) { + LWIP_DEBUGF(NETIF_DEBUG, ("corresponding esp-netif is NULL: netif=%p pbuf=%p len=%d\n", netif, p, p->len)); return ERR_IF; } if (q->next == NULL) { - ret = esp_eth_transmit(eth_handle, q->payload, q->len); + ret = esp_netif_transmit(esp_netif, q->payload, q->len); } else { LWIP_DEBUGF(PBUF_DEBUG, ("low_level_output: pbuf is a list, application may has bug")); q = pbuf_alloc(PBUF_RAW_TX, p->tot_len, PBUF_RAM); @@ -129,7 +128,7 @@ static err_t ethernet_low_level_output(struct netif *netif, struct pbuf *p) } else { return ERR_MEM; } - ret = esp_eth_transmit(eth_handle, q->payload, q->len); + ret = esp_netif_transmit(esp_netif, q->payload, q->len); /* content in payload has been copied to DMA buffer, it's safe to free pbuf now */ pbuf_free(q); } @@ -152,13 +151,13 @@ 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, uint16_t len) +void ethernetif_input(struct netif *netif, void *buffer, size_t len, void *eb) { struct pbuf *p; if (buffer == NULL || !netif_is_up(netif)) { if (buffer) { - ethernet_free_rx_buf_l2(buffer); + ethernet_free_rx_buf_l2(netif, buffer); } return; } @@ -166,7 +165,7 @@ void ethernetif_input(struct netif *netif, void *buffer, uint16_t len) /* acquire new pbuf, type: PBUF_REF */ p = pbuf_alloc(PBUF_RAW, len, PBUF_REF); if (p == NULL) { - ethernet_free_rx_buf_l2(buffer); + ethernet_free_rx_buf_l2(netif, buffer); return; } p->payload = buffer; @@ -194,7 +193,8 @@ void ethernetif_input(struct netif *netif, void *buffer, uint16_t len) err_t ethernetif_init(struct netif *netif) { LWIP_ASSERT("netif != NULL", (netif != NULL)); - esp_eth_handle_t eth_handle = (esp_eth_handle_t)netif->state; + /* Have to get the esp-netif handle from netif first and then driver==ethernet handle from there */ + esp_eth_handle_t eth_handle = esp_netif_get_io_driver(esp_netif_get_handle_from_netif_impl(netif)); /* Initialize interface hostname */ #if LWIP_NETIF_HOSTNAME #if ESP_LWIP @@ -207,6 +207,7 @@ err_t ethernetif_init(struct netif *netif) /* Initialize the snmp variables and counters inside the struct netif. */ eth_speed_t speed; + esp_eth_ioctl(eth_handle, ETH_CMD_G_SPEED, &speed); if (speed == ETH_SPEED_100M) { NETIF_INIT_SNMP(netif, snmp_ifType_ethernet_csmacd, 100000000); diff --git a/components/lwip/port/esp32/netif/nettestif.c b/components/lwip/port/esp32/netif/nettestif.c deleted file mode 100644 index 4422c21952..0000000000 --- a/components/lwip/port/esp32/netif/nettestif.c +++ /dev/null @@ -1,105 +0,0 @@ - -#include "lwip/opt.h" - -#include "lwip/def.h" -#include "lwip/mem.h" -#include "lwip/pbuf.h" -#include "lwip/stats.h" -#include "lwip/snmp.h" -#include "lwip/ethip6.h" -#include "netif/etharp.h" -#include "netif/wlanif.h" - -#include -#include - -#include "tcpip_adapter.h" - -static struct netif *g_last_netif = NULL; - - -err_t nettestif_output(struct netif *netif, struct pbuf *p) -{ - int i; - char *dat = p->payload; - - /* output the packet to stdout */ - printf("\nPacketOut:["); - for (i=0; ilen; i++) { - printf("%02x", *dat++); - } - printf("]\n"); - - return ERR_OK; -} - - -err_t nettestif_init(struct netif *netif) -{ - - g_last_netif = netif; - - netif->hostname = CONFIG_LWIP_LOCAL_HOSTNAME; - - /* - * Initialize the snmp variables and counters inside the struct netif. - * The last argument should be replaced with your link speed, in units - * of bits per second. - */ - NETIF_INIT_SNMP(netif, snmp_ifType_ethernet_csmacd, 100); - - /* We directly use etharp_output() here to save a function call. - * You can instead declare your own function an call etharp_output() - * from it if you have to do some checks before sending (e.g. if link - * is available...) */ - netif->output = etharp_output; -#if LWIP_IPV6 - netif->output_ip6 = ethip6_output; -#endif /* LWIP_IPV6 */ - netif->linkoutput = nettestif_output; - /* set MAC hardware address length */ - netif->hwaddr_len = ETHARP_HWADDR_LEN; - - /* set MAC hardware address */ - - /* maximum transfer unit */ - netif->mtu = 1500; - - /* device capabilities */ - /* don't set NETIF_FLAG_ETHARP if this device is not an ethernet one */ - netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP; - -#if ESP_LWIP -#if LWIP_IGMP - netif->flags |= NETIF_FLAG_IGMP; -#endif -#endif - return ERR_OK; - -} - -void nettestif_input(void *buffer, u16_t len) -{ - struct pbuf *p; - if (g_last_netif == NULL) { - printf("error!"); - return; - } - - printf("simul in: %d\n", len); - if (len==0) return; - - p = pbuf_alloc(PBUF_RAW, len, PBUF_RAM); - p->l2_owner = NULL; - memcpy(p->payload, buffer, len); - - /* full packet send to tcpip_thread to process - * on success - the packet is processed and deallocated in tcpip stack - * on failure - log error and deallocate the packet - */ - if (g_last_netif->input(p, g_last_netif) != ERR_OK) { - LWIP_DEBUGF(NETIF_DEBUG, ("ethernetif_input: IP input error\n")); - pbuf_free(p); - } - -} diff --git a/components/lwip/port/esp32/netif/wlanif.c b/components/lwip/port/esp32/netif/wlanif.c index 11e1fc857f..e4df53a0a1 100644 --- a/components/lwip/port/esp32/netif/wlanif.c +++ b/components/lwip/port/esp32/netif/wlanif.c @@ -50,8 +50,20 @@ #include #include -#include "tcpip_adapter.h" +#include "esp_netif.h" +#include "esp_netif_net_stack.h" +/** + * @brief Free resources allocated in L2 layer + * + * @param buf memory alloc in L2 layer + * @note this function is also the callback when invoke pbuf_free + */ +static void lwip_netif_wifi_free_rx_buffer(struct netif *netif, void *buf) +{ + esp_netif_t *esp_netif = esp_netif_get_handle_from_netif_impl(netif); + esp_netif_free_rx_buffer(esp_netif, buf); +} /** * In this function, the hardware should be initialized. @@ -82,7 +94,7 @@ low_level_init(struct netif *netif) #endif #if !ESP_L2_TO_L3_COPY - netif->l2_buffer_free_notify = esp_wifi_internal_free_rx_buffer; + netif->l2_buffer_free_notify = lwip_netif_wifi_free_rx_buffer; #endif } @@ -104,16 +116,21 @@ low_level_init(struct netif *netif) static err_t ESP_IRAM_ATTR low_level_output(struct netif *netif, struct pbuf *p) { - wifi_interface_t wifi_if = tcpip_adapter_get_esp_if(netif); - struct pbuf *q = p; - err_t ret; - - if (wifi_if >= ESP_IF_MAX) { + esp_netif_t *esp_netif = esp_netif_get_handle_from_netif_impl(netif); + if (esp_netif == NULL) { + return ERR_IF; + } + esp_netif_type_t type = esp_netif_get_type(esp_netif); + if (type != ESP_NETIF_TYPE_STA && type != ESP_NETIF_TYPE_AP) { return ERR_IF; } + struct pbuf *q = p; + err_t ret; + if(q->next == NULL) { - ret = esp_wifi_internal_tx(wifi_if, q->payload, q->len); + ret = esp_netif_transmit(esp_netif, q->payload, q->len); + } else { LWIP_DEBUGF(PBUF_DEBUG, ("low_level_output: pbuf is a list, application may has bug")); q = pbuf_alloc(PBUF_RAW_TX, p->tot_len, PBUF_RAM); @@ -123,7 +140,8 @@ low_level_output(struct netif *netif, struct pbuf *p) } else { return ERR_MEM; } - ret = esp_wifi_internal_tx(wifi_if, q->payload, q->len); + ret = esp_netif_transmit(esp_netif, q->payload, q->len); + pbuf_free(q); } @@ -140,13 +158,14 @@ 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, u16_t len, void* eb) +wlanif_input(struct netif *netif, void *buffer, size_t len, void* eb) { + esp_netif_t *esp_netif = esp_netif_get_handle_from_netif_impl(netif); struct pbuf *p; if(!buffer || !netif_is_up(netif)) { if (eb) { - esp_wifi_internal_free_rx_buffer(eb); + esp_netif_free_rx_buffer(esp_netif, eb); } return; } @@ -154,16 +173,18 @@ wlanif_input(struct netif *netif, void *buffer, u16_t len, void* eb) #if (ESP_L2_TO_L3_COPY == 1) p = pbuf_alloc(PBUF_RAW, len, PBUF_RAM); if (p == NULL) { - esp_wifi_internal_free_rx_buffer(eb); +// esp_wifi_internal_free_rx_buffer(eb); + esp_netif_free_rx_buffer(esp_netif, eb); return; } p->l2_owner = NULL; memcpy(p->payload, buffer, len); - esp_wifi_internal_free_rx_buffer(eb); + esp_netif_free_rx_buffer(esp_netif, eb); +// esp_wifi_internal_free_rx_buffer(eb); #else p = pbuf_alloc(PBUF_RAW, len, PBUF_REF); if (p == NULL){ - esp_wifi_internal_free_rx_buffer(eb); + esp_netif_free_rx_buffer(esp_netif, eb); return; } p->payload = buffer; diff --git a/components/mdns/mdns.c b/components/mdns/mdns.c index 8ee6b5d91e..31d0a96ce8 100644 --- a/components/mdns/mdns.c +++ b/components/mdns/mdns.c @@ -4161,7 +4161,7 @@ static void event_handler(void* arg, esp_event_base_t event_base, action->data.sys_event.event_id = event_id; if (event_base == IP_EVENT && event_id == IP_EVENT_GOT_IP6) { ip_event_got_ip6_t* event = (ip_event_got_ip6_t*) event_data; - action->data.sys_event.interface = event->if_index; + action->data.sys_event.interface = tcpip_adapter_if_from_esp_netif(event->esp_netif); } if (xQueueSend(_mdns_server->action_queue, &action, (portTickType)0) != pdPASS) { diff --git a/components/tcpip_adapter/CMakeLists.txt b/components/tcpip_adapter/CMakeLists.txt index a63bcdddeb..6fb448e82a 100644 --- a/components/tcpip_adapter/CMakeLists.txt +++ b/components/tcpip_adapter/CMakeLists.txt @@ -1,3 +1,3 @@ -idf_component_register(SRCS "event_handlers.c" "tcpip_adapter_lwip.c" +idf_component_register(SRCS "tcpip_adapter_compat.c" INCLUDE_DIRS include - REQUIRES lwip esp_eth) + REQUIRES esp_netif) diff --git a/components/tcpip_adapter/event_handlers.c b/components/tcpip_adapter/event_handlers.c deleted file mode 100644 index 146f872036..0000000000 --- a/components/tcpip_adapter/event_handlers.c +++ /dev/null @@ -1,312 +0,0 @@ -// Copyright 2018 Espressif Systems (Shanghai) PTE LTD -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include -#include "tcpip_adapter.h" -#include "esp_event.h" -#include "esp_wifi.h" -#include "esp_private/wifi.h" -#if CONFIG_ETH_ENABLED -#include "esp_eth.h" -#endif -#include "esp_err.h" -#include "esp_log.h" - -static const char *TAG = "tcpip_adapter"; - -#define API_CALL_CHECK(info, api_call, ret) \ -do{\ - esp_err_t __err = (api_call);\ - if ((ret) != __err) {\ - ESP_LOGE(TAG, "%s %d %s ret=0x%X", __FUNCTION__, __LINE__, (info), __err);\ - return;\ - }\ -} while(0) - -static void handle_ap_start(void *arg, esp_event_base_t base, int32_t event_id, void *data); -static void handle_ap_stop(void *arg, esp_event_base_t base, int32_t event_id, void *data); -static void handle_sta_start(void *arg, esp_event_base_t base, int32_t event_id, void *data); -static void handle_sta_stop(void *arg, esp_event_base_t base, int32_t event_id, void *data); -static void handle_sta_connected(void *arg, esp_event_base_t base, int32_t event_id, void *data); -static void handle_sta_disconnected(void *arg, esp_event_base_t base, int32_t event_id, void *data); -static void handle_sta_got_ip(void *arg, esp_event_base_t base, int32_t event_id, void *data); -#if CONFIG_ETH_ENABLED -static void handle_eth_start(void *arg, esp_event_base_t base, int32_t event_id, void *data); -static void handle_eth_stop(void *arg, esp_event_base_t base, int32_t event_id, void *data); -static void handle_eth_connected(void *arg, esp_event_base_t base, int32_t event_id, void *data); -static void handle_eth_disconnected(void *arg, esp_event_base_t base, int32_t event_id, void *data); -static void handle_eth_got_ip(void *arg, esp_event_base_t base, int32_t event_id, void *data); - -static void handle_eth_start(void *arg, esp_event_base_t base, int32_t event_id, void *data) -{ - tcpip_adapter_ip_info_t eth_ip; - uint8_t eth_mac[6]; - esp_eth_handle_t eth_handle = *(esp_eth_handle_t*)data; - esp_eth_ioctl(eth_handle, ETH_CMD_G_MAC_ADDR, eth_mac); - tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_ETH, ð_ip); - tcpip_adapter_eth_start(eth_mac, ð_ip, eth_handle); -} - -static void handle_eth_stop(void *arg, esp_event_base_t base, int32_t event_id, void *data) -{ - tcpip_adapter_stop(TCPIP_ADAPTER_IF_ETH); -} - -static void handle_eth_connected(void *arg, esp_event_base_t base, int32_t event_id, void *data) -{ - tcpip_adapter_dhcp_status_t status; - - tcpip_adapter_up(TCPIP_ADAPTER_IF_ETH); - - tcpip_adapter_dhcpc_get_status(TCPIP_ADAPTER_IF_ETH, &status); - - if (status == TCPIP_ADAPTER_DHCP_INIT) { - tcpip_adapter_dhcpc_start(TCPIP_ADAPTER_IF_ETH); - } else if (status == TCPIP_ADAPTER_DHCP_STOPPED) { - tcpip_adapter_ip_info_t eth_ip; - - tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_ETH, ð_ip); - - if (!(ip4_addr_isany_val(eth_ip.ip) || ip4_addr_isany_val(eth_ip.netmask))) { - ip_event_got_ip_t evt; - - //notify event - evt.if_index = TCPIP_ADAPTER_IF_ETH; - memcpy(&evt.ip_info, ð_ip, sizeof(tcpip_adapter_ip_info_t)); - API_CALL_CHECK("handle_eth_connected", esp_event_send_internal(IP_EVENT, IP_EVENT_ETH_GOT_IP, &evt, sizeof(evt), 0), ESP_OK); - } else { - ESP_LOGE(TAG, "invalid static ip"); - } - } -} - -static void handle_eth_disconnected(void *arg, esp_event_base_t base, int32_t event_id, void *data) -{ - tcpip_adapter_down(TCPIP_ADAPTER_IF_ETH); -} - -static void handle_eth_got_ip(void *arg, esp_event_base_t base, int32_t event_id, void *data) -{ - const ip_event_got_ip_t *event = (const ip_event_got_ip_t *) data; - ESP_LOGI(TAG, "eth ip: " IPSTR ", mask: " IPSTR ", gw: " IPSTR, - IP2STR(&event->ip_info.ip), - IP2STR(&event->ip_info.netmask), - IP2STR(&event->ip_info.gw)); -} -#endif - -static void handle_sta_got_ip(void *arg, esp_event_base_t base, int32_t event_id, void *data) -{ - API_CALL_CHECK("esp_wifi_internal_set_sta_ip", esp_wifi_internal_set_sta_ip(), ESP_OK); - - const ip_event_got_ip_t *event = (const ip_event_got_ip_t *) data; - ESP_LOGI(TAG, "sta ip: " IPSTR ", mask: " IPSTR ", gw: " IPSTR, - IP2STR(&event->ip_info.ip), - IP2STR(&event->ip_info.netmask), - IP2STR(&event->ip_info.gw)); -} - - -static void handle_ap_start(void *arg, esp_event_base_t base, int32_t event_id, void *data) -{ - tcpip_adapter_ip_info_t ap_ip; - uint8_t ap_mac[6]; - - API_CALL_CHECK("esp_wifi_internal_reg_rxcb", esp_wifi_internal_reg_rxcb(ESP_IF_WIFI_AP, (wifi_rxcb_t)tcpip_adapter_ap_input), ESP_OK); - API_CALL_CHECK("esp_wifi_mac_get", esp_wifi_get_mac(ESP_IF_WIFI_AP, ap_mac), ESP_OK); - - tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_AP, &ap_ip); - tcpip_adapter_ap_start(ap_mac, &ap_ip); -} - -static void handle_ap_stop(void *arg, esp_event_base_t base, int32_t event_id, void *data) -{ - API_CALL_CHECK("esp_wifi_internal_reg_rxcb", esp_wifi_internal_reg_rxcb(ESP_IF_WIFI_AP, NULL), ESP_OK); - - tcpip_adapter_stop(TCPIP_ADAPTER_IF_AP); -} - -static void handle_sta_start(void *arg, esp_event_base_t base, int32_t event_id, void *data) -{ - tcpip_adapter_ip_info_t sta_ip; - uint8_t sta_mac[6]; - - API_CALL_CHECK("esp_wifi_mac_get", esp_wifi_get_mac(ESP_IF_WIFI_STA, sta_mac), ESP_OK); - tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_STA, &sta_ip); - tcpip_adapter_sta_start(sta_mac, &sta_ip); -} - -static void handle_sta_stop(void *arg, esp_event_base_t base, int32_t event_id, void *data) -{ - tcpip_adapter_stop(TCPIP_ADAPTER_IF_STA); -} - -static void handle_sta_connected(void *arg, esp_event_base_t base, int32_t event_id, void *data) -{ - tcpip_adapter_dhcp_status_t status; - - API_CALL_CHECK("esp_wifi_internal_reg_rxcb", esp_wifi_internal_reg_rxcb(ESP_IF_WIFI_STA, (wifi_rxcb_t)tcpip_adapter_sta_input), ESP_OK); - - tcpip_adapter_up(TCPIP_ADAPTER_IF_STA); - - tcpip_adapter_dhcpc_get_status(TCPIP_ADAPTER_IF_STA, &status); - - if (status == TCPIP_ADAPTER_DHCP_INIT) { - tcpip_adapter_dhcpc_start(TCPIP_ADAPTER_IF_STA); - } else if (status == TCPIP_ADAPTER_DHCP_STOPPED) { - tcpip_adapter_ip_info_t sta_ip; - tcpip_adapter_ip_info_t sta_old_ip; - - tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_STA, &sta_ip); - tcpip_adapter_get_old_ip_info(TCPIP_ADAPTER_IF_STA, &sta_old_ip); - - if (!(ip4_addr_isany_val(sta_ip.ip) || ip4_addr_isany_val(sta_ip.netmask))) { - - ip_event_got_ip_t evt; - - evt.if_index = TCPIP_ADAPTER_IF_STA; - evt.ip_changed = false; - - if (memcmp(&sta_ip, &sta_old_ip, sizeof(sta_ip))) { - evt.ip_changed = true; - } - - memcpy(&evt.ip_info, &sta_ip, sizeof(tcpip_adapter_ip_info_t)); - tcpip_adapter_set_old_ip_info(TCPIP_ADAPTER_IF_STA, &sta_ip); - - API_CALL_CHECK("handle_sta_connected", esp_event_send_internal(IP_EVENT, IP_EVENT_STA_GOT_IP, &evt, sizeof(evt), 0), ESP_OK); - ESP_LOGD(TAG, "static ip: ip changed=%d", evt.ip_changed); - } else { - ESP_LOGE(TAG, "invalid static ip"); - } - } -} - -static void handle_sta_disconnected(void *arg, esp_event_base_t base, int32_t event_id, void *data) -{ - tcpip_adapter_down(TCPIP_ADAPTER_IF_STA); - API_CALL_CHECK("esp_wifi_internal_reg_rxcb", esp_wifi_internal_reg_rxcb(ESP_IF_WIFI_STA, NULL), ESP_OK); -} - - -esp_err_t tcpip_adapter_set_default_wifi_handlers(void) -{ - esp_err_t err; - err = esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_START, handle_sta_start, NULL); - if (err != ESP_OK) { - goto fail; - } - - err = esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_STOP, handle_sta_stop, NULL); - if (err != ESP_OK) { - goto fail; - } - - err = esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_CONNECTED, handle_sta_connected, NULL); - if (err != ESP_OK) { - goto fail; - } - - err = esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, handle_sta_disconnected, NULL); - if (err != ESP_OK) { - goto fail; - } - - err = esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_AP_START, handle_ap_start, NULL); - if (err != ESP_OK) { - goto fail; - } - - err = esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_AP_STOP, handle_ap_stop, NULL); - if (err != ESP_OK) { - goto fail; - } - - err = esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, handle_sta_got_ip, NULL); - if (err != ESP_OK) { - goto fail; - } - - err = esp_register_shutdown_handler((shutdown_handler_t)esp_wifi_stop); - if (err != ESP_OK && err != ESP_ERR_INVALID_STATE) { - goto fail; - } - - return ESP_OK; - -fail: - tcpip_adapter_clear_default_wifi_handlers(); - return err; -} - -esp_err_t tcpip_adapter_clear_default_wifi_handlers(void) -{ - esp_event_handler_unregister(WIFI_EVENT, WIFI_EVENT_STA_START, handle_sta_start); - esp_event_handler_unregister(WIFI_EVENT, WIFI_EVENT_STA_STOP, handle_sta_stop); - esp_event_handler_unregister(WIFI_EVENT, WIFI_EVENT_STA_CONNECTED, handle_sta_connected); - esp_event_handler_unregister(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, handle_sta_disconnected); - esp_event_handler_unregister(WIFI_EVENT, WIFI_EVENT_AP_START, handle_ap_start); - esp_event_handler_unregister(WIFI_EVENT, WIFI_EVENT_AP_STOP, handle_ap_stop); - esp_event_handler_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, handle_sta_got_ip); - esp_unregister_shutdown_handler((shutdown_handler_t)esp_wifi_stop); - - return ESP_OK; -} - -#if CONFIG_ETH_ENABLED -esp_err_t tcpip_adapter_set_default_eth_handlers(void) -{ - esp_err_t err; - err = esp_event_handler_register(ETH_EVENT, ETHERNET_EVENT_START, handle_eth_start, NULL); - if (err != ESP_OK) { - goto fail; - } - - err = esp_event_handler_register(ETH_EVENT, ETHERNET_EVENT_STOP, handle_eth_stop, NULL); - if (err != ESP_OK) { - goto fail; - } - - err = esp_event_handler_register(ETH_EVENT, ETHERNET_EVENT_CONNECTED, handle_eth_connected, NULL); - if (err != ESP_OK) { - goto fail; - } - - err = esp_event_handler_register(ETH_EVENT, ETHERNET_EVENT_DISCONNECTED, handle_eth_disconnected, NULL); - if (err != ESP_OK) { - goto fail; - } - - err = esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, handle_eth_got_ip, NULL); - if (err != ESP_OK) { - goto fail; - } - - return ESP_OK; - -fail: - tcpip_adapter_clear_default_eth_handlers(); - return err; -} - -esp_err_t tcpip_adapter_clear_default_eth_handlers(void) -{ - esp_event_handler_unregister(ETH_EVENT, ETHERNET_EVENT_START, handle_eth_start); - esp_event_handler_unregister(ETH_EVENT, ETHERNET_EVENT_STOP, handle_eth_stop); - esp_event_handler_unregister(ETH_EVENT, ETHERNET_EVENT_CONNECTED, handle_eth_connected); - esp_event_handler_unregister(ETH_EVENT, ETHERNET_EVENT_DISCONNECTED, handle_eth_disconnected); - esp_event_handler_unregister(IP_EVENT, IP_EVENT_ETH_GOT_IP, handle_eth_got_ip); - return ESP_OK; -} -#endif diff --git a/components/tcpip_adapter/include/tcpip_adapter.h b/components/tcpip_adapter/include/tcpip_adapter.h index f880511f71..c27fffaeaf 100644 --- a/components/tcpip_adapter/include/tcpip_adapter.h +++ b/components/tcpip_adapter/include/tcpip_adapter.h @@ -1,4 +1,4 @@ -// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD +// Copyright 2015-2019 Espressif Systems (Shanghai) PTE LTD // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -15,83 +15,25 @@ #ifndef _TCPIP_ADAPTER_H_ #define _TCPIP_ADAPTER_H_ -#include -#include "sys/queue.h" -#include "esp_wifi_types.h" -#include "sdkconfig.h" - -#if CONFIG_TCPIP_LWIP +#include "esp_netif.h" #include "lwip/ip_addr.h" #include "dhcpserver/dhcpserver.h" -typedef dhcps_lease_t tcpip_adapter_dhcps_lease_t; -#ifdef __cplusplus -extern "C" { -#endif +// +// Define compatible types if tcpip_adapter interface used +// +#define TCPIP_ADAPTER_DHCP_STARTED ESP_NETIF_DHCP_STARTED +#define TCPIP_ADAPTER_DHCP_STOPPED ESP_NETIF_DHCP_STOPPED +#define TCPIP_ADAPTER_DHCP_INIT ESP_NETIF_DHCP_INIT +#define TCPIP_ADAPTER_OP_SET ESP_NETIF_OP_SET +#define TCPIP_ADAPTER_OP_GET ESP_NETIF_OP_GET +#define TCPIP_ADAPTER_DOMAIN_NAME_SERVER ESP_NETIF_DOMAIN_NAME_SERVER +#define TCPIP_ADAPTER_ROUTER_SOLICITATION_ADDRESS ESP_NETIF_ROUTER_SOLICITATION_ADDRESS +#define TCPIP_ADAPTER_REQUESTED_IP_ADDRESS ESP_NETIF_REQUESTED_IP_ADDRESS +#define TCPIP_ADAPTER_IP_ADDRESS_LEASE_TIME ESP_NETIF_IP_ADDRESS_LEASE_TIME +#define TCPIP_ADAPTER_IP_REQUEST_RETRY_TIME ESP_NETIF_IP_REQUEST_RETRY_TIME -#define IP2STR(ipaddr) ip4_addr1_16(ipaddr), \ - ip4_addr2_16(ipaddr), \ - ip4_addr3_16(ipaddr), \ - ip4_addr4_16(ipaddr) - -#define IPSTR "%d.%d.%d.%d" - -#define IPV62STR(ipaddr) IP6_ADDR_BLOCK1(&(ipaddr)), \ - IP6_ADDR_BLOCK2(&(ipaddr)), \ - IP6_ADDR_BLOCK3(&(ipaddr)), \ - IP6_ADDR_BLOCK4(&(ipaddr)), \ - IP6_ADDR_BLOCK5(&(ipaddr)), \ - IP6_ADDR_BLOCK6(&(ipaddr)), \ - IP6_ADDR_BLOCK7(&(ipaddr)), \ - IP6_ADDR_BLOCK8(&(ipaddr)) - -#define IPV6STR "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x" - -/** @brief IPV4 IP address information - */ -typedef struct { - ip4_addr_t ip; /**< Interface IPV4 address */ - ip4_addr_t netmask; /**< Interface IPV4 netmask */ - ip4_addr_t gw; /**< Interface IPV4 gateway address */ -} tcpip_adapter_ip_info_t; - -/** @brief IPV6 IP address information - */ -typedef struct { - ip6_addr_t ip; /**< Interface IPV6 address */ -} tcpip_adapter_ip6_info_t; - -/** @brief IP address info of station connected to WLAN AP - * - * @note See also wifi_sta_info_t (MAC layer information only) - */ -typedef struct { - uint8_t mac[6]; /**< Station MAC address */ - ip4_addr_t ip; /**< Station assigned IP address */ -} tcpip_adapter_sta_info_t; - -/** @brief WLAN AP: Connected stations list - * - * Used to retrieve IP address information about connected stations. - */ -typedef struct { - tcpip_adapter_sta_info_t sta[ESP_WIFI_MAX_CONN_NUM]; /**< Connected stations */ - int num; /**< Number of connected stations */ -} tcpip_adapter_sta_list_t; - -#endif - -#define ESP_ERR_TCPIP_ADAPTER_BASE 0x5000 -#define ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS ESP_ERR_TCPIP_ADAPTER_BASE + 0x01 -#define ESP_ERR_TCPIP_ADAPTER_IF_NOT_READY ESP_ERR_TCPIP_ADAPTER_BASE + 0x02 -#define ESP_ERR_TCPIP_ADAPTER_DHCPC_START_FAILED ESP_ERR_TCPIP_ADAPTER_BASE + 0x03 -#define ESP_ERR_TCPIP_ADAPTER_DHCP_ALREADY_STARTED ESP_ERR_TCPIP_ADAPTER_BASE + 0x04 -#define ESP_ERR_TCPIP_ADAPTER_DHCP_ALREADY_STOPPED ESP_ERR_TCPIP_ADAPTER_BASE + 0x05 -#define ESP_ERR_TCPIP_ADAPTER_NO_MEM ESP_ERR_TCPIP_ADAPTER_BASE + 0x06 -#define ESP_ERR_TCPIP_ADAPTER_DHCP_NOT_STOPPED ESP_ERR_TCPIP_ADAPTER_BASE + 0x07 - -/* @brief On-chip network interfaces */ typedef enum { TCPIP_ADAPTER_IF_STA = 0, /**< Wi-Fi STA (station) interface */ TCPIP_ADAPTER_IF_AP, /**< Wi-Fi soft-AP interface */ @@ -100,657 +42,123 @@ typedef enum { TCPIP_ADAPTER_IF_MAX } tcpip_adapter_if_t; -/** @brief Type of DNS server */ -typedef enum { - TCPIP_ADAPTER_DNS_MAIN= 0, /**< DNS main server address*/ - TCPIP_ADAPTER_DNS_BACKUP, /**< DNS backup server address (Wi-Fi STA and Ethernet only) */ - TCPIP_ADAPTER_DNS_FALLBACK, /**< DNS fallback server address (Wi-Fi STA and Ethernet only) */ - TCPIP_ADAPTER_DNS_MAX -} tcpip_adapter_dns_type_t; - -/** @brief DNS server info */ +/** @brief legacy ip_info type + */ typedef struct { - ip_addr_t ip; /**< IPV4 address of DNS server */ -} tcpip_adapter_dns_info_t; - -/** @brief Status of DHCP client or DHCP server */ -typedef enum { - TCPIP_ADAPTER_DHCP_INIT = 0, /**< DHCP client/server is in initial state (not yet started) */ - TCPIP_ADAPTER_DHCP_STARTED, /**< DHCP client/server has been started */ - TCPIP_ADAPTER_DHCP_STOPPED, /**< DHCP client/server has been stopped */ - TCPIP_ADAPTER_DHCP_STATUS_MAX -} tcpip_adapter_dhcp_status_t; - -/** @brief Mode for DHCP client or DHCP server option functions */ -typedef enum{ - TCPIP_ADAPTER_OP_START = 0, - TCPIP_ADAPTER_OP_SET, /**< Set option */ - TCPIP_ADAPTER_OP_GET, /**< Get option */ - TCPIP_ADAPTER_OP_MAX -} tcpip_adapter_dhcp_option_mode_t; - -/* Deprecated name for tcpip_adapter_dhcp_option_mode_t, to remove after ESP-IDF V4.0 */ -typedef tcpip_adapter_dhcp_option_mode_t tcpip_adapter_option_mode_t; - -/** @brief Supported options for DHCP client or DHCP server */ -typedef enum{ - TCPIP_ADAPTER_DOMAIN_NAME_SERVER = 6, /**< Domain name server */ - TCPIP_ADAPTER_ROUTER_SOLICITATION_ADDRESS = 32, /**< Solicitation router address */ - TCPIP_ADAPTER_REQUESTED_IP_ADDRESS = 50, /**< Request specific IP address */ - TCPIP_ADAPTER_IP_ADDRESS_LEASE_TIME = 51, /**< Request IP address lease time */ - TCPIP_ADAPTER_IP_REQUEST_RETRY_TIME = 52, /**< Request IP address retry counter */ -} tcpip_adapter_dhcp_option_id_t; - -/* Deprecated name for tcpip_adapter_dhcp_option_id_t, to remove after ESP-IDF V4.0 */ -typedef tcpip_adapter_dhcp_option_id_t tcpip_adapter_option_id_t; - -/** 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_t; - -/** @brief IP event base declaration */ -ESP_EVENT_DECLARE_BASE(IP_EVENT); - -/** Event structure for IP_EVENT_STA_GOT_IP, IP_EVENT_ETH_GOT_IP events */ -typedef struct { - tcpip_adapter_if_t if_index; /*!< Interface for which the event is received */ - tcpip_adapter_ip_info_t ip_info; /*!< IP address, netmask, gatway IP address */ - bool ip_changed; /*!< Whether the assigned IP has changed or not */ -} ip_event_got_ip_t; - -/** Event structure for IP_EVENT_GOT_IP6 event */ -typedef struct { - tcpip_adapter_if_t if_index; /*!< Interface for which the event is received */ - tcpip_adapter_ip6_info_t ip6_info; /*!< IPv6 address of the interface */ -} ip_event_got_ip6_t; - -/** Event structure for IP_EVENT_AP_STAIPASSIGNED event */ -typedef struct { - ip4_addr_t ip; /*!< IP address which was assigned to the station */ -} ip_event_ap_staipassigned_t; + ip4_addr_t ip; /**< Interface IPV4 address */ + ip4_addr_t netmask; /**< Interface IPV4 netmask */ + ip4_addr_t gw; /**< Interface IPV4 gateway address */ +} tcpip_adapter_ip_info_t; +/** @brief legacy typedefs + */ +typedef esp_netif_dhcp_status_t tcpip_adapter_dhcp_status_t; +typedef dhcps_lease_t tcpip_adapter_dhcps_lease_t; +typedef esp_netif_dhcp_option_mode_t tcpip_adapter_dhcp_option_mode_t; +typedef esp_netif_dhcp_option_id_t tcpip_adapter_dhcp_option_id_t; +typedef esp_netif_dns_type_t tcpip_adapter_dns_type_t; +typedef esp_netif_dns_info_t tcpip_adapter_dns_info_t; +typedef esp_netif_sta_list_t tcpip_adapter_sta_list_t; +typedef esp_netif_sta_info_t tcpip_adapter_sta_info_t; /** - * @brief Initialize the underlying TCP/IP stack - * - * @note This function should be called exactly once from application code, when the application starts up. + * @brief tcpip adapter legacy init. It is used only to set the compatibility mode of esp-netif, which + * will enable backward compatibility of esp-netif. */ void tcpip_adapter_init(void); /** - * @brief Cause the TCP/IP stack to start the Ethernet interface with specified MAC and IP + * @brief Compatiblity mode: convert the esp-netif handle to tcpip_adapter legacy interface enum * - * @note This function should be called after the Ethernet MAC hardware is initialized. In the default configuration, application code does not need to call this function - it is called automatically by the default handler for the SYSTEM_EVENT_ETH_START event. + * @param esp_netif * - * @param[in] mac Set MAC address of this interface - * @param[in] ip_info Set IP address of this interface - * @param[in] args extra args passed to tcpip_adapter - * - * @return - * - ESP_OK - * - ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS - * - ESP_ERR_NO_MEM + * @return corresponding interface if valid or known esp_netif provided, TCPIP_ADAPTER_IF_MAX otherwise */ -esp_err_t tcpip_adapter_eth_start(uint8_t *mac, tcpip_adapter_ip_info_t *ip_info, void *args); +tcpip_adapter_if_t tcpip_adapter_if_from_esp_netif(esp_netif_t *esp_netif); /** - * @brief Cause the TCP/IP stack to start the Wi-Fi station interface with specified MAC and IP + * @brief Translates to esp_netif_get_ip_info * - * - * @note This function should be called after the Wi-Fi Station hardware is initialized. In the default configuration, application code does not need to call this function - it is called automatically by the default handler for the SYSTEM_EVENT_STA_START event. - * - * @param[in] mac Set MAC address of this interface - * @param[in] ip_info Set IP address of this interface - * - * @return - * - ESP_OK - * - ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS - * - ESP_ERR_NO_MEM - */ -esp_err_t tcpip_adapter_sta_start(uint8_t *mac, tcpip_adapter_ip_info_t *ip_info); - -/** - * @brief Cause the TCP/IP stack to start the Wi-Fi AP interface with specified MAC and IP - * - * @note This function should be called after the Wi-Fi AP hardware is initialized. In the default configuration, application code does not need to call this function - it is called automatically by the default handler for the SYSTEM_EVENT_AP_START event. - * - * DHCP server will be started automatically when this function is called. - * - * @param[in] mac Set MAC address of this interface - * @param[in] ip_info Set IP address of this interface - * - * @return - * - ESP_OK - * - ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS - * - ESP_ERR_NO_MEM - */ -esp_err_t tcpip_adapter_ap_start(uint8_t *mac, tcpip_adapter_ip_info_t *ip_info); - -/** - * @brief Cause the TCP/IP stack to stop a network interface - * - * Causes TCP/IP stack to clean up this interface. This includes stopping the DHCP server or client, if they are started. - * - * @note This API is called by the default Wi-Fi and Ethernet event handlers if the underlying network driver reports that the - * interface has stopped. - * - * @note To stop an interface from application code, call the network-specific API (esp_wifi_stop() or esp_eth_stop()). - * The driver layer will then send a stop event and the event handler should call this API. - * Otherwise, the driver and MAC layer will remain started. - * - * @param[in] tcpip_if Interface which will be stopped - * - * @return - * - ESP_OK - * - ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS - * - ESP_ERR_TCPIP_ADAPTER_IF_NOT_READY - */ -esp_err_t tcpip_adapter_stop(tcpip_adapter_if_t tcpip_if); - -/** - * @brief Cause the TCP/IP stack to bring up an interface - * - * @note This function is called automatically by the default event handlers for the Wi-Fi Station and Ethernet interfaces, - * in response to the SYSTEM_EVENT_STA_CONNECTED and SYSTEM_EVENT_ETH_CONNECTED events, respectively. - * - * @note This function is not normally used with Wi-Fi AP interface. If the AP interface is started, it is up. - * - * @param[in] tcpip_if Interface to bring up - * - * @return - * - ESP_OK - * - ESP_ERR_TCPIP_ADAPTER_IF_NOT_READY - */ -esp_err_t tcpip_adapter_up(tcpip_adapter_if_t tcpip_if); - -/** - * @brief Cause the TCP/IP stack to shutdown an interface - * - * @note This function is called automatically by the default event handlers for the Wi-Fi Station and Ethernet interfaces, - * in response to the SYSTEM_EVENT_STA_DISCONNECTED and SYSTEM_EVENT_ETH_DISCONNECTED events, respectively. - * - * @note This function is not normally used with Wi-Fi AP interface. If the AP interface is stopped, it is down. - * - * @param[in] tcpip_if Interface to shutdown - * - * @return - * - ESP_OK - * - ESP_ERR_TCPIP_ADAPTER_IF_NOT_READY - */ -esp_err_t tcpip_adapter_down(tcpip_adapter_if_t tcpip_if); - -/** - * @brief Get interface's IP address information - * - * If the interface is up, IP information is read directly from the TCP/IP stack. - * - * If the interface is down, IP information is read from a copy kept in the TCP/IP adapter - * library itself. - * - * @param[in] tcpip_if Interface to get IP information - * @param[out] ip_info If successful, IP information will be returned in this argument. - * - * @return - * - ESP_OK - * - ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS + * @param tcpip_if Interface type corresponding to appropriate instance of esp-netif + * @param ip_info See esp_netif_get_ip_info + * @return See esp_netif_get_ip_info */ esp_err_t tcpip_adapter_get_ip_info(tcpip_adapter_if_t tcpip_if, tcpip_adapter_ip_info_t *ip_info); /** - * @brief Set interface's IP address information + * @brief Translates to esp_netif_get_ip6_linklocal * - * This function is mainly used to set a static IP on an interface. - * - * If the interface is up, the new IP information is set directly in the TCP/IP stack. - * - * The copy of IP information kept in the TCP/IP adapter library is also updated (this - * copy is returned if the IP is queried while the interface is still down.) - * - * @note DHCP client/server must be stopped before setting new IP information. - * - * @note Calling this interface for the Wi-Fi STA or Ethernet interfaces may generate a - * SYSTEM_EVENT_STA_GOT_IP or SYSTEM_EVENT_ETH_GOT_IP event. - * - * @param[in] tcpip_if Interface to set IP information - * @param[in] ip_info IP information to set on the specified interface - * - * @return - * - ESP_OK - * - ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS - * - ESP_ERR_TCPIP_ADAPTER_DHCP_NOT_STOPPED If DHCP server or client is still running - */ -esp_err_t tcpip_adapter_set_ip_info(tcpip_adapter_if_t tcpip_if, const tcpip_adapter_ip_info_t *ip_info); - -/** - * @brief Set DNS Server information - * - * This function behaves differently for different interfaces: - * - * - For Wi-Fi Station interface and Ethernet interface, up to three types of DNS server can be set (in order of priority): - * - Main DNS Server (TCPIP_ADAPTER_DNS_MAIN) - * - Backup DNS Server (TCPIP_ADAPTER_DNS_BACKUP) - * - Fallback DNS Server (TCPIP_ADAPTER_DNS_FALLBACK) - * - * If DHCP client is enabled, main and backup DNS servers will be updated automatically from the DHCP lease if the relevant DHCP options are set. Fallback DNS Server is never updated from the DHCP lease and is designed to be set via this API. - * - * If DHCP client is disabled, all DNS server types can be set via this API only. - * - * - For Wi-Fi AP interface, the Main DNS Server setting is used by the DHCP server to provide a DNS Server option to DHCP clients (Wi-Fi stations). - * - The default Main DNS server is the IP of the Wi-Fi AP interface itself. - * - This function can override it by setting server type TCPIP_ADAPTER_DNS_MAIN. - * - Other DNS Server types are not supported for the Wi-Fi AP interface. - * - * @param[in] tcpip_if Interface to set DNS Server information - * @param[in] type Type of DNS Server to set: TCPIP_ADAPTER_DNS_MAIN, TCPIP_ADAPTER_DNS_BACKUP, TCPIP_ADAPTER_DNS_FALLBACK - * @param[in] dns DNS Server address to set - * - * @return - * - ESP_OK on success - * - ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS invalid params - */ -esp_err_t tcpip_adapter_set_dns_info(tcpip_adapter_if_t tcpip_if, tcpip_adapter_dns_type_t type, tcpip_adapter_dns_info_t *dns); - -/** - * @brief Get DNS Server information - * - * Return the currently configured DNS Server address for the specified interface and Server type. - * - * This may be result of a previous call to tcpip_adapter_set_dns_info(). If the interface's DHCP client is enabled, - * the Main or Backup DNS Server may be set by the current DHCP lease. - * - * @param[in] tcpip_if Interface to get DNS Server information - * @param[in] type Type of DNS Server to get: TCPIP_ADAPTER_DNS_MAIN, TCPIP_ADAPTER_DNS_BACKUP, TCPIP_ADAPTER_DNS_FALLBACK - * @param[out] dns DNS Server result is written here on success - * - * @return - * - ESP_OK on success - * - ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS invalid params - */ -esp_err_t tcpip_adapter_get_dns_info(tcpip_adapter_if_t tcpip_if, tcpip_adapter_dns_type_t type, tcpip_adapter_dns_info_t *dns); - -/** - * @brief Get interface's old IP information - * - * Returns an "old" IP address previously stored for the interface when the valid IP changed. - * - * If the IP lost timer has expired (meaning the interface was down for longer than the configured interval) - * then the old IP information will be zero. - * - * @param[in] tcpip_if Interface to get old IP information - * @param[out] ip_info If successful, IP information will be returned in this argument. - * - * @return - * - ESP_OK - * - ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS - */ -esp_err_t tcpip_adapter_get_old_ip_info(tcpip_adapter_if_t tcpip_if, tcpip_adapter_ip_info_t *ip_info); - -/** - * @brief Set interface old IP information - * - * This function is called from the DHCP client for the Wi-Fi STA and Ethernet interfaces, before a new IP is set. It is also called from the default handlers for the SYSTEM_EVENT_STA_CONNECTED and SYSTEM_EVENT_ETH_CONNECTED events. - * - * Calling this function stores the previously configured IP, which can be used to determine if the IP changes in the future. - * - * If the interface is disconnected or down for too long, the "IP lost timer" will expire (after the configured interval) and set the old IP information to zero. - * - * @param[in] tcpip_if Interface to set old IP information - * @param[in] ip_info Store the old IP information for the specified interface - * - * @return - * - ESP_OK - * - ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS - */ -esp_err_t tcpip_adapter_set_old_ip_info(tcpip_adapter_if_t tcpip_if, const tcpip_adapter_ip_info_t *ip_info); - - -/** - * @brief Create interface link-local IPv6 address - * - * Cause the TCP/IP stack to create a link-local IPv6 address for the specified interface. - * - * This function also registers a callback for the specified interface, so that if the link-local address becomes verified as the preferred address then a SYSTEM_EVENT_GOT_IP6 event will be sent. - * - * @param[in] tcpip_if Interface to create a link-local IPv6 address - * - * @return - * - ESP_OK - * - ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS - */ -esp_err_t tcpip_adapter_create_ip6_linklocal(tcpip_adapter_if_t tcpip_if); - -/** - * @brief Get interface link-local IPv6 address - * - * If the specified interface is up and a preferred link-local IPv6 address - * has been created for the interface, return a copy of it. - * - * @param[in] tcpip_if Interface to get link-local IPv6 address - * @param[out] if_ip6 IPv6 information will be returned in this argument if successful. - * - * @return - * - ESP_OK - * - ESP_FAIL If interface is down, does not have a link-local IPv6 address, or the link-local IPv6 address is not a preferred address. + * @param tcpip_if Interface type corresponding to appropriate instance of esp-netif + * @param if_ip6 See esp_netif_get_ip6_linklocal + * @return See esp_netif_get_ip6_linklocal */ esp_err_t tcpip_adapter_get_ip6_linklocal(tcpip_adapter_if_t tcpip_if, ip6_addr_t *if_ip6); -#if 0 -esp_err_t tcpip_adapter_get_mac(tcpip_adapter_if_t tcpip_if, uint8_t *mac); - -esp_err_t tcpip_adapter_set_mac(tcpip_adapter_if_t tcpip_if, uint8_t *mac); -#endif - /** - * @brief Get DHCP Server status - * - * @param[in] tcpip_if Interface to get status of DHCP server. - * @param[out] status If successful, the status of the DHCP server will be returned in this argument. - * - * @return - * - ESP_OK - */ -esp_err_t tcpip_adapter_dhcps_get_status(tcpip_adapter_if_t tcpip_if, tcpip_adapter_dhcp_status_t *status); - -/** - * @brief Set or Get DHCP server option - * - * @param[in] opt_op TCPIP_ADAPTER_OP_SET to set an option, TCPIP_ADAPTER_OP_GET to get an option. - * @param[in] opt_id Option index to get or set, must be one of the supported enum values. - * @param[inout] opt_val Pointer to the option parameter. - * @param[in] opt_len Length of the option parameter. - * - * @return - * - ESP_OK - * - ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS - * - ESP_ERR_TCPIP_ADAPTER_DHCP_ALREADY_STOPPED - * - ESP_ERR_TCPIP_ADAPTER_DHCP_ALREADY_STARTED - */ -esp_err_t tcpip_adapter_dhcps_option(tcpip_adapter_dhcp_option_mode_t opt_op, tcpip_adapter_dhcp_option_id_t opt_id, void *opt_val, uint32_t opt_len); - -/** - * @brief Start DHCP server - * - * @note Currently DHCP server is only supported on the Wi-Fi AP interface. - * - * @param[in] tcpip_if Interface to start DHCP server. Must be TCPIP_ADAPTER_IF_AP. - * - * @return - * - ESP_OK - * - ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS - * - ESP_ERR_TCPIP_ADAPTER_DHCP_ALREADY_STARTED - */ -esp_err_t tcpip_adapter_dhcps_start(tcpip_adapter_if_t tcpip_if); - -/** - * @brief Stop DHCP server - * - * @note Currently DHCP server is only supported on the Wi-Fi AP interface. - * - * @param[in] tcpip_if Interface to stop DHCP server. Must be TCPIP_ADAPTER_IF_AP. - * - * @return - * - ESP_OK - * - ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS - * - ESP_ERR_TCPIP_ADAPTER_DHCP_ALREADY_STOPPED - * - ESP_ERR_TCPIP_ADAPTER_IF_NOT_READY - */ -esp_err_t tcpip_adapter_dhcps_stop(tcpip_adapter_if_t tcpip_if); - -/** - * @brief Get DHCP client status - * - * @param[in] tcpip_if Interface to get status of DHCP client - * @param[out] status If successful, the status of DHCP client will be returned in this argument. - * - * @return - * - ESP_OK + * @brief`Translates to esp_netif_dhcpc_get_status + * @param tcpip_if Interface type corresponding to appropriate instance of esp-netif + * @param status + * @return See esp_netif_dhcpc_get_status */ esp_err_t tcpip_adapter_dhcpc_get_status(tcpip_adapter_if_t tcpip_if, tcpip_adapter_dhcp_status_t *status); /** - * @brief Set or Get DHCP client's option - * - * @note This function is not yet implemented - * - * @param[in] opt_op TCPIP_ADAPTER_OP_SET to set an option, TCPIP_ADAPTER_OP_GET to get an option. - * @param[in] opt_id Option index to get or set, must be one of the supported enum values. - * @param[inout] opt_val Pointer to the option parameter. - * @param[in] opt_len Length of the option parameter. - * - * @return - * - ESP_ERR_NOT_SUPPORTED (not implemented) - */ -esp_err_t tcpip_adapter_dhcpc_option(tcpip_adapter_dhcp_option_mode_t opt_op, tcpip_adapter_dhcp_option_id_t opt_id, void *opt_val, uint32_t opt_len); - -/** - * @brief Start DHCP client - * - * @note DHCP Client is only supported for the Wi-Fi station and Ethernet interfaces. - * - * @note The default event handlers for the SYSTEM_EVENT_STA_CONNECTED and SYSTEM_EVENT_ETH_CONNECTED events call this function. - * - * @param[in] tcpip_if Interface to start the DHCP client - * - * @return - * - ESP_OK - * - ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS - * - ESP_ERR_TCPIP_ADAPTER_DHCP_ALREADY_STARTED - * - ESP_ERR_TCPIP_ADAPTER_DHCPC_START_FAILED - */ -esp_err_t tcpip_adapter_dhcpc_start(tcpip_adapter_if_t tcpip_if); - -/** - * @brief Stop DHCP client - * - * @note DHCP Client is only supported for the Wi-Fi station and Ethernet interfaces. - * - * @note Calling tcpip_adapter_stop() or tcpip_adapter_down() will also stop the DHCP Client if it is running. - * - * @param[in] tcpip_if Interface to stop the DHCP client - * - * @return - * - ESP_OK - * - ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS - * - ESP_ERR_TCPIP_ADAPTER_DHCP_ALREADY_STOPPED - * - ESP_ERR_TCPIP_ADAPTER_IF_NOT_READY - */ -esp_err_t tcpip_adapter_dhcpc_stop(tcpip_adapter_if_t tcpip_if); - -/** - * @brief Receive an Ethernet frame from the Ethernet interface - * - * This function will automatically be installed by esp_eth_init(). The Ethernet driver layer will then call this function to forward frames to the TCP/IP stack. - * - * @note Application code does not usually need to use this function directly. - * - * @param[in] buffer Received data - * @param[in] len Length of the data frame - * @param[in] eb Pointer to internal Wi-Fi buffer (ignored for Ethernet) - * - * @return - * - ESP_OK - */ -esp_err_t tcpip_adapter_eth_input(void *buffer, uint16_t len, void *eb); - -/** - * @brief Receive an 802.11 data frame from the Wi-Fi Station interface - * - * This function should be installed by calling esp_wifi_reg_rxcb(). The Wi-Fi driver layer will then call this function to forward frames to the TCP/IP stack. - * - * @note Installation happens automatically in the default handler for the SYSTEM_EVENT_STA_CONNECTED event. - * - * @note Application code does not usually need to call this function directly. - * - * @param[in] buffer Received data - * @param[in] len Length of the data frame - * @param[in] eb Pointer to internal Wi-Fi buffer - * - * @return - * - ESP_OK - */ -esp_err_t tcpip_adapter_sta_input(void *buffer, uint16_t len, void *eb); - -/** - * @brief Receive an 802.11 data frame from the Wi-Fi AP interface - * - * This function should be installed by calling esp_wifi_reg_rxcb(). The Wi-Fi driver layer will then call this function to forward frames to the TCP/IP stack. - * - * @note Installation happens automatically in the default handler for the SYSTEM_EVENT_AP_START event. - * - * @note Application code does not usually need to call this function directly. - * - * @param[in] buffer Received data - * @param[in] len Length of the data frame - * @param[in] eb Pointer to internal Wi-Fi buffer - * - * @return - * - ESP_OK - */ -esp_err_t tcpip_adapter_ap_input(void *buffer, uint16_t len, void *eb); - -/** - * @brief Get network interface index - * - * Get network interface from TCP/IP implementation-specific interface pointer. - * - * @param[in] dev Implementation-specific TCP/IP stack interface pointer. - * - * @return - * - ESP_IF_WIFI_STA - * - ESP_IF_WIFI_AP - * - ESP_IF_ETH - * - ESP_IF_MAX - invalid parameter - */ -esp_interface_t tcpip_adapter_get_esp_if(void *dev); - -/** - * @brief Get IP information for stations connected to the Wi-Fi AP interface - * - * @param[in] wifi_sta_list Wi-Fi station info list, returned from esp_wifi_ap_get_sta_list() - * @param[out] tcpip_sta_list IP layer station info list, corresponding to MAC addresses provided in wifi_sta_list - * - * @return - * - ESP_OK - * - ESP_ERR_TCPIP_ADAPTER_NO_MEM - * - ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS - */ -esp_err_t tcpip_adapter_get_sta_list(const wifi_sta_list_t *wifi_sta_list, tcpip_adapter_sta_list_t *tcpip_sta_list); - -#define TCPIP_HOSTNAME_MAX_SIZE 32 -/** - * @brief Set the hostname of an interface - * - * @param[in] tcpip_if Interface to set the hostname - * @param[in] hostname New hostname for the interface. Maximum length 32 bytes. - * - * @return - * - ESP_OK - success - * - ESP_ERR_TCPIP_ADAPTER_IF_NOT_READY - interface status error - * - ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS - parameter error - */ -esp_err_t tcpip_adapter_set_hostname(tcpip_adapter_if_t tcpip_if, const char *hostname); - -/** - * @brief Get interface hostname. - * - * @param[in] tcpip_if Interface to get the hostname - * @param[out] hostname Returns a pointer to the hostname. May be NULL if no hostname is set. If set non-NULL, pointer remains valid (and string may change if the hostname changes). - * - * @return - * - ESP_OK - success - * - ESP_ERR_TCPIP_ADAPTER_IF_NOT_READY - interface status error - * - ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS - parameter error - */ -esp_err_t tcpip_adapter_get_hostname(tcpip_adapter_if_t tcpip_if, const char **hostname); - -/** - * @brief Get the TCP/IP stack-specific interface that is assigned to a given interface - * - * @note For lwIP, this returns a pointer to a netif structure. - * - * @param[in] tcpip_if Interface to get the implementation-specific interface - * @param[out] netif Pointer to the implementation-specific interface - * - * @return - * - ESP_OK - success - * - ESP_ERR_TCPIP_ADAPTER_IF_NOT_READY - interface status error - * - ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS - parameter error - */ -esp_err_t tcpip_adapter_get_netif(tcpip_adapter_if_t tcpip_if, void ** netif); - -/** - * @brief Test if supplied interface is up or down - * - * @param[in] tcpip_if Interface to test up/down status - * - * @return - * - true - Interface is up - * - false - Interface is down + * @brief Translates to esp_netif_is_netif_up + * @param tcpip_if Interface type corresponding to appropriate instance of esp-netif + * @return see esp_netif_is_netif_up */ bool tcpip_adapter_is_netif_up(tcpip_adapter_if_t tcpip_if); /** - * @brief Cause the TCP/IP stack to start the test interface with specified MAC and IP. - * Test interface is used to exercise network stack with injected packets from SW. - * - * @param[in] mac Set MAC address of this interface - * @param[in] ip_info Set IP address of this interface - * - * @return - * - ESP_OK - * - ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS - * - ESP_ERR_NO_MEM + * @brief Translates to esp_netif_get_netif + * @param tcpip_if Interface type corresponding to appropriate instance of esp-netif + * @param netif + * @return see esp_netif_get_netif */ -esp_err_t tcpip_adapter_test_start(uint8_t *mac, tcpip_adapter_ip_info_t *ip_info); +esp_err_t tcpip_adapter_get_netif(tcpip_adapter_if_t tcpip_if, void ** netif); /** - * @brief Install default event handlers for Ethernet interface - * @return - * - ESP_OK on success - * - one of the errors from esp_event on failure + * @brief Translates to esp_netif_create_ip6_linklocal + * @param tcpip_if Interface type corresponding to appropriate instance of esp-netif + * @return see esp_netif_create_ip6_linklocal + */ +esp_err_t tcpip_adapter_create_ip6_linklocal(tcpip_adapter_if_t tcpip_if); + +/** + * @brief Compatible version of setting ethernet default handlers + * @note Compatible version of wifi handlers are provided in a separate header, + * as this used to be called privately from wifi_init() + * @return ESP_OK on success */ esp_err_t tcpip_adapter_set_default_eth_handlers(void); /** - * @brief Uninstall default event handlers for Ethernet interface - * @return - * - ESP_OK on success - * - one of the errors from esp_event on failure + * @brief Compatible version of network stack input function. Translates to esp_netif_receive() + * @param buffer + * @param len + * @param eb + * @return see esp_netif_receive */ -esp_err_t tcpip_adapter_clear_default_eth_handlers(void); +esp_err_t tcpip_adapter_eth_input(void *buffer, uint16_t len, void *eb); /** - * @brief Install default event handlers for Wi-Fi interfaces (station and AP) - * @return - * - ESP_OK on success - * - one of the errors from esp_event on failure - */ -esp_err_t tcpip_adapter_set_default_wifi_handlers(void); - -/** - * @brief Uninstall default event handlers for Wi-Fi interfaces (station and AP) - * @return - * - ESP_OK on success - * - one of the errors from esp_event on failure + * @brief Compatible version of former tcpip_adapter API to clear default WIFI handlers + * @return ESP_OK on success */ esp_err_t tcpip_adapter_clear_default_wifi_handlers(void); -/** - * @brief Search nefit index through netif interface - * @param[in] tcpip_if Interface to search for netif index - * @return - * - netif_index on success - * - -1 if an invalid parameter is supplied - */ +esp_err_t tcpip_adapter_dhcps_stop(tcpip_adapter_if_t tcpip_if); +esp_err_t tcpip_adapter_dhcpc_stop(tcpip_adapter_if_t tcpip_if); +esp_err_t tcpip_adapter_dhcps_start(tcpip_adapter_if_t tcpip_if); +esp_err_t tcpip_adapter_dhcpc_start(tcpip_adapter_if_t tcpip_if); + +esp_err_t tcpip_adapter_dhcps_get_status(tcpip_adapter_if_t tcpip_if, tcpip_adapter_dhcp_status_t *status); +esp_err_t tcpip_adapter_dhcps_option(tcpip_adapter_dhcp_option_mode_t opt_op, tcpip_adapter_dhcp_option_id_t opt_id, void *opt_val, uint32_t opt_len); +esp_err_t tcpip_adapter_dhcpc_option(tcpip_adapter_dhcp_option_mode_t opt_op, tcpip_adapter_dhcp_option_id_t opt_id, void *opt_val, uint32_t opt_len); +esp_err_t tcpip_adapter_set_ip_info(tcpip_adapter_if_t tcpip_if, const tcpip_adapter_ip_info_t *ip_info); +esp_err_t tcpip_adapter_get_dns_info(tcpip_adapter_if_t tcpip_if, tcpip_adapter_dns_type_t type, tcpip_adapter_dns_info_t *dns); +esp_err_t tcpip_adapter_set_dns_info(tcpip_adapter_if_t tcpip_if, tcpip_adapter_dns_type_t type, tcpip_adapter_dns_info_t *dns); int tcpip_adapter_get_netif_index(tcpip_adapter_if_t tcpip_if); +esp_err_t tcpip_adapter_get_sta_list(const wifi_sta_list_t *wifi_sta_list, tcpip_adapter_sta_list_t *tcpip_sta_list); -#ifdef __cplusplus -} -#endif - -#endif /* _TCPIP_ADAPTER_H_ */ +#endif //_TCPIP_ADAPTER_H_ diff --git a/components/tcpip_adapter/include/tcpip_adapter_compatible/tcpip_adapter_compat.h b/components/tcpip_adapter/include/tcpip_adapter_compatible/tcpip_adapter_compat.h new file mode 100644 index 0000000000..45d5d6ac10 --- /dev/null +++ b/components/tcpip_adapter/include/tcpip_adapter_compatible/tcpip_adapter_compat.h @@ -0,0 +1,40 @@ +// Copyright 2015-2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef _TCPIP_ADAPTER_COMPAT_H_ +#define _TCPIP_ADAPTER_COMPAT_H_ + +/** + * @brief This function is called from wifi_init to assure backward compatibility mode + * of tcpip_adapter. In case of legacy use, default instances of ap and sta + * are created and handlers are registered + * + * @note This api is given in a separate header, which is included internally (from wifi driver) + * rather then user initialization code. + * + * @return ESP_OK on success + */ +esp_err_t tcpip_adapter_set_default_wifi_handlers(void); + +/** + * @brief This function is called from ethernet driver init code to facilitate + * autostart fo the driver in backward compatible tcpip_adapter way + * + * @param[in] h Handle to the ethernet driver + * + * @return ESP_OK on success + */ +esp_err_t tcpip_adapter_start_eth(void* h); + +#endif //_TCPIP_ADAPTER_COMPAT_H_ diff --git a/components/tcpip_adapter/include/tcpip_adapter_internal.h b/components/tcpip_adapter/include/tcpip_adapter_internal.h deleted file mode 100644 index 2b9879b4a7..0000000000 --- a/components/tcpip_adapter/include/tcpip_adapter_internal.h +++ /dev/null @@ -1,66 +0,0 @@ -// Copyright 2015-2018 Espressif Systems (Shanghai) PTE LTD -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at - -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#pragma once - -#include "tcpip_adapter.h" -#include "sys/queue.h" - -struct tcpip_adapter_api_msg_s; - -typedef int (*tcpip_adapter_api_fn)(struct tcpip_adapter_api_msg_s *msg); - -typedef struct tcpip_adapter_api_msg_s { - int type; /**< The first field MUST be int */ - int ret; - tcpip_adapter_api_fn api_fn; - tcpip_adapter_if_t tcpip_if; - tcpip_adapter_ip_info_t *ip_info; - uint8_t *mac; - void *data; -} tcpip_adapter_api_msg_t; - -typedef struct tcpip_adapter_dns_param_s { - tcpip_adapter_dns_type_t dns_type; - tcpip_adapter_dns_info_t *dns_info; -} tcpip_adapter_dns_param_t; - -typedef struct tcpip_adapter_ip_lost_timer_s { - bool timer_running; -} tcpip_adapter_ip_lost_timer_t; - - -#define TCPIP_ADAPTER_TRHEAD_SAFE 1 -#define TCPIP_ADAPTER_IPC_LOCAL 0 -#define TCPIP_ADAPTER_IPC_REMOTE 1 - -#define TCPIP_ADAPTER_IPC_CALL(_if, _mac, _ip, _data, _fn) do {\ - tcpip_adapter_api_msg_t msg;\ - if (tcpip_inited == false) {\ - ESP_LOGE(TAG, "tcpip_adapter is not initialized!");\ - abort();\ - }\ - memset(&msg, 0, sizeof(msg));\ - msg.tcpip_if = (_if);\ - msg.mac = (uint8_t*)(_mac);\ - msg.ip_info = (tcpip_adapter_ip_info_t*)(_ip);\ - msg.data = (void*)(_data);\ - msg.api_fn = (_fn);\ - if (TCPIP_ADAPTER_IPC_REMOTE == tcpip_adapter_ipc_check(&msg)) {\ - ESP_LOGV(TAG, "check: remote, if=%d fn=%p\n", (_if), (_fn));\ - return msg.ret;\ - } else {\ - ESP_LOGV(TAG, "check: local, if=%d fn=%p\n", (_if), (_fn));\ - }\ -} while(0) diff --git a/components/tcpip_adapter/sdkconfig.rename b/components/tcpip_adapter/sdkconfig.rename deleted file mode 100644 index 72405c0562..0000000000 --- a/components/tcpip_adapter/sdkconfig.rename +++ /dev/null @@ -1,5 +0,0 @@ -# sdkconfig replacement configurations for deprecated options formatted as -# CONFIG_DEPRECATED_OPTION CONFIG_NEW_OPTION - -CONFIG_IP_LOST_TIMER_INTERVAL CONFIG_NETIF_IP_LOST_TIMER_INTERVAL -CONFIG_USE_TCPIP_STACK_LIB CONFIG_NETIF_USE_TCPIP_STACK_LIB diff --git a/components/tcpip_adapter/tcpip_adapter_compat.c b/components/tcpip_adapter/tcpip_adapter_compat.c new file mode 100644 index 0000000000..cef9c8826f --- /dev/null +++ b/components/tcpip_adapter/tcpip_adapter_compat.c @@ -0,0 +1,274 @@ +// Copyright 2015-2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "esp_netif.h" +#include "tcpip_adapter_compatible/tcpip_adapter_compat.h" +#include "esp_private/wifi.h" +#include "esp_log.h" +#include "esp_netif_net_stack.h" +#include "tcpip_adapter.h" +#include "esp_eth.h" + +extern void _esp_wifi_set_default_ap_netif(esp_netif_t* esp_netif); +extern void _esp_wifi_set_default_sta_netif(esp_netif_t* esp_netif); +extern esp_err_t _esp_wifi_set_default_wifi_handlers(void); +extern esp_err_t esp_eth_set_default_handlers(void *esp_netif); +extern esp_err_t esp_wifi_clear_default_wifi_handlers(void); + +// +// Purpose of this module is to provide backward compatible version of esp-netif +// with legacy tcpip_adapter interface +// + +static const char* TAG = "tcpip_adapter_compat"; + +static esp_netif_t *s_esp_netifs[TCPIP_ADAPTER_IF_MAX] = { NULL }; +static const char* s_netif_keyif[TCPIP_ADAPTER_IF_MAX] = { + "WIFI_STA_DEF", + "WIFI_AP_DEF", + "ETH_DEF", +}; + +static bool s_tcpip_adapter_compat = false; + +void wifi_start(void *esp_netif, esp_event_base_t base, int32_t event_id, void *data); + +static void wifi_create_and_start_ap(void *esp_netif, esp_event_base_t base, int32_t event_id, void *data) +{ + if (s_esp_netifs[TCPIP_ADAPTER_IF_AP] == NULL) { + esp_netif_config_t cfg = ESP_NETIF_DEFAULT_WIFI_AP(); + esp_netif_t *ap_netif = esp_netif_new(&cfg); + + _esp_wifi_set_default_ap_netif(ap_netif); + s_esp_netifs[TCPIP_ADAPTER_IF_AP] = ap_netif; + } +} + +static void wifi_create_and_start_sta(void *esp_netif, esp_event_base_t base, int32_t event_id, void *data) +{ + if (s_esp_netifs[TCPIP_ADAPTER_IF_STA] == NULL) { + esp_netif_config_t cfg = ESP_NETIF_DEFAULT_WIFI_STA(); + esp_netif_t *sta_netif = esp_netif_new(&cfg); + + _esp_wifi_set_default_sta_netif(sta_netif); + s_esp_netifs[TCPIP_ADAPTER_IF_STA] = sta_netif; + } +} + +static inline esp_netif_t * netif_from_if(tcpip_adapter_if_t interface) +{ + if (interface < TCPIP_ADAPTER_IF_MAX) { + if (s_esp_netifs[interface] == NULL) { + s_esp_netifs[interface] = esp_netif_get_handle_from_ifkey(s_netif_keyif[interface]); + if (s_esp_netifs[interface] == NULL && s_tcpip_adapter_compat) { + // if not found in compat mode -> create it + if (interface == TCPIP_ADAPTER_IF_STA) { + wifi_create_and_start_sta(NULL, 0, 0, NULL); + s_esp_netifs[interface] = esp_netif_get_handle_from_ifkey("WIFI_STA_DEF"); + } else if (interface == TCPIP_ADAPTER_IF_AP) { + wifi_create_and_start_ap(NULL, 0, 0, NULL); + s_esp_netifs[interface] = esp_netif_get_handle_from_ifkey("WIFI_AP_DEF"); + } + } + } + return s_esp_netifs[interface]; + } + return NULL; +} + +void tcpip_adapter_init(void) +{ + s_tcpip_adapter_compat = true; + esp_err_t err; + if (ESP_OK != (err = esp_netif_init())) { + ESP_LOGE(TAG, "ESP-NETIF initialization failed with %d in tcpip_adapter compatibility mode", err); + } +} + +static void tcpip_adapter_eth_start(void *esp_netif, esp_event_base_t base, int32_t event_id, void *data) +{ + esp_eth_handle_t eth_handle = *(esp_eth_handle_t*)data; + esp_netif_attach(esp_netif, eth_handle); +} + +esp_err_t tcpip_adapter_set_default_eth_handlers(void) +{ + if (s_tcpip_adapter_compat) { + esp_netif_config_t cfg = ESP_NETIF_DEFAULT_ETH(); + esp_netif_t *eth_netif = esp_netif_new(&cfg); + + s_esp_netifs[TCPIP_ADAPTER_IF_ETH] = eth_netif; + // provide a separate "after driver start" hook to attach + esp_err_t ret = esp_event_handler_register(ETH_EVENT, ETHERNET_EVENT_START, tcpip_adapter_eth_start, eth_netif); + if (ret != ESP_OK) { + ESP_LOGE(TAG, "Failed to register "); + return ret; + } + + return esp_eth_set_default_handlers(eth_netif); + } + return ESP_OK; + +} + +esp_err_t tcpip_adapter_eth_input(void *buffer, uint16_t len, void *eb) +{ + return esp_netif_receive(netif_from_if(TCPIP_ADAPTER_IF_ETH), buffer, len, eb); +} + +esp_err_t tcpip_adapter_start_eth(void* eth_driver) +{ +#if CONFIG_ESP_NETIF_USE_TCPIP_ADAPTER_COMPATIBLE_LAYER + if (s_tcpip_adapter_compat) { + esp_netif_t *esp_netif = netif_from_if(TCPIP_ADAPTER_IF_ETH); + esp_netif_attach(esp_netif, eth_driver); + } + return ESP_OK; +#else + ESP_LOGE(TAG, "%s: tcpip adapter compatibility layer is disabled", __func__); + return ESP_ERR_INVALID_STATE; +#endif +} + +esp_err_t tcpip_adapter_set_default_wifi_handlers(void) +{ +#if CONFIG_ESP_NETIF_USE_TCPIP_ADAPTER_COMPATIBLE_LAYER + if (s_tcpip_adapter_compat) { + // create instances and register default handlers only on start event + esp_err_t err = esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_START, wifi_create_and_start_sta, NULL); + if (err != ESP_OK) { + return err; + } + err = esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_AP_START, wifi_create_and_start_ap, NULL); + if (err != ESP_OK) { + return err; + } + _esp_wifi_set_default_wifi_handlers(); + } + return ESP_OK; +#else + ESP_LOGE(TAG, "%s: tcpip adapter compatibility layer is disabled", __func__); + return ESP_ERR_INVALID_STATE; +#endif +} + +esp_err_t tcpip_adapter_clear_default_wifi_handlers(void) +{ + return esp_wifi_clear_default_wifi_handlers(); +} + +tcpip_adapter_if_t tcpip_adapter_if_from_esp_netif(esp_netif_t *esp_netif) +{ + for (int i=0; i -#include - -#include "tcpip_adapter_internal.h" -#if CONFIG_TCPIP_LWIP - -#include "lwip/inet.h" -#include "lwip/tcpip.h" -#include "lwip/dhcp.h" -#include "lwip/ip_addr.h" -#include "lwip/ip6_addr.h" -#include "lwip/nd6.h" -#include "lwip/priv/tcpip_priv.h" -#include "lwip/netif.h" -#if LWIP_DNS /* don't build if not configured for use in lwipopts.h */ -#include "lwip/dns.h" -#include "lwip/netif.h" -#endif -#include "netif/wlanif.h" -#ifdef CONFIG_ETH_ENABLED -#include "netif/ethernetif.h" -#endif -#include "netif/nettestif.h" - -#include "dhcpserver/dhcpserver.h" -#include "dhcpserver/dhcpserver_options.h" - -#include "esp_event.h" -#include "esp_log.h" - -static struct netif *esp_netif[TCPIP_ADAPTER_IF_MAX]; -static tcpip_adapter_ip_info_t esp_ip[TCPIP_ADAPTER_IF_MAX]; -static tcpip_adapter_ip_info_t esp_ip_old[TCPIP_ADAPTER_IF_MAX]; -static tcpip_adapter_ip6_info_t esp_ip6[TCPIP_ADAPTER_IF_MAX]; -static netif_init_fn esp_netif_init_fn[TCPIP_ADAPTER_IF_MAX]; -static tcpip_adapter_ip_lost_timer_t esp_ip_lost_timer[TCPIP_ADAPTER_IF_MAX]; - -static tcpip_adapter_dhcp_status_t dhcps_status = TCPIP_ADAPTER_DHCP_INIT; -static tcpip_adapter_dhcp_status_t dhcpc_status[TCPIP_ADAPTER_IF_MAX] = {TCPIP_ADAPTER_DHCP_INIT}; -static esp_err_t tcpip_adapter_start_api(tcpip_adapter_api_msg_t *msg); -static esp_err_t tcpip_adapter_stop_api(tcpip_adapter_api_msg_t *msg); -static esp_err_t tcpip_adapter_up_api(tcpip_adapter_api_msg_t *msg); -static esp_err_t tcpip_adapter_down_api(tcpip_adapter_api_msg_t *msg); -static esp_err_t tcpip_adapter_set_ip_info_api(tcpip_adapter_api_msg_t *msg); -static esp_err_t tcpip_adapter_set_dns_info_api(tcpip_adapter_api_msg_t *msg); -static esp_err_t tcpip_adapter_get_dns_info_api(tcpip_adapter_api_msg_t *msg); -static esp_err_t tcpip_adapter_create_ip6_linklocal_api(tcpip_adapter_api_msg_t *msg); -static esp_err_t tcpip_adapter_dhcps_start_api(tcpip_adapter_api_msg_t *msg); -static esp_err_t tcpip_adapter_dhcps_stop_api(tcpip_adapter_api_msg_t *msg); -static esp_err_t tcpip_adapter_dhcpc_start_api(tcpip_adapter_api_msg_t *msg); -static esp_err_t tcpip_adapter_dhcpc_stop_api(tcpip_adapter_api_msg_t *msg); -static esp_err_t tcpip_adapter_set_hostname_api(tcpip_adapter_api_msg_t *msg); -static esp_err_t tcpip_adapter_reset_ip_info(tcpip_adapter_if_t tcpip_if); -static esp_err_t tcpip_adapter_start_ip_lost_timer(tcpip_adapter_if_t tcpip_if); -static void tcpip_adapter_ip_lost_timer(void *arg); -static sys_sem_t api_sync_sem = NULL; -static bool tcpip_inited = false; -static sys_sem_t api_lock_sem = NULL; -extern sys_thread_t g_lwip_task; -static const char *TAG = "tcpip_adapter"; - -ESP_EVENT_DEFINE_BASE(IP_EVENT); - -static void tcpip_adapter_api_cb(void *api_msg) -{ - tcpip_adapter_api_msg_t *msg = (tcpip_adapter_api_msg_t *)api_msg; - - if (!msg || !msg->api_fn) { - ESP_LOGD(TAG, "null msg/api_fn"); - return; - } - - msg->ret = msg->api_fn(msg); - ESP_LOGV(TAG, "call api in lwip: ret=0x%x, give sem", msg->ret); - sys_sem_signal(&api_sync_sem); - - return; -} - -static void tcpip_adapter_dhcps_cb(u8_t client_ip[4]) -{ - int ret; - - ESP_LOGI(TAG, "softAP assign IP to station,IP is: %d.%d.%d.%d", - client_ip[0], client_ip[1], client_ip[2], client_ip[3]); - ip_event_ap_staipassigned_t evt; - - memset(&evt, 0, sizeof(ip_event_ap_staipassigned_t)); - memcpy((char *)&evt.ip.addr, (char *)client_ip, sizeof(evt.ip.addr)); - ret = esp_event_send_internal(IP_EVENT, IP_EVENT_AP_STAIPASSIGNED, &evt, sizeof(evt), 0); - if (ESP_OK != ret) { - ESP_LOGE(TAG, "dhcps cb: failed to post IP_EVENT_AP_STAIPASSIGNED (%x)", ret); - } -} - -void tcpip_adapter_init(void) -{ - int ret; - - if (tcpip_inited == false) { - tcpip_inited = true; - - tcpip_init(NULL, NULL); - - memset(esp_ip, 0, sizeof(tcpip_adapter_ip_info_t)*TCPIP_ADAPTER_IF_MAX); - memset(esp_ip_old, 0, sizeof(tcpip_adapter_ip_info_t)*TCPIP_ADAPTER_IF_MAX); - - IP4_ADDR(&esp_ip[TCPIP_ADAPTER_IF_AP].ip, 192, 168 , 4, 1); - IP4_ADDR(&esp_ip[TCPIP_ADAPTER_IF_AP].gw, 192, 168 , 4, 1); - IP4_ADDR(&esp_ip[TCPIP_ADAPTER_IF_AP].netmask, 255, 255 , 255, 0); - ret = sys_sem_new(&api_sync_sem, 0); - if (ERR_OK != ret) { - ESP_LOGE(TAG, "tcpip adatper api sync sem init fail"); - } - - ret = sys_sem_new(&api_lock_sem, 1); - if (ERR_OK != ret) { - ESP_LOGE(TAG, "tcpip adatper api lock sem init fail"); - } - } -} - -static inline netif_init_fn tcpip_if_to_netif_init_fn(tcpip_adapter_if_t tcpip_if) -{ - if (tcpip_if < TCPIP_ADAPTER_IF_MAX) { - return esp_netif_init_fn[tcpip_if]; - } else { - return NULL; - } -} - -static int tcpip_adapter_ipc_check(tcpip_adapter_api_msg_t *msg) -{ -#if TCPIP_ADAPTER_TRHEAD_SAFE - xTaskHandle local_task = xTaskGetCurrentTaskHandle(); - - if (local_task == g_lwip_task) { - return TCPIP_ADAPTER_IPC_LOCAL; - } - - sys_arch_sem_wait(&api_lock_sem, 0); - tcpip_send_msg_wait_sem((tcpip_callback_fn)tcpip_adapter_api_cb, msg, &api_sync_sem); - sys_sem_signal(&api_lock_sem); - - return TCPIP_ADAPTER_IPC_REMOTE; -#else - return TCPIP_ADAPTER_IPC_LOCAL; -#endif -} - -static esp_err_t tcpip_adapter_update_default_netif(void) -{ - if (esp_netif[TCPIP_ADAPTER_IF_STA] != NULL && netif_is_up(esp_netif[TCPIP_ADAPTER_IF_STA])) { - netif_set_default(esp_netif[TCPIP_ADAPTER_IF_STA]); - } else if (esp_netif[TCPIP_ADAPTER_IF_ETH] != NULL && netif_is_up(esp_netif[TCPIP_ADAPTER_IF_ETH])) { - netif_set_default(esp_netif[TCPIP_ADAPTER_IF_ETH]); - } else if (esp_netif[TCPIP_ADAPTER_IF_AP] != NULL && netif_is_up(esp_netif[TCPIP_ADAPTER_IF_AP])) { - netif_set_default(esp_netif[TCPIP_ADAPTER_IF_AP]); - } else if(esp_netif[TCPIP_ADAPTER_IF_TEST] != NULL && netif_is_up(esp_netif[TCPIP_ADAPTER_IF_TEST])) { - netif_set_default(esp_netif[TCPIP_ADAPTER_IF_TEST]); - } - - return ESP_OK; -} - -static esp_err_t tcpip_adapter_start(tcpip_adapter_if_t tcpip_if, uint8_t *mac, tcpip_adapter_ip_info_t *ip_info, void *args) -{ - netif_init_fn netif_init; - - TCPIP_ADAPTER_IPC_CALL(tcpip_if, mac, ip_info, args, tcpip_adapter_start_api); - - if (tcpip_if >= TCPIP_ADAPTER_IF_MAX || mac == NULL || ip_info == NULL) { - return ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS; - } - - if (esp_netif[tcpip_if] == NULL || !netif_is_up(esp_netif[tcpip_if])) { - if (esp_netif[tcpip_if] == NULL) { - esp_netif[tcpip_if] = calloc(1, sizeof(*esp_netif[tcpip_if])); - } - - if (esp_netif[tcpip_if] == NULL) { - return ESP_ERR_NO_MEM; - } - memcpy(esp_netif[tcpip_if]->hwaddr, mac, NETIF_MAX_HWADDR_LEN); - - netif_init = tcpip_if_to_netif_init_fn(tcpip_if); - assert(netif_init != NULL); - netif_add(esp_netif[tcpip_if], &ip_info->ip, &ip_info->netmask, &ip_info->gw, args, netif_init, tcpip_input); - -#if ESP_GRATUITOUS_ARP - if (tcpip_if == TCPIP_ADAPTER_IF_STA || tcpip_if == TCPIP_ADAPTER_IF_ETH) { - netif_set_garp_flag(esp_netif[tcpip_if]); - } -#endif - - } - - if (tcpip_if == TCPIP_ADAPTER_IF_AP) { - netif_set_up(esp_netif[tcpip_if]); - - if (dhcps_status == TCPIP_ADAPTER_DHCP_INIT) { - dhcps_set_new_lease_cb(tcpip_adapter_dhcps_cb); - - dhcps_start(esp_netif[tcpip_if], ip_info->ip); - - ESP_LOGD(TAG, "dhcp server start:(ip: " IPSTR ", mask: " IPSTR ", gw: " IPSTR ")", - IP2STR(&ip_info->ip), IP2STR(&ip_info->netmask), IP2STR(&ip_info->gw)); - - dhcps_status = TCPIP_ADAPTER_DHCP_STARTED; - } - } else if (tcpip_if == TCPIP_ADAPTER_IF_TEST) { - netif_set_up(esp_netif[tcpip_if]); - } - - tcpip_adapter_update_default_netif(); - - return ESP_OK; -} - -esp_err_t tcpip_adapter_eth_start(uint8_t *mac, tcpip_adapter_ip_info_t *ip_info, void *args) -{ -#ifdef CONFIG_ETH_ENABLED - esp_netif_init_fn[TCPIP_ADAPTER_IF_ETH] = ethernetif_init; - return tcpip_adapter_start(TCPIP_ADAPTER_IF_ETH, mac, ip_info, args); -#else - return ESP_ERR_NOT_SUPPORTED; -#endif -} - -esp_err_t tcpip_adapter_sta_start(uint8_t *mac, tcpip_adapter_ip_info_t *ip_info) -{ - esp_netif_init_fn[TCPIP_ADAPTER_IF_STA] = wlanif_init_sta; - return tcpip_adapter_start(TCPIP_ADAPTER_IF_STA, mac, ip_info, NULL); -} - -esp_err_t tcpip_adapter_test_start(uint8_t *mac, tcpip_adapter_ip_info_t *ip_info) -{ - esp_netif_init_fn[TCPIP_ADAPTER_IF_TEST] = nettestif_init; - return tcpip_adapter_start(TCPIP_ADAPTER_IF_TEST, mac, ip_info, NULL); -} - - -esp_err_t tcpip_adapter_ap_start(uint8_t *mac, tcpip_adapter_ip_info_t *ip_info) -{ - esp_netif_init_fn[TCPIP_ADAPTER_IF_AP] = wlanif_init_ap; - return tcpip_adapter_start(TCPIP_ADAPTER_IF_AP, mac, ip_info, NULL); -} - -static esp_err_t tcpip_adapter_start_api(tcpip_adapter_api_msg_t *msg) -{ - return tcpip_adapter_start(msg->tcpip_if, msg->mac, msg->ip_info, msg->data); -} - -esp_err_t tcpip_adapter_stop(tcpip_adapter_if_t tcpip_if) -{ - TCPIP_ADAPTER_IPC_CALL(tcpip_if, 0, 0, 0, tcpip_adapter_stop_api); - - if (tcpip_if >= TCPIP_ADAPTER_IF_MAX) { - return ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS; - } - - if (esp_netif[tcpip_if] == NULL) { - return ESP_ERR_TCPIP_ADAPTER_IF_NOT_READY; - } - - if (!netif_is_up(esp_netif[tcpip_if])) { - netif_remove(esp_netif[tcpip_if]); - return ESP_ERR_TCPIP_ADAPTER_IF_NOT_READY; - } - - if (tcpip_if == TCPIP_ADAPTER_IF_AP) { - dhcps_stop(esp_netif[tcpip_if]); // TODO: dhcps checks status by its self - if (TCPIP_ADAPTER_DHCP_STOPPED != dhcps_status) { - dhcps_status = TCPIP_ADAPTER_DHCP_INIT; - } - } else if (tcpip_if == TCPIP_ADAPTER_IF_STA || tcpip_if == TCPIP_ADAPTER_IF_ETH) { - dhcp_release(esp_netif[tcpip_if]); - dhcp_stop(esp_netif[tcpip_if]); - dhcp_cleanup(esp_netif[tcpip_if]); - - dhcpc_status[tcpip_if] = TCPIP_ADAPTER_DHCP_INIT; - - tcpip_adapter_reset_ip_info(tcpip_if); - } - - netif_set_down(esp_netif[tcpip_if]); - netif_remove(esp_netif[tcpip_if]); - tcpip_adapter_update_default_netif(); - - return ESP_OK; -} - -static esp_err_t tcpip_adapter_stop_api(tcpip_adapter_api_msg_t *msg) -{ - msg->ret = tcpip_adapter_stop(msg->tcpip_if); - return msg->ret; -} - -esp_err_t tcpip_adapter_up(tcpip_adapter_if_t tcpip_if) -{ - TCPIP_ADAPTER_IPC_CALL(tcpip_if, 0, 0, 0, tcpip_adapter_up_api); - - if (tcpip_if == TCPIP_ADAPTER_IF_STA || tcpip_if == TCPIP_ADAPTER_IF_ETH ) { - if (esp_netif[tcpip_if] == NULL) { - return ESP_ERR_TCPIP_ADAPTER_IF_NOT_READY; - } - - /* use last obtained ip, or static ip */ - netif_set_addr(esp_netif[tcpip_if], &esp_ip[tcpip_if].ip, &esp_ip[tcpip_if].netmask, &esp_ip[tcpip_if].gw); - netif_set_up(esp_netif[tcpip_if]); - } - - tcpip_adapter_update_default_netif(); - - return ESP_OK; -} - -static esp_err_t tcpip_adapter_up_api(tcpip_adapter_api_msg_t *msg) -{ - msg->ret = tcpip_adapter_up(msg->tcpip_if); - return msg->ret; -} - -esp_err_t tcpip_adapter_down(tcpip_adapter_if_t tcpip_if) -{ - TCPIP_ADAPTER_IPC_CALL(tcpip_if, 0, 0, 0, tcpip_adapter_down_api); - - if (tcpip_if == TCPIP_ADAPTER_IF_STA || tcpip_if == TCPIP_ADAPTER_IF_ETH ) { - if (esp_netif[tcpip_if] == NULL) { - return ESP_ERR_TCPIP_ADAPTER_IF_NOT_READY; - } - - if (dhcpc_status[tcpip_if] == TCPIP_ADAPTER_DHCP_STARTED) { - dhcp_stop(esp_netif[tcpip_if]); - - dhcpc_status[tcpip_if] = TCPIP_ADAPTER_DHCP_INIT; - - tcpip_adapter_reset_ip_info(tcpip_if); - } - - netif_set_addr(esp_netif[tcpip_if], IP4_ADDR_ANY4, IP4_ADDR_ANY4, IP4_ADDR_ANY4); - netif_set_down(esp_netif[tcpip_if]); - tcpip_adapter_start_ip_lost_timer(tcpip_if); - } - - tcpip_adapter_update_default_netif(); - - return ESP_OK; -} - -static esp_err_t tcpip_adapter_down_api(tcpip_adapter_api_msg_t *msg) -{ - return tcpip_adapter_down(msg->tcpip_if); -} - -esp_err_t tcpip_adapter_set_old_ip_info_api(tcpip_adapter_api_msg_t *msg) -{ - memcpy(&esp_ip_old[msg->tcpip_if], msg->ip_info, sizeof(tcpip_adapter_ip_info_t)); - return ESP_OK; -} - -esp_err_t tcpip_adapter_set_old_ip_info(tcpip_adapter_if_t tcpip_if, const tcpip_adapter_ip_info_t *ip_info) -{ - if (tcpip_if >= TCPIP_ADAPTER_IF_MAX || ip_info == NULL) { - return ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS; - } - - TCPIP_ADAPTER_IPC_CALL(tcpip_if, 0, ip_info, 0, tcpip_adapter_set_old_ip_info_api); - - return ESP_OK; -} - -esp_err_t tcpip_adapter_get_old_ip_info(tcpip_adapter_if_t tcpip_if, tcpip_adapter_ip_info_t *ip_info) -{ - if (tcpip_if >= TCPIP_ADAPTER_IF_MAX || ip_info == NULL) { - return ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS; - } - - memcpy(ip_info, &esp_ip_old[tcpip_if], sizeof(tcpip_adapter_ip_info_t)); - return ESP_OK; -} - -esp_err_t tcpip_adapter_get_ip_info(tcpip_adapter_if_t tcpip_if, tcpip_adapter_ip_info_t *ip_info) -{ - struct netif *p_netif; - - if (tcpip_if >= TCPIP_ADAPTER_IF_MAX || ip_info == NULL) { - return ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS; - } - - p_netif = esp_netif[tcpip_if]; - - if (p_netif != NULL && netif_is_up(p_netif)) { - ip4_addr_set(&ip_info->ip, ip_2_ip4(&p_netif->ip_addr)); - ip4_addr_set(&ip_info->netmask, ip_2_ip4(&p_netif->netmask)); - ip4_addr_set(&ip_info->gw, ip_2_ip4(&p_netif->gw)); - - return ESP_OK; - } - - ip4_addr_copy(ip_info->ip, esp_ip[tcpip_if].ip); - ip4_addr_copy(ip_info->gw, esp_ip[tcpip_if].gw); - ip4_addr_copy(ip_info->netmask, esp_ip[tcpip_if].netmask); - - return ESP_OK; -} - -esp_err_t tcpip_adapter_set_ip_info(tcpip_adapter_if_t tcpip_if, const tcpip_adapter_ip_info_t *ip_info) -{ - struct netif *p_netif; - tcpip_adapter_dhcp_status_t status; - - TCPIP_ADAPTER_IPC_CALL(tcpip_if, 0, ip_info, 0, tcpip_adapter_set_ip_info_api); - - if (tcpip_if >= TCPIP_ADAPTER_IF_MAX || ip_info == NULL) { - return ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS; - } - - if (tcpip_if == TCPIP_ADAPTER_IF_AP) { - tcpip_adapter_dhcps_get_status(tcpip_if, &status); - - if (status != TCPIP_ADAPTER_DHCP_STOPPED) { - return ESP_ERR_TCPIP_ADAPTER_DHCP_NOT_STOPPED; - } - } else if (tcpip_if == TCPIP_ADAPTER_IF_STA || tcpip_if == TCPIP_ADAPTER_IF_ETH ) { - tcpip_adapter_dhcpc_get_status(tcpip_if, &status); - - if (status != TCPIP_ADAPTER_DHCP_STOPPED) { - return ESP_ERR_TCPIP_ADAPTER_DHCP_NOT_STOPPED; - } -#if LWIP_DNS /* don't build if not configured for use in lwipopts.h */ - dns_clear_servers(true); -#endif - } - - ip4_addr_copy(esp_ip[tcpip_if].ip, ip_info->ip); - ip4_addr_copy(esp_ip[tcpip_if].gw, ip_info->gw); - ip4_addr_copy(esp_ip[tcpip_if].netmask, ip_info->netmask); - - p_netif = esp_netif[tcpip_if]; - - if (p_netif != NULL && netif_is_up(p_netif)) { - netif_set_addr(p_netif, &ip_info->ip, &ip_info->netmask, &ip_info->gw); - if (tcpip_if == TCPIP_ADAPTER_IF_STA || tcpip_if == TCPIP_ADAPTER_IF_ETH) { - if (!(ip4_addr_isany_val(ip_info->ip) || ip4_addr_isany_val(ip_info->netmask) || ip4_addr_isany_val(ip_info->gw))) { - - ip_event_t evt_id = IP_EVENT_STA_GOT_IP; - ip_event_got_ip_t evt; - int ret; - - memset(&evt, 0, sizeof(ip_event_got_ip_t)); - evt.if_index = tcpip_if; - evt.ip_changed = false; - - if (memcmp(ip_info, &esp_ip_old[tcpip_if], sizeof(tcpip_adapter_ip_info_t))) { - evt.ip_changed = true; - } - - if (tcpip_if == TCPIP_ADAPTER_IF_ETH) { - evt_id = IP_EVENT_ETH_GOT_IP; - } - - memcpy(&evt.ip_info, ip_info, sizeof(tcpip_adapter_ip_info_t)); - memcpy(&esp_ip_old[tcpip_if], ip_info, sizeof(tcpip_adapter_ip_info_t)); - ret = esp_event_send_internal(IP_EVENT, evt_id, &evt, sizeof(evt), 0); - if (ESP_OK != ret) { - ESP_LOGE(TAG, "set ip info: failed to post got ip event (%x)", ret); - } - - ESP_LOGD(TAG, "if%d tcpip adapter set static ip: ip changed=%d", tcpip_if, evt.ip_changed); - } - } - } - - return ESP_OK; -} - -static esp_err_t tcpip_adapter_set_ip_info_api(tcpip_adapter_api_msg_t *msg) -{ - return tcpip_adapter_set_ip_info(msg->tcpip_if, msg->ip_info); -} - -static void tcpip_adapter_nd6_cb(struct netif *p_netif, uint8_t ip_idex) -{ - tcpip_adapter_ip6_info_t *ip6_info; - int ret; - - ip_event_got_ip6_t evt; - memset(&evt, 0, sizeof(ip_event_got_ip6_t)); - //notify event - - if (!p_netif) { - ESP_LOGD(TAG, "null p_netif=%p", p_netif); - return; - } - - if (p_netif == esp_netif[TCPIP_ADAPTER_IF_STA]) { - ip6_info = &esp_ip6[TCPIP_ADAPTER_IF_STA]; - evt.if_index = TCPIP_ADAPTER_IF_STA; - } else if (p_netif == esp_netif[TCPIP_ADAPTER_IF_AP]) { - ip6_info = &esp_ip6[TCPIP_ADAPTER_IF_AP]; - evt.if_index = TCPIP_ADAPTER_IF_AP; - } else if (p_netif == esp_netif[TCPIP_ADAPTER_IF_ETH]) { - ip6_info = &esp_ip6[TCPIP_ADAPTER_IF_ETH]; - evt.if_index = TCPIP_ADAPTER_IF_ETH; - } else { - return; - } - - ip6_addr_set(&ip6_info->ip, ip_2_ip6(&p_netif->ip6_addr[ip_idex])); - - memcpy(&evt.ip6_info, ip6_info, sizeof(tcpip_adapter_ip6_info_t)); - ret = esp_event_send_internal(IP_EVENT, IP_EVENT_GOT_IP6, &evt, sizeof(evt), 0); - if (ESP_OK != ret) { - ESP_LOGE(TAG, "nd6 cb: failed to post IP_EVENT_GOT_IP6 (%x)", ret); - } -} - -esp_err_t tcpip_adapter_create_ip6_linklocal(tcpip_adapter_if_t tcpip_if) -{ - struct netif *p_netif; - - TCPIP_ADAPTER_IPC_CALL(tcpip_if, 0, 0, 0, tcpip_adapter_create_ip6_linklocal_api); - - if (tcpip_if >= TCPIP_ADAPTER_IF_MAX) { - return ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS; - } - - p_netif = esp_netif[tcpip_if]; - if (p_netif != NULL && netif_is_up(p_netif)) { - netif_create_ip6_linklocal_address(p_netif, 1); - nd6_set_cb(p_netif, tcpip_adapter_nd6_cb); - - return ESP_OK; - } else { - return ESP_FAIL; - } -} - -static esp_err_t tcpip_adapter_create_ip6_linklocal_api(tcpip_adapter_api_msg_t *msg) -{ - return tcpip_adapter_create_ip6_linklocal(msg->tcpip_if); -} - -esp_err_t tcpip_adapter_get_ip6_linklocal(tcpip_adapter_if_t tcpip_if, ip6_addr_t *if_ip6) -{ - struct netif *p_netif; - - if (tcpip_if >= TCPIP_ADAPTER_IF_MAX || if_ip6 == NULL) { - return ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS; - } - - p_netif = esp_netif[tcpip_if]; - if (p_netif != NULL && netif_is_up(p_netif) && ip6_addr_ispreferred(netif_ip6_addr_state(p_netif, 0))) { - memcpy(if_ip6, &p_netif->ip6_addr[0], sizeof(ip6_addr_t)); - } else { - return ESP_FAIL; - } - return ESP_OK; -} - -#if 0 -esp_err_t tcpip_adapter_get_mac(tcpip_adapter_if_t tcpip_if, uint8_t mac[6]) -{ - struct netif *p_netif; - - if (tcpip_if >= TCPIP_ADAPTER_IF_MAX || mac == NULL) { - return ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS; - } - - p_netif = esp_netif[tcpip_if]; - - if (p_netif != NULL) { - memcpy(mac, p_netif->hwaddr, NETIF_MAX_HWADDR_LEN); - - return ESP_OK; - } - - return ESP_ERR_TCPIP_ADAPTER_IF_NOT_READY; -} - -esp_err_t tcpip_adapter_set_mac(tcpip_adapter_if_t tcpip_if, uint8_t mac[6]) -{ - struct netif *p_netif; - - if (tcpip_if >= TCPIP_ADAPTER_IF_MAX || mac == NULL) { - return ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS; - } - - p_netif = esp_netif[tcpip_if]; - - if (p_netif != NULL) { - memcpy(p_netif->hwaddr, mac, NETIF_MAX_HWADDR_LEN); - - return ESP_OK; - } - - return ESP_ERR_TCPIP_ADAPTER_IF_NOT_READY; -} -#endif - -esp_err_t tcpip_adapter_dhcps_option(tcpip_adapter_dhcp_option_mode_t opt_op, tcpip_adapter_dhcp_option_id_t opt_id, void *opt_val, uint32_t opt_len) -{ - void *opt_info = dhcps_option_info(opt_id, opt_len); - - if (opt_info == NULL || opt_val == NULL) { - return ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS; - } - - if (opt_op == TCPIP_ADAPTER_OP_GET) { - if (dhcps_status == TCPIP_ADAPTER_DHCP_STOPPED) { - return ESP_ERR_TCPIP_ADAPTER_DHCP_ALREADY_STOPPED; - } - - switch (opt_id) { - case IP_ADDRESS_LEASE_TIME: { - *(uint32_t *)opt_val = *(uint32_t *)opt_info; - break; - } - case REQUESTED_IP_ADDRESS: { - memcpy(opt_val, opt_info, opt_len); - break; - } - case ROUTER_SOLICITATION_ADDRESS: { - if ((*(uint8_t *)opt_info) & OFFER_ROUTER) { - *(uint8_t *)opt_val = 1; - } else { - *(uint8_t *)opt_val = 0; - } - break; - } - case DOMAIN_NAME_SERVER: { - if ((*(uint8_t *)opt_info) & OFFER_DNS) { - *(uint8_t *)opt_val = 1; - } else { - *(uint8_t *)opt_val = 0; - } - break; - } - default: - break; - } - } else if (opt_op == TCPIP_ADAPTER_OP_SET) { - if (dhcps_status == TCPIP_ADAPTER_DHCP_STARTED) { - return ESP_ERR_TCPIP_ADAPTER_DHCP_ALREADY_STARTED; - } - - switch (opt_id) { - case IP_ADDRESS_LEASE_TIME: { - if (*(uint32_t *)opt_val != 0) { - *(uint32_t *)opt_info = *(uint32_t *)opt_val; - } else { - *(uint32_t *)opt_info = DHCPS_LEASE_TIME_DEF; - } - break; - } - case REQUESTED_IP_ADDRESS: { - tcpip_adapter_ip_info_t info; - uint32_t softap_ip = 0; - uint32_t start_ip = 0; - uint32_t end_ip = 0; - dhcps_lease_t *poll = opt_val; - - if (poll->enable) { - memset(&info, 0x00, sizeof(tcpip_adapter_ip_info_t)); - tcpip_adapter_get_ip_info(ESP_IF_WIFI_AP, &info); - softap_ip = htonl(info.ip.addr); - start_ip = htonl(poll->start_ip.addr); - end_ip = htonl(poll->end_ip.addr); - - /*config ip information can't contain local ip*/ - if ((start_ip <= softap_ip) && (softap_ip <= end_ip)) { - return ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS; - } - - /*config ip information must be in the same segment as the local ip*/ - softap_ip >>= 8; - if ((start_ip >> 8 != softap_ip) - || (end_ip >> 8 != softap_ip)) { - return ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS; - } - - if (end_ip - start_ip > DHCPS_MAX_LEASE) { - return ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS; - } - } - - memcpy(opt_info, opt_val, opt_len); - break; - } - case ROUTER_SOLICITATION_ADDRESS: { - if (*(uint8_t *)opt_val) { - *(uint8_t *)opt_info |= OFFER_ROUTER; - } else { - *(uint8_t *)opt_info &= ((~OFFER_ROUTER) & 0xFF); - } - break; - } - case DOMAIN_NAME_SERVER: { - if (*(uint8_t *)opt_val) { - *(uint8_t *)opt_info |= OFFER_DNS; - } else { - *(uint8_t *)opt_info &= ((~OFFER_DNS) & 0xFF); - } - break; - } - - default: - break; - } - dhcps_set_option_info(opt_id, opt_info, opt_len); - } else { - return ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS; - } - - return ESP_OK; -} - -esp_err_t tcpip_adapter_set_dns_info(tcpip_adapter_if_t tcpip_if, tcpip_adapter_dns_type_t type, tcpip_adapter_dns_info_t *dns) -{ - tcpip_adapter_dns_param_t dns_param; - - dns_param.dns_type = type; - dns_param.dns_info = dns; - - TCPIP_ADAPTER_IPC_CALL(tcpip_if, type, 0, &dns_param, tcpip_adapter_set_dns_info_api); - - if (tcpip_if >= TCPIP_ADAPTER_IF_MAX) { - ESP_LOGD(TAG, "set dns invalid if=%d", tcpip_if); - return ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS; - } - - if (!dns) { - ESP_LOGD(TAG, "set dns null dns"); - return ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS; - } - - if (type >= TCPIP_ADAPTER_DNS_MAX) { - ESP_LOGD(TAG, "set dns invalid type=%d", type); - return ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS; - } - - if (ip4_addr_isany_val(dns->ip.u_addr.ip4)) { - ESP_LOGD(TAG, "set dns invalid dns"); - return ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS; - } - - ESP_LOGD(TAG, "set dns if=%d type=%d dns=%x", tcpip_if, type, dns->ip.u_addr.ip4.addr); - dns->ip.type = IPADDR_TYPE_V4; - - if (tcpip_if == TCPIP_ADAPTER_IF_STA || tcpip_if == TCPIP_ADAPTER_IF_ETH) { - dns_setserver(type, &(dns->ip)); - } else { - if (type != TCPIP_ADAPTER_DNS_MAIN) { - ESP_LOGD(TAG, "set dns invalid type"); - return ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS; - } else { - dhcps_dns_setserver(&(dns->ip)); - } - } - - return ESP_OK; -} - -static esp_err_t tcpip_adapter_set_dns_info_api(tcpip_adapter_api_msg_t *msg) -{ - tcpip_adapter_dns_param_t *dns_param = (tcpip_adapter_dns_param_t *)msg->data; - - return tcpip_adapter_set_dns_info(msg->tcpip_if, dns_param->dns_type, dns_param->dns_info); -} - -esp_err_t tcpip_adapter_get_dns_info(tcpip_adapter_if_t tcpip_if, tcpip_adapter_dns_type_t type, tcpip_adapter_dns_info_t *dns) -{ - tcpip_adapter_dns_param_t dns_param; - - dns_param.dns_type = type; - dns_param.dns_info = dns; - const ip_addr_t* dns_ip = NULL; - - - TCPIP_ADAPTER_IPC_CALL(tcpip_if, type, 0, &dns_param, tcpip_adapter_get_dns_info_api); - if (!dns) { - ESP_LOGD(TAG, "get dns null dns"); - return ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS; - } - - if (type >= TCPIP_ADAPTER_DNS_MAX) { - ESP_LOGD(TAG, "get dns invalid type=%d", type); - return ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS; - } - - if (tcpip_if >= TCPIP_ADAPTER_IF_MAX) { - ESP_LOGD(TAG, "get dns invalid tcpip_if=%d", tcpip_if); - return ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS; - } - - if (tcpip_if == TCPIP_ADAPTER_IF_STA || tcpip_if == TCPIP_ADAPTER_IF_ETH) { - dns_ip = dns_getserver(type); - if(dns_ip != NULL){ - dns->ip = *dns_ip; - } - } else { - dns->ip.u_addr.ip4 = dhcps_dns_getserver(); - } - - return ESP_OK; -} - -static esp_err_t tcpip_adapter_get_dns_info_api(tcpip_adapter_api_msg_t *msg) -{ - tcpip_adapter_dns_param_t *dns_param = (tcpip_adapter_dns_param_t *)msg->data; - - return tcpip_adapter_get_dns_info(msg->tcpip_if, dns_param->dns_type, dns_param->dns_info); -} - -esp_err_t tcpip_adapter_dhcps_get_status(tcpip_adapter_if_t tcpip_if, tcpip_adapter_dhcp_status_t *status) -{ - *status = dhcps_status; - - return ESP_OK; -} - -esp_err_t tcpip_adapter_dhcps_start(tcpip_adapter_if_t tcpip_if) -{ - TCPIP_ADAPTER_IPC_CALL(tcpip_if, 0, 0, 0, tcpip_adapter_dhcps_start_api); - - /* only support ap now */ - if (tcpip_if != TCPIP_ADAPTER_IF_AP || tcpip_if >= TCPIP_ADAPTER_IF_MAX) { - ESP_LOGD(TAG, "dhcp server invalid if=%d", tcpip_if); - return ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS; - } - - if (dhcps_status != TCPIP_ADAPTER_DHCP_STARTED) { - struct netif *p_netif = esp_netif[tcpip_if]; - - if (p_netif != NULL && netif_is_up(p_netif)) { - tcpip_adapter_ip_info_t default_ip; - tcpip_adapter_get_ip_info(ESP_IF_WIFI_AP, &default_ip); - dhcps_start(p_netif, default_ip.ip); - dhcps_status = TCPIP_ADAPTER_DHCP_STARTED; - ESP_LOGD(TAG, "dhcp server start successfully"); - return ESP_OK; - } else { - ESP_LOGD(TAG, "dhcp server re init"); - dhcps_status = TCPIP_ADAPTER_DHCP_INIT; - return ESP_OK; - } - } - - ESP_LOGD(TAG, "dhcp server already start"); - return ESP_ERR_TCPIP_ADAPTER_DHCP_ALREADY_STARTED; -} - -static esp_err_t tcpip_adapter_dhcps_start_api(tcpip_adapter_api_msg_t *msg) -{ - return tcpip_adapter_dhcps_start(msg->tcpip_if); -} - - -esp_err_t tcpip_adapter_dhcps_stop(tcpip_adapter_if_t tcpip_if) -{ - TCPIP_ADAPTER_IPC_CALL(tcpip_if, 0, 0, 0, tcpip_adapter_dhcps_stop_api); - - /* only support ap now */ - if (tcpip_if != TCPIP_ADAPTER_IF_AP || tcpip_if >= TCPIP_ADAPTER_IF_MAX) { - ESP_LOGD(TAG, "dhcp server invalid if=%d", tcpip_if); - return ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS; - } - - if (dhcps_status == TCPIP_ADAPTER_DHCP_STARTED) { - struct netif *p_netif = esp_netif[tcpip_if]; - - if (p_netif != NULL) { - dhcps_stop(p_netif); - } else { - ESP_LOGD(TAG, "dhcp server if not ready"); - return ESP_ERR_TCPIP_ADAPTER_IF_NOT_READY; - } - } else if (dhcps_status == TCPIP_ADAPTER_DHCP_STOPPED) { - ESP_LOGD(TAG, "dhcp server already stoped"); - return ESP_ERR_TCPIP_ADAPTER_DHCP_ALREADY_STOPPED; - } - - ESP_LOGD(TAG, "dhcp server stop successfully"); - dhcps_status = TCPIP_ADAPTER_DHCP_STOPPED; - return ESP_OK; -} - -static esp_err_t tcpip_adapter_dhcps_stop_api(tcpip_adapter_api_msg_t *msg) -{ - return tcpip_adapter_dhcps_stop(msg->tcpip_if); -} - -esp_err_t tcpip_adapter_dhcpc_option(tcpip_adapter_dhcp_option_mode_t opt_op, tcpip_adapter_dhcp_option_id_t opt_id, void *opt_val, uint32_t opt_len) -{ - // TODO: when dhcp request timeout,change the retry count - return ESP_ERR_NOT_SUPPORTED; -} - -static void tcpip_adapter_dhcpc_cb(struct netif *netif) -{ - tcpip_adapter_ip_info_t *ip_info_old = NULL; - tcpip_adapter_ip_info_t *ip_info = NULL; - tcpip_adapter_if_t tcpip_if; - - if (!netif) { - ESP_LOGD(TAG, "null netif=%p", netif); - return; - } - - if ( netif == esp_netif[TCPIP_ADAPTER_IF_STA] ) { - tcpip_if = TCPIP_ADAPTER_IF_STA; - } else if (netif == esp_netif[TCPIP_ADAPTER_IF_ETH] ) { - tcpip_if = TCPIP_ADAPTER_IF_ETH; - } else { - ESP_LOGD(TAG, "err netif=%p", netif); - return; - } - - ESP_LOGD(TAG, "if%d dhcpc cb", tcpip_if); - ip_info = &esp_ip[tcpip_if]; - ip_info_old = &esp_ip_old[tcpip_if]; - - if ( !ip4_addr_cmp(ip_2_ip4(&netif->ip_addr), IP4_ADDR_ANY4) ) { - - //check whether IP is changed - if ( !ip4_addr_cmp(ip_2_ip4(&netif->ip_addr), (&ip_info->ip)) || - !ip4_addr_cmp(ip_2_ip4(&netif->netmask), (&ip_info->netmask)) || - !ip4_addr_cmp(ip_2_ip4(&netif->gw), (&ip_info->gw)) ) { - ip_event_got_ip_t evt; - ip_event_t evt_id; - int ret; - - memset(&evt, 0, sizeof(ip_event_got_ip_t)); - - ip4_addr_set(&ip_info->ip, ip_2_ip4(&netif->ip_addr)); - ip4_addr_set(&ip_info->netmask, ip_2_ip4(&netif->netmask)); - ip4_addr_set(&ip_info->gw, ip_2_ip4(&netif->gw)); - - //notify event - evt.if_index = tcpip_if; - if (tcpip_if == TCPIP_ADAPTER_IF_ETH) { - evt_id = IP_EVENT_ETH_GOT_IP; - evt.ip_changed = true; - } else { - evt_id = IP_EVENT_STA_GOT_IP; - evt.ip_changed = false; - } - - if (memcmp(ip_info, ip_info_old, sizeof(tcpip_adapter_ip_info_t))) { - evt.ip_changed = true; - } - - memcpy(&evt.ip_info, ip_info, sizeof(tcpip_adapter_ip_info_t)); - memcpy(ip_info_old, ip_info, sizeof(tcpip_adapter_ip_info_t)); - ESP_LOGD(TAG, "if%d ip changed=%d", tcpip_if, evt.ip_changed); - ret = esp_event_send_internal(IP_EVENT, evt_id, &evt, sizeof(evt), 0); - if (ESP_OK != ret) { - ESP_LOGE(TAG, "dhcpc cb: failed to post got ip event (%x)", ret); - } - } else { - ESP_LOGD(TAG, "if%d ip unchanged", tcpip_if); - } - } else { - if (!ip4_addr_cmp(&ip_info->ip, IP4_ADDR_ANY4)) { - tcpip_adapter_start_ip_lost_timer(tcpip_if); - } - } - - return; -} - -static esp_err_t tcpip_adapter_start_ip_lost_timer(tcpip_adapter_if_t tcpip_if) -{ - tcpip_adapter_ip_info_t *ip_info_old = &esp_ip_old[tcpip_if]; - struct netif *netif = esp_netif[tcpip_if]; - - ESP_LOGD(TAG, "if%d start ip lost tmr: enter", tcpip_if); - if (tcpip_if != TCPIP_ADAPTER_IF_STA) { - ESP_LOGD(TAG, "if%d start ip lost tmr: only sta support ip lost timer", tcpip_if); - return ESP_OK; - } - - if (esp_ip_lost_timer[tcpip_if].timer_running) { - ESP_LOGD(TAG, "if%d start ip lost tmr: already started", tcpip_if); - return ESP_OK; - } - - if ( netif && (CONFIG_NETIF_IP_LOST_TIMER_INTERVAL > 0) && !ip4_addr_isany_val(ip_info_old->ip)) { - esp_ip_lost_timer[tcpip_if].timer_running = true; - sys_timeout(CONFIG_NETIF_IP_LOST_TIMER_INTERVAL * 1000, tcpip_adapter_ip_lost_timer, (void *)tcpip_if); - ESP_LOGD(TAG, "if%d start ip lost tmr: interval=%d", tcpip_if, CONFIG_NETIF_IP_LOST_TIMER_INTERVAL); - return ESP_OK; - } - - ESP_LOGD(TAG, "if%d start ip lost tmr: no need start because netif=%p interval=%d ip=%x", - tcpip_if, netif, CONFIG_NETIF_IP_LOST_TIMER_INTERVAL, ip_info_old->ip.addr); - - return ESP_OK; -} - -static void tcpip_adapter_ip_lost_timer(void *arg) -{ - tcpip_adapter_if_t tcpip_if = (tcpip_adapter_if_t)arg; - - ESP_LOGD(TAG, "if%d ip lost tmr: enter", tcpip_if); - esp_ip_lost_timer[tcpip_if].timer_running = false; - - if (tcpip_if == TCPIP_ADAPTER_IF_STA) { - struct netif *netif = esp_netif[tcpip_if]; - - if ( (!netif) || (netif && ip4_addr_cmp(ip_2_ip4(&netif->ip_addr), IP4_ADDR_ANY4))) { - ip_event_got_ip_t evt; - int ret; - memset(&evt, 0, sizeof(ip_event_got_ip_t)); - - ESP_LOGD(TAG, "if%d ip lost tmr: raise ip lost event", tcpip_if); - evt.if_index = tcpip_if; - memset(&esp_ip_old[tcpip_if], 0, sizeof(tcpip_adapter_ip_info_t)); - ret = esp_event_send_internal(IP_EVENT, IP_EVENT_STA_LOST_IP, &evt, sizeof(evt), 0); - if (ESP_OK != ret) { - ESP_LOGE(TAG, "ip lost timer: failed to post lost ip event (%x)", ret); - } - } else { - ESP_LOGD(TAG, "if%d ip lost tmr: no need raise ip lost event", tcpip_if); - } - } else { - ESP_LOGD(TAG, "if%d ip lost tmr: not station", tcpip_if); - } -} - -esp_err_t tcpip_adapter_dhcpc_get_status(tcpip_adapter_if_t tcpip_if, tcpip_adapter_dhcp_status_t *status) -{ - *status = dhcpc_status[tcpip_if]; - - return ESP_OK; -} - -esp_err_t tcpip_adapter_dhcpc_start(tcpip_adapter_if_t tcpip_if) -{ - TCPIP_ADAPTER_IPC_CALL(tcpip_if, 0, 0, 0, tcpip_adapter_dhcpc_start_api); - - if ((tcpip_if != TCPIP_ADAPTER_IF_STA && tcpip_if != TCPIP_ADAPTER_IF_ETH) || tcpip_if >= TCPIP_ADAPTER_IF_MAX) { - ESP_LOGD(TAG, "dhcp client invalid if=%d", tcpip_if); - return ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS; - } - - if (dhcpc_status[tcpip_if] != TCPIP_ADAPTER_DHCP_STARTED) { - struct netif *p_netif = esp_netif[tcpip_if]; - - tcpip_adapter_reset_ip_info(tcpip_if); -#if LWIP_DNS - dns_clear_servers(true); -#endif - - if (p_netif != NULL) { - if (netif_is_up(p_netif)) { - ESP_LOGD(TAG, "dhcp client init ip/mask/gw to all-0"); - ip_addr_set_zero(&p_netif->ip_addr); - ip_addr_set_zero(&p_netif->netmask); - ip_addr_set_zero(&p_netif->gw); - tcpip_adapter_start_ip_lost_timer(tcpip_if); - } else { - ESP_LOGD(TAG, "dhcp client re init"); - dhcpc_status[tcpip_if] = TCPIP_ADAPTER_DHCP_INIT; - return ESP_OK; - } - - if (dhcp_start(p_netif) != ERR_OK) { - ESP_LOGD(TAG, "dhcp client start failed"); - return ESP_ERR_TCPIP_ADAPTER_DHCPC_START_FAILED; - } - - dhcp_set_cb(p_netif, tcpip_adapter_dhcpc_cb); - - ESP_LOGD(TAG, "dhcp client start successfully"); - dhcpc_status[tcpip_if] = TCPIP_ADAPTER_DHCP_STARTED; - return ESP_OK; - } else { - ESP_LOGD(TAG, "dhcp client re init"); - dhcpc_status[tcpip_if] = TCPIP_ADAPTER_DHCP_INIT; - return ESP_OK; - } - } - - ESP_LOGD(TAG, "dhcp client already started"); - return ESP_ERR_TCPIP_ADAPTER_DHCP_ALREADY_STARTED; -} - -static esp_err_t tcpip_adapter_dhcpc_start_api(tcpip_adapter_api_msg_t *msg) -{ - return tcpip_adapter_dhcpc_start(msg->tcpip_if); -} - -esp_err_t tcpip_adapter_dhcpc_stop(tcpip_adapter_if_t tcpip_if) -{ - TCPIP_ADAPTER_IPC_CALL(tcpip_if, 0, 0, 0, tcpip_adapter_dhcpc_stop_api); - - if ((tcpip_if != TCPIP_ADAPTER_IF_STA && tcpip_if != TCPIP_ADAPTER_IF_ETH) || tcpip_if >= TCPIP_ADAPTER_IF_MAX) { - ESP_LOGD(TAG, "dhcp client invalid if=%d", tcpip_if); - return ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS; - } - - if (dhcpc_status[tcpip_if] == TCPIP_ADAPTER_DHCP_STARTED) { - struct netif *p_netif = esp_netif[tcpip_if]; - - if (p_netif != NULL) { - dhcp_stop(p_netif); - tcpip_adapter_reset_ip_info(tcpip_if); - tcpip_adapter_start_ip_lost_timer(tcpip_if); - } else { - ESP_LOGD(TAG, "dhcp client if not ready"); - return ESP_ERR_TCPIP_ADAPTER_IF_NOT_READY; - } - } else if (dhcpc_status[tcpip_if] == TCPIP_ADAPTER_DHCP_STOPPED) { - ESP_LOGD(TAG, "dhcp client already stoped"); - return ESP_ERR_TCPIP_ADAPTER_DHCP_ALREADY_STOPPED; - } - - ESP_LOGD(TAG, "dhcp client stop successfully"); - dhcpc_status[tcpip_if] = TCPIP_ADAPTER_DHCP_STOPPED; - - LWIP_DHCP_IP_ADDR_ERASE(); - - return ESP_OK; -} - -static esp_err_t tcpip_adapter_dhcpc_stop_api(tcpip_adapter_api_msg_t *msg) -{ - return tcpip_adapter_dhcpc_stop(msg->tcpip_if); -} - -esp_err_t tcpip_adapter_eth_input(void *buffer, uint16_t len, void *eb) -{ -#ifdef CONFIG_ETH_ENABLED - ethernetif_input(esp_netif[TCPIP_ADAPTER_IF_ETH], buffer, len); - return ESP_OK; -#else - return ESP_ERR_NOT_SUPPORTED; -#endif -} - -esp_err_t tcpip_adapter_sta_input(void *buffer, uint16_t len, void *eb) -{ - wlanif_input(esp_netif[TCPIP_ADAPTER_IF_STA], buffer, len, eb); - return ESP_OK; -} - -esp_err_t tcpip_adapter_ap_input(void *buffer, uint16_t len, void *eb) -{ - wlanif_input(esp_netif[TCPIP_ADAPTER_IF_AP], buffer, len, eb); - return ESP_OK; -} - -#if 0 -bool tcpip_dep_output(wifi_interface_t wifi_if, void *buffer, uint16_t len) -{ - - return true; -} -#endif - -esp_interface_t tcpip_adapter_get_esp_if(void *dev) -{ - struct netif *p_netif = (struct netif *)dev; - - if (p_netif == esp_netif[TCPIP_ADAPTER_IF_STA]) { - return ESP_IF_WIFI_STA; - } else if (p_netif == esp_netif[TCPIP_ADAPTER_IF_AP]) { - return ESP_IF_WIFI_AP; - } else if (p_netif == esp_netif[TCPIP_ADAPTER_IF_ETH]) { - return ESP_IF_ETH; - } - - return ESP_IF_MAX; -} - -esp_err_t tcpip_adapter_get_sta_list(const wifi_sta_list_t *wifi_sta_list, tcpip_adapter_sta_list_t *tcpip_sta_list) -{ - int i; - - if ((wifi_sta_list == NULL) || (tcpip_sta_list == NULL)) { - return ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS; - } - - memset(tcpip_sta_list, 0, sizeof(tcpip_adapter_sta_list_t)); - tcpip_sta_list->num = wifi_sta_list->num; - for (i = 0; i < wifi_sta_list->num; i++) { - memcpy(tcpip_sta_list->sta[i].mac, wifi_sta_list->sta[i].mac, 6); - dhcp_search_ip_on_mac(tcpip_sta_list->sta[i].mac, &tcpip_sta_list->sta[i].ip); - } - - return ESP_OK; -} - -esp_err_t tcpip_adapter_set_hostname(tcpip_adapter_if_t tcpip_if, const char *hostname) -{ -#if LWIP_NETIF_HOSTNAME - TCPIP_ADAPTER_IPC_CALL(tcpip_if, 0, 0, hostname, tcpip_adapter_set_hostname_api); - struct netif *p_netif; - static char hostinfo[TCPIP_ADAPTER_IF_MAX][TCPIP_HOSTNAME_MAX_SIZE + 1]; - - if (tcpip_if >= TCPIP_ADAPTER_IF_MAX || hostname == NULL) { - return ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS; - } - - if (strlen(hostname) > TCPIP_HOSTNAME_MAX_SIZE) { - return ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS; - } - - p_netif = esp_netif[tcpip_if]; - if (p_netif != NULL) { - memset(hostinfo[tcpip_if], 0, sizeof(hostinfo[tcpip_if])); - strlcpy(hostinfo[tcpip_if], hostname, sizeof(hostinfo[tcpip_if])); - p_netif->hostname = hostinfo[tcpip_if]; - return ESP_OK; - } else { - return ESP_ERR_TCPIP_ADAPTER_IF_NOT_READY; - } -#else - return ESP_ERR_TCPIP_ADAPTER_IF_NOT_READY; -#endif -} - -static esp_err_t tcpip_adapter_set_hostname_api(tcpip_adapter_api_msg_t *msg) -{ - const char *hostname = (char *) msg->data; - - return tcpip_adapter_set_hostname(msg->tcpip_if, hostname); -} - -esp_err_t tcpip_adapter_get_hostname(tcpip_adapter_if_t tcpip_if, const char **hostname) -{ -#if LWIP_NETIF_HOSTNAME - struct netif *p_netif = NULL; - if (tcpip_if >= TCPIP_ADAPTER_IF_MAX || hostname == NULL) { - return ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS; - } - - p_netif = esp_netif[tcpip_if]; - if (p_netif != NULL) { - *hostname = p_netif->hostname; - return ESP_OK; - } else { - return ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS; - } -#else - return ESP_ERR_TCPIP_ADAPTER_IF_NOT_READY; -#endif -} - -static esp_err_t tcpip_adapter_reset_ip_info(tcpip_adapter_if_t tcpip_if) -{ - ip4_addr_set_zero(&esp_ip[tcpip_if].ip); - ip4_addr_set_zero(&esp_ip[tcpip_if].gw); - ip4_addr_set_zero(&esp_ip[tcpip_if].netmask); - return ESP_OK; -} - -esp_err_t tcpip_adapter_get_netif(tcpip_adapter_if_t tcpip_if, void **netif) -{ - if (tcpip_if >= TCPIP_ADAPTER_IF_MAX) { - return ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS; - } - - *netif = esp_netif[tcpip_if]; - - if (*netif == NULL) { - return ESP_ERR_TCPIP_ADAPTER_IF_NOT_READY; - } - return ESP_OK; -} - -bool tcpip_adapter_is_netif_up(tcpip_adapter_if_t tcpip_if) -{ - if (esp_netif[tcpip_if] != NULL && netif_is_up(esp_netif[tcpip_if])) { - return true; - } else { - return false; - } -} - -int tcpip_adapter_get_netif_index(tcpip_adapter_if_t tcpip_if) -{ - if (tcpip_if >= TCPIP_ADAPTER_IF_MAX || esp_netif[tcpip_if] == NULL) { - return -1; - } - return netif_get_index(esp_netif[tcpip_if]); -} - -#endif /* CONFIG_TCPIP_LWIP */ diff --git a/components/wifi_provisioning/src/handlers.c b/components/wifi_provisioning/src/handlers.c index a53c9d6e43..66f138a275 100644 --- a/components/wifi_provisioning/src/handlers.c +++ b/components/wifi_provisioning/src/handlers.c @@ -18,7 +18,7 @@ #include #include -#include +#include #include "wifi_provisioning/wifi_config.h" #include "wifi_provisioning/wifi_scan.h" @@ -64,10 +64,10 @@ static esp_err_t get_status_handler(wifi_prov_config_get_data_t *resp_data, wifi ESP_LOGD(TAG, "Got state : connected"); /* IP Addr assigned to STA */ - tcpip_adapter_ip_info_t ip_info; - tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_STA, &ip_info); - char *ip_addr = ip4addr_ntoa(&ip_info.ip); - strcpy(resp_data->conn_info.ip_addr, ip_addr); + esp_netif_ip_info_t ip_info; + esp_netif_get_ip_info(esp_netif_get_handle_from_ifkey("WIFI_STA_DEF"), &ip_info); + esp_ip4addr_ntoa(&ip_info.ip, resp_data->conn_info.ip_addr, sizeof(resp_data->conn_info.ip_addr)); + /* AP information to which STA is connected */ wifi_ap_record_t ap_info; diff --git a/examples/system/network_tests/main/CMakeLists.txt b/examples/system/network_tests/main/CMakeLists.txt index f379bf03a2..db11fd0532 100644 --- a/examples/system/network_tests/main/CMakeLists.txt +++ b/examples/system/network_tests/main/CMakeLists.txt @@ -1,2 +1,2 @@ -idf_component_register(SRCS "net_suite.c" +idf_component_register(SRCS "net_suite.c" "lwip_test_netif.c" INCLUDE_DIRS ".") \ No newline at end of file diff --git a/examples/system/network_tests/main/lwip_test_netif.c b/examples/system/network_tests/main/lwip_test_netif.c new file mode 100644 index 0000000000..0efbbbf2e0 --- /dev/null +++ b/examples/system/network_tests/main/lwip_test_netif.c @@ -0,0 +1,125 @@ +/* Net-suite test code: lwip netif API for creating test interface + + This example code is in the Public Domain (or CC0 licensed, at your option.) + + Unless required by applicable law or agreed to in writing, this + software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + CONDITIONS OF ANY KIND, either express or implied. +*/ +#include "lwip/opt.h" + +#include "lwip/def.h" +#include "lwip/mem.h" +#include "lwip/pbuf.h" +#include "lwip/stats.h" +#include "lwip/snmp.h" +#include "lwip/ethip6.h" +#include "netif/etharp.h" +#include "netif/wlanif.h" + +#include "esp_netif.h" + +#include +#include + +static struct netif *g_last_netif = NULL; + +// LWIP netif specific defines +struct esp_netif_netstack_config { + esp_netif_netstack_base_config_t base; + err_t (*init_fn)(struct netif*); + void (*input_fn)(struct netif *netif, void *buffer, size_t len, void *eb); +}; + +err_t testnetif_init(struct netif *netif); + +void testnetif_input(struct netif *netif, void *buffer, size_t len, void *eb); + +const struct esp_netif_netstack_config _g_test_netif_stack_config = { { ESP_NETIF_NETWORK_STACK_IS_LWIP }, testnetif_init, testnetif_input}; + + +err_t testnetif_output(struct netif *netif, struct pbuf *p) +{ + int i; + char *dat = p->payload; + + /* output the packet to stdout */ + printf("\nPacketOut:["); + for (i=0; ilen; i++) { + printf("%02x", *dat++); + } + printf("]\n"); + + return ERR_OK; +} + + +err_t testnetif_init(struct netif *netif) +{ + + g_last_netif = netif; + + netif->hostname = CONFIG_LWIP_LOCAL_HOSTNAME; + + /* + * Initialize the snmp variables and counters inside the struct netif. + * The last argument should be replaced with your link speed, in units + * of bits per second. + */ + NETIF_INIT_SNMP(netif, snmp_ifType_ethernet_csmacd, 100); + + /* We directly use etharp_output() here to save a function call. + * You can instead declare your own function an call etharp_output() + * from it if you have to do some checks before sending (e.g. if link + * is available...) */ + netif->output = etharp_output; +#if LWIP_IPV6 + netif->output_ip6 = ethip6_output; +#endif /* LWIP_IPV6 */ + netif->linkoutput = testnetif_output; + /* set MAC hardware address length */ + netif->hwaddr_len = ETHARP_HWADDR_LEN; + + /* set MAC hardware address */ + + /* maximum transfer unit */ + netif->mtu = 1500; + + /* device capabilities */ + /* don't set NETIF_FLAG_ETHARP if this device is not an ethernet one */ + netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP; + +#if ESP_LWIP + #if LWIP_IGMP + netif->flags |= NETIF_FLAG_IGMP; +#endif +#endif + return ERR_OK; + +} + +void testnetif_input(struct netif *netif, void *buffer, size_t len, void *eb) +{ + struct pbuf *p; + if (g_last_netif == NULL) { + printf("error!"); + return; + } + + printf("simul in: %d\n", len); + if (len==0) return; + + p = pbuf_alloc(PBUF_RAW, len, PBUF_RAM); + p->l2_owner = NULL; + memcpy(p->payload, buffer, len); + + /* full packet send to tcpip_thread to process + * on success - the packet is processed and deallocated in tcpip stack + * on failure - log error and deallocate the packet + */ + if (g_last_netif->input(p, g_last_netif) != ERR_OK) { + LWIP_DEBUGF(NETIF_DEBUG, ("ethernetif_input: IP input error\n")); + pbuf_free(p); + } + +} diff --git a/examples/system/network_tests/main/net_suite.c b/examples/system/network_tests/main/net_suite.c index d474dda5a1..09bb037237 100644 --- a/examples/system/network_tests/main/net_suite.c +++ b/examples/system/network_tests/main/net_suite.c @@ -25,7 +25,8 @@ #include "lwip/debug.h" #include "lwip/stats.h" #include "lwip/tcp.h" -void nettestif_input(void *buffer, u16_t len); + +extern const struct esp_netif_netstack_config _g_test_netif_stack_config; /* these data configures ARP cache so the test IPs are knows */ static char arp1[] = { @@ -135,21 +136,39 @@ void app_main(void) { char packet[128]; - tcpip_adapter_ip_info_t ip_info; + // Netif configs + // + esp_netif_ip_info_t ip_info; + uint8_t mac[] = { 0,0,0,0,0,1}; + esp_netif_inherent_config_t netif_common_config = { + .flags = ESP_NETIF_FLAG_AUTOUP, + .ip_info = (esp_netif_ip_info_t*)&ip_info, + }; + esp_netif_set_ip4_addr(&ip_info.ip, 10, 0 , 0, 1); + esp_netif_set_ip4_addr(&ip_info.gw, 10, 0 , 0, 1); + esp_netif_set_ip4_addr(&ip_info.netmask, 255, 255 , 255, 0); - uint8_t ap_mac[6] = { 0,0,0,0,0,1}; - IP4_ADDR(&ip_info.ip, 10, 0 , 0, 1); - IP4_ADDR(&ip_info.gw, 10, 0 , 0, 1); - IP4_ADDR(&ip_info.netmask, 255, 255 , 255, 0); + esp_netif_config_t config = { + .base = &netif_common_config, + .stack = &_g_test_netif_stack_config, + .driver = NULL + }; - tcpip_adapter_init(); + // Netif creation and configure + // + esp_netif_init(); + esp_netif_t* netif = esp_netif_new(&config); + assert(netif); - tcpip_adapter_test_start(ap_mac, &ip_info); + // Start the netif in a manual way, no need for events + // + esp_netif_set_mac(netif, mac); + esp_netif_action_start(netif, NULL, 0, NULL); // initializes TCP endpoint on DUT per https://github.com/intel/net-test-suites#21-endpoints test_tcp_init(); // Inject ARP packet to let the network stack know about IP/MAC of the counterpart - nettestif_input(arp1, sizeof(arp1)); + esp_netif_receive(netif, arp1, sizeof(arp1), NULL); // Initialize VFS & UART so we can use std::cout/cin setvbuf(stdin, NULL, _IONBF, 0); setvbuf(stdout, NULL, _IONBF, 0); @@ -173,7 +192,8 @@ void app_main(void) size = process_line(line, packet); - nettestif_input(packet, size); + esp_netif_receive(netif, packet, size, NULL); + linenoiseFree(line); } From 21464465ea1468de4d57a57392df399d96c5df1c Mon Sep 17 00:00:00 2001 From: David Cermak Date: Sat, 31 Aug 2019 16:08:46 +0200 Subject: [PATCH 03/31] examples: common component initialization code to use new esp_netif instead of tcpip_adapter --- .../protocol_examples_common/CMakeLists.txt | 4 +- .../protocol_examples_common/connect.c | 58 +++++++++++++++---- .../include/protocol_examples_common.h | 11 +++- 3 files changed, 59 insertions(+), 14 deletions(-) diff --git a/examples/common_components/protocol_examples_common/CMakeLists.txt b/examples/common_components/protocol_examples_common/CMakeLists.txt index 0ccb219b4a..7c4ebe34d6 100644 --- a/examples/common_components/protocol_examples_common/CMakeLists.txt +++ b/examples/common_components/protocol_examples_common/CMakeLists.txt @@ -1,2 +1,4 @@ idf_component_register(SRCS "connect.c" "stdin_out.c" - INCLUDE_DIRS "include") + INCLUDE_DIRS "include" + PRIV_REQUIRES esp_netif + ) diff --git a/examples/common_components/protocol_examples_common/connect.c b/examples/common_components/protocol_examples_common/connect.c index 0e9b6a5eae..b261e0b4f9 100644 --- a/examples/common_components/protocol_examples_common/connect.c +++ b/examples/common_components/protocol_examples_common/connect.c @@ -16,7 +16,7 @@ #include "esp_eth.h" #endif #include "esp_log.h" -#include "tcpip_adapter.h" +#include "esp_netif.h" #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "freertos/event_groups.h" @@ -33,11 +33,12 @@ #endif static EventGroupHandle_t s_connect_event_group; -static ip4_addr_t s_ip_addr; +static esp_ip4_addr_t s_ip_addr; static const char *s_connection_name; +static esp_netif_t *s_example_esp_netif = NULL; #ifdef CONFIG_EXAMPLE_CONNECT_IPV6 -static ip6_addr_t s_ipv6_addr; +static esp_ip6_addr_t s_ipv6_addr; #endif static const char *TAG = "example_connect"; @@ -51,6 +52,7 @@ static void stop(void); static void on_got_ip(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data) { + ESP_LOGI(TAG, "Got IP event!"); ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data; memcpy(&s_ip_addr, &event->ip_info.ip, sizeof(s_ip_addr)); xEventGroupSetBits(s_connect_event_group, GOT_IPV4_BIT); @@ -61,6 +63,7 @@ static void on_got_ip(void *arg, esp_event_base_t event_base, static void on_got_ipv6(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data) { + ESP_LOGI(TAG, "Got IPv6 event!"); ip_event_got_ip6_t *event = (ip_event_got_ip6_t *)event_data; memcpy(&s_ipv6_addr, &event->ip6_info.ip, sizeof(s_ipv6_addr)); xEventGroupSetBits(s_connect_event_group, GOT_IPV6_BIT); @@ -76,6 +79,7 @@ esp_err_t example_connect(void) s_connect_event_group = xEventGroupCreate(); start(); ESP_ERROR_CHECK(esp_register_shutdown_handler(&stop)); + ESP_LOGI(TAG, "Waiting for IP"); xEventGroupWaitBits(s_connect_event_group, CONNECTED_BITS, true, true, portMAX_DELAY); ESP_LOGI(TAG, "Connected to %s", s_connection_name); ESP_LOGI(TAG, "IPv4 address: " IPSTR, IP2STR(&s_ip_addr)); @@ -113,10 +117,10 @@ static void on_wifi_disconnect(void *arg, esp_event_base_t event_base, #ifdef CONFIG_EXAMPLE_CONNECT_IPV6 -static void on_wifi_connect(void *arg, esp_event_base_t event_base, +static void on_wifi_connect(void *esp_netif, esp_event_base_t event_base, int32_t event_id, void *event_data) { - tcpip_adapter_create_ip6_linklocal(TCPIP_ADAPTER_IF_STA); + esp_netif_create_ip6_linklocal(esp_netif); } #endif // CONFIG_EXAMPLE_CONNECT_IPV6 @@ -126,10 +130,20 @@ static void start(void) wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); ESP_ERROR_CHECK(esp_wifi_init(&cfg)); + esp_netif_config_t netif_config = ESP_NETIF_DEFAULT_WIFI_STA(); + + esp_netif_t* netif = esp_netif_new(&netif_config); + + assert(netif); + + esp_wifi_set_default_wifi_sta_handlers(netif); + + s_example_esp_netif = netif; + ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &on_wifi_disconnect, NULL)); ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &on_got_ip, NULL)); #ifdef CONFIG_EXAMPLE_CONNECT_IPV6 - ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_CONNECTED, &on_wifi_connect, NULL)); + ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_CONNECTED, &on_wifi_connect, netif)); ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_GOT_IP6, &on_got_ipv6, NULL)); #endif @@ -162,6 +176,9 @@ static void stop(void) } ESP_ERROR_CHECK(err); ESP_ERROR_CHECK(esp_wifi_deinit()); + + esp_netif_destroy(s_example_esp_netif); + s_example_esp_netif = NULL; } #endif // CONFIG_EXAMPLE_CONNECT_WIFI @@ -170,13 +187,13 @@ static void stop(void) #ifdef CONFIG_EXAMPLE_CONNECT_IPV6 /** Event handler for Ethernet events */ -static void on_eth_event(void *arg, esp_event_base_t event_base, +static void on_eth_event(void *esp_netif, esp_event_base_t event_base, int32_t event_id, void *event_data) { switch (event_id) { case ETHERNET_EVENT_CONNECTED: ESP_LOGI(TAG, "Ethernet Link Up"); - tcpip_adapter_create_ip6_linklocal(TCPIP_ADAPTER_IF_ETH); + esp_netif_create_ip6_linklocal(esp_netif); break; default: break; @@ -191,10 +208,19 @@ static esp_eth_phy_t *s_phy = NULL; static void start(void) { - ESP_ERROR_CHECK(tcpip_adapter_set_default_eth_handlers()); + esp_netif_config_t netif_config = ESP_NETIF_DEFAULT_ETH(); + + esp_netif_t* netif = esp_netif_new(&netif_config); + + assert(netif); + + ESP_ERROR_CHECK(esp_eth_set_default_handlers(netif)); + + s_example_esp_netif = netif; + ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &on_got_ip, NULL)); #ifdef CONFIG_EXAMPLE_CONNECT_IPV6 - ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ETHERNET_EVENT_CONNECTED, &on_eth_event, NULL)); + ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ETHERNET_EVENT_CONNECTED, &on_eth_event, netif)); ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_GOT_IP6, &on_got_ipv6, NULL)); #endif eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG(); @@ -241,7 +267,10 @@ static void start(void) #endif esp_eth_config_t config = ETH_DEFAULT_CONFIG(s_mac, s_phy); + ESP_ERROR_CHECK(esp_eth_driver_install(&config, &s_eth_handle)); + + esp_netif_attach(netif, s_eth_handle); s_connection_name = "Ethernet"; } @@ -255,6 +284,15 @@ static void stop(void) ESP_ERROR_CHECK(esp_eth_driver_uninstall(s_eth_handle)); ESP_ERROR_CHECK(s_phy->del(s_phy)); ESP_ERROR_CHECK(s_mac->del(s_mac)); + + esp_eth_clear_default_handlers(s_example_esp_netif); + esp_netif_destroy(s_example_esp_netif); + s_example_esp_netif = NULL; } #endif // CONFIG_EXAMPLE_CONNECT_ETHERNET + +esp_netif_t *get_example_netif(void) +{ + return s_example_esp_netif; +} \ No newline at end of file diff --git a/examples/common_components/protocol_examples_common/include/protocol_examples_common.h b/examples/common_components/protocol_examples_common/include/protocol_examples_common.h index d4f6e1fbbb..98f67e3486 100644 --- a/examples/common_components/protocol_examples_common/include/protocol_examples_common.h +++ b/examples/common_components/protocol_examples_common/include/protocol_examples_common.h @@ -14,14 +14,14 @@ extern "C" { #endif #include "esp_err.h" -#include "tcpip_adapter.h" +#include "esp_netif.h" #ifdef CONFIG_EXAMPLE_CONNECT_ETHERNET -#define EXAMPLE_INTERFACE TCPIP_ADAPTER_IF_ETH +#define EXAMPLE_INTERFACE get_example_netif() #endif #ifdef CONFIG_EXAMPLE_CONNECT_WIFI -#define EXAMPLE_INTERFACE TCPIP_ADAPTER_IF_STA +#define EXAMPLE_INTERFACE get_example_netif() #endif /** @@ -54,6 +54,11 @@ esp_err_t example_disconnect(void); */ esp_err_t example_configure_stdin_stdout(void); +/** + * @brief Returns esp-netif pointer created by example_connect() + * + */ +esp_netif_t *get_example_netif(void); #ifdef __cplusplus } #endif From a49b934ef895690f2b5e3709340db856e27475e2 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Sat, 31 Aug 2019 16:19:21 +0200 Subject: [PATCH 04/31] examples: protocol examples which use common connection component updated to use esp_netif_init instead of tcpip_adapter in initialization code --- .../coap_client/main/coap_client_example_main.c | 2 +- .../coap_server/main/coap_server_example_main.c | 2 +- .../esp_http_client/main/esp_http_client_example.c | 4 ++-- .../http2_request/main/http2_request_example_main.c | 4 ++-- .../http_request/main/http_request_example_main.c | 2 +- .../protocols/http_server/advanced_tests/main/main.c | 4 ++-- .../protocols/http_server/file_serving/main/main.c | 4 ++-- .../http_server/persistent_sockets/main/main.c | 4 ++-- .../http_server/restful_server/main/esp_rest_main.c | 4 ++-- examples/protocols/http_server/simple/main/main.c | 4 ++-- .../https_mbedtls/main/https_mbedtls_example_main.c | 4 ++-- .../https_request/main/https_request_example_main.c | 4 ++-- examples/protocols/https_server/main/main.c | 4 ++-- examples/protocols/mdns/main/mdns_example_main.c | 2 +- .../protocols/mqtt/publish_test/main/publish_test.c | 4 ++-- examples/protocols/mqtt/ssl/main/app_main.c | 4 ++-- .../protocols/mqtt/ssl_mutual_auth/main/app_main.c | 4 ++-- examples/protocols/mqtt/tcp/main/app_main.c | 4 ++-- examples/protocols/mqtt/ws/main/app_main.c | 4 ++-- examples/protocols/mqtt/wss/main/app_main.c | 4 ++-- .../main/openssl_client_example_main.c | 4 ++-- .../main/openssl_server_example_main.c | 4 ++-- examples/protocols/sntp/main/sntp_example_main.c | 2 +- .../protocols/sockets/tcp_client/main/tcp_client.c | 4 ++-- .../protocols/sockets/tcp_server/main/tcp_server.c | 4 ++-- .../protocols/sockets/udp_client/main/udp_client.c | 4 ++-- .../udp_multicast/main/udp_multicast_example_main.c | 12 ++++++------ .../protocols/sockets/udp_server/main/udp_server.c | 4 ++-- .../protocols/websocket/main/websocket_example.c | 2 +- .../main/advanced_https_ota_example.c | 2 +- .../ota/native_ota_example/main/native_ota_example.c | 2 +- .../ota/simple_ota_example/main/simple_ota_example.c | 2 +- 32 files changed, 59 insertions(+), 59 deletions(-) diff --git a/examples/protocols/coap_client/main/coap_client_example_main.c b/examples/protocols/coap_client/main/coap_client_example_main.c index de7ec60be9..423e9f7a18 100644 --- a/examples/protocols/coap_client/main/coap_client_example_main.c +++ b/examples/protocols/coap_client/main/coap_client_example_main.c @@ -440,7 +440,7 @@ clean_up: void app_main(void) { ESP_ERROR_CHECK( nvs_flash_init() ); - tcpip_adapter_init(); + esp_netif_init(); ESP_ERROR_CHECK(esp_event_loop_create_default()); /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig. diff --git a/examples/protocols/coap_server/main/coap_server_example_main.c b/examples/protocols/coap_server/main/coap_server_example_main.c index 562c85a833..b8adb783da 100644 --- a/examples/protocols/coap_server/main/coap_server_example_main.c +++ b/examples/protocols/coap_server/main/coap_server_example_main.c @@ -306,7 +306,7 @@ clean_up: void app_main(void) { ESP_ERROR_CHECK( nvs_flash_init() ); - tcpip_adapter_init(); + esp_netif_init(); ESP_ERROR_CHECK(esp_event_loop_create_default()); /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig. diff --git a/examples/protocols/esp_http_client/main/esp_http_client_example.c b/examples/protocols/esp_http_client/main/esp_http_client_example.c index 348471a615..10d96034c9 100644 --- a/examples/protocols/esp_http_client/main/esp_http_client_example.c +++ b/examples/protocols/esp_http_client/main/esp_http_client_example.c @@ -15,7 +15,7 @@ #include "esp_system.h" #include "nvs_flash.h" #include "esp_event.h" -#include "tcpip_adapter.h" +#include "esp_netif.h" #include "protocol_examples_common.h" #include "esp_tls.h" @@ -541,7 +541,7 @@ void app_main(void) ret = nvs_flash_init(); } ESP_ERROR_CHECK(ret); - tcpip_adapter_init(); + esp_netif_init(); ESP_ERROR_CHECK(esp_event_loop_create_default()); /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig. diff --git a/examples/protocols/http2_request/main/http2_request_example_main.c b/examples/protocols/http2_request/main/http2_request_example_main.c index 9b6bb739ec..aaaa251a8d 100644 --- a/examples/protocols/http2_request/main/http2_request_example_main.c +++ b/examples/protocols/http2_request/main/http2_request_example_main.c @@ -21,7 +21,7 @@ #include "esp_system.h" #include "nvs_flash.h" #include "protocol_examples_common.h" -#include "tcpip_adapter.h" +#include "esp_netif.h" #include "sh2lib.h" @@ -131,7 +131,7 @@ static void http2_task(void *args) void app_main(void) { ESP_ERROR_CHECK( nvs_flash_init() ); - tcpip_adapter_init(); + esp_netif_init(); ESP_ERROR_CHECK(esp_event_loop_create_default()); /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig. diff --git a/examples/protocols/http_request/main/http_request_example_main.c b/examples/protocols/http_request/main/http_request_example_main.c index 12621e06ae..e3a485bca9 100644 --- a/examples/protocols/http_request/main/http_request_example_main.c +++ b/examples/protocols/http_request/main/http_request_example_main.c @@ -122,7 +122,7 @@ static void http_get_task(void *pvParameters) void app_main(void) { ESP_ERROR_CHECK( nvs_flash_init() ); - tcpip_adapter_init(); + esp_netif_init(); ESP_ERROR_CHECK(esp_event_loop_create_default()); /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig. diff --git a/examples/protocols/http_server/advanced_tests/main/main.c b/examples/protocols/http_server/advanced_tests/main/main.c index 118a380a8f..c30683ef04 100644 --- a/examples/protocols/http_server/advanced_tests/main/main.c +++ b/examples/protocols/http_server/advanced_tests/main/main.c @@ -12,7 +12,7 @@ #include "esp_log.h" #include "esp_system.h" #include "nvs_flash.h" -#include "tcpip_adapter.h" +#include "esp_netif.h" #include "esp_eth.h" #include "protocol_examples_common.h" @@ -46,7 +46,7 @@ void app_main(void) static httpd_handle_t server = NULL; ESP_ERROR_CHECK(nvs_flash_init()); - tcpip_adapter_init(); + esp_netif_init(); ESP_ERROR_CHECK(esp_event_loop_create_default()); /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig. diff --git a/examples/protocols/http_server/file_serving/main/main.c b/examples/protocols/http_server/file_serving/main/main.c index 4a6faf5a2d..463e6c643a 100644 --- a/examples/protocols/http_server/file_serving/main/main.c +++ b/examples/protocols/http_server/file_serving/main/main.c @@ -15,7 +15,7 @@ #include "esp_system.h" #include "esp_spiffs.h" #include "nvs_flash.h" -#include "tcpip_adapter.h" +#include "esp_netif.h" #include "protocol_examples_common.h" /* This example demonstrates how to create file server @@ -67,7 +67,7 @@ esp_err_t start_file_server(const char *base_path); void app_main(void) { ESP_ERROR_CHECK(nvs_flash_init()); - tcpip_adapter_init(); + esp_netif_init(); ESP_ERROR_CHECK(esp_event_loop_create_default()); /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig. diff --git a/examples/protocols/http_server/persistent_sockets/main/main.c b/examples/protocols/http_server/persistent_sockets/main/main.c index 8e6132b1e6..3905c44501 100644 --- a/examples/protocols/http_server/persistent_sockets/main/main.c +++ b/examples/protocols/http_server/persistent_sockets/main/main.c @@ -12,7 +12,7 @@ #include #include #include -#include "tcpip_adapter.h" +#include "esp_netif.h" #include "esp_eth.h" #include "protocol_examples_common.h" @@ -216,7 +216,7 @@ void app_main(void) static httpd_handle_t server = NULL; ESP_ERROR_CHECK(nvs_flash_init()); - tcpip_adapter_init(); + esp_netif_init(); ESP_ERROR_CHECK(esp_event_loop_create_default()); /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig. diff --git a/examples/protocols/http_server/restful_server/main/esp_rest_main.c b/examples/protocols/http_server/restful_server/main/esp_rest_main.c index 9e28bf0ec7..15580769b3 100644 --- a/examples/protocols/http_server/restful_server/main/esp_rest_main.c +++ b/examples/protocols/http_server/restful_server/main/esp_rest_main.c @@ -13,7 +13,7 @@ #include "esp_spiffs.h" #include "sdmmc_cmd.h" #include "nvs_flash.h" -#include "tcpip_adapter.h" +#include "esp_netif.h" #include "esp_event.h" #include "esp_log.h" #include "mdns.h" @@ -126,7 +126,7 @@ esp_err_t init_fs(void) void app_main(void) { ESP_ERROR_CHECK(nvs_flash_init()); - tcpip_adapter_init(); + esp_netif_init(); ESP_ERROR_CHECK(esp_event_loop_create_default()); initialise_mdns(); netbiosns_init(); diff --git a/examples/protocols/http_server/simple/main/main.c b/examples/protocols/http_server/simple/main/main.c index d7bec44662..93e6241704 100644 --- a/examples/protocols/http_server/simple/main/main.c +++ b/examples/protocols/http_server/simple/main/main.c @@ -14,7 +14,7 @@ #include #include #include "nvs_flash.h" -#include "tcpip_adapter.h" +#include "esp_netif.h" #include "esp_eth.h" #include "protocol_examples_common.h" @@ -272,7 +272,7 @@ void app_main(void) static httpd_handle_t server = NULL; ESP_ERROR_CHECK(nvs_flash_init()); - tcpip_adapter_init(); + esp_netif_init(); ESP_ERROR_CHECK(esp_event_loop_create_default()); /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig. diff --git a/examples/protocols/https_mbedtls/main/https_mbedtls_example_main.c b/examples/protocols/https_mbedtls/main/https_mbedtls_example_main.c index ce5823f60f..482133bf86 100644 --- a/examples/protocols/https_mbedtls/main/https_mbedtls_example_main.c +++ b/examples/protocols/https_mbedtls/main/https_mbedtls_example_main.c @@ -31,7 +31,7 @@ #include "esp_system.h" #include "nvs_flash.h" #include "protocol_examples_common.h" -#include "tcpip_adapter.h" +#include "esp_netif.h" #include "lwip/err.h" #include "lwip/sockets.h" @@ -274,7 +274,7 @@ static void https_get_task(void *pvParameters) void app_main(void) { ESP_ERROR_CHECK( nvs_flash_init() ); - tcpip_adapter_init(); + esp_netif_init(); ESP_ERROR_CHECK(esp_event_loop_create_default()); /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig. diff --git a/examples/protocols/https_request/main/https_request_example_main.c b/examples/protocols/https_request/main/https_request_example_main.c index 3b986f6bdd..b41fadb59b 100644 --- a/examples/protocols/https_request/main/https_request_example_main.c +++ b/examples/protocols/https_request/main/https_request_example_main.c @@ -32,7 +32,7 @@ #include "esp_system.h" #include "nvs_flash.h" #include "protocol_examples_common.h" -#include "tcpip_adapter.h" +#include "esp_netif.h" #include "lwip/err.h" #include "lwip/sockets.h" @@ -151,7 +151,7 @@ static void https_get_task(void *pvParameters) void app_main(void) { ESP_ERROR_CHECK( nvs_flash_init() ); - tcpip_adapter_init(); + esp_netif_init(); ESP_ERROR_CHECK(esp_event_loop_create_default()); /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig. diff --git a/examples/protocols/https_server/main/main.c b/examples/protocols/https_server/main/main.c index 6d6a36931a..5f68eab0a4 100644 --- a/examples/protocols/https_server/main/main.c +++ b/examples/protocols/https_server/main/main.c @@ -13,7 +13,7 @@ #include #include #include -#include "tcpip_adapter.h" +#include "esp_netif.h" #include "esp_eth.h" #include "protocol_examples_common.h" @@ -103,7 +103,7 @@ void app_main(void) static httpd_handle_t server = NULL; ESP_ERROR_CHECK(nvs_flash_init()); - tcpip_adapter_init(); + esp_netif_init(); ESP_ERROR_CHECK(esp_event_loop_create_default()); /* Register event handlers to start server when Wi-Fi or Ethernet is connected, diff --git a/examples/protocols/mdns/main/mdns_example_main.c b/examples/protocols/mdns/main/mdns_example_main.c index 003c8e7848..76cb3b1b6e 100644 --- a/examples/protocols/mdns/main/mdns_example_main.c +++ b/examples/protocols/mdns/main/mdns_example_main.c @@ -186,7 +186,7 @@ static void mdns_example_task(void *pvParameters) void app_main(void) { ESP_ERROR_CHECK(nvs_flash_init()); - tcpip_adapter_init(); + ESP_ERROR_CHECK(esp_netif_init()); ESP_ERROR_CHECK(esp_event_loop_create_default()); initialise_mdns(); diff --git a/examples/protocols/mqtt/publish_test/main/publish_test.c b/examples/protocols/mqtt/publish_test/main/publish_test.c index 918f9a191a..2a20d1a412 100644 --- a/examples/protocols/mqtt/publish_test/main/publish_test.c +++ b/examples/protocols/mqtt/publish_test/main/publish_test.c @@ -14,7 +14,7 @@ #include "esp_system.h" #include "nvs_flash.h" #include "esp_event.h" -#include "tcpip_adapter.h" +#include "esp_netif.h" #include "protocol_examples_common.h" #include "freertos/FreeRTOS.h" @@ -169,7 +169,7 @@ void app_main(void) esp_log_level_set("OUTBOX", ESP_LOG_VERBOSE); ESP_ERROR_CHECK(nvs_flash_init()); - tcpip_adapter_init(); + esp_netif_init(); ESP_ERROR_CHECK(esp_event_loop_create_default()); /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig. diff --git a/examples/protocols/mqtt/ssl/main/app_main.c b/examples/protocols/mqtt/ssl/main/app_main.c index 007465429d..f8e857351f 100644 --- a/examples/protocols/mqtt/ssl/main/app_main.c +++ b/examples/protocols/mqtt/ssl/main/app_main.c @@ -14,7 +14,7 @@ #include "esp_system.h" #include "nvs_flash.h" #include "esp_event.h" -#include "tcpip_adapter.h" +#include "esp_netif.h" #include "protocol_examples_common.h" #include "esp_log.h" @@ -139,7 +139,7 @@ void app_main(void) esp_log_level_set("OUTBOX", ESP_LOG_VERBOSE); ESP_ERROR_CHECK(nvs_flash_init()); - tcpip_adapter_init(); + esp_netif_init(); ESP_ERROR_CHECK(esp_event_loop_create_default()); /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig. diff --git a/examples/protocols/mqtt/ssl_mutual_auth/main/app_main.c b/examples/protocols/mqtt/ssl_mutual_auth/main/app_main.c index 5c6eab7ee5..858f58aa7a 100644 --- a/examples/protocols/mqtt/ssl_mutual_auth/main/app_main.c +++ b/examples/protocols/mqtt/ssl_mutual_auth/main/app_main.c @@ -14,7 +14,7 @@ #include "esp_system.h" #include "nvs_flash.h" #include "esp_event.h" -#include "tcpip_adapter.h" +#include "esp_netif.h" #include "protocol_examples_common.h" #include "freertos/FreeRTOS.h" @@ -111,7 +111,7 @@ void app_main(void) esp_log_level_set("OUTBOX", ESP_LOG_VERBOSE); ESP_ERROR_CHECK(nvs_flash_init()); - tcpip_adapter_init(); + esp_netif_init(); ESP_ERROR_CHECK(esp_event_loop_create_default()); /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig. diff --git a/examples/protocols/mqtt/tcp/main/app_main.c b/examples/protocols/mqtt/tcp/main/app_main.c index 7acd685360..d4b631e1a5 100644 --- a/examples/protocols/mqtt/tcp/main/app_main.c +++ b/examples/protocols/mqtt/tcp/main/app_main.c @@ -15,7 +15,7 @@ #include "esp_system.h" #include "nvs_flash.h" #include "esp_event.h" -#include "tcpip_adapter.h" +#include "esp_netif.h" #include "protocol_examples_common.h" #include "freertos/FreeRTOS.h" @@ -138,7 +138,7 @@ void app_main(void) esp_log_level_set("OUTBOX", ESP_LOG_VERBOSE); ESP_ERROR_CHECK(nvs_flash_init()); - tcpip_adapter_init(); + esp_netif_init(); ESP_ERROR_CHECK(esp_event_loop_create_default()); /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig. diff --git a/examples/protocols/mqtt/ws/main/app_main.c b/examples/protocols/mqtt/ws/main/app_main.c index cacea73e9d..5af121fee2 100644 --- a/examples/protocols/mqtt/ws/main/app_main.c +++ b/examples/protocols/mqtt/ws/main/app_main.c @@ -14,7 +14,7 @@ #include "esp_system.h" #include "nvs_flash.h" #include "esp_event.h" -#include "tcpip_adapter.h" +#include "esp_netif.h" #include "protocol_examples_common.h" #include "freertos/FreeRTOS.h" @@ -111,7 +111,7 @@ void app_main(void) esp_log_level_set("OUTBOX", ESP_LOG_VERBOSE); ESP_ERROR_CHECK(nvs_flash_init()); - tcpip_adapter_init(); + esp_netif_init(); ESP_ERROR_CHECK(esp_event_loop_create_default()); /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig. diff --git a/examples/protocols/mqtt/wss/main/app_main.c b/examples/protocols/mqtt/wss/main/app_main.c index 529e960605..29b5de16a3 100644 --- a/examples/protocols/mqtt/wss/main/app_main.c +++ b/examples/protocols/mqtt/wss/main/app_main.c @@ -14,7 +14,7 @@ #include "esp_system.h" #include "nvs_flash.h" #include "esp_event.h" -#include "tcpip_adapter.h" +#include "esp_netif.h" #include "protocol_examples_common.h" #include "freertos/FreeRTOS.h" @@ -120,7 +120,7 @@ void app_main(void) esp_log_level_set("OUTBOX", ESP_LOG_VERBOSE); ESP_ERROR_CHECK(nvs_flash_init()); - tcpip_adapter_init(); + esp_netif_init(); ESP_ERROR_CHECK(esp_event_loop_create_default()); /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig. diff --git a/examples/protocols/openssl_client/main/openssl_client_example_main.c b/examples/protocols/openssl_client/main/openssl_client_example_main.c index a06ef6cdd4..09f508c9f8 100644 --- a/examples/protocols/openssl_client/main/openssl_client_example_main.c +++ b/examples/protocols/openssl_client/main/openssl_client_example_main.c @@ -20,7 +20,7 @@ #include "esp_wifi.h" #include "esp_event.h" #include "nvs_flash.h" -#include "tcpip_adapter.h" +#include "esp_netif.h" #include "protocol_examples_common.h" #include "lwip/sockets.h" @@ -174,7 +174,7 @@ void app_main(void) { ESP_ERROR_CHECK( nvs_flash_init() ); ESP_ERROR_CHECK(nvs_flash_init()); - tcpip_adapter_init(); + esp_netif_init(); ESP_ERROR_CHECK(esp_event_loop_create_default()); /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig. diff --git a/examples/protocols/openssl_server/main/openssl_server_example_main.c b/examples/protocols/openssl_server/main/openssl_server_example_main.c index 9db6fc04f0..2c94dd36d4 100644 --- a/examples/protocols/openssl_server/main/openssl_server_example_main.c +++ b/examples/protocols/openssl_server/main/openssl_server_example_main.c @@ -19,7 +19,7 @@ #include "esp_log.h" #include "esp_wifi.h" #include "esp_event.h" -#include "tcpip_adapter.h" +#include "esp_netif.h" #include "nvs_flash.h" #include "protocol_examples_common.h" @@ -208,7 +208,7 @@ static void openssl_server_init(void) void app_main(void) { ESP_ERROR_CHECK(nvs_flash_init()); - tcpip_adapter_init(); + esp_netif_init(); ESP_ERROR_CHECK(esp_event_loop_create_default()); /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig. diff --git a/examples/protocols/sntp/main/sntp_example_main.c b/examples/protocols/sntp/main/sntp_example_main.c index 59d22d7fec..94e8421530 100644 --- a/examples/protocols/sntp/main/sntp_example_main.c +++ b/examples/protocols/sntp/main/sntp_example_main.c @@ -119,7 +119,7 @@ void app_main(void) static void obtain_time(void) { ESP_ERROR_CHECK( nvs_flash_init() ); - tcpip_adapter_init(); + esp_netif_init(); ESP_ERROR_CHECK( esp_event_loop_create_default() ); /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig. diff --git a/examples/protocols/sockets/tcp_client/main/tcp_client.c b/examples/protocols/sockets/tcp_client/main/tcp_client.c index 5e98718df5..f8a93eaa76 100644 --- a/examples/protocols/sockets/tcp_client/main/tcp_client.c +++ b/examples/protocols/sockets/tcp_client/main/tcp_client.c @@ -16,7 +16,7 @@ #include "esp_event.h" #include "esp_log.h" #include "nvs_flash.h" -#include "tcpip_adapter.h" +#include "esp_netif.h" #include "protocol_examples_common.h" #include "lwip/err.h" @@ -112,7 +112,7 @@ static void tcp_client_task(void *pvParameters) void app_main(void) { ESP_ERROR_CHECK(nvs_flash_init()); - tcpip_adapter_init(); + esp_netif_init(); ESP_ERROR_CHECK(esp_event_loop_create_default()); /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig. diff --git a/examples/protocols/sockets/tcp_server/main/tcp_server.c b/examples/protocols/sockets/tcp_server/main/tcp_server.c index bd9606e3a1..6f3de3f043 100644 --- a/examples/protocols/sockets/tcp_server/main/tcp_server.c +++ b/examples/protocols/sockets/tcp_server/main/tcp_server.c @@ -15,7 +15,7 @@ #include "esp_event.h" #include "esp_log.h" #include "nvs_flash.h" -#include "tcpip_adapter.h" +#include "esp_netif.h" #include "protocol_examples_common.h" #include "lwip/err.h" @@ -137,7 +137,7 @@ CLEAN_UP: void app_main(void) { ESP_ERROR_CHECK(nvs_flash_init()); - tcpip_adapter_init(); + esp_netif_init(); ESP_ERROR_CHECK(esp_event_loop_create_default()); /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig. diff --git a/examples/protocols/sockets/udp_client/main/udp_client.c b/examples/protocols/sockets/udp_client/main/udp_client.c index bb31755795..36de61fde2 100644 --- a/examples/protocols/sockets/udp_client/main/udp_client.c +++ b/examples/protocols/sockets/udp_client/main/udp_client.c @@ -16,7 +16,7 @@ #include "esp_event.h" #include "esp_log.h" #include "nvs_flash.h" -#include "tcpip_adapter.h" +#include "esp_netif.h" #include "protocol_examples_common.h" #include "lwip/err.h" @@ -111,7 +111,7 @@ static void udp_client_task(void *pvParameters) void app_main(void) { ESP_ERROR_CHECK(nvs_flash_init()); - tcpip_adapter_init(); + esp_netif_init(); ESP_ERROR_CHECK(esp_event_loop_create_default()); /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig. diff --git a/examples/protocols/sockets/udp_multicast/main/udp_multicast_example_main.c b/examples/protocols/sockets/udp_multicast/main/udp_multicast_example_main.c index e20ce9fd37..1aaec8b74a 100644 --- a/examples/protocols/sockets/udp_multicast/main/udp_multicast_example_main.c +++ b/examples/protocols/sockets/udp_multicast/main/udp_multicast_example_main.c @@ -16,7 +16,7 @@ #include "esp_event.h" #include "esp_log.h" #include "nvs_flash.h" -#include "tcpip_adapter.h" +#include "esp_netif.h" #include "protocol_examples_common.h" #include "lwip/err.h" @@ -62,8 +62,8 @@ static int socket_add_ipv4_multicast_group(int sock, bool assign_source_if) #if LISTEN_ALL_IF imreq.imr_interface.s_addr = IPADDR_ANY; #else - tcpip_adapter_ip_info_t ip_info = { 0 }; - err = tcpip_adapter_get_ip_info(EXAMPLE_INTERFACE, &ip_info); + esp_netif_ip_info_t ip_info = { 0 }; + err = esp_netif_get_ip_info(get_example_netif(), &ip_info); if (err != ESP_OK) { ESP_LOGE(V4TAG, "Failed to get IP address info. Error 0x%x", err); goto err; @@ -203,7 +203,7 @@ static int create_multicast_ipv6_socket(void) // (Note the interface may have other non-LL IPV6 addresses as well, // but it doesn't matter in this context as the address is only // used to identify the interface.) - err = tcpip_adapter_get_ip6_linklocal(EXAMPLE_INTERFACE, &if_ipaddr); + err = esp_netif_get_ip6_linklocal(EXAMPLE_INTERFACE, (esp_ip6_addr_t*)&if_ipaddr); inet6_addr_from_ip6addr(&if_inaddr, &if_ipaddr); if (err != ESP_OK) { ESP_LOGE(V6TAG, "Failed to get IPV6 LL address. Error 0x%x", err); @@ -212,7 +212,7 @@ static int create_multicast_ipv6_socket(void) #endif // LISTEN_ALL_IF // search for netif index - netif_index = tcpip_adapter_get_netif_index(EXAMPLE_INTERFACE); + netif_index = esp_netif_get_netif_index(EXAMPLE_INTERFACE); if(netif_index < 0) { ESP_LOGE(V6TAG, "Failed to get netif index"); goto err; @@ -484,7 +484,7 @@ static void mcast_example_task(void *pvParameters) void app_main(void) { ESP_ERROR_CHECK(nvs_flash_init()); - tcpip_adapter_init(); + esp_netif_init(); ESP_ERROR_CHECK(esp_event_loop_create_default()); /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig. diff --git a/examples/protocols/sockets/udp_server/main/udp_server.c b/examples/protocols/sockets/udp_server/main/udp_server.c index 262d2b2137..812126afe4 100644 --- a/examples/protocols/sockets/udp_server/main/udp_server.c +++ b/examples/protocols/sockets/udp_server/main/udp_server.c @@ -15,7 +15,7 @@ #include "esp_event.h" #include "esp_log.h" #include "nvs_flash.h" -#include "tcpip_adapter.h" +#include "esp_netif.h" #include "protocol_examples_common.h" #include "lwip/err.h" @@ -112,7 +112,7 @@ static void udp_server_task(void *pvParameters) void app_main(void) { ESP_ERROR_CHECK(nvs_flash_init()); - tcpip_adapter_init(); + esp_netif_init(); ESP_ERROR_CHECK(esp_event_loop_create_default()); /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig. diff --git a/examples/protocols/websocket/main/websocket_example.c b/examples/protocols/websocket/main/websocket_example.c index f82ac94947..82e7ba385c 100644 --- a/examples/protocols/websocket/main/websocket_example.c +++ b/examples/protocols/websocket/main/websocket_example.c @@ -90,7 +90,7 @@ void app_main(void) esp_log_level_set("TRANS_TCP", ESP_LOG_DEBUG); ESP_ERROR_CHECK(nvs_flash_init()); - tcpip_adapter_init(); + esp_netif_init(); ESP_ERROR_CHECK(esp_event_loop_create_default()); /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig. diff --git a/examples/system/ota/advanced_https_ota/main/advanced_https_ota_example.c b/examples/system/ota/advanced_https_ota/main/advanced_https_ota_example.c index e6b4bd15c8..6728e63b29 100644 --- a/examples/system/ota/advanced_https_ota/main/advanced_https_ota_example.c +++ b/examples/system/ota/advanced_https_ota/main/advanced_https_ota_example.c @@ -125,7 +125,7 @@ void app_main(void) } ESP_ERROR_CHECK( err ); - tcpip_adapter_init(); + esp_netif_init(); ESP_ERROR_CHECK(esp_event_loop_create_default()); /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig. diff --git a/examples/system/ota/native_ota_example/main/native_ota_example.c b/examples/system/ota/native_ota_example/main/native_ota_example.c index 08b6c9bb62..0e8b32d5e4 100644 --- a/examples/system/ota/native_ota_example/main/native_ota_example.c +++ b/examples/system/ota/native_ota_example/main/native_ota_example.c @@ -278,7 +278,7 @@ void app_main(void) } ESP_ERROR_CHECK( err ); - tcpip_adapter_init(); + esp_netif_init(); ESP_ERROR_CHECK(esp_event_loop_create_default()); /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig. diff --git a/examples/system/ota/simple_ota_example/main/simple_ota_example.c b/examples/system/ota/simple_ota_example/main/simple_ota_example.c index 494c85f54f..086b0f83a9 100644 --- a/examples/system/ota/simple_ota_example/main/simple_ota_example.c +++ b/examples/system/ota/simple_ota_example/main/simple_ota_example.c @@ -112,7 +112,7 @@ void app_main(void) } ESP_ERROR_CHECK(err); - tcpip_adapter_init(); + esp_netif_init(); ESP_ERROR_CHECK(esp_event_loop_create_default()); /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig. From 204492bd7838d3687719473a7de30876f3d1ee7e Mon Sep 17 00:00:00 2001 From: David Cermak Date: Sat, 31 Aug 2019 16:21:31 +0200 Subject: [PATCH 05/31] examples: wifi getting started examples to use esp_netif_init() and create default netif instances for station and AP --- .../wifi/getting_started/softAP/main/softap_example_main.c | 3 ++- .../wifi/getting_started/station/main/station_example_main.c | 4 +++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/examples/wifi/getting_started/softAP/main/softap_example_main.c b/examples/wifi/getting_started/softAP/main/softap_example_main.c index 624e020ed6..ca1883e041 100644 --- a/examples/wifi/getting_started/softAP/main/softap_example_main.c +++ b/examples/wifi/getting_started/softAP/main/softap_example_main.c @@ -45,8 +45,9 @@ static void wifi_event_handler(void* arg, esp_event_base_t event_base, void wifi_init_softap(void) { - tcpip_adapter_init(); + esp_netif_init(); ESP_ERROR_CHECK(esp_event_loop_create_default()); + esp_netif_create_default_wifi_ap(); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); ESP_ERROR_CHECK(esp_wifi_init(&cfg)); diff --git a/examples/wifi/getting_started/station/main/station_example_main.c b/examples/wifi/getting_started/station/main/station_example_main.c index ce349e8395..9a5a94ac69 100644 --- a/examples/wifi/getting_started/station/main/station_example_main.c +++ b/examples/wifi/getting_started/station/main/station_example_main.c @@ -64,13 +64,15 @@ void wifi_init_sta(void) { s_wifi_event_group = xEventGroupCreate(); - tcpip_adapter_init(); + esp_netif_init(); ESP_ERROR_CHECK(esp_event_loop_create_default()); + esp_netif_create_default_wifi_sta(); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); ESP_ERROR_CHECK(esp_wifi_init(&cfg)); + ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL)); ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL)); From c00123c77b45c0143b797a38385c8e126d01489f Mon Sep 17 00:00:00 2001 From: David Cermak Date: Sat, 31 Aug 2019 16:24:45 +0200 Subject: [PATCH 06/31] provisioning: examples updated to use esp_netif instead of tcpip_adapter --- examples/provisioning/ble_prov/main/app_main.c | 5 +++-- examples/provisioning/ble_prov/main/app_prov_handlers.c | 9 ++++----- examples/provisioning/console_prov/main/app_main.c | 5 +++-- .../provisioning/console_prov/main/app_prov_handlers.c | 9 ++++----- examples/provisioning/custom_config/main/app_main.c | 6 ++++-- .../provisioning/custom_config/main/app_prov_handlers.c | 9 ++++----- examples/provisioning/manager/main/app_main.c | 5 +++-- examples/provisioning/softap_prov/main/app_main.c | 6 ++++-- .../provisioning/softap_prov/main/app_prov_handlers.c | 9 ++++----- 9 files changed, 33 insertions(+), 30 deletions(-) diff --git a/examples/provisioning/ble_prov/main/app_main.c b/examples/provisioning/ble_prov/main/app_main.c index 04361574b1..67f2968262 100644 --- a/examples/provisioning/ble_prov/main/app_main.c +++ b/examples/provisioning/ble_prov/main/app_main.c @@ -112,7 +112,7 @@ static void start_ble_provisioning(void) void app_main(void) { /* Initialize networking stack */ - tcpip_adapter_init(); + esp_netif_init(); /* Create default event loop needed by the * main app and the provisioning service */ @@ -121,7 +121,8 @@ void app_main(void) /* Initialize NVS needed by Wi-Fi */ ESP_ERROR_CHECK(nvs_flash_init()); - /* Initialize Wi-Fi with default config */ + /* Initialize Wi-Fi including netif with default config */ + esp_netif_create_default_wifi_sta(); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); ESP_ERROR_CHECK(esp_wifi_init(&cfg)); diff --git a/examples/provisioning/ble_prov/main/app_prov_handlers.c b/examples/provisioning/ble_prov/main/app_prov_handlers.c index 05dc895dcc..87cc64f0c1 100644 --- a/examples/provisioning/ble_prov/main/app_prov_handlers.c +++ b/examples/provisioning/ble_prov/main/app_prov_handlers.c @@ -15,7 +15,7 @@ #include #include -#include +#include #include @@ -60,10 +60,9 @@ static esp_err_t get_status_handler(wifi_prov_config_get_data_t *resp_data, wifi ESP_LOGI(TAG, "Connected state"); /* IP Addr assigned to STA */ - tcpip_adapter_ip_info_t ip_info; - tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_STA, &ip_info); - char *ip_addr = ip4addr_ntoa(&ip_info.ip); - strcpy(resp_data->conn_info.ip_addr, ip_addr); + esp_netif_ip_info_t ip_info; + esp_netif_get_ip_info(esp_netif_get_handle_from_ifkey("WIFI_STA_DEF"), &ip_info); + esp_ip4addr_ntoa(&ip_info.ip, resp_data->conn_info.ip_addr, sizeof(resp_data->conn_info.ip_addr)); /* AP information to which STA is connected */ wifi_ap_record_t ap_info; diff --git a/examples/provisioning/console_prov/main/app_main.c b/examples/provisioning/console_prov/main/app_main.c index 366a0a49ab..f735078bc8 100644 --- a/examples/provisioning/console_prov/main/app_main.c +++ b/examples/provisioning/console_prov/main/app_main.c @@ -83,7 +83,7 @@ static void start_console_provisioning(void) void app_main(void) { /* Initialize networking stack */ - tcpip_adapter_init(); + esp_netif_init(); /* Create default event loop needed by the * main app and the provisioning service */ @@ -92,7 +92,8 @@ void app_main(void) /* Initialize NVS needed by Wi-Fi */ ESP_ERROR_CHECK(nvs_flash_init()); - /* Initialize Wi-Fi with default config */ + /* Initialize Wi-Fi including netif with default config */ + esp_netif_create_default_wifi_sta(); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); ESP_ERROR_CHECK(esp_wifi_init(&cfg)); diff --git a/examples/provisioning/console_prov/main/app_prov_handlers.c b/examples/provisioning/console_prov/main/app_prov_handlers.c index e3fa614962..850be09c15 100644 --- a/examples/provisioning/console_prov/main/app_prov_handlers.c +++ b/examples/provisioning/console_prov/main/app_prov_handlers.c @@ -15,7 +15,7 @@ #include #include -#include +#include #include @@ -60,10 +60,9 @@ static esp_err_t get_status_handler(wifi_prov_config_get_data_t *resp_data, wifi ESP_LOGI(TAG, "Connected state"); /* IP Addr assigned to STA */ - tcpip_adapter_ip_info_t ip_info; - tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_STA, &ip_info); - char *ip_addr = ip4addr_ntoa(&ip_info.ip); - strcpy(resp_data->conn_info.ip_addr, ip_addr); + esp_netif_ip_info_t ip_info; + esp_netif_get_ip_info(esp_netif_get_handle_from_ifkey("WIFI_STA_DEF"), &ip_info); + esp_ip4addr_ntoa(&ip_info.ip, resp_data->conn_info.ip_addr, sizeof(resp_data->conn_info.ip_addr)); /* AP information to which STA is connected */ wifi_ap_record_t ap_info; diff --git a/examples/provisioning/custom_config/main/app_main.c b/examples/provisioning/custom_config/main/app_main.c index 3eb53365f4..7c39f69eb5 100644 --- a/examples/provisioning/custom_config/main/app_main.c +++ b/examples/provisioning/custom_config/main/app_main.c @@ -84,7 +84,7 @@ static void start_softap_provisioning(void) void app_main(void) { /* Initialize networking stack */ - tcpip_adapter_init(); + esp_netif_init(); /* Create default event loop needed by the * main app and the provisioning service */ @@ -93,7 +93,9 @@ void app_main(void) /* Initialize NVS needed by Wi-Fi */ ESP_ERROR_CHECK(nvs_flash_init()); - /* Initialize Wi-Fi with default config */ + /* Initialize Wi-Fi including netif with default config */ + esp_netif_create_default_wifi_sta(); + esp_netif_create_default_wifi_ap(); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); ESP_ERROR_CHECK(esp_wifi_init(&cfg)); diff --git a/examples/provisioning/custom_config/main/app_prov_handlers.c b/examples/provisioning/custom_config/main/app_prov_handlers.c index 2f320890e0..490be081c8 100644 --- a/examples/provisioning/custom_config/main/app_prov_handlers.c +++ b/examples/provisioning/custom_config/main/app_prov_handlers.c @@ -15,7 +15,7 @@ #include #include -#include +#include #include #include @@ -72,10 +72,9 @@ static esp_err_t get_status_handler(wifi_prov_config_get_data_t *resp_data, wifi ESP_LOGI(TAG, "Connected state"); /* IP Addr assigned to STA */ - tcpip_adapter_ip_info_t ip_info; - tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_STA, &ip_info); - char *ip_addr = ip4addr_ntoa(&ip_info.ip); - strcpy(resp_data->conn_info.ip_addr, ip_addr); + esp_netif_ip_info_t ip_info; + esp_netif_get_ip_info(esp_netif_get_handle_from_ifkey("WIFI_STA_DEF"), &ip_info); + esp_ip4addr_ntoa(&ip_info.ip, resp_data->conn_info.ip_addr, sizeof(resp_data->conn_info.ip_addr)); /* AP information to which STA is connected */ wifi_ap_record_t ap_info; diff --git a/examples/provisioning/manager/main/app_main.c b/examples/provisioning/manager/main/app_main.c index 799b0c634b..2b859702b1 100644 --- a/examples/provisioning/manager/main/app_main.c +++ b/examples/provisioning/manager/main/app_main.c @@ -107,7 +107,7 @@ void app_main(void) } /* Initialize TCP/IP */ - tcpip_adapter_init(); + esp_netif_init(); /* Initialize the event loop */ ESP_ERROR_CHECK(esp_event_loop_create_default()); @@ -118,7 +118,8 @@ void app_main(void) ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL)); ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL)); - /* Initialize Wi-Fi */ + /* Initialize Wi-Fi including netif with default config */ + esp_netif_create_default_wifi_sta(); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); ESP_ERROR_CHECK(esp_wifi_init(&cfg)); diff --git a/examples/provisioning/softap_prov/main/app_main.c b/examples/provisioning/softap_prov/main/app_main.c index 676c85925d..8eb9225cd2 100644 --- a/examples/provisioning/softap_prov/main/app_main.c +++ b/examples/provisioning/softap_prov/main/app_main.c @@ -99,7 +99,7 @@ static void start_softap_provisioning(void) void app_main(void) { /* Initialize networking stack */ - tcpip_adapter_init(); + esp_netif_init(); /* Create default event loop needed by the * main app and the provisioning service */ @@ -108,7 +108,9 @@ void app_main(void) /* Initialize NVS needed by Wi-Fi */ ESP_ERROR_CHECK(nvs_flash_init()); - /* Initialize Wi-Fi with default config */ + /* Initialize Wi-Fi including netif with default config */ + esp_netif_create_default_wifi_sta(); + esp_netif_create_default_wifi_ap(); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); ESP_ERROR_CHECK(esp_wifi_init(&cfg)); diff --git a/examples/provisioning/softap_prov/main/app_prov_handlers.c b/examples/provisioning/softap_prov/main/app_prov_handlers.c index 05dc895dcc..87cc64f0c1 100644 --- a/examples/provisioning/softap_prov/main/app_prov_handlers.c +++ b/examples/provisioning/softap_prov/main/app_prov_handlers.c @@ -15,7 +15,7 @@ #include #include -#include +#include #include @@ -60,10 +60,9 @@ static esp_err_t get_status_handler(wifi_prov_config_get_data_t *resp_data, wifi ESP_LOGI(TAG, "Connected state"); /* IP Addr assigned to STA */ - tcpip_adapter_ip_info_t ip_info; - tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_STA, &ip_info); - char *ip_addr = ip4addr_ntoa(&ip_info.ip); - strcpy(resp_data->conn_info.ip_addr, ip_addr); + esp_netif_ip_info_t ip_info; + esp_netif_get_ip_info(esp_netif_get_handle_from_ifkey("WIFI_STA_DEF"), &ip_info); + esp_ip4addr_ntoa(&ip_info.ip, resp_data->conn_info.ip_addr, sizeof(resp_data->conn_info.ip_addr)); /* AP information to which STA is connected */ wifi_ap_record_t ap_info; From 19e24fe61ed5ea6698dfd5e1f427e783360aa846 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Sat, 31 Aug 2019 19:06:54 +0200 Subject: [PATCH 07/31] mdns: update mdns to use esp-netif for mdns supported services such as STA, AP, ETH removes also include dependency on lwip to use esp_netif defined address fields and structures --- components/mdns/CMakeLists.txt | 2 +- components/mdns/include/mdns.h | 18 +- components/mdns/mdns.c | 269 +++++++++++------- components/mdns/mdns_console.c | 6 +- components/mdns/mdns_networking.c | 48 ++-- .../mdns/private_include/mdns_networking.h | 6 +- .../mdns/private_include/mdns_private.h | 30 +- .../protocols/mdns/main/mdns_example_main.c | 11 +- 8 files changed, 226 insertions(+), 164 deletions(-) diff --git a/components/mdns/CMakeLists.txt b/components/mdns/CMakeLists.txt index 7b744dde56..15b0d40f80 100644 --- a/components/mdns/CMakeLists.txt +++ b/components/mdns/CMakeLists.txt @@ -3,5 +3,5 @@ idf_component_register(SRCS "mdns.c" "mdns_networking.c" INCLUDE_DIRS "include" PRIV_INCLUDE_DIRS "private_include" - REQUIRES lwip mbedtls console tcpip_adapter) + REQUIRES lwip mbedtls console esp_netif) diff --git a/components/mdns/include/mdns.h b/components/mdns/include/mdns.h index 1ffa312c05..5e93d2fc29 100644 --- a/components/mdns/include/mdns.h +++ b/components/mdns/include/mdns.h @@ -18,7 +18,7 @@ extern "C" { #endif -#include +#include #include "esp_event.h" #define MDNS_TYPE_A 0x0001 @@ -52,17 +52,25 @@ typedef struct { * @brief mDNS query linked list IP item */ typedef struct mdns_ip_addr_s { - ip_addr_t addr; /*!< IP address */ + esp_ip_addr_t addr; /*!< IP address */ struct mdns_ip_addr_s * next; /*!< next IP, or NULL for the last IP in the list */ } mdns_ip_addr_t; +typedef enum mdns_if_internal { + MDNS_IF_STA = 0, + MDNS_IF_AP = 1, + MDNS_IF_ETH = 2, + MDNS_IF_MAX +} mdns_if_t; + /** * @brief mDNS query result structure */ typedef struct mdns_result_s { struct mdns_result_s * next; /*!< next result, or NULL for the last result in the list */ - tcpip_adapter_if_t tcpip_if; /*!< interface on which the result came (AP/STA/ETH) */ + mdns_if_t tcpip_if; /*!< interface index */ + mdns_ip_protocol_t ip_protocol; /*!< ip_protocol type of the interface (v4/v6) */ // PTR char * instance_name; /*!< instance name */ @@ -329,7 +337,7 @@ esp_err_t mdns_query_txt(const char * instance_name, const char * service_type, * - ESP_ERR_NO_MEM memory error * - ESP_ERR_INVALID_ARG parameter error */ -esp_err_t mdns_query_a(const char * host_name, uint32_t timeout, ip4_addr_t * addr); +esp_err_t mdns_query_a(const char * host_name, uint32_t timeout, esp_ip4_addr_t * addr); /** * @brief Query mDNS for A record @@ -344,7 +352,7 @@ esp_err_t mdns_query_a(const char * host_name, uint32_t timeout, ip4_addr_t * ad * - ESP_ERR_NO_MEM memory error * - ESP_ERR_INVALID_ARG parameter error */ -esp_err_t mdns_query_aaaa(const char * host_name, uint32_t timeout, ip6_addr_t * addr); +esp_err_t mdns_query_aaaa(const char * host_name, uint32_t timeout, esp_ip6_addr_t * addr); /** * @brief System event handler diff --git a/components/mdns/mdns.c b/components/mdns/mdns.c index 31d0a96ce8..cb5f86ea08 100644 --- a/components/mdns/mdns.c +++ b/components/mdns/mdns.c @@ -37,11 +37,42 @@ static volatile TaskHandle_t _mdns_service_task_handle = NULL; static SemaphoreHandle_t _mdns_service_semaphore = NULL; static void _mdns_search_finish_done(void); -static mdns_search_once_t * _mdns_search_find_from(mdns_search_once_t * search, mdns_name_t * name, uint16_t type, tcpip_adapter_if_t tcpip_if, mdns_ip_protocol_t ip_protocol); -static void _mdns_search_result_add_ip(mdns_search_once_t * search, const char * hostname, ip_addr_t * ip, tcpip_adapter_if_t tcpip_if, mdns_ip_protocol_t ip_protocol); -static void _mdns_search_result_add_srv(mdns_search_once_t * search, const char * hostname, uint16_t port, tcpip_adapter_if_t tcpip_if, mdns_ip_protocol_t ip_protocol); -static void _mdns_search_result_add_txt(mdns_search_once_t * search, mdns_txt_item_t * txt, size_t txt_count, tcpip_adapter_if_t tcpip_if, mdns_ip_protocol_t ip_protocol); -static mdns_result_t * _mdns_search_result_add_ptr(mdns_search_once_t * search, const char * instance, tcpip_adapter_if_t tcpip_if, mdns_ip_protocol_t ip_protocol); +static mdns_search_once_t * _mdns_search_find_from(mdns_search_once_t * search, mdns_name_t * name, uint16_t type, mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol); +static void _mdns_search_result_add_ip(mdns_search_once_t * search, const char * hostname, esp_ip_addr_t * ip, mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol); +static void _mdns_search_result_add_srv(mdns_search_once_t * search, const char * hostname, uint16_t port, mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol); +static void _mdns_search_result_add_txt(mdns_search_once_t * search, mdns_txt_item_t * txt, size_t txt_count, mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol); +static mdns_result_t * _mdns_search_result_add_ptr(mdns_search_once_t * search, const char * instance, mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol); + +/* + * @brief Internal collection of mdns supported interfaces + * + */ +static esp_netif_t * s_esp_netifs[MDNS_IF_MAX] = {}; + +/* + * @brief Convert mnds if to esp-netif handle + */ +esp_netif_t *_mdns_get_esp_netif(mdns_if_t tcpip_if) +{ + if (tcpip_if < MDNS_IF_MAX) { + return s_esp_netifs[tcpip_if]; + } + return NULL; +} + +/* + * @brief Convert esp-netif handle to mnds if + */ +static mdns_if_t _mdns_get_if_from_esp_netif(esp_netif_t *interface) +{ + for (int i=0; iinterfaces[tcpip_if].pcbs[MDNS_IP_PROTOCOL_V4].state == PCB_DUP @@ -833,7 +864,7 @@ static bool _mdns_if_is_dup(tcpip_adapter_if_t tcpip_if) /** * @brief Check if IPv6 address is NULL */ -static bool _ipv6_address_is_zero(ip6_addr_t ip6) +static bool _ipv6_address_is_zero(esp_ip6_addr_t ip6) { uint8_t i; uint8_t * data = (uint8_t *)ip6.addr; @@ -850,7 +881,7 @@ static bool _ipv6_address_is_zero(ip6_addr_t ip6) * * @return number of answers added to the packet */ -static uint8_t _mdns_append_answer(uint8_t * packet, uint16_t * index, mdns_out_answer_t * answer, tcpip_adapter_if_t tcpip_if) +static uint8_t _mdns_append_answer(uint8_t * packet, uint16_t * index, mdns_out_answer_t * answer, mdns_if_t tcpip_if) { if (answer->type == MDNS_TYPE_PTR) { @@ -871,11 +902,11 @@ static uint8_t _mdns_append_answer(uint8_t * packet, uint16_t * index, mdns_out_ } else if (answer->type == MDNS_TYPE_SDPTR) { return _mdns_append_sdptr_record(packet, index, answer->service, answer->flush, answer->bye) > 0; } else if (answer->type == MDNS_TYPE_A) { - tcpip_adapter_ip_info_t if_ip_info; + esp_netif_ip_info_t if_ip_info; if (!_mdns_server->interfaces[tcpip_if].pcbs[MDNS_IP_PROTOCOL_V4].pcb && _mdns_server->interfaces[tcpip_if].pcbs[MDNS_IP_PROTOCOL_V4].state != PCB_DUP) { return 0; } - if (tcpip_adapter_get_ip_info(tcpip_if, &if_ip_info)) { + if (esp_netif_get_ip_info(_mdns_get_esp_netif(tcpip_if), &if_ip_info)) { return 0; } if (_mdns_append_a_record(packet, index, if_ip_info.ip.addr, answer->flush, answer->bye) <= 0) { @@ -884,8 +915,8 @@ static uint8_t _mdns_append_answer(uint8_t * packet, uint16_t * index, mdns_out_ if (!_mdns_if_is_dup(tcpip_if)) { return 1; } - tcpip_adapter_if_t other_if = _mdns_get_other_if (tcpip_if); - if (tcpip_adapter_get_ip_info(other_if, &if_ip_info)) { + mdns_if_t other_if = _mdns_get_other_if (tcpip_if); + if (esp_netif_get_ip_info(_mdns_get_esp_netif(other_if), &if_ip_info)) { return 1; } if (_mdns_append_a_record(packet, index, if_ip_info.ip.addr, answer->flush, answer->bye) > 0) { @@ -893,11 +924,11 @@ static uint8_t _mdns_append_answer(uint8_t * packet, uint16_t * index, mdns_out_ } return 1; } else if (answer->type == MDNS_TYPE_AAAA) { - struct ip6_addr if_ip6; + struct esp_ip6_addr if_ip6; if (!_mdns_server->interfaces[tcpip_if].pcbs[MDNS_IP_PROTOCOL_V6].pcb && _mdns_server->interfaces[tcpip_if].pcbs[MDNS_IP_PROTOCOL_V6].state != PCB_DUP) { return 0; } - if (tcpip_adapter_get_ip6_linklocal(tcpip_if, &if_ip6)) { + if (esp_netif_get_ip6_linklocal(_mdns_get_esp_netif(tcpip_if), &if_ip6)) { return 0; } if (_ipv6_address_is_zero(if_ip6)) { @@ -909,8 +940,8 @@ static uint8_t _mdns_append_answer(uint8_t * packet, uint16_t * index, mdns_out_ if (!_mdns_if_is_dup(tcpip_if)) { return 1; } - tcpip_adapter_if_t other_if = _mdns_get_other_if (tcpip_if); - if (tcpip_adapter_get_ip6_linklocal(other_if, &if_ip6)) { + mdns_if_t other_if = _mdns_get_other_if (tcpip_if); + if (esp_netif_get_ip6_linklocal(_mdns_get_esp_netif(other_if), &if_ip6)) { return 1; } if (_mdns_append_aaaa_record(packet, index, (uint8_t*)if_ip6.addr, answer->flush, answer->bye) > 0) { @@ -1046,7 +1077,7 @@ static void _mdns_clear_tx_queue_head(void) * @param tcpip_if the interface * @param ip_protocol pcb type V4/V6 */ -static void _mdns_clear_pcb_tx_queue_head(tcpip_adapter_if_t tcpip_if, mdns_ip_protocol_t ip_protocol) +static void _mdns_clear_pcb_tx_queue_head(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol) { mdns_tx_packet_t * q, * p; while (_mdns_server->tx_queue_head && _mdns_server->tx_queue_head->tcpip_if == tcpip_if && _mdns_server->tx_queue_head->ip_protocol == ip_protocol) { @@ -1074,7 +1105,7 @@ static void _mdns_clear_pcb_tx_queue_head(tcpip_adapter_if_t tcpip_if, mdns_ip_p * @param tcpip_if the interface * @param ip_protocol pcb type V4/V6 */ -static mdns_tx_packet_t * _mdns_get_next_pcb_packet(tcpip_adapter_if_t tcpip_if, mdns_ip_protocol_t ip_protocol) +static mdns_tx_packet_t * _mdns_get_next_pcb_packet(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol) { mdns_tx_packet_t * q = _mdns_server->tx_queue_head; while (q) { @@ -1089,7 +1120,7 @@ static mdns_tx_packet_t * _mdns_get_next_pcb_packet(tcpip_adapter_if_t tcpip_if, /** * @brief Find, remove and free answer from the scheduled packets */ -static void _mdns_remove_scheduled_answer(tcpip_adapter_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, uint16_t type, mdns_srv_item_t * service) +static void _mdns_remove_scheduled_answer(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, uint16_t type, mdns_srv_item_t * service) { mdns_srv_item_t s = {NULL, NULL}; if (!service) { @@ -1178,7 +1209,7 @@ static bool _mdns_alloc_answer(mdns_out_answer_t ** destnation, uint16_t type, m /** * @brief Allocate new packet for sending */ -static mdns_tx_packet_t * _mdns_alloc_packet_default(tcpip_adapter_if_t tcpip_if, mdns_ip_protocol_t ip_protocol) +static mdns_tx_packet_t * _mdns_alloc_packet_default(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol) { mdns_tx_packet_t * packet = (mdns_tx_packet_t*)malloc(sizeof(mdns_tx_packet_t)); if (!packet) { @@ -1192,8 +1223,8 @@ static mdns_tx_packet_t * _mdns_alloc_packet_default(tcpip_adapter_if_t tcpip_if if (ip_protocol == MDNS_IP_PROTOCOL_V4) { IP_ADDR4(&packet->dst, 224, 0, 0, 251); } else { - ip_addr_t addr = IPADDR6_INIT(0x000002ff, 0, 0, 0xfb000000); - memcpy(&packet->dst, &addr, sizeof(ip_addr_t)); + esp_ip_addr_t addr = IPADDR6_INIT(0x000002ff, 0, 0, 0xfb000000); + memcpy(&packet->dst, &addr, sizeof(esp_ip_addr_t)); } return packet; } @@ -1275,7 +1306,7 @@ static void _mdns_create_answer_from_parsed_packet(mdns_parsed_packet_t * parsed q = q->next; } if (unicast || !send_flush) { - memcpy(&packet->dst, &parsed_packet->src, sizeof(ip_addr_t)); + memcpy(&packet->dst, &parsed_packet->src, sizeof(esp_ip_addr_t)); packet->port = parsed_packet->src_port; } @@ -1309,7 +1340,7 @@ static bool _mdns_question_exists(mdns_out_question_t * needle, mdns_out_questio /** * @brief Create probe packet for particular services on particular PCB */ -static mdns_tx_packet_t * _mdns_create_probe_packet(tcpip_adapter_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, mdns_srv_item_t * services[], size_t len, bool first, bool include_ip) +static mdns_tx_packet_t * _mdns_create_probe_packet(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, mdns_srv_item_t * services[], size_t len, bool first, bool include_ip) { mdns_tx_packet_t * packet = _mdns_alloc_packet_default(tcpip_if, ip_protocol); if (!packet) { @@ -1385,7 +1416,7 @@ static mdns_tx_packet_t * _mdns_create_probe_packet(tcpip_adapter_if_t tcpip_if, /** * @brief Create announce packet for particular services on particular PCB */ -static mdns_tx_packet_t * _mdns_create_announce_packet(tcpip_adapter_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, mdns_srv_item_t * services[], size_t len, bool include_ip) +static mdns_tx_packet_t * _mdns_create_announce_packet(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, mdns_srv_item_t * services[], size_t len, bool include_ip) { mdns_tx_packet_t * packet = _mdns_alloc_packet_default(tcpip_if, ip_protocol); if (!packet) { @@ -1451,7 +1482,7 @@ static mdns_tx_packet_t * _mdns_create_announce_from_probe(mdns_tx_packet_t * pr /** * @brief Send by for particular services on particular PCB */ -static void _mdns_pcb_send_bye(tcpip_adapter_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, mdns_srv_item_t ** services, size_t len, bool include_ip) +static void _mdns_pcb_send_bye(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, mdns_srv_item_t ** services, size_t len, bool include_ip) { mdns_tx_packet_t * packet = _mdns_alloc_packet_default(tcpip_if, ip_protocol); if (!packet) { @@ -1476,7 +1507,7 @@ static void _mdns_pcb_send_bye(tcpip_adapter_if_t tcpip_if, mdns_ip_protocol_t i /** * @brief Send probe for additional services on particular PCB */ -static void _mdns_init_pcb_probe_new_service(tcpip_adapter_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, mdns_srv_item_t ** services, size_t len, bool probe_ip) +static void _mdns_init_pcb_probe_new_service(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, mdns_srv_item_t ** services, size_t len, bool probe_ip) { mdns_pcb_t * pcb = &_mdns_server->interfaces[tcpip_if].pcbs[ip_protocol]; size_t services_final_len = len; @@ -1532,7 +1563,7 @@ static void _mdns_init_pcb_probe_new_service(tcpip_adapter_if_t tcpip_if, mdns_i * - If pcb probing then add only non-probing services and restarts probing * - If pcb not probing, run probing for all specified services */ -static void _mdns_init_pcb_probe(tcpip_adapter_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, mdns_srv_item_t ** services, size_t len, bool probe_ip) +static void _mdns_init_pcb_probe(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, mdns_srv_item_t ** services, size_t len, bool probe_ip) { mdns_pcb_t * pcb = &_mdns_server->interfaces[tcpip_if].pcbs[ip_protocol]; @@ -1572,7 +1603,7 @@ static void _mdns_init_pcb_probe(tcpip_adapter_if_t tcpip_if, mdns_ip_protocol_t /** * @brief Restart the responder on particular PCB */ -static void _mdns_restart_pcb(tcpip_adapter_if_t tcpip_if, mdns_ip_protocol_t ip_protocol) +static void _mdns_restart_pcb(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol) { size_t srv_count = 0; mdns_srv_item_t * a = _mdns_server->services; @@ -1600,10 +1631,10 @@ static void _mdns_send_bye(mdns_srv_item_t ** services, size_t len, bool include return; } - for (i=0; iinterfaces[i].pcbs[j].pcb && _mdns_server->interfaces[i].pcbs[j].state == PCB_RUNNING) { - _mdns_pcb_send_bye((tcpip_adapter_if_t)i, (mdns_ip_protocol_t)j, services, len, include_ip); + _mdns_pcb_send_bye((mdns_if_t)i, (mdns_ip_protocol_t)j, services, len, include_ip); } } } @@ -1612,7 +1643,7 @@ static void _mdns_send_bye(mdns_srv_item_t ** services, size_t len, bool include /** * @brief Send announcement on particular PCB */ -static void _mdns_announce_pcb(tcpip_adapter_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, mdns_srv_item_t ** services, size_t len, bool include_ip) +static void _mdns_announce_pcb(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, mdns_srv_item_t ** services, size_t len, bool include_ip) { mdns_pcb_t * _pcb = &_mdns_server->interfaces[tcpip_if].pcbs[ip_protocol]; size_t i; @@ -1659,7 +1690,7 @@ static void _mdns_announce_pcb(tcpip_adapter_if_t tcpip_if, mdns_ip_protocol_t i static void _mdns_probe_all_pcbs(mdns_srv_item_t ** services, size_t len, bool probe_ip, bool clear_old_probe) { uint8_t i, j; - for (i=0; iinterfaces[i].pcbs[j].pcb) { mdns_pcb_t * _pcb = &_mdns_server->interfaces[i].pcbs[j]; @@ -1669,7 +1700,7 @@ static void _mdns_probe_all_pcbs(mdns_srv_item_t ** services, size_t len, bool p _pcb->probe_services_len = 0; _pcb->probe_running = false; } - _mdns_init_pcb_probe((tcpip_adapter_if_t)i, (mdns_ip_protocol_t)j, services, len, probe_ip); + _mdns_init_pcb_probe((mdns_if_t)i, (mdns_ip_protocol_t)j, services, len, probe_ip); } } } @@ -1681,9 +1712,9 @@ static void _mdns_probe_all_pcbs(mdns_srv_item_t ** services, size_t len, bool p static void _mdns_announce_all_pcbs(mdns_srv_item_t ** services, size_t len, bool include_ip) { uint8_t i, j; - for (i=0; iinterfaces[other_if].pcbs[i].pcb) { //stop this interface and mark as dup @@ -2161,27 +2192,27 @@ static void _mdns_dup_interface(tcpip_adapter_if_t tcpip_if) /** * @brief Detect IPv4 address collision */ -static int _mdns_check_a_collision(ip4_addr_t * ip, tcpip_adapter_if_t tcpip_if) +static int _mdns_check_a_collision(esp_ip4_addr_t * ip, mdns_if_t tcpip_if) { - tcpip_adapter_ip_info_t if_ip_info; - tcpip_adapter_ip_info_t other_ip_info; + esp_netif_ip_info_t if_ip_info; + esp_netif_ip_info_t other_ip_info; if (!ip->addr) { return 1;//denial! they win } - if (tcpip_adapter_get_ip_info(tcpip_if, &if_ip_info)) { + if (esp_netif_get_ip_info(_mdns_get_esp_netif(tcpip_if), &if_ip_info)) { return 1;//they win } - int ret = memcmp((uint8_t*)&if_ip_info.ip.addr, (uint8_t*)&ip->addr, sizeof(ip4_addr_t)); + int ret = memcmp((uint8_t*)&if_ip_info.ip.addr, (uint8_t*)&ip->addr, sizeof(esp_ip4_addr_t)); if (ret > 0) { return -1;//we win } else if (ret < 0) { //is it the other interface? - tcpip_adapter_if_t other_if = _mdns_get_other_if (tcpip_if); - if (other_if == TCPIP_ADAPTER_IF_MAX) { + mdns_if_t other_if = _mdns_get_other_if (tcpip_if); + if (other_if == MDNS_IF_MAX) { return 1;//AP interface! They win } - if (tcpip_adapter_get_ip_info(other_if, &other_ip_info)) { + if (esp_netif_get_ip_info(_mdns_get_esp_netif(other_if), &other_ip_info)) { return 1;//IPv4 not active! They win } if (ip->addr != other_ip_info.ip.addr) { @@ -2196,14 +2227,14 @@ static int _mdns_check_a_collision(ip4_addr_t * ip, tcpip_adapter_if_t tcpip_if) /** * @brief Detect IPv6 address collision */ -static int _mdns_check_aaaa_collision(ip6_addr_t * ip, tcpip_adapter_if_t tcpip_if) +static int _mdns_check_aaaa_collision(esp_ip6_addr_t * ip, mdns_if_t tcpip_if) { - struct ip6_addr if_ip6; - struct ip6_addr other_ip6; + struct esp_ip6_addr if_ip6; + struct esp_ip6_addr other_ip6; if (_ipv6_address_is_zero(*ip)) { return 1;//denial! they win } - if (tcpip_adapter_get_ip6_linklocal(tcpip_if, &if_ip6)) { + if (esp_netif_get_ip6_linklocal(_mdns_get_esp_netif(tcpip_if), &if_ip6)) { return 1;//they win } int ret = memcmp((uint8_t*)&if_ip6.addr, (uint8_t*)ip->addr, _MDNS_SIZEOF_IP6_ADDR); @@ -2211,11 +2242,11 @@ static int _mdns_check_aaaa_collision(ip6_addr_t * ip, tcpip_adapter_if_t tcpip_ return -1;//we win } else if (ret < 0) { //is it the other interface? - tcpip_adapter_if_t other_if = _mdns_get_other_if (tcpip_if); - if (other_if == TCPIP_ADAPTER_IF_MAX) { + mdns_if_t other_if = _mdns_get_other_if (tcpip_if); + if (other_if == MDNS_IF_MAX) { return 1;//AP interface! They win } - if (tcpip_adapter_get_ip6_linklocal(other_if, &other_ip6)) { + if (esp_netif_get_ip6_linklocal(_mdns_get_esp_netif(other_if), &other_ip6)) { return 1;//IPv6 not active! They win } if (memcmp((uint8_t*)&other_ip6.addr, (uint8_t*)ip->addr, _MDNS_SIZEOF_IP6_ADDR)) { @@ -2917,7 +2948,7 @@ void mdns_parse_packet(mdns_rx_packet_t * packet) } } else if (type == MDNS_TYPE_AAAA) {//ipv6 - ip_addr_t ip6; + esp_ip_addr_t ip6; ip6.type = IPADDR_TYPE_V6; memcpy(ip6.u_addr.ip6.addr, data_ptr, MDNS_ANSWER_AAAA_SIZE); if (search_result) { @@ -2963,7 +2994,7 @@ void mdns_parse_packet(mdns_rx_packet_t * packet) } } else if (type == MDNS_TYPE_A) { - ip_addr_t ip; + esp_ip_addr_t ip; ip.type = IPADDR_TYPE_V4; memcpy(&(ip.u_addr.ip4.addr), data_ptr, 4); if (search_result) { @@ -3037,7 +3068,7 @@ clear_rx_packet: /** * @brief Enable mDNS interface */ -void _mdns_enable_pcb(tcpip_adapter_if_t tcpip_if, mdns_ip_protocol_t ip_protocol) +void _mdns_enable_pcb(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol) { if (!_mdns_server->interfaces[tcpip_if].pcbs[ip_protocol].pcb) { if (_mdns_pcb_init(tcpip_if, ip_protocol)) { @@ -3050,13 +3081,13 @@ void _mdns_enable_pcb(tcpip_adapter_if_t tcpip_if, mdns_ip_protocol_t ip_protoco /** * @brief Disable mDNS interface */ -void _mdns_disable_pcb(tcpip_adapter_if_t tcpip_if, mdns_ip_protocol_t ip_protocol) +void _mdns_disable_pcb(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol) { if (_mdns_server->interfaces[tcpip_if].pcbs[ip_protocol].pcb) { _mdns_clear_pcb_tx_queue_head(tcpip_if, ip_protocol); _mdns_pcb_deinit(tcpip_if, ip_protocol); - tcpip_adapter_if_t other_if = _mdns_get_other_if (tcpip_if); - if (other_if != TCPIP_ADAPTER_IF_MAX && _mdns_server->interfaces[other_if].pcbs[ip_protocol].state == PCB_DUP) { + mdns_if_t other_if = _mdns_get_other_if (tcpip_if); + if (other_if != MDNS_IF_MAX && _mdns_server->interfaces[other_if].pcbs[ip_protocol].state == PCB_DUP) { _mdns_server->interfaces[other_if].pcbs[ip_protocol].state = PCB_OFF; _mdns_enable_pcb(other_if, ip_protocol); } @@ -3068,32 +3099,41 @@ void _mdns_disable_pcb(tcpip_adapter_if_t tcpip_if, mdns_ip_protocol_t ip_protoc * @brief Dispatch interface changes based on system events */ static void _mdns_handle_system_event(esp_event_base_t event_base, - int32_t event_id, tcpip_adapter_if_t interface) + int32_t event_id, esp_netif_t* interface) { if (!_mdns_server) { return; } - tcpip_adapter_dhcp_status_t dcst; + // Initialize handles to esp-netif if appropriate mdns supported interface started + if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) { + s_esp_netifs[MDNS_IF_STA] = esp_netif_get_handle_from_ifkey("WIFI_STA_DEF"); + } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_AP_START) { + s_esp_netifs[MDNS_IF_AP] = esp_netif_get_handle_from_ifkey("WIFI_AP_DEF"); + } else if (event_base == ETH_EVENT && event_id == ETHERNET_EVENT_START) { + s_esp_netifs[MDNS_IF_ETH] = esp_netif_get_handle_from_ifkey("ETH_DEF"); + } + + esp_netif_dhcp_status_t dcst; if (event_base == WIFI_EVENT) { switch(event_id) { case WIFI_EVENT_STA_CONNECTED: - if (!tcpip_adapter_dhcpc_get_status(TCPIP_ADAPTER_IF_STA, &dcst)) { - if (dcst == TCPIP_ADAPTER_DHCP_STOPPED) { - _mdns_enable_pcb(TCPIP_ADAPTER_IF_STA, MDNS_IP_PROTOCOL_V4); + if (!esp_netif_dhcpc_get_status(_mdns_get_esp_netif(MDNS_IF_STA), &dcst)) { + if (dcst == ESP_NETIF_DHCP_STOPPED) { + _mdns_enable_pcb(MDNS_IF_STA, MDNS_IP_PROTOCOL_V4); } } break; case WIFI_EVENT_STA_DISCONNECTED: - _mdns_disable_pcb(TCPIP_ADAPTER_IF_STA, MDNS_IP_PROTOCOL_V4); - _mdns_disable_pcb(TCPIP_ADAPTER_IF_STA, MDNS_IP_PROTOCOL_V6); + _mdns_disable_pcb(MDNS_IF_STA, MDNS_IP_PROTOCOL_V4); + _mdns_disable_pcb(MDNS_IF_STA, MDNS_IP_PROTOCOL_V6); break; case WIFI_EVENT_AP_START: - _mdns_enable_pcb(TCPIP_ADAPTER_IF_AP, MDNS_IP_PROTOCOL_V4); + _mdns_enable_pcb(MDNS_IF_AP, MDNS_IP_PROTOCOL_V4); break; case WIFI_EVENT_AP_STOP: - _mdns_disable_pcb(TCPIP_ADAPTER_IF_AP, MDNS_IP_PROTOCOL_V4); - _mdns_disable_pcb(TCPIP_ADAPTER_IF_AP, MDNS_IP_PROTOCOL_V6); + _mdns_disable_pcb(MDNS_IF_AP, MDNS_IP_PROTOCOL_V4); + _mdns_disable_pcb(MDNS_IF_AP, MDNS_IP_PROTOCOL_V6); break; default: break; @@ -3103,15 +3143,15 @@ static void _mdns_handle_system_event(esp_event_base_t event_base, else if (event_base == ETH_EVENT) { switch (event_id) { case ETHERNET_EVENT_CONNECTED: - if (!tcpip_adapter_dhcpc_get_status(TCPIP_ADAPTER_IF_ETH, &dcst)) { - if (dcst == TCPIP_ADAPTER_DHCP_STOPPED) { - _mdns_enable_pcb(TCPIP_ADAPTER_IF_ETH, MDNS_IP_PROTOCOL_V4); + if (!esp_netif_dhcpc_get_status(_mdns_get_esp_netif(MDNS_IF_ETH), &dcst)) { + if (dcst == ESP_NETIF_DHCP_STOPPED) { + _mdns_enable_pcb(MDNS_IF_ETH, MDNS_IP_PROTOCOL_V4); } } break; case ETHERNET_EVENT_DISCONNECTED: - _mdns_disable_pcb(TCPIP_ADAPTER_IF_ETH, MDNS_IP_PROTOCOL_V4); - _mdns_disable_pcb(TCPIP_ADAPTER_IF_ETH, MDNS_IP_PROTOCOL_V6); + _mdns_disable_pcb(MDNS_IF_ETH, MDNS_IP_PROTOCOL_V4); + _mdns_disable_pcb(MDNS_IF_ETH, MDNS_IP_PROTOCOL_V6); break; default: break; @@ -3121,17 +3161,21 @@ static void _mdns_handle_system_event(esp_event_base_t event_base, else if (event_base == IP_EVENT) { switch (event_id) { case IP_EVENT_STA_GOT_IP: - _mdns_enable_pcb(TCPIP_ADAPTER_IF_STA, MDNS_IP_PROTOCOL_V4); - _mdns_announce_pcb(TCPIP_ADAPTER_IF_STA, MDNS_IP_PROTOCOL_V6, NULL, 0, true); + _mdns_enable_pcb(MDNS_IF_STA, MDNS_IP_PROTOCOL_V4); + _mdns_announce_pcb(MDNS_IF_STA, MDNS_IP_PROTOCOL_V6, NULL, 0, true); break; #if CONFIG_ETH_ENABLED case IP_EVENT_ETH_GOT_IP: - _mdns_enable_pcb(TCPIP_ADAPTER_IF_ETH, MDNS_IP_PROTOCOL_V4); + _mdns_enable_pcb(MDNS_IF_ETH, MDNS_IP_PROTOCOL_V4); break; #endif case IP_EVENT_GOT_IP6: - _mdns_enable_pcb(interface, MDNS_IP_PROTOCOL_V6); - _mdns_announce_pcb(interface, MDNS_IP_PROTOCOL_V4, NULL, 0, true); + { + mdns_if_t mdns_if = _mdns_get_if_from_esp_netif(interface); + _mdns_enable_pcb(mdns_if, MDNS_IP_PROTOCOL_V6); + _mdns_announce_pcb(mdns_if, MDNS_IP_PROTOCOL_V4, NULL, 0, true); + + } break; default: break; @@ -3248,7 +3292,7 @@ static void _mdns_search_finish_done(void) /** * @brief Create linked IP (copy) from parsed one */ -static mdns_ip_addr_t * _mdns_result_addr_create_ip(ip_addr_t * ip) +static mdns_ip_addr_t * _mdns_result_addr_create_ip(esp_ip_addr_t * ip) { mdns_ip_addr_t * a = (mdns_ip_addr_t *)malloc(sizeof(mdns_ip_addr_t)); if (!a) { @@ -3268,7 +3312,7 @@ static mdns_ip_addr_t * _mdns_result_addr_create_ip(ip_addr_t * ip) /** * @brief Chain new IP to search result */ -static void _mdns_result_add_ip(mdns_result_t * r, ip_addr_t * ip) +static void _mdns_result_add_ip(mdns_result_t * r, esp_ip_addr_t * ip) { mdns_ip_addr_t * a = r->addr; while (a) { @@ -3293,7 +3337,7 @@ static void _mdns_result_add_ip(mdns_result_t * r, ip_addr_t * ip) /** * @brief Called from parser to add A/AAAA data to search result */ -static void _mdns_search_result_add_ip(mdns_search_once_t * search, const char * hostname, ip_addr_t * ip, tcpip_adapter_if_t tcpip_if, mdns_ip_protocol_t ip_protocol) +static void _mdns_search_result_add_ip(mdns_search_once_t * search, const char * hostname, esp_ip_addr_t * ip, mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol) { mdns_result_t * r = NULL; mdns_ip_addr_t * a = NULL; @@ -3346,7 +3390,7 @@ static void _mdns_search_result_add_ip(mdns_search_once_t * search, const char * /** * @brief Called from parser to add PTR data to search result */ -static mdns_result_t * _mdns_search_result_add_ptr(mdns_search_once_t * search, const char * instance, tcpip_adapter_if_t tcpip_if, mdns_ip_protocol_t ip_protocol) +static mdns_result_t * _mdns_search_result_add_ptr(mdns_search_once_t * search, const char * instance, mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol) { mdns_result_t * r = search->result; while (r) { @@ -3382,7 +3426,7 @@ static mdns_result_t * _mdns_search_result_add_ptr(mdns_search_once_t * search, /** * @brief Called from parser to add SRV data to search result */ -static void _mdns_search_result_add_srv(mdns_search_once_t * search, const char * hostname, uint16_t port, tcpip_adapter_if_t tcpip_if, mdns_ip_protocol_t ip_protocol) +static void _mdns_search_result_add_srv(mdns_search_once_t * search, const char * hostname, uint16_t port, mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol) { mdns_result_t * r = search->result; while (r) { @@ -3416,7 +3460,7 @@ static void _mdns_search_result_add_srv(mdns_search_once_t * search, const char /** * @brief Called from parser to add TXT data to search result */ -static void _mdns_search_result_add_txt(mdns_search_once_t * search, mdns_txt_item_t * txt, size_t txt_count, tcpip_adapter_if_t tcpip_if, mdns_ip_protocol_t ip_protocol) +static void _mdns_search_result_add_txt(mdns_search_once_t * search, mdns_txt_item_t * txt, size_t txt_count, mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol) { int i; mdns_result_t * r = search->result; @@ -3460,7 +3504,7 @@ free_txt: /** * @brief Called from packet parser to find matching running search */ -static mdns_search_once_t * _mdns_search_find_from(mdns_search_once_t * s, mdns_name_t * name, uint16_t type, tcpip_adapter_if_t tcpip_if, mdns_ip_protocol_t ip_protocol) +static mdns_search_once_t * _mdns_search_find_from(mdns_search_once_t * s, mdns_name_t * name, uint16_t type, mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol) { mdns_result_t * r = NULL; while (s) { @@ -3530,7 +3574,7 @@ static mdns_search_once_t * _mdns_search_find_from(mdns_search_once_t * s, mdns_ /** * @brief Create search packet for partidular interface */ -static mdns_tx_packet_t * _mdns_create_search_packet(mdns_search_once_t * search, tcpip_adapter_if_t tcpip_if, mdns_ip_protocol_t ip_protocol) +static mdns_tx_packet_t * _mdns_create_search_packet(mdns_search_once_t * search, mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol) { mdns_result_t * r = NULL; mdns_tx_packet_t * packet = _mdns_alloc_packet_default(tcpip_if, ip_protocol); @@ -3586,7 +3630,7 @@ static mdns_tx_packet_t * _mdns_create_search_packet(mdns_search_once_t * search /** * @brief Send search packet to particular interface */ -static void _mdns_search_send_pcb(mdns_search_once_t * search, tcpip_adapter_if_t tcpip_if, mdns_ip_protocol_t ip_protocol) +static void _mdns_search_send_pcb(mdns_search_once_t * search, mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol) { mdns_tx_packet_t * packet = NULL; if (_mdns_server->interfaces[tcpip_if].pcbs[ip_protocol].pcb && _mdns_server->interfaces[tcpip_if].pcbs[ip_protocol].state > PCB_INIT) { @@ -3621,9 +3665,9 @@ static void _mdns_search_send(mdns_search_once_t * search) } uint8_t i, j; - for (i=0; idata.sys_event.event_id = event_id; if (event_base == IP_EVENT && event_id == IP_EVENT_GOT_IP6) { ip_event_got_ip6_t* event = (ip_event_got_ip6_t*) event_data; - action->data.sys_event.interface = tcpip_adapter_if_from_esp_netif(event->esp_netif); + action->data.sys_event.interface = event->esp_netif; } if (xQueueSend(_mdns_server->action_queue, &action, (portTickType)0) != pdPASS) { @@ -4207,14 +4251,14 @@ esp_err_t mdns_init(void) } #endif uint8_t i; - ip6_addr_t tmp_addr6; - tcpip_adapter_ip_info_t if_ip_info; + esp_ip6_addr_t tmp_addr6; + esp_netif_ip_info_t if_ip_info; - for (i=0; i>>>>>> mdns: update mdns to use esp-netif for mdns supported services such as STA, AP, ETH _mdns_dbg_printf(IPV6STR "\n", IPV62STR(ip6)); } else if (type == MDNS_TYPE_A) { - ip4_addr_t ip; - memcpy(&ip, data_ptr, sizeof(ip4_addr_t)); + esp_ip4_addr_t ip; + memcpy(&ip, data_ptr, sizeof(esp_ip4_addr_t)); _mdns_dbg_printf(IPSTR "\n", IP2STR(&ip)); } else if (type == MDNS_TYPE_NSEC) { const uint8_t * old_ptr = data_ptr; diff --git a/components/mdns/mdns_console.c b/components/mdns/mdns_console.c index adbbf0b8f1..2c36a6aee4 100644 --- a/components/mdns/mdns_console.c +++ b/components/mdns/mdns_console.c @@ -42,7 +42,7 @@ static void mdns_print_results(mdns_result_t * results) } a = r->addr; while (a) { - if (a->addr.type == IPADDR_TYPE_V6) { + if (a->addr.type == ESP_IPADDR_TYPE_V6) { printf(" AAAA: " IPV6STR "\n", IPV62STR(a->addr.u_addr.ip6)); } else { printf(" A : " IPSTR "\n", IP2STR(&(a->addr.u_addr.ip4))); @@ -81,7 +81,7 @@ static int cmd_mdns_query_a(int argc, char** argv) printf("Query A: %s.local, Timeout: %d\n", hostname, timeout); - struct ip4_addr addr; + struct esp_ip4_addr addr; addr.addr = 0; esp_err_t err = mdns_query_a(hostname, timeout, &addr); @@ -138,7 +138,7 @@ static int cmd_mdns_query_aaaa(int argc, char** argv) printf("Query AAAA: %s.local, Timeout: %d\n", hostname, timeout); - struct ip6_addr addr; + struct esp_ip6_addr addr; memset(addr.addr, 0, 16); esp_err_t err = mdns_query_aaaa(hostname, timeout, &addr); diff --git a/components/mdns/mdns_networking.c b/components/mdns/mdns_networking.c index ac69b093dc..5f900cf4d1 100644 --- a/components/mdns/mdns_networking.c +++ b/components/mdns/mdns_networking.c @@ -6,6 +6,7 @@ #include #include "mdns_networking.h" #include "esp_log.h" +#include "esp_netif_net_stack.h" extern mdns_server_t * _mdns_server; @@ -60,21 +61,18 @@ static void _udp_pcb_main_deinit(void) /** * @brief Low level UDP Multicast membership control */ -static esp_err_t _udp_join_group(tcpip_adapter_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, bool join) +static esp_err_t _udp_join_group(mdns_if_t if_inx, mdns_ip_protocol_t ip_protocol, bool join) { struct netif * netif = NULL; - void * nif = NULL; + esp_netif_t *tcpip_if = _mdns_get_esp_netif(if_inx); - if (!tcpip_adapter_is_netif_up(tcpip_if)) { + if (!esp_netif_is_netif_up(tcpip_if)) { // Network interface went down before event propagated, skipping IGMP config return ESP_ERR_INVALID_STATE; } - esp_err_t err = tcpip_adapter_get_netif(tcpip_if, &nif); - if (err) { - return ESP_ERR_INVALID_ARG; - } - netif = (struct netif *)nif; + netif = esp_netif_get_netif_impl(tcpip_if); + assert(netif); if (ip_protocol == MDNS_IP_PROTOCOL_V4) { ip_addr_t multicast_addr; @@ -126,7 +124,7 @@ static void _udp_recv(void *arg, struct udp_pcb *upcb, struct pbuf *pb, const ip continue; } - packet->tcpip_if = TCPIP_ADAPTER_IF_MAX; + packet->tcpip_if = MDNS_IF_MAX; packet->pb = this_pb; packet->src_port = rport; memcpy(&packet->src, raddr, sizeof(ip_addr_t)); @@ -145,12 +143,10 @@ static void _udp_recv(void *arg, struct udp_pcb *upcb, struct pbuf *pb, const ip //lwip does not return the proper pcb if you have more than one for the same multicast address (but different interfaces) struct netif * netif = NULL; - void * nif = NULL; struct udp_pcb * pcb = NULL; - for (i=0; iinterfaces[i].pcbs[packet->ip_protocol].pcb; - tcpip_adapter_get_netif (i, &nif); - netif = (struct netif *)nif; + netif = esp_netif_get_netif_impl(_mdns_get_esp_netif(i)); if (pcb && netif && netif == ip_current_input_netif ()) { if (packet->src.type == IPADDR_TYPE_V4) { if ((packet->src.u_addr.ip4.addr & netif->netmask.u_addr.ip4.addr) != (netif->ip_addr.u_addr.ip4.addr & netif->netmask.u_addr.ip4.addr)) { @@ -179,7 +175,7 @@ static void _udp_recv(void *arg, struct udp_pcb *upcb, struct pbuf *pb, const ip */ static bool _udp_pcb_is_in_use(void){ int i, p; - for (i=0; iinterfaces[i].pcbs[p].pcb){ return true; @@ -192,7 +188,7 @@ static bool _udp_pcb_is_in_use(void){ /** * @brief Stop PCB Main code */ -static void _udp_pcb_deinit(tcpip_adapter_if_t tcpip_if, mdns_ip_protocol_t ip_protocol) +static void _udp_pcb_deinit(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol) { if (!_mdns_server) { return; @@ -217,7 +213,7 @@ static void _udp_pcb_deinit(tcpip_adapter_if_t tcpip_if, mdns_ip_protocol_t ip_p /** * @brief Start PCB Main code */ -static esp_err_t _udp_pcb_init(tcpip_adapter_if_t tcpip_if, mdns_ip_protocol_t ip_protocol) +static esp_err_t _udp_pcb_init(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol) { if (!_mdns_server || _mdns_server->interfaces[tcpip_if].pcbs[ip_protocol].pcb) { return ESP_ERR_INVALID_STATE; @@ -240,7 +236,7 @@ static esp_err_t _udp_pcb_init(tcpip_adapter_if_t tcpip_if, mdns_ip_protocol_t i typedef struct { struct tcpip_api_call_data call; - tcpip_adapter_if_t tcpip_if; + mdns_if_t tcpip_if; mdns_ip_protocol_t ip_protocol; struct pbuf *pbt; const ip_addr_t *ip; @@ -274,7 +270,7 @@ static err_t _mdns_pcb_deinit_api(struct tcpip_api_call_data *api_call_msg) * - _mdns prefixed * - commented in mdns_networking.h header */ -esp_err_t _mdns_pcb_init(tcpip_adapter_if_t tcpip_if, mdns_ip_protocol_t ip_protocol) +esp_err_t _mdns_pcb_init(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol) { mdns_api_call_t msg = { .tcpip_if = tcpip_if, @@ -284,7 +280,7 @@ esp_err_t _mdns_pcb_init(tcpip_adapter_if_t tcpip_if, mdns_ip_protocol_t ip_prot return msg.err; } -esp_err_t _mdns_pcb_deinit(tcpip_adapter_if_t tcpip_if, mdns_ip_protocol_t ip_protocol) +esp_err_t _mdns_pcb_deinit(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol) { mdns_api_call_t msg = { .tcpip_if = tcpip_if, @@ -299,19 +295,19 @@ static err_t _mdns_udp_pcb_write_api(struct tcpip_api_call_data *api_call_msg) void * nif = NULL; mdns_api_call_t * msg = (mdns_api_call_t *)api_call_msg; mdns_pcb_t * _pcb = &_mdns_server->interfaces[msg->tcpip_if].pcbs[msg->ip_protocol]; - esp_err_t err = tcpip_adapter_get_netif(msg->tcpip_if, &nif); - if (err) { + nif = esp_netif_get_netif_impl(_mdns_get_esp_netif(msg->tcpip_if)); + if (!nif) { pbuf_free(msg->pbt); - msg->err = err; - return err; + msg->err = ERR_IF; + return ERR_IF; } - err = udp_sendto_if (_pcb->pcb, msg->pbt, msg->ip, msg->port, (struct netif *)nif); + esp_err_t err = udp_sendto_if (_pcb->pcb, msg->pbt, msg->ip, msg->port, (struct netif *)nif); pbuf_free(msg->pbt); msg->err = err; return err; } -size_t _mdns_udp_pcb_write(tcpip_adapter_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, const ip_addr_t *ip, uint16_t port, uint8_t * data, size_t len) +size_t _mdns_udp_pcb_write(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, const esp_ip_addr_t *ip, uint16_t port, uint8_t * data, size_t len) { struct pbuf* pbt = pbuf_alloc(PBUF_TRANSPORT, len, PBUF_RAM); if (pbt == NULL) { @@ -323,7 +319,7 @@ size_t _mdns_udp_pcb_write(tcpip_adapter_if_t tcpip_if, mdns_ip_protocol_t ip_pr .tcpip_if = tcpip_if, .ip_protocol = ip_protocol, .pbt = pbt, - .ip = ip, + .ip = (ip_addr_t *)ip, .port = port }; tcpip_api_call(_mdns_udp_pcb_write_api, &msg.call); diff --git a/components/mdns/private_include/mdns_networking.h b/components/mdns/private_include/mdns_networking.h index 6b32ec7f13..12316f9edc 100644 --- a/components/mdns/private_include/mdns_networking.h +++ b/components/mdns/private_include/mdns_networking.h @@ -34,12 +34,12 @@ esp_err_t _mdns_send_rx_action(mdns_rx_packet_t * packet); /** * @brief Start PCB */ -esp_err_t _mdns_pcb_init(tcpip_adapter_if_t tcpip_if, mdns_ip_protocol_t ip_protocol); +esp_err_t _mdns_pcb_init(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol); /** * @brief Stop PCB */ -esp_err_t _mdns_pcb_deinit(tcpip_adapter_if_t tcpip_if, mdns_ip_protocol_t ip_protocol); +esp_err_t _mdns_pcb_deinit(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol); /** * @brief send packet over UDP @@ -50,6 +50,6 @@ esp_err_t _mdns_pcb_deinit(tcpip_adapter_if_t tcpip_if, mdns_ip_protocol_t ip_pr * * @return length of sent packet or 0 on error */ -size_t _mdns_udp_pcb_write(tcpip_adapter_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, const ip_addr_t *ip, uint16_t port, uint8_t * data, size_t len); +size_t _mdns_udp_pcb_write(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, const esp_ip_addr_t *ip, uint16_t port, uint8_t * data, size_t len); #endif /* ESP_MDNS_NETWORKING_H_ */ diff --git a/components/mdns/private_include/mdns_private.h b/components/mdns/private_include/mdns_private.h index 3c495dd825..900c28ced8 100644 --- a/components/mdns/private_include/mdns_private.h +++ b/components/mdns/private_include/mdns_private.h @@ -214,10 +214,10 @@ typedef struct mdns_parsed_record_s { } mdns_parsed_record_t; typedef struct { - tcpip_adapter_if_t tcpip_if; + mdns_if_t tcpip_if; mdns_ip_protocol_t ip_protocol; //struct udp_pcb *pcb; - ip_addr_t src; + esp_ip_addr_t src; uint16_t src_port; uint8_t multicast; uint8_t authoritative; @@ -229,11 +229,11 @@ typedef struct { } mdns_parsed_packet_t; typedef struct { - tcpip_adapter_if_t tcpip_if; + mdns_if_t tcpip_if; mdns_ip_protocol_t ip_protocol; struct pbuf *pb; - ip_addr_t src; - ip_addr_t dest; + esp_ip_addr_t src; + esp_ip_addr_t dest; uint16_t src_port; uint8_t multicast; } mdns_rx_packet_t; @@ -283,9 +283,9 @@ typedef struct mdns_out_answer_s { typedef struct mdns_tx_packet_s { struct mdns_tx_packet_s * next; uint32_t send_at; - tcpip_adapter_if_t tcpip_if; + mdns_if_t tcpip_if; mdns_ip_protocol_t ip_protocol; - ip_addr_t dst; + esp_ip_addr_t dst; uint16_t port; uint16_t flags; uint8_t distributed; @@ -333,7 +333,7 @@ typedef struct mdns_search_once_s { typedef struct mdns_server_s { struct { mdns_pcb_t pcbs[MDNS_IP_PROTOCOL_MAX]; - } interfaces[TCPIP_ADAPTER_IF_MAX]; + } interfaces[MDNS_IF_MAX]; const char * hostname; const char * instance; mdns_srv_item_t * services; @@ -352,7 +352,7 @@ typedef struct { struct { esp_event_base_t event_base; int32_t event_id; - tcpip_adapter_if_t interface; + esp_netif_t* interface; } sys_event; struct { mdns_srv_item_t * service; @@ -393,4 +393,16 @@ typedef struct { } data; } mdns_action_t; +/* + * @brief Convert mnds if to esp-netif handle + * + * @param tcpip_if mdns supported interface as internal enum + * + * @return + * - ptr to esp-netif on success + * - NULL if no available netif for current interface index + */ +esp_netif_t *_mdns_get_esp_netif(mdns_if_t tcpip_if); + + #endif /* MDNS_PRIVATE_H_ */ diff --git a/examples/protocols/mdns/main/mdns_example_main.c b/examples/protocols/mdns/main/mdns_example_main.c index 76cb3b1b6e..c15f37d189 100644 --- a/examples/protocols/mdns/main/mdns_example_main.c +++ b/examples/protocols/mdns/main/mdns_example_main.c @@ -9,18 +9,15 @@ #include #include "freertos/FreeRTOS.h" #include "freertos/task.h" -#include "freertos/event_groups.h" #include "esp_system.h" -#include "esp_wifi.h" #include "esp_event.h" #include "esp_log.h" #include "nvs_flash.h" -#include "tcpip_adapter.h" +#include "esp_netif.h" #include "protocol_examples_common.h" #include "mdns.h" #include "driver/gpio.h" -#include -#include +#include "netdb.h" #define EXAMPLE_MDNS_INSTANCE CONFIG_MDNS_INSTANCE @@ -88,7 +85,7 @@ static void mdns_print_results(mdns_result_t * results){ } a = r->addr; while(a){ - if(a->addr.type == IPADDR_TYPE_V6){ + if(a->addr.type == ESP_IPADDR_TYPE_V6){ printf(" AAAA: " IPV6STR "\n", IPV62STR(a->addr.u_addr.ip6)); } else { printf(" A : " IPSTR "\n", IP2STR(&(a->addr.u_addr.ip4))); @@ -123,7 +120,7 @@ static void query_mdns_host(const char * host_name) { ESP_LOGI(TAG, "Query A: %s.local", host_name); - struct ip4_addr addr; + struct esp_ip4_addr addr; addr.addr = 0; esp_err_t err = mdns_query_a(host_name, 2000, &addr); From 7f4c8a0b4f10e9c5a6cc7f82e8fa80ee9e894875 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Sun, 1 Sep 2019 11:15:11 +0200 Subject: [PATCH 08/31] ethernet_examples: use esp_netif instead of tcpip-adapter --- .../ethernet/basic/main/ethernet_example_main.c | 11 +++++++---- .../ethernet/eth2ap/main/ethernet_example_main.c | 1 - examples/ethernet/iperf/main/cmd_ethernet.c | 14 +++++++++----- 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/examples/ethernet/basic/main/ethernet_example_main.c b/examples/ethernet/basic/main/ethernet_example_main.c index 2157a9132a..136d5f6879 100644 --- a/examples/ethernet/basic/main/ethernet_example_main.c +++ b/examples/ethernet/basic/main/ethernet_example_main.c @@ -10,7 +10,7 @@ #include #include "freertos/FreeRTOS.h" #include "freertos/task.h" -#include "tcpip_adapter.h" +#include "esp_netif.h" #include "esp_eth.h" #include "esp_event.h" #include "esp_log.h" @@ -52,7 +52,7 @@ static void got_ip_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data) { ip_event_got_ip_t *event = (ip_event_got_ip_t *) event_data; - const tcpip_adapter_ip_info_t *ip_info = &event->ip_info; + const esp_netif_ip_info_t *ip_info = &event->ip_info; ESP_LOGI(TAG, "Ethernet Got IP Address"); ESP_LOGI(TAG, "~~~~~~~~~~~"); @@ -64,10 +64,12 @@ static void got_ip_event_handler(void *arg, esp_event_base_t event_base, void app_main(void) { - tcpip_adapter_init(); + esp_netif_init(); ESP_ERROR_CHECK(esp_event_loop_create_default()); - ESP_ERROR_CHECK(tcpip_adapter_set_default_eth_handlers()); + esp_netif_config_t cfg = ESP_NETIF_DEFAULT_ETH(); + esp_netif_t* eth_netif = esp_netif_new(&cfg); + ESP_ERROR_CHECK(esp_eth_set_default_handlers(eth_netif)); ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, ð_event_handler, NULL)); ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &got_ip_event_handler, NULL)); @@ -112,4 +114,5 @@ void app_main(void) esp_eth_config_t config = ETH_DEFAULT_CONFIG(mac, phy); esp_eth_handle_t eth_handle = NULL; ESP_ERROR_CHECK(esp_eth_driver_install(&config, ð_handle)); + ESP_ERROR_CHECK(esp_netif_attach(eth_netif, eth_handle)); } diff --git a/examples/ethernet/eth2ap/main/ethernet_example_main.c b/examples/ethernet/eth2ap/main/ethernet_example_main.c index f3df345f67..7d65a5bff2 100644 --- a/examples/ethernet/eth2ap/main/ethernet_example_main.c +++ b/examples/ethernet/eth2ap/main/ethernet_example_main.c @@ -192,7 +192,6 @@ static void initialize_wifi(void) ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, wifi_event_handler, NULL)); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); ESP_ERROR_CHECK(esp_wifi_init(&cfg)); - ESP_ERROR_CHECK(tcpip_adapter_clear_default_wifi_handlers()); ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM)); wifi_config_t wifi_config = { .ap = { diff --git a/examples/ethernet/iperf/main/cmd_ethernet.c b/examples/ethernet/iperf/main/cmd_ethernet.c index 075908b13b..33dfba6ead 100644 --- a/examples/ethernet/iperf/main/cmd_ethernet.c +++ b/examples/ethernet/iperf/main/cmd_ethernet.c @@ -10,7 +10,7 @@ #include #include "freertos/FreeRTOS.h" #include "freertos/event_groups.h" -#include "tcpip_adapter.h" +#include "esp_netif.h" #include "esp_log.h" #include "esp_console.h" #include "esp_event.h" @@ -19,11 +19,12 @@ #include "iperf.h" #include "sdkconfig.h" -static tcpip_adapter_ip_info_t ip; +static esp_netif_ip_info_t ip; static bool started = false; static EventGroupHandle_t eth_event_group; static const int GOTIP_BIT = BIT0; static esp_eth_handle_t eth_handle = NULL; +static esp_netif_t* eth_netif = NULL; /* "ethernet" command */ static struct { @@ -43,7 +44,7 @@ static int eth_cmd_control(int argc, char **argv) uint8_t mac_addr[6]; esp_eth_ioctl(eth_handle, ETH_CMD_G_MAC_ADDR, mac_addr); printf("HW ADDR: " MACSTR "\r\n", MAC2STR(mac_addr)); - tcpip_adapter_get_ip_info(ESP_IF_ETH, &ip); + esp_netif_get_ip_info(eth_netif, &ip); printf("ETHIP: " IPSTR "\r\n", IP2STR(&ip.ip)); printf("ETHMASK: " IPSTR "\r\n", IP2STR(&ip.netmask)); printf("ETHGW: " IPSTR "\r\n", IP2STR(&ip.gw)); @@ -176,9 +177,11 @@ static void event_handler(void *arg, esp_event_base_t event_base, void register_ethernet(void) { eth_event_group = xEventGroupCreate(); - tcpip_adapter_init(); + esp_netif_init(); ESP_ERROR_CHECK(esp_event_loop_create_default()); - ESP_ERROR_CHECK(tcpip_adapter_set_default_eth_handlers()); + esp_netif_config_t cfg = ESP_NETIF_DEFAULT_ETH(); + eth_netif = esp_netif_new(&cfg); + ESP_ERROR_CHECK(esp_eth_set_default_handlers(eth_netif)); ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL)); ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &event_handler, NULL)); @@ -222,6 +225,7 @@ void register_ethernet(void) #endif esp_eth_config_t config = ETH_DEFAULT_CONFIG(mac, phy); ESP_ERROR_CHECK(esp_eth_driver_install(&config, ð_handle)); + ESP_ERROR_CHECK(esp_netif_attach(eth_netif, eth_handle)); eth_control_args.control = arg_str1(NULL, NULL, "", "Get info of Ethernet"); eth_control_args.end = arg_end(1); From 0eec84bc4fee2fd20d1fa847e4f86702bb09c02a Mon Sep 17 00:00:00 2001 From: David Cermak Date: Sun, 1 Sep 2019 17:23:18 +0200 Subject: [PATCH 09/31] mesh_examples: use esp_netif instead of tcpip_adapter --- .../internal_communication/main/mesh_main.c | 46 +++++++++++++++---- .../mesh/manual_networking/main/mesh_main.c | 46 +++++++++++++++---- 2 files changed, 76 insertions(+), 16 deletions(-) diff --git a/examples/mesh/internal_communication/main/mesh_main.c b/examples/mesh/internal_communication/main/mesh_main.c index 0bbff5165e..7a0b6d0a9f 100644 --- a/examples/mesh/internal_communication/main/mesh_main.c +++ b/examples/mesh/internal_communication/main/mesh_main.c @@ -37,6 +37,7 @@ static bool is_running = true; static bool is_mesh_connected = false; static mesh_addr_t mesh_parent_addr; static int mesh_layer = -1; +static esp_netif_t *netif_sta = NULL; mesh_light_ctl_t light_on = { .cmd = MESH_CONTROL_CMD, @@ -244,7 +245,7 @@ void mesh_event_handler(void *arg, esp_event_base_t event_base, mesh_connected_indicator(mesh_layer); is_mesh_connected = true; if (esp_mesh_is_root()) { - tcpip_adapter_dhcpc_start(TCPIP_ADAPTER_IF_STA); + esp_netif_dhcpc_start(netif_sta); } esp_mesh_comm_p2p_start(); } @@ -371,20 +372,49 @@ void ip_event_handler(void *arg, esp_event_base_t event_base, } +/** + * @brief Creates mesh network interfaces based on default STA and AP, + * but without DHCP, this is to be enabled separately only on root node + */ +static esp_err_t create_wifi_netifs(void) +{ + esp_netif_inherent_config_t netif_cfg; + memcpy(&netif_cfg, ESP_NETIF_BASE_DEFAULT_WIFI_AP, sizeof(netif_cfg)); + netif_cfg.flags &= ~ESP_NETIF_DHCPS; + esp_netif_config_t cfg_ap = { + .base = &netif_cfg, + .stack = ESP_NETIF_NETSTACK_DEFAULT_WIFI_AP, + .driver = ESP_NETIF_DRIVER_DEFAULT_WIFI_AP, + }; + esp_netif_t *netif_ap = esp_netif_new(&cfg_ap); + assert(netif_ap); + ESP_ERROR_CHECK(esp_wifi_set_default_wifi_ap_handlers(netif_ap)); + + + memcpy(&netif_cfg, ESP_NETIF_BASE_DEFAULT_WIFI_STA, sizeof(netif_cfg)); + netif_cfg.flags &= ~ESP_NETIF_DHCPC; + esp_netif_config_t cfg_sta = { + .base = &netif_cfg, + .stack = ESP_NETIF_NETSTACK_DEFAULT_WIFI_STA, + .driver = ESP_NETIF_DRIVER_DEFAULT_WIFI_STA, + }; + + netif_sta = esp_netif_new(&cfg_sta); + assert(netif_sta); + ESP_ERROR_CHECK(esp_wifi_set_default_wifi_sta_handlers(netif_sta)); + return ESP_OK; +} + void app_main(void) { ESP_ERROR_CHECK(mesh_light_init()); ESP_ERROR_CHECK(nvs_flash_init()); /* tcpip initialization */ - tcpip_adapter_init(); - /* for mesh - * stop DHCP server on softAP interface by default - * stop DHCP client on station interface by default - * */ - ESP_ERROR_CHECK(tcpip_adapter_dhcps_stop(TCPIP_ADAPTER_IF_AP)); - ESP_ERROR_CHECK(tcpip_adapter_dhcpc_stop(TCPIP_ADAPTER_IF_STA)); + esp_netif_init(); /* event initialization */ ESP_ERROR_CHECK(esp_event_loop_create_default()); + /* crete network interfaces for mesh */ + ESP_ERROR_CHECK(create_wifi_netifs()); /* wifi initialization */ wifi_init_config_t config = WIFI_INIT_CONFIG_DEFAULT(); ESP_ERROR_CHECK(esp_wifi_init(&config)); diff --git a/examples/mesh/manual_networking/main/mesh_main.c b/examples/mesh/manual_networking/main/mesh_main.c index 4135a72353..3c2bd255b4 100644 --- a/examples/mesh/manual_networking/main/mesh_main.c +++ b/examples/mesh/manual_networking/main/mesh_main.c @@ -36,6 +36,7 @@ static const char *MESH_TAG = "mesh_main"; static const uint8_t MESH_ID[6] = { 0x77, 0x77, 0x77, 0x77, 0x77, 0x77}; static mesh_addr_t mesh_parent_addr; static int mesh_layer = -1; +static esp_netif_t *netif_sta = NULL; /******************************************************* * Function Declarations @@ -228,7 +229,7 @@ void mesh_event_handler(void *arg, esp_event_base_t event_base, last_layer = mesh_layer; mesh_connected_indicator(mesh_layer); if (esp_mesh_is_root()) { - tcpip_adapter_dhcpc_start(TCPIP_ADAPTER_IF_STA); + esp_netif_dhcpc_start(netif_sta); } } break; @@ -295,20 +296,49 @@ void ip_event_handler(void *arg, esp_event_base_t event_base, ESP_LOGI(MESH_TAG, "IP:" IPSTR, IP2STR(&event->ip_info.ip)); } +/** + * @brief Creates mesh network interfaces based on default STA and AP, + * but without DHCP, this is to be enabled separately only on root node + */ +static esp_err_t create_wifi_netifs(void) +{ + esp_netif_inherent_config_t netif_cfg; + memcpy(&netif_cfg, ESP_NETIF_BASE_DEFAULT_WIFI_AP, sizeof(netif_cfg)); + netif_cfg.flags &= ~ESP_NETIF_DHCPS; + esp_netif_config_t cfg_ap = { + .base = &netif_cfg, + .stack = ESP_NETIF_NETSTACK_DEFAULT_WIFI_AP, + .driver = ESP_NETIF_DRIVER_DEFAULT_WIFI_AP, + }; + esp_netif_t *netif_ap = esp_netif_new(&cfg_ap); + assert(netif_ap); + ESP_ERROR_CHECK(esp_wifi_set_default_wifi_ap_handlers(netif_ap)); + + + memcpy(&netif_cfg, ESP_NETIF_BASE_DEFAULT_WIFI_STA, sizeof(netif_cfg)); + netif_cfg.flags &= ~ESP_NETIF_DHCPC; + esp_netif_config_t cfg_sta = { + .base = &netif_cfg, + .stack = ESP_NETIF_NETSTACK_DEFAULT_WIFI_STA, + .driver = ESP_NETIF_DRIVER_DEFAULT_WIFI_STA, + }; + + netif_sta = esp_netif_new(&cfg_sta); + assert(netif_sta); + ESP_ERROR_CHECK(esp_wifi_set_default_wifi_sta_handlers(netif_sta)); + return ESP_OK; +} + void app_main(void) { ESP_ERROR_CHECK(mesh_light_init()); ESP_ERROR_CHECK(nvs_flash_init()); /* tcpip initialization */ - tcpip_adapter_init(); - /* for mesh - * stop DHCP server on softAP interface by default - * stop DHCP client on station interface by default - * */ - ESP_ERROR_CHECK(tcpip_adapter_dhcps_stop(TCPIP_ADAPTER_IF_AP)); - ESP_ERROR_CHECK(tcpip_adapter_dhcpc_stop(TCPIP_ADAPTER_IF_STA)); + esp_netif_init(); /* event initialization */ ESP_ERROR_CHECK(esp_event_loop_create_default()); + /* crete network interfaces for mesh */ + ESP_ERROR_CHECK(create_wifi_netifs()); /* wifi initialization */ wifi_init_config_t config = WIFI_INIT_CONFIG_DEFAULT(); ESP_ERROR_CHECK(esp_wifi_init(&config)); From b834c99148d901050e73728aa7bf0546068e1873 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Sun, 1 Sep 2019 18:25:23 +0200 Subject: [PATCH 10/31] examples: modify other examples and tests to use esp_netif instead of tcpip_adapter --- components/driver/test/esp32/test_adc2.c | 6 ++++-- components/esp_eth/test/test_emac.c | 9 ++++++--- components/esp_http_client/lib/http_auth.c | 2 +- components/esp_wifi/src/smartconfig_ack.c | 6 +++--- components/esp_wifi/test/esp32/test_wifi.c | 6 +++--- .../ble/blufi/main/blufi_example_main.c | 3 ++- .../components/iperf/cmd_wifi.c | 16 ++++++++++------ .../protocols/esp_local_ctrl/main/app_main.c | 3 ++- .../protocols/icmp_echo/main/echo_example_main.c | 2 +- examples/protocols/mqtt/ssl_psk/main/app_main.c | 4 ++-- .../components/modem/src/esp_modem.c | 2 +- .../pppos_client/main/pppos_client_main.c | 4 ++-- examples/system/console/main/cmd_wifi.c | 6 ++++-- examples/system/select/main/select_example.c | 4 ++-- examples/wifi/espnow/main/espnow_example_main.c | 4 ++-- examples/wifi/fast_scan/main/fast_scan.c | 7 ++++++- examples/wifi/iperf/main/cmd_wifi.c | 16 ++++++++++------ examples/wifi/power_save/main/power_save.c | 3 ++- examples/wifi/scan/main/scan.c | 3 ++- .../main/simple_sniffer_example_main.c | 4 ++-- .../wifi/smart_config/main/smartconfig_main.c | 5 +++-- .../wpa2_enterprise/main/wpa2_enterprise_main.c | 15 ++++++++++----- examples/wifi/wps/main/wps.c | 3 ++- .../components/test_utils/test_utils.c | 6 +++--- 24 files changed, 85 insertions(+), 54 deletions(-) diff --git a/components/driver/test/esp32/test_adc2.c b/components/driver/test/esp32/test_adc2.c index fa45232280..176cc17bfb 100644 --- a/components/driver/test/esp32/test_adc2.c +++ b/components/driver/test/esp32/test_adc2.c @@ -94,8 +94,10 @@ TEST_CASE("adc2 work with wifi","[adc]") r = nvs_flash_init(); } TEST_ESP_OK( r); - tcpip_adapter_init(); + esp_netif_init(); event_init(); + esp_netif_create_default_wifi_sta(); + wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); TEST_ESP_OK(esp_wifi_init(&cfg)); wifi_config_t wifi_config = { @@ -144,5 +146,5 @@ TEST_CASE("adc2 work with wifi","[adc]") printf("test passed...\n"); - TEST_IGNORE_MESSAGE("this test case is ignored due to the critical memory leak of tcpip_adapter and event_loop."); + TEST_IGNORE_MESSAGE("this test case is ignored due to the critical memory leak of esp_netif and event_loop."); } diff --git a/components/esp_eth/test/test_emac.c b/components/esp_eth/test/test_emac.c index 368494cbf7..47123db8ab 100644 --- a/components/esp_eth/test/test_emac.c +++ b/components/esp_eth/test/test_emac.c @@ -62,7 +62,7 @@ static void got_ip_event_handler(void *arg, esp_event_base_t event_base, { EventGroupHandle_t eth_event_group = (EventGroupHandle_t)arg; ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data; - const tcpip_adapter_ip_info_t *ip_info = (tcpip_adapter_ip_info_t *)&event->ip_info; + const esp_netif_ip_info_t *ip_info = &event->ip_info; ESP_LOGI(TAG, "Ethernet Got IP Address"); ESP_LOGI(TAG, "~~~~~~~~~~~"); @@ -309,9 +309,11 @@ TEST_CASE("dm9051 io test", "[ethernet][ignore]") }; TEST_ESP_OK(spi_bus_add_device(HSPI_HOST, &devcfg, &spi_handle)); gpio_install_isr_service(0); - tcpip_adapter_init(); + esp_netif_init(); TEST_ESP_OK(esp_event_loop_create_default()); - TEST_ESP_OK(tcpip_adapter_set_default_eth_handlers()); + esp_netif_config_t cfg = ESP_NETIF_DEFAULT_ETH(); + esp_netif_t* eth_netif = esp_netif_new(&cfg); + TEST_ESP_OK(esp_eth_set_default_handlers(eth_netif)); TEST_ESP_OK(esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, ð_event_handler, NULL)); TEST_ESP_OK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &got_ip_event_handler, NULL)); eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG(); @@ -322,6 +324,7 @@ TEST_CASE("dm9051 io test", "[ethernet][ignore]") esp_eth_config_t config = ETH_DEFAULT_CONFIG(mac, phy); esp_eth_handle_t eth_handle = NULL; TEST_ESP_OK(esp_eth_driver_install(&config, ð_handle)); + TEST_ESP_OK(esp_netif_attach(eth_netif, eth_handle)); vTaskDelay(pdMS_TO_TICKS(portMAX_DELAY)); TEST_ESP_OK(esp_eth_driver_uninstall(eth_handle)); TEST_ESP_OK(phy->del(phy)); diff --git a/components/esp_http_client/lib/http_auth.c b/components/esp_http_client/lib/http_auth.c index 7463a92f59..1fcc2f1152 100644 --- a/components/esp_http_client/lib/http_auth.c +++ b/components/esp_http_client/lib/http_auth.c @@ -17,7 +17,7 @@ #include #include -#include "tcpip_adapter.h" +#include "esp_netif.h" #include "lwip/sockets.h" #include "esp32/rom/md5_hash.h" #include "mbedtls/base64.h" diff --git a/components/esp_wifi/src/smartconfig_ack.c b/components/esp_wifi/src/smartconfig_ack.c index 3cda1a21c6..db9276d8d4 100644 --- a/components/esp_wifi/src/smartconfig_ack.c +++ b/components/esp_wifi/src/smartconfig_ack.c @@ -21,7 +21,7 @@ #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "lwip/sockets.h" -#include "tcpip_adapter.h" +#include "esp_netif.h" #include "esp_log.h" #include "esp_wifi.h" #include "esp_event.h" @@ -69,7 +69,7 @@ static int sc_ack_send_get_errno(int fd) static void sc_ack_send_task(void *pvParameters) { sc_ack_t *ack = (sc_ack_t *)pvParameters; - tcpip_adapter_ip_info_t local_ip; + esp_netif_ip_info_t local_ip; uint8_t remote_ip[4]; memcpy(remote_ip, ack->ctx.ip, sizeof(remote_ip)); int remote_port = (ack->type == SC_TYPE_ESPTOUCH) ? SC_ACK_TOUCH_SERVER_PORT : SC_ACK_AIRKISS_SERVER_PORT; @@ -94,7 +94,7 @@ static void sc_ack_send_task(void *pvParameters) while (s_sc_ack_send) { /* Get local IP address of station */ - ret = tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_STA, &local_ip); + ret = esp_netif_get_ip_info(esp_netif_get_handle_from_ifkey("WIFI_STA_DEF"), &local_ip); if ((ESP_OK == ret) && (local_ip.ip.addr != INADDR_ANY)) { /* If ESP touch, smartconfig ACK contains local IP address. */ if (ack->type == SC_TYPE_ESPTOUCH) { diff --git a/components/esp_wifi/test/esp32/test_wifi.c b/components/esp_wifi/test/esp32/test_wifi.c index 0d1385f6bc..1a467baaee 100644 --- a/components/esp_wifi/test/esp32/test_wifi.c +++ b/components/esp_wifi/test/esp32/test_wifi.c @@ -144,8 +144,8 @@ TEST_CASE("wifi stop and deinit","[wifi]") } TEST_ESP_OK(r); //init tcpip - ESP_LOGI(TAG, EMPH_STR("tcpip_adapter_init")); - tcpip_adapter_init(); + ESP_LOGI(TAG, EMPH_STR("esp_netif_init")); + esp_netif_init(); //init event loop ESP_LOGI(TAG, EMPH_STR("event_init")); @@ -163,7 +163,7 @@ TEST_CASE("wifi stop and deinit","[wifi]") nvs_flash_deinit(); ESP_LOGI(TAG, "test passed..."); - TEST_IGNORE_MESSAGE("this test case is ignored due to the critical memory leak of tcpip_adapter and event_loop."); + TEST_IGNORE_MESSAGE("this test case is ignored due to the critical memory leak of esp_netif and event_loop."); } static void start_wifi_as_softap(void) diff --git a/examples/bluetooth/bluedroid/ble/blufi/main/blufi_example_main.c b/examples/bluetooth/bluedroid/ble/blufi/main/blufi_example_main.c index cf3e153303..3cb5e6a51b 100644 --- a/examples/bluetooth/bluedroid/ble/blufi/main/blufi_example_main.c +++ b/examples/bluetooth/bluedroid/ble/blufi/main/blufi_example_main.c @@ -197,9 +197,10 @@ static void wifi_event_handler(void* arg, esp_event_base_t event_base, static void initialise_wifi(void) { - tcpip_adapter_init(); + esp_netif_init(); wifi_event_group = xEventGroupCreate(); ESP_ERROR_CHECK(esp_event_loop_create_default()); + assert(esp_netif_create_default_wifi_sta()); ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler, NULL)); ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &ip_event_handler, NULL)); diff --git a/examples/bluetooth/esp_ble_mesh/ble_mesh_wifi_coexist/components/iperf/cmd_wifi.c b/examples/bluetooth/esp_ble_mesh/ble_mesh_wifi_coexist/components/iperf/cmd_wifi.c index 8fd95ce474..a4e060ec8a 100644 --- a/examples/bluetooth/esp_ble_mesh/ble_mesh_wifi_coexist/components/iperf/cmd_wifi.c +++ b/examples/bluetooth/esp_ble_mesh/ble_mesh_wifi_coexist/components/iperf/cmd_wifi.c @@ -16,7 +16,7 @@ #include "freertos/FreeRTOS.h" #include "freertos/event_groups.h" #include "esp_wifi.h" -#include "tcpip_adapter.h" +#include "esp_netif.h" #include "iperf.h" typedef struct { @@ -51,6 +51,8 @@ static const char *TAG = "iperf"; static EventGroupHandle_t wifi_event_group; const int CONNECTED_BIT = BIT0; const int DISCONNECTED_BIT = BIT1; +static esp_netif_t * sta_netif = NULL; +static esp_netif_t * ap_netif = NULL; static void scan_done_handler(void) { @@ -124,9 +126,11 @@ void initialise_wifi(void) return; } - tcpip_adapter_init(); + esp_netif_init(); wifi_event_group = xEventGroupCreate(); ESP_ERROR_CHECK(esp_event_loop_create_default()); + assert(sta_netif = esp_netif_create_default_wifi_sta()); + assert(ap_netif = esp_netif_create_default_wifi_ap()); ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler, NULL)); ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &ip_event_handler, NULL)); @@ -286,22 +290,22 @@ static int wifi_cmd_query(int argc, char **argv) static uint32_t wifi_get_local_ip(void) { int bits = xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT, 0, 1, 0); - tcpip_adapter_if_t ifx = TCPIP_ADAPTER_IF_AP; - tcpip_adapter_ip_info_t ip_info; + esp_netif_t * ifx = ap_netif; + esp_netif_ip_info_t ip_info; wifi_mode_t mode; esp_wifi_get_mode(&mode); if (WIFI_MODE_STA == mode) { bits = xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT, 0, 1, 0); if (bits & CONNECTED_BIT) { - ifx = TCPIP_ADAPTER_IF_STA; + ifx = sta_netif; } else { ESP_LOGE(TAG, "sta has no IP"); return 0; } } - tcpip_adapter_get_ip_info(ifx, &ip_info); + esp_netif_get_ip_info(ifx, &ip_info); return ip_info.ip.addr; } diff --git a/examples/protocols/esp_local_ctrl/main/app_main.c b/examples/protocols/esp_local_ctrl/main/app_main.c index 65651cc667..43ba8bf241 100644 --- a/examples/protocols/esp_local_ctrl/main/app_main.c +++ b/examples/protocols/esp_local_ctrl/main/app_main.c @@ -65,9 +65,10 @@ void wifi_init_sta(void) { s_wifi_event_group = xEventGroupCreate(); - tcpip_adapter_init(); + esp_netif_init(); ESP_ERROR_CHECK(esp_event_loop_create_default()); + assert(esp_netif_create_default_wifi_sta()); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); ESP_ERROR_CHECK(esp_wifi_init(&cfg)); diff --git a/examples/protocols/icmp_echo/main/echo_example_main.c b/examples/protocols/icmp_echo/main/echo_example_main.c index 3f222bb157..53acb17422 100644 --- a/examples/protocols/icmp_echo/main/echo_example_main.c +++ b/examples/protocols/icmp_echo/main/echo_example_main.c @@ -80,7 +80,7 @@ void app_main(void) initialize_console(); ESP_ERROR_CHECK(nvs_flash_init()); - tcpip_adapter_init(); + esp_netif_init(); ESP_ERROR_CHECK(esp_event_loop_create_default()); /* wait for active network connection */ ESP_ERROR_CHECK(example_connect()); diff --git a/examples/protocols/mqtt/ssl_psk/main/app_main.c b/examples/protocols/mqtt/ssl_psk/main/app_main.c index 7a08d17725..48e73f8c09 100644 --- a/examples/protocols/mqtt/ssl_psk/main/app_main.c +++ b/examples/protocols/mqtt/ssl_psk/main/app_main.c @@ -15,7 +15,7 @@ #include "esp_system.h" #include "nvs_flash.h" #include "esp_event.h" -#include "tcpip_adapter.h" +#include "esp_netif.h" #include "protocol_examples_common.h" #include "freertos/FreeRTOS.h" @@ -128,7 +128,7 @@ void app_main(void) esp_log_level_set("OUTBOX", ESP_LOG_VERBOSE); ESP_ERROR_CHECK(nvs_flash_init()); - tcpip_adapter_init(); + esp_netif_init(); ESP_ERROR_CHECK(esp_event_loop_create_default()); /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig. diff --git a/examples/protocols/pppos_client/components/modem/src/esp_modem.c b/examples/protocols/pppos_client/components/modem/src/esp_modem.c index d59da0b5f2..99e10db2a5 100644 --- a/examples/protocols/pppos_client/components/modem/src/esp_modem.c +++ b/examples/protocols/pppos_client/components/modem/src/esp_modem.c @@ -20,7 +20,7 @@ #include "netif/ppp/pppapi.h" #include "netif/ppp/pppos.h" #include "lwip/dns.h" -#include "tcpip_adapter.h" +#include "esp_netif.h" #include "esp_modem.h" #include "esp_log.h" #include "sdkconfig.h" diff --git a/examples/protocols/pppos_client/main/pppos_client_main.c b/examples/protocols/pppos_client/main/pppos_client_main.c index c3a18a6cb6..7df7c49388 100644 --- a/examples/protocols/pppos_client/main/pppos_client_main.c +++ b/examples/protocols/pppos_client/main/pppos_client_main.c @@ -9,7 +9,7 @@ #include #include "freertos/FreeRTOS.h" #include "freertos/event_groups.h" -#include "tcpip_adapter.h" +#include "esp_netif.h" #include "mqtt_client.h" #include "esp_modem.h" #include "esp_log.h" @@ -181,7 +181,7 @@ static esp_err_t mqtt_event_handler(esp_mqtt_event_handle_t event) void app_main(void) { - tcpip_adapter_init(); + esp_netif_init(); event_group = xEventGroupCreate(); /* create dte object */ esp_modem_dte_config_t config = ESP_MODEM_DTE_DEFAULT_CONFIG(); diff --git a/examples/system/console/main/cmd_wifi.c b/examples/system/console/main/cmd_wifi.c index b9c24f0cd1..e26a9dcea9 100644 --- a/examples/system/console/main/cmd_wifi.c +++ b/examples/system/console/main/cmd_wifi.c @@ -16,7 +16,7 @@ #include "freertos/FreeRTOS.h" #include "freertos/event_groups.h" #include "esp_wifi.h" -#include "tcpip_adapter.h" +#include "esp_netif.h" #include "esp_event.h" #include "cmd_wifi.h" @@ -44,9 +44,11 @@ static void initialise_wifi(void) if (initialized) { return; } - tcpip_adapter_init(); + esp_netif_init(); wifi_event_group = xEventGroupCreate(); ESP_ERROR_CHECK(esp_event_loop_create_default()); + assert(esp_netif_create_default_wifi_ap()); + assert(esp_netif_create_default_wifi_sta()); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); ESP_ERROR_CHECK( esp_wifi_init(&cfg) ); ESP_ERROR_CHECK( esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &event_handler, NULL) ); diff --git a/examples/system/select/main/select_example.c b/examples/system/select/main/select_example.c index 5da28c7dde..78d1516274 100644 --- a/examples/system/select/main/select_example.c +++ b/examples/system/select/main/select_example.c @@ -18,7 +18,7 @@ #include "esp_vfs.h" #include "esp_vfs_dev.h" #include "driver/uart.h" -#include "tcpip_adapter.h" +#include "esp_netif.h" #include "lwip/sockets.h" #include "lwip/netdb.h" @@ -43,7 +43,7 @@ static void socket_init(void) int err; struct sockaddr_in saddr = { 0 }; - tcpip_adapter_init(); + esp_netif_init(); err = getaddrinfo("localhost", "80", &hints, &res); diff --git a/examples/wifi/espnow/main/espnow_example_main.c b/examples/wifi/espnow/main/espnow_example_main.c index ad540eb335..981a8477e0 100644 --- a/examples/wifi/espnow/main/espnow_example_main.c +++ b/examples/wifi/espnow/main/espnow_example_main.c @@ -21,7 +21,7 @@ #include "freertos/timers.h" #include "nvs_flash.h" #include "esp_event.h" -#include "tcpip_adapter.h" +#include "esp_netif.h" #include "esp_wifi.h" #include "esp_log.h" #include "esp_system.h" @@ -41,7 +41,7 @@ static void example_espnow_deinit(example_espnow_send_param_t *send_param); /* WiFi should start before using ESPNOW */ static void example_wifi_init(void) { - tcpip_adapter_init(); + esp_netif_init(); ESP_ERROR_CHECK(esp_event_loop_create_default()); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); ESP_ERROR_CHECK( esp_wifi_init(&cfg) ); diff --git a/examples/wifi/fast_scan/main/fast_scan.c b/examples/wifi/fast_scan/main/fast_scan.c index 43df223e1c..185cacc0cb 100644 --- a/examples/wifi/fast_scan/main/fast_scan.c +++ b/examples/wifi/fast_scan/main/fast_scan.c @@ -85,7 +85,7 @@ static void event_handler(void* arg, esp_event_base_t event_base, /* Initialize Wi-Fi as sta and set scan method */ static void fast_scan(void) { - tcpip_adapter_init(); + esp_netif_init(); ESP_ERROR_CHECK(esp_event_loop_create_default()); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); @@ -94,6 +94,11 @@ static void fast_scan(void) ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL)); ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL)); + // Initialize default station as network interface instance (esp-netif) + esp_netif_t *sta_netif = esp_netif_create_default_wifi_sta(); + assert(sta_netif); + + // Initialize and start WiFi wifi_config_t wifi_config = { .sta = { .ssid = DEFAULT_SSID, diff --git a/examples/wifi/iperf/main/cmd_wifi.c b/examples/wifi/iperf/main/cmd_wifi.c index 8bd3819a18..6710dcdee1 100644 --- a/examples/wifi/iperf/main/cmd_wifi.c +++ b/examples/wifi/iperf/main/cmd_wifi.c @@ -16,7 +16,7 @@ #include "freertos/FreeRTOS.h" #include "freertos/event_groups.h" #include "esp_wifi.h" -#include "tcpip_adapter.h" +#include "esp_netif.h" #include "esp_event.h" #include "iperf.h" @@ -48,6 +48,8 @@ static wifi_scan_arg_t scan_args; static wifi_args_t ap_args; static bool reconnect = true; static const char *TAG="cmd_wifi"; +static esp_netif_t *netif_ap = NULL; +static esp_netif_t *netif_sta = NULL; static EventGroupHandle_t wifi_event_group; const int CONNECTED_BIT = BIT0; @@ -106,9 +108,11 @@ void initialise_wifi(void) return; } - tcpip_adapter_init(); + esp_netif_init(); wifi_event_group = xEventGroupCreate(); ESP_ERROR_CHECK( esp_event_loop_create_default() ); + assert(netif_ap = esp_netif_create_default_wifi_ap()); + assert(netif_sta = esp_netif_create_default_wifi_sta()); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); ESP_ERROR_CHECK( esp_wifi_init(&cfg) ); ESP_ERROR_CHECK( esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_SCAN_DONE, &scan_done_handler, NULL) ); @@ -266,22 +270,22 @@ static int wifi_cmd_query(int argc, char** argv) static uint32_t wifi_get_local_ip(void) { int bits = xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT, 0, 1, 0); - tcpip_adapter_if_t ifx = TCPIP_ADAPTER_IF_AP; - tcpip_adapter_ip_info_t ip_info; + esp_netif_t * netif = netif_ap; + esp_netif_ip_info_t ip_info; wifi_mode_t mode; esp_wifi_get_mode(&mode); if (WIFI_MODE_STA == mode) { bits = xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT, 0, 1, 0); if (bits & CONNECTED_BIT) { - ifx = TCPIP_ADAPTER_IF_STA; + netif = netif_sta; } else { ESP_LOGE(TAG, "sta has no IP"); return 0; } } - tcpip_adapter_get_ip_info(ifx, &ip_info); + esp_netif_get_ip_info(netif, &ip_info); return ip_info.ip.addr; } diff --git a/examples/wifi/power_save/main/power_save.c b/examples/wifi/power_save/main/power_save.c index 21b7e7b51b..8e2a5ef6d9 100644 --- a/examples/wifi/power_save/main/power_save.c +++ b/examples/wifi/power_save/main/power_save.c @@ -55,8 +55,9 @@ static void event_handler(void* arg, esp_event_base_t event_base, /*init wifi as sta and set power save mode*/ static void wifi_power_save(void) { - tcpip_adapter_init(); + esp_netif_init(); ESP_ERROR_CHECK(esp_event_loop_create_default()); + assert(esp_netif_create_default_wifi_sta()); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); ESP_ERROR_CHECK(esp_wifi_init(&cfg)); diff --git a/examples/wifi/scan/main/scan.c b/examples/wifi/scan/main/scan.c index a3ab6253e1..d3759d3c98 100644 --- a/examples/wifi/scan/main/scan.c +++ b/examples/wifi/scan/main/scan.c @@ -114,8 +114,9 @@ static void print_cipher_type(int pairwise_cipher, int group_cipher) /* Initialize Wi-Fi as sta and set scan method */ static void wifi_scan(void) { - tcpip_adapter_init(); + esp_netif_init(); ESP_ERROR_CHECK(esp_event_loop_create_default()); + assert(esp_netif_create_default_wifi_sta()); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); ESP_ERROR_CHECK(esp_wifi_init(&cfg)); diff --git a/examples/wifi/simple_sniffer/main/simple_sniffer_example_main.c b/examples/wifi/simple_sniffer/main/simple_sniffer_example_main.c index e1a78a42ce..3a60fc0ba4 100644 --- a/examples/wifi/simple_sniffer/main/simple_sniffer_example_main.c +++ b/examples/wifi/simple_sniffer/main/simple_sniffer_example_main.c @@ -11,7 +11,7 @@ #include #include "linenoise/linenoise.h" #include "argtable3/argtable3.h" -#include "tcpip_adapter.h" +#include "esp_netif.h" #include "esp_console.h" #include "esp_event.h" #include "esp_vfs_dev.h" @@ -64,7 +64,7 @@ static void initialize_nvs(void) /* Initialize wifi with tcp/ip adapter */ static void initialize_wifi(void) { - tcpip_adapter_init(); + esp_netif_init(); ESP_ERROR_CHECK(esp_event_loop_create_default()); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); ESP_ERROR_CHECK(esp_wifi_init(&cfg)); diff --git a/examples/wifi/smart_config/main/smartconfig_main.c b/examples/wifi/smart_config/main/smartconfig_main.c index 65e7c50331..62b3ab4a91 100644 --- a/examples/wifi/smart_config/main/smartconfig_main.c +++ b/examples/wifi/smart_config/main/smartconfig_main.c @@ -18,7 +18,7 @@ #include "esp_log.h" #include "esp_system.h" #include "nvs_flash.h" -#include "tcpip_adapter.h" +#include "esp_netif.h" #include "esp_smartconfig.h" /* FreeRTOS event group to signal when we are connected & ready to make a request */ @@ -78,9 +78,10 @@ static void event_handler(void* arg, esp_event_base_t event_base, static void initialise_wifi(void) { - tcpip_adapter_init(); + esp_netif_init(); s_wifi_event_group = xEventGroupCreate(); ESP_ERROR_CHECK(esp_event_loop_create_default()); + assert(esp_netif_create_default_wifi_sta()); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); ESP_ERROR_CHECK( esp_wifi_init(&cfg) ); diff --git a/examples/wifi/wpa2_enterprise/main/wpa2_enterprise_main.c b/examples/wifi/wpa2_enterprise/main/wpa2_enterprise_main.c index 00b6ff74f3..a9386a63d0 100644 --- a/examples/wifi/wpa2_enterprise/main/wpa2_enterprise_main.c +++ b/examples/wifi/wpa2_enterprise/main/wpa2_enterprise_main.c @@ -27,7 +27,7 @@ #include "esp_log.h" #include "esp_system.h" #include "nvs_flash.h" -#include "tcpip_adapter.h" +#include "esp_netif.h" /* The examples use simple WiFi configuration that you can set via project configuration menu. @@ -48,6 +48,9 @@ /* FreeRTOS event group to signal when we are connected & ready to make a request */ static EventGroupHandle_t wifi_event_group; +/* esp netif object representing the WIFI station */ +static esp_netif_t *sta_netif = NULL; + /* The event group allows multiple bits for each event, but we only care about one event - are we connected to the AP with an IP? */ @@ -101,9 +104,11 @@ static void initialise_wifi(void) unsigned int client_key_bytes = client_key_end - client_key_start; #endif /* CONFIG_EXAMPLE_EAP_METHOD_TLS */ - tcpip_adapter_init(); + esp_netif_init(); wifi_event_group = xEventGroupCreate(); ESP_ERROR_CHECK(esp_event_loop_create_default()); + assert(sta_netif = esp_netif_create_default_wifi_sta()); + wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); ESP_ERROR_CHECK( esp_wifi_init(&cfg) ); ESP_ERROR_CHECK( esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL) ); @@ -139,14 +144,14 @@ static void initialise_wifi(void) static void wpa2_enterprise_example_task(void *pvParameters) { - tcpip_adapter_ip_info_t ip; - memset(&ip, 0, sizeof(tcpip_adapter_ip_info_t)); + esp_netif_ip_info_t ip; + memset(&ip, 0, sizeof(esp_netif_ip_info_t)); vTaskDelay(2000 / portTICK_PERIOD_MS); while (1) { vTaskDelay(2000 / portTICK_PERIOD_MS); - if (tcpip_adapter_get_ip_info(ESP_IF_WIFI_STA, &ip) == 0) { + if (esp_netif_get_ip_info(sta_netif, &ip) == 0) { ESP_LOGI(TAG, "~~~~~~~~~~~"); ESP_LOGI(TAG, "IP:"IPSTR, IP2STR(&ip.ip)); ESP_LOGI(TAG, "MASK:"IPSTR, IP2STR(&ip.netmask)); diff --git a/examples/wifi/wps/main/wps.c b/examples/wifi/wps/main/wps.c index 640ad952c6..17095e9316 100644 --- a/examples/wifi/wps/main/wps.c +++ b/examples/wifi/wps/main/wps.c @@ -97,8 +97,9 @@ static void got_ip_event_handler(void* arg, esp_event_base_t event_base, /*init wifi as sta and start wps*/ static void start_wps(void) { - tcpip_adapter_init(); + esp_netif_init(); ESP_ERROR_CHECK(esp_event_loop_create_default()); + assert(esp_netif_create_default_wifi_sta()); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); ESP_ERROR_CHECK(esp_wifi_init(&cfg)); diff --git a/tools/unit-test-app/components/test_utils/test_utils.c b/tools/unit-test-app/components/test_utils/test_utils.c index ec45b85727..3624f1cfd4 100644 --- a/tools/unit-test-app/components/test_utils/test_utils.c +++ b/tools/unit-test-app/components/test_utils/test_utils.c @@ -17,7 +17,7 @@ #include "test_utils.h" #include "freertos/FreeRTOS.h" #include "freertos/task.h" -#include "tcpip_adapter.h" +#include "esp_netif.h" #include "lwip/sockets.h" const esp_partition_t *get_test_data_partition(void) @@ -32,7 +32,7 @@ const esp_partition_t *get_test_data_partition(void) void test_case_uses_tcpip(void) { // Can be called more than once, does nothing on subsequent calls - tcpip_adapter_init(); + esp_netif_init(); // Allocate all sockets then free them // (First time each socket is allocated some one-time allocations happen.) @@ -49,7 +49,7 @@ void test_case_uses_tcpip(void) // Allow LWIP tasks to finish initialising themselves vTaskDelay(25 / portTICK_RATE_MS); - printf("Note: tcpip_adapter_init() has been called. Until next reset, TCP/IP task will periodicially allocate memory and consume CPU time.\n"); + printf("Note: esp_netif_init() has been called. Until next reset, TCP/IP task will periodicially allocate memory and consume CPU time.\n"); // Reset the leak checker as LWIP allocates a lot of memory on first run unity_reset_leak_checks(); From ba13275c6b40f26d65a4bd2b164b751ea6780c23 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Mon, 2 Sep 2019 11:22:09 +0200 Subject: [PATCH 11/31] esp_netif: update default DHCP IP addresses to be in line with old interface, added loopback implementation, explicit esp-netif init, sanity checks for parameters added --- components/esp_common/src/esp_err_to_name.c | 4 +- components/esp_eth/include/esp_eth.h | 9 - components/esp_eth/src/esp_eth.c | 18 +- components/esp_netif/CMakeLists.txt | 1 + components/esp_netif/Kconfig | 14 +- components/esp_netif/README.md | 6 +- components/esp_netif/esp_netif_defaults.c | 5 +- components/esp_netif/esp_netif_objects.c | 6 - components/esp_netif/include/esp_netif.h | 35 +- .../esp_netif/include/esp_netif_defaults.h | 2 +- .../esp_netif/include/esp_netif_ip_addr.h | 10 + .../esp_netif/include/esp_netif_types.h | 1 + .../esp_netif/loopback/esp_netif_loopback.c | 466 ++++++++++++++++++ components/esp_netif/lwip/esp_netif_lwip.c | 443 +++++++++-------- .../esp_netif/lwip/esp_netif_lwip_internal.h | 22 - components/esp_netif/test/test_esp_netif.c | 13 +- components/esp_wifi/src/wifi_init.c | 4 +- .../tcpip_adapter/include/tcpip_adapter.h | 93 ++-- .../include/tcpip_adapter_types.h | 61 +++ .../tcpip_adapter/tcpip_adapter_compat.c | 24 +- .../main/udp_multicast_example_main.c | 2 +- 21 files changed, 887 insertions(+), 352 deletions(-) create mode 100644 components/esp_netif/loopback/esp_netif_loopback.c create mode 100644 components/tcpip_adapter/include/tcpip_adapter_types.h diff --git a/components/esp_common/src/esp_err_to_name.c b/components/esp_common/src/esp_err_to_name.c index e234a26b19..06fbd1358a 100644 --- a/components/esp_common/src/esp_err_to_name.c +++ b/components/esp_common/src/esp_err_to_name.c @@ -50,8 +50,8 @@ #if __has_include("nvs.h") #include "nvs.h" #endif -#if __has_include("tcpip_adapter.h") -#include "tcpip_adapter.h" +#if __has_include("esp_netif.h") +#include "esp_netif.h" #endif #if __has_include("ulp_common.h") #include "ulp_common.h" diff --git a/components/esp_eth/include/esp_eth.h b/components/esp_eth/include/esp_eth.h index 39393e8235..5d7d6d66dd 100644 --- a/components/esp_eth/include/esp_eth.h +++ b/components/esp_eth/include/esp_eth.h @@ -102,15 +102,6 @@ typedef struct { .on_lowlevel_deinit_done = NULL, \ } -/** -* @brief Default esp-netif driver related configuration -* -*/ -#define ESP_NETIF_DRIVER_DEFAULT_ETH _g_eth_driver_ifconfig - -struct esp_netif_driver_ifconfig; -extern const struct esp_netif_driver_ifconfig *_g_eth_driver_ifconfig; - /** * @brief Install Ethernet driver * diff --git a/components/esp_eth/src/esp_eth.c b/components/esp_eth/src/esp_eth.c index fcd5991f53..b1e36839aa 100644 --- a/components/esp_eth/src/esp_eth.c +++ b/components/esp_eth/src/esp_eth.c @@ -35,15 +35,6 @@ static const char *TAG = "esp_eth"; ESP_EVENT_DEFINE_BASE(ETH_EVENT); -// Default driver interface config, at the init moment doesn't include driver handle, as it is created -// in starup phase, netif is updated in event handler on eth_start event with valid ethernet driver handle -static const esp_netif_driver_ifconfig_t _s_eth_driver_ifconfig = { - .handle = NULL, - .transmit = esp_eth_transmit, -}; - -const esp_netif_driver_ifconfig_t *_g_eth_driver_ifconfig = &_s_eth_driver_ifconfig; - /** * @brief The Ethernet driver mainly consists of PHY, MAC and * the mediator who will handle the request/response from/to MAC, PHY and Users. @@ -171,17 +162,16 @@ static esp_err_t esp_eth_driver_start(esp_netif_t * esp_netif, void * args) esp_err_t ret = ESP_OK; esp_eth_driver_t *eth_driver = args; eth_driver->base.netif = esp_netif; - // Update esp-netif with already created ethernet handle + + // Set driver related config to esp-netif esp_netif_driver_ifconfig_t driver_ifconfig = { .handle = eth_driver, .transmit = esp_eth_transmit, + .driver_free_rx_buffer = NULL }; - esp_netif_config_t cfg = { - .driver = &driver_ifconfig, - }; - ESP_ERROR_CHECK(esp_netif_configure(esp_netif, &cfg)); + ESP_ERROR_CHECK(esp_netif_set_driver_config(esp_netif, &driver_ifconfig)); uint8_t eth_mac[6]; esp_eth_ioctl(eth_driver, ETH_CMD_G_MAC_ADDR, eth_mac); ESP_LOGI(TAG, "%x %x %x %x %x %x", eth_mac[0], eth_mac[1], eth_mac[2], eth_mac[3], eth_mac[4], eth_mac[5]); diff --git a/components/esp_netif/CMakeLists.txt b/components/esp_netif/CMakeLists.txt index bdcdd80bb2..a47bcd65db 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" + "loopback/esp_netif_loopback.c" "lwip/esp_netif_lwip_defaults.c" INCLUDE_DIRS include PRIV_INCLUDE_DIRS lwip private_include diff --git a/components/esp_netif/Kconfig b/components/esp_netif/Kconfig index fd693386f5..7744150429 100644 --- a/components/esp_netif/Kconfig +++ b/components/esp_netif/Kconfig @@ -15,20 +15,26 @@ menu "ESP NETIF Adapter" choice ESP_NETIF_USE_TCPIP_STACK_LIB prompt "TCP/IP Stack Library" - default TCPIP_LWIP + default ESP_NETIF_TCPIP_LWIP help Choose the TCP/IP Stack to work, for example, LwIP, uIP, etc. - config TCPIP_LWIP + config ESP_NETIF_TCPIP_LWIP bool "LwIP" help lwIP is a small independent implementation of the TCP/IP protocol suite. + + config ESP_NETIF_LOOPBACK + bool "Loopback" + help + Dummy implementation of esp-netif functionality which connects driver transmit + to receive function. This option is for testing purpose only endchoice - config ESP_NETIF_USE_TCPIP_ADAPTER_COMPATIBLE_LAYER + config ESP_NETIF_TCPIP_ADAPTER_COMPATIBLE_LAYER bool "Enable backward compatible tcpip_adapter interface" default y help Backward compatible interface to tcpip_adapter is enabled by default to support - legacy tcpip stack initialisation code. Disable this option to use only esp-netif + legacy TCP/IP stack initialisation code. Disable this option to use only esp-netif interface. endmenu diff --git a/components/esp_netif/README.md b/components/esp_netif/README.md index 4231a42721..b3837e7fdb 100644 --- a/components/esp_netif/README.md +++ b/components/esp_netif/README.md @@ -62,8 +62,8 @@ Overall application interaction with communication media and network stack * setters, getters * network stack abstraction: enabling user interaction with TCP/IP stack - netif up/down - - dhcp server, client - - dns api + - DHCP server, client + - DNS API * driver conversion utilities ### D) Network stack: no public interaction with user code (wrtt interfaces) @@ -73,7 +73,7 @@ Overall application interaction with communication media and network stack * `........` Initialization line from user code to esp-netif and comm driver -* `--<--->--` Data packets going from communication media to tcpip stack and back +* `--<--->--` Data packets going from communication media to TCP/IP stack and back * `********` Events agregated in ESP-NETIP propagates to driver, user code and network stack diff --git a/components/esp_netif/esp_netif_defaults.c b/components/esp_netif/esp_netif_defaults.c index abbb76da47..d5d9fda825 100644 --- a/components/esp_netif/esp_netif_defaults.c +++ b/components/esp_netif/esp_netif_defaults.c @@ -13,7 +13,6 @@ // limitations under the License. #include "esp_netif.h" -#include "esp_event.h" #include "esp_wifi_default.h" #include "esp_eth.h" @@ -44,8 +43,8 @@ const esp_netif_inherent_config_t _g_esp_netif_inherent_sta_config = { }; static const esp_netif_ip_info_t soft_ap_ip = { - .ip = { .addr = IP4TOADDR( 192, 168, 8, 1) }, - .gw = { .addr = IP4TOADDR( 192, 168, 8, 1) }, + .ip = { .addr = IP4TOADDR( 192, 168, 4, 1) }, + .gw = { .addr = IP4TOADDR( 192, 168, 4, 1) }, .netmask = { .addr = IP4TOADDR( 255, 255, 255, 0) }, }; diff --git a/components/esp_netif/esp_netif_objects.c b/components/esp_netif/esp_netif_objects.c index 1eba1930e2..f188d0615d 100644 --- a/components/esp_netif/esp_netif_objects.c +++ b/components/esp_netif/esp_netif_objects.c @@ -23,8 +23,6 @@ static const char *TAG = "esp_netif_objects"; -//#define ESP_NETIF_TYPE_DEFINE(id) const esp_netif_type_t id = #id - typedef struct slist_netifs_s slist_netifs_t; struct slist_netifs_s { esp_netif_t *netif; @@ -37,10 +35,6 @@ static size_t s_esp_netif_counter = 0; ESP_EVENT_DEFINE_BASE(IP_EVENT); -//ESP_NETIF_TYPE_DEFINE(ESP_NETIF_TYPE_STA); -//ESP_NETIF_TYPE_DEFINE(ESP_NETIF_TYPE_AP); -//ESP_NETIF_TYPE_DEFINE(ESP_NETIF_TYPE_ETH); - // // List manipulation functions diff --git a/components/esp_netif/include/esp_netif.h b/components/esp_netif/include/esp_netif.h index 6cd8b13dde..1819b15b49 100644 --- a/components/esp_netif/include/esp_netif.h +++ b/components/esp_netif/include/esp_netif.h @@ -67,20 +67,18 @@ esp_netif_t *esp_netif_new(const esp_netif_config_t *esp_netif_config); void esp_netif_destroy(esp_netif_t *esp_netif); /** - * @brief Configures the esp_netif object - * - * Note: if some of the configuration parameter is null, the corresponding config - * option is skipped. This enables calling this function multiple times to configure - * different parts related to for example network stack or io driver separately + * @brief Configures driver related options of esp_netif object * * @param[inout] pointer to the object to be configured - * @param[in] esp_netif_config pointer esp-netif configuration + * @param[in] driver_config pointer esp-netif io driver related configuration * @return - * - ESP_OK + * - ESP_OK on success + * - ESP_ERR_ESP_NETIF_INVALID_PARAMS if invalid parameters provided * */ -esp_err_t esp_netif_configure(esp_netif_t *esp_netif, - const esp_netif_config_t *esp_netif_config); +esp_err_t esp_netif_set_driver_config(esp_netif_t *esp_netif, + const esp_netif_driver_ifconfig_t *driver_config); + esp_err_t esp_netif_attach(esp_netif_t *esp_netif, esp_netif_iodriver_handle driver_handle); @@ -629,13 +627,16 @@ size_t esp_netif_get_nr_of_ifs(void); */ esp_err_t esp_netif_get_sta_list(const wifi_sta_list_t *wifi_sta_list, esp_netif_sta_list_t *tcpip_sta_list); -int esp_netif_get_netif_index(esp_netif_t *esp_netif); - -#if CONFIG_ESP_NETIF_USE_TCPIP_ADAPTER_COMPATIBLE_LAYER -// -// 8) Compatibility layer -// -#include "tcpip_adapter.h" -#endif +/** + * @brief Get net interface index from network stack implementation + * + * @note This index could be used in `setsockopt()` to bind socket with multicast interface + * + * @param[in] esp_netif Handle to esp-netif instance + * + * @return + * implementation specific index of interface represented with supplied esp_netif + */ +int esp_netif_get_netif_impl_index(esp_netif_t *esp_netif); #endif /* _ESP_NETIF_H_ */ diff --git a/components/esp_netif/include/esp_netif_defaults.h b/components/esp_netif/include/esp_netif_defaults.h index 19b7208e94..b7d7c5810b 100644 --- a/components/esp_netif/include/esp_netif_defaults.h +++ b/components/esp_netif/include/esp_netif_defaults.h @@ -26,7 +26,7 @@ { \ .base = ESP_NETIF_BASE_DEFAULT_ETH, \ .stack = ESP_NETIF_NETSTACK_DEFAULT_ETH, \ - .driver = ESP_NETIF_DRIVER_DEFAULT_ETH, \ + .driver = NULL, \ } /** diff --git a/components/esp_netif/include/esp_netif_ip_addr.h b/components/esp_netif/include/esp_netif_ip_addr.h index 1680ac9764..620a494d28 100644 --- a/components/esp_netif/include/esp_netif_ip_addr.h +++ b/components/esp_netif/include/esp_netif_ip_addr.h @@ -24,6 +24,12 @@ (((x) & (uint32_t)0x00ff0000UL) >> 8) | \ (((x) & (uint32_t)0xff000000UL) >> 24)) #endif + +#define esp_netif_ip4_makeu32(a,b,c,d) (((uint32_t)((a) & 0xff) << 24) | \ + ((uint32_t)((b) & 0xff) << 16) | \ + ((uint32_t)((c) & 0xff) << 8) | \ + (uint32_t)((d) & 0xff)) + // Access address in 16-bit block #define ESP_IP6_ADDR_BLOCK1(ip6addr) ((uint16_t)((esp_netif_htonl((ip6addr)->addr[0]) >> 16) & 0xffff)) #define ESP_IP6_ADDR_BLOCK2(ip6addr) ((uint16_t)((esp_netif_htonl((ip6addr)->addr[0])) & 0xffff)) @@ -63,6 +69,10 @@ ESP_IP6_ADDR_BLOCK7(&(ipaddr)), \ ESP_IP6_ADDR_BLOCK8(&(ipaddr)) +#define ESP_IPADDR_TYPE_V4 0U +#define ESP_IPADDR_TYPE_V6 6U +#define ESP_IPADDR_TYPE_ANY 46U + struct esp_ip6_addr { uint32_t addr[4]; diff --git a/components/esp_netif/include/esp_netif_types.h b/components/esp_netif/include/esp_netif_types.h index 5ce7837c11..7f08bf1cfd 100644 --- a/components/esp_netif/include/esp_netif_types.h +++ b/components/esp_netif/include/esp_netif_types.h @@ -202,6 +202,7 @@ typedef struct esp_netif_driver_ifconfig esp_netif_driver_ifconfig_t; */ typedef enum esp_netif_netstack_type { ESP_NETIF_NETWORK_STACK_IS_LWIP = 0, + ESP_NETIF_NETWORK_STACK_IS_LOOPBACK = 1, ESP_NETIF_NETWORK_STACK_MAX, } esp_netif_netstack_type_t; diff --git a/components/esp_netif/loopback/esp_netif_loopback.c b/components/esp_netif/loopback/esp_netif_loopback.c new file mode 100644 index 0000000000..0052a686cb --- /dev/null +++ b/components/esp_netif/loopback/esp_netif_loopback.c @@ -0,0 +1,466 @@ +// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include + +#include "esp_netif_lwip_internal.h" + +#include "esp_netif.h" +#include "esp_netif_private.h" + +#if CONFIG_ESP_NETIF_LOOPBACK + +#include "esp_log.h" + +// +// Purpose of this module is to implement minimal loopback netif to facilitate +// low level driver testing +// + +#define ESP_NETIF_HOSTNAME_MAX_SIZE 32 + +static const char *TAG = "esp_netif_loopback"; + +static bool s_netif_initialized = false; +static bool s_netif_started = false; +static bool s_netif_up = false; + +/** + * @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; + + // 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); + + // misc flags, types, keys, priority + esp_netif_flags_t flags; + char * hostname; + char * if_key; + esp_netif_type_t if_type; + int route_prio; +}; + +void esp_netif_set_ip4_addr(esp_ip4_addr_t *addr, uint8_t a, uint8_t b, uint8_t c, uint8_t d) +{ + memset(addr, 0, sizeof(esp_ip4_addr_t)); + addr->addr = esp_netif_htonl(esp_netif_ip4_makeu32(a,b,c,d)); +} + +char * esp_ip4addr_ntoa(const esp_ip4_addr_t *addr, char *buf, int buflen) +{ + return NULL; +} + +esp_netif_iodriver_handle esp_netif_get_io_driver(esp_netif_t *esp_netif) +{ + return esp_netif->driver_handle; +} + +esp_netif_t* esp_netif_get_handle_from_netif_impl(void *dev) +{ + return NULL; +} + +void* esp_netif_get_netif_impl(esp_netif_t *esp_netif) +{ + return NULL; +} + +esp_err_t esp_netif_init(void) +{ + ESP_LOGI(TAG, "loopback initialization"); + if (s_netif_initialized) { + ESP_LOGE(TAG, "esp-netif has already been initialized"); + return ESP_ERR_INVALID_SIZE; + } + + s_netif_initialized = true; + ESP_LOGD(TAG, "esp-netif has been successfully initialized"); + return ESP_OK; +} + +esp_err_t esp_netif_deinit(void) +{ + ESP_LOGI(TAG, "loopback initialization"); + if (!s_netif_initialized) { + ESP_LOGE(TAG, "esp-netif has not been initialized yet"); + return ESP_ERR_INVALID_SIZE; + } + s_netif_initialized = false; + ESP_LOGD(TAG, "esp-netif has been successfully deinitialized"); + return ESP_OK; +} + +static esp_err_t esp_netif_init_configuration(esp_netif_t *esp_netif, const esp_netif_config_t *cfg) +{ + // Basic esp_netif and lwip is a mandatory configuration and cannot be updated after esp_netif_new() + if (cfg == NULL || cfg->base == NULL || cfg->stack == NULL) { + return ESP_ERR_ESP_NETIF_INVALID_PARAMS; + } + + // Configure general esp-netif properties + memcpy(esp_netif->mac, cfg->base->mac, NETIF_MAX_HWADDR_LEN); + if (cfg->base->ip_info == NULL) { + ip4_addr_set_zero(&esp_netif->ip_info->ip); + ip4_addr_set_zero(&esp_netif->ip_info->gw); + ip4_addr_set_zero(&esp_netif->ip_info->netmask); + } else { + memcpy(esp_netif->ip_info, cfg->base->ip_info, sizeof(esp_netif_ip_info_t)); + } + memcpy(esp_netif->ip_info_old, esp_netif->ip_info, sizeof(esp_netif_ip_info_t)); + + // Setup main config parameters + esp_netif->flags = cfg->base->flags; + + if (cfg->base->if_key) { + esp_netif->if_key = strdup(cfg->base->if_key); + } + if (cfg->base->if_type) { + esp_netif->if_type = cfg->base->if_type; + } + if (cfg->base->route_prio) { + esp_netif->route_prio = cfg->base->route_prio; + } + + // Install network stack functions -- connects netif and L3 stack + if (cfg->stack->base.type != ESP_NETIF_NETWORK_STACK_IS_LOOPBACK) { + ESP_LOGE(TAG, "Failed to configure uknown network stack %d", cfg->stack->base.type); + return ESP_ERR_NOT_SUPPORTED; + } + + // Install IO functions only if provided -- connects driver and netif + // this configuration could be updated after esp_netif_new(), typically in post_attach callback + if (cfg->driver) { + const esp_netif_driver_ifconfig_t *esp_netif_driver_config = cfg->driver; + if (esp_netif_driver_config->handle) { + esp_netif->driver_handle = esp_netif_driver_config->handle; + } + if (esp_netif_driver_config->transmit) { + esp_netif->driver_transmit = esp_netif_driver_config->transmit; + } + if (esp_netif_driver_config->driver_free_rx_buffer) { + esp_netif->driver_free_rx_buffer = esp_netif_driver_config->driver_free_rx_buffer; + } + } + return ESP_OK; +} + +esp_netif_t *esp_netif_new(const esp_netif_config_t *esp_netif_config) +{ + // mandatory configuration must be provided when creating esp_netif object + if (esp_netif_config == NULL) { + return NULL; + } + + // Create parent esp-netif object + esp_netif_t *esp_netif = calloc(1, sizeof(struct esp_netif_obj)); + if (!esp_netif) { + return NULL; + } + + // Create ip info + esp_netif_ip_info_t *ip_info = calloc(1, sizeof(esp_netif_ip_info_t)); + if (!ip_info) { + free(esp_netif); + return NULL; + } + esp_netif->ip_info = ip_info; + + // creating another ip info (to store old ip) + ip_info = calloc(1, sizeof(esp_netif_ip_info_t)); + if (!ip_info) { + free(esp_netif->ip_info); + free(esp_netif); + return NULL; + } + esp_netif->ip_info_old = ip_info; + + esp_netif_add_to_list(esp_netif); + + // Configure the created object with provided configuration + esp_err_t ret = esp_netif_init_configuration(esp_netif, esp_netif_config); + if (ret != ESP_OK) { + ESP_LOGE(TAG, "Initial configuration of esp_netif failed with %d", ret); + esp_netif_destroy(esp_netif); + return NULL; + } + + return esp_netif; +} + +void esp_netif_destroy(esp_netif_t *esp_netif) +{ + if (esp_netif) { + esp_netif_remove_from_list(esp_netif); + free(esp_netif->ip_info); + free(esp_netif->ip_info_old); + free(esp_netif->if_key); + free(esp_netif); + } +} + +esp_err_t esp_netif_attach(esp_netif_t *esp_netif, esp_netif_iodriver_handle driver_handle) +{ + esp_netif_driver_base_t *base_driver = driver_handle; + + esp_netif->driver_handle = driver_handle; + if (base_driver->post_attach) { + esp_err_t ret = base_driver->post_attach(esp_netif, driver_handle); + if (ret != ESP_OK) { + ESP_LOGE(TAG, "Post-attach callback of driver(%p) failed with %d", driver_handle, ret); + return ESP_ERR_ESP_NETIF_DRIVER_ATACH_FAILED; + } + } + return ESP_OK; +} + +esp_err_t esp_netif_set_driver_config(esp_netif_t *esp_netif, + const esp_netif_driver_ifconfig_t *driver_config) +{ + if (esp_netif == NULL || driver_config == NULL) { + return ESP_ERR_ESP_NETIF_INVALID_PARAMS; + } + esp_netif->driver_handle = driver_config->handle; + esp_netif->driver_transmit = driver_config->transmit; + esp_netif->driver_free_rx_buffer = driver_config->driver_free_rx_buffer; + return ESP_OK; +} + +esp_err_t esp_netif_set_mac(esp_netif_t *esp_netif, uint8_t mac[]) +{ + return ESP_ERR_NOT_SUPPORTED; +} + +esp_err_t esp_netif_start(esp_netif_t *esp_netif) +{ + ESP_LOGI(TAG, "Netif started"); + s_netif_started = true; + return ESP_OK; +} + + +esp_err_t esp_netif_stop(esp_netif_t *esp_netif) +{ + ESP_LOGI(TAG, "Netif stopped"); + s_netif_started = false; + return ESP_OK; +} + +// +// IO translate functions +// +void esp_netif_free_rx_buffer(void *h, void* buffer) +{ + esp_netif_t *esp_netif = h; + esp_netif->driver_free_rx_buffer(esp_netif->driver_handle, buffer); +} + +esp_err_t esp_netif_transmit(esp_netif_t *esp_netif, void* data, size_t len) +{ + ESP_LOGV(TAG, "Transmitting data: ptr:%p, size:%d", data, len); + return (esp_netif->driver_transmit)(esp_netif->driver_handle, data, len); +} + +esp_err_t esp_netif_receive(esp_netif_t *esp_netif, void *buffer, size_t len, void *eb) +{ + ESP_LOGV(TAG, "Received data: ptr:%p, size:%d", buffer, len); + esp_netif_transmit(esp_netif, buffer, len); + if (eb) { + esp_netif_free_rx_buffer(esp_netif, eb); + } + return ESP_OK; +} + +esp_err_t esp_netif_dhcpc_stop(esp_netif_t *esp_netif) +{ + return ESP_ERR_NOT_SUPPORTED; +} + +esp_err_t esp_netif_dhcpc_start(esp_netif_t *esp_netif) +{ + return ESP_ERR_NOT_SUPPORTED; +} + +esp_err_t esp_netif_dhcps_get_status(esp_netif_t *esp_netif, esp_netif_dhcp_status_t *status) +{ + return ESP_ERR_NOT_SUPPORTED; +} + +esp_err_t esp_netif_dhcpc_get_status(esp_netif_t *esp_netif, esp_netif_dhcp_status_t *status) +{ + return ESP_ERR_NOT_SUPPORTED; +} + +esp_err_t esp_netif_dhcps_start(esp_netif_t *esp_netif) +{ + return ESP_ERR_NOT_SUPPORTED; +} + +esp_err_t esp_netif_dhcps_stop(esp_netif_t *esp_netif) +{ + return ESP_ERR_NOT_SUPPORTED; +} + +esp_err_t esp_netif_set_hostname(esp_netif_t *esp_netif, const char *hostname) +{ + return ESP_ERR_NOT_SUPPORTED; +} + +esp_err_t esp_netif_get_hostname(esp_netif_t *esp_netif, const char **hostname) +{ + return ESP_ERR_NOT_SUPPORTED; +} + +esp_err_t esp_netif_up(esp_netif_t *esp_netif) +{ + ESP_LOGI(TAG, "Netif going up"); + s_netif_up = true; + return ESP_OK; +} + +esp_err_t esp_netif_down(esp_netif_t *esp_netif) +{ + ESP_LOGI(TAG, "Netif going down"); + s_netif_up = false; + return ESP_OK; +} + +bool esp_netif_is_netif_up(esp_netif_t *esp_netif) +{ + return s_netif_up; +} + +esp_err_t esp_netif_get_old_ip_info(esp_netif_t *esp_netif, esp_netif_ip_info_t *ip_info) +{ + ESP_LOGD(TAG, "%s esp_netif:%p", __func__, esp_netif); + + if (esp_netif == NULL || ip_info == NULL) { + return ESP_ERR_INVALID_ARG; + } + memcpy(ip_info, esp_netif->ip_info_old, sizeof(esp_netif_ip_info_t)); + return ESP_OK; +} + +esp_err_t esp_netif_get_ip_info(esp_netif_t *esp_netif, esp_netif_ip_info_t *ip_info) +{ + ESP_LOGD(TAG, "%s esp_netif:%p", __func__, esp_netif); + + if (esp_netif == NULL || ip_info == NULL) { + return ESP_ERR_INVALID_ARG; + } + + memcpy(ip_info, esp_netif->ip_info, sizeof(esp_netif_ip_info_t)); + return ESP_OK; +} + + +bool esp_netif_is_valid_static_ip(esp_netif_ip_info_t *ip_info) +{ + return true; +} + +esp_err_t esp_netif_set_old_ip_info(esp_netif_t *esp_netif, const esp_netif_ip_info_t *ip_info) +{ + ESP_LOGD(TAG, "%s esp_netif:%p", __func__, esp_netif); + + if (esp_netif == NULL || ip_info == NULL) { + return ESP_ERR_INVALID_ARG; + } + memcpy(esp_netif->ip_info_old, ip_info, sizeof(esp_netif_ip_info_t)); + return ESP_OK; +} + +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) +{ + return ESP_ERR_NOT_SUPPORTED; +} + +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) +{ + return ESP_ERR_NOT_SUPPORTED; +} + +esp_err_t esp_netif_get_sta_list(const wifi_sta_list_t *wifi_sta_list, esp_netif_sta_list_t *netif_sta_list) +{ + return ESP_ERR_NOT_SUPPORTED; +} + +esp_err_t esp_netif_create_ip6_linklocal(esp_netif_t *esp_netif) +{ + return ESP_ERR_NOT_SUPPORTED; +} + +esp_err_t esp_netif_get_ip6_linklocal(esp_netif_t *esp_netif, esp_ip6_addr_t *if_ip6) +{ + return ESP_ERR_NOT_SUPPORTED; +} + +esp_netif_flags_t esp_netif_get_flags(esp_netif_t *esp_netif) +{ + return esp_netif->flags; +} + +char *esp_netif_get_ifkey(esp_netif_t *esp_netif) +{ + return esp_netif->if_key; +} + +esp_netif_type_t esp_netif_get_type(esp_netif_t *esp_netif) +{ + return esp_netif->if_type; +} + +esp_netif_t *esp_netif_get_handle_from_ifkey(const char *if_key) +{ + esp_netif_t *esp_netif = esp_netif_next(NULL); + do { + if (esp_netif && strcmp(if_key, esp_netif->if_key)==0) { + return esp_netif; + } + } while (NULL != (esp_netif = esp_netif_next(esp_netif))); + return NULL; +} + +uint32_t esp_netif_get_event_id(esp_netif_t *esp_netif, esp_netif_ip_event_type_t event_type) +{ + return 0; +} + +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) +{ + return ESP_ERR_NOT_SUPPORTED; +} + +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) +{ + return ESP_ERR_NOT_SUPPORTED; +} + +int esp_netif_get_netif_impl_index(esp_netif_t *esp_netif) +{ + return 0; +} + +#endif /* CONFIG_ESP_NETIF_LOOPBACK */ diff --git a/components/esp_netif/lwip/esp_netif_lwip.c b/components/esp_netif/lwip/esp_netif_lwip.c index 6ff408ec46..ee9b4e57fb 100644 --- a/components/esp_netif/lwip/esp_netif_lwip.c +++ b/components/esp_netif/lwip/esp_netif_lwip.c @@ -12,18 +12,15 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include #include #include "esp_netif_lwip_internal.h" #include "esp_netif.h" #include "esp_netif_private.h" -#include "esp_netif_net_stack.h" -#if CONFIG_TCPIP_LWIP +#if CONFIG_ESP_NETIF_TCPIP_LWIP -#include "lwip/inet.h" #include "lwip/tcpip.h" #include "lwip/dhcp.h" #include "lwip/ip_addr.h" @@ -34,14 +31,12 @@ #if LWIP_DNS /* don't build if not configured for use in lwipopts.h */ #include "lwip/dns.h" #endif -#include "esp_netif_lwip_internal.h" #include "dhcpserver/dhcpserver.h" #include "dhcpserver/dhcpserver_options.h" #include "esp_event.h" #include "esp_log.h" -#include "lwip/ip_addr.h" // // This is the main module implementing lwip interaction with esp-netif @@ -64,8 +59,6 @@ static sys_sem_t api_lock_sem = NULL; static bool tcpip_initialized = false; static esp_netif_t *s_last_default_esp_netif = NULL; -static int esp_netif_ipc_check(esp_netif_api_msg_t *msg); - /** * @brief Main esp-netif container with interface related information * @@ -73,7 +66,7 @@ static int esp_netif_ipc_check(esp_netif_api_msg_t *msg); */ struct esp_netif_obj { // default interface addresses - uint8_t mac[6]; + uint8_t mac[NETIF_MAX_HWADDR_LEN]; esp_netif_ip_info_t* ip_info; esp_netif_ip_info_t* ip_info_old; @@ -104,6 +97,55 @@ struct esp_netif_obj { int route_prio; }; +/** + * @brief thread safe tcpip function utility macro + */ +#define _LWIP_TASK_IPC_CALL(function, netif, param) \ +{ \ + return esp_netif_lwip_ipc_call(function, netif, (void *)param); \ +} + +/** + * @brief Api callback from tcpip thread used to call esp-netif + * function in lwip task context + */ +static void esp_netif_api_cb(void *api_msg) +{ + esp_netif_api_msg_t *msg = (esp_netif_api_msg_t *)api_msg; + + if (!msg || !msg->api_fn) { + ESP_LOGD(TAG, "null msg/api_fn"); + return; + } + + msg->ret = msg->api_fn(msg); + ESP_LOGD(TAG, "call api in lwip: ret=0x%x, give sem", msg->ret); + sys_sem_signal(&api_sync_sem); + +} + +/** + * @brief Initiates a tcpip remote call if called from another task + * or calls the function directly if executed from lwip task + */ +static inline esp_err_t esp_netif_lwip_ipc_call(esp_netif_api_fn fn, esp_netif_t *netif, void *data) +{ + esp_netif_api_msg_t msg = { + .esp_netif = netif, + .data = data, + .api_fn = fn + }; + if (g_lwip_task == xTaskGetCurrentTaskHandle()) { + ESP_LOGD(TAG, "check: remote, if=%p fn=%p\n", netif, fn); + sys_arch_sem_wait(&api_lock_sem, 0); + tcpip_send_msg_wait_sem((tcpip_callback_fn)esp_netif_api_cb, &msg, &api_sync_sem); + sys_sem_signal(&api_lock_sem); + return msg.ret; + } + ESP_LOGD(TAG, "check: local, if=%p fn=%p\n", netif, fn); + return fn(&msg); +} + /** * @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 @@ -172,7 +214,9 @@ esp_netif_t* esp_netif_get_handle_from_netif_impl(void *dev) void* esp_netif_get_netif_impl(esp_netif_t *esp_netif) { - return esp_netif->lwip_netif; + if (esp_netif) + return esp_netif->lwip_netif; + return NULL; } esp_err_t esp_netif_init(void) @@ -218,8 +262,76 @@ esp_err_t esp_netif_deinit(void) return ESP_ERR_INVALID_STATE; } +static esp_err_t esp_netif_init_configuration(esp_netif_t *esp_netif, const esp_netif_config_t *cfg) +{ + // Basic esp_netif and lwip is a mandatory configuration and cannot be updated after esp_netif_new() + if (cfg == NULL || cfg->base == NULL || cfg->stack == NULL) { + return ESP_ERR_ESP_NETIF_INVALID_PARAMS; + } + + // Configure general esp-netif properties + memcpy(esp_netif->mac, cfg->base->mac, NETIF_MAX_HWADDR_LEN); + if (cfg->base->ip_info == NULL) { + ip4_addr_set_zero(&esp_netif->ip_info->ip); + ip4_addr_set_zero(&esp_netif->ip_info->gw); + ip4_addr_set_zero(&esp_netif->ip_info->netmask); + } else { + memcpy(esp_netif->ip_info, cfg->base->ip_info, sizeof(esp_netif_ip_info_t)); + } + memcpy(esp_netif->ip_info_old, esp_netif->ip_info, sizeof(esp_netif_ip_info_t)); + + // Setup main config parameters + esp_netif->lost_ip_event = cfg->base->lost_ip_event; + esp_netif->get_ip_event = cfg->base->get_ip_event; + esp_netif->flags = cfg->base->flags; + + if (cfg->base->if_key) { + esp_netif->if_key = strdup(cfg->base->if_key); + } + if (cfg->base->if_type) { + esp_netif->if_type = cfg->base->if_type; + } + if (cfg->base->route_prio) { + esp_netif->route_prio = cfg->base->route_prio; + } + + // Install network stack functions -- connects netif and L3 stack + const esp_netif_netstack_config_t *esp_netif_stack_config = cfg->stack; + if (cfg->stack->base.type != ESP_NETIF_NETWORK_STACK_IS_LWIP) { + ESP_LOGE(TAG, "Failed to configure uknown network stack %d", cfg->stack->base.type); + return ESP_ERR_NOT_SUPPORTED; + } + 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; + } + + // Install IO functions only if provided -- connects driver and netif + // this configuration could be updated after esp_netif_new(), typically in post_attach callback + if (cfg->driver) { + const esp_netif_driver_ifconfig_t *esp_netif_driver_config = cfg->driver; + if (esp_netif_driver_config->handle) { + esp_netif->driver_handle = esp_netif_driver_config->handle; + } + if (esp_netif_driver_config->transmit) { + esp_netif->driver_transmit = esp_netif_driver_config->transmit; + } + if (esp_netif_driver_config->driver_free_rx_buffer) { + esp_netif->driver_free_rx_buffer = esp_netif_driver_config->driver_free_rx_buffer; + } + } + return ESP_OK; +} + esp_netif_t *esp_netif_new(const esp_netif_config_t *esp_netif_config) { + // mandatory configuration must be provided when creating esp_netif object + if (esp_netif_config == NULL) { + return NULL; + } + // Create parent esp-netif object esp_netif_t *esp_netif = calloc(1, sizeof(struct esp_netif_obj)); if (!esp_netif) { @@ -258,8 +370,11 @@ esp_netif_t *esp_netif_new(const esp_netif_config_t *esp_netif_config) esp_netif_add_to_list(esp_netif); // Configure the created object with provided configuration - if (esp_netif_config) { - ESP_ERROR_CHECK(esp_netif_configure(esp_netif, esp_netif_config)); + esp_err_t ret = esp_netif_init_configuration(esp_netif, esp_netif_config); + if (ret != ESP_OK) { + ESP_LOGE(TAG, "Initial configuration of esp_netif failed with %d", ret); + esp_netif_destroy(esp_netif); + return NULL; } return esp_netif; @@ -292,67 +407,15 @@ esp_err_t esp_netif_attach(esp_netif_t *esp_netif, esp_netif_iodriver_handle dri return ESP_OK; } -esp_err_t esp_netif_configure(esp_netif_t *esp_netif, const esp_netif_config_t *cfg) - +esp_err_t esp_netif_set_driver_config(esp_netif_t *esp_netif, + const esp_netif_driver_ifconfig_t *driver_config) { - if (cfg == NULL) { + if (esp_netif == NULL || driver_config == NULL) { return ESP_ERR_ESP_NETIF_INVALID_PARAMS; } -// Configure general esp-netif properties - if (cfg->base) { - memcpy(esp_netif->mac, cfg->base->mac, NETIF_MAX_HWADDR_LEN); - if (cfg->base->ip_info == NULL) { - ip4_addr_set_zero(&esp_netif->ip_info->ip); - ip4_addr_set_zero(&esp_netif->ip_info->gw); - ip4_addr_set_zero(&esp_netif->ip_info->netmask); - } else { - memcpy(esp_netif->ip_info, cfg->base->ip_info, sizeof(esp_netif_ip_info_t)); - } - memcpy(esp_netif->ip_info_old, esp_netif->ip_info, sizeof(esp_netif_ip_info_t)); - - // Setup main config parameters - esp_netif->lost_ip_event = cfg->base->lost_ip_event; - esp_netif->get_ip_event = cfg->base->get_ip_event; - esp_netif->flags = cfg->base->flags; - - if (cfg->base->if_key) { - esp_netif->if_key = strdup(cfg->base->if_key); - } - if (cfg->base->if_type) { - esp_netif->if_type = cfg->base->if_type; - } - if (cfg->base->route_prio) { - esp_netif->route_prio = cfg->base->route_prio; - } - } - -// Install network stack functions -- connects netif and L3 stack - if (cfg->stack) { - const esp_netif_netstack_config_t *esp_netif_stack_config = cfg->stack; - if (cfg->stack->base.type != ESP_NETIF_NETWORK_STACK_IS_LWIP) { - ESP_LOGE(TAG, "Failed to configure uknown network stack %d", cfg->stack->base.type); - return ESP_ERR_NOT_SUPPORTED; - } - 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; - } - } -// Install IO functions -- connects driver and netif - if (cfg->driver) { - const esp_netif_driver_ifconfig_t *esp_netif_driver_config = cfg->driver; - if (esp_netif_driver_config->handle) { - esp_netif->driver_handle = esp_netif_driver_config->handle; - } - if (esp_netif_driver_config->transmit) { - esp_netif->driver_transmit = esp_netif_driver_config->transmit; - } - if (esp_netif_driver_config->driver_free_rx_buffer) { - esp_netif->driver_free_rx_buffer = esp_netif_driver_config->driver_free_rx_buffer; - } - } + esp_netif->driver_handle = driver_config->handle; + esp_netif->driver_transmit = driver_config->transmit; + esp_netif->driver_free_rx_buffer = driver_config->driver_free_rx_buffer; return ESP_OK; } @@ -374,10 +437,6 @@ esp_err_t esp_netif_set_mac(esp_netif_t *esp_netif, uint8_t mac[]) return ESP_OK; } -esp_err_t esp_netif_start_api(esp_netif_api_msg_t *msg) -{ - return esp_netif_start(msg->esp_netif); -} static void esp_netif_dhcps_cb(u8_t client_ip[4]) { @@ -393,13 +452,37 @@ static void esp_netif_dhcps_cb(u8_t client_ip[4]) } } - -esp_err_t esp_netif_start(esp_netif_t *esp_netif) +static esp_err_t esp_netif_config_sanity_check(const esp_netif_t * esp_netif) { - ESP_NETIF_IPC_CALL(esp_netif, NULL, esp_netif_start_api); + if (esp_netif == NULL) { + ESP_LOGE(TAG, "Cannot start esp_netif: esp_netif must not be null"); + return ESP_ERR_INVALID_STATE; + } + + if (esp_netif->driver_transmit == NULL || + esp_netif->driver_handle == NULL || + esp_netif->lwip_input_fn == NULL || + esp_netif->lwip_init_fn == NULL || + esp_netif->lwip_netif == NULL) { + ESP_LOGE(TAG, "Cannot start esp_netif: Missing mandatory configuration:\n" + "esp_netif->driver_transmit: %p, esp_netif->driver_handle:%p, " + "esp_netif->lwip_input_fn: %p, esp_netif->lwip_init_fn:%p, esp_netif->lwip_netif: %p", + esp_netif->driver_transmit, esp_netif->driver_handle, + esp_netif->lwip_input_fn, esp_netif->lwip_init_fn, esp_netif->lwip_netif); + + return ESP_ERR_INVALID_STATE; + } + return ESP_OK; +} + +static esp_err_t esp_netif_start_api(esp_netif_api_msg_t *msg) +{ + esp_netif_t * esp_netif = msg->esp_netif; ESP_LOGD(TAG, "%s %p", __func__, esp_netif); + ESP_ERROR_CHECK(esp_netif_config_sanity_check(esp_netif)); + 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); if (esp_netif->flags&ESP_NETIF_FLAG_GARP) { @@ -441,14 +524,11 @@ esp_err_t esp_netif_start(esp_netif_t *esp_netif) return ESP_OK; } -esp_err_t esp_netif_stop_api(esp_netif_api_msg_t *msg) -{ - return esp_netif_stop(msg->esp_netif); -} +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_stop(esp_netif_t *esp_netif) +static esp_err_t esp_netif_stop_api(esp_netif_api_msg_t *msg) { - ESP_NETIF_IPC_CALL(esp_netif, NULL, esp_netif_stop_api); + esp_netif_t *esp_netif = msg->esp_netif; struct netif *lwip_netif = esp_netif->lwip_netif; if (lwip_netif == NULL) { @@ -482,6 +562,11 @@ esp_err_t esp_netif_stop(esp_netif_t *esp_netif) 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) + +// +// IO translate functions +// void esp_netif_free_rx_buffer(void *h, void* buffer) { esp_netif_t *esp_netif = h; @@ -499,41 +584,6 @@ esp_err_t esp_netif_receive(esp_netif_t *esp_netif, void *buffer, size_t len, vo return ESP_OK; } - -static void esp_netif_api_cb(void *api_msg) -{ - esp_netif_api_msg_t *msg = (esp_netif_api_msg_t *)api_msg; - - if (!msg || !msg->api_fn) { - ESP_LOGD(TAG, "null msg/api_fn"); - return; - } - - msg->ret = msg->api_fn(msg); - ESP_LOGD(TAG, "call api in lwip: ret=0x%x, give sem", msg->ret); - sys_sem_signal(&api_sync_sem); - -} - -static int esp_netif_ipc_check(esp_netif_api_msg_t *msg) -{ -#if ESP_NETIF_TRHEAD_SAFE - xTaskHandle local_task = xTaskGetCurrentTaskHandle(); - - if (local_task == g_lwip_task) { - return ESP_NETIF_IPC_LOCAL; - } - - sys_arch_sem_wait(&api_lock_sem, 0); - tcpip_send_msg_wait_sem((tcpip_callback_fn)esp_netif_api_cb, msg, &api_sync_sem); - sys_sem_signal(&api_lock_sem); - - return ESP_NETIF_IPC_REMOTE; -#else - return ESP_NETIF_IPC_LOCAL; -#endif -} - // // DHCP: // @@ -603,8 +653,6 @@ static void esp_netif_ip_lost_timer(void *arg) struct netif *netif = esp_netif->lwip_netif; - // @TODO: check if netif type is sta - if ( (!netif) || (netif && ip4_addr_cmp(ip_2_ip4(&netif->ip_addr), IP4_ADDR_ANY4))) { ip_event_got_ip_t evt = { .esp_netif = esp_netif, @@ -654,15 +702,10 @@ static esp_err_t esp_netif_start_ip_lost_timer(esp_netif_t *esp_netif) static esp_err_t esp_netif_dhcpc_stop_api(esp_netif_api_msg_t *msg) { - return esp_netif_dhcpc_stop(msg->esp_netif); -} + esp_netif_t *esp_netif = msg->esp_netif; -esp_err_t esp_netif_dhcpc_stop(esp_netif_t *esp_netif) -{ ESP_LOGD(TAG, "%s esp_netif:%p", __func__, esp_netif); - ESP_NETIF_IPC_CALL(esp_netif, NULL, esp_netif_dhcpc_stop_api); - if (esp_netif == NULL) { ESP_LOGE(TAG, "dhcp client stop called with NULL api"); return ESP_ERR_ESP_NETIF_INVALID_PARAMS; @@ -690,25 +733,20 @@ esp_err_t esp_netif_dhcpc_stop(esp_netif_t *esp_netif) LWIP_DHCP_IP_ADDR_ERASE(esp_netif); 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) static esp_err_t esp_netif_dhcpc_start_api(esp_netif_api_msg_t *msg) { - return esp_netif_dhcpc_start(msg->esp_netif); -} + esp_netif_t *esp_netif = msg->esp_netif; -esp_err_t esp_netif_dhcpc_start(esp_netif_t *esp_netif) -{ ESP_LOGD(TAG, "%s esp_netif:%p", __func__, esp_netif); if (!esp_netif) { return ESP_ERR_INVALID_ARG; } - ESP_NETIF_IPC_CALL(esp_netif, NULL, esp_netif_dhcpc_start_api); - struct netif *p_netif = esp_netif->lwip_netif; esp_netif_reset_ip_info(esp_netif); @@ -746,6 +784,8 @@ esp_err_t esp_netif_dhcpc_start(esp_netif_t *esp_netif) } } +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_dhcps_get_status(esp_netif_t *esp_netif, esp_netif_dhcp_status_t *status) { if (!esp_netif || (esp_netif->flags&ESP_NETIF_DHCPC)) { @@ -768,19 +808,14 @@ esp_err_t esp_netif_dhcpc_get_status(esp_netif_t *esp_netif, esp_netif_dhcp_stat static esp_err_t esp_netif_dhcps_start_api(esp_netif_api_msg_t *msg) { - return esp_netif_dhcps_start(msg->esp_netif); -} + esp_netif_t *esp_netif = msg->esp_netif; -esp_err_t esp_netif_dhcps_start(esp_netif_t *esp_netif) -{ ESP_LOGD(TAG, "%s esp_netif:%p", __func__, esp_netif); - if (!esp_netif || (!(esp_netif->flags&ESP_NETIF_DHCPS))) { + if (!esp_netif) { return ESP_ERR_INVALID_ARG; } - ESP_NETIF_IPC_CALL(esp_netif, NULL, esp_netif_dhcps_start_api); - struct netif *p_netif = esp_netif->lwip_netif; if (p_netif != NULL && netif_is_up(p_netif)) { esp_netif_ip_info_t *default_ip = esp_netif->ip_info; @@ -798,19 +833,17 @@ esp_err_t esp_netif_dhcps_start(esp_netif_t *esp_netif) } } +esp_err_t esp_netif_dhcps_start(esp_netif_t *esp_netif) _LWIP_TASK_IPC_CALL(esp_netif_dhcps_start_api, esp_netif, NULL) + static esp_err_t esp_netif_dhcps_stop_api(esp_netif_api_msg_t *msg) { - return esp_netif_dhcps_stop(msg->esp_netif); -} + esp_netif_t *esp_netif = msg->esp_netif; -esp_err_t esp_netif_dhcps_stop(esp_netif_t *esp_netif) -{ ESP_LOGD(TAG, "%s esp_netif:%p", __func__, esp_netif); if (!esp_netif) { return ESP_ERR_INVALID_ARG; } - ESP_NETIF_IPC_CALL(esp_netif, NULL, esp_netif_dhcps_stop_api); struct netif *p_netif = esp_netif->lwip_netif; if (esp_netif->dhcps_status == ESP_NETIF_DHCP_STARTED) { @@ -830,13 +863,13 @@ esp_err_t esp_netif_dhcps_stop(esp_netif_t *esp_netif) 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) + static esp_err_t esp_netif_set_hostname_api(esp_netif_api_msg_t *msg) { - return esp_netif_set_hostname(msg->esp_netif, msg->data); -} + esp_netif_t *esp_netif = msg->esp_netif; + const char *hostname = msg->data; -esp_err_t esp_netif_set_hostname(esp_netif_t *esp_netif, const char *hostname) -{ ESP_LOGD(TAG, "%s esp_netif:%p hostname %s", __func__, esp_netif, hostname); if (!esp_netif) { @@ -844,7 +877,6 @@ esp_err_t esp_netif_set_hostname(esp_netif_t *esp_netif, const char *hostname) } #if LWIP_NETIF_HOSTNAME - ESP_NETIF_IPC_CALL(esp_netif, hostname, esp_netif_set_hostname_api); struct netif *p_netif = esp_netif->lwip_netif; if (esp_netif->hostname) { @@ -870,6 +902,8 @@ esp_err_t esp_netif_set_hostname(esp_netif_t *esp_netif, const char *hostname) #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_get_hostname(esp_netif_t *esp_netif, const char **hostname) { ESP_LOGD(TAG, "%s esp_netif:%p", __func__, esp_netif); @@ -894,17 +928,13 @@ esp_err_t esp_netif_get_hostname(esp_netif_t *esp_netif, const char **hostname) esp_err_t esp_netif_up_api(esp_netif_api_msg_t *msg) { - return esp_netif_up(msg->esp_netif); -} + esp_netif_t *esp_netif = msg->esp_netif; -esp_err_t esp_netif_up(esp_netif_t *esp_netif) -{ ESP_LOGD(TAG, "%s esp_netif:%p", __func__, esp_netif); if (!esp_netif) { return ESP_ERR_INVALID_STATE; } - ESP_NETIF_IPC_CALL(esp_netif, NULL, esp_netif_up_api); struct netif *lwip_netif = esp_netif->lwip_netif; @@ -917,24 +947,17 @@ esp_err_t esp_netif_up(esp_netif_t *esp_netif) 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) + static esp_err_t esp_netif_down_api(esp_netif_api_msg_t *msg) { - return esp_netif_down(msg->esp_netif); -} + esp_netif_t *esp_netif = msg->esp_netif; -esp_err_t esp_netif_down(esp_netif_t *esp_netif) -{ ESP_LOGD(TAG, "%s esp_netif:%p", __func__, esp_netif); if (!esp_netif) { return ESP_ERR_INVALID_STATE; } - ESP_NETIF_IPC_CALL(esp_netif, NULL, &esp_netif_down_api); - - if (esp_netif->flags&ESP_NETIF_FLAG_AUTOUP) { - ESP_LOGE(TAG, "Interface if%p asked to go up, but configured to AUTO-UP flag (flags=%x)", esp_netif, esp_netif->flags); - return ESP_ERR_INVALID_STATE; - } struct netif *lwip_netif = esp_netif->lwip_netif; @@ -958,6 +981,8 @@ esp_err_t esp_netif_down(esp_netif_t *esp_netif) 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) + bool esp_netif_is_netif_up(esp_netif_t *esp_netif) { ESP_LOGV(TAG, "%s esp_netif:%p", __func__, esp_netif); @@ -1015,34 +1040,31 @@ bool esp_netif_is_valid_static_ip(esp_netif_ip_info_t *ip_info) static esp_err_t esp_netif_set_ip_old_info_api(esp_netif_api_msg_t *msg) { - memcpy(msg->esp_netif->ip_info, msg->data, sizeof(esp_netif_ip_info_t)); - return ESP_OK; -} + esp_netif_t *esp_netif = msg->esp_netif; + const esp_netif_ip_info_t *ip_info = msg->data; -esp_err_t esp_netif_set_old_ip_info(esp_netif_t *esp_netif, const esp_netif_ip_info_t *ip_info) -{ ESP_LOGD(TAG, "%s esp_netif:%p", __func__, esp_netif); if (esp_netif == NULL || ip_info == NULL) { return ESP_ERR_INVALID_STATE; } - ESP_NETIF_IPC_CALL(esp_netif, ip_info, esp_netif_set_ip_old_info_api); + + memcpy(msg->esp_netif->ip_info_old, msg->data, sizeof(esp_netif_ip_info_t)); 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) + static esp_err_t esp_netif_set_ip_info_api(esp_netif_api_msg_t *msg) { - return esp_netif_set_ip_info(msg->esp_netif, msg->data); -} + esp_netif_t *esp_netif = msg->esp_netif; + const esp_netif_ip_info_t *ip_info = msg->data; -esp_err_t esp_netif_set_ip_info(esp_netif_t *esp_netif, const esp_netif_ip_info_t *ip_info) -{ ESP_LOGD(TAG, "%s esp_netif:%p", __func__, esp_netif); if (esp_netif == NULL || ip_info == NULL) { return ESP_ERR_INVALID_STATE; } - ESP_NETIF_IPC_CALL(esp_netif, ip_info, esp_netif_set_ip_info_api); if (esp_netif->flags&ESP_NETIF_DHCPS) { if (esp_netif->dhcps_status != ESP_NETIF_DHCP_STOPPED) { @@ -1092,24 +1114,17 @@ esp_err_t esp_netif_set_ip_info(esp_netif_t *esp_netif, const esp_netif_ip_info_ 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) + static esp_err_t esp_netif_set_dns_info_api(esp_netif_api_msg_t *msg) { - esp_netif_dns_param_t *dns_param = (esp_netif_dns_param_t *)msg->data; + esp_netif_t *esp_netif = msg->esp_netif; + esp_netif_dns_param_t *dns_param = msg->data; + esp_netif_dns_type_t type = dns_param->dns_type; + esp_netif_dns_info_t *dns = dns_param->dns_info; - return esp_netif_set_dns_info(msg->esp_netif, dns_param->dns_type, dns_param->dns_info); -} - -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) -{ ESP_LOGD(TAG, "%s esp_netif:%p", __func__, esp_netif); - esp_netif_dns_param_t dns_param; - - dns_param.dns_type = type; - dns_param.dns_info = dns; - - ESP_NETIF_IPC_CALL(esp_netif, &dns_param, esp_netif_set_dns_info_api); - if (esp_netif == NULL) { return ESP_ERR_ESP_NETIF_INVALID_PARAMS; } @@ -1144,25 +1159,26 @@ esp_err_t esp_netif_set_dns_info(esp_netif_t *esp_netif, esp_netif_dns_type_t ty return ESP_OK; } -static esp_err_t esp_netif_get_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) { - esp_netif_dns_param_t *dns_param = (esp_netif_dns_param_t *)msg->data; - - return esp_netif_get_dns_info(msg->esp_netif, dns_param->dns_type, dns_param->dns_info); + esp_netif_dns_param_t dns_param = { + .dns_type = type, + .dns_info = dns + }; + return esp_netif_lwip_ipc_call(esp_netif_set_dns_info_api, esp_netif, (void *)&dns_param); } -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) +static esp_err_t esp_netif_get_dns_info_api(esp_netif_api_msg_t *msg) { + esp_netif_t *esp_netif = msg->esp_netif; + esp_netif_dns_param_t *dns_param = msg->data; + esp_netif_dns_type_t type = dns_param->dns_type; + esp_netif_dns_info_t *dns = dns_param->dns_info; + ESP_LOGD(TAG, "%s esp_netif:%p", __func__, esp_netif); - esp_netif_dns_param_t dns_param; - - dns_param.dns_type = type; - dns_param.dns_info = dns; - - ESP_NETIF_IPC_CALL(esp_netif, &dns_param, esp_netif_get_dns_info_api); if (!dns) { - ESP_LOGD(TAG, "get dns null dns"); + ESP_LOGE(TAG, "%s: dns_info cannot be NULL", __func__); return ESP_ERR_ESP_NETIF_INVALID_PARAMS; } @@ -1180,6 +1196,15 @@ esp_err_t esp_netif_get_dns_info(esp_netif_t *esp_netif, esp_netif_dns_type_t ty return ESP_OK; } +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) +{ + esp_netif_dns_param_t dns_param = { + .dns_type = type, + .dns_info = dns + }; + return esp_netif_lwip_ipc_call(esp_netif_get_dns_info_api, esp_netif, (void *)&dns_param); +} + esp_err_t esp_netif_get_sta_list(const wifi_sta_list_t *wifi_sta_list, esp_netif_sta_list_t *netif_sta_list) { ESP_LOGD(TAG, "%s entered", __func__); @@ -1203,7 +1228,7 @@ static void esp_netif_nd6_cb(struct netif *p_netif, uint8_t ip_idex) { ESP_LOGD(TAG, "%s lwip-netif:%p", __func__, p_netif); if (!p_netif) { - ESP_LOGD(TAG, "null p_netif=%p", p_netif); + ESP_LOGD(TAG, "esp_netif_nd6_cb called with null p_netif"); return; } @@ -1225,20 +1250,14 @@ static void esp_netif_nd6_cb(struct netif *p_netif, uint8_t ip_idex) if (ESP_OK != ret) { ESP_LOGE(TAG, "nd6 cb: failed to post IP_EVENT_GOT_IP6 (%x)", ret); } - } - static esp_err_t esp_netif_create_ip6_linklocal_api(esp_netif_api_msg_t *msg) { - return esp_netif_create_ip6_linklocal(msg->esp_netif); -} + esp_netif_t *esp_netif = msg->esp_netif; -esp_err_t esp_netif_create_ip6_linklocal(esp_netif_t *esp_netif) -{ ESP_LOGD(TAG, "%s esp-netif:%p", __func__, esp_netif); - ESP_NETIF_IPC_CALL(esp_netif, NULL, esp_netif_create_ip6_linklocal_api); struct netif *p_netif = esp_netif->lwip_netif; if (p_netif != NULL && netif_is_up(p_netif)) { netif_create_ip6_linklocal_address(p_netif, 1); @@ -1249,6 +1268,8 @@ esp_err_t esp_netif_create_ip6_linklocal(esp_netif_t *esp_netif) } } +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_get_ip6_linklocal(esp_netif_t *esp_netif, esp_ip6_addr_t *if_ip6) { ESP_LOGD(TAG, "%s esp-netif:%p", __func__, esp_netif); @@ -1433,7 +1454,7 @@ esp_err_t esp_netif_dhcpc_option(esp_netif_t *esp_netif, esp_netif_dhcp_option_m return ESP_ERR_NOT_SUPPORTED; } -int esp_netif_get_netif_index(esp_netif_t *esp_netif) +int esp_netif_get_netif_impl_index(esp_netif_t *esp_netif) { if (esp_netif == NULL || esp_netif->lwip_netif == NULL) { return -1; @@ -1441,4 +1462,4 @@ int esp_netif_get_netif_index(esp_netif_t *esp_netif) return netif_get_index(esp_netif->lwip_netif); } -#endif /* CONFIG_TCPIP_LWIP */ +#endif /* CONFIG_ESP_NETIF_TCPIP_LWIP */ diff --git a/components/esp_netif/lwip/esp_netif_lwip_internal.h b/components/esp_netif/lwip/esp_netif_lwip_internal.h index 181c3bf17c..46f2f123f7 100644 --- a/components/esp_netif/lwip/esp_netif_lwip_internal.h +++ b/components/esp_netif/lwip/esp_netif_lwip_internal.h @@ -46,25 +46,3 @@ typedef struct esp_netif_ip_lost_timer_s { bool timer_running; } esp_netif_ip_lost_timer_t; - -#define ESP_NETIF_TRHEAD_SAFE 1 -#define ESP_NETIF_IPC_LOCAL 0 -#define ESP_NETIF_IPC_REMOTE 1 - -#define ESP_NETIF_IPC_CALL(_if, _data, _fn) do {\ - esp_netif_api_msg_t msg;\ - if (tcpip_initialized == false) {\ - ESP_LOGE(TAG, "esp_netif is not initialized!");\ - abort();\ - }\ - memset(&msg, 0, sizeof(msg));\ - msg.esp_netif = (_if);\ - msg.data = (void*)(_data);\ - msg.api_fn = (_fn);\ - if (ESP_NETIF_IPC_REMOTE == esp_netif_ipc_check(&msg)) {\ - ESP_LOGD(TAG, "check: remote, if=%p fn=%p\n", (_if), (_fn));\ - return msg.ret;\ - } else {\ - ESP_LOGD(TAG, "check: local, if=%p fn=%p\n", (_if), (_fn));\ - }\ -} while(0) diff --git a/components/esp_netif/test/test_esp_netif.c b/components/esp_netif/test/test_esp_netif.c index 3eedd10c38..b2b4ddbcda 100644 --- a/components/esp_netif/test/test_esp_netif.c +++ b/components/esp_netif/test/test_esp_netif.c @@ -4,11 +4,14 @@ TEST_CASE("esp_netif: init and destroy", "[esp_netif][leaks=0]") { - esp_netif_config_t cfg = {}; + esp_netif_inherent_config_t base = {}; + const esp_netif_netstack_base_config_t stack = { .type = ESP_NETIF_NETWORK_STACK_IS_LWIP }; + esp_netif_config_t cfg = { .base = &base, .stack = (const esp_netif_netstack_config_t*)&stack }; esp_netif_t *esp_netif = esp_netif_new(NULL); - TEST_ASSERT_EQUAL(ESP_ERR_ESP_NETIF_INVALID_PARAMS, esp_netif_configure(esp_netif, NULL)); - TEST_ASSERT_EQUAL(ESP_OK, esp_netif_configure(esp_netif, &cfg)); + TEST_ASSERT_EQUAL(NULL, esp_netif); + esp_netif = esp_netif_new(&cfg); + TEST_ASSERT_NOT_EQUAL(NULL, esp_netif); esp_netif_destroy(esp_netif); } @@ -41,8 +44,8 @@ TEST_CASE("esp_netif: create and delete multiple netifs", "[esp_netif][leaks=0]" // create 10 wifi stations for (int i=0; i Date: Tue, 3 Sep 2019 16:26:19 +0200 Subject: [PATCH 12/31] esp_event: fix petty include dependency issue --- components/esp_event/event_loop_legacy.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/esp_event/event_loop_legacy.c b/components/esp_event/event_loop_legacy.c index 3b80387bfc..85e1b58ff6 100644 --- a/components/esp_event/event_loop_legacy.c +++ b/components/esp_event/event_loop_legacy.c @@ -14,8 +14,8 @@ #include "esp_err.h" #include "esp_log.h" -#include "esp_event_legacy.h" #include "esp_event.h" +#include "esp_event_legacy.h" #include "sdkconfig.h" From 3a19bf055d62d7cea4c7a62c8cc5f3b6a7e25b7c Mon Sep 17 00:00:00 2001 From: David Cermak Date: Wed, 4 Sep 2019 13:58:29 +0200 Subject: [PATCH 13/31] esp_netif and examples: using wifi driver handle, update examples and tests to pass the CI --- components/esp_eth/test/test_emac.c | 19 ++- components/esp_netif/esp_netif_defaults.c | 4 +- components/esp_netif/esp_netif_handlers.c | 2 +- components/esp_netif/include/esp_netif.h | 9 ++ .../esp_netif/include/esp_netif_defaults.h | 4 +- components/esp_netif/lwip/esp_netif_lwip.c | 38 +++++- components/esp_wifi/include/esp_wifi.h | 1 - .../esp_wifi/include/esp_wifi_default.h | 17 ++- components/esp_wifi/src/wifi_default.c | 127 +++++++++++------- components/esp_wifi/test/esp32/test_wifi.c | 3 + components/esp_wifi/test/test_wifi_init.c | 8 +- .../tcpip_adapter/include/tcpip_adapter.h | 6 + .../tcpip_adapter/tcpip_adapter_compat.c | 10 +- .../components/iperf/cmd_wifi.c | 2 +- .../protocol_examples_common/connect.c | 5 +- examples/ethernet/iperf/main/cmd_ethernet.c | 2 +- .../internal_communication/main/mesh_main.c | 7 +- .../mesh/manual_networking/main/mesh_main.c | 7 +- .../asio/chat_client/main/chat_client.cpp | 3 +- .../asio/chat_server/main/chat_server.cpp | 3 +- .../asio/tcp_echo_server/main/echo_server.cpp | 3 +- .../udp_echo_server/main/udp_echo_server.cpp | 3 +- .../components/modem/include/esp_modem.h | 1 + examples/wifi/iperf/main/cmd_wifi.c | 2 +- 24 files changed, 193 insertions(+), 93 deletions(-) diff --git a/components/esp_eth/test/test_emac.c b/components/esp_eth/test/test_emac.c index 47123db8ab..3bb6e555d6 100644 --- a/components/esp_eth/test/test_emac.c +++ b/components/esp_eth/test/test_emac.c @@ -4,7 +4,6 @@ #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "freertos/event_groups.h" -#include "tcpip_adapter.h" #include "unity.h" #include "test_utils.h" #include "esp_event.h" @@ -15,6 +14,9 @@ #include "lwip/sockets.h" #include "ping/ping_sock.h" +#define GOT_IP_BIT BIT(0) +#define ETHERNET_GET_IP_TIMEOUT_MS (20000) + static const char *TAG = "esp_eth_test"; #define ETH_START_BIT BIT(0) @@ -63,7 +65,6 @@ static void got_ip_event_handler(void *arg, esp_event_base_t event_base, EventGroupHandle_t eth_event_group = (EventGroupHandle_t)arg; ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data; const esp_netif_ip_info_t *ip_info = &event->ip_info; - ESP_LOGI(TAG, "Ethernet Got IP Address"); ESP_LOGI(TAG, "~~~~~~~~~~~"); ESP_LOGI(TAG, "ETHIP:" IPSTR, IP2STR(&ip_info->ip)); @@ -148,6 +149,8 @@ TEST_CASE("esp32 ethernet event test", "[ethernet][test_env=UT_T2_Ethernet]") EventGroupHandle_t eth_event_group = xEventGroupCreate(); TEST_ASSERT(eth_event_group != NULL); TEST_ESP_OK(esp_event_loop_create_default()); + esp_netif_config_t cfg = ESP_NETIF_DEFAULT_ETH(); + esp_netif_t* eth_netif = esp_netif_new(&cfg); TEST_ESP_OK(esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, ð_event_handler, eth_event_group)); eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG(); esp_eth_mac_t *mac = esp_eth_mac_new_esp32(&mac_config); @@ -156,6 +159,7 @@ TEST_CASE("esp32 ethernet event test", "[ethernet][test_env=UT_T2_Ethernet]") esp_eth_config_t eth_config = ETH_DEFAULT_CONFIG(mac, phy); esp_eth_handle_t eth_handle = NULL; TEST_ESP_OK(esp_eth_driver_install(ð_config, ð_handle)); + TEST_ESP_OK(esp_netif_attach(eth_netif, eth_handle)); /* wait for connection start */ bits = xEventGroupWaitBits(eth_event_group, ETH_START_BIT, true, true, pdMS_TO_TICKS(ETH_START_TIMEOUT_MS)); TEST_ASSERT((bits & ETH_START_BIT) == ETH_START_BIT); @@ -173,6 +177,7 @@ TEST_CASE("esp32 ethernet event test", "[ethernet][test_env=UT_T2_Ethernet]") TEST_ESP_OK(esp_event_handler_unregister(ETH_EVENT, ESP_EVENT_ANY_ID, eth_event_handler)); TEST_ESP_OK(esp_event_loop_delete_default()); vEventGroupDelete(eth_event_group); + esp_netif_destroy(eth_netif); } TEST_CASE("esp32 ethernet dhcp test", "[ethernet][test_env=UT_T2_Ethernet]") @@ -182,7 +187,10 @@ TEST_CASE("esp32 ethernet dhcp test", "[ethernet][test_env=UT_T2_Ethernet]") TEST_ASSERT(eth_event_group != NULL); test_case_uses_tcpip(); TEST_ESP_OK(esp_event_loop_create_default()); - TEST_ESP_OK(tcpip_adapter_set_default_eth_handlers()); + esp_netif_config_t cfg = ESP_NETIF_DEFAULT_ETH(); + esp_netif_t* eth_netif = esp_netif_new(&cfg); + TEST_ESP_OK(esp_eth_set_default_handlers(eth_netif)); + TEST_ESP_OK(esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, ð_event_handler, eth_event_group)); TEST_ESP_OK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &got_ip_event_handler, eth_event_group)); eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG(); @@ -192,6 +200,8 @@ TEST_CASE("esp32 ethernet dhcp test", "[ethernet][test_env=UT_T2_Ethernet]") esp_eth_config_t eth_config = ETH_DEFAULT_CONFIG(mac, phy); esp_eth_handle_t eth_handle = NULL; TEST_ESP_OK(esp_eth_driver_install(ð_config, ð_handle)); + TEST_ESP_OK(esp_netif_attach(eth_netif, eth_handle)); + /* wait for IP lease */ bits = xEventGroupWaitBits(eth_event_group, ETH_GOT_IP_BIT, true, true, pdMS_TO_TICKS(ETH_GET_IP_TIMEOUT_MS)); TEST_ASSERT((bits & ETH_GOT_IP_BIT) == ETH_GOT_IP_BIT); @@ -205,9 +215,10 @@ TEST_CASE("esp32 ethernet dhcp test", "[ethernet][test_env=UT_T2_Ethernet]") TEST_ESP_OK(mac->del(mac)); TEST_ESP_OK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_ETH_GOT_IP, got_ip_event_handler)); TEST_ESP_OK(esp_event_handler_unregister(ETH_EVENT, ESP_EVENT_ANY_ID, eth_event_handler)); - TEST_ESP_OK(tcpip_adapter_clear_default_eth_handlers()); + TEST_ESP_OK(esp_eth_clear_default_handlers(eth_netif)); TEST_ESP_OK(esp_event_loop_delete_default()); vEventGroupDelete(eth_event_group); + esp_netif_destroy(eth_netif); } TEST_CASE("esp32 ethernet icmp test", "[ethernet][test_env=UT_T2_Ethernet]") diff --git a/components/esp_netif/esp_netif_defaults.c b/components/esp_netif/esp_netif_defaults.c index d5d9fda825..67d2410b90 100644 --- a/components/esp_netif/esp_netif_defaults.c +++ b/components/esp_netif/esp_netif_defaults.c @@ -71,7 +71,7 @@ esp_netif_t* esp_netif_create_default_wifi_ap(void) esp_netif_config_t cfg = ESP_NETIF_DEFAULT_WIFI_AP(); esp_netif_t *netif = esp_netif_new(&cfg); assert(netif); - esp_wifi_set_default_wifi_ap_handlers(netif); + esp_wifi_set_default_wifi_driver_and_handlers(ESP_IF_WIFI_AP, netif); return netif; } @@ -80,7 +80,7 @@ esp_netif_t* esp_netif_create_default_wifi_sta(void) esp_netif_config_t cfg = ESP_NETIF_DEFAULT_WIFI_STA(); esp_netif_t *netif = esp_netif_new(&cfg); assert(netif); - esp_wifi_set_default_wifi_sta_handlers(netif); + esp_wifi_set_default_wifi_driver_and_handlers(ESP_IF_WIFI_STA, netif); return netif; } diff --git a/components/esp_netif/esp_netif_handlers.c b/components/esp_netif/esp_netif_handlers.c index 8aac2e79fa..8bad5f9117 100644 --- a/components/esp_netif/esp_netif_handlers.c +++ b/components/esp_netif/esp_netif_handlers.c @@ -108,7 +108,7 @@ void esp_netif_action_connected(void *esp_netif, esp_event_base_t base, int32_t void esp_netif_action_disconnected(void *esp_netif, esp_event_base_t base, int32_t event_id, void *data) { - ESP_LOGD(TAG, "esp_netif action connected with netif%p from event_id=%d", esp_netif, event_id); + ESP_LOGD(TAG, "esp_netif action disconnected with netif%p from event_id=%d", esp_netif, event_id); esp_netif_down(esp_netif); } diff --git a/components/esp_netif/include/esp_netif.h b/components/esp_netif/include/esp_netif.h index 1819b15b49..4d690c2756 100644 --- a/components/esp_netif/include/esp_netif.h +++ b/components/esp_netif/include/esp_netif.h @@ -531,6 +531,15 @@ void esp_netif_set_ip4_addr(esp_ip4_addr_t *addr, uint8_t a, uint8_t b, uint8_t */ char * esp_ip4addr_ntoa(const esp_ip4_addr_t *addr, char *buf, int buflen); +/** + * @brief Ascii internet address interpretation routine + * The value returned is in network order. + * + * @param addr IP address in ascii representation (e.g. "127.0.0.1") + * @return ip address in network order +*/ +uint32_t esp_ip4addr_aton(const char *addr); + // // 6) Driver conversion utilities to related keys, flags, implementation handle, list of netifs // diff --git a/components/esp_netif/include/esp_netif_defaults.h b/components/esp_netif/include/esp_netif_defaults.h index b7d7c5810b..c8b817b1fe 100644 --- a/components/esp_netif/include/esp_netif_defaults.h +++ b/components/esp_netif/include/esp_netif_defaults.h @@ -36,7 +36,7 @@ { \ .base = ESP_NETIF_BASE_DEFAULT_WIFI_AP, \ .stack = ESP_NETIF_NETSTACK_DEFAULT_WIFI_AP, \ - .driver = ESP_NETIF_DRIVER_DEFAULT_WIFI_AP, \ + .driver = NULL, \ } /** @@ -46,7 +46,7 @@ { \ .base = ESP_NETIF_BASE_DEFAULT_WIFI_STA, \ .stack = ESP_NETIF_NETSTACK_DEFAULT_WIFI_STA, \ - .driver = ESP_NETIF_DRIVER_DEFAULT_WIFI_STA, \ + .driver = NULL, \ } /** * @brief Default base config (esp-netif inherent) of WIFI STA diff --git a/components/esp_netif/lwip/esp_netif_lwip.c b/components/esp_netif/lwip/esp_netif_lwip.c index ee9b4e57fb..b8b8710c13 100644 --- a/components/esp_netif/lwip/esp_netif_lwip.c +++ b/components/esp_netif/lwip/esp_netif_lwip.c @@ -201,6 +201,12 @@ char * esp_ip4addr_ntoa(const esp_ip4_addr_t *addr, char *buf, int buflen) return ip4addr_ntoa_r((ip4_addr_t *)addr, buf, buflen); } +uint32_t esp_ip4addr_aton(const char *addr) +{ + return ipaddr_addr(addr); +} + + esp_netif_iodriver_handle esp_netif_get_io_driver(esp_netif_t *esp_netif) { return esp_netif->driver_handle; @@ -380,13 +386,25 @@ esp_netif_t *esp_netif_new(const esp_netif_config_t *esp_netif_config) return esp_netif; } +static void esp_netif_lwip_remove(esp_netif_t *esp_netif) +{ + if (esp_netif->lwip_netif) { + if (netif_is_up(esp_netif->lwip_netif)) { + netif_set_down(esp_netif->lwip_netif); + } + netif_remove(esp_netif->lwip_netif); + free(esp_netif->lwip_netif); + esp_netif->lwip_netif = NULL; + } +} + void esp_netif_destroy(esp_netif_t *esp_netif) { if (esp_netif) { esp_netif_remove_from_list(esp_netif); free(esp_netif->ip_info); free(esp_netif->ip_info_old); - free(esp_netif->lwip_netif); + esp_netif_lwip_remove(esp_netif); free(esp_netif->if_key); free(esp_netif); } @@ -536,7 +554,7 @@ static esp_err_t esp_netif_stop_api(esp_netif_api_msg_t *msg) } if (!netif_is_up(lwip_netif)) { - netif_remove(lwip_netif); + esp_netif_lwip_remove(esp_netif); return ESP_ERR_ESP_NETIF_IF_NOT_READY; } @@ -556,7 +574,7 @@ static esp_err_t esp_netif_stop_api(esp_netif_api_msg_t *msg) } netif_set_down(lwip_netif); - netif_remove(lwip_netif); + esp_netif_lwip_remove(esp_netif); esp_netif_update_default_netif(esp_netif, ESP_NETIF_STOPPED);; return ESP_OK; @@ -645,7 +663,19 @@ static void esp_netif_dhcpc_cb(struct netif *netif) static void esp_netif_ip_lost_timer(void *arg) { - esp_netif_t *esp_netif = (esp_netif_t*)arg; + esp_netif_t *esp_netif = NULL; + esp_netif_t *nif = esp_netif_next(NULL); + do { + if (nif && nif == arg) { + esp_netif = nif; + break; + } + } while (NULL != (nif = esp_netif_next(nif))); + + if (esp_netif == NULL) { + ESP_LOGD(TAG, "%s esp_netif not found in the list", __func__); + return; + } ESP_LOGD(TAG, "%s esp_netif:%p", __func__, esp_netif); diff --git a/components/esp_wifi/include/esp_wifi.h b/components/esp_wifi/include/esp_wifi.h index d638c9b3a0..6566abee72 100644 --- a/components/esp_wifi/include/esp_wifi.h +++ b/components/esp_wifi/include/esp_wifi.h @@ -63,7 +63,6 @@ #include "esp_wifi_types.h" #include "esp_event.h" #include "esp_private/esp_wifi_private.h" -#include "esp_wifi_default.h" #ifdef __cplusplus extern "C" { diff --git a/components/esp_wifi/include/esp_wifi_default.h b/components/esp_wifi/include/esp_wifi_default.h index 85b14ef029..e6cd40cffc 100644 --- a/components/esp_wifi/include/esp_wifi_default.h +++ b/components/esp_wifi/include/esp_wifi_default.h @@ -15,9 +15,6 @@ #ifndef _ESP_WIFI_DEFAULT_H #define _ESP_WIFI_DEFAULT_H -#define ESP_NETIF_DRIVER_DEFAULT_WIFI_STA &_g_wifi_driver_sta_ifconfig -#define ESP_NETIF_DRIVER_DEFAULT_WIFI_AP &_g_wifi_driver_ap_ifconfig - /** * @brief Sets default wifi event handlers for STA interface * @@ -36,11 +33,19 @@ esp_err_t esp_wifi_set_default_wifi_sta_handlers(void *esp_netif); * @return * - ESP_OK on success, error returned from esp_event_handler_register if failed */ +esp_err_t esp_wifi_set_default_wifi_driver_and_handlers(wifi_interface_t wifi_if, void *esp_netif); + esp_err_t esp_wifi_set_default_wifi_ap_handlers(void *esp_netif); - -extern const esp_netif_driver_ifconfig_t _g_wifi_driver_sta_ifconfig; -extern const esp_netif_driver_ifconfig_t _g_wifi_driver_ap_ifconfig; +/** + * @brief Clears default wifi event handlers for supplied network interface + * + * @param esp_netif instance of corresponding if object + * + * @return + * - ESP_OK on success, error returned from esp_event_handler_register if failed + */ +esp_err_t esp_wifi_clear_default_wifi_driver_and_handlers(void *esp_netif); #endif //_ESP_WIFI_DEFAULT_H diff --git a/components/esp_wifi/src/wifi_default.c b/components/esp_wifi/src/wifi_default.c index b23247c52f..9351172d6c 100644 --- a/components/esp_wifi/src/wifi_default.c +++ b/components/esp_wifi/src/wifi_default.c @@ -22,33 +22,37 @@ // Also this module holds esp-netif handles for AP and STA // +typedef struct wifi_netif_driver { + esp_netif_driver_base_t base; + wifi_interface_t wifi_if; +}* wifi_netif_driver_t; + static const char* TAG = "wifi_init_default"; -static esp_netif_t *sta_netif = NULL; -static esp_netif_t *ap_netif = NULL; -static bool wifi_default_handlers_set = false; +#define MAX_WIFI_IFS (2) +static esp_netif_t *s_wifi_netifs[MAX_WIFI_IFS] = { NULL }; +static bool wifi_default_handlers_set = false; static esp_err_t wifi_sta_receive(void *buffer, uint16_t len, void *eb) { - return esp_netif_receive(sta_netif, buffer, len, eb); + return esp_netif_receive(s_wifi_netifs[WIFI_IF_STA], buffer, len, eb); } static esp_err_t wifi_ap_receive(void *buffer, uint16_t len, void *eb) { - return esp_netif_receive(ap_netif, buffer, len, eb); + return esp_netif_receive(s_wifi_netifs[WIFI_IF_AP], buffer, len, eb); } - void wifi_free(void *h, void* buffer) { esp_wifi_internal_free_rx_buffer(buffer); } - esp_err_t wifi_transmit(void *h, void *buffer, size_t len) { - return esp_wifi_internal_tx((wifi_interface_t)h, buffer, len); + wifi_netif_driver_t driver = h; + return esp_wifi_internal_tx(driver->wifi_if, buffer, len); } void wifi_start(void *esp_netif, esp_event_base_t base, int32_t event_id, void *data) @@ -58,7 +62,8 @@ void wifi_start(void *esp_netif, esp_event_base_t base, int32_t event_id, void * uint8_t mac[6]; esp_err_t ret; - wifi_interface_t wifi_interface = (wifi_interface_t) esp_netif_get_io_driver(esp_netif); + wifi_netif_driver_t driver = esp_netif_get_io_driver(esp_netif); + wifi_interface_t wifi_interface = driver->wifi_if; if ((ret = esp_wifi_get_mac(wifi_interface, mac)) != ESP_OK) { ESP_LOGE(TAG, "esp_wifi_get_mac failed with %d", ret); @@ -81,21 +86,21 @@ void wifi_start(void *esp_netif, esp_event_base_t base, int32_t event_id, void * static void wifi_default_action_sta_start(void *arg, esp_event_base_t base, int32_t event_id, void *data) { - if (sta_netif != NULL) { - wifi_start(sta_netif, base, event_id, data); + if (s_wifi_netifs[WIFI_IF_STA] != NULL) { + wifi_start(s_wifi_netifs[WIFI_IF_STA], base, event_id, data); } } static void wifi_default_action_sta_stop(void *arg, esp_event_base_t base, int32_t event_id, void *data) { - if (sta_netif != NULL) { - esp_netif_action_stop(sta_netif, base, event_id, data); + if (s_wifi_netifs[WIFI_IF_STA] != NULL) { + esp_netif_action_stop(s_wifi_netifs[WIFI_IF_STA], base, event_id, data); } } static void wifi_default_action_sta_connected(void *arg, esp_event_base_t base, int32_t event_id, void *data) { - if (sta_netif != NULL) { + if (s_wifi_netifs[WIFI_IF_STA] != NULL) { esp_err_t ret; // By default register wifi rxcb once the STA gets connected if ((ret = esp_wifi_internal_reg_rxcb(ESP_IF_WIFI_STA, wifi_sta_receive)) != ESP_OK) { @@ -103,40 +108,40 @@ static void wifi_default_action_sta_connected(void *arg, esp_event_base_t base, return; } - esp_netif_action_connected(sta_netif, base, event_id, data); + esp_netif_action_connected(s_wifi_netifs[WIFI_IF_STA], base, event_id, data); } } static void wifi_default_action_sta_disconnected(void *arg, esp_event_base_t base, int32_t event_id, void *data) { - if (sta_netif != NULL) { - esp_netif_action_disconnected(sta_netif, base, event_id, data); + if (s_wifi_netifs[WIFI_IF_STA] != NULL) { + esp_netif_action_disconnected(s_wifi_netifs[WIFI_IF_STA], base, event_id, data); } } static void wifi_default_action_ap_start(void *arg, esp_event_base_t base, int32_t event_id, void *data) { - if (ap_netif != NULL) { - wifi_start(ap_netif, base, event_id, data); + if (s_wifi_netifs[WIFI_IF_AP] != NULL) { + wifi_start(s_wifi_netifs[WIFI_IF_AP], base, event_id, data); } } static void wifi_default_action_ap_stop(void *arg, esp_event_base_t base, int32_t event_id, void *data) { - if (ap_netif != NULL) { - esp_netif_action_stop(ap_netif, base, event_id, data); + if (s_wifi_netifs[WIFI_IF_AP] != NULL) { + esp_netif_action_stop(s_wifi_netifs[WIFI_IF_AP], base, event_id, data); } } static void wifi_default_action_sta_got_ip(void *arg, esp_event_base_t base, int32_t event_id, void *data) { - if (sta_netif != NULL) { + if (s_wifi_netifs[WIFI_IF_STA] != NULL) { ESP_LOGD(TAG, "Got IP wifi default handler entered"); int ret = esp_wifi_internal_set_sta_ip(); if (ret != ESP_OK) { ESP_LOGI(TAG, "esp_wifi_internal_set_sta_ip failed with %d", ret); } - esp_netif_action_got_ip(sta_netif, base, event_id, data); + esp_netif_action_got_ip(s_wifi_netifs[WIFI_IF_STA], base, event_id, data); } } @@ -154,16 +159,40 @@ esp_err_t esp_wifi_clear_default_wifi_handlers(void) return ESP_OK; } -void _esp_wifi_set_default_ap_netif(esp_netif_t* esp_netif) +static esp_err_t wifi_driver_start(esp_netif_t * esp_netif, void * args) { - assert(esp_netif); - ap_netif = esp_netif; + wifi_netif_driver_t driver = args; + driver->base.netif = esp_netif; + esp_netif_driver_ifconfig_t driver_ifconfig = { + .handle = driver, + .transmit = wifi_transmit, + .driver_free_rx_buffer = wifi_free + }; + + return esp_netif_set_driver_config(esp_netif, &driver_ifconfig); } -void _esp_wifi_set_default_sta_netif(esp_netif_t* esp_netif) +static esp_err_t disconnect_and_destroy(esp_netif_t* esp_netif) { - assert(esp_netif); - sta_netif = esp_netif; + wifi_netif_driver_t driver = esp_netif_get_io_driver(esp_netif); + driver->base.netif = NULL; + esp_netif_driver_ifconfig_t driver_ifconfig = { }; + esp_err_t ret = esp_netif_set_driver_config(esp_netif, &driver_ifconfig); + free(driver); + return ret; +} + +static esp_err_t create_and_attach(wifi_interface_t wifi_if, esp_netif_t* esp_netif) +{ + wifi_netif_driver_t driver = calloc(1, sizeof(struct wifi_netif_driver)); + if (driver == NULL) { + ESP_LOGE(TAG, "No memory to create a wifi driver"); + return ESP_ERR_NO_MEM; + } + driver->wifi_if = wifi_if; + driver->base.post_attach = wifi_driver_start; + esp_netif_attach(esp_netif, driver); + return ESP_OK; } esp_err_t _esp_wifi_set_default_wifi_handlers(void) @@ -219,26 +248,32 @@ fail: return err; } -esp_err_t esp_wifi_set_default_wifi_sta_handlers(void *esp_netif) +esp_err_t esp_wifi_set_default_wifi_driver_and_handlers(wifi_interface_t wifi_if, void *esp_netif) { - _esp_wifi_set_default_sta_netif(esp_netif); + assert(esp_netif); + assert(wifi_if < MAX_WIFI_IFS); + s_wifi_netifs[wifi_if] = esp_netif; + ESP_ERROR_CHECK(create_and_attach(wifi_if, esp_netif)); return _esp_wifi_set_default_wifi_handlers(); } -esp_err_t esp_wifi_set_default_wifi_ap_handlers(void *esp_netif) +esp_err_t esp_wifi_clear_default_wifi_driver_and_handlers(void *esp_netif) { - _esp_wifi_set_default_ap_netif(esp_netif); - return _esp_wifi_set_default_wifi_handlers(); + int i; + for (i = 0; i< MAX_WIFI_IFS; ++i) { + // clear internal static pointers to netifs + if (s_wifi_netifs[i] == esp_netif) { + s_wifi_netifs[i] = NULL; + } + // check if all netifs are cleared to delete default handlers + if (s_wifi_netifs[i] != NULL) { + break; + } + } + + if (i == MAX_WIFI_IFS) { // if all wifi default netifs are null + ESP_LOGD(TAG, "Clearing wifi default handlers"); + esp_wifi_clear_default_wifi_handlers(); + } + return disconnect_and_destroy(esp_netif); } - -const esp_netif_driver_ifconfig_t _g_wifi_driver_sta_ifconfig = { - .handle = (void*)WIFI_IF_STA, - .transmit = wifi_transmit, - .driver_free_rx_buffer = wifi_free, -}; - -const esp_netif_driver_ifconfig_t _g_wifi_driver_ap_ifconfig = { - .handle = (void*)WIFI_IF_AP, - .transmit = wifi_transmit, - .driver_free_rx_buffer = wifi_free, -}; diff --git a/components/esp_wifi/test/esp32/test_wifi.c b/components/esp_wifi/test/esp32/test_wifi.c index 1a467baaee..888ed18787 100644 --- a/components/esp_wifi/test/esp32/test_wifi.c +++ b/components/esp_wifi/test/esp32/test_wifi.c @@ -80,6 +80,9 @@ static esp_err_t event_init(void) ESP_ERROR_CHECK(esp_event_loop_create_default()); ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler, NULL)); ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, ESP_EVENT_ANY_ID, &ip_event_handler, NULL)); + esp_netif_create_default_wifi_sta(); + esp_netif_create_default_wifi_ap(); + return ESP_OK; } diff --git a/components/esp_wifi/test/test_wifi_init.c b/components/esp_wifi/test/test_wifi_init.c index 0fcc6a4ec6..d59be611df 100644 --- a/components/esp_wifi/test/test_wifi_init.c +++ b/components/esp_wifi/test/test_wifi_init.c @@ -47,7 +47,7 @@ static void ip_event_handler(void* arg, esp_event_base_t event_base, int32_t eve case IP_EVENT_STA_GOT_IP: event = (ip_event_got_ip_t*)event_data; ESP_LOGI(TAG, "IP_EVENT_STA_GOT_IP"); - ESP_LOGI(TAG, "got ip:%s\n", ip4addr_ntoa(&event->ip_info.ip)); + ESP_LOGI(TAG, "got ip:" IPSTR, IP2STR(&event->ip_info.ip)); if (wifi_events) { xEventGroupSetBits(wifi_events, GOT_IP_EVENT); } @@ -123,9 +123,9 @@ static void wifi_start_stop_task(void* arg) r = nvs_flash_init(); } TEST_ESP_OK(r); - //init tcpip - ESP_LOGI(TAG, EMPH_STR("tcpip_adapter_init")); - tcpip_adapter_init(); + //init tcpip stack + ESP_LOGI(TAG, EMPH_STR("esp_netif_init")); + esp_netif_init(); //init event loop ESP_LOGI(TAG, EMPH_STR("event_init")); event_init(); diff --git a/components/tcpip_adapter/include/tcpip_adapter.h b/components/tcpip_adapter/include/tcpip_adapter.h index 3854a13a90..90d0d6ae0f 100644 --- a/components/tcpip_adapter/include/tcpip_adapter.h +++ b/components/tcpip_adapter/include/tcpip_adapter.h @@ -106,6 +106,12 @@ esp_err_t tcpip_adapter_eth_input(void *buffer, uint16_t len, void *eb); */ esp_err_t tcpip_adapter_clear_default_wifi_handlers(void); +/** + * @brief Compatible version of former tcpip_adapter API to clear default ethernet handlers + * @return ESP_OK on success + */ +esp_err_t tcpip_adapter_clear_default_eth_handlers(void); + /** * @brief Compatible version of former tcpip_adapter API of esp_netif_dhcps_stop */ diff --git a/components/tcpip_adapter/tcpip_adapter_compat.c b/components/tcpip_adapter/tcpip_adapter_compat.c index 769fd3384d..5efc1574de 100644 --- a/components/tcpip_adapter/tcpip_adapter_compat.c +++ b/components/tcpip_adapter/tcpip_adapter_compat.c @@ -21,6 +21,7 @@ #include "esp_eth.h" #include "tcpip_adapter_types.h" +#include "esp_wifi_default.h" extern void _esp_wifi_set_default_ap_netif(esp_netif_t* esp_netif); extern void _esp_wifi_set_default_sta_netif(esp_netif_t* esp_netif); @@ -52,7 +53,7 @@ static void wifi_create_and_start_ap(void *esp_netif, esp_event_base_t base, int esp_netif_config_t cfg = ESP_NETIF_DEFAULT_WIFI_AP(); esp_netif_t *ap_netif = esp_netif_new(&cfg); - _esp_wifi_set_default_ap_netif(ap_netif); + esp_wifi_set_default_wifi_driver_and_handlers(ESP_IF_WIFI_AP, ap_netif); s_esp_netifs[TCPIP_ADAPTER_IF_AP] = ap_netif; } } @@ -63,7 +64,7 @@ static void wifi_create_and_start_sta(void *esp_netif, esp_event_base_t base, in esp_netif_config_t cfg = ESP_NETIF_DEFAULT_WIFI_STA(); esp_netif_t *sta_netif = esp_netif_new(&cfg); - _esp_wifi_set_default_sta_netif(sta_netif); + esp_wifi_set_default_wifi_driver_and_handlers(ESP_IF_WIFI_STA, sta_netif); s_esp_netifs[TCPIP_ADAPTER_IF_STA] = sta_netif; } } @@ -104,6 +105,11 @@ static void tcpip_adapter_eth_start(void *esp_netif, esp_event_base_t base, int3 esp_netif_attach(esp_netif, eth_handle); } +esp_err_t tcpip_adapter_clear_default_eth_handlers(void) +{ + return esp_eth_clear_default_handlers(netif_from_if(TCPIP_ADAPTER_IF_ETH)); +} + esp_err_t tcpip_adapter_set_default_eth_handlers(void) { if (s_tcpip_adapter_compat) { diff --git a/examples/bluetooth/esp_ble_mesh/ble_mesh_wifi_coexist/components/iperf/cmd_wifi.c b/examples/bluetooth/esp_ble_mesh/ble_mesh_wifi_coexist/components/iperf/cmd_wifi.c index a4e060ec8a..b082e153a5 100644 --- a/examples/bluetooth/esp_ble_mesh/ble_mesh_wifi_coexist/components/iperf/cmd_wifi.c +++ b/examples/bluetooth/esp_ble_mesh/ble_mesh_wifi_coexist/components/iperf/cmd_wifi.c @@ -335,7 +335,7 @@ static int wifi_cmd_iperf(int argc, char **argv) if (iperf_args.ip->count == 0) { cfg.flag |= IPERF_FLAG_SERVER; } else { - cfg.dip = ipaddr_addr(iperf_args.ip->sval[0]); + cfg.dip = esp_ip4addr_aton(iperf_args.ip->sval[0]); cfg.flag |= IPERF_FLAG_CLIENT; } diff --git a/examples/common_components/protocol_examples_common/connect.c b/examples/common_components/protocol_examples_common/connect.c index b261e0b4f9..f9b974fba4 100644 --- a/examples/common_components/protocol_examples_common/connect.c +++ b/examples/common_components/protocol_examples_common/connect.c @@ -12,6 +12,7 @@ #include "sdkconfig.h" #include "esp_event.h" #include "esp_wifi.h" +#include "esp_wifi_default.h" #if CONFIG_EXAMPLE_CONNECT_ETHERNET #include "esp_eth.h" #endif @@ -136,7 +137,7 @@ static void start(void) assert(netif); - esp_wifi_set_default_wifi_sta_handlers(netif); + esp_wifi_set_default_wifi_driver_and_handlers(ESP_IF_WIFI_STA, netif); s_example_esp_netif = netif; @@ -176,7 +177,7 @@ static void stop(void) } ESP_ERROR_CHECK(err); ESP_ERROR_CHECK(esp_wifi_deinit()); - + ESP_ERROR_CHECK(esp_wifi_clear_default_wifi_driver_and_handlers(s_example_esp_netif)); esp_netif_destroy(s_example_esp_netif); s_example_esp_netif = NULL; } diff --git a/examples/ethernet/iperf/main/cmd_ethernet.c b/examples/ethernet/iperf/main/cmd_ethernet.c index 33dfba6ead..d4ae74b768 100644 --- a/examples/ethernet/iperf/main/cmd_ethernet.c +++ b/examples/ethernet/iperf/main/cmd_ethernet.c @@ -95,7 +95,7 @@ static int eth_cmd_iperf(int argc, char **argv) } /* iperf -c SERVER_ADDRESS */ else { - cfg.dip = ipaddr_addr(iperf_args.ip->sval[0]); + cfg.dip = esp_ip4addr_aton(iperf_args.ip->sval[0]); cfg.flag |= IPERF_FLAG_CLIENT; } diff --git a/examples/mesh/internal_communication/main/mesh_main.c b/examples/mesh/internal_communication/main/mesh_main.c index 7a0b6d0a9f..2e564551bb 100644 --- a/examples/mesh/internal_communication/main/mesh_main.c +++ b/examples/mesh/internal_communication/main/mesh_main.c @@ -8,6 +8,7 @@ */ #include #include "esp_wifi.h" +#include "esp_wifi_default.h" #include "esp_system.h" #include "esp_event.h" #include "esp_log.h" @@ -384,11 +385,10 @@ static esp_err_t create_wifi_netifs(void) esp_netif_config_t cfg_ap = { .base = &netif_cfg, .stack = ESP_NETIF_NETSTACK_DEFAULT_WIFI_AP, - .driver = ESP_NETIF_DRIVER_DEFAULT_WIFI_AP, }; esp_netif_t *netif_ap = esp_netif_new(&cfg_ap); assert(netif_ap); - ESP_ERROR_CHECK(esp_wifi_set_default_wifi_ap_handlers(netif_ap)); + ESP_ERROR_CHECK(esp_wifi_set_default_wifi_driver_and_handlers(ESP_IF_WIFI_AP, netif_ap)); memcpy(&netif_cfg, ESP_NETIF_BASE_DEFAULT_WIFI_STA, sizeof(netif_cfg)); @@ -396,12 +396,11 @@ static esp_err_t create_wifi_netifs(void) esp_netif_config_t cfg_sta = { .base = &netif_cfg, .stack = ESP_NETIF_NETSTACK_DEFAULT_WIFI_STA, - .driver = ESP_NETIF_DRIVER_DEFAULT_WIFI_STA, }; netif_sta = esp_netif_new(&cfg_sta); assert(netif_sta); - ESP_ERROR_CHECK(esp_wifi_set_default_wifi_sta_handlers(netif_sta)); + ESP_ERROR_CHECK(esp_wifi_set_default_wifi_driver_and_handlers(ESP_IF_WIFI_STA, netif_sta)); return ESP_OK; } diff --git a/examples/mesh/manual_networking/main/mesh_main.c b/examples/mesh/manual_networking/main/mesh_main.c index 3c2bd255b4..c4c481b09d 100644 --- a/examples/mesh/manual_networking/main/mesh_main.c +++ b/examples/mesh/manual_networking/main/mesh_main.c @@ -8,6 +8,7 @@ */ #include #include "esp_wifi.h" +#include "esp_wifi_default.h" #include "esp_system.h" #include "esp_event.h" #include "esp_log.h" @@ -308,11 +309,10 @@ static esp_err_t create_wifi_netifs(void) esp_netif_config_t cfg_ap = { .base = &netif_cfg, .stack = ESP_NETIF_NETSTACK_DEFAULT_WIFI_AP, - .driver = ESP_NETIF_DRIVER_DEFAULT_WIFI_AP, }; esp_netif_t *netif_ap = esp_netif_new(&cfg_ap); assert(netif_ap); - ESP_ERROR_CHECK(esp_wifi_set_default_wifi_ap_handlers(netif_ap)); + ESP_ERROR_CHECK(esp_wifi_set_default_wifi_driver_and_handlers(ESP_IF_WIFI_AP, netif_ap)); memcpy(&netif_cfg, ESP_NETIF_BASE_DEFAULT_WIFI_STA, sizeof(netif_cfg)); @@ -320,12 +320,11 @@ static esp_err_t create_wifi_netifs(void) esp_netif_config_t cfg_sta = { .base = &netif_cfg, .stack = ESP_NETIF_NETSTACK_DEFAULT_WIFI_STA, - .driver = ESP_NETIF_DRIVER_DEFAULT_WIFI_STA, }; netif_sta = esp_netif_new(&cfg_sta); assert(netif_sta); - ESP_ERROR_CHECK(esp_wifi_set_default_wifi_sta_handlers(netif_sta)); + ESP_ERROR_CHECK(esp_wifi_set_default_wifi_driver_and_handlers(ESP_IF_WIFI_STA, netif_sta)); return ESP_OK; } diff --git a/examples/protocols/asio/chat_client/main/chat_client.cpp b/examples/protocols/asio/chat_client/main/chat_client.cpp index 0b0f7097f0..10ab860c9d 100644 --- a/examples/protocols/asio/chat_client/main/chat_client.cpp +++ b/examples/protocols/asio/chat_client/main/chat_client.cpp @@ -16,7 +16,6 @@ #include "chat_message.hpp" #include "protocol_examples_common.h" #include "esp_event.h" -#include "tcpip_adapter.h" #include "nvs_flash.h" using asio::ip::tcp; @@ -137,7 +136,7 @@ void read_line(char * line, int max_chars); extern "C" void app_main(void) { ESP_ERROR_CHECK(nvs_flash_init()); - tcpip_adapter_init(); + esp_netif_init(); ESP_ERROR_CHECK(esp_event_loop_create_default()); /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig. diff --git a/examples/protocols/asio/chat_server/main/chat_server.cpp b/examples/protocols/asio/chat_server/main/chat_server.cpp index c1838b53f0..edcc1278c4 100644 --- a/examples/protocols/asio/chat_server/main/chat_server.cpp +++ b/examples/protocols/asio/chat_server/main/chat_server.cpp @@ -19,7 +19,6 @@ #include "chat_message.hpp" #include "protocol_examples_common.h" #include "esp_event.h" -#include "tcpip_adapter.h" #include "nvs_flash.h" @@ -205,7 +204,7 @@ private: extern "C" void app_main(void) { ESP_ERROR_CHECK(nvs_flash_init()); - tcpip_adapter_init(); + esp_netif_init(); ESP_ERROR_CHECK(esp_event_loop_create_default()); /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig. diff --git a/examples/protocols/asio/tcp_echo_server/main/echo_server.cpp b/examples/protocols/asio/tcp_echo_server/main/echo_server.cpp index d1ed69e05f..1770fa55ec 100644 --- a/examples/protocols/asio/tcp_echo_server/main/echo_server.cpp +++ b/examples/protocols/asio/tcp_echo_server/main/echo_server.cpp @@ -3,7 +3,6 @@ #include #include "protocol_examples_common.h" #include "esp_event.h" -#include "tcpip_adapter.h" #include "nvs_flash.h" using asio::ip::tcp; @@ -87,7 +86,7 @@ private: extern "C" void app_main(void) { ESP_ERROR_CHECK(nvs_flash_init()); - tcpip_adapter_init(); + esp_netif_init(); ESP_ERROR_CHECK(esp_event_loop_create_default()); /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig. diff --git a/examples/protocols/asio/udp_echo_server/main/udp_echo_server.cpp b/examples/protocols/asio/udp_echo_server/main/udp_echo_server.cpp index 68adf41482..14eef948e3 100644 --- a/examples/protocols/asio/udp_echo_server/main/udp_echo_server.cpp +++ b/examples/protocols/asio/udp_echo_server/main/udp_echo_server.cpp @@ -15,7 +15,6 @@ #include "protocol_examples_common.h" #include "esp_event.h" -#include "tcpip_adapter.h" #include "nvs_flash.h" @@ -69,7 +68,7 @@ private: extern "C" void app_main(void) { ESP_ERROR_CHECK(nvs_flash_init()); - tcpip_adapter_init(); + esp_netif_init(); ESP_ERROR_CHECK(esp_event_loop_create_default()); /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig. diff --git a/examples/protocols/pppos_client/components/modem/include/esp_modem.h b/examples/protocols/pppos_client/components/modem/include/esp_modem.h index 8ed0b4ba55..f2da7aeb08 100644 --- a/examples/protocols/pppos_client/components/modem/include/esp_modem.h +++ b/examples/protocols/pppos_client/components/modem/include/esp_modem.h @@ -21,6 +21,7 @@ extern "C" { #include "esp_modem_dte.h" #include "esp_event.h" #include "driver/uart.h" +#include "lwip/ip_addr.h" /** * @brief Declare Event Base for ESP Modem diff --git a/examples/wifi/iperf/main/cmd_wifi.c b/examples/wifi/iperf/main/cmd_wifi.c index 6710dcdee1..5da2736da1 100644 --- a/examples/wifi/iperf/main/cmd_wifi.c +++ b/examples/wifi/iperf/main/cmd_wifi.c @@ -315,7 +315,7 @@ static int wifi_cmd_iperf(int argc, char** argv) if (iperf_args.ip->count == 0) { cfg.flag |= IPERF_FLAG_SERVER; } else { - cfg.dip = ipaddr_addr(iperf_args.ip->sval[0]); + cfg.dip = esp_ip4addr_aton(iperf_args.ip->sval[0]); cfg.flag |= IPERF_FLAG_CLIENT; } From eb94d87935aa0e8da563be3ab75f613539ae91a1 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Fri, 6 Sep 2019 11:28:04 +0200 Subject: [PATCH 14/31] esp_netif: address failures on tcpip-task ipc call, deinit lwip netif and ip address issues --- components/esp_common/src/esp_err_to_name.c | 43 ++++----- components/esp_eth/src/esp_eth.c | 3 +- components/esp_event/event_send_compat.inc | 2 +- components/esp_netif/lwip/esp_netif_lwip.c | 98 +++++++++++++++------ 4 files changed, 94 insertions(+), 52 deletions(-) diff --git a/components/esp_common/src/esp_err_to_name.c b/components/esp_common/src/esp_err_to_name.c index 06fbd1358a..97d12112a9 100644 --- a/components/esp_common/src/esp_err_to_name.c +++ b/components/esp_common/src/esp_err_to_name.c @@ -23,6 +23,9 @@ #if __has_include("esp_mesh.h") #include "esp_mesh.h" #endif +#if __has_include("esp_netif_types.h") +#include "esp_netif_types.h" +#endif #if __has_include("esp_now.h") #include "esp_now.h" #endif @@ -50,9 +53,6 @@ #if __has_include("nvs.h") #include "nvs.h" #endif -#if __has_include("esp_netif.h") -#include "esp_netif.h" -#endif #if __has_include("ulp_common.h") #include "ulp_common.h" #endif @@ -454,30 +454,33 @@ static const esp_err_msg_t esp_err_msg_table[] = { # ifdef ESP_ERR_MESH_VOTING ERR_TBL_IT(ESP_ERR_MESH_VOTING), /* 16406 0x4016 */ # endif - // components/tcpip_adapter/include/tcpip_adapter.h -# ifdef ESP_ERR_TCPIP_ADAPTER_BASE - ERR_TBL_IT(ESP_ERR_TCPIP_ADAPTER_BASE), /* 20480 0x5000 */ + // components/esp_netif/include/esp_netif_types.h +# ifdef ESP_ERR_ESP_NETIF_BASE + ERR_TBL_IT(ESP_ERR_ESP_NETIF_BASE), /* 20480 0x5000 */ # endif -# ifdef ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS - ERR_TBL_IT(ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS), /* 20481 0x5001 */ +# ifdef ESP_ERR_ESP_NETIF_INVALID_PARAMS + ERR_TBL_IT(ESP_ERR_ESP_NETIF_INVALID_PARAMS), /* 20481 0x5001 */ # endif -# ifdef ESP_ERR_TCPIP_ADAPTER_IF_NOT_READY - ERR_TBL_IT(ESP_ERR_TCPIP_ADAPTER_IF_NOT_READY), /* 20482 0x5002 */ +# ifdef ESP_ERR_ESP_NETIF_IF_NOT_READY + ERR_TBL_IT(ESP_ERR_ESP_NETIF_IF_NOT_READY), /* 20482 0x5002 */ # endif -# ifdef ESP_ERR_TCPIP_ADAPTER_DHCPC_START_FAILED - ERR_TBL_IT(ESP_ERR_TCPIP_ADAPTER_DHCPC_START_FAILED), /* 20483 0x5003 */ +# ifdef ESP_ERR_ESP_NETIF_DHCPC_START_FAILED + ERR_TBL_IT(ESP_ERR_ESP_NETIF_DHCPC_START_FAILED), /* 20483 0x5003 */ # endif -# ifdef ESP_ERR_TCPIP_ADAPTER_DHCP_ALREADY_STARTED - ERR_TBL_IT(ESP_ERR_TCPIP_ADAPTER_DHCP_ALREADY_STARTED), /* 20484 0x5004 */ +# ifdef ESP_ERR_ESP_NETIF_DHCP_ALREADY_STARTED + ERR_TBL_IT(ESP_ERR_ESP_NETIF_DHCP_ALREADY_STARTED), /* 20484 0x5004 */ # endif -# ifdef ESP_ERR_TCPIP_ADAPTER_DHCP_ALREADY_STOPPED - ERR_TBL_IT(ESP_ERR_TCPIP_ADAPTER_DHCP_ALREADY_STOPPED), /* 20485 0x5005 */ +# ifdef ESP_ERR_ESP_NETIF_DHCP_ALREADY_STOPPED + ERR_TBL_IT(ESP_ERR_ESP_NETIF_DHCP_ALREADY_STOPPED), /* 20485 0x5005 */ # endif -# ifdef ESP_ERR_TCPIP_ADAPTER_NO_MEM - ERR_TBL_IT(ESP_ERR_TCPIP_ADAPTER_NO_MEM), /* 20486 0x5006 */ +# ifdef ESP_ERR_ESP_NETIF_NO_MEM + ERR_TBL_IT(ESP_ERR_ESP_NETIF_NO_MEM), /* 20486 0x5006 */ # endif -# ifdef ESP_ERR_TCPIP_ADAPTER_DHCP_NOT_STOPPED - ERR_TBL_IT(ESP_ERR_TCPIP_ADAPTER_DHCP_NOT_STOPPED), /* 20487 0x5007 */ +# ifdef ESP_ERR_ESP_NETIF_DHCP_NOT_STOPPED + ERR_TBL_IT(ESP_ERR_ESP_NETIF_DHCP_NOT_STOPPED), /* 20487 0x5007 */ +# endif +# ifdef ESP_ERR_ESP_NETIF_DRIVER_ATACH_FAILED + ERR_TBL_IT(ESP_ERR_ESP_NETIF_DRIVER_ATACH_FAILED), /* 20488 0x5008 */ # endif // components/esp_common/include/esp_err.h # ifdef ESP_ERR_FLASH_BASE diff --git a/components/esp_eth/src/esp_eth.c b/components/esp_eth/src/esp_eth.c index b1e36839aa..475becb45f 100644 --- a/components/esp_eth/src/esp_eth.c +++ b/components/esp_eth/src/esp_eth.c @@ -159,12 +159,12 @@ static void eth_check_link_timer_cb(TimerHandle_t xTimer) static esp_err_t esp_eth_driver_start(esp_netif_t * esp_netif, void * args) { + uint8_t eth_mac[6]; esp_err_t ret = ESP_OK; esp_eth_driver_t *eth_driver = args; eth_driver->base.netif = esp_netif; // Set driver related config to esp-netif - esp_netif_driver_ifconfig_t driver_ifconfig = { .handle = eth_driver, .transmit = esp_eth_transmit, @@ -172,7 +172,6 @@ static esp_err_t esp_eth_driver_start(esp_netif_t * esp_netif, void * args) }; ESP_ERROR_CHECK(esp_netif_set_driver_config(esp_netif, &driver_ifconfig)); - uint8_t eth_mac[6]; esp_eth_ioctl(eth_driver, ETH_CMD_G_MAC_ADDR, eth_mac); ESP_LOGI(TAG, "%x %x %x %x %x %x", eth_mac[0], eth_mac[1], eth_mac[2], eth_mac[3], eth_mac[4], eth_mac[5]); diff --git a/components/esp_event/event_send_compat.inc b/components/esp_event/event_send_compat.inc index 7e1e59ef86..57abb34cbb 100644 --- a/components/esp_event/event_send_compat.inc +++ b/components/esp_event/event_send_compat.inc @@ -272,7 +272,7 @@ static void esp_system_event_debug(const system_event_t* event) } case SYSTEM_EVENT_GOT_IP6: { const esp_ip6_addr_t *addr = &event->event_info.got_ip6.ip6_info.ip; - ESP_LOGD(TAG, "SYSTEM_EVENT_AP_STA_GOT_IP6 address %04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x", IPV62STR(*addr)); + ESP_LOGD(TAG, "SYSTEM_EVENT_AP_STA_GOT_IP6 address " IPV6STR, IPV62STR(*addr)); break; } #if CONFIG_IDF_TARGET_ESP32 diff --git a/components/esp_netif/lwip/esp_netif_lwip.c b/components/esp_netif/lwip/esp_netif_lwip.c index b8b8710c13..1fc0e3c815 100644 --- a/components/esp_netif/lwip/esp_netif_lwip.c +++ b/components/esp_netif/lwip/esp_netif_lwip.c @@ -135,7 +135,7 @@ static inline esp_err_t esp_netif_lwip_ipc_call(esp_netif_api_fn fn, esp_netif_t .data = data, .api_fn = fn }; - if (g_lwip_task == xTaskGetCurrentTaskHandle()) { + if (g_lwip_task != xTaskGetCurrentTaskHandle()) { ESP_LOGD(TAG, "check: remote, if=%p fn=%p\n", netif, fn); sys_arch_sem_wait(&api_lock_sem, 0); tcpip_send_msg_wait_sem((tcpip_callback_fn)esp_netif_api_cb, &msg, &api_sync_sem); @@ -146,6 +146,28 @@ static inline esp_err_t esp_netif_lwip_ipc_call(esp_netif_api_fn fn, esp_netif_t return fn(&msg); } +/** + * @brief Check if supplied esp_netif handle is active, i.e. available within registered interfaces + * as it might have already been destroyed. Returns the supplied handle if active, nullptr otherwise + * + * @param esp_netif handle to check if available in the list of registered interfaces + * @return esp_netif handle if available, or NULL if it wasn't found + */ +static esp_netif_t* esp_netif_is_active(esp_netif_t *arg) +{ + esp_netif_t *esp_netif = NULL; + // looking for the netif in the list of registered interfaces + // as it might have already been destroyed + esp_netif_t *nif = esp_netif_next(NULL); + do { + if (nif && nif == arg) { + esp_netif = nif; + break; + } + } while (NULL != (nif = esp_netif_next(nif))); + return esp_netif; +} + /** * @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 @@ -157,6 +179,9 @@ static inline esp_err_t esp_netif_lwip_ipc_call(esp_netif_api_fn fn, esp_netif_t static void esp_netif_update_default_netif(esp_netif_t *esp_netif, esp_netif_action_t action) { switch (action) { case ESP_NETIF_STARTED: + { + // check if previously default interface hasn't been destroyed in the meantime + 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); @@ -164,7 +189,9 @@ static void esp_netif_update_default_netif(esp_netif_t *esp_netif, esp_netif_act s_last_default_esp_netif = esp_netif; netif_set_default(s_last_default_esp_netif->lwip_netif); } - break; + } + break; + default: case ESP_NETIF_STOPPED: { @@ -186,7 +213,8 @@ static void esp_netif_update_default_netif(esp_netif_t *esp_netif, esp_netif_act 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); } - } break; + } + break; } } @@ -393,19 +421,40 @@ static void esp_netif_lwip_remove(esp_netif_t *esp_netif) netif_set_down(esp_netif->lwip_netif); } netif_remove(esp_netif->lwip_netif); - free(esp_netif->lwip_netif); - esp_netif->lwip_netif = NULL; } } +static esp_err_t esp_netif_lwip_add(esp_netif_t *esp_netif) +{ + if (esp_netif->lwip_netif == NULL) { + esp_netif->lwip_netif = calloc(1, sizeof(struct netif)); + if (esp_netif->lwip_netif == NULL) { + return ESP_ERR_NO_MEM; + } + } + 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)) { + esp_netif_lwip_remove(esp_netif); + return ESP_ERR_ESP_NETIF_IF_NOT_READY; + } + return ESP_OK; +} + void esp_netif_destroy(esp_netif_t *esp_netif) { if (esp_netif) { esp_netif_remove_from_list(esp_netif); free(esp_netif->ip_info); free(esp_netif->ip_info_old); - esp_netif_lwip_remove(esp_netif); free(esp_netif->if_key); + esp_netif_lwip_remove(esp_netif); + free(esp_netif->lwip_netif); + free(esp_netif->hostname); + if (s_last_default_esp_netif == esp_netif) { + // clear last default netif if it happens to be this just destroyed interface + s_last_default_esp_netif = NULL; + } free(esp_netif); } } @@ -440,8 +489,8 @@ esp_err_t esp_netif_set_driver_config(esp_netif_t *esp_netif, static esp_err_t esp_netif_reset_ip_info(esp_netif_t *esp_netif) { ip4_addr_set_zero(&(esp_netif->ip_info->ip)); - ip4_addr_set_zero(&esp_netif->ip_info->gw); - ip4_addr_set_zero(&esp_netif->ip_info->netmask); + ip4_addr_set_zero(&(esp_netif->ip_info->gw)); + ip4_addr_set_zero(&(esp_netif->ip_info->netmask)); return ESP_OK; } @@ -480,13 +529,12 @@ static esp_err_t esp_netif_config_sanity_check(const esp_netif_t * esp_netif) if (esp_netif->driver_transmit == NULL || esp_netif->driver_handle == NULL || esp_netif->lwip_input_fn == NULL || - esp_netif->lwip_init_fn == NULL || - esp_netif->lwip_netif == NULL) { + esp_netif->lwip_init_fn == NULL) { ESP_LOGE(TAG, "Cannot start esp_netif: Missing mandatory configuration:\n" "esp_netif->driver_transmit: %p, esp_netif->driver_handle:%p, " - "esp_netif->lwip_input_fn: %p, esp_netif->lwip_init_fn:%p, esp_netif->lwip_netif: %p", + "esp_netif->lwip_input_fn: %p, esp_netif->lwip_init_fn:%p", esp_netif->driver_transmit, esp_netif->driver_handle, - esp_netif->lwip_input_fn, esp_netif->lwip_init_fn, esp_netif->lwip_netif); + esp_netif->lwip_input_fn, esp_netif->lwip_init_fn); return ESP_ERR_INVALID_STATE; } @@ -501,7 +549,7 @@ static esp_err_t esp_netif_start_api(esp_netif_api_msg_t *msg) ESP_ERROR_CHECK(esp_netif_config_sanity_check(esp_netif)); - 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); + ESP_ERROR_CHECK(esp_netif_lwip_add(esp_netif)); if (esp_netif->flags&ESP_NETIF_FLAG_GARP) { #if ESP_GRATUITOUS_ARP @@ -663,17 +711,10 @@ static void esp_netif_dhcpc_cb(struct netif *netif) static void esp_netif_ip_lost_timer(void *arg) { - esp_netif_t *esp_netif = NULL; - esp_netif_t *nif = esp_netif_next(NULL); - do { - if (nif && nif == arg) { - esp_netif = nif; - break; - } - } while (NULL != (nif = esp_netif_next(nif))); + esp_netif_t *esp_netif = esp_netif_is_active(arg); if (esp_netif == NULL) { - ESP_LOGD(TAG, "%s esp_netif not found in the list", __func__); + ESP_LOGD(TAG, "%s esp_netif=%p not active any more", __func__, arg); return; } @@ -691,7 +732,7 @@ static void esp_netif_ip_lost_timer(void *arg) int ret; ESP_LOGD(TAG, "if%p ip lost tmr: raise ip lost event", esp_netif); - memset(&esp_netif->ip_info_old, 0, sizeof(esp_netif_ip_info_t)); + memset(esp_netif->ip_info_old, 0, sizeof(esp_netif_ip_info_t)); if (esp_netif->lost_ip_event) { ret = esp_event_send_internal(IP_EVENT, esp_netif->lost_ip_event, &evt, sizeof(evt), 0); @@ -707,7 +748,7 @@ static void esp_netif_ip_lost_timer(void *arg) static esp_err_t esp_netif_start_ip_lost_timer(esp_netif_t *esp_netif) { - esp_netif_ip_info_t *ip_info_old = (esp_netif_ip_info_t *)&esp_netif->ip_info; + esp_netif_ip_info_t *ip_info_old = esp_netif->ip_info; struct netif *netif = esp_netif->lwip_netif; ESP_LOGD(TAG, "%s esp_netif:%p", __func__, esp_netif); @@ -956,7 +997,7 @@ esp_err_t esp_netif_get_hostname(esp_netif_t *esp_netif, const char **hostname) #endif } -esp_err_t esp_netif_up_api(esp_netif_api_msg_t *msg) +static esp_err_t esp_netif_up_api(esp_netif_api_msg_t *msg) { esp_netif_t *esp_netif = msg->esp_netif; @@ -1017,7 +1058,7 @@ bool esp_netif_is_netif_up(esp_netif_t *esp_netif) { ESP_LOGV(TAG, "%s esp_netif:%p", __func__, esp_netif); - if (esp_netif != NULL && netif_is_up(esp_netif->lwip_netif)) { + if (esp_netif != NULL && esp_netif->lwip_netif != NULL && netif_is_up(esp_netif->lwip_netif)) { return true; } else { return false; @@ -1123,13 +1164,12 @@ static esp_err_t esp_netif_set_ip_info_api(esp_netif_api_msg_t *msg) ip_event_t evt_id = esp_netif->get_ip_event; ip_event_got_ip_t evt = { .esp_netif = esp_netif, .if_index = -1, .ip_changed = false}; int ret; - - if (memcmp(ip_info, &esp_netif->ip_info_old, sizeof(esp_netif_ip_info_t))) { + if (memcmp(ip_info, esp_netif->ip_info_old, sizeof(esp_netif_ip_info_t))) { evt.ip_changed = true; } memcpy(&evt.ip_info, ip_info, sizeof(esp_netif_ip_info_t)); - memcpy(&esp_netif->ip_info_old, ip_info, sizeof(esp_netif_ip_info_t)); + memcpy(esp_netif->ip_info_old, ip_info, sizeof(esp_netif_ip_info_t)); ret = esp_event_send_internal(IP_EVENT, evt_id, &evt, sizeof(evt), 0); if (ESP_OK != ret) { ESP_LOGE(TAG, "set ip info: failed to post got ip event (%x)", ret); From d471266b46947e513ae317b5f539de29ed2aa3aa Mon Sep 17 00:00:00 2001 From: David Cermak Date: Thu, 12 Sep 2019 09:24:56 +0200 Subject: [PATCH 15/31] esp_netif: documentation udpated and grouped the esp-netif API structure. Updated rst documenentation and diagram of esp-netif cooperation with other components. --- components/esp_common/src/esp_err_to_name.c | 4 +- components/esp_netif/include/esp_netif.h | 247 ++++++++++++------ .../esp_netif/include/esp_netif_net_stack.h | 40 ++- .../esp_netif/include/esp_netif_types.h | 2 +- components/esp_netif/lwip/esp_netif_lwip.c | 2 +- docs/Doxyfile | 4 +- docs/en/api-guides/lwip.rst | 2 +- docs/en/api-guides/wifi.rst | 12 +- docs/en/api-reference/network/esp_netif.rst | 146 +++++++++++ docs/en/api-reference/network/index.rst | 2 +- .../api-reference/network/tcpip_adapter.rst | 19 -- .../zh_CN/api-reference/network/esp_netif.rst | 2 + docs/zh_CN/api-reference/network/index.rst | 2 +- .../api-reference/network/tcpip_adapter.rst | 2 - 14 files changed, 372 insertions(+), 114 deletions(-) create mode 100644 docs/en/api-reference/network/esp_netif.rst delete mode 100644 docs/en/api-reference/network/tcpip_adapter.rst create mode 100644 docs/zh_CN/api-reference/network/esp_netif.rst delete mode 100644 docs/zh_CN/api-reference/network/tcpip_adapter.rst diff --git a/components/esp_common/src/esp_err_to_name.c b/components/esp_common/src/esp_err_to_name.c index 97d12112a9..1a06c99059 100644 --- a/components/esp_common/src/esp_err_to_name.c +++ b/components/esp_common/src/esp_err_to_name.c @@ -479,8 +479,8 @@ static const esp_err_msg_t esp_err_msg_table[] = { # ifdef ESP_ERR_ESP_NETIF_DHCP_NOT_STOPPED ERR_TBL_IT(ESP_ERR_ESP_NETIF_DHCP_NOT_STOPPED), /* 20487 0x5007 */ # endif -# ifdef ESP_ERR_ESP_NETIF_DRIVER_ATACH_FAILED - ERR_TBL_IT(ESP_ERR_ESP_NETIF_DRIVER_ATACH_FAILED), /* 20488 0x5008 */ +# ifdef ESP_ERR_ESP_NETIF_DRIVER_ATTACH_FAILED + ERR_TBL_IT(ESP_ERR_ESP_NETIF_DRIVER_ATTACH_FAILED), /* 20488 0x5008 */ # endif // components/esp_common/include/esp_err.h # ifdef ESP_ERR_FLASH_BASE diff --git a/components/esp_netif/include/esp_netif.h b/components/esp_netif/include/esp_netif.h index 4d690c2756..e06a3f5833 100644 --- a/components/esp_netif/include/esp_netif.h +++ b/components/esp_netif/include/esp_netif.h @@ -22,9 +22,15 @@ #include "esp_netif_types.h" #include "esp_netif_defaults.h" -// -// 1) Initialization API: -// +/** + * @defgroup ESP_NETIF_INIT_API ESP-NETIF Initialization API + * @brief Initialization and deinitialization of underlying TCP/IP stack and esp-netif instances + * + */ + +/** @addtogroup ESP_NETIF_INIT_API + * @{ + */ /** * @brief Initialize the underlying TCP/IP stack @@ -62,14 +68,14 @@ esp_netif_t *esp_netif_new(const esp_netif_config_t *esp_netif_config); /** * @brief Destroys the esp_netif object * - * @param[in] pointer to the object to be deleted + * @param[in] esp_netif pointer to the object to be deleted */ void esp_netif_destroy(esp_netif_t *esp_netif); /** * @brief Configures driver related options of esp_netif object * - * @param[inout] pointer to the object to be configured + * @param[inout] esp_netif pointer to the object to be configured * @param[in] driver_config pointer esp-netif io driver related configuration * @return * - ESP_OK on success @@ -79,40 +85,46 @@ 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); - +/** + * @brief Attaches esp_netif instance to the io driver handle + * + * Calling this function enables connecting specific esp_netif object + * with already initialized io driver to update esp_netif object with driver + * specific configuration (i.e. calls post_attach callback, which typically + * sets io driver callbacks to esp_netif instance and starts the driver) + * + * @param[inout] esp_netif pointer to esp_netif object to be attached + * @param[in] driver_handle pointer to the driver handle + * @return + * - ESP_OK on success + * - ESP_ERR_ESP_NETIF_DRIVER_ATTACH_FAILED if driver's pot_attach callback failed + */ esp_err_t esp_netif_attach(esp_netif_t *esp_netif, esp_netif_iodriver_handle driver_handle); -// -// 2) Input - Output APIs -// +/** + * @} + */ /** - * @brief Outputs packets from the TCP/IP stack to the media to be transmitted - * - * This function gets called from network stack to output packets to IO driver. - * - * @note This function is installed automatically for default interfaces. - * Custom interface may need to install if the rx buffer passed as pointer and the IO driver has to - * deallocate the data in driver context - * - * @param[in] esp_netif Handle to esp-netif instance - * @param[in] void* buffer: rx buffer pointer - */ -esp_err_t esp_netif_transmit(esp_netif_t *esp_netif, void* data, size_t len); + * @defgroup ESP_NETIF_DATA_IO_API ESP-NETIF Input Output API + * @brief Input and Output functions to pass data packets from communication media (IO driver) + * to TCP/IP stack. + * + * These functions are usually not directly called from user code, but installed, or registered + * as callbacks in either IO driver on one hand or TCP/IP stack on the other. More specifically + * esp_netif_receive is typically called from io driver on reception callback to input the packets + * to TCP/IP stack. Similarly esp_netif_transmit is called from the TCP/IP stack whenever + * a packet ought to output to the communication media. + * + * @note These IO functions are registerd (installed) automatically for default interfaces + * (interfaces with the keys such as WIFI_STA_DEF, WIFI_AP_DEF, ETH_DEF). Custom interface + * has to register these IO functions when creating interface using @ref esp_netif_new + * + */ -/** - * @brief Free the rx buffer allocated by the media driver - * - * This function gets called from network stack when the rx buffer to be freed in media driver context. - * - * @note This function is installed automatically for default interfaces. - * Custom interface may need to install if the rx buffer passed as pointer and the IO driver has to - * deallocate the data in driver context - * - * @param[in] esp_netif Handle to esp-netif instance - * @param[in] void* buffer: rx buffer pointer - */ -void esp_netif_free_rx_buffer(void *esp_netif, void* buffer); +/** @addtogroup ESP_NETIF_DATA_IO_API + * @{ + */ /** * @brief Passes the raw packets from communication media to the appropriate TCP/IP stack @@ -120,11 +132,6 @@ void esp_netif_free_rx_buffer(void *esp_netif, void* buffer); * This function is called from the configured (peripheral) driver layer. * The data are then forwarded as frames to the TCP/IP stack. * - * @note Installation happens automatically for default interfaces; custom IO drivers must install - * this function so that it is called on a reception of incoming packet - * - * @note Application code does not usually need to call this function directly. - * * @param[in] esp_netif Handle to esp-netif instance * @param[in] buffer Received data * @param[in] len Length of the data frame @@ -135,10 +142,20 @@ void esp_netif_free_rx_buffer(void *esp_netif, void* buffer); */ esp_err_t esp_netif_receive(esp_netif_t *esp_netif, void *buffer, size_t len, void *eb); -// -// 3) ESP-NETIF lifecycle -// +/** + * @} + */ +/** + * @defgroup ESP_NETIF_LIFECYCLE ESP-NETIF Lifecycle control + * @brief These APIS define basic building blocks to control network interface lifecycle, i.e. + * start, stop, set_up or set_down. These functions can be directly used as event handlers + * registered to follow the events from communication media. + */ + +/** @addtogroup ESP_NETIF_LIFECYCLE + * @{ + */ /** * @brief Default building block for network interface action upon IO driver start event @@ -202,9 +219,28 @@ void esp_netif_action_disconnected(void *esp_netif, esp_event_base_t base, int32 */ void esp_netif_action_got_ip(void *esp_netif, esp_event_base_t base, int32_t event_id, void *data); -// -/// 4) Configuration (getter - setters) -// +/** + * @} + */ + +/** + * @defgroup ESP_NETIF_GET_SET ESP-NETIF Runtime configuration + * @brief Getters and setters for various TCP/IP related parameters + */ + +/** @addtogroup ESP_NETIF_GET_SET + * @{ + */ + +/** + * @brief Set the mac address for the interface instance + + * @param[in] esp_netif Handle to esp-netif instance + * @param[in] mac Desired mac address for the related network interface + * @return ESP_OK + */ +esp_err_t esp_netif_set_mac(esp_netif_t *esp_netif, uint8_t mac[]); + /** * @brief Set the hostname of an interface * @@ -317,14 +353,31 @@ esp_err_t esp_netif_set_ip_info(esp_netif_t *esp_netif, const 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); +/** + * @brief Get net interface index from network stack implementation + * + * @note This index could be used in `setsockopt()` to bind socket with multicast interface + * + * @param[in] esp_netif Handle to esp-netif instance + * + * @return + * implementation specific index of interface represented with supplied esp_netif + */ +int esp_netif_get_netif_impl_index(esp_netif_t *esp_netif); -// -// 5) Network stack related API -// +/** + * @} + */ + +/** + * @defgroup ESP_NETIF_NET_DHCP ESP-NETIF DHCP Settings + * @brief Network stack related interface to DHCP client and server + */ + +/** @addtogroup ESP_NETIF_NET_DHCP + * @{ + */ -// -// DHCP -// /** * @brief Set or Get DHCP server option * @@ -342,6 +395,21 @@ esp_err_t esp_netif_set_old_ip_info(esp_netif_t *esp_netif, const esp_netif_ip_i */ 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 + * + * @param[in] esp_netif Handle to esp-netif instance + * @param[in] opt_op ESP_NETIF_OP_SET to set an option, ESP_NETIF_OP_GET to get an option. + * @param[in] opt_id Option index to get or set, must be one of the supported enum values. + * @param[inout] opt_val Pointer to the option parameter. + * @param[in] opt_len Length of the option parameter. + * + * @return + * - ESP_OK + * - ESP_ERR_ESP_NETIF_INVALID_PARAMS + * - 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); /** @@ -421,9 +489,19 @@ esp_err_t esp_netif_dhcps_start(esp_netif_t *esp_netif); */ esp_err_t esp_netif_dhcps_stop(esp_netif_t *esp_netif); -// -// DNS: -// +/** + * @} + */ + +/** + * @defgroup ESP_NETIF_NET_DNS ESP-NETIF DNS Settings + * @brief Network stack related interface to NDS + */ + +/** @addtogroup ESP_NETIF_NET_DNS + * @{ + */ + /** * @brief Set DNS Server information * @@ -467,14 +545,19 @@ esp_err_t esp_netif_set_dns_info(esp_netif_t *esp_netif, esp_netif_dns_type_t ty * - ESP_ERR_ESP_NETIF_INVALID_PARAMS invalid params */ 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); -/** - * @brief Set the mac address for the interface instance - * @param[in] esp_netif Handle to esp-netif instance - * @param[in] mac Desired mac address for the related network interface - * @return ESP_OK +/** + * @} + */ + +/** + * @defgroup ESP_NETIF_NET_IP ESP-NETIF IP address related interface + * @brief Network stack related interface to IP + */ + +/** @addtogroup ESP_NETIF_NET_IP + * @{ */ -esp_err_t esp_netif_set_mac(esp_netif_t *esp_netif, uint8_t mac[]); /** * @brief Create interface link-local IPv6 address @@ -540,9 +623,18 @@ char * esp_ip4addr_ntoa(const esp_ip4_addr_t *addr, char *buf, int buflen); */ uint32_t esp_ip4addr_aton(const char *addr); -// -// 6) Driver conversion utilities to related keys, flags, implementation handle, list of netifs -// +/** + * @} + */ + +/** + * @defgroup ESP_NETIF_CONVERT ESP-NETIF Conversion utilities + * @brief ESP-NETIF conversion utilities to related keys, flags, implementation handle + */ + +/** @addtogroup ESP_NETIF_CONVERT + * @{ + */ /** * @brief Gets media driver handle for this esp-netif instance @@ -600,9 +692,19 @@ esp_netif_type_t esp_netif_get_type(esp_netif_t *esp_netif); */ uint32_t esp_netif_get_event_id(esp_netif_t *esp_netif, esp_netif_ip_event_type_t event_type); -// -// 7) esp_netif list API -// +/** + * @} + */ + +/** + * @defgroup ESP_NETIF_LIST ESP-NETIF List of interfaces + * @brief APIs to enumerate all registered interfaces + */ + +/** @addtogroup ESP_NETIF_LIST + * @{ + */ + /** * @brief Iterates over list of interfaces. Returns first netif if NULL given as parameter * @@ -610,7 +712,7 @@ uint32_t esp_netif_get_event_id(esp_netif_t *esp_netif, esp_netif_ip_event_type_ * * @return First netif from the list if supplied parameter is NULL, next one otherwise */ -esp_netif_t* esp_netif_next(esp_netif_t* netif); +esp_netif_t* esp_netif_next(esp_netif_t* esp_netif); /** * @brief Returns number of registered esp_netif objects @@ -636,16 +738,5 @@ size_t esp_netif_get_nr_of_ifs(void); */ esp_err_t esp_netif_get_sta_list(const wifi_sta_list_t *wifi_sta_list, esp_netif_sta_list_t *tcpip_sta_list); -/** - * @brief Get net interface index from network stack implementation - * - * @note This index could be used in `setsockopt()` to bind socket with multicast interface - * - * @param[in] esp_netif Handle to esp-netif instance - * - * @return - * implementation specific index of interface represented with supplied esp_netif - */ -int esp_netif_get_netif_impl_index(esp_netif_t *esp_netif); #endif /* _ESP_NETIF_H_ */ diff --git a/components/esp_netif/include/esp_netif_net_stack.h b/components/esp_netif/include/esp_netif_net_stack.h index bfef36fd04..d2a6b0f0c3 100644 --- a/components/esp_netif/include/esp_netif_net_stack.h +++ b/components/esp_netif/include/esp_netif_net_stack.h @@ -19,6 +19,10 @@ // Network stack API: This ESP-NETIF API are supposed to be called only from internals of TCP/IP stack // +/** @addtogroup ESP_NETIF_CONVERT + * @{ + */ + /** * @brief Returns esp-netif handle * @@ -28,7 +32,6 @@ */ esp_netif_t* esp_netif_get_handle_from_netif_impl(void *dev); - /** * @brief Returns network stack specific implementation handle * @@ -38,4 +41,39 @@ esp_netif_t* esp_netif_get_handle_from_netif_impl(void *dev); */ void* esp_netif_get_netif_impl(esp_netif_t *esp_netif); +/** + * @} + */ + +/** @addtogroup ESP_NETIF_DATA_IO_API + * @{ + */ + +/** + * @brief Outputs packets from the TCP/IP stack to the media to be transmitted + * + * This function gets called from network stack to output packets to IO driver. + * + * @param[in] esp_netif Handle to esp-netif instance + * @param[in] data Data to be tranmitted + * @param[in] len Length of the data frame + */ +esp_err_t esp_netif_transmit(esp_netif_t *esp_netif, void* data, size_t len); + +/** + * @brief Free the rx buffer allocated by the media driver + * + * This function gets called from network stack when the rx buffer to be freed in IO driver context, + * i.e. to deallocate a buffer owned by io driver (when data packets were passed to higher levels + * to avoid copying) + * + * @param[in] esp_netif Handle to esp-netif instance + * @param[in] void* buffer: rx buffer pointer + */ +void esp_netif_free_rx_buffer(void *esp_netif, void* buffer); + +/** + * @} + */ + #endif //_ESP_NETIF_NET_STACK_H_ diff --git a/components/esp_netif/include/esp_netif_types.h b/components/esp_netif/include/esp_netif_types.h index 7f08bf1cfd..960df2d888 100644 --- a/components/esp_netif/include/esp_netif_types.h +++ b/components/esp_netif/include/esp_netif_types.h @@ -26,7 +26,7 @@ #define ESP_ERR_ESP_NETIF_DHCP_ALREADY_STOPPED ESP_ERR_ESP_NETIF_BASE + 0x05 #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_ATACH_FAILED ESP_ERR_ESP_NETIF_BASE + 0x08 +#define ESP_ERR_ESP_NETIF_DRIVER_ATTACH_FAILED ESP_ERR_ESP_NETIF_BASE + 0x08 /** @brief Type of esp_netif_object server */ diff --git a/components/esp_netif/lwip/esp_netif_lwip.c b/components/esp_netif/lwip/esp_netif_lwip.c index 1fc0e3c815..751b00653c 100644 --- a/components/esp_netif/lwip/esp_netif_lwip.c +++ b/components/esp_netif/lwip/esp_netif_lwip.c @@ -468,7 +468,7 @@ esp_err_t esp_netif_attach(esp_netif_t *esp_netif, esp_netif_iodriver_handle dri esp_err_t ret = base_driver->post_attach(esp_netif, driver_handle); if (ret != ESP_OK) { ESP_LOGE(TAG, "Post-attach callback of driver(%p) failed with %d", driver_handle, ret); - return ESP_ERR_ESP_NETIF_DRIVER_ATACH_FAILED; + return ESP_ERR_ESP_NETIF_DRIVER_ATTACH_FAILED; } } return ESP_OK; diff --git a/docs/Doxyfile b/docs/Doxyfile index dc233500df..af1a5b3424 100644 --- a/docs/Doxyfile +++ b/docs/Doxyfile @@ -109,8 +109,8 @@ INPUT = \ ../../components/soc/esp32/include/soc/touch_channel.h \ ../../components/soc/esp32/include/soc/uart_channel.h \ ../../components/soc/esp32/include/soc/rtc_gpio_channel.h \ - ## tcpip_adapter - API Reference - ../../components/tcpip_adapter/include/tcpip_adapter.h \ + ## esp_netif - API Reference + ../../components/esp_netif/include/esp_netif.h \ ## ## Protocols - API Reference ## diff --git a/docs/en/api-guides/lwip.rst b/docs/en/api-guides/lwip.rst index 4eb528b61b..3c5199e8a1 100644 --- a/docs/en/api-guides/lwip.rst +++ b/docs/en/api-guides/lwip.rst @@ -16,7 +16,7 @@ Adapted APIs Some common lwIP "app" APIs are supported indirectly by ESP-IDF: -- DHCP Server & Client are supported indirectly via the :doc:`/api-reference/network/tcpip_adapter` functionality +- DHCP Server & Client are supported indirectly via the :doc:`/api-reference/network/esp_netif` functionality - Simple Network Time Protocol (SNTP) is supported via the :component_file:`lwip/include/apps/esp_sntp.h` functions (see also :example:`protocols/sntp`) - ICMP Ping is supported using a variation on the lwIP ping API. See :doc:`/api-reference/protocols/icmp_echo`. - NetBIOS lookup is available using the standard lwIP API. :example:`protocols/http_server/restful_server` has an option to demonstrate using NetBIOS to look up a host on the LAN. diff --git a/docs/en/api-guides/wifi.rst b/docs/en/api-guides/wifi.rst index 02f488fa31..c47669111e 100644 --- a/docs/en/api-guides/wifi.rst +++ b/docs/en/api-guides/wifi.rst @@ -122,7 +122,7 @@ The ESP32 Wi-Fi programming model is depicted as follows: The Wi-Fi driver can be considered a black box that knows nothing about high-layer code, such as the TCP/IP stack, application task, event task, etc. The application task (code) generally calls :doc:`Wi-Fi driver APIs <../api-reference/network/esp_wifi>` to initialize Wi-Fi and handles Wi-Fi events when necessary. Wi-Fi driver receives API calls, handles them, and post events to the application. -Wi-Fi event handling is based on the :doc:`esp_event library <../api-reference/system/esp_event>`. Events are sent by the Wi-Fi driver to the :ref:`default event loop `. Application may handle these events in callbacks registered using :cpp:func:`esp_event_handler_register`. Wi-Fi events are also handled by :doc:`tcpip_adapter component <../api-reference/network/tcpip_adapter>` to provide a set of default behaviors. For example, when Wi-Fi station connects to an AP, tcpip_adapter will automatically start the DHCP client. +Wi-Fi event handling is based on the :doc:`esp_event library <../api-reference/system/esp_event>`. Events are sent by the Wi-Fi driver to the :ref:`default event loop `. Application may handle these events in callbacks registered using :cpp:func:`esp_event_handler_register`. Wi-Fi events are also handled by :doc:`esp_netif component <../api-reference/network/esp_netif>` to provide a set of default behaviors. For example, when Wi-Fi station connects to an AP, esp_netif will automatically start the DHCP client (by default). ESP32 Wi-Fi Event Description ------------------------------------ @@ -300,15 +300,17 @@ Below is a "big scenario" which describes some small scenarios in Station mode: 1. Wi-Fi/LwIP Init Phase ++++++++++++++++++++++++++++++ - - s1.1: The main task calls tcpip_adapter_init() to create an LwIP core task and initialize LwIP-related work. + - s1.1: The main task calls esp_netif_init() to create an LwIP core task and initialize LwIP-related work. - s1.2: The main task calls esp_event_loop_init() to create a system Event task and initialize an application event's callback function. In the scenario above, the application event's callback function does nothing but relaying the event to the application task. - - s1.3: The main task calls esp_wifi_init() to create the Wi-Fi driver task and initialize the Wi-Fi driver. + - s1.3: The main task calls esp_netif_create_default_wifi_ap() or esp_netif_create_default_wifi_sta() to create default network interface instance binding station or AP with TCP/IP stack. - - s1.4: The main task calls OS API to create the application task. + - s1.4: The main task calls esp_wifi_init() to create the Wi-Fi driver task and initialize the Wi-Fi driver. -Step 1.1~1.4 is a recommended sequence that initializes a Wi-Fi-/LwIP-based application. However, it is **NOT** a must-follow sequence, which means that you can create the application task in step 1.1 and put all other initializations in the application task. Moreover, you may not want to create the application task in the initialization phase if the application task depends on the sockets. Rather, you can defer the task creation until the IP is obtained. + - s1.5: The main task calls OS API to create the application task. + +Step 1.1~1.5 is a recommended sequence that initializes a Wi-Fi-/LwIP-based application. However, it is **NOT** a must-follow sequence, which means that you can create the application task in step 1.1 and put all other initializations in the application task. Moreover, you may not want to create the application task in the initialization phase if the application task depends on the sockets. Rather, you can defer the task creation until the IP is obtained. 2. Wi-Fi Configuration Phase +++++++++++++++++++++++++++++++ diff --git a/docs/en/api-reference/network/esp_netif.rst b/docs/en/api-reference/network/esp_netif.rst new file mode 100644 index 0000000000..eeae49b314 --- /dev/null +++ b/docs/en/api-reference/network/esp_netif.rst @@ -0,0 +1,146 @@ +ESP-NETIF +========= + +The purpose of ESP-NETIF library is twofold: + +- It provides an abstraction layer for the application on top of the TCP/IP stack. This will allow applications to choose between IP stacks in the future. +- The APIs it provides are thread safe, even if the underlying TCP/IP stack APIs are not. + +ESP-IDF currently implements ESP-NETIF for the lwIP TCP/IP stack only. However, the adapter itself is TCP/IP implementation agnostic and different implementations are possible. + +Some ESP-NETIF API functions are intended to be called by application code, for example to get/set interface IP addresses, configure DHCP. Other functions are intended for internal ESP-IDF use by the network driver layer. + +In many cases, applications do not need to call ESP-NETIF APIs directly as they are called from the default network event handlers. + + +ESP-NETIF architecture +====================== + +.. code-block:: text + + | (A) USER CODE | + | | + .............| init settings events | + . +----------------------------------------+ + . . | * + . . | * + --------+ +===========================+ * +-----------------------+ + | | new/config get/set | * | | + | | |...*.....| init | + | |---------------------------| * | | + init | | |**** | | + start |********| event handler |*********| DHCP | + stop | | | | | + | |---------------------------| | | + | | | | NETIF | + +-----| | | +-----------------+ | + | glue|----<---| esp_netif_transmit |--<------| netif_output | | + | | | | | | | + | |---->---| esp_netif_receive |-->------| netif_input | | + | | | | + ----------------+ | + | |....<...| esp_netif_free_rx_buffer |...<.....| packet buffer | + +-----| | | | | + | | | | (D) | + (B) | | (C) | +-----------------------+ + --------+ +===========================+ + communication NETWORK STACK + DRIVER ESP-NETIF + + +Data and event flow in the diagram +---------------------------------- + +* ``........`` Initialization line from user code to ESP-NETIF and communication driver + +* ``--<--->--`` Data packets going from communication media to TCP/IP stack and back + +* ``********`` Events aggregated in ESP-NETIF propagates to driver, user code and network stack + +* ``|`` User settings and runtime configuration + +ESP-NETIF interaction +--------------------- + +A) User code, boiler plate +^^^^^^^^^^^^^^^^^^^^^^^^^^ + +Overall application interaction with a specific IO driver for communication media and configured TCP/IP network stack +is abstracted using ESP-NETIF APIs and outlined as below: + +A) Initialization code + + 1) Initializes IO driver + 2) Creates a new instance of ESP-NETIF and configure with + + * ESP-NETIF specific options (flags, behaviour, name) + * Network stack options (netif init and input functions, not publicly available) + * IO driver specific options (transmit, free rx buffer functions, IO driver handle) + + 3) Attaches the IO driver handle to the ESP-NETIF instance created in the above steps + 4) Configures event handlers + + * use default handlers for common interfaces defined in IO drivers; or define a specific handlers for customised behaviour/new interfaces + * register handlers for app related events (such as IP lost/acquired) + +B) Interaction with network interfaces using ESP-NETIF API + + * Getting and setting TCP/IP related parameters (DHCP, IP, etc) + * Receiving IP events (connect/disconnect) + * Controlling application lifecycle (set interface up/down) + +Please note that the initialization code as well as registering event handlers for default interfaces, +such as WiFi softAP and WiFi station, are provided as a separate APIs (for example ``esp_netif_create_default_wifi_ap()`` and +``esp_netif_create_default_wifi_sta()``) to facilitate simple startup code for most applications. + + +B) Communication driver, IO driver, media driver +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +Communication driver plays these two important roles in relation with ESP-NETIF: + +1) Event handlers: Define behaviour patterns of interaction with ESP-NETIF (for example: ethernet link-up -> turn netif on) + +2) Glue IO layer: Adapts the input/output functions to use ESP-NETIF transmit, receive and free receive buffer + + * Installs driver_transmit to appropriate ESP-NETIF object, so that outgoing packets from network stack are passed to the IO driver + * Calls ``esp_netif_receive()`` to pass incoming data to network stack + + +C) ESP-NETIF, former tcpip_adapter +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +ESP-NETIF is an intermediary between an IO driver and a network stack, connecting packet data path between these two. +As that it provides a set of interfaces for attaching a driver to ESP-NETIF object (runtime) and +configuring a network stack (compile time). In addition to that a set of API is provided to control network interface lifecycle +and its TCP/IP properties. As an overview, the ESP-NETIF public interface could be divided into these 6 groups: + +1) Initialization APIs (to create and configure ESP-NETIF instance) +2) Input/Output API (for passing data between IO driver and network stack) +3) Event or Action API + + * Used for network interface lifecycle management + * ESP-NETIF provides building blocks for designing event handlers + +4) Setters and Getters for basic network interface properties +5) Network stack abstraction: enabling user interaction with TCP/IP stack + + * Set interface up or down + * DHCP server and client API + * DNS API + +6) Driver conversion utilities + + +D) Network stack +^^^^^^^^^^^^^^^^ + +Network stack has no public interaction with application code with regard to public interfaces and shall be fully abstracted by +ESP-NETIF API. + + + +API Reference +------------- + +.. include:: /_build/inc/esp_netif.inc + diff --git a/docs/en/api-reference/network/index.rst b/docs/en/api-reference/network/index.rst index cb5c5801b3..93adede1d1 100644 --- a/docs/en/api-reference/network/index.rst +++ b/docs/en/api-reference/network/index.rst @@ -36,7 +36,7 @@ IP Network Layer .. toctree:: :maxdepth: 1 - TCP/IP Adapter + ESP-NETIF Code examples for TCP/IP socket APIs are provided in the :example:`protocols/sockets` directory of ESP-IDF examples. diff --git a/docs/en/api-reference/network/tcpip_adapter.rst b/docs/en/api-reference/network/tcpip_adapter.rst deleted file mode 100644 index e3b8fdd53c..0000000000 --- a/docs/en/api-reference/network/tcpip_adapter.rst +++ /dev/null @@ -1,19 +0,0 @@ -TCP/IP Adapter -============== - -The purpose of TCP/IP Adapter library is twofold: - -- It provides an abstraction layer for the application on top of the TCP/IP stack. This will allow applications to choose between IP stacks in the future. -- The APIs it provides are thread safe, even if the underlying TCP/IP stack APIs are not. - -ESP-IDF currently implements TCP/IP Adapter for the lwIP TCP/IP stack only. However, the adapter itself is TCP/IP implementation agnostic and different implementations are possible. - -Some TCP/IP Adapter API functions are intended to be called by application code, for example to get/set interface IP addresses, configure DHCP. Other functions are intended for internal ESP-IDF use by the network driver layer. - -In many cases, applications do not need to call TCP/IP Adapter APIs directly as they are called from the default network event handlers. - -API Reference -------------- - -.. include:: /_build/inc/tcpip_adapter.inc - diff --git a/docs/zh_CN/api-reference/network/esp_netif.rst b/docs/zh_CN/api-reference/network/esp_netif.rst new file mode 100644 index 0000000000..b48cbe39be --- /dev/null +++ b/docs/zh_CN/api-reference/network/esp_netif.rst @@ -0,0 +1,2 @@ +.. include:: ../../../en/api-reference/network/esp_netif.rst + diff --git a/docs/zh_CN/api-reference/network/index.rst b/docs/zh_CN/api-reference/network/index.rst index 3a78560014..83bc3ce5ba 100644 --- a/docs/zh_CN/api-reference/network/index.rst +++ b/docs/zh_CN/api-reference/network/index.rst @@ -36,7 +36,7 @@ IP 网络层协议 .. toctree:: :maxdepth: 1 - TCP/IP Adapter + ESP-NETIF TCP/IP 套接字 API 的示例代码存放在 ESP-IDF 示例项目的 :example:`protocols/sockets` 目录下。 diff --git a/docs/zh_CN/api-reference/network/tcpip_adapter.rst b/docs/zh_CN/api-reference/network/tcpip_adapter.rst deleted file mode 100644 index 210dfd7c1c..0000000000 --- a/docs/zh_CN/api-reference/network/tcpip_adapter.rst +++ /dev/null @@ -1,2 +0,0 @@ -.. include:: ../../../en/api-reference/network/tcpip_adapter.rst - From 549ee87912db6e2911b8268c590c0c8ca61cade8 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Thu, 12 Sep 2019 10:06:50 +0200 Subject: [PATCH 16/31] esp_netif: moving default netifs to wifi, moved sta_list to a separate header -- note will fail unit tests --- components/esp_netif/CMakeLists.txt | 1 + components/esp_netif/esp_netif_defaults.c | 27 -------- components/esp_netif/include/esp_netif.h | 18 +----- .../esp_netif/include/esp_netif_defaults.h | 26 -------- .../esp_netif/include/esp_netif_sta_list.h | 62 +++++++++++++++++++ .../esp_netif/include/esp_netif_types.h | 9 --- components/esp_netif/lwip/esp_netif_lwip.c | 21 +------ .../esp_netif/lwip/esp_netif_sta_list.c | 43 +++++++++++++ components/esp_wifi/include/esp_wifi.h | 1 + .../esp_wifi/include/esp_wifi_default.h | 13 ++++ components/esp_wifi/src/wifi_default.c | 18 ++++++ .../include/tcpip_adapter_types.h | 1 + 12 files changed, 142 insertions(+), 98 deletions(-) create mode 100644 components/esp_netif/include/esp_netif_sta_list.h create mode 100644 components/esp_netif/lwip/esp_netif_sta_list.c diff --git a/components/esp_netif/CMakeLists.txt b/components/esp_netif/CMakeLists.txt index a47bcd65db..6155f95795 100644 --- a/components/esp_netif/CMakeLists.txt +++ b/components/esp_netif/CMakeLists.txt @@ -4,6 +4,7 @@ idf_component_register(SRCS "esp_netif_handlers.c" "lwip/esp_netif_lwip.c" "loopback/esp_netif_loopback.c" "lwip/esp_netif_lwip_defaults.c" + "lwip/esp_netif_sta_list.c" INCLUDE_DIRS include PRIV_INCLUDE_DIRS lwip private_include REQUIRES lwip esp_eth tcpip_adapter) diff --git a/components/esp_netif/esp_netif_defaults.c b/components/esp_netif/esp_netif_defaults.c index 67d2410b90..c1e20e2017 100644 --- a/components/esp_netif/esp_netif_defaults.c +++ b/components/esp_netif/esp_netif_defaults.c @@ -65,30 +65,3 @@ const esp_netif_inherent_config_t _g_esp_netif_inherent_eth_config = { .if_type = ESP_NETIF_TYPE_ETH, .route_prio = 50 }; - -esp_netif_t* esp_netif_create_default_wifi_ap(void) -{ - esp_netif_config_t cfg = ESP_NETIF_DEFAULT_WIFI_AP(); - esp_netif_t *netif = esp_netif_new(&cfg); - assert(netif); - esp_wifi_set_default_wifi_driver_and_handlers(ESP_IF_WIFI_AP, netif); - return netif; -} - -esp_netif_t* esp_netif_create_default_wifi_sta(void) -{ - esp_netif_config_t cfg = ESP_NETIF_DEFAULT_WIFI_STA(); - esp_netif_t *netif = esp_netif_new(&cfg); - assert(netif); - esp_wifi_set_default_wifi_driver_and_handlers(ESP_IF_WIFI_STA, netif); - return netif; -} - -esp_netif_t* esp_netif_create_default_eth(void * eth_driver) -{ - esp_netif_config_t cfg = ESP_NETIF_DEFAULT_ETH(); - esp_netif_t* eth_netif = esp_netif_new(&cfg); - assert(eth_netif); - ESP_ERROR_CHECK(esp_netif_attach(eth_netif, eth_driver)); - return eth_netif; -} \ No newline at end of file diff --git a/components/esp_netif/include/esp_netif.h b/components/esp_netif/include/esp_netif.h index e06a3f5833..d2ec9dd509 100644 --- a/components/esp_netif/include/esp_netif.h +++ b/components/esp_netif/include/esp_netif.h @@ -670,7 +670,7 @@ esp_netif_flags_t esp_netif_get_flags(esp_netif_t *esp_netif); * * @return Textual description of related interface */ -char *esp_netif_get_ifkey(esp_netif_t *esp_netif); +const char *esp_netif_get_ifkey(esp_netif_t *esp_netif); /** * @brief Returns configured interface type for this esp-netif instance @@ -721,22 +721,8 @@ esp_netif_t* esp_netif_next(esp_netif_t* esp_netif); */ size_t esp_netif_get_nr_of_ifs(void); -// -// 8) MISC: List of STAs -// - /** - * @brief Get IP information for stations connected to the Wi-Fi AP interface - * - * @param[in] wifi_sta_list Wi-Fi station info list, returned from esp_wifi_ap_get_sta_list() - * @param[out] tcpip_sta_list IP layer station info list, corresponding to MAC addresses provided in wifi_sta_list - * - * @return - * - ESP_OK - * - ESP_ERR_ESP_NETIF_NO_MEM - * - ESP_ERR_ESP_NETIF_INVALID_PARAMS + * @} */ -esp_err_t esp_netif_get_sta_list(const wifi_sta_list_t *wifi_sta_list, esp_netif_sta_list_t *tcpip_sta_list); - #endif /* _ESP_NETIF_H_ */ diff --git a/components/esp_netif/include/esp_netif_defaults.h b/components/esp_netif/include/esp_netif_defaults.h index c8b817b1fe..c1bc5d1e6e 100644 --- a/components/esp_netif/include/esp_netif_defaults.h +++ b/components/esp_netif/include/esp_netif_defaults.h @@ -85,30 +85,4 @@ 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; -// -// API for creating default interfaces -// - -/** - * @brief Creates default WIFI AP. In case of any init error this API aborts. - * - * @return pointer to esp-netif instance - */ -esp_netif_t* esp_netif_create_default_wifi_ap(void); - -/** - * @brief Creates default WIFI STA. In case of any init error this API aborts. - * - * @return pointer to esp-netif instance - */ -esp_netif_t* esp_netif_create_default_wifi_sta(void); - -/** - * @brief Creates default ethernet interface. In case of any init error this API aborts. - * - * @return pointer to esp-netif instance - */ -esp_netif_t* esp_netif_create_default_eth(void * eth_driver); - - #endif //_ESP_NETIF_DEFAULTS_H diff --git a/components/esp_netif/include/esp_netif_sta_list.h b/components/esp_netif/include/esp_netif_sta_list.h new file mode 100644 index 0000000000..3f672ec6e6 --- /dev/null +++ b/components/esp_netif/include/esp_netif_sta_list.h @@ -0,0 +1,62 @@ +// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef _ESP_NETIF_STA_LIST_H_ +#define _ESP_NETIF_STA_LIST_H_ + +#include "esp_netif_types.h" + +/** + * @brief station list info element + */ +typedef struct { + uint8_t mac[6]; /**< Station MAC address */ + esp_ip4_addr_t ip; /**< Station assigned IP address */ +} esp_netif_sta_info_t; + +/** + * @brief station list structure + */ +typedef struct { + esp_netif_sta_info_t sta[ESP_WIFI_MAX_CONN_NUM]; /**< Connected stations */ + int num; /**< Number of connected stations */ +} esp_netif_sta_list_t; + +/** + * @defgroup ESP_NETIF_STA_LIST ESP-NETIF STA list api + * @brief List of stations for Wi-Fi AP interface + * + */ + +/** @addtogroup ESP_NETIF_STA_LIST + * @{ + */ + +/** + * @brief Get IP information for stations connected to the Wi-Fi AP interface + * + * @param[in] wifi_sta_list Wi-Fi station info list, returned from esp_wifi_ap_get_sta_list() + * @param[out] tcpip_sta_list IP layer station info list, corresponding to MAC addresses provided in wifi_sta_list + * + * @return + * - ESP_OK + * - ESP_ERR_ESP_NETIF_NO_MEM + * - ESP_ERR_ESP_NETIF_INVALID_PARAMS + */ +esp_err_t esp_netif_get_sta_list(const wifi_sta_list_t *wifi_sta_list, esp_netif_sta_list_t *tcpip_sta_list); + +/** + * @} + */ +#endif //_ESP_NETIF_STA_LIST_H_ diff --git a/components/esp_netif/include/esp_netif_types.h b/components/esp_netif/include/esp_netif_types.h index 960df2d888..364b498c59 100644 --- a/components/esp_netif/include/esp_netif_types.h +++ b/components/esp_netif/include/esp_netif_types.h @@ -120,17 +120,8 @@ typedef struct { } ip_event_ap_staipassigned_t; -typedef struct { - uint8_t mac[6]; /**< Station MAC address */ - esp_ip4_addr_t ip; /**< Station assigned IP address */ -} esp_netif_sta_info_t; -typedef struct { - esp_netif_sta_info_t sta[ESP_WIFI_MAX_CONN_NUM]; /**< Connected stations */ - int num; /**< Number of connected stations */ -} esp_netif_sta_list_t; - typedef enum esp_netif_flags { ESP_NETIF_DHCPC = 1 << 0, ESP_NETIF_DHCPS = 1 << 1, diff --git a/components/esp_netif/lwip/esp_netif_lwip.c b/components/esp_netif/lwip/esp_netif_lwip.c index 751b00653c..ef22c888b9 100644 --- a/components/esp_netif/lwip/esp_netif_lwip.c +++ b/components/esp_netif/lwip/esp_netif_lwip.c @@ -1275,25 +1275,6 @@ esp_err_t esp_netif_get_dns_info(esp_netif_t *esp_netif, esp_netif_dns_type_t ty return esp_netif_lwip_ipc_call(esp_netif_get_dns_info_api, esp_netif, (void *)&dns_param); } -esp_err_t esp_netif_get_sta_list(const wifi_sta_list_t *wifi_sta_list, esp_netif_sta_list_t *netif_sta_list) -{ - ESP_LOGD(TAG, "%s entered", __func__); - - if ((wifi_sta_list == NULL) || (netif_sta_list == NULL)) { - return ESP_ERR_ESP_NETIF_INVALID_PARAMS; - } - - memset(netif_sta_list, 0, sizeof(esp_netif_sta_list_t)); - netif_sta_list->num = wifi_sta_list->num; - for (int i = 0; i < wifi_sta_list->num; i++) { - memcpy(netif_sta_list->sta[i].mac, wifi_sta_list->sta[i].mac, 6); - dhcp_search_ip_on_mac(netif_sta_list->sta[i].mac, (ip4_addr_t*)&netif_sta_list->sta[i].ip); - } - - return ESP_OK; -} - - static void esp_netif_nd6_cb(struct netif *p_netif, uint8_t ip_idex) { ESP_LOGD(TAG, "%s lwip-netif:%p", __func__, p_netif); @@ -1362,7 +1343,7 @@ esp_netif_flags_t esp_netif_get_flags(esp_netif_t *esp_netif) return esp_netif->flags; } -char *esp_netif_get_ifkey(esp_netif_t *esp_netif) +const char *esp_netif_get_ifkey(esp_netif_t *esp_netif) { return esp_netif->if_key; } diff --git a/components/esp_netif/lwip/esp_netif_sta_list.c b/components/esp_netif/lwip/esp_netif_sta_list.c new file mode 100644 index 0000000000..0bbdcaadad --- /dev/null +++ b/components/esp_netif/lwip/esp_netif_sta_list.c @@ -0,0 +1,43 @@ +// Copyright 2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include "esp_netif.h" +#include "esp_netif_sta_list.h" +#include "dhcpserver/dhcpserver.h" +#include "esp_log.h" + +#if CONFIG_ESP_NETIF_TCPIP_LWIP + +static const char *TAG = "esp_netif_sta_list"; + +esp_err_t esp_netif_get_sta_list(const wifi_sta_list_t *wifi_sta_list, esp_netif_sta_list_t *netif_sta_list) +{ + ESP_LOGD(TAG, "%s entered", __func__); + + if ((wifi_sta_list == NULL) || (netif_sta_list == NULL)) { + return ESP_ERR_ESP_NETIF_INVALID_PARAMS; + } + + memset(netif_sta_list, 0, sizeof(esp_netif_sta_list_t)); + netif_sta_list->num = wifi_sta_list->num; + for (int i = 0; i < wifi_sta_list->num; i++) { + memcpy(netif_sta_list->sta[i].mac, wifi_sta_list->sta[i].mac, 6); + dhcp_search_ip_on_mac(netif_sta_list->sta[i].mac, (ip4_addr_t*)&netif_sta_list->sta[i].ip); + } + + return ESP_OK; +} + +#endif // CONFIG_ESP_NETIF_TCPIP_LWIP diff --git a/components/esp_wifi/include/esp_wifi.h b/components/esp_wifi/include/esp_wifi.h index 6566abee72..d638c9b3a0 100644 --- a/components/esp_wifi/include/esp_wifi.h +++ b/components/esp_wifi/include/esp_wifi.h @@ -63,6 +63,7 @@ #include "esp_wifi_types.h" #include "esp_event.h" #include "esp_private/esp_wifi_private.h" +#include "esp_wifi_default.h" #ifdef __cplusplus extern "C" { diff --git a/components/esp_wifi/include/esp_wifi_default.h b/components/esp_wifi/include/esp_wifi_default.h index e6cd40cffc..3dfc5a4b58 100644 --- a/components/esp_wifi/include/esp_wifi_default.h +++ b/components/esp_wifi/include/esp_wifi_default.h @@ -47,5 +47,18 @@ esp_err_t esp_wifi_set_default_wifi_ap_handlers(void *esp_netif); */ esp_err_t esp_wifi_clear_default_wifi_driver_and_handlers(void *esp_netif); +/** + * @brief Creates default WIFI AP. In case of any init error this API aborts. + * + * @return pointer to esp-netif instance + */ +esp_netif_t* esp_netif_create_default_wifi_ap(void); + +/** + * @brief Creates default WIFI STA. In case of any init error this API aborts. + * + * @return pointer to esp-netif instance + */ +esp_netif_t* esp_netif_create_default_wifi_sta(void); #endif //_ESP_WIFI_DEFAULT_H diff --git a/components/esp_wifi/src/wifi_default.c b/components/esp_wifi/src/wifi_default.c index 9351172d6c..251f12d640 100644 --- a/components/esp_wifi/src/wifi_default.c +++ b/components/esp_wifi/src/wifi_default.c @@ -277,3 +277,21 @@ esp_err_t esp_wifi_clear_default_wifi_driver_and_handlers(void *esp_netif) } return disconnect_and_destroy(esp_netif); } + +esp_netif_t* esp_netif_create_default_wifi_ap(void) +{ + esp_netif_config_t cfg = ESP_NETIF_DEFAULT_WIFI_AP(); + esp_netif_t *netif = esp_netif_new(&cfg); + assert(netif); + esp_wifi_set_default_wifi_driver_and_handlers(ESP_IF_WIFI_AP, netif); + return netif; +} + +esp_netif_t* esp_netif_create_default_wifi_sta(void) +{ + esp_netif_config_t cfg = ESP_NETIF_DEFAULT_WIFI_STA(); + esp_netif_t *netif = esp_netif_new(&cfg); + assert(netif); + esp_wifi_set_default_wifi_driver_and_handlers(ESP_IF_WIFI_STA, netif); + return netif; +} \ No newline at end of file diff --git a/components/tcpip_adapter/include/tcpip_adapter_types.h b/components/tcpip_adapter/include/tcpip_adapter_types.h index c2fe0b4c17..e8346125ba 100644 --- a/components/tcpip_adapter/include/tcpip_adapter_types.h +++ b/components/tcpip_adapter/include/tcpip_adapter_types.h @@ -17,6 +17,7 @@ #include "lwip/ip_addr.h" #include "dhcpserver/dhcpserver.h" +#include "esp_netif_sta_list.h" // // Define compatible types if tcpip_adapter interface used // From 3f60837de2419db5c858762baeae67ae6fed4a41 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Fri, 13 Sep 2019 15:08:33 +0200 Subject: [PATCH 17/31] esp_eth: make esp_eth_driver_start public API so application could start ethernet when used without esp-netif --- components/esp_eth/include/esp_eth.h | 15 ++++++++++++ components/esp_eth/src/esp_eth.c | 23 +++++++++++-------- .../eth2ap/main/ethernet_example_main.c | 1 + 3 files changed, 30 insertions(+), 9 deletions(-) diff --git a/components/esp_eth/include/esp_eth.h b/components/esp_eth/include/esp_eth.h index 5d7d6d66dd..48f364f8b4 100644 --- a/components/esp_eth/include/esp_eth.h +++ b/components/esp_eth/include/esp_eth.h @@ -116,6 +116,21 @@ typedef struct { */ esp_err_t esp_eth_driver_install(const esp_eth_config_t *config, esp_eth_handle_t *out_hdl); +/** +* @brief Start ethernet driver **ONLY** in standalone mode, i.e. without TCP/IP stack +* +* Note that ethernet driver is typically started as soon as it is attached to esp-netif. +* This API should only be called if ethernet is used separately without esp-netif, for example +* when esp_eth_config_t.stack_input is not NULL. +* +* @param[in] eth_handle handle of Ethernet driver +* +* @return +* - ESP_OK: starts ethernet driver +* - ESP_ERR_INVALID_STATE: if event loop hasn't been initialized +*/ +esp_err_t esp_eth_driver_start(esp_eth_handle_t eth_handle); + /** * @brief Uninstall Ethernet driver * diff --git a/components/esp_eth/src/esp_eth.c b/components/esp_eth/src/esp_eth.c index 475becb45f..dd4f369a25 100644 --- a/components/esp_eth/src/esp_eth.c +++ b/components/esp_eth/src/esp_eth.c @@ -151,16 +151,9 @@ static void eth_check_link_timer_cb(TimerHandle_t xTimer) phy->get_link(phy); } -////////////////////////////////User face APIs//////////////////////////////////////////////// -// User has to pass the handle of Ethernet driver to each API. -// Different Ethernet driver instance is identified with a unique handle. -// It's helpful for us to support multiple Ethernet port on ESP32. -////////////////////////////////////////////////////////////////////////////////////////////// - -static esp_err_t esp_eth_driver_start(esp_netif_t * esp_netif, void * args) +static esp_err_t esp_eth_post_attach_driver_start(esp_netif_t * esp_netif, void * args) { uint8_t eth_mac[6]; - esp_err_t ret = ESP_OK; esp_eth_driver_t *eth_driver = args; eth_driver->base.netif = esp_netif; @@ -177,6 +170,18 @@ static esp_err_t esp_eth_driver_start(esp_netif_t * esp_netif, void * args) esp_netif_set_mac(esp_netif, eth_mac); ESP_LOGI(TAG, "ETH netif started"); + return esp_eth_driver_start(eth_driver); +} + +////////////////////////////////User face APIs//////////////////////////////////////////////// +// User has to pass the handle of Ethernet driver to each API. +// Different Ethernet driver instance is identified with a unique handle. +// It's helpful for us to support multiple Ethernet port on ESP32. +////////////////////////////////////////////////////////////////////////////////////////////// +esp_err_t esp_eth_driver_start(esp_eth_handle_t eth_handle) +{ + esp_err_t ret = ESP_OK; + esp_eth_driver_t *eth_driver = eth_handle; ETH_CHECK(esp_event_post(ETH_EVENT, ETHERNET_EVENT_START, ð_driver, sizeof(eth_driver), 0) == ESP_OK, "send ETHERNET_EVENT_START event failed", err_event, ESP_FAIL); @@ -216,7 +221,7 @@ esp_err_t esp_eth_driver_install(const esp_eth_config_t *config, esp_eth_handle_ eth_driver, eth_check_link_timer_cb); ETH_CHECK(eth_driver->check_link_timer, "create eth_link_timer failed", err_create_timer, ESP_FAIL); ETH_CHECK(xTimerStart(eth_driver->check_link_timer, 0) == pdPASS, "start eth_link_timer failed", err_start_timer, ESP_FAIL); - eth_driver->base.post_attach = esp_eth_driver_start; + eth_driver->base.post_attach = esp_eth_post_attach_driver_start; *out_hdl = (esp_eth_handle_t)eth_driver; tcpip_adapter_start_eth(eth_driver); return ESP_OK; diff --git a/examples/ethernet/eth2ap/main/ethernet_example_main.c b/examples/ethernet/eth2ap/main/ethernet_example_main.c index 7d65a5bff2..78c04b1e8b 100644 --- a/examples/ethernet/eth2ap/main/ethernet_example_main.c +++ b/examples/ethernet/eth2ap/main/ethernet_example_main.c @@ -185,6 +185,7 @@ static void initialize_ethernet(void) config.stack_input = pkt_eth2wifi; ESP_ERROR_CHECK(esp_eth_driver_install(&config, &s_eth_handle)); esp_eth_ioctl(s_eth_handle, ETH_CMD_S_PROMISCUOUS, (void *)true); + esp_eth_driver_start(s_eth_handle); } static void initialize_wifi(void) From cf710a3cb131b00bf8682b541d945b7fc7d93d14 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Fri, 13 Sep 2019 15:44:23 +0200 Subject: [PATCH 18/31] esp_netif: include tcpip_adapter legacy header by default to provide *implicit* compatiblity --- components/esp_netif/component.mk | 2 +- components/esp_netif/esp_netif_defaults.c | 6 ++--- components/esp_netif/esp_netif_handlers.c | 26 +------------------ components/esp_netif/include/esp_netif.h | 12 ++++++++- .../esp_netif/include/esp_netif_types.h | 20 +------------- .../esp_netif/loopback/esp_netif_loopback.c | 21 +++++++-------- components/esp_netif/lwip/esp_netif_lwip.c | 15 +++++------ .../esp_netif/lwip/esp_netif_lwip_defaults.c | 3 --- .../esp_netif/lwip/esp_netif_lwip_internal.h | 1 - components/esp_netif/test/test_esp_netif.c | 4 +-- components/lwip/port/esp32/netif/wlanif.c | 4 --- .../tcpip_adapter/include/tcpip_adapter.h | 9 ++++--- .../protocols/https_server/sdkconfig.defaults | 1 + .../protocols/mqtt/publish_test/sdkconfig.ci | 3 ++- examples/protocols/mqtt/ssl/sdkconfig.ci | 1 + examples/protocols/mqtt/tcp/sdkconfig.ci | 1 + examples/protocols/mqtt/ws/sdkconfig.ci | 1 + examples/protocols/mqtt/wss/sdkconfig.ci | 2 +- .../network_tests/main/lwip_test_netif.c | 4 +-- 19 files changed, 47 insertions(+), 89 deletions(-) diff --git a/components/esp_netif/component.mk b/components/esp_netif/component.mk index c45836abad..8402e569d2 100644 --- a/components/esp_netif/component.mk +++ b/components/esp_netif/component.mk @@ -3,4 +3,4 @@ # COMPONENT_ADD_INCLUDEDIRS := include COMPONENT_PRIV_INCLUDEDIRS := private_include lwip -COMPONENT_SRCDIRS := . lwip \ No newline at end of file +COMPONENT_SRCDIRS := . lwip loopback \ No newline at end of file diff --git a/components/esp_netif/esp_netif_defaults.c b/components/esp_netif/esp_netif_defaults.c index c1e20e2017..9bd38bb9d3 100644 --- a/components/esp_netif/esp_netif_defaults.c +++ b/components/esp_netif/esp_netif_defaults.c @@ -38,7 +38,7 @@ const esp_netif_inherent_config_t _g_esp_netif_inherent_sta_config = { .lost_ip_event = IP_EVENT_STA_LOST_IP, .get_ip_event = IP_EVENT_STA_GOT_IP, .if_key = "WIFI_STA_DEF", - .if_type = ESP_NETIF_TYPE_STA, + .if_desc = "sta", .route_prio = 100 }; @@ -53,7 +53,7 @@ const esp_netif_inherent_config_t _g_esp_netif_inherent_ap_config = { .flags = ESP_NETIF_DHCPS | ESP_NETIF_FLAG_AUTOUP, .ip_info = (esp_netif_ip_info_t*)&soft_ap_ip, .if_key = "WIFI_AP_DEF", - .if_type = ESP_NETIF_TYPE_AP, + .if_desc = "ap", .route_prio = 10 }; @@ -62,6 +62,6 @@ const esp_netif_inherent_config_t _g_esp_netif_inherent_eth_config = { .lost_ip_event = 0, .flags = ESP_NETIF_DHCPC | ESP_NETIF_FLAG_GARP | ESP_NETIF_FLAG_EVENT_IP_MODIFIED, .if_key = "ETH_DEF", - .if_type = ESP_NETIF_TYPE_ETH, + .if_desc = "eth", .route_prio = 50 }; diff --git a/components/esp_netif/esp_netif_handlers.c b/components/esp_netif/esp_netif_handlers.c index 8bad5f9117..04d3c5f7bf 100644 --- a/components/esp_netif/esp_netif_handlers.c +++ b/components/esp_netif/esp_netif_handlers.c @@ -28,30 +28,6 @@ static const char *TAG = "esp_netif_handlers"; -#define _STR(x) #x - -/** - * @brief This function converts interface type to string, which - * helps with backward compatibility of test infrastructure - * to check for standard message that specific interface (sta, ap, eth) - * obtained IP address - */ -static const char* get_netif_type(esp_netif_t* netif) { - const char* s_esp_netif_type_desc[] = { - _STR(ESP_NETIF_TYPE_UNKNOWN), - "sta", - "ap", - "eth", - _STR(ESP_NETIF_TYPE_OTHER) - }; - size_t type_nr = esp_netif_get_type(netif); - if (type_nr > sizeof(s_esp_netif_type_desc)/sizeof(s_esp_netif_type_desc[0])) { - type_nr = 0; - } - return s_esp_netif_type_desc[type_nr]; -} - - void esp_netif_action_start(void *esp_netif, esp_event_base_t base, int32_t event_id, void *data) { ESP_LOGD(TAG, "esp_netif action has started with netif%p from event_id=%d", esp_netif, event_id); @@ -117,7 +93,7 @@ void esp_netif_action_got_ip(void *esp_netif, esp_event_base_t base, int32_t eve { ESP_LOGD(TAG, "esp_netif action got_ip with netif%p from event_id=%d", esp_netif, event_id); const ip_event_got_ip_t *event = (const ip_event_got_ip_t *) data; - ESP_LOGI(TAG, "%s ip: " IPSTR ", mask: " IPSTR ", gw: " IPSTR, get_netif_type(esp_netif), + ESP_LOGI(TAG, "%s ip: " IPSTR ", mask: " IPSTR ", gw: " IPSTR, esp_netif_get_desc(esp_netif), IP2STR(&event->ip_info.ip), IP2STR(&event->ip_info.netmask), IP2STR(&event->ip_info.gw)); diff --git a/components/esp_netif/include/esp_netif.h b/components/esp_netif/include/esp_netif.h index d2ec9dd509..65623a716d 100644 --- a/components/esp_netif/include/esp_netif.h +++ b/components/esp_netif/include/esp_netif.h @@ -22,6 +22,16 @@ #include "esp_netif_types.h" #include "esp_netif_defaults.h" +// +// 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 +// +#if CONFIG_ESP_NETIF_TCPIP_ADAPTER_COMPATIBLE_LAYER +#define _ESP_NETIF_SUPPRESS_LEGACY_WARNING_ +#include "tcpip_adapter.h" +#undef _ESP_NETIF_SUPPRESS_LEGACY_WARNING_ +#endif // CONFIG_ESP_NETIF_TCPIP_ADAPTER_COMPATIBLE_LAYER + /** * @defgroup ESP_NETIF_INIT_API ESP-NETIF Initialization API * @brief Initialization and deinitialization of underlying TCP/IP stack and esp-netif instances @@ -679,7 +689,7 @@ const char *esp_netif_get_ifkey(esp_netif_t *esp_netif); * * @return Enumerated type of this interface, such as station, AP, ethernet */ -esp_netif_type_t esp_netif_get_type(esp_netif_t *esp_netif); +const char *esp_netif_get_desc(esp_netif_t *esp_netif); /** * @brief Returns configured event for this esp-netif instance and supplied event type diff --git a/components/esp_netif/include/esp_netif_types.h b/components/esp_netif/include/esp_netif_types.h index 364b498c59..908f151334 100644 --- a/components/esp_netif/include/esp_netif_types.h +++ b/components/esp_netif/include/esp_netif_types.h @@ -130,15 +130,6 @@ typedef enum esp_netif_flags { ESP_NETIF_FLAG_EVENT_IP_MODIFIED = 1 << 4 } esp_netif_flags_t; -typedef enum esp_netif_type { - ESP_NETIF_TYPE_UNKNOWN, - ESP_NETIF_TYPE_STA, - ESP_NETIF_TYPE_AP, - ESP_NETIF_TYPE_ETH, - ESP_NETIF_TYPE_OTHER, - ESP_NETIF_TYPE_MAX -} esp_netif_type_t; - typedef enum esp_netif_ip_event_type { ESP_NETIF_IP_EVENT_GOT_IP = 1, ESP_NETIF_IP_EVENT_LOST_IP = 2, @@ -158,8 +149,8 @@ typedef struct esp_netif_inherent_config { esp_netif_ip_info_t* ip_info; /*!< initial ip address for this interface */ uint32_t get_ip_event; /*!< event id to be raised when interface gets an IP */ uint32_t lost_ip_event; /*!< event id to be raised when interface losts its IP */ - esp_netif_type_t if_type; /*!< enum type of the interface */ const char * if_key; /*!< string identifier of the interface */ + const char * if_desc; /*!< textual description of the interface */ int route_prio; /*!< numeric priority of this interface to become a default routing if (if other netifs are up) */ } esp_netif_inherent_config_t; @@ -191,15 +182,6 @@ typedef struct esp_netif_driver_ifconfig esp_netif_driver_ifconfig_t; /** * @brief Specific L3 network stack configuration */ -typedef enum esp_netif_netstack_type { - ESP_NETIF_NETWORK_STACK_IS_LWIP = 0, - ESP_NETIF_NETWORK_STACK_IS_LOOPBACK = 1, - ESP_NETIF_NETWORK_STACK_MAX, -} esp_netif_netstack_type_t; - -typedef struct esp_netif_netstack_base_config { - esp_netif_netstack_type_t type; -} esp_netif_netstack_base_config_t; typedef struct esp_netif_netstack_config esp_netif_netstack_config_t; diff --git a/components/esp_netif/loopback/esp_netif_loopback.c b/components/esp_netif/loopback/esp_netif_loopback.c index 0052a686cb..5f481052c0 100644 --- a/components/esp_netif/loopback/esp_netif_loopback.c +++ b/components/esp_netif/loopback/esp_netif_loopback.c @@ -56,7 +56,7 @@ struct esp_netif_obj { esp_netif_flags_t flags; char * hostname; char * if_key; - esp_netif_type_t if_type; + char * if_desc; int route_prio; }; @@ -135,18 +135,14 @@ static esp_err_t esp_netif_init_configuration(esp_netif_t *esp_netif, const esp_ if (cfg->base->if_key) { esp_netif->if_key = strdup(cfg->base->if_key); } - if (cfg->base->if_type) { - esp_netif->if_type = cfg->base->if_type; + if (cfg->base->if_desc) { + esp_netif->if_desc = strdup(cfg->base->if_desc); } if (cfg->base->route_prio) { esp_netif->route_prio = cfg->base->route_prio; } - // Install network stack functions -- connects netif and L3 stack - if (cfg->stack->base.type != ESP_NETIF_NETWORK_STACK_IS_LOOPBACK) { - ESP_LOGE(TAG, "Failed to configure uknown network stack %d", cfg->stack->base.type); - return ESP_ERR_NOT_SUPPORTED; - } + // Network stack is bypassed in loopback interface // Install IO functions only if provided -- connects driver and netif // this configuration could be updated after esp_netif_new(), typically in post_attach callback @@ -215,6 +211,7 @@ void esp_netif_destroy(esp_netif_t *esp_netif) free(esp_netif->ip_info); free(esp_netif->ip_info_old); free(esp_netif->if_key); + free(esp_netif->if_desc); free(esp_netif); } } @@ -228,7 +225,7 @@ esp_err_t esp_netif_attach(esp_netif_t *esp_netif, esp_netif_iodriver_handle dri esp_err_t ret = base_driver->post_attach(esp_netif, driver_handle); if (ret != ESP_OK) { ESP_LOGE(TAG, "Post-attach callback of driver(%p) failed with %d", driver_handle, ret); - return ESP_ERR_ESP_NETIF_DRIVER_ATACH_FAILED; + return ESP_ERR_ESP_NETIF_DRIVER_ATTACH_FAILED; } } return ESP_OK; @@ -420,14 +417,14 @@ esp_netif_flags_t esp_netif_get_flags(esp_netif_t *esp_netif) return esp_netif->flags; } -char *esp_netif_get_ifkey(esp_netif_t *esp_netif) +const char *esp_netif_get_ifkey(esp_netif_t *esp_netif) { return esp_netif->if_key; } -esp_netif_type_t esp_netif_get_type(esp_netif_t *esp_netif) +const char *esp_netif_get_desc(esp_netif_t *esp_netif) { - return esp_netif->if_type; + return esp_netif->if_desc; } esp_netif_t *esp_netif_get_handle_from_ifkey(const char *if_key) diff --git a/components/esp_netif/lwip/esp_netif_lwip.c b/components/esp_netif/lwip/esp_netif_lwip.c index ef22c888b9..41080148a7 100644 --- a/components/esp_netif/lwip/esp_netif_lwip.c +++ b/components/esp_netif/lwip/esp_netif_lwip.c @@ -93,7 +93,7 @@ struct esp_netif_obj { esp_netif_flags_t flags; char * hostname; char * if_key; - esp_netif_type_t if_type; + char * if_desc; int route_prio; }; @@ -322,8 +322,8 @@ static esp_err_t esp_netif_init_configuration(esp_netif_t *esp_netif, const esp_ if (cfg->base->if_key) { esp_netif->if_key = strdup(cfg->base->if_key); } - if (cfg->base->if_type) { - esp_netif->if_type = cfg->base->if_type; + if (cfg->base->if_desc) { + esp_netif->if_desc = strdup(cfg->base->if_desc); } if (cfg->base->route_prio) { esp_netif->route_prio = cfg->base->route_prio; @@ -331,10 +331,6 @@ 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 (cfg->stack->base.type != ESP_NETIF_NETWORK_STACK_IS_LWIP) { - ESP_LOGE(TAG, "Failed to configure uknown network stack %d", cfg->stack->base.type); - return ESP_ERR_NOT_SUPPORTED; - } if (esp_netif_stack_config->init_fn) { esp_netif->lwip_init_fn = esp_netif_stack_config->init_fn; } @@ -448,6 +444,7 @@ void esp_netif_destroy(esp_netif_t *esp_netif) free(esp_netif->ip_info); free(esp_netif->ip_info_old); free(esp_netif->if_key); + free(esp_netif->if_desc); esp_netif_lwip_remove(esp_netif); free(esp_netif->lwip_netif); free(esp_netif->hostname); @@ -1348,9 +1345,9 @@ const char *esp_netif_get_ifkey(esp_netif_t *esp_netif) return esp_netif->if_key; } -esp_netif_type_t esp_netif_get_type(esp_netif_t *esp_netif) +const char *esp_netif_get_desc(esp_netif_t *esp_netif) { - return esp_netif->if_type; + return esp_netif->if_desc; } esp_netif_t *esp_netif_get_handle_from_ifkey(const char *if_key) diff --git a/components/esp_netif/lwip/esp_netif_lwip_defaults.c b/components/esp_netif/lwip/esp_netif_lwip_defaults.c index 6fce8067ac..6fc61088ac 100644 --- a/components/esp_netif/lwip/esp_netif_lwip_defaults.c +++ b/components/esp_netif/lwip/esp_netif_lwip_defaults.c @@ -24,17 +24,14 @@ // static const struct esp_netif_netstack_config s_eth_netif_config = { - .base = { .type = ESP_NETIF_NETWORK_STACK_IS_LWIP }, .init_fn = ethernetif_init, .input_fn = ethernetif_input }; static const struct esp_netif_netstack_config s_wifi_netif_config_ap = { - .base = { .type = ESP_NETIF_NETWORK_STACK_IS_LWIP }, .init_fn = wlanif_init_ap, .input_fn = wlanif_input }; static const struct esp_netif_netstack_config s_wifi_netif_config_sta = { - .base = { .type = ESP_NETIF_NETWORK_STACK_IS_LWIP }, .init_fn = wlanif_init_sta, .input_fn = wlanif_input }; diff --git a/components/esp_netif/lwip/esp_netif_lwip_internal.h b/components/esp_netif/lwip/esp_netif_lwip_internal.h index 46f2f123f7..56ac90d68b 100644 --- a/components/esp_netif/lwip/esp_netif_lwip_internal.h +++ b/components/esp_netif/lwip/esp_netif_lwip_internal.h @@ -19,7 +19,6 @@ // LWIP netif specific network stack configuration struct esp_netif_netstack_config { - esp_netif_netstack_base_config_t base; err_t (*init_fn)(struct netif*); void (*input_fn)(struct netif *netif, void *buffer, size_t len, void *eb); }; diff --git a/components/esp_netif/test/test_esp_netif.c b/components/esp_netif/test/test_esp_netif.c index b2b4ddbcda..329259c0b1 100644 --- a/components/esp_netif/test/test_esp_netif.c +++ b/components/esp_netif/test/test_esp_netif.c @@ -4,9 +4,7 @@ TEST_CASE("esp_netif: init and destroy", "[esp_netif][leaks=0]") { - esp_netif_inherent_config_t base = {}; - const esp_netif_netstack_base_config_t stack = { .type = ESP_NETIF_NETWORK_STACK_IS_LWIP }; - esp_netif_config_t cfg = { .base = &base, .stack = (const esp_netif_netstack_config_t*)&stack }; + esp_netif_config_t cfg = ESP_NETIF_DEFAULT_WIFI_STA(); esp_netif_t *esp_netif = esp_netif_new(NULL); TEST_ASSERT_EQUAL(NULL, esp_netif); diff --git a/components/lwip/port/esp32/netif/wlanif.c b/components/lwip/port/esp32/netif/wlanif.c index e4df53a0a1..4b5c839d1c 100644 --- a/components/lwip/port/esp32/netif/wlanif.c +++ b/components/lwip/port/esp32/netif/wlanif.c @@ -120,10 +120,6 @@ low_level_output(struct netif *netif, struct pbuf *p) if (esp_netif == NULL) { return ERR_IF; } - esp_netif_type_t type = esp_netif_get_type(esp_netif); - if (type != ESP_NETIF_TYPE_STA && type != ESP_NETIF_TYPE_AP) { - return ERR_IF; - } struct pbuf *q = p; err_t ret; diff --git a/components/tcpip_adapter/include/tcpip_adapter.h b/components/tcpip_adapter/include/tcpip_adapter.h index 90d0d6ae0f..c50493abe4 100644 --- a/components/tcpip_adapter/include/tcpip_adapter.h +++ b/components/tcpip_adapter/include/tcpip_adapter.h @@ -11,20 +11,23 @@ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. +#ifndef _ESP_NETIF_SUPPRESS_LEGACY_WARNING_ +#warning "This header is deprecated, please use new network related API in esp_netif.h" +#include "esp_netif.h" +#endif #ifndef _TCPIP_ADAPTER_H_ #define _TCPIP_ADAPTER_H_ -#warning "This header is deprecated, please use new network related API in esp_netif.h" - #include "esp_netif.h" + #include "tcpip_adapter_types.h" /** * @brief tcpip adapter legacy init. It is used only to set the compatibility mode of esp-netif, which * will enable backward compatibility of esp-netif. */ -void tcpip_adapter_init(void); +void tcpip_adapter_init(void) __attribute__ ((deprecated)); /** * @brief Compatiblity mode: convert the esp-netif handle to tcpip_adapter legacy interface enum diff --git a/examples/protocols/https_server/sdkconfig.defaults b/examples/protocols/https_server/sdkconfig.defaults index a9595bf0c1..abb6640942 100644 --- a/examples/protocols/https_server/sdkconfig.defaults +++ b/examples/protocols/https_server/sdkconfig.defaults @@ -1 +1,2 @@ CONFIG_ESP_HTTPS_SERVER_ENABLE=y +CONFIG_ESP_NETIF_TCPIP_ADAPTER_COMPATIBLE_LAYER=n diff --git a/examples/protocols/mqtt/publish_test/sdkconfig.ci b/examples/protocols/mqtt/publish_test/sdkconfig.ci index 34319fcf33..3ae004960c 100644 --- a/examples/protocols/mqtt/publish_test/sdkconfig.ci +++ b/examples/protocols/mqtt/publish_test/sdkconfig.ci @@ -5,4 +5,5 @@ CONFIG_EXAMPLE_BROKER_WSS_URI="wss://${EXAMPLE_MQTT_BROKER_WSS}/ws" CONFIG_EXAMPLE_BROKER_CERTIFICATE_OVERRIDE="${EXAMPLE_MQTT_BROKER_CERTIFICATE}" CONFIG_MBEDTLS_ASYMMETRIC_CONTENT_LEN=y CONFIG_MBEDTLS_SSL_IN_CONTENT_LEN=16384 -CONFIG_MBEDTLS_SSL_OUT_CONTENT_LEN=16384 \ No newline at end of file +CONFIG_MBEDTLS_SSL_OUT_CONTENT_LEN=16384 +CONFIG_ESP_NETIF_TCPIP_ADAPTER_COMPATIBLE_LAYER=n diff --git a/examples/protocols/mqtt/ssl/sdkconfig.ci b/examples/protocols/mqtt/ssl/sdkconfig.ci index b3557c28d7..716fb5bb08 100644 --- a/examples/protocols/mqtt/ssl/sdkconfig.ci +++ b/examples/protocols/mqtt/ssl/sdkconfig.ci @@ -10,3 +10,4 @@ CONFIG_MQTT_TASK_STACK_SIZE=6144 CONFIG_MBEDTLS_ASYMMETRIC_CONTENT_LEN=y CONFIG_MBEDTLS_SSL_IN_CONTENT_LEN=16384 CONFIG_MBEDTLS_SSL_OUT_CONTENT_LEN=4096 +CONFIG_ESP_NETIF_TCPIP_ADAPTER_COMPATIBLE_LAYER=n diff --git a/examples/protocols/mqtt/tcp/sdkconfig.ci b/examples/protocols/mqtt/tcp/sdkconfig.ci index 09ca8f37cc..43a5f637ec 100644 --- a/examples/protocols/mqtt/tcp/sdkconfig.ci +++ b/examples/protocols/mqtt/tcp/sdkconfig.ci @@ -1,2 +1,3 @@ CONFIG_LOG_DEFAULT_LEVEL_DEBUG=y CONFIG_BROKER_URL="FROM_STDIN" +CONFIG_ESP_NETIF_TCPIP_ADAPTER_COMPATIBLE_LAYER=n diff --git a/examples/protocols/mqtt/ws/sdkconfig.ci b/examples/protocols/mqtt/ws/sdkconfig.ci index 4f14eef990..caa4f0ca3c 100644 --- a/examples/protocols/mqtt/ws/sdkconfig.ci +++ b/examples/protocols/mqtt/ws/sdkconfig.ci @@ -1 +1,2 @@ CONFIG_BROKER_URI="ws://${EXAMPLE_MQTT_BROKER_WS}/ws" +CONFIG_ESP_NETIF_TCPIP_ADAPTER_COMPATIBLE_LAYER=n diff --git a/examples/protocols/mqtt/wss/sdkconfig.ci b/examples/protocols/mqtt/wss/sdkconfig.ci index d0dd4929e9..d088025f6a 100644 --- a/examples/protocols/mqtt/wss/sdkconfig.ci +++ b/examples/protocols/mqtt/wss/sdkconfig.ci @@ -1,3 +1,3 @@ CONFIG_BROKER_URI="wss://${EXAMPLE_MQTT_BROKER_WSS}/ws" CONFIG_BROKER_CERTIFICATE_OVERRIDE="${EXAMPLE_MQTT_BROKER_CERTIFICATE}" - +CONFIG_ESP_NETIF_TCPIP_ADAPTER_COMPATIBLE_LAYER=n diff --git a/examples/system/network_tests/main/lwip_test_netif.c b/examples/system/network_tests/main/lwip_test_netif.c index 0efbbbf2e0..859e518bb6 100644 --- a/examples/system/network_tests/main/lwip_test_netif.c +++ b/examples/system/network_tests/main/lwip_test_netif.c @@ -26,7 +26,6 @@ static struct netif *g_last_netif = NULL; // LWIP netif specific defines struct esp_netif_netstack_config { - esp_netif_netstack_base_config_t base; err_t (*init_fn)(struct netif*); void (*input_fn)(struct netif *netif, void *buffer, size_t len, void *eb); }; @@ -35,8 +34,7 @@ err_t testnetif_init(struct netif *netif); void testnetif_input(struct netif *netif, void *buffer, size_t len, void *eb); -const struct esp_netif_netstack_config _g_test_netif_stack_config = { { ESP_NETIF_NETWORK_STACK_IS_LWIP }, testnetif_init, testnetif_input}; - +const struct esp_netif_netstack_config _g_test_netif_stack_config = { testnetif_init, testnetif_input}; err_t testnetif_output(struct netif *netif, struct pbuf *p) { From 359f6b3a210a7b3c70b36c7cb443cf4931cfbabe Mon Sep 17 00:00:00 2001 From: David Cermak Date: Sun, 15 Sep 2019 09:47:43 +0200 Subject: [PATCH 19/31] esp_netif: add consistency checks for configs and interface key duplication --- components/esp_netif/lwip/esp_netif_lwip.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/components/esp_netif/lwip/esp_netif_lwip.c b/components/esp_netif/lwip/esp_netif_lwip.c index 41080148a7..269375e0f9 100644 --- a/components/esp_netif/lwip/esp_netif_lwip.c +++ b/components/esp_netif/lwip/esp_netif_lwip.c @@ -358,19 +358,27 @@ static esp_err_t esp_netif_init_configuration(esp_netif_t *esp_netif, const esp_ esp_netif_t *esp_netif_new(const esp_netif_config_t *esp_netif_config) { // mandatory configuration must be provided when creating esp_netif object - if (esp_netif_config == NULL) { + if (esp_netif_config == NULL || + esp_netif_config->base->if_key == NULL || + NULL != esp_netif_get_handle_from_ifkey(esp_netif_config->base->if_key)) { + ESP_LOGE(TAG, "%s: Failed to configure netif with config=%p (config or if_key is NULL or duplicate key)", + __func__, esp_netif_config); return NULL; } // Create parent esp-netif object esp_netif_t *esp_netif = calloc(1, sizeof(struct esp_netif_obj)); if (!esp_netif) { + ESP_LOGE(TAG, "Failed to allocate %d bytes (fee heap size %d)", sizeof(struct esp_netif_obj), + esp_get_free_heap_size()); return NULL; } // Create ip info esp_netif_ip_info_t *ip_info = calloc(1, sizeof(esp_netif_ip_info_t)); if (!ip_info) { + ESP_LOGE(TAG, "Failed to allocate %d bytes (fee heap size %d)", sizeof(esp_netif_ip_info_t), + esp_get_free_heap_size()); free(esp_netif); return NULL; } @@ -379,6 +387,8 @@ esp_netif_t *esp_netif_new(const esp_netif_config_t *esp_netif_config) // creating another ip info (to store old ip) ip_info = calloc(1, sizeof(esp_netif_ip_info_t)); if (!ip_info) { + ESP_LOGE(TAG, "Failed to allocate %d bytes (fee heap size %d)", sizeof(esp_netif_ip_info_t), + esp_get_free_heap_size()); free(esp_netif->ip_info); free(esp_netif); return NULL; @@ -388,6 +398,8 @@ esp_netif_t *esp_netif_new(const esp_netif_config_t *esp_netif_config) // Create underlying lwip netif struct netif * lwip_netif = calloc(1, sizeof(struct netif)); if (!lwip_netif) { + ESP_LOGE(TAG, "Failed to allocate %d bytes (fee heap size %d)", sizeof(struct netif), + esp_get_free_heap_size()); free(esp_netif->ip_info_old); free(esp_netif->ip_info); free(esp_netif); From 20add7da60d2e7c1a2f372258bdd286a9b30865e Mon Sep 17 00:00:00 2001 From: David Cermak Date: Sun, 15 Sep 2019 19:49:45 +0200 Subject: [PATCH 20/31] esp_netif: extract wifi_netif module as an abstraction to wifi universal interface defined by if handle and callback --- .../esp_netif/include/esp_netif_types.h | 6 +- components/esp_wifi/CMakeLists.txt | 1 + .../esp_wifi/include/esp_wifi_default.h | 32 ++- components/esp_wifi/include/esp_wifi_netif.h | 84 ++++++++ components/esp_wifi/src/wifi_default.c | 202 ++++++++++-------- components/esp_wifi/src/wifi_netif.c | 146 +++++++++++++ .../tcpip_adapter/tcpip_adapter_compat.c | 13 +- .../protocol_examples_common/connect.c | 3 +- .../internal_communication/main/mesh_main.c | 8 +- .../mesh/manual_networking/main/mesh_main.c | 8 +- 10 files changed, 389 insertions(+), 114 deletions(-) create mode 100644 components/esp_wifi/include/esp_wifi_netif.h create mode 100644 components/esp_wifi/src/wifi_netif.c diff --git a/components/esp_netif/include/esp_netif_types.h b/components/esp_netif/include/esp_netif_types.h index 908f151334..8f4d43c5cc 100644 --- a/components/esp_netif/include/esp_netif_types.h +++ b/components/esp_netif/include/esp_netif_types.h @@ -176,7 +176,6 @@ struct esp_netif_driver_ifconfig { void (*driver_free_rx_buffer)(void *h, void* buffer); }; -//typedef struct esp_netif_net_stack_ifconfig esp_netif_net_stack_ifconfig_t; typedef struct esp_netif_driver_ifconfig esp_netif_driver_ifconfig_t; /** @@ -194,4 +193,9 @@ struct esp_netif_config { const esp_netif_netstack_config_t *stack; }; +/** + * @brief ESP-NETIF Receive function type + */ +typedef esp_err_t (*esp_netif_receive_t)(esp_netif_t *esp_netif, void *buffer, size_t len, void *eb); + #endif // _ESP_NETIF_TYPES_H_ \ No newline at end of file diff --git a/components/esp_wifi/CMakeLists.txt b/components/esp_wifi/CMakeLists.txt index ec7432648d..7338c480c7 100644 --- a/components/esp_wifi/CMakeLists.txt +++ b/components/esp_wifi/CMakeLists.txt @@ -16,6 +16,7 @@ idf_component_register(SRCS "src/coexist.c" "src/smartconfig_ack.c" "src/wifi_init.c" "src/wifi_default.c" + "src/wifi_netif.c" INCLUDE_DIRS "include" "${idf_target}/include" PRIV_REQUIRES wpa_supplicant nvs_flash esp_netif LDFRAGMENTS "${ldfragments}") diff --git a/components/esp_wifi/include/esp_wifi_default.h b/components/esp_wifi/include/esp_wifi_default.h index 3dfc5a4b58..a6fb03014f 100644 --- a/components/esp_wifi/include/esp_wifi_default.h +++ b/components/esp_wifi/include/esp_wifi_default.h @@ -16,26 +16,42 @@ #define _ESP_WIFI_DEFAULT_H /** - * @brief Sets default wifi event handlers for STA interface + * @brief Attaches wifi station interface to supplied netif * - * @param esp_netif instance of corresponding if object + * @param esp_netif instance to attach the wifi station to * * @return - * - ESP_OK on success, error returned from esp_event_handler_register if failed + * - ESP_OK on success + * - ESP_FAIL if attach failed */ -esp_err_t esp_wifi_set_default_wifi_sta_handlers(void *esp_netif); +esp_err_t esp_netif_attach_wifi_station(esp_netif_t *esp_netif); + +/** + * @brief Attaches wifi soft AP interface to supplied netif + * + * @param esp_netif instance to attach the wifi AP to + * + * @return + * - ESP_OK on success + * - ESP_FAIL if attach failed + */ +esp_err_t esp_netif_attach_wifi_ap(esp_netif_t *esp_netif); /** * @brief Sets default wifi event handlers for STA interface * - * @param esp_netif instance of corresponding if object + * @return + * - ESP_OK on success, error returned from esp_event_handler_register if failed + */ +esp_err_t esp_wifi_set_default_wifi_sta_handlers(void); + +/** + * @brief Sets default wifi event handlers for STA interface * * @return * - ESP_OK on success, error returned from esp_event_handler_register if failed */ -esp_err_t esp_wifi_set_default_wifi_driver_and_handlers(wifi_interface_t wifi_if, void *esp_netif); - -esp_err_t esp_wifi_set_default_wifi_ap_handlers(void *esp_netif); +esp_err_t esp_wifi_set_default_wifi_ap_handlers(void); /** * @brief Clears default wifi event handlers for supplied network interface diff --git a/components/esp_wifi/include/esp_wifi_netif.h b/components/esp_wifi/include/esp_wifi_netif.h new file mode 100644 index 0000000000..777ab5b07e --- /dev/null +++ b/components/esp_wifi/include/esp_wifi_netif.h @@ -0,0 +1,84 @@ +// Copyright 2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef _ESP_WIFI_NETIF_H +#define _ESP_WIFI_NETIF_H + +/** + * @brief Number of WiFi interfaces used by wifi-netif abstraction + */ +#define MAX_WIFI_IFS (2) + +/** + * @brief Forward declaration of WiFi interface handle + */ +typedef struct wifi_netif_driver* wifi_netif_driver_t; + +/** + * @brief Creates wifi driver instance to be used with esp-netif + * + * @param wifi_if wifi interface type (station, softAP) + * + * @return + * - pointer to wifi interface handle on success + * - NULL otherwise + */ +wifi_netif_driver_t esp_wifi_create_if_driver(wifi_interface_t wifi_if); + +/** + * @brief Destroys wifi driver instance + * + * @param h pointer to wifi interface handle + * + */ +void esp_wifi_destroy_if_driver(wifi_netif_driver_t h); + +/** + * @brief Return mac of specified wifi driver instance + * + * @param[in] ifx pointer to wifi interface handle + * @param[out] mac output mac address + * + * @return ESP_OK on success + * + */ +esp_err_t esp_wifi_get_if_mac(wifi_netif_driver_t ifx, uint8_t mac[6]); + +/** + * @brief Return true if the supplied interface instance is ready after start. + * Typically used when registering on receive callback, which ought to be + * installed as soon as AP started, but once STA gets connected. + * + * @param[in] ifx pointer to wifi interface handle + * + * @return + * - true if ready after intertace started (typically Access Point type) + * - false if ready once intertace connected (typically for Station type) + */ +bool esp_wifi_is_if_ready_when_stared(wifi_netif_driver_t ifx); + +/** + * @brief Register interface receive callback function with argument + * + * @param[in] ifx pointer to wifi interface handle + * @param[in] fn funtion to be registered (typically esp_netif_receive) + * @param[in] arg argument to be supplied to registered function (typically esp_netif ptr) + * + * @return ESP_OK on success + * + */ +esp_err_t esp_wifi_register_if_rxcb(wifi_netif_driver_t ifx, esp_netif_receive_t fn, void * arg); + + +#endif //_ESP_WIFI_NETIF_H \ No newline at end of file diff --git a/components/esp_wifi/src/wifi_default.c b/components/esp_wifi/src/wifi_default.c index 251f12d640..ab45ecdd5c 100644 --- a/components/esp_wifi/src/wifi_default.c +++ b/components/esp_wifi/src/wifi_default.c @@ -15,66 +15,44 @@ #include "esp_netif.h" #include "esp_log.h" #include "esp_private/wifi.h" +#include "esp_wifi_netif.h" // // Purpose of this module is to provide basic wifi initialization setup for -// station and AP and their conversion to esp-netif objects -// Also this module holds esp-netif handles for AP and STA +// default station and AP and to register default handles for these interfaces // - -typedef struct wifi_netif_driver { - esp_netif_driver_base_t base; - wifi_interface_t wifi_if; -}* wifi_netif_driver_t; - static const char* TAG = "wifi_init_default"; -#define MAX_WIFI_IFS (2) - static esp_netif_t *s_wifi_netifs[MAX_WIFI_IFS] = { NULL }; static bool wifi_default_handlers_set = false; -static esp_err_t wifi_sta_receive(void *buffer, uint16_t len, void *eb) -{ - return esp_netif_receive(s_wifi_netifs[WIFI_IF_STA], buffer, len, eb); -} +static esp_err_t disconnect_and_destroy(esp_netif_t* esp_netif); -static esp_err_t wifi_ap_receive(void *buffer, uint16_t len, void *eb) -{ - return esp_netif_receive(s_wifi_netifs[WIFI_IF_AP], buffer, len, eb); -} +// +// Default event handlers +// -void wifi_free(void *h, void* buffer) +/** + * @brief Wifi start action when station or AP get started + */ +static void wifi_start(void *esp_netif, esp_event_base_t base, int32_t event_id, void *data) { - esp_wifi_internal_free_rx_buffer(buffer); -} - -esp_err_t wifi_transmit(void *h, void *buffer, size_t len) -{ - wifi_netif_driver_t driver = h; - return esp_wifi_internal_tx(driver->wifi_if, buffer, len); -} - -void wifi_start(void *esp_netif, esp_event_base_t base, int32_t event_id, void *data) -{ - ESP_LOGD(TAG, "%s esp-netif:%p event-id%d", __func__, esp_netif, event_id); - uint8_t mac[6]; esp_err_t ret; - wifi_netif_driver_t driver = esp_netif_get_io_driver(esp_netif); - wifi_interface_t wifi_interface = driver->wifi_if; + ESP_LOGD(TAG, "%s esp-netif:%p event-id%d", __func__, esp_netif, event_id); - if ((ret = esp_wifi_get_mac(wifi_interface, mac)) != ESP_OK) { + wifi_netif_driver_t driver = esp_netif_get_io_driver(esp_netif); + + if ((ret = esp_wifi_get_if_mac(driver, mac)) != ESP_OK) { ESP_LOGE(TAG, "esp_wifi_get_mac failed with %d", ret); return; } ESP_LOGD(TAG, "WIFI mac address: %x %x %x %x %x %x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); - if (wifi_interface == ESP_IF_WIFI_AP) { - // By default register wifi rxcb on start for AP only, station gets it registered on connect event - if ((ret = esp_wifi_internal_reg_rxcb(wifi_interface, wifi_ap_receive)) != ESP_OK) { - ESP_LOGE(TAG, "esp_wifi_internal_reg_rxcb for if=%d failed with %d", wifi_interface, ret); + if (esp_wifi_is_if_ready_when_stared(driver)) { + if ((ret = esp_wifi_register_if_rxcb(driver, esp_netif_receive, esp_netif)) != ESP_OK) { + ESP_LOGE(TAG, "esp_wifi_register_if_rxcb for if=%p failed with %d", driver, ret); return; } } @@ -83,6 +61,9 @@ void wifi_start(void *esp_netif, esp_event_base_t base, int32_t event_id, void * esp_netif_action_start(esp_netif, base, event_id, data); } +/** + * @brief Wifi default handlers for specific events for station and APs + */ static void wifi_default_action_sta_start(void *arg, esp_event_base_t base, int32_t event_id, void *data) { @@ -102,10 +83,15 @@ static void wifi_default_action_sta_connected(void *arg, esp_event_base_t base, { if (s_wifi_netifs[WIFI_IF_STA] != NULL) { esp_err_t ret; - // By default register wifi rxcb once the STA gets connected - if ((ret = esp_wifi_internal_reg_rxcb(ESP_IF_WIFI_STA, wifi_sta_receive)) != ESP_OK) { - ESP_LOGE(TAG, "esp_wifi_internal_reg_rxcb for if=%d failed with %d", ESP_IF_WIFI_STA, ret); - return; + esp_netif_t *esp_netif = s_wifi_netifs[WIFI_IF_STA]; + wifi_netif_driver_t driver = esp_netif_get_io_driver(esp_netif); + + if (!esp_wifi_is_if_ready_when_stared(driver)) { + // if interface not ready when started, rxcb to be registered on connection + if ((ret = esp_wifi_register_if_rxcb(driver, esp_netif_receive, esp_netif)) != ESP_OK) { + ESP_LOGE(TAG, "esp_wifi_register_if_rxcb for if=%p failed with %d", driver, ret); + return; + } } esp_netif_action_connected(s_wifi_netifs[WIFI_IF_STA], base, event_id, data); @@ -145,7 +131,10 @@ static void wifi_default_action_sta_got_ip(void *arg, esp_event_base_t base, int } } -esp_err_t esp_wifi_clear_default_wifi_handlers(void) +/** + * @brief Clear default handlers + */ +esp_err_t _esp_wifi_clear_default_wifi_handlers(void) { esp_event_handler_unregister(WIFI_EVENT, WIFI_EVENT_STA_START, wifi_default_action_sta_start); esp_event_handler_unregister(WIFI_EVENT, WIFI_EVENT_STA_STOP, wifi_default_action_sta_stop); @@ -159,42 +148,9 @@ esp_err_t esp_wifi_clear_default_wifi_handlers(void) return ESP_OK; } -static esp_err_t wifi_driver_start(esp_netif_t * esp_netif, void * args) -{ - wifi_netif_driver_t driver = args; - driver->base.netif = esp_netif; - esp_netif_driver_ifconfig_t driver_ifconfig = { - .handle = driver, - .transmit = wifi_transmit, - .driver_free_rx_buffer = wifi_free - }; - - return esp_netif_set_driver_config(esp_netif, &driver_ifconfig); -} - -static esp_err_t disconnect_and_destroy(esp_netif_t* esp_netif) -{ - wifi_netif_driver_t driver = esp_netif_get_io_driver(esp_netif); - driver->base.netif = NULL; - esp_netif_driver_ifconfig_t driver_ifconfig = { }; - esp_err_t ret = esp_netif_set_driver_config(esp_netif, &driver_ifconfig); - free(driver); - return ret; -} - -static esp_err_t create_and_attach(wifi_interface_t wifi_if, esp_netif_t* esp_netif) -{ - wifi_netif_driver_t driver = calloc(1, sizeof(struct wifi_netif_driver)); - if (driver == NULL) { - ESP_LOGE(TAG, "No memory to create a wifi driver"); - return ESP_ERR_NO_MEM; - } - driver->wifi_if = wifi_if; - driver->base.post_attach = wifi_driver_start; - esp_netif_attach(esp_netif, driver); - return ESP_OK; -} - +/** + * @brief Set default handlers + */ esp_err_t _esp_wifi_set_default_wifi_handlers(void) { if (wifi_default_handlers_set) { @@ -244,19 +200,29 @@ esp_err_t _esp_wifi_set_default_wifi_handlers(void) return ESP_OK; fail: - esp_wifi_clear_default_wifi_handlers(); + _esp_wifi_clear_default_wifi_handlers(); return err; } -esp_err_t esp_wifi_set_default_wifi_driver_and_handlers(wifi_interface_t wifi_if, void *esp_netif) +/** + * @brief Set default handlers for station (official API) + */ +esp_err_t esp_wifi_set_default_wifi_sta_handlers(void) { - assert(esp_netif); - assert(wifi_if < MAX_WIFI_IFS); - s_wifi_netifs[wifi_if] = esp_netif; - ESP_ERROR_CHECK(create_and_attach(wifi_if, esp_netif)); return _esp_wifi_set_default_wifi_handlers(); } +/** + * @brief Set default handlers for AP (official API) + */ +esp_err_t esp_wifi_set_default_wifi_ap_handlers(void) +{ + return _esp_wifi_set_default_wifi_handlers(); +} + +/** + * @brief Clear default handlers and destroy appropriate objects (official API) + */ esp_err_t esp_wifi_clear_default_wifi_driver_and_handlers(void *esp_netif) { int i; @@ -273,25 +239,83 @@ esp_err_t esp_wifi_clear_default_wifi_driver_and_handlers(void *esp_netif) if (i == MAX_WIFI_IFS) { // if all wifi default netifs are null ESP_LOGD(TAG, "Clearing wifi default handlers"); - esp_wifi_clear_default_wifi_handlers(); + _esp_wifi_clear_default_wifi_handlers(); } return disconnect_and_destroy(esp_netif); } + +// +// Object manipulation +// + +/** + * @brief Create and destroy objects + */ +static esp_err_t disconnect_and_destroy(esp_netif_t* esp_netif) +{ + wifi_netif_driver_t driver = esp_netif_get_io_driver(esp_netif); + esp_netif_driver_ifconfig_t driver_ifconfig = { }; + esp_err_t ret = esp_netif_set_driver_config(esp_netif, &driver_ifconfig); + esp_wifi_destroy_if_driver(driver); + return ret; +} + +static esp_err_t create_and_attach(wifi_interface_t wifi_if, esp_netif_t* esp_netif) +{ + wifi_netif_driver_t driver = esp_wifi_create_if_driver(wifi_if); + if (driver == NULL) { + ESP_LOGE(TAG, "Failed to create wifi interface handle"); + return ESP_FAIL; + } + return esp_netif_attach(esp_netif, driver); +} + +esp_err_t esp_netif_attach_wifi_station(esp_netif_t *esp_netif) +{ + if (esp_netif == NULL) { + return ESP_ERR_INVALID_ARG; + } + s_wifi_netifs[WIFI_IF_STA] = esp_netif; + return create_and_attach(WIFI_IF_STA, esp_netif); +} + +esp_err_t esp_netif_attach_wifi_ap(esp_netif_t *esp_netif) +{ + if (esp_netif == NULL) { + return ESP_ERR_INVALID_ARG; + } + s_wifi_netifs[WIFI_IF_AP] = esp_netif; + return create_and_attach(WIFI_IF_AP, esp_netif); +} + + +// +// Default WiFi creation from user code +// + +/** + * @brief User init default AP (official API) + */ esp_netif_t* esp_netif_create_default_wifi_ap(void) { esp_netif_config_t cfg = ESP_NETIF_DEFAULT_WIFI_AP(); esp_netif_t *netif = esp_netif_new(&cfg); assert(netif); - esp_wifi_set_default_wifi_driver_and_handlers(ESP_IF_WIFI_AP, netif); + esp_netif_attach_wifi_ap(netif); + esp_wifi_set_default_wifi_ap_handlers(); return netif; } +/** + * @brief User init default station (official API) + */ esp_netif_t* esp_netif_create_default_wifi_sta(void) { esp_netif_config_t cfg = ESP_NETIF_DEFAULT_WIFI_STA(); esp_netif_t *netif = esp_netif_new(&cfg); assert(netif); - esp_wifi_set_default_wifi_driver_and_handlers(ESP_IF_WIFI_STA, netif); + esp_netif_attach_wifi_station(netif); + esp_wifi_set_default_wifi_sta_handlers(); return netif; } \ No newline at end of file diff --git a/components/esp_wifi/src/wifi_netif.c b/components/esp_wifi/src/wifi_netif.c new file mode 100644 index 0000000000..884b2e33b1 --- /dev/null +++ b/components/esp_wifi/src/wifi_netif.c @@ -0,0 +1,146 @@ +// Copyright 2018 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +#include "esp_wifi.h" +#include "esp_netif.h" +#include "esp_log.h" +#include "esp_private/wifi.h" +#include "esp_wifi_netif.h" + +// +// Purpose of this module is provide object oriented abstraction to wifi interfaces +// in order to integrate wifi as esp-netif driver +// + +/** + * @brief WiFi netif driver structure + */ +typedef struct wifi_netif_driver { + esp_netif_driver_base_t base; + wifi_interface_t wifi_if; +}* wifi_netif_driver_t; + +static const char* TAG = "wifi_netif"; + +/** + * @brief Local storage for netif handles and callbacks for specific wifi interfaces + */ +static esp_netif_receive_t s_wifi_rxcbs[MAX_WIFI_IFS] = { NULL }; +static esp_netif_t *s_wifi_netifs[MAX_WIFI_IFS] = { NULL }; + +/** + * @brief WiFi netif driver IO functions, a thin glue layer + * to the original wifi interface API + */ +static esp_err_t wifi_sta_receive(void *buffer, uint16_t len, void *eb) +{ + return s_wifi_rxcbs[WIFI_IF_STA](s_wifi_netifs[WIFI_IF_STA], buffer, len, eb); +} + +static esp_err_t wifi_ap_receive(void *buffer, uint16_t len, void *eb) +{ + return s_wifi_rxcbs[WIFI_IF_AP](s_wifi_netifs[WIFI_IF_AP], buffer, len, eb); +} + +static void wifi_free(void *h, void* buffer) +{ + esp_wifi_internal_free_rx_buffer(buffer); +} + +static esp_err_t wifi_transmit(void *h, void *buffer, size_t len) +{ + wifi_netif_driver_t driver = h; + return esp_wifi_internal_tx(driver->wifi_if, buffer, len); +} + +static esp_err_t wifi_driver_start(esp_netif_t * esp_netif, void * args) +{ + wifi_netif_driver_t driver = args; + driver->base.netif = esp_netif; + esp_netif_driver_ifconfig_t driver_ifconfig = { + .handle = driver, + .transmit = wifi_transmit, + .driver_free_rx_buffer = wifi_free + }; + + return esp_netif_set_driver_config(esp_netif, &driver_ifconfig); +} + +void esp_wifi_destroy_if_driver(wifi_netif_driver_t h) +{ + free(h); +} + +wifi_netif_driver_t esp_wifi_create_if_driver(wifi_interface_t wifi_if) +{ + wifi_netif_driver_t driver = calloc(1, sizeof(struct wifi_netif_driver)); + if (driver == NULL) { + ESP_LOGE(TAG, "No memory to create a wifi interface handle"); + return NULL; + } + driver->wifi_if = wifi_if; + driver->base.post_attach = wifi_driver_start; + return driver; +} + +esp_err_t esp_wifi_get_if_mac(wifi_netif_driver_t ifx, uint8_t mac[6]) +{ + wifi_interface_t wifi_interface = ifx->wifi_if; + + return esp_wifi_get_mac(wifi_interface, mac); +} + +bool esp_wifi_is_if_ready_when_stared(wifi_netif_driver_t ifx) +{ + // WiFi rxcb to be register wifi rxcb on start for AP only, station gets it registered on connect event + return (ifx->wifi_if == WIFI_IF_AP); +} + +esp_err_t esp_wifi_register_if_rxcb(wifi_netif_driver_t ifx, esp_netif_receive_t fn, void * arg) +{ + if (ifx->base.netif != arg) { + ESP_LOGE(TAG, "Invalid argument: supplied netif=%p does not equal to interface netif=%p", arg, ifx->base.netif); + return ESP_ERR_INVALID_ARG; + } + wifi_interface_t wifi_interface = ifx->wifi_if; + s_wifi_rxcbs[wifi_interface] = fn; + wifi_rxcb_t rxcb = NULL; + esp_err_t ret; + + switch (wifi_interface) + { + case WIFI_IF_STA: + rxcb = wifi_sta_receive; + break; + + case WIFI_IF_AP: + rxcb = wifi_ap_receive; + break; + + default: + break; + } + + if (rxcb == NULL) { + ESP_LOGE(TAG, "Unknown wifi interface id if=%d", wifi_interface); + return ESP_ERR_NOT_SUPPORTED; + } + + if ((ret = esp_wifi_internal_reg_rxcb(wifi_interface, rxcb)) != ESP_OK) { + ESP_LOGE(TAG, "esp_wifi_internal_reg_rxcb for if=%d failed with %d", wifi_interface, ret); + return ESP_ERR_INVALID_STATE; + } + + s_wifi_netifs[wifi_interface] = ifx->base.netif; + return ESP_OK; +} diff --git a/components/tcpip_adapter/tcpip_adapter_compat.c b/components/tcpip_adapter/tcpip_adapter_compat.c index 5efc1574de..3cc87a2668 100644 --- a/components/tcpip_adapter/tcpip_adapter_compat.c +++ b/components/tcpip_adapter/tcpip_adapter_compat.c @@ -26,8 +26,7 @@ extern void _esp_wifi_set_default_ap_netif(esp_netif_t* esp_netif); extern void _esp_wifi_set_default_sta_netif(esp_netif_t* esp_netif); extern esp_err_t _esp_wifi_set_default_wifi_handlers(void); -extern esp_err_t esp_eth_set_default_handlers(void *esp_netif); -extern esp_err_t esp_wifi_clear_default_wifi_handlers(void); +extern esp_err_t _esp_wifi_clear_default_wifi_handlers(void); // // Purpose of this module is to provide backward compatible version of esp-netif @@ -45,15 +44,14 @@ static const char* s_netif_keyif[TCPIP_ADAPTER_IF_MAX] = { static bool s_tcpip_adapter_compat = false; -void wifi_start(void *esp_netif, esp_event_base_t base, int32_t event_id, void *data); - static void wifi_create_and_start_ap(void *esp_netif, esp_event_base_t base, int32_t event_id, void *data) { if (s_esp_netifs[TCPIP_ADAPTER_IF_AP] == NULL) { esp_netif_config_t cfg = ESP_NETIF_DEFAULT_WIFI_AP(); esp_netif_t *ap_netif = esp_netif_new(&cfg); - esp_wifi_set_default_wifi_driver_and_handlers(ESP_IF_WIFI_AP, ap_netif); + esp_netif_attach_wifi_ap(ap_netif); + esp_wifi_set_default_wifi_sta_handlers(); s_esp_netifs[TCPIP_ADAPTER_IF_AP] = ap_netif; } } @@ -64,7 +62,8 @@ static void wifi_create_and_start_sta(void *esp_netif, esp_event_base_t base, in esp_netif_config_t cfg = ESP_NETIF_DEFAULT_WIFI_STA(); esp_netif_t *sta_netif = esp_netif_new(&cfg); - esp_wifi_set_default_wifi_driver_and_handlers(ESP_IF_WIFI_STA, sta_netif); + esp_netif_attach_wifi_station(sta_netif); + esp_wifi_set_default_wifi_sta_handlers(); s_esp_netifs[TCPIP_ADAPTER_IF_STA] = sta_netif; } } @@ -168,7 +167,7 @@ esp_err_t tcpip_adapter_set_default_wifi_handlers(void) esp_err_t tcpip_adapter_clear_default_wifi_handlers(void) { - return esp_wifi_clear_default_wifi_handlers(); + return _esp_wifi_clear_default_wifi_handlers(); } tcpip_adapter_if_t tcpip_adapter_if_from_esp_netif(esp_netif_t *esp_netif) diff --git a/examples/common_components/protocol_examples_common/connect.c b/examples/common_components/protocol_examples_common/connect.c index f9b974fba4..b161e9a6ec 100644 --- a/examples/common_components/protocol_examples_common/connect.c +++ b/examples/common_components/protocol_examples_common/connect.c @@ -137,7 +137,8 @@ static void start(void) assert(netif); - esp_wifi_set_default_wifi_driver_and_handlers(ESP_IF_WIFI_STA, netif); + esp_netif_attach_wifi_station(netif); + esp_wifi_set_default_wifi_sta_handlers(); s_example_esp_netif = netif; diff --git a/examples/mesh/internal_communication/main/mesh_main.c b/examples/mesh/internal_communication/main/mesh_main.c index 2e564551bb..2c29d51e4e 100644 --- a/examples/mesh/internal_communication/main/mesh_main.c +++ b/examples/mesh/internal_communication/main/mesh_main.c @@ -8,7 +8,6 @@ */ #include #include "esp_wifi.h" -#include "esp_wifi_default.h" #include "esp_system.h" #include "esp_event.h" #include "esp_log.h" @@ -388,8 +387,8 @@ static esp_err_t create_wifi_netifs(void) }; esp_netif_t *netif_ap = esp_netif_new(&cfg_ap); assert(netif_ap); - ESP_ERROR_CHECK(esp_wifi_set_default_wifi_driver_and_handlers(ESP_IF_WIFI_AP, netif_ap)); - + ESP_ERROR_CHECK(esp_netif_attach_wifi_ap(netif_ap)); + ESP_ERROR_CHECK(esp_wifi_set_default_wifi_sta_handlers()); memcpy(&netif_cfg, ESP_NETIF_BASE_DEFAULT_WIFI_STA, sizeof(netif_cfg)); netif_cfg.flags &= ~ESP_NETIF_DHCPC; @@ -400,7 +399,8 @@ static esp_err_t create_wifi_netifs(void) netif_sta = esp_netif_new(&cfg_sta); assert(netif_sta); - ESP_ERROR_CHECK(esp_wifi_set_default_wifi_driver_and_handlers(ESP_IF_WIFI_STA, netif_sta)); + ESP_ERROR_CHECK(esp_netif_attach_wifi_station(netif_sta)); + ESP_ERROR_CHECK(esp_wifi_set_default_wifi_sta_handlers()); return ESP_OK; } diff --git a/examples/mesh/manual_networking/main/mesh_main.c b/examples/mesh/manual_networking/main/mesh_main.c index c4c481b09d..bb203b67ee 100644 --- a/examples/mesh/manual_networking/main/mesh_main.c +++ b/examples/mesh/manual_networking/main/mesh_main.c @@ -8,7 +8,6 @@ */ #include #include "esp_wifi.h" -#include "esp_wifi_default.h" #include "esp_system.h" #include "esp_event.h" #include "esp_log.h" @@ -312,8 +311,8 @@ static esp_err_t create_wifi_netifs(void) }; esp_netif_t *netif_ap = esp_netif_new(&cfg_ap); assert(netif_ap); - ESP_ERROR_CHECK(esp_wifi_set_default_wifi_driver_and_handlers(ESP_IF_WIFI_AP, netif_ap)); - + ESP_ERROR_CHECK(esp_netif_attach_wifi_ap(netif_ap)); + ESP_ERROR_CHECK(esp_wifi_set_default_wifi_sta_handlers()); memcpy(&netif_cfg, ESP_NETIF_BASE_DEFAULT_WIFI_STA, sizeof(netif_cfg)); netif_cfg.flags &= ~ESP_NETIF_DHCPC; @@ -324,7 +323,8 @@ static esp_err_t create_wifi_netifs(void) netif_sta = esp_netif_new(&cfg_sta); assert(netif_sta); - ESP_ERROR_CHECK(esp_wifi_set_default_wifi_driver_and_handlers(ESP_IF_WIFI_STA, netif_sta)); + ESP_ERROR_CHECK(esp_netif_attach_wifi_station(netif_sta)); + ESP_ERROR_CHECK(esp_wifi_set_default_wifi_sta_handlers()); return ESP_OK; } From 6e0d274f58aefb81cd5b8cae14cee2137de1085c Mon Sep 17 00:00:00 2001 From: David Cermak Date: Thu, 17 Oct 2019 11:36:34 +0200 Subject: [PATCH 21/31] esp_netif: example init code fixed to assert only variables, not function calls so it won't be skipped if compiled without asserts --- .../bluetooth/bluedroid/ble/blufi/main/blufi_example_main.c | 3 ++- .../ble_mesh_wifi_coexist/components/iperf/cmd_wifi.c | 6 ++++-- examples/protocols/esp_local_ctrl/main/app_main.c | 3 ++- examples/system/console/main/cmd_wifi.c | 6 ++++-- examples/wifi/iperf/main/cmd_wifi.c | 6 ++++-- examples/wifi/power_save/main/power_save.c | 3 ++- examples/wifi/scan/main/scan.c | 3 ++- examples/wifi/smart_config/main/smartconfig_main.c | 3 ++- examples/wifi/wpa2_enterprise/main/wpa2_enterprise_main.c | 3 ++- examples/wifi/wps/main/wps.c | 3 ++- 10 files changed, 26 insertions(+), 13 deletions(-) diff --git a/examples/bluetooth/bluedroid/ble/blufi/main/blufi_example_main.c b/examples/bluetooth/bluedroid/ble/blufi/main/blufi_example_main.c index 3cb5e6a51b..d3a8c847ab 100644 --- a/examples/bluetooth/bluedroid/ble/blufi/main/blufi_example_main.c +++ b/examples/bluetooth/bluedroid/ble/blufi/main/blufi_example_main.c @@ -200,7 +200,8 @@ static void initialise_wifi(void) esp_netif_init(); wifi_event_group = xEventGroupCreate(); ESP_ERROR_CHECK(esp_event_loop_create_default()); - assert(esp_netif_create_default_wifi_sta()); + esp_netif_t *sta_netif = esp_netif_create_default_wifi_sta(); + assert(sta_netif); ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler, NULL)); ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &ip_event_handler, NULL)); diff --git a/examples/bluetooth/esp_ble_mesh/ble_mesh_wifi_coexist/components/iperf/cmd_wifi.c b/examples/bluetooth/esp_ble_mesh/ble_mesh_wifi_coexist/components/iperf/cmd_wifi.c index b082e153a5..87db1c7853 100644 --- a/examples/bluetooth/esp_ble_mesh/ble_mesh_wifi_coexist/components/iperf/cmd_wifi.c +++ b/examples/bluetooth/esp_ble_mesh/ble_mesh_wifi_coexist/components/iperf/cmd_wifi.c @@ -129,8 +129,10 @@ void initialise_wifi(void) esp_netif_init(); wifi_event_group = xEventGroupCreate(); ESP_ERROR_CHECK(esp_event_loop_create_default()); - assert(sta_netif = esp_netif_create_default_wifi_sta()); - assert(ap_netif = esp_netif_create_default_wifi_ap()); + esp_netif_t *sta_netif = esp_netif_create_default_wifi_sta(); + assert(sta_netif); + esp_netif_t *ap_netif = esp_netif_create_default_wifi_ap(); + assert(ap_netif); ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler, NULL)); ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &ip_event_handler, NULL)); diff --git a/examples/protocols/esp_local_ctrl/main/app_main.c b/examples/protocols/esp_local_ctrl/main/app_main.c index 43ba8bf241..a4dc11dc51 100644 --- a/examples/protocols/esp_local_ctrl/main/app_main.c +++ b/examples/protocols/esp_local_ctrl/main/app_main.c @@ -68,7 +68,8 @@ void wifi_init_sta(void) esp_netif_init(); ESP_ERROR_CHECK(esp_event_loop_create_default()); - assert(esp_netif_create_default_wifi_sta()); + esp_netif_t *sta_netif = esp_netif_create_default_wifi_sta(); + assert(sta_netif); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); ESP_ERROR_CHECK(esp_wifi_init(&cfg)); diff --git a/examples/system/console/main/cmd_wifi.c b/examples/system/console/main/cmd_wifi.c index e26a9dcea9..87555b6565 100644 --- a/examples/system/console/main/cmd_wifi.c +++ b/examples/system/console/main/cmd_wifi.c @@ -47,8 +47,10 @@ static void initialise_wifi(void) esp_netif_init(); wifi_event_group = xEventGroupCreate(); ESP_ERROR_CHECK(esp_event_loop_create_default()); - assert(esp_netif_create_default_wifi_ap()); - assert(esp_netif_create_default_wifi_sta()); + esp_netif_t *ap_netif = esp_netif_create_default_wifi_ap(); + assert(ap_netif); + esp_netif_t *sta_netif = esp_netif_create_default_wifi_sta(); + assert(sta_netif); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); ESP_ERROR_CHECK( esp_wifi_init(&cfg) ); ESP_ERROR_CHECK( esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &event_handler, NULL) ); diff --git a/examples/wifi/iperf/main/cmd_wifi.c b/examples/wifi/iperf/main/cmd_wifi.c index 5da2736da1..380d7a9689 100644 --- a/examples/wifi/iperf/main/cmd_wifi.c +++ b/examples/wifi/iperf/main/cmd_wifi.c @@ -111,8 +111,10 @@ void initialise_wifi(void) esp_netif_init(); wifi_event_group = xEventGroupCreate(); ESP_ERROR_CHECK( esp_event_loop_create_default() ); - assert(netif_ap = esp_netif_create_default_wifi_ap()); - assert(netif_sta = esp_netif_create_default_wifi_sta()); + netif_ap = esp_netif_create_default_wifi_ap(); + assert(netif_ap); + netif_sta = esp_netif_create_default_wifi_sta(); + assert(netif_sta); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); ESP_ERROR_CHECK( esp_wifi_init(&cfg) ); ESP_ERROR_CHECK( esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_SCAN_DONE, &scan_done_handler, NULL) ); diff --git a/examples/wifi/power_save/main/power_save.c b/examples/wifi/power_save/main/power_save.c index 8e2a5ef6d9..fd7bbbf96b 100644 --- a/examples/wifi/power_save/main/power_save.c +++ b/examples/wifi/power_save/main/power_save.c @@ -57,7 +57,8 @@ static void wifi_power_save(void) { esp_netif_init(); ESP_ERROR_CHECK(esp_event_loop_create_default()); - assert(esp_netif_create_default_wifi_sta()); + esp_netif_t *sta_netif = esp_netif_create_default_wifi_sta(); + assert(sta_netif); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); ESP_ERROR_CHECK(esp_wifi_init(&cfg)); diff --git a/examples/wifi/scan/main/scan.c b/examples/wifi/scan/main/scan.c index d3759d3c98..b3547551e0 100644 --- a/examples/wifi/scan/main/scan.c +++ b/examples/wifi/scan/main/scan.c @@ -116,7 +116,8 @@ static void wifi_scan(void) { esp_netif_init(); ESP_ERROR_CHECK(esp_event_loop_create_default()); - assert(esp_netif_create_default_wifi_sta()); + esp_netif_t *sta_netif = esp_netif_create_default_wifi_sta(); + assert(sta_netif); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); ESP_ERROR_CHECK(esp_wifi_init(&cfg)); diff --git a/examples/wifi/smart_config/main/smartconfig_main.c b/examples/wifi/smart_config/main/smartconfig_main.c index 62b3ab4a91..b6498ca54b 100644 --- a/examples/wifi/smart_config/main/smartconfig_main.c +++ b/examples/wifi/smart_config/main/smartconfig_main.c @@ -81,7 +81,8 @@ static void initialise_wifi(void) esp_netif_init(); s_wifi_event_group = xEventGroupCreate(); ESP_ERROR_CHECK(esp_event_loop_create_default()); - assert(esp_netif_create_default_wifi_sta()); + esp_netif_t *sta_netif = esp_netif_create_default_wifi_sta(); + assert(sta_netif); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); ESP_ERROR_CHECK( esp_wifi_init(&cfg) ); diff --git a/examples/wifi/wpa2_enterprise/main/wpa2_enterprise_main.c b/examples/wifi/wpa2_enterprise/main/wpa2_enterprise_main.c index a9386a63d0..b800a40025 100644 --- a/examples/wifi/wpa2_enterprise/main/wpa2_enterprise_main.c +++ b/examples/wifi/wpa2_enterprise/main/wpa2_enterprise_main.c @@ -107,7 +107,8 @@ static void initialise_wifi(void) esp_netif_init(); wifi_event_group = xEventGroupCreate(); ESP_ERROR_CHECK(esp_event_loop_create_default()); - assert(sta_netif = esp_netif_create_default_wifi_sta()); + esp_netif_t *sta_netif = esp_netif_create_default_wifi_sta(); + assert(sta_netif); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); ESP_ERROR_CHECK( esp_wifi_init(&cfg) ); diff --git a/examples/wifi/wps/main/wps.c b/examples/wifi/wps/main/wps.c index 17095e9316..14d9a28599 100644 --- a/examples/wifi/wps/main/wps.c +++ b/examples/wifi/wps/main/wps.c @@ -99,7 +99,8 @@ static void start_wps(void) { esp_netif_init(); ESP_ERROR_CHECK(esp_event_loop_create_default()); - assert(esp_netif_create_default_wifi_sta()); + esp_netif_t *sta_netif = esp_netif_create_default_wifi_sta(); + assert(sta_netif); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); ESP_ERROR_CHECK(esp_wifi_init(&cfg)); From f839a1328ccc411eace56277a7d0ac96bc31c7e1 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Tue, 22 Oct 2019 09:43:20 +0200 Subject: [PATCH 22/31] esp_netif: added locking for netif list management, unit tests to use unique if_keys, updated comments --- components/esp_netif/README.md | 2 +- components/esp_netif/esp_netif_handlers.c | 2 +- components/esp_netif/esp_netif_objects.c | 91 +++++++++++++++++++ components/esp_netif/include/esp_netif.h | 2 +- .../esp_netif/loopback/esp_netif_loopback.c | 11 --- components/esp_netif/lwip/esp_netif_lwip.c | 30 ++---- .../private_include/esp_netif_private.h | 34 +++++++ components/esp_netif/test/test_esp_netif.c | 9 +- components/esp_wifi/include/esp_wifi_netif.h | 2 +- components/esp_wifi/src/wifi_default.c | 6 +- components/esp_wifi/src/wifi_netif.c | 4 +- 11 files changed, 148 insertions(+), 45 deletions(-) diff --git a/components/esp_netif/README.md b/components/esp_netif/README.md index b3837e7fdb..ee62db678c 100644 --- a/components/esp_netif/README.md +++ b/components/esp_netif/README.md @@ -57,7 +57,7 @@ Overall application interaction with communication media and network stack ### C) ESP-NETIF, former tcpip_adapter * init API (new, configure) * IO API: for passing data between IO driver and network stack -* event/actiona API (esp-netif lifecycle management) +* event/action API (esp-netif lifecycle management) - building blocks for designing event handlers * setters, getters * network stack abstraction: enabling user interaction with TCP/IP stack diff --git a/components/esp_netif/esp_netif_handlers.c b/components/esp_netif/esp_netif_handlers.c index 04d3c5f7bf..c57266576d 100644 --- a/components/esp_netif/esp_netif_handlers.c +++ b/components/esp_netif/esp_netif_handlers.c @@ -1,4 +1,4 @@ -// Copyright 2018 Espressif Systems (Shanghai) PTE LTD +// Copyright 2019 Espressif Systems (Shanghai) PTE LTD // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/components/esp_netif/esp_netif_objects.c b/components/esp_netif/esp_netif_objects.c index f188d0615d..cf53416da8 100644 --- a/components/esp_netif/esp_netif_objects.c +++ b/components/esp_netif/esp_netif_objects.c @@ -15,6 +15,10 @@ #include "esp_netif.h" #include "sys/queue.h" #include "esp_log.h" +#include "freertos/FreeRTOS.h" +#include "freertos/semphr.h" +#include "esp_netif_private.h" +#include // // Purpose of this module is to provide list of esp-netif structures @@ -32,15 +36,38 @@ struct slist_netifs_s { SLIST_HEAD(slisthead, slist_netifs_s) s_head = { .slh_first = NULL, }; static size_t s_esp_netif_counter = 0; +static xSemaphoreHandle s_list_lock = NULL; ESP_EVENT_DEFINE_BASE(IP_EVENT); +esp_err_t esp_netif_list_lock(void) +{ + if (s_list_lock == NULL) { + s_list_lock = xSemaphoreCreateMutex(); + if (s_list_lock == NULL) { + return ESP_ERR_NO_MEM; + } + } + xSemaphoreTake(s_list_lock, portMAX_DELAY); + return ESP_OK; +} + +void esp_netif_list_unlock(void) +{ + assert(s_list_lock); + xSemaphoreGive(s_list_lock); + if (s_esp_netif_counter == 0) { + vQueueDelete(s_list_lock); + s_list_lock = NULL; + } +} // // List manipulation functions // esp_err_t esp_netif_add_to_list(esp_netif_t *netif) { + esp_err_t ret; struct slist_netifs_s *item = calloc(1, sizeof(struct slist_netifs_s)); ESP_LOGD(TAG, "%s %p", __func__, netif); if (item == NULL) { @@ -48,9 +75,14 @@ esp_err_t esp_netif_add_to_list(esp_netif_t *netif) } item->netif = netif; + if ((ret = esp_netif_list_lock()) != ESP_OK) { + return ret; + } + SLIST_INSERT_HEAD(&s_head, item, next); ++s_esp_netif_counter; ESP_LOGD(TAG, "%s netif added successfully (total netifs: %d)", __func__, s_esp_netif_counter); + esp_netif_list_unlock(); return ESP_OK; } @@ -58,7 +90,12 @@ esp_err_t esp_netif_add_to_list(esp_netif_t *netif) esp_err_t esp_netif_remove_from_list(esp_netif_t *netif) { struct slist_netifs_s *item; + esp_err_t ret; + if ((ret = esp_netif_list_lock()) != ESP_OK) { + return ret; + } ESP_LOGV(TAG, "%s %p", __func__, netif); + SLIST_FOREACH(item, &s_head, next) { if (item->netif == netif) { SLIST_REMOVE(&s_head, item, slist_netifs_s, next); @@ -66,9 +103,11 @@ esp_err_t esp_netif_remove_from_list(esp_netif_t *netif) --s_esp_netif_counter; ESP_LOGD(TAG, "%s netif successfully removed (total netifs: %d)", __func__, s_esp_netif_counter); free(item); + esp_netif_list_unlock(); return ESP_OK; } } + esp_netif_list_unlock(); return ESP_ERR_NOT_FOUND; } @@ -78,6 +117,19 @@ size_t esp_netif_get_nr_of_ifs(void) } esp_netif_t* esp_netif_next(esp_netif_t* netif) +{ + esp_err_t ret; + esp_netif_t* result; + if ((ret = esp_netif_list_lock()) != ESP_OK) { + ESP_LOGE(TAG, "Failed to lock esp-netif list with %d", ret); + return NULL; + } + result = esp_netif_next_unsafe(netif); + esp_netif_list_unlock(); + return result; +} + +esp_netif_t* esp_netif_next_unsafe(esp_netif_t* netif) { ESP_LOGV(TAG, "%s %p", __func__, netif); struct slist_netifs_s *item; @@ -93,4 +145,43 @@ esp_netif_t* esp_netif_next(esp_netif_t* netif) } } return NULL; +} + +bool esp_netif_is_netif_listed(esp_netif_t *esp_netif) +{ + esp_err_t ret; + if ((ret = esp_netif_list_lock()) != ESP_OK) { + ESP_LOGE(TAG, "Failed to lock esp-netif list with %d", ret); + return NULL; + } + + // looking for the netif in the list of registered interfaces + esp_netif_t *it = esp_netif_next_unsafe(NULL); + do { + if (it && it == esp_netif) { + esp_netif_list_unlock(); + return true; + } + } while (NULL != (it = esp_netif_next_unsafe(it))); + esp_netif_list_unlock(); + return false; +} + +esp_netif_t *esp_netif_get_handle_from_ifkey(const char *if_key) +{ + esp_err_t ret; + if ((ret = esp_netif_list_lock()) != ESP_OK) { + ESP_LOGE(TAG, "Failed to lock esp-netif list with %d", ret); + return NULL; + } + + esp_netif_t *esp_netif = esp_netif_next_unsafe(NULL); + do { + if (esp_netif && strcmp(if_key, esp_netif_get_ifkey(esp_netif))==0) { + esp_netif_list_unlock(); + return esp_netif; + } + } while (NULL != (esp_netif = esp_netif_next_unsafe(esp_netif))); + esp_netif_list_unlock(); + return NULL; } \ No newline at end of file diff --git a/components/esp_netif/include/esp_netif.h b/components/esp_netif/include/esp_netif.h index 65623a716d..b3d2f72a90 100644 --- a/components/esp_netif/include/esp_netif.h +++ b/components/esp_netif/include/esp_netif.h @@ -1,4 +1,4 @@ -// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD +// Copyright 2019 Espressif Systems (Shanghai) PTE LTD // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/components/esp_netif/loopback/esp_netif_loopback.c b/components/esp_netif/loopback/esp_netif_loopback.c index 5f481052c0..a434ee9608 100644 --- a/components/esp_netif/loopback/esp_netif_loopback.c +++ b/components/esp_netif/loopback/esp_netif_loopback.c @@ -427,17 +427,6 @@ const char *esp_netif_get_desc(esp_netif_t *esp_netif) return esp_netif->if_desc; } -esp_netif_t *esp_netif_get_handle_from_ifkey(const char *if_key) -{ - esp_netif_t *esp_netif = esp_netif_next(NULL); - do { - if (esp_netif && strcmp(if_key, esp_netif->if_key)==0) { - return esp_netif; - } - } while (NULL != (esp_netif = esp_netif_next(esp_netif))); - return NULL; -} - uint32_t esp_netif_get_event_id(esp_netif_t *esp_netif, esp_netif_ip_event_type_t event_type) { return 0; diff --git a/components/esp_netif/lwip/esp_netif_lwip.c b/components/esp_netif/lwip/esp_netif_lwip.c index 269375e0f9..3138bb8da5 100644 --- a/components/esp_netif/lwip/esp_netif_lwip.c +++ b/components/esp_netif/lwip/esp_netif_lwip.c @@ -155,17 +155,12 @@ static inline esp_err_t esp_netif_lwip_ipc_call(esp_netif_api_fn fn, esp_netif_t */ static esp_netif_t* esp_netif_is_active(esp_netif_t *arg) { - esp_netif_t *esp_netif = NULL; // looking for the netif in the list of registered interfaces // as it might have already been destroyed - esp_netif_t *nif = esp_netif_next(NULL); - do { - if (nif && nif == arg) { - esp_netif = nif; - break; - } - } while (NULL != (nif = esp_netif_next(nif))); - return esp_netif; + if (esp_netif_is_netif_listed(arg)) { + return arg; + } + return NULL; } /** @@ -195,8 +190,9 @@ static void esp_netif_update_default_netif(esp_netif_t *esp_netif, esp_netif_act default: case ESP_NETIF_STOPPED: { - esp_netif_t *netif = esp_netif_next(NULL); s_last_default_esp_netif = NULL; + esp_netif_list_lock(); + esp_netif_t *netif = esp_netif_next_unsafe(NULL); while (netif) { if (esp_netif_is_netif_up(netif)) { if (s_last_default_esp_netif && esp_netif_is_netif_up(s_last_default_esp_netif)) { @@ -208,8 +204,9 @@ static void esp_netif_update_default_netif(esp_netif_t *esp_netif, esp_netif_act s_last_default_esp_netif = netif; } } - netif = esp_netif_next(netif); + netif = esp_netif_next_unsafe(netif); } + 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); } @@ -1362,17 +1359,6 @@ const char *esp_netif_get_desc(esp_netif_t *esp_netif) return esp_netif->if_desc; } -esp_netif_t *esp_netif_get_handle_from_ifkey(const char *if_key) -{ - esp_netif_t *esp_netif = esp_netif_next(NULL); - do { - if (esp_netif && strcmp(if_key, esp_netif->if_key)==0) { - return esp_netif; - } - } while (NULL != (esp_netif = esp_netif_next(esp_netif))); - return NULL; -} - uint32_t esp_netif_get_event_id(esp_netif_t *esp_netif, esp_netif_ip_event_type_t event_type) { switch(event_type) { diff --git a/components/esp_netif/private_include/esp_netif_private.h b/components/esp_netif/private_include/esp_netif_private.h index 0519c397d3..b34711d163 100644 --- a/components/esp_netif/private_include/esp_netif_private.h +++ b/components/esp_netif/private_include/esp_netif_private.h @@ -111,4 +111,38 @@ esp_err_t esp_netif_add_to_list(esp_netif_t* netif); */ esp_err_t esp_netif_remove_from_list(esp_netif_t* netif); +/** + * @brief Iterates over list of interfaces without list locking. Returns first netif if NULL given as parameter + * + * Used for bulk search loops to avoid locking and unlocking every iteration. esp_netif_list_lock and esp_netif_list_unlock + * must be used to guard the search loop + * + * @param[in] esp_netif Handle to esp-netif instance + * + * @return First netif from the list if supplied parameter is NULL, next one otherwise + */ +esp_netif_t* esp_netif_next_unsafe(esp_netif_t* netif); + +/** + * @brief Locking network interface list. Use only in connection with esp_netif_next_unsafe + * + * @return ESP_OK on success, specific mutex error if failed to lock + */ +esp_err_t esp_netif_list_lock(void); + +/** + * @brief Unlocking network interface list. Use only in connection with esp_netif_next_unsafe + * + */ +void esp_netif_list_unlock(void); + +/** + * @brief Iterates over list of registered interfaces to check if supplied netif is listed + * + * @param esp_netif network interface to check + * + * @return true if supplied interface is listed + */ +bool esp_netif_is_netif_listed(esp_netif_t *esp_netif); + #endif //_ESP_NETIF_PRIVATE_H_ diff --git a/components/esp_netif/test/test_esp_netif.c b/components/esp_netif/test/test_esp_netif.c index 329259c0b1..8013cdb1c4 100644 --- a/components/esp_netif/test/test_esp_netif.c +++ b/components/esp_netif/test/test_esp_netif.c @@ -2,7 +2,7 @@ #include "esp_netif.h" #include "esp_wifi.h" -TEST_CASE("esp_netif: init and destroy", "[esp_netif][leaks=0]") +TEST_CASE("esp_netif: init and destroy", "[esp_netif]") { esp_netif_config_t cfg = ESP_NETIF_DEFAULT_WIFI_STA(); esp_netif_t *esp_netif = esp_netif_new(NULL); @@ -36,12 +36,15 @@ TEST_CASE("esp_netif: get from if_key", "[esp_netif][leaks=0]") TEST_CASE("esp_netif: create and delete multiple netifs", "[esp_netif][leaks=0]") { - const int nr_of_netifs = 10; + // interface key has to be a unique identifier + const char* if_keys[] = { "if1", "if2", "if3", "if4", "if5", "if6", "if7", "if8", "if9" }; + const int nr_of_netifs = sizeof(if_keys)/sizeof(char*); esp_netif_t *netifs[nr_of_netifs]; - esp_netif_config_t cfg = ESP_NETIF_DEFAULT_WIFI_STA(); // create 10 wifi stations for (int i=0; iwifi_if == WIFI_IF_AP); From 7ef385963cc32cd31c0e5523f06103dd8ddb28ec Mon Sep 17 00:00:00 2001 From: David Cermak Date: Wed, 23 Oct 2019 15:17:18 +0200 Subject: [PATCH 23/31] esp_netif: minor update of coding style based on clang-tidy guidelines and fix some copyright notices --- components/esp_netif/esp_netif_objects.c | 13 ++--- components/esp_netif/include/esp_netif.h | 3 +- .../esp_netif/include/esp_netif_sta_list.h | 6 +-- components/esp_netif/lwip/esp_netif_lwip.c | 54 ++++++++++--------- 4 files changed, 42 insertions(+), 34 deletions(-) diff --git a/components/esp_netif/esp_netif_objects.c b/components/esp_netif/esp_netif_objects.c index cf53416da8..2c23126bf9 100644 --- a/components/esp_netif/esp_netif_objects.c +++ b/components/esp_netif/esp_netif_objects.c @@ -133,15 +133,16 @@ esp_netif_t* esp_netif_next_unsafe(esp_netif_t* netif) { ESP_LOGV(TAG, "%s %p", __func__, netif); struct slist_netifs_s *item; + // Getting the first netif if argument is NULL if (netif == NULL) { item = SLIST_FIRST(&s_head); return (item == NULL) ? NULL : item->netif; - } else { - SLIST_FOREACH(item, &s_head, next) { - if (item->netif == netif) { - item = SLIST_NEXT(item, next); - return (item == NULL) ? NULL : item->netif; - } + } + // otherwise the next one (after the supplied netif) + SLIST_FOREACH(item, &s_head, next) { + if (item->netif == netif) { + item = SLIST_NEXT(item, next); + return (item == NULL) ? NULL : item->netif; } } return NULL; diff --git a/components/esp_netif/include/esp_netif.h b/components/esp_netif/include/esp_netif.h index b3d2f72a90..3233d4ec0a 100644 --- a/components/esp_netif/include/esp_netif.h +++ b/components/esp_netif/include/esp_netif.h @@ -699,8 +699,9 @@ const char *esp_netif_get_desc(esp_netif_t *esp_netif); * @param event_type (either get or lost IP) * * @return specific event id which is configured to be raised if the interface lost or acquired IP address + * -1 if supplied event_type is not known */ -uint32_t esp_netif_get_event_id(esp_netif_t *esp_netif, esp_netif_ip_event_type_t event_type); +int32_t esp_netif_get_event_id(esp_netif_t *esp_netif, esp_netif_ip_event_type_t event_type); /** * @} diff --git a/components/esp_netif/include/esp_netif_sta_list.h b/components/esp_netif/include/esp_netif_sta_list.h index 3f672ec6e6..d5bc6a690a 100644 --- a/components/esp_netif/include/esp_netif_sta_list.h +++ b/components/esp_netif/include/esp_netif_sta_list.h @@ -1,4 +1,4 @@ -// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD +// Copyright 2019 Espressif Systems (Shanghai) PTE LTD // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -47,14 +47,14 @@ typedef struct { * @brief Get IP information for stations connected to the Wi-Fi AP interface * * @param[in] wifi_sta_list Wi-Fi station info list, returned from esp_wifi_ap_get_sta_list() - * @param[out] tcpip_sta_list IP layer station info list, corresponding to MAC addresses provided in wifi_sta_list + * @param[out] netif_sta_list IP layer station info list, corresponding to MAC addresses provided in wifi_sta_list * * @return * - ESP_OK * - ESP_ERR_ESP_NETIF_NO_MEM * - ESP_ERR_ESP_NETIF_INVALID_PARAMS */ -esp_err_t esp_netif_get_sta_list(const wifi_sta_list_t *wifi_sta_list, esp_netif_sta_list_t *tcpip_sta_list); +esp_err_t esp_netif_get_sta_list(const wifi_sta_list_t *wifi_sta_list, esp_netif_sta_list_t *netif_sta_list); /** * @} diff --git a/components/esp_netif/lwip/esp_netif_lwip.c b/components/esp_netif/lwip/esp_netif_lwip.c index 3138bb8da5..701991a563 100644 --- a/components/esp_netif/lwip/esp_netif_lwip.c +++ b/components/esp_netif/lwip/esp_netif_lwip.c @@ -1,4 +1,4 @@ -// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD +// Copyright 2019 Espressif Systems (Shanghai) PTE LTD // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -44,25 +44,25 @@ #define ESP_NETIF_HOSTNAME_MAX_SIZE 32 +/** + * @brief thread safe tcpip function utility macro + */ +#define _LWIP_TASK_IPC_CALL(function, netif, param) \ +{ \ + return esp_netif_lwip_ipc_call(function, netif, (void *)(param)); \ +} + +// +// Internal types +// typedef enum esp_netif_action { ESP_NETIF_UNDEF, ESP_NETIF_STARTED, ESP_NETIF_STOPPED, } esp_netif_action_t; -extern sys_thread_t g_lwip_task; - -static const char *TAG = "esp_netif_lwip"; - -static sys_sem_t api_sync_sem = NULL; -static sys_sem_t api_lock_sem = NULL; -static bool tcpip_initialized = false; -static esp_netif_t *s_last_default_esp_netif = NULL; - /** * @brief Main esp-netif container with interface related information - * - * */ struct esp_netif_obj { // default interface addresses @@ -97,13 +97,17 @@ struct esp_netif_obj { int route_prio; }; -/** - * @brief thread safe tcpip function utility macro - */ -#define _LWIP_TASK_IPC_CALL(function, netif, param) \ -{ \ - return esp_netif_lwip_ipc_call(function, netif, (void *)param); \ -} +// +// Internal variables for this module +// +extern sys_thread_t g_lwip_task; + +static const char *TAG = "esp_netif_lwip"; + +static sys_sem_t api_sync_sem = NULL; +static sys_sem_t api_lock_sem = NULL; +static bool tcpip_initialized = false; +static esp_netif_t *s_last_default_esp_netif = NULL; /** * @brief Api callback from tcpip thread used to call esp-netif @@ -245,8 +249,9 @@ esp_netif_t* esp_netif_get_handle_from_netif_impl(void *dev) void* esp_netif_get_netif_impl(esp_netif_t *esp_netif) { - if (esp_netif) + if (esp_netif) { return esp_netif->lwip_netif; + } return NULL; } @@ -613,7 +618,7 @@ static esp_err_t esp_netif_stop_api(esp_netif_api_msg_t *msg) } if (esp_netif->flags&ESP_NETIF_DHCPS) { - dhcps_stop(lwip_netif); // TODO: dhcps checks status by its self + dhcps_stop(lwip_netif); // TODO(IDF-1099): dhcps checks status by its self if (ESP_NETIF_DHCP_STOPPED != esp_netif->dhcps_status) { esp_netif->dhcps_status = ESP_NETIF_DHCP_INIT; } @@ -1359,15 +1364,16 @@ const char *esp_netif_get_desc(esp_netif_t *esp_netif) return esp_netif->if_desc; } -uint32_t esp_netif_get_event_id(esp_netif_t *esp_netif, esp_netif_ip_event_type_t event_type) +int32_t esp_netif_get_event_id(esp_netif_t *esp_netif, esp_netif_ip_event_type_t event_type) { switch(event_type) { case ESP_NETIF_IP_EVENT_GOT_IP: return esp_netif->get_ip_event; case ESP_NETIF_IP_EVENT_LOST_IP: return esp_netif->lost_ip_event; + default: + return -1; } - return 0; } 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, @@ -1496,7 +1502,7 @@ esp_err_t esp_netif_dhcps_option(esp_netif_t *esp_netif, esp_netif_dhcp_option_m 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) { - // TODO: when dhcp request timeout,change the retry count + // TODO(IDF-1100): when dhcp request timeout,change the retry count return ESP_ERR_NOT_SUPPORTED; } From 7f5cda1b825586903f85dc4ad7736b35712e46d7 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Wed, 23 Oct 2019 16:47:16 +0200 Subject: [PATCH 24/31] tcpip_adapter: updated tcpip_adapter compatablity layer to include all public API and keep 100% backward compatibility update build of tcpip adapter when ethernet disabled --- components/esp_eth/src/esp_eth.c | 2 +- components/esp_netif/esp_netif_defaults.c | 2 + components/mdns/mdns.c | 2 + .../tcpip_adapter/include/tcpip_adapter.h | 78 ++++++++++++- .../tcpip_adapter_compat.h | 29 ++++- .../include/tcpip_adapter_types.h | 11 ++ .../tcpip_adapter/tcpip_adapter_compat.c | 108 +++++++++++++++++- 7 files changed, 217 insertions(+), 15 deletions(-) diff --git a/components/esp_eth/src/esp_eth.c b/components/esp_eth/src/esp_eth.c index dd4f369a25..03f7362cee 100644 --- a/components/esp_eth/src/esp_eth.c +++ b/components/esp_eth/src/esp_eth.c @@ -223,7 +223,7 @@ esp_err_t esp_eth_driver_install(const esp_eth_config_t *config, esp_eth_handle_ ETH_CHECK(xTimerStart(eth_driver->check_link_timer, 0) == pdPASS, "start eth_link_timer failed", err_start_timer, ESP_FAIL); eth_driver->base.post_attach = esp_eth_post_attach_driver_start; *out_hdl = (esp_eth_handle_t)eth_driver; - tcpip_adapter_start_eth(eth_driver); + tcpip_adapter_compat_start_eth(eth_driver); return ESP_OK; err_start_timer: xTimerDelete(eth_driver->check_link_timer, 0); diff --git a/components/esp_netif/esp_netif_defaults.c b/components/esp_netif/esp_netif_defaults.c index 9bd38bb9d3..5c7d871ab3 100644 --- a/components/esp_netif/esp_netif_defaults.c +++ b/components/esp_netif/esp_netif_defaults.c @@ -14,7 +14,9 @@ #include "esp_netif.h" #include "esp_wifi_default.h" +#if CONFIG_ETH_ENABLED #include "esp_eth.h" +#endif // // Purpose of this module is to provide diff --git a/components/mdns/mdns.c b/components/mdns/mdns.c index cb5f86ea08..f6131f2bfa 100644 --- a/components/mdns/mdns.c +++ b/components/mdns/mdns.c @@ -3110,8 +3110,10 @@ static void _mdns_handle_system_event(esp_event_base_t event_base, s_esp_netifs[MDNS_IF_STA] = esp_netif_get_handle_from_ifkey("WIFI_STA_DEF"); } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_AP_START) { s_esp_netifs[MDNS_IF_AP] = esp_netif_get_handle_from_ifkey("WIFI_AP_DEF"); +#if CONFIG_ETH_ENABLED } else if (event_base == ETH_EVENT && event_id == ETHERNET_EVENT_START) { s_esp_netifs[MDNS_IF_ETH] = esp_netif_get_handle_from_ifkey("ETH_DEF"); +#endif } esp_netif_dhcp_status_t dcst; diff --git a/components/tcpip_adapter/include/tcpip_adapter.h b/components/tcpip_adapter/include/tcpip_adapter.h index c50493abe4..6c110b88a0 100644 --- a/components/tcpip_adapter/include/tcpip_adapter.h +++ b/components/tcpip_adapter/include/tcpip_adapter.h @@ -96,13 +96,19 @@ esp_err_t tcpip_adapter_set_default_eth_handlers(void); /** * @brief Compatible version of network stack input function. Translates to esp_netif_receive() - * @param buffer - * @param len - * @param eb - * @return see esp_netif_receive */ esp_err_t tcpip_adapter_eth_input(void *buffer, uint16_t len, void *eb); +/** + * @brief Compatible version of network stack input function. Translates to esp_netif_receive() + */ +esp_err_t tcpip_adapter_sta_input(void *buffer, uint16_t len, void *eb); + +/** + * @brief Compatible version of network stack input function. Translates to esp_netif_receive() + */ +esp_err_t tcpip_adapter_ap_input(void *buffer, uint16_t len, void *eb); + /** * @brief Compatible version of former tcpip_adapter API to clear default WIFI handlers * @return ESP_OK on success @@ -175,4 +181,68 @@ int tcpip_adapter_get_netif_index(tcpip_adapter_if_t tcpip_if); */ esp_err_t tcpip_adapter_get_sta_list(const wifi_sta_list_t *wifi_sta_list, tcpip_adapter_sta_list_t *tcpip_sta_list); +/** + * @brief Compatible version of former tcpip_adapter API of esp_netif_action_start for default ethernet +*/ +esp_err_t tcpip_adapter_eth_start(uint8_t *mac, tcpip_adapter_ip_info_t *ip_info, void *args); + +/** + * @brief Compatible version of former tcpip_adapter API of esp_netif_action_start for default station +*/ +esp_err_t tcpip_adapter_sta_start(uint8_t *mac, tcpip_adapter_ip_info_t *ip_info); + +/** + * @brief Compatible version of former tcpip_adapter API of esp_netif_action_start for default softAP +*/ +esp_err_t tcpip_adapter_ap_start(uint8_t *mac, tcpip_adapter_ip_info_t *ip_info); + +/** + * @brief Compatible version of former tcpip_adapter API of esp_netif_action_stop +*/ +esp_err_t tcpip_adapter_stop(tcpip_adapter_if_t tcpip_if); + +/** + * @brief Compatible version of former tcpip_adapter API of esp_netif_up +*/ +esp_err_t tcpip_adapter_up(tcpip_adapter_if_t tcpip_if); + +/** + * @brief Compatible version of former tcpip_adapter API of esp_netif_down +*/ +esp_err_t tcpip_adapter_down(tcpip_adapter_if_t tcpip_if); + +/** + * @brief Compatible version of former tcpip_adapter API of esp_netif_get_old_ip_info +*/ +esp_err_t tcpip_adapter_get_old_ip_info(tcpip_adapter_if_t tcpip_if, tcpip_adapter_ip_info_t *ip_info); + +/** + * @brief Compatible version of former tcpip_adapter API of esp_netif_set_old_ip_info +*/ +esp_err_t tcpip_adapter_set_old_ip_info(tcpip_adapter_if_t tcpip_if, const tcpip_adapter_ip_info_t *ip_info); + +/** + * @brief Compatible version of former tcpip_adapter API of esp_netif_get_handle_from_netif_impl +*/ +esp_interface_t tcpip_adapter_get_esp_if(void *dev); + +/** + * @brief Compatible version of former tcpip_adapter API of esp_netif_set_hostname +*/ +esp_err_t tcpip_adapter_set_hostname(tcpip_adapter_if_t tcpip_if, const char *hostname); + +/** + * @brief Compatible version of former tcpip_adapter API of esp_netif_get_hostname +*/ +esp_err_t tcpip_adapter_get_hostname(tcpip_adapter_if_t tcpip_if, const char **hostname); + +/** + * @brief This function is called from wifi_init to assure backward compatibility mode + * of tcpip_adapter. In case of legacy use, default instances of ap and sta + * are created and handlers are registered + * + * @return ESP_OK on success + */ +esp_err_t tcpip_adapter_set_default_wifi_handlers(void); + #endif //_TCPIP_ADAPTER_H_ diff --git a/components/tcpip_adapter/include/tcpip_adapter_compatible/tcpip_adapter_compat.h b/components/tcpip_adapter/include/tcpip_adapter_compatible/tcpip_adapter_compat.h index 45d5d6ac10..bf32bd81d8 100644 --- a/components/tcpip_adapter/include/tcpip_adapter_compatible/tcpip_adapter_compat.h +++ b/components/tcpip_adapter/include/tcpip_adapter_compatible/tcpip_adapter_compat.h @@ -15,26 +15,43 @@ #ifndef _TCPIP_ADAPTER_COMPAT_H_ #define _TCPIP_ADAPTER_COMPAT_H_ +/** + * @brief This function is called from ethernet driver init code to facilitate + * autostart fo the driver in backward compatible tcpip_adapter way + * + * @note This api is provided in a separate header, which is included internally only (from wifi driver) + * rather then user initialization code. + * + * @param[in] h Handle to the ethernet driver + * + * @return ESP_OK on success + */ +esp_err_t tcpip_adapter_compat_start_eth(void* h); + /** * @brief This function is called from wifi_init to assure backward compatibility mode * of tcpip_adapter. In case of legacy use, default instances of ap and sta * are created and handlers are registered * - * @note This api is given in a separate header, which is included internally (from wifi driver) - * rather then user initialization code. + * @note This API is provided in a separate header, which is included internally only (from wifi_init) + * rather then user initialization code. At this same time this API is also a public API of former tcqpip_adapter + * and thus provided also in tcpip_adapter.h * * @return ESP_OK on success */ esp_err_t tcpip_adapter_set_default_wifi_handlers(void); /** - * @brief This function is called from ethernet driver init code to facilitate - * autostart fo the driver in backward compatible tcpip_adapter way + * @brief This function is called from wifi_init to assure backward compatibility mode + * of tcpip_adapter. In case of legacy use, default instances of ap and sta + * are destroyed and handlers are unregistered * - * @param[in] h Handle to the ethernet driver + * @note This API is provided in a separate header, which is included internally only (from wifi_init) + * rather then user initialization code. At this same time this API is also a public API of former tcqpip_adapter + * and thus provided also in tcpip_adapter.h * * @return ESP_OK on success */ -esp_err_t tcpip_adapter_start_eth(void* h); +esp_err_t tcpip_adapter_clear_default_wifi_handlers(void); #endif //_TCPIP_ADAPTER_COMPAT_H_ diff --git a/components/tcpip_adapter/include/tcpip_adapter_types.h b/components/tcpip_adapter/include/tcpip_adapter_types.h index e8346125ba..59f637fc77 100644 --- a/components/tcpip_adapter/include/tcpip_adapter_types.h +++ b/components/tcpip_adapter/include/tcpip_adapter_types.h @@ -32,6 +32,17 @@ #define TCPIP_ADAPTER_IP_ADDRESS_LEASE_TIME ESP_NETIF_IP_ADDRESS_LEASE_TIME #define TCPIP_ADAPTER_IP_REQUEST_RETRY_TIME ESP_NETIF_IP_REQUEST_RETRY_TIME +/** @brief Legacy error code definitions + * + */ +#define ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS ESP_ERR_ESP_NETIF_INVALID_PARAMS +#define ESP_ERR_TCPIP_ADAPTER_IF_NOT_READY ESP_ERR_ESP_NETIF_IF_NOT_READY +#define ESP_ERR_TCPIP_ADAPTER_DHCPC_START_FAILED ESP_ERR_ESP_NETIF_DHCPC_START_FAILED +#define ESP_ERR_TCPIP_ADAPTER_DHCP_ALREADY_STARTED ESP_ERR_ESP_NETIF_DHCP_ALREADY_STARTED +#define ESP_ERR_TCPIP_ADAPTER_DHCP_ALREADY_STOPPED ESP_ERR_ESP_NETIF_DHCP_ALREADY_STOPPED +#define ESP_ERR_TCPIP_ADAPTER_NO_MEM ESP_ERR_ESP_NETIF_NO_MEM +#define ESP_ERR_TCPIP_ADAPTER_DHCP_NOT_STOPPED ESP_ERR_ESP_NETIF_DHCP_NOT_STOPPED + typedef enum { TCPIP_ADAPTER_IF_STA = 0, /**< Wi-Fi STA (station) interface */ TCPIP_ADAPTER_IF_AP, /**< Wi-Fi soft-AP interface */ diff --git a/components/tcpip_adapter/tcpip_adapter_compat.c b/components/tcpip_adapter/tcpip_adapter_compat.c index 3cc87a2668..8329867f28 100644 --- a/components/tcpip_adapter/tcpip_adapter_compat.c +++ b/components/tcpip_adapter/tcpip_adapter_compat.c @@ -19,14 +19,21 @@ #if CONFIG_ESP_NETIF_TCPIP_ADAPTER_COMPATIBLE_LAYER +#if CONFIG_ETH_ENABLED #include "esp_eth.h" +#endif #include "tcpip_adapter_types.h" #include "esp_wifi_default.h" +// +// Accessing some internal default interfaces and esp-netifs +// extern void _esp_wifi_set_default_ap_netif(esp_netif_t* esp_netif); extern void _esp_wifi_set_default_sta_netif(esp_netif_t* esp_netif); extern esp_err_t _esp_wifi_set_default_wifi_handlers(void); extern esp_err_t _esp_wifi_clear_default_wifi_handlers(void); +extern esp_err_t esp_netif_up(esp_netif_t *esp_netif); +extern esp_err_t esp_netif_down(esp_netif_t *esp_netif); // // Purpose of this module is to provide backward compatible version of esp-netif @@ -98,7 +105,8 @@ void tcpip_adapter_init(void) } } -static void tcpip_adapter_eth_start(void *esp_netif, esp_event_base_t base, int32_t event_id, void *data) +#if CONFIG_ETH_ENABLED +static void tcpip_adapter_attach_eth_to_netif(void *esp_netif, esp_event_base_t base, int32_t event_id, void *data) { esp_eth_handle_t eth_handle = *(esp_eth_handle_t*)data; esp_netif_attach(esp_netif, eth_handle); @@ -106,6 +114,7 @@ static void tcpip_adapter_eth_start(void *esp_netif, esp_event_base_t base, int3 esp_err_t tcpip_adapter_clear_default_eth_handlers(void) { + ESP_ERROR_CHECK(esp_event_handler_unregister(ETH_EVENT, ETHERNET_EVENT_START, tcpip_adapter_attach_eth_to_netif)); return esp_eth_clear_default_handlers(netif_from_if(TCPIP_ADAPTER_IF_ETH)); } @@ -117,7 +126,7 @@ esp_err_t tcpip_adapter_set_default_eth_handlers(void) s_esp_netifs[TCPIP_ADAPTER_IF_ETH] = eth_netif; // provide a separate "after driver start" hook to attach - esp_err_t ret = esp_event_handler_register(ETH_EVENT, ETHERNET_EVENT_START, tcpip_adapter_eth_start, eth_netif); + esp_err_t ret = esp_event_handler_register(ETH_EVENT, ETHERNET_EVENT_START, tcpip_adapter_attach_eth_to_netif, eth_netif); if (ret != ESP_OK) { ESP_LOGE(TAG, "Failed to register "); return ret; @@ -128,13 +137,24 @@ esp_err_t tcpip_adapter_set_default_eth_handlers(void) return ESP_OK; } +#endif esp_err_t tcpip_adapter_eth_input(void *buffer, uint16_t len, void *eb) { return esp_netif_receive(netif_from_if(TCPIP_ADAPTER_IF_ETH), buffer, len, eb); } -esp_err_t tcpip_adapter_start_eth(void* eth_driver) +esp_err_t tcpip_adapter_sta_input(void *buffer, uint16_t len, void *eb) +{ + return esp_netif_receive(netif_from_if(TCPIP_ADAPTER_IF_STA), buffer, len, eb); +} + +esp_err_t tcpip_adapter_ap_input(void *buffer, uint16_t len, void *eb) +{ + return esp_netif_receive(netif_from_if(TCPIP_ADAPTER_IF_AP), buffer, len, eb); +} + +esp_err_t tcpip_adapter_compat_start_eth(void* eth_driver) { if (s_tcpip_adapter_compat) { esp_netif_t *esp_netif = netif_from_if(TCPIP_ADAPTER_IF_ETH); @@ -275,9 +295,89 @@ esp_err_t tcpip_adapter_get_sta_list(const wifi_sta_list_t *wifi_sta_list, tcpip return esp_netif_get_sta_list(wifi_sta_list, tcpip_sta_list); } +static esp_err_t tcpip_adapter_compat_start_netif(esp_netif_t *netif, uint8_t *mac, tcpip_adapter_ip_info_t *ip_info) +{ + if (netif == NULL || mac == NULL || ip_info == NULL) { + return ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS; + } + esp_netif_set_mac(netif, mac); + esp_netif_set_ip_info(netif, (esp_netif_ip_info_t *)ip_info); + esp_netif_action_start(netif, NULL, 0, NULL); + return ESP_OK; +} + +esp_err_t tcpip_adapter_eth_start(uint8_t *mac, tcpip_adapter_ip_info_t *ip_info, void *args) +{ + return tcpip_adapter_compat_start_netif(netif_from_if(TCPIP_ADAPTER_IF_ETH), + mac, ip_info); +} + +esp_err_t tcpip_adapter_sta_start(uint8_t *mac, tcpip_adapter_ip_info_t *ip_info) +{ + return tcpip_adapter_compat_start_netif(netif_from_if(TCPIP_ADAPTER_IF_STA), + mac, ip_info); +} + +esp_err_t tcpip_adapter_ap_start(uint8_t *mac, tcpip_adapter_ip_info_t *ip_info) +{ + return tcpip_adapter_compat_start_netif(netif_from_if(TCPIP_ADAPTER_IF_AP), + mac, ip_info); +} + +esp_err_t tcpip_adapter_stop(tcpip_adapter_if_t tcpip_if) +{ + esp_netif_t *netif = netif_from_if(tcpip_if); + if (netif == NULL) { + return ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS; + } + esp_netif_action_stop(netif_from_if(tcpip_if), NULL, 0, NULL); + return ESP_OK; +} + +esp_err_t tcpip_adapter_up(tcpip_adapter_if_t tcpip_if) +{ + return esp_netif_up(netif_from_if(tcpip_if)); +} + +esp_err_t tcpip_adapter_down(tcpip_adapter_if_t tcpip_if) +{ + return esp_netif_down(netif_from_if(tcpip_if)); +} + +esp_err_t tcpip_adapter_get_old_ip_info(tcpip_adapter_if_t tcpip_if, tcpip_adapter_ip_info_t *ip_info) +{ + return esp_netif_get_old_ip_info(netif_from_if(tcpip_if), (esp_netif_ip_info_t *)ip_info); +} + +esp_err_t tcpip_adapter_set_old_ip_info(tcpip_adapter_if_t tcpip_if, const tcpip_adapter_ip_info_t *ip_info) +{ + return esp_netif_set_old_ip_info(netif_from_if(tcpip_if), (esp_netif_ip_info_t *)ip_info); +} + +esp_interface_t tcpip_adapter_get_esp_if(void *dev) +{ + esp_netif_t *netif = esp_netif_get_handle_from_netif_impl(dev); + for (int i=0; i< TCPIP_ADAPTER_IF_MAX; ++i) { + if (s_esp_netifs[i] == netif) { + return i; + } + } + return ESP_IF_MAX; +} + +esp_err_t tcpip_adapter_set_hostname(tcpip_adapter_if_t tcpip_if, const char *hostname) +{ + return esp_netif_set_hostname(netif_from_if(tcpip_if), hostname); +} + +esp_err_t tcpip_adapter_get_hostname(tcpip_adapter_if_t tcpip_if, const char **hostname) +{ + return esp_netif_get_hostname(netif_from_if(tcpip_if), hostname); +} + #else -esp_err_t tcpip_adapter_start_eth(void* eth_driver) +esp_err_t tcpip_adapter_compat_start_eth(void* eth_driver) { return ESP_OK; } From 4857e92e2cb242cc7d51048204e782f6708a9fc4 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Wed, 30 Oct 2019 19:56:27 +0100 Subject: [PATCH 25/31] esp_mesh: creation of wifi network interfaces for esp-mesh examples moved to used common esp_wifi_default API --- .../esp_wifi/include/esp_wifi_default.h | 18 +++++++ components/esp_wifi/src/wifi_default.c | 47 +++++++++++++++++++ .../internal_communication/main/mesh_main.c | 36 +------------- .../mesh/manual_networking/main/mesh_main.c | 36 +------------- 4 files changed, 69 insertions(+), 68 deletions(-) diff --git a/components/esp_wifi/include/esp_wifi_default.h b/components/esp_wifi/include/esp_wifi_default.h index a6fb03014f..fc6af5441e 100644 --- a/components/esp_wifi/include/esp_wifi_default.h +++ b/components/esp_wifi/include/esp_wifi_default.h @@ -77,4 +77,22 @@ esp_netif_t* esp_netif_create_default_wifi_ap(void); */ esp_netif_t* esp_netif_create_default_wifi_sta(void); +/** + * @brief Creates default STA and AP network interfaces for esp-mesh. + * + * Both netifs are almost identical to the default station and softAP, but with + * DHCP client and server disabled. Please note that the DHCP client is typically + * enabled only if the device is promoted to a root node. + * + * Returns created interfaces which could be ignored setting parameters to NULL + * if an application code does not need to save the interface instances + * for further processing. + * + * @param[out] p_netif_sta pointer where the resultant STA interface is saved (if non NULL) + * @param[out] p_netif_ap pointer where the resultant AP interface is saved (if non NULL) + * + * @return ESP_OK on success + */ +esp_err_t esp_netif_create_default_wifi_mesh_netifs(esp_netif_t **p_netif_sta, esp_netif_t **p_netif_ap); + #endif //_ESP_WIFI_DEFAULT_H diff --git a/components/esp_wifi/src/wifi_default.c b/components/esp_wifi/src/wifi_default.c index 6301cc2030..8922ffe32b 100644 --- a/components/esp_wifi/src/wifi_default.c +++ b/components/esp_wifi/src/wifi_default.c @@ -16,6 +16,7 @@ #include "esp_log.h" #include "esp_private/wifi.h" #include "esp_wifi_netif.h" +#include // // Purpose of this module is to provide basic wifi initialization setup for @@ -318,4 +319,50 @@ esp_netif_t* esp_netif_create_default_wifi_sta(void) esp_netif_attach_wifi_station(netif); esp_wifi_set_default_wifi_sta_handlers(); return netif; +} + +/** + * @brief Creates mesh network interfaces based on default STA and AP, + * but without DHCP, this is to be enabled separately only on root node + */ +esp_err_t esp_netif_create_default_wifi_mesh_netifs(esp_netif_t **p_netif_sta, esp_netif_t **p_netif_ap) +{ + // Create "almost" default AP, with un-flagged DHCP server + esp_netif_inherent_config_t netif_cfg; + memcpy(&netif_cfg, ESP_NETIF_BASE_DEFAULT_WIFI_AP, sizeof(netif_cfg)); + netif_cfg.flags &= ~ESP_NETIF_DHCPS; + esp_netif_config_t cfg_ap = { + .base = &netif_cfg, + .stack = ESP_NETIF_NETSTACK_DEFAULT_WIFI_AP, + }; + esp_netif_t *netif_ap = esp_netif_new(&cfg_ap); + assert(netif_ap); + ESP_ERROR_CHECK(esp_netif_attach_wifi_ap(netif_ap)); + ESP_ERROR_CHECK(esp_wifi_set_default_wifi_ap_handlers()); + + // ...and stop DHCP server to be compatible with former tcpip_adapter (to keep the ESP_NETIF_DHCP_STOPPED state) + ESP_ERROR_CHECK(esp_netif_dhcps_stop(netif_ap)); + + // Create "almost" default station, but with un-flagged DHCP client + memcpy(&netif_cfg, ESP_NETIF_BASE_DEFAULT_WIFI_STA, sizeof(netif_cfg)); + netif_cfg.flags &= ~ESP_NETIF_DHCPC; + esp_netif_config_t cfg_sta = { + .base = &netif_cfg, + .stack = ESP_NETIF_NETSTACK_DEFAULT_WIFI_STA, + }; + esp_netif_t *netif_sta = esp_netif_new(&cfg_sta); + assert(netif_sta); + ESP_ERROR_CHECK(esp_netif_attach_wifi_station(netif_sta)); + ESP_ERROR_CHECK(esp_wifi_set_default_wifi_sta_handlers()); + + // ...and stop DHCP client (to be started separately if the station were promoted to root) + ESP_ERROR_CHECK(esp_netif_dhcpc_stop(netif_sta)); + + if (p_netif_sta) { + *p_netif_sta = netif_sta; + } + if (p_netif_ap) { + *p_netif_ap = netif_ap; + } + return ESP_OK; } \ No newline at end of file diff --git a/examples/mesh/internal_communication/main/mesh_main.c b/examples/mesh/internal_communication/main/mesh_main.c index 2c29d51e4e..46fff40af7 100644 --- a/examples/mesh/internal_communication/main/mesh_main.c +++ b/examples/mesh/internal_communication/main/mesh_main.c @@ -372,38 +372,6 @@ void ip_event_handler(void *arg, esp_event_base_t event_base, } -/** - * @brief Creates mesh network interfaces based on default STA and AP, - * but without DHCP, this is to be enabled separately only on root node - */ -static esp_err_t create_wifi_netifs(void) -{ - esp_netif_inherent_config_t netif_cfg; - memcpy(&netif_cfg, ESP_NETIF_BASE_DEFAULT_WIFI_AP, sizeof(netif_cfg)); - netif_cfg.flags &= ~ESP_NETIF_DHCPS; - esp_netif_config_t cfg_ap = { - .base = &netif_cfg, - .stack = ESP_NETIF_NETSTACK_DEFAULT_WIFI_AP, - }; - esp_netif_t *netif_ap = esp_netif_new(&cfg_ap); - assert(netif_ap); - ESP_ERROR_CHECK(esp_netif_attach_wifi_ap(netif_ap)); - ESP_ERROR_CHECK(esp_wifi_set_default_wifi_sta_handlers()); - - memcpy(&netif_cfg, ESP_NETIF_BASE_DEFAULT_WIFI_STA, sizeof(netif_cfg)); - netif_cfg.flags &= ~ESP_NETIF_DHCPC; - esp_netif_config_t cfg_sta = { - .base = &netif_cfg, - .stack = ESP_NETIF_NETSTACK_DEFAULT_WIFI_STA, - }; - - netif_sta = esp_netif_new(&cfg_sta); - assert(netif_sta); - ESP_ERROR_CHECK(esp_netif_attach_wifi_station(netif_sta)); - ESP_ERROR_CHECK(esp_wifi_set_default_wifi_sta_handlers()); - return ESP_OK; -} - void app_main(void) { ESP_ERROR_CHECK(mesh_light_init()); @@ -412,8 +380,8 @@ void app_main(void) esp_netif_init(); /* event initialization */ ESP_ERROR_CHECK(esp_event_loop_create_default()); - /* crete network interfaces for mesh */ - ESP_ERROR_CHECK(create_wifi_netifs()); + /* crete network interfaces for mesh (only station instance saved for further manipulation, soft AP instance ignored */ + ESP_ERROR_CHECK(esp_netif_create_default_wifi_mesh_netifs(&netif_sta, NULL)); /* wifi initialization */ wifi_init_config_t config = WIFI_INIT_CONFIG_DEFAULT(); ESP_ERROR_CHECK(esp_wifi_init(&config)); diff --git a/examples/mesh/manual_networking/main/mesh_main.c b/examples/mesh/manual_networking/main/mesh_main.c index bb203b67ee..9ffc971a3b 100644 --- a/examples/mesh/manual_networking/main/mesh_main.c +++ b/examples/mesh/manual_networking/main/mesh_main.c @@ -296,38 +296,6 @@ void ip_event_handler(void *arg, esp_event_base_t event_base, ESP_LOGI(MESH_TAG, "IP:" IPSTR, IP2STR(&event->ip_info.ip)); } -/** - * @brief Creates mesh network interfaces based on default STA and AP, - * but without DHCP, this is to be enabled separately only on root node - */ -static esp_err_t create_wifi_netifs(void) -{ - esp_netif_inherent_config_t netif_cfg; - memcpy(&netif_cfg, ESP_NETIF_BASE_DEFAULT_WIFI_AP, sizeof(netif_cfg)); - netif_cfg.flags &= ~ESP_NETIF_DHCPS; - esp_netif_config_t cfg_ap = { - .base = &netif_cfg, - .stack = ESP_NETIF_NETSTACK_DEFAULT_WIFI_AP, - }; - esp_netif_t *netif_ap = esp_netif_new(&cfg_ap); - assert(netif_ap); - ESP_ERROR_CHECK(esp_netif_attach_wifi_ap(netif_ap)); - ESP_ERROR_CHECK(esp_wifi_set_default_wifi_sta_handlers()); - - memcpy(&netif_cfg, ESP_NETIF_BASE_DEFAULT_WIFI_STA, sizeof(netif_cfg)); - netif_cfg.flags &= ~ESP_NETIF_DHCPC; - esp_netif_config_t cfg_sta = { - .base = &netif_cfg, - .stack = ESP_NETIF_NETSTACK_DEFAULT_WIFI_STA, - }; - - netif_sta = esp_netif_new(&cfg_sta); - assert(netif_sta); - ESP_ERROR_CHECK(esp_netif_attach_wifi_station(netif_sta)); - ESP_ERROR_CHECK(esp_wifi_set_default_wifi_sta_handlers()); - return ESP_OK; -} - void app_main(void) { ESP_ERROR_CHECK(mesh_light_init()); @@ -336,8 +304,8 @@ void app_main(void) esp_netif_init(); /* event initialization */ ESP_ERROR_CHECK(esp_event_loop_create_default()); - /* crete network interfaces for mesh */ - ESP_ERROR_CHECK(create_wifi_netifs()); + /* crete network interfaces for mesh (only station instance saved for further manipulation, soft AP instance ignored */ + ESP_ERROR_CHECK(esp_netif_create_default_wifi_mesh_netifs(&netif_sta, NULL)); /* wifi initialization */ wifi_init_config_t config = WIFI_INIT_CONFIG_DEFAULT(); ESP_ERROR_CHECK(esp_wifi_init(&config)); From d0afdaaf24d4af694ce3735f3dc0e95d68965459 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Wed, 30 Oct 2019 20:21:27 +0100 Subject: [PATCH 26/31] esp_eth: moved starting link timer to eth driver start --- components/esp_eth/src/esp_eth.c | 6 +++--- components/esp_eth/test/test_emac.c | 9 +++++++-- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/components/esp_eth/src/esp_eth.c b/components/esp_eth/src/esp_eth.c index 03f7362cee..643a303c03 100644 --- a/components/esp_eth/src/esp_eth.c +++ b/components/esp_eth/src/esp_eth.c @@ -182,10 +182,13 @@ esp_err_t esp_eth_driver_start(esp_eth_handle_t eth_handle) { esp_err_t ret = ESP_OK; esp_eth_driver_t *eth_driver = eth_handle; + ETH_CHECK(xTimerStart(eth_driver->check_link_timer, 0) == pdPASS, "start eth_link_timer failed", err_start_timer, ESP_FAIL); ETH_CHECK(esp_event_post(ETH_EVENT, ETHERNET_EVENT_START, ð_driver, sizeof(eth_driver), 0) == ESP_OK, "send ETHERNET_EVENT_START event failed", err_event, ESP_FAIL); return ret; +err_start_timer: + xTimerDelete(eth_driver->check_link_timer, 0); err_event: esp_eth_driver_uninstall(eth_driver); return ret; @@ -220,13 +223,10 @@ esp_err_t esp_eth_driver_install(const esp_eth_config_t *config, esp_eth_handle_ eth_driver->check_link_timer = xTimerCreate("eth_link_timer", pdMS_TO_TICKS(config->check_link_period_ms), pdTRUE, eth_driver, eth_check_link_timer_cb); ETH_CHECK(eth_driver->check_link_timer, "create eth_link_timer failed", err_create_timer, ESP_FAIL); - ETH_CHECK(xTimerStart(eth_driver->check_link_timer, 0) == pdPASS, "start eth_link_timer failed", err_start_timer, ESP_FAIL); eth_driver->base.post_attach = esp_eth_post_attach_driver_start; *out_hdl = (esp_eth_handle_t)eth_driver; tcpip_adapter_compat_start_eth(eth_driver); return ESP_OK; -err_start_timer: - xTimerDelete(eth_driver->check_link_timer, 0); err_create_timer: phy->deinit(phy); err_init_phy: diff --git a/components/esp_eth/test/test_emac.c b/components/esp_eth/test/test_emac.c index 3bb6e555d6..96e10c50db 100644 --- a/components/esp_eth/test/test_emac.c +++ b/components/esp_eth/test/test_emac.c @@ -228,7 +228,10 @@ TEST_CASE("esp32 ethernet icmp test", "[ethernet][test_env=UT_T2_Ethernet]") TEST_ASSERT(eth_event_group != NULL); test_case_uses_tcpip(); TEST_ESP_OK(esp_event_loop_create_default()); - TEST_ESP_OK(tcpip_adapter_set_default_eth_handlers()); + esp_netif_config_t cfg = ESP_NETIF_DEFAULT_ETH(); + esp_netif_t* eth_netif = esp_netif_new(&cfg); + TEST_ESP_OK(esp_eth_set_default_handlers(eth_netif)); + TEST_ESP_OK(esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, ð_event_handler, eth_event_group)); TEST_ESP_OK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &got_ip_event_handler, eth_event_group)); eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG(); @@ -238,6 +241,7 @@ TEST_CASE("esp32 ethernet icmp test", "[ethernet][test_env=UT_T2_Ethernet]") esp_eth_config_t eth_config = ETH_DEFAULT_CONFIG(mac, phy); esp_eth_handle_t eth_handle = NULL; TEST_ESP_OK(esp_eth_driver_install(ð_config, ð_handle)); + TEST_ESP_OK(esp_netif_attach(eth_netif, eth_handle)); /* wait for IP lease */ bits = xEventGroupWaitBits(eth_event_group, ETH_GOT_IP_BIT, true, true, pdMS_TO_TICKS(ETH_GET_IP_TIMEOUT_MS)); TEST_ASSERT((bits & ETH_GOT_IP_BIT) == ETH_GOT_IP_BIT); @@ -293,9 +297,10 @@ TEST_CASE("esp32 ethernet icmp test", "[ethernet][test_env=UT_T2_Ethernet]") TEST_ESP_OK(phy->del(phy)); TEST_ESP_OK(mac->del(mac)); TEST_ESP_OK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_ETH_GOT_IP, got_ip_event_handler)); - TEST_ESP_OK(tcpip_adapter_clear_default_eth_handlers()); + TEST_ESP_OK(esp_eth_clear_default_handlers(eth_netif)); TEST_ESP_OK(esp_event_loop_delete_default()); vEventGroupDelete(eth_event_group); + esp_netif_destroy(eth_netif); } #if CONFIG_ETH_USE_SPI_ETHERNET From 064bed710ed07c5a882ac8cc97aff0705a5e2fb8 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Wed, 30 Oct 2019 20:27:07 +0100 Subject: [PATCH 27/31] esp_netif: fix esp_netif_stop() to stop DHCP client if configured --- components/esp_netif/lwip/esp_netif_lwip.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/esp_netif/lwip/esp_netif_lwip.c b/components/esp_netif/lwip/esp_netif_lwip.c index 701991a563..63918cbccb 100644 --- a/components/esp_netif/lwip/esp_netif_lwip.c +++ b/components/esp_netif/lwip/esp_netif_lwip.c @@ -622,7 +622,7 @@ static esp_err_t esp_netif_stop_api(esp_netif_api_msg_t *msg) if (ESP_NETIF_DHCP_STOPPED != esp_netif->dhcps_status) { esp_netif->dhcps_status = ESP_NETIF_DHCP_INIT; } - } else if (esp_netif->flags&ESP_NETIF_DHCPS) { + } else if (esp_netif->flags&ESP_NETIF_DHCPC) { dhcp_release(lwip_netif); dhcp_stop(lwip_netif); dhcp_cleanup(lwip_netif); From f91d69efb2f91cc2168dc4688201eacbc0415db3 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Wed, 30 Oct 2019 20:29:13 +0100 Subject: [PATCH 28/31] esp_netif: rename DHCP flags for client and server --- components/esp_netif/esp_netif_defaults.c | 6 ++--- .../esp_netif/include/esp_netif_types.h | 4 ++-- components/esp_netif/lwip/esp_netif_lwip.c | 22 +++++++++---------- components/esp_wifi/src/wifi_default.c | 4 ++-- components/lwip/port/esp32/netif/dhcp_state.c | 2 +- 5 files changed, 19 insertions(+), 19 deletions(-) diff --git a/components/esp_netif/esp_netif_defaults.c b/components/esp_netif/esp_netif_defaults.c index 5c7d871ab3..5fc037f1db 100644 --- a/components/esp_netif/esp_netif_defaults.c +++ b/components/esp_netif/esp_netif_defaults.c @@ -36,7 +36,7 @@ // Default configuration of common interfaces, such as STA, AP, ETH // const esp_netif_inherent_config_t _g_esp_netif_inherent_sta_config = { - .flags = ESP_NETIF_DHCPC | ESP_NETIF_FLAG_GARP | ESP_NETIF_FLAG_EVENT_IP_MODIFIED, + .flags = ESP_NETIF_DHCP_CLIENT | ESP_NETIF_FLAG_GARP | ESP_NETIF_FLAG_EVENT_IP_MODIFIED, .lost_ip_event = IP_EVENT_STA_LOST_IP, .get_ip_event = IP_EVENT_STA_GOT_IP, .if_key = "WIFI_STA_DEF", @@ -52,7 +52,7 @@ static const esp_netif_ip_info_t soft_ap_ip = { }; const esp_netif_inherent_config_t _g_esp_netif_inherent_ap_config = { - .flags = ESP_NETIF_DHCPS | ESP_NETIF_FLAG_AUTOUP, + .flags = ESP_NETIF_DHCP_SERVER | ESP_NETIF_FLAG_AUTOUP, .ip_info = (esp_netif_ip_info_t*)&soft_ap_ip, .if_key = "WIFI_AP_DEF", .if_desc = "ap", @@ -62,7 +62,7 @@ const esp_netif_inherent_config_t _g_esp_netif_inherent_ap_config = { const esp_netif_inherent_config_t _g_esp_netif_inherent_eth_config = { .get_ip_event = IP_EVENT_ETH_GOT_IP, .lost_ip_event = 0, - .flags = ESP_NETIF_DHCPC | ESP_NETIF_FLAG_GARP | ESP_NETIF_FLAG_EVENT_IP_MODIFIED, + .flags = ESP_NETIF_DHCP_CLIENT | ESP_NETIF_FLAG_GARP | ESP_NETIF_FLAG_EVENT_IP_MODIFIED, .if_key = "ETH_DEF", .if_desc = "eth", .route_prio = 50 diff --git a/components/esp_netif/include/esp_netif_types.h b/components/esp_netif/include/esp_netif_types.h index 8f4d43c5cc..0767bae1a9 100644 --- a/components/esp_netif/include/esp_netif_types.h +++ b/components/esp_netif/include/esp_netif_types.h @@ -123,8 +123,8 @@ typedef struct { typedef enum esp_netif_flags { - ESP_NETIF_DHCPC = 1 << 0, - ESP_NETIF_DHCPS = 1 << 1, + ESP_NETIF_DHCP_CLIENT = 1 << 0, + 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 diff --git a/components/esp_netif/lwip/esp_netif_lwip.c b/components/esp_netif/lwip/esp_netif_lwip.c index 63918cbccb..6419d8eb2a 100644 --- a/components/esp_netif/lwip/esp_netif_lwip.c +++ b/components/esp_netif/lwip/esp_netif_lwip.c @@ -574,7 +574,7 @@ static esp_err_t esp_netif_start_api(esp_netif_api_msg_t *msg) ESP_LOGD(TAG, "%s Setting the lwip netif%p UP", __func__, p_netif); netif_set_up(p_netif); } - if (esp_netif->flags&ESP_NETIF_DHCPS) { + if (esp_netif->flags & ESP_NETIF_DHCP_SERVER) { if (esp_netif->dhcps_status != ESP_NETIF_DHCP_STARTED) { if (p_netif != NULL && netif_is_up(p_netif)) { esp_netif_ip_info_t *default_ip = esp_netif->ip_info; @@ -617,12 +617,12 @@ static esp_err_t esp_netif_stop_api(esp_netif_api_msg_t *msg) return ESP_ERR_ESP_NETIF_IF_NOT_READY; } - if (esp_netif->flags&ESP_NETIF_DHCPS) { + if (esp_netif->flags & ESP_NETIF_DHCP_SERVER) { dhcps_stop(lwip_netif); // TODO(IDF-1099): dhcps checks status by its self if (ESP_NETIF_DHCP_STOPPED != esp_netif->dhcps_status) { esp_netif->dhcps_status = ESP_NETIF_DHCP_INIT; } - } else if (esp_netif->flags&ESP_NETIF_DHCPC) { + } else if (esp_netif->flags & ESP_NETIF_DHCP_CLIENT) { dhcp_release(lwip_netif); dhcp_stop(lwip_netif); dhcp_cleanup(lwip_netif); @@ -870,7 +870,7 @@ esp_err_t esp_netif_dhcpc_start(esp_netif_t *esp_netif) _LWIP_TASK_IPC_CALL(esp_ 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_DHCPC)) { + if (!esp_netif || (esp_netif->flags & ESP_NETIF_DHCP_CLIENT)) { return ESP_ERR_INVALID_ARG; } @@ -880,7 +880,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_DHCPS)) { + if (!esp_netif || (esp_netif->flags & ESP_NETIF_DHCP_SERVER)) { return ESP_ERR_INVALID_ARG; } @@ -1043,7 +1043,7 @@ static esp_err_t esp_netif_down_api(esp_netif_api_msg_t *msg) struct netif *lwip_netif = esp_netif->lwip_netif; - if (esp_netif->flags&ESP_NETIF_DHCPC && esp_netif->dhcpc_status == ESP_NETIF_DHCP_STARTED) { + if (esp_netif->flags & ESP_NETIF_DHCP_CLIENT && esp_netif->dhcpc_status == ESP_NETIF_DHCP_STARTED) { dhcp_stop(esp_netif->lwip_netif); esp_netif->dhcpc_status = ESP_NETIF_DHCP_INIT; @@ -1054,7 +1054,7 @@ static esp_err_t esp_netif_down_api(esp_netif_api_msg_t *msg) netif_set_addr(lwip_netif, IP4_ADDR_ANY4, IP4_ADDR_ANY4, IP4_ADDR_ANY4); netif_set_down(lwip_netif); - if (esp_netif->flags&ESP_NETIF_DHCPC) { + if (esp_netif->flags & ESP_NETIF_DHCP_CLIENT) { esp_netif_start_ip_lost_timer(esp_netif); } @@ -1148,11 +1148,11 @@ static esp_err_t esp_netif_set_ip_info_api(esp_netif_api_msg_t *msg) return ESP_ERR_INVALID_STATE; } - if (esp_netif->flags&ESP_NETIF_DHCPS) { + if (esp_netif->flags & ESP_NETIF_DHCP_SERVER) { if (esp_netif->dhcps_status != ESP_NETIF_DHCP_STOPPED) { return ESP_ERR_ESP_NETIF_DHCP_NOT_STOPPED; } - } else if (esp_netif->flags&ESP_NETIF_DHCPC) { + } else if (esp_netif->flags & ESP_NETIF_DHCP_CLIENT) { if (esp_netif->dhcpc_status != ESP_NETIF_DHCP_STOPPED) { return ESP_ERR_ESP_NETIF_DHCP_NOT_STOPPED; } @@ -1225,7 +1225,7 @@ static esp_err_t esp_netif_set_dns_info_api(esp_netif_api_msg_t *msg) ip_addr_t *lwip_ip = (ip_addr_t*)&dns->ip; lwip_ip->type = IPADDR_TYPE_V4; - if (esp_netif->flags&ESP_NETIF_DHCPS) { + if (esp_netif->flags & ESP_NETIF_DHCP_SERVER) { // if DHCP server configured to set DNS in dhcps API if (type != ESP_NETIF_DNS_MAIN) { ESP_LOGD(TAG, "set dns invalid type"); @@ -1263,7 +1263,7 @@ static esp_err_t esp_netif_get_dns_info_api(esp_netif_api_msg_t *msg) return ESP_ERR_ESP_NETIF_INVALID_PARAMS; } - if (esp_netif->flags&ESP_NETIF_DHCPS) { + if (esp_netif->flags & ESP_NETIF_DHCP_SERVER) { ip4_addr_t dns_ip = dhcps_dns_getserver(); memcpy(&dns->ip.u_addr.ip4, &dns_ip, sizeof(ip4_addr_t)); } else { diff --git a/components/esp_wifi/src/wifi_default.c b/components/esp_wifi/src/wifi_default.c index 8922ffe32b..d5b1c89299 100644 --- a/components/esp_wifi/src/wifi_default.c +++ b/components/esp_wifi/src/wifi_default.c @@ -330,7 +330,7 @@ esp_err_t esp_netif_create_default_wifi_mesh_netifs(esp_netif_t **p_netif_sta, e // Create "almost" default AP, with un-flagged DHCP server esp_netif_inherent_config_t netif_cfg; memcpy(&netif_cfg, ESP_NETIF_BASE_DEFAULT_WIFI_AP, sizeof(netif_cfg)); - netif_cfg.flags &= ~ESP_NETIF_DHCPS; + netif_cfg.flags &= ~ESP_NETIF_DHCP_SERVER; esp_netif_config_t cfg_ap = { .base = &netif_cfg, .stack = ESP_NETIF_NETSTACK_DEFAULT_WIFI_AP, @@ -345,7 +345,7 @@ esp_err_t esp_netif_create_default_wifi_mesh_netifs(esp_netif_t **p_netif_sta, e // Create "almost" default station, but with un-flagged DHCP client memcpy(&netif_cfg, ESP_NETIF_BASE_DEFAULT_WIFI_STA, sizeof(netif_cfg)); - netif_cfg.flags &= ~ESP_NETIF_DHCPC; + netif_cfg.flags &= ~ESP_NETIF_DHCP_CLIENT; esp_netif_config_t cfg_sta = { .base = &netif_cfg, .stack = ESP_NETIF_NETSTACK_DEFAULT_WIFI_STA, diff --git a/components/lwip/port/esp32/netif/dhcp_state.c b/components/lwip/port/esp32/netif/dhcp_state.c index a442e12a0b..0eeee2b551 100644 --- a/components/lwip/port/esp32/netif/dhcp_state.c +++ b/components/lwip/port/esp32/netif/dhcp_state.c @@ -26,7 +26,7 @@ #define DHCP_NAMESPACE "dhcp_state" // DHCP_Client has to be enabled for this netif -#define VALID_NETIF_ID(netif) (ESP_NETIF_DHCPC&esp_netif_get_flags(netif)) +#define VALID_NETIF_ID(netif) (ESP_NETIF_DHCP_CLIENT&esp_netif_get_flags(netif)) bool dhcp_ip_addr_restore(void *netif) { From 9f2a45f15fad59028272667bdd787dfc73c8f392 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Sun, 3 Nov 2019 20:44:44 +0100 Subject: [PATCH 29/31] esp_netif: add dhcp state transition unit tests for default wifi interfaces --- components/esp_netif/test/CMakeLists.txt | 2 +- components/esp_netif/test/test_esp_netif.c | 129 +++++++++++++++++++++ 2 files changed, 130 insertions(+), 1 deletion(-) diff --git a/components/esp_netif/test/CMakeLists.txt b/components/esp_netif/test/CMakeLists.txt index 9dfde6842b..513d8ad984 100644 --- a/components/esp_netif/test/CMakeLists.txt +++ b/components/esp_netif/test/CMakeLists.txt @@ -1,5 +1,5 @@ set(COMPONENT_SRCDIRS ".") set(COMPONENT_PRIV_INCLUDEDIRS "../private_include" ".") -set(COMPONENT_PRIV_REQUIRES unity test_utils esp_netif) +set(COMPONENT_PRIV_REQUIRES unity test_utils esp_netif nvs_flash) register_component() \ No newline at end of file diff --git a/components/esp_netif/test/test_esp_netif.c b/components/esp_netif/test/test_esp_netif.c index 8013cdb1c4..342018a325 100644 --- a/components/esp_netif/test/test_esp_netif.c +++ b/components/esp_netif/test/test_esp_netif.c @@ -1,6 +1,8 @@ #include "unity.h" +#include "test_utils.h" #include "esp_netif.h" #include "esp_wifi.h" +#include "nvs_flash.h" TEST_CASE("esp_netif: init and destroy", "[esp_netif]") { @@ -58,3 +60,130 @@ TEST_CASE("esp_netif: create and delete multiple netifs", "[esp_netif][leaks=0]" } } + +TEST_CASE("esp_netif: test dhcp client state transitions for wifi station", "[esp_netif]") +{ + // init default wifi netif + test_case_uses_tcpip(); + TEST_ESP_OK(nvs_flash_init()); + esp_netif_config_t cfg = ESP_NETIF_DEFAULT_WIFI_STA(); + esp_netif_t *sta = esp_netif_new(&cfg); + TEST_ASSERT_NOT_NULL(sta); + esp_netif_attach_wifi_station(sta); + wifi_init_config_t wifi_cfg = WIFI_INIT_CONFIG_DEFAULT(); + TEST_ESP_OK(esp_wifi_init(&wifi_cfg)); + + esp_netif_dhcp_status_t state; + + // testing DHCP states per netif state transitions + esp_netif_action_start(sta, NULL, 0, NULL); + TEST_ASSERT_EQUAL(ESP_OK, esp_netif_dhcpc_get_status(sta, &state)); + + TEST_ASSERT_EQUAL(ESP_NETIF_DHCP_INIT, state); + esp_netif_action_connected(sta, NULL, 0, NULL); + TEST_ASSERT_EQUAL(ESP_OK, esp_netif_dhcpc_get_status(sta, &state)); + + TEST_ASSERT_EQUAL(ESP_NETIF_DHCP_STARTED, state); + esp_netif_action_stop(sta, NULL, 0, NULL); + TEST_ASSERT_EQUAL(ESP_OK, esp_netif_dhcpc_get_status(sta, &state)); + + TEST_ASSERT_EQUAL(ESP_NETIF_DHCP_INIT, state); + + // destroy default wifi netif + esp_netif_destroy(sta); + TEST_ASSERT(esp_wifi_stop() == ESP_OK); + TEST_ASSERT(esp_wifi_deinit() == ESP_OK); + nvs_flash_deinit(); +} + +TEST_CASE("esp_netif: test dhcp server state transitions for wifi soft AP", "[esp_netif]") +{ + // init default wifi netif + test_case_uses_tcpip(); + TEST_ESP_OK(nvs_flash_init()); + esp_netif_config_t cfg = ESP_NETIF_DEFAULT_WIFI_AP(); + esp_netif_t *ap = esp_netif_new(&cfg); + TEST_ASSERT_NOT_NULL(ap); + esp_netif_attach_wifi_station(ap); + wifi_init_config_t wifi_cfg = WIFI_INIT_CONFIG_DEFAULT(); + TEST_ESP_OK(esp_wifi_init(&wifi_cfg)); + + esp_netif_dhcp_status_t state; + + // testing DHCP server states per netif state transitions + esp_netif_action_start(ap, NULL, 0, NULL); + TEST_ASSERT_EQUAL(ESP_OK, esp_netif_dhcps_get_status(ap, &state)); + TEST_ASSERT_EQUAL(ESP_NETIF_DHCP_STARTED, state); + + esp_netif_action_stop(ap, NULL, 0, NULL); + TEST_ASSERT_EQUAL(ESP_OK, esp_netif_dhcps_get_status(ap, &state)); + TEST_ASSERT_EQUAL(ESP_NETIF_DHCP_INIT, state); + + // destroy default wifi netif + esp_netif_destroy(ap); + TEST_ASSERT(esp_wifi_stop() == ESP_OK); + TEST_ASSERT(esp_wifi_deinit() == ESP_OK); + nvs_flash_deinit(); +} + +TEST_CASE("esp_netif: test dhcp state transitions for mesh netifs", "[esp_netif]") +{ + esp_netif_t *ap = NULL; + esp_netif_t *sta = NULL; + esp_netif_dhcp_status_t state; + + // init two mesh network interfaces + test_case_uses_tcpip(); + TEST_ESP_OK(nvs_flash_init()); + TEST_ESP_OK(esp_event_loop_create_default()); + TEST_ESP_OK(esp_netif_create_default_wifi_mesh_netifs(&sta, &ap)); + TEST_ASSERT_NOT_NULL(sta); + TEST_ASSERT_NOT_NULL(ap); + wifi_init_config_t wifi_cfg = WIFI_INIT_CONFIG_DEFAULT(); + TEST_ESP_OK(esp_wifi_init(&wifi_cfg)); + + // test both server and client are *not* STARTED after interfaces created + TEST_ESP_OK(esp_netif_dhcpc_get_status(sta, &state)); + TEST_ASSERT_NOT_EQUAL(ESP_NETIF_DHCP_STARTED, state); + TEST_ESP_OK(esp_netif_dhcps_get_status(ap, &state)); + TEST_ASSERT_NOT_EQUAL(ESP_NETIF_DHCP_STARTED, state); + + // test both server and client are still *not* STARTED after start + esp_netif_action_start(ap, NULL, 0, NULL); + esp_netif_action_start(sta, NULL, 0, NULL); + TEST_ESP_OK(esp_netif_dhcpc_get_status(sta, &state)); + TEST_ASSERT_NOT_EQUAL(ESP_NETIF_DHCP_STARTED, state); + TEST_ESP_OK(esp_netif_dhcps_get_status(ap, &state)); + TEST_ASSERT_NOT_EQUAL(ESP_NETIF_DHCP_STARTED, state); + + // test both server and client are still *not* STARTED even after connect + esp_netif_action_connected(ap, NULL, 0, NULL); + esp_netif_action_connected(sta, NULL, 0, NULL); + TEST_ESP_OK(esp_netif_dhcpc_get_status(sta, &state)); + TEST_ASSERT_NOT_EQUAL(ESP_NETIF_DHCP_STARTED, state); + TEST_ESP_OK(esp_netif_dhcps_get_status(ap, &state)); + TEST_ASSERT_NOT_EQUAL(ESP_NETIF_DHCP_STARTED, state); + + // test station gets promoted to be a root (so DHCP client started manually) and client is in STATED state + esp_netif_dhcpc_start(sta); + esp_netif_action_connected(sta, NULL, 0, NULL); + TEST_ESP_OK(esp_netif_dhcpc_get_status(sta, &state)); + TEST_ASSERT_EQUAL(ESP_NETIF_DHCP_STARTED, state); + esp_netif_dhcpc_stop(sta); + + // test both server and client are still *not* STARTED even after stop + esp_netif_action_stop(sta, NULL, 0, NULL); + esp_netif_action_stop(ap, NULL, 0, NULL); + TEST_ESP_OK(esp_netif_dhcpc_get_status(sta, &state)); + TEST_ASSERT_NOT_EQUAL(ESP_NETIF_DHCP_STARTED, state); + TEST_ESP_OK(esp_netif_dhcps_get_status(ap, &state)); + TEST_ASSERT_NOT_EQUAL(ESP_NETIF_DHCP_STARTED, state); + + // destroy event_loop, netifs, wifi, nvs + TEST_ESP_OK(esp_event_loop_delete_default()); + esp_netif_destroy(ap); + esp_netif_destroy(sta); + TEST_ASSERT(esp_wifi_stop() == ESP_OK); + TEST_ASSERT(esp_wifi_deinit() == ESP_OK); + nvs_flash_deinit(); +} From e535376257c442594d199c89906230ac4df684c8 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Thu, 7 Nov 2019 16:38:44 +0100 Subject: [PATCH 30/31] tools: updated generator of error codes to names to ignore tcpip_adapter compatibility types since the error codes are reused in esp-netif and in tcpip_adapter mapped to the same number. --- tools/gen_esp_err_to_name.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tools/gen_esp_err_to_name.py b/tools/gen_esp_err_to_name.py index 68a7d21125..9a56be1509 100755 --- a/tools/gen_esp_err_to_name.py +++ b/tools/gen_esp_err_to_name.py @@ -40,7 +40,9 @@ import textwrap import functools # list files here which should not be parsed -ignore_files = ['components/mdns/test_afl_fuzz_host/esp32_compat.h'] +ignore_files = ['components/mdns/test_afl_fuzz_host/esp32_compat.h', # used only for host tests (redefines some err codes) + 'components/tcpip_adapter/include/tcpip_adapter_types.h' # tcpip_adapter in compatibility mode from 4.1 (errors reused in esp-netif) + ] # add directories here which should not be parsed ignore_dirs = ('examples') From 76f612e14f033b5a379a29430658974a29233e87 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Mon, 11 Nov 2019 11:37:59 +0100 Subject: [PATCH 31/31] ci: increased number of parallel jobs to have enough runners for unit tests --- tools/ci/config/target-test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/ci/config/target-test.yml b/tools/ci/config/target-test.yml index 2501f3a2d0..92109a5772 100644 --- a/tools/ci/config/target-test.yml +++ b/tools/ci/config/target-test.yml @@ -472,7 +472,7 @@ UT_034: UT_035: extends: .unit_test_template - parallel: 28 + parallel: 32 tags: - ESP32S2BETA_IDF - UT_T1_1