123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303 |
- #define SER_INPUT_SIZE 70
- char serBuffer[SER_INPUT_SIZE + 1];
- int serBufferCount;
- void serialEvent() {
- char ch = Serial.read();
- serBuffer[serBufferCount] = ch;
- serBufferCount++;
- if (ch == 13 || ch == 10) { // ASCII code 13 = "CR", 10 = "LF"
- serBuffer[serBufferCount - 1] = '\0';
- #ifdef DEBUG_VERBOSE
- Serial.print("serial cmd: '");
- Serial.print(serBuffer);
- Serial.println("'");
- #endif
- strlcpy(cmdPayload, serBuffer, sizeof(cmdPayload));
- cmdInQueue = true;
- evalCmd();
- serBufferCount = 0;
- }
- }
- void evalCmd() {
- if (cmdInQueue) {
- //Serial.print("cmdPayload: ");
- //Serial.println(cmdPayload);
- char cmdPayload_lower[sizeof(cmdPayload)];
- strlcpy(cmdPayload_lower, cmdPayload, sizeof(cmdPayload_lower));
- strlwr(cmdPayload_lower);
- if (strncmp(cmdPayload_lower, "loadconf", 8) == 0) {
- sendLog(F("loading config..."));
- loadConf_all();
- }
- else if (strncmp(cmdPayload_lower, "set ", 4) == 0) {
- char buf[81];
- char setconfCmd[16];
- uint8_t len = strlen(cmdPayload) - 4;
- for (unsigned char i = 0; i < len; i++) {
- if (i < (sizeof(buf)-1)) buf[i] = cmdPayload[i + 4];
- }
- if (len <= (sizeof(buf)-1)) buf[len] = '\0';
- else buf[(sizeof(buf)-1)] = '\0';
- #ifdef DEBUG_VERBOSE
- Serial.print("Buf: ");
- Serial.println(buf);
- #endif
- bool cmdNoPayload = false;
- uint8_t setconfCmdLen = 0;
- for (unsigned char i = 0; i < len; i++) {
- //if (buf[i] == 32 || buf[i] == '\0') break; // if SPACE command name is finished, if \0 command parameter is missing
- if (buf[i] == 32 || buf[i] == 61 || buf[i] == 58) break; // if SPACE(32) or =(61) or :(58) command name is finished
- else if (buf[i] == 0 || buf[i] == 10 || buf[i] == 13) { // if \0, LF (10) or CR (13) command parameter is missing
- cmdNoPayload = true;
- break;
- }
- setconfCmd[i] = buf[i];
- setconfCmdLen++;
- }
- setconfCmd[setconfCmdLen] = '\0';
- yield();
- if (setconfCmdLen == len) cmdNoPayload = true;
- #ifdef DEBUG_VERBOSE
- Serial.print("setconfCmd: '");
- Serial.print(setconfCmd);
- Serial.print("'");
- #endif
- if ( cmdNoPayload ) {
- #ifdef DEBUG_VERBOSE
- Serial.println(", no payload, displaying current value");
- #endif
- //getConfig(setconfCmd);
- cmdPrintHelp();
-
- }
- else {
- char setconfPayload[62];
- #ifdef DEBUG_VERBOSE
- Serial.println();
- #endif
- int setconfPayloadLen = 0;
- for (int i = 0; i < len; i++) {
- char c = buf[i + setconfCmdLen + 1];
- if (c == 0 || c == 10 || c == 13) break; // if \0, LF (10) or CR (13) command parameter is finished
- setconfPayload[i] = c;
- setconfPayloadLen++;
- }
- setconfPayload[setconfPayloadLen] = '\0';
- #ifdef DEBUG_VERBOSE
- Serial.print("setconfPayload: '");
- Serial.print(setconfPayload);
- Serial.println("'");
- #endif
- char bufg[100];
- sprintf(bufg, "set '%s'->'%s'", setconfCmd, setconfPayload);
- sendLog(bufg);
- setConfig(setconfCmd, setconfPayload);
- }
- }
- else if (strncmp(cmdPayload_lower, "get ", 4) == 0) {
- char buf[81];
- char setconfCmd[16];
- uint8_t len = strlen(cmdPayload) - 4;
- for (int i = 0; i < len; i++) {
- if (i < 81) buf[i] = cmdPayload[i + 4];
- }
- if (len <= (sizeof(buf)-1)) buf[len] = '\0';
- else buf[(sizeof(buf)-1)] = '\0';
- #ifdef DEBUG_VERBOSE
- Serial.print("Buf: ");
- Serial.println(buf);
- #endif
-
- uint8_t setconfCmdLen = 0;
- for (int i = 0; i < len; i++) {
- //if (buf[i] == 32 || buf[i] == '\0') break; // if SPACE command name is finished, if \0 command parameter is missing
- if (buf[i] == 32 || buf[i] == 61 || buf[i] == 58) break; // if SPACE(32) or =(61) or :(58) command name is finished
- else if (buf[i] == 0 || buf[i] == 10 || buf[i] == 13) { // if \0, LF (10) or CR (13) command parameter is missing
- break;
- }
- setconfCmd[i] = buf[i];
- setconfCmdLen++;
- }
- setconfCmd[setconfCmdLen] = '\0';
- yield();
- #ifdef DEBUG_VERBOSE
- Serial.print("setconfCmd: '");
- Serial.print(setconfCmd);
- Serial.println("'");
- #endif
- char bufg[50];
- sprintf(bufg, "get '%s'", setconfCmd);
- sendLog(bufg);
- getConfig(setconfCmd);
- }
- /*else if (strncmp(cmdPayload, "enc ", 4) == 0) {
- char buf[81];
- char setconfCmd[16];
- uint8_t len = strlen(cmdPayload) - 4;
- for (int i = 0; i < len; i++) {
- if (i < 81) buf[i] = cmdPayload[i + 4];
- }
- if (len <= (sizeof(buf)-1)) buf[len] = '\0';
- else buf[(sizeof(buf)-1)] = '\0';
- #ifdef DEBUG_VERBOSE
- Serial.print("Buf: ");
- Serial.println(buf);
- #endif
-
- uint8_t setconfCmdLen = 0;
- for (int i = 0; i < len; i++) {
- //if (buf[i] == 32 || buf[i] == '\0') break; // if SPACE command name is finished, if \0 command parameter is missing
- if (buf[i] == 32 || buf[i] == 61 || buf[i] == 58) break; // if SPACE(32) or =(61) or :(58) command name is finished
- else if (buf[i] == 0 || buf[i] == 10 || buf[i] == 13) { // if \0, LF (10) or CR (13) command parameter is missing
- break;
- }
- setconfCmd[i] = buf[i];
- setconfCmdLen++;
- }
- setconfCmd[setconfCmdLen] = '\0';
- yield();
- #ifdef DEBUG_VERBOSE
- Serial.print("setconfCmd: '");
- Serial.print(setconfCmd);
- Serial.println("'");
- #endif
- char bufg[50];
- sprintf(bufg, "enc '%s'", setconfCmd);
- sendLog(bufg);
- sprintf(bufg, "%s", XORENC(setconfCmd, encKey));
- sendLog(bufg);
- //getConfig(setconfCmd);
- }*/
- else if (strncmp(cmdPayload_lower, "restart", 7) == 0) {
- sendLog(F("restarting..."));
- restart();
- }
- else if (strncmp(cmdPayload_lower, "sysinfo", 7) == 0) {
- logSysdata();
- }
- else if (strncmp(cmdPayload_lower, "debugmode 1", 11) == 0) {
- sendLog(F("debugmode on"));
- sysInfoEverySecond = true;
- }
- else if (strncmp(cmdPayload_lower, "debugmode 0", 11) == 0) {
- sendLog(F("debugmode off"));
- sysInfoEverySecond = false;
- }
- #ifdef ENABLE_FEATURE_NTP_TIME
- else if (strncmp(cmdPayload_lower, "date", 7) == 0) {
- printDate();
- }
- #endif
- #ifdef ENABLE_FEATURE_NTP_TIME
- else if (strncmp(cmdPayload_lower, "syncclock", 7) == 0) {
- syncClock(true);
- }
- #endif
- else if (strncmp(cmdPayload_lower, "clearcreds", 10) == 0) {
- confClearCredentials();
- }
- else if (strncmp(cmdPayload_lower, "clearwifi", 10) == 0) {
- confClearWiFiCredentials();
- }
- else if (strncmp(cmdPayload_lower, "saveconf", 8) == 0) {
- saveConfig_all();
- }
-
- else if (strncmp(cmdPayload_lower, "savevalues", 10) == 0) {
- saveValues();
- }
- else if (strncmp(cmdPayload_lower, "encrypt", 10) == 0) {
- confEncrypt();
- }
- else if (strncmp(cmdPayload_lower, "decrypt", 10) == 0) {
- confDecrypt();
- }
- else if (strncmp(cmdPayload_lower, "ls", 2) == 0) {
- SPIFFS_listFiles();
- }
- else if (strncmp(cmdPayload_lower, "ipcfg", 5) == 0) {
- printIpcfg();
- }
-
- else if (strncmp(cmdPayload_lower, "help", 4) == 0) {
- cmdPrintHelp();
- }
- else if (strncmp(cmdPayload_lower, "getconf", 7) == 0) {
- sendLog(F("CONF: listing all conf parameters:"));
- getConfig((char*)"confDevWiFi");
- getConfig((char*)"confWeb");
- getConfig((char*)"confMqtt");
- getConfig((char*)"confBas");
- getConfig((char*)"confAdv");
- getConfig((char*)"confAdd");
- getConfig((char*)"confTime");
- getConfig((char*)"confLog");
- sendLog(F("------"));
- }
- else if (strncmp(cmdPayload_lower, "delconf", 7) == 0) {
- sendLog(F("deleting configuration..."));
- deleteConfig();
- }
-
- cmdInQueue = false;
- }
- }
- void cmdPrintHelp() {
- sendLog(F("valid commands:"));
- sendLog(F(" sysinfo show uptime, heap usage..."));
- sendLog(F(" date show current date/time"));
- sendLog(F(" syncclock perform NTP time sync now"));
- sendLog(F(" debugmode [0/1]"));
- sendLog(F(" loadconf load saved configuration"));
- sendLog(F(" getconf list all conf parameters"));
- sendLog(F(" get [PARAM] show conf value"));
- sendLog(F(" get values list current saved values"));
- sendLog(F(" set [PARAM] [VALUE] (1 blank after PARAM to clear)"));
- sendLog(F(" saveconf save all configurations"));
- sendLog(F(" savevalues save set values"));
- sendLog(F(" clearcreds delete all passwords"));
- sendLog(F(" clearwifi delete WiFi configurations"));
- sendLog(F(" encrypt switch on encrypted stored passwords"));
- sendLog(F(" decrypt switch off encrypted stored passwords"));
- sendLog(F(" WARNING: deletes all currently saved credentials"));
- sendLog(F(" ls list files on SPIFFS"));
- sendLog(F(" delconf delete ALL configuration"));
- sendLog(F(" restart"));
- }
|