From 292453d3060abf81e13aeb7bc7d76d3c3709da79 Mon Sep 17 00:00:00 2001
From: Kevin O'Connor <kevin@koconnor.net>
Date: Thu, 15 Jun 2017 15:06:10 -0400
Subject: [PATCH] command: Move command_task() to board specific code

Move the command_task() code from the generic code to the board
specific code.  This enables more flexibility in how the board
specific code processes input.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
---
 src/avr/serial.c     | 18 ++++++++++++++++--
 src/avr/usbserial.c  | 18 ++++++++++++++++--
 src/command.c        | 18 ++----------------
 src/command.h        |  2 ++
 src/generic/misc.h   |  2 --
 src/pru/main.c       | 17 +++++++++++++++--
 src/sam3x8e/serial.c | 18 ++++++++++++++++--
 src/simulator/main.c | 13 -------------
 8 files changed, 67 insertions(+), 39 deletions(-)

diff --git a/src/avr/serial.c b/src/avr/serial.c
index 3221b6f06..213b10ed1 100644
--- a/src/avr/serial.c
+++ b/src/avr/serial.c
@@ -92,7 +92,7 @@ enable_tx_irq(void)
  ****************************************************************/
 
 // Return a buffer (and length) containing any incoming messages
-char *
+static char *
 console_get_input(uint8_t *plen)
 {
     *plen = readb(&receive_pos);
@@ -100,7 +100,7 @@ console_get_input(uint8_t *plen)
 }
 
 // Remove from the receive buffer the given number of bytes
-void
+static void
 console_pop_input(uint8_t len)
 {
     uint8_t copied = 0;
@@ -124,6 +124,20 @@ console_pop_input(uint8_t len)
     }
 }
 
+// Process any incoming commands
+void
+console_task(void)
+{
+    uint8_t buf_len, pop_count;
+    char *buf = console_get_input(&buf_len);
+    int8_t ret = command_find_block(buf, buf_len, &pop_count);
+    if (ret > 0)
+        command_dispatch(buf, pop_count);
+    if (ret)
+        console_pop_input(pop_count);
+}
+DECL_TASK(console_task);
+
 // Return an output buffer that the caller may fill with transmit messages
 char *
 console_get_output(uint8_t len)
diff --git a/src/avr/usbserial.c b/src/avr/usbserial.c
index 3b14ea0a3..78ef719e6 100644
--- a/src/avr/usbserial.c
+++ b/src/avr/usbserial.c
@@ -22,7 +22,7 @@ usbserial_init(void)
 DECL_INIT(usbserial_init);
 
 // Return a buffer (and length) containing any incoming messages
-char *
+static char *
 console_get_input(uint8_t *plen)
 {
     for (;;) {
@@ -38,7 +38,7 @@ console_get_input(uint8_t *plen)
 }
 
 // Remove from the receive buffer the given number of bytes
-void
+static void
 console_pop_input(uint8_t len)
 {
     uint8_t needcopy = receive_pos - len;
@@ -47,6 +47,20 @@ console_pop_input(uint8_t len)
     receive_pos = needcopy;
 }
 
+// Process any incoming commands
+void
+console_task(void)
+{
+    uint8_t buf_len, pop_count;
+    char *buf = console_get_input(&buf_len);
+    int8_t ret = command_find_block(buf, buf_len, &pop_count);
+    if (ret > 0)
+        command_dispatch(buf, pop_count);
+    if (ret)
+        console_pop_input(pop_count);
+}
+DECL_TASK(console_task);
+
 // Return an output buffer that the caller may fill with transmit messages
 char *
 console_get_output(uint8_t len)
diff --git a/src/command.c b/src/command.c
index 6bc42bedb..709e75c4d 100644
--- a/src/command.c
+++ b/src/command.c
@@ -231,7 +231,7 @@ command_lookup_parser(uint8_t cmdid)
 enum { CF_NEED_SYNC=1<<0, CF_NEED_VALID=1<<1 };
 
 // Find the next complete message.
-static int8_t
+int8_t
 command_find_block(char *buf, uint8_t buf_len, uint8_t *pop_count)
 {
     static uint8_t sync_state;
@@ -293,7 +293,7 @@ nak:
 }
 
 // Dispatch all the commands found in a message block
-static void
+void
 command_dispatch(char *buf, uint8_t msglen)
 {
     char *p = &buf[MESSAGE_HEADER_SIZE];
@@ -312,17 +312,3 @@ command_dispatch(char *buf, uint8_t msglen)
         func(args);
     }
 }
-
-// Background task that reads commands from the board serial port
-void
-command_task(void)
-{
-    uint8_t buf_len, pop_count;
-    char *buf = console_get_input(&buf_len);
-    uint8_t ret = command_find_block(buf, buf_len, &pop_count);
-    if (ret > 0)
-        command_dispatch(buf, pop_count);
-    if (ret)
-        console_pop_input(pop_count);
-}
-DECL_TASK(command_task);
diff --git a/src/command.h b/src/command.h
index 5bd6faf4b..f2d90ee4a 100644
--- a/src/command.h
+++ b/src/command.h
@@ -35,6 +35,8 @@
 // command.c
 struct command_encoder;
 void _sendf(const struct command_encoder *ce, ...);
+int8_t command_find_block(char *buf, uint8_t buf_len, uint8_t *pop_count);
+void command_dispatch(char *buf, uint8_t msglen);
 
 // out/compile_time_request.c (auto generated file)
 struct command_encoder {
diff --git a/src/generic/misc.h b/src/generic/misc.h
index 4408a34a1..9847e9a94 100644
--- a/src/generic/misc.h
+++ b/src/generic/misc.h
@@ -4,8 +4,6 @@
 #include <stddef.h> // size_t
 #include <stdint.h> // uint8_t
 
-char *console_get_input(uint8_t *plen);
-void console_pop_input(uint8_t len);
 char *console_get_output(uint8_t len);
 void console_push_output(uint8_t len);
 
diff --git a/src/pru/main.c b/src/pru/main.c
index 9a132a020..cebcaa262 100644
--- a/src/pru/main.c
+++ b/src/pru/main.c
@@ -100,7 +100,7 @@ DECL_INIT(timer_init);
  ****************************************************************/
 
 // Return a buffer (and length) containing any incoming messages
-char *
+static char *
 console_get_input(uint8_t *plen)
 {
     uint32_t read_count = readl(&SHARED_MEM->read_count);
@@ -111,12 +111,25 @@ console_get_input(uint8_t *plen)
 }
 
 // Remove from the receive buffer the given number of bytes
-void
+static void
 console_pop_input(uint8_t len)
 {
     writel(&SHARED_MEM->read_count, 0);
 }
 
+// Process any incoming commands
+void
+console_task(void)
+{
+    uint8_t buf_len, pop_count;
+    char *buf = console_get_input(&buf_len);
+    int8_t ret = command_find_block(buf, buf_len, &pop_count);
+    if (ret)
+        command_dispatch(buf, pop_count);
+    console_pop_input(pop_count);
+}
+DECL_TASK(console_task);
+
 // Return an output buffer that the caller may fill with transmit messages
 char *
 console_get_output(uint8_t len)
diff --git a/src/sam3x8e/serial.c b/src/sam3x8e/serial.c
index 652d54d44..c06534436 100644
--- a/src/sam3x8e/serial.c
+++ b/src/sam3x8e/serial.c
@@ -83,7 +83,7 @@ enable_tx_irq(void)
  ****************************************************************/
 
 // Return a buffer (and length) containing any incoming messages
-char *
+static char *
 console_get_input(uint8_t *plen)
 {
     *plen = readl(&receive_pos);
@@ -91,7 +91,7 @@ console_get_input(uint8_t *plen)
 }
 
 // Remove from the receive buffer the given number of bytes
-void
+static void
 console_pop_input(uint8_t len)
 {
     uint32_t copied = 0;
@@ -115,6 +115,20 @@ console_pop_input(uint8_t len)
     }
 }
 
+// Process any incoming commands
+void
+console_task(void)
+{
+    uint8_t buf_len, pop_count;
+    char *buf = console_get_input(&buf_len);
+    int8_t ret = command_find_block(buf, buf_len, &pop_count);
+    if (ret > 0)
+        command_dispatch(buf, pop_count);
+    if (ret)
+        console_pop_input(pop_count);
+}
+DECL_TASK(console_task);
+
 // Return an output buffer that the caller may fill with transmit messages
 char *
 console_get_output(uint8_t len)
diff --git a/src/simulator/main.c b/src/simulator/main.c
index 47dd49f4b..bdad1a6a4 100644
--- a/src/simulator/main.c
+++ b/src/simulator/main.c
@@ -85,19 +85,6 @@ timer_read_time(void)
  * Turn stdin/stdout into serial console
  ****************************************************************/
 
-// XXX
-char *
-console_get_input(uint8_t *plen)
-{
-    *plen = 0;
-    return NULL;
-}
-
-void
-console_pop_input(uint8_t len)
-{
-}
-
 // Return an output buffer that the caller may fill with transmit messages
 char *
 console_get_output(uint8_t len)