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_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;

View File

@@ -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<bool> authFailed;
@@ -27,6 +31,4 @@ class WiFi {
static std::string getIP();
};
extern WiFi bmWiFi;
#endif

View File

@@ -2,6 +2,11 @@
#include "defines.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() {
nvs_handle_t calibHandle;
if (nvs_open(nvsCalib, NVS_READONLY, &calibHandle) == ESP_OK) {

View File

@@ -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<int32_t> DownTicks;
std::atomic<int32_t> 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<int32_t> DownTicks;
static std::atomic<int32_t> UpTicks;
private:
std::atomic<bool> calibrated;
static std::atomic<bool> calibrated;
};
extern Calibration calib;
#endif

View File

@@ -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

View File

@@ -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();

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");
// 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);

View File

@@ -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();