Browse Source

Fix crash on loading malformed settings, option to load partial configuration

v2
Xose Pérez 6 years ago
parent
commit
bce6774c17
2 changed files with 19 additions and 5 deletions
  1. +14
    -3
      code/espurna/settings.ino
  2. +5
    -2
      code/espurna/web.ino

+ 14
- 3
code/espurna/settings.ino View File

@ -375,19 +375,30 @@ size_t settingsMaxSize() {
bool settingsRestoreJson(JsonObject& data) { bool settingsRestoreJson(JsonObject& data) {
// Check this is an ESPurna configuration file (must have "app":"ESPURNA")
const char* app = data["app"]; const char* app = data["app"];
if (strcmp(app, APP_NAME) != 0) return false;
if (!app || strcmp(app, APP_NAME) != 0) {
DEBUG_MSG_P(PSTR("[SETTING] Wrong or missing 'app' key\n"));
return false;
}
for (unsigned int i = EEPROM_DATA_END; i < SPI_FLASH_SEC_SIZE; i++) {
EEPROMr.write(i, 0xFF);
// Clear settings
bool is_backup = data["backup"];
if (is_backup) {
for (unsigned int i = EEPROM_DATA_END; i < SPI_FLASH_SEC_SIZE; i++) {
EEPROMr.write(i, 0xFF);
}
} }
// Dump settings to memory buffer
for (auto element : data) { for (auto element : data) {
if (strcmp(element.key, "app") == 0) continue; if (strcmp(element.key, "app") == 0) continue;
if (strcmp(element.key, "version") == 0) continue; if (strcmp(element.key, "version") == 0) continue;
if (strcmp(element.key, "backup") == 0) continue;
setSetting(element.key, element.value.as<char*>()); setSetting(element.key, element.value.as<char*>());
} }
// Persist to EEPROM
saveSettings(); saveSettings();
DEBUG_MSG_P(PSTR("[SETTINGS] Settings restored successfully\n")); DEBUG_MSG_P(PSTR("[SETTINGS] Settings restored successfully\n"));


+ 5
- 2
code/espurna/web.ino View File

@ -123,14 +123,17 @@ void _onGetConfig(AsyncWebServerRequest *request) {
response->addHeader("X-Content-Type-Options", "nosniff"); response->addHeader("X-Content-Type-Options", "nosniff");
response->addHeader("X-Frame-Options", "deny"); response->addHeader("X-Frame-Options", "deny");
response->printf("{\n \"app\": \"%s\",\n \"version\": \"%s\"", APP_NAME, APP_VERSION);
response->printf("{\n\"app\": \"%s\"", APP_NAME);
response->printf(",\n\"version\": \"%s\"", APP_VERSION);
response->printf(",\n\"backup\": \"1\"");
response->printf(",\n\"timestamp\": \"%s\"", ntpDateTime().c_str());
// Write the keys line by line (not sorted) // Write the keys line by line (not sorted)
unsigned long count = settingsKeyCount(); unsigned long count = settingsKeyCount();
for (unsigned int i=0; i<count; i++) { for (unsigned int i=0; i<count; i++) {
String key = settingsKeyName(i); String key = settingsKeyName(i);
String value = getSetting(key); String value = getSetting(key);
response->printf(",\n \"%s\": \"%s\"", key.c_str(), value.c_str());
response->printf(",\n\"%s\": \"%s\"", key.c_str(), value.c_str());
} }
response->printf("\n}"); response->printf("\n}");


Loading…
Cancel
Save