This is an automated email from the ASF dual-hosted git repository.

jerzy pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-core.git

commit e43161034c68500503991c1d6c20cc66b849cdaf
Author: Jerzy Kasenberg <[email protected]>
AuthorDate: Tue Oct 15 12:55:49 2019 +0200

    sys/console: Consolidate nlip code for input
    
    Code that handle input part of nlip protocol was
    placed in 3 parts
    - handle_nlip - that was handling only second byte of header
    - first byte was handled together with non-printable characters
    - rest was handle just after handle_nlip returned 1
    
    Now all nlip related input handling is done in handle_nlip which
    could be in the future easier disabled.
    
    Code sending complete line to upper layer was placed in nlip section
    and in non-printable section that took care of \r \n.
    
    This code is now put in separate function console_handle_line.
    (As consequence console_handle_char's private static variable 'ev'
    is now visible in file scope).
---
 sys/console/full/src/console.c | 149 +++++++++++++++++++++--------------------
 1 file changed, 77 insertions(+), 72 deletions(-)

diff --git a/sys/console/full/src/console.c b/sys/console/full/src/console.c
index fec003c..acb949c 100644
--- a/sys/console/full/src/console.c
+++ b/sys/console/full/src/console.c
@@ -86,6 +86,7 @@ static uint16_t cur;
 static uint16_t trailing_chars;
 static struct os_eventq avail_queue;
 static struct os_eventq *lines_queue;
+static struct os_event *current_line_ev;
 static completion_cb completion;
 bool g_console_silence;
 bool g_console_silence_non_nlip;
@@ -660,27 +661,80 @@ ansi_cmd:
     esc_state &= ~ESC_ANSI;
 }
 
+static void
+console_handle_line(void)
+{
+    cur = 0;
+    trailing_chars = 0;
+    os_eventq_put(lines_queue, current_line_ev);
+
+#if MYNEWT_VAL(CONSOLE_COMPAT)
+    if (console_compat_rx_cb) {
+        console_compat_rx_cb();
+    }
+#endif
+
+    current_line_ev = NULL;
+}
+
 static int
 handle_nlip(uint8_t byte)
 {
-    if ((nlip_state == NLIP_PKT_START2) ||
-        (nlip_state == NLIP_DATA_START2))
-    {
-        return 1;
-    }
+    int handled;
+    struct console_input *input;
 
-    if ((nlip_state == NLIP_PKT_START1) &&
-        (byte == CONSOLE_NLIP_PKT_START2)) {
-        nlip_state == NLIP_PKT_START2;
-        return 1;
-    } else if ((nlip_state == NLIP_DATA_START1) &&
-               (byte == CONSOLE_NLIP_DATA_START2)) {
-        nlip_state == NLIP_DATA_START2;
-        return 1;
-    } else {
-        nlip_state = 0;
-        return 0;
+    input = current_line_ev->ev_arg;
+    handled = 0;
+
+    switch (nlip_state) {
+    case NLIP_PKT_START2:
+    case NLIP_DATA_START2:
+        handled = 1;
+        insert_char(&input->line[cur], byte);
+        if (byte == '\n') {
+            input->line[cur] = '\0';
+            console_echo(1);
+            nlip_state = 0;
+
+            console_handle_line();
+        }
+        break;
+    case NLIP_PKT_START1:
+        if (byte == CONSOLE_NLIP_PKT_START2) {
+            handled = 1;
+            nlip_state = NLIP_PKT_START2;
+            /* Disable echo to not flood the UART */
+            console_echo(0);
+            insert_char(&input->line[cur], CONSOLE_NLIP_PKT_START1);
+            insert_char(&input->line[cur], CONSOLE_NLIP_PKT_START2);
+        } else {
+            nlip_state = 0;
+        }
+        break;
+    case NLIP_DATA_START1:
+        if (byte == CONSOLE_NLIP_DATA_START2) {
+            handled = 1;
+            nlip_state = NLIP_DATA_START2;
+            /* Disable echo to not flood the UART */
+            console_echo(0);
+            insert_char(&input->line[cur], CONSOLE_NLIP_DATA_START1);
+            insert_char(&input->line[cur], CONSOLE_NLIP_DATA_START2);
+        } else {
+            nlip_state = 0;
+        }
+        break;
+    default:
+        if (byte == CONSOLE_NLIP_DATA_START1) {
+            handled = 1;
+            nlip_state = NLIP_DATA_START1;
+        } else if (byte == CONSOLE_NLIP_PKT_START1) {
+            handled = 1;
+            nlip_state = NLIP_PKT_START1;
+        }
+        break;
     }
+
+    return handled;
 }
 
 static int
@@ -711,55 +765,23 @@ console_handle_char(uint8_t byte)
 #if !MYNEWT_VAL(CONSOLE_INPUT)
     return 0;
 #endif
-    static struct os_event *ev;
-    static struct console_input *input;
+    struct console_input *input;
     static char prev_endl = '\0';
 
     if (!lines_queue) {
         return 0;
     }
 
-    if (!ev) {
-        ev = os_eventq_get_no_wait(&avail_queue);
-        if (!ev) {
+    if (!current_line_ev) {
+        current_line_ev = os_eventq_get_no_wait(&avail_queue);
+        if (!current_line_ev) {
             rx_stalled = true;
             return -1;
         }
-        input = ev->ev_arg;
     }
+    input = current_line_ev->ev_arg;
 
-    if (handle_nlip(byte))  {
-        if (byte == '\n') {
-            insert_char(&input->line[cur], byte);
-            input->line[cur] = '\0';
-            cur = 0;
-            trailing_chars = 0;
-            os_eventq_put(lines_queue, ev);
-            nlip_state = 0;
-
-#if MYNEWT_VAL(CONSOLE_COMPAT)
-            if (console_compat_rx_cb) {
-                console_compat_rx_cb();
-            }
-#endif
-
-            input = NULL;
-            ev = NULL;
-            console_echo(1);
-            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);
-            insert_char(&input->line[cur], CONSOLE_NLIP_PKT_START1);
-        } else if (byte == CONSOLE_NLIP_DATA_START2) {
-            /* Disable echo to not flood the UART */
-            console_echo(0);
-            insert_char(&input->line[cur], CONSOLE_NLIP_DATA_START1);
-        }
-
-        insert_char(&input->line[cur], byte);
-
+    if (handle_nlip(byte)) {
         return 0;
     }
 
@@ -795,12 +817,6 @@ console_handle_char(uint8_t byte)
     if (!isprint(byte)) {
         handle_ansi(byte, input->line);
         switch (byte) {
-        case CONSOLE_NLIP_PKT_START1:
-            nlip_state == NLIP_PKT_START1;
-            break;
-        case CONSOLE_NLIP_DATA_START1:
-            nlip_state == NLIP_DATA_START1;
-            break;
         case DEL:
         case BS:
             if (g_console_ignore_non_nlip) {
@@ -835,21 +851,10 @@ console_handle_char(uint8_t byte)
             input->line[cur + trailing_chars] = '\0';
             console_out('\r');
             console_out('\n');
-            cur = 0;
-            trailing_chars = 0;
-            os_eventq_put(lines_queue, ev);
 #if MYNEWT_VAL(CONSOLE_HISTORY_SIZE) > 0
             console_hist_add(input->line);
 #endif
-
-#if MYNEWT_VAL(CONSOLE_COMPAT)
-            if (console_compat_rx_cb) {
-                console_compat_rx_cb();
-            }
-#endif
-
-            input = NULL;
-            ev = NULL;
+            console_handle_line();
             break;
         case '\t':
             if (g_console_ignore_non_nlip) {

Reply via email to