From 516551adc4c6c8b88d634e6cf38c6ce6019841ea Mon Sep 17 00:00:00 2001 From: Kapil Gupta Date: Tue, 29 Oct 2024 23:53:55 +0530 Subject: [PATCH 1/9] fix(wifi): Fix some issues observed in roaming app --- .../roaming_app/include/esp_roaming.h | 32 +++- .../wifi_apps/roaming_app/src/roaming_app.c | 141 +++++++++++++----- components/wpa_supplicant/port/eloop.c | 1 + 3 files changed, 134 insertions(+), 40 deletions(-) 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 8fa7402e6a..64b4575937 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 @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -46,12 +46,24 @@ extern "C" { #define MAX_CANDIDATE_COUNT CONFIG_ESP_WIFI_ROAMING_MAX_CANDIDATES /* Legacy roaming configuration */ +#ifdef CONFIG_ESP_WIFI_ROAMING_LEGACY_ROAMING #define LEGACY_ROAM_ENABLED CONFIG_ESP_WIFI_ROAMING_LEGACY_ROAMING +#else +#define LEGACY_ROAM_ENABLED 0 +#endif +#ifdef CONFIG_ESP_WIFI_NETWORK_ASSISTED_ROAMING_RETRY_COUNT #define BSS_TM_RETRY_COUNT CONFIG_ESP_WIFI_NETWORK_ASSISTED_ROAMING_RETRY_COUNT +#else +#define BSS_TM_RETRY_COUNT 0 +#endif /* Network Assisted Roaming */ +#ifdef CONFIG_ESP_WIFI_ROAMING_NETWORK_ASSISTED_ROAM #define NETWORK_ASSISTED_ROAMING_ENABLED CONFIG_ESP_WIFI_ROAMING_NETWORK_ASSISTED_ROAM +#else +#define NETWORK_ASSISTED_ROAMING_ENABLED 0 +#endif /* Periodic RRM configuration */ #define PERIODIC_RRM_MONITORING CONFIG_ESP_WIFI_ROAMING_PERIODIC_RRM_MONITORING @@ -85,8 +97,26 @@ struct cand_bss { uint8_t bssid[ETH_ALEN]; }; +struct roam_config { + uint8_t backoff_time; + bool low_rssi_roam_trigger; + uint8_t low_rssi_threshold; + uint8_t rssi_threshold_reduction_offset; + bool scan_monitor; + uint8_t scan_interval; + uint8_t scan_rssi_threshold; + uint8_t scan_rssi_diff; + bool legacy_roam_enabled; + uint8_t btm_retry_cnt; + bool btm_roaming_enabled; + bool rrm_monitor; + uint8_t rrm_monitor_time; + uint8_t rrm_monitor_rssi_threshold; +}; + struct roaming_app { wifi_scan_config_t scan_params; + struct roam_config config; bool scan_ongoing; int8_t current_rssi_threshold; char *btm_neighbor_list; 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 e7ca6cbc53..8c00e6c6cc 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 @@ -71,10 +71,10 @@ static void roaming_app_get_ap_info(wifi_ap_record_t *ap_info) * If the current rssi is below the configured rssi threshold for * low rssi based roaming and the current rssi threshold is below that, * we should reset the rssi threshold back to the configured rssi threshold */ - if ((ap_info->rssi > ROAMING_LOW_RSSI_THRESHOLD) && (g_roaming_app.current_low_rssi_threshold < ROAMING_LOW_RSSI_THRESHOLD)) { - g_roaming_app.current_low_rssi_threshold = ROAMING_LOW_RSSI_THRESHOLD; - esp_wifi_set_rssi_threshold(ROAMING_LOW_RSSI_THRESHOLD); - ESP_LOGD(ROAMING_TAG, "Reset the low rssi threshold back to %d", ROAMING_LOW_RSSI_THRESHOLD); + if ((ap_info->rssi > g_roaming_app.config.low_rssi_threshold) && (g_roaming_app.current_low_rssi_threshold < g_roaming_app.config.low_rssi_threshold)) { + g_roaming_app.current_low_rssi_threshold = g_roaming_app.config.low_rssi_threshold; + esp_wifi_set_rssi_threshold(g_roaming_app.config.low_rssi_threshold); + ESP_LOGD(ROAMING_TAG, "Reset the low rssi threshold back to %d", g_roaming_app.config.low_rssi_threshold); } #endif /*LOW_RSSI_ROAMING_ENABLED*/ } @@ -118,6 +118,10 @@ static int8_t initialize_roaming_event(void) #if PERIODIC_RRM_MONITORING static void init_periodic_rrm_event(void) { + if (!g_roaming_app.config.rrm_monitor) { + ESP_LOGI(ROAMING_TAG, "RRM monitor is disabled in config"); + return; + } if (!neighbor_list_lock) { neighbor_list_lock = os_recursive_mutex_create(); if (!neighbor_list_lock) { @@ -126,7 +130,7 @@ static void init_periodic_rrm_event(void) } ESP_LOGV(ROAMING_TAG, "Initialised Periodic RRM Monitoring event!"); g_roaming_app.periodic_rrm_active = true; - if (eloop_register_timeout(RRM_MONITOR_TIME, 0, roaming_app_periodic_rrm_internal_handler, NULL, NULL)) { + if (eloop_register_timeout(g_roaming_app.config.rrm_monitor_time, 0, roaming_app_periodic_rrm_internal_handler, NULL, NULL)) { ESP_LOGE(ROAMING_TAG, "Could not register periodic neighbor report event."); } } @@ -135,9 +139,13 @@ static void init_periodic_rrm_event(void) #if PERIODIC_SCAN_MONITORING static void init_periodic_scan_roam_event(void) { + if (!g_roaming_app.config.scan_monitor) { + ESP_LOGI(ROAMING_TAG, "%s: Scan monitor is disabled in config", __func__); + return; + } ESP_LOGV(ROAMING_TAG, "Initialised Periodic Scan Roam event!"); g_roaming_app.periodic_scan_active = true; - if (eloop_register_timeout(SCAN_MONITOR_INTERVAL, 0, roaming_app_periodic_scan_internal_handler, NULL, NULL)) { + if (eloop_register_timeout(g_roaming_app.config.scan_interval, 0, roaming_app_periodic_scan_internal_handler, NULL, NULL)) { ESP_LOGE(ROAMING_TAG, "Could not register periodic scan monitoring event"); } } @@ -185,15 +193,15 @@ static void roaming_app_sta_stop_event_handler(void* arg, esp_event_base_t event static void roaming_app_connected_event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) { -#if LOW_RSSI_ROAMING_ENABLED roaming_app_get_ap_info(&g_roaming_app.ap_info); g_roaming_app.scan_params.ssid = g_roaming_app.ap_info.ssid; - if (g_roaming_app.ap_info.rssi < ROAMING_LOW_RSSI_THRESHOLD) { +#if LOW_RSSI_ROAMING_ENABLED + if (g_roaming_app.ap_info.rssi < g_roaming_app.config.low_rssi_threshold) { /* To ensure that the threshold is set to one offset below the current AP RSSI * in case, the AP is already below the RSSI threshold */ - g_roaming_app.current_low_rssi_threshold = g_roaming_app.ap_info.rssi - RSSI_THRESHOLD_REDUCTION_OFFSET; + g_roaming_app.current_low_rssi_threshold = g_roaming_app.ap_info.rssi - g_roaming_app.config.rssi_threshold_reduction_offset; } else { - g_roaming_app.current_low_rssi_threshold = ROAMING_LOW_RSSI_THRESHOLD; + g_roaming_app.current_low_rssi_threshold = g_roaming_app.config.low_rssi_threshold; } ESP_LOGD(ROAMING_TAG, "setting rssi threshold as %d", g_roaming_app.current_low_rssi_threshold); esp_wifi_set_rssi_threshold(g_roaming_app.current_low_rssi_threshold); @@ -218,7 +226,7 @@ static void roaming_app_connected_event_handler(void* arg, esp_event_base_t even ESP_LOGE(ROAMING_TAG, "Failed to Initialise roaming events"); } #if LEGACY_ROAM_ENABLED - g_roaming_app.force_roam_ongoing = true; + g_roaming_app.force_roam_ongoing = false; #endif /*LEGACY_ROAM_ENABLED*/ g_roaming_app.allow_reconnect = true; } @@ -359,6 +367,7 @@ static void roaming_app_neighbor_report_recv_handler(void* arg, esp_event_base_t } #endif /*PERIODIC_RRM_MONITORING*/ + #if LOW_RSSI_ROAMING_ENABLED static void roaming_app_rssi_low_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) { @@ -367,7 +376,7 @@ static void roaming_app_rssi_low_handler(void* arg, esp_event_base_t event_base, roaming_app_get_ap_info(&g_roaming_app.ap_info); determine_best_ap(0); - g_roaming_app.current_low_rssi_threshold -= RSSI_THRESHOLD_REDUCTION_OFFSET; + g_roaming_app.current_low_rssi_threshold -= g_roaming_app.config.rssi_threshold_reduction_offset; ESP_LOGD(ROAMING_TAG, "Resetting RSSI Threshold to %d", g_roaming_app.current_low_rssi_threshold); esp_wifi_set_rssi_threshold(g_roaming_app.current_low_rssi_threshold); @@ -388,8 +397,9 @@ static void trigger_network_assisted_roam(void) #endif /*PERIODIC_RRM_MONITORING*/ ESP_LOGD(ROAMING_TAG, "Sent BTM Query"); gettimeofday(&g_roaming_app.last_roamed_time, NULL); +#if LEGACY_ROAM_ENABLED g_roaming_app.btm_attempt++; - +#endif } #endif /*NETWORK_ASSISTED_ROAMING*/ @@ -415,14 +425,14 @@ void roaming_app_trigger_roam(struct cand_bss *bss) struct timeval now; gettimeofday(&now, NULL); ESP_LOGD(ROAMING_TAG,"Processing trigger roaming request."); - if (time_diff_sec(&now, &g_roaming_app.last_roamed_time) < ROAMING_BACKOFF_TIME ) { + if (time_diff_sec(&now, &g_roaming_app.last_roamed_time) < g_roaming_app.config.backoff_time ) { ESP_LOGD(ROAMING_TAG,"Ignoring request as time difference to last request is %ld",time_diff_sec(&now, &g_roaming_app.last_roamed_time)); goto free_bss; } #if NETWORK_ASSISTED_ROAMING_ENABLED - if (g_roaming_app.btm_support) { + if (g_roaming_app.config.btm_roaming_enabled && g_roaming_app.btm_support) { #if LEGACY_ROAM_ENABLED && NETWORK_ASSISTED_ROAMING_ENABLED - if (g_roaming_app.btm_attempt <= BSS_TM_RETRY_COUNT) { + if (g_roaming_app.btm_attempt <= g_roaming_app.config.btm_retry_cnt) { #endif trigger_network_assisted_roam(); goto free_bss; @@ -435,7 +445,9 @@ void roaming_app_trigger_roam(struct cand_bss *bss) } #endif /*NETWORK_ASSISTED_ROAMING_ENABLED*/ #if LEGACY_ROAM_ENABLED - trigger_legacy_roam(bss); + if (g_roaming_app.config.legacy_roam_enabled) { + trigger_legacy_roam(bss); + } #endif /*LEGACY_ROAM_ENABLED*/ free_bss : os_free(bss); @@ -490,7 +502,7 @@ void print_ap_records(struct scanned_ap_info *ap_info) static void periodic_rrm_request(struct timeval *now) { roaming_app_get_ap_info(&g_roaming_app.ap_info); - if (esp_rrm_is_rrm_supported_connection() && (g_roaming_app.ap_info.rssi < RRM_MONITOR_RSSI_THRESHOLD)) { + if (esp_rrm_is_rrm_supported_connection() && (g_roaming_app.ap_info.rssi < g_roaming_app.config.rrm_monitor_rssi_threshold)) { if (esp_rrm_send_neighbor_report_request() < 0) { ESP_LOGE(ROAMING_TAG, "failed to send neighbor report request"); return; @@ -643,18 +655,19 @@ static void periodic_scan_roam(struct timeval *now) * as the results produced by a scan at this time would not be used by * supplicant to build candidate lists. * */ - if (time_diff_sec(now, &g_roaming_app.last_roamed_time) < ROAMING_BACKOFF_TIME - SUPPLICANT_CANDIDATE_LIST_EXPIRY) { + if (time_diff_sec(now, &g_roaming_app.last_roamed_time) < g_roaming_app.config.backoff_time - SUPPLICANT_CANDIDATE_LIST_EXPIRY) { return; } #endif /*NETWORK_ASSISTED_ROAMING_ENABLED && !LEGACY_ROAM_ENABLED*/ /* If the current RSSI is not worse than the configured threshold * for station initiated roam, then do not trigger roam */ roaming_app_get_ap_info(&g_roaming_app.ap_info); - if (g_roaming_app.ap_info.rssi > SCAN_MONITOR_RSSI_THRESHOLD) { + ESP_LOGD(ROAMING_TAG, "Connected AP's RSSI=%d", g_roaming_app.ap_info.rssi); + if (g_roaming_app.ap_info.rssi > g_roaming_app.config.scan_rssi_threshold) { return; } - determine_best_ap(SCAN_ROAM_RSSI_DIFF - 1); + determine_best_ap(g_roaming_app.config.scan_rssi_diff - 1); } #endif /*PERIODIC_SCAN_MONITORING*/ @@ -662,13 +675,19 @@ static void periodic_scan_roam(struct timeval *now) void roaming_app_periodic_rrm_internal_handler(void *data, void *ctx) { struct timeval now; + + if (!g_roaming_app.config.rrm_monitor) { + ESP_LOGI(ROAMING_TAG, "%s:RRM monitor is disabled in config", __func__); + return; + } + if (g_roaming_app.periodic_rrm_active) { ESP_LOGD(ROAMING_TAG,"Triggered periodic rrm event!"); gettimeofday(&now, NULL); - /* This will be done every RRM_MONITOR_TIME */ + /* This will be done every g_roaming_app.config.rrm_monitor_time */ periodic_rrm_request(&now); - if (eloop_register_timeout(RRM_MONITOR_TIME, 0, roaming_app_periodic_rrm_internal_handler, NULL, NULL)) { + if (eloop_register_timeout(g_roaming_app.config.rrm_monitor_time, 0, roaming_app_periodic_rrm_internal_handler, NULL, NULL)) { ESP_LOGE(ROAMING_TAG,"Could not register periodic neighbor report request event."); } } @@ -679,14 +698,20 @@ void roaming_app_periodic_rrm_internal_handler(void *data, void *ctx) void roaming_app_periodic_scan_internal_handler(void *data, void *ctx) { struct timeval now; + + if (!g_roaming_app.config.scan_monitor) { + ESP_LOGI(ROAMING_TAG, "%s: Scan monitor is disabled in config", __func__); + return; + } + if (g_roaming_app.periodic_scan_active) { ESP_LOGD(ROAMING_TAG,"Started the periodic scan roam event!"); gettimeofday(&now, NULL); - /* This will be done every SCAN_MONITOR_INTERVAL */ + /* This will be done every g_roaming_app.config.scan_interval */ periodic_scan_roam(&now); - if (eloop_register_timeout(SCAN_MONITOR_INTERVAL, 0, roaming_app_periodic_scan_internal_handler, NULL, NULL)) { + if (eloop_register_timeout(g_roaming_app.config.scan_interval, 0, roaming_app_periodic_scan_internal_handler, NULL, NULL)) { ESP_LOGE(ROAMING_TAG,"Could not register periodic scan event."); } } @@ -759,7 +784,51 @@ cleanup: return ret; } -esp_err_t init_scan_params(void) + +static esp_err_t init_config_params(void) +{ + g_roaming_app.config.backoff_time = ROAMING_BACKOFF_TIME; + + g_roaming_app.config.low_rssi_roam_trigger = LOW_RSSI_ROAMING_ENABLED; + g_roaming_app.config.low_rssi_threshold = ROAMING_LOW_RSSI_THRESHOLD; + g_roaming_app.config.rssi_threshold_reduction_offset = RSSI_THRESHOLD_REDUCTION_OFFSET; + + g_roaming_app.config.scan_monitor = PERIODIC_SCAN_MONITORING; + g_roaming_app.config.scan_interval = SCAN_MONITOR_INTERVAL; + g_roaming_app.config.scan_rssi_threshold = SCAN_MONITOR_RSSI_THRESHOLD; + g_roaming_app.config.scan_rssi_diff = SCAN_ROAM_RSSI_DIFF; + + g_roaming_app.config.legacy_roam_enabled = LEGACY_ROAM_ENABLED; + g_roaming_app.config.btm_retry_cnt = BSS_TM_RETRY_COUNT; + g_roaming_app.config.btm_roaming_enabled = NETWORK_ASSISTED_ROAMING_ENABLED; + + g_roaming_app.config.rrm_monitor = PERIODIC_RRM_MONITORING; + g_roaming_app.config.rrm_monitor_time = RRM_MONITOR_TIME; + g_roaming_app.config.rrm_monitor_rssi_threshold = RRM_MONITOR_RSSI_THRESHOLD; + + ESP_LOGD(ROAMING_TAG, "Roaming app config :"); + + ESP_LOGD(ROAMING_TAG, "backoff time=%d low_rssi_roam_trigger=%d low_rssi_threshold=%d rssi_threshold_reduction_offset=%d", + g_roaming_app.config.backoff_time, g_roaming_app.config.low_rssi_roam_trigger, + g_roaming_app.config.low_rssi_threshold, g_roaming_app.config.rssi_threshold_reduction_offset); + + ESP_LOGD(ROAMING_TAG, "scan_monitor=%d scan_interval=%d scan_rssi_threshold=%d scan_rssi_diff=%d", + g_roaming_app.config.scan_monitor, g_roaming_app.config.scan_interval, + g_roaming_app.config.scan_rssi_threshold, g_roaming_app.config.scan_rssi_diff); + + ESP_LOGD(ROAMING_TAG, "legacy_roam_enabled=%d, btm_retry_cnt=%d btm_roaming_enabled=%d", + g_roaming_app.config.legacy_roam_enabled, + g_roaming_app.config.btm_retry_cnt, g_roaming_app.config.btm_roaming_enabled); + + ESP_LOGD(ROAMING_TAG, "rrm_monitor=%d, rrm_monitor_time=%d rrm_monitor_rssi_threshold=%d", + g_roaming_app.config.rrm_monitor, + g_roaming_app.config.rrm_monitor_time, + g_roaming_app.config.rrm_monitor_rssi_threshold); + + return ESP_OK; +} + +static esp_err_t init_scan_params(void) { if (!scan_results_lock) { scan_results_lock = os_recursive_mutex_create(); @@ -798,8 +867,9 @@ void init_roaming_app(void) #if LOW_RSSI_ROAMING_ENABLED ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_BSS_RSSI_LOW, &roaming_app_rssi_low_handler, NULL)); -#endif /*ROAMING_LOW_RSSI_THRESHOLD*/ +#endif /*LOW_RSSI_ROAMING_ENABLED*/ ESP_ERROR_CHECK(init_scan_params()); + ESP_ERROR_CHECK(init_config_params()); #if PERIODIC_RRM_MONITORING ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_NEIGHBOR_REP, &roaming_app_neighbor_report_recv_handler, NULL)); @@ -825,23 +895,16 @@ void deinit_roaming_app(void) #if LOW_RSSI_ROAMING_ENABLED ESP_ERROR_CHECK(esp_event_handler_unregister(WIFI_EVENT, WIFI_EVENT_STA_BSS_RSSI_LOW, &roaming_app_rssi_low_handler)); -#endif /*ROAMING_LOW_RSSI_THRESHOLD*/ - -#if PERIODIC_RRM_MONITORING - ESP_ERROR_CHECK(esp_event_handler_unregister(WIFI_EVENT, WIFI_EVENT_STA_NEIGHBOR_REP, - &roaming_app_neighbor_report_recv_handler)); -#endif /*PERIODIC_RRM_MONITORING*/ - - /* Disabling the periodic scan and RRM events */ -#if PERIODIC_RRM_MONITORING - g_roaming_app.periodic_rrm_active = false; -#endif /*PERIODIC_RRM_MONITORING*/ +#endif /*LOW_RSSI_ROAMING_ENABLED*/ #if PERIODIC_SCAN_MONITORING g_roaming_app.periodic_scan_active = false; #endif /*PERIODIC_SCAN_MONITORING*/ - #if PERIODIC_RRM_MONITORING + ESP_ERROR_CHECK(esp_event_handler_unregister(WIFI_EVENT, WIFI_EVENT_STA_NEIGHBOR_REP, + &roaming_app_neighbor_report_recv_handler)); + /* Disabling the periodic scan and RRM events */ + g_roaming_app.periodic_rrm_active = false; if (neighbor_list_lock) { os_mutex_delete(neighbor_list_lock); neighbor_list_lock = NULL; diff --git a/components/wpa_supplicant/port/eloop.c b/components/wpa_supplicant/port/eloop.c index c1c71f274b..758e78151c 100644 --- a/components/wpa_supplicant/port/eloop.c +++ b/components/wpa_supplicant/port/eloop.c @@ -17,6 +17,7 @@ #include "list.h" #include "eloop.h" #include "esp_wifi_driver.h" +#include "rom/ets_sys.h" struct eloop_timeout { struct dl_list list; From 84ae3dc8489c3cfa94c09f44b4a4dfa9a095181f Mon Sep 17 00:00:00 2001 From: Kapil Gupta Date: Wed, 30 Oct 2024 12:18:23 +0530 Subject: [PATCH 2/9] fix(esp_wifi): Set minimum scan time to 30ms in roaming app --- components/esp_wifi/wifi_apps/roaming_app/src/Kconfig.roaming | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/esp_wifi/wifi_apps/roaming_app/src/Kconfig.roaming b/components/esp_wifi/wifi_apps/roaming_app/src/Kconfig.roaming index f36211db54..ab6ee2d20b 100644 --- a/components/esp_wifi/wifi_apps/roaming_app/src/Kconfig.roaming +++ b/components/esp_wifi/wifi_apps/roaming_app/src/Kconfig.roaming @@ -96,7 +96,7 @@ menu "Scan Configuration" config ESP_WIFI_ROAMING_SCAN_MIN_SCAN_TIME int "Minimum duration (in milliseconds) of station's per channel active scan" - default 10 + default 30 range 0 120 help Minimum duration of active scanning per channel in milliseconds. From e296094b00df42fbff178a48983dab32be35cdfd Mon Sep 17 00:00:00 2001 From: Kapil Gupta Date: Fri, 8 Nov 2024 16:20:30 +0530 Subject: [PATCH 3/9] fix(wifi): Provide a config option to skip IP renew during roam --- components/esp_wifi/src/wifi_default.c | 19 +++++++++++++++++++ .../wifi_apps/roaming_app/src/Kconfig.roaming | 8 ++++++++ 2 files changed, 27 insertions(+) diff --git a/components/esp_wifi/src/wifi_default.c b/components/esp_wifi/src/wifi_default.c index 4d3acdc741..b3e01a7e85 100644 --- a/components/esp_wifi/src/wifi_default.c +++ b/components/esp_wifi/src/wifi_default.c @@ -26,6 +26,9 @@ static esp_netif_t *s_wifi_netifs[MAX_WIFI_IFS] = { NULL }; static bool wifi_default_handlers_set = false; static esp_err_t disconnect_and_destroy(esp_netif_t* esp_netif); +#ifdef ESP_WIFI_NETWORK_ASSISTED_ROAMING_IP_RENEW_SKIP +static bool roaming_ongoing = false; +#endif // // Default event handlers @@ -84,6 +87,13 @@ static void wifi_default_action_sta_stop(void *arg, esp_event_base_t base, int32 static void wifi_default_action_sta_connected(void *arg, esp_event_base_t base, int32_t event_id, void *data) { +#ifdef ESP_WIFI_NETWORK_ASSISTED_ROAMING_IP_RENEW_SKIP + if (roaming_ongoing) { + /* IP stack is already in ready state */ + roaming_ongoing = false; + return; + } +#endif if (s_wifi_netifs[WIFI_IF_STA] != NULL) { esp_err_t ret; esp_netif_t *esp_netif = s_wifi_netifs[WIFI_IF_STA]; @@ -103,6 +113,15 @@ static void wifi_default_action_sta_connected(void *arg, esp_event_base_t base, static void wifi_default_action_sta_disconnected(void *arg, esp_event_base_t base, int32_t event_id, void *data) { + wifi_event_sta_disconnected_t *disconn = data; +#ifdef ESP_WIFI_NETWORK_ASSISTED_ROAMING_IP_RENEW_SKIP + if (disconn->reason == WIFI_REASON_ROAMING) { + roaming_ongoing = true; + /* do nothing else */ + return; + } + roaming_ongoing = false; +#endif if (s_wifi_netifs[WIFI_IF_STA] != NULL) { esp_netif_action_disconnected(s_wifi_netifs[WIFI_IF_STA], base, event_id, data); } diff --git a/components/esp_wifi/wifi_apps/roaming_app/src/Kconfig.roaming b/components/esp_wifi/wifi_apps/roaming_app/src/Kconfig.roaming index ab6ee2d20b..98e768f899 100644 --- a/components/esp_wifi/wifi_apps/roaming_app/src/Kconfig.roaming +++ b/components/esp_wifi/wifi_apps/roaming_app/src/Kconfig.roaming @@ -90,6 +90,14 @@ menu "Roaming Methods" Retry threshold after which the station should stop using Network Assisted roaming methods and start using legacy roaming instead. + config ESP_WIFI_NETWORK_ASSISTED_ROAMING_IP_RENEW_SKIP + bool "Skip IP renew during BTM based roaming" + depends on ESP_WIFI_ROAMING_NETWORK_ASSISTED_ROAM + default y + help + Station will not ask for IP renew after a BTM based roaming. Before enabling please + make sure your network supports this. + endmenu #"Roaming Methods" menu "Scan Configuration" From 7c94365ce948ce70c0de5eac573214cbb9366051 Mon Sep 17 00:00:00 2001 From: Kapil Gupta Date: Wed, 13 Nov 2024 15:35:17 +0530 Subject: [PATCH 4/9] fix(roaming_app): Add get set config params for the app --- .../wifi_apps/roaming_app/src/roaming_app.c | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) 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 8c00e6c6cc..f7e3fafec3 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 @@ -915,3 +915,53 @@ void deinit_roaming_app(void) scan_results_lock = NULL; } } + +esp_err_t roam_get_config_params(struct roam_config *config) +{ + memcpy(config, &g_roaming_app.config, sizeof(*config)); + + return ESP_OK; +} + +esp_err_t roam_set_config_params(struct roam_config *config) +{ + g_roaming_app.config.backoff_time = config->backoff_time; + + g_roaming_app.config.low_rssi_roam_trigger = config->low_rssi_roam_trigger; + g_roaming_app.config.low_rssi_threshold = config->low_rssi_threshold; + g_roaming_app.config.rssi_threshold_reduction_offset = config->rssi_threshold_reduction_offset; + + g_roaming_app.config.scan_monitor = config->scan_monitor; + g_roaming_app.config.scan_interval = config->scan_interval; + g_roaming_app.config.scan_rssi_threshold = config->scan_rssi_threshold; + g_roaming_app.config.scan_rssi_diff = config->scan_rssi_diff; + + g_roaming_app.config.legacy_roam_enabled = config->legacy_roam_enabled; + g_roaming_app.config.btm_retry_cnt = config->btm_retry_cnt; + g_roaming_app.config.btm_roaming_enabled = config->btm_roaming_enabled; + + g_roaming_app.config.rrm_monitor = config->rrm_monitor; + g_roaming_app.config.rrm_monitor_time = config->rrm_monitor_time; + g_roaming_app.config.rrm_monitor_rssi_threshold = config->rrm_monitor_rssi_threshold; + + ESP_LOGD(ROAMING_TAG, "Updated Roaming app config :"); + + ESP_LOGD(ROAMING_TAG, "backoff time=%d low_rssi_roam_trigger=%d low_rssi_threshold=%d rssi_threshold_reduction_offset=%d", + g_roaming_app.config.backoff_time, g_roaming_app.config.low_rssi_roam_trigger, + g_roaming_app.config.low_rssi_threshold, g_roaming_app.config.rssi_threshold_reduction_offset); + + ESP_LOGD(ROAMING_TAG, "scan_monitor=%d scan_interval=%d scan_rssi_threshold=%d scan_rssi_diff=%d", + g_roaming_app.config.scan_monitor, g_roaming_app.config.scan_interval, + g_roaming_app.config.scan_rssi_threshold, g_roaming_app.config.scan_rssi_diff); + + ESP_LOGD(ROAMING_TAG, "legacy_roam_enabled=%d, btm_retry_cnt=%d btm_roaming_enabled=%d", + g_roaming_app.config.legacy_roam_enabled, + g_roaming_app.config.btm_retry_cnt, g_roaming_app.config.btm_roaming_enabled); + + ESP_LOGD(ROAMING_TAG, "rrm_monitor=%d, rrm_monitor_time=%d rrm_monitor_rssi_threshold=%d", + g_roaming_app.config.rrm_monitor, + g_roaming_app.config.rrm_monitor_time, + g_roaming_app.config.rrm_monitor_rssi_threshold); + + return ESP_OK; +} From 8ed1cf8050c48e6b135049fde81601982bf81c6b Mon Sep 17 00:00:00 2001 From: Kapil Gupta Date: Wed, 13 Nov 2024 20:53:07 +0530 Subject: [PATCH 5/9] fix(esp_wifi): moving around roaming app code a bit --- .../roaming_app/include/esp_roaming.h | 125 +--------------- .../wifi_apps/roaming_app/src/esp_roaming_i.h | 136 ++++++++++++++++++ .../wifi_apps/roaming_app/src/roaming_app.c | 60 ++++---- 3 files changed, 170 insertions(+), 151 deletions(-) create mode 100644 components/esp_wifi/wifi_apps/roaming_app/src/esp_roaming_i.h 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 64b4575937..35669c1513 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 @@ -16,87 +16,6 @@ extern "C" { #endif -#define SUPPLICANT_CANDIDATE_LIST_EXPIRY 10 - -/* Global Roaming Configuration */ -#define ROAMING_BACKOFF_TIME CONFIG_ESP_WIFI_ROAMING_BACKOFF_TIME - -/* Low RSSI based roaming configuration */ -#define LOW_RSSI_ROAMING_ENABLED CONFIG_ESP_WIFI_ROAMING_LOW_RSSI_ROAMING -#if LOW_RSSI_ROAMING_ENABLED -#define ROAMING_LOW_RSSI_THRESHOLD CONFIG_ESP_WIFI_ROAMING_LOW_RSSI_THRESHOLD -#define RSSI_THRESHOLD_REDUCTION_OFFSET CONFIG_ESP_WIFI_ROAMING_LOW_RSSI_OFFSET -#endif /*LOW_RSSI_ROAMING_ENABLED*/ - -/* Periodic Scan based Roaming configuration */ -#define PERIODIC_SCAN_MONITORING CONFIG_ESP_WIFI_ROAMING_PERIODIC_SCAN_MONITOR -#if PERIODIC_SCAN_MONITORING -#define SCAN_MONITOR_INTERVAL CONFIG_ESP_WIFI_ROAMING_SCAN_MONITOR_INTERVAL -#define SCAN_MONITOR_RSSI_THRESHOLD CONFIG_ESP_WIFI_ROAMING_PERIODIC_SCAN_THRESHOLD -#define SCAN_ROAM_RSSI_DIFF CONFIG_ESP_WIFI_ROAMING_SCAN_ROAM_RSSI_DIFF -#endif /* PERIODIC_SCAN_MONITORING */ - -/* Scan configuration */ -#define SCAN_TIME_MIN_DURATION CONFIG_ESP_WIFI_ROAMING_SCAN_MIN_SCAN_TIME -#define SCAN_TIME_MAX_DURATION CONFIG_ESP_WIFI_ROAMING_SCAN_MAX_SCAN_TIME -#define HOME_CHANNEL_DWELL_TIME CONFIG_ESP_WIFI_ROAMING_HOME_CHANNEL_DWELL_TIME -#define SCAN_PREFERRED_CHAN_LIST CONFIG_ESP_WIFI_ROAMING_SCAN_CHAN_LIST -#define DEFAULT_PREFERRED_SCAN_CHAN_LIST "None" -#define SCAN_RESULTS_USABILITY_WINDOW CONFIG_ESP_WIFI_ROAMING_SCAN_EXPIRY_WINDOW -#define MAX_CANDIDATE_COUNT CONFIG_ESP_WIFI_ROAMING_MAX_CANDIDATES - -/* Legacy roaming configuration */ -#ifdef CONFIG_ESP_WIFI_ROAMING_LEGACY_ROAMING -#define LEGACY_ROAM_ENABLED CONFIG_ESP_WIFI_ROAMING_LEGACY_ROAMING -#else -#define LEGACY_ROAM_ENABLED 0 -#endif - -#ifdef CONFIG_ESP_WIFI_NETWORK_ASSISTED_ROAMING_RETRY_COUNT -#define BSS_TM_RETRY_COUNT CONFIG_ESP_WIFI_NETWORK_ASSISTED_ROAMING_RETRY_COUNT -#else -#define BSS_TM_RETRY_COUNT 0 -#endif - -/* Network Assisted Roaming */ -#ifdef CONFIG_ESP_WIFI_ROAMING_NETWORK_ASSISTED_ROAM -#define NETWORK_ASSISTED_ROAMING_ENABLED CONFIG_ESP_WIFI_ROAMING_NETWORK_ASSISTED_ROAM -#else -#define NETWORK_ASSISTED_ROAMING_ENABLED 0 -#endif - -/* Periodic RRM configuration */ -#define PERIODIC_RRM_MONITORING CONFIG_ESP_WIFI_ROAMING_PERIODIC_RRM_MONITORING -#if PERIODIC_RRM_MONITORING -#define RRM_MONITOR_TIME CONFIG_ESP_WIFI_ROAMING_RRM_MONITOR_TIME -#define RRM_MONITOR_RSSI_THRESHOLD CONFIG_ESP_WIFI_ROAMING_RRM_MONITOR_THRESHOLD -#endif /*PERIODIC_RRM_MONITORING*/ - -#define MAX_SCAN_CHAN_LIST_COUNT 14 - -#define MAX_NEIGHBOR_LEN 512 - -#define IS_PSK(authmode) \ - (((authmode == WIFI_AUTH_WPA_PSK) || (authmode == WIFI_AUTH_WPA2_PSK) || \ - (authmode == WIFI_AUTH_WPA_WPA2_PSK) || (authmode == WIFI_AUTH_WPA3_PSK) || \ - (authmode == WIFI_AUTH_WPA2_WPA3_PSK) || (authmode == WIFI_AUTH_WAPI_PSK) ? 1 : 0)) - -#define OWE_COMPATIBLE(curr_auth, cand_auth) \ - ((((curr_auth == WIFI_AUTH_OPEN) || (curr_auth == WIFI_AUTH_OWE)) && ((cand_auth == WIFI_AUTH_OPEN) || (cand_auth == WIFI_AUTH_OWE)))? 1 : 0) - -#define PSK_COMPATIBLE(curr_auth, cand_auth) \ - ((IS_PSK(curr_auth) && IS_PSK(cand_auth)) ? 1 : 0) - -struct scanned_ap_info { - uint16_t current_count; - struct timeval time; - wifi_ap_record_t ap_records[MAX_CANDIDATE_COUNT]; -}; -struct cand_bss { - uint8_t channel; - uint8_t bssid[ETH_ALEN]; -}; - struct roam_config { uint8_t backoff_time; bool low_rssi_roam_trigger; @@ -112,54 +31,14 @@ struct roam_config { bool rrm_monitor; uint8_t rrm_monitor_time; uint8_t rrm_monitor_rssi_threshold; -}; - -struct roaming_app { - wifi_scan_config_t scan_params; - struct roam_config config; - bool scan_ongoing; - int8_t current_rssi_threshold; - char *btm_neighbor_list; - struct timeval last_roamed_time; - wifi_ap_record_t ap_info; - struct scanned_ap_info scanned_aps; - bool btm_support; - bool rrm_support; - -#if LOW_RSSI_ROAMING_ENABLED - int8_t current_low_rssi_threshold; -#endif -#if LEGACY_ROAM_ENABLED && NETWORK_ASSISTED_ROAMING_ENABLED - uint8_t btm_attempt; -#endif -#if LEGACY_ROAM_ENABLED - bool force_roam_ongoing; -#endif -#if PERIODIC_RRM_MONITORING - bool periodic_rrm_active; - bool rrm_request_active; -#endif -#if PERIODIC_SCAN_MONITORING - bool periodic_scan_active; -#endif - bool allow_reconnect; + wifi_scan_config_t scan_config; }; void init_roaming_app(void); void deinit_roaming_app(void); - -#if PERIODIC_RRM_MONITORING -void roaming_app_periodic_rrm_internal_handler(void *data, void *ctx); -#endif /*PERIODIC_RRM_MONITORING*/ - -#if PERIODIC_SCAN_MONITORING -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/esp_roaming_i.h b/components/esp_wifi/wifi_apps/roaming_app/src/esp_roaming_i.h new file mode 100644 index 0000000000..87400622d6 --- /dev/null +++ b/components/esp_wifi/wifi_apps/roaming_app/src/esp_roaming_i.h @@ -0,0 +1,136 @@ +/* + * SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#pragma once + +#include "sdkconfig.h" +#include "esp_log.h" +#include "esp_err.h" +#include "esp_wifi_types.h" +#include "utils/common.h" +#include +#ifdef __cplusplus +extern "C" { +#endif + +#define SUPPLICANT_CANDIDATE_LIST_EXPIRY 10 + +/* Global Roaming Configuration */ +#define ROAMING_BACKOFF_TIME CONFIG_ESP_WIFI_ROAMING_BACKOFF_TIME + +/* Low RSSI based roaming configuration */ +#define LOW_RSSI_ROAMING_ENABLED CONFIG_ESP_WIFI_ROAMING_LOW_RSSI_ROAMING +#if LOW_RSSI_ROAMING_ENABLED +#define ROAMING_LOW_RSSI_THRESHOLD CONFIG_ESP_WIFI_ROAMING_LOW_RSSI_THRESHOLD +#define RSSI_THRESHOLD_REDUCTION_OFFSET CONFIG_ESP_WIFI_ROAMING_LOW_RSSI_OFFSET +#endif /*LOW_RSSI_ROAMING_ENABLED*/ + +/* Periodic Scan based Roaming configuration */ +#define PERIODIC_SCAN_MONITORING CONFIG_ESP_WIFI_ROAMING_PERIODIC_SCAN_MONITOR +#if PERIODIC_SCAN_MONITORING +#define SCAN_MONITOR_INTERVAL CONFIG_ESP_WIFI_ROAMING_SCAN_MONITOR_INTERVAL +#define SCAN_MONITOR_RSSI_THRESHOLD CONFIG_ESP_WIFI_ROAMING_PERIODIC_SCAN_THRESHOLD +#define SCAN_ROAM_RSSI_DIFF CONFIG_ESP_WIFI_ROAMING_SCAN_ROAM_RSSI_DIFF +#endif /* PERIODIC_SCAN_MONITORING */ + +/* Scan configuration */ +#define SCAN_TIME_MIN_DURATION CONFIG_ESP_WIFI_ROAMING_SCAN_MIN_SCAN_TIME +#define SCAN_TIME_MAX_DURATION CONFIG_ESP_WIFI_ROAMING_SCAN_MAX_SCAN_TIME +#define HOME_CHANNEL_DWELL_TIME CONFIG_ESP_WIFI_ROAMING_HOME_CHANNEL_DWELL_TIME +#define SCAN_PREFERRED_CHAN_LIST CONFIG_ESP_WIFI_ROAMING_SCAN_CHAN_LIST +#define DEFAULT_PREFERRED_SCAN_CHAN_LIST "None" +#define SCAN_RESULTS_USABILITY_WINDOW CONFIG_ESP_WIFI_ROAMING_SCAN_EXPIRY_WINDOW +#define MAX_CANDIDATE_COUNT CONFIG_ESP_WIFI_ROAMING_MAX_CANDIDATES + +/* Legacy roaming configuration */ +#ifdef CONFIG_ESP_WIFI_ROAMING_LEGACY_ROAMING +#define LEGACY_ROAM_ENABLED CONFIG_ESP_WIFI_ROAMING_LEGACY_ROAMING +#else +#define LEGACY_ROAM_ENABLED 0 +#endif + +#ifdef CONFIG_ESP_WIFI_NETWORK_ASSISTED_ROAMING_RETRY_COUNT +#define BSS_TM_RETRY_COUNT CONFIG_ESP_WIFI_NETWORK_ASSISTED_ROAMING_RETRY_COUNT +#else +#define BSS_TM_RETRY_COUNT 0 +#endif + +/* Network Assisted Roaming */ +#ifdef CONFIG_ESP_WIFI_ROAMING_NETWORK_ASSISTED_ROAM +#define NETWORK_ASSISTED_ROAMING_ENABLED CONFIG_ESP_WIFI_ROAMING_NETWORK_ASSISTED_ROAM +#else +#define NETWORK_ASSISTED_ROAMING_ENABLED 0 +#endif + +/* Periodic RRM configuration */ +#define PERIODIC_RRM_MONITORING CONFIG_ESP_WIFI_ROAMING_PERIODIC_RRM_MONITORING +#if PERIODIC_RRM_MONITORING +#define RRM_MONITOR_TIME CONFIG_ESP_WIFI_ROAMING_RRM_MONITOR_TIME +#define RRM_MONITOR_RSSI_THRESHOLD CONFIG_ESP_WIFI_ROAMING_RRM_MONITOR_THRESHOLD +#endif /*PERIODIC_RRM_MONITORING*/ + +#define MAX_SCAN_CHAN_LIST_COUNT 14 + +#define MAX_NEIGHBOR_LEN 512 + +#define IS_PSK(authmode) \ + (((authmode == WIFI_AUTH_WPA_PSK) || (authmode == WIFI_AUTH_WPA2_PSK) || \ + (authmode == WIFI_AUTH_WPA_WPA2_PSK) || (authmode == WIFI_AUTH_WPA3_PSK) || \ + (authmode == WIFI_AUTH_WPA2_WPA3_PSK) || (authmode == WIFI_AUTH_WAPI_PSK) ? 1 : 0)) + +#define OWE_COMPATIBLE(curr_auth, cand_auth) \ + ((((curr_auth == WIFI_AUTH_OPEN) || (curr_auth == WIFI_AUTH_OWE)) && ((cand_auth == WIFI_AUTH_OPEN) || (cand_auth == WIFI_AUTH_OWE)))? 1 : 0) + +#define PSK_COMPATIBLE(curr_auth, cand_auth) \ + ((IS_PSK(curr_auth) && IS_PSK(cand_auth)) ? 1 : 0) + +struct scanned_ap_info { + uint16_t current_count; + struct timeval time; + wifi_ap_record_t ap_records[MAX_CANDIDATE_COUNT]; +}; +struct cand_bss { + uint8_t channel; + uint8_t bssid[ETH_ALEN]; +}; + +struct roam_bss_info { + wifi_ap_record_t ap; + bool btm_support; + bool rrm_support; +}; + +struct roaming_app { + struct roam_config config; + bool scan_ongoing; + int8_t current_rssi_threshold; + char *btm_neighbor_list; + struct timeval last_roamed_time; + struct scanned_ap_info scanned_aps; + struct roam_bss_info current_bss; + +#if LOW_RSSI_ROAMING_ENABLED + int8_t current_low_rssi_threshold; +#endif +#if LEGACY_ROAM_ENABLED && NETWORK_ASSISTED_ROAMING_ENABLED + uint8_t btm_attempt; +#endif +#if LEGACY_ROAM_ENABLED + bool force_roam_ongoing; +#endif +#if PERIODIC_RRM_MONITORING + bool periodic_rrm_active; + bool rrm_request_active; +#endif +#if PERIODIC_SCAN_MONITORING + bool periodic_scan_active; +#endif + bool allow_reconnect; +}; + +#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 f7e3fafec3..7d2f62e703 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 @@ -22,6 +22,7 @@ #include "regex.h" #include #include "esp_roaming.h" +#include "esp_roaming_i.h" #include "utils/common.h" #include "esp_wifi_driver.h" #include "utils/eloop.h" @@ -43,6 +44,8 @@ static void *neighbor_list_lock = NULL; static int wifi_post_roam_event(struct cand_bss *bss); static void determine_best_ap(int8_t rssi_threshold); +static void roaming_app_periodic_rrm_internal_handler(void *data, void *ctx); +static void roaming_app_periodic_scan_internal_handler(void *data, void *ctx); static const char *ROAMING_TAG = "ROAM"; @@ -193,28 +196,28 @@ static void roaming_app_sta_stop_event_handler(void* arg, esp_event_base_t event static void roaming_app_connected_event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) { - roaming_app_get_ap_info(&g_roaming_app.ap_info); - g_roaming_app.scan_params.ssid = g_roaming_app.ap_info.ssid; + roaming_app_get_ap_info(&g_roaming_app.current_bss.ap); + g_roaming_app.config.scan_config.ssid = g_roaming_app.current_bss.ap.ssid; #if LOW_RSSI_ROAMING_ENABLED - if (g_roaming_app.ap_info.rssi < g_roaming_app.config.low_rssi_threshold) { + if (g_roaming_app.current_bss.ap.rssi < g_roaming_app.config.low_rssi_threshold) { /* To ensure that the threshold is set to one offset below the current AP RSSI * in case, the AP is already below the RSSI threshold */ - g_roaming_app.current_low_rssi_threshold = g_roaming_app.ap_info.rssi - g_roaming_app.config.rssi_threshold_reduction_offset; + g_roaming_app.current_low_rssi_threshold = g_roaming_app.current_bss.ap.rssi - g_roaming_app.config.rssi_threshold_reduction_offset; } else { g_roaming_app.current_low_rssi_threshold = g_roaming_app.config.low_rssi_threshold; } ESP_LOGD(ROAMING_TAG, "setting rssi threshold as %d", g_roaming_app.current_low_rssi_threshold); esp_wifi_set_rssi_threshold(g_roaming_app.current_low_rssi_threshold); #endif /*LOW_RSSI_ROAMING_ENABLED*/ - g_roaming_app.rrm_support = esp_rrm_is_rrm_supported_connection(); - g_roaming_app.btm_support = esp_wnm_is_btm_supported_connection(); + g_roaming_app.current_bss.rrm_support = esp_rrm_is_rrm_supported_connection(); + g_roaming_app.current_bss.btm_support = esp_wnm_is_btm_supported_connection(); ESP_LOGD(ROAMING_TAG, "Station connected, RRM %ssupported, BTM %ssupported", - g_roaming_app.rrm_support ? " " : "not ", - g_roaming_app.btm_support ? " " : "not "); + g_roaming_app.current_bss.rrm_support ? " " : "not ", + g_roaming_app.current_bss.btm_support ? " " : "not "); gettimeofday(&g_roaming_app.last_roamed_time, NULL); if (!initialize_roaming_event()) { #if PERIODIC_RRM_MONITORING - if (g_roaming_app.rrm_support) { + if (g_roaming_app.current_bss.rrm_support) { init_periodic_rrm_event(); } #endif /*PERIODIC_RRM_MONITORING*/ @@ -374,7 +377,7 @@ static void roaming_app_rssi_low_handler(void* arg, esp_event_base_t event_base, wifi_event_bss_rssi_low_t *event = event_data; ESP_LOGI(ROAMING_TAG, "%s:bss rssi is=%ld", __func__, event->rssi); - roaming_app_get_ap_info(&g_roaming_app.ap_info); + roaming_app_get_ap_info(&g_roaming_app.current_bss.ap); determine_best_ap(0); g_roaming_app.current_low_rssi_threshold -= g_roaming_app.config.rssi_threshold_reduction_offset; ESP_LOGD(ROAMING_TAG, "Resetting RSSI Threshold to %d", g_roaming_app.current_low_rssi_threshold); @@ -430,7 +433,7 @@ void roaming_app_trigger_roam(struct cand_bss *bss) goto free_bss; } #if NETWORK_ASSISTED_ROAMING_ENABLED - if (g_roaming_app.config.btm_roaming_enabled && g_roaming_app.btm_support) { + if (g_roaming_app.config.btm_roaming_enabled && g_roaming_app.current_bss.btm_support) { #if LEGACY_ROAM_ENABLED && NETWORK_ASSISTED_ROAMING_ENABLED if (g_roaming_app.btm_attempt <= g_roaming_app.config.btm_retry_cnt) { #endif @@ -501,8 +504,8 @@ void print_ap_records(struct scanned_ap_info *ap_info) #if PERIODIC_RRM_MONITORING static void periodic_rrm_request(struct timeval *now) { - roaming_app_get_ap_info(&g_roaming_app.ap_info); - if (esp_rrm_is_rrm_supported_connection() && (g_roaming_app.ap_info.rssi < g_roaming_app.config.rrm_monitor_rssi_threshold)) { + roaming_app_get_ap_info(&g_roaming_app.current_bss.ap); + if (esp_rrm_is_rrm_supported_connection() && (g_roaming_app.current_bss.ap.rssi < g_roaming_app.config.rrm_monitor_rssi_threshold)) { if (esp_rrm_send_neighbor_report_request() < 0) { ESP_LOGE(ROAMING_TAG, "failed to send neighbor report request"); return; @@ -514,7 +517,7 @@ static void periodic_rrm_request(struct timeval *now) static bool candidate_security_match(wifi_ap_record_t candidate) { - wifi_auth_mode_t curr_auth = g_roaming_app.ap_info.authmode; + wifi_auth_mode_t curr_auth = g_roaming_app.current_bss.ap.authmode; wifi_auth_mode_t cand_auth = candidate.authmode; ESP_LOGV(ROAMING_TAG, "Cand authmode : %d, Current Authmode : %d", cand_auth, curr_auth); if (cand_auth == curr_auth) { @@ -618,7 +621,7 @@ static void conduct_scan(void) gettimeofday(&g_roaming_app.scanned_aps.time, NULL); /* Issue scan */ os_memset(&g_roaming_app.scanned_aps, 0, sizeof(struct scanned_ap_info)); - if (esp_wifi_promiscuous_scan_start(&g_roaming_app.scan_params, scan_done_event_handler) < 0) { + if (esp_wifi_promiscuous_scan_start(&g_roaming_app.config.scan_config, scan_done_event_handler) < 0) { ESP_LOGE(ROAMING_TAG, "failed to issue scan"); return; } @@ -661,9 +664,9 @@ static void periodic_scan_roam(struct timeval *now) #endif /*NETWORK_ASSISTED_ROAMING_ENABLED && !LEGACY_ROAM_ENABLED*/ /* If the current RSSI is not worse than the configured threshold * for station initiated roam, then do not trigger roam */ - roaming_app_get_ap_info(&g_roaming_app.ap_info); - ESP_LOGD(ROAMING_TAG, "Connected AP's RSSI=%d", g_roaming_app.ap_info.rssi); - if (g_roaming_app.ap_info.rssi > g_roaming_app.config.scan_rssi_threshold) { + roaming_app_get_ap_info(&g_roaming_app.current_bss.ap); + ESP_LOGD(ROAMING_TAG, "Connected AP's RSSI=%d", g_roaming_app.current_bss.ap.rssi); + if (g_roaming_app.current_bss.ap.rssi > g_roaming_app.config.scan_rssi_threshold) { return; } @@ -672,7 +675,7 @@ static void periodic_scan_roam(struct timeval *now) #endif /*PERIODIC_SCAN_MONITORING*/ #if PERIODIC_RRM_MONITORING -void roaming_app_periodic_rrm_internal_handler(void *data, void *ctx) +static void roaming_app_periodic_rrm_internal_handler(void *data, void *ctx) { struct timeval now; @@ -695,7 +698,7 @@ void roaming_app_periodic_rrm_internal_handler(void *data, void *ctx) #endif /*PERIODIC_RRM_MONITORING*/ #if PERIODIC_SCAN_MONITORING -void roaming_app_periodic_scan_internal_handler(void *data, void *ctx) +static void roaming_app_periodic_scan_internal_handler(void *data, void *ctx) { struct timeval now; @@ -761,14 +764,14 @@ static int8_t parse_scan_chan_list(void) char* token; token = strsep(&scan_chan_string, ","); - g_roaming_app.scan_params.channel_bitmap.ghz_2_channels = 0; + g_roaming_app.config.scan_config.channel_bitmap.ghz_2_channels = 0; while (token != NULL) { uint8_t channel = atoi(token); /* Check if the number is within the required range */ if (channel >= 1 && channel <= 14) { /* Check if the number is already present in the array */ - g_roaming_app.scan_params.channel_bitmap.ghz_2_channels |= (1 << channel); + g_roaming_app.config.scan_config.channel_bitmap.ghz_2_channels |= (1 << channel); } else { ESP_LOGE(ROAMING_TAG, "Channel out of range: %d", channel); ret = -1; @@ -828,7 +831,7 @@ static esp_err_t init_config_params(void) return ESP_OK; } -static esp_err_t init_scan_params(void) +static esp_err_t init_scan_config(void) { if (!scan_results_lock) { scan_results_lock = os_recursive_mutex_create(); @@ -842,10 +845,10 @@ static esp_err_t init_scan_params(void) ESP_ERROR_CHECK(parse_scan_chan_list()); } - g_roaming_app.scan_params.scan_type = 0; - g_roaming_app.scan_params.scan_time.active.min = SCAN_TIME_MIN_DURATION; - g_roaming_app.scan_params.scan_time.active.max = SCAN_TIME_MAX_DURATION; - g_roaming_app.scan_params.home_chan_dwell_time = HOME_CHANNEL_DWELL_TIME; + g_roaming_app.config.scan_config.scan_type = 0; + g_roaming_app.config.scan_config.scan_time.active.min = SCAN_TIME_MIN_DURATION; + g_roaming_app.config.scan_config.scan_time.active.max = SCAN_TIME_MAX_DURATION; + g_roaming_app.config.scan_config.home_chan_dwell_time = HOME_CHANNEL_DWELL_TIME; gettimeofday(&g_roaming_app.scanned_aps.time, NULL); return ESP_OK; } @@ -868,7 +871,7 @@ void init_roaming_app(void) ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_BSS_RSSI_LOW, &roaming_app_rssi_low_handler, NULL)); #endif /*LOW_RSSI_ROAMING_ENABLED*/ - ESP_ERROR_CHECK(init_scan_params()); + ESP_ERROR_CHECK(init_scan_config()); ESP_ERROR_CHECK(init_config_params()); #if PERIODIC_RRM_MONITORING ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_NEIGHBOR_REP, @@ -943,6 +946,7 @@ esp_err_t roam_set_config_params(struct roam_config *config) g_roaming_app.config.rrm_monitor = config->rrm_monitor; g_roaming_app.config.rrm_monitor_time = config->rrm_monitor_time; g_roaming_app.config.rrm_monitor_rssi_threshold = config->rrm_monitor_rssi_threshold; + g_roaming_app.config.scan_config = config->scan_config; ESP_LOGD(ROAMING_TAG, "Updated Roaming app config :"); From e5915c3ce3821348a50229ceeb3ceb7120f1a300 Mon Sep 17 00:00:00 2001 From: Kapil Gupta Date: Tue, 3 Dec 2024 12:34:04 +0530 Subject: [PATCH 6/9] fix(esp_wifi): Some more cleanup for roaming app --- components/esp_wifi/src/wifi_default.c | 2 +- .../roaming_app/include/esp_roaming.h | 14 ++---- .../wifi_apps/roaming_app/src/roaming_app.c | 46 +++++++++---------- .../esp_supplicant/src/esp_common.c | 3 -- 4 files changed, 27 insertions(+), 38 deletions(-) diff --git a/components/esp_wifi/src/wifi_default.c b/components/esp_wifi/src/wifi_default.c index b3e01a7e85..d11a028201 100644 --- a/components/esp_wifi/src/wifi_default.c +++ b/components/esp_wifi/src/wifi_default.c @@ -113,8 +113,8 @@ static void wifi_default_action_sta_connected(void *arg, esp_event_base_t base, static void wifi_default_action_sta_disconnected(void *arg, esp_event_base_t base, int32_t event_id, void *data) { - wifi_event_sta_disconnected_t *disconn = data; #ifdef ESP_WIFI_NETWORK_ASSISTED_ROAMING_IP_RENEW_SKIP + wifi_event_sta_disconnected_t *disconn = data; if (disconn->reason == WIFI_REASON_ROAMING) { roaming_ongoing = true; /* do nothing else */ 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 35669c1513..186ed196e2 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 @@ -6,12 +6,6 @@ #pragma once -#include "sdkconfig.h" -#include "esp_log.h" -#include "esp_err.h" -#include "esp_wifi_types.h" -#include "utils/common.h" -#include #ifdef __cplusplus extern "C" { #endif @@ -19,18 +13,18 @@ extern "C" { struct roam_config { uint8_t backoff_time; bool low_rssi_roam_trigger; - uint8_t low_rssi_threshold; + int8_t low_rssi_threshold; uint8_t rssi_threshold_reduction_offset; bool scan_monitor; uint8_t scan_interval; - uint8_t scan_rssi_threshold; + int8_t scan_rssi_threshold; uint8_t scan_rssi_diff; bool legacy_roam_enabled; uint8_t btm_retry_cnt; bool btm_roaming_enabled; bool rrm_monitor; uint8_t rrm_monitor_time; - uint8_t rrm_monitor_rssi_threshold; + int8_t rrm_monitor_rssi_threshold; wifi_scan_config_t scan_config; }; @@ -38,6 +32,8 @@ void init_roaming_app(void); void deinit_roaming_app(void); void roaming_app_disable_reconnect(void); void roaming_app_enable_reconnect(void); +esp_err_t roam_get_config_params(struct roam_config *config); +esp_err_t roam_set_config_params(struct roam_config *config); #ifdef __cplusplus } 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 7d2f62e703..f2d4bed0c2 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 @@ -919,6 +919,7 @@ void deinit_roaming_app(void) } } +/* No need for this to be done in pptask ctx */ esp_err_t roam_get_config_params(struct roam_config *config) { memcpy(config, &g_roaming_app.config, sizeof(*config)); @@ -926,46 +927,41 @@ esp_err_t roam_get_config_params(struct roam_config *config) return ESP_OK; } -esp_err_t roam_set_config_params(struct roam_config *config) +static int update_config_params(void *data) { - g_roaming_app.config.backoff_time = config->backoff_time; + struct roam_config *config = data; + g_roaming_app.config = *config; - g_roaming_app.config.low_rssi_roam_trigger = config->low_rssi_roam_trigger; - g_roaming_app.config.low_rssi_threshold = config->low_rssi_threshold; - g_roaming_app.config.rssi_threshold_reduction_offset = config->rssi_threshold_reduction_offset; + ESP_LOGI(ROAMING_TAG, "Updated Roaming app config :"); - g_roaming_app.config.scan_monitor = config->scan_monitor; - g_roaming_app.config.scan_interval = config->scan_interval; - g_roaming_app.config.scan_rssi_threshold = config->scan_rssi_threshold; - g_roaming_app.config.scan_rssi_diff = config->scan_rssi_diff; - - g_roaming_app.config.legacy_roam_enabled = config->legacy_roam_enabled; - g_roaming_app.config.btm_retry_cnt = config->btm_retry_cnt; - g_roaming_app.config.btm_roaming_enabled = config->btm_roaming_enabled; - - g_roaming_app.config.rrm_monitor = config->rrm_monitor; - g_roaming_app.config.rrm_monitor_time = config->rrm_monitor_time; - g_roaming_app.config.rrm_monitor_rssi_threshold = config->rrm_monitor_rssi_threshold; - g_roaming_app.config.scan_config = config->scan_config; - - ESP_LOGD(ROAMING_TAG, "Updated Roaming app config :"); - - ESP_LOGD(ROAMING_TAG, "backoff time=%d low_rssi_roam_trigger=%d low_rssi_threshold=%d rssi_threshold_reduction_offset=%d", + ESP_LOGI(ROAMING_TAG, "backoff time=%d low_rssi_roam_trigger=%d low_rssi_threshold=%d rssi_threshold_reduction_offset=%d", g_roaming_app.config.backoff_time, g_roaming_app.config.low_rssi_roam_trigger, g_roaming_app.config.low_rssi_threshold, g_roaming_app.config.rssi_threshold_reduction_offset); - ESP_LOGD(ROAMING_TAG, "scan_monitor=%d scan_interval=%d scan_rssi_threshold=%d scan_rssi_diff=%d", + ESP_LOGI(ROAMING_TAG, "scan_monitor=%d scan_interval=%d scan_rssi_threshold=%d scan_rssi_diff=%d", g_roaming_app.config.scan_monitor, g_roaming_app.config.scan_interval, g_roaming_app.config.scan_rssi_threshold, g_roaming_app.config.scan_rssi_diff); - ESP_LOGD(ROAMING_TAG, "legacy_roam_enabled=%d, btm_retry_cnt=%d btm_roaming_enabled=%d", + ESP_LOGI(ROAMING_TAG, "legacy_roam_enabled=%d, btm_retry_cnt=%d btm_roaming_enabled=%d", g_roaming_app.config.legacy_roam_enabled, g_roaming_app.config.btm_retry_cnt, g_roaming_app.config.btm_roaming_enabled); - ESP_LOGD(ROAMING_TAG, "rrm_monitor=%d, rrm_monitor_time=%d rrm_monitor_rssi_threshold=%d", + ESP_LOGI(ROAMING_TAG, "rrm_monitor=%d, rrm_monitor_time=%d rrm_monitor_rssi_threshold=%d", g_roaming_app.config.rrm_monitor, g_roaming_app.config.rrm_monitor_time, g_roaming_app.config.rrm_monitor_rssi_threshold); return ESP_OK; } + +esp_err_t roam_set_config_params(struct roam_config *config) +{ + wifi_ipc_config_t cfg; + + cfg.fn = update_config_params; + cfg.arg = config; + cfg.arg_size = sizeof(*config); + esp_wifi_ipc_internal(&cfg, false); + + return ESP_OK; +} diff --git a/components/wpa_supplicant/esp_supplicant/src/esp_common.c b/components/wpa_supplicant/esp_supplicant/src/esp_common.c index ce2a17cb37..f2b02dbdd3 100644 --- a/components/wpa_supplicant/esp_supplicant/src/esp_common.c +++ b/components/wpa_supplicant/esp_supplicant/src/esp_common.c @@ -23,9 +23,6 @@ #include "rsn_supp/wpa_i.h" #include "rsn_supp/wpa.h" #include "esp_private/wifi.h" -#if CONFIG_ESP_WIFI_ENABLE_ROAMING_APP -#include "esp_roaming.h" -#endif /* Utility Functions */ esp_err_t esp_supplicant_str_to_mac(const char *str, uint8_t dest[6]) From 2b495713b2552b21ad6a3601c0782e8a4497199a Mon Sep 17 00:00:00 2001 From: Kapil Gupta Date: Tue, 3 Dec 2024 13:29:44 +0530 Subject: [PATCH 7/9] fix(esp_wifi): Roaming app, sync api naming --- components/esp_wifi/src/wifi_default.c | 6 +++--- components/esp_wifi/src/wifi_init.c | 12 ++++++------ .../wifi_apps/roaming_app/include/esp_roaming.h | 8 ++++---- .../esp_wifi/wifi_apps/roaming_app/src/roaming_app.c | 8 ++++---- 4 files changed, 17 insertions(+), 17 deletions(-) diff --git a/components/esp_wifi/src/wifi_default.c b/components/esp_wifi/src/wifi_default.c index d11a028201..ade23eabb6 100644 --- a/components/esp_wifi/src/wifi_default.c +++ b/components/esp_wifi/src/wifi_default.c @@ -26,7 +26,7 @@ static esp_netif_t *s_wifi_netifs[MAX_WIFI_IFS] = { NULL }; static bool wifi_default_handlers_set = false; static esp_err_t disconnect_and_destroy(esp_netif_t* esp_netif); -#ifdef ESP_WIFI_NETWORK_ASSISTED_ROAMING_IP_RENEW_SKIP +#ifdef CONFIG_ESP_WIFI_NETWORK_ASSISTED_ROAMING_IP_RENEW_SKIP static bool roaming_ongoing = false; #endif @@ -87,7 +87,7 @@ static void wifi_default_action_sta_stop(void *arg, esp_event_base_t base, int32 static void wifi_default_action_sta_connected(void *arg, esp_event_base_t base, int32_t event_id, void *data) { -#ifdef ESP_WIFI_NETWORK_ASSISTED_ROAMING_IP_RENEW_SKIP +#ifdef CONFIG_ESP_WIFI_NETWORK_ASSISTED_ROAMING_IP_RENEW_SKIP if (roaming_ongoing) { /* IP stack is already in ready state */ roaming_ongoing = false; @@ -113,7 +113,7 @@ static void wifi_default_action_sta_connected(void *arg, esp_event_base_t base, static void wifi_default_action_sta_disconnected(void *arg, esp_event_base_t base, int32_t event_id, void *data) { -#ifdef ESP_WIFI_NETWORK_ASSISTED_ROAMING_IP_RENEW_SKIP +#ifdef CONFIG_ESP_WIFI_NETWORK_ASSISTED_ROAMING_IP_RENEW_SKIP wifi_event_sta_disconnected_t *disconn = data; if (disconn->reason == WIFI_REASON_ROAMING) { roaming_ongoing = true; diff --git a/components/esp_wifi/src/wifi_init.c b/components/esp_wifi/src/wifi_init.c index 3bf2ac5c02..42625fce05 100644 --- a/components/esp_wifi/src/wifi_init.c +++ b/components/esp_wifi/src/wifi_init.c @@ -182,7 +182,7 @@ static esp_err_t wifi_deinit_internal(void) esp_supplicant_deinit(); #if CONFIG_ESP_WIFI_ENABLE_ROAMING_APP - deinit_roaming_app(); + roam_deinit_app(); #endif err = esp_wifi_deinit_internal(); @@ -447,7 +447,7 @@ esp_err_t esp_wifi_init(const wifi_init_config_t *config) } #if CONFIG_ESP_WIFI_ENABLE_ROAMING_APP - init_roaming_app(); + roam_init_app(); #endif } else { @@ -485,7 +485,7 @@ esp_err_t esp_wifi_connect(void) ret = esp_wifi_connect_internal(); #if CONFIG_ESP_WIFI_ENABLE_ROAMING_APP - roaming_app_enable_reconnect(); + roam_enable_reconnect(); #endif return ret; } @@ -493,11 +493,11 @@ esp_err_t esp_wifi_connect(void) esp_err_t esp_wifi_disconnect(void) { esp_err_t ret = ESP_OK; +#if CONFIG_ESP_WIFI_ENABLE_ROAMING_APP + roam_disable_reconnect(); +#endif ret = esp_wifi_disconnect_internal(); -#if CONFIG_ESP_WIFI_ENABLE_ROAMING_APP - roaming_app_disable_reconnect(); -#endif return ret; } 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 186ed196e2..13d4627747 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 @@ -28,10 +28,10 @@ struct roam_config { wifi_scan_config_t scan_config; }; -void init_roaming_app(void); -void deinit_roaming_app(void); -void roaming_app_disable_reconnect(void); -void roaming_app_enable_reconnect(void); +void roam_init_app(void); +void roam_deinit_app(void); +void roam_disable_reconnect(void); +void roam_enable_reconnect(void); esp_err_t roam_get_config_params(struct roam_config *config); esp_err_t roam_set_config_params(struct roam_config *config); 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 f2d4bed0c2..80600838fd 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 @@ -54,13 +54,13 @@ 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) +void roam_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) +void roam_enable_reconnect(void) { ESP_LOGD(ROAMING_TAG, "Switching on reconnect due to application trigerred reconnect"); g_roaming_app.allow_reconnect = true; @@ -853,7 +853,7 @@ static esp_err_t init_scan_config(void) return ESP_OK; } -void init_roaming_app(void) +void roam_init_app(void) { #if !LOW_RSSI_ROAMING_ENABLED && !PERIODIC_SCAN_MONITORING ESP_LOGE(ROAMING_TAG, "No roaming trigger enabled. Roaming app cannot be initialized"); @@ -880,7 +880,7 @@ void init_roaming_app(void) ESP_LOGI(ROAMING_TAG, "Roaming app initialization done"); } -void deinit_roaming_app(void) +void roam_deinit_app(void) { #if !LOW_RSSI_ROAMING_ENABLED && !PERIODIC_SCAN_MONITORING ESP_LOGE(ROAMING_TAG, "No roaming trigger enabled. Roaming app cannot be de-initialized"); From 55742059353cc5004313a530d275d9e293de55d7 Mon Sep 17 00:00:00 2001 From: Kapil Gupta Date: Tue, 3 Dec 2024 14:01:17 +0530 Subject: [PATCH 8/9] fix(esp_wifi): Removed unnecessary handlers to cleanup --- components/esp_wifi/src/wifi_default.c | 15 +++++ .../roaming_app/include/esp_roaming.h | 2 + .../wifi_apps/roaming_app/src/roaming_app.c | 60 ++++++++++++------- .../esp_supplicant/src/esp_wpa_main.c | 3 - 4 files changed, 54 insertions(+), 26 deletions(-) diff --git a/components/esp_wifi/src/wifi_default.c b/components/esp_wifi/src/wifi_default.c index ade23eabb6..d5d07855e2 100644 --- a/components/esp_wifi/src/wifi_default.c +++ b/components/esp_wifi/src/wifi_default.c @@ -15,6 +15,9 @@ #ifdef CONFIG_ESP_WIFI_NAN_ENABLE #include "apps_private/wifi_apps_private.h" #endif +#if CONFIG_ESP_WIFI_ENABLE_ROAMING_APP +#include "esp_roaming.h" +#endif // // Purpose of this module is to provide basic wifi initialization setup for @@ -80,6 +83,12 @@ static void wifi_default_action_sta_start(void *arg, esp_event_base_t base, int3 static void wifi_default_action_sta_stop(void *arg, esp_event_base_t base, int32_t event_id, void *data) { +#ifdef CONFIG_ESP_WIFI_ENABLE_ROAMING_APP + roam_disable_reconnect(); +#ifdef CONFIG_ESP_WIFI_NETWORK_ASSISTED_ROAMING_IP_RENEW_SKIP + roaming_ongoing = false; +#endif +#endif /* CONFIG_ESP_WIFI_ENABLE_ROAMING_APP */ if (s_wifi_netifs[WIFI_IF_STA] != NULL) { esp_netif_action_stop(s_wifi_netifs[WIFI_IF_STA], base, event_id, data); } @@ -87,12 +96,15 @@ static void wifi_default_action_sta_stop(void *arg, esp_event_base_t base, int32 static void wifi_default_action_sta_connected(void *arg, esp_event_base_t base, int32_t event_id, void *data) { +#if CONFIG_ESP_WIFI_ENABLE_ROAMING_APP + roam_sta_connected(); #ifdef CONFIG_ESP_WIFI_NETWORK_ASSISTED_ROAMING_IP_RENEW_SKIP if (roaming_ongoing) { /* IP stack is already in ready state */ roaming_ongoing = false; return; } +#endif #endif if (s_wifi_netifs[WIFI_IF_STA] != NULL) { esp_err_t ret; @@ -113,6 +125,8 @@ static void wifi_default_action_sta_connected(void *arg, esp_event_base_t base, static void wifi_default_action_sta_disconnected(void *arg, esp_event_base_t base, int32_t event_id, void *data) { +#if CONFIG_ESP_WIFI_ENABLE_ROAMING_APP + roam_sta_disconnected(data); #ifdef CONFIG_ESP_WIFI_NETWORK_ASSISTED_ROAMING_IP_RENEW_SKIP wifi_event_sta_disconnected_t *disconn = data; if (disconn->reason == WIFI_REASON_ROAMING) { @@ -121,6 +135,7 @@ static void wifi_default_action_sta_disconnected(void *arg, esp_event_base_t bas return; } roaming_ongoing = false; +#endif #endif if (s_wifi_netifs[WIFI_IF_STA] != NULL) { esp_netif_action_disconnected(s_wifi_netifs[WIFI_IF_STA], base, event_id, data); 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 13d4627747..6ed20ce949 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 @@ -32,6 +32,8 @@ void roam_init_app(void); void roam_deinit_app(void); void roam_disable_reconnect(void); void roam_enable_reconnect(void); +void roam_sta_connected(void); +void roam_sta_disconnected(void *disconn); esp_err_t roam_get_config_params(struct roam_config *config); esp_err_t roam_set_config_params(struct roam_config *config); 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 80600838fd..61ffb88afb 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 @@ -54,16 +54,26 @@ static inline long time_diff_sec(struct timeval *a, struct timeval *b) return (a->tv_sec - b->tv_sec); } +static void disable_reconnect(void *ctx, void *data) +{ + ESP_LOGD(ROAMING_TAG, "Disable roaming app reconnect"); + g_roaming_app.allow_reconnect = false; +} + +static void enable_reconnect(void *ctx, void *data) +{ + ESP_LOGD(ROAMING_TAG, "Enable roaming app reconnect"); + g_roaming_app.allow_reconnect = true; +} + void roam_disable_reconnect(void) { - ESP_LOGD(ROAMING_TAG, "Switching off reconnect due to application trigerred disconnect"); - g_roaming_app.allow_reconnect = false; + eloop_register_timeout(0, 0, disable_reconnect, NULL, NULL); } void roam_enable_reconnect(void) { - ESP_LOGD(ROAMING_TAG, "Switching on reconnect due to application trigerred reconnect"); - g_roaming_app.allow_reconnect = true; + eloop_register_timeout(0, 0, enable_reconnect, NULL, NULL); } static void roaming_app_get_ap_info(wifi_ap_record_t *ap_info) @@ -154,10 +164,8 @@ static void init_periodic_scan_roam_event(void) } #endif /*PERIODIC_SCAN_ROAM_MONITORING*/ -static void roaming_app_disconnected_event_handler(void* arg, esp_event_base_t event_base, - int32_t event_id, void* event_data) +static void roaming_app_disconnected_event_handler(void *ctx, void *data) { - #if PERIODIC_RRM_MONITORING g_roaming_app.periodic_rrm_active = false; #endif /*PERIODIC_RRM_MONITORING*/ @@ -166,7 +174,7 @@ static void roaming_app_disconnected_event_handler(void* arg, esp_event_base_t e g_roaming_app.periodic_scan_active = false; #endif /*PERIODIC_SCAN_MONITORING*/ - wifi_event_sta_disconnected_t *disconn = event_data; + wifi_event_sta_disconnected_t *disconn = data; 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"); @@ -185,16 +193,10 @@ static void roaming_app_disconnected_event_handler(void* arg, esp_event_base_t e #endif /*LEGACY_ROAM_ENABLED*/ esp_wifi_connect(); } + os_free(disconn); } -static void roaming_app_sta_stop_event_handler(void* arg, esp_event_base_t event_base, - int32_t event_id, void* event_data) -{ - g_roaming_app.allow_reconnect = false; -} - -static void roaming_app_connected_event_handler(void* arg, esp_event_base_t event_base, - int32_t event_id, void* event_data) +static void roaming_app_connected_event_handler(void *ctx, void *data) { roaming_app_get_ap_info(&g_roaming_app.current_bss.ap); g_roaming_app.config.scan_config.ssid = g_roaming_app.current_bss.ap.ssid; @@ -233,6 +235,25 @@ static void roaming_app_connected_event_handler(void* arg, esp_event_base_t even #endif /*LEGACY_ROAM_ENABLED*/ g_roaming_app.allow_reconnect = true; } + +void roam_sta_connected(void) +{ + eloop_register_timeout(0, 0, roaming_app_connected_event_handler, NULL, NULL); +} + +void roam_sta_disconnected(void *data) +{ + wifi_event_sta_disconnected_t *disconn = os_malloc(sizeof(*disconn)); + + if (!disconn) { + return; + } + os_memcpy(disconn, data, sizeof(*disconn)); + if (eloop_register_timeout(0, 0, roaming_app_disconnected_event_handler, NULL, disconn) != 0) { + os_free(disconn); + } +} + #define MAX_NEIGHBOR_LEN 512 #if PERIODIC_RRM_MONITORING static char * get_btm_neighbor_list(uint8_t *report, size_t report_len) @@ -864,9 +885,6 @@ void roam_init_app(void) ESP_LOGE(ROAMING_TAG, "No roaming method enabled. Roaming app cannot be initialized"); return; #endif - ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_CONNECTED, roaming_app_connected_event_handler, NULL)); - ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, roaming_app_disconnected_event_handler, NULL)); - ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_STOP, roaming_app_sta_stop_event_handler, NULL)); #if LOW_RSSI_ROAMING_ENABLED ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_BSS_RSSI_LOW, &roaming_app_rssi_low_handler, NULL)); @@ -891,10 +909,6 @@ void roam_deinit_app(void) ESP_LOGE(ROAMING_TAG, "No roaming trigger enabled. Roaming app cannot be de-initialized"); return; #endif - /* Unregister Event handlers */ - ESP_ERROR_CHECK(esp_event_handler_unregister(WIFI_EVENT, WIFI_EVENT_STA_CONNECTED, roaming_app_connected_event_handler)); - ESP_ERROR_CHECK(esp_event_handler_unregister(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, roaming_app_disconnected_event_handler)); - ESP_ERROR_CHECK(esp_event_handler_unregister(WIFI_EVENT, WIFI_EVENT_STA_STOP, roaming_app_sta_stop_event_handler)); #if LOW_RSSI_ROAMING_ENABLED ESP_ERROR_CHECK(esp_event_handler_unregister(WIFI_EVENT, WIFI_EVENT_STA_BSS_RSSI_LOW, &roaming_app_rssi_low_handler)); 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 092c6bc3bb..24cbc34a21 100644 --- a/components/wpa_supplicant/esp_supplicant/src/esp_wpa_main.c +++ b/components/wpa_supplicant/esp_supplicant/src/esp_wpa_main.c @@ -40,9 +40,6 @@ #include "ap/sta_info.h" #include "wps/wps_defs.h" #include "wps/wps.h" -#if CONFIG_ESP_WIFI_ENABLE_ROAMING_APP -#include "esp_roaming.h" -#endif #include "rsn_supp/pmksa_cache.h" #ifdef CONFIG_DPP From d255e7e7b500b35054fbc34d3e6c1f92bc1ef7c9 Mon Sep 17 00:00:00 2001 From: Kapil Gupta Date: Mon, 3 Mar 2025 16:32:22 +0530 Subject: [PATCH 9/9] fix(esp_wifi): Fix some compilation errors in roaming app --- .../esp_wifi/wifi_apps/roaming_app/src/esp_roaming_i.h | 7 +++++++ .../esp_wifi/wifi_apps/roaming_app/src/roaming_app.c | 4 ++++ 2 files changed, 11 insertions(+) diff --git a/components/esp_wifi/wifi_apps/roaming_app/src/esp_roaming_i.h b/components/esp_wifi/wifi_apps/roaming_app/src/esp_roaming_i.h index 87400622d6..9ca515e6af 100644 --- a/components/esp_wifi/wifi_apps/roaming_app/src/esp_roaming_i.h +++ b/components/esp_wifi/wifi_apps/roaming_app/src/esp_roaming_i.h @@ -66,10 +66,17 @@ extern "C" { #endif /* Periodic RRM configuration */ +#ifdef CONFIG_ESP_WIFI_ROAMING_PERIODIC_RRM_MONITORING #define PERIODIC_RRM_MONITORING CONFIG_ESP_WIFI_ROAMING_PERIODIC_RRM_MONITORING +#else +#define PERIODIC_RRM_MONITORING 0 +#endif #if PERIODIC_RRM_MONITORING #define RRM_MONITOR_TIME CONFIG_ESP_WIFI_ROAMING_RRM_MONITOR_TIME #define RRM_MONITOR_RSSI_THRESHOLD CONFIG_ESP_WIFI_ROAMING_RRM_MONITOR_THRESHOLD +#else +#define RRM_MONITOR_TIME 232 +#define RRM_MONITOR_RSSI_THRESHOLD -100 #endif /*PERIODIC_RRM_MONITORING*/ #define MAX_SCAN_CHAN_LIST_COUNT 14 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 61ffb88afb..2612af40be 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 @@ -44,8 +44,12 @@ static void *neighbor_list_lock = NULL; static int wifi_post_roam_event(struct cand_bss *bss); static void determine_best_ap(int8_t rssi_threshold); +#if PERIODIC_RRM_MONITORING static void roaming_app_periodic_rrm_internal_handler(void *data, void *ctx); +#endif +#if PERIODIC_SCAN_MONITORING static void roaming_app_periodic_scan_internal_handler(void *data, void *ctx); +#endif static const char *ROAMING_TAG = "ROAM";