|
|
|
|
@@ -24,7 +24,7 @@
|
|
|
|
|
#include "serialcommands.h"
|
|
|
|
|
#include "logging/Logger.h"
|
|
|
|
|
#include <CmdCallback.hpp>
|
|
|
|
|
#include <libb64/cdecode.h>
|
|
|
|
|
#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 \"<SSID>\" \"<PASSWORD>\"");
|
|
|
|
|
} 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 <B64SSID> <B64PASSWORD>");
|
|
|
|
|
} 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();
|
|
|
|
|
|
|
|
|
|
|