Merge branch 'cherry-pick-4ec2abbf' into 'release/v2.1'

Merge branch 'feature/some_refactor_for_tcpip_adapter' into 'master'

See merge request !1000
This commit is contained in:
Jiang Jiang Jian 2017-07-17 21:10:04 +08:00
commit a281359db0
4 changed files with 46 additions and 19 deletions

View file

@ -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);

View 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:
;
}
/**

View 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:
;
}
/**

View 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;
}