Skip to content

Commit

Permalink
[#292] Disconnect from Wi-Fi after MIC_FAILURE
Browse files Browse the repository at this point in the history
  • Loading branch information
TheSomeMan committed May 28, 2024
1 parent fb1b7b2 commit 976da1b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 33 deletions.
54 changes: 27 additions & 27 deletions src/wifi_manager_handle_msg.c
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,31 @@ wifi_handle_cmd_connect_sta(const wifiman_msg_param_t* const p_param)
}
}

static void
wifi_handle_ev_sta_handle_lost_connection(const EventBits_t event_bits)
{
if ((!g_wifi_wps_enabled) && (0 != (event_bits & WIFI_MANAGER_STA_ACTIVE_BIT)))
{
TimeUnitsSeconds_t delay_sec = WIFI_MANAGER_RECONNECT_STA_DEFAULT_TIMEOUT_SEC;
if (0 == g_wifi_mic_failure_count)
{
delay_sec = WIFI_MANAGER_RECONNECT_STA_DEFAULT_TIMEOUT_SEC;
}
else
{
delay_sec = WIFI_MANAGER_RECONNECT_STA_AFTER_MIC_FAILURE_TIMEOUT_SEC;
LOG_INFO("esp_wifi_disconnect after MIC_FAILURE");
const esp_err_t err = esp_wifi_disconnect();
if (ESP_OK != err)
{
LOG_ERR_ESP(err, "%s failed", "esp_wifi_disconnect");
}
}
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));
}
}

/**
* @brief Handle event EVENT_STA_DISCONNECTED
* @note this even can be posted in numerous different conditions
Expand Down Expand Up @@ -302,33 +327,7 @@ wifi_handle_ev_sta_disconnected(const wifiman_msg_param_t* const p_param)
{
LOG_INFO("lost connection");
update_reason_code = UPDATE_LOST_CONNECTION;
if ((!g_wifi_wps_enabled) && (0 != (event_bits & WIFI_MANAGER_STA_ACTIVE_BIT)))
{
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));
}
wifi_handle_ev_sta_handle_lost_connection(event_bits);
}
const wifiman_wifi_ssid_t ssid = wifiman_config_sta_get_ssid();
wifi_manager_update_network_connection_info(update_reason_code, &ssid, NULL, NULL);
Expand Down Expand Up @@ -528,6 +527,7 @@ wifi_handle_cmd_disconnect_sta(void)
(printf_uint_t)event_bits);

xEventGroupClearBits(g_p_wifi_manager_event_group, WIFI_MANAGER_STA_ACTIVE_BIT);
LOG_INFO("esp_wifi_disconnect");
const esp_err_t err = esp_wifi_disconnect();
if (ESP_OK != err)
{
Expand Down
8 changes: 2 additions & 6 deletions src/wifi_manager_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,8 @@
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)
#define WIFI_MANAGER_RECONNECT_STA_DEFAULT_TIMEOUT_SEC (1U)
#define WIFI_MANAGER_RECONNECT_STA_AFTER_MIC_FAILURE_TIMEOUT_SEC (70U)

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

0 comments on commit 976da1b

Please sign in to comment.