commands.ino 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. #define SER_INPUT_SIZE 70
  2. char serBuffer[SER_INPUT_SIZE + 1];
  3. int serBufferCount;
  4. void serialEvent()
  5. {
  6. char ch = Serial.read();
  7. serBuffer[serBufferCount] = ch;
  8. serBufferCount++;
  9. if (ch == 13 || ch == 10)
  10. { // ASCII code 13 = "CR", 10 = "LF"
  11. serBuffer[serBufferCount - 1] = '\0';
  12. #ifdef DEBUG_VERBOSE
  13. Serial.print(F("serial cmd: '"));
  14. Serial.print(serBuffer);
  15. Serial.println("'");
  16. #endif
  17. strlcpy(cmdPayload, serBuffer, sizeof(cmdPayload));
  18. cmdInQueue = true;
  19. evalCmd();
  20. serBufferCount = 0;
  21. }
  22. }
  23. void evalCmd()
  24. {
  25. if (cmdInQueue)
  26. {
  27. //Serial.print("cmdPayload: ");
  28. //Serial.println(cmdPayload);
  29. char cmdPayload_lower[sizeof(cmdPayload)];
  30. strlcpy(cmdPayload_lower, cmdPayload, sizeof(cmdPayload_lower));
  31. strlwr(cmdPayload_lower);
  32. if (strncmp(cmdPayload_lower, "loadconf", 8) == 0)
  33. {
  34. sprintf_P(logBuf, PGMStr_conf_loading);
  35. sendLog(logBuf);
  36. loadConf_all();
  37. }
  38. else if (strncmp(cmdPayload_lower, "set ", 4) == 0)
  39. {
  40. char buf[81];
  41. char setconfCmd[16];
  42. uint8_t len = strlen(cmdPayload) - 4;
  43. for (unsigned char i = 0; i < len; i++)
  44. {
  45. if (i < (sizeof(buf) - 1))
  46. buf[i] = cmdPayload[i + 4];
  47. }
  48. if (len <= (sizeof(buf) - 1))
  49. buf[len] = '\0';
  50. else
  51. buf[(sizeof(buf) - 1)] = '\0';
  52. #ifdef DEBUG_VERBOSE
  53. Serial.print(F("Buf: "));
  54. Serial.println(buf);
  55. #endif
  56. bool cmdNoPayload = false;
  57. uint8_t setconfCmdLen = 0;
  58. for (unsigned char i = 0; i < len; i++)
  59. {
  60. //if (buf[i] == 32 || buf[i] == '\0') break; // if SPACE command name is finished, if \0 command parameter is missing
  61. if (buf[i] == 32 || buf[i] == 61 || buf[i] == 58)
  62. break; // if SPACE(32) or =(61) or :(58) command name is finished
  63. else if (buf[i] == 0 || buf[i] == 10 || buf[i] == 13)
  64. { // if \0, LF (10) or CR (13) command parameter is missing
  65. cmdNoPayload = true;
  66. break;
  67. }
  68. setconfCmd[i] = buf[i];
  69. setconfCmdLen++;
  70. }
  71. setconfCmd[setconfCmdLen] = '\0';
  72. yield();
  73. if (setconfCmdLen == len)
  74. cmdNoPayload = true;
  75. #ifdef DEBUG_VERBOSE
  76. Serial.print(F("setconfCmd: '"));
  77. Serial.print(setconfCmd);
  78. Serial.print("'");
  79. #endif
  80. if (cmdNoPayload)
  81. {
  82. #ifdef DEBUG_VERBOSE
  83. Serial.println(F(", no payload, displaying current value"));
  84. #endif
  85. //getConfig(setconfCmd);
  86. cmdPrintHelp();
  87. }
  88. else
  89. {
  90. char setconfPayload[62];
  91. #ifdef DEBUG_VERBOSE
  92. Serial.println();
  93. #endif
  94. int setconfPayloadLen = 0;
  95. for (int i = 0; i < len; i++)
  96. {
  97. char c = buf[i + setconfCmdLen + 1];
  98. if (c == 0 || c == 10 || c == 13)
  99. break; // if \0, LF (10) or CR (13) command parameter is finished
  100. setconfPayload[i] = c;
  101. setconfPayloadLen++;
  102. }
  103. setconfPayload[setconfPayloadLen] = '\0';
  104. #ifdef DEBUG_VERBOSE
  105. Serial.print(F("setconfPayload: '"));
  106. Serial.print(setconfPayload);
  107. Serial.println("'");
  108. #endif
  109. snprintf(logBuf, LOG_BUFFER_SIZE, "set '%s'->'%s'", setconfCmd, setconfPayload);
  110. sendLog(logBuf, LOGLEVEL_VERBOSE);
  111. setConfig(setconfCmd, setconfPayload);
  112. }
  113. }
  114. #ifdef DEBUGMODE
  115. else if (strncmp(cmdPayload_lower, "get eeprom", 10) == 0)
  116. {
  117. getEEPROMContent();
  118. }
  119. #endif
  120. else if (strncmp(cmdPayload_lower, "get ", 4) == 0)
  121. {
  122. char buf[81];
  123. char setconfCmd[16];
  124. uint8_t len = strlen(cmdPayload) - 4;
  125. for (int i = 0; i < len; i++)
  126. {
  127. if (i < 81)
  128. buf[i] = cmdPayload[i + 4];
  129. }
  130. if (len <= (sizeof(buf) - 1))
  131. buf[len] = '\0';
  132. else
  133. buf[(sizeof(buf) - 1)] = '\0';
  134. #ifdef DEBUG_VERBOSE
  135. Serial.print("Buf: ");
  136. Serial.println(buf);
  137. #endif
  138. uint8_t setconfCmdLen = 0;
  139. for (int i = 0; i < len; i++)
  140. {
  141. //if (buf[i] == 32 || buf[i] == '\0') break; // if SPACE command name is finished, if \0 command parameter is missing
  142. if (buf[i] == 32 || buf[i] == 61 || buf[i] == 58)
  143. break; // if SPACE(32) or =(61) or :(58) command name is finished
  144. else if (buf[i] == 0 || buf[i] == 10 || buf[i] == 13)
  145. { // if \0, LF (10) or CR (13) command parameter is missing
  146. break;
  147. }
  148. setconfCmd[i] = buf[i];
  149. setconfCmdLen++;
  150. }
  151. setconfCmd[setconfCmdLen] = '\0';
  152. yield();
  153. #ifdef DEBUG_VERBOSE
  154. Serial.print("setconfCmd: '");
  155. Serial.print(setconfCmd);
  156. Serial.println("'");
  157. #endif
  158. snprintf(logBuf, LOG_BUFFER_SIZE, "get '%s'", setconfCmd);
  159. sendLog(logBuf);
  160. getConfig(setconfCmd);
  161. }
  162. /*else if (strncmp(cmdPayload, "enc ", 4) == 0) {
  163. char buf[81];
  164. char setconfCmd[16];
  165. uint8_t len = strlen(cmdPayload) - 4;
  166. for (int i = 0; i < len; i++) {
  167. if (i < 81) buf[i] = cmdPayload[i + 4];
  168. }
  169. if (len <= (sizeof(buf)-1)) buf[len] = '\0';
  170. else buf[(sizeof(buf)-1)] = '\0';
  171. #ifdef DEBUG_VERBOSE
  172. Serial.print("Buf: ");
  173. Serial.println(buf);
  174. #endif
  175. uint8_t setconfCmdLen = 0;
  176. for (int i = 0; i < len; i++) {
  177. //if (buf[i] == 32 || buf[i] == '\0') break; // if SPACE command name is finished, if \0 command parameter is missing
  178. if (buf[i] == 32 || buf[i] == 61 || buf[i] == 58) break; // if SPACE(32) or =(61) or :(58) command name is finished
  179. else if (buf[i] == 0 || buf[i] == 10 || buf[i] == 13) { // if \0, LF (10) or CR (13) command parameter is missing
  180. break;
  181. }
  182. setconfCmd[i] = buf[i];
  183. setconfCmdLen++;
  184. }
  185. setconfCmd[setconfCmdLen] = '\0';
  186. yield();
  187. #ifdef DEBUG_VERBOSE
  188. Serial.print("setconfCmd: '");
  189. Serial.print(setconfCmd);
  190. Serial.println("'");
  191. #endif
  192. char bufg[50];
  193. sprintf(bufg, "enc '%s'", setconfCmd);
  194. sendLog(bufg);
  195. sprintf(bufg, "%s", XORENC(setconfCmd, encKey));
  196. sendLog(bufg);
  197. //getConfig(setconfCmd);
  198. }*/
  199. else if (strncmp(cmdPayload_lower, "restart", 7) == 0)
  200. {
  201. sendLog(F("restarting..."));
  202. restart();
  203. }
  204. else if (strncmp(cmdPayload_lower, "sysinfo", 7) == 0)
  205. {
  206. log_sysdata();
  207. }
  208. #ifdef ENABLE_I2C_INTERFACE
  209. else if (strncmp(cmdPayload_lower, "i2cscan", 7) == 0)
  210. {
  211. i2cscan();
  212. }
  213. #endif
  214. //else if (strncmp(cmdPayload_lower, "debugmode 1", 11) == 0)
  215. //{
  216. // sendLog(F("debugmode on"));
  217. // sysInfoEverySecond = true;
  218. //}
  219. //
  220. //else if (strncmp(cmdPayload_lower, "debugmode 0", 11) == 0)
  221. //{
  222. // sendLog(F("debugmode off"));
  223. // sysInfoEverySecond = false;
  224. //}
  225. else if (strncmp(cmdPayload_lower, "debug 2", 7) == 0)
  226. {
  227. sendLog(F("DEBUG=2"));
  228. conf_setDebugMode();
  229. sysInfoEverySecond = true;
  230. }
  231. else if (strncmp(cmdPayload_lower, "debug 1", 7) == 0)
  232. {
  233. sendLog(F("DEBUG=1"));
  234. conf_setDebugMode();
  235. sysInfoEverySecond = false;
  236. }
  237. else if (strncmp(cmdPayload_lower, "debug 0", 7) == 0)
  238. {
  239. sendLog(F("DEBUG=0"));
  240. conf_clearDebugMode();
  241. sysInfoEverySecond = false;
  242. }
  243. else if (strncmp(cmdPayload_lower, "debug", 5) == 0)
  244. {
  245. snprintf(logBuf, LOG_BUFFER_SIZE, "DEBUG=%d", debug);
  246. sendLog(logBuf);
  247. }
  248. #ifdef ENABLE_FEATURE_NTP_TIME
  249. else if (strncmp(cmdPayload_lower, "date", 7) == 0)
  250. {
  251. printDate();
  252. }
  253. #endif
  254. #ifdef ENABLE_FEATURE_NTP_TIME
  255. else if (strncmp(cmdPayload_lower, "syncclock", 7) == 0)
  256. {
  257. syncClock(true);
  258. }
  259. #endif
  260. else if (strncmp(cmdPayload_lower, "clearcreds", 10) == 0)
  261. {
  262. confClearCredentials();
  263. }
  264. else if (strncmp(cmdPayload_lower, "clearsecrets", 12) == 0)
  265. {
  266. #ifdef STORE_SECRETS_IN_EEPROM
  267. clearSecrets_EEPROM();
  268. #else
  269. FS_deleteFile("/confSecrets");
  270. #endif
  271. }
  272. else if (strncmp(cmdPayload_lower, "format eeprom", 13) == 0)
  273. {
  274. formatEEPROM_AllData();
  275. }
  276. else if (strncmp(cmdPayload_lower, "clearwifi", 10) == 0)
  277. {
  278. confClearWiFiCredentials();
  279. }
  280. else if (strncmp(cmdPayload_lower, "saveconf /f", 11) == 0)
  281. {
  282. saveConfig_all(true);
  283. }
  284. else if (strncmp(cmdPayload_lower, "saveconf", 8) == 0)
  285. {
  286. saveConfig_all();
  287. }
  288. else if (strncmp(cmdPayload_lower, "savevalues", 10) == 0)
  289. {
  290. #ifdef FIRMWARE_VARIANT_THERMOSTAT
  291. thermostat_saveValues();
  292. #endif
  293. }
  294. #ifdef ENABLE_FEATURE_SECRETS_ENCRYPTION
  295. else if (strncmp(cmdPayload_lower, "encryptoff /y", 13) == 0)
  296. {
  297. if(confCheckEncrypted()) {
  298. confEncryptOff();
  299. }
  300. }
  301. else if (strncmp(cmdPayload_lower, "encryptoff", 10) == 0)
  302. {
  303. if(confCheckEncrypted()) {
  304. snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_commands_encryptoff);
  305. sendLog(logBuf);
  306. }
  307. }
  308. else if (strncmp(cmdPayload_lower, "encrypt /y", 10) == 0)
  309. {
  310. confEncrypt();
  311. }
  312. else if (strncmp(cmdPayload_lower, "encrypt /f", 10) == 0)
  313. {
  314. confEncrypt(true);
  315. }
  316. else if (strncmp(cmdPayload_lower, "encrypt", 7) == 0)
  317. {
  318. if(!confCheckEncrypted()) {
  319. snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_commands_encrypt);
  320. sendLog(logBuf);
  321. snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_commands_encrypt2);
  322. sendLog(logBuf);
  323. }
  324. }
  325. else if (strncmp(cmdPayload_lower, "decrypt", 7) == 0)
  326. {
  327. if(confCheckEncrypted()) {
  328. snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_commands_decrypt);
  329. sendLog(logBuf);
  330. snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_commands_encryptoff);
  331. sendLog(logBuf);
  332. }
  333. }
  334. #endif
  335. else if (strncmp(cmdPayload_lower, "ls", 2) == 0)
  336. {
  337. FS_listFiles();
  338. }
  339. else if (strncmp(cmdPayload_lower, "del ", 4) == 0)
  340. {
  341. char _buf[31];
  342. uint8_t _buf_i=0;
  343. for(uint8_t _i=4; _i < sizeof(cmdPayload); _i++) {
  344. if(_buf_i < (sizeof(_buf)-1)) {
  345. _buf[_buf_i] = cmdPayload[_i];
  346. _buf_i++;
  347. }
  348. }
  349. _buf[_buf_i] = '\0';
  350. FS_deleteFile(_buf);
  351. }
  352. else if (strncmp(cmdPayload_lower, "rm ", 3) == 0)
  353. {
  354. char _buf[31];
  355. uint8_t _buf_i=0;
  356. for(uint8_t _i=3; _i < sizeof(cmdPayload); _i++) {
  357. if(_buf_i < (sizeof(_buf)-1)) {
  358. _buf[_buf_i] = cmdPayload[_i];
  359. _buf_i++;
  360. }
  361. }
  362. _buf[_buf_i] = '\0';
  363. FS_deleteFile(_buf);
  364. }
  365. else if (strncmp(cmdPayload_lower, "ren ", 4) == 0)
  366. {
  367. char _buf_fileFrom[51];
  368. char _buf_fileTo[51];
  369. uint8_t _startFromChar = 4;
  370. // 1st parameter
  371. uint8_t _buf_i=0;
  372. for(uint8_t _i = _startFromChar; _i < sizeof(cmdPayload); _i++) {
  373. if(_buf_i < (sizeof(_buf_fileFrom)-1)) {
  374. if(cmdPayload[_i] == ' ') break;
  375. else {
  376. _buf_fileFrom[_buf_i] = cmdPayload[_i];
  377. _buf_i++;
  378. }
  379. }
  380. }
  381. _buf_fileFrom[_buf_i] = '\0';
  382. //snprintf(logBuf, LOG_BUFFER_SIZE, "ren: 1st='%s', _buf_i=%d", _buf_fileFrom, _buf_i);
  383. //sendLog(logBuf);
  384. // 2nd parameter
  385. _startFromChar += _buf_i + 1;
  386. uint8_t _buf_i2=0;
  387. for(uint8_t _i = _startFromChar; _i < sizeof(cmdPayload); _i++) {
  388. if(_buf_i2 < (sizeof(_buf_fileTo)-1)) {
  389. if(cmdPayload[_i] == ' ') break;
  390. else {
  391. _buf_fileTo[_buf_i2] = cmdPayload[_i];
  392. _buf_i2++;
  393. }
  394. }
  395. }
  396. _buf_fileTo[_buf_i2] = '\0';
  397. //snprintf(logBuf, LOG_BUFFER_SIZE, "ren: 2nd='%s', _buf_i2=%d", _buf_fileTo, _buf_i2);
  398. //sendLog(logBuf);
  399. //snprintf(logBuf, LOG_BUFFER_SIZE, "ren '%s' to '%s'", _buf_fileFrom, _buf_fileTo);
  400. //sendLog(logBuf);
  401. FS_renameFile(_buf_fileFrom, _buf_fileTo);
  402. }
  403. else if (strncmp(cmdPayload_lower, "ipcfg", 5) == 0)
  404. {
  405. printIpcfg();
  406. }
  407. else if (strncmp(cmdPayload_lower, "help", 4) == 0)
  408. {
  409. cmdPrintHelp();
  410. }
  411. else if (strncmp(cmdPayload_lower, "mqttreset", 9) == 0)
  412. {
  413. mqttResetConnection();
  414. }
  415. else if (strncmp(cmdPayload_lower, "mqttinfo", 8) == 0)
  416. {
  417. mqtt_publishStatus();
  418. }
  419. else if (strncmp(cmdPayload_lower, "getconf", 7) == 0)
  420. {
  421. sendLog(F("CONF: listing all conf parameters:"));
  422. getConfig((char *)"confDevWiFi");
  423. getConfig((char *)"confWeb");
  424. getConfig((char *)"confMqtt");
  425. getConfig((char *)"confAdd");
  426. getConfig((char *)"confTime");
  427. getConfig((char *)"confLog");
  428. getConfig((char *)"confSens");
  429. #ifdef FIRMWARE_VARIANT_THERMOSTAT
  430. getConfig((char *)"confTherm");
  431. getConfig((char *)"confThermAdv");
  432. #endif
  433. #ifdef FIRMWARE_VARIANT_HEATCONTROL
  434. getConfig((char *)"confHeatc");
  435. #endif
  436. sendLog(F("------"));
  437. }
  438. else if (strncmp(cmdPayload_lower, "delconf", 7) == 0)
  439. {
  440. sendLog(F("deleting configuration..."));
  441. deleteConfig();
  442. }
  443. #ifdef ENABLE_SENSORS_ONEWIRE
  444. else if (strncmp(cmdPayload_lower, "sensassign", 10) == 0)
  445. {
  446. oneWireSensors_loadAssignments(true);
  447. }
  448. else if (strncmp(cmdPayload_lower, "sensshow", 8) == 0)
  449. {
  450. oneWireSensors_show();
  451. }
  452. #endif
  453. cmdInQueue = false;
  454. }
  455. }
  456. void cmdPrintHelp()
  457. {
  458. sendLog(F("valid commands:"));
  459. sendLog(F(" sysinfo show uptime, heap usage..."));
  460. sendLog(F(" date show current date/time"));
  461. sendLog(F(" syncclock perform NTP time sync now"));
  462. sendLog(F(" debug [0/1]"));
  463. sendLog(F(" loadconf load saved configuration"));
  464. sendLog(F(" getconf list all conf parameters"));
  465. sendLog(F(" get [PARAM] show conf value"));
  466. sendLog(F(" get values list current saved values"));
  467. sendLog(F(" set [PARAM] [VALUE] (1 blank after PARAM to clear)"));
  468. sendLog(F(" saveconf save all configurations"));
  469. sendLog(F(" savevalues save set values"));
  470. sendLog(F(" clearcreds delete all passwords"));
  471. sendLog(F(" clearwifi delete WiFi configurations"));
  472. sendLog(F(" mqttinfo MQTT connection status"));
  473. sendLog(F(" mqttreset reset MQTT connection"));
  474. sendLog(F(" encrypt switch on encrypted stored passwords"));
  475. sendLog(F(" decrypt switch off encrypted stored passwords"));
  476. sendLog(F(" WARNING: deletes all currently saved credentials"));
  477. #ifdef ENABLE_SENSORS_ONEWIRE
  478. sendLog(F(" "));
  479. sendLog(F(" sensshow list OneWire sensors"));
  480. sendLog(F(" sensassign (re)load OneWire sensor assignments"));
  481. #endif
  482. sendLog(F(" "));
  483. sendLog(F(" ls list files on File System"));
  484. sendLog(F(" del [name] delete a file on File System"));
  485. sendLog(F(" ren [name] rename a file on File System"));
  486. sendLog(F(" "));
  487. sendLog(F(" delconf delete ALL configuration"));
  488. sendLog(F(" restart"));
  489. }