commands.ino 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. #define SER_INPUT_SIZE 70
  2. char serBuffer[SER_INPUT_SIZE + 1];
  3. int serBufferCount;
  4. void serialEvent() {
  5. char ch = Serial.read();
  6. serBuffer[serBufferCount] = ch;
  7. serBufferCount++;
  8. if (ch == 13 || ch == 10) { // ASCII code 13 = "CR", 10 = "LF"
  9. serBuffer[serBufferCount - 1] = '\0';
  10. #ifdef DEBUG_VERBOSE
  11. Serial.print("serial cmd: '");
  12. Serial.print(serBuffer);
  13. Serial.println("'");
  14. #endif
  15. strlcpy(cmdPayload, serBuffer, sizeof(cmdPayload));
  16. cmdInQueue = true;
  17. evalCmd();
  18. serBufferCount = 0;
  19. }
  20. }
  21. void evalCmd() {
  22. if (cmdInQueue) {
  23. //Serial.print("cmdPayload: ");
  24. //Serial.println(cmdPayload);
  25. char cmdPayload_lower[sizeof(cmdPayload)];
  26. strlcpy(cmdPayload_lower, cmdPayload, sizeof(cmdPayload_lower));
  27. strlwr(cmdPayload_lower);
  28. if (strncmp(cmdPayload_lower, "loadconf", 8) == 0) {
  29. sendLog(F("loading config..."));
  30. loadConf_all();
  31. }
  32. else if (strncmp(cmdPayload_lower, "set ", 4) == 0) {
  33. char buf[81];
  34. char setconfCmd[16];
  35. uint8_t len = strlen(cmdPayload) - 4;
  36. for (unsigned char i = 0; i < len; i++) {
  37. if (i < (sizeof(buf)-1)) buf[i] = cmdPayload[i + 4];
  38. }
  39. if (len <= (sizeof(buf)-1)) buf[len] = '\0';
  40. else buf[(sizeof(buf)-1)] = '\0';
  41. #ifdef DEBUG_VERBOSE
  42. Serial.print("Buf: ");
  43. Serial.println(buf);
  44. #endif
  45. bool cmdNoPayload = false;
  46. uint8_t setconfCmdLen = 0;
  47. for (unsigned char i = 0; i < len; i++) {
  48. //if (buf[i] == 32 || buf[i] == '\0') break; // if SPACE command name is finished, if \0 command parameter is missing
  49. if (buf[i] == 32 || buf[i] == 61 || buf[i] == 58) break; // if SPACE(32) or =(61) or :(58) command name is finished
  50. else if (buf[i] == 0 || buf[i] == 10 || buf[i] == 13) { // if \0, LF (10) or CR (13) command parameter is missing
  51. cmdNoPayload = true;
  52. break;
  53. }
  54. setconfCmd[i] = buf[i];
  55. setconfCmdLen++;
  56. }
  57. setconfCmd[setconfCmdLen] = '\0';
  58. yield();
  59. if (setconfCmdLen == len) cmdNoPayload = true;
  60. #ifdef DEBUG_VERBOSE
  61. Serial.print("setconfCmd: '");
  62. Serial.print(setconfCmd);
  63. Serial.print("'");
  64. #endif
  65. if ( cmdNoPayload ) {
  66. #ifdef DEBUG_VERBOSE
  67. Serial.println(", no payload, displaying current value");
  68. #endif
  69. //getConfig(setconfCmd);
  70. cmdPrintHelp();
  71. }
  72. else {
  73. char setconfPayload[62];
  74. #ifdef DEBUG_VERBOSE
  75. Serial.println();
  76. #endif
  77. int setconfPayloadLen = 0;
  78. for (int i = 0; i < len; i++) {
  79. char c = buf[i + setconfCmdLen + 1];
  80. if (c == 0 || c == 10 || c == 13) break; // if \0, LF (10) or CR (13) command parameter is finished
  81. setconfPayload[i] = c;
  82. setconfPayloadLen++;
  83. }
  84. setconfPayload[setconfPayloadLen] = '\0';
  85. #ifdef DEBUG_VERBOSE
  86. Serial.print("setconfPayload: '");
  87. Serial.print(setconfPayload);
  88. Serial.println("'");
  89. #endif
  90. char bufg[100];
  91. sprintf(bufg, "set '%s'->'%s'", setconfCmd, setconfPayload);
  92. sendLog(bufg);
  93. setConfig(setconfCmd, setconfPayload);
  94. }
  95. }
  96. else if (strncmp(cmdPayload_lower, "get ", 4) == 0) {
  97. char buf[81];
  98. char setconfCmd[16];
  99. uint8_t len = strlen(cmdPayload) - 4;
  100. for (int i = 0; i < len; i++) {
  101. if (i < 81) buf[i] = cmdPayload[i + 4];
  102. }
  103. if (len <= (sizeof(buf)-1)) buf[len] = '\0';
  104. else buf[(sizeof(buf)-1)] = '\0';
  105. #ifdef DEBUG_VERBOSE
  106. Serial.print("Buf: ");
  107. Serial.println(buf);
  108. #endif
  109. uint8_t setconfCmdLen = 0;
  110. for (int i = 0; i < len; i++) {
  111. //if (buf[i] == 32 || buf[i] == '\0') break; // if SPACE command name is finished, if \0 command parameter is missing
  112. if (buf[i] == 32 || buf[i] == 61 || buf[i] == 58) break; // if SPACE(32) or =(61) or :(58) command name is finished
  113. else if (buf[i] == 0 || buf[i] == 10 || buf[i] == 13) { // if \0, LF (10) or CR (13) command parameter is missing
  114. break;
  115. }
  116. setconfCmd[i] = buf[i];
  117. setconfCmdLen++;
  118. }
  119. setconfCmd[setconfCmdLen] = '\0';
  120. yield();
  121. #ifdef DEBUG_VERBOSE
  122. Serial.print("setconfCmd: '");
  123. Serial.print(setconfCmd);
  124. Serial.println("'");
  125. #endif
  126. char bufg[50];
  127. sprintf(bufg, "get '%s'", setconfCmd);
  128. sendLog(bufg);
  129. getConfig(setconfCmd);
  130. }
  131. /*else if (strncmp(cmdPayload, "enc ", 4) == 0) {
  132. char buf[81];
  133. char setconfCmd[16];
  134. uint8_t len = strlen(cmdPayload) - 4;
  135. for (int i = 0; i < len; i++) {
  136. if (i < 81) buf[i] = cmdPayload[i + 4];
  137. }
  138. if (len <= (sizeof(buf)-1)) buf[len] = '\0';
  139. else buf[(sizeof(buf)-1)] = '\0';
  140. #ifdef DEBUG_VERBOSE
  141. Serial.print("Buf: ");
  142. Serial.println(buf);
  143. #endif
  144. uint8_t setconfCmdLen = 0;
  145. for (int i = 0; i < len; i++) {
  146. //if (buf[i] == 32 || buf[i] == '\0') break; // if SPACE command name is finished, if \0 command parameter is missing
  147. if (buf[i] == 32 || buf[i] == 61 || buf[i] == 58) break; // if SPACE(32) or =(61) or :(58) command name is finished
  148. else if (buf[i] == 0 || buf[i] == 10 || buf[i] == 13) { // if \0, LF (10) or CR (13) command parameter is missing
  149. break;
  150. }
  151. setconfCmd[i] = buf[i];
  152. setconfCmdLen++;
  153. }
  154. setconfCmd[setconfCmdLen] = '\0';
  155. yield();
  156. #ifdef DEBUG_VERBOSE
  157. Serial.print("setconfCmd: '");
  158. Serial.print(setconfCmd);
  159. Serial.println("'");
  160. #endif
  161. char bufg[50];
  162. sprintf(bufg, "enc '%s'", setconfCmd);
  163. sendLog(bufg);
  164. sprintf(bufg, "%s", XORENC(setconfCmd, encKey));
  165. sendLog(bufg);
  166. //getConfig(setconfCmd);
  167. }*/
  168. else if (strncmp(cmdPayload_lower, "restart", 7) == 0) {
  169. sendLog(F("restarting..."));
  170. restart();
  171. }
  172. else if (strncmp(cmdPayload_lower, "sysinfo", 7) == 0) {
  173. logSysdata();
  174. }
  175. else if (strncmp(cmdPayload_lower, "debugmode 1", 11) == 0) {
  176. sendLog(F("debugmode on"));
  177. sysInfoEverySecond = true;
  178. }
  179. else if (strncmp(cmdPayload_lower, "debugmode 0", 11) == 0) {
  180. sendLog(F("debugmode off"));
  181. sysInfoEverySecond = false;
  182. }
  183. #ifdef ENABLE_FEATURE_NTP_TIME
  184. else if (strncmp(cmdPayload_lower, "date", 7) == 0) {
  185. printDate();
  186. }
  187. #endif
  188. #ifdef ENABLE_FEATURE_NTP_TIME
  189. else if (strncmp(cmdPayload_lower, "syncclock", 7) == 0) {
  190. syncClock(true);
  191. }
  192. #endif
  193. else if (strncmp(cmdPayload_lower, "clearcreds", 10) == 0) {
  194. confClearCredentials();
  195. }
  196. else if (strncmp(cmdPayload_lower, "clearwifi", 10) == 0) {
  197. confClearWiFiCredentials();
  198. }
  199. else if (strncmp(cmdPayload_lower, "saveconf", 8) == 0) {
  200. saveConfig_all();
  201. }
  202. else if (strncmp(cmdPayload_lower, "savevalues", 10) == 0) {
  203. saveValues();
  204. }
  205. else if (strncmp(cmdPayload_lower, "encrypt", 10) == 0) {
  206. confEncrypt();
  207. }
  208. else if (strncmp(cmdPayload_lower, "decrypt", 10) == 0) {
  209. confDecrypt();
  210. }
  211. else if (strncmp(cmdPayload_lower, "ls", 2) == 0) {
  212. SPIFFS_listFiles();
  213. }
  214. else if (strncmp(cmdPayload_lower, "ipcfg", 5) == 0) {
  215. printIpcfg();
  216. }
  217. else if (strncmp(cmdPayload_lower, "help", 4) == 0) {
  218. cmdPrintHelp();
  219. }
  220. else if (strncmp(cmdPayload_lower, "getconf", 7) == 0) {
  221. sendLog(F("CONF: listing all conf parameters:"));
  222. getConfig((char*)"confDevWiFi");
  223. getConfig((char*)"confWeb");
  224. getConfig((char*)"confMqtt");
  225. getConfig((char*)"confBas");
  226. getConfig((char*)"confAdv");
  227. getConfig((char*)"confAdd");
  228. getConfig((char*)"confTime");
  229. getConfig((char*)"confLog");
  230. sendLog(F("------"));
  231. }
  232. else if (strncmp(cmdPayload_lower, "delconf", 7) == 0) {
  233. sendLog(F("deleting configuration..."));
  234. deleteConfig();
  235. }
  236. cmdInQueue = false;
  237. }
  238. }
  239. void cmdPrintHelp() {
  240. sendLog(F("valid commands:"));
  241. sendLog(F(" sysinfo show uptime, heap usage..."));
  242. sendLog(F(" date show current date/time"));
  243. sendLog(F(" syncclock perform NTP time sync now"));
  244. sendLog(F(" debugmode [0/1]"));
  245. sendLog(F(" loadconf load saved configuration"));
  246. sendLog(F(" getconf list all conf parameters"));
  247. sendLog(F(" get [PARAM] show conf value"));
  248. sendLog(F(" get values list current saved values"));
  249. sendLog(F(" set [PARAM] [VALUE] (1 blank after PARAM to clear)"));
  250. sendLog(F(" saveconf save all configurations"));
  251. sendLog(F(" savevalues save set values"));
  252. sendLog(F(" clearcreds delete all passwords"));
  253. sendLog(F(" clearwifi delete WiFi configurations"));
  254. sendLog(F(" encrypt switch on encrypted stored passwords"));
  255. sendLog(F(" decrypt switch off encrypted stored passwords"));
  256. sendLog(F(" WARNING: deletes all currently saved credentials"));
  257. sendLog(F(" ls list files on SPIFFS"));
  258. sendLog(F(" delconf delete ALL configuration"));
  259. sendLog(F(" restart"));
  260. }