config_load.ino 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. bool loadConf_Secrets() // from EEPROM or File System, with or without encryption
  2. {
  3. bool _isEncrypted = confCheckEncrypted();
  4. #ifdef ENABLE_FEATURE_SECRETS_ENCRYPTION
  5. struct confDataSecrets
  6. {
  7. char WiFiPW1[64];
  8. char WiFiPW2[64];
  9. char WiFiAPModePassword[64];
  10. char http_token[31];
  11. char http_user[31];
  12. char http_pass[31];
  13. char http_user1[31];
  14. char http_pass1[31];
  15. char http_user2[31];
  16. char http_pass2[31];
  17. char mqtt_user[31];
  18. char mqtt_pass[31];
  19. // --- 471 bytes
  20. char spare[23];
  21. // --- 494 bytes + 2 bytes size information
  22. // in EEPROM before the actual data => 512 bytes
  23. } _confSecrets_encrypted;
  24. #endif
  25. bool _success = false;
  26. #ifdef STORE_SECRETS_IN_EEPROM
  27. uint16_t _size;
  28. EEPROM.begin(EEPROM_SIZE);
  29. EEPROM.get(EEPROM_STARTADDR_SECRETS, _size);
  30. if( (_size + EEPROM_STARTADDR_SECRETS + 2) <= EEPROM_SIZE ) {
  31. if(_size == sizeof(confSecrets)) {
  32. // size of stored data is stored in first 2 bytes
  33. // as it matches sizeof struct - proceed
  34. // restore complete confSecrets struct from EEPROM
  35. #ifdef ENABLE_FEATURE_SECRETS_ENCRYPTION
  36. EEPROM.get(EEPROM_STARTADDR_SECRETS + 2, _confSecrets_encrypted);
  37. #else
  38. EEPROM.get(EEPROM_STARTADDR_SECRETS + 2, confSecrets);
  39. #endif
  40. EEPROM.end();
  41. snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_loadConf_restoredSecrets_EEPROM, _size);
  42. sendLog(logBuf);
  43. // // size in EEPROM is smaller than the struct
  44. // // so there was a program extension/update
  45. // // --> fill up rest of struct with 0x00
  46. // // as it will now contain most likely all 0xFF
  47. // // from previously unused EEPROM
  48. // if(_size < sizeof(confSecrets)) {
  49. // snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_loadConf_restoredSecrets_fillUp_EEPROM, sizeof(confSecrets) - _size);
  50. // sendLog(logBuf);
  51. // uint8_t _zero = '\0';
  52. // for(uint16_t _i = 0; _i < sizeof(confSecrets); _i++) {
  53. // if(_i >= (_size - 1)) {
  54. // memcpy(&confSecrets + _i, &_zero, 1);
  55. // }
  56. // }
  57. // }
  58. // --> untested, dont use this
  59. _success = true;
  60. }
  61. else {
  62. EEPROM.end();
  63. snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_loadConf_errorRestoreSecrets_EEPROM, _size, sizeof(confSecrets));
  64. sendLog(logBuf);
  65. _success = false;
  66. }
  67. }
  68. else {
  69. EEPROM.end();
  70. snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_loadConf_errorRestoreSecretsExceedSize_EEPROM);
  71. sendLog(logBuf);
  72. _success = false;
  73. }
  74. #else // STORE_SECRETS_IN_EEPROM is not defined - use file on LittleFS
  75. char configFileName[] = "/confSecrets";
  76. if (LittleFS.exists(configFileName)) {
  77. File configFile = LittleFS.open(configFileName, "r");
  78. if (!configFile) {
  79. snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_loadConf_ErrorOpenFile, configFileName);
  80. sendLog(logBuf);
  81. _success = false;
  82. }
  83. else
  84. {
  85. #ifdef ENABLE_FEATURE_SECRETS_ENCRYPTION
  86. configFile.read((byte *)&_confSecrets_encrypted, sizeof(_confSecrets_encrypted));
  87. #else
  88. configFile.read((byte *)&confSecrets, sizeof(confSecrets));
  89. #endif
  90. snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_loadConf_restoredConf, configFileName, configFile.size());
  91. sendLog(logBuf);
  92. configFile.close();
  93. _success = true;
  94. }
  95. }
  96. else
  97. {
  98. snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_loadConf_FileNotExist, configFileName);
  99. sendLog(logBuf);
  100. _success = false;
  101. }
  102. #endif
  103. if(_success) {
  104. #ifdef ENABLE_FEATURE_SECRETS_ENCRYPTION
  105. if(_isEncrypted) {
  106. XORENC(_confSecrets_encrypted.WiFiPW1, encKey);
  107. XORENC(_confSecrets_encrypted.WiFiPW2, encKey);
  108. XORENC(_confSecrets_encrypted.WiFiAPModePassword, encKey);
  109. XORENC(_confSecrets_encrypted.http_token, encKey);
  110. XORENC(_confSecrets_encrypted.http_user, encKey);
  111. XORENC(_confSecrets_encrypted.http_pass, encKey);
  112. XORENC(_confSecrets_encrypted.http_user1, encKey);
  113. XORENC(_confSecrets_encrypted.http_pass1, encKey);
  114. XORENC(_confSecrets_encrypted.http_user2, encKey);
  115. XORENC(_confSecrets_encrypted.http_pass2, encKey);
  116. XORENC(_confSecrets_encrypted.mqtt_user, encKey);
  117. XORENC(_confSecrets_encrypted.mqtt_pass, encKey);
  118. snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_loadConf_encryptedSecretsLoaded);
  119. sendLog(logBuf);
  120. }
  121. memcpy(&confSecrets, &_confSecrets_encrypted, sizeof(confSecrets));
  122. #ifdef DEBUG_SECRETS_ENCRYPTION
  123. if(_isEncrypted) {
  124. Serial.println(F("Secrets UNencrypted:"));
  125. Serial.print(F("WiFiPW1: ")); Serial.println(confSecrets.WiFiPW1);
  126. Serial.print(F("WiFiPW2: ")); Serial.println(confSecrets.WiFiPW2);
  127. Serial.print(F("WiFiAPPW: ")); Serial.println(confSecrets.WiFiAPModePassword);
  128. Serial.print(F("http_token: ")); Serial.println(confSecrets.http_token);
  129. Serial.print(F("http_user: ")); Serial.println(confSecrets.http_user);
  130. Serial.print(F("http_pass: ")); Serial.println(confSecrets.http_pass);
  131. Serial.print(F("http_user1: ")); Serial.println(confSecrets.http_user1);
  132. Serial.print(F("http_pass1: ")); Serial.println(confSecrets.http_pass1);
  133. Serial.print(F("http_user2: ")); Serial.println(confSecrets.http_user2);
  134. Serial.print(F("http_pass2: ")); Serial.println(confSecrets.http_pass2);
  135. Serial.print(F("mqtt_user: ")); Serial.println(confSecrets.mqtt_user);
  136. Serial.print(F("mqtt_pass: ")); Serial.println(confSecrets.mqtt_pass);
  137. Serial.println(F("Secrets Encrypted:"));
  138. Serial.print(F("WiFiPW1: ")); Serial.println(_confSecrets_encrypted.WiFiPW1);
  139. Serial.print(F("WiFiPW2: ")); Serial.println(_confSecrets_encrypted.WiFiPW2);
  140. Serial.print(F("WiFiAPPW: ")); Serial.println(_confSecrets_encrypted.WiFiAPModePassword);
  141. Serial.print(F("http_token: ")); Serial.println(_confSecrets_encrypted.http_token);
  142. Serial.print(F("http_user: ")); Serial.println(_confSecrets_encrypted.http_user);
  143. Serial.print(F("http_pass: ")); Serial.println(_confSecrets_encrypted.http_pass);
  144. Serial.print(F("http_user1: ")); Serial.println(_confSecrets_encrypted.http_user1);
  145. Serial.print(F("http_pass1: ")); Serial.println(_confSecrets_encrypted.http_pass1);
  146. Serial.print(F("http_user2: ")); Serial.println(_confSecrets_encrypted.http_user2);
  147. Serial.print(F("http_pass2: ")); Serial.println(_confSecrets_encrypted.http_pass2);
  148. Serial.print(F("mqtt_user: ")); Serial.println(_confSecrets_encrypted.mqtt_user);
  149. Serial.print(F("mqtt_pass: ")); Serial.println(_confSecrets_encrypted.mqtt_pass);
  150. Serial.println();
  151. }
  152. #endif
  153. #endif
  154. return true;
  155. }
  156. else {
  157. return false;
  158. }
  159. } // loadConf_Secrets
  160. bool loadConf_DevWiFi()
  161. {
  162. char configFileName[] = "/confDevWiFi";
  163. if (LittleFS.exists(configFileName)) {
  164. File configFile = LittleFS.open(configFileName, "r");
  165. if (!configFile) {
  166. snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_loadConf_ErrorOpenFile, configFileName);
  167. sendLog(logBuf);
  168. return false;
  169. }
  170. else
  171. {
  172. configFile.read((byte *)&confDevWiFi, sizeof(confDevWiFi));
  173. snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_loadConf_restoredConf, configFileName, configFile.size());
  174. sendLog(logBuf);
  175. configFile.close();
  176. return true;
  177. }
  178. }
  179. else
  180. {
  181. snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_loadConf_FileNotExist, configFileName);
  182. sendLog(logBuf);
  183. return false;
  184. }
  185. } // loadConf_DevWiFi
  186. bool loadConf_Web()
  187. {
  188. //char configFileName[] = "/confWeb.json";
  189. char configFileName[] = "/confWeb";
  190. if (LittleFS.exists(configFileName))
  191. {
  192. File configFile = LittleFS.open(configFileName, "r");
  193. if (!configFile) {
  194. snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_loadConf_ErrorOpenFile, configFileName);
  195. sendLog(logBuf);
  196. return false;
  197. }
  198. else
  199. {
  200. configFile.read((byte *)&confWeb, sizeof(confWeb));
  201. snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_loadConf_restoredConf, configFileName, configFile.size());
  202. sendLog(logBuf);
  203. configFile.close();
  204. return true;
  205. }
  206. }
  207. else
  208. {
  209. snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_loadConf_FileNotExist, configFileName);
  210. sendLog(logBuf);
  211. return false;
  212. }
  213. } // loadConf_Web
  214. bool loadConf_Mqtt()
  215. {
  216. char configFileName[] = "/confMqtt";
  217. if (LittleFS.exists(configFileName))
  218. {
  219. File configFile = LittleFS.open(configFileName, "r");
  220. if (!configFile) {
  221. snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_loadConf_ErrorOpenFile, configFileName);
  222. sendLog(logBuf);
  223. return false;
  224. }
  225. else
  226. {
  227. configFile.read((byte *)&confMqtt, sizeof(confMqtt));
  228. snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_loadConf_restoredConf, configFileName, configFile.size());
  229. sendLog(logBuf);
  230. configFile.close();
  231. return true;
  232. }
  233. }
  234. else
  235. {
  236. snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_loadConf_FileNotExist, configFileName);
  237. sendLog(logBuf);
  238. return false;
  239. }
  240. } // loadConf_Mqtt
  241. #ifdef FIRMWARE_VARIANT_THERMOSTAT
  242. bool loadConf_Therm()
  243. {
  244. char configFileName[] = "/confTherm";
  245. if (LittleFS.exists(configFileName))
  246. {
  247. File configFile = LittleFS.open(configFileName, "r");
  248. if (!configFile) {
  249. snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_loadConf_ErrorOpenFile, configFileName);
  250. sendLog(logBuf);
  251. return false;
  252. }
  253. else
  254. {
  255. configFile.read((byte *)&confTherm, sizeof(confTherm));
  256. snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_loadConf_restoredConf, configFileName, configFile.size());
  257. sendLog(logBuf);
  258. configFile.close();
  259. return true;
  260. }
  261. }
  262. else
  263. {
  264. snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_loadConf_FileNotExist, configFileName);
  265. sendLog(logBuf);
  266. return false;
  267. }
  268. } // loadConf_Therm
  269. #endif
  270. bool loadConf_Add()
  271. {
  272. char configFileName[] = "/confAdd";
  273. if (LittleFS.exists(configFileName))
  274. {
  275. File configFile = LittleFS.open(configFileName, "r");
  276. if (!configFile) {
  277. snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_loadConf_ErrorOpenFile, configFileName);
  278. sendLog(logBuf);
  279. return false;
  280. }
  281. else
  282. {
  283. configFile.read((byte *)&confAdd, sizeof(confAdd));
  284. snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_loadConf_restoredConf, configFileName, configFile.size());
  285. sendLog(logBuf);
  286. configFile.close();
  287. return true;
  288. }
  289. }
  290. else
  291. {
  292. snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_loadConf_FileNotExist, configFileName);
  293. sendLog(logBuf);
  294. return false;
  295. }
  296. } // loadConf_Add
  297. #ifdef ENABLE_FEATURE_NTP_TIME
  298. bool loadConf_Time()
  299. {
  300. char configFileName[] = "/confTime";
  301. if (LittleFS.exists(configFileName))
  302. {
  303. File configFile = LittleFS.open(configFileName, "r");
  304. if (!configFile) {
  305. snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_loadConf_ErrorOpenFile, configFileName);
  306. sendLog(logBuf);
  307. return false;
  308. }
  309. else
  310. {
  311. configFile.read((byte *)&confTime, sizeof(confTime));
  312. snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_loadConf_restoredConf, configFileName, configFile.size());
  313. sendLog(logBuf);
  314. configFile.close();
  315. return true;
  316. }
  317. }
  318. else
  319. {
  320. snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_loadConf_FileNotExist, configFileName);
  321. sendLog(logBuf);
  322. return false;
  323. }
  324. } // loadConf_Time
  325. #endif
  326. bool loadConf_Log()
  327. {
  328. char configFileName[] = "/confLog";
  329. if (LittleFS.exists(configFileName))
  330. {
  331. File configFile = LittleFS.open(configFileName, "r");
  332. if (!configFile) {
  333. snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_loadConf_ErrorOpenFile, configFileName);
  334. sendLog(logBuf);
  335. return false;
  336. }
  337. else
  338. {
  339. configFile.read((byte *)&confLog, sizeof(confLog));
  340. snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_loadConf_restoredConf, configFileName, configFile.size());
  341. sendLog(logBuf);
  342. configFile.close();
  343. return true;
  344. }
  345. }
  346. else
  347. {
  348. snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_loadConf_FileNotExist, configFileName);
  349. sendLog(logBuf);
  350. return false;
  351. }
  352. } // loadConf_Log
  353. bool loadConf_Sens()
  354. {
  355. char configFileName[] = "/confSens";
  356. if (LittleFS.exists(configFileName))
  357. {
  358. File configFile = LittleFS.open(configFileName, "r");
  359. if (!configFile) {
  360. snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_loadConf_ErrorOpenFile, configFileName);
  361. sendLog(logBuf);
  362. return false;
  363. }
  364. else
  365. {
  366. configFile.read((byte *)&confSens, sizeof(confSens));
  367. snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_loadConf_restoredConf, configFileName, configFile.size());
  368. sendLog(logBuf);
  369. configFile.close();
  370. return true;
  371. }
  372. }
  373. else
  374. {
  375. snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_loadConf_FileNotExist, configFileName);
  376. sendLog(logBuf);
  377. return false;
  378. }
  379. } // loadConf_Sens
  380. #ifdef FIRMWARE_VARIANT_HEATCONTROL
  381. bool loadConf_Heatc()
  382. {
  383. char configFileName[] = "/confHeatc";
  384. if (LittleFS.exists(configFileName))
  385. {
  386. File configFile = LittleFS.open(configFileName, "r");
  387. if (!configFile) {
  388. snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_loadConf_ErrorOpenFile, configFileName);
  389. sendLog(logBuf);
  390. return false;
  391. }
  392. else
  393. {
  394. configFile.read((byte *)&confHeatc, sizeof(confHeatc));
  395. snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_loadConf_restoredConf, configFileName, configFile.size());
  396. sendLog(logBuf);
  397. configFile.close();
  398. return true;
  399. }
  400. }
  401. else
  402. {
  403. snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_loadConf_FileNotExist, configFileName);
  404. sendLog(logBuf);
  405. return false;
  406. }
  407. } // loadConf_Heatc
  408. #endif