mirror of
https://github.com/HASwitchPlate/openHASP.git
synced 2025-07-24 03:36:38 +00:00
Add consoleStart and consoleStop functionality
This commit is contained in:
parent
d194d7f04f
commit
31389d92dd
@ -14,7 +14,7 @@
|
||||
#define debug_print(io, ...) io->printf(__VA_ARGS__)
|
||||
#define debug_newline(io) io->println()
|
||||
|
||||
bool debugSerialStarted = false;
|
||||
// bool debugSerialStarted = false;
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
@ -23,7 +23,7 @@ bool debugSerialStarted = false;
|
||||
#define debug_print(io, ...) fprintf(stdout, __VA_ARGS__)
|
||||
#define debug_newline(io) fprintf(stdout, "\n")
|
||||
|
||||
bool debugSerialStarted = true;
|
||||
// bool debugSerialStarted = true;
|
||||
#endif
|
||||
|
||||
bool debugAnsiCodes = true;
|
||||
@ -107,7 +107,7 @@ void debugEverySecond()
|
||||
// printLocalTime();
|
||||
}
|
||||
|
||||
void debugStart()
|
||||
void debugStart(void)
|
||||
{
|
||||
|
||||
#if defined(WINDOWS) || defined(POSIX)
|
||||
@ -115,25 +115,34 @@ void debugStart()
|
||||
debugPrintHaspHeader(NULL);
|
||||
debug_newline();
|
||||
|
||||
LOG_INFO(TAG_DEBG, F("Console started"));
|
||||
LOG_INFO(TAG_DEBG, F("Environment: " PIOENV));
|
||||
LOG_INFO(TAG_DEBG, F("Console started"));
|
||||
|
||||
debug_flush();
|
||||
#else
|
||||
|
||||
#if HASP_USE_CONSOLE > 0
|
||||
consoleSetup();
|
||||
#endif
|
||||
|
||||
if(debugSerialStarted) {
|
||||
debug_flush();
|
||||
#endif
|
||||
|
||||
// Serial.println();
|
||||
// Serial.println(debugHaspHeader());
|
||||
// debug_flush();
|
||||
}
|
||||
/*
|
||||
if(debugSerialStarted) {
|
||||
|
||||
// prepare syslog configuration here (can be anywhere before first call of
|
||||
// log/logf method)
|
||||
// Serial.println();
|
||||
// Serial.println(debugHaspHeader());
|
||||
// debug_flush();
|
||||
}
|
||||
|
||||
// prepare syslog configuration here (can be anywhere before first call of
|
||||
// log/logf method)
|
||||
*/
|
||||
}
|
||||
|
||||
void debugStop()
|
||||
{
|
||||
if(debugSerialStarted) debug_flush();
|
||||
// if(debugSerialStarted) debug_flush();
|
||||
}
|
||||
|
||||
/* ===== Special Event Processors ===== */
|
||||
|
@ -138,6 +138,7 @@ void debugLvglLogEvent(lv_log_level_t level, const char* file, uint32_t line, co
|
||||
IRAM_ATTR void debugLoop(void);
|
||||
void debugEverySecond(void);
|
||||
void debugStart(void);
|
||||
void debugStartSerial(void);
|
||||
void debugStop(void);
|
||||
void debugPrintHaspHeader(Print* output);
|
||||
void debugPrintTag(uint8_t tag, Print* _logOutput);
|
||||
|
@ -84,7 +84,7 @@ WiFiUDP* syslogClient;
|
||||
// char serialInputBuffer[220] = "";
|
||||
// uint16_t historyIndex = sizeof(serialInputBuffer) - 1; // Empty buffer
|
||||
uint16_t debugSerialBaud = SERIAL_SPEED / 10; // Multiplied by 10
|
||||
extern bool debugSerialStarted;
|
||||
// extern bool debugSerialStarted;
|
||||
extern bool debugAnsiCodes;
|
||||
|
||||
extern dispatch_conf_t dispatch_setings;
|
||||
@ -285,19 +285,16 @@ void debugPrintSuffix(uint8_t tag, int level, Print* _logOutput)
|
||||
}
|
||||
}
|
||||
|
||||
// Do NOT call Log function before debugSetup is called
|
||||
void debugSetup(JsonObject settings)
|
||||
// Start Serial Port at correct
|
||||
void debugStartSerial()
|
||||
{
|
||||
Log.begin(LOG_LEVEL_WARNING, true);
|
||||
Log.setPrefix(debugPrintPrefix); // Uncomment to get timestamps as prefix
|
||||
Log.setSuffix(debugPrintSuffix); // Uncomment to get newline as suffix
|
||||
|
||||
uint32_t baudrate = 0;
|
||||
#if HASP_USE_CONFIG > 0
|
||||
baudrate = settings[FPSTR(FP_CONFIG_BAUD)].as<uint32_t>() * 10;
|
||||
#endif
|
||||
if(debugSerialBaud == 0) {
|
||||
baudrate = SERIAL_SPEED;
|
||||
} else {
|
||||
baudrate = debugSerialBaud * 10;
|
||||
}
|
||||
|
||||
if(baudrate == 0) baudrate = SERIAL_SPEED;
|
||||
if(baudrate >= 9600u) { /* the baudrates are stored divided by 10 */
|
||||
|
||||
#if defined(STM32F4xx)
|
||||
@ -309,7 +306,8 @@ void debugSetup(JsonObject settings)
|
||||
Serial.begin(baudrate); /* prepare for possible serial debug */
|
||||
delay(10);
|
||||
Log.registerOutput(0, &Serial, LOG_LEVEL_VERBOSE, true); // LOG_LEVEL_VERBOSE
|
||||
debugSerialStarted = true;
|
||||
|
||||
// debugSerialStarted = true;
|
||||
|
||||
Serial.println();
|
||||
debugPrintHaspHeader(&Serial);
|
||||
@ -317,9 +315,27 @@ void debugSetup(JsonObject settings)
|
||||
|
||||
LOG_INFO(TAG_DEBG, F(D_SERVICE_STARTED " @ %u Bps"), baudrate);
|
||||
LOG_INFO(TAG_DEBG, F("Environment: " PIOENV));
|
||||
|
||||
} else {
|
||||
LOG_WARNING(TAG_DEBG, F(D_SERVICE_DISABLED " (%u Bps)"), baudrate);
|
||||
}
|
||||
}
|
||||
|
||||
// Do NOT call Log function before debugSetup is called
|
||||
void debugSetup(JsonObject settings)
|
||||
{
|
||||
Log.begin(LOG_LEVEL_WARNING, true);
|
||||
Log.setPrefix(debugPrintPrefix); // Uncomment to get timestamps as prefix
|
||||
Log.setSuffix(debugPrintSuffix); // Uncomment to get newline as suffix
|
||||
Log.unregisterOutput(0);
|
||||
Log.unregisterOutput(1);
|
||||
Log.unregisterOutput(3);
|
||||
|
||||
#if HASP_USE_CONFIG > 0
|
||||
debugSerialBaud = settings[FPSTR(FP_CONFIG_BAUD)].as<uint16_t>();
|
||||
#endif
|
||||
}
|
||||
|
||||
IRAM_ATTR void debugLoop(void)
|
||||
{}
|
||||
|
||||
|
@ -57,7 +57,7 @@ void setup()
|
||||
* Read & Apply User Configuration
|
||||
***************************/
|
||||
#if HASP_USE_CONFIG > 0
|
||||
configSetup(); // also runs debugSetup() and debugStart()
|
||||
configSetup(); // also runs debugSetup(), debugStart() and consoleSetup()
|
||||
#endif
|
||||
|
||||
dispatchSetup(); // before hasp and oobe, asap after logging starts
|
||||
@ -98,9 +98,9 @@ void setup()
|
||||
httpSetup();
|
||||
#endif
|
||||
|
||||
#if HASP_USE_CONSOLE > 0
|
||||
consoleSetup();
|
||||
#endif
|
||||
// #if HASP_USE_CONSOLE > 0
|
||||
// consoleSetup(); // the consoleSetup is called in debugSetup
|
||||
// #endif
|
||||
|
||||
#if HASP_USE_TELNET > 0
|
||||
telnetSetup();
|
||||
|
@ -6,6 +6,7 @@
|
||||
#if HASP_USE_CONSOLE > 0
|
||||
|
||||
#include "ConsoleInput.h"
|
||||
#include <StreamUtils.h>
|
||||
|
||||
#include "hasp_debug.h"
|
||||
#include "hasp_config.h"
|
||||
@ -13,26 +14,138 @@
|
||||
|
||||
#include "../../hasp/hasp_dispatch.h"
|
||||
|
||||
uint8_t consoleInputEnabled = true;
|
||||
ConsoleInput debugConsole(&Serial, HASP_CONSOLE_BUFFER);
|
||||
#if HASP_USE_HTTP > 0 || HASP_USE_HTTP_ASYNC > 0
|
||||
extern hasp_http_config_t http_config;
|
||||
#endif
|
||||
|
||||
// Create a new Stream that buffers all writes to serialClient
|
||||
HardwareSerial* bufferedSerialClient = &Serial;
|
||||
|
||||
uint8_t consoleLoginState = CONSOLE_UNAUTHENTICATED;
|
||||
uint16_t serialPort = 0;
|
||||
uint8_t consoleEnabled = true; // Enable serial debug output
|
||||
uint8_t consoleLoginAttempt = 0; // Initial attempt
|
||||
ConsoleInput* console;
|
||||
|
||||
void console_update_prompt()
|
||||
{
|
||||
debugConsole.update();
|
||||
if(console) console->update();
|
||||
bufferedSerialClient->flush();
|
||||
}
|
||||
|
||||
static void console_timeout()
|
||||
{
|
||||
// todo
|
||||
}
|
||||
|
||||
static void console_logoff()
|
||||
{
|
||||
consoleLoginState = CONSOLE_UNAUTHENTICATED;
|
||||
consoleLoginAttempt = 0; // Reset attempt counter
|
||||
}
|
||||
|
||||
static void console_logon()
|
||||
{
|
||||
// bufferedSerialClient->println();
|
||||
// debugPrintHaspHeader(bufferedSerialClient);
|
||||
|
||||
consoleLoginState = CONSOLE_AUTHENTICATED; // User and Pass are correct
|
||||
consoleLoginAttempt = 0; // Reset attempt counter
|
||||
|
||||
LOG_TRACE(TAG_CONS, F(D_TELNET_CLIENT_LOGIN_FROM), "serial");
|
||||
}
|
||||
|
||||
static void console_process_line(const char* input)
|
||||
{
|
||||
switch(consoleLoginState) {
|
||||
case CONSOLE_UNAUTHENTICATED: {
|
||||
char buffer[20];
|
||||
snprintf_P(buffer, sizeof(buffer), PSTR(D_PASSWORD " %c%c%c\n"), 0xFF, 0xFB,
|
||||
0x01); // Hide characters
|
||||
bufferedSerialClient->print(buffer);
|
||||
#if HASP_USE_HTTP > 0 || HASP_USE_HTTP_ASYNC > 0
|
||||
consoleLoginState = strcmp(input, http_config.user) == 0 ? CONSOLE_USERNAME_OK : CONSOLE_USERNAME_NOK;
|
||||
break;
|
||||
}
|
||||
case CONSOLE_USERNAME_OK:
|
||||
case CONSOLE_USERNAME_NOK: {
|
||||
bufferedSerialClient->printf(PSTR("%c%c%c\n"), 0xFF, 0xFC, 0x01); // Show characters
|
||||
if(consoleLoginState == CONSOLE_USERNAME_OK && strcmp(input, http_config.password) == 0) {
|
||||
console_logon();
|
||||
} else {
|
||||
consoleLoginState = CONSOLE_UNAUTHENTICATED;
|
||||
consoleLoginAttempt++; // Subsequent attempt
|
||||
bufferedSerialClient->println(F(D_NETWORK_CONNECTION_UNAUTHORIZED "\r\n"));
|
||||
LOG_WARNING(TAG_CONS, F(D_TELNET_INCORRECT_LOGIN_ATTEMPT), "serial");
|
||||
if(consoleLoginAttempt >= 3) {
|
||||
console_timeout();
|
||||
} else {
|
||||
bufferedSerialClient->print(F(D_USERNAME " "));
|
||||
}
|
||||
}
|
||||
#else
|
||||
console_logon();
|
||||
#endif
|
||||
break;
|
||||
}
|
||||
default:
|
||||
if(strcasecmp_P(input, PSTR("logoff")) == 0) {
|
||||
#if HASP_USE_HTTP > 0 || HASP_USE_HTTP_ASYNC > 0
|
||||
if(strcmp(input, http_config.password) == 0) {
|
||||
bufferedSerialClient->println(F("\r\n" D_USERNAME " "));
|
||||
consoleLoginState = CONSOLE_UNAUTHENTICATED;
|
||||
} else {
|
||||
}
|
||||
#endif
|
||||
} else {
|
||||
dispatch_text_line(input);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void consoleStart()
|
||||
{
|
||||
LOG_TRACE(TAG_MSGR, F(D_SERVICE_STARTING));
|
||||
console = new ConsoleInput(bufferedSerialClient, HASP_CONSOLE_BUFFER);
|
||||
if(console) {
|
||||
debugStartSerial(); // open Serial port
|
||||
|
||||
/* Now register logger for serial */
|
||||
Log.registerOutput(0, bufferedSerialClient, LOG_LEVEL_VERBOSE, true);
|
||||
bufferedSerialClient->flush();
|
||||
|
||||
LOG_INFO(TAG_CONS, F(D_SERVICE_STARTED));
|
||||
|
||||
console->setLineCallback(console_process_line);
|
||||
console_logon(); // todo: logon
|
||||
} else {
|
||||
console_logoff();
|
||||
LOG_ERROR(TAG_CONS, F(D_SERVICE_START_FAILED));
|
||||
}
|
||||
}
|
||||
|
||||
void consoleStop()
|
||||
{
|
||||
console_logoff();
|
||||
Log.unregisterOutput(0); // serialClient
|
||||
Serial.end();
|
||||
|
||||
delete console;
|
||||
console = NULL;
|
||||
}
|
||||
|
||||
void consoleSetup()
|
||||
{
|
||||
LOG_TRACE(TAG_MSGR, F(D_SERVICE_STARTING));
|
||||
debugConsole.setLineCallback(dispatch_text_line);
|
||||
LOG_INFO(TAG_CONS, F(D_SERVICE_STARTED));
|
||||
#if HASP_START_CONSOLE
|
||||
consoleStart();
|
||||
#endif
|
||||
}
|
||||
|
||||
IRAM_ATTR void consoleLoop()
|
||||
{
|
||||
if(!consoleInputEnabled) return;
|
||||
if(!console) return;
|
||||
|
||||
while(int16_t keypress = debugConsole.readKey()) {
|
||||
while(int16_t keypress = console->readKey()) {
|
||||
switch(keypress) {
|
||||
|
||||
case ConsoleInput::KEY_PAGE_UP:
|
||||
|
Loading…
x
Reference in New Issue
Block a user