Replace buffer lock magic numbers with defines (#5217)

* replace magic numbers with defines
This commit is contained in:
Damian Schneider
2026-01-30 08:14:53 +01:00
committed by GitHub
parent c9f47d4b5c
commit 1031e70d70
18 changed files with 49 additions and 24 deletions

View File

@@ -124,7 +124,7 @@ public:
char objKey[14];
bool parsed = false;
if (!requestJSONBufferLock(22)) return false;
if (!requestJSONBufferLock(JSON_LOCK_REMOTE)) return false;
sprintf_P(objKey, PSTR("\"%d\":"), button);

View File

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

View File

@@ -1987,7 +1987,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;

View File

@@ -791,7 +791,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..."));
@@ -812,7 +812,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<JsonObject>();
@@ -1256,7 +1256,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) {
@@ -1310,7 +1310,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<JsonObject>();

View File

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

View File

@@ -395,7 +395,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);
@@ -487,7 +487,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;

View File

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

View File

@@ -1142,7 +1142,7 @@ void serveJson(AsyncWebServerRequest* request)
return;
}
if (!requestJSONBufferLock(17)) {
if (!requestJSONBufferLock(JSON_LOCK_SERVEJSON)) {
request->deferResponse();
return;
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -395,7 +395,7 @@ void initServer()
bool verboseResponse = false;
bool isConfig = false;
if (!requestJSONBufferLock(14)) {
if (!requestJSONBufferLock(JSON_LOCK_SERVER)) {
request->deferResponse();
return;
}

View File

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

View File

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