From ef23607a673ccdc3d26648805816061d4a1cb39a Mon Sep 17 00:00:00 2001 From: Wu Jian Gang Date: Fri, 2 Sep 2016 18:18:22 +0800 Subject: [PATCH 1/7] lwip: remove netif_reg_addr_change_cb --- components/lwip/core/netif.c | 11 ----------- components/tcpip_adapter/tcpip_adapter_lwip.c | 3 +-- 2 files changed, 1 insertion(+), 13 deletions(-) diff --git a/components/lwip/core/netif.c b/components/lwip/core/netif.c index 42309f1a3..33e030412 100755 --- a/components/lwip/core/netif.c +++ b/components/lwip/core/netif.c @@ -315,14 +315,6 @@ netif_add(struct netif *netif, return netif; } -typedef int (*netif_addr_change_cb_t)(struct netif *netif); -static netif_addr_change_cb_t g_netif_addr_change_cb = NULL; - -void netif_reg_addr_change_cb(void *cb) -{ - g_netif_addr_change_cb = (netif_addr_change_cb_t)cb; -} - #if LWIP_IPV4 /** * Change IP address configuration for a network interface (including netmask @@ -341,9 +333,6 @@ netif_set_addr(struct netif *netif, const ip4_addr_t *ipaddr, const ip4_addr_t * netif_set_gw(netif, gw); /* set ipaddr last to ensure netmask/gw have been set when status callback is called */ netif_set_ipaddr(netif, ipaddr); - if (g_netif_addr_change_cb){ - g_netif_addr_change_cb(netif); - } } #endif /* LWIP_IPV4*/ diff --git a/components/tcpip_adapter/tcpip_adapter_lwip.c b/components/tcpip_adapter/tcpip_adapter_lwip.c index 3cd6965bd..b59606bf6 100644 --- a/components/tcpip_adapter/tcpip_adapter_lwip.c +++ b/components/tcpip_adapter/tcpip_adapter_lwip.c @@ -11,6 +11,7 @@ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. + #include #include @@ -53,8 +54,6 @@ void tcpip_adapter_init(void) 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); } - - netif_reg_addr_change_cb(tcpip_adapter_addr_change_cb); } esp_err_t tcpip_adapter_start(tcpip_adapter_if_t tcpip_if, uint8_t *mac, struct ip_info *info) From 3cf377b06e372f7a7842a460dd260cb885b10447 Mon Sep 17 00:00:00 2001 From: Wu Jian Gang Date: Fri, 2 Sep 2016 20:00:45 +0800 Subject: [PATCH 2/7] dhcp: add dhcp callback The callback has no parameter now. TODO: add a parameter to show dhcp status if needed. --- components/lwip/core/ipv4/dhcp.c | 41 +++++++++++++----------- components/lwip/include/lwip/lwip/dhcp.h | 8 +++++ 2 files changed, 31 insertions(+), 18 deletions(-) diff --git a/components/lwip/core/ipv4/dhcp.c b/components/lwip/core/ipv4/dhcp.c index 079880e67..cd780d00a 100755 --- a/components/lwip/core/ipv4/dhcp.c +++ b/components/lwip/core/ipv4/dhcp.c @@ -710,6 +710,24 @@ void dhcp_cleanup(struct netif *netif) } } +/* Espressif add start. */ + +/** Set callback for dhcp, reversed parameter for future use. + * + * @param netif the netif from which to remove the struct dhcp + * @param cb callback for chcp + */ +void dhcp_set_cb(struct netif *netif, void (*cb)(void)) +{ + LWIP_ASSERT("netif != NULL", netif != NULL); + + if (netif->dhcp != NULL) { + netif->dhcp->cb = cb; + } +} + +/* Espressif add end. */ + /** * Start DHCP negotiation for a network interface. * @@ -1117,32 +1135,19 @@ dhcp_bind(struct netif *netif) } #endif /* LWIP_DHCP_AUTOIP_COOP */ - /* Espressif add start. */ - /* back up old ip/netmask/gw */ - ip4_addr_t ip, mask, gw; - ip4_addr_set(&ip, ip_2_ip4(&netif->ip_addr)); - ip4_addr_set(&mask, ip_2_ip4(&netif->netmask)); - ip4_addr_set(&gw, ip_2_ip4(&netif->gw)); - /* Espressif add end. */ - LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_STATE, ("dhcp_bind(): IP: 0x%08"X32_F" SN: 0x%08"X32_F" GW: 0x%08"X32_F"\n", ip4_addr_get_u32(&dhcp->offered_ip_addr), ip4_addr_get_u32(&sn_mask), ip4_addr_get_u32(&gw_addr))); netif_set_addr(netif, &dhcp->offered_ip_addr, &sn_mask, &gw_addr); /* interface is used by routing now that an address is set */ -#ifdef LWIP_ESP8266 - /* use old ip/mask/gw to check whether ip/mask/gw changed */ -// extern void system_station_got_ip_set(ip4_addr_t *ip, ip4_addr_t *mask, ip4_addr_t *gw); -// system_station_got_ip_set(&ip, &mask, &gw); -#endif - - /* use old ip/mask/gw to check whether ip/mask/gw changed */ -// extern void system_station_got_ip_set(ip4_addr_t *ip, ip4_addr_t *mask, ip4_addr_t *gw); -// system_station_got_ip_set(&ip, &mask, &gw); - /* netif is now bound to DHCP leased address */ dhcp_set_state(dhcp, DHCP_STATE_BOUND); + /* Espressif add start. */ + if (dhcp->cb != NULL) { + dhcp->cb(); + } + /* Espressif add end. */ } /** diff --git a/components/lwip/include/lwip/lwip/dhcp.h b/components/lwip/include/lwip/lwip/dhcp.h index 57b635957..2d8926eca 100755 --- a/components/lwip/include/lwip/lwip/dhcp.h +++ b/components/lwip/include/lwip/lwip/dhcp.h @@ -94,6 +94,10 @@ struct dhcp ip_addr_t offered_si_addr; char boot_file_name[DHCP_FILE_LEN]; #endif /* LWIP_DHCP_BOOTPFILE */ + + /* Espressif add start. */ + void (*cb)(void); /* callback for dhcp, add a parameter to show dhcp status if needed */ + /* Espressif add end. */ }; /* MUST be compiled with "pack structs" or equivalent! */ @@ -140,6 +144,10 @@ void dhcp_set_struct(struct netif *netif, struct dhcp *dhcp); /** Remove a struct dhcp previously set to the netif using dhcp_set_struct() */ #define dhcp_remove_struct(netif) do { (netif)->dhcp = NULL; } while(0) void dhcp_cleanup(struct netif *netif); +/* Espressif add start. */ +/** set callback for DHCP */ +void dhcp_set_cb(struct netif *netif, void (*cb)(void)); +/* Espressif add end. */ /** start DHCP configuration */ err_t dhcp_start(struct netif *netif); /** enforce early lease renewal (not needed normally)*/ From c3af6e51d748bb45e1d479e8a774c45946536c1e Mon Sep 17 00:00:00 2001 From: Wu Jian Gang Date: Fri, 2 Sep 2016 20:27:22 +0800 Subject: [PATCH 3/7] tcpip_adapter: use dhcp callback to post got ip event --- .../tcpip_adapter/include/tcpip_adapter.h | 6 +- components/tcpip_adapter/tcpip_adapter_lwip.c | 97 +++++++++---------- 2 files changed, 49 insertions(+), 54 deletions(-) diff --git a/components/tcpip_adapter/include/tcpip_adapter.h b/components/tcpip_adapter/include/tcpip_adapter.h index 7fb7b91d5..8726ac833 100644 --- a/components/tcpip_adapter/include/tcpip_adapter.h +++ b/components/tcpip_adapter/include/tcpip_adapter.h @@ -34,9 +34,9 @@ struct ip_info { /* Also used by dhcpserver.h */ struct dhcps_lease { - bool enable; - ip4_addr_t start_ip; - ip4_addr_t end_ip; + bool enable; + ip4_addr_t start_ip; + ip4_addr_t end_ip; }; typedef struct dhcps_lease tcpip_adapter_dhcps_lease; diff --git a/components/tcpip_adapter/tcpip_adapter_lwip.c b/components/tcpip_adapter/tcpip_adapter_lwip.c index b59606bf6..bf1d6a73c 100644 --- a/components/tcpip_adapter/tcpip_adapter_lwip.c +++ b/components/tcpip_adapter/tcpip_adapter_lwip.c @@ -36,9 +36,6 @@ static struct ip_info esp_ip[TCPIP_ADAPTER_IF_MAX]; static tcpip_adapter_dhcp_status_t dhcps_status = TCPIP_ADAPTER_DHCP_INIT; static tcpip_adapter_dhcp_status_t dhcpc_status = TCPIP_ADAPTER_DHCP_INIT; - -static esp_err_t tcpip_adapter_addr_change_cb(struct netif *netif); - #define TCPIP_ADAPTER_DEBUG(...) void tcpip_adapter_init(void) @@ -156,6 +153,10 @@ esp_err_t tcpip_adapter_down(tcpip_adapter_if_t tcpip_if) if (dhcpc_status != TCPIP_ADAPTER_DHCP_STOPPED) { dhcpc_status = TCPIP_ADAPTER_DHCP_INIT; } + + ip4_addr_set_zero(&esp_ip[tcpip_if].ip); + ip4_addr_set_zero(&esp_ip[tcpip_if].gw); + ip4_addr_set_zero(&esp_ip[tcpip_if].netmask); } netif_set_down(esp_netif[tcpip_if]); @@ -164,54 +165,6 @@ esp_err_t tcpip_adapter_down(tcpip_adapter_if_t tcpip_if) return ESP_OK; } -esp_err_t tcpip_adapter_addr_change_cb(struct netif *netif) -{ - tcpip_adapter_if_t tcpip_if; - system_event_t evt; - - if (!netif) { - TCPIP_ADAPTER_DEBUG("null netif=%p\n", netif); - return ESP_ERR_TCPIP_ADAPTER_IF_NOT_READY; - } - - if (netif == esp_netif[TCPIP_ADAPTER_IF_STA]) { - tcpip_if = TCPIP_ADAPTER_IF_STA; - } else if (netif == esp_netif[TCPIP_ADAPTER_IF_AP]){ - tcpip_if = TCPIP_ADAPTER_IF_AP; - } else { - TCPIP_ADAPTER_DEBUG("invalid netif=%p\n", netif); - return ESP_ERR_TCPIP_ADAPTER_IF_NOT_READY; - } - - //check whether IP is changed - if ( !ip4_addr_cmp(ip_2_ip4(&netif->ip_addr), &esp_ip[tcpip_if].ip) || - !ip4_addr_cmp(ip_2_ip4(&netif->gw), &esp_ip[tcpip_if].gw) || - !ip4_addr_cmp(ip_2_ip4(&netif->netmask), &esp_ip[tcpip_if].netmask) ) { - - ip4_addr_set(&esp_ip[tcpip_if].ip, ip_2_ip4(&netif->ip_addr)); - ip4_addr_set(&esp_ip[tcpip_if].netmask, ip_2_ip4(&netif->netmask)); - ip4_addr_set(&esp_ip[tcpip_if].gw, ip_2_ip4(&netif->gw)); - - //notify event - if ( !ip4_addr_cmp(ip_2_ip4(&netif->ip_addr), IP4_ADDR_ANY) ) { - evt.event_id = SYSTEM_EVENT_STA_GOTIP; - memcpy(&evt.event_info.got_ip.ip, &esp_ip[tcpip_if].ip, sizeof(evt.event_info.got_ip.ip)); - memcpy(&evt.event_info.got_ip.netmask, &esp_ip[tcpip_if].netmask, sizeof(evt.event_info.got_ip.netmask)); - memcpy(&evt.event_info.got_ip.gw, &esp_ip[tcpip_if].gw, sizeof(evt.event_info.got_ip.gw)); - - esp_event_send(&evt); - - printf("ip: %s, ", inet_ntoa(esp_ip[tcpip_if].ip)); - printf("mask: %s, ", inet_ntoa(esp_ip[tcpip_if].netmask)); - printf("gw: %s\n", inet_ntoa(esp_ip[tcpip_if].gw)); - } - } else { - TCPIP_ADAPTER_DEBUG("ip unchanged\n"); - } - - return ESP_OK; -} - esp_err_t tcpip_adapter_get_ip_info(tcpip_adapter_if_t tcpip_if, struct ip_info *if_ip) { struct netif *p_netif; @@ -475,6 +428,46 @@ esp_err_t tcpip_adapter_dhcpc_option(tcpip_adapter_option_mode opt_op, tcpip_ada return ESP_OK; } +static void tcpip_adapter_dhcpc_cb(void) +{ + struct netif *netif = esp_netif[TCPIP_ADAPTER_IF_STA]; + struct ip_info *ip_info = &esp_ip[TCPIP_ADAPTER_IF_STA]; + system_event_t evt; + + if (!netif) { + TCPIP_ADAPTER_DEBUG("null netif=%p\n", netif); + return; + } + + if ( !ip4_addr_cmp(ip_2_ip4(&netif->ip_addr), IP4_ADDR_ANY) ) { + //check whether IP is changed + if ( !ip4_addr_cmp(ip_2_ip4(&netif->ip_addr), &ip_info->ip) || + !ip4_addr_cmp(ip_2_ip4(&netif->netmask), &ip_info->netmask) || + !ip4_addr_cmp(ip_2_ip4(&netif->gw), &ip_info->gw) ) { + + ip4_addr_set(&ip_info->ip, ip_2_ip4(&netif->ip_addr)); + ip4_addr_set(&ip_info->netmask, ip_2_ip4(&netif->netmask)); + ip4_addr_set(&ip_info->gw, ip_2_ip4(&netif->gw)); + + //notify event + evt.event_id = SYSTEM_EVENT_STA_GOTIP; + memcpy(&evt.event_info.got_ip.ip, &ip_info->ip, sizeof(evt.event_info.got_ip.ip)); + memcpy(&evt.event_info.got_ip.netmask, &ip_info->netmask, sizeof(evt.event_info.got_ip.netmask)); + memcpy(&evt.event_info.got_ip.gw, &ip_info->gw, sizeof(evt.event_info.got_ip.gw)); + + esp_event_send(&evt); + + printf("ip: %s, ", inet_ntoa(ip_info->ip)); + printf("mask: %s, ", inet_ntoa(ip_info->netmask)); + printf("gw: %s\n", inet_ntoa(ip_info->gw)); + } else { + TCPIP_ADAPTER_DEBUG("ip unchanged\n"); + } + } + + return; +} + esp_err_t tcpip_adapter_dhcpc_get_status(tcpip_adapter_if_t tcpip_if, tcpip_adapter_dhcp_status_t *status) { *status = dhcpc_status; @@ -510,6 +503,8 @@ esp_err_t tcpip_adapter_dhcpc_start(tcpip_adapter_if_t tcpip_if) return ESP_ERR_TCPIP_ADAPTER_DHCPC_START_FAILED; } + dhcp_set_cb(p_netif, tcpip_adapter_dhcpc_cb); + TCPIP_ADAPTER_DEBUG("dhcp client start successfully\n"); dhcpc_status = TCPIP_ADAPTER_DHCP_STARTED; return ESP_OK; From 1588d1aa6ee06366c01656ce0f64887c54b594cc Mon Sep 17 00:00:00 2001 From: Wu Jian Gang Date: Mon, 5 Sep 2016 11:05:07 +0800 Subject: [PATCH 4/7] event: post got ip event when use static ip TW6926 --- components/esp32/event.c | 20 +++++++++++++++++++ .../tcpip_adapter/include/tcpip_adapter.h | 10 +++++++++- components/tcpip_adapter/tcpip_adapter_lwip.c | 9 ++------- 3 files changed, 31 insertions(+), 8 deletions(-) diff --git a/components/esp32/event.c b/components/esp32/event.c index d8e5c0a4f..fa1830e47 100644 --- a/components/esp32/event.c +++ b/components/esp32/event.c @@ -13,6 +13,7 @@ // limitations under the License. #include #include +#include #include "esp_err.h" #include "esp_wifi.h" @@ -77,6 +78,12 @@ static esp_err_t system_event_sta_gotip_default(system_event_t *event) { extern esp_err_t esp_wifi_set_sta_ip(void); WIFI_API_CALL_CHECK("esp_wifi_set_sta_ip", esp_wifi_set_sta_ip(), ESP_OK); + + printf("ip: " IPSTR ", mask: " IPSTR ", gw: " IPSTR "\n", + IP2STR(&event->event_info.got_ip.ip), + IP2STR(&event->event_info.got_ip.netmask), + IP2STR(&event->event_info.got_ip.gw)); + return ESP_OK; } @@ -134,6 +141,19 @@ esp_err_t system_event_sta_connected_handle_default(system_event_t *event) if (status == TCPIP_ADAPTER_DHCP_INIT) { tcpip_adapter_dhcpc_start(TCPIP_ADAPTER_IF_STA); + } else if (status == TCPIP_ADAPTER_DHCP_STOPPED) { + struct ip_info ip_info; + system_event_t evt; + + tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_STA, &ip_info); + + //notify event + evt.event_id = SYSTEM_EVENT_STA_GOTIP; + memcpy(&evt.event_info.got_ip.ip, &ip_info.ip, sizeof(evt.event_info.got_ip.ip)); + memcpy(&evt.event_info.got_ip.netmask, &ip_info.netmask, sizeof(evt.event_info.got_ip.netmask)); + memcpy(&evt.event_info.got_ip.gw, &ip_info.gw, sizeof(evt.event_info.got_ip.gw)); + + esp_event_send(&evt); } return ESP_OK; diff --git a/components/tcpip_adapter/include/tcpip_adapter.h b/components/tcpip_adapter/include/tcpip_adapter.h index 8726ac833..8a51eae0f 100644 --- a/components/tcpip_adapter/include/tcpip_adapter.h +++ b/components/tcpip_adapter/include/tcpip_adapter.h @@ -17,6 +17,8 @@ #include +#include "rom/queue.h" + #include "esp_wifi.h" #define CONFIG_TCPIP_LWIP 1 @@ -24,7 +26,13 @@ #if CONFIG_TCPIP_LWIP #include "lwip/ip_addr.h" -#include "rom/queue.h" + +#define IP2STR(ipaddr) ip4_addr1_16(ipaddr), \ + ip4_addr2_16(ipaddr), \ + ip4_addr3_16(ipaddr), \ + ip4_addr4_16(ipaddr) + +#define IPSTR "%d.%d.%d.%d" struct ip_info { ip4_addr_t ip; diff --git a/components/tcpip_adapter/tcpip_adapter_lwip.c b/components/tcpip_adapter/tcpip_adapter_lwip.c index bf1d6a73c..3fb42aeb1 100644 --- a/components/tcpip_adapter/tcpip_adapter_lwip.c +++ b/components/tcpip_adapter/tcpip_adapter_lwip.c @@ -74,9 +74,8 @@ esp_err_t tcpip_adapter_start(tcpip_adapter_if_t tcpip_if, uint8_t *mac, struct if (dhcps_status == TCPIP_ADAPTER_DHCP_INIT) { dhcps_start(esp_netif[tcpip_if], info); - printf("dhcp server start:(ip: %s, ", inet_ntoa(info->ip)); - printf("mask: %s, ", inet_ntoa(info->netmask)); - printf("gw: %s)\n", inet_ntoa(info->gw)); + printf("dhcp server start:(ip: " IPSTR ", mask: " IPSTR ", gw: " IPSTR ")\n", + IP2STR(&info->ip), IP2STR(&info->netmask), IP2STR(&info->gw)); dhcps_status = TCPIP_ADAPTER_DHCP_STARTED; } @@ -456,10 +455,6 @@ static void tcpip_adapter_dhcpc_cb(void) memcpy(&evt.event_info.got_ip.gw, &ip_info->gw, sizeof(evt.event_info.got_ip.gw)); esp_event_send(&evt); - - printf("ip: %s, ", inet_ntoa(ip_info->ip)); - printf("mask: %s, ", inet_ntoa(ip_info->netmask)); - printf("gw: %s\n", inet_ntoa(ip_info->gw)); } else { TCPIP_ADAPTER_DEBUG("ip unchanged\n"); } From df53d1588a56a94174dfae0dcb705d39015aa568 Mon Sep 17 00:00:00 2001 From: Wu Jian Gang Date: Tue, 6 Sep 2016 13:02:02 +0800 Subject: [PATCH 5/7] tcpip_adapter: typedef clean up --- components/esp32/event.c | 20 +++--- components/esp32/include/esp_event.h | 14 ++-- components/lwip/apps/dhcpserver.c | 11 ++-- .../lwip/include/lwip/apps/dhcpserver.h | 31 ++++----- .../tcpip_adapter/include/tcpip_adapter.h | 51 ++++++-------- components/tcpip_adapter/tcpip_adapter_lwip.c | 66 +++++++++---------- 6 files changed, 81 insertions(+), 112 deletions(-) diff --git a/components/esp32/event.c b/components/esp32/event.c index fa1830e47..9592bf4c6 100644 --- a/components/esp32/event.c +++ b/components/esp32/event.c @@ -80,16 +80,16 @@ static esp_err_t system_event_sta_gotip_default(system_event_t *event) WIFI_API_CALL_CHECK("esp_wifi_set_sta_ip", esp_wifi_set_sta_ip(), ESP_OK); printf("ip: " IPSTR ", mask: " IPSTR ", gw: " IPSTR "\n", - IP2STR(&event->event_info.got_ip.ip), - IP2STR(&event->event_info.got_ip.netmask), - IP2STR(&event->event_info.got_ip.gw)); + IP2STR(&event->event_info.got_ip.ip_info.ip), + IP2STR(&event->event_info.got_ip.ip_info.netmask), + IP2STR(&event->event_info.got_ip.ip_info.gw)); return ESP_OK; } esp_err_t system_event_ap_start_handle_default(system_event_t *event) { - struct ip_info ap_ip; + tcpip_adapter_ip_info_t ap_ip; uint8_t ap_mac[6]; WIFI_API_CALL_CHECK("esp_wifi_reg_rxcb", esp_wifi_reg_rxcb(WIFI_IF_AP, (wifi_rxcb_t)tcpip_adapter_ap_input), ESP_OK); @@ -112,7 +112,7 @@ esp_err_t system_event_ap_stop_handle_default(system_event_t *event) esp_err_t system_event_sta_start_handle_default(system_event_t *event) { - struct ip_info sta_ip; + tcpip_adapter_ip_info_t sta_ip; uint8_t sta_mac[6]; WIFI_API_CALL_CHECK("esp_wifi_mac_get", esp_wifi_get_mac(WIFI_IF_STA, sta_mac), ESP_OK); @@ -142,16 +142,14 @@ esp_err_t system_event_sta_connected_handle_default(system_event_t *event) if (status == TCPIP_ADAPTER_DHCP_INIT) { tcpip_adapter_dhcpc_start(TCPIP_ADAPTER_IF_STA); } else if (status == TCPIP_ADAPTER_DHCP_STOPPED) { - struct ip_info ip_info; + tcpip_adapter_ip_info_t sta_ip; system_event_t evt; - tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_STA, &ip_info); + tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_STA, &sta_ip); //notify event evt.event_id = SYSTEM_EVENT_STA_GOTIP; - memcpy(&evt.event_info.got_ip.ip, &ip_info.ip, sizeof(evt.event_info.got_ip.ip)); - memcpy(&evt.event_info.got_ip.netmask, &ip_info.netmask, sizeof(evt.event_info.got_ip.netmask)); - memcpy(&evt.event_info.got_ip.gw, &ip_info.gw, sizeof(evt.event_info.got_ip.gw)); + memcpy(&evt.event_info.got_ip.ip_info, &sta_ip, sizeof(tcpip_adapter_ip_info_t)); esp_event_send(&evt); } @@ -233,7 +231,7 @@ static esp_err_t esp_system_event_debug(system_event_t *event) } case SYSTEM_EVENT_STA_GOTIP: { - system_event_sta_gotip_t *got_ip; + system_event_sta_got_ip_t *got_ip; got_ip = &event->event_info.got_ip; WIFI_DEBUG("SYSTEM_EVENT_STA_GOTIP\n"); break; diff --git a/components/esp32/include/esp_event.h b/components/esp32/include/esp_event.h index ec9affe13..04a53636b 100755 --- a/components/esp32/include/esp_event.h +++ b/components/esp32/include/esp_event.h @@ -21,6 +21,8 @@ #include "esp_err.h" #include "esp_wifi.h" +#include "tcpip_adapter.h" + #ifdef __cplusplus extern "C" { #endif @@ -42,10 +44,6 @@ typedef enum { SYSTEM_EVENT_MAX } system_event_id_t; -typedef struct { - uint32_t addr; -} esp_ip_addr_t; - typedef struct { uint32_t status; /**< status of scanning APs*/ uint8_t number; @@ -73,10 +71,8 @@ typedef struct { } system_event_sta_authmode_change_t; typedef struct { - esp_ip_addr_t ip; - esp_ip_addr_t netmask; - esp_ip_addr_t gw; -} system_event_sta_gotip_t; + tcpip_adapter_ip_info_t ip_info; +} system_event_sta_got_ip_t; typedef struct { uint8_t mac[6]; /**< MAC address of the station connected to ESP32 soft-AP */ @@ -98,7 +94,7 @@ typedef union { system_event_sta_disconnected_t disconnected; /**< ESP32 station disconnected to AP */ system_event_sta_scan_done_t scan_done; /**< ESP32 station scan (APs) done */ system_event_sta_authmode_change_t auth_change; /**< the auth mode of AP ESP32 station connected to changed */ - system_event_sta_gotip_t got_ip; + system_event_sta_got_ip_t got_ip; system_event_ap_staconnected_t sta_connected; /**< a station connected to ESP32 soft-AP */ system_event_ap_stadisconnected_t sta_disconnected; /**< a station disconnected to ESP32 soft-AP */ system_event_ap_probe_req_rx_t ap_probereqrecved; /**< ESP32 softAP receive probe request packet */ diff --git a/components/lwip/apps/dhcpserver.c b/components/lwip/apps/dhcpserver.c index 8abf7ca7c..ca0592c91 100644 --- a/components/lwip/apps/dhcpserver.c +++ b/components/lwip/apps/dhcpserver.c @@ -266,9 +266,9 @@ static u8_t *add_offer_options(u8_t *optptr) *optptr++ = ip4_addr4(&ipadd); if (dhcps_router_enabled(dhcps_offer)) { - struct ip_info if_ip; + tcpip_adapter_ip_info_t if_ip; //bzero(&if_ip, sizeof(struct ip_info)); - memset(&if_ip , 0x00, sizeof(struct ip_info)); + memset(&if_ip , 0x00, sizeof(tcpip_adapter_ip_info_t)); tcpip_adapter_get_ip_info(WIFI_IF_AP, &if_ip); @@ -1024,18 +1024,17 @@ static void dhcps_poll_set(u32_t ip) * : info -- The current ip info * Returns : none *******************************************************************************/ -void dhcps_start(struct netif *netif, struct ip_info *info) +void dhcps_start(struct netif *netif, ip4_addr_t ip) { struct netif *apnetif = netif; - if (apnetif->dhcps_pcb != NULL) { udp_remove(apnetif->dhcps_pcb); } pcb_dhcps = udp_new(); - if (pcb_dhcps == NULL || info == NULL) { + if (pcb_dhcps == NULL || ip4_addr_isany_val(ip)) { printf("dhcps_start(): could not obtain pcb\n"); } @@ -1043,7 +1042,7 @@ void dhcps_start(struct netif *netif, struct ip_info *info) IP4_ADDR(&broadcast_dhcps, 255, 255, 255, 255); - server_address = info->ip; + server_address.addr = ip.addr; dhcps_poll_set(server_address.addr); client_address_plus.addr = dhcps_poll.start_ip.addr; diff --git a/components/lwip/include/lwip/apps/dhcpserver.h b/components/lwip/include/lwip/apps/dhcpserver.h index 021457153..39781d966 100644 --- a/components/lwip/include/lwip/apps/dhcpserver.h +++ b/components/lwip/include/lwip/apps/dhcpserver.h @@ -17,17 +17,6 @@ #include "lwip/ip_addr.h" //#include "esp_common.h" #define USE_DNS -/* Here for now until needed in other places in lwIP */ -#ifndef isprint -#define in_range(c, lo, up) ((u8_t)c >= lo && (u8_t)c <= up) -#define isprint(c) in_range(c, 0x20, 0x7f) -#define isdigit(c) in_range(c, '0', '9') -#define isxdigit(c) (isdigit(c) || in_range(c, 'a', 'f') || in_range(c, 'A', 'F')) -#define islower(c) in_range(c, 'a', 'z') -#define isspace(c) (c == ' ' || c == '\f' || c == '\n' || c == '\r' || c == '\t' || c == '\v') -#endif - -#ifdef LWIP_ESP8266 typedef struct dhcps_state{ s16_t state; @@ -164,6 +153,12 @@ typedef enum CLASSLESS_ROUTE = 121, } dhcp_msg_option; +/* Defined in esp_misc.h */ +typedef struct { + bool enable; + ip4_addr_t start_ip; + ip4_addr_t end_ip; +} dhcps_lease_t; enum dhcps_offer_option{ OFFER_START = 0x00, @@ -188,22 +183,18 @@ typedef struct _list_node{ typedef u32_t dhcps_time_t; typedef u8_t dhcps_offer_t; -/* struct dhcps_lease defined in tcpip_adapter.h */ -typedef struct dhcps_lease dhcps_lease_t; - -typedef struct _dhcps_options{ +typedef struct { dhcps_offer_t dhcps_offer; dhcps_time_t dhcps_time; dhcps_lease_t dhcps_poll; -}dhcps_options_t; +} dhcps_options_t; -#define dhcps_router_enabled(offer) ((offer & OFFER_ROUTER) != 0) -void dhcps_start(struct netif *netif, struct ip_info *info); +#define dhcps_router_enabled(offer) ((offer & OFFER_ROUTER) != 0) + +void dhcps_start(struct netif *netif, ip4_addr_t ip); void dhcps_stop(struct netif *netif); void *dhcps_option_info(u8_t op_id, u32_t opt_len); bool dhcp_search_ip_on_mac(u8_t *mac, ip4_addr_t *ip); #endif -#endif - diff --git a/components/tcpip_adapter/include/tcpip_adapter.h b/components/tcpip_adapter/include/tcpip_adapter.h index 8a51eae0f..9cae530ef 100644 --- a/components/tcpip_adapter/include/tcpip_adapter.h +++ b/components/tcpip_adapter/include/tcpip_adapter.h @@ -26,6 +26,7 @@ #if CONFIG_TCPIP_LWIP #include "lwip/ip_addr.h" +#include "apps/dhcpserver.h" #define IP2STR(ipaddr) ip4_addr1_16(ipaddr), \ ip4_addr2_16(ipaddr), \ @@ -34,22 +35,13 @@ #define IPSTR "%d.%d.%d.%d" -struct ip_info { +typedef struct { ip4_addr_t ip; ip4_addr_t netmask; ip4_addr_t gw; -}; +} tcpip_adapter_ip_info_t; -/* Also used by dhcpserver.h */ -struct dhcps_lease { - bool enable; - ip4_addr_t start_ip; - ip4_addr_t end_ip; -}; - -typedef struct dhcps_lease tcpip_adapter_dhcps_lease; - -#endif +typedef dhcps_lease_t tcpip_adapter_dhcps_lease_t; #if CONFIG_DHCP_STA_LIST struct station_list { @@ -59,6 +51,8 @@ struct station_list { }; #endif +#endif + #define ESP_ERR_TCPIP_ADAPTER_BASE 0x5000 // base should be moved to esp_err.h #define ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS ESP_ERR_TCPIP_ADAPTER_BASE + 0x00 @@ -83,24 +77,23 @@ typedef enum { TCPIP_ADAPTER_DHCP_STATUS_MAX } tcpip_adapter_dhcp_status_t; -/*op*/ typedef enum{ TCPIP_ADAPTER_OP_START = 0, TCPIP_ADAPTER_OP_SET, TCPIP_ADAPTER_OP_GET, TCPIP_ADAPTER_OP_MAX -} tcpip_adapter_option_mode; +} tcpip_adapter_option_mode_t; typedef enum{ - TCPIP_ADAPTER_ROUTER_SOLICITATION_ADDRESS = 32, - TCPIP_ADAPTER_REQUESTED_IP_ADDRESS = 50, - TCPIP_ADAPTER_IP_ADDRESS_LEASE_TIME = 51, - TCPIP_ADAPTER_IP_REQUEST_RETRY_TIME = 52, -} tcpip_adapter_option_id; + TCPIP_ADAPTER_ROUTER_SOLICITATION_ADDRESS = 32, + TCPIP_ADAPTER_REQUESTED_IP_ADDRESS = 50, + TCPIP_ADAPTER_IP_ADDRESS_LEASE_TIME = 51, + TCPIP_ADAPTER_IP_REQUEST_RETRY_TIME = 52, +} tcpip_adapter_option_id_t; void tcpip_adapter_init(void); -esp_err_t tcpip_adapter_start(tcpip_adapter_if_t tcpip_if, uint8_t *mac, struct ip_info *info); +esp_err_t tcpip_adapter_start(tcpip_adapter_if_t tcpip_if, uint8_t *mac, tcpip_adapter_ip_info_t *ip_info); esp_err_t tcpip_adapter_stop(tcpip_adapter_if_t tcpip_if); @@ -108,9 +101,9 @@ esp_err_t tcpip_adapter_up(tcpip_adapter_if_t tcpip_if); esp_err_t tcpip_adapter_down(tcpip_adapter_if_t tcpip_if); -esp_err_t tcpip_adapter_get_ip_info(tcpip_adapter_if_t tcpip_if, struct ip_info *if_ip); +esp_err_t tcpip_adapter_get_ip_info(tcpip_adapter_if_t tcpip_if, tcpip_adapter_ip_info_t *ip_info); -esp_err_t tcpip_adapter_set_ip_info(tcpip_adapter_if_t tcpip_if, struct ip_info *if_ip); +esp_err_t tcpip_adapter_set_ip_info(tcpip_adapter_if_t tcpip_if, tcpip_adapter_ip_info_t *ip_info); #if 0 esp_err_t tcpip_adapter_get_mac(tcpip_adapter_if_t tcpip_if, uint8_t *mac); @@ -119,23 +112,17 @@ esp_err_t tcpip_adapter_set_mac(tcpip_adapter_if_t tcpip_if, uint8_t *mac); #endif esp_err_t tcpip_adapter_dhcps_get_status(tcpip_adapter_if_t tcpip_if, tcpip_adapter_dhcp_status_t *status); -esp_err_t tcpip_adapter_dhcps_option(tcpip_adapter_option_mode opt_op, tcpip_adapter_option_id opt_id, void *opt_val, uint32_t opt_len); +esp_err_t tcpip_adapter_dhcps_option(tcpip_adapter_option_mode_t opt_op, tcpip_adapter_option_id_t opt_id, void *opt_val, uint32_t opt_len); esp_err_t tcpip_adapter_dhcps_start(tcpip_adapter_if_t tcpip_if); esp_err_t tcpip_adapter_dhcps_stop(tcpip_adapter_if_t tcpip_if); esp_err_t tcpip_adapter_dhcpc_get_status(tcpip_adapter_if_t tcpip_if, tcpip_adapter_dhcp_status_t *status); -esp_err_t tcpip_adapter_dhcpc_option(tcpip_adapter_option_mode opt_op, tcpip_adapter_option_id opt_id, void *opt_val, uint32_t opt_len); +esp_err_t tcpip_adapter_dhcpc_option(tcpip_adapter_option_mode_t opt_op, tcpip_adapter_option_id_t opt_id, void *opt_val, uint32_t opt_len); esp_err_t tcpip_adapter_dhcpc_start(tcpip_adapter_if_t tcpip_if); esp_err_t tcpip_adapter_dhcpc_stop(tcpip_adapter_if_t tcpip_if); -esp_err_t tcpip_adapter_sta_input(void *buffer, uint16_t len, void* eb); -esp_err_t tcpip_adapter_ap_input(void *buffer, uint16_t len, void* eb); - -#if 0 -//esp_err_t tcpip_dep_input(wifi_interface_t wifi_if, void *buffer, uint16 len); - -esp_err_t tcpip_adapter_output(tcpip_adapter_if_t tcpip_if, void *buffer, uint16_t len); -#endif +esp_err_t tcpip_adapter_sta_input(void *buffer, uint16_t len, void *eb); +esp_err_t tcpip_adapter_ap_input(void *buffer, uint16_t len, void *eb); wifi_interface_t tcpip_adapter_get_wifi_if(void *dev); diff --git a/components/tcpip_adapter/tcpip_adapter_lwip.c b/components/tcpip_adapter/tcpip_adapter_lwip.c index 3fb42aeb1..0375e6284 100644 --- a/components/tcpip_adapter/tcpip_adapter_lwip.c +++ b/components/tcpip_adapter/tcpip_adapter_lwip.c @@ -31,7 +31,7 @@ #include "esp_event.h" static struct netif *esp_netif[TCPIP_ADAPTER_IF_MAX]; -static struct ip_info esp_ip[TCPIP_ADAPTER_IF_MAX]; +static tcpip_adapter_ip_info_t esp_ip[TCPIP_ADAPTER_IF_MAX]; static tcpip_adapter_dhcp_status_t dhcps_status = TCPIP_ADAPTER_DHCP_INIT; static tcpip_adapter_dhcp_status_t dhcpc_status = TCPIP_ADAPTER_DHCP_INIT; @@ -53,9 +53,9 @@ void tcpip_adapter_init(void) } } -esp_err_t tcpip_adapter_start(tcpip_adapter_if_t tcpip_if, uint8_t *mac, struct ip_info *info) +esp_err_t tcpip_adapter_start(tcpip_adapter_if_t tcpip_if, uint8_t *mac, tcpip_adapter_ip_info_t *ip_info) { - if (tcpip_if >= TCPIP_ADAPTER_IF_MAX || mac == NULL || info == NULL) { + if (tcpip_if >= TCPIP_ADAPTER_IF_MAX || mac == NULL || ip_info == NULL) { return ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS; } @@ -65,17 +65,17 @@ esp_err_t tcpip_adapter_start(tcpip_adapter_if_t tcpip_if, uint8_t *mac, struct return ESP_ERR_NO_MEM; } memcpy(esp_netif[tcpip_if]->hwaddr, mac, NETIF_MAX_HWADDR_LEN); - netif_add(esp_netif[tcpip_if], &info->ip, &info->netmask, &info->gw, NULL, wlanif_init, tcpip_input); + netif_add(esp_netif[tcpip_if], &ip_info->ip, &ip_info->netmask, &ip_info->gw, NULL, wlanif_init, tcpip_input); } if (tcpip_if == TCPIP_ADAPTER_IF_AP) { netif_set_up(esp_netif[tcpip_if]); if (dhcps_status == TCPIP_ADAPTER_DHCP_INIT) { - dhcps_start(esp_netif[tcpip_if], info); + dhcps_start(esp_netif[tcpip_if], ip_info->ip); printf("dhcp server start:(ip: " IPSTR ", mask: " IPSTR ", gw: " IPSTR ")\n", - IP2STR(&info->ip), IP2STR(&info->netmask), IP2STR(&info->gw)); + IP2STR(&ip_info->ip), IP2STR(&ip_info->netmask), IP2STR(&ip_info->gw)); dhcps_status = TCPIP_ADAPTER_DHCP_STARTED; } @@ -164,37 +164,37 @@ esp_err_t tcpip_adapter_down(tcpip_adapter_if_t tcpip_if) return ESP_OK; } -esp_err_t tcpip_adapter_get_ip_info(tcpip_adapter_if_t tcpip_if, struct ip_info *if_ip) +esp_err_t tcpip_adapter_get_ip_info(tcpip_adapter_if_t tcpip_if, tcpip_adapter_ip_info_t *ip_info) { struct netif *p_netif; - if (tcpip_if >= TCPIP_ADAPTER_IF_MAX || if_ip == NULL) { + if (tcpip_if >= TCPIP_ADAPTER_IF_MAX || ip_info == NULL) { return ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS; } p_netif = esp_netif[tcpip_if]; if (p_netif != NULL && netif_is_up(p_netif)) { - ip4_addr_set(&if_ip->ip, ip_2_ip4(&p_netif->ip_addr)); - ip4_addr_set(&if_ip->netmask, ip_2_ip4(&p_netif->netmask)); - ip4_addr_set(&if_ip->gw, ip_2_ip4(&p_netif->gw)); + ip4_addr_set(&ip_info->ip, ip_2_ip4(&p_netif->ip_addr)); + ip4_addr_set(&ip_info->netmask, ip_2_ip4(&p_netif->netmask)); + ip4_addr_set(&ip_info->gw, ip_2_ip4(&p_netif->gw)); return ESP_OK; } - ip4_addr_copy(if_ip->ip, esp_ip[tcpip_if].ip); - ip4_addr_copy(if_ip->gw, esp_ip[tcpip_if].gw); - ip4_addr_copy(if_ip->netmask, esp_ip[tcpip_if].netmask); + ip4_addr_copy(ip_info->ip, esp_ip[tcpip_if].ip); + ip4_addr_copy(ip_info->gw, esp_ip[tcpip_if].gw); + ip4_addr_copy(ip_info->netmask, esp_ip[tcpip_if].netmask); return ESP_OK; } -esp_err_t tcpip_adapter_set_ip_info(tcpip_adapter_if_t tcpip_if, struct ip_info *if_ip) +esp_err_t tcpip_adapter_set_ip_info(tcpip_adapter_if_t tcpip_if, tcpip_adapter_ip_info_t *ip_info) { struct netif *p_netif; tcpip_adapter_dhcp_status_t status; - if (tcpip_if >= TCPIP_ADAPTER_IF_MAX || if_ip == NULL) { + if (tcpip_if >= TCPIP_ADAPTER_IF_MAX || ip_info == NULL) { return ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS; } @@ -212,14 +212,14 @@ esp_err_t tcpip_adapter_set_ip_info(tcpip_adapter_if_t tcpip_if, struct ip_info } } - ip4_addr_copy(esp_ip[tcpip_if].ip, if_ip->ip); - ip4_addr_copy(esp_ip[tcpip_if].gw, if_ip->gw); - ip4_addr_copy(esp_ip[tcpip_if].netmask, if_ip->netmask); + ip4_addr_copy(esp_ip[tcpip_if].ip, ip_info->ip); + ip4_addr_copy(esp_ip[tcpip_if].gw, ip_info->gw); + ip4_addr_copy(esp_ip[tcpip_if].netmask, ip_info->netmask); p_netif = esp_netif[tcpip_if]; if (p_netif != NULL && netif_is_up(p_netif)) { - netif_set_addr(p_netif, &if_ip->ip, &if_ip->netmask, &if_ip->gw); + netif_set_addr(p_netif, &ip_info->ip, &ip_info->netmask, &ip_info->gw); } return ESP_OK; @@ -265,7 +265,7 @@ esp_err_t tcpip_adapter_set_mac(tcpip_adapter_if_t tcpip_if, uint8_t mac[6]) } #endif -esp_err_t tcpip_adapter_dhcps_option(tcpip_adapter_option_mode opt_op, tcpip_adapter_option_id opt_id, void *opt_val, uint32_t opt_len) +esp_err_t tcpip_adapter_dhcps_option(tcpip_adapter_option_mode_t opt_op, tcpip_adapter_option_id_t opt_id, void *opt_val, uint32_t opt_len) { void *opt_info = dhcps_option_info(opt_id, opt_len); @@ -308,18 +308,18 @@ esp_err_t tcpip_adapter_dhcps_option(tcpip_adapter_option_mode opt_op, tcpip_ada if (*(uint32_t*)opt_val != 0) *(uint32_t*)opt_info = *(uint32_t*)opt_val; else - *(uint32_t*)opt_info = DHCPS_LEASE_TIME_DEF; + *(uint32_t*)opt_info = DHCPS_LEASE_TIME_DEF; break; } case REQUESTED_IP_ADDRESS: { - struct ip_info info; + tcpip_adapter_ip_info_t info; uint32_t softap_ip = 0; uint32_t start_ip = 0; uint32_t end_ip = 0; - struct dhcps_lease *poll = opt_val; + dhcps_lease_t *poll = opt_val; - memset(&info, 0x00, sizeof(struct ip_info)); + memset(&info, 0x00, sizeof(tcpip_adapter_ip_info_t)); tcpip_adapter_get_ip_info(WIFI_IF_AP, &info); softap_ip = htonl(info.ip.addr); start_ip = htonl(poll->start_ip.addr); @@ -377,9 +377,9 @@ esp_err_t tcpip_adapter_dhcps_start(tcpip_adapter_if_t tcpip_if) struct netif *p_netif = esp_netif[tcpip_if]; if (p_netif != NULL && netif_is_up(p_netif)) { - struct ip_info default_ip; + tcpip_adapter_ip_info_t default_ip; tcpip_adapter_get_ip_info(WIFI_IF_AP, &default_ip); - dhcps_start(p_netif, &default_ip); + dhcps_start(p_netif, default_ip.ip); dhcps_status = TCPIP_ADAPTER_DHCP_STARTED; TCPIP_ADAPTER_DEBUG("dhcp server start successfully\n"); return ESP_OK; @@ -421,7 +421,7 @@ esp_err_t tcpip_adapter_dhcps_stop(tcpip_adapter_if_t tcpip_if) return ESP_OK; } -esp_err_t tcpip_adapter_dhcpc_option(tcpip_adapter_option_mode opt_op, tcpip_adapter_option_id opt_id, void *opt_val, uint32_t opt_len) +esp_err_t tcpip_adapter_dhcpc_option(tcpip_adapter_option_mode_t opt_op, tcpip_adapter_option_id_t opt_id, void *opt_val, uint32_t opt_len) { // TODO: when dhcp request timeout,change the retry count return ESP_OK; @@ -430,7 +430,7 @@ esp_err_t tcpip_adapter_dhcpc_option(tcpip_adapter_option_mode opt_op, tcpip_ada static void tcpip_adapter_dhcpc_cb(void) { struct netif *netif = esp_netif[TCPIP_ADAPTER_IF_STA]; - struct ip_info *ip_info = &esp_ip[TCPIP_ADAPTER_IF_STA]; + tcpip_adapter_ip_info_t *ip_info = &esp_ip[TCPIP_ADAPTER_IF_STA]; system_event_t evt; if (!netif) { @@ -450,9 +450,7 @@ static void tcpip_adapter_dhcpc_cb(void) //notify event evt.event_id = SYSTEM_EVENT_STA_GOTIP; - memcpy(&evt.event_info.got_ip.ip, &ip_info->ip, sizeof(evt.event_info.got_ip.ip)); - memcpy(&evt.event_info.got_ip.netmask, &ip_info->netmask, sizeof(evt.event_info.got_ip.netmask)); - memcpy(&evt.event_info.got_ip.gw, &ip_info->gw, sizeof(evt.event_info.got_ip.gw)); + memcpy(&evt.event_info.got_ip.ip_info, ip_info, sizeof(tcpip_adapter_ip_info_t)); esp_event_send(&evt); } else { @@ -541,13 +539,13 @@ esp_err_t tcpip_adapter_dhcpc_stop(tcpip_adapter_if_t tcpip_if) return ESP_OK; } -esp_err_t tcpip_adapter_sta_input(void *buffer, uint16_t len, void* eb) +esp_err_t tcpip_adapter_sta_input(void *buffer, uint16_t len, void *eb) { wlanif_input(esp_netif[TCPIP_ADAPTER_IF_STA], buffer, len, eb); return ESP_OK; } -esp_err_t tcpip_adapter_ap_input(void *buffer, uint16_t len, void* eb) +esp_err_t tcpip_adapter_ap_input(void *buffer, uint16_t len, void *eb) { wlanif_input(esp_netif[TCPIP_ADAPTER_IF_AP], buffer, len, eb); return ESP_OK; From 80d60270bb63d1c79b4ccdc757a2f826043ca0d1 Mon Sep 17 00:00:00 2001 From: Wu Jian Gang Date: Mon, 5 Sep 2016 20:14:58 +0800 Subject: [PATCH 6/7] event: not post got ip event if static is invalid --- components/esp32/event.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/components/esp32/event.c b/components/esp32/event.c index 9592bf4c6..92b751038 100644 --- a/components/esp32/event.c +++ b/components/esp32/event.c @@ -143,15 +143,20 @@ esp_err_t system_event_sta_connected_handle_default(system_event_t *event) tcpip_adapter_dhcpc_start(TCPIP_ADAPTER_IF_STA); } else if (status == TCPIP_ADAPTER_DHCP_STOPPED) { tcpip_adapter_ip_info_t sta_ip; - system_event_t evt; tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_STA, &sta_ip); - //notify event - evt.event_id = SYSTEM_EVENT_STA_GOTIP; - memcpy(&evt.event_info.got_ip.ip_info, &sta_ip, sizeof(tcpip_adapter_ip_info_t)); + if (!(ip4_addr_isany_val(sta_ip.ip) || ip4_addr_isany_val(sta_ip.netmask) || ip4_addr_isany_val(sta_ip.gw))) { + system_event_t evt; - esp_event_send(&evt); + //notify event + evt.event_id = SYSTEM_EVENT_STA_GOTIP; + memcpy(&evt.event_info.got_ip.ip_info, &sta_ip, sizeof(tcpip_adapter_ip_info_t)); + + esp_event_send(&evt); + } else { + WIFI_DEBUG("invalid static ip\n"); + } } return ESP_OK; From c97aa4a709668b88ed4af6e385e88f108320fd79 Mon Sep 17 00:00:00 2001 From: Wu Jian Gang Date: Mon, 5 Sep 2016 20:24:47 +0800 Subject: [PATCH 7/7] tcpip_adapter: fix dhcp client work flow --- components/tcpip_adapter/tcpip_adapter_lwip.c | 33 +++++++++++++------ 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/components/tcpip_adapter/tcpip_adapter_lwip.c b/components/tcpip_adapter/tcpip_adapter_lwip.c index 0375e6284..2facea578 100644 --- a/components/tcpip_adapter/tcpip_adapter_lwip.c +++ b/components/tcpip_adapter/tcpip_adapter_lwip.c @@ -105,8 +105,15 @@ esp_err_t tcpip_adapter_stop(tcpip_adapter_if_t tcpip_if) dhcps_stop(esp_netif[tcpip_if]); // TODO: dhcps checks status by its self dhcps_status = TCPIP_ADAPTER_DHCP_INIT; } else if (tcpip_if == TCPIP_ADAPTER_IF_STA) { + dhcp_release(esp_netif[tcpip_if]); dhcp_stop(esp_netif[tcpip_if]); + dhcp_cleanup(esp_netif[tcpip_if]); + dhcpc_status = TCPIP_ADAPTER_DHCP_INIT; + + ip4_addr_set_zero(&esp_ip[tcpip_if].ip); + ip4_addr_set_zero(&esp_ip[tcpip_if].gw); + ip4_addr_set_zero(&esp_ip[tcpip_if].netmask); } netif_remove(esp_netif[tcpip_if]); @@ -144,20 +151,16 @@ esp_err_t tcpip_adapter_down(tcpip_adapter_if_t tcpip_if) return ESP_ERR_TCPIP_ADAPTER_IF_NOT_READY; } - if (esp_netif[tcpip_if]->dhcp != NULL) { - dhcp_release(esp_netif[tcpip_if]); + if (dhcpc_status == TCPIP_ADAPTER_DHCP_STARTED) { dhcp_stop(esp_netif[tcpip_if]); - dhcp_cleanup(esp_netif[tcpip_if]); - if (dhcpc_status != TCPIP_ADAPTER_DHCP_STOPPED) { - dhcpc_status = TCPIP_ADAPTER_DHCP_INIT; - } + dhcpc_status = TCPIP_ADAPTER_DHCP_INIT; ip4_addr_set_zero(&esp_ip[tcpip_if].ip); ip4_addr_set_zero(&esp_ip[tcpip_if].gw); ip4_addr_set_zero(&esp_ip[tcpip_if].netmask); } - + netif_set_down(esp_netif[tcpip_if]); } @@ -194,7 +197,8 @@ esp_err_t tcpip_adapter_set_ip_info(tcpip_adapter_if_t tcpip_if, tcpip_adapter_i struct netif *p_netif; tcpip_adapter_dhcp_status_t status; - if (tcpip_if >= TCPIP_ADAPTER_IF_MAX || ip_info == NULL) { + if (tcpip_if >= TCPIP_ADAPTER_IF_MAX || ip_info == NULL || + ip4_addr_isany_val(ip_info->ip) || ip4_addr_isany_val(ip_info->netmask) || ip4_addr_isany_val(ip_info->gw)) { return ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS; } @@ -430,8 +434,6 @@ esp_err_t tcpip_adapter_dhcpc_option(tcpip_adapter_option_mode_t opt_op, tcpip_a static void tcpip_adapter_dhcpc_cb(void) { struct netif *netif = esp_netif[TCPIP_ADAPTER_IF_STA]; - tcpip_adapter_ip_info_t *ip_info = &esp_ip[TCPIP_ADAPTER_IF_STA]; - system_event_t evt; if (!netif) { TCPIP_ADAPTER_DEBUG("null netif=%p\n", netif); @@ -439,10 +441,13 @@ static void tcpip_adapter_dhcpc_cb(void) } if ( !ip4_addr_cmp(ip_2_ip4(&netif->ip_addr), IP4_ADDR_ANY) ) { + tcpip_adapter_ip_info_t *ip_info = &esp_ip[TCPIP_ADAPTER_IF_STA]; + //check whether IP is changed if ( !ip4_addr_cmp(ip_2_ip4(&netif->ip_addr), &ip_info->ip) || !ip4_addr_cmp(ip_2_ip4(&netif->netmask), &ip_info->netmask) || !ip4_addr_cmp(ip_2_ip4(&netif->gw), &ip_info->gw) ) { + system_event_t evt; ip4_addr_set(&ip_info->ip, ip_2_ip4(&netif->ip_addr)); ip4_addr_set(&ip_info->netmask, ip_2_ip4(&netif->netmask)); @@ -479,6 +484,10 @@ esp_err_t tcpip_adapter_dhcpc_start(tcpip_adapter_if_t tcpip_if) if (dhcpc_status != TCPIP_ADAPTER_DHCP_STARTED) { struct netif *p_netif = esp_netif[tcpip_if]; + ip4_addr_set_zero(&esp_ip[tcpip_if].ip); + ip4_addr_set_zero(&esp_ip[tcpip_if].gw); + ip4_addr_set_zero(&esp_ip[tcpip_if].netmask); + if (p_netif != NULL) { if (netif_is_up(p_netif)) { TCPIP_ADAPTER_DEBUG("dhcp client init ip/mask/gw to all-0\n"); @@ -525,6 +534,10 @@ esp_err_t tcpip_adapter_dhcpc_stop(tcpip_adapter_if_t tcpip_if) if (p_netif != NULL) { dhcp_stop(p_netif); + + ip4_addr_set_zero(&esp_ip[tcpip_if].ip); + ip4_addr_set_zero(&esp_ip[tcpip_if].gw); + ip4_addr_set_zero(&esp_ip[tcpip_if].netmask); } else { TCPIP_ADAPTER_DEBUG("dhcp client if not ready\n"); return ESP_ERR_TCPIP_ADAPTER_IF_NOT_READY;