From a24130b390e4f4e53e38f21402021d12526deba1 Mon Sep 17 00:00:00 2001 From: Kedar Sovani Date: Tue, 8 Aug 2017 10:18:14 +0530 Subject: [PATCH] tcpip_adapter: Decompose tcpip_adapter_start() into interface specification options Since only the used interface's start function gets called, it pulls in only the functions that are required in the current application, thereby saving footprint. --- components/esp32/event_default_handlers.c | 6 +-- .../tcpip_adapter/include/tcpip_adapter.h | 35 ++++++++++++++--- components/tcpip_adapter/tcpip_adapter_lwip.c | 39 ++++++++++++------- 3 files changed, 57 insertions(+), 23 deletions(-) diff --git a/components/esp32/event_default_handlers.c b/components/esp32/event_default_handlers.c index 34a0ddcb4d..d08fe43e7c 100644 --- a/components/esp32/event_default_handlers.c +++ b/components/esp32/event_default_handlers.c @@ -103,7 +103,7 @@ esp_err_t system_event_eth_start_handle_default(system_event_t *event) esp_eth_get_mac(eth_mac); tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_ETH, ð_ip); - tcpip_adapter_start(TCPIP_ADAPTER_IF_ETH, eth_mac, ð_ip); + tcpip_adapter_eth_start(eth_mac, ð_ip); return ESP_OK; } @@ -174,7 +174,7 @@ esp_err_t system_event_ap_start_handle_default(system_event_t *event) WIFI_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_start(TCPIP_ADAPTER_IF_AP, ap_mac, &ap_ip); + tcpip_adapter_ap_start(ap_mac, &ap_ip); return ESP_OK; } @@ -195,7 +195,7 @@ esp_err_t system_event_sta_start_handle_default(system_event_t *event) WIFI_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_start(TCPIP_ADAPTER_IF_STA, sta_mac, &sta_ip); + tcpip_adapter_sta_start(sta_mac, &sta_ip); return ESP_OK; } diff --git a/components/tcpip_adapter/include/tcpip_adapter.h b/components/tcpip_adapter/include/tcpip_adapter.h index f1a0a9e2f8..6d99b4a811 100644 --- a/components/tcpip_adapter/include/tcpip_adapter.h +++ b/components/tcpip_adapter/include/tcpip_adapter.h @@ -177,13 +177,38 @@ typedef struct tcpip_adapter_api_msg_s { void tcpip_adapter_init(void); /** - * @brief Start an interface with specific MAC and IP + * @brief Start the ethernet interface with specific MAC and IP * - * softAP or station interface will be initialized, connect WiFi stack with TCPIP stack. + * @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_eth_start(uint8_t *mac, tcpip_adapter_ip_info_t *ip_info); + +/** + * @brief Start the Wi-Fi station interface with specific MAC and IP + * + * Station interface will be initialized, connect WiFi stack with TCPIP stack. + * + * @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 Start the Wi-Fi AP interface with specific MAC and IP + * + * softAP interface will be initialized, connect WiFi stack with TCPIP stack. * - * For softAP interface, DHCP server will be started automatically. + * DHCP server will be started automatically. * - * @param[in] tcpip_if: the interface which we will start * @param[in] mac: set MAC address of this interface * @param[in] ip_info: set IP address of this interface * @@ -191,7 +216,7 @@ void tcpip_adapter_init(void); * ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS * ESP_ERR_NO_MEM */ -esp_err_t tcpip_adapter_start(tcpip_adapter_if_t tcpip_if, uint8_t *mac, tcpip_adapter_ip_info_t *ip_info); +esp_err_t tcpip_adapter_ap_start(uint8_t *mac, tcpip_adapter_ip_info_t *ip_info); /** * @brief Stop an interface diff --git a/components/tcpip_adapter/tcpip_adapter_lwip.c b/components/tcpip_adapter/tcpip_adapter_lwip.c index 37be2bd44c..372c6d3eec 100644 --- a/components/tcpip_adapter/tcpip_adapter_lwip.c +++ b/components/tcpip_adapter/tcpip_adapter_lwip.c @@ -40,6 +40,7 @@ static struct netif *esp_netif[TCPIP_ADAPTER_IF_MAX]; static tcpip_adapter_ip_info_t esp_ip[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_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}; @@ -96,22 +97,12 @@ void tcpip_adapter_init(void) } } -static netif_init_fn tcpip_if_to_netif_init_fn(tcpip_adapter_if_t tcpip_if) +static inline netif_init_fn tcpip_if_to_netif_init_fn(tcpip_adapter_if_t tcpip_if) { - switch(tcpip_if) { -#ifdef CONFIG_WIFI_ENABLED - case TCPIP_ADAPTER_IF_AP: - return wlanif_init_ap; - case TCPIP_ADAPTER_IF_STA: - return wlanif_init_sta; -#endif -#ifdef CONFIG_ETHERNET - case TCPIP_ADAPTER_IF_ETH: - return ethernetif_init; -#endif - default: - return NULL; - } + 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) @@ -181,6 +172,24 @@ esp_err_t tcpip_adapter_start(tcpip_adapter_if_t tcpip_if, uint8_t *mac, tcpip_a return ESP_OK; } +esp_err_t tcpip_adapter_eth_start(uint8_t *mac, tcpip_adapter_ip_info_t *ip_info) +{ + esp_netif_init_fn[TCPIP_ADAPTER_IF_ETH] = ethernetif_init; + return tcpip_adapter_start(TCPIP_ADAPTER_IF_ETH, mac, ip_info); +} + +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); +} + +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); +} + 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); -- 2.40.0