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 #274: Sometimes gateway initiates reconnection to Wi-Fi after successful connection during configuration #275

Merged
merged 2 commits into from
Dec 18, 2023
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
3 changes: 3 additions & 0 deletions src/include/wifi_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,9 @@ wifi_manager_is_working(void);
bool
wifi_manager_is_ap_active(void);

bool
wifi_manager_is_sta_active(void);

bool
wifi_manager_is_req_from_lan_blocked_while_ap_is_active(void);

Expand Down
7 changes: 7 additions & 0 deletions src/wifi_manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ wifi_manager_update_network_connection_info(
{
LOG_INFO("WIFI_MANAGER:EV_STATE: Set WIFI_MANAGER_WIFI_CONNECTED_BIT");
xEventGroupSetBits(g_p_wifi_manager_event_group, WIFI_MANAGER_WIFI_CONNECTED_BIT);
wifi_manager_stop_timer_reconnect_sta_after_timeout();
}
if (NULL != p_ip_info)
{
Expand Down Expand Up @@ -376,6 +377,12 @@ wifi_manager_is_ap_active(void)
return (0 != (xEventGroupGetBits(g_p_wifi_manager_event_group) & WIFI_MANAGER_AP_ACTIVE));
}

bool
wifi_manager_is_sta_active(void)
{
return (0 != (xEventGroupGetBits(g_p_wifi_manager_event_group) & WIFI_MANAGER_STA_ACTIVE_BIT));
}

bool
wifi_manager_is_req_from_lan_blocked_while_ap_is_active(void)
{
Expand Down
34 changes: 17 additions & 17 deletions src/wifi_manager_handle_msg.c
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ wifi_handle_cmd_connect_sta(const wifiman_msg_param_t* const p_param)
break;
}

if (0 != (event_bits & WIFI_MANAGER_WIFI_CONNECTED_BIT))
if (0 != (event_bits & WIFI_MANAGER_STA_ACTIVE_BIT))
{
LOG_WARN("%s: Already connected to WiFi, first need to disconnect, then reconnect", __func__);
LOG_INFO("WIFI_MANAGER:EV_STATE: Set WIFI_MANAGER_REQUEST_RESTORE_STA_BIT");
Expand All @@ -156,6 +156,7 @@ wifi_handle_cmd_connect_sta(const wifiman_msg_param_t* const p_param)
{
LOG_INFO("WIFI_MANAGER:EV_STATE: Clear WIFI_MANAGER_REQUEST_DISCONNECT_BIT");
xEventGroupClearBits(g_p_wifi_manager_event_group, WIFI_MANAGER_REQUEST_DISCONNECT_BIT);
xEventGroupSetBits(g_p_wifi_manager_event_group, WIFI_MANAGER_STA_ACTIVE_BIT);

/* update config to latest and attempt connection */
wifi_config_t wifi_config = {
Expand Down Expand Up @@ -240,8 +241,11 @@ wifi_handle_cmd_connect_sta(const wifiman_msg_param_t* const p_param)
static bool
wifi_handle_ev_sta_disconnected(const wifiman_msg_param_t* const p_param)
{
const wifiman_disconnection_reason_t reason = wifiman_conv_param_to_reason(p_param);
const EventBits_t event_bits = xEventGroupClearBits(g_p_wifi_manager_event_group, WIFI_MANAGER_SCAN_BIT);
const wifiman_disconnection_reason_t reason = wifiman_conv_param_to_reason(p_param);
const EventBits_t event_bits = xEventGroupClearBits(
g_p_wifi_manager_event_group,
WIFI_MANAGER_SCAN_BIT | WIFI_MANAGER_INITIAL_CONNECTION_BIT | WIFI_MANAGER_STA_ACTIVE_BIT
| WIFI_MANAGER_WIFI_CONNECTED_BIT);

LOG_INFO(
"MESSAGE: EVENT_STA_DISCONNECTED with Reason code: %d (%s), event_bits=0x%04x",
Expand Down Expand Up @@ -285,7 +289,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)
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();
Expand All @@ -297,7 +301,7 @@ wifi_handle_ev_sta_disconnected(const wifiman_msg_param_t* const p_param)
{
wifiman_msg_send_cmd_connect_sta(CONNECTION_REQUEST_USER);
}
if (!is_connected_to_wifi)
if ((!is_connected_to_wifi) && (0 == (event_bits & WIFI_MANAGER_INITIAL_CONNECTION_BIT)))
{
return false;
}
Expand Down Expand Up @@ -425,8 +429,10 @@ static void
wifi_handle_ev_ap_sta_connected(void)
{
LOG_INFO("MESSAGE: EVENT_AP_STA_CONNECTED");
LOG_INFO("WIFI_MANAGER:EV_STATE: Clear WIFI_MANAGER_AP_STA_IP_ASSIGNED_BIT");
xEventGroupClearBits(g_p_wifi_manager_event_group, WIFI_MANAGER_AP_STA_IP_ASSIGNED_BIT);
LOG_INFO("WIFI_MANAGER:EV_STATE: Clear WIFI_MANAGER_AP_STA_IP_ASSIGNED_BIT | WIFI_MANAGER_INITIAL_CONNECTION_BIT");
xEventGroupClearBits(
g_p_wifi_manager_event_group,
WIFI_MANAGER_AP_STA_IP_ASSIGNED_BIT | WIFI_MANAGER_INITIAL_CONNECTION_BIT);
LOG_INFO("WIFI_MANAGER:EV_STATE: Set WIFI_MANAGER_AP_STA_CONNECTED_BIT");
xEventGroupSetBits(g_p_wifi_manager_event_group, WIFI_MANAGER_AP_STA_CONNECTED_BIT);
wifi_callback_on_ap_sta_connected();
Expand Down Expand Up @@ -479,24 +485,18 @@ wifi_handle_cmd_disconnect_sta(void)
"WIFI_MANAGER:EV_STATE: Set WIFI_MANAGER_REQUEST_DISCONNECT_BIT, event_bits=0x%04x",
(printf_uint_t)event_bits);

const bool is_connected_to_wifi = (0 != (event_bits & WIFI_MANAGER_WIFI_CONNECTED_BIT)) ? true : false;

xEventGroupClearBits(g_p_wifi_manager_event_group, WIFI_MANAGER_STA_ACTIVE_BIT);
const esp_err_t err = esp_wifi_disconnect();
if (ESP_OK != err)
{
LOG_ERR_ESP(err, "%s failed", "esp_wifi_disconnect");
}
wifi_callback_on_disconnect_sta_cmd();

if (!is_connected_to_wifi)
xEventGroupClearBits(g_p_wifi_manager_event_group, WIFI_MANAGER_REQUEST_DISCONNECT_BIT);
if (0 != (event_bits & WIFI_MANAGER_REQUEST_STA_CONNECT_BIT))
{
LOG_INFO("Got a command to disconnect from WiFi AP, but we are not currently connected");
LOG_INFO("WIFI_MANAGER:EV_STATE: Clear WIFI_MANAGER_REQUEST_DISCONNECT_BIT");
xEventGroupClearBits(g_p_wifi_manager_event_group, WIFI_MANAGER_REQUEST_DISCONNECT_BIT);
if (0 != (event_bits & WIFI_MANAGER_REQUEST_STA_CONNECT_BIT))
{
wifiman_msg_send_cmd_connect_sta(CONNECTION_REQUEST_USER);
}
wifiman_msg_send_cmd_connect_sta(CONNECTION_REQUEST_USER);
}
}

Expand Down
1 change: 1 addition & 0 deletions src/wifi_manager_internal.c
Original file line number Diff line number Diff line change
Expand Up @@ -702,6 +702,7 @@ wifi_manager_init(

if (flag_connect_sta)
{
xEventGroupSetBits(g_p_wifi_manager_event_group, WIFI_MANAGER_INITIAL_CONNECTION_BIT);
wifi_manager_reconnect_sta();
}
return true;
Expand Down
6 changes: 6 additions & 0 deletions src/wifi_manager_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ extern "C" {
/* @brief Block requests from LAN while WiFi access point is active. */
#define WIFI_MANAGER_BLOCK_REQ_FROM_LAN_WHILE_AP_ACTIVE ((uint32_t)(BIT11))

/* @brief indicates that the ESP32 is working as Wi-Fi station and trying to connect or connected to a hotspot. */
#define WIFI_MANAGER_STA_ACTIVE_BIT ((uint32_t)(BIT12))

/* @brief indicates that the ESP32 is trying to connect to a hotspot for the first time. */
#define WIFI_MANAGER_INITIAL_CONNECTION_BIT ((uint32_t)(BIT13))

#define WIFI_MANAGER_DELAY_BETWEEN_SCANNING_WIFI_CHANNELS_MS (200U)

#define WIFI_MANAGER_WIFI_COUNTRY_DEFAULT_FIRST_CHANNEL (1U)
Expand Down