diff --git a/include/Settings.h b/include/Settings.h index b51a848..9605277 100644 --- a/include/Settings.h +++ b/include/Settings.h @@ -1,7 +1,6 @@ #pragma once #include -#include #include "macros.h" #include "types.h" @@ -401,8 +400,4 @@ struct Variables { bool resetFault = false; bool resetDiagnostic = false; } actions; - - struct { - std::atomic upgrading{false}; - } states; }; diff --git a/include/app.h b/include/app.h index 9c01e04..bfcc5e8 100644 --- a/include/app.h +++ b/include/app.h @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -71,6 +72,7 @@ SettingResult fromJson(const JsonVariantConst src, Variables& dst); bool applyAction(std::string_view action, bool value); // Application state and task managers +inline std::atomic upgrading{false}; extern Variables vars; extern TaskManager coreTaskManager; extern TaskManager systemTaskManager; @@ -107,7 +109,7 @@ extern AsyncWebServer* webServer; void initPortal(); extern Task mqttConfigureTask; // Protect MQTT client and task pointers while checking and using them. -extern std::mutex mqttMutex; +inline std::mutex mqttMutex; extern Task* mqttPublishSettingsTask; extern Task* mqttPublishStateTask; extern Task* mqttPublishSensorsTask; diff --git a/src/config.cpp b/src/config.cpp index 0c4d9fe..7fcdec5 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -3606,7 +3606,7 @@ static SettingResult applyAction(Variables& dst, std::string_view action, bool v } bool applyAction(std::string_view action, bool value) { - if (vars.states.upgrading || restartTask.scheduled() || restartTask.active()) { + if (upgrading || restartTask.scheduled() || restartTask.active()) { return false; } @@ -3617,7 +3617,7 @@ SettingResult fromJson(const JsonVariantConst src, Variables& dst) { SettingResult result; // Admit the whole request before one of its actions schedules a restart. - if (vars.states.upgrading || restartTask.scheduled() || restartTask.active()) { + if (upgrading || restartTask.scheduled() || restartTask.active()) { return result; } diff --git a/src/mqtt.cpp b/src/mqtt.cpp index 00ee4a0..894f15c 100644 --- a/src/mqtt.cpp +++ b/src/mqtt.cpp @@ -18,8 +18,6 @@ #include #include -std::mutex mqttMutex; - static std::unique_ptr mqttClient; // Keep the applied prefix separate from settings until MQTT is reconfigured. @@ -55,7 +53,7 @@ static bool isSensorReconfigurationPending(uint8_t sensorId) { } static bool isMqttBlocked() { - return vars.states.upgrading || restartTask.scheduled() || restartTask.active(); + return upgrading || restartTask.scheduled() || restartTask.active(); } static bool isMqttReady() { @@ -1154,7 +1152,7 @@ Task mqttConfigureTask("Configure MQTT", Task::Type::ONCE, [](void*) { void initMqtt() { // Keep only the configuration dispatcher when MQTT publications are disabled. mqttConfigureTask.pause().setEnabledWhen([] { - return !vars.states.upgrading && !restartTask.scheduled() && !restartTask.active(); + return !upgrading && !restartTask.scheduled() && !restartTask.active(); }); coreTaskManager.addTask(mqttConfigureTask); diff --git a/src/portal.cpp b/src/portal.cpp index 0b44245..ad1db73 100644 --- a/src/portal.cpp +++ b/src/portal.cpp @@ -236,15 +236,15 @@ static void restApi() { return false; } // UpgradeHandler invokes this callback for every upload chunk. - if (vars.states.upgrading) { + if (upgrading) { return true; } - vars.states.upgrading = true; + upgrading = true; coreTaskManager.pause(); // Core owns MQTT; finish its current callback before stopping the client. if (!coreTaskManager.waitForPaused(5000)) { - vars.states.upgrading = false; + upgrading = false; if (!restartTask.scheduled() && !restartTask.active()) { coreTaskManager.resume(); } @@ -256,14 +256,14 @@ static void restApi() { // An MQTT command already in flight may have scheduled a restart before end(). if (restartTask.scheduled() || restartTask.active()) { - vars.states.upgrading = false; + upgrading = false; return false; } sensorsTaskManager.pause(); if (!sensorsTaskManager.waitForPaused(5000)) { requestReconfigureMqtt(); - vars.states.upgrading = false; + upgrading = false; if (!restartTask.scheduled() && !restartTask.active()) { coreTaskManager.resume(); sensorsTaskManager.resume(); @@ -275,7 +275,7 @@ static void restApi() { openthermTaskManager.pause(); if (!openthermTaskManager.waitForPaused(5000)) { requestReconfigureMqtt(); - vars.states.upgrading = false; + upgrading = false; if (!restartTask.scheduled() && !restartTask.active()) { coreTaskManager.resume(); sensorsTaskManager.resume(); @@ -302,7 +302,7 @@ static void restApi() { requestReconfigureMqtt(); } - vars.states.upgrading = false; + upgrading = false; if (status != 202 && !restartTask.scheduled() && !restartTask.active()) { coreTaskManager.resume(); sensorsTaskManager.resume(); @@ -374,7 +374,7 @@ static void restApi() { // restart webServer->on("/api/restart", HTTP_POST, [](AsyncWebServerRequest *request) { - if (vars.states.upgrading || restartTask.scheduled() || restartTask.active()) { + if (upgrading || restartTask.scheduled() || restartTask.active()) { return request->send(503); } @@ -398,7 +398,7 @@ static void restApi() { }).addMiddleware(&authMiddleware); auto& nsHandler = webServer->on("/api/network/settings", HTTP_POST, [](AsyncWebServerRequest *request, JsonVariant &requestDoc) { - if (vars.states.upgrading || restartTask.scheduled() || restartTask.active()) { + if (upgrading || restartTask.scheduled() || restartTask.active()) { return request->send(503); } @@ -532,7 +532,7 @@ static void restApi() { }).addMiddleware(&authMiddleware); auto& settingsHandler = webServer->on(AsyncURIMatcher::prefix("/api/settings/"), HTTP_POST, [](AsyncWebServerRequest *request, JsonVariant &requestDoc) { - if (vars.states.upgrading || restartTask.scheduled() || restartTask.active()) { + if (upgrading || restartTask.scheduled() || restartTask.active()) { request->send(503); return; } @@ -712,7 +712,7 @@ static void restApi() { }).addMiddleware(&authMiddleware); auto& ssHandler = webServer->on(AsyncURIMatcher::exact("/api/sensor"), HTTP_POST, [](AsyncWebServerRequest *request, JsonVariant &requestDoc) { - if (vars.states.upgrading || restartTask.scheduled() || restartTask.active()) { + if (upgrading || restartTask.scheduled() || restartTask.active()) { return request->send(503); } @@ -818,7 +818,7 @@ static void restApi() { }).addMiddleware(&authMiddleware); auto& sensorValueHandler = webServer->on(AsyncURIMatcher::exact("/api/sensor/value"), HTTP_POST, [](AsyncWebServerRequest *request, JsonVariant &requestDoc) { - if (vars.states.upgrading || restartTask.scheduled() || restartTask.active()) { + if (upgrading || restartTask.scheduled() || restartTask.active()) { return request->send(503); } @@ -881,7 +881,7 @@ static void restApi() { }); auto& vHandler = webServer->on("/api/vars", HTTP_POST, [](AsyncWebServerRequest *request, JsonVariant &requestDoc) { - if (vars.states.upgrading || restartTask.scheduled() || restartTask.active()) { + if (upgrading || restartTask.scheduled() || restartTask.active()) { return request->send(503); } diff --git a/src/sensors.cpp b/src/sensors.cpp index 2bda7ee..132baa8 100644 --- a/src/sensors.cpp +++ b/src/sensors.cpp @@ -770,7 +770,7 @@ static void updateConnectionStatus() { } static bool canRunSensors() { - return !vars.states.upgrading && !restartTask.scheduled() && !restartTask.active(); + return !upgrading && !restartTask.scheduled() && !restartTask.active(); } static Task readDallasSensorsTask("Read Dallas sensors", [](void*) { diff --git a/src/system.cpp b/src/system.cpp index 9b6c7db..bf3ced7 100644 --- a/src/system.cpp +++ b/src/system.cpp @@ -94,7 +94,7 @@ static Task heapTask("Heap monitoring", [](void*) { unsigned int maxFreeBlockHeap = memory.heap.largest_free_block; /*if (freeHeap < 2048 || maxFreeBlockHeap < 2048) { - if (!vars.states.upgrading) { + if (!upgrading) { esp_system_abort("OTGateway: low internal heap"); } }*/