Fix AP mode

This commit is contained in:
2026-01-14 20:56:04 -05:00
parent 62bdc8588e
commit 579592d746

View File

@@ -1,8 +1,8 @@
#include <cstring> #include <cstring>
#include "wireless/WifiManager.h"
#include "ConfigManager.h" #include "ConfigManager.h"
#include "constants/wifi.h" #include "constants/wifi.h"
#include "wireless/WifiManager.h"
#define TAG "WifiManager" #define TAG "WifiManager"
#define SOFTAP_SCAN_FREQUENCY_MS 30000 #define SOFTAP_SCAN_FREQUENCY_MS 30000
@@ -35,41 +35,43 @@ int WifiManager::disconnect() {
// Wifi state machine // 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"); 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..."); 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"); 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: case wifi_state::broadcasting:
ESP_LOGI(TAG, "Broadcasting in softap mode"); 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 vTaskDelay(SOFTAP_SCAN_FREQUENCY_MS /
handle_broadcasting(); // scans for known networks portTICK_PERIOD_MS); // only scan every 30 seconds, as we may
break; // disconnect users
case wifi_state::disconnect: handle_broadcasting(); // scans for known networks
ESP_LOGI(TAG, "Shutting down wifi (will restart)"); break;
handle_disconnect(); case wifi_state::disconnect:
update_state(wifi_state::disconnected); ESP_LOGI(TAG, "Shutting down wifi (will restart)");
break; handle_disconnect();
case wifi_state::disconnected: update_state(wifi_state::disconnected);
ESP_LOGI(TAG, "Disconnected from wifi, starting back up"); break;
update_state(wifi_state::connect); case wifi_state::disconnected:
break; ESP_LOGI(TAG, "Disconnected from wifi, starting back up");
case wifi_state::shutdown: update_state(wifi_state::connect);
ESP_LOGI(TAG, "Shutting down wifi (will not automatically restart)"); break;
handle_disconnect(); case wifi_state::shutdown:
update_state(wifi_state::disabled); ESP_LOGI(TAG, "Shutting down wifi (will not automatically restart)");
default: handle_disconnect();
vTaskSuspend(nullptr); update_state(wifi_state::disabled);
break; default:
vTaskSuspend(nullptr);
break;
} }
vTaskDelay(1000 / portTICK_PERIOD_MS); vTaskDelay(1000 / portTICK_PERIOD_MS);
@@ -85,7 +87,8 @@ int WifiManager::init_connection() {
this->handle_disconnect(); this->handle_disconnect();
} }
this->m_netif = esp_netif_create_default_wifi_sta(); // Must be destroyed with esp_netif_destroy_default_wifi() this->m_netif = esp_netif_create_default_wifi_sta(); // Must be destroyed with
// esp_netif_destroy_default_wifi()
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
esp_wifi_init(&cfg); esp_wifi_init(&cfg);
@@ -94,10 +97,7 @@ int WifiManager::init_connection() {
esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, wifi_event_handler, this); esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, wifi_event_handler, this);
wifi_config_t wifi_configuration; wifi_config_t wifi_configuration;
wifi_configuration = { wifi_configuration = {.sta = {}};
.sta = {
}
};
std::string ssid = m_config_manager.get_wifi_ssid(); std::string ssid = m_config_manager.get_wifi_ssid();
std::string pass = m_config_manager.get_wifi_password(); std::string pass = m_config_manager.get_wifi_password();
@@ -146,7 +146,7 @@ int WifiManager::handle_disconnect() {
int WifiManager::handle_broadcasting() { int WifiManager::handle_broadcasting() {
ESP_LOGI(TAG, "In softap mode, scanning for known networks"); ESP_LOGI(TAG, "In softap mode, scanning for known networks");
wifi_scan_config_t scan_config {}; wifi_scan_config_t scan_config{};
scan_config.ssid = reinterpret_cast<uint8_t *>(m_config_manager.get_wifi_ssid().data()); scan_config.ssid = reinterpret_cast<uint8_t *>(m_config_manager.get_wifi_ssid().data());
scan_config.scan_time = {.passive = 500}; scan_config.scan_time = {.passive = 500};
@@ -187,17 +187,18 @@ int WifiManager::init_softap() {
esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, wifi_event_handler, this); esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, wifi_event_handler, this);
wifi_config_t wifi_configuration = { wifi_config_t wifi_configuration = {
.ap = { .ap =
.ssid = WIFI_SSID, {
.password = "", .ssid = WIFI_SSID,
.channel = SOFTAP_CHANNEL, .password = "",
.authmode = WIFI_AUTH_OPEN, .channel = SOFTAP_CHANNEL,
.ssid_hidden = 0, .authmode = WIFI_AUTH_OPEN,
.max_connection = SOFTAP_MAX_CONNECTIONS, .ssid_hidden = 0,
.beacon_interval = 100, .max_connection = SOFTAP_MAX_CONNECTIONS,
.csa_count = 3, .beacon_interval = 100,
.dtim_period = 1, .csa_count = 3,
}, .dtim_period = 1,
},
}; };
esp_wifi_set_mode(WIFI_MODE_APSTA); // enable both ap and station mode for scanning esp_wifi_set_mode(WIFI_MODE_APSTA); // enable both ap and station mode for scanning
@@ -205,7 +206,7 @@ int WifiManager::init_softap() {
esp_wifi_start(); esp_wifi_start();
esp_netif_set_default_netif(esp_netif_get_handle_from_ifkey("WIFI_STA_DEF")); esp_netif_set_default_netif(esp_netif_get_handle_from_ifkey("WIFI_AP_DEF"));
return 0; return 0;
} }
@@ -217,8 +218,8 @@ void WifiManager::update_state(const wifi_state state) {
xSemaphoreGive(this->m_mutex); xSemaphoreGive(this->m_mutex);
} }
void WifiManager::wifi_event_handler(void *event_handler_arg, esp_event_base_t event_base, const int32_t event_id, void *event_data) { void WifiManager::wifi_event_handler(void *event_handler_arg, esp_event_base_t event_base,
// Passed in as a parameter since c (freertos) cannot call the member function directly. const int32_t event_id, void *event_data) {
const auto that = static_cast<WifiManager *>(event_handler_arg); const auto that = static_cast<WifiManager *>(event_handler_arg);
if (WIFI_EVENT_STA_START == event_id) { if (WIFI_EVENT_STA_START == event_id) {