From 66429b1ba7d1ead4e9c4c5c1eb88a69ddadb3343 Mon Sep 17 00:00:00 2001 From: "tarun.kumar" Date: Wed, 4 Feb 2026 14:24:07 +0530 Subject: [PATCH] fix(wifi) : Reschedule roam in based on backoff time --- .../wifi_apps/roaming_app/src/esp_roaming_i.h | 1 + .../wifi_apps/roaming_app/src/roaming_app.c | 28 +++++++++++++++++-- 2 files changed, 26 insertions(+), 3 deletions(-) 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 1a17f431401..71e589cb40d 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 @@ -146,6 +146,7 @@ struct roaming_app { uint8_t bssid_blacklist_count; #endif bool allow_reconnect; + struct cand_bss *pending_roam_bss; }; #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 c35d29b6948..e55552a1aff 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 @@ -1481,10 +1481,25 @@ void roaming_app_trigger_roam(struct cand_bss *bss) gettimeofday(&now, NULL); ESP_LOGD(ROAMING_TAG, "Processing trigger roaming request."); + if (g_roaming_app.pending_roam_bss) { + eloop_cancel_timeout(roaming_app_trigger_roam_internal_handler, NULL, g_roaming_app.pending_roam_bss); + os_free(g_roaming_app.pending_roam_bss); + g_roaming_app.pending_roam_bss = NULL; + } if (g_roaming_app.sta_connected && roaming_app_roam_backoff_active(&now, 0)) { - ESP_LOGD(ROAMING_TAG, "Ignoring request as time difference to last roam attempt is %ld", - time_diff_sec(&now, &g_roaming_app.last_roam_attempt_time)); - goto free_bss; + const struct timeval *anchor = roaming_app_get_backoff_anchor(); + long elapsed_sec = time_diff_sec(&now, anchor); + long remaining_sec = (long)g_roaming_app.config.backoff_time - elapsed_sec; + ESP_LOGD(ROAMING_TAG, + "Deferring roam during backoff (elapsed %ld s since anchor), reschedule in %ld s", + elapsed_sec, remaining_sec); + if (eloop_register_timeout((unsigned int)remaining_sec, 0, + roaming_app_trigger_roam_internal_handler, NULL, (void *)bss)) { + ESP_LOGE(ROAMING_TAG, "Could not register roaming event."); + goto free_bss; + } + g_roaming_app.pending_roam_bss = bss; + return; } #if NETWORK_ASSISTED_ROAMING_ENABLED if (g_roaming_app.sta_connected && g_roaming_app.config.btm_roaming_enabled && g_roaming_app.current_bss.btm_support) { @@ -1532,6 +1547,9 @@ static void roaming_app_trigger_roam_internal_handler(void *ctx, void *data) if (!data) { ESP_LOGE(ROAMING_TAG, "No data received for roaming event"); } else { + if (g_roaming_app.pending_roam_bss == data) { + g_roaming_app.pending_roam_bss = NULL; + } roaming_app_trigger_roam((struct cand_bss *)data); } } @@ -2158,6 +2176,10 @@ static void roaming_app_cancel_pending_events(void) eloop_cancel_timeout(roaming_app_connected_event_handler, ELOOP_ALL_CTX, ELOOP_ALL_CTX); eloop_cancel_timeout(roaming_app_disconnected_event_handler, ELOOP_ALL_CTX, ELOOP_ALL_CTX); eloop_cancel_timeout(roaming_app_trigger_roam_internal_handler, ELOOP_ALL_CTX, ELOOP_ALL_CTX); + if (g_roaming_app.pending_roam_bss) { + os_free(g_roaming_app.pending_roam_bss); + g_roaming_app.pending_roam_bss = NULL; + } eloop_cancel_timeout(enable_reconnect, ELOOP_ALL_CTX, ELOOP_ALL_CTX); eloop_cancel_timeout(disable_reconnect, ELOOP_ALL_CTX, ELOOP_ALL_CTX); #if LOW_RSSI_ROAMING_ENABLED