Skip to content

Commit

Permalink
Dump settings line by line to prevent memory issues (#896)
Browse files Browse the repository at this point in the history
  • Loading branch information
xoseperez committed Jun 21, 2018
1 parent 7b63521 commit 5da801b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
10 changes: 5 additions & 5 deletions code/espurna/settings.ino
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ unsigned long settingsSize() {

// -----------------------------------------------------------------------------

unsigned int _settingsKeyCount() {
unsigned int settingsKeyCount() {
unsigned count = 0;
unsigned pos = SPI_FLASH_SEC_SIZE - 1;
while (size_t len = EEPROMr.read(pos)) {
Expand All @@ -50,7 +50,7 @@ unsigned int _settingsKeyCount() {
return count;
}

String _settingsKeyName(unsigned int index) {
String settingsKeyName(unsigned int index) {

String s;

Expand Down Expand Up @@ -80,11 +80,11 @@ std::vector<String> _settingsKeys() {
std::vector<String> keys;

//unsigned int size = settingsKeyCount();
unsigned int size = _settingsKeyCount();
unsigned int size = settingsKeyCount();
for (unsigned int i=0; i<size; i++) {

//String key = settingsKeyName(i);
String key = _settingsKeyName(i);
String key = settingsKeyName(i);
bool inserted = false;
for (unsigned char j=0; j<keys.size(); j++) {

Expand Down Expand Up @@ -277,7 +277,7 @@ void _settingsInitCommands() {
DEBUG_MSG_P(PSTR("+OK\n"));
});
#endif

settingsRegisterCommand(F("RESET"), [](Embedis* e) {
DEBUG_MSG_P(PSTR("+OK\n"));
deferredReset(100, CUSTOM_RESET_TERMINAL);
Expand Down
20 changes: 12 additions & 8 deletions code/espurna/web.ino
Original file line number Diff line number Diff line change
Expand Up @@ -67,20 +67,24 @@ void _onGetConfig(AsyncWebServerRequest *request) {

AsyncResponseStream *response = request->beginResponseStream("text/json");

DynamicJsonBuffer jsonBuffer;
JsonObject &root = jsonBuffer.createObject();
root["app"] = APP_NAME;
root["version"] = APP_VERSION;
settingsGetJson(root);
root.prettyPrintTo(*response);
jsonBuffer.clear();

char buffer[100];
snprintf_P(buffer, sizeof(buffer), PSTR("attachment; filename=\"%s-backup.json\""), (char *) getSetting("hostname").c_str());
response->addHeader("Content-Disposition", buffer);
response->addHeader("X-XSS-Protection", "1; mode=block");
response->addHeader("X-Content-Type-Options", "nosniff");
response->addHeader("X-Frame-Options", "deny");

response->printf("{\n \"app\": \"%s\",\n \"version\": \"%s\"", APP_NAME, APP_VERSION);

// Write the keys line by line (not sorted)
unsigned long count = settingsKeyCount();
for (unsigned int i=0; i<count; i++) {
String key = settingsKeyName(i);
String value = getSetting(key);
response->printf(",\n \"%s\": \"%s\"", key.c_str(), value.c_str());
}
response->printf("\n}");

request->send(response);

}
Expand Down

1 comment on commit 5da801b

@mcspr
Copy link
Collaborator

@mcspr mcspr commented on 5da801b Jun 21, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW would you suggest easier way to test this than filling eeprom with garbage through telnet / serial set X Y times 100? I remember similar issue as keys does when key in the middle is missing sometimes so the data, seemingly fine, has missing pieces. And I wonder how this will behave with tcp nodelay turned back on.

Please sign in to comment.