Skip to content

Commit a59376a

Browse files
committed
Adding verification of json files, added config restore at bootup if broken
1 parent ab69078 commit a59376a

File tree

4 files changed

+38
-4
lines changed

4 files changed

+38
-4
lines changed

wled00/cfg.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -782,6 +782,10 @@ bool restoreConfig() {
782782
return restoreFile(s_cfg_json);
783783
}
784784

785+
bool verifyConfig() {
786+
return validateJsonFile(s_cfg_json);
787+
}
788+
785789
// rename config file and reboot
786790
void resetConfig() {
787791
DEBUG_PRINTLN(F("Reset config"));

wled00/fcn_declare.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ void IRAM_ATTR touchButtonISR();
2626
//cfg.cpp
2727
bool backupConfig();
2828
bool restoreConfig();
29+
bool verifyConfig();
2930
void resetConfig();
3031
bool deserializeConfig(JsonObject doc, bool fromFS = false);
3132
bool deserializeConfigFromFS();
@@ -229,6 +230,7 @@ inline bool readObjectFromFile(const String &file, const char* key, JsonDocument
229230
bool copyFile(const char* src_path, const char* dst_path);
230231
bool backupFile(const char* filename);
231232
bool restoreFile(const char* filename);
233+
bool validateJsonFile(const char* filename);
232234
void dumpFilesToSerial();
233235

234236
//hue.cpp

wled00/file.cpp

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ bool handleFileRead(AsyncWebServerRequest* request, String path){
442442

443443
// copy a file, delete destination file if incomplete to prevent corrupted files
444444
bool copyFile(const char* src_path, const char* dst_path) {
445-
DEBUG_PRINTF("copyFile from %s to %s", src_path, dst_path);
445+
DEBUG_PRINTF("copyFile from %s to %s\n", src_path, dst_path);
446446
if(!WLED_FS.exists(src_path)) {
447447
DEBUG_PRINTLN(F("file not found"));
448448
return false;
@@ -481,8 +481,11 @@ bool copyFile(const char* src_path, const char* dst_path) {
481481
static const char s_backup_json[] PROGMEM = ".bkp.json";
482482

483483
bool backupFile(const char* filename) {
484-
DEBUG_PRINTF("backup %s", filename);
485-
if (!WLED_FS.exists(filename)) return false;
484+
DEBUG_PRINTF("backup %s \n", filename);
485+
if (!validateJsonFile(filename)) {
486+
DEBUG_PRINTLN(F("broken file"));
487+
return false;
488+
}
486489
char backupname[32];
487490
strcpy(backupname, filename);
488491
strcat(backupname, s_backup_json);
@@ -496,7 +499,7 @@ bool backupFile(const char* filename) {
496499
}
497500

498501
bool restoreFile(const char* filename) {
499-
DEBUG_PRINTF("restore %s", filename);
502+
DEBUG_PRINTF("restore %s \n", filename);
500503
char backupname[32];
501504
strcpy(backupname, filename);
502505
strcat(backupname, s_backup_json);
@@ -506,6 +509,11 @@ bool restoreFile(const char* filename) {
506509
return false;
507510
}
508511

512+
if (!validateJsonFile(backupname)) {
513+
DEBUG_PRINTLN(F("broken backup"));
514+
return false;
515+
}
516+
509517
if (copyFile(backupname, filename)) {
510518
DEBUG_PRINTLN(F("restore ok"));
511519
return true;
@@ -514,6 +522,21 @@ bool restoreFile(const char* filename) {
514522
return false;
515523
}
516524

525+
bool validateJsonFile(const char* filename) {
526+
if (!WLED_FS.exists(filename)) return false;
527+
File file = WLED_FS.open(filename, "r");
528+
if (!file) return false;
529+
StaticJsonDocument<0> doc, filter; // https://arduinojson.org/v6/how-to/validate-json/
530+
bool result = deserializeJson(doc, file, DeserializationOption::Filter(filter)) == DeserializationError::Ok;
531+
file.close();
532+
if (!result) {
533+
DEBUG_PRINTF("Invalid JSON file %s\n", filename);
534+
} else {
535+
DEBUG_PRINTF("Valid JSON file %s\n", filename);
536+
}
537+
return result;
538+
}
539+
517540
// print contents of all files in root dir to Serial except wsec files
518541
void dumpFilesToSerial() {
519542
File rootdir = WLED_FS.open("/", "r");

wled00/wled.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,11 @@ void WLED::setup()
425425
WLED_SET_AP_SSID(); // otherwise it is empty on first boot until config is saved
426426
multiWiFi.push_back(WiFiConfig(CLIENT_SSID,CLIENT_PASS)); // initialise vector with default WiFi
427427

428+
if(!verifyConfig()) {
429+
if(!restoreConfig()) {
430+
resetConfig();
431+
}
432+
}
428433
DEBUG_PRINTLN(F("Reading config"));
429434
bool needsCfgSave = deserializeConfigFromFS();
430435
DEBUG_PRINTF_P(PSTR("heap %u\n"), ESP.getFreeHeap());

0 commit comments

Comments
 (0)