shell; fix issues with partial reads and multiple lines of input being
handled in a single event.


Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/repo
Commit: 
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/commit/b6fa07ad
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/b6fa07ad
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/b6fa07ad

Branch: refs/heads/develop
Commit: b6fa07ad928de4d8cd1dd37d419d8f9e3bc33b1b
Parents: 524192c
Author: Marko Kiiskila <[email protected]>
Authored: Wed May 25 13:10:30 2016 -0700
Committer: Marko Kiiskila <[email protected]>
Committed: Wed May 25 13:10:30 2016 -0700

----------------------------------------------------------------------
 libs/shell/include/shell/shell.h |  2 +-
 libs/shell/src/shell.c           | 74 +++++++++++++----------------------
 2 files changed, 29 insertions(+), 47 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/b6fa07ad/libs/shell/include/shell/shell.h
----------------------------------------------------------------------
diff --git a/libs/shell/include/shell/shell.h b/libs/shell/include/shell/shell.h
index 29ff2dc..53c890d 100644
--- a/libs/shell/include/shell/shell.h
+++ b/libs/shell/include/shell/shell.h
@@ -39,7 +39,7 @@ typedef int (*shell_nlip_input_func_t)(struct os_mbuf *, void 
*arg);
 int shell_nlip_input_register(shell_nlip_input_func_t nf, void *arg);
 int shell_nlip_output(struct os_mbuf *m);
 
-void shell_console_rx_cb(int full_line);
+void shell_console_rx_cb(void);
 int shell_task_init(uint8_t prio, os_stack_t *stack, uint16_t stack_size,
                     int max_input_length);
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/b6fa07ad/libs/shell/src/shell.c
----------------------------------------------------------------------
diff --git a/libs/shell/src/shell.c b/libs/shell/src/shell.c
index f46b433..95ddcb0 100644
--- a/libs/shell/src/shell.c
+++ b/libs/shell/src/shell.c
@@ -70,21 +70,20 @@ static struct os_task shell_task;
 static struct os_eventq shell_evq;
 static struct os_event console_rdy_ev;
 
-static struct os_mutex g_shell_cmd_list_lock; 
+static struct os_mutex g_shell_cmd_list_lock;
 
 static char *shell_line;
 static int shell_line_capacity;
 static int shell_line_len;
 static char *argv[SHELL_MAX_ARGS];
-static uint8_t shell_full_line;
 
-static STAILQ_HEAD(, shell_cmd) g_shell_cmd_list = 
+static STAILQ_HEAD(, shell_cmd) g_shell_cmd_list =
     STAILQ_HEAD_INITIALIZER(g_shell_cmd_list);
 
 static struct os_mbuf *g_nlip_mbuf;
 static uint16_t g_nlip_expected_len;
 
-static int 
+static int
 shell_cmd_list_lock(void)
 {
     int rc;
@@ -102,7 +101,7 @@ err:
     return (rc);
 }
 
-static int 
+static int
 shell_cmd_list_unlock(void)
 {
     int rc;
@@ -143,7 +142,7 @@ err:
     return (rc);
 }
 
-static int 
+static int
 shell_cmd(char *cmd, char **argv, int argc)
 {
     struct shell_cmd *sc;
@@ -395,7 +394,7 @@ shell_nlip_input_register(shell_nlip_input_func_t nf, void 
*arg)
     return (0);
 }
 
-int 
+int
 shell_nlip_output(struct os_mbuf *m)
 {
     int rc;
@@ -410,25 +409,21 @@ err:
     return (rc);
 }
 
-static int
+static void
 shell_read_console(void)
 {
     int rc;
+    int full_line;
 
     while (1) {
         rc = console_read(shell_line + shell_line_len,
-                          shell_line_capacity - shell_line_len);
-        if (rc < 0) {
-            goto err;
-        }
-        if (rc == 0) {
+          shell_line_capacity - shell_line_len, &full_line);
+        if (rc <= 0 && !full_line) {
             break;
         }
-
-        if (shell_full_line) {
-            shell_line_len = 0;
-            shell_full_line = 0;
-            if (rc > 2) {
+        shell_line_len += rc;
+        if (full_line) {
+            if (shell_line_len > 2) {
                 if (shell_line[0] == SHELL_NLIP_PKT_START1 &&
                         shell_line[1] == SHELL_NLIP_PKT_START2) {
                     if (g_nlip_mbuf) {
@@ -437,35 +432,24 @@ shell_read_console(void)
                     }
                     g_nlip_expected_len = 0;
 
-                    rc = shell_nlip_process(&shell_line[2], rc-2);
+                    rc = shell_nlip_process(&shell_line[2], shell_line_len - 
2);
                 } else if (shell_line[0] == SHELL_NLIP_DATA_START1 &&
                         shell_line[1] == SHELL_NLIP_DATA_START2) {
-                    rc = shell_nlip_process(&shell_line[2], rc-2);
+                    rc = shell_nlip_process(&shell_line[2], shell_line_len - 
2);
                 } else {
-                    rc = shell_process_command(shell_line, rc);
-                    if (rc != 0) {
-                        goto err;
-                    }
+                    shell_process_command(shell_line, shell_line_len);
                 }
             } else {
-                rc = shell_process_command(shell_line, rc);
-                if (rc != 0) {
-                    goto err;
-                }
+                shell_process_command(shell_line, shell_line_len);
             }
-        } else {
-            shell_line_len += rc;
+            shell_line_len = 0;
         }
     }
-
-    return (0);
-err:
-    return (rc);
 }
 
 
 static void
-shell_task_func(void *arg) 
+shell_task_func(void *arg)
 {
     struct os_event *ev;
 
@@ -476,9 +460,9 @@ shell_task_func(void *arg)
         assert(ev != NULL);
 
         switch (ev->ev_type) {
-            case OS_EVENT_T_CONSOLE_RDY: 
+            case OS_EVENT_T_CONSOLE_RDY:
                 // Read and process all available lines on the console.
-                (void) shell_read_console();
+                shell_read_console();
                 break;
             case OS_EVENT_T_MQUEUE_DATA:
                 shell_nlip_mqueue_process();
@@ -488,22 +472,20 @@ shell_task_func(void *arg)
 }
 
 /**
- * This function is called from the console APIs when data is available 
- * to be read.  This is either a full line (full_line = 1), or when the 
- * console buffer (default = 128) is full.   At the moment, we assert() 
- * when the buffer is filled to avoid double buffering this data. 
+ * This function is called from the console APIs when data is available
+ * to be read.  This is either a full line, or when the
+ * console buffer (default = 128) is full.
  */
 void
-shell_console_rx_cb(int full_line)
+shell_console_rx_cb(void)
 {
-    shell_full_line = full_line;
     os_eventq_put(&shell_evq, &console_rdy_ev);
 }
 
 static int
 shell_echo_cmd(int argc, char **argv)
 {
-    int i; 
+    int i;
 
     for (i = 1; i < argc; i++) {
         console_write(argv[i], strlen(argv[i]));
@@ -540,7 +522,7 @@ shell_help_cmd(int argc, char **argv)
     return (0);
 }
 
-int 
+int
 shell_task_init(uint8_t prio, os_stack_t *stack, uint16_t stack_size,
                 int max_input_length)
 {
@@ -590,7 +572,7 @@ shell_task_init(uint8_t prio, os_stack_t *stack, uint16_t 
stack_size,
     os_eventq_init(&shell_evq);
     os_mqueue_init(&g_shell_nlip_mq, NULL);
 
-    rc = os_task_init(&shell_task, "shell", shell_task_func, 
+    rc = os_task_init(&shell_task, "shell", shell_task_func,
             NULL, prio, OS_WAIT_FOREVER, stack, stack_size);
     if (rc != 0) {
         goto err;

Reply via email to