revert to non-task-driven, no tangible difference in performance. May change back later.
This commit is contained in:
@@ -5,147 +5,14 @@
|
||||
#include "defines.h"
|
||||
#include "bmHTTP.hpp"
|
||||
#include "socketIO.hpp"
|
||||
#include <mutex>
|
||||
|
||||
// External declarations from BLE.cpp
|
||||
extern std::mutex dataMutex;
|
||||
extern wifi_auth_mode_t auth;
|
||||
extern std::string SSID;
|
||||
extern std::string PASS;
|
||||
extern std::string UNAME;
|
||||
extern std::string TOKEN;
|
||||
extern std::atomic<bool> scanBlock;
|
||||
extern std::atomic<bool> finalAuth;
|
||||
|
||||
// External functions from BLE.cpp
|
||||
extern void notifyConnectionStatus(bool success);
|
||||
extern void notifyAuthStatus(bool success);
|
||||
|
||||
// BLE setup task - event-driven instead of polling
|
||||
void bleSetupTask(void* arg) {
|
||||
NimBLEAdvertising* pAdvertising = initBLE();
|
||||
EventBits_t bits;
|
||||
|
||||
printf("BLE setup task started\n");
|
||||
|
||||
while (1) {
|
||||
// Wait for BLE events instead of polling
|
||||
bits = xEventGroupWaitBits(
|
||||
g_system_events,
|
||||
EVENT_BLE_SCAN_REQUEST | EVENT_BLE_CREDS_RECEIVED | EVENT_BLE_TOKEN_RECEIVED,
|
||||
pdTRUE, // Clear bits on exit
|
||||
pdFALSE, // Wait for ANY bit (not all)
|
||||
portMAX_DELAY // Block forever until event
|
||||
);
|
||||
|
||||
if (bits & EVENT_BLE_SCAN_REQUEST) {
|
||||
if (!scanBlock) {
|
||||
scanBlock = true;
|
||||
printf("Scanning WiFi...\n");
|
||||
WiFi::scanAndUpdateSSIDList();
|
||||
}
|
||||
else printf("Duplicate scan request\n");
|
||||
}
|
||||
|
||||
if (bits & EVENT_BLE_CREDS_RECEIVED) {
|
||||
std::string tmpSSID;
|
||||
std::string tmpUNAME;
|
||||
std::string tmpPASS;
|
||||
wifi_auth_mode_t tmpAUTH;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(dataMutex);
|
||||
tmpSSID = SSID;
|
||||
tmpUNAME = UNAME;
|
||||
tmpPASS = PASS;
|
||||
tmpAUTH = auth;
|
||||
}
|
||||
|
||||
bool wifiConnect;
|
||||
if (WiFi::isEnterpriseMode(tmpAUTH))
|
||||
wifiConnect = WiFi::attemptConnect(tmpSSID, tmpUNAME, tmpPASS, tmpAUTH);
|
||||
else wifiConnect = WiFi::attemptConnect(tmpSSID, tmpPASS, tmpAUTH);
|
||||
|
||||
if (!wifiConnect) {
|
||||
notifyConnectionStatus(false);
|
||||
printf("Connection failed\n");
|
||||
continue;
|
||||
}
|
||||
|
||||
nvs_handle_t WiFiHandle;
|
||||
esp_err_t err = nvs_open(nvsWiFi, NVS_READWRITE, &WiFiHandle);
|
||||
if (err == ESP_OK) {
|
||||
esp_err_t saveErr = ESP_OK;
|
||||
saveErr |= nvs_set_str(WiFiHandle, ssidTag, tmpSSID.c_str());
|
||||
saveErr |= nvs_set_u8(WiFiHandle, authTag, (uint8_t)tmpAUTH);
|
||||
|
||||
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);
|
||||
printf("WiFi credentials saved to NVS\n");
|
||||
notifyConnectionStatus(true);
|
||||
} else {
|
||||
printf("Failed to save WiFi credentials\n");
|
||||
notifyConnectionStatus(false);
|
||||
}
|
||||
nvs_close(WiFiHandle);
|
||||
}
|
||||
}
|
||||
|
||||
if (bits & EVENT_BLE_TOKEN_RECEIVED) {
|
||||
std::string tmpTOKEN;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(dataMutex);
|
||||
tmpTOKEN = TOKEN;
|
||||
}
|
||||
|
||||
cJSON* JSONresponse = NULL;
|
||||
if (httpGET("api/devices/auth", tmpTOKEN, JSONresponse)) {
|
||||
cJSON *success = cJSON_GetObjectItem(JSONresponse, "success");
|
||||
bool authSuccess = cJSON_IsTrue(success);
|
||||
|
||||
if (authSuccess) {
|
||||
nvs_handle_t authHandle;
|
||||
if (nvs_open(nvsAuth, NVS_READWRITE, &authHandle) == ESP_OK) {
|
||||
if (nvs_set_str(authHandle, tokenTag, tmpTOKEN.c_str()) == ESP_OK) {
|
||||
nvs_commit(authHandle);
|
||||
printf("Token saved to NVS\n");
|
||||
notifyAuthStatus(true);
|
||||
finalAuth = true;
|
||||
|
||||
// Task complete - delete itself
|
||||
vTaskDelete(NULL);
|
||||
}
|
||||
nvs_close(authHandle);
|
||||
}
|
||||
} else {
|
||||
printf("Token authentication failed\n");
|
||||
notifyAuthStatus(false);
|
||||
}
|
||||
cJSON_Delete(JSONresponse);
|
||||
} else {
|
||||
printf("HTTP request failed\n");
|
||||
notifyAuthStatus(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void initialSetup() {
|
||||
printf("Entered Setup\n");
|
||||
|
||||
// Create BLE setup task instead of polling loop
|
||||
xTaskCreate(bleSetupTask, "ble_setup", 8192, NULL, BLE_TASK_PRIORITY, NULL);
|
||||
|
||||
// Wait for BLE setup to complete (finalAuth = true)
|
||||
while (!finalAuth) {
|
||||
NimBLEAdvertising* pAdv = initBLE();
|
||||
|
||||
while (!BLEtick(pAdv)) {
|
||||
vTaskDelay(pdMS_TO_TICKS(100));
|
||||
}
|
||||
|
||||
printf("BLE setup complete\n");
|
||||
}
|
||||
|
||||
void setupLoop() {
|
||||
@@ -170,7 +37,7 @@ void setupLoop() {
|
||||
char pw[pwSize];
|
||||
nvs_get_str(WiFiHandle, passTag, pw, &pwSize);
|
||||
nvs_close(WiFiHandle);
|
||||
if (!WiFi::attemptConnect(ssid, pw, (wifi_auth_mode_t)authMode)) {
|
||||
if (!bmWiFi.attemptConnect(ssid, pw, (wifi_auth_mode_t)authMode)) {
|
||||
// Make RGB LED certain color (Blue?)
|
||||
printf("Found credentials, failed to connect.\n");
|
||||
initialSetup();
|
||||
@@ -189,28 +56,29 @@ void setupLoop() {
|
||||
// The server will verify the token during connection handshake
|
||||
webToken = std::string(token);
|
||||
printf("Connecting to Socket.IO server with saved token...\n");
|
||||
statusResolved = false;
|
||||
initSocketIO();
|
||||
|
||||
// Wait for connection event with timeout
|
||||
EventBits_t bits = xEventGroupWaitBits(
|
||||
g_system_events,
|
||||
EVENT_SOCKETIO_CONNECTED | EVENT_SOCKETIO_DISCONNECTED,
|
||||
pdTRUE, // Clear on exit
|
||||
pdFALSE, // Wait for ANY bit
|
||||
pdMS_TO_TICKS(10000) // 10 second timeout
|
||||
);
|
||||
// Wait for device_init message from server with timeout
|
||||
int timeout_count = 0;
|
||||
const int MAX_TIMEOUT = 60; // 10 seconds (20 * 500ms)
|
||||
while (!statusResolved && timeout_count < MAX_TIMEOUT) {
|
||||
printf("Waiting for device_init message... (%d/%d)\n", timeout_count, MAX_TIMEOUT);
|
||||
vTaskDelay(pdMS_TO_TICKS(500));
|
||||
timeout_count++;
|
||||
}
|
||||
|
||||
if (bits & EVENT_SOCKETIO_CONNECTED) {
|
||||
if (timeout_count >= MAX_TIMEOUT) {
|
||||
printf("Timeout waiting for device_init - connection failed\n");
|
||||
stopSocketIO();
|
||||
initSuccess = false;
|
||||
initialSetup();
|
||||
} else {
|
||||
initSuccess = connected;
|
||||
if (!initSuccess) {
|
||||
printf("Device authentication failed - entering setup\n");
|
||||
initialSetup();
|
||||
}
|
||||
} else {
|
||||
printf("Timeout waiting for connection\n");
|
||||
stopSocketIO();
|
||||
initSuccess = false;
|
||||
initialSetup();
|
||||
}
|
||||
}
|
||||
else {
|
||||
|
||||
Reference in New Issue
Block a user