From 8ddae6bba0ee1b3dea5440729da82037e170655f Mon Sep 17 00:00:00 2001 From: cschwinne Date: Wed, 4 Nov 2020 17:17:54 +0100 Subject: [PATCH] wsec start --- wled00/cfg.cpp | 39 +++++++++++++++++++++++++++++++++++++++ wled00/file.cpp | 3 ++- wled00/wled_eeprom.cpp | 2 +- 3 files changed, 42 insertions(+), 2 deletions(-) diff --git a/wled00/cfg.cpp b/wled00/cfg.cpp index 89949e79..7197c73e 100644 --- a/wled00/cfg.cpp +++ b/wled00/cfg.cpp @@ -15,6 +15,13 @@ void getStringFromJson(char* dest, const char* src, size_t len) { void deserializeSettings() { DynamicJsonDocument doc(JSON_BUFFER_SIZE); + DEBUG_PRINTLN(F("Reading settings from /cfg.json...")); + + bool success = readObjectFromFile("/cfg.json", nullptr, &doc); + if (!success) { //if file does not exist, try reading from EEPROM + loadSettingsFromEEPROM(); + } + //deserializeJson(doc, json); //int rev_major = doc["rev"][0]; // 1 @@ -229,6 +236,7 @@ void deserializeSettings() { getStringFromJson(ntpServerName, if_ntp["host"], 33); // "1.wled.pool.ntp.org" CJSON(currentTimezone, if_ntp["tz"]); CJSON(utcOffsetSecs, if_ntp["offset"]); + CJSON(useAMPM, if_ntp["ampm"]); JsonObject ol = doc["ol"]; CJSON(overlayDefault ,ol["clock"]); // 0 @@ -281,6 +289,13 @@ void deserializeSettings() { CJSON(aOtaEnabled, ota["aota"]); getStringFromJson(otaPass, pwd, 33); //normally not present due to security } + + DEBUG_PRINTLN(F("Reading settings from /wsec.json...")); + + success = readObjectFromFile("/wsec.json", nullptr, &doc); + if (!success) { //if file does not exist, try reading from EEPROM + loadSettingsFromEEPROM(); + } } void serializeSettings() { @@ -510,6 +525,7 @@ void serializeSettings() { if_ntp["host"] = ntpServerName; if_ntp["tz"] = currentTimezone; if_ntp["offset"] = utcOffsetSecs; + if_ntp["ampm"] = useAMPM; JsonObject ol = doc.createNestedObject("ol"); ol["clock"] = overlayDefault; @@ -542,4 +558,27 @@ void serializeSettings() { ota["aota"] = aOtaEnabled; serializeJson(doc, Serial); +} + +//settings in /wsec.json, not accessible via webserver, for passwords and tokens +void deserializeSettingsSec() { + DynamicJsonDocument doc(JSON_BUFFER_SIZE); + + JsonObject nw_ins_0 = doc["nw"]["ins"][0]; + getStringFromJson(clientPass, nw_ins_0["psk"], 65); + + JsonObject ap = doc["ap"]; + getStringFromJson(apPass, ap["psk"] , 65); + + //mqtt pass + + //blynk token + + //hue token + + //ota pass +} + +void serializeSettingsSec() { + } \ No newline at end of file diff --git a/wled00/file.cpp b/wled00/file.cpp index 65b1bbc8..38e71444 100644 --- a/wled00/file.cpp +++ b/wled00/file.cpp @@ -330,6 +330,7 @@ bool readObjectFromFileUsingId(const char* file, uint16_t id, JsonDocument* dest return readObjectFromFile(file, objKey, dest); } +//if the key is a nullptr, deserialize entire object bool readObjectFromFile(const char* file, const char* key, JsonDocument* dest) { if (doCloseFile) closeFile(); @@ -340,7 +341,7 @@ bool readObjectFromFile(const char* file, const char* key, JsonDocument* dest) f = WLED_FS.open(file, "r"); if (!f) return false; - if (!bufferedFind(key)) //key does not exist in file + if (key != nullptr && !bufferedFind(key)) //key does not exist in file { f.close(); dest->clear(); diff --git a/wled00/wled_eeprom.cpp b/wled00/wled_eeprom.cpp index 3bff4dda..5a54acea 100644 --- a/wled00/wled_eeprom.cpp +++ b/wled00/wled_eeprom.cpp @@ -786,7 +786,7 @@ void deEEP() { DEBUG_PRINTLN(F("Preset file not found, attempting to load from EEPROM")); DEBUGFS_PRINTLN(F("Allocating saving buffer for dEEP")); - DynamicJsonDocument dDoc(JSON_BUFFER_SIZE); + DynamicJsonDocument dDoc(JSON_BUFFER_SIZE *2); JsonObject sObj = dDoc.to(); sObj.createNestedObject("0");