diff --git a/usermods/usermod_v2_RF433/usermod_v2_RF433.cpp b/usermods/usermod_v2_RF433/usermod_v2_RF433.cpp index 9ac6c416d1..ed3d5eb26f 100644 --- a/usermods/usermod_v2_RF433/usermod_v2_RF433.cpp +++ b/usermods/usermod_v2_RF433/usermod_v2_RF433.cpp @@ -124,7 +124,7 @@ class RF433Usermod : public Usermod char objKey[14]; bool parsed = false; - if (!requestJSONBufferLock(22)) return false; + if (!requestJSONBufferLock(JSON_LOCK_REMOTE)) return false; sprintf_P(objKey, PSTR("\"%d\":"), button); diff --git a/wled00/FX_2Dfcn.cpp b/wled00/FX_2Dfcn.cpp index 063d3a6bb3..34b619ea36 100644 --- a/wled00/FX_2Dfcn.cpp +++ b/wled00/FX_2Dfcn.cpp @@ -74,7 +74,7 @@ void WS2812FX::setUpMatrix() { size_t gapSize = 0; int8_t *gapTable = nullptr; - if (isFile && requestJSONBufferLock(20)) { + if (isFile && requestJSONBufferLock(JSON_LOCK_LEDGAP)) { DEBUG_PRINT(F("Reading LED gap from ")); DEBUG_PRINTLN(fileName); // read the array into global JSON buffer diff --git a/wled00/FX_fcn.cpp b/wled00/FX_fcn.cpp index 3f357daa29..a9b1967889 100644 --- a/wled00/FX_fcn.cpp +++ b/wled00/FX_fcn.cpp @@ -1982,7 +1982,7 @@ bool WS2812FX::deserializeMap(unsigned n) { return false; } - if (!isFile || !requestJSONBufferLock(7)) return false; + if (!isFile || !requestJSONBufferLock(JSON_LOCK_LEDMAP)) return false; StaticJsonDocument<64> filter; filter[F("width")] = true; diff --git a/wled00/cfg.cpp b/wled00/cfg.cpp index e30be759b6..f799b555af 100644 --- a/wled00/cfg.cpp +++ b/wled00/cfg.cpp @@ -790,7 +790,7 @@ void resetConfig() { bool deserializeConfigFromFS() { [[maybe_unused]] bool success = deserializeConfigSec(); - if (!requestJSONBufferLock(1)) return false; + if (!requestJSONBufferLock(JSON_LOCK_CFG_DES)) return false; DEBUG_PRINTLN(F("Reading settings from /cfg.json...")); @@ -811,7 +811,7 @@ void serializeConfigToFS() { DEBUG_PRINTLN(F("Writing settings to /cfg.json...")); - if (!requestJSONBufferLock(2)) return; + if (!requestJSONBufferLock(JSON_LOCK_CFG_SER)) return; JsonObject root = pDoc->to(); @@ -1255,7 +1255,7 @@ static const char s_wsec_json[] PROGMEM = "/wsec.json"; bool deserializeConfigSec() { DEBUG_PRINTLN(F("Reading settings from /wsec.json...")); - if (!requestJSONBufferLock(3)) return false; + if (!requestJSONBufferLock(JSON_LOCK_CFG_SEC_DES)) return false; bool success = readObjectFromFile(s_wsec_json, nullptr, pDoc); if (!success) { @@ -1309,7 +1309,7 @@ bool deserializeConfigSec() { void serializeConfigSec() { DEBUG_PRINTLN(F("Writing settings to /wsec.json...")); - if (!requestJSONBufferLock(4)) return; + if (!requestJSONBufferLock(JSON_LOCK_CFG_SEC_SER)) return; JsonObject root = pDoc->to(); diff --git a/wled00/const.h b/wled00/const.h index 9067d9b16c..3c50ddbcc0 100644 --- a/wled00/const.h +++ b/wled00/const.h @@ -441,6 +441,31 @@ static_assert(WLED_MAX_BUSSES <= 32, "WLED_MAX_BUSSES exceeds hard limit"); #define ERR_OVERCURRENT 31 // An attached current sensor has measured a current above the threshold (not implemented) #define ERR_UNDERVOLT 32 // An attached voltmeter has measured a voltage below the threshold (not implemented) +// JSON buffer lock owners +#define JSON_LOCK_UNKNOWN 255 +#define JSON_LOCK_CFG_DES 1 +#define JSON_LOCK_CFG_SER 2 +#define JSON_LOCK_CFG_SEC_DES 3 +#define JSON_LOCK_CFG_SEC_SER 4 +#define JSON_LOCK_SETTINGS 5 +#define JSON_LOCK_XML 6 +#define JSON_LOCK_LEDMAP 7 +// unused 8 +#define JSON_LOCK_PRESET_LOAD 9 +#define JSON_LOCK_PRESET_SAVE 10 +#define JSON_LOCK_WS_RECEIVE 11 +#define JSON_LOCK_WS_SEND 12 +#define JSON_LOCK_IR 13 +#define JSON_LOCK_SERVER 14 +#define JSON_LOCK_MQTT 15 +#define JSON_LOCK_SERIAL 16 +#define JSON_LOCK_SERVEJSON 17 +#define JSON_LOCK_NOTIFY 18 +#define JSON_LOCK_PRESET_NAME 19 +#define JSON_LOCK_LEDGAP 20 +#define JSON_LOCK_LEDMAP_ENUM 21 +#define JSON_LOCK_REMOTE 22 + // Timer mode types #define NL_MODE_SET 0 //After nightlight time elapsed, set to target brightness #define NL_MODE_FADE 1 //Fade to target brightness gradually diff --git a/wled00/fcn_declare.h b/wled00/fcn_declare.h index 4bbedfacb0..86e50278fc 100644 --- a/wled00/fcn_declare.h +++ b/wled00/fcn_declare.h @@ -394,7 +394,7 @@ size_t printSetFormIndex(Print& settingsScript, const char* key, int index); size_t printSetClassElementHTML(Print& settingsScript, const char* key, const int index, const char* val); void prepareHostname(char* hostname); [[gnu::pure]] bool isAsterisksOnly(const char* str, byte maxLen); -bool requestJSONBufferLock(uint8_t moduleID=255); +bool requestJSONBufferLock(uint8_t moduleID=JSON_LOCK_UNKNOWN); void releaseJSONBufferLock(); uint8_t extractModeName(uint8_t mode, const char *src, char *dest, uint8_t maxLen); uint8_t extractModeSlider(uint8_t mode, uint8_t slider, char *dest, uint8_t maxLen, uint8_t *var = nullptr); @@ -486,7 +486,7 @@ void bootloopCheckOTA(); // swap boot image if bootloop is detected instead of r class JSONBufferGuard { bool holding_lock; public: - inline JSONBufferGuard(uint8_t module=255) : holding_lock(requestJSONBufferLock(module)) {}; + inline JSONBufferGuard(uint8_t module=JSON_LOCK_UNKNOWN) : holding_lock(requestJSONBufferLock(module)) {}; inline ~JSONBufferGuard() { if (holding_lock) releaseJSONBufferLock(); }; inline JSONBufferGuard(const JSONBufferGuard&) = delete; // Noncopyable inline JSONBufferGuard& operator=(const JSONBufferGuard&) = delete; diff --git a/wled00/ir.cpp b/wled00/ir.cpp index b2fec76f1f..fe0950ab14 100644 --- a/wled00/ir.cpp +++ b/wled00/ir.cpp @@ -559,7 +559,7 @@ static void decodeIRJson(uint32_t code) JsonObject fdo; JsonObject jsonCmdObj; - if (!requestJSONBufferLock(13)) return; + if (!requestJSONBufferLock(JSON_LOCK_IR)) return; sprintf_P(objKey, PSTR("\"0x%lX\":"), (unsigned long)code); strcpy_P(fileName, PSTR("/ir.json")); // for FS.exists() diff --git a/wled00/json.cpp b/wled00/json.cpp index f23080135f..6f5a5fc764 100644 --- a/wled00/json.cpp +++ b/wled00/json.cpp @@ -1140,7 +1140,7 @@ void serveJson(AsyncWebServerRequest* request) return; } - if (!requestJSONBufferLock(17)) { + if (!requestJSONBufferLock(JSON_LOCK_SERVEJSON)) { request->deferResponse(); return; } diff --git a/wled00/mqtt.cpp b/wled00/mqtt.cpp index ea42297bf7..d64dc29d9b 100644 --- a/wled00/mqtt.cpp +++ b/wled00/mqtt.cpp @@ -113,7 +113,7 @@ static void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProp colorFromDecOrHexString(colPri, payloadStr); colorUpdated(CALL_MODE_DIRECT_CHANGE); } else if (strcmp_P(topic, PSTR("/api")) == 0) { - if (requestJSONBufferLock(15)) { + if (requestJSONBufferLock(JSON_LOCK_MQTT)) { if (payloadStr[0] == '{') { //JSON API deserializeJson(*pDoc, payloadStr); deserializeState(pDoc->as()); diff --git a/wled00/presets.cpp b/wled00/presets.cpp index fed2c1ed92..9023baf344 100644 --- a/wled00/presets.cpp +++ b/wled00/presets.cpp @@ -32,7 +32,7 @@ static void doSaveState() { unsigned long maxWait = millis() + strip.getFrameTime(); while (strip.isUpdating() && millis() < maxWait) delay(1); // wait for strip to finish updating, accessing FS during sendout causes glitches - if (!requestJSONBufferLock(10)) return; + if (!requestJSONBufferLock(JSON_LOCK_PRESET_SAVE)) return; initPresetsFile(); // just in case if someone deleted presets.json using /edit JsonObject sObj = pDoc->to(); @@ -86,7 +86,7 @@ static void doSaveState() { bool getPresetName(byte index, String& name) { - if (!requestJSONBufferLock(19)) return false; + if (!requestJSONBufferLock(JSON_LOCK_PRESET_NAME)) return false; bool presetExists = false; if (readObjectFromFileUsingId(getPresetsFileName(), index, pDoc)) { JsonObject fdo = pDoc->as(); @@ -152,7 +152,7 @@ void handlePresets() return; } - if (presetToApply == 0 || !requestJSONBufferLock(9)) return; // no preset waiting to apply, or JSON buffer is already allocated, return to loop until free + if (presetToApply == 0 || !requestJSONBufferLock(JSON_LOCK_PRESET_LOAD)) return; // no preset waiting to apply, or JSON buffer is already allocated, return to loop until free bool changePreset = false; uint8_t tmpPreset = presetToApply; // store temporary since deserializeState() may call applyPreset() diff --git a/wled00/remote.cpp b/wled00/remote.cpp index 14c3c0d01d..2232498bec 100644 --- a/wled00/remote.cpp +++ b/wled00/remote.cpp @@ -120,7 +120,7 @@ static bool remoteJson(int button) char objKey[10]; bool parsed = false; - if (!requestJSONBufferLock(22)) return false; + if (!requestJSONBufferLock(JSON_LOCK_REMOTE)) return false; sprintf_P(objKey, PSTR("\"%d\":"), button); diff --git a/wled00/set.cpp b/wled00/set.cpp index 2a633bf07d..d0864acd1d 100644 --- a/wled00/set.cpp +++ b/wled00/set.cpp @@ -651,7 +651,7 @@ void handleSettingsSet(AsyncWebServerRequest *request, byte subPage) //USERMODS if (subPage == SUBPAGE_UM) { - if (!requestJSONBufferLock(5)) { + if (!requestJSONBufferLock(JSON_LOCK_SETTINGS)) { request->deferResponse(); return; } diff --git a/wled00/udp.cpp b/wled00/udp.cpp index 26a80ac349..82f025b211 100644 --- a/wled00/udp.cpp +++ b/wled00/udp.cpp @@ -646,7 +646,7 @@ void handleNotifications() // API over UDP udpIn[packetSize] = '\0'; - if (requestJSONBufferLock(18)) { + if (requestJSONBufferLock(JSON_LOCK_NOTIFY)) { if (udpIn[0] >= 'A' && udpIn[0] <= 'Z') { //HTTP API String apireq = "win"; apireq += '&'; // reduce flash string usage apireq += (char*)udpIn; diff --git a/wled00/util.cpp b/wled00/util.cpp index 1330bb6c7e..0e0c0f2093 100644 --- a/wled00/util.cpp +++ b/wled00/util.cpp @@ -561,7 +561,7 @@ void enumerateLedmaps() { ledMaps |= 1 << i; #ifndef ESP8266 - if (requestJSONBufferLock(21)) { + if (requestJSONBufferLock(JSON_LOCK_LEDMAP_ENUM)) { if (readObjectFromFile(fileName, nullptr, pDoc, &filter)) { size_t len = 0; JsonObject root = pDoc->as(); diff --git a/wled00/wled_serial.cpp b/wled00/wled_serial.cpp index a0e59c531f..7675976ba8 100644 --- a/wled00/wled_serial.cpp +++ b/wled00/wled_serial.cpp @@ -102,7 +102,7 @@ void handleSerial() else if (next == 'O') { continuousSendLED = true; } // Enable Continuous Serial Streaming else if (next == '{') { //JSON API bool verboseResponse = false; - if (!requestJSONBufferLock(16)) { + if (!requestJSONBufferLock(JSON_LOCK_SERIAL)) { Serial.printf_P(PSTR("{\"error\":%d}\n"), ERR_NOBUF); return; } diff --git a/wled00/wled_server.cpp b/wled00/wled_server.cpp index ffb259b858..ace5728347 100644 --- a/wled00/wled_server.cpp +++ b/wled00/wled_server.cpp @@ -395,7 +395,7 @@ void initServer() bool verboseResponse = false; bool isConfig = false; - if (!requestJSONBufferLock(14)) { + if (!requestJSONBufferLock(JSON_LOCK_SERVER)) { request->deferResponse(); return; } diff --git a/wled00/ws.cpp b/wled00/ws.cpp index 873261beec..6d74a5a0b8 100644 --- a/wled00/ws.cpp +++ b/wled00/ws.cpp @@ -42,7 +42,7 @@ void wsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventTyp } bool verboseResponse = false; - if (!requestJSONBufferLock(11)) { + if (!requestJSONBufferLock(JSON_LOCK_WS_RECEIVE)) { client->text(F("{\"error\":3}")); // ERR_NOBUF return; } @@ -136,7 +136,7 @@ void sendDataWs(AsyncWebSocketClient * client) { if (!ws.count()) return; - if (!requestJSONBufferLock(12)) { + if (!requestJSONBufferLock(JSON_LOCK_WS_SEND)) { const char* error = PSTR("{\"error\":3}"); if (client) { client->text(FPSTR(error)); // ERR_NOBUF diff --git a/wled00/xml.cpp b/wled00/xml.cpp index 194256d82e..4efdecaaac 100644 --- a/wled00/xml.cpp +++ b/wled00/xml.cpp @@ -99,7 +99,7 @@ void appendGPIOinfo(Print& settingsScript) settingsScript.printf_P(PSTR(",%d,%d"), spi_mosi, spi_sclk); } // usermod pin reservations will become unnecessary when settings pages will read cfg.json directly - if (requestJSONBufferLock(6)) { + if (requestJSONBufferLock(JSON_LOCK_XML)) { // if we can't allocate JSON buffer ignore usermod pins JsonObject mods = pDoc->createNestedObject("um"); UsermodManager::addToConfig(mods);