123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475 |
- bool loadConf_Secrets() // from EEPROM or File System, with or without encryption
- {
- bool _isEncrypted = confCheckEncrypted();
- #ifdef ENABLE_FEATURE_SECRETS_ENCRYPTION
- struct confDataSecrets
- {
- char WiFiPW1[64];
- char WiFiPW2[64];
- char WiFiAPModePassword[64];
- char http_token[31];
- char http_user[31];
- char http_pass[31];
- char http_user1[31];
- char http_pass1[31];
- char http_user2[31];
- char http_pass2[31];
- char mqtt_user[31];
- char mqtt_pass[31];
- // --- 471 bytes
- char spare[23];
- // --- 494 bytes + 2 bytes size information
- // in EEPROM before the actual data => 512 bytes
- } _confSecrets_encrypted;
- #endif
- bool _success = false;
-
- #ifdef STORE_SECRETS_IN_EEPROM
- uint16_t _size;
- EEPROM.begin(EEPROM_SIZE);
- EEPROM.get(EEPROM_STARTADDR_SECRETS, _size);
- if( (_size + EEPROM_STARTADDR_SECRETS + 2) <= EEPROM_SIZE ) {
- if(_size == sizeof(confSecrets)) {
- // size of stored data is stored in first 2 bytes
- // as it matches sizeof struct - proceed
- // restore complete confSecrets struct from EEPROM
- #ifdef ENABLE_FEATURE_SECRETS_ENCRYPTION
- EEPROM.get(EEPROM_STARTADDR_SECRETS + 2, _confSecrets_encrypted);
- #else
- EEPROM.get(EEPROM_STARTADDR_SECRETS + 2, confSecrets);
- #endif
- EEPROM.end();
- snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_loadConf_restoredSecrets_EEPROM, _size);
- sendLog(logBuf);
- // // size in EEPROM is smaller than the struct
- // // so there was a program extension/update
- // // --> fill up rest of struct with 0x00
- // // as it will now contain most likely all 0xFF
- // // from previously unused EEPROM
- // if(_size < sizeof(confSecrets)) {
- // snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_loadConf_restoredSecrets_fillUp_EEPROM, sizeof(confSecrets) - _size);
- // sendLog(logBuf);
- // uint8_t _zero = '\0';
- // for(uint16_t _i = 0; _i < sizeof(confSecrets); _i++) {
- // if(_i >= (_size - 1)) {
- // memcpy(&confSecrets + _i, &_zero, 1);
- // }
- // }
- // }
- // --> untested, dont use this
- _success = true;
- }
- else {
- EEPROM.end();
- snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_loadConf_errorRestoreSecrets_EEPROM, _size, sizeof(confSecrets));
- sendLog(logBuf);
- _success = false;
- }
- }
- else {
- EEPROM.end();
- snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_loadConf_errorRestoreSecretsExceedSize_EEPROM);
- sendLog(logBuf);
- _success = false;
- }
-
- #else // STORE_SECRETS_IN_EEPROM is not defined - use file on LittleFS
- char configFileName[] = "/confSecrets";
- if (LittleFS.exists(configFileName)) {
- File configFile = LittleFS.open(configFileName, "r");
- if (!configFile) {
- snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_loadConf_ErrorOpenFile, configFileName);
- sendLog(logBuf);
- _success = false;
- }
- else
- {
- #ifdef ENABLE_FEATURE_SECRETS_ENCRYPTION
- configFile.read((byte *)&_confSecrets_encrypted, sizeof(_confSecrets_encrypted));
- #else
- configFile.read((byte *)&confSecrets, sizeof(confSecrets));
- #endif
- snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_loadConf_restoredConf, configFileName, configFile.size());
- sendLog(logBuf);
- configFile.close();
- _success = true;
- }
- }
- else
- {
- snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_loadConf_FileNotExist, configFileName);
- sendLog(logBuf);
- _success = false;
- }
- #endif
- if(_success) {
- #ifdef ENABLE_FEATURE_SECRETS_ENCRYPTION
- if(_isEncrypted) {
- XORENC(_confSecrets_encrypted.WiFiPW1, encKey);
- XORENC(_confSecrets_encrypted.WiFiPW2, encKey);
- XORENC(_confSecrets_encrypted.WiFiAPModePassword, encKey);
- XORENC(_confSecrets_encrypted.http_token, encKey);
- XORENC(_confSecrets_encrypted.http_user, encKey);
- XORENC(_confSecrets_encrypted.http_pass, encKey);
- XORENC(_confSecrets_encrypted.http_user1, encKey);
- XORENC(_confSecrets_encrypted.http_pass1, encKey);
- XORENC(_confSecrets_encrypted.http_user2, encKey);
- XORENC(_confSecrets_encrypted.http_pass2, encKey);
- XORENC(_confSecrets_encrypted.mqtt_user, encKey);
- XORENC(_confSecrets_encrypted.mqtt_pass, encKey);
- snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_loadConf_encryptedSecretsLoaded);
- sendLog(logBuf);
- }
- memcpy(&confSecrets, &_confSecrets_encrypted, sizeof(confSecrets));
-
- #ifdef DEBUG_SECRETS_ENCRYPTION
- if(_isEncrypted) {
- Serial.println(F("Secrets UNencrypted:"));
- Serial.print(F("WiFiPW1: ")); Serial.println(confSecrets.WiFiPW1);
- Serial.print(F("WiFiPW2: ")); Serial.println(confSecrets.WiFiPW2);
- Serial.print(F("WiFiAPPW: ")); Serial.println(confSecrets.WiFiAPModePassword);
- Serial.print(F("http_token: ")); Serial.println(confSecrets.http_token);
- Serial.print(F("http_user: ")); Serial.println(confSecrets.http_user);
- Serial.print(F("http_pass: ")); Serial.println(confSecrets.http_pass);
- Serial.print(F("http_user1: ")); Serial.println(confSecrets.http_user1);
- Serial.print(F("http_pass1: ")); Serial.println(confSecrets.http_pass1);
- Serial.print(F("http_user2: ")); Serial.println(confSecrets.http_user2);
- Serial.print(F("http_pass2: ")); Serial.println(confSecrets.http_pass2);
- Serial.print(F("mqtt_user: ")); Serial.println(confSecrets.mqtt_user);
- Serial.print(F("mqtt_pass: ")); Serial.println(confSecrets.mqtt_pass);
- Serial.println(F("Secrets Encrypted:"));
- Serial.print(F("WiFiPW1: ")); Serial.println(_confSecrets_encrypted.WiFiPW1);
- Serial.print(F("WiFiPW2: ")); Serial.println(_confSecrets_encrypted.WiFiPW2);
- Serial.print(F("WiFiAPPW: ")); Serial.println(_confSecrets_encrypted.WiFiAPModePassword);
- Serial.print(F("http_token: ")); Serial.println(_confSecrets_encrypted.http_token);
- Serial.print(F("http_user: ")); Serial.println(_confSecrets_encrypted.http_user);
- Serial.print(F("http_pass: ")); Serial.println(_confSecrets_encrypted.http_pass);
- Serial.print(F("http_user1: ")); Serial.println(_confSecrets_encrypted.http_user1);
- Serial.print(F("http_pass1: ")); Serial.println(_confSecrets_encrypted.http_pass1);
- Serial.print(F("http_user2: ")); Serial.println(_confSecrets_encrypted.http_user2);
- Serial.print(F("http_pass2: ")); Serial.println(_confSecrets_encrypted.http_pass2);
- Serial.print(F("mqtt_user: ")); Serial.println(_confSecrets_encrypted.mqtt_user);
- Serial.print(F("mqtt_pass: ")); Serial.println(_confSecrets_encrypted.mqtt_pass);
- Serial.println();
- }
- #endif
- #endif
- return true;
- }
- else {
- return false;
- }
-
- } // loadConf_Secrets
- bool loadConf_DevWiFi()
- {
- char configFileName[] = "/confDevWiFi";
- if (LittleFS.exists(configFileName)) {
- File configFile = LittleFS.open(configFileName, "r");
- if (!configFile) {
- snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_loadConf_ErrorOpenFile, configFileName);
- sendLog(logBuf);
- return false;
- }
- else
- {
- configFile.read((byte *)&confDevWiFi, sizeof(confDevWiFi));
- snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_loadConf_restoredConf, configFileName, configFile.size());
- sendLog(logBuf);
- configFile.close();
- return true;
- }
- }
- else
- {
- snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_loadConf_FileNotExist, configFileName);
- sendLog(logBuf);
- return false;
- }
- } // loadConf_DevWiFi
- bool loadConf_Web()
- {
- //char configFileName[] = "/confWeb.json";
- char configFileName[] = "/confWeb";
- if (LittleFS.exists(configFileName))
- {
- File configFile = LittleFS.open(configFileName, "r");
- if (!configFile) {
- snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_loadConf_ErrorOpenFile, configFileName);
- sendLog(logBuf);
- return false;
- }
- else
- {
- configFile.read((byte *)&confWeb, sizeof(confWeb));
- snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_loadConf_restoredConf, configFileName, configFile.size());
- sendLog(logBuf);
- configFile.close();
- return true;
- }
- }
- else
- {
- snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_loadConf_FileNotExist, configFileName);
- sendLog(logBuf);
- return false;
- }
- } // loadConf_Web
- bool loadConf_Mqtt()
- {
- char configFileName[] = "/confMqtt";
- if (LittleFS.exists(configFileName))
- {
- File configFile = LittleFS.open(configFileName, "r");
- if (!configFile) {
- snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_loadConf_ErrorOpenFile, configFileName);
- sendLog(logBuf);
- return false;
- }
- else
- {
- configFile.read((byte *)&confMqtt, sizeof(confMqtt));
- snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_loadConf_restoredConf, configFileName, configFile.size());
- sendLog(logBuf);
- configFile.close();
- return true;
- }
- }
- else
- {
- snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_loadConf_FileNotExist, configFileName);
- sendLog(logBuf);
- return false;
- }
- } // loadConf_Mqtt
- #ifdef FIRMWARE_VARIANT_THERMOSTAT
- bool loadConf_Therm()
- {
- char configFileName[] = "/confTherm";
- if (LittleFS.exists(configFileName))
- {
- File configFile = LittleFS.open(configFileName, "r");
- if (!configFile) {
- snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_loadConf_ErrorOpenFile, configFileName);
- sendLog(logBuf);
- return false;
- }
- else
- {
- configFile.read((byte *)&confTherm, sizeof(confTherm));
- snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_loadConf_restoredConf, configFileName, configFile.size());
- sendLog(logBuf);
- configFile.close();
- return true;
- }
- }
- else
- {
- snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_loadConf_FileNotExist, configFileName);
- sendLog(logBuf);
- return false;
- }
- } // loadConf_Therm
- #endif
- bool loadConf_Add()
- {
- char configFileName[] = "/confAdd";
- if (LittleFS.exists(configFileName))
- {
- File configFile = LittleFS.open(configFileName, "r");
- if (!configFile) {
- snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_loadConf_ErrorOpenFile, configFileName);
- sendLog(logBuf);
- return false;
- }
- else
- {
- configFile.read((byte *)&confAdd, sizeof(confAdd));
- snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_loadConf_restoredConf, configFileName, configFile.size());
- sendLog(logBuf);
- configFile.close();
- return true;
- }
- }
- else
- {
- snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_loadConf_FileNotExist, configFileName);
- sendLog(logBuf);
- return false;
- }
- } // loadConf_Add
- #ifdef ENABLE_FEATURE_NTP_TIME
- bool loadConf_Time()
- {
- char configFileName[] = "/confTime";
- if (LittleFS.exists(configFileName))
- {
- File configFile = LittleFS.open(configFileName, "r");
- if (!configFile) {
- snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_loadConf_ErrorOpenFile, configFileName);
- sendLog(logBuf);
- return false;
- }
- else
- {
- configFile.read((byte *)&confTime, sizeof(confTime));
- snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_loadConf_restoredConf, configFileName, configFile.size());
- sendLog(logBuf);
- configFile.close();
- return true;
- }
- }
- else
- {
- snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_loadConf_FileNotExist, configFileName);
- sendLog(logBuf);
- return false;
- }
- } // loadConf_Time
- #endif
- bool loadConf_Log()
- {
- char configFileName[] = "/confLog";
- if (LittleFS.exists(configFileName))
- {
- File configFile = LittleFS.open(configFileName, "r");
- if (!configFile) {
- snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_loadConf_ErrorOpenFile, configFileName);
- sendLog(logBuf);
- return false;
- }
- else
- {
- configFile.read((byte *)&confLog, sizeof(confLog));
- snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_loadConf_restoredConf, configFileName, configFile.size());
- sendLog(logBuf);
- configFile.close();
- return true;
- }
- }
- else
- {
- snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_loadConf_FileNotExist, configFileName);
- sendLog(logBuf);
- return false;
- }
- } // loadConf_Log
- bool loadConf_Sens()
- {
-
- char configFileName[] = "/confSens";
- if (LittleFS.exists(configFileName))
- {
- File configFile = LittleFS.open(configFileName, "r");
- if (!configFile) {
- snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_loadConf_ErrorOpenFile, configFileName);
- sendLog(logBuf);
- return false;
- }
- else
- {
- configFile.read((byte *)&confSens, sizeof(confSens));
- snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_loadConf_restoredConf, configFileName, configFile.size());
- sendLog(logBuf);
- configFile.close();
- return true;
- }
- }
- else
- {
- snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_loadConf_FileNotExist, configFileName);
- sendLog(logBuf);
- return false;
- }
- } // loadConf_Sens
- #ifdef FIRMWARE_VARIANT_HEATCONTROL
- bool loadConf_Heatc()
- {
- char configFileName[] = "/confHeatc";
- if (LittleFS.exists(configFileName))
- {
- File configFile = LittleFS.open(configFileName, "r");
- if (!configFile) {
- snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_loadConf_ErrorOpenFile, configFileName);
- sendLog(logBuf);
- return false;
- }
- else
- {
- configFile.read((byte *)&confHeatc, sizeof(confHeatc));
- snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_loadConf_restoredConf, configFileName, configFile.size());
- sendLog(logBuf);
- configFile.close();
- return true;
- }
- }
- else
- {
- snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_loadConf_FileNotExist, configFileName);
- sendLog(logBuf);
- return false;
- }
- } // loadConf_Heatc
- #endif
|