From 636aaf64f806bb1e92ccdcbb3f4b384d562d08c3 Mon Sep 17 00:00:00 2001 From: pulipakaa24 Date: Sun, 4 Jan 2026 11:43:23 -0600 Subject: [PATCH] Static classes, enterprise networks properly handled --- include/WiFi.cpp | 12 ++++++++++-- include/WiFi.hpp | 6 ++++-- include/calibration.cpp | 5 +++++ include/calibration.hpp | 22 ++++++++++------------ include/servo.cpp | 16 ++++++++-------- include/setup.cpp | 16 ++++++++-------- include/socketIO.cpp | 6 +++--- src/main.cpp | 11 ++++------- 8 files changed, 52 insertions(+), 42 deletions(-) diff --git a/include/WiFi.cpp b/include/WiFi.cpp index 80ca08c..3d55bbe 100644 --- a/include/WiFi.cpp +++ b/include/WiFi.cpp @@ -12,8 +12,6 @@ esp_event_handler_instance_t WiFi::instance_got_ip = NULL; #define WIFI_CONNECTED_BIT BIT0 #define WIFI_STARTED_BIT BIT1 -WiFi bmWiFi; - // The Event Handler (The engine room) void WiFi::event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) { @@ -119,6 +117,16 @@ bool WiFi::isConnected() { 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 --- std::string WiFi::getIP() { esp_netif_ip_info_t ip_info; diff --git a/include/WiFi.hpp b/include/WiFi.hpp index 77bde73..fb58bff 100644 --- a/include/WiFi.hpp +++ b/include/WiFi.hpp @@ -14,6 +14,10 @@ class WiFi { const std::string password, const wifi_auth_mode_t authMode); static bool isConnected(); static void scanAndUpdateSSIDList(); + + // Helper to check if auth mode requires enterprise credentials + static bool isEnterpriseMode(wifi_auth_mode_t authMode); + private: static void processScanResults(); static std::atomic authFailed; @@ -27,6 +31,4 @@ class WiFi { static std::string getIP(); }; -extern WiFi bmWiFi; - #endif \ No newline at end of file diff --git a/include/calibration.cpp b/include/calibration.cpp index e472015..6a27d5d 100644 --- a/include/calibration.cpp +++ b/include/calibration.cpp @@ -2,6 +2,11 @@ #include "defines.h" #include "nvs_flash.h" +// Define static members +std::atomic Calibration::DownTicks{0}; +std::atomic Calibration::UpTicks{0}; +std::atomic Calibration::calibrated{false}; + void Calibration::init() { nvs_handle_t calibHandle; if (nvs_open(nvsCalib, NVS_READONLY, &calibHandle) == ESP_OK) { diff --git a/include/calibration.hpp b/include/calibration.hpp index e862e7e..ff3b0dc 100644 --- a/include/calibration.hpp +++ b/include/calibration.hpp @@ -5,20 +5,18 @@ class Calibration { public: - void init(); - bool beginDownwardCalib(Encoder& topEnc); - bool completeCalib(Encoder& topEnc); - int32_t convertToTicks(uint8_t appPos); - uint8_t convertToAppPos(int32_t ticks); - bool getCalibrated() {return calibrated;} - bool clearCalibrated(); - std::atomic DownTicks; - std::atomic UpTicks; + static void init(); + static bool beginDownwardCalib(Encoder& topEnc); + static bool completeCalib(Encoder& topEnc); + static int32_t convertToTicks(uint8_t appPos); + static uint8_t convertToAppPos(int32_t ticks); + static bool getCalibrated() {return calibrated;} + static bool clearCalibrated(); + static std::atomic DownTicks; + static std::atomic UpTicks; private: - std::atomic calibrated; + static std::atomic calibrated; }; -extern Calibration calib; - #endif \ No newline at end of file diff --git a/include/servo.cpp b/include/servo.cpp index db3ee30..ed8ac20 100644 --- a/include/servo.cpp +++ b/include/servo.cpp @@ -42,7 +42,7 @@ void servoInit() { gpio_set_level(servoSwitch, 0); // Start with servo power off topEnc->count = servoReadPos(); - if (calib.getCalibrated()) initMainLoop(); + if (Calibration::getCalibrated()) initMainLoop(); } void servoOn(uint8_t dir, uint8_t manOrServer) { @@ -72,7 +72,7 @@ bool servoInitCalib() { bottomEnc->wandListen = false; topEnc->wandListen = false; topEnc->serverListen = false; - if (!calib.clearCalibrated()) return false; + if (!Calibration::clearCalibrated()) return false; if (topEnc == nullptr || bottomEnc == nullptr) { printf("ERROR: CALIBRATION STARTED BEFORE SERVO INITIALIZATION\n"); return false; @@ -93,7 +93,7 @@ bool servoBeginDownwardCalib() { calibListen = false; servoOff(); vTaskDelay(pdMS_TO_TICKS(1000)); - if (!calib.beginDownwardCalib(*topEnc)) return false; + if (!Calibration::beginDownwardCalib(*topEnc)) return false; baseDiff = bottomEnc->getCount() - topEnc->getCount(); calibListen = true; return true; @@ -103,7 +103,7 @@ bool servoCompleteCalib() { calibListen = false; servoOff(); vTaskDelay(pdMS_TO_TICKS(1000)); - if (!calib.completeCalib(*topEnc)) return false; + if (!Calibration::completeCalib(*topEnc)) return false; initMainLoop(); return true; } @@ -183,8 +183,8 @@ void servoWandListen() { stopServerRun(); // freeze atomic values - int32_t upBound = calib.UpTicks; - int32_t downBound = calib.DownTicks; + int32_t upBound = Calibration::UpTicks; + int32_t downBound = Calibration::DownTicks; int32_t bottomCount = bottomEnc->getCount(); int32_t topCount = topEnc->getCount(); @@ -231,13 +231,13 @@ void servoServerListen() { void runToAppPos(uint8_t appPos) { // manual control takes precedence over remote control, always. // also do not begin operation if not calibrated; - if (runningManual || !calib.getCalibrated()) return; + if (runningManual || !Calibration::getCalibrated()) return; servoOff(); // allow servo position to settle vTaskDelay(pdMS_TO_TICKS(500)); 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; startLess = topCount < target; if (runningManual) return; // check again before starting remote control diff --git a/include/setup.cpp b/include/setup.cpp index 35ee13c..4792d2e 100644 --- a/include/setup.cpp +++ b/include/setup.cpp @@ -42,7 +42,7 @@ void bleSetupTask(void* arg) { if (!scanBlock) { scanBlock = true; printf("Scanning WiFi...\n"); - bmWiFi.scanAndUpdateSSIDList(); + WiFi::scanAndUpdateSSIDList(); } else printf("Duplicate scan request\n"); } @@ -61,9 +61,9 @@ void bleSetupTask(void* arg) { } bool wifiConnect; - if (tmpAUTH == WIFI_AUTH_WPA2_ENTERPRISE || tmpAUTH == WIFI_AUTH_WPA3_ENTERPRISE) - wifiConnect = bmWiFi.attemptConnect(tmpSSID, tmpUNAME, tmpPASS, tmpAUTH); - else wifiConnect = bmWiFi.attemptConnect(tmpSSID, tmpPASS, tmpAUTH); + if (WiFi::isEnterpriseMode(tmpAUTH)) + wifiConnect = WiFi::attemptConnect(tmpSSID, tmpUNAME, tmpPASS, tmpAUTH); + else wifiConnect = WiFi::attemptConnect(tmpSSID, tmpPASS, tmpAUTH); if (!wifiConnect) { notifyConnectionStatus(false); @@ -76,12 +76,12 @@ void bleSetupTask(void* arg) { if (err == ESP_OK) { esp_err_t saveErr = ESP_OK; 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); - if (tmpUNAME.length() > 0) { + if (tmpUNAME.length() > 0) 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) { nvs_commit(WiFiHandle); @@ -170,7 +170,7 @@ void setupLoop() { char pw[pwSize]; nvs_get_str(WiFiHandle, passTag, pw, &pwSize); 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?) printf("Found credentials, failed to connect.\n"); initialSetup(); diff --git a/include/socketIO.cpp b/include/socketIO.cpp index d8d2ac0..0c79e22 100644 --- a/include/socketIO.cpp +++ b/include/socketIO.cpp @@ -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"); // Report back actual calibration status from device else { - bool deviceCalibrated = calib.getCalibrated(); + bool deviceCalibrated = Calibration::getCalibrated(); emitCalibStatus(deviceCalibrated); printf(" Reported calibrated=%d for port %d\n", deviceCalibrated, port); 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); } else { printf("Device authentication failed\n"); - calib.clearCalibrated(); + Calibration::clearCalibrated(); deleteWiFiAndTokenDetails(); connected = false; 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); } } - calib.clearCalibrated(); + Calibration::clearCalibrated(); deleteWiFiAndTokenDetails(); connected = false; xEventGroupSetBits(g_system_events, EVENT_SOCKETIO_DISCONNECTED); diff --git a/src/main.cpp b/src/main.cpp index b357bdf..4a17883 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -19,9 +19,6 @@ SemaphoreHandle_t g_calibration_mutex = NULL; Encoder* topEnc = new Encoder(ENCODER_PIN_A, ENCODER_PIN_B); Encoder* bottomEnc = new Encoder(InputEnc_PIN_A, InputEnc_PIN_B); -// Global calibration instance -Calibration calib; - void mainTask(void* arg) { EventBits_t bits; @@ -50,7 +47,7 @@ void mainTask(void* arg) { if (bits & EVENT_CLEAR_CALIB) { xSemaphoreTake(g_calibration_mutex, portMAX_DELAY); - calib.clearCalibrated(); + Calibration::clearCalibrated(); xSemaphoreGive(g_calibration_mutex); emitCalibStatus(false); } @@ -59,7 +56,7 @@ void mainTask(void* arg) { servoSavePos(); // Send position update to server - uint8_t currentAppPos = calib.convertToAppPos(topEnc->getCount()); + uint8_t currentAppPos = Calibration::convertToAppPos(topEnc->getCount()); emitPosHit(currentAppPos); printf("Sent pos_hit: position %d\n", currentAppPos); @@ -106,8 +103,8 @@ extern "C" void app_main() { } // Initialize hardware - bmWiFi.init(); - calib.init(); + WiFi::init(); + Calibration::init(); // Initialize encoders topEnc->init();