From 6157ca63f606a109b81abc3ba8fdad7cfc193469 Mon Sep 17 00:00:00 2001 From: Dong Heng Date: Tue, 31 Jul 2018 13:18:14 +0800 Subject: [PATCH 1/3] fix(tcpip_adapter): Fix netif multi-thread error --- components/tcpip_adapter/tcpip_adapter_lwip.c | 75 ++++++++++++++++--- 1 file changed, 66 insertions(+), 9 deletions(-) diff --git a/components/tcpip_adapter/tcpip_adapter_lwip.c b/components/tcpip_adapter/tcpip_adapter_lwip.c index 2481c6f4..70fab03f 100644 --- a/components/tcpip_adapter/tcpip_adapter_lwip.c +++ b/components/tcpip_adapter/tcpip_adapter_lwip.c @@ -26,6 +26,7 @@ #include "lwip/errno.h" #include "lwip/timeouts.h" #include "lwip/prot/dhcp.h" +#include "lwip/priv/tcpip_priv.h" #include "netif/etharp.h" #include "esp_wifi.h" #include "esp_timer.h" @@ -44,6 +45,12 @@ struct tcpip_adapter_pbuf { struct netif *netif; }; +struct tcpip_adapter_api_call_data { + struct tcpip_api_call_data call; + + struct netif *netif; +}; + static struct netif *esp_netif[TCPIP_ADAPTER_IF_MAX]; static tcpip_adapter_ip_info_t esp_ip[TCPIP_ADAPTER_IF_MAX]; static tcpip_adapter_ip_info_t esp_ip_old[TCPIP_ADAPTER_IF_MAX]; @@ -83,6 +90,60 @@ static void tcpip_adapter_dhcps_cb(u8_t client_ip[4]) esp_event_send(&evt); } +static err_t _dhcp_start(struct tcpip_api_call_data *p) +{ + struct tcpip_adapter_api_call_data *call = (struct tcpip_adapter_api_call_data *)p; + + return dhcp_start(call->netif); +} + +static err_t _dhcp_stop(struct tcpip_api_call_data *p) +{ + struct tcpip_adapter_api_call_data *call = (struct tcpip_adapter_api_call_data *)p; + + dhcp_stop(call->netif); + + return 0; +} + +static err_t _dhcp_release(struct tcpip_api_call_data *p) +{ + struct tcpip_adapter_api_call_data *call = (struct tcpip_adapter_api_call_data *)p; + + dhcp_release(call->netif); + dhcp_stop(call->netif); + dhcp_cleanup(call->netif); + + return 0; +} + +static int tcpip_adapter_start_dhcp(struct netif *netif) +{ + struct tcpip_adapter_api_call_data call; + + call.netif = netif; + + return tcpip_api_call(_dhcp_start, (struct tcpip_api_call_data *)&call); +} + +static int tcpip_adapter_stop_dhcp(struct netif *netif) +{ + struct tcpip_adapter_api_call_data call; + + call.netif = netif; + + return tcpip_api_call(_dhcp_stop, (struct tcpip_api_call_data *)&call); +} + +static int tcpip_adapter_release_dhcp(struct netif *netif) +{ + struct tcpip_adapter_api_call_data call; + + call.netif = netif; + + return tcpip_api_call(_dhcp_release, (struct tcpip_api_call_data *)&call); +} + void tcpip_adapter_init(void) { if (tcpip_inited == false) { @@ -121,9 +182,7 @@ static void tcpip_adapter_dhcpc_done() } } else { dhcp_fail_time = 0; - dhcp_release(esp_netif[TCPIP_ADAPTER_IF_STA]); - dhcp_stop(esp_netif[TCPIP_ADAPTER_IF_STA]); - dhcp_cleanup(esp_netif[TCPIP_ADAPTER_IF_STA]); + tcpip_adapter_release_dhcp(esp_netif[TCPIP_ADAPTER_IF_STA]); dhcpc_status[TCPIP_ADAPTER_IF_STA] = TCPIP_ADAPTER_DHCP_INIT; @@ -224,9 +283,7 @@ esp_err_t tcpip_adapter_stop(tcpip_adapter_if_t tcpip_if) dhcps_status = TCPIP_ADAPTER_DHCP_INIT; } } else if (tcpip_if == TCPIP_ADAPTER_IF_STA || tcpip_if == TCPIP_ADAPTER_IF_ETH) { - dhcp_release(esp_netif[tcpip_if]); - dhcp_stop(esp_netif[tcpip_if]); - dhcp_cleanup(esp_netif[tcpip_if]); + tcpip_adapter_release_dhcp(esp_netif[tcpip_if]); dhcpc_status[tcpip_if] = TCPIP_ADAPTER_DHCP_INIT; @@ -357,7 +414,7 @@ esp_err_t tcpip_adapter_down(tcpip_adapter_if_t tcpip_if) } if (dhcpc_status[tcpip_if] == TCPIP_ADAPTER_DHCP_STARTED) { - dhcp_stop(esp_netif[tcpip_if]); + tcpip_adapter_stop_dhcp(esp_netif[tcpip_if]); dhcpc_status[tcpip_if] = TCPIP_ADAPTER_DHCP_INIT; @@ -959,7 +1016,7 @@ esp_err_t tcpip_adapter_dhcpc_start(tcpip_adapter_if_t tcpip_if) return ESP_OK; } - if (dhcp_start(p_netif) != ERR_OK) { + if (tcpip_adapter_start_dhcp(p_netif) != ERR_OK) { ESP_LOGD(TAG, "dhcp client start failed"); return ESP_ERR_TCPIP_ADAPTER_DHCPC_START_FAILED; } @@ -995,7 +1052,7 @@ esp_err_t tcpip_adapter_dhcpc_stop(tcpip_adapter_if_t tcpip_if) struct netif *p_netif = esp_netif[tcpip_if]; if (p_netif != NULL) { - dhcp_stop(p_netif); + tcpip_adapter_stop_dhcp(p_netif); tcpip_adapter_reset_ip_info(tcpip_if); } else { ESP_LOGD(TAG, "dhcp client if not ready"); From c0c331b665a256e41ec0ad4fc8110be6151d74ce Mon Sep 17 00:00:00 2001 From: Dong Heng Date: Tue, 31 Jul 2018 19:24:34 +0800 Subject: [PATCH 2/3] fix(tcpip_adapter): Fix dhcp pcb not free --- components/tcpip_adapter/tcpip_adapter_lwip.c | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/components/tcpip_adapter/tcpip_adapter_lwip.c b/components/tcpip_adapter/tcpip_adapter_lwip.c index 70fab03f..8e8a932e 100644 --- a/components/tcpip_adapter/tcpip_adapter_lwip.c +++ b/components/tcpip_adapter/tcpip_adapter_lwip.c @@ -117,6 +117,16 @@ static err_t _dhcp_release(struct tcpip_api_call_data *p) return 0; } +static err_t _dhcp_clean(struct tcpip_api_call_data *p) +{ + struct tcpip_adapter_api_call_data *call = (struct tcpip_adapter_api_call_data *)p; + + dhcp_stop(call->netif); + dhcp_cleanup(call->netif); + + return 0; +} + static int tcpip_adapter_start_dhcp(struct netif *netif) { struct tcpip_adapter_api_call_data call; @@ -144,6 +154,15 @@ static int tcpip_adapter_release_dhcp(struct netif *netif) return tcpip_api_call(_dhcp_release, (struct tcpip_api_call_data *)&call); } +static int tcpip_adapter_clean_dhcp(struct netif *netif) +{ + struct tcpip_adapter_api_call_data call; + + call.netif = netif; + + return tcpip_api_call(_dhcp_clean, (struct tcpip_api_call_data *)&call); +} + void tcpip_adapter_init(void) { if (tcpip_inited == false) { @@ -273,6 +292,7 @@ esp_err_t tcpip_adapter_stop(tcpip_adapter_if_t tcpip_if) esp_close((int)esp_netif[tcpip_if]->state); if (!netif_is_up(esp_netif[tcpip_if])) { + tcpip_adapter_clean_dhcp(esp_netif[tcpip_if]); netif_remove(esp_netif[tcpip_if]); return ESP_ERR_TCPIP_ADAPTER_IF_NOT_READY; } From 509882c5b93fb01fa44a3d0d55903e685aadc849 Mon Sep 17 00:00:00 2001 From: Dong Heng Date: Tue, 31 Jul 2018 20:33:22 +0800 Subject: [PATCH 3/3] feat(tcpip_adapter): Modify RT timer to OS timer --- components/tcpip_adapter/tcpip_adapter_lwip.c | 20 +++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/components/tcpip_adapter/tcpip_adapter_lwip.c b/components/tcpip_adapter/tcpip_adapter_lwip.c index 8e8a932e..5dd762e2 100644 --- a/components/tcpip_adapter/tcpip_adapter_lwip.c +++ b/components/tcpip_adapter/tcpip_adapter_lwip.c @@ -36,6 +36,7 @@ #include "net/sockio.h" #include "esp_socket.h" #include "esp_log.h" +#include "esp_wifi_osi.h" struct tcpip_adapter_pbuf { struct pbuf_custom pbuf; @@ -67,6 +68,7 @@ static esp_err_t tcpip_adapter_start_ip_lost_timer(tcpip_adapter_if_t tcpip_if); static void tcpip_adapter_dhcpc_cb(struct netif *netif); static void tcpip_adapter_ip_lost_timer(void *arg); static int tcpip_adapter_bind_netcard(const char *name, struct netif *netif); +static void tcpip_adapter_dhcpc_done(void *arg); static bool tcpip_inited = false; static const char* TAG = "tcpip_adapter"; @@ -79,7 +81,7 @@ void system_station_got_ip_set(); static int dhcp_fail_time = 0; static tcpip_adapter_ip_info_t esp_ip[TCPIP_ADAPTER_IF_MAX]; -static os_timer_t dhcp_check_timer; +static void *dhcp_check_timer; static void tcpip_adapter_dhcps_cb(u8_t client_ip[4]) { @@ -175,14 +177,19 @@ void tcpip_adapter_init(void) 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); + + dhcp_check_timer = wifi_timer_create("check_dhcp", wifi_task_ms_to_ticks(500), true, NULL, tcpip_adapter_dhcpc_done); + if (!dhcp_check_timer) { + ESP_LOGI(TAG, "TCPIP adapter timer create error"); + } } } -static void tcpip_adapter_dhcpc_done() +static void tcpip_adapter_dhcpc_done(void *arg) { struct dhcp *clientdhcp = netif_dhcp_data(esp_netif[TCPIP_ADAPTER_IF_STA]) ; - os_timer_disarm(&dhcp_check_timer); + wifi_timer_stop(dhcp_check_timer, 0); if (netif_is_up(esp_netif[TCPIP_ADAPTER_IF_STA])) { if (clientdhcp->state == DHCP_STATE_BOUND) { /*send event here*/ @@ -193,8 +200,7 @@ static void tcpip_adapter_dhcpc_done() } else if (dhcp_fail_time < (CONFIG_IP_LOST_TIMER_INTERVAL * 1000 / 500)) { ESP_LOGD(TAG,"dhcpc time(ms): %d\n", dhcp_fail_time * 500); dhcp_fail_time ++; - os_timer_setfn(&dhcp_check_timer, (os_timer_func_t*)tcpip_adapter_dhcpc_done, NULL); - os_timer_arm(&dhcp_check_timer, 500, 0); + wifi_timer_reset(dhcp_check_timer, 0); } else { dhcp_fail_time = 0; ESP_LOGD(TAG,"ERROR dhcp get ip error\n"); @@ -1042,9 +1048,7 @@ esp_err_t tcpip_adapter_dhcpc_start(tcpip_adapter_if_t tcpip_if) } dhcp_fail_time = 0; - os_timer_disarm(&dhcp_check_timer); - os_timer_setfn(&dhcp_check_timer, (os_timer_func_t*)tcpip_adapter_dhcpc_done, NULL); - os_timer_arm(&dhcp_check_timer, 500, 0); + wifi_timer_reset(dhcp_check_timer, 0); ESP_LOGD(TAG, "dhcp client start successfully"); dhcpc_status[tcpip_if] = TCPIP_ADAPTER_DHCP_STARTED; return ESP_OK;