]> granicus.if.org Git - esp-idf/commitdiff
tcpip_adapter: not free netif when tcpip adapter is stopped
authorLiu Zhi Fu <liuzhifu@espressif.com>
Fri, 30 Jun 2017 08:49:42 +0000 (16:49 +0800)
committerLiu Zhi Fu <liuzhifu@espressif.com>
Thu, 13 Jul 2017 10:42:45 +0000 (18:42 +0800)
When tcpip adapter is stop, don't free the netif

components/lwip/include/lwip/lwip/netif.h
components/lwip/port/netif/ethernetif.c
components/lwip/port/netif/wlanif.c
components/tcpip_adapter/tcpip_adapter_lwip.c

index 13cbb798e06b6c9d468d4c9382290ff73de1f3ae..f5ca067edea36da3010b2557e1de13dd387bbcd9 100755 (executable)
@@ -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);
index 1930c5e4b27a3215dbd179da2f8ac25a17a86f12..f4bb9241310870bf2fb002f132e03557bc09888a 100644 (file)
@@ -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:
-;
 }
 
 /**
index 1ce2b35fc2540598cf7783dff4fb68c7ec261fcf..7f145624b50bddd98c75efcfd65f89a84792c10e 100644 (file)
@@ -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:
-;
 }
 
 /**
index 93cf8e93e5d6df4510fdce64716069ed95670ad7..37be2bd44c278b3853bafaee8683f16b008c4a7f 100644 (file)
@@ -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;
 }