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

Close #270: Enable WPS to automatically configure Wi-Fi #271

Merged
merged 5 commits into from
Dec 6, 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
2 changes: 1 addition & 1 deletion .github/workflows/sonar-scan.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ jobs:
sudo locale-gen de_DE.UTF-8

- name: Install sonar-scanner and build-wrapper
uses: SonarSource/sonarcloud-github-c-cpp@v1
uses: SonarSource/sonarcloud-github-c-cpp@v2

- name: Set IDF_PATH
run: echo "IDF_PATH=$HOME/esp/esp-idf" >> $GITHUB_ENV
Expand Down
14 changes: 14 additions & 0 deletions src/http_server_handle_req.c
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ http_server_handle_req_delete(
{
LOG_INFO("http_server_netconn_serve: DELETE /connect.json");
dns_server_stop();
wifi_manager_disable_wps();
if (wifi_manager_is_connected_to_ethernet())
{
wifi_manager_disconnect_eth();
Expand Down Expand Up @@ -454,6 +455,15 @@ http_server_handle_req_post_connect_json(const http_req_body_t http_body)
return http_server_resp_400();
}

static http_server_resp_t
http_server_handle_req_post_connect_wps(void)
{
LOG_INFO("http_server_netconn_serve: POST /connect_wps");

wifi_manager_enable_wps();
return http_server_resp_200_json("{}");
}

static http_server_resp_t
http_server_handle_req_post(
const char* p_file_name,
Expand Down Expand Up @@ -505,6 +515,10 @@ http_server_handle_req_post(
{
return http_server_handle_req_post_connect_json(http_body);
}
if (0 == strcmp(p_file_name, "connect_wps"))
{
return http_server_handle_req_post_connect_wps();
}
return wifi_manager_cb_on_http_post(p_file_name, p_uri_params, http_body, p_param->flag_access_from_lan);
}

Expand Down
18 changes: 18 additions & 0 deletions src/include/wifi_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,18 @@ wifi_manager_stop_ap(void);
void
wifi_manager_start_ap(const bool flag_block_req_from_lan);

/**
* @brief Enable WPS
*/
void
wifi_manager_enable_wps(void);

/**
* @brief Disable WPS
*/
void
wifi_manager_disable_wps(void);

/**
* @brief Stop Wi-Fi Manger
*/
Expand All @@ -111,6 +123,12 @@ wifi_manager_stop(void);
void
wifi_manager_connect_async(void);

/**
* @brief requests a connection to an access point using SSID and password got by WPS.
*/
void
wifi_manager_connect_async_by_wps(void);

/**
* @brief scan WiFi APs and return json
*/
Expand Down
11 changes: 9 additions & 2 deletions src/include/wifi_manager_defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,10 @@ typedef enum message_code_e
EVENT_AP_STA_CONNECTED = 14,
EVENT_AP_STA_DISCONNECTED = 15,
EVENT_AP_STA_IP_ASSIGNED = 16,
ORDER_TASK_WATCHDOG_FEED = 17,
MESSAGE_CODE_COUNT = 18 /* important for the callback array */
ORDER_ENABLE_WPS = 17,
ORDER_DISABLE_WPS = 18,
ORDER_TASK_WATCHDOG_FEED = 19,
MESSAGE_CODE_COUNT = 20 /* important for the callback array */
} message_code_e;

typedef void (*wifi_manager_cb_ptr)(void*);
Expand All @@ -207,6 +209,7 @@ typedef enum connection_request_made_by_code_e
CONNECTION_REQUEST_USER = 1,
CONNECTION_REQUEST_AUTO_RECONNECT = 2,
CONNECTION_REQUEST_RESTORE_CONNECTION = 3,
CONNECTION_REQUEST_WPS = 4,
CONNECTION_REQUEST_MAX = 0x7fffffff /*force the creation of this enum as a 32 bit int */
} connection_request_made_by_code_e;

Expand Down Expand Up @@ -338,6 +341,8 @@ typedef struct wifiman_config_t wifiman_config_t;
typedef void (*wifi_manager_callback_on_cmd_connect_eth_t)(void);
typedef void (*wifi_manager_callback_on_cmd_disconnect_eth_t)(void);
typedef void (*wifi_manager_callback_on_cmd_disconnect_sta_t)(void);
typedef void (*wifi_manager_callback_on_wps_started_t)(void);
typedef void (*wifi_manager_callback_on_wps_stopped_t)(void);
typedef void (*wifi_manager_callback_on_ap_started_t)(void);
typedef void (*wifi_manager_callback_on_ap_stopped_t)(void);
typedef void (*wifi_manager_callback_on_ap_sta_connected_t)(void);
Expand All @@ -355,6 +360,8 @@ typedef struct wifi_manager_callbacks_t
wifi_manager_callback_on_cmd_connect_eth_t cb_on_connect_eth_cmd;
wifi_manager_callback_on_cmd_disconnect_eth_t cb_on_disconnect_eth_cmd;
wifi_manager_callback_on_cmd_disconnect_sta_t cb_on_disconnect_sta_cmd;
wifi_manager_callback_on_wps_started_t cb_on_wps_started;
wifi_manager_callback_on_wps_stopped_t cb_on_wps_stopped;
wifi_manager_callback_on_ap_started_t cb_on_ap_started;
wifi_manager_callback_on_ap_stopped_t cb_on_ap_stopped;
wifi_manager_callback_on_ap_sta_connected_t cb_on_ap_sta_connected;
Expand Down
37 changes: 32 additions & 5 deletions src/wifi_manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ wifi_manager_set_config_sta(const wifiman_config_sta_t* const p_wifi_cfg_sta)
/* STA - Wifi Station configuration setup */
wifi_manager_netif_configure_sta();

LOG_INFO("%s: ### Configure WiFi mode: Station", __func__);
/* by default the mode is STA because wifi_manager will not start the access point unless it has to! */
const esp_err_t err = esp_wifi_set_mode(WIFI_MODE_STA);
if (ESP_OK != err)
Expand Down Expand Up @@ -218,15 +219,27 @@ wifi_manager_update_network_connection_info(
void
wifi_manager_connect_async(void)
{
/* in order to avoid a false positive on the front end app we need to quickly flush the ip json
* There'se a risk the front end sees an IP or a password error when in fact
* it's a remnant from a previous connection
*/
wifi_manager_lock();
json_network_info_clear();
wifi_manager_unlock();
LOG_INFO("%s: wifiman_msg_send_cmd_connect_sta: CONNECTION_REQUEST_USER", __func__);
wifiman_msg_send_cmd_connect_sta(CONNECTION_REQUEST_USER);
if (!wifiman_msg_send_cmd_connect_sta(CONNECTION_REQUEST_USER))
{
LOG_ERR("%s failed", "wifiman_msg_send_cmd_connect_sta");
}
}

void
wifi_manager_connect_async_by_wps(void)
{
wifi_manager_lock();
json_network_info_clear();
wifi_manager_unlock();
LOG_INFO("%s: wifiman_msg_send_cmd_connect_sta: CONNECTION_REQUEST_WPS", __func__);
if (!wifiman_msg_send_cmd_connect_sta(CONNECTION_REQUEST_WPS))
{
LOG_ERR("%s failed", "wifiman_msg_send_cmd_connect_sta");
}
}

void
Expand All @@ -243,6 +256,20 @@ wifi_manager_start_ap(const bool flag_block_req_from_lan)
wifiman_msg_send_cmd_start_ap(flag_block_req_from_lan);
}

void
wifi_manager_enable_wps(void)
{
LOG_INFO("%s", __func__);
wifiman_msg_send_cmd_enable_wps();
}

void
wifi_manager_disable_wps(void)
{
LOG_INFO("%s", __func__);
wifiman_msg_send_cmd_disable_wps();
}

void
wifi_manager_stop(void)
{
Expand Down
62 changes: 60 additions & 2 deletions src/wifi_manager_handle_msg.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* @copyright Ruuvi Innovations Ltd, license BSD-3-Clause.
*/

#include <esp_wps.h>
#include "wifi_manager_internal.h"
#include "freertos/FreeRTOS.h"
#include "freertos/event_groups.h"
Expand Down Expand Up @@ -33,6 +34,8 @@ 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];

bool g_wifi_wps_enabled;

static bool
wifi_scan_next(wifi_manager_scan_info_t* const p_scan_info)
{
Expand Down Expand Up @@ -126,6 +129,14 @@ wifi_handle_cmd_connect_sta(const wifiman_msg_param_t* const p_param)
LOG_INFO("WIFI_MANAGER:EV_STATE: Set WIFI_MANAGER_REQUEST_RESTORE_STA_BIT");
xEventGroupSetBits(g_p_wifi_manager_event_group, WIFI_MANAGER_REQUEST_RESTORE_STA_BIT);
break;
case CONNECTION_REQUEST_WPS:
LOG_INFO(
"MESSAGE: ORDER_CONNECT_STA: CONNECTION_REQUEST_WPS, event_bits=0x%04x",
(printf_uint_t)event_bits);
LOG_INFO("WIFI_MANAGER:EV_STATE: Set WIFI_MANAGER_REQUEST_STA_CONNECT_BIT");
xEventGroupClearBits(g_p_wifi_manager_event_group, WIFI_MANAGER_REQUEST_RESTORE_STA_BIT);
xEventGroupSetBits(g_p_wifi_manager_event_group, WIFI_MANAGER_REQUEST_STA_CONNECT_BIT);
break;
default:
LOG_WARN(
"MESSAGE: ORDER_CONNECT_STA: req_type=%d, event_bits=0x%04x",
Expand Down Expand Up @@ -274,8 +285,11 @@ wifi_handle_ev_sta_disconnected(const wifiman_msg_param_t* const p_param)
{
LOG_INFO("lost connection");
update_reason_code = UPDATE_LOST_CONNECTION;
LOG_INFO("%s: activate reconnection after timeout", __func__);
wifi_manager_start_timer_reconnect_sta_after_timeout();
if (!g_wifi_wps_enabled)
{
LOG_INFO("%s: activate reconnection after timeout", __func__);
wifi_manager_start_timer_reconnect_sta_after_timeout();
}
}
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 @@ -304,6 +318,7 @@ wifi_handle_cmd_start_ap(const bool flag_block_req_from_lan)
xEventGroupSetBits(
g_p_wifi_manager_event_group,
WIFI_MANAGER_AP_ACTIVE | (flag_block_req_from_lan ? WIFI_MANAGER_BLOCK_REQ_FROM_LAN_WHILE_AP_ACTIVE : 0));

wifi_callback_on_ap_started();
}

Expand All @@ -324,6 +339,43 @@ wifi_handle_cmd_stop_ap(void)
wifi_callback_on_ap_stopped();
}

static void
wifi_handle_cmd_enable_wps(void)
{
LOG_INFO("MESSAGE: ORDER_ENABLE_WPS");
esp_err_t err = esp_wifi_wps_enable(&g_wps_config);
if (ESP_OK != err)
{
LOG_ERR_ESP(err, "%s failed", "esp_wifi_wps_enable");
}
err = esp_wifi_wps_start(0);
if (ESP_OK != err)
{
LOG_ERR_ESP(err, "%s failed", "esp_wifi_wps_start");
}
else
{
g_wifi_wps_enabled = true;
wifi_callback_on_wps_started();
}
}

static void
wifi_handle_cmd_disable_wps(void)
{
LOG_INFO("MESSAGE: ORDER_DISABLE_WPS");
const esp_err_t err = esp_wifi_wps_disable();
g_wifi_wps_enabled = false;
if (ESP_OK != err)
{
LOG_ERR_ESP(err, "%s failed", "esp_wifi_wps_disable");
}
else
{
wifi_callback_on_wps_stopped();
}
}

static void
wifi_handle_ev_sta_got_ip(const wifiman_msg_param_t* const p_param)
{
Expand Down Expand Up @@ -616,6 +668,12 @@ wifi_manager_recv_and_handle_msg(void)
case EVENT_AP_STA_IP_ASSIGNED:
wifi_handle_ev_ap_sta_ip_assigned();
break;
case ORDER_ENABLE_WPS:
wifi_handle_cmd_enable_wps();
break;
case ORDER_DISABLE_WPS:
wifi_handle_cmd_disable_wps();
break;
case ORDER_TASK_WATCHDOG_FEED:
wifiman_msg_clear_flag_wdog_feed_active();
wifi_manager_wdt_task_reset();
Expand Down
Loading