123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431 |
- // EEPROM_SIZE 512
- // struct confSecrets size: 471
- // EEPROM start addr: 16 (0-15 used/reserved)
- // + 2 bytes size information
- // resulting used size: 489
- bool saveConf_Secrets() {
- return saveConf_Secrets(false);
- }
- bool saveConf_Secrets(bool _force)
- {
- if (confSecrets_wasChanged || _force)
- {
- #ifdef STORE_SECRETS_IN_EEPROM
- EEPROM.begin(EEPROM_SIZE);
- // write sizeof(confSecrets) to EEPROM in the first 2 bytes
- EEPROM.put(EEPROM_STARTADDR_SECRETS, (uint16_t)sizeof(confSecrets));
- #endif
- #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;
- if(confEncryptionEnabled) {
- // copy entire struct - the copy will be encrypted and saved
- memcpy(&_confSecrets_encrypted, &confSecrets, sizeof(confSecrets));
- 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);
- }
- #endif
- #ifdef DEBUG_SECRETS_ENCRYPTION
- #ifdef ENABLE_FEATURE_SECRETS_ENCRYPTION
- if(confEncryptionEnabled) {
- 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
- #ifdef STORE_SECRETS_IN_EEPROM
- #ifdef ENABLE_FEATURE_SECRETS_ENCRYPTION
- // encryption on - put encrypted copy of confSecrets struct to EEPROM
- if(confEncryptionEnabled) {
- EEPROM.put(EEPROM_STARTADDR_SECRETS + 2, _confSecrets_encrypted);
- }
- else {
- // encryption off - put complete origin confSecrets struct to EEPROM
- EEPROM.put(EEPROM_STARTADDR_SECRETS + 2, confSecrets);
- }
- #else
- // compiled without encryption support - put complete origin confSecrets struct to EEPROM
- EEPROM.put(EEPROM_STARTADDR_SECRETS + 2, confSecrets);
- #endif
- if(EEPROM.commit()) {
- snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_loadConf_storedSecrets_EEPROM);
- sendLog(logBuf, LOGLEVEL_INFO);
- EEPROM.end();
- confSecrets_wasChanged = false;
- return true;
- }
- else { // error
- EEPROM.end();
- snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_loadConf_errorStoreSecrets_EEPROM);
- sendLog(logBuf, LOGLEVEL_INFO);
- return false;
- }
- #else // STORE_SECRETS_IN_EEPROM is not defined - use file on LittleFS
- char configFileName[] = "/confSecrets";
- File configFile = LittleFS.open(configFileName, "w");
- if (!configFile)
- {
- snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_safeConf_configFileWriteError, configFileName);
- sendLog(logBuf, LOGLEVEL_INFO);
- return false;
- }
- #ifdef ENABLE_FEATURE_SECRETS_ENCRYPTION
- if(confEncryptionEnabled) {
- // encryption on - save encrypted copy of confSecrets struct to file
- configFile.write((byte*) &_confSecrets_encrypted, sizeof(_confSecrets_encrypted));
- }
- else {
- // encryption off - save complete origin confSecrets struct to file
- configFile.write((byte*) &confSecrets, sizeof(confSecrets));
- }
- #else
- // compiled without encryption support - save complete origin confSecrets struct to file
- configFile.write((byte*) &confSecrets, sizeof(confSecrets));
- #endif
- configFile.close();
- snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_safeConf_configFileWritten, configFileName);
- sendLog(logBuf, LOGLEVEL_INFO);
- confSecrets_wasChanged = false;
- return true;
- #endif
- }
- else return false;
- } // saveConf_Secrets
- bool saveConf_DevWiFi() {
- return saveConf_DevWiFi(false);
- }
- bool saveConf_DevWiFi(bool _force)
- {
- if (confDevWiFi_wasChanged || _force)
- {
- char configFileName[] = "/confDevWiFi";
- File configFile = LittleFS.open(configFileName, "w");
- if (!configFile)
- {
- snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_safeConf_configFileWriteError, configFileName);
- sendLog(logBuf, LOGLEVEL_INFO);
- return false;
- }
- configFile.write((byte*) &confDevWiFi, sizeof(confDevWiFi));
- configFile.close();
-
- snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_safeConf_configFileWritten, configFileName);
- sendLog(logBuf, LOGLEVEL_INFO);
- confDevWiFi_wasChanged = false;
- return true;
- }
- else return false;
- } // saveConf_DevWiFi
- bool saveConf_Web() {
- return saveConf_Web(false);
- }
- bool saveConf_Web(bool _force)
- {
- if (confWeb_wasChanged || _force)
- {
- char configFileName[] = "/confWeb";
- File configFile = LittleFS.open(configFileName, "w");
- if (!configFile)
- {
- snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_safeConf_configFileWriteError, configFileName);
- sendLog(logBuf, LOGLEVEL_INFO);
- return false;
- }
- configFile.write((byte*) &confWeb, sizeof(confWeb));
- configFile.close();
-
- snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_safeConf_configFileWritten, configFileName);
- sendLog(logBuf, LOGLEVEL_INFO);
-
- confWeb_wasChanged = false;
- return true;
- }
- else
- return false;
- } // saveConf_Web
- bool saveConf_Mqtt() {
- return saveConf_Mqtt(false);
- }
- bool saveConf_Mqtt(bool _force)
- {
- if (confMqtt_wasChanged || _force)
- {
- char configFileName[] = "/confMqtt";
- File configFile = LittleFS.open(configFileName, "w");
- if (!configFile)
- {
- snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_safeConf_configFileWriteError, configFileName);
- sendLog(logBuf, LOGLEVEL_INFO);
- return false;
- }
- configFile.write((byte*) &confMqtt, sizeof(confMqtt));
- configFile.close();
- snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_safeConf_configFileWritten, configFileName);
- sendLog(logBuf, LOGLEVEL_INFO);
- confMqtt_wasChanged = false;
- return true;
- }
- else return false;
- } // saveConf_Mqtt
- #ifdef FIRMWARE_VARIANT_THERMOSTAT
- bool saveConf_Therm() {
- return saveConf_Therm(false);
- }
- bool saveConf_Therm(bool _force)
- {
- if (confTherm_wasChanged || _force)
- {
- char configFileName[] = "/confTherm";
- File configFile = LittleFS.open(configFileName, "w");
- if (!configFile)
- {
- snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_safeConf_configFileWriteError, configFileName);
- sendLog(logBuf, LOGLEVEL_INFO);
- return false;
- }
- configFile.write((byte*) &confTherm, sizeof(confTherm));
- configFile.close();
- snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_safeConf_configFileWritten, configFileName);
- sendLog(logBuf, LOGLEVEL_INFO);
-
- confTherm_wasChanged = false;
- return true;
- }
- else
- return false;
- } // saveConf_Therm
- #endif
- bool saveConf_Add() {
- return saveConf_Add(false);
- }
- bool saveConf_Add(bool _force)
- {
- if (confAdd_wasChanged || _force)
- {
- char configFileName[] = "/confAdd";
- File configFile = LittleFS.open(configFileName, "w");
- if (!configFile)
- {
- snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_safeConf_configFileWriteError, configFileName);
- sendLog(logBuf, LOGLEVEL_INFO);
- return false;
- }
- configFile.write((byte*) &confAdd, sizeof(confAdd));
- configFile.close();
-
- snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_safeConf_configFileWritten, configFileName);
- sendLog(logBuf, LOGLEVEL_INFO);
- confAdd_wasChanged = false;
- return true;
- }
- else
- return false;
- } // saveConf_Add
- #ifdef ENABLE_FEATURE_NTP_TIME
- bool saveConf_Time() {
- return saveConf_Time(false);
- }
- bool saveConf_Time(bool _force)
- {
- if (confTime_wasChanged || _force)
- {
- char configFileName[] = "/confTime";
- File configFile = LittleFS.open(configFileName, "w");
- if (!configFile)
- {
- snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_safeConf_configFileWriteError, configFileName);
- sendLog(logBuf, LOGLEVEL_INFO);
- return false;
- }
- configFile.write((byte*) &confTime, sizeof(confTime));
- configFile.close();
-
- snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_safeConf_configFileWritten, configFileName);
- sendLog(logBuf, LOGLEVEL_INFO);
- confTime_wasChanged = false;
- return true;
- }
- else
- return false;
- } // saveConf_Time
- #endif
- bool saveConf_Log() {
- return saveConf_Log(false);
- }
- bool saveConf_Log(bool _force)
- {
- if (confLog_wasChanged || _force)
- {
- char configFileName[] = "/confLog";
- File configFile = LittleFS.open(configFileName, "w");
- if (!configFile)
- {
- snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_safeConf_configFileWriteError, configFileName);
- sendLog(logBuf, LOGLEVEL_INFO);
- return false;
- }
- configFile.write((byte*) &confLog, sizeof(confLog));
- configFile.close();
- snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_safeConf_configFileWritten, configFileName);
- sendLog(logBuf, LOGLEVEL_INFO);
- confLog_wasChanged = false;
- return true;
- }
- else
- return false;
- } // saveConf_Log
- bool saveConf_Sens() {
- return saveConf_Sens(false);
- }
- bool saveConf_Sens(bool _force)
- {
- if (confSens_wasChanged || _force)
- {
- char configFileName[] = "/confSens";
- File configFile = LittleFS.open(configFileName, "w");
- if (!configFile)
- {
- snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_safeConf_configFileWriteError, configFileName);
- sendLog(logBuf, LOGLEVEL_INFO);
- return false;
- }
- configFile.write((byte*) &confSens, sizeof(confSens));
- configFile.close();
-
- snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_safeConf_configFileWritten, configFileName);
- sendLog(logBuf, LOGLEVEL_INFO);
- confSens_wasChanged = false;
- return true;
- }
- else
- return false;
- } // saveConf_Sens
- #ifdef FIRMWARE_VARIANT_HEATCONTROL
- bool saveConf_Heatc() {
- return saveConf_Heatc(false);
- }
- bool saveConf_Heatc(bool _force)
- {
- if (confHeatc_wasChanged || _force)
- {
- char configFileName[] = "/confHeatc";
- File configFile = LittleFS.open(configFileName, "w");
- if (!configFile)
- {
- snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_safeConf_configFileWriteError, configFileName);
- sendLog(logBuf, LOGLEVEL_INFO);
- return false;
- }
- configFile.write((byte*) &confHeatc, sizeof(confHeatc));
- configFile.close();
- snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_safeConf_configFileWritten, configFileName);
- sendLog(logBuf, LOGLEVEL_INFO);
-
- confHeatc_wasChanged = false;
- return true;
- }
- else
- return false;
- } // saveConf_Heatc
- #endif
|