commands.ino 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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("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. sendLog(F("loading config..."));
  35. loadConf_all();
  36. }
  37. else if (strncmp(cmdPayload_lower, "set ", 4) == 0)
  38. {
  39. char buf[81];
  40. char setconfCmd[16];
  41. uint8_t len = strlen(cmdPayload) - 4;
  42. for (unsigned char i = 0; i < len; i++)
  43. {
  44. if (i < (sizeof(buf) - 1))
  45. buf[i] = cmdPayload[i + 4];
  46. }
  47. if (len <= (sizeof(buf) - 1))
  48. buf[len] = '\0';
  49. else
  50. buf[(sizeof(buf) - 1)] = '\0';
  51. #ifdef DEBUG_VERBOSE
  52. Serial.print("Buf: ");
  53. Serial.println(buf);
  54. #endif
  55. bool cmdNoPayload = false;
  56. uint8_t setconfCmdLen = 0;
  57. for (unsigned char i = 0; i < len; i++)
  58. {
  59. //if (buf[i] == 32 || buf[i] == '\0') break; // if SPACE command name is finished, if \0 command parameter is missing
  60. if (buf[i] == 32 || buf[i] == 61 || buf[i] == 58)
  61. break; // if SPACE(32) or =(61) or :(58) command name is finished
  62. else if (buf[i] == 0 || buf[i] == 10 || buf[i] == 13)
  63. { // if \0, LF (10) or CR (13) command parameter is missing
  64. cmdNoPayload = true;
  65. break;
  66. }
  67. setconfCmd[i] = buf[i];
  68. setconfCmdLen++;
  69. }
  70. setconfCmd[setconfCmdLen] = '\0';
  71. yield();
  72. if (setconfCmdLen == len)
  73. cmdNoPayload = true;
  74. #ifdef DEBUG_VERBOSE
  75. Serial.print("setconfCmd: '");
  76. Serial.print(setconfCmd);
  77. Serial.print("'");
  78. #endif
  79. if (cmdNoPayload)
  80. {
  81. #ifdef DEBUG_VERBOSE
  82. Serial.println(", no payload, displaying current value");
  83. #endif
  84. //getConfig(setconfCmd);
  85. cmdPrintHelp();
  86. }
  87. else
  88. {
  89. char setconfPayload[62];
  90. #ifdef DEBUG_VERBOSE
  91. Serial.println();
  92. #endif
  93. int setconfPayloadLen = 0;
  94. for (int i = 0; i < len; i++)
  95. {
  96. char c = buf[i + setconfCmdLen + 1];
  97. if (c == 0 || c == 10 || c == 13)
  98. break; // if \0, LF (10) or CR (13) command parameter is finished
  99. setconfPayload[i] = c;
  100. setconfPayloadLen++;
  101. }
  102. setconfPayload[setconfPayloadLen] = '\0';
  103. #ifdef DEBUG_VERBOSE
  104. Serial.print("setconfPayload: '");
  105. Serial.print(setconfPayload);
  106. Serial.println("'");
  107. #endif
  108. char bufg[100];
  109. sprintf(bufg, "set '%s'->'%s'", setconfCmd, setconfPayload);
  110. sendLog(bufg, LOGLEVEL_VERBOSE);
  111. setConfig(setconfCmd, setconfPayload);
  112. }
  113. }
  114. else if (strncmp(cmdPayload_lower, "get ", 4) == 0)
  115. {
  116. char buf[81];
  117. char setconfCmd[16];
  118. uint8_t len = strlen(cmdPayload) - 4;
  119. for (int i = 0; i < len; i++)
  120. {
  121. if (i < 81)
  122. buf[i] = cmdPayload[i + 4];
  123. }
  124. if (len <= (sizeof(buf) - 1))
  125. buf[len] = '\0';
  126. else
  127. buf[(sizeof(buf) - 1)] = '\0';
  128. #ifdef DEBUG_VERBOSE
  129. Serial.print("Buf: ");
  130. Serial.println(buf);
  131. #endif
  132. uint8_t setconfCmdLen = 0;
  133. for (int i = 0; i < len; i++)
  134. {
  135. //if (buf[i] == 32 || buf[i] == '\0') break; // if SPACE command name is finished, if \0 command parameter is missing
  136. if (buf[i] == 32 || buf[i] == 61 || buf[i] == 58)
  137. break; // if SPACE(32) or =(61) or :(58) command name is finished
  138. else if (buf[i] == 0 || buf[i] == 10 || buf[i] == 13)
  139. { // if \0, LF (10) or CR (13) command parameter is missing
  140. break;
  141. }
  142. setconfCmd[i] = buf[i];
  143. setconfCmdLen++;
  144. }
  145. setconfCmd[setconfCmdLen] = '\0';
  146. yield();
  147. #ifdef DEBUG_VERBOSE
  148. Serial.print("setconfCmd: '");
  149. Serial.print(setconfCmd);
  150. Serial.println("'");
  151. #endif
  152. char bufg[50];
  153. sprintf(bufg, "get '%s'", setconfCmd);
  154. sendLog(bufg);
  155. getConfig(setconfCmd);
  156. }
  157. /*else if (strncmp(cmdPayload, "enc ", 4) == 0) {
  158. char buf[81];
  159. char setconfCmd[16];
  160. uint8_t len = strlen(cmdPayload) - 4;
  161. for (int i = 0; i < len; i++) {
  162. if (i < 81) buf[i] = cmdPayload[i + 4];
  163. }
  164. if (len <= (sizeof(buf)-1)) buf[len] = '\0';
  165. else buf[(sizeof(buf)-1)] = '\0';
  166. #ifdef DEBUG_VERBOSE
  167. Serial.print("Buf: ");
  168. Serial.println(buf);
  169. #endif
  170. uint8_t setconfCmdLen = 0;
  171. for (int i = 0; i < len; i++) {
  172. //if (buf[i] == 32 || buf[i] == '\0') break; // if SPACE command name is finished, if \0 command parameter is missing
  173. if (buf[i] == 32 || buf[i] == 61 || buf[i] == 58) break; // if SPACE(32) or =(61) or :(58) command name is finished
  174. else if (buf[i] == 0 || buf[i] == 10 || buf[i] == 13) { // if \0, LF (10) or CR (13) command parameter is missing
  175. break;
  176. }
  177. setconfCmd[i] = buf[i];
  178. setconfCmdLen++;
  179. }
  180. setconfCmd[setconfCmdLen] = '\0';
  181. yield();
  182. #ifdef DEBUG_VERBOSE
  183. Serial.print("setconfCmd: '");
  184. Serial.print(setconfCmd);
  185. Serial.println("'");
  186. #endif
  187. char bufg[50];
  188. sprintf(bufg, "enc '%s'", setconfCmd);
  189. sendLog(bufg);
  190. sprintf(bufg, "%s", XORENC(setconfCmd, encKey));
  191. sendLog(bufg);
  192. //getConfig(setconfCmd);
  193. }*/
  194. else if (strncmp(cmdPayload_lower, "restart", 7) == 0)
  195. {
  196. sendLog(F("restarting..."));
  197. restart();
  198. }
  199. else if (strncmp(cmdPayload_lower, "sysinfo", 7) == 0)
  200. {
  201. logSysdata();
  202. }
  203. else if (strncmp(cmdPayload_lower, "debugmode 1", 11) == 0)
  204. {
  205. sendLog(F("debugmode on"));
  206. sysInfoEverySecond = true;
  207. }
  208. else if (strncmp(cmdPayload_lower, "debugmode 0", 11) == 0)
  209. {
  210. sendLog(F("debugmode off"));
  211. sysInfoEverySecond = false;
  212. }
  213. #ifdef ENABLE_FEATURE_NTP_TIME
  214. else if (strncmp(cmdPayload_lower, "date", 7) == 0)
  215. {
  216. printDate();
  217. }
  218. #endif
  219. #ifdef ENABLE_FEATURE_NTP_TIME
  220. else if (strncmp(cmdPayload_lower, "syncclock", 7) == 0)
  221. {
  222. syncClock(true);
  223. }
  224. #endif
  225. else if (strncmp(cmdPayload_lower, "clearcreds", 10) == 0)
  226. {
  227. confClearCredentials();
  228. }
  229. else if (strncmp(cmdPayload_lower, "clearwifi", 10) == 0)
  230. {
  231. confClearWiFiCredentials();
  232. }
  233. else if (strncmp(cmdPayload_lower, "saveconf", 8) == 0)
  234. {
  235. saveConfig_all();
  236. }
  237. else if (strncmp(cmdPayload_lower, "savevalues", 10) == 0)
  238. {
  239. saveValues();
  240. }
  241. else if (strncmp(cmdPayload_lower, "encrypt", 10) == 0)
  242. {
  243. confEncrypt();
  244. }
  245. else if (strncmp(cmdPayload_lower, "decrypt", 10) == 0)
  246. {
  247. confDecrypt();
  248. }
  249. else if (strncmp(cmdPayload_lower, "ls", 2) == 0)
  250. {
  251. SPIFFS_listFiles();
  252. }
  253. else if (strncmp(cmdPayload_lower, "ipcfg", 5) == 0)
  254. {
  255. printIpcfg();
  256. }
  257. else if (strncmp(cmdPayload_lower, "help", 4) == 0)
  258. {
  259. cmdPrintHelp();
  260. }
  261. else if (strncmp(cmdPayload_lower, "mqttreset", 9) == 0)
  262. {
  263. mqttResetConnection();
  264. }
  265. else if (strncmp(cmdPayload_lower, "mqttinfo", 8) == 0)
  266. {
  267. mqttPublishStatus();
  268. }
  269. else if (strncmp(cmdPayload_lower, "getconf", 7) == 0)
  270. {
  271. sendLog(F("CONF: listing all conf parameters:"));
  272. getConfig((char *)"confDevWiFi");
  273. getConfig((char *)"confWeb");
  274. getConfig((char *)"confMqtt");
  275. getConfig((char *)"confBas");
  276. getConfig((char *)"confAdv");
  277. getConfig((char *)"confAdd");
  278. getConfig((char *)"confTime");
  279. getConfig((char *)"confLog");
  280. sendLog(F("------"));
  281. }
  282. else if (strncmp(cmdPayload_lower, "delconf", 7) == 0)
  283. {
  284. sendLog(F("deleting configuration..."));
  285. deleteConfig();
  286. }
  287. cmdInQueue = false;
  288. }
  289. }
  290. void cmdPrintHelp()
  291. {
  292. sendLog(F("valid commands:"));
  293. sendLog(F(" sysinfo show uptime, heap usage..."));
  294. sendLog(F(" date show current date/time"));
  295. sendLog(F(" syncclock perform NTP time sync now"));
  296. sendLog(F(" debugmode [0/1]"));
  297. sendLog(F(" loadconf load saved configuration"));
  298. sendLog(F(" getconf list all conf parameters"));
  299. sendLog(F(" get [PARAM] show conf value"));
  300. sendLog(F(" get values list current saved values"));
  301. sendLog(F(" set [PARAM] [VALUE] (1 blank after PARAM to clear)"));
  302. sendLog(F(" saveconf save all configurations"));
  303. sendLog(F(" savevalues save set values"));
  304. sendLog(F(" clearcreds delete all passwords"));
  305. sendLog(F(" clearwifi delete WiFi configurations"));
  306. sendLog(F(" mqttinfo MQTT connection status"));
  307. sendLog(F(" mqttreset reset MQTT connection"));
  308. sendLog(F(" encrypt switch on encrypted stored passwords"));
  309. sendLog(F(" decrypt switch off encrypted stored passwords"));
  310. sendLog(F(" WARNING: deletes all currently saved credentials"));
  311. sendLog(F(" ls list files on SPIFFS"));
  312. sendLog(F(" delconf delete ALL configuration"));
  313. sendLog(F(" restart"));
  314. }