Skip to content

Commit

Permalink
Fix #276: Attempting to connect to Wi-Fi twice triggers task watchdog (
Browse files Browse the repository at this point in the history
…#277)

* Fix #276: Attempting to connect to Wi-Fi twice triggers task watchdog

* [#276] Set "user_disconnect" network status on DELETE /connect.json, clear network status on reconnection to saved Wi-Fi
  • Loading branch information
TheSomeMan authored Jan 9, 2024
1 parent ad10ed3 commit 3f550d8
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/http_server_handle_req.c
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,9 @@ http_server_handle_req_delete(
LOG_INFO("http_server_netconn_serve: DELETE /connect.json");
dns_server_stop();
wifi_manager_disable_wps();
wifi_manager_lock();
json_network_info_set_reason_user_disconnect();
wifi_manager_unlock();
if (wifi_manager_is_connected_to_ethernet())
{
wifi_manager_disconnect_eth();
Expand Down Expand Up @@ -436,6 +439,9 @@ http_server_handle_req_post_connect_json(const http_req_body_t http_body)
login_info.ssid.ssid_buf);
wifiman_config_sta_set_ssid_and_password(&login_info.ssid, NULL);
}
wifi_manager_lock();
json_network_info_clear();
wifi_manager_unlock();
LOG_DBG("http_server_netconn_serve: wifi_manager_connect_async() call");
wifi_manager_start_timer_reconnect_sta_after_timeout();
return http_server_resp_200_json("{}");
Expand Down
12 changes: 12 additions & 0 deletions src/json_network_info.c
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,18 @@ json_network_info_clear(void)
json_network_info_do_action_without_param(&json_network_info_do_clear);
}

static void
json_network_info_do_set_reason_user_disconnect(json_network_info_t* const p_info)
{
p_info->update_reason_code = UPDATE_USER_DISCONNECT;
}

void
json_network_info_set_reason_user_disconnect(void)
{
json_network_info_do_action_without_param(&json_network_info_do_set_reason_user_disconnect);
}

void
json_network_info_do_generate_internal(
const json_network_info_t* const p_info,
Expand Down
7 changes: 7 additions & 0 deletions src/json_network_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,13 @@ json_network_set_extra_info(const char* const p_extra);
void
json_network_info_clear(void);

/**
* @brief Updates the connection status json - sets reason to "user disconnect".
* @note This is not thread-safe and should be called only if wifi_manager_lock_json_buffer call is successful.
*/
void
json_network_info_set_reason_user_disconnect(void);

#ifdef __cplusplus
}
#endif
Expand Down
11 changes: 10 additions & 1 deletion src/wifi_manager_handle_msg.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ wifi_handle_cmd_connect_eth(void)
static void
wifi_handle_cmd_connect_sta(const wifiman_msg_param_t* const p_param)
{
xEventGroupClearBits(g_p_wifi_manager_event_group, WIFI_MANAGER_CMD_STA_CONNECT_BIT);

const EventBits_t event_bits = xEventGroupGetBits(g_p_wifi_manager_event_group);
const connection_request_made_by_code_e conn_req = wifiman_conv_param_to_conn_req(p_param);
switch (conn_req)
Expand Down Expand Up @@ -299,7 +301,14 @@ wifi_handle_ev_sta_disconnected(const wifiman_msg_param_t* const p_param)
wifi_manager_update_network_connection_info(update_reason_code, &ssid, NULL, NULL);
if (flag_need_to_reconnect)
{
wifiman_msg_send_cmd_connect_sta(CONNECTION_REQUEST_USER);
if (0 != (event_bits & WIFI_MANAGER_CMD_STA_CONNECT_BIT))
{
LOG_INFO("%s: Command to connect to STA was already sent from ORDER_DISCONNECT_STA handler", __func__);
}
else
{
wifiman_msg_send_cmd_connect_sta(CONNECTION_REQUEST_USER);
}
}
if ((!is_connected_to_wifi) && (0 == (event_bits & WIFI_MANAGER_INITIAL_CONNECTION_BIT)))
{
Expand Down
3 changes: 3 additions & 0 deletions src/wifi_manager_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ extern "C" {
/* @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))

/* @brief indicates that command to connect to STA was sent, but has not handled yet. */
#define WIFI_MANAGER_CMD_STA_CONNECT_BIT ((uint32_t)(BIT14))

#define WIFI_MANAGER_DELAY_BETWEEN_SCANNING_WIFI_CHANNELS_MS (200U)

#define WIFI_MANAGER_WIFI_COUNTRY_DEFAULT_FIRST_CHANNEL (1U)
Expand Down
1 change: 1 addition & 0 deletions src/wifiman_msg.c
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ wifiman_msg_send_cmd_connect_sta(const connection_request_made_by_code_e conn_re
.val = conn_req_code,
};
LOG_INFO("Send msg: ORDER_CONNECT_STA, conn_req_code=%d", (printf_int_t)conn_req_code);
xEventGroupSetBits(g_p_wifi_manager_event_group, WIFI_MANAGER_CMD_STA_CONNECT_BIT);
return wifiman_msg_send(ORDER_CONNECT_STA, msg_param);
}

Expand Down

0 comments on commit 3f550d8

Please sign in to comment.