Replace buffer lock magic numbers with defines (#5217)
* replace magic numbers with defines
This commit is contained in:
@@ -124,7 +124,7 @@ public:
|
|||||||
char objKey[14];
|
char objKey[14];
|
||||||
bool parsed = false;
|
bool parsed = false;
|
||||||
|
|
||||||
if (!requestJSONBufferLock(22)) return false;
|
if (!requestJSONBufferLock(JSON_LOCK_REMOTE)) return false;
|
||||||
|
|
||||||
sprintf_P(objKey, PSTR("\"%d\":"), button);
|
sprintf_P(objKey, PSTR("\"%d\":"), button);
|
||||||
|
|
||||||
|
|||||||
@@ -74,7 +74,7 @@ void WS2812FX::setUpMatrix() {
|
|||||||
size_t gapSize = 0;
|
size_t gapSize = 0;
|
||||||
int8_t *gapTable = nullptr;
|
int8_t *gapTable = nullptr;
|
||||||
|
|
||||||
if (isFile && requestJSONBufferLock(20)) {
|
if (isFile && requestJSONBufferLock(JSON_LOCK_LEDGAP)) {
|
||||||
DEBUG_PRINT(F("Reading LED gap from "));
|
DEBUG_PRINT(F("Reading LED gap from "));
|
||||||
DEBUG_PRINTLN(fileName);
|
DEBUG_PRINTLN(fileName);
|
||||||
// read the array into global JSON buffer
|
// read the array into global JSON buffer
|
||||||
|
|||||||
@@ -1987,7 +1987,7 @@ bool WS2812FX::deserializeMap(unsigned n) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!isFile || !requestJSONBufferLock(7)) return false;
|
if (!isFile || !requestJSONBufferLock(JSON_LOCK_LEDMAP)) return false;
|
||||||
|
|
||||||
StaticJsonDocument<64> filter;
|
StaticJsonDocument<64> filter;
|
||||||
filter[F("width")] = true;
|
filter[F("width")] = true;
|
||||||
|
|||||||
@@ -791,7 +791,7 @@ void resetConfig() {
|
|||||||
bool deserializeConfigFromFS() {
|
bool deserializeConfigFromFS() {
|
||||||
[[maybe_unused]] bool success = deserializeConfigSec();
|
[[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..."));
|
DEBUG_PRINTLN(F("Reading settings from /cfg.json..."));
|
||||||
|
|
||||||
@@ -812,7 +812,7 @@ void serializeConfigToFS() {
|
|||||||
|
|
||||||
DEBUG_PRINTLN(F("Writing settings to /cfg.json..."));
|
DEBUG_PRINTLN(F("Writing settings to /cfg.json..."));
|
||||||
|
|
||||||
if (!requestJSONBufferLock(2)) return;
|
if (!requestJSONBufferLock(JSON_LOCK_CFG_SER)) return;
|
||||||
|
|
||||||
JsonObject root = pDoc->to<JsonObject>();
|
JsonObject root = pDoc->to<JsonObject>();
|
||||||
|
|
||||||
@@ -1256,7 +1256,7 @@ static const char s_wsec_json[] PROGMEM = "/wsec.json";
|
|||||||
bool deserializeConfigSec() {
|
bool deserializeConfigSec() {
|
||||||
DEBUG_PRINTLN(F("Reading settings from /wsec.json..."));
|
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);
|
bool success = readObjectFromFile(s_wsec_json, nullptr, pDoc);
|
||||||
if (!success) {
|
if (!success) {
|
||||||
@@ -1310,7 +1310,7 @@ bool deserializeConfigSec() {
|
|||||||
void serializeConfigSec() {
|
void serializeConfigSec() {
|
||||||
DEBUG_PRINTLN(F("Writing settings to /wsec.json..."));
|
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>();
|
JsonObject root = pDoc->to<JsonObject>();
|
||||||
|
|
||||||
|
|||||||
@@ -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_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)
|
#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
|
// Timer mode types
|
||||||
#define NL_MODE_SET 0 //After nightlight time elapsed, set to target brightness
|
#define NL_MODE_SET 0 //After nightlight time elapsed, set to target brightness
|
||||||
#define NL_MODE_FADE 1 //Fade to target brightness gradually
|
#define NL_MODE_FADE 1 //Fade to target brightness gradually
|
||||||
|
|||||||
@@ -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);
|
size_t printSetClassElementHTML(Print& settingsScript, const char* key, const int index, const char* val);
|
||||||
void prepareHostname(char* hostname);
|
void prepareHostname(char* hostname);
|
||||||
[[gnu::pure]] bool isAsterisksOnly(const char* str, byte maxLen);
|
[[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();
|
void releaseJSONBufferLock();
|
||||||
uint8_t extractModeName(uint8_t mode, const char *src, char *dest, uint8_t maxLen);
|
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);
|
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 {
|
class JSONBufferGuard {
|
||||||
bool holding_lock;
|
bool holding_lock;
|
||||||
public:
|
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() { if (holding_lock) releaseJSONBufferLock(); };
|
||||||
inline JSONBufferGuard(const JSONBufferGuard&) = delete; // Noncopyable
|
inline JSONBufferGuard(const JSONBufferGuard&) = delete; // Noncopyable
|
||||||
inline JSONBufferGuard& operator=(const JSONBufferGuard&) = delete;
|
inline JSONBufferGuard& operator=(const JSONBufferGuard&) = delete;
|
||||||
|
|||||||
@@ -559,7 +559,7 @@ static void decodeIRJson(uint32_t code)
|
|||||||
JsonObject fdo;
|
JsonObject fdo;
|
||||||
JsonObject jsonCmdObj;
|
JsonObject jsonCmdObj;
|
||||||
|
|
||||||
if (!requestJSONBufferLock(13)) return;
|
if (!requestJSONBufferLock(JSON_LOCK_IR)) return;
|
||||||
|
|
||||||
sprintf_P(objKey, PSTR("\"0x%lX\":"), (unsigned long)code);
|
sprintf_P(objKey, PSTR("\"0x%lX\":"), (unsigned long)code);
|
||||||
strcpy_P(fileName, PSTR("/ir.json")); // for FS.exists()
|
strcpy_P(fileName, PSTR("/ir.json")); // for FS.exists()
|
||||||
|
|||||||
@@ -1142,7 +1142,7 @@ void serveJson(AsyncWebServerRequest* request)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!requestJSONBufferLock(17)) {
|
if (!requestJSONBufferLock(JSON_LOCK_SERVEJSON)) {
|
||||||
request->deferResponse();
|
request->deferResponse();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -113,7 +113,7 @@ static void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProp
|
|||||||
colorFromDecOrHexString(colPri, payloadStr);
|
colorFromDecOrHexString(colPri, payloadStr);
|
||||||
colorUpdated(CALL_MODE_DIRECT_CHANGE);
|
colorUpdated(CALL_MODE_DIRECT_CHANGE);
|
||||||
} else if (strcmp_P(topic, PSTR("/api")) == 0) {
|
} else if (strcmp_P(topic, PSTR("/api")) == 0) {
|
||||||
if (requestJSONBufferLock(15)) {
|
if (requestJSONBufferLock(JSON_LOCK_MQTT)) {
|
||||||
if (payloadStr[0] == '{') { //JSON API
|
if (payloadStr[0] == '{') { //JSON API
|
||||||
deserializeJson(*pDoc, payloadStr);
|
deserializeJson(*pDoc, payloadStr);
|
||||||
deserializeState(pDoc->as<JsonObject>());
|
deserializeState(pDoc->as<JsonObject>());
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ static void doSaveState() {
|
|||||||
unsigned long maxWait = millis() + strip.getFrameTime();
|
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
|
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
|
initPresetsFile(); // just in case if someone deleted presets.json using /edit
|
||||||
JsonObject sObj = pDoc->to<JsonObject>();
|
JsonObject sObj = pDoc->to<JsonObject>();
|
||||||
@@ -86,7 +86,7 @@ static void doSaveState() {
|
|||||||
|
|
||||||
bool getPresetName(byte index, String& name)
|
bool getPresetName(byte index, String& name)
|
||||||
{
|
{
|
||||||
if (!requestJSONBufferLock(19)) return false;
|
if (!requestJSONBufferLock(JSON_LOCK_PRESET_NAME)) return false;
|
||||||
bool presetExists = false;
|
bool presetExists = false;
|
||||||
if (readObjectFromFileUsingId(getPresetsFileName(), index, pDoc)) {
|
if (readObjectFromFileUsingId(getPresetsFileName(), index, pDoc)) {
|
||||||
JsonObject fdo = pDoc->as<JsonObject>();
|
JsonObject fdo = pDoc->as<JsonObject>();
|
||||||
@@ -152,7 +152,7 @@ void handlePresets()
|
|||||||
return;
|
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;
|
bool changePreset = false;
|
||||||
uint8_t tmpPreset = presetToApply; // store temporary since deserializeState() may call applyPreset()
|
uint8_t tmpPreset = presetToApply; // store temporary since deserializeState() may call applyPreset()
|
||||||
|
|||||||
@@ -120,7 +120,7 @@ static bool remoteJson(int button)
|
|||||||
char objKey[10];
|
char objKey[10];
|
||||||
bool parsed = false;
|
bool parsed = false;
|
||||||
|
|
||||||
if (!requestJSONBufferLock(22)) return false;
|
if (!requestJSONBufferLock(JSON_LOCK_REMOTE)) return false;
|
||||||
|
|
||||||
sprintf_P(objKey, PSTR("\"%d\":"), button);
|
sprintf_P(objKey, PSTR("\"%d\":"), button);
|
||||||
|
|
||||||
|
|||||||
@@ -651,7 +651,7 @@ void handleSettingsSet(AsyncWebServerRequest *request, byte subPage)
|
|||||||
//USERMODS
|
//USERMODS
|
||||||
if (subPage == SUBPAGE_UM)
|
if (subPage == SUBPAGE_UM)
|
||||||
{
|
{
|
||||||
if (!requestJSONBufferLock(5)) {
|
if (!requestJSONBufferLock(JSON_LOCK_SETTINGS)) {
|
||||||
request->deferResponse();
|
request->deferResponse();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -646,7 +646,7 @@ void handleNotifications()
|
|||||||
// API over UDP
|
// API over UDP
|
||||||
udpIn[packetSize] = '\0';
|
udpIn[packetSize] = '\0';
|
||||||
|
|
||||||
if (requestJSONBufferLock(18)) {
|
if (requestJSONBufferLock(JSON_LOCK_NOTIFY)) {
|
||||||
if (udpIn[0] >= 'A' && udpIn[0] <= 'Z') { //HTTP API
|
if (udpIn[0] >= 'A' && udpIn[0] <= 'Z') { //HTTP API
|
||||||
String apireq = "win"; apireq += '&'; // reduce flash string usage
|
String apireq = "win"; apireq += '&'; // reduce flash string usage
|
||||||
apireq += (char*)udpIn;
|
apireq += (char*)udpIn;
|
||||||
|
|||||||
@@ -561,7 +561,7 @@ void enumerateLedmaps() {
|
|||||||
ledMaps |= 1 << i;
|
ledMaps |= 1 << i;
|
||||||
|
|
||||||
#ifndef ESP8266
|
#ifndef ESP8266
|
||||||
if (requestJSONBufferLock(21)) {
|
if (requestJSONBufferLock(JSON_LOCK_LEDMAP_ENUM)) {
|
||||||
if (readObjectFromFile(fileName, nullptr, pDoc, &filter)) {
|
if (readObjectFromFile(fileName, nullptr, pDoc, &filter)) {
|
||||||
size_t len = 0;
|
size_t len = 0;
|
||||||
JsonObject root = pDoc->as<JsonObject>();
|
JsonObject root = pDoc->as<JsonObject>();
|
||||||
|
|||||||
@@ -102,7 +102,7 @@ void handleSerial()
|
|||||||
else if (next == 'O') { continuousSendLED = true; } // Enable Continuous Serial Streaming
|
else if (next == 'O') { continuousSendLED = true; } // Enable Continuous Serial Streaming
|
||||||
else if (next == '{') { //JSON API
|
else if (next == '{') { //JSON API
|
||||||
bool verboseResponse = false;
|
bool verboseResponse = false;
|
||||||
if (!requestJSONBufferLock(16)) {
|
if (!requestJSONBufferLock(JSON_LOCK_SERIAL)) {
|
||||||
Serial.printf_P(PSTR("{\"error\":%d}\n"), ERR_NOBUF);
|
Serial.printf_P(PSTR("{\"error\":%d}\n"), ERR_NOBUF);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -395,7 +395,7 @@ void initServer()
|
|||||||
bool verboseResponse = false;
|
bool verboseResponse = false;
|
||||||
bool isConfig = false;
|
bool isConfig = false;
|
||||||
|
|
||||||
if (!requestJSONBufferLock(14)) {
|
if (!requestJSONBufferLock(JSON_LOCK_SERVER)) {
|
||||||
request->deferResponse();
|
request->deferResponse();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -42,7 +42,7 @@ void wsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventTyp
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool verboseResponse = false;
|
bool verboseResponse = false;
|
||||||
if (!requestJSONBufferLock(11)) {
|
if (!requestJSONBufferLock(JSON_LOCK_WS_RECEIVE)) {
|
||||||
client->text(F("{\"error\":3}")); // ERR_NOBUF
|
client->text(F("{\"error\":3}")); // ERR_NOBUF
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -136,7 +136,7 @@ void sendDataWs(AsyncWebSocketClient * client)
|
|||||||
{
|
{
|
||||||
if (!ws.count()) return;
|
if (!ws.count()) return;
|
||||||
|
|
||||||
if (!requestJSONBufferLock(12)) {
|
if (!requestJSONBufferLock(JSON_LOCK_WS_SEND)) {
|
||||||
const char* error = PSTR("{\"error\":3}");
|
const char* error = PSTR("{\"error\":3}");
|
||||||
if (client) {
|
if (client) {
|
||||||
client->text(FPSTR(error)); // ERR_NOBUF
|
client->text(FPSTR(error)); // ERR_NOBUF
|
||||||
|
|||||||
@@ -99,7 +99,7 @@ void appendGPIOinfo(Print& settingsScript)
|
|||||||
settingsScript.printf_P(PSTR(",%d,%d"), spi_mosi, spi_sclk);
|
settingsScript.printf_P(PSTR(",%d,%d"), spi_mosi, spi_sclk);
|
||||||
}
|
}
|
||||||
// usermod pin reservations will become unnecessary when settings pages will read cfg.json directly
|
// 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
|
// if we can't allocate JSON buffer ignore usermod pins
|
||||||
JsonObject mods = pDoc->createNestedObject("um");
|
JsonObject mods = pDoc->createNestedObject("um");
|
||||||
UsermodManager::addToConfig(mods);
|
UsermodManager::addToConfig(mods);
|
||||||
|
|||||||
Reference in New Issue
Block a user