]> granicus.if.org Git - esp-idf/commitdiff
examples: gracefully shut down Wi-Fi before restart
authorIvan Grokhotkov <ivan@espressif.com>
Mon, 7 Oct 2019 14:46:25 +0000 (16:46 +0200)
committerIvan Grokhotkov <ivan@espressif.com>
Mon, 7 Oct 2019 14:47:49 +0000 (16:47 +0200)
This fixes the issue that if Wi-Fi is stopped from a shutdown handler,
the code in connect.c tries to reconnect, and fails because Wi-Fi is
already stopped.
Also make the error check in connect.c less strict.

examples/common_components/protocol_examples_common/connect.c

index 38d68a3a529e53cedecd1f54a5bc18575f5e00f6..36e5326eb573b7d6bb809ea2e6144bc888b20793 100644 (file)
@@ -73,6 +73,7 @@ esp_err_t example_connect(void)
     }
     s_connect_event_group = xEventGroupCreate();
     start();
+    ESP_ERROR_CHECK(esp_register_shutdown_handler(&stop));
     xEventGroupWaitBits(s_connect_event_group, CONNECTED_BITS, true, true, portMAX_DELAY);
     ESP_LOGI(TAG, "Connected to %s", s_connection_name);
     ESP_LOGI(TAG, "IPv4 address: " IPSTR, IP2STR(&s_ip_addr));
@@ -101,7 +102,11 @@ static void on_wifi_disconnect(void *arg, esp_event_base_t event_base,
                                int32_t event_id, void *event_data)
 {
     ESP_LOGI(TAG, "Wi-Fi disconnected, trying to reconnect...");
-    ESP_ERROR_CHECK(esp_wifi_connect());
+    esp_err_t err = esp_wifi_connect();
+    if (err == ESP_ERR_WIFI_NOT_STARTED) {
+        return;
+    }
+    ESP_ERROR_CHECK(err);
 }
 
 #ifdef CONFIG_EXAMPLE_CONNECT_IPV6
@@ -149,7 +154,11 @@ static void stop(void)
     ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_GOT_IP6, &on_got_ipv6));
     ESP_ERROR_CHECK(esp_event_handler_unregister(WIFI_EVENT, WIFI_EVENT_STA_CONNECTED, &on_wifi_connect));
 #endif
-    ESP_ERROR_CHECK(esp_wifi_stop());
+    esp_err_t err = esp_wifi_stop();
+    if (err == ESP_ERR_WIFI_NOT_INIT) {
+        return;
+    }
+    ESP_ERROR_CHECK(err);
     ESP_ERROR_CHECK(esp_wifi_deinit());
 }
 #endif // CONFIG_EXAMPLE_CONNECT_WIFI