config_save.ino 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. // EEPROM_SIZE 512
  2. // struct confSecrets size: 471
  3. // EEPROM start addr: 16 (0-15 used/reserved)
  4. // + 2 bytes size information
  5. // resulting used size: 489
  6. bool saveConf_Secrets() {
  7. return saveConf_Secrets(false);
  8. }
  9. bool saveConf_Secrets(bool _force)
  10. {
  11. if (confSecrets_wasChanged || _force)
  12. {
  13. #ifdef STORE_SECRETS_IN_EEPROM
  14. EEPROM.begin(EEPROM_SIZE);
  15. // write sizeof(confSecrets) to EEPROM in the first 2 bytes
  16. EEPROM.put(EEPROM_STARTADDR_SECRETS, (uint16_t)sizeof(confSecrets));
  17. #endif
  18. #ifdef ENABLE_FEATURE_SECRETS_ENCRYPTION
  19. struct confDataSecrets
  20. {
  21. char WiFiPW1[64];
  22. char WiFiPW2[64];
  23. char WiFiAPModePassword[64];
  24. char http_token[31];
  25. char http_user[31];
  26. char http_pass[31];
  27. char http_user1[31];
  28. char http_pass1[31];
  29. char http_user2[31];
  30. char http_pass2[31];
  31. char mqtt_user[31];
  32. char mqtt_pass[31];
  33. // --- 471 bytes
  34. char spare[23];
  35. // --- 494 bytes + 2 bytes size information
  36. // in EEPROM before the actual data => 512 bytes
  37. } _confSecrets_encrypted;
  38. if(confEncryptionEnabled) {
  39. // copy entire struct - the copy will be encrypted and saved
  40. memcpy(&_confSecrets_encrypted, &confSecrets, sizeof(confSecrets));
  41. XORENC(_confSecrets_encrypted.WiFiPW1, encKey);
  42. XORENC(_confSecrets_encrypted.WiFiPW2, encKey);
  43. XORENC(_confSecrets_encrypted.WiFiAPModePassword, encKey);
  44. XORENC(_confSecrets_encrypted.http_token, encKey);
  45. XORENC(_confSecrets_encrypted.http_user, encKey);
  46. XORENC(_confSecrets_encrypted.http_pass, encKey);
  47. XORENC(_confSecrets_encrypted.http_user1, encKey);
  48. XORENC(_confSecrets_encrypted.http_pass1, encKey);
  49. XORENC(_confSecrets_encrypted.http_user2, encKey);
  50. XORENC(_confSecrets_encrypted.http_pass2, encKey);
  51. XORENC(_confSecrets_encrypted.mqtt_user, encKey);
  52. XORENC(_confSecrets_encrypted.mqtt_pass, encKey);
  53. }
  54. #endif
  55. #ifdef DEBUG_SECRETS_ENCRYPTION
  56. #ifdef ENABLE_FEATURE_SECRETS_ENCRYPTION
  57. if(confEncryptionEnabled) {
  58. Serial.println(F("Secrets UNencrypted:"));
  59. Serial.print(F("WiFiPW1: ")); Serial.println(confSecrets.WiFiPW1);
  60. Serial.print(F("WiFiPW2: ")); Serial.println(confSecrets.WiFiPW2);
  61. Serial.print(F("WiFiAPPW: ")); Serial.println(confSecrets.WiFiAPModePassword);
  62. Serial.print(F("http_token: ")); Serial.println(confSecrets.http_token);
  63. Serial.print(F("http_user: ")); Serial.println(confSecrets.http_user);
  64. Serial.print(F("http_pass: ")); Serial.println(confSecrets.http_pass);
  65. Serial.print(F("http_user1: ")); Serial.println(confSecrets.http_user1);
  66. Serial.print(F("http_pass1: ")); Serial.println(confSecrets.http_pass1);
  67. Serial.print(F("http_user2: ")); Serial.println(confSecrets.http_user2);
  68. Serial.print(F("http_pass2: ")); Serial.println(confSecrets.http_pass2);
  69. Serial.print(F("mqtt_user: ")); Serial.println(confSecrets.mqtt_user);
  70. Serial.print(F("mqtt_pass: ")); Serial.println(confSecrets.mqtt_pass);
  71. Serial.println(F("Secrets Encrypted:"));
  72. Serial.print(F("WiFiPW1: ")); Serial.println(_confSecrets_encrypted.WiFiPW1);
  73. Serial.print(F("WiFiPW2: ")); Serial.println(_confSecrets_encrypted.WiFiPW2);
  74. Serial.print(F("WiFiAPPW: ")); Serial.println(_confSecrets_encrypted.WiFiAPModePassword);
  75. Serial.print(F("http_token: ")); Serial.println(_confSecrets_encrypted.http_token);
  76. Serial.print(F("http_user: ")); Serial.println(_confSecrets_encrypted.http_user);
  77. Serial.print(F("http_pass: ")); Serial.println(_confSecrets_encrypted.http_pass);
  78. Serial.print(F("http_user1: ")); Serial.println(_confSecrets_encrypted.http_user1);
  79. Serial.print(F("http_pass1: ")); Serial.println(_confSecrets_encrypted.http_pass1);
  80. Serial.print(F("http_user2: ")); Serial.println(_confSecrets_encrypted.http_user2);
  81. Serial.print(F("http_pass2: ")); Serial.println(_confSecrets_encrypted.http_pass2);
  82. Serial.print(F("mqtt_user: ")); Serial.println(_confSecrets_encrypted.mqtt_user);
  83. Serial.print(F("mqtt_pass: ")); Serial.println(_confSecrets_encrypted.mqtt_pass);
  84. Serial.println();
  85. }
  86. #endif
  87. #endif
  88. #ifdef STORE_SECRETS_IN_EEPROM
  89. #ifdef ENABLE_FEATURE_SECRETS_ENCRYPTION
  90. // encryption on - put encrypted copy of confSecrets struct to EEPROM
  91. if(confEncryptionEnabled) {
  92. EEPROM.put(EEPROM_STARTADDR_SECRETS + 2, _confSecrets_encrypted);
  93. }
  94. else {
  95. // encryption off - put complete origin confSecrets struct to EEPROM
  96. EEPROM.put(EEPROM_STARTADDR_SECRETS + 2, confSecrets);
  97. }
  98. #else
  99. // compiled without encryption support - put complete origin confSecrets struct to EEPROM
  100. EEPROM.put(EEPROM_STARTADDR_SECRETS + 2, confSecrets);
  101. #endif
  102. if(EEPROM.commit()) {
  103. snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_loadConf_storedSecrets_EEPROM);
  104. sendLog(logBuf, LOGLEVEL_INFO);
  105. EEPROM.end();
  106. confSecrets_wasChanged = false;
  107. return true;
  108. }
  109. else { // error
  110. EEPROM.end();
  111. snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_loadConf_errorStoreSecrets_EEPROM);
  112. sendLog(logBuf, LOGLEVEL_INFO);
  113. return false;
  114. }
  115. #else // STORE_SECRETS_IN_EEPROM is not defined - use file on LittleFS
  116. char configFileName[] = "/confSecrets";
  117. File configFile = LittleFS.open(configFileName, "w");
  118. if (!configFile)
  119. {
  120. snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_safeConf_configFileWriteError, configFileName);
  121. sendLog(logBuf, LOGLEVEL_INFO);
  122. return false;
  123. }
  124. #ifdef ENABLE_FEATURE_SECRETS_ENCRYPTION
  125. if(confEncryptionEnabled) {
  126. // encryption on - save encrypted copy of confSecrets struct to file
  127. configFile.write((byte*) &_confSecrets_encrypted, sizeof(_confSecrets_encrypted));
  128. }
  129. else {
  130. // encryption off - save complete origin confSecrets struct to file
  131. configFile.write((byte*) &confSecrets, sizeof(confSecrets));
  132. }
  133. #else
  134. // compiled without encryption support - save complete origin confSecrets struct to file
  135. configFile.write((byte*) &confSecrets, sizeof(confSecrets));
  136. #endif
  137. configFile.close();
  138. snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_safeConf_configFileWritten, configFileName);
  139. sendLog(logBuf, LOGLEVEL_INFO);
  140. confSecrets_wasChanged = false;
  141. return true;
  142. #endif
  143. }
  144. else return false;
  145. } // saveConf_Secrets
  146. bool saveConf_DevWiFi() {
  147. return saveConf_DevWiFi(false);
  148. }
  149. bool saveConf_DevWiFi(bool _force)
  150. {
  151. if (confDevWiFi_wasChanged || _force)
  152. {
  153. char configFileName[] = "/confDevWiFi";
  154. File configFile = LittleFS.open(configFileName, "w");
  155. if (!configFile)
  156. {
  157. snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_safeConf_configFileWriteError, configFileName);
  158. sendLog(logBuf, LOGLEVEL_INFO);
  159. return false;
  160. }
  161. configFile.write((byte*) &confDevWiFi, sizeof(confDevWiFi));
  162. configFile.close();
  163. snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_safeConf_configFileWritten, configFileName);
  164. sendLog(logBuf, LOGLEVEL_INFO);
  165. confDevWiFi_wasChanged = false;
  166. return true;
  167. }
  168. else return false;
  169. } // saveConf_DevWiFi
  170. bool saveConf_Web() {
  171. return saveConf_Web(false);
  172. }
  173. bool saveConf_Web(bool _force)
  174. {
  175. if (confWeb_wasChanged || _force)
  176. {
  177. char configFileName[] = "/confWeb";
  178. File configFile = LittleFS.open(configFileName, "w");
  179. if (!configFile)
  180. {
  181. snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_safeConf_configFileWriteError, configFileName);
  182. sendLog(logBuf, LOGLEVEL_INFO);
  183. return false;
  184. }
  185. configFile.write((byte*) &confWeb, sizeof(confWeb));
  186. configFile.close();
  187. snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_safeConf_configFileWritten, configFileName);
  188. sendLog(logBuf, LOGLEVEL_INFO);
  189. confWeb_wasChanged = false;
  190. return true;
  191. }
  192. else
  193. return false;
  194. } // saveConf_Web
  195. bool saveConf_Mqtt() {
  196. return saveConf_Mqtt(false);
  197. }
  198. bool saveConf_Mqtt(bool _force)
  199. {
  200. if (confMqtt_wasChanged || _force)
  201. {
  202. char configFileName[] = "/confMqtt";
  203. File configFile = LittleFS.open(configFileName, "w");
  204. if (!configFile)
  205. {
  206. snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_safeConf_configFileWriteError, configFileName);
  207. sendLog(logBuf, LOGLEVEL_INFO);
  208. return false;
  209. }
  210. configFile.write((byte*) &confMqtt, sizeof(confMqtt));
  211. configFile.close();
  212. snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_safeConf_configFileWritten, configFileName);
  213. sendLog(logBuf, LOGLEVEL_INFO);
  214. confMqtt_wasChanged = false;
  215. return true;
  216. }
  217. else return false;
  218. } // saveConf_Mqtt
  219. #ifdef FIRMWARE_VARIANT_THERMOSTAT
  220. bool saveConf_Therm() {
  221. return saveConf_Therm(false);
  222. }
  223. bool saveConf_Therm(bool _force)
  224. {
  225. if (confTherm_wasChanged || _force)
  226. {
  227. char configFileName[] = "/confTherm";
  228. File configFile = LittleFS.open(configFileName, "w");
  229. if (!configFile)
  230. {
  231. snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_safeConf_configFileWriteError, configFileName);
  232. sendLog(logBuf, LOGLEVEL_INFO);
  233. return false;
  234. }
  235. configFile.write((byte*) &confTherm, sizeof(confTherm));
  236. configFile.close();
  237. snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_safeConf_configFileWritten, configFileName);
  238. sendLog(logBuf, LOGLEVEL_INFO);
  239. confTherm_wasChanged = false;
  240. return true;
  241. }
  242. else
  243. return false;
  244. } // saveConf_Therm
  245. #endif
  246. bool saveConf_Add() {
  247. return saveConf_Add(false);
  248. }
  249. bool saveConf_Add(bool _force)
  250. {
  251. if (confAdd_wasChanged || _force)
  252. {
  253. char configFileName[] = "/confAdd";
  254. File configFile = LittleFS.open(configFileName, "w");
  255. if (!configFile)
  256. {
  257. snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_safeConf_configFileWriteError, configFileName);
  258. sendLog(logBuf, LOGLEVEL_INFO);
  259. return false;
  260. }
  261. configFile.write((byte*) &confAdd, sizeof(confAdd));
  262. configFile.close();
  263. snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_safeConf_configFileWritten, configFileName);
  264. sendLog(logBuf, LOGLEVEL_INFO);
  265. confAdd_wasChanged = false;
  266. return true;
  267. }
  268. else
  269. return false;
  270. } // saveConf_Add
  271. #ifdef ENABLE_FEATURE_NTP_TIME
  272. bool saveConf_Time() {
  273. return saveConf_Time(false);
  274. }
  275. bool saveConf_Time(bool _force)
  276. {
  277. if (confTime_wasChanged || _force)
  278. {
  279. char configFileName[] = "/confTime";
  280. File configFile = LittleFS.open(configFileName, "w");
  281. if (!configFile)
  282. {
  283. snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_safeConf_configFileWriteError, configFileName);
  284. sendLog(logBuf, LOGLEVEL_INFO);
  285. return false;
  286. }
  287. configFile.write((byte*) &confTime, sizeof(confTime));
  288. configFile.close();
  289. snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_safeConf_configFileWritten, configFileName);
  290. sendLog(logBuf, LOGLEVEL_INFO);
  291. confTime_wasChanged = false;
  292. return true;
  293. }
  294. else
  295. return false;
  296. } // saveConf_Time
  297. #endif
  298. bool saveConf_Log() {
  299. return saveConf_Log(false);
  300. }
  301. bool saveConf_Log(bool _force)
  302. {
  303. if (confLog_wasChanged || _force)
  304. {
  305. char configFileName[] = "/confLog";
  306. File configFile = LittleFS.open(configFileName, "w");
  307. if (!configFile)
  308. {
  309. snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_safeConf_configFileWriteError, configFileName);
  310. sendLog(logBuf, LOGLEVEL_INFO);
  311. return false;
  312. }
  313. configFile.write((byte*) &confLog, sizeof(confLog));
  314. configFile.close();
  315. snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_safeConf_configFileWritten, configFileName);
  316. sendLog(logBuf, LOGLEVEL_INFO);
  317. confLog_wasChanged = false;
  318. return true;
  319. }
  320. else
  321. return false;
  322. } // saveConf_Log
  323. bool saveConf_Sens() {
  324. return saveConf_Sens(false);
  325. }
  326. bool saveConf_Sens(bool _force)
  327. {
  328. if (confSens_wasChanged || _force)
  329. {
  330. char configFileName[] = "/confSens";
  331. File configFile = LittleFS.open(configFileName, "w");
  332. if (!configFile)
  333. {
  334. snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_safeConf_configFileWriteError, configFileName);
  335. sendLog(logBuf, LOGLEVEL_INFO);
  336. return false;
  337. }
  338. configFile.write((byte*) &confSens, sizeof(confSens));
  339. configFile.close();
  340. snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_safeConf_configFileWritten, configFileName);
  341. sendLog(logBuf, LOGLEVEL_INFO);
  342. confSens_wasChanged = false;
  343. return true;
  344. }
  345. else
  346. return false;
  347. } // saveConf_Sens
  348. #ifdef FIRMWARE_VARIANT_HEATCONTROL
  349. bool saveConf_Heatc() {
  350. return saveConf_Heatc(false);
  351. }
  352. bool saveConf_Heatc(bool _force)
  353. {
  354. if (confHeatc_wasChanged || _force)
  355. {
  356. char configFileName[] = "/confHeatc";
  357. File configFile = LittleFS.open(configFileName, "w");
  358. if (!configFile)
  359. {
  360. snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_safeConf_configFileWriteError, configFileName);
  361. sendLog(logBuf, LOGLEVEL_INFO);
  362. return false;
  363. }
  364. configFile.write((byte*) &confHeatc, sizeof(confHeatc));
  365. configFile.close();
  366. snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_safeConf_configFileWritten, configFileName);
  367. sendLog(logBuf, LOGLEVEL_INFO);
  368. confHeatc_wasChanged = false;
  369. return true;
  370. }
  371. else
  372. return false;
  373. } // saveConf_Heatc
  374. #endif