From 4bfc44e73c4cf0523b8cb41e64b0228fd01327a1 Mon Sep 17 00:00:00 2001 From: twystd Date: Fri, 16 Aug 2024 14:36:36 -0700 Subject: [PATCH] CLI: reworked terminal report parser to handle multiple messages in a buffer --- Rev.0/firmware/core/src/cli.c | 54 ++++++++++++++++++++++++----------- TODO.md | 6 ++-- 2 files changed, 40 insertions(+), 20 deletions(-) diff --git a/Rev.0/firmware/core/src/cli.c b/Rev.0/firmware/core/src/cli.c index f9313dd..2a19260 100644 --- a/Rev.0/firmware/core/src/cli.c +++ b/Rev.0/firmware/core/src/cli.c @@ -35,6 +35,7 @@ const uint8_t height = 25; int64_t cli_timeout(alarm_id_t id, void *data); int64_t cli_ping_timeout(alarm_id_t id, void *data); +void cli_on_terminal_report(const char *buffer, int N); void echo(const char *line); void clearline(); @@ -121,8 +122,9 @@ const char *HELP[] = { */ void cli_init() { print(TERMINAL_CLEAR); + print(TERMINAL_QUERY_CODE); print(TERMINAL_QUERY_SIZE); - // print(TERMINAL_QUERY_CODE); + printf(TERMINAL_QUERY_STATUS); } /** Queries the terminal ID 'out of band'. @@ -147,25 +149,18 @@ void cli_rx(const struct buffer *received) { // terminal message? if (N > 0 && received->data[0] == 27) { const char *data = received->data; + int ix = 0; - // ... report device code ? - // e.g. [27 91 53 55 59 50 48 50 82 27 91 63 49 59 50 99 ] - if (N >= 5 && data[0] == 27 && data[1] == '[' && data[N - 1] == 'c') { - } - - // ... report device status? - if (N >= 4 && data[0] == 27 && data[1] == '[' && data[2] == '0' && data[3] == 'n') { - set_mode(MODE_CLI); - cancel_alarm(cli.ping); - } + while (ix < N) { + if (data[ix] == 27) { + int jx = ix; + while (++jx < N && data[jx] != 27) { + } - // ... report cursor position (ESC[#;#R) - // e.g. [27 91 53 55 59 50 48 50 82 ] - if (N >= 6 && data[0] == 27 && data[1] == '[' && data[N - 1] == 'R') { - char *code = strndup(&data[2], N - 3); + cli_on_terminal_report(&data[ix], jx - ix); + } - cpr(code); - free(code); + ix++; } return; @@ -236,6 +231,31 @@ void cli_rx(const struct buffer *received) { } } +/** Processes ANSI/VT100 terminal report. + * + */ +void cli_on_terminal_report(const char *data, int N) { + // ... report device code ? + // e.g. [27 91 53 55 59 50 48 50 82 27 91 63 49 59 50 99 ] + if (N >= 5 && data[0] == 27 && data[1] == '[' && data[N - 1] == 'c') { + } + + // ... report device status? + if (N >= 4 && data[0] == 27 && data[1] == '[' && data[2] == '0' && data[3] == 'n') { + set_mode(MODE_CLI); + cancel_alarm(cli.ping); + } + + // ... report cursor position (ESC[#;#R) + // e.g. [27 91 53 55 59 50 48 50 82 ] + if (N >= 6 && data[0] == 27 && data[1] == '[' && data[N - 1] == 'R') { + char *code = strndup(&data[2], N - 3); + + cpr(code); + free(code); + } +} + /* Timeout handler. Clears the current command and command line. * */ diff --git a/TODO.md b/TODO.md index 8b7a136..c884820 100644 --- a/TODO.md +++ b/TODO.md @@ -63,9 +63,9 @@ - [x] LED red/green for CLI mode - [x] LED blue/green for SSMP mode - [x] CLI idle - ping for terminal status - - [ ] dropping last typed character if echo uses sys::print - - printf works but sys::print doesn't - - parse **whole** terminal message buffer (may contain multiple messages) + - [x] parse buffer with multiple terminal report messages + - [ ] echo weirdness: printf works but sys::print doesn't + - [ ] parse commands using strtok ## PiZeroW - [ ] Reduce power consumption