diff --git a/include/BLE.cpp b/include/BLE.cpp index bcf6b6c..08b890b 100644 --- a/include/BLE.cpp +++ b/include/BLE.cpp @@ -20,12 +20,12 @@ static std::string PASS = ""; static std::string UNAME = ""; // Global pointers to characteristics for notification support -NimBLECharacteristic* ssidListChar = nullptr; -NimBLECharacteristic* connectConfirmChar = nullptr; -NimBLECharacteristic* authConfirmChar = nullptr; -NimBLECharacteristic* credsChar = nullptr; -NimBLECharacteristic* tokenChar = nullptr; -NimBLECharacteristic* ssidRefreshChar = nullptr; +std::atomic ssidListChar = nullptr; +std::atomic connectConfirmChar = nullptr; +std::atomic authConfirmChar = nullptr; +std::atomic credsChar = nullptr; +std::atomic tokenChar = nullptr; +std::atomic ssidRefreshChar = nullptr; NimBLEAdvertising* initBLE() { NimBLEDevice::init("BlindMaster-C6"); @@ -51,7 +51,7 @@ NimBLEAdvertising* initBLE() { "0000", NIMBLE_PROPERTY::READ ); - ssidListChar->createDescriptor("2902"); // Add BLE2902 descriptor for notifications + ssidListChar.load()->createDescriptor("2902"); // Add BLE2902 descriptor for notifications // 0x0001 - Credentials JSON (WRITE) - Replaces separate SSID/Password/Uname // Expected JSON format: {"ssid":"network","password":"pass"} @@ -59,35 +59,35 @@ NimBLEAdvertising* initBLE() { "0001", NIMBLE_PROPERTY::WRITE ); - credsChar->setCallbacks(charCallbacks); + credsChar.load()->setCallbacks(charCallbacks); // 0x0002 - Token (WRITE) tokenChar = pService->createCharacteristic( "0002", NIMBLE_PROPERTY::WRITE ); - tokenChar->setCallbacks(charCallbacks); + tokenChar.load()->setCallbacks(charCallbacks); // 0x0003 - Auth Confirmation (READ + NOTIFY) authConfirmChar = pService->createCharacteristic( "0003", NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::NOTIFY ); - authConfirmChar->createDescriptor("2902"); // Add BLE2902 descriptor for notifications + authConfirmChar.load()->createDescriptor("2902"); // Add BLE2902 descriptor for notifications // 0x0004 - SSID Refresh (WRITE) ssidRefreshChar = pService->createCharacteristic( "0004", NIMBLE_PROPERTY::WRITE | NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::NOTIFY ); - ssidRefreshChar->setCallbacks(charCallbacks); + ssidRefreshChar.load()->setCallbacks(charCallbacks); // 0x0005 - Connect Confirmation (READ + NOTIFY) connectConfirmChar = pService->createCharacteristic( "0005", NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::NOTIFY ); - connectConfirmChar->createDescriptor("2902"); // Add BLE2902 descriptor for notifications + connectConfirmChar.load()->createDescriptor("2902"); // Add BLE2902 descriptor for notifications // Start pService->start(); @@ -106,14 +106,16 @@ NimBLEAdvertising* initBLE() { } void notifyConnectionStatus(bool success) { - connectConfirmChar->setValue(success ? "Connected" : "Error"); - connectConfirmChar->notify(); - connectConfirmChar->setValue(""); // Clear value after notify + NimBLECharacteristic* tmpConfChar = connectConfirmChar.load(); + tmpConfChar->setValue(success ? "Connected" : "Error"); + tmpConfChar->notify(); + tmpConfChar->setValue(""); // Clear value after notify } void notifyAuthStatus(bool success) { - authConfirmChar->setValue(success ? "Authenticated" : "Error"); - authConfirmChar->notify(); - authConfirmChar->setValue(""); // Clear value after notify + NimBLECharacteristic* tmpConfChar = authConfirmChar.load(); + tmpConfChar->setValue(success ? "Authenticated" : "Error"); + tmpConfChar->notify(); + tmpConfChar->setValue(""); // Clear value after notify } bool BLEtick(NimBLEAdvertising* pAdvertising) { diff --git a/include/BLE.hpp b/include/BLE.hpp index b5f317f..8a3d34f 100644 --- a/include/BLE.hpp +++ b/include/BLE.hpp @@ -8,8 +8,8 @@ #include "esp_wifi_types.h" // Global pointers to characteristics for notification support -extern NimBLECharacteristic* ssidListChar; -extern NimBLECharacteristic* ssidRefreshChar; +extern std::atomic ssidListChar; +extern std::atomic ssidRefreshChar; extern std::atomic isBLEClientConnected; class MyServerCallbacks : public NimBLEServerCallbacks { diff --git a/include/WiFi.cpp b/include/WiFi.cpp index aadb68c..fe3fc1d 100644 --- a/include/WiFi.cpp +++ b/include/WiFi.cpp @@ -232,9 +232,10 @@ void WiFi::processScanResults() { // 5. Update BLE if (ssidListChar != nullptr) { - ssidListChar->setValue(std::string(json_string)); - ssidRefreshChar->setValue("Ready"); - ssidRefreshChar->notify(); + ssidListChar.load()->setValue(std::string(json_string)); + NimBLECharacteristic *tmpRefreshChar = ssidRefreshChar.load(); + tmpRefreshChar->setValue("Ready"); + tmpRefreshChar->notify(); } // 6. Cleanup Memory