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