You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by cc...@apache.org on 2019/05/31 19:48:17 UTC

[mynewt-core] branch master updated (02ff200 -> e4c46a7)

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

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


    from 02ff200  [Feature]: Main Task OS Sanity Check (#1838)
     new 31f54a1  kernel/os: Fn to convert OS_ codes to SYS_E codes
     new b9e9544  encoding/tinycbor: encode indefinite text strings
     new 385e381  sys/console: console_vprintf
     new 15d9910  util/streamer: generic data streaming interface
     new e178c3d  sys/shell: Use the util/streamer interface
     new f6f145b  hw/battery: Fix shell command definitions
     new 4d37e68  sys/shell: shell-newtmgr bridge
     new 65c4723  sys/shell: Change the OS cmds to extended
     new 84faab7  sys/stats: Change the stats shell cmds to extended
     new e4c46a7  flash_test: Change shell cmds to extended

The 10 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 encoding/tinycbor/include/tinycbor/cbor.h          |   1 +
 encoding/tinycbor/src/cborencoder.c                |  14 ++
 hw/battery/src/battery_shell.c                     |  12 +-
 kernel/os/include/os/os_error.h                    |  10 +
 kernel/os/{include/os/mynewt.h => src/os_error.c}  |  30 +--
 mgmt/mgmt/include/mgmt/mgmt.h                      |   1 +
 sys/console/full/include/console/console.h         |   1 +
 sys/console/full/src/console_fmt.c                 |  54 +++--
 sys/console/minimal/include/console/console.h      |   6 +
 sys/console/stub/include/console/console.h         |   6 +
 sys/shell/include/shell/shell.h                    |  66 +++++-
 sys/shell/pkg.yml                                  |   4 +
 sys/shell/src/shell.c                              | 226 +++++++++++++--------
 sys/shell/src/shell_bridge.c                       | 109 ++++++++++
 sys/shell/src/shell_bridge_streamer.c              |  81 ++++++++
 sys/shell/src/shell_os.c                           |  95 ++++-----
 sys/shell/src/shell_priv.h                         |  18 ++
 sys/shell/src/shell_prompt.c                       |   2 +-
 sys/shell/syscfg.yml                               |  16 ++
 sys/stats/full/src/stats_shell.c                   |  44 ++--
 test/flash_test/src/flash_test.c                   |  94 +++++----
 util/streamer/include/streamer/streamer.h          | 108 ++++++++++
 util/{taskpool => streamer}/pkg.yml                |   8 +-
 .../hal_flash.c => util/streamer/src/streamer.c    |  32 +--
 .../streamer/src/streamer_console.c                |  37 ++--
 util/streamer/src/streamer_mbuf.c                  |  99 +++++++++
 {fs/fatfs => util/streamer}/syscfg.yml             |  11 +-
 27 files changed, 888 insertions(+), 297 deletions(-)
 copy kernel/os/{include/os/mynewt.h => src/os_error.c} (61%)
 create mode 100644 sys/shell/src/shell_bridge.c
 create mode 100644 sys/shell/src/shell_bridge_streamer.c
 create mode 100644 util/streamer/include/streamer/streamer.h
 copy util/{taskpool => streamer}/pkg.yml (90%)
 copy hw/mcu/stm/stm32f0xx/src/hal_flash.c => util/streamer/src/streamer.c (63%)
 copy sys/console/full/src/ticks.c => util/streamer/src/streamer_console.c (57%)
 create mode 100644 util/streamer/src/streamer_mbuf.c
 copy {fs/fatfs => util/streamer}/syscfg.yml (83%)


[mynewt-core] 08/10: sys/shell: Change the OS cmds to extended

Posted by cc...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit 65c47238a5c3d7d5c6e931376f4e7e630ac59cc4
Author: Christopher Collins <cc...@apache.org>
AuthorDate: Fri May 10 18:40:04 2019 -0700

    sys/shell: Change the OS cmds to extended
    
    This allows the OS shell commands to be executed via newtmgr.
---
 sys/shell/src/shell_os.c | 95 +++++++++++++++++++-----------------------------
 1 file changed, 37 insertions(+), 58 deletions(-)

diff --git a/sys/shell/src/shell_os.c b/sys/shell/src/shell_os.c
index a4d0cdf..098e4f4 100644
--- a/sys/shell/src/shell_os.c
+++ b/sys/shell/src/shell_os.c
@@ -26,13 +26,15 @@
 
 #include "hal/hal_system.h"
 #include "hal/hal_nvreg.h"
+#include "streamer/streamer.h"
 #include "shell/shell.h"
 #include "shell_priv.h"
 
 #define SHELL_OS "os"
 
-int
-shell_os_tasks_display_cmd(int argc, char **argv)
+static int
+shell_os_tasks_display_cmd(const struct shell_cmd *cmd, int argc, char **argv,
+                           struct streamer *streamer)
 {
     struct os_task *prev_task;
     struct os_task_info oti;
@@ -46,9 +48,9 @@ shell_os_tasks_display_cmd(int argc, char **argv)
         name = argv[1];
     }
 
-    console_printf("Tasks: \n");
+    streamer_printf(streamer, "Tasks: \n");
     prev_task = NULL;
-    console_printf("%8s %3s %3s %8s %8s %8s %8s %8s %8s %3s\n",
+    streamer_printf(streamer, "%8s %3s %3s %8s %8s %8s %8s %8s %8s %3s\n",
       "task", "pri", "tid", "runtime", "csw", "stksz", "stkuse",
       "lcheck", "ncheck", "flg");
     while (1) {
@@ -65,7 +67,7 @@ shell_os_tasks_display_cmd(int argc, char **argv)
             }
         }
 
-        console_printf("%8s %3u %3u %8lu %8lu %8u %8u %8lu %8lu\n",
+        streamer_printf(streamer, "%8s %3u %3u %8lu %8lu %8u %8u %8lu %8lu\n",
                 oti.oti_name, oti.oti_prio, oti.oti_taskid,
                 (unsigned long)oti.oti_runtime, (unsigned long)oti.oti_cswcnt,
                 oti.oti_stksize, oti.oti_stkusage,
@@ -75,14 +77,15 @@ shell_os_tasks_display_cmd(int argc, char **argv)
     }
 
     if (name && !found) {
-        console_printf("Couldn't find task with name %s\n", name);
+        streamer_printf(streamer, "Couldn't find task with name %s\n", name);
     }
 
     return 0;
 }
 
 int
-shell_os_mpool_display_cmd(int argc, char **argv)
+shell_os_mpool_display_cmd(const struct shell_cmd *cmd, int argc, char **argv,
+                           struct streamer *streamer)
 {
     struct os_mempool *mp;
     struct os_mempool_info omi;
@@ -96,10 +99,10 @@ shell_os_mpool_display_cmd(int argc, char **argv)
         name = argv[1];
     }
 
-    console_printf("Mempools: \n");
+    streamer_printf(streamer, "Mempools: \n");
     mp = NULL;
-    console_printf("%32s %5s %4s %4s %4s\n", "name", "blksz", "cnt", "free",
-                   "min");
+    streamer_printf(streamer, "%32s %5s %4s %4s %4s\n",
+                    "name", "blksz", "cnt", "free", "min");
     while (1) {
         mp = os_mempool_info_get_next(mp, &omi);
         if (mp == NULL) {
@@ -114,13 +117,13 @@ shell_os_mpool_display_cmd(int argc, char **argv)
             }
         }
 
-        console_printf("%32s %5d %4d %4d %4d\n", omi.omi_name,
+        streamer_printf(streamer, "%32s %5d %4d %4d %4d\n", omi.omi_name,
                        omi.omi_block_size, omi.omi_num_blocks,
                        omi.omi_num_free, omi.omi_min_free);
     }
 
     if (name && !found) {
-        console_printf("Couldn't find a memory pool with name %s\n",
+        streamer_printf(streamer, "Couldn't find a memory pool with name %s\n",
                 name);
     }
 
@@ -128,7 +131,8 @@ shell_os_mpool_display_cmd(int argc, char **argv)
 }
 
 int
-shell_os_date_cmd(int argc, char **argv)
+shell_os_date_cmd(const struct shell_cmd *cmd, int argc, char **argv,
+                  struct streamer *streamer)
 {
     int rc;
 #if MYNEWT_VAL(SHELL_OS_DATETIME_CMD)
@@ -146,7 +150,7 @@ shell_os_date_cmd(int argc, char **argv)
         assert(rc == 0);
         rc = datetime_format(&tv, &tz, buf, sizeof(buf));
         assert(rc == 0);
-        console_printf("%s\n", buf);
+        streamer_printf(streamer, "%s\n", buf);
 #endif
     } else if (argc == 1) {
 #if (MYNEWT_VAL(SHELL_OS_DATETIME_CMD) & 2) == 2
@@ -155,7 +159,7 @@ shell_os_date_cmd(int argc, char **argv)
         if (rc == 0) {
             rc = os_settimeofday(&tv, &tz);
         } else {
-            console_printf("Invalid datetime\n");
+            streamer_printf(streamer, "Invalid datetime\n");
         }
 #endif
     } else {
@@ -166,13 +170,14 @@ shell_os_date_cmd(int argc, char **argv)
 }
 
 int
-shell_os_reset_cmd(int argc, char **argv)
+shell_os_reset_cmd(const struct shell_cmd *cmd, int argc, char **argv,
+                   struct streamer *streamer)
 {
 #if MYNEWT_VAL(SHELL_OS_SERIAL_BOOT_NVREG)
     if (argc == 2 && !strcmp(argv[1], "serial_boot")) {
         hal_nvreg_write(MYNEWT_VAL(BOOT_SERIAL_NVREG_INDEX),
                         MYNEWT_VAL(BOOT_SERIAL_NVREG_MAGIC));
-        console_printf("serial_boot mode\n");
+        streamer_printf(streamer, "serial_boot mode\n");
     }
 #endif
     os_time_delay(OS_TICKS_PER_SEC / 10);
@@ -183,16 +188,20 @@ shell_os_reset_cmd(int argc, char **argv)
 static int
 shell_os_ls_dev(struct os_dev *dev, void *arg)
 {
-    console_printf("%4d %3x %s\n",
-                   dev->od_open_ref, dev->od_flags, dev->od_name);
+    struct streamer *streamer;
+
+    streamer = arg;
+    streamer_printf(streamer, "%4d %3x %s\n",
+                    dev->od_open_ref, dev->od_flags, dev->od_name);
     return 0;
 }
 
 int
-shell_os_ls_dev_cmd(int argc, char **argv)
+shell_os_ls_dev_cmd(const struct shell_cmd *cmd, int argc, char **argv,
+                    struct streamer *streamer)
 {
-    console_printf("%4s %3s %s\n", "ref", "flg", "name");
-    os_dev_walk(shell_os_ls_dev, NULL);
+    streamer_printf(streamer, "%4s %3s %s\n", "ref", "flg", "name");
+    os_dev_walk(shell_os_ls_dev, streamer);
     return 0;
 }
 
@@ -253,42 +262,12 @@ static const struct shell_cmd_help ls_dev_help = {
 #endif
 
 static const struct shell_cmd os_commands[] = {
-    {
-        .sc_cmd = "tasks",
-        .sc_cmd_func = shell_os_tasks_display_cmd,
-#if MYNEWT_VAL(SHELL_CMD_HELP)
-        .help = &tasks_help,
-#endif
-    },
-    {
-        .sc_cmd = "mpool",
-        .sc_cmd_func = shell_os_mpool_display_cmd,
-#if MYNEWT_VAL(SHELL_CMD_HELP)
-        .help = &mpool_help,
-#endif
-    },
-    {
-        .sc_cmd = "date",
-        .sc_cmd_func = shell_os_date_cmd,
-#if MYNEWT_VAL(SHELL_CMD_HELP)
-        .help = &date_help,
-#endif
-    },
-    {
-        .sc_cmd = "reset",
-        .sc_cmd_func = shell_os_reset_cmd,
-#if MYNEWT_VAL(SHELL_CMD_HELP)
-        .help = &reset_help,
-#endif
-    },
-    {
-        .sc_cmd = "lsdev",
-        .sc_cmd_func = shell_os_ls_dev_cmd,
-#if MYNEWT_VAL(SHELL_CMD_HELP)
-        .help = &ls_dev_help,
-#endif
-    },
-    { NULL, NULL, NULL },
+    SHELL_CMD_EXT("tasks", shell_os_tasks_display_cmd, &tasks_help),
+    SHELL_CMD_EXT("mpool", shell_os_mpool_display_cmd, &mpool_help),
+    SHELL_CMD_EXT("date", shell_os_date_cmd, &date_help),
+    SHELL_CMD_EXT("reset", shell_os_reset_cmd, &reset_help),
+    SHELL_CMD_EXT("lsdev", shell_os_ls_dev_cmd, &ls_dev_help),
+    { 0 },
 };
 
 void


[mynewt-core] 03/10: sys/console: console_vprintf

Posted by cc...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit 385e381d9783ae430bcdf4580df44c87d15d97e9
Author: Christopher Collins <cc...@apache.org>
AuthorDate: Fri May 10 12:54:21 2019 -0700

    sys/console: console_vprintf
---
 sys/console/full/include/console/console.h    |  1 +
 sys/console/full/src/console_fmt.c            | 54 +++++++++++++--------------
 sys/console/minimal/include/console/console.h |  6 +++
 sys/console/stub/include/console/console.h    |  6 +++
 4 files changed, 39 insertions(+), 28 deletions(-)

diff --git a/sys/console/full/include/console/console.h b/sys/console/full/include/console/console.h
index 08f166f..ac15e5b 100644
--- a/sys/console/full/include/console/console.h
+++ b/sys/console/full/include/console/console.h
@@ -63,6 +63,7 @@ void console_blocking_mode(void);
 void console_non_blocking_mode(void);
 void console_echo(int on);
 
+int console_vprintf(const char *fmt, va_list ap);
 int console_printf(const char *fmt, ...)
     __attribute__ ((format (printf, 1, 2)));;
 
diff --git a/sys/console/full/src/console_fmt.c b/sys/console/full/src/console_fmt.c
index 89c624d..8b16e7c 100644
--- a/sys/console/full/src/console_fmt.c
+++ b/sys/console/full/src/console_fmt.c
@@ -24,21 +24,11 @@
 
 #define CONS_OUTPUT_MAX_LINE    128
 
-
 #if MYNEWT_VAL(BASELIBC_PRESENT)
 
-/**
- * Prints the specified format string to the console.
- *
- * @return                      The number of characters that would have been
- *                                  printed if the console buffer were
- *                                  unlimited.  This return value is analogous
- *                                  to that of snprintf.
- */
 int
-console_printf(const char *fmt, ...)
+console_vprintf(const char *fmt, va_list ap)
 {
-    va_list args;
     int num_chars;
 
     num_chars = 0;
@@ -50,28 +40,16 @@ console_printf(const char *fmt, ...)
         }
     }
 
-    va_start(args, fmt);
-    num_chars += vprintf(fmt, args);
-    va_end(args);
+    num_chars += vprintf(fmt, ap);
 
     return num_chars;
 }
 
-
 #else
 
-/**
- * Prints the specified format string to the console.
- *
- * @return                      The number of characters that would have been
- *                                  printed if the console buffer were
- *                                  unlimited.  This return value is analogous
- *                                  to that of snprintf.
- */
 int
-console_printf(const char *fmt, ...)
+console_vprintf(const char *fmt, va_list ap)
 {
-    va_list args;
     char buf[CONS_OUTPUT_MAX_LINE];
     int num_chars;
     int len;
@@ -88,15 +66,35 @@ console_printf(const char *fmt, ...)
         }
     }
 
-    va_start(args, fmt);
-    len = vsnprintf(buf, sizeof(buf), fmt, args);
+    len = vsnprintf(buf, sizeof(buf), fmt, ap);
     num_chars += len;
     if (len >= sizeof(buf)) {
         len = sizeof(buf) - 1;
     }
     console_write(buf, len);
-    va_end(args);
 
     return num_chars;
 }
+
 #endif
+
+/**
+ * Prints the specified format string to the console.
+ *
+ * @return                      The number of characters that would have been
+ *                                  printed if the console buffer were
+ *                                  unlimited.  This return value is analogous
+ *                                  to that of snprintf.
+ */
+int
+console_printf(const char *fmt, ...)
+{
+    va_list args;
+    int num_chars;
+
+    va_start(args, fmt);
+    num_chars = console_vprintf(fmt, args);
+    va_end(args);
+
+    return num_chars;
+}
diff --git a/sys/console/minimal/include/console/console.h b/sys/console/minimal/include/console/console.h
index b05bebd..df1d2da 100644
--- a/sys/console/minimal/include/console/console.h
+++ b/sys/console/minimal/include/console/console.h
@@ -47,6 +47,12 @@ void console_blocking_mode(void);
 void console_non_blocking_mode(void);
 void console_echo(int on);
 
+static int inline
+console_vprintf(const char *fmt, va_list ap)
+{
+    return 0;
+}
+
 static int console_printf(const char *fmt, ...)
     __attribute__ ((format (printf, 1, 2)));;
 static int inline
diff --git a/sys/console/stub/include/console/console.h b/sys/console/stub/include/console/console.h
index b1b6f48..7d08fe8 100644
--- a/sys/console/stub/include/console/console.h
+++ b/sys/console/stub/include/console/console.h
@@ -86,6 +86,12 @@ console_echo(int on)
 {
 }
 
+static int inline
+console_vprintf(const char *fmt, va_list ap)
+{
+    return 0;
+}
+
 static int inline console_printf(const char *fmt, ...)
     __attribute__ ((format (printf, 1, 2)));
 


[mynewt-core] 05/10: sys/shell: Use the util/streamer interface

Posted by cc...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit e178c3d79cf526fbc146bce25f3a1e15b15a51b8
Author: Christopher Collins <cc...@apache.org>
AuthorDate: Fri May 10 18:51:50 2019 -0700

    sys/shell: Use the util/streamer interface
    
    This change allows shell commands to send the output somewhere other
    than the console.  This is in preparation for the ability to invoke
    shell commands via newtmgr.
---
 sys/shell/include/shell/shell.h |  66 +++++++++++-
 sys/shell/pkg.yml               |   1 +
 sys/shell/src/shell.c           | 226 ++++++++++++++++++++++++----------------
 sys/shell/src/shell_priv.h      |  18 ++++
 sys/shell/src/shell_prompt.c    |   2 +-
 5 files changed, 223 insertions(+), 90 deletions(-)

diff --git a/sys/shell/include/shell/shell.h b/sys/shell/include/shell/shell.h
index 859b9fa..8eecbf5 100644
--- a/sys/shell/include/shell/shell.h
+++ b/sys/shell/include/shell/shell.h
@@ -25,8 +25,14 @@ extern "C" {
 #endif
 
 #include "os/mynewt.h"
+#include "streamer/streamer.h"
 
 struct os_eventq;
+struct streamer;
+struct shell_cmd;
+
+/** Command IDs in the "shell" newtmgr group. */
+#define SHELL_NMGR_OP_EXEC      0
 
 /** @brief Callback called when command is entered.
  *
@@ -37,6 +43,21 @@ struct os_eventq;
  */
 typedef int (*shell_cmd_func_t)(int argc, char *argv[]);
 
+/**
+ * @brief Callback for "extended" shell commands.
+ *
+ * @param cmd                   The shell command being executed.
+ * @param argc                  Number of arguments passed.
+ * @param argv                  Array of option strings. First option is always
+ *                                  command name.
+ * @param streamer              The streamer to write shell output to.
+ *
+ * @return                      0 on success; SYS_E[...] on failure.
+ */
+typedef int (*shell_cmd_ext_func_t)(const struct shell_cmd *cmd,
+                                    int argc, char *argv[],
+                                    struct streamer *streamer);
+
 struct shell_param {
     const char *param_name;
     const char *help;
@@ -49,8 +70,13 @@ struct shell_cmd_help {
 };
 
 struct shell_cmd {
+    uint8_t sc_ext : 1; /* 1 if this is an extended shell comand. */
+    union {
+        shell_cmd_func_t sc_cmd_func;
+        shell_cmd_ext_func_t sc_cmd_ext_func;
+    };
+
     const char *sc_cmd;
-    shell_cmd_func_t sc_cmd_func;
     const struct shell_cmd_help *help;
 };
 
@@ -59,6 +85,32 @@ struct shell_module {
     const struct shell_cmd *commands;
 };
 
+#if MYNEWT_VAL(SHELL_CMD_HELP)
+#define SHELL_HELP_(help_) (help_)
+#else
+#define SHELL_HELP_(help_)
+#endif
+
+/**
+ * @brief constructs a legacy shell command.
+ */
+#define SHELL_CMD(cmd_, func_, help_) {         \
+    .sc_ext = 0,                                \
+    .sc_cmd_func = func_,                       \
+    .sc_cmd = cmd_,                             \
+    .help = SHELL_HELP_(help_),                 \
+}
+
+/**
+ * @brief constructs an extended shell command.
+ */
+#define SHELL_CMD_EXT(cmd_, func_, help_) {     \
+    .sc_ext = 1,                                \
+    .sc_cmd_ext_func = func_,                   \
+    .sc_cmd = cmd_,                             \
+    .help = SHELL_HELP_(help_),                 \
+}
+
 /** @brief Register a shell_module object
  *
  *  @param shell_name Module name to be entered in shell console.
@@ -101,6 +153,18 @@ void shell_register_default_module(const char *name);
  */
 void shell_evq_set(struct os_eventq *evq);
 
+/**
+ * @brief Processes a set of arguments and executes their corresponding shell
+ * command.
+ *
+ * @param argc                  The argument count (including command name).
+ * @param argv                  The argument list ([0] is command name).
+ * @param streamer              The streamer to send output to.
+ *
+ * @return                      0 on success; SYS_E[...] on failure.
+ */
+int shell_exec(int argc, char **argv, struct streamer *streamer);
+
 #if MYNEWT_VAL(SHELL_NEWTMGR)
 struct os_mbuf;
 typedef int (*shell_nlip_input_func_t)(struct os_mbuf *, void *arg);
diff --git a/sys/shell/pkg.yml b/sys/shell/pkg.yml
index 6c6f6cf..cf009a8 100644
--- a/sys/shell/pkg.yml
+++ b/sys/shell/pkg.yml
@@ -26,6 +26,7 @@ pkg.keywords:
 pkg.deps:
     - "@apache-mynewt-core/kernel/os"
     - "@apache-mynewt-core/time/datetime"
+    - "@apache-mynewt-core/util/streamer"
 
 pkg.deps.SHELL_NEWTMGR:
     - "@apache-mynewt-core/mgmt/mgmt"
diff --git a/sys/shell/src/shell.c b/sys/shell/src/shell.c
index 335564e..b63fe94 100644
--- a/sys/shell/src/shell.c
+++ b/sys/shell/src/shell.c
@@ -23,6 +23,8 @@
 
 #include "os/mynewt.h"
 #include "console/console.h"
+#include "streamer/streamer.h"
+#include "modlog/modlog.h"
 #include "shell/shell.h"
 #include "shell_priv.h"
 
@@ -34,7 +36,7 @@ static size_t num_of_shell_entities;
 static const char *prompt;
 static int default_module = -1;
 
-static shell_cmd_func_t app_cmd_handler;
+static struct shell_cmd app_cmd;
 static shell_prompt_function_t app_prompt_handler;
 
 /* Shared queue for shell events to be processed */
@@ -76,8 +78,16 @@ print_prompt(void)
     console_printf("%s%s", get_prompt(), MYNEWT_VAL(SHELL_PROMPT_SUFFIX));
 }
 
+static void
+print_prompt_if_console(struct streamer *streamer)
+{
+    if (streamer == streamer_console_get()) {
+        print_prompt();
+    }
+}
+
 static size_t
-line2argv(char *str, char *argv[], size_t size)
+line2argv(char *str, char *argv[], size_t size, struct streamer *streamer)
 {
     size_t argc = 0;
 
@@ -109,7 +119,8 @@ line2argv(char *str, char *argv[], size_t size)
         argv[argc++] = str;
 
         if (argc == size) {
-            console_printf("Too many parameters (max %zu)\n", size - 1);
+            streamer_printf(streamer, "Too many parameters (max %zu)\n",
+                            size - 1);
             return 0;
         }
     }
@@ -144,24 +155,24 @@ get_destination_module(const char *module_str, int len)
  * If a default module was selected: argv[0] = command name
  */
 static const char *
-get_command_and_module(char *argv[], int *module)
+get_command_and_module(char *argv[], int *module, struct streamer *streamer)
 {
     *module = -1;
 
     if (!argv[0]) {
-        console_printf("Unrecognized command\n");
+        streamer_printf(streamer, "Unrecognized command\n");
         return NULL;
     }
 
     if (default_module == -1) {
         if (!argv[1] || argv[1][0] == '\0') {
-            console_printf("Unrecognized command: %s\n", argv[0]);
+            streamer_printf(streamer, "Unrecognized command: %s\n", argv[0]);
             return NULL;
         }
 
         *module = get_destination_module(argv[0], -1);
         if (*module == -1) {
-            console_printf("Illegal module %s\n", argv[0]);
+            streamer_printf(streamer, "Illegal module %s\n", argv[0]);
             return NULL;
         }
 
@@ -173,7 +184,8 @@ get_command_and_module(char *argv[], int *module)
 }
 
 static void
-print_command_params(const int module, const int command)
+print_command_params(const int module, const int command,
+                     struct streamer *streamer)
 {
 	const struct shell_module *shell_module = &shell_modules[module];
 	const struct shell_cmd *shell_cmd = &shell_module->commands[command];
@@ -184,13 +196,14 @@ print_command_params(const int module, const int command)
 	}
 
 	for (i = 0; shell_cmd->help->params[i].param_name; i++) {
-		console_printf("%-30s%s\n", shell_cmd->help->params[i].param_name,
-			       shell_cmd->help->params[i].help);
+		streamer_printf(streamer, "%-30s%s\n",
+                        shell_cmd->help->params[i].param_name,
+			            shell_cmd->help->params[i].help);
 	}
 }
 
 static int
-show_cmd_help(char *argv[])
+show_cmd_help(char *argv[], struct streamer *streamer)
 {
     const char *command = NULL;
     int module = -1;
@@ -198,7 +211,7 @@ show_cmd_help(char *argv[])
     const struct shell_cmd *cmd;
     int i;
 
-    command = get_command_and_module(argv, &module);
+    command = get_command_and_module(argv, &module, streamer);
     if ((module == -1) || (command == NULL)) {
         return 0;
     }
@@ -208,73 +221,75 @@ show_cmd_help(char *argv[])
         cmd = &shell_module->commands[i];
 
         if (!strcmp(command, cmd->sc_cmd)) {
-
             if (!cmd->help || (!cmd->help->summary &&
                                !cmd->help->usage &&
                                !cmd->help->params)) {
-                console_printf("(no help available)\n");
+                streamer_printf(streamer, "(no help available)\n");
                 return 0;
             }
 
             if (cmd->help->summary) {
-                console_printf("Summary:\n");
-                console_printf("%s\n", cmd->help->summary);
+                streamer_printf(streamer, "Summary:\n");
+                streamer_printf(streamer, "%s\n", cmd->help->summary);
             }
 
             if (cmd->help->usage) {
-                console_printf("Usage:\n");
-                console_printf("%s\n", cmd->help->usage);
+                streamer_printf(streamer, "Usage:\n");
+                streamer_printf(streamer, "%s\n", cmd->help->usage);
             }
 
             if (cmd->help->params) {
-                console_printf("Parameters:\n");
-                print_command_params(module, i);
+                streamer_printf(streamer, "Parameters:\n");
+                print_command_params(module, i, streamer);
             }
 
             return 0;
         }
     }
 
-    console_printf("Unrecognized command: %s\n", argv[0]);
+    streamer_printf(streamer, "Unrecognized command: %s\n", argv[0]);
     return 0;
 }
 
 static void
-print_modules(void)
+print_modules(struct streamer *streamer)
 {
     int module;
 
     for (module = 0; module < num_of_shell_entities; module++) {
-        console_printf("%s\n", shell_modules[module].name);
+        streamer_printf(streamer, "%s\n", shell_modules[module].name);
     }
 }
 
 static void
-print_module_commands(const int module)
+print_module_commands(const int module, struct streamer *streamer)
 {
     const struct shell_module *shell_module = &shell_modules[module];
     int i;
 
-    console_printf("help\n");
+    streamer_printf(streamer, "help\n");
 
     for (i = 0; shell_module->commands[i].sc_cmd; i++) {
-        console_printf("%-30s", shell_module->commands[i].sc_cmd);
+        streamer_printf(streamer, "%-30s", shell_module->commands[i].sc_cmd);
         if (shell_module->commands[i].help &&
             shell_module->commands[i].help->summary) {
-        console_printf("%s", shell_module->commands[i].help->summary);
+        
+            streamer_printf(streamer, "%s",
+                            shell_module->commands[i].help->summary);
         }
-        console_printf("\n");
+        streamer_printf(streamer, "\n");
     }
 }
 
 static int
-show_help(int argc, char *argv[])
+show_help(const struct shell_cmd *cmd, int argc, char *argv[],
+          struct streamer *streamer)
 {
     int module;
 
     /* help per command */
     if ((argc > 2) || ((default_module != -1) && (argc == 2))) {
-        return show_cmd_help(&argv[1]);
+        return show_cmd_help(&argv[1], streamer);
     }
 
     /* help per module */
@@ -282,23 +297,27 @@ show_help(int argc, char *argv[])
         if (default_module == -1) {
             module = get_destination_module(argv[1], -1);
             if (module == -1) {
-                console_printf("Illegal module %s\n", argv[1]);
+                streamer_printf(streamer, "Illegal module %s\n", argv[1]);
                 return 0;
             }
         } else {
             module = default_module;
         }
 
-        print_module_commands(module);
+        print_module_commands(module, streamer);
     } else { /* help for all entities */
-        console_printf("Available modules:\n");
-        print_modules();
-        console_printf("To select a module, enter 'select <module name>'.\n");
+        streamer_printf(streamer, "Available modules:\n");
+        print_modules(streamer);
+        streamer_printf(streamer,
+                        "To select a module, enter 'select <module name>'.\n");
     }
 
     return 0;
 }
 
+static const struct shell_cmd shell_cmd_help =
+    SHELL_CMD_EXT("help", show_help, NULL);
+
 static int
 set_default_module(const char *name)
 {
@@ -307,7 +326,6 @@ set_default_module(const char *name)
     module = get_destination_module(name, -1);
 
     if (module == -1) {
-        console_printf("Illegal module %s, default is not changed\n", name);
         return -1;
     }
 
@@ -317,7 +335,8 @@ set_default_module(const char *name)
 }
 
 static int
-select_module(int argc, char *argv[])
+select_module(const struct shell_cmd *cmd, int argc, char *argv[],
+              struct streamer *streamer)
 {
     if (argc == 1) {
         default_module = -1;
@@ -328,8 +347,11 @@ select_module(int argc, char *argv[])
     return 0;
 }
 
-static shell_cmd_func_t
-get_cb(int argc, char *argv[])
+static const struct shell_cmd shell_cmd_select_module =
+    SHELL_CMD_EXT("select", select_module, NULL);
+
+static const struct shell_cmd *
+shell_find_cmd(int argc, char *argv[], struct streamer *streamer)
 {
     const char *first_string = argv[0];
     int module = -1;
@@ -338,24 +360,24 @@ get_cb(int argc, char *argv[])
     int i;
 
     if (!first_string || first_string[0] == '\0') {
-        console_printf("Illegal parameter\n");
+        streamer_printf(streamer, "Illegal parameter\n");
         return NULL;
     }
 
     if (!strcmp(first_string, "help")) {
-        return show_help;
+        return &shell_cmd_help;
     }
 
     if (!strcmp(first_string, "select")) {
-        return select_module;
+        return &shell_cmd_select_module;
     }
 
     if ((argc == 1) && (default_module == -1)) {
-        console_printf("Missing parameter\n");
+        streamer_printf(streamer, "Missing parameter\n");
         return NULL;
     }
 
-    command = get_command_and_module(argv, &module);
+    command = get_command_and_module(argv, &module, streamer);
     if ((module == -1) || (command == NULL)) {
         return NULL;
     }
@@ -363,58 +385,75 @@ get_cb(int argc, char *argv[])
     shell_module = &shell_modules[module];
     for (i = 0; shell_module->commands[i].sc_cmd; i++) {
         if (!strcmp(command, shell_module->commands[i].sc_cmd)) {
-            return shell_module->commands[i].sc_cmd_func;
+            return &shell_module->commands[i];
         }
     }
 
     return NULL;
 }
 
-static void
-shell_process_command(char *line)
+int
+shell_exec(int argc, char **argv, struct streamer *streamer)
 {
-    char *argv[MYNEWT_VAL(SHELL_CMD_ARGC_MAX) + 1];
-    shell_cmd_func_t sc_cmd_func;
+    const struct shell_cmd *cmd;
     size_t argc_offset = 0;
-    size_t argc;
+    int rc;
 
-    argc = line2argv(line, argv, MYNEWT_VAL(SHELL_CMD_ARGC_MAX) + 1);
-    if (!argc) {
-        print_prompt();
-        return;
-    }
-
-    sc_cmd_func = get_cb(argc, argv);
-    if (!sc_cmd_func) {
-        if (app_cmd_handler != NULL) {
-            sc_cmd_func = app_cmd_handler;
+    cmd = shell_find_cmd(argc, argv, streamer);
+    if (!cmd) {
+        if (app_cmd.sc_cmd_func != NULL) {
+            cmd = &app_cmd;
         } else {
-            console_printf("Unrecognized command: %s\n", argv[0]);
-            console_printf("Type 'help' for list of available commands\n");
-            print_prompt();
-            return;
+            streamer_printf(streamer, "Unrecognized command: %s\n", argv[0]);
+            streamer_printf(streamer,
+                            "Type 'help' for list of available commands\n");
+            print_prompt_if_console(streamer);
+            return SYS_ENOENT;
         }
     }
 
     /* Allow invoking a cmd with module name as a prefix; a command should
      * not know how it was invoked (with or without prefix)
      */
-    if (default_module == -1 && sc_cmd_func != select_module &&
-        sc_cmd_func != show_help) {
+    if (default_module == -1 && cmd != &shell_cmd_select_module &&
+        cmd != &shell_cmd_help) {
         argc_offset = 1;
     }
 
     /* Execute callback with arguments */
-    if (sc_cmd_func(argc - argc_offset, &argv[argc_offset]) < 0) {
-        show_cmd_help(argv);
+    if (!cmd->sc_ext) {
+        rc = cmd->sc_cmd_func(argc - argc_offset, &argv[argc_offset]);
+    } else {
+        rc = cmd->sc_cmd_ext_func(cmd, argc - argc_offset, &argv[argc_offset],
+                                  streamer);
+    }
+    if (rc < 0) {
+        show_cmd_help(argv, streamer);
     }
 
-    print_prompt();
+    print_prompt_if_console(streamer);
+
+    return rc;
+}
+
+static void
+shell_process_command(char *line, struct streamer *streamer)
+{
+    char *argv[MYNEWT_VAL(SHELL_CMD_ARGC_MAX) + 1];
+    size_t argc;
+
+    argc = line2argv(line, argv, MYNEWT_VAL(SHELL_CMD_ARGC_MAX) + 1, streamer);
+    if (!argc) {
+        print_prompt_if_console(streamer);
+        return;
+    }
+
+    shell_exec(argc, argv, streamer);
 }
 
 #if MYNEWT_VAL(SHELL_NEWTMGR)
 static void
-shell_process_nlip_line(char *shell_line)
+shell_process_nlip_line(char *shell_line, struct streamer *streamer)
 {
     size_t shell_line_len;
 
@@ -428,10 +467,10 @@ shell_process_nlip_line(char *shell_line)
                 shell_line[1] == SHELL_NLIP_DATA_START2) {
             shell_nlip_process(&shell_line[2], shell_line_len - 2);
         } else {
-            shell_process_command(shell_line);
+            shell_process_command(shell_line, streamer);
         }
     } else {
-        shell_process_command(shell_line);
+        shell_process_command(shell_line, streamer);
     }
 }
 #endif
@@ -440,6 +479,7 @@ static void
 shell(struct os_event *ev)
 {
     struct console_input *cmd;
+    struct streamer *streamer;
 
     if (!ev) {
         print_prompt();
@@ -452,10 +492,12 @@ shell(struct os_event *ev)
         return;
     }
 
+    streamer = streamer_console_get();
+
 #if MYNEWT_VAL(SHELL_NEWTMGR)
-    shell_process_nlip_line(cmd->line);
+    shell_process_nlip_line(cmd->line, streamer);
 #else
-    shell_process_command(cmd->line);
+    shell_process_command(cmd->line, streamer);
 #endif
 
     console_line_event_put(ev);
@@ -752,7 +794,7 @@ complete_select(char *line, char *cur,
             return;
         }
         console_printf("\n");
-        print_modules();
+        print_modules(streamer_console_get());
         print_prompt();
         console_printf("%s", line);
         return;
@@ -788,9 +830,9 @@ completion(char *line, console_append_char_cb append_char)
     if (tok_len == 0) {
         console_printf("\n");
         if (default_module == -1) {
-            print_modules();
+            print_modules(streamer_console_get());
         } else {
-            print_module_commands(default_module);
+            print_module_commands(default_module, streamer_console_get());
         }
         print_prompt();
         console_printf("%s", line);
@@ -827,7 +869,7 @@ completion(char *line, console_append_char_cb append_char)
 
         if (tok_len == 0) {
             console_printf("\n");
-            print_module_commands(module);
+            print_module_commands(module, streamer_console_get());
             print_prompt();
             console_printf("%s", line);
             return;
@@ -849,7 +891,7 @@ completion(char *line, console_append_char_cb append_char)
     tok_len = get_last_token(&cur);
     if (tok_len == 0) {
         console_printf("\n");
-        print_command_params(module, command);
+        print_command_params(module, command, streamer_console_get());
         print_prompt();
         console_printf("%s", line);
         return;
@@ -863,7 +905,7 @@ completion(char *line, console_append_char_cb append_char)
 void
 shell_register_app_cmd_handler(shell_cmd_func_t handler)
 {
-    app_cmd_handler = handler;
+    app_cmd.sc_cmd_func = handler;
 }
 
 void
@@ -899,7 +941,7 @@ int
 shell_register(const char *module_name, const struct shell_cmd *commands)
 {
     if (num_of_shell_entities >= MYNEWT_VAL(SHELL_MAX_MODULES)) {
-        console_printf("Max number of modules reached\n");
+        MODLOG_ERROR(LOG_MODULE_DEFAULT, "Max number of modules reached\n");
         assert(0);
     }
 
@@ -919,22 +961,27 @@ static int module_registered;
 int
 shell_cmd_register(const struct shell_cmd *sc)
 {
+    int rc;
+
     if (num_compat_commands >= MYNEWT_VAL(SHELL_MAX_COMPAT_COMMANDS)) {
-        console_printf("Max number of compat commands reached\n");
+        MODLOG_ERROR(LOG_MODULE_DEFAULT,
+                     "Max number of compat commands reached\n");
         assert(0);
     }
 
     if (!module_registered) {
         shell_register(SHELL_COMPAT_MODULE_NAME, compat_commands);
-        set_default_module(SHELL_COMPAT_MODULE_NAME);
         module_registered = 1;
+
+        rc = set_default_module(SHELL_COMPAT_MODULE_NAME);
+        if (rc != 0) {
+            MODLOG_ERROR(LOG_MODULE_DEFAULT,
+                         "Illegal module %s, default is not changed\n",
+                         SHELL_COMPAT_MODULE_NAME);
+        }
     }
 
-    compat_commands[num_compat_commands].sc_cmd = sc->sc_cmd;
-    compat_commands[num_compat_commands].sc_cmd_func = sc->sc_cmd_func;
-#if MYNEWT_VAL(SHELL_CMD_HELP)
-    compat_commands[num_compat_commands].help = sc->help;
-#endif
+    compat_commands[num_compat_commands] = *sc;
     ++num_compat_commands;
     return 0;
 }
@@ -969,4 +1016,7 @@ shell_init(void)
 #if MYNEWT_VAL(SHELL_PROMPT_MODULE)
     shell_prompt_register();
 #endif
+#if MYNEWT_VAL(SHELL_BRIDGE)
+    shell_bridge_init();
+#endif
 }
diff --git a/sys/shell/src/shell_priv.h b/sys/shell/src/shell_priv.h
index 315ae1c..36a4863 100644
--- a/sys/shell/src/shell_priv.h
+++ b/sys/shell/src/shell_priv.h
@@ -24,7 +24,19 @@
 extern "C" {
 #endif
 
+#include "streamer/streamer.h"
 #include "shell/shell.h"
+struct CborEncoder;
+ 
+#if MYNEWT_VAL(SHELL_BRIDGE)
+/**
+ * Streams CBOR text strings to its encoder.
+ */
+struct shell_bridge_streamer {
+    struct streamer streamer;
+    struct CborEncoder *str_encoder;
+};
+#endif
 
 #if MYNEWT_VAL(SHELL_NEWTMGR)
 #define SHELL_NLIP_PKT_START1 (6)
@@ -40,6 +52,12 @@ void shell_nlip_clear_pkt(void);
 void shell_os_register(void);
 void shell_prompt_register(void);
 
+#if MYNEWT_VAL(SHELL_BRIDGE)
+void shell_bridge_streamer_new(struct shell_bridge_streamer *sbs,
+                               struct CborEncoder *str_encoder);
+int shell_bridge_init(void);
+#endif
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/sys/shell/src/shell_prompt.c b/sys/shell/src/shell_prompt.c
index 0a88500..00a1c18 100644
--- a/sys/shell/src/shell_prompt.c
+++ b/sys/shell/src/shell_prompt.c
@@ -73,7 +73,7 @@ static const struct shell_cmd prompt_commands[] = {
         .help = &ticks_help,
 #endif
     },
-    { NULL, NULL, NULL },
+    { 0 },
 };
 
 


[mynewt-core] 01/10: kernel/os: Fn to convert OS_ codes to SYS_E codes

Posted by cc...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit 31f54a1e4449ec7afc1b471bc81efd713568cfb1
Author: Christopher Collins <cc...@apache.org>
AuthorDate: Thu May 30 14:05:39 2019 -0700

    kernel/os: Fn to convert OS_ codes to SYS_E codes
    
    /**
     * @brief Converts an OS error code (`OS_[...]`) to an equivalent system
     * error code (`SYS_E[...]`).
     *
     * @param os_error              The OS error code to convert.
     *
     * @return                      The equivalent system error code.
     */
    int os_error_to_sys(os_error_t os_error);
---
 kernel/os/include/os/os_error.h                    | 10 +++++
 .../os/{include/os/os_error.h => src/os_error.c}   | 46 +++++++---------------
 2 files changed, 25 insertions(+), 31 deletions(-)

diff --git a/kernel/os/include/os/os_error.h b/kernel/os/include/os/os_error.h
index 01df389..ec26699 100644
--- a/kernel/os/include/os/os_error.h
+++ b/kernel/os/include/os/os_error.h
@@ -45,6 +45,16 @@ enum os_error {
 
 typedef enum os_error os_error_t;
 
+/**
+ * @brief Converts an OS error code (`OS_[...]`) to an equivalent system error
+ * code (`SYS_E[...]`).
+ *
+ * @param os_error              The OS error code to convert.
+ *
+ * @return                      The equivalent system error code.
+ */
+int os_error_to_sys(os_error_t os_error);
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/kernel/os/include/os/os_error.h b/kernel/os/src/os_error.c
similarity index 52%
copy from kernel/os/include/os/os_error.h
copy to kernel/os/src/os_error.c
index 01df389..713ffa4 100644
--- a/kernel/os/include/os/os_error.h
+++ b/kernel/os/src/os_error.c
@@ -17,36 +17,20 @@
  * under the License.
  */
 
-#ifndef H_OS_ERROR_
-#define H_OS_ERROR_
+#include "defs/error.h"
+#include "os/os_error.h"
 
-#include "syscfg/syscfg.h"
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-/* OS error enumerations */
-enum os_error {
-    OS_OK = 0,
-    OS_ENOMEM = 1,
-    OS_EINVAL = 2,
-    OS_INVALID_PARM = 3,
-    OS_MEM_NOT_ALIGNED = 4,
-    OS_BAD_MUTEX = 5,
-    OS_TIMEOUT = 6,
-    OS_ERR_IN_ISR = 7,      /* Function cannot be called from ISR */
-    OS_ERR_PRIV = 8,        /* Privileged access error */
-    OS_NOT_STARTED = 9,     /* OS must be started to call this function, but isn't */
-    OS_ENOENT = 10,         /* No such thing */
-    OS_EBUSY = 11,          /* Resource busy */
-    OS_ERROR = 12,          /* Generic Error */
-};
-
-typedef enum os_error os_error_t;
-
-#ifdef __cplusplus
+int
+os_error_to_sys(os_error_t os_error)
+{
+    switch (os_error) {
+        case OS_OK:             return SYS_EOK;
+        case OS_ENOMEM:         return SYS_ENOMEM; 
+        case OS_EINVAL:         return SYS_EINVAL; 
+        case OS_INVALID_PARM:   return SYS_EINVAL; 
+        case OS_TIMEOUT:        return SYS_ETIMEOUT; 
+        case OS_ENOENT:         return SYS_ENOENT; 
+        case OS_EBUSY:          return SYS_EBUSY; 
+        default:                return SYS_EUNKNOWN;
+    }
 }
-#endif
-
-#endif


[mynewt-core] 04/10: util/streamer: generic data streaming interface

Posted by cc...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit 15d991048f905d7c4595cdd29bdd3189f923b675
Author: Christopher Collins <cc...@apache.org>
AuthorDate: Fri May 10 10:04:19 2019 -0700

    util/streamer: generic data streaming interface
    
    This package defines a generic data streaming interface.  Supported
    operations are:
        * write flat buf
        * write printf-formatted text
    
    There are two built-in implementations:
        * console
        * mbuf chain
---
 util/streamer/include/streamer/streamer.h | 108 ++++++++++++++++++++++++++++++
 util/streamer/pkg.yml                     |  28 ++++++++
 util/streamer/src/streamer.c              |  45 +++++++++++++
 util/streamer/src/streamer_console.c      |  50 ++++++++++++++
 util/streamer/src/streamer_mbuf.c         |  99 +++++++++++++++++++++++++++
 util/streamer/syscfg.yml                  |  23 +++++++
 6 files changed, 353 insertions(+)

diff --git a/util/streamer/include/streamer/streamer.h b/util/streamer/include/streamer/streamer.h
new file mode 100644
index 0000000..87ea802
--- /dev/null
+++ b/util/streamer/include/streamer/streamer.h
@@ -0,0 +1,108 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#ifndef H_STREAMER_
+#define H_STREAMER_
+
+#include "os/mynewt.h"
+
+struct streamer;
+
+typedef int streamer_write_fn(struct streamer *streamer,
+                              const void *src, size_t len);
+
+typedef int streamer_vprintf_fn(struct streamer *streamer,
+                                const char *fmt, va_list ap); 
+
+struct streamer_cfg {
+    streamer_write_fn *write_cb;
+    streamer_vprintf_fn *vprintf_cb;
+};
+
+/**
+ * @brief Provides a generic data streaming interface.
+ */
+struct streamer {
+    const struct streamer_cfg *cfg;
+};
+
+/**
+ * @brief Streams data to an mbuf chain.
+ */
+struct streamer_mbuf {
+    struct streamer streamer; /* Must be first member. */
+    struct os_mbuf *om;
+};
+
+/**
+ * @brief Writes the given flat buffer to a streamer.
+ *
+ * @param streamer              The streamer to write to.
+ * @param src                   The flat buffer to write.
+ * @param len                   The number of bytes to write.
+ *
+ * @return                      0 on success; SYS_E[...] on failure.
+ */
+int streamer_write(struct streamer *streamer, const void *src, size_t len);
+
+/**
+ * @brief Writes printf-formatted text to a streamer.
+ *
+ * A null-terminator does *not* get written.
+ *
+ * @param streamer              The streamer to write to.
+ * @param src                   The flat buffer to write.
+ * @param len                   The number of bytes to write.
+ *
+ * @return                      Number of bytes written on success;
+ *                              SYS_E[...] on failure.
+ */
+int streamer_vprintf(struct streamer *streamer, const char *fmt, va_list ap);
+
+/**
+ * @brief Writes printf-formatted text to a streamer.
+ */
+int streamer_printf(struct streamer *streamer, const char *fmt, ...);
+
+/**
+ * @brief Acquires the singleton console streamer.
+ */
+struct streamer *streamer_console_get(void);
+
+/**
+ * Constructs an mbuf streamer.
+ *
+ * @param sm                    The mbuf streamer object to populate.
+ * @param om                    The mbuf chain to write to.  This may already
+ *                                  contain data.
+ *
+ * @return                      0 on success; SYS_E[...] on failure.
+ */
+int streamer_mbuf_new(struct streamer_mbuf *sm, struct os_mbuf *om);
+
+/**
+ * Constructs an mbuf streamer that uses mbufs acquired from msys.
+ *
+ * @param sm                    The mbuf streamer object to populate.
+ *
+ * @return                      0 on success; SYS_E[...] on failure.
+ */
+int streamer_msys_new(struct streamer_mbuf *sm);
+
+#endif
diff --git a/util/streamer/pkg.yml b/util/streamer/pkg.yml
new file mode 100644
index 0000000..77cf34a
--- /dev/null
+++ b/util/streamer/pkg.yml
@@ -0,0 +1,28 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+# 
+#  http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+pkg.name: util/streamer
+pkg.description: "Generic streaming utility"
+pkg.author: "Apache Mynewt <de...@mynewt.apache.org>"
+pkg.homepage: "http://mynewt.apache.org/"
+pkg.keywords:
+
+pkg.deps: 
+    - "@apache-mynewt-core/kernel/os"
+
+pkg.req_apis:
+    - console
diff --git a/util/streamer/src/streamer.c b/util/streamer/src/streamer.c
new file mode 100644
index 0000000..a33072b
--- /dev/null
+++ b/util/streamer/src/streamer.c
@@ -0,0 +1,45 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include "streamer/streamer.h"
+
+int
+streamer_write(struct streamer *streamer, const void *src, size_t len)
+{
+    return streamer->cfg->write_cb(streamer, src, len);
+}
+
+int
+streamer_vprintf(struct streamer *streamer, const char *fmt, va_list ap)
+{
+    return streamer->cfg->vprintf_cb(streamer, fmt, ap);
+}
+
+int
+streamer_printf(struct streamer *streamer, const char *fmt, ...)
+{
+    va_list ap;
+    int rc;
+
+    va_start(ap, fmt);
+    rc = streamer_vprintf(streamer, fmt, ap);
+    va_end(ap);
+
+    return rc;
+}
diff --git a/util/streamer/src/streamer_console.c b/util/streamer/src/streamer_console.c
new file mode 100644
index 0000000..5a3b24a
--- /dev/null
+++ b/util/streamer/src/streamer_console.c
@@ -0,0 +1,50 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include "console/console.h"
+#include "streamer/streamer.h"
+
+static int
+streamer_console_write(struct streamer *streamer, const void *src, size_t len)
+{
+    console_write(src, len);
+    return 0;
+}
+
+static int
+streamer_console_vprintf(struct streamer *streamer,
+                         const char *fmt, va_list ap)
+{
+    return console_vprintf(fmt, ap);
+}
+
+static const struct streamer_cfg streamer_cfg_console = {
+    .write_cb = streamer_console_write,
+    .vprintf_cb = streamer_console_vprintf,
+};
+
+static struct streamer streamer_console = {
+    .cfg = &streamer_cfg_console,
+};
+
+struct streamer *
+streamer_console_get(void)
+{
+    return &streamer_console;
+}
diff --git a/util/streamer/src/streamer_mbuf.c b/util/streamer/src/streamer_mbuf.c
new file mode 100644
index 0000000..800527e
--- /dev/null
+++ b/util/streamer/src/streamer_mbuf.c
@@ -0,0 +1,99 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include "os/mynewt.h"
+#include "streamer/streamer.h"
+
+static int
+streamer_mbuf_write(struct streamer *streamer, const void *src, size_t len)
+{
+    struct streamer_mbuf *sm;
+    int rc;
+
+    if (len > UINT16_MAX) {
+        return SYS_EINVAL;
+    }
+
+    sm = (struct streamer_mbuf *)streamer;
+    rc = os_mbuf_append(sm->om, src, len);
+    if (rc != 0) {
+        return os_error_to_sys(rc);
+    }
+
+    return 0;
+}
+
+static int
+streamer_mbuf_vprintf(struct streamer *streamer, const char *fmt, va_list ap)
+{
+    char buf[MYNEWT_VAL(STREAMER_MBUF_PRINTF_MAX)];
+    int num_chars;
+    int rc;
+
+    num_chars = vsnprintf(buf, sizeof buf, fmt, ap);
+    if (num_chars > sizeof buf - 1) {
+        num_chars = sizeof buf - 1;
+    }
+
+    rc = streamer_mbuf_write(streamer, buf, num_chars);
+    if (rc != 0) {
+        return rc;
+    }
+
+    return num_chars;
+}
+
+static const struct streamer_cfg streamer_cfg_mbuf = {
+    .write_cb = streamer_mbuf_write,
+    .vprintf_cb = streamer_mbuf_vprintf,
+};
+
+int
+streamer_mbuf_new(struct streamer_mbuf *sm, struct os_mbuf *om)
+{
+    if (sm == NULL || om == NULL) {
+        return SYS_EINVAL;
+    }
+
+    *sm = (struct streamer_mbuf) {
+        .streamer.cfg = &streamer_cfg_mbuf,
+        .om = om,
+    };
+
+    return 0;
+}
+
+int
+streamer_msys_new(struct streamer_mbuf *sm)
+{
+    struct os_mbuf *om;
+    int rc;
+
+    om = os_msys_get_pkthdr(0, 0);
+    if (om == NULL) {
+        return SYS_ENOMEM;
+    }
+
+    rc = streamer_mbuf_new(sm, om);
+    if (rc != 0) {
+        return rc;
+    }
+
+    return 0;
+}
diff --git a/util/streamer/syscfg.yml b/util/streamer/syscfg.yml
new file mode 100644
index 0000000..36e0edb
--- /dev/null
+++ b/util/streamer/syscfg.yml
@@ -0,0 +1,23 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#  http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+syscfg.defs:
+    STREAMER_MBUF_PRINTF_MAX:
+        description: >
+            Maximum number of characters that can be streamed to an mbuf in a
+            single printf call.
+        value: 128


[mynewt-core] 10/10: flash_test: Change shell cmds to extended

Posted by cc...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit e4c46a7eaee5cc55ae786ed8b94d670b392c8211
Author: Christopher Collins <cc...@apache.org>
AuthorDate: Thu May 30 17:51:27 2019 -0700

    flash_test: Change shell cmds to extended
    
    This allows the flash_test shell commands to be executed via newtmgr.
---
 test/flash_test/src/flash_test.c | 94 +++++++++++++++++++++-------------------
 1 file changed, 50 insertions(+), 44 deletions(-)

diff --git a/test/flash_test/src/flash_test.c b/test/flash_test/src/flash_test.c
index e394b69..4758e84 100644
--- a/test/flash_test/src/flash_test.c
+++ b/test/flash_test/src/flash_test.c
@@ -27,14 +27,20 @@
 #include <stdio.h>
 #include <string.h>
 
-static int flash_cli_cmd(int argc, char **argv);
-static struct shell_cmd flash_cmd_struct = {
-    .sc_cmd = "flash",
-    .sc_cmd_func = flash_cli_cmd
-};
+static int flash_cli_cmd(const struct shell_cmd *cmd, int argc, char **argv,
+                         struct streamer *streamer);
+static int flash_speed_test_cli(const struct shell_cmd *cmd, int argc,
+                                char **argv, struct streamer *streamer);
+
+static struct shell_cmd flash_cmd_struct =
+    SHELL_CMD_EXT("flash", flash_cli_cmd, NULL);
+
+static struct shell_cmd flash_speed_cli_struct =
+    SHELL_CMD_EXT("flash_speed", flash_speed_test_cli, NULL);
 
 static int
-flash_cli_cmd(int argc, char **argv)
+flash_cli_cmd(const struct shell_cmd *cmd, int argc, char **argv,
+              struct streamer *streamer)
 {
     const struct hal_flash *hf;
     uint32_t off = 0;
@@ -48,11 +54,11 @@ flash_cli_cmd(int argc, char **argv)
     char pr_str[80];
 
     if (argc > 1 && (!strcmp(argv[1], "?") || !strcmp(argv[1], "help"))) {
-        console_printf("Commands Available\n");
-        console_printf("flash [flash-id] -- dumps sector map \n");
-        console_printf("flash <flash-id> read <offset> <size> -- reads bytes from flash \n");
-        console_printf("flash <flash-id> write <offset>  <size>  -- writes incrementing data pattern 0-8 to flash \n");
-        console_printf("flash <flash-id> erase <offset> <size> -- erases flash \n");
+        streamer_printf(streamer, "Commands Available\n");
+        streamer_printf(streamer, "flash [flash-id] -- dumps sector map \n");
+        streamer_printf(streamer, "flash <flash-id> read <offset> <size> -- reads bytes from flash \n");
+        streamer_printf(streamer, "flash <flash-id> write <offset>  <size>  -- writes incrementing data pattern 0-8 to flash \n");
+        streamer_printf(streamer, "flash <flash-id> erase <offset> <size> -- erases flash \n");
         return 0;
     }
 
@@ -61,7 +67,7 @@ flash_cli_cmd(int argc, char **argv)
         if (argc == 2) {
             devid = strtoul(argv[1], &eptr, 0);
             if (*eptr != 0) {
-                console_printf("Invalid flash id %s\n", argv[1]);
+                streamer_printf(streamer, "Invalid flash id %s\n", argv[1]);
                 return 0;
             }
         }
@@ -69,12 +75,12 @@ flash_cli_cmd(int argc, char **argv)
             hf = hal_bsp_flash_dev(devid);
             if (!hf) {
                 if (argc == 2) {
-                    console_printf("Flash device not present\n");
+                    streamer_printf(streamer, "Flash device not present\n");
                 }
                 return 0;
             }
-            console_printf("Flash %d at 0x%lx size 0x%lx with %d sectors,"
-              " alignment req %d bytes\n",
+            streamer_printf(streamer, "Flash %d at 0x%lx size 0x%lx with %d "
+                                      "sectors, alignment req %d bytes\n",
                     devid,
                     (long unsigned int) hf->hf_base_addr,
                     (long unsigned int) hf->hf_size,
@@ -85,11 +91,12 @@ flash_cli_cmd(int argc, char **argv)
                 sec_cnt = 32;
             }
             for (i = 0; i < sec_cnt; i++) {
-                console_printf("  %d: %lx\n", i,
+                streamer_printf(streamer, "  %d: %lx\n", i,
                         (long unsigned int) hal_flash_sector_size(hf, i));
             }
             if (sec_cnt != hf->hf_sector_cnt) {
-                console_printf("...  %d: %lx\n", hf->hf_sector_cnt - 1,
+                streamer_printf(streamer, "...  %d: %lx\n",
+                  hf->hf_sector_cnt - 1,
                   (long unsigned int) hal_flash_sector_size(hf, hf->hf_sector_cnt - 1));
             }
             ++devid;
@@ -101,40 +108,40 @@ flash_cli_cmd(int argc, char **argv)
     if (argc > 1) {
         devid = strtoul(argv[1], &eptr, 0);
         if (*eptr != 0) {
-            console_printf("Invalid flash id %s\n", argv[1]);
+            streamer_printf(streamer, "Invalid flash id %s\n", argv[1]);
             goto err;
         }
     }
     if (argc > 3) {
         off = strtoul(argv[3], &eptr, 0);
         if (*eptr != '\0') {
-            console_printf("Invalid offset %s\n", argv[2]);
+            streamer_printf(streamer, "Invalid offset %s\n", argv[2]);
             goto err;
         }
     }
     if (argc > 4) {
         sz = strtoul(argv[4], &eptr, 0);
         if (*eptr != '\0') {
-            console_printf("Invalid size %s\n", argv[3]);
+            streamer_printf(streamer, "Invalid size %s\n", argv[3]);
             goto err;
         }
     }
     if (!strcmp(argv[2], "erase")) {
-        console_printf("Erase 0x%lx + %lx\n",
+        streamer_printf(streamer, "Erase 0x%lx + %lx\n",
                 (long unsigned int) off, (long unsigned int) sz);
 
         if (hal_flash_erase(devid, off, sz)) {
-            console_printf("Flash erase failed\n");
+            streamer_printf(streamer, "Flash erase failed\n");
         }
-        console_printf("Done!\n");
+        streamer_printf(streamer, "Done!\n");
     } else if (!strcmp(argv[2], "read")) {
-        console_printf("Read 0x%lx + %lx\n",
+        streamer_printf(streamer, "Read 0x%lx + %lx\n",
                 (long unsigned int) off, (long unsigned int) sz);
         sz += off;
         while (off < sz) {
             sec_cnt = min(sizeof(tmp_buf), sz - off);
             if (hal_flash_read(devid, off, tmp_buf, sec_cnt)) {
-                console_printf("flash read failure at %lx\n",
+                streamer_printf(streamer, "flash read failure at %lx\n",
                         (long unsigned int) off);
                 break;
             }
@@ -142,20 +149,20 @@ flash_cli_cmd(int argc, char **argv)
                 soff += snprintf(pr_str + soff, sizeof(pr_str) - soff,
                   "0x%02x ", tmp_buf[i] & 0xff);
                 if (i % 8 == 7) {
-                    console_printf("  0x%lx: %s\n",
+                    streamer_printf(streamer, "  0x%lx: %s\n",
                                    (long unsigned int) off, pr_str);
                     soff = 0;
                     off += 8;
                 }
             }
             if (i % 8) {
-                console_printf("  0x%lx: %s\n",
+                streamer_printf(streamer, "  0x%lx: %s\n",
                                (long unsigned int) off, pr_str);
                 off += i;
             }
         }
     } else if (!strcmp(argv[2], "write")) {
-        console_printf("Write 0x%lx + %lx\n",
+        streamer_printf(streamer, "Write 0x%lx + %lx\n",
                 (long unsigned int) off, (long unsigned int) sz);
 
         sz += off;
@@ -166,12 +173,12 @@ flash_cli_cmd(int argc, char **argv)
         while (off < sz) {
             sec_cnt = min(sizeof(tmp_buf), sz - off);
             if (hal_flash_write(devid, off, tmp_buf, sec_cnt)) {
-                console_printf("flash write failure at %lx\n",
+                streamer_printf(streamer, "flash write failure at %lx\n",
                         (long unsigned int) off);
             }
             off += sec_cnt;
         }
-        console_printf("Done!\n");
+        streamer_printf(streamer, "Done!\n");
     }
     return 0;
 err:
@@ -234,7 +241,8 @@ flash_speed_test(int flash_dev, uint32_t addr, int sz, int move)
 }
 
 static int
-flash_speed_test_cli(int argc, char **argv)
+flash_speed_test_cli(const struct shell_cmd *cmd, int argc, char **argv,
+                     struct streamer *streamer)
 {
     char *ep;
     int flash_dev;
@@ -245,19 +253,20 @@ flash_speed_test_cli(int argc, char **argv)
     int i;
 
     if (argc < 4) {
-        console_printf("flash_speed <flash_id> <addr> <rd_sz>|range [move]\n");
+        streamer_printf(streamer,
+          "flash_speed <flash_id> <addr> <rd_sz>|range [move]\n");
         return 0;
     }
 
     flash_dev = strtoul(argv[1], &ep, 10);
     if (*ep != '\0') {
-        console_printf("Invalid flash_id: %s\n", argv[1]);
+        streamer_printf(streamer, "Invalid flash_id: %s\n", argv[1]);
         return 0;
     }
 
     addr = strtoul(argv[2], &ep, 0);
     if (*ep != '\0') {
-        console_printf("Invalid address: %s\n", argv[2]);
+        streamer_printf(streamer, "Invalid address: %s\n", argv[2]);
         return 0;
     }
 
@@ -267,7 +276,7 @@ flash_speed_test_cli(int argc, char **argv)
         i = 0;
         sz = strtoul(argv[3], &ep, 0);
         if (*ep != '\0') {
-            console_printf("Invalid read size: %s\n", argv[3]);
+            streamer_printf(streamer, "Invalid read size: %s\n", argv[3]);
             return 0;
         }
     }
@@ -278,32 +287,29 @@ flash_speed_test_cli(int argc, char **argv)
     }
 
     if (i == 0) {
-        console_printf("Speed test, hal_flash_read(%d, 0x%x%s, %d)\n",
+        streamer_printf(streamer,
+          "Speed test, hal_flash_read(%d, 0x%x%s, %d)\n",
           flash_dev, (unsigned int)addr, move?"..":"", (unsigned int)sz);
         cnt = flash_speed_test(flash_dev, addr, sz, move);
-        console_printf("%d\n", cnt >> 1);
+        streamer_printf(streamer, "%d\n", cnt >> 1);
     } else {
         uint32_t sizes[] = {
             1, 2, 4, 8, 16, 24, 32, 48, 64, 96, 128, 192, 256
         };
 
-        console_printf("Speed test, hal_flash_read(%d, 0x%x%s, X)\n",
+        streamer_printf(streamer,
+          "Speed test, hal_flash_read(%d, 0x%x%s, X)\n",
           flash_dev, (unsigned int)addr, move?"..":"");
 
         for (i = 0; i < sizeof(sizes) / sizeof(sizes[0]); i++) {
             cnt = flash_speed_test(flash_dev, addr, sizes[i], move);
-            console_printf("%3d %d\n", (int)sizes[i], cnt >> 1);
+            streamer_printf(streamer, "%3d %d\n", (int)sizes[i], cnt >> 1);
             os_time_delay(OS_TICKS_PER_SEC / 8);
         }
     }
     return 0;
 }
 
-static struct shell_cmd flash_speed_cli_struct = {
-    .sc_cmd = "flash_speed",
-    .sc_cmd_func = flash_speed_test_cli
-};
-
 /*
  * Initialize the package. Only called from sysinit().
  */


[mynewt-core] 09/10: sys/stats: Change the stats shell cmds to extended

Posted by cc...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit 84faab7679d12f50b791d1aa2c0483f7b5a91ac3
Author: Christopher Collins <cc...@apache.org>
AuthorDate: Fri May 10 18:40:46 2019 -0700

    sys/stats: Change the stats shell cmds to extended
    
    This allows the stats shell commands to be executed via newtmgr.
---
 sys/stats/full/src/stats_shell.c | 44 +++++++++++++++++++++++++---------------
 1 file changed, 28 insertions(+), 16 deletions(-)

diff --git a/sys/stats/full/src/stats_shell.c b/sys/stats/full/src/stats_shell.c
index 983d9ae..b9cf8c5 100644
--- a/sys/stats/full/src/stats_shell.c
+++ b/sys/stats/full/src/stats_shell.c
@@ -26,35 +26,43 @@
 
 #include <string.h>
 #include "shell/shell.h"
-#include "console/console.h"
+#include "streamer/streamer.h"
 #include "stats/stats.h"
 
-static int shell_stats_display(int argc, char **argv);
-static struct shell_cmd shell_stats_cmd = {
-    .sc_cmd = "stat",
-    .sc_cmd_func = shell_stats_display
-};
+static int shell_stats_display(const struct shell_cmd *cmd,
+                               int argc, char **argv,
+                               struct streamer *streamer);
+
+static struct shell_cmd shell_stats_cmd =
+    SHELL_CMD_EXT("stat", shell_stats_display, NULL);
+
 uint8_t stats_shell_registered;
 
 static int 
 stats_shell_display_entry(struct stats_hdr *hdr, void *arg, char *name,
         uint16_t stat_off)
 {
+    struct streamer *streamer;
     void *stat_val;
 
+    streamer = arg;
+
     stat_val = (uint8_t *)hdr + stat_off;
     switch (hdr->s_size) {
         case sizeof(uint16_t):
-            console_printf("%s: %u\n", name, *(uint16_t *) stat_val);
+            streamer_printf(streamer, "%s: %u\n", name,
+                    *(uint16_t *) stat_val);
             break;
         case sizeof(uint32_t):
-            console_printf("%s: %lu\n", name, *(unsigned long *) stat_val);
+            streamer_printf(streamer, "%s: %lu\n", name,
+                    *(unsigned long *) stat_val);
             break;
         case sizeof(uint64_t):
-            console_printf("%s: %llu\n", name, *(uint64_t *) stat_val);
+            streamer_printf(streamer, "%s: %llu\n", name,
+                    *(uint64_t *) stat_val);
             break;
         default:
-            console_printf("Unknown stat size for %s %u\n", name, 
+            streamer_printf(streamer, "Unknown stat size for %s %u\n", name, 
                     hdr->s_size);
             break;
     }
@@ -65,12 +73,16 @@ stats_shell_display_entry(struct stats_hdr *hdr, void *arg, char *name,
 static int 
 stats_shell_display_group(struct stats_hdr *hdr, void *arg)
 {
-    console_printf("\t%s\n", hdr->s_name);
+    struct streamer *streamer;
+
+    streamer = arg;
+    streamer_printf(streamer, "\t%s\n", hdr->s_name);
     return (0);
 }
 
 static int
-shell_stats_display(int argc, char **argv)
+shell_stats_display(const struct shell_cmd *cmd, int argc, char **argv,
+                    struct streamer *streamer)
 {
     struct stats_hdr *hdr;
     char *name;
@@ -78,21 +90,21 @@ shell_stats_display(int argc, char **argv)
 
     name = argv[1];
     if (name == NULL || !strcmp(name, "")) {
-        console_printf("Must specify a statistic name to dump, "
+        streamer_printf(streamer, "Must specify a statistic name to dump, "
                 "possible names are:\n");
-        stats_group_walk(stats_shell_display_group, NULL);
+        stats_group_walk(stats_shell_display_group, streamer);
         rc = OS_EINVAL;
         goto err;
     }
 
     hdr = stats_group_find(name);
     if (!hdr) {
-        console_printf("Could not find statistic group %s\n", name);
+        streamer_printf(streamer, "Could not find statistic group %s\n", name);
         rc = OS_EINVAL;
         goto err;
     }
 
-    rc = stats_walk(hdr, stats_shell_display_entry, NULL);
+    rc = stats_walk(hdr, stats_shell_display_entry, streamer);
     if (rc != 0) {
         goto err;
     }


[mynewt-core] 07/10: sys/shell: shell-newtmgr bridge

Posted by cc...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit 4d37e68313f9dbae9f5abd6cc695372b57f58fdc
Author: Christopher Collins <cc...@apache.org>
AuthorDate: Fri May 10 18:59:39 2019 -0700

    sys/shell: shell-newtmgr bridge
    
    Implement a new newtmgr command: `shell exec`.  This command allows
    shell commands to be executed via newtmgr.
---
 mgmt/mgmt/include/mgmt/mgmt.h         |   1 +
 sys/shell/pkg.yml                     |   3 +
 sys/shell/src/shell_bridge.c          | 109 ++++++++++++++++++++++++++++++++++
 sys/shell/src/shell_bridge_streamer.c |  81 +++++++++++++++++++++++++
 sys/shell/syscfg.yml                  |  16 +++++
 5 files changed, 210 insertions(+)

diff --git a/mgmt/mgmt/include/mgmt/mgmt.h b/mgmt/mgmt/include/mgmt/mgmt.h
index 427be1d..179d67f 100644
--- a/mgmt/mgmt/include/mgmt/mgmt.h
+++ b/mgmt/mgmt/include/mgmt/mgmt.h
@@ -58,6 +58,7 @@ extern "C" {
 #define MGMT_GROUP_ID_SPLIT     (6)
 #define MGMT_GROUP_ID_RUN       (7)
 #define MGMT_GROUP_ID_FS        (8)
+#define MGMT_GROUP_ID_SHELL     (9)
 #define MGMT_GROUP_ID_PERUSER   (64)
 
 /**
diff --git a/sys/shell/pkg.yml b/sys/shell/pkg.yml
index cf009a8..dee374f 100644
--- a/sys/shell/pkg.yml
+++ b/sys/shell/pkg.yml
@@ -33,6 +33,9 @@ pkg.deps.SHELL_NEWTMGR:
     - "@apache-mynewt-core/encoding/base64"
     - "@apache-mynewt-core/util/crc"
 
+pkg.deps.SHELL_BRIDGE:
+    - "@apache-mynewt-core/encoding/tinycbor"
+
 pkg.req_apis:
     - console
 
diff --git a/sys/shell/src/shell_bridge.c b/sys/shell/src/shell_bridge.c
new file mode 100644
index 0000000..f1ca335
--- /dev/null
+++ b/sys/shell/src/shell_bridge.c
@@ -0,0 +1,109 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include "os/mynewt.h"
+
+#if MYNEWT_VAL(SHELL_BRIDGE)
+
+#include "mgmt/mgmt.h"
+#include "cborattr/cborattr.h"
+#include "streamer/streamer.h"
+#include "tinycbor/cbor_mbuf_writer.h"
+#include "shell_priv.h"
+
+static struct mgmt_group shell_bridge_group;
+
+static int shell_bridge_exec(struct mgmt_cbuf *cb);
+
+static struct mgmt_handler shell_bridge_group_handlers[] = {
+    [SHELL_NMGR_OP_EXEC] = { NULL, shell_bridge_exec },
+};
+
+/**
+ * Handler for the `shell exec` newtmgr command.
+ */
+static int
+shell_bridge_exec(struct mgmt_cbuf *cb)
+{
+    char line[MYNEWT_VAL(SHELL_BRIDGE_MAX_IN_LEN)];
+    char *argv[MYNEWT_VAL(SHELL_CMD_ARGC_MAX)];
+    struct shell_bridge_streamer sbs;
+    CborEncoder str_encoder;
+    CborError err;
+    int argc;
+    int rc;
+
+    const struct cbor_attr_t attrs[] = {
+        {
+            .attribute = "argv",
+            .type = CborAttrArrayType,
+            .addr.array = {
+                .element_type = CborAttrTextStringType,
+                .arr.strings.ptrs = argv,
+                .arr.strings.store = line,
+                .arr.strings.storelen = sizeof line,
+                .count = &argc,
+                .maxlen = sizeof argv / sizeof argv[0],
+            },
+        },
+        { 0 },
+    };
+
+    err = cbor_read_object(&cb->it, attrs);
+    if (err != 0) {
+        return MGMT_ERR_EINVAL;
+    }
+
+    /* Key="o"; value=<command-output> */
+    err |= cbor_encode_text_stringz(&cb->encoder, "o");
+    err |= cbor_encoder_create_indef_text_string(&cb->encoder, &str_encoder);
+
+    shell_bridge_streamer_new(&sbs, &str_encoder);
+    rc = shell_exec(argc, argv, &sbs.streamer);
+
+    err |= cbor_encoder_close_container(&cb->encoder, &str_encoder);
+
+    /* Key="rc"; value=<status> */
+    err |= cbor_encode_text_stringz(&cb->encoder, "rc");
+    err |= cbor_encode_int(&cb->encoder, rc);
+
+    if (err != 0) {
+        return MGMT_ERR_ENOMEM;
+    }
+
+    return 0;
+}
+
+int
+shell_bridge_init(void)
+{
+    int rc;
+
+    MGMT_GROUP_SET_HANDLERS(&shell_bridge_group, shell_bridge_group_handlers);
+    shell_bridge_group.mg_group_id = MGMT_GROUP_ID_SHELL;
+
+    rc = mgmt_group_register(&shell_bridge_group);
+    if (rc) {
+        return rc;
+    }
+
+    return 0;
+}
+
+#endif
diff --git a/sys/shell/src/shell_bridge_streamer.c b/sys/shell/src/shell_bridge_streamer.c
new file mode 100644
index 0000000..d9df4dd
--- /dev/null
+++ b/sys/shell/src/shell_bridge_streamer.c
@@ -0,0 +1,81 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include "os/mynewt.h"
+
+#if MYNEWT_VAL(SHELL_BRIDGE)
+
+#include "tinycbor/cbor.h"
+#include "shell_priv.h"
+
+static int
+shell_bridge_streamer_write(struct streamer *streamer,
+                            const void *src, size_t len)
+{
+    struct shell_bridge_streamer *sbs;
+    int err;
+
+    sbs = (struct shell_bridge_streamer *)streamer;
+
+    /* Encode the data as a CBOR text string. */
+    err = cbor_encode_text_string(sbs->str_encoder, src, len);
+    if (err != 0) {
+        return SYS_ENOMEM;
+    }
+
+    return 0;
+}
+
+static int
+shell_bridge_streamer_vprintf(struct streamer *streamer,
+                              const char *fmt, va_list ap)
+{
+    char buf[MYNEWT_VAL(SHELL_BRIDGE_PRINTF_LEN)];
+    int num_chars;
+    int rc;
+
+    num_chars = vsnprintf(buf, sizeof buf, fmt, ap);
+    if (num_chars > sizeof buf - 1) {
+        num_chars = sizeof buf - 1;
+    }
+
+    rc = shell_bridge_streamer_write(streamer, buf, num_chars);
+    if (rc != 0) {
+        return rc;
+    }
+
+    return num_chars;
+}
+
+static const struct streamer_cfg shell_bridge_streamer_cfg = {
+    .write_cb = shell_bridge_streamer_write,
+    .vprintf_cb = shell_bridge_streamer_vprintf,
+};
+
+void
+shell_bridge_streamer_new(struct shell_bridge_streamer *sbs,
+                          struct CborEncoder *str_encoder)
+{
+    *sbs = (struct shell_bridge_streamer) {
+        .streamer.cfg = &shell_bridge_streamer_cfg,
+        .str_encoder = str_encoder,
+    };
+}
+
+#endif
diff --git a/sys/shell/syscfg.yml b/sys/shell/syscfg.yml
index 1f416bc..638f968 100644
--- a/sys/shell/syscfg.yml
+++ b/sys/shell/syscfg.yml
@@ -74,6 +74,22 @@ syscfg.defs:
             write 2, read/write 3 (default)
         value: 3 
 
+    SHELL_BRIDGE:
+        description: >
+            Enables the `shell exec` newtmgr command; executes shell commands
+            via newtmgr.
+        value: 0
+    SHELL_BRIDGE_MAX_IN_LEN:
+        description: >
+            Maximum combined length of arguments passed to the `shell exec`
+            newtmgr command.
+        value: 128
+    SHELL_BRIDGE_PRINTF_LEN:
+        description: >
+            Maximum number of characters that can be streamed by a single
+            printf call during processing of the `shell exec` newtmgr command.
+        value: 128
+
 ## duplicated from boot/boot_serial
     BOOT_SERIAL_NVREG_MAGIC:
         description: >


[mynewt-core] 02/10: encoding/tinycbor: encode indefinite text strings

Posted by cc...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit b9e9544ec3c0ee7ead72ac974436a82cf4cde146
Author: Christopher Collins <cc...@apache.org>
AuthorDate: Fri May 10 12:51:32 2019 -0700

    encoding/tinycbor: encode indefinite text strings
    
    There was already a function to encode indefinite length byte strings.
    This commit adds a text string counterpart.
---
 encoding/tinycbor/include/tinycbor/cbor.h |  1 +
 encoding/tinycbor/src/cborencoder.c       | 14 ++++++++++++++
 2 files changed, 15 insertions(+)

diff --git a/encoding/tinycbor/include/tinycbor/cbor.h b/encoding/tinycbor/include/tinycbor/cbor.h
index ce45d5b..9ef16c0 100644
--- a/encoding/tinycbor/include/tinycbor/cbor.h
+++ b/encoding/tinycbor/include/tinycbor/cbor.h
@@ -215,6 +215,7 @@ CBOR_INLINE_API CborError cbor_encode_double(CborEncoder *encoder, double value)
 
 CBOR_API CborError cbor_encoder_create_array(CborEncoder *encoder, CborEncoder *arrayEncoder, size_t length);
 CBOR_API CborError cbor_encoder_create_map(CborEncoder *encoder, CborEncoder *mapEncoder, size_t length);
+CBOR_API CborError cbor_encoder_create_indef_text_string(CborEncoder *encoder, CborEncoder *stringEncoder);
 CBOR_API CborError cbor_encoder_create_indef_byte_string(CborEncoder *encoder, CborEncoder *stringEncoder);
 CBOR_API CborError cbor_encoder_close_container(CborEncoder *encoder, const CborEncoder *containerEncoder);
 CBOR_API CborError cbor_encoder_close_container_checked(CborEncoder *encoder, const CborEncoder *containerEncoder);
diff --git a/encoding/tinycbor/src/cborencoder.c b/encoding/tinycbor/src/cborencoder.c
index ab19b54..3827a84 100644
--- a/encoding/tinycbor/src/cborencoder.c
+++ b/encoding/tinycbor/src/cborencoder.c
@@ -522,6 +522,20 @@ CborError cbor_encoder_create_map(CborEncoder *encoder, CborEncoder *mapEncoder,
 }
 
 /**
+ * Creates a indefinite-length text string in the CBOR stream provided by
+ * \a encoder and initializes \a stringEncoder so that chunks of original string
+ * can be added using the CborEncoder functions. The string must be terminated by
+ * calling cbor_encoder_close_container() with the same \a encoder and
+ * \a stringEncoder parameters.
+ *
+ * \sa cbor_encoder_create_array
+ */
+CborError cbor_encoder_create_indef_text_string(CborEncoder *encoder, CborEncoder *stringEncoder)
+{
+    return create_container(encoder, stringEncoder, CborIndefiniteLength, TextStringType << MajorTypeShift);
+}
+
+/**
  * Creates a indefinite-length byte string in the CBOR stream provided by
  * \a encoder and initializes \a stringEncoder so that chunks of original string
  * can be added using the CborEncoder functions. The string must be terminated by


[mynewt-core] 06/10: hw/battery: Fix shell command definitions

Posted by cc...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit f6f145b5d20a0f7af454c8a3e2ce3e386ad24020
Author: Christopher Collins <cc...@apache.org>
AuthorDate: Fri May 10 18:59:30 2019 -0700

    hw/battery: Fix shell command definitions
    
    A recent change to the `struct shell_cmd` type is incompatible with how
    `hw/battery` defines its shell command.  Some extra fields were added to
    this struct, and the battery shell command was defined with positional
    values.
---
 hw/battery/src/battery_shell.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/hw/battery/src/battery_shell.c b/hw/battery/src/battery_shell.c
index c5b6db8..33243d6 100644
--- a/hw/battery/src/battery_shell.c
+++ b/hw/battery/src/battery_shell.c
@@ -476,12 +476,12 @@ err:
 
 static const struct shell_cmd bat_cli_commands[] =
 {
-    { "read", cmd_bat_read, HELP(bat_read_help) },
-    { "write", cmd_bat_write, HELP(bat_write_help) },
-    { "list", cmd_bat_list, HELP(bat_list_help) },
-    { "pollrate", cmd_bat_poll_rate, HELP(bat_poll_rate_help) },
-    { "monitor", cmd_bat_monitor, HELP(bat_monitor_help) },
-    { NULL, NULL, NULL }
+    SHELL_CMD("read", cmd_bat_read, &bat_read_help),
+    SHELL_CMD("write", cmd_bat_write, &bat_write_help),
+    SHELL_CMD("list", cmd_bat_list, &bat_list_help),
+    SHELL_CMD("pollrate", cmd_bat_poll_rate, &bat_poll_rate_help),
+    SHELL_CMD("monitor", cmd_bat_monitor, &bat_monitor_help),
+    { 0 },
 };
 
 /**