diff --git a/components/esp_wifi/include/esp_private/wifi.h b/components/esp_wifi/include/esp_private/wifi.h index 59ea571db8..d9f06401bd 100644 --- a/components/esp_wifi/include/esp_private/wifi.h +++ b/components/esp_wifi/include/esp_private/wifi.h @@ -750,6 +750,33 @@ esp_err_t esp_nan_internal_datapath_resp(wifi_nan_datapath_resp_t *resp); */ esp_err_t esp_nan_internal_datapath_end(wifi_nan_datapath_end_req_t *req); +/** + * @brief Connect WiFi station to the AP. + * + * @attention 1. This API only impact WIFI_MODE_STA or WIFI_MODE_APSTA mode + * + * @return + * - ESP_OK: succeed + * - ESP_ERR_WIFI_NOT_INIT: WiFi is not initialized by esp_wifi_init + * - ESP_ERR_WIFI_NOT_STARTED: WiFi is not started by esp_wifi_start + * - ESP_ERR_WIFI_MODE: WiFi mode error + * - 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 + */ +esp_err_t esp_wifi_connect_internal(void); + +/** + * @brief Disconnect WiFi station from the AP. + * + * @return + * - ESP_OK: succeed + * - ESP_ERR_WIFI_NOT_INIT: WiFi was not initialized by esp_wifi_init + * - ESP_ERR_WIFI_NOT_STARTED: WiFi was not started by esp_wifi_start + * - ESP_FAIL: other WiFi internal errors + * + */ +esp_err_t esp_wifi_disconnect_internal(void); + #ifdef __cplusplus } #endif diff --git a/components/esp_wifi/lib b/components/esp_wifi/lib index e809681ae3..f78422a3cb 160000 --- a/components/esp_wifi/lib +++ b/components/esp_wifi/lib @@ -1 +1 @@ -Subproject commit e809681ae309506087ca75d09028c1df4e42d9ba +Subproject commit f78422a3cb497b8f73a3d35956cf31d677a4e08b diff --git a/components/esp_wifi/src/wifi_init.c b/components/esp_wifi/src/wifi_init.c index c98461c075..039433d0b6 100644 --- a/components/esp_wifi/src/wifi_init.c +++ b/components/esp_wifi/src/wifi_init.c @@ -38,6 +38,10 @@ #include "esp_private/sleep_retention.h" #endif +#if CONFIG_ESP_WIFI_ENABLE_ROAMING_APP +#include "esp_roaming.h" +#endif + static bool s_wifi_inited = false; #if (CONFIG_ESP_WIFI_RX_BA_WIN > CONFIG_ESP_WIFI_DYNAMIC_RX_BUFFER_NUM) @@ -176,6 +180,11 @@ static esp_err_t wifi_deinit_internal(void) #endif esp_supplicant_deinit(); + +#if CONFIG_ESP_WIFI_ENABLE_ROAMING_APP + deinit_roaming_app(); +#endif + err = esp_wifi_deinit_internal(); if (err != ESP_OK) { ESP_LOGE(TAG, "Failed to deinit Wi-Fi driver (0x%x)", err); @@ -435,6 +444,11 @@ esp_err_t esp_wifi_init(const wifi_init_config_t *config) ESP_LOGE(TAG, "Failed to init supplicant (0x%x)", result); goto _deinit; } + +#if CONFIG_ESP_WIFI_ENABLE_ROAMING_APP + init_roaming_app(); +#endif + } else { goto _deinit; } @@ -464,6 +478,28 @@ _deinit: return result; } +esp_err_t esp_wifi_connect(void) +{ + esp_err_t ret = ESP_OK; + ret = esp_wifi_connect_internal(); + +#if CONFIG_ESP_WIFI_ENABLE_ROAMING_APP + roaming_app_enable_reconnect(); +#endif + return ret; +} + +esp_err_t esp_wifi_disconnect(void) +{ + esp_err_t ret = ESP_OK; + ret = esp_wifi_disconnect_internal(); + +#if CONFIG_ESP_WIFI_ENABLE_ROAMING_APP + roaming_app_disable_reconnect(); +#endif + return ret; +} + #ifdef CONFIG_PM_ENABLE void wifi_apb80m_request(void) { diff --git a/components/esp_wifi/wifi_apps/roaming_app/include/esp_roaming.h b/components/esp_wifi/wifi_apps/roaming_app/include/esp_roaming.h index 7f5e581e08..8fa7402e6a 100644 --- a/components/esp_wifi/wifi_apps/roaming_app/include/esp_roaming.h +++ b/components/esp_wifi/wifi_apps/roaming_app/include/esp_roaming.h @@ -112,6 +112,7 @@ struct roaming_app { #if PERIODIC_SCAN_MONITORING bool periodic_scan_active; #endif + bool allow_reconnect; }; void init_roaming_app(void); @@ -126,6 +127,9 @@ void roaming_app_periodic_scan_internal_handler(void *data, void *ctx); #endif /*PERIODIC_SCAN_ROAM_MONITORING*/ void roaming_app_trigger_roam_internal_handler(void *data, void *ctx); + +void roaming_app_disable_reconnect(void); +void roaming_app_enable_reconnect(void); #ifdef __cplusplus } #endif diff --git a/components/esp_wifi/wifi_apps/roaming_app/src/roaming_app.c b/components/esp_wifi/wifi_apps/roaming_app/src/roaming_app.c index 5f33b2c89b..f1d2e4ea3d 100644 --- a/components/esp_wifi/wifi_apps/roaming_app/src/roaming_app.c +++ b/components/esp_wifi/wifi_apps/roaming_app/src/roaming_app.c @@ -50,6 +50,19 @@ static inline long time_diff_sec(struct timeval *a, struct timeval *b) { return (a->tv_sec - b->tv_sec); } + +void roaming_app_disable_reconnect(void) +{ + ESP_LOGD(ROAMING_TAG, "Switching off reconnect due to application trigerred disconnect"); + g_roaming_app.allow_reconnect = false; +} + +void roaming_app_enable_reconnect(void) +{ + ESP_LOGD(ROAMING_TAG, "Switching on reconnect due to application trigerred reconnect"); + g_roaming_app.allow_reconnect = true; +} + static void roaming_app_get_ap_info(wifi_ap_record_t *ap_info) { esp_wifi_sta_get_ap_info(ap_info); @@ -145,6 +158,8 @@ static void roaming_app_disconnected_event_handler(void* arg, esp_event_base_t e ESP_LOGD(ROAMING_TAG, "station got disconnected reason=%d", disconn->reason); if (disconn->reason == WIFI_REASON_ROAMING) { ESP_LOGD(ROAMING_TAG, "station roaming, do nothing"); + } else if (g_roaming_app.allow_reconnect == false) { + ESP_LOGD(ROAMING_TAG, "station initiated disconnect, do nothing"); } else { #if LEGACY_ROAM_ENABLED /* @@ -198,6 +213,7 @@ static void roaming_app_connected_event_handler(void* arg, esp_event_base_t even #if LEGACY_ROAM_ENABLED g_roaming_app.force_roam_ongoing = true; #endif /*LEGACY_ROAM_ENABLED*/ + g_roaming_app.allow_reconnect = true; } #define MAX_NEIGHBOR_LEN 512 #if PERIODIC_RRM_MONITORING @@ -475,8 +491,6 @@ static void periodic_rrm_request(struct timeval *now) g_roaming_app.rrm_request_active = true; } } -#else -static void periodic_rrm_request(struct timeval *now) { } #endif static bool candidate_security_match(wifi_ap_record_t candidate) @@ -763,7 +777,7 @@ esp_err_t init_scan_params(void) void init_roaming_app(void) { #if !LOW_RSSI_ROAMING_ENABLED && !PERIODIC_SCAN_MONITORING - ESP_LOGE(ROAMING_TAG, "No roaming method enabled. Roaming app cannot be initialized"); + ESP_LOGE(ROAMING_TAG, "No roaming trigger enabled. Roaming app cannot be initialized"); return; #endif diff --git a/components/wpa_supplicant/esp_supplicant/src/esp_common.c b/components/wpa_supplicant/esp_supplicant/src/esp_common.c index 62c257cbbd..993cf24857 100644 --- a/components/wpa_supplicant/esp_supplicant/src/esp_common.c +++ b/components/wpa_supplicant/esp_supplicant/src/esp_common.c @@ -427,9 +427,6 @@ void esp_supplicant_common_deinit(void) } s_supplicant_task_init_done = false; #endif /* CONFIG_SUPPLICANT_TASK */ -#if CONFIG_ESP_WIFI_ENABLE_ROAMING_APP - deinit_roaming_app(); -#endif #endif /* defined(CONFIG_IEEE80211KV) || defined(CONFIG_IEEE80211R) */ } diff --git a/components/wpa_supplicant/esp_supplicant/src/esp_wpa_main.c b/components/wpa_supplicant/esp_supplicant/src/esp_wpa_main.c index 58d562cdb1..067e09705c 100644 --- a/components/wpa_supplicant/esp_supplicant/src/esp_wpa_main.c +++ b/components/wpa_supplicant/esp_supplicant/src/esp_wpa_main.c @@ -481,6 +481,7 @@ int esp_supplicant_init(void) #ifdef CONFIG_OWE_STA esp_wifi_register_owe_cb(wpa_cb); #endif /* CONFIG_OWE_STA */ + eloop_init(); ret = esp_supplicant_common_init(wpa_cb); @@ -494,10 +495,6 @@ int esp_supplicant_init(void) ret = esp_wifi_internal_wapi_init(); #endif -#if CONFIG_ESP_WIFI_ENABLE_ROAMING_APP - init_roaming_app(); -#endif - return ret; } diff --git a/examples/wifi/roaming/roaming_app/main/roaming_app_example.c b/examples/wifi/roaming/roaming_app/main/roaming_app_example.c index c9c8de2c41..2d4c02b065 100644 --- a/examples/wifi/roaming/roaming_app/main/roaming_app_example.c +++ b/examples/wifi/roaming/roaming_app/main/roaming_app_example.c @@ -43,8 +43,6 @@ static EventGroupHandle_t s_wifi_event_group; #define MAXIMUM_RETRY 5 const char *TAG = "wifi roaming app"; -static int s_retry_num = 0; - static void event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) { @@ -55,19 +53,12 @@ static void event_handler(void* arg, esp_event_base_t event_base, if (disconn->reason == WIFI_REASON_ROAMING) { ESP_LOGI(TAG, "station disconnected during roaming"); } else { - if (s_retry_num < MAXIMUM_RETRY) { - ESP_LOGI(TAG, "station disconnected with reason %d", disconn->reason); - esp_wifi_connect(); - s_retry_num++; - ESP_LOGI(TAG, "retry to connect to the AP"); - } else { - xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT); - } + ESP_LOGI(TAG, "station disconnected with reason %d", disconn->reason); + xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT); } } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) { ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data; ESP_LOGI(TAG, "got ip:" IPSTR, IP2STR(&event->ip_info.ip)); - s_retry_num = 0; xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT); } }