// 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_private/wifi.h" #include "esp_log.h" #include "esp_netif_net_stack.h" #if CONFIG_ESP_NETIF_TCPIP_ADAPTER_COMPATIBLE_LAYER #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); 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_wifi_driver_and_handlers(ESP_IF_WIFI_AP, 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_wifi_driver_and_handlers(ESP_IF_WIFI_STA, 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_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) { 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 (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; } esp_err_t tcpip_adapter_set_default_wifi_handlers(void) { #if CONFIG_ESP_NETIF_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