Static classes, enterprise networks properly handled

This commit is contained in:
2026-01-04 11:43:23 -06:00
parent daced162ba
commit 636aaf64f8
8 changed files with 52 additions and 42 deletions

View File

@@ -12,8 +12,6 @@ esp_event_handler_instance_t WiFi::instance_got_ip = NULL;
#define WIFI_CONNECTED_BIT BIT0 #define WIFI_CONNECTED_BIT BIT0
#define WIFI_STARTED_BIT BIT1 #define WIFI_STARTED_BIT BIT1
WiFi bmWiFi;
// The Event Handler (The engine room) // The Event Handler (The engine room)
void WiFi::event_handler(void* arg, esp_event_base_t event_base, void WiFi::event_handler(void* arg, esp_event_base_t event_base,
int32_t event_id, void* event_data) { int32_t event_id, void* event_data) {
@@ -119,6 +117,16 @@ bool WiFi::isConnected() {
return (bits & WIFI_CONNECTED_BIT); return (bits & WIFI_CONNECTED_BIT);
} }
// Helper to check if auth mode requires enterprise credentials
bool WiFi::isEnterpriseMode(wifi_auth_mode_t authMode) {
return (authMode == WIFI_AUTH_WPA2_ENTERPRISE ||
authMode == WIFI_AUTH_WPA3_ENTERPRISE ||
authMode == WIFI_AUTH_WPA2_WPA3_ENTERPRISE ||
authMode == WIFI_AUTH_WPA_ENTERPRISE ||
authMode == WIFI_AUTH_WPA3_ENT_192 ||
authMode == WIFI_AUTH_ENTERPRISE); // Deprecated alias for WPA2
}
// --- GET IP AS STRING --- // --- GET IP AS STRING ---
std::string WiFi::getIP() { std::string WiFi::getIP() {
esp_netif_ip_info_t ip_info; esp_netif_ip_info_t ip_info;

View File

@@ -14,6 +14,10 @@ class WiFi {
const std::string password, const wifi_auth_mode_t authMode); const std::string password, const wifi_auth_mode_t authMode);
static bool isConnected(); static bool isConnected();
static void scanAndUpdateSSIDList(); static void scanAndUpdateSSIDList();
// Helper to check if auth mode requires enterprise credentials
static bool isEnterpriseMode(wifi_auth_mode_t authMode);
private: private:
static void processScanResults(); static void processScanResults();
static std::atomic<bool> authFailed; static std::atomic<bool> authFailed;
@@ -27,6 +31,4 @@ class WiFi {
static std::string getIP(); static std::string getIP();
}; };
extern WiFi bmWiFi;
#endif #endif

View File

@@ -2,6 +2,11 @@
#include "defines.h" #include "defines.h"
#include "nvs_flash.h" #include "nvs_flash.h"
// Define static members
std::atomic<int32_t> Calibration::DownTicks{0};
std::atomic<int32_t> Calibration::UpTicks{0};
std::atomic<bool> Calibration::calibrated{false};
void Calibration::init() { void Calibration::init() {
nvs_handle_t calibHandle; nvs_handle_t calibHandle;
if (nvs_open(nvsCalib, NVS_READONLY, &calibHandle) == ESP_OK) { if (nvs_open(nvsCalib, NVS_READONLY, &calibHandle) == ESP_OK) {

View File

@@ -5,20 +5,18 @@
class Calibration { class Calibration {
public: public:
void init(); static void init();
bool beginDownwardCalib(Encoder& topEnc); static bool beginDownwardCalib(Encoder& topEnc);
bool completeCalib(Encoder& topEnc); static bool completeCalib(Encoder& topEnc);
int32_t convertToTicks(uint8_t appPos); static int32_t convertToTicks(uint8_t appPos);
uint8_t convertToAppPos(int32_t ticks); static uint8_t convertToAppPos(int32_t ticks);
bool getCalibrated() {return calibrated;} static bool getCalibrated() {return calibrated;}
bool clearCalibrated(); static bool clearCalibrated();
std::atomic<int32_t> DownTicks; static std::atomic<int32_t> DownTicks;
std::atomic<int32_t> UpTicks; static std::atomic<int32_t> UpTicks;
private: private:
std::atomic<bool> calibrated; static std::atomic<bool> calibrated;
}; };
extern Calibration calib;
#endif #endif

View File

@@ -42,7 +42,7 @@ void servoInit() {
gpio_set_level(servoSwitch, 0); // Start with servo power off gpio_set_level(servoSwitch, 0); // Start with servo power off
topEnc->count = servoReadPos(); topEnc->count = servoReadPos();
if (calib.getCalibrated()) initMainLoop(); if (Calibration::getCalibrated()) initMainLoop();
} }
void servoOn(uint8_t dir, uint8_t manOrServer) { void servoOn(uint8_t dir, uint8_t manOrServer) {
@@ -72,7 +72,7 @@ bool servoInitCalib() {
bottomEnc->wandListen = false; bottomEnc->wandListen = false;
topEnc->wandListen = false; topEnc->wandListen = false;
topEnc->serverListen = false; topEnc->serverListen = false;
if (!calib.clearCalibrated()) return false; if (!Calibration::clearCalibrated()) return false;
if (topEnc == nullptr || bottomEnc == nullptr) { if (topEnc == nullptr || bottomEnc == nullptr) {
printf("ERROR: CALIBRATION STARTED BEFORE SERVO INITIALIZATION\n"); printf("ERROR: CALIBRATION STARTED BEFORE SERVO INITIALIZATION\n");
return false; return false;
@@ -93,7 +93,7 @@ bool servoBeginDownwardCalib() {
calibListen = false; calibListen = false;
servoOff(); servoOff();
vTaskDelay(pdMS_TO_TICKS(1000)); vTaskDelay(pdMS_TO_TICKS(1000));
if (!calib.beginDownwardCalib(*topEnc)) return false; if (!Calibration::beginDownwardCalib(*topEnc)) return false;
baseDiff = bottomEnc->getCount() - topEnc->getCount(); baseDiff = bottomEnc->getCount() - topEnc->getCount();
calibListen = true; calibListen = true;
return true; return true;
@@ -103,7 +103,7 @@ bool servoCompleteCalib() {
calibListen = false; calibListen = false;
servoOff(); servoOff();
vTaskDelay(pdMS_TO_TICKS(1000)); vTaskDelay(pdMS_TO_TICKS(1000));
if (!calib.completeCalib(*topEnc)) return false; if (!Calibration::completeCalib(*topEnc)) return false;
initMainLoop(); initMainLoop();
return true; return true;
} }
@@ -183,8 +183,8 @@ void servoWandListen() {
stopServerRun(); stopServerRun();
// freeze atomic values // freeze atomic values
int32_t upBound = calib.UpTicks; int32_t upBound = Calibration::UpTicks;
int32_t downBound = calib.DownTicks; int32_t downBound = Calibration::DownTicks;
int32_t bottomCount = bottomEnc->getCount(); int32_t bottomCount = bottomEnc->getCount();
int32_t topCount = topEnc->getCount(); int32_t topCount = topEnc->getCount();
@@ -231,13 +231,13 @@ void servoServerListen() {
void runToAppPos(uint8_t appPos) { void runToAppPos(uint8_t appPos) {
// manual control takes precedence over remote control, always. // manual control takes precedence over remote control, always.
// also do not begin operation if not calibrated; // also do not begin operation if not calibrated;
if (runningManual || !calib.getCalibrated()) return; if (runningManual || !Calibration::getCalibrated()) return;
servoOff(); servoOff();
// allow servo position to settle // allow servo position to settle
vTaskDelay(pdMS_TO_TICKS(500)); vTaskDelay(pdMS_TO_TICKS(500));
int32_t topCount = topEnc->getCount(); int32_t topCount = topEnc->getCount();
target = calib.convertToTicks(appPos); // calculate target encoder position target = Calibration::convertToTicks(appPos); // calculate target encoder position
if (abs(topCount - target) <= 1) return; if (abs(topCount - target) <= 1) return;
startLess = topCount < target; startLess = topCount < target;
if (runningManual) return; // check again before starting remote control if (runningManual) return; // check again before starting remote control

View File

@@ -42,7 +42,7 @@ void bleSetupTask(void* arg) {
if (!scanBlock) { if (!scanBlock) {
scanBlock = true; scanBlock = true;
printf("Scanning WiFi...\n"); printf("Scanning WiFi...\n");
bmWiFi.scanAndUpdateSSIDList(); WiFi::scanAndUpdateSSIDList();
} }
else printf("Duplicate scan request\n"); else printf("Duplicate scan request\n");
} }
@@ -61,9 +61,9 @@ void bleSetupTask(void* arg) {
} }
bool wifiConnect; bool wifiConnect;
if (tmpAUTH == WIFI_AUTH_WPA2_ENTERPRISE || tmpAUTH == WIFI_AUTH_WPA3_ENTERPRISE) if (WiFi::isEnterpriseMode(tmpAUTH))
wifiConnect = bmWiFi.attemptConnect(tmpSSID, tmpUNAME, tmpPASS, tmpAUTH); wifiConnect = WiFi::attemptConnect(tmpSSID, tmpUNAME, tmpPASS, tmpAUTH);
else wifiConnect = bmWiFi.attemptConnect(tmpSSID, tmpPASS, tmpAUTH); else wifiConnect = WiFi::attemptConnect(tmpSSID, tmpPASS, tmpAUTH);
if (!wifiConnect) { if (!wifiConnect) {
notifyConnectionStatus(false); notifyConnectionStatus(false);
@@ -76,12 +76,12 @@ void bleSetupTask(void* arg) {
if (err == ESP_OK) { if (err == ESP_OK) {
esp_err_t saveErr = ESP_OK; esp_err_t saveErr = ESP_OK;
saveErr |= nvs_set_str(WiFiHandle, ssidTag, tmpSSID.c_str()); saveErr |= nvs_set_str(WiFiHandle, ssidTag, tmpSSID.c_str());
saveErr |= nvs_set_str(WiFiHandle, passTag, tmpPASS.c_str());
saveErr |= nvs_set_u8(WiFiHandle, authTag, (uint8_t)tmpAUTH); saveErr |= nvs_set_u8(WiFiHandle, authTag, (uint8_t)tmpAUTH);
if (tmpUNAME.length() > 0) { if (tmpUNAME.length() > 0)
saveErr |= nvs_set_str(WiFiHandle, unameTag, tmpUNAME.c_str()); saveErr |= nvs_set_str(WiFiHandle, unameTag, tmpUNAME.c_str());
} if (tmpPASS.length() > 0)
saveErr |= nvs_set_str(WiFiHandle, passTag, tmpPASS.c_str());
if (saveErr == ESP_OK) { if (saveErr == ESP_OK) {
nvs_commit(WiFiHandle); nvs_commit(WiFiHandle);
@@ -170,7 +170,7 @@ void setupLoop() {
char pw[pwSize]; char pw[pwSize];
nvs_get_str(WiFiHandle, passTag, pw, &pwSize); nvs_get_str(WiFiHandle, passTag, pw, &pwSize);
nvs_close(WiFiHandle); nvs_close(WiFiHandle);
if (!bmWiFi.attemptConnect(ssid, pw, (wifi_auth_mode_t)authMode)) { if (!WiFi::attemptConnect(ssid, pw, (wifi_auth_mode_t)authMode)) {
// Make RGB LED certain color (Blue?) // Make RGB LED certain color (Blue?)
printf("Found credentials, failed to connect.\n"); printf("Found credentials, failed to connect.\n");
initialSetup(); initialSetup();

View File

@@ -91,7 +91,7 @@ static void socketio_event_handler(void *handler_args, esp_event_base_t base,
if (port != 1) printf("ERROR: NON-1 PORT RECEIVED\n"); if (port != 1) printf("ERROR: NON-1 PORT RECEIVED\n");
// Report back actual calibration status from device // Report back actual calibration status from device
else { else {
bool deviceCalibrated = calib.getCalibrated(); bool deviceCalibrated = Calibration::getCalibrated();
emitCalibStatus(deviceCalibrated); emitCalibStatus(deviceCalibrated);
printf(" Reported calibrated=%d for port %d\n", deviceCalibrated, port); printf(" Reported calibrated=%d for port %d\n", deviceCalibrated, port);
runToAppPos(lastPos); runToAppPos(lastPos);
@@ -104,7 +104,7 @@ static void socketio_event_handler(void *handler_args, esp_event_base_t base,
xEventGroupSetBits(g_system_events, EVENT_SOCKETIO_CONNECTED); xEventGroupSetBits(g_system_events, EVENT_SOCKETIO_CONNECTED);
} else { } else {
printf("Device authentication failed\n"); printf("Device authentication failed\n");
calib.clearCalibrated(); Calibration::clearCalibrated();
deleteWiFiAndTokenDetails(); deleteWiFiAndTokenDetails();
connected = false; connected = false;
xEventGroupSetBits(g_system_events, EVENT_SOCKETIO_DISCONNECTED); xEventGroupSetBits(g_system_events, EVENT_SOCKETIO_DISCONNECTED);
@@ -121,7 +121,7 @@ static void socketio_event_handler(void *handler_args, esp_event_base_t base,
printf("Server message: %s\n", message->valuestring); printf("Server message: %s\n", message->valuestring);
} }
} }
calib.clearCalibrated(); Calibration::clearCalibrated();
deleteWiFiAndTokenDetails(); deleteWiFiAndTokenDetails();
connected = false; connected = false;
xEventGroupSetBits(g_system_events, EVENT_SOCKETIO_DISCONNECTED); xEventGroupSetBits(g_system_events, EVENT_SOCKETIO_DISCONNECTED);

View File

@@ -19,9 +19,6 @@ SemaphoreHandle_t g_calibration_mutex = NULL;
Encoder* topEnc = new Encoder(ENCODER_PIN_A, ENCODER_PIN_B); Encoder* topEnc = new Encoder(ENCODER_PIN_A, ENCODER_PIN_B);
Encoder* bottomEnc = new Encoder(InputEnc_PIN_A, InputEnc_PIN_B); Encoder* bottomEnc = new Encoder(InputEnc_PIN_A, InputEnc_PIN_B);
// Global calibration instance
Calibration calib;
void mainTask(void* arg) { void mainTask(void* arg) {
EventBits_t bits; EventBits_t bits;
@@ -50,7 +47,7 @@ void mainTask(void* arg) {
if (bits & EVENT_CLEAR_CALIB) { if (bits & EVENT_CLEAR_CALIB) {
xSemaphoreTake(g_calibration_mutex, portMAX_DELAY); xSemaphoreTake(g_calibration_mutex, portMAX_DELAY);
calib.clearCalibrated(); Calibration::clearCalibrated();
xSemaphoreGive(g_calibration_mutex); xSemaphoreGive(g_calibration_mutex);
emitCalibStatus(false); emitCalibStatus(false);
} }
@@ -59,7 +56,7 @@ void mainTask(void* arg) {
servoSavePos(); servoSavePos();
// Send position update to server // Send position update to server
uint8_t currentAppPos = calib.convertToAppPos(topEnc->getCount()); uint8_t currentAppPos = Calibration::convertToAppPos(topEnc->getCount());
emitPosHit(currentAppPos); emitPosHit(currentAppPos);
printf("Sent pos_hit: position %d\n", currentAppPos); printf("Sent pos_hit: position %d\n", currentAppPos);
@@ -106,8 +103,8 @@ extern "C" void app_main() {
} }
// Initialize hardware // Initialize hardware
bmWiFi.init(); WiFi::init();
calib.init(); Calibration::init();
// Initialize encoders // Initialize encoders
topEnc->init(); topEnc->init();