mirror of
https://github.com/BotChain-Robots/firmware.git
synced 2026-07-08 17:47:21 +02:00
Occasionally scan for known networks when in softap mode
This commit is contained in:
@@ -6,6 +6,8 @@
|
|||||||
#include "mDNSDiscoveryService.h"
|
#include "mDNSDiscoveryService.h"
|
||||||
|
|
||||||
#define TAG "WifiManager"
|
#define TAG "WifiManager"
|
||||||
|
#define SOFTAP_SCAN_FREQUENCY_MS 30000
|
||||||
|
#define NUM_CONNECT_ATTEMPTS 5
|
||||||
|
|
||||||
WifiManager::~WifiManager() {
|
WifiManager::~WifiManager() {
|
||||||
this->handle_disconnect();
|
this->handle_disconnect();
|
||||||
@@ -32,26 +34,36 @@ int WifiManager::disconnect() {
|
|||||||
const auto state = this->m_state;
|
const auto state = this->m_state;
|
||||||
xSemaphoreGive(this->m_mutex);
|
xSemaphoreGive(this->m_mutex);
|
||||||
|
|
||||||
|
// Wifi state machine
|
||||||
switch (state) {
|
switch (state) {
|
||||||
case wifi_state::connect:
|
case wifi_state::connect:
|
||||||
ESP_LOGI(TAG, "Attempting to connect to wifi in station mode\n");
|
ESP_LOGI(TAG, "Attempting to connect to wifi in station mode");
|
||||||
init_connection();
|
init_connection();
|
||||||
update_state(wifi_state::connecting);
|
update_state(wifi_state::connecting);
|
||||||
break;
|
break;
|
||||||
case wifi_state::connecting:
|
case wifi_state::connecting:
|
||||||
ESP_LOGI(TAG, "connecting...\n");
|
ESP_LOGI(TAG, "connecting...");
|
||||||
handle_connecting();
|
handle_connecting();
|
||||||
break;
|
break;
|
||||||
case wifi_state::broadcast:
|
case wifi_state::broadcast:
|
||||||
ESP_LOGI(TAG, "Attempting to broadcast in softap mode\n");
|
ESP_LOGI(TAG, "Attempting to broadcast in softap mode");
|
||||||
init_softap();
|
init_softap();
|
||||||
update_state(wifi_state::broadcasting);
|
update_state(wifi_state::broadcasting);
|
||||||
break;
|
break;
|
||||||
|
case wifi_state::broadcasting:
|
||||||
|
ESP_LOGI(TAG, "Broadcasting in softap mode");
|
||||||
|
vTaskDelay(SOFTAP_SCAN_FREQUENCY_MS / portTICK_PERIOD_MS); // only scan every 30 seconds, as we may disconnect users
|
||||||
|
handle_broadcasting(); // scans for known networks
|
||||||
|
break;
|
||||||
case wifi_state::disconnect:
|
case wifi_state::disconnect:
|
||||||
ESP_LOGI(TAG, "Shutting down wifi\n");
|
ESP_LOGI(TAG, "Shutting down wifi");
|
||||||
handle_disconnect();
|
handle_disconnect();
|
||||||
update_state(wifi_state::disconnected);
|
update_state(wifi_state::disconnected);
|
||||||
break;
|
break;
|
||||||
|
case wifi_state::disconnected:
|
||||||
|
ESP_LOGI(TAG, "Disconnected from wifi, starting back up");
|
||||||
|
update_state(wifi_state::connect);
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
vTaskSuspend(nullptr);
|
vTaskSuspend(nullptr);
|
||||||
break;
|
break;
|
||||||
@@ -101,7 +113,7 @@ int WifiManager::init_connection() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int WifiManager::handle_connecting() {
|
int WifiManager::handle_connecting() {
|
||||||
if (this->m_attempts > 10) {
|
if (this->m_attempts > NUM_CONNECT_ATTEMPTS) {
|
||||||
handle_disconnect();
|
handle_disconnect();
|
||||||
update_state(wifi_state::broadcast);
|
update_state(wifi_state::broadcast);
|
||||||
}
|
}
|
||||||
@@ -121,10 +133,42 @@ int WifiManager::handle_disconnect() {
|
|||||||
esp_wifi_scan_stop();
|
esp_wifi_scan_stop();
|
||||||
esp_wifi_disconnect();
|
esp_wifi_disconnect();
|
||||||
esp_wifi_stop();
|
esp_wifi_stop();
|
||||||
|
esp_wifi_clear_ap_list();
|
||||||
|
esp_wifi_deinit();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int WifiManager::handle_broadcasting() {
|
||||||
|
ESP_LOGI(TAG, "In softap mode, scanning for known networks");
|
||||||
|
wifi_scan_config_t scan_config {};
|
||||||
|
scan_config.ssid = reinterpret_cast<uint8_t *>(m_config_manager.get_wifi_ssid().data());
|
||||||
|
scan_config.scan_time = {.passive = 500};
|
||||||
|
|
||||||
|
if (const auto err = esp_wifi_scan_start(&scan_config, false); ESP_OK != err) {
|
||||||
|
ESP_LOGE(TAG, "Failed to scan for wifi networks, err: %d", err);
|
||||||
|
esp_wifi_clear_ap_list(); // must call to free memory allocated by scan.
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
||||||
|
|
||||||
|
uint16_t found_aps = 0;
|
||||||
|
if (const auto err = esp_wifi_scan_get_ap_num(&found_aps); ESP_OK != err) {
|
||||||
|
ESP_LOGE(TAG, "Failed to get count of scanned aps");
|
||||||
|
esp_wifi_clear_ap_list();
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (found_aps > 1) {
|
||||||
|
ESP_LOGI(TAG, "Found a known network, switching to station mode");
|
||||||
|
update_state(wifi_state::disconnect);
|
||||||
|
}
|
||||||
|
|
||||||
|
esp_wifi_clear_ap_list(); // must call to free memory allocated by scan.
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
int WifiManager::init_softap() {
|
int WifiManager::init_softap() {
|
||||||
if (nullptr != this->m_netif) {
|
if (nullptr != this->m_netif) {
|
||||||
this->handle_disconnect();
|
this->handle_disconnect();
|
||||||
@@ -151,7 +195,7 @@ int WifiManager::init_softap() {
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
esp_wifi_set_mode(WIFI_MODE_AP);
|
esp_wifi_set_mode(WIFI_MODE_APSTA); // enable both ap and station mode for scanning
|
||||||
esp_wifi_set_config(static_cast<wifi_interface_t>(ESP_IF_WIFI_AP), &wifi_configuration);
|
esp_wifi_set_config(static_cast<wifi_interface_t>(ESP_IF_WIFI_AP), &wifi_configuration);
|
||||||
|
|
||||||
esp_wifi_start();
|
esp_wifi_start();
|
||||||
@@ -159,7 +203,7 @@ int WifiManager::init_softap() {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void WifiManager::update_state(wifi_state state) {
|
void WifiManager::update_state(const wifi_state state) {
|
||||||
xSemaphoreTake(this->m_mutex, portMAX_DELAY);
|
xSemaphoreTake(this->m_mutex, portMAX_DELAY);
|
||||||
this->m_attempts = 0;
|
this->m_attempts = 0;
|
||||||
this->m_state = state;
|
this->m_state = state;
|
||||||
@@ -182,7 +226,6 @@ void WifiManager::wifi_event_handler(void *event_handler_arg, esp_event_base_t e
|
|||||||
if (that->m_state == wifi_state::connected) {
|
if (that->m_state == wifi_state::connected) {
|
||||||
xSemaphoreGive(that->m_mutex);
|
xSemaphoreGive(that->m_mutex);
|
||||||
that->handle_disconnect();
|
that->handle_disconnect();
|
||||||
that->update_state(wifi_state::connect);
|
|
||||||
} else {
|
} else {
|
||||||
xSemaphoreGive(that->m_mutex);
|
xSemaphoreGive(that->m_mutex);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -37,6 +37,7 @@ private:
|
|||||||
int init_connection();
|
int init_connection();
|
||||||
int handle_connecting();
|
int handle_connecting();
|
||||||
int handle_disconnect();
|
int handle_disconnect();
|
||||||
|
int handle_broadcasting();
|
||||||
int init_softap();
|
int init_softap();
|
||||||
|
|
||||||
static void wifi_event_handler(void *event_handler_arg, esp_event_base_t event_base, int32_t event_id, void *event_data);
|
static void wifi_event_handler(void *event_handler_arg, esp_event_base_t event_base, int32_t event_id, void *event_data);
|
||||||
|
|||||||
Reference in New Issue
Block a user