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