mirror of
https://github.com/arendst/Tasmota.git
synced 2025-07-27 20:56:35 +00:00
Fix telnet async input
This commit is contained in:
parent
9e3ad52356
commit
b47a1bc1e2
@ -24,7 +24,7 @@
|
||||
* To start telnet at restart add a rule like
|
||||
* on system#boot do backlog telnetcolor 33,36,32; telnet 1 endon
|
||||
*
|
||||
* Supported color codes:
|
||||
* Supported ANSI Escape Color codes:
|
||||
* Black 30
|
||||
* Red 31
|
||||
* Green 32
|
||||
@ -59,8 +59,10 @@ struct {
|
||||
char *buffer = nullptr;
|
||||
uint16_t port;
|
||||
uint16_t buffer_size;
|
||||
uint16_t in_byte_counter;
|
||||
uint8_t prompt;
|
||||
uint8_t color[3];
|
||||
bool overrun;
|
||||
bool ip_filter_enabled;
|
||||
} Telnet;
|
||||
|
||||
@ -124,61 +126,55 @@ void TelnetLoop(void) {
|
||||
}
|
||||
}
|
||||
|
||||
if (0 == Telnet.prompt) {
|
||||
WiFiClient &client = Telnet.client;
|
||||
if (client) {
|
||||
if (0 == Telnet.prompt) {
|
||||
client.printf("\x1b[%dm%s:#\x1b[0m ", Telnet.color[0], TasmotaGlobal.hostname); // \x1b[33m = Yellow, \x1b[0m = end color
|
||||
Telnet.prompt = 1; // Print single linefeed for non-requested data
|
||||
}
|
||||
while (client.available()) { client.read(); } // Flush input
|
||||
return;
|
||||
}
|
||||
|
||||
bool busy;
|
||||
uint32_t buf_len = 0;
|
||||
do {
|
||||
busy = false; // Exit loop if no data was transferred
|
||||
bool overrun = false;
|
||||
WiFiClient &client = Telnet.client;
|
||||
while (client && (client.available())) {
|
||||
uint8_t c = client.read();
|
||||
if (c >= 0) {
|
||||
busy = true;
|
||||
if (isprint(c)) { // Any char between 32 and 127
|
||||
if (buf_len < Telnet.buffer_size -1) { // Add char to string if it still fits
|
||||
Telnet.buffer[buf_len++] = c;
|
||||
while (client.available()) {
|
||||
yield();
|
||||
uint8_t in_byte = client.read();
|
||||
if (isprint(in_byte)) { // Any char between 32 and 127
|
||||
if (Telnet.in_byte_counter < Telnet.buffer_size -1) { // Add char to string if it still fits
|
||||
Telnet.buffer[Telnet.in_byte_counter++] = in_byte;
|
||||
} else {
|
||||
overrun = true; // Signal overrun but continue reading input to flush until '\n' (EOL)
|
||||
Telnet.overrun = true; // Signal overrun but continue reading input to flush until '\n' (EOL)
|
||||
}
|
||||
}
|
||||
else if (c == '\n') {
|
||||
Telnet.buffer[buf_len] = 0; // Telnet data completed
|
||||
else if (in_byte == '\n') {
|
||||
Telnet.buffer[Telnet.in_byte_counter] = 0; // Telnet data completed
|
||||
TasmotaGlobal.seriallog_level = (Settings->seriallog_level < LOG_LEVEL_INFO) ? (uint8_t)LOG_LEVEL_INFO : Settings->seriallog_level;
|
||||
client.printf("\r"); // Move cursor to begin of line (needed for non-buffered input)
|
||||
Telnet.prompt = 2; // Do not print linefeed for requested data
|
||||
if (overrun) {
|
||||
if (Telnet.overrun) {
|
||||
AddLog(LOG_LEVEL_INFO, PSTR("TLN: buffer overrun"));
|
||||
} else {
|
||||
AddLog(LOG_LEVEL_INFO, PSTR("TLN: %s"), Telnet.buffer);
|
||||
ExecuteCommand(Telnet.buffer, SRC_TELNET);
|
||||
}
|
||||
Telnet.in_byte_counter = 0;
|
||||
Telnet.overrun = false;
|
||||
Telnet.prompt = 0; // Print prompt
|
||||
client.flush();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
yield(); // Avoid WDT if heavy traffic
|
||||
} while (busy);
|
||||
}
|
||||
|
||||
void TelnetStop(void) {
|
||||
if (Telnet.server) {
|
||||
Telnet.server->stop();
|
||||
delete Telnet.server;
|
||||
Telnet.server = nullptr;
|
||||
|
||||
}
|
||||
WiFiClient &client = Telnet.client;
|
||||
if (client) {
|
||||
client.stop();
|
||||
}
|
||||
|
||||
free(Telnet.buffer);
|
||||
Telnet.buffer = nullptr;
|
||||
}
|
||||
@ -228,7 +224,7 @@ void CmndTelnet(void) {
|
||||
if (Telnet.buffer) {
|
||||
if (1 == Telnet.port) { Telnet.port = 23; }
|
||||
Telnet.server = new WiFiServer(Telnet.port);
|
||||
Telnet.server->begin(); // start TCP server
|
||||
Telnet.server->begin(); // Start TCP server
|
||||
Telnet.server->setNoDelay(true);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user