From 1a9108aaf685e1e640d09090a3412854b7774103 Mon Sep 17 00:00:00 2001 From: Jerzy Kasenberg Date: Mon, 9 Mar 2020 13:50:06 +0100 Subject: [PATCH 1/4] sys/console: Allow to disable NLIP in console Add syscfg switch that allows builds with NLIP code disabled. It should reduce console code size if NLIP is not needed. --- sys/console/full/src/console.c | 38 +++++++++++++++-------------- sys/console/full/syscfg.yml | 3 +++ sys/console/minimal/src/console.c | 40 +++++++++++++++++-------------- sys/console/minimal/syscfg.yml | 3 +++ 4 files changed, 48 insertions(+), 36 deletions(-) diff --git a/sys/console/full/src/console.c b/sys/console/full/src/console.c index e2a6e22419..5e5fc9688e 100644 --- a/sys/console/full/src/console.c +++ b/sys/console/full/src/console.c @@ -517,31 +517,33 @@ console_write(const char *str, int cnt) return; } - if (cnt >= 2 && str[0] == CONSOLE_NLIP_DATA_START1 && - str[1] == CONSOLE_NLIP_DATA_START2) { - g_is_output_nlip = 1; - } + if (MYNEWT_VAL(CONSOLE_NLIP)) { + if (cnt >= 2 && str[0] == CONSOLE_NLIP_DATA_START1 && + str[1] == CONSOLE_NLIP_DATA_START2) { + g_is_output_nlip = 1; + } - /* From the shell the first byte is always \n followed by the - * actual pkt start bytes, hence checking byte 1 and 2 - */ - if (cnt >= 3 && str[1] == CONSOLE_NLIP_PKT_START1 && - str[2] == CONSOLE_NLIP_PKT_START2) { - g_is_output_nlip = 1; - } + /* From the shell the first byte is always \n followed by the + * actual pkt start bytes, hence checking byte 1 and 2 + */ + if (cnt >= 3 && str[1] == CONSOLE_NLIP_PKT_START1 && + str[2] == CONSOLE_NLIP_PKT_START2) { + g_is_output_nlip = 1; + } - /* If the byte string is non nlip and we are silencing non nlip bytes, - * do not let it go out on the console - */ - if (!g_is_output_nlip && g_console_silence_non_nlip) { - goto done; + /* If the byte string is non nlip and we are silencing non nlip bytes, + * do not let it go out on the console + */ + if (!g_is_output_nlip && g_console_silence_non_nlip) { + goto done; + } } console_switch_to_logs(); console_filter_write(str, cnt); done: - if (cnt > 0 && str[cnt - 1] == '\n') { + if (MYNEWT_VAL(CONSOLE_NLIP) && cnt > 0 && str[cnt - 1] == '\n') { g_is_output_nlip = 0; } (void)console_unlock(); @@ -1082,7 +1084,7 @@ console_handle_char(uint8_t byte) } input = current_line_ev->ev_arg; - if (handle_nlip(byte)) { + if (MYNEWT_VAL(CONSOLE_NLIP) && handle_nlip(byte)) { return 0; } diff --git a/sys/console/full/syscfg.yml b/sys/console/full/syscfg.yml index 34a039d9f2..5115566d98 100644 --- a/sys/console/full/syscfg.yml +++ b/sys/console/full/syscfg.yml @@ -35,6 +35,9 @@ syscfg.defs: CONSOLE_ECHO: description: 'Default console echo' value: 1 + CONSOLE_NLIP: + description: 'Enable NLIP handling in console code' + value: 1 CONSOLE_COMPAT: description: 'Console backward compatibility' value: 1 diff --git a/sys/console/minimal/src/console.c b/sys/console/minimal/src/console.c index d44f447fa5..62099174be 100644 --- a/sys/console/minimal/src/console.c +++ b/sys/console/minimal/src/console.c @@ -149,25 +149,27 @@ console_write(const char *str, int cnt) return; } - if (cnt >= 2 && str[0] == CONSOLE_NLIP_DATA_START1 && - str[1] == CONSOLE_NLIP_DATA_START2) { - g_is_output_nlip = 1; - } + if (MYNEWT_VAL(CONSOLE_NLIP)) { + if (cnt >= 2 && str[0] == CONSOLE_NLIP_DATA_START1 && + str[1] == CONSOLE_NLIP_DATA_START2) { + g_is_output_nlip = 1; + } - /* From the shell the first byte is always \n followed by the - * actual pkt start bytes, hence checking byte 1 and 2 - */ - if (cnt >= 3 && str[1] == CONSOLE_NLIP_PKT_START1 && - str[2] == CONSOLE_NLIP_PKT_START2) { - g_is_output_nlip = 1; - } + /* From the shell the first byte is always \n followed by the + * actual pkt start bytes, hence checking byte 1 and 2 + */ + if (cnt >= 3 && str[1] == CONSOLE_NLIP_PKT_START1 && + str[2] == CONSOLE_NLIP_PKT_START2) { + g_is_output_nlip = 1; + } - /* If the byte string is non nlip and we are silencing non nlip bytes, - * do not let it go out on the console - */ - if (!g_is_output_nlip && g_console_silence_non_nlip) { - goto done; + /* If the byte string is non nlip and we are silencing non nlip bytes, + * do not let it go out on the console + */ + if (!g_is_output_nlip && g_console_silence_non_nlip) { + goto done; + } } for (i = 0; i < cnt; i++) { @@ -177,7 +179,7 @@ console_write(const char *str, int cnt) } done: - if (cnt > 0 && str[cnt - 1] == '\n') { + if (MYNEWT_VAL(CONSOLE_NLIP) && cnt > 0 && str[cnt - 1] == '\n') { g_is_output_nlip = 0; } (void)console_unlock(); @@ -296,7 +298,7 @@ console_handle_char(uint8_t byte) input = ev->ev_arg; } - if (handle_nlip(byte)) { + if (MYNEWT_VAL(CONSOLE_NLIP) && handle_nlip(byte)) { if (byte == '\n') { insert_char(&input->line[cur], byte, end); input->line[cur] = '\0'; @@ -334,12 +336,14 @@ console_handle_char(uint8_t byte) /* Handle special control characters */ if (!isprint(byte)) { switch (byte) { +#if MYNEWT_VAL(CONSOLE_NLIP) case CONSOLE_NLIP_PKT_START1: nlip_state |= NLIP_PKT_START1; break; case CONSOLE_NLIP_DATA_START1: nlip_state |= NLIP_DATA_START1; break; +#endif default: insert_char(&input->line[cur], byte, end); /* Falls through. */ diff --git a/sys/console/minimal/syscfg.yml b/sys/console/minimal/syscfg.yml index 5a8dc01dd0..bacc751f1c 100644 --- a/sys/console/minimal/syscfg.yml +++ b/sys/console/minimal/syscfg.yml @@ -29,6 +29,9 @@ syscfg.defs: CONSOLE_ECHO: description: 'Default console echo' value: 1 + CONSOLE_NLIP: + description: 'Enable NLIP handling in console code' + value: 1 CONSOLE_COMPAT: description: 'Console backward compatibility' value: 1 From ae6bfbc2ec748ed4ffdd9b036316a78d4216efd6 Mon Sep 17 00:00:00 2001 From: Jerzy Kasenberg Date: Mon, 9 Mar 2020 14:04:03 +0100 Subject: [PATCH 2/4] sys/console: Fix console to respect echo request Function console_echo(1) was called in some cases during nlip handling. This resulted in console echo being turned on while user code could turn it off on purpose before. Now console echo will not turned of if user setting/code did not wanted to have echo enabled. --- sys/console/full/src/console.c | 26 +++++++++++++++++++------- sys/console/minimal/src/console.c | 10 ++++++---- 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/sys/console/full/src/console.c b/sys/console/full/src/console.c index 5e5fc9688e..c8c057a503 100644 --- a/sys/console/full/src/console.c +++ b/sys/console/full/src/console.c @@ -85,6 +85,7 @@ static struct os_eventq compat_lines_queue; static int esc_state; static int nlip_state; static int echo = MYNEWT_VAL(CONSOLE_ECHO); +static int console_is_interactive = MYNEWT_VAL(CONSOLE_ECHO); static unsigned int ansi_val, ansi_val_2; static bool rx_stalled; @@ -128,6 +129,7 @@ console_out_nolock(int c) void console_echo(int on) { + console_is_interactive = on; echo = on; } @@ -674,7 +676,7 @@ insert_char(char *pos, char c) return; } - if (echo) { + if (console_is_interactive) { if (!MYNEWT_VAL_CHOICE(CONSOLE_HISTORY, none) && MYNEWT_VAL(CONSOLE_HISTORY_AUTO_SEARCH) && trailing_selection) { cursor_clear_line(); @@ -702,7 +704,7 @@ insert_char(char *pos, char c) end = trailing_chars; while (end-- > 0) { - if (echo) { + if (console_is_interactive) { console_out_nolock(tmp); } c = *pos; @@ -711,7 +713,7 @@ insert_char(char *pos, char c) } /* Move cursor back to right place */ - if (echo) { + if (console_is_interactive) { cursor_backward(trailing_chars); } } @@ -983,7 +985,7 @@ handle_nlip(uint8_t byte) insert_char(&input->line[cur], byte); if (byte == '\n') { input->line[cur] = '\0'; - console_echo(1); + console_is_interactive = echo; nlip_state = 0; console_handle_line(); @@ -993,7 +995,7 @@ handle_nlip(uint8_t byte) if (byte == CONSOLE_NLIP_PKT_START2) { nlip_state = NLIP_PKT_START2; /* Disable echo to not flood the UART */ - console_echo(0); + console_is_interactive = 0; insert_char(&input->line[cur], CONSOLE_NLIP_PKT_START1); insert_char(&input->line[cur], CONSOLE_NLIP_PKT_START2); } else { @@ -1005,7 +1007,7 @@ handle_nlip(uint8_t byte) if (byte == CONSOLE_NLIP_DATA_START2) { nlip_state = NLIP_DATA_START2; /* Disable echo to not flood the UART */ - console_echo(0); + console_is_interactive = 0; insert_char(&input->line[cur], CONSOLE_NLIP_DATA_START1); insert_char(&input->line[cur], CONSOLE_NLIP_DATA_START2); } else { @@ -1041,7 +1043,7 @@ console_append_char(char *line, uint8_t byte) return 1; } - if (echo) { + if (console_is_interactive) { /* Echo back to console */ console_switch_to_prompt(); console_out_nolock(byte); @@ -1088,6 +1090,16 @@ console_handle_char(uint8_t byte) return 0; } + if (!console_is_interactive) { + if (byte == '\r' || byte == '\n') { + input->line[cur + trailing_chars] = '\0'; + console_handle_line(); + } else { + insert_char(&input->line[cur], byte); + } + return 0; + } + if (console_lock(1000)) { return -1; } diff --git a/sys/console/minimal/src/console.c b/sys/console/minimal/src/console.c index 62099174be..58081afe07 100644 --- a/sys/console/minimal/src/console.c +++ b/sys/console/minimal/src/console.c @@ -53,6 +53,7 @@ static struct os_eventq compat_lines_queue; static int nlip_state; static int echo = MYNEWT_VAL(CONSOLE_ECHO); +static int console_is_interactive = MYNEWT_VAL(CONSOLE_ECHO); static uint8_t cur, end; static struct os_eventq *avail_queue; @@ -72,6 +73,7 @@ void console_echo(int on) { echo = on; + console_is_interactive = on; } int @@ -240,7 +242,7 @@ insert_char(char *pos, char c, uint8_t end) return; } - if (echo) { + if (console_is_interactive) { /* Echo back to console */ console_out(c); } @@ -315,16 +317,16 @@ console_handle_char(uint8_t byte) input = NULL; ev = NULL; - console_echo(1); + console_is_interactive = echo; return 0; /* Ignore characters if there's no more buffer space */ } else if (byte == CONSOLE_NLIP_PKT_START2) { /* Disable echo to not flood the UART */ - console_echo(0); + console_is_interactive = 0; insert_char(&input->line[cur], CONSOLE_NLIP_PKT_START1, end); } else if (byte == CONSOLE_NLIP_DATA_START2) { /* Disable echo to not flood the UART */ - console_echo(0); + console_is_interactive = 0; insert_char(&input->line[cur], CONSOLE_NLIP_DATA_START1, end); } From 1c357950df292a8fd9ea65ca9a1988f148dff2bd Mon Sep 17 00:00:00 2001 From: Jerzy Kasenberg Date: Mon, 9 Mar 2020 14:36:25 +0100 Subject: [PATCH 3/4] sys/console: Make echo of LF optional in silenced NLIP Code was echoing back LF character even when application requested to silence non-NLIP data on input. Now this behaviour is still possible but require explicit setting CONSOLE_NLIP_ECHO_LF which is disabled by default. --- sys/console/full/src/console.c | 11 +++++++++-- sys/console/full/syscfg.yml | 7 +++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/sys/console/full/src/console.c b/sys/console/full/src/console.c index c8c057a503..d937837d01 100644 --- a/sys/console/full/src/console.c +++ b/sys/console/full/src/console.c @@ -1021,8 +1021,15 @@ handle_nlip(uint8_t byte) } else if (byte == CONSOLE_NLIP_PKT_START1) { nlip_state = NLIP_PKT_START1; } else { - /* For old code compatibility end of lines characters pass through */ - handled = g_console_ignore_non_nlip && byte != '\r' && byte != '\n'; + if (!g_console_ignore_non_nlip) { + handled = 0; + } else if ((byte == '\r' || byte == '\n') && MYNEWT_VAL(CONSOLE_NLIP_ECHO_LF)) { + /* For old code compatibility end of lines characters pass through */ + if (0 == console_lock(1000)) { + console_out_nolock(byte); + console_unlock(); + } + } } break; } diff --git a/sys/console/full/syscfg.yml b/sys/console/full/syscfg.yml index 5115566d98..16d041344d 100644 --- a/sys/console/full/syscfg.yml +++ b/sys/console/full/syscfg.yml @@ -38,6 +38,13 @@ syscfg.defs: CONSOLE_NLIP: description: 'Enable NLIP handling in console code' value: 1 + CONSOLE_NLIP_ECHO_LF: + description: > + Enable LF echo even if function console_ignore_non_nlip() + was called to ignore any non NLIP input. This allows for + simple check if application is running correctly + by using simple terminal. + value: 0 CONSOLE_COMPAT: description: 'Console backward compatibility' value: 1 From 5abaebe388461459d857fbf7380372c53990c49d Mon Sep 17 00:00:00 2001 From: Jerzy Kasenberg Date: Mon, 16 Mar 2020 14:41:02 +0100 Subject: [PATCH 4/4] sys/console: Don't show prompt when echo is off Prompt is output on console that did not originated in application code. This is similar to echo characters that goes out of console outside of application decision. This disable printing prompt when echo is turned off so console code does not add anything to the output. --- sys/console/full/src/console.c | 4 ++++ sys/console/minimal/src/console.c | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/sys/console/full/src/console.c b/sys/console/full/src/console.c index d937837d01..a2dc532043 100644 --- a/sys/console/full/src/console.c +++ b/sys/console/full/src/console.c @@ -471,6 +471,10 @@ console_prompt_set(const char *prompt, const char *line) } trailing_chars = 0; + if (!console_is_interactive) { + return; + } + locked = console_lock(1000) == OS_OK; console_switch_to_prompt(); diff --git a/sys/console/minimal/src/console.c b/sys/console/minimal/src/console.c index 58081afe07..a850d9fd59 100644 --- a/sys/console/minimal/src/console.c +++ b/sys/console/minimal/src/console.c @@ -134,6 +134,10 @@ console_out(int c) void console_prompt_set(const char *prompt, const char *line) { + if (!console_is_interactive) { + return; + } + console_write(prompt, strlen(prompt)); if (line) { console_write(line, strlen(line));