Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #292: Implement dynamic delay after Wi-Fi MIC_FAILURE connection problem #293

Merged
merged 1 commit into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 35 additions & 2 deletions src/wifi_manager_handle_msg.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "access_points_list.h"
#include "dns_server.h"
#include "json_access_points.h"
#include "time_units.h"

#define LOG_LOCAL_LEVEL LOG_LEVEL_INFO
#include "log.h"
Expand All @@ -33,6 +34,7 @@ static wifi_manager_cb_ptr g_wifi_cb_ptr_arr[MESSAGE_CODE_COUNT];
static wifi_manager_scan_info_t g_wifi_scan_info;
static uint16_t g_wifi_ap_num = MAX_AP_NUM;
static wifi_ap_record_t g_wifi_ap_records[2 * MAX_AP_NUM];
static uint32_t g_wifi_mic_failure_count;

bool g_wifi_wps_enabled;

Expand Down Expand Up @@ -255,6 +257,15 @@ wifi_handle_ev_sta_disconnected(const wifiman_msg_param_t* const p_param)
wifiman_disconnection_reason_to_str(reason),
(printf_uint_t)event_bits);

if (WIFI_REASON_MIC_FAILURE == reason)
{
g_wifi_mic_failure_count += 1;
}
else
{
g_wifi_mic_failure_count = 0;
}

/* if a DISCONNECT message is posted while a scan is in progress this scan will NEVER end, causing scan
* to never work again. For this reason SCAN_BIT is cleared too */
const bool is_connected_to_wifi = (0 != (event_bits & WIFI_MANAGER_WIFI_CONNECTED_BIT)) ? true : false;
Expand Down Expand Up @@ -293,8 +304,30 @@ wifi_handle_ev_sta_disconnected(const wifiman_msg_param_t* const p_param)
update_reason_code = UPDATE_LOST_CONNECTION;
if ((!g_wifi_wps_enabled) && (0 != (event_bits & WIFI_MANAGER_STA_ACTIVE_BIT)))
{
LOG_INFO("%s: activate reconnection after timeout", __func__);
wifi_manager_start_timer_reconnect_sta_after_timeout();
TimeUnitsSeconds_t delay_sec = WIFI_MANAGER_RECONNECT_STA_DEFAULT_TIMEOUT_SEC;
switch (g_wifi_mic_failure_count)
{
case 0:
delay_sec = WIFI_MANAGER_RECONNECT_STA_DEFAULT_TIMEOUT_SEC;
break;
case 1:
delay_sec = WIFI_MANAGER_RECONNECT_STA_AFTER_MIC_FAILURE1_TIMEOUT_SEC;
break;
case 2:
delay_sec = WIFI_MANAGER_RECONNECT_STA_AFTER_MIC_FAILURE2_TIMEOUT_SEC;
break;
case 3:
delay_sec = WIFI_MANAGER_RECONNECT_STA_AFTER_MIC_FAILURE3_TIMEOUT_SEC;
break;
case 4:
delay_sec = WIFI_MANAGER_RECONNECT_STA_AFTER_MIC_FAILURE4_TIMEOUT_SEC;
break;
default:
delay_sec = WIFI_MANAGER_RECONNECT_STA_AFTER_MIC_FAILURE5_TIMEOUT_SEC;
break;
}
LOG_INFO("%s: activate reconnection after timeout: %u seconds", __func__, (printf_uint_t)delay_sec);
wifi_manager_start_timer_reconnect_sta_after_timeout(pdMS_TO_TICKS(delay_sec * TIME_UNITS_MS_PER_SECOND));
}
}
const wifiman_wifi_ssid_t ssid = wifiman_config_sta_get_ssid();
Expand Down
7 changes: 4 additions & 3 deletions src/wifi_manager_internal.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "dns_server.h"
#include "json_access_points.h"
#include "wifiman_config.h"
#include "time_units.h"

#define LOG_LOCAL_LEVEL LOG_LEVEL_INFO
#include "log.h"
Expand Down Expand Up @@ -672,7 +673,7 @@ wifi_manager_init(
g_p_wifi_manager_timer_reconnect_sta = os_timer_one_shot_cptr_without_arg_create_static(
&g_wifi_manager_timer_reconnect_sta_mem,
"wifi:reconnect",
pdMS_TO_TICKS(1000U),
pdMS_TO_TICKS(WIFI_MANAGER_RECONNECT_STA_DEFAULT_TIMEOUT_SEC * TIME_UNITS_MS_PER_SECOND),
&wifi_manager_timer_cb_reconnect);

wifi_manager_set_callbacks(p_callbacks);
Expand Down Expand Up @@ -902,9 +903,9 @@ wifi_manger_notify_scan_done(void)
}

void
wifi_manager_start_timer_reconnect_sta_after_timeout(void)
wifi_manager_start_timer_reconnect_sta_after_timeout(const os_delta_ticks_t delay_ticks)
{
os_timer_one_shot_cptr_without_arg_start(g_p_wifi_manager_timer_reconnect_sta);
os_timer_one_shot_cptr_without_arg_restart(g_p_wifi_manager_timer_reconnect_sta, delay_ticks);
}

void
Expand Down
9 changes: 8 additions & 1 deletion src/wifi_manager_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@
extern "C" {
#endif

#define WIFI_MANAGER_RECONNECT_STA_DEFAULT_TIMEOUT_SEC (1U)
#define WIFI_MANAGER_RECONNECT_STA_AFTER_MIC_FAILURE1_TIMEOUT_SEC (1U * TIME_UNITS_SECONDS_PER_MINUTE)
#define WIFI_MANAGER_RECONNECT_STA_AFTER_MIC_FAILURE2_TIMEOUT_SEC (5U * TIME_UNITS_SECONDS_PER_MINUTE)
#define WIFI_MANAGER_RECONNECT_STA_AFTER_MIC_FAILURE3_TIMEOUT_SEC (10U * TIME_UNITS_SECONDS_PER_MINUTE)
#define WIFI_MANAGER_RECONNECT_STA_AFTER_MIC_FAILURE4_TIMEOUT_SEC (20U * TIME_UNITS_SECONDS_PER_MINUTE)
#define WIFI_MANAGER_RECONNECT_STA_AFTER_MIC_FAILURE5_TIMEOUT_SEC (30U * TIME_UNITS_SECONDS_PER_MINUTE)

/* @brief indicates that wifi_manager is working. */
#define WIFI_MANAGER_IS_WORKING ((uint32_t)(BIT0))

Expand Down Expand Up @@ -217,7 +224,7 @@ void
wifi_manger_notify_scan_done(void);

void
wifi_manager_start_timer_reconnect_sta_after_timeout(void);
wifi_manager_start_timer_reconnect_sta_after_timeout(const os_delta_ticks_t delay_ticks);

void
wifi_manager_stop_timer_reconnect_sta_after_timeout(void);
Expand Down
Loading