diff --git a/examples/common/rmaker_app_network/CHANGELOG.md b/examples/common/rmaker_app_network/CHANGELOG.md index 62398d8..cfe2f11 100644 --- a/examples/common/rmaker_app_network/CHANGELOG.md +++ b/examples/common/rmaker_app_network/CHANGELOG.md @@ -2,6 +2,14 @@ All notable changes to this project will be documented in this file. +## [1.2.0] + +### Fixed +- WiFi event handlers are now registered only after provisioning succeeds or when device is already provisioned, + preventing interference with the provisioning manager's internal WiFi connection handling +- Fixed issue where `network_prov_mgr_reset_wifi_sm_state_on_failure()` could fail due to WiFi being in connecting state +- Removed retry logic when `CONFIG_APP_NETWORK_RESET_PROV_ON_FAILURE` is not set, allowing re-provisioning from phone app after failure + ## [1.1.0] ### Added diff --git a/examples/common/rmaker_app_network/Kconfig.projbuild b/examples/common/rmaker_app_network/Kconfig.projbuild index 1703e47..b39e2d4 100644 --- a/examples/common/rmaker_app_network/Kconfig.projbuild +++ b/examples/common/rmaker_app_network/Kconfig.projbuild @@ -46,7 +46,7 @@ menu "ESP RainMaker App Wi-Fi Provisioning" config APP_NETWORK_PROV_MAX_RETRY_CNT int - default 5 + default 3 prompt "Max retries before reseting provisioning state machine" depends on APP_NETWORK_RESET_PROV_ON_FAILURE help diff --git a/examples/common/rmaker_app_network/app_wifi_internal.c b/examples/common/rmaker_app_network/app_wifi_internal.c index 2a2dd7f..4e8a36b 100644 --- a/examples/common/rmaker_app_network/app_wifi_internal.c +++ b/examples/common/rmaker_app_network/app_wifi_internal.c @@ -27,13 +27,28 @@ #include #include - #define APP_PROV_STOP_ON_CREDS_MISMATCH #ifdef CONFIG_ESP_RMAKER_NETWORK_OVER_WIFI static const char* TAG = "app_wifi"; -/* Event handler for catching system events */ -static void event_handler(void* arg, esp_event_base_t event_base, + +/* WiFi event handler for post-provisioning reconnection management. + * This handler should only be active after provisioning succeeds or when device is already provisioned. + * During provisioning, the network_provisioning component handles WiFi connections internally. + */ +static void wifi_event_handler(void* arg, esp_event_base_t event_base, + int32_t event_id, void* event_data) +{ + if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) { + esp_wifi_connect(); + } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) { + ESP_LOGI(TAG, "Disconnected. Connecting to the AP again..."); + esp_wifi_connect(); + } +} + +/* Event handler for provisioning events */ +static void prov_event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) { if (event_base == NETWORK_PROV_EVENT) { @@ -70,15 +85,14 @@ static void event_handler(void* arg, esp_event_base_t event_base, } case NETWORK_PROV_WIFI_CRED_SUCCESS: ESP_LOGI(TAG, "Provisioning successful"); + /* After provisioning succeeds, register WiFi event handlers for reconnection management. + * The provisioning manager will deinit, so we need to handle WiFi reconnections ourselves. + */ + ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler, NULL)); break; default: break; } - } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) { - esp_wifi_connect(); - } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) { - ESP_LOGI(TAG, "Disconnected. Connecting to the AP again..."); - esp_wifi_connect(); } } @@ -94,9 +108,8 @@ esp_err_t app_wifi_internal_init(void) #ifdef CONFIG_ESP_RMAKER_NETWORK_OVER_WIFI /* Initialize TCP/IP */ esp_netif_init(); - /* Register our event handler for Wi-Fi, IP and Provisioning related events */ - ESP_ERROR_CHECK(esp_event_handler_register(NETWORK_PROV_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL)); - ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL)); + /* Register event handler for Provisioning related events */ + ESP_ERROR_CHECK(esp_event_handler_register(NETWORK_PROV_EVENT, ESP_EVENT_ANY_ID, &prov_event_handler, NULL)); /* Initialize Wi-Fi including netif with default config */ esp_netif_create_default_wifi_sta(); @@ -200,6 +213,11 @@ esp_err_t app_wifi_internal_start(const char *pop, const char *service_name, * so let's release it's resources */ network_prov_mgr_deinit(); + /* Register WiFi event handlers for reconnection management. + * Since device is already provisioned, we need to handle WiFi reconnections ourselves. + */ + ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler, NULL)); + /* Start Wi-Fi station */ wifi_init_sta(); } diff --git a/examples/common/rmaker_app_network/idf_component.yml b/examples/common/rmaker_app_network/idf_component.yml index 90e8856..058fa69 100644 --- a/examples/common/rmaker_app_network/idf_component.yml +++ b/examples/common/rmaker_app_network/idf_component.yml @@ -1,5 +1,5 @@ ## IDF Component Manager Manifest File -version: "1.1.0" +version: "1.2.0" description: Network connectivity helper component for ESP RainMaker applications with WiFi and Thread support url: https://github.com/espressif/esp-rainmaker/tree/master/examples/common/rmaker_app_network repository: https://github.com/espressif/esp-rainmaker.git @@ -12,4 +12,4 @@ dependencies: espressif/qrcode: version: "*" espressif/network_provisioning: - version: "~1.2.0" + version: "~1.2.1"