This commit is contained in:
2025-05-26 21:20:01 -04:00
parent 7396290467
commit d79d99ed80
5 changed files with 89 additions and 18 deletions

View File

@@ -0,0 +1 @@
idf_component_register(INCLUDE_DIRS "include")

View File

@@ -0,0 +1,12 @@
//
// Created by Johnathon Slightham on 2025-05-26.
//
#ifndef WIFI_H
#define WIFI_H
#define WIFI_SSID "botchain"
#define SOFTAP_CHANNEL 1
#define SOFTAP_MAX_CONNECTIONS 10
#endif //WIFI_H

View File

@@ -1,3 +1,3 @@
idf_component_register(SRCS "rpc.cpp" "WifiManager.cpp" "mDNSDiscoveryService.cpp" idf_component_register(SRCS "rpc.cpp" "WifiManager.cpp" "mDNSDiscoveryService.cpp"
PRIV_REQUIRES driver esp_event nvs_flash esp_netif esp_wifi espressif__mdns PRIV_REQUIRES driver esp_event nvs_flash esp_netif esp_wifi espressif__mdns constants
INCLUDE_DIRS "include") INCLUDE_DIRS "include")

View File

@@ -3,24 +3,32 @@
#include <esp_netif.h> #include <esp_netif.h>
#include <esp_event.h> #include <esp_event.h>
#include <freertos/semphr.h> #include <freertos/semphr.h>
#include <cstring>
#include "freertos/FreeRTOS.h" #include "freertos/FreeRTOS.h"
#include "freertos/semphr.h" #include "freertos/semphr.h"
#include "esp_wifi.h" #include "esp_wifi.h"
#include "constants/wifi.h"
WifiManager::WifiManager() { WifiManager::WifiManager() {
esp_netif_init(); esp_netif_init();
esp_event_loop_create_default(); esp_event_loop_create_default();
esp_netif_create_default_wifi_sta();
this->m_mutex = xSemaphoreCreateMutex(); this->m_mutex = xSemaphoreCreateMutex();
this->m_state = wifi_state::disconnected; this->m_state = wifi_state::disconnected;
this->m_attempts = 0; this->m_attempts = 0;
this->m_task = nullptr; // suppress warning this->m_task = nullptr;
this->m_netif = nullptr;
xTaskCreate(reinterpret_cast<TaskFunction_t>(s_manage), "wifi_task", 4096, this, 5, &m_task); xTaskCreate(reinterpret_cast<TaskFunction_t>(s_manage), "wifi_task", 4096, this, 5, &m_task);
} }
WifiManager::~WifiManager() {
this->handle_disconnect();
vTaskDelete(this->m_task);
vSemaphoreDelete(this->m_mutex);
}
int WifiManager::connect() { int WifiManager::connect() {
this->update_state(wifi_state::connect); this->update_state(wifi_state::connect);
vTaskResume(this->m_task); vTaskResume(this->m_task);
@@ -42,16 +50,21 @@ int WifiManager::disconnect() {
switch (state) { switch (state) {
case wifi_state::connect: case wifi_state::connect:
printf("Attempting to connect to wifi in station mode\n");
init_connection(); init_connection();
update_state(wifi_state::connecting); update_state(wifi_state::connecting);
break; break;
case wifi_state::connecting: case wifi_state::connecting:
printf("connecting...\n");
handle_connecting(); handle_connecting();
break; break;
case wifi_state::searching: case wifi_state::broadcast:
handle_search(); printf("Attempting to broadcast in softap mode\n");
init_softap();
update_state(wifi_state::broadcasting);
break; break;
case wifi_state::disconnect: case wifi_state::disconnect:
printf("Shutting down wifi\n");
handle_disconnect(); handle_disconnect();
update_state(wifi_state::disconnected); update_state(wifi_state::disconnected);
break; break;
@@ -69,6 +82,12 @@ int WifiManager::disconnect() {
} }
int WifiManager::init_connection() { int WifiManager::init_connection() {
if (nullptr != this->m_netif) {
this->handle_disconnect();
}
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,15 +113,20 @@ int WifiManager::init_connection() {
} }
int WifiManager::handle_connecting() { int WifiManager::handle_connecting() {
if (m_attempts > 10) { if (this->m_attempts > 10) {
handle_disconnect(); handle_disconnect();
update_state(wifi_state::searching); update_state(wifi_state::broadcast);
} }
return 0; return 0;
} }
int WifiManager::handle_disconnect() { int WifiManager::handle_disconnect() {
if (nullptr != this->m_netif) {
esp_netif_destroy_default_wifi(this->m_netif);
this->m_netif = nullptr;
}
esp_event_handler_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, wifi_event_handler); esp_event_handler_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, wifi_event_handler);
esp_event_handler_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, wifi_event_handler); esp_event_handler_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, wifi_event_handler);
@@ -113,10 +137,37 @@ int WifiManager::handle_disconnect() {
return 0; return 0;
} }
int WifiManager::handle_search() { int WifiManager::init_softap() {
// todo: softap if (nullptr != this->m_netif) {
this->handle_disconnect();
}
this->m_netif = esp_netif_create_default_wifi_ap();
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
esp_wifi_init(&cfg);
esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, wifi_event_handler, this);
wifi_config_t wifi_configuration = {
.ap = {
.ssid = WIFI_SSID,
.password = "",
.channel = SOFTAP_CHANNEL,
.authmode = WIFI_AUTH_OPEN,
.ssid_hidden = 0,
.max_connection = SOFTAP_MAX_CONNECTIONS,
.beacon_interval = 100,
.csa_count = 3,
.dtim_period = 1,
},
};
esp_wifi_set_mode(WIFI_MODE_AP);
esp_wifi_set_config(static_cast<wifi_interface_t>(ESP_IF_WIFI_AP), &wifi_configuration);
esp_wifi_start();
printf("Waiting for configuration");
return 0; return 0;
} }
@@ -127,17 +178,17 @@ void WifiManager::update_state(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, int32_t event_id, void *event_data) { void WifiManager::wifi_event_handler(void *event_handler_arg, esp_event_base_t event_base, const int32_t event_id, void *event_data) {
// Passed in as a parameter since c (freertos) cannot call the member function directly. // Passed in as a parameter since c (freertos) cannot call the member function directly.
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) {
printf("Connecting to wifi\n"); printf("Station mode started\n");
} else if (WIFI_EVENT_STA_CONNECTED == event_id) { } else if (WIFI_EVENT_STA_CONNECTED == event_id) {
printf("Connected to wifi\n"); printf("Connected to wifi in station mode\n");
that->update_state(wifi_state::connected); that->update_state(wifi_state::connected);
} else if (WIFI_EVENT_STA_DISCONNECTED == event_id) { } else if (WIFI_EVENT_STA_DISCONNECTED == event_id) {
printf("Disconnected from wifi\n"); printf("Station mode shutdown\n");
xSemaphoreTake(that->m_mutex, portMAX_DELAY); xSemaphoreTake(that->m_mutex, portMAX_DELAY);
if (that->m_state == wifi_state::connected) { if (that->m_state == wifi_state::connected) {
xSemaphoreGive(that->m_mutex); xSemaphoreGive(that->m_mutex);
@@ -147,6 +198,10 @@ void WifiManager::wifi_event_handler(void *event_handler_arg, esp_event_base_t e
xSemaphoreGive(that->m_mutex); xSemaphoreGive(that->m_mutex);
} }
} else if (IP_EVENT_STA_GOT_IP == event_id) { } else if (IP_EVENT_STA_GOT_IP == event_id) {
printf("Got IP \n"); printf("Got IP as station\n");
} else if (WIFI_EVENT_AP_STACONNECTED == event_id) {
printf("User connected to AP\n");
} else if (WIFI_EVENT_AP_STADISCONNECTED == event_id) {
printf("User disconnected from AP\n");
} }
} }

View File

@@ -1,6 +1,8 @@
#ifndef NETWORKMANAGER_H #ifndef NETWORKMANAGER_H
#define NETWORKMANAGER_H #define NETWORKMANAGER_H
#include <esp_netif_types.h>
#include "esp_event.h" #include "esp_event.h"
#include "freertos/FreeRTOS.h" #include "freertos/FreeRTOS.h"
@@ -11,13 +13,13 @@
class WifiManager final : IWifiManager { class WifiManager final : IWifiManager {
public: public:
WifiManager(); WifiManager();
~WifiManager() override = default; ~WifiManager() override;
int connect() override; int connect() override;
int disconnect() override; int disconnect() override;
[[noreturn]] void manage(); [[noreturn]] void manage();
enum class wifi_state { connect, connected, disconnected, connecting, disconnect, searching }; enum class wifi_state { connect, connecting, connected, disconnect, disconnected, broadcast, broadcasting };
private: private:
void update_state(wifi_state state); void update_state(wifi_state state);
@@ -25,7 +27,7 @@ private:
int init_connection(); int init_connection();
int handle_connecting(); int handle_connecting();
int handle_disconnect(); int handle_disconnect();
int handle_search(); 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);
@@ -33,6 +35,7 @@ private:
wifi_state m_state; wifi_state m_state;
TaskHandle_t m_task; TaskHandle_t m_task;
unsigned int m_attempts; unsigned int m_attempts;
esp_netif_t *m_netif;
}; };