251 lines
5.7 KiB
C++
251 lines
5.7 KiB
C++
#include <console.h>
|
|
#include <log.h>
|
|
#include <Esp.h>
|
|
#include "base.h"
|
|
#include "mqtt.h"
|
|
#include "wifi.h"
|
|
#include "config.h"
|
|
|
|
char consoleBuffer[64] = "";
|
|
|
|
char *consoleBufferW = consoleBuffer;
|
|
|
|
void _debug();
|
|
|
|
void _reboot();
|
|
|
|
void _info();
|
|
|
|
void _wifi();
|
|
|
|
void _mqtt();
|
|
|
|
void _setConfigString(const char *name, bool allowEmpty);
|
|
|
|
const char *getFlashChipMode();
|
|
|
|
void consoleSetup() {
|
|
delay(500);
|
|
Serial.begin(115200);
|
|
Serial.print("\n\n\n");
|
|
info("Startup...");
|
|
}
|
|
|
|
void consoleLoop() {
|
|
uint8_t i = 0;
|
|
while (Serial.available() > 0 && i++ < 100) {
|
|
const char symbol = static_cast<char>(Serial.read());
|
|
if (symbol == '\n' || symbol == '\r') {
|
|
if (*consoleBuffer != 0) {
|
|
Serial.print('\n');
|
|
info("CONSOLE > %s", consoleBuffer);
|
|
consoleHandle(consoleBuffer);
|
|
consoleBufferW = consoleBuffer;
|
|
*consoleBufferW = 0;
|
|
}
|
|
} else if (symbol == '\b') {
|
|
if (consoleBufferW > consoleBuffer) {
|
|
consoleBufferW--;
|
|
*consoleBufferW = 0;
|
|
Serial.print("\b \b");
|
|
}
|
|
} else {
|
|
if (consoleBufferW < consoleBuffer + sizeof consoleBuffer - 1) {
|
|
*consoleBufferW = symbol;
|
|
consoleBufferW++;
|
|
*consoleBufferW = 0;
|
|
Serial.print("\r");
|
|
Serial.print(consoleBuffer);
|
|
} else {
|
|
Serial.print("\nCONSOLE BUFFER OVERFLOW\n");
|
|
Serial.print(consoleBuffer);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void consoleHandle(char *cmd) {
|
|
char *first = strtok(cmd, " ");
|
|
if (first == nullptr) {
|
|
consolePrintUsage();
|
|
return;
|
|
}
|
|
if (strcmp(first, "help") == 0) {
|
|
consolePrintUsage();
|
|
} else if (strcmp(first, "wifi") == 0) {
|
|
_wifi();
|
|
} else if (strcmp(first, "mqtt") == 0) {
|
|
_mqtt();
|
|
} else if (strcmp(first, "info") == 0) {
|
|
_info();
|
|
} else if (strcmp(first, "reboot") == 0) {
|
|
_reboot();
|
|
} else if (strcmp(first, "debug") == 0) {
|
|
_debug();
|
|
} else if (strcmp(first, "config_reset") == 0) {
|
|
configReset();
|
|
} else if (!patrix_command((char *) cmd)) {
|
|
info("Unknown command: %s", cmd);
|
|
}
|
|
}
|
|
|
|
void consolePrintUsage() {
|
|
info(R"(USAGE:
|
|
help
|
|
info
|
|
debug
|
|
reboot
|
|
config_reset
|
|
wifi reconnect
|
|
wifi ssid <SSID>
|
|
wifi pkey <PKEY>
|
|
mqtt reconnect
|
|
mqtt host <HOST>
|
|
)");
|
|
}
|
|
|
|
void _debug() {
|
|
setDebugEnabled(!isDebugEnabled());
|
|
info("DEBUG: %s", isDebugEnabled() ? "ON" : "OFF");
|
|
}
|
|
|
|
void _reboot() {
|
|
info("Rebooting...");
|
|
delay(500);
|
|
yield();
|
|
mqttDisconnect();
|
|
ESP.restart();
|
|
}
|
|
|
|
void _mqtt() {
|
|
char *sub = strtok(nullptr, " ");
|
|
if (sub == nullptr) {
|
|
consolePrintUsage();
|
|
return;
|
|
}
|
|
if (strcmp(sub, "reconnect") == 0) {
|
|
info("Reconnecting MQTT...");
|
|
mqttDisconnect();
|
|
} else if (strcmp(sub, "host") == 0) {
|
|
_setConfigString("MQTT_HOST", false);
|
|
}
|
|
}
|
|
|
|
void _wifi() {
|
|
char *sub = strtok(nullptr, " ");
|
|
if (sub == nullptr) {
|
|
consolePrintUsage();
|
|
return;
|
|
}
|
|
if (strcmp(sub, "reconnect") == 0) {
|
|
info("Reconnecting WiFi...");
|
|
wifiConnect();
|
|
} else if (strcmp(sub, "ssid") == 0) {
|
|
_setConfigString("WIFI_SSID", false);
|
|
} else if (strcmp(sub, "pkey") == 0) {
|
|
_setConfigString("WIFI_PKEY", false);
|
|
}
|
|
}
|
|
|
|
void _setConfigString(const char *name, bool allowEmpty) {
|
|
char *value = strtok(nullptr, "");
|
|
if (value == nullptr) {
|
|
consolePrintUsage();
|
|
return;
|
|
}
|
|
if (!allowEmpty && strcmp(value, "") == 0) {
|
|
error(R"(Value for "%s" cannot be empty!")", name);
|
|
return;
|
|
}
|
|
configPutString(name, value);
|
|
if (strcmp(name, "WIFI_PKEY") == 0) {
|
|
info(R"(Set "%s" to [***])", name);
|
|
} else {
|
|
info(R"(Set "%s" to "%s")", name, value);
|
|
}
|
|
}
|
|
|
|
void _info() {
|
|
uint64_t millis = getUptimeMillis();
|
|
uint64_t seconds = millis / 1000;
|
|
uint32_t minutes = seconds / 60;
|
|
uint32_t hours = minutes / 60;
|
|
uint16_t days = hours / 24;
|
|
char datetime[26];
|
|
getDateTime(datetime, sizeof datetime);
|
|
|
|
info("WiFi:");
|
|
info(" hostname: %s", HOSTNAME);
|
|
info(" mac: %s", WiFi.macAddress().c_str());
|
|
info(" ip: %s", WiFi.localIP().toString().c_str());
|
|
info(" gateway: %s", WiFi.gatewayIP().toString().c_str());
|
|
info(" ssid: %s", WiFi.SSID().c_str());
|
|
info(" bssid: %s", WiFi.BSSIDstr().c_str());
|
|
info(" rssi: %d", WiFi.RSSI());
|
|
|
|
configPrint();
|
|
|
|
info("Time:");
|
|
info(" uptime: %dd %2dh %2dm %2ds", days, hours % 24, minutes % 60, seconds % 60);
|
|
info(" sys: %s", datetime);
|
|
|
|
info("Chip:");
|
|
#ifdef ESP32
|
|
info(" model: %s", ESP.getChipModel());
|
|
info(" cores: %d", ESP.getChipCores());
|
|
#endif
|
|
info(" freq: %d", ESP.getCpuFreqMHz());
|
|
|
|
info("Flash:");
|
|
info(" mode: %s", getFlashChipMode());
|
|
info(" size: %d", ESP.getFlashChipSize());
|
|
info(" speed: %d", ESP.getFlashChipSpeed());
|
|
|
|
info("Heap:");
|
|
info(" free: %d", ESP.getFreeHeap());
|
|
#ifdef ESP32
|
|
info(" size: %d", ESP.getHeapSize());
|
|
info(" minFree: %d", ESP.getMinFreeHeap());
|
|
info(" maxAlloc: %d", ESP.getMaxAllocHeap());
|
|
#endif
|
|
|
|
#ifdef ESP32
|
|
info("PS RAM:");
|
|
info(" free: %d", ESP.getFreePsram());
|
|
info(" size: %d", ESP.getPsramSize());
|
|
info(" minFree: %d", ESP.getMinFreePsram());
|
|
info(" maxAlloc: %d", ESP.getMaxAllocPsram());
|
|
#endif
|
|
|
|
info("SDK:");
|
|
info(" free: %d", ESP.getFreeSketchSpace());
|
|
info(" size: %d", ESP.getSketchSize());
|
|
info(" MD5: %s", ESP.getSketchMD5().c_str());
|
|
|
|
info("SDK:");
|
|
info(" version: %s", ESP.getSdkVersion());
|
|
}
|
|
|
|
const char *getFlashChipMode() {
|
|
switch (ESP.getFlashChipMode()) {
|
|
case FM_QIO:
|
|
return "QIO";
|
|
case FM_QOUT:
|
|
return "QOUT";
|
|
case FM_DIO:
|
|
return "DIO";
|
|
case FM_DOUT:
|
|
return "DOUT";
|
|
#ifdef ESP32
|
|
case FM_FAST_READ:
|
|
return "FAST_READ";
|
|
case FM_SLOW_READ:
|
|
return "SLOW_READ";
|
|
#endif
|
|
case FM_UNKNOWN:
|
|
return "UNKNOWN";
|
|
default:
|
|
return "[???]";
|
|
}
|
|
}
|