From 31389d92dd9899fce341e8405b20264c62f8c62b Mon Sep 17 00:00:00 2001 From: fvanroie <15969459+fvanroie@users.noreply.github.com> Date: Mon, 19 Jul 2021 00:31:54 +0200 Subject: [PATCH] Add consoleStart and consoleStop functionality --- src/hasp_debug.cpp | 35 ++++++---- src/hasp_debug.h | 1 + src/log/hasp_debug.cpp | 40 +++++++---- src/main_arduino.cpp | 8 +-- src/sys/svc/hasp_console.cpp | 129 ++++++++++++++++++++++++++++++++--- 5 files changed, 176 insertions(+), 37 deletions(-) diff --git a/src/hasp_debug.cpp b/src/hasp_debug.cpp index dd51f515..8e865014 100644 --- a/src/hasp_debug.cpp +++ b/src/hasp_debug.cpp @@ -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 #include @@ -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 ===== */ diff --git a/src/hasp_debug.h b/src/hasp_debug.h index 275ec663..c4757b55 100644 --- a/src/hasp_debug.h +++ b/src/hasp_debug.h @@ -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); diff --git a/src/log/hasp_debug.cpp b/src/log/hasp_debug.cpp index 52dd8217..6a8ed8cc 100644 --- a/src/log/hasp_debug.cpp +++ b/src/log/hasp_debug.cpp @@ -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() * 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(); +#endif +} + IRAM_ATTR void debugLoop(void) {} diff --git a/src/main_arduino.cpp b/src/main_arduino.cpp index 20576d43..44a6633d 100644 --- a/src/main_arduino.cpp +++ b/src/main_arduino.cpp @@ -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(); diff --git a/src/sys/svc/hasp_console.cpp b/src/sys/svc/hasp_console.cpp index c4397ba8..8dacc274 100644 --- a/src/sys/svc/hasp_console.cpp +++ b/src/sys/svc/hasp_console.cpp @@ -6,6 +6,7 @@ #if HASP_USE_CONSOLE > 0 #include "ConsoleInput.h" +#include #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: