Refactor Config_Web to use heap-allocated JSON document and add stack usage check script
This commit is contained in:
12
scripts/check_config_web_stack_usage.sh
Executable file
12
scripts/check_config_web_stack_usage.sh
Executable file
@@ -0,0 +1,12 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
|
set -euo pipefail
|
||||||
|
|
||||||
|
cd "$(dirname "$0")/.."
|
||||||
|
|
||||||
|
if rg -n '^[[:space:]]+StaticJsonDocument<kConfigJsonCapacity>[[:space:]]+[A-Za-z_][A-Za-z0-9_]*;' src/Config_Web.cpp; then
|
||||||
|
echo "Config_Web.cpp still allocates kConfigJsonCapacity-sized StaticJsonDocument on the stack" >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "Config_Web.cpp avoids stack-local kConfigJsonCapacity StaticJsonDocument allocations"
|
||||||
@@ -9,6 +9,7 @@ namespace
|
|||||||
const size_t kConfigJsonCapacity = 4096;
|
const size_t kConfigJsonCapacity = 4096;
|
||||||
const byte kDnsPort = 53;
|
const byte kDnsPort = 53;
|
||||||
const unsigned long kWifiReconnectIntervalMs = 10000;
|
const unsigned long kWifiReconnectIntervalMs = 10000;
|
||||||
|
using ConfigJsonDocument = StaticJsonDocument<kConfigJsonCapacity>;
|
||||||
|
|
||||||
WebServer server(80);
|
WebServer server(80);
|
||||||
DNSServer dnsServer;
|
DNSServer dnsServer;
|
||||||
@@ -17,6 +18,14 @@ namespace
|
|||||||
bool staReconnectEnabled = false;
|
bool staReconnectEnabled = false;
|
||||||
bool staConnectionKnown = false;
|
bool staConnectionKnown = false;
|
||||||
unsigned long lastStaReconnectAttemptMs = 0;
|
unsigned long lastStaReconnectAttemptMs = 0;
|
||||||
|
ConfigJsonDocument g_configJsonScratch;
|
||||||
|
|
||||||
|
ConfigJsonDocument &configJsonScratch()
|
||||||
|
{
|
||||||
|
// Keep the large config JSON buffer off ESP32 Arduino's 8 KB loopTask stack.
|
||||||
|
g_configJsonScratch.clear();
|
||||||
|
return g_configJsonScratch;
|
||||||
|
}
|
||||||
|
|
||||||
void redirectToRoot()
|
void redirectToRoot()
|
||||||
{
|
{
|
||||||
@@ -371,7 +380,7 @@ bool loadConfigFromFile()
|
|||||||
return saveConfigToFile();
|
return saveConfigToFile();
|
||||||
}
|
}
|
||||||
|
|
||||||
StaticJsonDocument<kConfigJsonCapacity> doc;
|
ConfigJsonDocument &doc = configJsonScratch();
|
||||||
DeserializationError err = deserializeJson(doc, file);
|
DeserializationError err = deserializeJson(doc, file);
|
||||||
file.close();
|
file.close();
|
||||||
if (err)
|
if (err)
|
||||||
@@ -424,7 +433,7 @@ bool saveConfigToFile()
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
StaticJsonDocument<kConfigJsonCapacity> doc;
|
ConfigJsonDocument &doc = configJsonScratch();
|
||||||
JsonArray params = doc.createNestedArray("parameters");
|
JsonArray params = doc.createNestedArray("parameters");
|
||||||
for (size_t i = 0; i < configParameterCount; ++i)
|
for (size_t i = 0; i < configParameterCount; ++i)
|
||||||
{
|
{
|
||||||
@@ -461,7 +470,7 @@ bool saveConfigToFile()
|
|||||||
|
|
||||||
String buildConfigJson()
|
String buildConfigJson()
|
||||||
{
|
{
|
||||||
StaticJsonDocument<kConfigJsonCapacity> doc;
|
ConfigJsonDocument &doc = configJsonScratch();
|
||||||
JsonArray params = doc.createNestedArray("parameters");
|
JsonArray params = doc.createNestedArray("parameters");
|
||||||
for (size_t i = 0; i < configParameterCount; ++i)
|
for (size_t i = 0; i < configParameterCount; ++i)
|
||||||
{
|
{
|
||||||
@@ -497,7 +506,7 @@ String buildConfigJson()
|
|||||||
|
|
||||||
bool applyConfigJson(const String &payload, String &errorMessage)
|
bool applyConfigJson(const String &payload, String &errorMessage)
|
||||||
{
|
{
|
||||||
StaticJsonDocument<kConfigJsonCapacity> doc;
|
ConfigJsonDocument &doc = configJsonScratch();
|
||||||
DeserializationError err = deserializeJson(doc, payload);
|
DeserializationError err = deserializeJson(doc, payload);
|
||||||
if (err)
|
if (err)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user