You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by st...@apache.org on 2015/12/03 00:49:50 UTC

[1/2] incubator-mynewt-larva git commit: add msys -- system mbufs. add base64 encoding to util. add base of newtmgr code, to provide remote statistics gathering. THIS IS an interim commit.

Repository: incubator-mynewt-larva
Updated Branches:
  refs/heads/master 9361e8b6f -> af9cb85b7


add msys -- system mbufs.  add base64 encoding to util.  add base of newtmgr code, to provide remote statistics gathering.  THIS IS an interim commit.


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/af9cb85b
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/tree/af9cb85b
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/diff/af9cb85b

Branch: refs/heads/master
Commit: af9cb85b7d88d019417861dab0180e1e12f63222
Parents: 3568eb5
Author: Sterling Hughes <st...@apache.org>
Authored: Wed Dec 2 15:49:18 2015 -0800
Committer: Sterling Hughes <st...@apache.org>
Committed: Wed Dec 2 15:49:28 2015 -0800

----------------------------------------------------------------------
 libs/os/include/os/os_mbuf.h     |  15 +++
 libs/os/src/os_mbuf.c            |  80 +++++++++++++++
 libs/shell/include/shell/shell.h |  11 ++
 libs/shell/src/shell.c           | 184 +++++++++++++++++++++++++++++++++-
 4 files changed, 285 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/af9cb85b/libs/os/include/os/os_mbuf.h
----------------------------------------------------------------------
diff --git a/libs/os/include/os/os_mbuf.h b/libs/os/include/os/os_mbuf.h
index 881ddba..effa8c2 100644
--- a/libs/os/include/os/os_mbuf.h
+++ b/libs/os/include/os/os_mbuf.h
@@ -40,6 +40,11 @@ struct os_mbuf_pool {
      * The memory pool which to allocate mbufs out of 
      */
     struct os_mempool *omp_pool;
+
+    /**
+     * Link to the next mbuf pool for system memory pools.
+     */
+    STAILQ_ENTRY(os_mbuf_pool) omp_next;
 };
 
 
@@ -201,6 +206,16 @@ struct os_mbuf *os_mqueue_get(struct os_mqueue *);
 /* Put an element in a mbuf queue */
 int os_mqueue_put(struct os_mqueue *, struct os_eventq *, struct os_mbuf *);
 
+/* Register an mbuf pool with the system pool registry */
+int os_msys_register(struct os_mbuf_pool *);
+
+/* Return a mbuf from the system pool, given an indicative mbuf size */
+struct os_mbuf *os_msys_get(uint16_t dsize, uint16_t leadingspace);
+
+/* Return a packet header mbuf from the system pool */
+struct os_mbuf *os_msys_get_pkthdr(uint16_t dsize, uint16_t pkthdr_len);
+
+
 /* Initialize a mbuf pool */
 int os_mbuf_pool_init(struct os_mbuf_pool *, struct os_mempool *mp, 
         uint16_t, uint16_t);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/af9cb85b/libs/os/src/os_mbuf.c
----------------------------------------------------------------------
diff --git a/libs/os/src/os_mbuf.c b/libs/os/src/os_mbuf.c
index b1be0fc..e5d8ff3 100644
--- a/libs/os/src/os_mbuf.c
+++ b/libs/os/src/os_mbuf.c
@@ -54,6 +54,9 @@
 
 #include <string.h>
 
+STAILQ_HEAD(, os_mbuf_pool) g_msys_pool_list = 
+    STAILQ_HEAD_INITIALIZER(g_msys_pool_list);
+
 int 
 os_mqueue_init(struct os_mqueue *mq, void *arg)
 {
@@ -117,6 +120,83 @@ err:
     return (rc);
 }
 
+int 
+os_msys_register(struct os_mbuf_pool *new_pool)  
+{
+    struct os_mbuf_pool *pool;
+
+    pool = NULL;
+    STAILQ_FOREACH(pool, &g_msys_pool_list, omp_next) {
+        if (new_pool->omp_databuf_len > pool->omp_databuf_len) {
+            break;
+        }
+    }
+
+    if (pool) {
+        STAILQ_INSERT_AFTER(&g_msys_pool_list, pool, new_pool, omp_next);
+    } else {
+        STAILQ_INSERT_TAIL(&g_msys_pool_list, new_pool, omp_next);
+    }
+
+    return (0);
+}
+
+static struct os_mbuf_pool *
+_os_msys_find_pool(uint16_t dsize) 
+{
+    struct os_mbuf_pool *pool;
+
+    pool = NULL;
+    STAILQ_FOREACH(pool, &g_msys_pool_list, omp_next) {
+        if (dsize <= pool->omp_databuf_len) {
+            break;
+        }
+    }
+
+    if (!pool) {
+        pool = STAILQ_LAST(&g_msys_pool_list, os_mbuf_pool, omp_next);
+    }
+
+    return (pool);
+}
+
+
+
+struct os_mbuf *
+os_msys_get(uint16_t dsize, uint16_t leadingspace)
+{
+    struct os_mbuf *m;
+    struct os_mbuf_pool *pool;
+
+    pool = _os_msys_find_pool(dsize);
+    if (!pool) {
+        goto err;
+    }
+
+    m = os_mbuf_get(pool, leadingspace);
+    return (m);
+err:
+    return (NULL);
+}
+
+
+struct os_mbuf *
+os_msys_get_pkthdr(uint16_t dsize, uint16_t pkthdr_len)
+{
+    struct os_mbuf *m;
+    struct os_mbuf_pool *pool;
+
+    pool = _os_msys_find_pool(dsize);
+    if (!pool) {
+        goto err;
+    }
+    
+    m = os_mbuf_get_pkthdr(pool, pkthdr_len);
+    return (m);
+err:
+    return (NULL);
+}
+
 
 /**
  * Initialize a pool of mbufs. 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/af9cb85b/libs/shell/include/shell/shell.h
----------------------------------------------------------------------
diff --git a/libs/shell/include/shell/shell.h b/libs/shell/include/shell/shell.h
index 529d158..320e92e 100644
--- a/libs/shell/include/shell/shell.h
+++ b/libs/shell/include/shell/shell.h
@@ -27,6 +27,17 @@ struct shell_cmd {
 
 int shell_cmd_register(struct shell_cmd *sc, char *cmd, 
         shell_cmd_func_t func);
+
+
+#define SHELL_NLIP_PKT_START1 (6)
+#define SHELL_NLIP_PKT_START2 (9)
+#define SHELL_NLIP_DATA_START1 (4)
+#define SHELL_NLIP_DATA_START2 (20)
+
+typedef int (*shell_nlip_input_func_t)(struct os_mbuf *, void *arg);
+int shell_nlip_input_register(shell_nlip_input_func_t nf, void *arg);
+int shell_nlip_output(struct os_mbuf *m);
+
 void shell_console_rx_cb(int full_line);
 int shell_task_init(uint8_t prio, os_stack_t *stack, uint16_t stack_size);
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/af9cb85b/libs/shell/src/shell.c
----------------------------------------------------------------------
diff --git a/libs/shell/src/shell.c b/libs/shell/src/shell.c
index 87361ad..187ca7d 100644
--- a/libs/shell/src/shell.c
+++ b/libs/shell/src/shell.c
@@ -18,12 +18,18 @@
 
 #include <console/console.h>
 
-#include "shell/shell.h" 
+#include <shell/shell.h>
+#include <util/base64.h>
 
 #include <stdio.h>
 #include <string.h>
 #include <assert.h>
 
+static shell_nlip_input_func_t g_shell_nlip_in_func;
+static void *g_shell_nlip_in_arg;
+
+static struct os_mqueue g_shell_nlip_mq;
+
 #define OS_EVENT_T_CONSOLE_RDY (OS_EVENT_T_PERUSER)
 
 static struct shell_cmd g_shell_echo_cmd;
@@ -40,6 +46,10 @@ char *argv[20];
 static STAILQ_HEAD(, shell_cmd) g_shell_cmd_list = 
     STAILQ_HEAD_INITIALIZER(g_shell_cmd_list);
 
+static struct os_mbuf *g_nlip_mbuf;
+static uint16_t g_nlip_expected_len;
+
+
 static int 
 shell_cmd_list_lock(void)
 {
@@ -157,6 +167,145 @@ shell_process_command(char *line, int len)
     return (0);
 }
 
+
+static int 
+shell_nlip_process(char *data, int len)
+{
+    uint16_t copy_len;
+    int rc;
+
+    rc = base64_decode(data, data);
+    if (rc < 0) {
+        goto err;
+    }
+    len = rc;
+
+    if (g_nlip_mbuf == NULL) {
+        if (len < 2) {
+            rc = -1;
+            goto err;
+        }
+
+        g_nlip_expected_len = ntohs(*(uint16_t *) data);
+        g_nlip_mbuf = os_msys_get_pkthdr(g_nlip_expected_len, 0);
+        if (!g_nlip_mbuf) {
+            rc = -1;
+            goto err;
+        }
+
+        data += sizeof(uint16_t);
+        len -= sizeof(uint16_t);
+    }
+
+    copy_len = min(g_nlip_expected_len - OS_MBUF_PKTHDR(g_nlip_mbuf)->omp_len,
+            len);
+
+    rc = os_mbuf_copyinto(g_nlip_mbuf, OS_MBUF_PKTHDR(g_nlip_mbuf)->omp_len, 
+            data, copy_len);
+    if (rc != 0) {
+        goto err;
+    }
+
+    if (OS_MBUF_PKTHDR(g_nlip_mbuf)->omp_len == g_nlip_expected_len) {
+        if (g_shell_nlip_in_func) {
+            g_shell_nlip_in_func(g_nlip_mbuf, g_shell_nlip_in_arg);
+        } else {
+            os_mbuf_free_chain(g_nlip_mbuf);
+        }
+        g_nlip_mbuf = NULL;
+        g_nlip_expected_len = 0;
+    }
+
+    return (0);
+err:
+    return (rc);
+}
+
+static int 
+shell_nlip_mtx(struct os_mbuf *m)
+{
+    uint8_t buf[12];
+    uint16_t totlen;
+    uint16_t dlen;
+    uint16_t off;
+    int rc;
+
+    /* Convert the mbuf into a packet.
+     *
+     * starts with 06 09 
+     * base64 encode:
+     *  - 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. 
+     */
+    totlen = OS_MBUF_PKTHDR(m)->omp_len;
+
+    while (totlen > 0) {
+        dlen = min(sizeof(buf), totlen);
+
+        rc = os_mbuf_copydata(m, off, dlen, buf);
+        if (rc != 0) {
+            goto err;
+        }
+        off += dlen;
+
+        console_write((char *) buf, dlen);
+        totlen -= dlen;
+    }
+
+    return (0);
+err:
+    return (rc);
+}
+
+static void
+shell_nlip_mqueue_process(void)
+{
+    struct os_mbuf *m;
+
+    /* Copy data out of the mbuf 12 bytes at a time and write it to 
+     * the console.
+     */
+    while (1) {
+        m = os_mqueue_get(&g_shell_nlip_mq);
+        if (!m) {
+            break;
+        }
+
+        (void) shell_nlip_mtx(m);
+
+        os_mbuf_free_chain(m);
+    }
+}
+
+int
+shell_nlip_input_register(shell_nlip_input_func_t nf, void *arg)
+{
+    g_shell_nlip_in_func = nf;
+    g_shell_nlip_in_arg = arg;
+
+    return (0);
+}
+
+int 
+shell_nlip_output(struct os_mbuf *m)
+{
+    int rc;
+
+    rc = os_mqueue_put(&g_shell_nlip_mq, &shell_evq, m);
+    if (rc != 0) {
+        goto err;
+    }
+
+    return (0);
+err:
+    return (rc);
+}
+
 static int 
 shell_read_console(void)
 {
@@ -171,9 +320,30 @@ shell_read_console(void)
             break;
         }
 
-        rc = shell_process_command(shell_line, rc);
-        if (rc != 0) {
-            goto err;
+        if (rc > 2) {
+            if (shell_line[0] == SHELL_NLIP_PKT_START1 && 
+                    shell_line[1] == SHELL_NLIP_PKT_START2) {
+                if (g_nlip_mbuf) {
+                    os_mbuf_free_chain(g_nlip_mbuf);
+                    g_nlip_mbuf = NULL;
+                }
+                g_nlip_expected_len = 0;
+
+                rc = shell_nlip_process(shell_line, rc);
+            } else if (shell_line[0] == SHELL_NLIP_DATA_START1 && 
+                    shell_line[1] == SHELL_NLIP_DATA_START2) {
+                rc = shell_nlip_process(shell_line, rc);
+            } else {
+                rc = shell_process_command(shell_line, rc);
+                if (rc != 0) {
+                    goto err;
+                }
+            }
+        } else {
+            rc = shell_process_command(shell_line, rc);
+            if (rc != 0) {
+                goto err;
+            }
         }
     }
 
@@ -182,12 +352,14 @@ err:
     return (rc);
 }
 
+
 static void
 shell_task_func(void *arg) 
 {
     struct os_event *ev;
 
     os_eventq_init(&shell_evq);
+    os_mqueue_init(&g_shell_nlip_mq, NULL);
     
     console_rdy_ev.ev_type = OS_EVENT_T_CONSOLE_RDY;
 
@@ -200,6 +372,9 @@ shell_task_func(void *arg)
                 // Read and process all available lines on the console.
                 (void) shell_read_console();
                 break;
+            case OS_EVENT_T_MQUEUE_DATA:
+                shell_nlip_mqueue_process();
+                break;
         }
     }
 }
@@ -254,7 +429,6 @@ shell_task_init(uint8_t prio, os_stack_t *stack, uint16_t stack_size)
         goto err;
     }
 
-
     return (0);
 err:
     return (rc);


[2/2] incubator-mynewt-larva git commit: add msys -- system mbufs. add base64 encoding to util. add base of newtmgr code, to provide remote statistics gathering. THIS IS an interim commit.

Posted by st...@apache.org.
add msys -- system mbufs.  add base64 encoding to util.  add base of newtmgr code, to provide remote statistics gathering.  THIS IS an interim commit.


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/3568eb51
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/tree/3568eb51
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/diff/3568eb51

Branch: refs/heads/master
Commit: 3568eb51f2a6b5b633bc9c651282c9d11fa926eb
Parents: 9361e8b
Author: Sterling Hughes <st...@apache.org>
Authored: Wed Dec 2 15:48:55 2015 -0800
Committer: Sterling Hughes <st...@apache.org>
Committed: Wed Dec 2 15:49:28 2015 -0800

----------------------------------------------------------------------
 libs/newtmgr/egg.yml                   |   5 +
 libs/newtmgr/include/newtmgr/newtmgr.h |  63 ++++++
 libs/newtmgr/src/newtmgr.c             | 316 ++++++++++++++++++++++++++++
 libs/util/include/util/base64.h        |  24 +++
 libs/util/src/base64.c                 | 167 +++++++++++++++
 5 files changed, 575 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/3568eb51/libs/newtmgr/egg.yml
----------------------------------------------------------------------
diff --git a/libs/newtmgr/egg.yml b/libs/newtmgr/egg.yml
new file mode 100644
index 0000000..086e911
--- /dev/null
+++ b/libs/newtmgr/egg.yml
@@ -0,0 +1,5 @@
+egg.name: libs/newtmgr
+egg.vers: 0.1
+egg.deps:
+    - libs/os
+    - libs/testutil

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/3568eb51/libs/newtmgr/include/newtmgr/newtmgr.h
----------------------------------------------------------------------
diff --git a/libs/newtmgr/include/newtmgr/newtmgr.h b/libs/newtmgr/include/newtmgr/newtmgr.h
new file mode 100644
index 0000000..f961e6e
--- /dev/null
+++ b/libs/newtmgr/include/newtmgr/newtmgr.h
@@ -0,0 +1,63 @@
+/**
+ * Copyright (c) 2015 Runtime Inc.
+ *
+ * Licensed 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 _NEWTMGR_H_
+#define _NEWTMGR_H_
+
+#include <util/tpq.h>
+
+#define NMGR_OP_READ (0)
+#define NMGR_OP_READ_RSP (1) 
+#define NMGR_OP_WRITE (2)
+#define NMGR_OP_WRITE_RSP (3) 
+
+struct nmgr_hdr {
+    uint8_t nh_op; 
+    uint8_t nh_flags;
+    uint16_t nh_len;
+    uint16_t nh_group;
+    uint16_t nh_id;
+};
+
+typedef int (*nmgr_handler_func_t)(struct nmgr_hdr *hdr, struct os_mbuf *req, 
+        uint16_t srcoff, struct os_mbuf *rsp);
+
+struct nmgr_handler {
+    nmgr_handle_func_t nh_read;
+    nmgr_handle_func_t nh_write;
+};
+
+struct nmgr_pkt {
+    struct tpq_element np_tpq_elem;
+    struct os_mbuf np_m;
+};
+
+struct nmgr_group {
+    struct nmgr_handler *ng_handlers;
+    uint16_t ng_handlers_count;
+    uint16_t ng_group_id;
+    STAILQ_ENTRY(nmgr_group) ng_next;
+};
+
+typedef int (*nmgr_transport_out_func_t)(struct nmgr_transport *nt, 
+        struct os_mbuf *m);
+
+struct nmgr_transport {
+    struct os_mqueue nt_imq;
+    nmgr_transport_out_func_t nt_output; 
+};
+
+#endif /* _NETMGR_H */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/3568eb51/libs/newtmgr/src/newtmgr.c
----------------------------------------------------------------------
diff --git a/libs/newtmgr/src/newtmgr.c b/libs/newtmgr/src/newtmgr.c
new file mode 100644
index 0000000..fad6bb6
--- /dev/null
+++ b/libs/newtmgr/src/newtmgr.c
@@ -0,0 +1,316 @@
+/**
+ * Copyright (c) 2015 Runtime Inc.
+ *
+ * Licensed 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/os.h>
+
+#include <string.h>
+
+struct nmgr_transport g_nmgr_transport;
+
+struct os_mutex g_nmgr_group_list_lock;
+
+struct os_eventq g_nmgr_evq;
+struct os_task g_nmgr_task;
+
+STAILQ_HEAD(, nmgr_group) g_nmgr_group_list = 
+    STAILQ_HEAD_INITIALIZER(nmgr_group);
+
+int 
+nmgr_group_list_lock(void)
+{
+    int rc;
+
+    if (!os_started()) {
+        return (0);
+    }
+
+    rc = os_mutex_pend(&g_nmgr_group_list_lock);
+    if (rc != 0) {
+        goto err;
+    }
+
+    return (0);
+err:
+    return (rc);
+}
+
+int 
+nmgr_group_list_unlock(void)
+{
+    int rc;
+
+    if (!os_started()) {
+        return (0);
+    }
+
+    rc = os_mutex_release(&g_nmgr_group_list_lock);
+    if (rc != 0) {
+        goto err;
+    }
+
+    return (0);
+err:
+    return (rc);
+}
+
+
+int 
+nmgr_group_register(struct nmgr_group *group)
+{
+    int rc;
+
+    rc = nmgr_group_list_lock();
+    if (rc != 0) {
+        goto err;
+    }
+
+    STAILQ_INSERT_TAIL(&g_nmgr_group_list, group, ng_next);
+
+    rc = nmgr_group_list_unlock();
+    if (rc != 0) {
+        goto err;
+    }
+
+
+    return (0);
+err:
+    return (rc);
+}
+
+static struct nmgr_group *
+nmgr_find_group(uint16_t group_id)
+{
+    struct nmgr_group *group;
+    int rc;
+
+    group = NULL;
+
+    rc = nmgr_group_list_lock();
+    if (rc != 0) {
+        goto err;
+    }
+
+    STAILQ_FOREACH(group, &g_nmgr_group_list, ng_next) {
+        if (group->ng_group_id == group_id) {
+            break;
+        }
+    }
+
+    rc = nmgr_group_list_unlock();
+    if (rc != 0) {
+        goto err;
+    }
+
+    return (group);
+err:
+    return (NULL);
+}
+
+static struct nmgr_handler *
+nmgr_find_handler(uint16_t group_id, uint16_t handler_id)
+{
+    struct nmgr_group *group;
+    struct nmgr_handler *handler;
+
+    group = nmgr_find_group(hdr->nh_group);
+    if (!group) {
+        goto err;
+    }
+
+    if (hdr->nh_id > group->ng_handlers_count) {
+        goto err;
+    }
+
+    handler = &group->ng_handlers[hdr->nh_id];
+
+    return (handler);
+err:
+    return (NULL);
+}
+
+
+static int 
+nmgr_handle_req(struct nmgr_transport *nt, struct os_mbuf *req)
+{
+    struct os_mbuf *rsp;
+    struct nmgr_handler *handler;
+    struct nmgr_hdr hdr;
+    uint32_t off;
+    uint32_t len;
+
+    rsp = os_msys_get_pkthdr(512, 0);
+    if (!rsp) {
+        rc = EINVAL;
+        goto err;
+    }
+
+    off = 0;
+    len = OS_MBUF_PKTHDR(req)->omp_len;
+
+    while (off < len) {
+        rc = os_mbuf_copydata(req, off, sizeof(hdr), &hdr);
+        if (rc < 0) {
+            rc = EINVAL;
+            goto err;
+        }
+
+        hdr.nh_len = ntohs(hdr.nh_len);
+        hdr.nh_group = ntohs(hdr.nh_group);
+        hdr.nh_id = ntohs(hdr.nh_id);
+
+        handler = nmgr_find_handler(hdr.nh_group, hdr.nh_id);
+        if (!handler) {
+            rc = EINVAL;
+            goto err;
+        }
+
+        if (hdr.nh_op == NMGR_OP_READ) {
+            rc = handler->nh_read(&hdr, req, off, rsp);
+        } else if (hdr.nh_op == NMGR_OP_WRITE) {
+            rc = handler->nh_write(&hdr, req, off, rsp);
+        } else {
+            rc = OS_EINVAL;
+            goto err;
+        }
+
+        off += sizeof(hdr) + OS_ALIGN(hdr.nh_len, 4);
+    }
+
+    nt->nt_output(nt, rsp);
+
+    return (0);
+err:
+    return (rc);
+}
+
+
+void
+nmgr_process(struct nmgr_transport *nt)
+{
+    struct os_mbuf *m;
+
+    while (1) {
+        m = os_mqueue_get(&nt->nt_imq);
+        if (!m) {
+            break;
+        }
+
+        nmgr_handle_req(nt, m);
+    } 
+}
+
+void
+nmgr_task(void *arg)
+{
+    struct nmgr_transport *nt;
+    struct os_event *ev;
+
+    while (1) {
+        ev = os_eventq_get(&g_nmgr_evq);
+        switch (ev->ev_type) {
+            case OS_EVENT_T_MQUEUE_DATA:
+                nt = (struct nmgr_transport *) ev->ev_arg;
+                nmgr_process(nt);
+                break;
+        }
+    }
+}
+
+int 
+nmgr_transport_init(struct nmgr_transport *nt, 
+        nmgr_transport_out_func_t output_func)
+{
+    int rc;
+
+    nt->nt_output = output_func;
+
+    rc = os_mqueue_init(&nt->nt_imq, nt);
+    if (rc != 0) {
+        goto err;
+    }
+
+    return (0);
+err:
+    return (rc);
+}
+
+static int 
+nmgr_shell_out(struct nmgr_transport *nt, struct os_mbuf *m)
+{
+    int rc;
+
+    rc = shell_nlip_output(m);
+    if (rc != 0) {
+        goto err;
+    }
+
+    return (0);
+err:
+    return (rc);
+}
+
+int 
+nmgr_shell_in(struct os_mbuf *m, void *arg)
+{
+    struct nmgr_transport *nt;
+    int rc;
+
+    nt = (struct nmgr_transport *) arg;
+
+    rc = os_mqueue_put(&nt->nt_imq, &g_nmgr_evq, m);
+    if (rc != 0) {
+        goto err;
+    }
+
+    return (0);
+err:
+    return (rc);
+}
+
+    
+int 
+nmgr_task_init(uint8_t prio, os_stack_t *stack_ptr, uint16_t stack_len)
+{
+    int rc;
+
+    rc = os_eventq_init(&g_nmgr_evq);
+    if (rc != 0) {
+        goto err;
+    }
+    
+    rc = nmgr_transport_init(&g_nmgr_shell_transport, nmgr_shell_out);
+    if (rc != 0) {
+        goto err;
+    }
+
+    rc = shell_nlip_input_register(nmgr_shell_in, 
+            (void *) &g_nmgr_shell_transport);
+    if (rc != 0) {
+        goto err;
+    }
+
+    rc = os_task_init(&nmgr_task, "newtmgr", nmgr_task, NULL, prio, 
+            OS_WAIT_FOREVER, stack_ptr, stack_len);
+    if (rc != 0) {
+        goto err;
+    }
+
+    return (0);
+err:
+    return (rc);
+}
+
+

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/3568eb51/libs/util/include/util/base64.h
----------------------------------------------------------------------
diff --git a/libs/util/include/util/base64.h b/libs/util/include/util/base64.h
new file mode 100644
index 0000000..d6baca5
--- /dev/null
+++ b/libs/util/include/util/base64.h
@@ -0,0 +1,24 @@
+/**
+ * Copyright (c) 2015 Runtime Inc.
+ *
+ * Licensed 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 __UTIL_BASE64_H 
+#define __UTIL_BASE64_H 
+
+int base64_encode(const void *, int, char *, uint8_t);
+int base64_decode(const char *, void *buf);
+
+#define BASE64_ENCODE_SIZE(size) (((__size) * (4 / 3)) + 4)
+
+#endif /* __UTIL_BASE64_H__ */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/3568eb51/libs/util/src/base64.c
----------------------------------------------------------------------
diff --git a/libs/util/src/base64.c b/libs/util/src/base64.c
new file mode 100644
index 0000000..3b07362
--- /dev/null
+++ b/libs/util/src/base64.c
@@ -0,0 +1,167 @@
+/**
+ * Copyright (c) 2015 Runtime Inc.
+ *
+ * Licensed 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.
+ */
+
+/* 
+ * This file is based on roken from the FreeBSD source.  It has been modified
+ * to not use malloc() and instead expect static buffers, and tabs have been 
+ * replaced with spaces.  Also, instead of strlen() on the resulting string, 
+ * pointer arithmitic is done, as p represents the end of the buffer.
+ */
+
+/* 
+ * Copyright (c) 1995-2001 Kungliga Tekniska Högskolan
+ * (Royal Institute of Technology, Stockholm, Sweden).
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * 3. Neither the name of the Institute nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <stdlib.h>
+#include <string.h>
+
+#include <util/base64.h>
+
+static const char base64_chars[] = 
+    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
+
+static int 
+pos(char c)
+{
+    const char *p;
+    for (p = base64_chars; *p; p++)
+        if (*p == c)
+            return p - base64_chars;
+    return -1;
+}
+
+int 
+base64_encode(const void *data, int size, char *s, uint8_t should_pad)
+{
+    char *p;
+    int i;
+    int c;
+    const unsigned char *q;
+    char *last;
+    int diff;
+
+    p = s;
+
+    q = (const unsigned char *) data;
+    last = NULL;
+    i = 0;
+    while (i < size) {
+        c = q[i++];
+        c *= 256;
+        if (i < size)
+            c += q[i];
+        i++;
+        c *= 256;
+        if (i < size)
+            c += q[i];
+        i++;
+        p[0] = base64_chars[(c & 0x00fc0000) >> 18];
+        p[1] = base64_chars[(c & 0x0003f000) >> 12];
+        p[2] = base64_chars[(c & 0x00000fc0) >> 6];
+        p[3] = base64_chars[(c & 0x0000003f) >> 0];
+        last = p;
+        p += 4;
+    }
+
+    if (last) {
+        diff = i - size;
+        if (diff > 0) {
+            if (should_pad) {
+                memset(last + (4 - diff), '=', diff);
+            } else {
+                p = last + (4 - diff);
+            }
+        }
+    } 
+
+    *p = 0;
+
+    return (p - s);
+}
+
+#define DECODE_ERROR -1
+
+static unsigned int
+token_decode(const char *token)
+{
+    int i;
+    unsigned int val = 0;
+    int marker = 0;
+    if (strlen(token) < 4)
+        return DECODE_ERROR;
+    for (i = 0; i < 4; i++) {
+        val *= 64;
+        if (token[i] == '=')
+            marker++;
+        else if (marker > 0)
+            return DECODE_ERROR;
+        else
+            val += pos(token[i]);
+    }
+    if (marker > 2)
+        return DECODE_ERROR;
+    return (marker << 24) | val;
+}
+
+int 
+base64_decode(const char *str, void *data)
+{
+    const char *p;
+    unsigned char *q;
+
+    q = data;
+    for (p = str; *p && (*p == '=' || strchr(base64_chars, *p)); p += 4) {
+        unsigned int val = token_decode(p);
+        unsigned int marker = (val >> 24) & 0xff;
+        if (val == DECODE_ERROR)
+            return -1;
+        *q++ = (val >> 16) & 0xff;
+        if (marker < 2)
+            *q++ = (val >> 8) & 0xff;
+        if (marker < 1)
+            *q++ = val & 0xff;
+    }
+    return q - (unsigned char *) data;
+}