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/15 07:48:33 UTC
incubator-mynewt-larva git commit: add support for newtmgr echo to
the main blinky project.
Repository: incubator-mynewt-larva
Updated Branches:
refs/heads/master 2254c0aa9 -> b19b6c7f0
add support for newtmgr echo to the main blinky project.
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/b19b6c7f
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/tree/b19b6c7f
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/diff/b19b6c7f
Branch: refs/heads/master
Commit: b19b6c7f01b7fde72cd28c15091dab89c7fcf333
Parents: 2254c0a
Author: Sterling Hughes <st...@apache.org>
Authored: Mon Dec 14 22:46:19 2015 -0800
Committer: Sterling Hughes <st...@apache.org>
Committed: Mon Dec 14 22:46:34 2015 -0800
----------------------------------------------------------------------
libs/newtmgr/egg.yml | 3 +
libs/newtmgr/include/newtmgr/newtmgr.h | 32 ++++---
libs/newtmgr/src/newtmgr.c | 130 ++++++++++++++++++++++++----
libs/os/src/os_mbuf.c | 6 +-
libs/shell/src/shell.c | 2 +-
libs/util/src/stats.c | 3 +
project/blinky/blinky.yml | 1 +
project/blinky/egg.yml | 1 +
project/blinky/src/main.c | 29 +++++++
9 files changed, 179 insertions(+), 28 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/b19b6c7f/libs/newtmgr/egg.yml
----------------------------------------------------------------------
diff --git a/libs/newtmgr/egg.yml b/libs/newtmgr/egg.yml
index 086e911..719abd2 100644
--- a/libs/newtmgr/egg.yml
+++ b/libs/newtmgr/egg.yml
@@ -2,4 +2,7 @@ egg.name: libs/newtmgr
egg.vers: 0.1
egg.deps:
- libs/os
+ - libs/util
- libs/testutil
+egg.deps.SHELL:
+ - libs/shell
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/b19b6c7f/libs/newtmgr/include/newtmgr/newtmgr.h
----------------------------------------------------------------------
diff --git a/libs/newtmgr/include/newtmgr/newtmgr.h b/libs/newtmgr/include/newtmgr/newtmgr.h
index f961e6e..5abc69f 100644
--- a/libs/newtmgr/include/newtmgr/newtmgr.h
+++ b/libs/newtmgr/include/newtmgr/newtmgr.h
@@ -17,7 +17,11 @@
#ifndef _NEWTMGR_H_
#define _NEWTMGR_H_
-#include <util/tpq.h>
+/* First 64 groups are reserved for system level newtmgr commands.
+ * Per-user commands are then defined after group 64.
+ */
+#define NMGR_GROUP_ID_DEFAULT (0)
+#define NMGR_GROUP_ID_PERUSER (64)
#define NMGR_OP_READ (0)
#define NMGR_OP_READ_RSP (1)
@@ -32,17 +36,16 @@ struct nmgr_hdr {
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);
+typedef int (*nmgr_handler_func_t)(struct nmgr_hdr *nmr, struct os_mbuf *req,
+ uint16_t srcoff, struct nmgr_hdr *rsp_hdr, struct os_mbuf *rsp);
-struct nmgr_handler {
- nmgr_handle_func_t nh_read;
- nmgr_handle_func_t nh_write;
-};
+#define NMGR_HANDLER_FUNC(__name) \
+ int __name(struct nmgr_hdr *nmr, struct os_mbuf *req, uint16_t srcoff, \
+ struct os_mbuf *rsp)
-struct nmgr_pkt {
- struct tpq_element np_tpq_elem;
- struct os_mbuf np_m;
+struct nmgr_handler {
+ nmgr_handler_func_t nh_read;
+ nmgr_handler_func_t nh_write;
};
struct nmgr_group {
@@ -52,6 +55,12 @@ struct nmgr_group {
STAILQ_ENTRY(nmgr_group) ng_next;
};
+#define NMGR_GROUP_SET_HANDLERS(__group, __handlers) \
+ (__group)->ng_handlers = (__handlers); \
+ (__group)->ng_handlers_count = (sizeof((__handlers)) / \
+ sizeof(struct nmgr_handler));
+
+struct nmgr_transport;
typedef int (*nmgr_transport_out_func_t)(struct nmgr_transport *nt,
struct os_mbuf *m);
@@ -60,4 +69,7 @@ struct nmgr_transport {
nmgr_transport_out_func_t nt_output;
};
+int nmgr_task_init(uint8_t, os_stack_t *, uint16_t);
+int nmgr_rsp_extend(struct nmgr_hdr *, struct os_mbuf *, void *data, uint16_t);
+
#endif /* _NETMGR_H */
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/b19b6c7f/libs/newtmgr/src/newtmgr.c
----------------------------------------------------------------------
diff --git a/libs/newtmgr/src/newtmgr.c b/libs/newtmgr/src/newtmgr.c
index fad6bb6..998a4ff 100644
--- a/libs/newtmgr/src/newtmgr.c
+++ b/libs/newtmgr/src/newtmgr.c
@@ -16,9 +16,14 @@
#include <os/os.h>
+#include <assert.h>
+
+#include <shell/shell.h>
+#include <newtmgr/newtmgr.h>
+
#include <string.h>
-struct nmgr_transport g_nmgr_transport;
+struct nmgr_transport g_nmgr_shell_transport;
struct os_mutex g_nmgr_group_list_lock;
@@ -26,7 +31,41 @@ 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);
+ STAILQ_HEAD_INITIALIZER(g_nmgr_group_list);
+
+static int nmgr_def_echo(struct nmgr_hdr *, struct os_mbuf *, uint16_t,
+ struct nmgr_hdr *, struct os_mbuf *);
+
+static struct nmgr_group nmgr_def_group;
+/* ORDER MATTERS HERE.
+ * Each element represents the command ID, referenced from newtmgr.
+ */
+static struct nmgr_handler nmgr_def_group_handlers[] = {
+ {nmgr_def_echo, nmgr_def_echo}
+};
+
+
+static int
+nmgr_def_echo(struct nmgr_hdr *nmr, struct os_mbuf *req, uint16_t srcoff,
+ struct nmgr_hdr *rsp_hdr, struct os_mbuf *rsp)
+{
+ uint8_t echo_buf[128];
+ int rc;
+
+ rc = os_mbuf_copydata(req, srcoff + sizeof(*nmr), nmr->nh_len, echo_buf);
+ if (rc != 0) {
+ goto err;
+ }
+
+ rc = nmgr_rsp_extend(rsp_hdr, rsp, echo_buf, nmr->nh_len);
+ if (rc != 0) {
+ goto err;
+ }
+
+ return (0);
+err:
+ return (rc);
+}
int
nmgr_group_list_lock(void)
@@ -37,7 +76,7 @@ nmgr_group_list_lock(void)
return (0);
}
- rc = os_mutex_pend(&g_nmgr_group_list_lock);
+ rc = os_mutex_pend(&g_nmgr_group_list_lock, OS_WAIT_FOREVER);
if (rc != 0) {
goto err;
}
@@ -125,35 +164,53 @@ 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);
+ group = nmgr_find_group(group_id);
if (!group) {
goto err;
}
- if (hdr->nh_id > group->ng_handlers_count) {
+ if (handler_id > group->ng_handlers_count) {
goto err;
}
- handler = &group->ng_handlers[hdr->nh_id];
+ handler = &group->ng_handlers[handler_id];
return (handler);
err:
return (NULL);
}
+int
+nmgr_rsp_extend(struct nmgr_hdr *hdr, struct os_mbuf *rsp, void *data,
+ uint16_t len)
+{
+ int rc;
+
+ rc = os_mbuf_append(rsp, data, len);
+ if (rc != 0) {
+ goto err;
+ }
+ hdr->nh_len += len;
+
+ return (0);
+err:
+ return (rc);
+}
static int
nmgr_handle_req(struct nmgr_transport *nt, struct os_mbuf *req)
{
struct os_mbuf *rsp;
struct nmgr_handler *handler;
+ struct nmgr_hdr *rsp_hdr;
struct nmgr_hdr hdr;
uint32_t off;
uint32_t len;
+ int rc;
rsp = os_msys_get_pkthdr(512, 0);
if (!rsp) {
- rc = EINVAL;
+ rc = OS_EINVAL;
goto err;
}
@@ -163,7 +220,7 @@ nmgr_handle_req(struct nmgr_transport *nt, struct os_mbuf *req)
while (off < len) {
rc = os_mbuf_copydata(req, off, sizeof(hdr), &hdr);
if (rc < 0) {
- rc = EINVAL;
+ rc = OS_EINVAL;
goto err;
}
@@ -173,19 +230,40 @@ nmgr_handle_req(struct nmgr_transport *nt, struct os_mbuf *req)
handler = nmgr_find_handler(hdr.nh_group, hdr.nh_id);
if (!handler) {
- rc = EINVAL;
+ rc = OS_EINVAL;
+ goto err;
+ }
+
+ /* Build response header apriori. Then pass to the handlers
+ * to fill out the response data, and adjust length & flags.
+ */
+ rsp_hdr = (struct nmgr_hdr *) os_mbuf_extend(rsp,
+ sizeof(struct nmgr_hdr));
+ if (!rsp_hdr) {
+ rc = OS_EINVAL;
goto err;
}
+ rsp_hdr->nh_len = 0;
+ rsp_hdr->nh_flags = 0;
+ rsp_hdr->nh_op = (hdr.nh_op == NMGR_OP_READ) ? NMGR_OP_READ_RSP :
+ NMGR_OP_WRITE_RSP;
+ rsp_hdr->nh_group = hdr.nh_group;
+ rsp_hdr->nh_id = hdr.nh_id;
+
if (hdr.nh_op == NMGR_OP_READ) {
- rc = handler->nh_read(&hdr, req, off, rsp);
+ rc = handler->nh_read(&hdr, req, off, rsp_hdr, rsp);
} else if (hdr.nh_op == NMGR_OP_WRITE) {
- rc = handler->nh_write(&hdr, req, off, rsp);
+ rc = handler->nh_write(&hdr, req, off, rsp_hdr, rsp);
} else {
rc = OS_EINVAL;
goto err;
}
+ rsp_hdr->nh_len = htons(rsp_hdr->nh_len);
+ rsp_hdr->nh_group = htons(rsp_hdr->nh_group);
+ rsp_hdr->nh_id = htons(rsp_hdr->nh_id);
+
off += sizeof(hdr) + OS_ALIGN(hdr.nh_len, 4);
}
@@ -280,16 +358,31 @@ err:
return (rc);
}
-
-int
-nmgr_task_init(uint8_t prio, os_stack_t *stack_ptr, uint16_t stack_len)
+
+static int
+nmgr_default_groups_register(void)
{
int rc;
- rc = os_eventq_init(&g_nmgr_evq);
+ NMGR_GROUP_SET_HANDLERS(&nmgr_def_group, nmgr_def_group_handlers);
+ nmgr_def_group.ng_group_id = NMGR_GROUP_ID_DEFAULT;
+
+ rc = nmgr_group_register(&nmgr_def_group);
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;
+
+ os_eventq_init(&g_nmgr_evq);
rc = nmgr_transport_init(&g_nmgr_shell_transport, nmgr_shell_out);
if (rc != 0) {
@@ -302,12 +395,17 @@ nmgr_task_init(uint8_t prio, os_stack_t *stack_ptr, uint16_t stack_len)
goto err;
}
- rc = os_task_init(&nmgr_task, "newtmgr", nmgr_task, NULL, prio,
+ rc = os_task_init(&g_nmgr_task, "newtmgr", nmgr_task, NULL, prio,
OS_WAIT_FOREVER, stack_ptr, stack_len);
if (rc != 0) {
goto err;
}
+ rc = nmgr_default_groups_register();
+ if (rc != 0) {
+ goto err;
+ }
+
return (0);
err:
return (rc);
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/b19b6c7f/libs/os/src/os_mbuf.c
----------------------------------------------------------------------
diff --git a/libs/os/src/os_mbuf.c b/libs/os/src/os_mbuf.c
index fc57fc8..f14466a 100644
--- a/libs/os/src/os_mbuf.c
+++ b/libs/os/src/os_mbuf.c
@@ -87,7 +87,11 @@ os_mqueue_get(struct os_mqueue *mq)
}
OS_EXIT_CRITICAL(sr);
- m = OS_MBUF_PKTHDR_TO_MBUF(mp);
+ if (mp) {
+ m = OS_MBUF_PKTHDR_TO_MBUF(mp);
+ } else {
+ m = NULL;
+ }
return (m);
}
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/b19b6c7f/libs/shell/src/shell.c
----------------------------------------------------------------------
diff --git a/libs/shell/src/shell.c b/libs/shell/src/shell.c
index c0d57de..15304ee 100644
--- a/libs/shell/src/shell.c
+++ b/libs/shell/src/shell.c
@@ -267,8 +267,8 @@ shell_nlip_mtx(struct os_mbuf *m)
if (nwritten != 0 &&
((nwritten + BASE64_ENCODE_SIZE(dlen)) % 122) == 0) {
- console_write((char *) esc_seq, sizeof(esc_seq));
console_write("\n", sizeof("\n")-1);
+ console_write((char *) esc_seq, sizeof(esc_seq));
}
rc = os_mbuf_copydata(m, off, dlen, readbuf);
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/b19b6c7f/libs/util/src/stats.c
----------------------------------------------------------------------
diff --git a/libs/util/src/stats.c b/libs/util/src/stats.c
index 0d2eb36..e61d785 100644
--- a/libs/util/src/stats.c
+++ b/libs/util/src/stats.c
@@ -43,6 +43,7 @@ uint8_t stats_shell_registered;
struct shell_cmd shell_stats_cmd;
#endif
+
#ifdef SHELL_PRESENT
static void
@@ -130,6 +131,7 @@ done:
#endif
+
int
stats_module_init(void)
{
@@ -141,6 +143,7 @@ stats_module_init(void)
}
#endif
+
rc = stats_init(STATS_HDR(stats), STATS_SIZE_INIT_PARMS(stats, STATS_SIZE_32),
STATS_NAME_INIT_PARMS(stats));
if (rc != 0) {
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/b19b6c7f/project/blinky/blinky.yml
----------------------------------------------------------------------
diff --git a/project/blinky/blinky.yml b/project/blinky/blinky.yml
index 5bd9134..4fcdd7d 100644
--- a/project/blinky/blinky.yml
+++ b/project/blinky/blinky.yml
@@ -2,5 +2,6 @@ project.name: blinky
project.eggs:
- libs/console/full
- libs/shell
+ - libs/newtmgr
- libs/util
- libs/os
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/b19b6c7f/project/blinky/egg.yml
----------------------------------------------------------------------
diff --git a/project/blinky/egg.yml b/project/blinky/egg.yml
index 1a4dad7..34612a0 100644
--- a/project/blinky/egg.yml
+++ b/project/blinky/egg.yml
@@ -4,4 +4,5 @@ egg.deps:
- libs/os
- libs/console/full
- libs/shell
+ - libs/newtmgr
- hw/hal
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/b19b6c7f/project/blinky/src/main.c
----------------------------------------------------------------------
diff --git a/project/blinky/src/main.c b/project/blinky/src/main.c
index 0ca3998..5cad08b 100755
--- a/project/blinky/src/main.c
+++ b/project/blinky/src/main.c
@@ -20,6 +20,7 @@
#include "shell/shell.h"
#include "util/log.h"
#include "util/stats.h"
+#include <newtmgr/newtmgr.h>
#include <assert.h>
#include <string.h>
#ifdef ARCH_sim
@@ -47,6 +48,10 @@ os_stack_t stack2[TASK2_STACK_SIZE];
#define SHELL_TASK_STACK_SIZE (OS_STACK_ALIGN(1024))
os_stack_t shell_stack[SHELL_TASK_STACK_SIZE];
+#define NEWTMGR_TASK_PRIO (4)
+#define NEWTMGR_TASK_STACK_SIZE (OS_STACK_ALIGN(1024))
+os_stack_t newtmgr_stack[NEWTMGR_TASK_STACK_SIZE];
+
struct cbmem log_mem;
struct ul_handler log_mem_handler;
struct util_log my_log;
@@ -60,6 +65,16 @@ struct os_sem g_test_sem;
/* For LED toggling */
int g_led_pin;
+#define DEFAULT_MBUF_MPOOL_BUF_LEN (256)
+#define DEFAULT_MBUF_MPOOL_NBUFS (5)
+
+uint8_t default_mbuf_mpool_data[DEFAULT_MBUF_MPOOL_BUF_LEN *
+ DEFAULT_MBUF_MPOOL_NBUFS];
+
+struct os_mbuf_pool default_mbuf_pool;
+struct os_mempool default_mbuf_mpool;
+
+
void
task1_handler(void *arg)
{
@@ -160,6 +175,20 @@ main(int argc, char **argv)
os_init();
+ rc = os_mempool_init(&default_mbuf_mpool, DEFAULT_MBUF_MPOOL_NBUFS,
+ DEFAULT_MBUF_MPOOL_BUF_LEN, default_mbuf_mpool_data,
+ "default_mbuf_data");
+ assert(rc == 0);
+
+ rc = os_mbuf_pool_init(&default_mbuf_pool, &default_mbuf_mpool,
+ DEFAULT_MBUF_MPOOL_BUF_LEN, DEFAULT_MBUF_MPOOL_NBUFS);
+ assert(rc == 0);
+
+ rc = os_msys_register(&default_mbuf_pool);
+ assert(rc == 0);
+
+ nmgr_task_init(NEWTMGR_TASK_PRIO, newtmgr_stack, NEWTMGR_TASK_STACK_SIZE);
+
shell_task_init(SHELL_TASK_PRIO, shell_stack, SHELL_TASK_STACK_SIZE);
(void) console_init(shell_console_rx_cb);