From 1fb777cc4917b70b5f56fe0a9de6d50e73f768d5 Mon Sep 17 00:00:00 2001 From: Jiang Jiang Jian Date: Thu, 13 Jul 2017 22:16:00 +0800 Subject: [PATCH] Merge branch 'feature/some_refactor_for_tcpip_adapter' into 'master' tcpip_adapter: not remove netif when tcpip adapter is stopped See merge request !943 --- components/lwip/include/lwip/lwip/netif.h | 2 +- components/lwip/port/netif/ethernetif.c | 13 +++++-- components/lwip/port/netif/wlanif.c | 11 ++++-- components/tcpip_adapter/tcpip_adapter_lwip.c | 39 ++++++++++++++----- 4 files changed, 46 insertions(+), 19 deletions(-) diff --git a/components/lwip/include/lwip/lwip/netif.h b/components/lwip/include/lwip/lwip/netif.h index 13cbb798e..f5ca067ed 100755 --- a/components/lwip/include/lwip/lwip/netif.h +++ b/components/lwip/include/lwip/lwip/netif.h @@ -385,7 +385,7 @@ void netif_set_gw(struct netif *netif, const ip4_addr_t *gw); void netif_set_up(struct netif *netif); void netif_set_down(struct netif *netif); /** Ask if an interface is up */ -#define netif_is_up(netif) (((netif)->flags & NETIF_FLAG_UP) ? (u8_t)1 : (u8_t)0) +#define netif_is_up(netif) ( ((netif) && ((netif)->flags & NETIF_FLAG_UP)) ? (u8_t)1 : (u8_t)0) #if LWIP_NETIF_STATUS_CALLBACK void netif_set_status_callback(struct netif *netif, netif_status_callback_fn status_callback); diff --git a/components/lwip/port/netif/ethernetif.c b/components/lwip/port/netif/ethernetif.c index 1930c5e4b..f4bb92413 100644 --- a/components/lwip/port/netif/ethernetif.c +++ b/components/lwip/port/netif/ethernetif.c @@ -153,11 +153,17 @@ ethernetif_input(struct netif *netif, void *buffer, uint16_t len) { struct pbuf *p; - if(buffer== NULL || netif == NULL) - goto _exit; + if(buffer== NULL || !netif_is_up(netif)) { + if (buffer) { + esp_eth_free_rx_buf(buffer); + } + return; + } + #ifdef CONFIG_EMAC_L2_TO_L3_RX_BUF_MODE p = pbuf_alloc(PBUF_RAW, len, PBUF_RAM); if (p == NULL) { + esp_eth_free_rx_buf(buffer); return; } p->l2_owner = NULL; @@ -172,6 +178,7 @@ if (netif->input(p, netif) != ERR_OK) { #else p = pbuf_alloc(PBUF_RAW, len, PBUF_REF); if (p == NULL){ + esp_eth_free_rx_buf(buffer); return; } p->payload = buffer; @@ -185,8 +192,6 @@ if (netif->input(p, netif) != ERR_OK) { pbuf_free(p); } #endif -_exit: -; } /** diff --git a/components/lwip/port/netif/wlanif.c b/components/lwip/port/netif/wlanif.c index 1ce2b35fc..7f145624b 100644 --- a/components/lwip/port/netif/wlanif.c +++ b/components/lwip/port/netif/wlanif.c @@ -144,8 +144,12 @@ wlanif_input(struct netif *netif, void *buffer, u16_t len, void* eb) { struct pbuf *p; - if(!buffer || !netif) - goto _exit; + if(!buffer || !netif_is_up(netif)) { + if (eb) { + esp_wifi_internal_free_rx_buffer(eb); + } + return; + } #if (ESP_L2_TO_L3_COPY == 1) p = pbuf_alloc(PBUF_RAW, len, PBUF_RAM); @@ -161,6 +165,7 @@ wlanif_input(struct netif *netif, void *buffer, u16_t len, void* eb) p = pbuf_alloc(PBUF_RAW, len, PBUF_REF); if (p == NULL){ ESP_STATS_DROP_INC(esp.wlanif_input_pbuf_fail); + esp_wifi_internal_free_rx_buffer(eb); return; } p->payload = buffer; @@ -174,8 +179,6 @@ wlanif_input(struct netif *netif, void *buffer, u16_t len, void* eb) pbuf_free(p); } -_exit: -; } /** diff --git a/components/tcpip_adapter/tcpip_adapter_lwip.c b/components/tcpip_adapter/tcpip_adapter_lwip.c index 93cf8e93e..37be2bd44 100644 --- a/components/tcpip_adapter/tcpip_adapter_lwip.c +++ b/components/tcpip_adapter/tcpip_adapter_lwip.c @@ -85,6 +85,7 @@ void tcpip_adapter_init(void) tcpip_init(NULL, NULL); + memset(esp_ip, 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); @@ -140,8 +141,11 @@ esp_err_t tcpip_adapter_start(tcpip_adapter_if_t tcpip_if, uint8_t *mac, tcpip_a return ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS; } - if (esp_netif[tcpip_if] == NULL) { - esp_netif[tcpip_if] = calloc(1, sizeof(*esp_netif[tcpip_if])); + 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; } @@ -166,11 +170,11 @@ esp_err_t tcpip_adapter_start(tcpip_adapter_if_t tcpip_if, uint8_t *mac, tcpip_a } /* if ap is on, choose ap as default if */ - if (esp_netif[TCPIP_ADAPTER_IF_AP]) { + if (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_STA]) { + } else if (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] ) { + } else if (netif_is_up(esp_netif[TCPIP_ADAPTER_IF_ETH])) { netif_set_default(esp_netif[TCPIP_ADAPTER_IF_ETH]); } @@ -194,6 +198,11 @@ esp_err_t tcpip_adapter_stop(tcpip_adapter_if_t tcpip_if) 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) { @@ -211,14 +220,16 @@ esp_err_t tcpip_adapter_stop(tcpip_adapter_if_t tcpip_if) ip4_addr_set_zero(&esp_ip[tcpip_if].netmask); } + netif_set_down(esp_netif[tcpip_if]); netif_remove(esp_netif[tcpip_if]); - free(esp_netif[tcpip_if]); - esp_netif[tcpip_if] = NULL; - /* in ap + sta mode, if stop ap, choose sta as default if */ - if (esp_netif[TCPIP_ADAPTER_IF_STA] && tcpip_if == TCPIP_ADAPTER_IF_AP) { - netif_set_default(esp_netif[TCPIP_ADAPTER_IF_STA]); + if (tcpip_if == TCPIP_ADAPTER_IF_AP) { + if (netif_is_up(esp_netif[TCPIP_ADAPTER_IF_STA])) { + netif_set_default(esp_netif[TCPIP_ADAPTER_IF_STA]); + } else if (netif_is_up(esp_netif[TCPIP_ADAPTER_IF_ETH])) { + netif_set_default(esp_netif[TCPIP_ADAPTER_IF_ETH]); + } } return ESP_OK; @@ -244,6 +255,14 @@ esp_err_t tcpip_adapter_up(tcpip_adapter_if_t tcpip_if) netif_set_up(esp_netif[tcpip_if]); } + if (netif_is_up(esp_netif[TCPIP_ADAPTER_IF_AP])) { + netif_set_default(esp_netif[TCPIP_ADAPTER_IF_AP]); + } else if (netif_is_up(esp_netif[TCPIP_ADAPTER_IF_STA])) { + netif_set_default(esp_netif[TCPIP_ADAPTER_IF_STA]); + } else if (netif_is_up(esp_netif[TCPIP_ADAPTER_IF_ETH])) { + netif_set_default(esp_netif[TCPIP_ADAPTER_IF_ETH]); + } + return ESP_OK; }