Skip to content

Commit

Permalink
Merge branch 'feature/some_refactor_for_tcpip_adapter' into 'master'
Browse files Browse the repository at this point in the history
tcpip_adapter: not remove netif when tcpip adapter is stopped

See merge request !943
  • Loading branch information
jack0c committed Jul 13, 2017
2 parents 5ac0503 + d724cc2 commit 4ec2abb
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 19 deletions.
2 changes: 1 addition & 1 deletion components/lwip/include/lwip/lwip/netif.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
13 changes: 9 additions & 4 deletions components/lwip/port/netif/ethernetif.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -185,8 +192,6 @@ if (netif->input(p, netif) != ERR_OK) {
pbuf_free(p);
}
#endif
_exit:
;
}

/**
Expand Down
11 changes: 7 additions & 4 deletions components/lwip/port/netif/wlanif.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
Expand All @@ -174,8 +179,6 @@ wlanif_input(struct netif *netif, void *buffer, u16_t len, void* eb)
pbuf_free(p);
}

_exit:
;
}

/**
Expand Down
39 changes: 29 additions & 10 deletions components/tcpip_adapter/tcpip_adapter_lwip.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
Expand All @@ -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]);
}

Expand All @@ -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) {
Expand All @@ -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;
Expand All @@ -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;
}

Expand Down

0 comments on commit 4ec2abb

Please sign in to comment.