You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by ma...@apache.org on 2016/01/07 20:13:31 UTC

[1/2] incubator-mynewt-larva git commit: Add option for native target to log all UART comm to a file.

Repository: incubator-mynewt-larva
Updated Branches:
  refs/heads/master 10c2873c3 -> 0554811fa


Add option for native target to log all UART comm to a file.


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

Branch: refs/heads/master
Commit: 95e42e808f240ae6740aa8b4c8cbe3a5d688456f
Parents: 10c2873
Author: Marko Kiiskila <ma...@runtime.io>
Authored: Thu Jan 7 11:10:42 2016 -0800
Committer: Marko Kiiskila <ma...@runtime.io>
Committed: Thu Jan 7 11:10:42 2016 -0800

----------------------------------------------------------------------
 hw/mcu/native/include/mcu/mcu_sim.h |  1 +
 hw/mcu/native/src/hal_system.c      | 10 +++--
 hw/mcu/native/src/hal_uart.c        | 75 ++++++++++++++++++++++++++++++++
 3 files changed, 83 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/95e42e80/hw/mcu/native/include/mcu/mcu_sim.h
----------------------------------------------------------------------
diff --git a/hw/mcu/native/include/mcu/mcu_sim.h b/hw/mcu/native/include/mcu/mcu_sim.h
index 7ab0842..33ea83c 100644
--- a/hw/mcu/native/include/mcu/mcu_sim.h
+++ b/hw/mcu/native/include/mcu/mcu_sim.h
@@ -17,6 +17,7 @@
 #define __MCU_SIM_H__
 
 extern char *native_flash_file;
+extern char *native_uart_log_file;
 
 void mcu_sim_parse_args(int argc, char **argv);
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/95e42e80/hw/mcu/native/src/hal_system.c
----------------------------------------------------------------------
diff --git a/hw/mcu/native/src/hal_system.c b/hw/mcu/native/src/hal_system.c
index eb64b60..191ce8f 100644
--- a/hw/mcu/native/src/hal_system.c
+++ b/hw/mcu/native/src/hal_system.c
@@ -36,9 +36,10 @@ static void
 usage(char *progname, int rc)
 {
     const char msg[] =
-      "Usage: %s [-f flash_file]\n"
+      "Usage: %s [-f flash_file] [-u uart_log_file]\n"
       "     -f flash_file tells where binary flash file is located. It gets\n"
-      "        created if it doesn't already exist.\n";
+      "        created if it doesn't already exist.\n"
+      "     -u uart_log_file puts all UART data exchanges into a logfile.\n";
 
     write(2, msg, strlen(msg));
     exit(rc);
@@ -50,11 +51,14 @@ mcu_sim_parse_args(int argc, char **argv)
     int ch;
     char *progname = argv[0];
 
-    while ((ch = getopt(argc, argv, "hf:")) != -1) {
+    while ((ch = getopt(argc, argv, "hf:u:")) != -1) {
         switch (ch) {
         case 'f':
             native_flash_file = optarg;
             break;
+        case 'u':
+            native_uart_log_file = optarg;
+            break;
         case 'h':
             usage(progname, 0);
             break;

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/95e42e80/hw/mcu/native/src/hal_uart.c
----------------------------------------------------------------------
diff --git a/hw/mcu/native/src/hal_uart.c b/hw/mcu/native/src/hal_uart.c
index 2f66016..196ac6c 100644
--- a/hw/mcu/native/src/hal_uart.c
+++ b/hw/mcu/native/src/hal_uart.c
@@ -24,6 +24,7 @@
 #ifdef MN_OSX
 #include <util.h>
 #endif
+#include <ctype.h>
 #include <stdio.h>
 #include <fcntl.h>
 #include <assert.h>
@@ -49,12 +50,82 @@ struct uart {
  * XXXX should try to use O_ASYNC/SIGIO for byte arrival notification,
  * so we wouldn't need an OS for pseudo ttys.
  */
+char *native_uart_log_file;
+static int uart_log_fd = -1;
+
 static struct uart uarts[UART_CNT];
 static int uart_poller_running;
 static struct os_task uart_poller_task;
 static os_stack_t uart_poller_stack[UART_POLLER_STACK_SZ];
 
 static void
+uart_open_log(void)
+{
+    if (native_uart_log_file && uart_log_fd < 0) {
+        uart_log_fd = open(native_uart_log_file, O_WRONLY | O_CREAT | O_TRUNC,
+          0666);
+        assert(uart_log_fd >= 0);
+    }
+}
+
+static void
+uart_log_data(struct uart *u, int istx, uint8_t data)
+{
+    static struct {
+        struct uart *uart;
+        int istx;
+        uint32_t time;
+        int chars_in_line;
+    } state = {
+        .uart = NULL,
+        .istx = 0
+    };
+    uint32_t now;
+    char tmpbuf[32];
+    int len;
+
+    if (uart_log_fd < 0) {
+        return;
+    }
+    now = os_time_get();
+    if (state.uart) {
+        if (u != state.uart || now != state.time || istx != state.istx) {
+            /*
+             * End current printout.
+             */
+            if (write(uart_log_fd, "\n", 1) != 1) {
+                assert(0);
+            }
+            state.uart = NULL;
+        } else {
+            if (state.chars_in_line == 8) {
+                if (write(uart_log_fd, "\n\t", 2) != 2) {
+                    assert(0);
+                }
+                state.chars_in_line = 0;
+            }
+            len = snprintf(tmpbuf, sizeof(tmpbuf), "%c (%02x) ",
+              isalnum(data) ? data : '?', data);
+            if (write(uart_log_fd, tmpbuf, len) != len) {
+                assert(0);
+            }
+            state.chars_in_line++;
+        }
+    }
+    if (u && state.uart == NULL) {
+        len = snprintf(tmpbuf, sizeof(tmpbuf), "%u:uart%d %s\n\t%c (%02x) ",
+          now, u - uarts, istx ? "tx" : "rx", isalnum(data) ? data : '?', data);
+        if (write(uart_log_fd, tmpbuf, len) != len) {
+            assert(0);
+        }
+        state.chars_in_line = 1;
+        state.uart = u;
+        state.istx = istx;
+        state.time = now;
+    }
+}
+
+static void
 uart_poller(void *arg)
 {
     int i;
@@ -86,6 +157,7 @@ uart_poller(void *arg)
                         OS_EXIT_CRITICAL(sr);
                         break;
                     }
+                    uart_log_data(uart, 1, ch);
                     OS_EXIT_CRITICAL(sr);
                     ch = rc;
                     rc = write(uart->u_fd, &ch, 1);
@@ -110,6 +182,7 @@ uart_poller(void *arg)
                 }
                 if (uart->u_rx_char >= 0) {
                     OS_ENTER_CRITICAL(sr);
+                    uart_log_data(uart, 0, uart->u_rx_char);
                     rc = uart->u_rx_func(uart->u_func_arg, uart->u_rx_char);
                     /* Delivered */
                     if (rc >= 0) {
@@ -119,6 +192,7 @@ uart_poller(void *arg)
                 }
             }
         }
+        uart_log_data(NULL, 0, 0);
         os_time_delay(10);
     }
 }
@@ -274,6 +348,7 @@ hal_uart_config(int port, int32_t baudrate, uint8_t databits, uint8_t stopbits,
     }
     set_nonblock(uart->u_fd);
 
+    uart_open_log();
     uart->u_open = 1;
     return 0;
 }


[2/2] incubator-mynewt-larva git commit: Include packet length when sending newtmgr response back over serial. Send a newline at the end of response output.

Posted by ma...@apache.org.
Include packet length when sending newtmgr response back over serial.
Send a newline at the end of response output.


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

Branch: refs/heads/master
Commit: 0554811faeb02b30120d7fc787cae70418a25bdc
Parents: 95e42e8
Author: Marko Kiiskila <ma...@runtime.io>
Authored: Thu Jan 7 11:11:40 2016 -0800
Committer: Marko Kiiskila <ma...@runtime.io>
Committed: Thu Jan 7 11:11:40 2016 -0800

----------------------------------------------------------------------
 libs/shell/src/shell.c | 46 ++++++++++++++++++++++-----------------------
 1 file changed, 23 insertions(+), 23 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/0554811f/libs/shell/src/shell.c
----------------------------------------------------------------------
diff --git a/libs/shell/src/shell.c b/libs/shell/src/shell.c
index 15304ee..80c578a 100644
--- a/libs/shell/src/shell.c
+++ b/libs/shell/src/shell.c
@@ -228,64 +228,64 @@ shell_nlip_mtx(struct os_mbuf *m)
 #define SHELL_NLIP_MTX_BUF_SIZE (12)
     uint8_t readbuf[SHELL_NLIP_MTX_BUF_SIZE];
     char encodebuf[BASE64_ENCODE_SIZE(SHELL_NLIP_MTX_BUF_SIZE)];
-    uint8_t esc_seq[2];
-    uint8_t pkt_seq[2];
+    char pkt_seq[2] = { SHELL_NLIP_PKT_START1, SHELL_NLIP_PKT_START2 };
+    char esc_seq[2] = { SHELL_NLIP_DATA_START1, SHELL_NLIP_DATA_START2 };
     uint16_t totlen;
     uint16_t dlen;
     uint16_t off;
+    int rb_off;
     int elen;
     uint16_t nwritten;
     int rc;
 
     /* Convert the mbuf into a packet.
      *
-     * starts with 06 09 
+     * starts with 06 09
      * base64 encode:
-     *  - total packet length (uint16_t) 
-     *  - data 
-     * base64 encoded data must be less than 122 bytes per line to 
+     *  - total packet length (uint16_t)
+     *  - data
+     * base64 encoded data must be less than 122 bytes per line to
      * avoid overflows and adhere to convention.
      *
-     * continuation packets are preceded by 04 20 until the entire 
-     * buffer has been sent. 
+     * continuation packets are preceded by 04 20 until the entire
+     * buffer has been sent.
      */
     totlen = OS_MBUF_PKTHDR(m)->omp_len;
     nwritten = 0;
     off = 0;
 
-    pkt_seq[0] = SHELL_NLIP_PKT_START1;
-    pkt_seq[1] = SHELL_NLIP_PKT_START2;
-
-    esc_seq[0] = SHELL_NLIP_DATA_START1;
-    esc_seq[1] = SHELL_NLIP_DATA_START2;
-
     /* Start a packet */
-    console_write((char *) pkt_seq, sizeof(pkt_seq));
+    console_write(pkt_seq, sizeof(pkt_seq));
+
+    rb_off = 2;
+    dlen = htons(totlen);
+    memcpy(readbuf, &dlen, sizeof(dlen));
 
     while (totlen > 0) {
-        dlen = min(SHELL_NLIP_MTX_BUF_SIZE, totlen);
+        dlen = min(SHELL_NLIP_MTX_BUF_SIZE - rb_off, totlen);
 
-        if (nwritten != 0 && 
+        if (nwritten != 0 &&
                 ((nwritten + BASE64_ENCODE_SIZE(dlen)) % 122) == 0) {
-            console_write("\n", sizeof("\n")-1);
-            console_write((char *) esc_seq, sizeof(esc_seq));
+            console_write("\n", 1);
+            console_write(esc_seq, sizeof(esc_seq));
         }
 
-        rc = os_mbuf_copydata(m, off, dlen, readbuf);
+        rc = os_mbuf_copydata(m, off, dlen, readbuf + rb_off);
         if (rc != 0) {
             goto err;
         }
         off += dlen;
 
-        elen = base64_encode(readbuf, dlen, encodebuf, 
+        elen = base64_encode(readbuf, dlen + rb_off, encodebuf,
                 totlen - dlen > 0 ? 0 : 1);
+        rb_off = 0;
 
         console_write(encodebuf, elen);
 
         nwritten += elen;
         totlen -= dlen;
-        
     }
+    console_write("\n", 1);
 
     return (0);
 err:
@@ -390,7 +390,7 @@ shell_task_func(void *arg)
 
     os_eventq_init(&shell_evq);
     os_mqueue_init(&g_shell_nlip_mq, NULL);
-    
+
     console_rdy_ev.ev_type = OS_EVENT_T_CONSOLE_RDY;
 
     while (1) {