Skip to content

Commit

Permalink
CLI: reworked terminal report parser to handle multiple messages in a…
Browse files Browse the repository at this point in the history
… buffer
  • Loading branch information
twystd committed Aug 16, 2024
1 parent b216af7 commit 4bfc44e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 20 deletions.
54 changes: 37 additions & 17 deletions Rev.0/firmware/core/src/cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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'.
Expand All @@ -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;
Expand Down Expand Up @@ -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.
*
*/
Expand Down
6 changes: 3 additions & 3 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 4bfc44e

Please sign in to comment.