Skip to content

Commit 1031e70

Browse files
authored
Replace buffer lock magic numbers with defines (#5217)
* replace magic numbers with defines
1 parent c9f47d4 commit 1031e70

18 files changed

Lines changed: 49 additions & 24 deletions

usermods/usermod_v2_RF433/usermod_v2_RF433.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ class RF433Usermod : public Usermod
124124
char objKey[14];
125125
bool parsed = false;
126126

127-
if (!requestJSONBufferLock(22)) return false;
127+
if (!requestJSONBufferLock(JSON_LOCK_REMOTE)) return false;
128128

129129
sprintf_P(objKey, PSTR("\"%d\":"), button);
130130

wled00/FX_2Dfcn.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ void WS2812FX::setUpMatrix() {
7474
size_t gapSize = 0;
7575
int8_t *gapTable = nullptr;
7676

77-
if (isFile && requestJSONBufferLock(20)) {
77+
if (isFile && requestJSONBufferLock(JSON_LOCK_LEDGAP)) {
7878
DEBUG_PRINT(F("Reading LED gap from "));
7979
DEBUG_PRINTLN(fileName);
8080
// read the array into global JSON buffer

wled00/FX_fcn.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1987,7 +1987,7 @@ bool WS2812FX::deserializeMap(unsigned n) {
19871987
return false;
19881988
}
19891989

1990-
if (!isFile || !requestJSONBufferLock(7)) return false;
1990+
if (!isFile || !requestJSONBufferLock(JSON_LOCK_LEDMAP)) return false;
19911991

19921992
StaticJsonDocument<64> filter;
19931993
filter[F("width")] = true;

wled00/cfg.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -791,7 +791,7 @@ void resetConfig() {
791791
bool deserializeConfigFromFS() {
792792
[[maybe_unused]] bool success = deserializeConfigSec();
793793

794-
if (!requestJSONBufferLock(1)) return false;
794+
if (!requestJSONBufferLock(JSON_LOCK_CFG_DES)) return false;
795795

796796
DEBUG_PRINTLN(F("Reading settings from /cfg.json..."));
797797

@@ -812,7 +812,7 @@ void serializeConfigToFS() {
812812

813813
DEBUG_PRINTLN(F("Writing settings to /cfg.json..."));
814814

815-
if (!requestJSONBufferLock(2)) return;
815+
if (!requestJSONBufferLock(JSON_LOCK_CFG_SER)) return;
816816

817817
JsonObject root = pDoc->to<JsonObject>();
818818

@@ -1256,7 +1256,7 @@ static const char s_wsec_json[] PROGMEM = "/wsec.json";
12561256
bool deserializeConfigSec() {
12571257
DEBUG_PRINTLN(F("Reading settings from /wsec.json..."));
12581258

1259-
if (!requestJSONBufferLock(3)) return false;
1259+
if (!requestJSONBufferLock(JSON_LOCK_CFG_SEC_DES)) return false;
12601260

12611261
bool success = readObjectFromFile(s_wsec_json, nullptr, pDoc);
12621262
if (!success) {
@@ -1310,7 +1310,7 @@ bool deserializeConfigSec() {
13101310
void serializeConfigSec() {
13111311
DEBUG_PRINTLN(F("Writing settings to /wsec.json..."));
13121312

1313-
if (!requestJSONBufferLock(4)) return;
1313+
if (!requestJSONBufferLock(JSON_LOCK_CFG_SEC_SER)) return;
13141314

13151315
JsonObject root = pDoc->to<JsonObject>();
13161316

wled00/const.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,31 @@ static_assert(WLED_MAX_BUSSES <= 32, "WLED_MAX_BUSSES exceeds hard limit");
441441
#define ERR_OVERCURRENT 31 // An attached current sensor has measured a current above the threshold (not implemented)
442442
#define ERR_UNDERVOLT 32 // An attached voltmeter has measured a voltage below the threshold (not implemented)
443443

444+
// JSON buffer lock owners
445+
#define JSON_LOCK_UNKNOWN 255
446+
#define JSON_LOCK_CFG_DES 1
447+
#define JSON_LOCK_CFG_SER 2
448+
#define JSON_LOCK_CFG_SEC_DES 3
449+
#define JSON_LOCK_CFG_SEC_SER 4
450+
#define JSON_LOCK_SETTINGS 5
451+
#define JSON_LOCK_XML 6
452+
#define JSON_LOCK_LEDMAP 7
453+
// unused 8
454+
#define JSON_LOCK_PRESET_LOAD 9
455+
#define JSON_LOCK_PRESET_SAVE 10
456+
#define JSON_LOCK_WS_RECEIVE 11
457+
#define JSON_LOCK_WS_SEND 12
458+
#define JSON_LOCK_IR 13
459+
#define JSON_LOCK_SERVER 14
460+
#define JSON_LOCK_MQTT 15
461+
#define JSON_LOCK_SERIAL 16
462+
#define JSON_LOCK_SERVEJSON 17
463+
#define JSON_LOCK_NOTIFY 18
464+
#define JSON_LOCK_PRESET_NAME 19
465+
#define JSON_LOCK_LEDGAP 20
466+
#define JSON_LOCK_LEDMAP_ENUM 21
467+
#define JSON_LOCK_REMOTE 22
468+
444469
// Timer mode types
445470
#define NL_MODE_SET 0 //After nightlight time elapsed, set to target brightness
446471
#define NL_MODE_FADE 1 //Fade to target brightness gradually

wled00/fcn_declare.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ size_t printSetFormIndex(Print& settingsScript, const char* key, int index);
395395
size_t printSetClassElementHTML(Print& settingsScript, const char* key, const int index, const char* val);
396396
void prepareHostname(char* hostname);
397397
[[gnu::pure]] bool isAsterisksOnly(const char* str, byte maxLen);
398-
bool requestJSONBufferLock(uint8_t moduleID=255);
398+
bool requestJSONBufferLock(uint8_t moduleID=JSON_LOCK_UNKNOWN);
399399
void releaseJSONBufferLock();
400400
uint8_t extractModeName(uint8_t mode, const char *src, char *dest, uint8_t maxLen);
401401
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
487487
class JSONBufferGuard {
488488
bool holding_lock;
489489
public:
490-
inline JSONBufferGuard(uint8_t module=255) : holding_lock(requestJSONBufferLock(module)) {};
490+
inline JSONBufferGuard(uint8_t module=JSON_LOCK_UNKNOWN) : holding_lock(requestJSONBufferLock(module)) {};
491491
inline ~JSONBufferGuard() { if (holding_lock) releaseJSONBufferLock(); };
492492
inline JSONBufferGuard(const JSONBufferGuard&) = delete; // Noncopyable
493493
inline JSONBufferGuard& operator=(const JSONBufferGuard&) = delete;

wled00/ir.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,7 @@ static void decodeIRJson(uint32_t code)
559559
JsonObject fdo;
560560
JsonObject jsonCmdObj;
561561

562-
if (!requestJSONBufferLock(13)) return;
562+
if (!requestJSONBufferLock(JSON_LOCK_IR)) return;
563563

564564
sprintf_P(objKey, PSTR("\"0x%lX\":"), (unsigned long)code);
565565
strcpy_P(fileName, PSTR("/ir.json")); // for FS.exists()

wled00/json.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1142,7 +1142,7 @@ void serveJson(AsyncWebServerRequest* request)
11421142
return;
11431143
}
11441144

1145-
if (!requestJSONBufferLock(17)) {
1145+
if (!requestJSONBufferLock(JSON_LOCK_SERVEJSON)) {
11461146
request->deferResponse();
11471147
return;
11481148
}

wled00/mqtt.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ static void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProp
113113
colorFromDecOrHexString(colPri, payloadStr);
114114
colorUpdated(CALL_MODE_DIRECT_CHANGE);
115115
} else if (strcmp_P(topic, PSTR("/api")) == 0) {
116-
if (requestJSONBufferLock(15)) {
116+
if (requestJSONBufferLock(JSON_LOCK_MQTT)) {
117117
if (payloadStr[0] == '{') { //JSON API
118118
deserializeJson(*pDoc, payloadStr);
119119
deserializeState(pDoc->as<JsonObject>());

wled00/presets.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ static void doSaveState() {
3232
unsigned long maxWait = millis() + strip.getFrameTime();
3333
while (strip.isUpdating() && millis() < maxWait) delay(1); // wait for strip to finish updating, accessing FS during sendout causes glitches
3434

35-
if (!requestJSONBufferLock(10)) return;
35+
if (!requestJSONBufferLock(JSON_LOCK_PRESET_SAVE)) return;
3636

3737
initPresetsFile(); // just in case if someone deleted presets.json using /edit
3838
JsonObject sObj = pDoc->to<JsonObject>();
@@ -86,7 +86,7 @@ static void doSaveState() {
8686

8787
bool getPresetName(byte index, String& name)
8888
{
89-
if (!requestJSONBufferLock(19)) return false;
89+
if (!requestJSONBufferLock(JSON_LOCK_PRESET_NAME)) return false;
9090
bool presetExists = false;
9191
if (readObjectFromFileUsingId(getPresetsFileName(), index, pDoc)) {
9292
JsonObject fdo = pDoc->as<JsonObject>();
@@ -152,7 +152,7 @@ void handlePresets()
152152
return;
153153
}
154154

155-
if (presetToApply == 0 || !requestJSONBufferLock(9)) return; // no preset waiting to apply, or JSON buffer is already allocated, return to loop until free
155+
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
156156

157157
bool changePreset = false;
158158
uint8_t tmpPreset = presetToApply; // store temporary since deserializeState() may call applyPreset()

0 commit comments

Comments
 (0)