Add consoleStart and consoleStop functionality

This commit is contained in:
fvanroie 2021-07-19 00:31:54 +02:00
parent d194d7f04f
commit 31389d92dd
5 changed files with 176 additions and 37 deletions

View File

@ -14,7 +14,7 @@
#define debug_print(io, ...) io->printf(__VA_ARGS__) #define debug_print(io, ...) io->printf(__VA_ARGS__)
#define debug_newline(io) io->println() #define debug_newline(io) io->println()
bool debugSerialStarted = false; // bool debugSerialStarted = false;
#else #else
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@ -23,7 +23,7 @@ bool debugSerialStarted = false;
#define debug_print(io, ...) fprintf(stdout, __VA_ARGS__) #define debug_print(io, ...) fprintf(stdout, __VA_ARGS__)
#define debug_newline(io) fprintf(stdout, "\n") #define debug_newline(io) fprintf(stdout, "\n")
bool debugSerialStarted = true; // bool debugSerialStarted = true;
#endif #endif
bool debugAnsiCodes = true; bool debugAnsiCodes = true;
@ -107,7 +107,7 @@ void debugEverySecond()
// printLocalTime(); // printLocalTime();
} }
void debugStart() void debugStart(void)
{ {
#if defined(WINDOWS) || defined(POSIX) #if defined(WINDOWS) || defined(POSIX)
@ -115,12 +115,20 @@ void debugStart()
debugPrintHaspHeader(NULL); debugPrintHaspHeader(NULL);
debug_newline(); debug_newline();
LOG_INFO(TAG_DEBG, F("Console started"));
LOG_INFO(TAG_DEBG, F("Environment: " PIOENV)); LOG_INFO(TAG_DEBG, F("Environment: " PIOENV));
LOG_INFO(TAG_DEBG, F("Console started"));
debug_flush();
#else
#if HASP_USE_CONSOLE > 0
consoleSetup();
#endif #endif
#endif
/*
if(debugSerialStarted) { if(debugSerialStarted) {
debug_flush();
// Serial.println(); // Serial.println();
// Serial.println(debugHaspHeader()); // Serial.println(debugHaspHeader());
@ -129,11 +137,12 @@ void debugStart()
// prepare syslog configuration here (can be anywhere before first call of // prepare syslog configuration here (can be anywhere before first call of
// log/logf method) // log/logf method)
*/
} }
void debugStop() void debugStop()
{ {
if(debugSerialStarted) debug_flush(); // if(debugSerialStarted) debug_flush();
} }
/* ===== Special Event Processors ===== */ /* ===== Special Event Processors ===== */

View File

@ -138,6 +138,7 @@ void debugLvglLogEvent(lv_log_level_t level, const char* file, uint32_t line, co
IRAM_ATTR void debugLoop(void); IRAM_ATTR void debugLoop(void);
void debugEverySecond(void); void debugEverySecond(void);
void debugStart(void); void debugStart(void);
void debugStartSerial(void);
void debugStop(void); void debugStop(void);
void debugPrintHaspHeader(Print* output); void debugPrintHaspHeader(Print* output);
void debugPrintTag(uint8_t tag, Print* _logOutput); void debugPrintTag(uint8_t tag, Print* _logOutput);

View File

@ -84,7 +84,7 @@ WiFiUDP* syslogClient;
// char serialInputBuffer[220] = ""; // char serialInputBuffer[220] = "";
// uint16_t historyIndex = sizeof(serialInputBuffer) - 1; // Empty buffer // uint16_t historyIndex = sizeof(serialInputBuffer) - 1; // Empty buffer
uint16_t debugSerialBaud = SERIAL_SPEED / 10; // Multiplied by 10 uint16_t debugSerialBaud = SERIAL_SPEED / 10; // Multiplied by 10
extern bool debugSerialStarted; // extern bool debugSerialStarted;
extern bool debugAnsiCodes; extern bool debugAnsiCodes;
extern dispatch_conf_t dispatch_setings; 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 // Start Serial Port at correct
void debugSetup(JsonObject settings) 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; uint32_t baudrate = 0;
#if HASP_USE_CONFIG > 0 if(debugSerialBaud == 0) {
baudrate = settings[FPSTR(FP_CONFIG_BAUD)].as<uint32_t>() * 10; baudrate = SERIAL_SPEED;
#endif } else {
baudrate = debugSerialBaud * 10;
}
if(baudrate == 0) baudrate = SERIAL_SPEED;
if(baudrate >= 9600u) { /* the baudrates are stored divided by 10 */ if(baudrate >= 9600u) { /* the baudrates are stored divided by 10 */
#if defined(STM32F4xx) #if defined(STM32F4xx)
@ -309,7 +306,8 @@ void debugSetup(JsonObject settings)
Serial.begin(baudrate); /* prepare for possible serial debug */ Serial.begin(baudrate); /* prepare for possible serial debug */
delay(10); delay(10);
Log.registerOutput(0, &Serial, LOG_LEVEL_VERBOSE, true); // LOG_LEVEL_VERBOSE Log.registerOutput(0, &Serial, LOG_LEVEL_VERBOSE, true); // LOG_LEVEL_VERBOSE
debugSerialStarted = true;
// debugSerialStarted = true;
Serial.println(); Serial.println();
debugPrintHaspHeader(&Serial); 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(D_SERVICE_STARTED " @ %u Bps"), baudrate);
LOG_INFO(TAG_DEBG, F("Environment: " PIOENV)); 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) IRAM_ATTR void debugLoop(void)
{} {}

View File

@ -57,7 +57,7 @@ void setup()
* Read & Apply User Configuration * Read & Apply User Configuration
***************************/ ***************************/
#if HASP_USE_CONFIG > 0 #if HASP_USE_CONFIG > 0
configSetup(); // also runs debugSetup() and debugStart() configSetup(); // also runs debugSetup(), debugStart() and consoleSetup()
#endif #endif
dispatchSetup(); // before hasp and oobe, asap after logging starts dispatchSetup(); // before hasp and oobe, asap after logging starts
@ -98,9 +98,9 @@ void setup()
httpSetup(); httpSetup();
#endif #endif
#if HASP_USE_CONSOLE > 0 // #if HASP_USE_CONSOLE > 0
consoleSetup(); // consoleSetup(); // the consoleSetup is called in debugSetup
#endif // #endif
#if HASP_USE_TELNET > 0 #if HASP_USE_TELNET > 0
telnetSetup(); telnetSetup();

View File

@ -6,6 +6,7 @@
#if HASP_USE_CONSOLE > 0 #if HASP_USE_CONSOLE > 0
#include "ConsoleInput.h" #include "ConsoleInput.h"
#include <StreamUtils.h>
#include "hasp_debug.h" #include "hasp_debug.h"
#include "hasp_config.h" #include "hasp_config.h"
@ -13,26 +14,138 @@
#include "../../hasp/hasp_dispatch.h" #include "../../hasp/hasp_dispatch.h"
uint8_t consoleInputEnabled = true; #if HASP_USE_HTTP > 0 || HASP_USE_HTTP_ASYNC > 0
ConsoleInput debugConsole(&Serial, HASP_CONSOLE_BUFFER); 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() 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() void consoleSetup()
{ {
LOG_TRACE(TAG_MSGR, F(D_SERVICE_STARTING)); #if HASP_START_CONSOLE
debugConsole.setLineCallback(dispatch_text_line); consoleStart();
LOG_INFO(TAG_CONS, F(D_SERVICE_STARTED)); #endif
} }
IRAM_ATTR void consoleLoop() IRAM_ATTR void consoleLoop()
{ {
if(!consoleInputEnabled) return; if(!console) return;
while(int16_t keypress = debugConsole.readKey()) { while(int16_t keypress = console->readKey()) {
switch(keypress) { switch(keypress) {
case ConsoleInput::KEY_PAGE_UP: case ConsoleInput::KEY_PAGE_UP: