diff --git a/.editorconfig b/.editorconfig index 980ef21..5ae0730 100644 --- a/.editorconfig +++ b/.editorconfig @@ -10,3 +10,7 @@ end_of_line = lf charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true + +# the files need the trailing withe space at the end of = else it does not work +[{platformio.ini,platformio-tools.ini}] +trim_trailing_whitespace = false diff --git a/platformio-tools.ini b/platformio-tools.ini index ca97fc8..e5a268c 100644 --- a/platformio-tools.ini +++ b/platformio-tools.ini @@ -1,6 +1,7 @@ [env] lib_deps= https://github.com/SlimeVR/CmdParser.git + https://github.com/SlimeVR/base64_arduino.git monitor_speed = 115200 framework = arduino build_flags = -O2 -std=gnu++17 diff --git a/platformio.ini b/platformio.ini index dec2c23..6d4d674 100644 --- a/platformio.ini +++ b/platformio.ini @@ -14,6 +14,7 @@ [env] lib_deps= https://github.com/SlimeVR/CmdParser.git + https://github.com/SlimeVR/base64_arduino.git monitor_speed = 115200 monitor_echo = yes monitor_filters = colorize diff --git a/src/serial/serialcommands.cpp b/src/serial/serialcommands.cpp index 8030b93..cb61fae 100644 --- a/src/serial/serialcommands.cpp +++ b/src/serial/serialcommands.cpp @@ -24,7 +24,7 @@ #include "serialcommands.h" #include "logging/Logger.h" #include -#include +#include "base64.hpp" #include "GlobalVars.h" #include "batterymonitor.h" #include "utils.h" @@ -40,14 +40,42 @@ namespace SerialCommands { CmdParser cmdParser; CmdBuffer<256> cmdBuffer; - void cmdSet(CmdParser * parser) { - if(parser->getParamCount() != 1) { + + bool lengthCheck (const char* const text, unsigned int length, const char* const cmd, const char* const name) + { + size_t l = text != nullptr ? strlen(text) : 0; + if ((l > length)) { + logger.error("%s ERROR: %s is longer than %d bytes / Characters", cmd, name, length); + return false; + } + return true; + } + + unsigned int decode_base64_length_null(const char* const b64char, unsigned int* b64ssidlength) + { + if (b64char==NULL) { + return 0; + } + *b64ssidlength = (unsigned int)strlen(b64char); + return decode_base64_length((unsigned char *)b64char, *b64ssidlength); + } + + void cmdSet(CmdParser * parser) { + if(parser->getParamCount() != 1) { if (parser->equalCmdParam(1, "WIFI")) { if(parser->getParamCount() < 3) { logger.error("CMD SET WIFI ERROR: Too few arguments"); logger.info("Syntax: SET WIFI \"\" \"\""); } else { - WiFiNetwork::setWiFiCredentials(parser->getCmdParam(2), parser->getCmdParam(3)); + const char *sc_ssid = parser->getCmdParam(2); + const char *sc_pw = parser->getCmdParam(3); + + if (!lengthCheck(sc_ssid, 32, "CMD SET WIFI", "SSID") && + !lengthCheck(sc_pw, 64, "CMD SET WIFI", "Password")) { + return; + } + + WiFiNetwork::setWiFiCredentials(sc_ssid, sc_pw); logger.info("CMD SET WIFI OK: New wifi credentials set, reconnecting"); } } else if (parser->equalCmdParam(1, "BWIFI")) { @@ -55,22 +83,44 @@ namespace SerialCommands { logger.error("CMD SET BWIFI ERROR: Too few arguments"); logger.info("Syntax: SET BWIFI "); } else { - char ssid[33]; - char pass[65]; const char * b64ssid = parser->getCmdParam(2); const char * b64pass = parser->getCmdParam(3); - base64_decode_chars(b64ssid, strlen_P(b64ssid), ssid); - base64_decode_chars(b64pass, strlen_P(b64pass), pass); - WiFiNetwork::setWiFiCredentials(ssid, pass); + unsigned int b64ssidlength = 0; + unsigned int b64passlength = 0; + unsigned int ssidlength = decode_base64_length_null(b64ssid, &b64ssidlength); + unsigned int passlength = decode_base64_length_null(b64pass, &b64passlength); + + // alloc the strings and set them to 0 (null terminating) + char ssid[ssidlength+1]; + memset(ssid, 0, ssidlength+1); + char pass[passlength+1]; + memset(pass, 0, passlength+1); + // make a pointer to pass + char *ppass = pass; + decode_base64((const unsigned char *)b64ssid, b64ssidlength, (unsigned char*)ssid); + if (!lengthCheck(ssid, 32, "CMD SET BWIFI", "SSID")) { + return; + } + + if ((b64pass!=NULL) && (b64passlength > 0)) { + decode_base64((const unsigned char *)b64pass, b64passlength, (unsigned char*)pass); + if (!lengthCheck(pass, 64, "CMD SET BWIFI", "Password")) { + return; + } + } else { + // set the pointer for pass to null for no password + ppass = NULL; + } + WiFiNetwork::setWiFiCredentials(ssid, ppass); logger.info("CMD SET BWIFI OK: New wifi credentials set, reconnecting"); } } else { - logger.error("CMD SET ERROR: Unrecognized variable to set"); + logger.error("CMD SET ERROR: Unrecognized variable to set"); } - } else { - logger.error("CMD SET ERROR: No variable to set"); - } - } + } else { + logger.error("CMD SET ERROR: No variable to set"); + } + } void printState() { logger.info( @@ -178,6 +228,9 @@ namespace SerialCommands { // Scan would fail if connecting, stop connecting before scan if (WiFi.status() != WL_CONNECTED) WiFi.disconnect(); + if (WiFiNetwork::isProvisioning()) { + WiFiNetwork::stopProvisioning(); + } WiFi.scanNetworks(); diff --git a/src/serial/serialcommands.h b/src/serial/serialcommands.h index 21cbb0c..a07f06c 100644 --- a/src/serial/serialcommands.h +++ b/src/serial/serialcommands.h @@ -30,4 +30,4 @@ namespace SerialCommands { void printState(); } -#endif // SLIMEVR_SERIALCOMMANDS_H_ \ No newline at end of file +#endif // SLIMEVR_SERIALCOMMANDS_H_