Просмотр исходного кода

example_common: fix netif ips may be printed before got ipv4

Chen Yudong 3 лет назад
Родитель
Сommit
9c28e3f1cb

+ 24 - 8
examples/common_components/protocol_examples_common/eth_connect.c

@@ -21,6 +21,9 @@
 
 static const char *TAG = "ethernet_connect";
 static SemaphoreHandle_t s_semph_get_ip_addrs = NULL;
+#if CONFIG_EXAMPLE_CONNECT_IPV6
+static SemaphoreHandle_t s_semph_get_ip6_addrs = NULL;
+#endif
 
 static esp_netif_t *eth_start(void);
 static void eth_stop(void);
@@ -36,9 +39,7 @@ static void eth_on_got_ip(void *arg, esp_event_base_t event_base,
         return;
     }
     ESP_LOGI(TAG, "Got IPv4 event: Interface \"%s\" address: " IPSTR, esp_netif_get_desc(event->esp_netif), IP2STR(&event->ip_info.ip));
-    if (s_semph_get_ip_addrs) {
-        xSemaphoreGive(s_semph_get_ip_addrs);
-    }
+    xSemaphoreGive(s_semph_get_ip_addrs);
 }
 
 #if CONFIG_EXAMPLE_CONNECT_IPV6
@@ -54,7 +55,7 @@ static void eth_on_got_ipv6(void *arg, esp_event_base_t event_base,
     ESP_LOGI(TAG, "Got IPv6 event: Interface \"%s\" address: " IPV6STR ", type: %s", esp_netif_get_desc(event->esp_netif),
              IPV62STR(event->ip6_info.ip), example_ipv6_addr_types_to_str[ipv6_type]);
     if (ipv6_type == EXAMPLE_CONNECT_PREFERRED_IPV6_TYPE) {
-        xSemaphoreGive(s_semph_get_ip_addrs);
+        xSemaphoreGive(s_semph_get_ip6_addrs);
     }
 }
 
@@ -204,16 +205,31 @@ void example_ethernet_shutdown(void)
     }
     vSemaphoreDelete(s_semph_get_ip_addrs);
     s_semph_get_ip_addrs = NULL;
+#if CONFIG_EXAMPLE_CONNECT_IPV6
+    vSemaphoreDelete(s_semph_get_ip6_addrs);
+    s_semph_get_ip6_addrs = NULL;
+#endif
     eth_stop();
 }
 
 esp_err_t example_ethernet_connect(void)
 {
-    s_semph_get_ip_addrs = xSemaphoreCreateCounting(NR_OF_IP_ADDRESSES_TO_WAIT_FOR, 0);
+    s_semph_get_ip_addrs = xSemaphoreCreateBinary();
+    if (s_semph_get_ip_addrs == NULL) {
+        return ESP_ERR_NO_MEM;
+    }
+#if CONFIG_EXAMPLE_CONNECT_IPV6
+    s_semph_get_ip6_addrs = xSemaphoreCreateBinary();
+    if (s_semph_get_ip6_addrs == NULL) {
+        vSemaphoreDelete(s_semph_get_ip_addrs);
+        return ESP_ERR_NO_MEM;
+    }
+#endif
     eth_start();
     ESP_LOGI(TAG, "Waiting for IP(s).");
-    for (int i = 0; i < NR_OF_IP_ADDRESSES_TO_WAIT_FOR; ++i) {
-        xSemaphoreTake(s_semph_get_ip_addrs, portMAX_DELAY);
-    }
+    xSemaphoreTake(s_semph_get_ip_addrs, portMAX_DELAY);
+#if CONFIG_EXAMPLE_CONNECT_IPV6
+    xSemaphoreTake(s_semph_get_ip6_addrs, portMAX_DELAY);
+#endif
     return ESP_OK;
 }

+ 0 - 7
examples/common_components/protocol_examples_common/include/example_common_private.h

@@ -15,13 +15,6 @@
 extern "C" {
 #endif
 
-#if CONFIG_EXAMPLE_CONNECT_IPV6
-#define NR_OF_IP_ADDRESSES_TO_WAIT_FOR (2)
-#else
-#define NR_OF_IP_ADDRESSES_TO_WAIT_FOR (1)
-#endif
-
-
 #if CONFIG_EXAMPLE_CONNECT_IPV6
 #define MAX_IP6_ADDRS_PER_NETIF (5)
 

+ 30 - 9
examples/common_components/protocol_examples_common/wifi_connect.c

@@ -22,6 +22,9 @@
 static const char *TAG = "example_connect";
 static esp_netif_t *s_example_sta_netif = NULL;
 static SemaphoreHandle_t s_semph_get_ip_addrs = NULL;
+#if CONFIG_EXAMPLE_CONNECT_IPV6
+static SemaphoreHandle_t s_semph_get_ip6_addrs = NULL;
+#endif
 
 #if CONFIG_EXAMPLE_WIFI_SCAN_METHOD_FAST
 #define EXAMPLE_WIFI_SCAN_METHOD WIFI_FAST_SCAN
@@ -63,13 +66,15 @@ static void example_handler_on_wifi_disconnect(void *arg, esp_event_base_t event
     s_retry_num++;
     if (s_retry_num > CONFIG_EXAMPLE_WIFI_CONN_MAX_RETRY) {
         ESP_LOGI(TAG, "WiFi Connect failed %d times, stop reconnect.", s_retry_num);
+        /* let example_wifi_sta_do_connect() return */
         if (s_semph_get_ip_addrs) {
-            /* let example_wifi_sta_do_connect() return */
             xSemaphoreGive(s_semph_get_ip_addrs);
+        }
 #if CONFIG_EXAMPLE_CONNECT_IPV6
-            xSemaphoreGive(s_semph_get_ip_addrs);
-#endif
+        if (s_semph_get_ip6_addrs) {
+            xSemaphoreGive(s_semph_get_ip6_addrs);
         }
+#endif
         return;
     }
     ESP_LOGI(TAG, "Wi-Fi disconnected, trying to reconnect...");
@@ -117,8 +122,8 @@ static void example_handler_on_sta_got_ipv6(void *arg, esp_event_base_t event_ba
              IPV62STR(event->ip6_info.ip), example_ipv6_addr_types_to_str[ipv6_type]);
 
     if (ipv6_type == EXAMPLE_CONNECT_PREFERRED_IPV6_TYPE) {
-        if (s_semph_get_ip_addrs) {
-            xSemaphoreGive(s_semph_get_ip_addrs);
+        if (s_semph_get_ip6_addrs) {
+            xSemaphoreGive(s_semph_get_ip6_addrs);
         } else {
             ESP_LOGI(TAG, "- IPv6 address: " IPV6STR ", type: %s", IPV62STR(event->ip6_info.ip), example_ipv6_addr_types_to_str[ipv6_type]);
         }
@@ -162,7 +167,17 @@ void example_wifi_stop(void)
 esp_err_t example_wifi_sta_do_connect(wifi_config_t wifi_config, bool wait)
 {
     if (wait) {
-        s_semph_get_ip_addrs = xSemaphoreCreateCounting(NR_OF_IP_ADDRESSES_TO_WAIT_FOR, 0);
+        s_semph_get_ip_addrs = xSemaphoreCreateBinary();
+        if (s_semph_get_ip_addrs == NULL) {
+            return ESP_ERR_NO_MEM;
+        }
+#if CONFIG_EXAMPLE_CONNECT_IPV6
+        s_semph_get_ip6_addrs = xSemaphoreCreateBinary();
+        if (s_semph_get_ip6_addrs == NULL) {
+            vSemaphoreDelete(s_semph_get_ip_addrs);
+            return ESP_ERR_NO_MEM;
+        }
+#endif
     }
     s_retry_num = 0;
     ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &example_handler_on_wifi_disconnect, NULL));
@@ -181,9 +196,10 @@ esp_err_t example_wifi_sta_do_connect(wifi_config_t wifi_config, bool wait)
     }
     if (wait) {
         ESP_LOGI(TAG, "Waiting for IP(s)");
-        for (int i = 0; i < NR_OF_IP_ADDRESSES_TO_WAIT_FOR; ++i) {
-            xSemaphoreTake(s_semph_get_ip_addrs, portMAX_DELAY);
-        }
+        xSemaphoreTake(s_semph_get_ip_addrs, portMAX_DELAY);
+#if CONFIG_EXAMPLE_CONNECT_IPV6
+        xSemaphoreTake(s_semph_get_ip6_addrs, portMAX_DELAY);
+#endif
         if (s_retry_num > CONFIG_EXAMPLE_WIFI_CONN_MAX_RETRY) {
             return ESP_FAIL;
         }
@@ -202,6 +218,11 @@ esp_err_t example_wifi_sta_do_disconnect(void)
     if (s_semph_get_ip_addrs) {
         vSemaphoreDelete(s_semph_get_ip_addrs);
     }
+#if CONFIG_EXAMPLE_CONNECT_IPV6
+    if (s_semph_get_ip6_addrs) {
+        vSemaphoreDelete(s_semph_get_ip6_addrs);
+    }
+#endif
     return esp_wifi_disconnect();
 }