From 20fcc23f88bf8d0ebc31e28d24f7c575d4fa95b3 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Wed, 21 Jun 2023 12:26:29 +0200 Subject: [PATCH] fix(wifi): Fix default wifi-netif creation to assert if no event loop - Added ESP_ERROR_CHECK() checks to `esp_wifi_set_default_wifi_..._handlers()` calls - Added ESP_ERROR_CHECH() to `esp_netif_attach_wifi_...()` calls - Updated documentation to reflect the changes Closes https://github.com/espressif/esp-idf/issues/11580 --- .../test_app_esp_netif/main/esp_netif_test.c | 2 ++ components/esp_wifi/include/esp_wifi_default.h | 13 +++++++++---- components/esp_wifi/src/wifi_default.c | 8 ++++---- 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/components/esp_netif/test_apps/test_app_esp_netif/main/esp_netif_test.c b/components/esp_netif/test_apps/test_app_esp_netif/main/esp_netif_test.c index 57ce25b06d..18b4ee92e5 100644 --- a/components/esp_netif/test_apps/test_app_esp_netif/main/esp_netif_test.c +++ b/components/esp_netif/test_apps/test_app_esp_netif/main/esp_netif_test.c @@ -325,6 +325,7 @@ TEST(esp_netif, create_destroy_default_wifi) // Helper constants to refer default STA and AP's params static const esp_netif_inherent_config_t default_sta_cfg = ESP_NETIF_INHERENT_DEFAULT_WIFI_STA(); static const esp_netif_inherent_config_t default_ap_cfg = ESP_NETIF_INHERENT_DEFAULT_WIFI_AP(); + TEST_ESP_OK(esp_event_loop_create_default()); // create default station esp_netif_t *sta = esp_netif_create_default_wifi_sta(); @@ -352,6 +353,7 @@ TEST(esp_netif, create_destroy_default_wifi) sta = esp_netif_create_default_wifi_sta(); TEST_ASSERT_NOT_NULL(sta); esp_netif_destroy_default_wifi(sta); + TEST_ESP_OK(esp_event_loop_delete_default()); } TEST(esp_netif, get_set_hostname) diff --git a/components/esp_wifi/include/esp_wifi_default.h b/components/esp_wifi/include/esp_wifi_default.h index b1e1c24d91..0bb6450ab7 100644 --- a/components/esp_wifi/include/esp_wifi_default.h +++ b/components/esp_wifi/include/esp_wifi_default.h @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2019-2022 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2019-2023 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -73,7 +73,9 @@ esp_err_t esp_wifi_clear_default_wifi_driver_and_handlers(void *esp_netif); * @brief Creates default WIFI AP. In case of any init error this API aborts. * * @note The API creates esp_netif object with default WiFi access point config, - * attaches the netif to wifi and registers default wifi handlers. + * attaches the netif to wifi and registers wifi handlers to the default event loop. + * This API uses assert() to check for potential errors, so it could abort the program. + * (Note that the default event loop needs to be created prior to calling this API) * * @return pointer to esp-netif instance */ @@ -83,7 +85,9 @@ esp_netif_t* esp_netif_create_default_wifi_ap(void); * @brief Creates default WIFI STA. In case of any init error this API aborts. * * @note The API creates esp_netif object with default WiFi station config, - * attaches the netif to wifi and registers default wifi handlers. + * attaches the netif to wifi and registers wifi handlers to the default event loop. + * This API uses assert() to check for potential errors, so it could abort the program. + * (Note that the default event loop needs to be created prior to calling this API) * * @return pointer to esp-netif instance */ @@ -93,7 +97,8 @@ esp_netif_t* esp_netif_create_default_wifi_sta(void); * @brief Creates default WIFI NAN. In case of any init error this API aborts. * * @note The API creates esp_netif object with default WiFi station config, - * attaches the netif to wifi and registers default wifi handlers. + * attaches the netif to wifi and registers wifi handlers to the default event loop. + * (Note that the default event loop needs to be created prior to calling this API) * * @return pointer to esp-netif instance */ diff --git a/components/esp_wifi/src/wifi_default.c b/components/esp_wifi/src/wifi_default.c index 2279eb04c9..9b8d06bd9d 100644 --- a/components/esp_wifi/src/wifi_default.c +++ b/components/esp_wifi/src/wifi_default.c @@ -373,8 +373,8 @@ esp_netif_t* esp_netif_create_default_wifi_ap(void) esp_netif_config_t cfg = ESP_NETIF_DEFAULT_WIFI_AP(); esp_netif_t *netif = esp_netif_new(&cfg); assert(netif); - esp_netif_attach_wifi_ap(netif); - esp_wifi_set_default_wifi_ap_handlers(); + ESP_ERROR_CHECK(esp_netif_attach_wifi_ap(netif)); + ESP_ERROR_CHECK(esp_wifi_set_default_wifi_ap_handlers()); return netif; } #endif @@ -387,8 +387,8 @@ esp_netif_t* esp_netif_create_default_wifi_sta(void) esp_netif_config_t cfg = ESP_NETIF_DEFAULT_WIFI_STA(); esp_netif_t *netif = esp_netif_new(&cfg); assert(netif); - esp_netif_attach_wifi_station(netif); - esp_wifi_set_default_wifi_sta_handlers(); + ESP_ERROR_CHECK(esp_netif_attach_wifi_station(netif)); + ESP_ERROR_CHECK(esp_wifi_set_default_wifi_sta_handlers()); return netif; }