Fix serial wifi and bwifi. Crash bwifi when no ... (#298)

Co-authored-by: DevMiner <tobigames200@gmail.com>
This commit is contained in:
unlogisch04
2023-10-13 14:54:50 +02:00
committed by GitHub
parent 9968f152fc
commit 26f53ae5e5
5 changed files with 74 additions and 15 deletions

View File

@@ -10,3 +10,7 @@ end_of_line = lf
charset = utf-8 charset = utf-8
trim_trailing_whitespace = true trim_trailing_whitespace = true
insert_final_newline = 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

View File

@@ -1,6 +1,7 @@
[env] [env]
lib_deps= lib_deps=
https://github.com/SlimeVR/CmdParser.git https://github.com/SlimeVR/CmdParser.git
https://github.com/SlimeVR/base64_arduino.git
monitor_speed = 115200 monitor_speed = 115200
framework = arduino framework = arduino
build_flags = -O2 -std=gnu++17 build_flags = -O2 -std=gnu++17

View File

@@ -14,6 +14,7 @@
[env] [env]
lib_deps= lib_deps=
https://github.com/SlimeVR/CmdParser.git https://github.com/SlimeVR/CmdParser.git
https://github.com/SlimeVR/base64_arduino.git
monitor_speed = 115200 monitor_speed = 115200
monitor_echo = yes monitor_echo = yes
monitor_filters = colorize monitor_filters = colorize

View File

@@ -24,7 +24,7 @@
#include "serialcommands.h" #include "serialcommands.h"
#include "logging/Logger.h" #include "logging/Logger.h"
#include <CmdCallback.hpp> #include <CmdCallback.hpp>
#include <libb64/cdecode.h> #include "base64.hpp"
#include "GlobalVars.h" #include "GlobalVars.h"
#include "batterymonitor.h" #include "batterymonitor.h"
#include "utils.h" #include "utils.h"
@@ -40,14 +40,42 @@ namespace SerialCommands {
CmdParser cmdParser; CmdParser cmdParser;
CmdBuffer<256> cmdBuffer; 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->equalCmdParam(1, "WIFI")) {
if(parser->getParamCount() < 3) { if(parser->getParamCount() < 3) {
logger.error("CMD SET WIFI ERROR: Too few arguments"); logger.error("CMD SET WIFI ERROR: Too few arguments");
logger.info("Syntax: SET WIFI \"<SSID>\" \"<PASSWORD>\""); logger.info("Syntax: SET WIFI \"<SSID>\" \"<PASSWORD>\"");
} else { } 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"); logger.info("CMD SET WIFI OK: New wifi credentials set, reconnecting");
} }
} else if (parser->equalCmdParam(1, "BWIFI")) { } else if (parser->equalCmdParam(1, "BWIFI")) {
@@ -55,22 +83,44 @@ namespace SerialCommands {
logger.error("CMD SET BWIFI ERROR: Too few arguments"); logger.error("CMD SET BWIFI ERROR: Too few arguments");
logger.info("Syntax: SET BWIFI <B64SSID> <B64PASSWORD>"); logger.info("Syntax: SET BWIFI <B64SSID> <B64PASSWORD>");
} else { } else {
char ssid[33];
char pass[65];
const char * b64ssid = parser->getCmdParam(2); const char * b64ssid = parser->getCmdParam(2);
const char * b64pass = parser->getCmdParam(3); const char * b64pass = parser->getCmdParam(3);
base64_decode_chars(b64ssid, strlen_P(b64ssid), ssid); unsigned int b64ssidlength = 0;
base64_decode_chars(b64pass, strlen_P(b64pass), pass); unsigned int b64passlength = 0;
WiFiNetwork::setWiFiCredentials(ssid, pass); 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"); logger.info("CMD SET BWIFI OK: New wifi credentials set, reconnecting");
} }
} else { } else {
logger.error("CMD SET ERROR: Unrecognized variable to set"); logger.error("CMD SET ERROR: Unrecognized variable to set");
} }
} else { } else {
logger.error("CMD SET ERROR: No variable to set"); logger.error("CMD SET ERROR: No variable to set");
} }
} }
void printState() { void printState() {
logger.info( logger.info(
@@ -178,6 +228,9 @@ namespace SerialCommands {
// Scan would fail if connecting, stop connecting before scan // Scan would fail if connecting, stop connecting before scan
if (WiFi.status() != WL_CONNECTED) WiFi.disconnect(); if (WiFi.status() != WL_CONNECTED) WiFi.disconnect();
if (WiFiNetwork::isProvisioning()) {
WiFiNetwork::stopProvisioning();
}
WiFi.scanNetworks(); WiFi.scanNetworks();

View File

@@ -30,4 +30,4 @@ namespace SerialCommands {
void printState(); void printState();
} }
#endif // SLIMEVR_SERIALCOMMANDS_H_ #endif // SLIMEVR_SERIALCOMMANDS_H_