From f0563b384478d9abe4956084ee4b93df2339e05b Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Mon, 7 Oct 2019 16:36:18 +0200 Subject: [PATCH 1/3] system_api: call shutdown handlers in reverse order Similar to how destructors should be called in reverse order to the constructors. --- components/esp32/system_api.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/components/esp32/system_api.c b/components/esp32/system_api.c index c3d77c01d..4e28dc31e 100644 --- a/components/esp32/system_api.c +++ b/components/esp32/system_api.c @@ -230,12 +230,11 @@ void esp_restart_noos(void) __attribute__ ((noreturn)); void IRAM_ATTR esp_restart(void) { - int i; - for (i = 0; i < SHUTDOWN_HANDLERS_NO; i++) { - if (shutdown_handlers[i]) { - shutdown_handlers[i](); - } - } + for (int i = SHUTDOWN_HANDLERS_NO - 1; i >= 0; i--) { + if (shutdown_handlers[i]) { + shutdown_handlers[i](); + } + } // Disable scheduler on this core. vTaskSuspendAll(); From 28a440521e9f6c6f6c5cec5f583fb32bdc41fd97 Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Mon, 7 Oct 2019 16:46:25 +0200 Subject: [PATCH 2/3] examples: gracefully shut down Wi-Fi before restart 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. --- .../protocol_examples_common/connect.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/examples/common_components/protocol_examples_common/connect.c b/examples/common_components/protocol_examples_common/connect.c index 38d68a3a5..36e5326eb 100644 --- a/examples/common_components/protocol_examples_common/connect.c +++ b/examples/common_components/protocol_examples_common/connect.c @@ -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 From 5768102d4e75c9bc9c51b8a14ebbadb524e16d27 Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Mon, 7 Oct 2019 16:48:32 +0200 Subject: [PATCH 3/3] esp_wifi: fix typo in comments (ESP_ERR_WIFI_NOT_STARTED) --- components/esp_wifi/include/esp_wifi.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/esp_wifi/include/esp_wifi.h b/components/esp_wifi/include/esp_wifi.h index 82fa00bfc..969aa62d9 100644 --- a/components/esp_wifi/include/esp_wifi.h +++ b/components/esp_wifi/include/esp_wifi.h @@ -330,7 +330,7 @@ esp_err_t esp_wifi_restore(void); * @return * - ESP_OK: succeed * - ESP_ERR_WIFI_NOT_INIT: WiFi is not initialized by esp_wifi_init - * - ESP_ERR_WIFI_NOT_START: WiFi is not started by esp_wifi_start + * - ESP_ERR_WIFI_NOT_STARTED: WiFi is not started by esp_wifi_start * - ESP_ERR_WIFI_CONN: WiFi internal error, station or soft-AP control block wrong * - ESP_ERR_WIFI_SSID: SSID of AP which station connects is invalid */