mynewt-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ccoll...@apache.org
Subject [mynewt-mcumgr] 06/08: SMP transport for zephyr
Date Thu, 04 Jan 2018 03:01:20 GMT
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-mcumgr.git

commit 916a93cfcd3683244e826073abe9640284fb0f28
Author: Christopher Collins <ccollins@apache.org>
AuthorDate: Wed Jan 3 17:23:43 2018 -0800

    SMP transport for zephyr
---
 smp/include/smp/smp.h                           |   1 -
 smp/port/zephyr/include/zephyr_smp/zephyr_smp.h |  37 ++++
 smp/port/zephyr/src/zephyr_smp.c                | 234 ++++++++++++++++++++++++
 3 files changed, 271 insertions(+), 1 deletion(-)

diff --git a/smp/include/smp/smp.h b/smp/include/smp/smp.h
index 2feaa47..15e0b40 100644
--- a/smp/include/smp/smp.h
+++ b/smp/include/smp/smp.h
@@ -28,7 +28,6 @@
 extern "C" {
 #endif
 
-struct mynewt_smp_transport;
 struct smp_streamer;
 struct mgmt_hdr;
 
diff --git a/smp/port/zephyr/include/zephyr_smp/zephyr_smp.h b/smp/port/zephyr/include/zephyr_smp/zephyr_smp.h
new file mode 100644
index 0000000..d74e8c4
--- /dev/null
+++ b/smp/port/zephyr/include/zephyr_smp/zephyr_smp.h
@@ -0,0 +1,37 @@
+#ifndef H_ZEPHYR_SMP_
+#define H_ZEPHYR_SMP_
+
+struct zephyr_smp_transport;
+
+/* XXX: Note: `zephyr_nmgr_pkt` is only used here during development.  Most
+ * likely, a net_buf will be used instead.
+ */
+struct zephyr_nmgr_pkt;
+
+/**
+ * Transmit function.  The supplied mbuf is always consumed, regardless of
+ * return code.
+ */
+typedef int zephyr_smp_transport_out_fn(struct zephyr_smp_transport *zst,
+                                        struct zephyr_nmgr_pkt *pkt);
+typedef uint16_t zephyr_smp_transport_get_mtu_fn(void);
+
+struct zephyr_smp_transport {
+    /* Must be the first member. */
+    struct k_work zst_work;
+
+    /* FIFO containing incoming requests to be processed. */
+    struct k_fifo zst_fifo;
+
+    zephyr_smp_transport_out_fn *zst_output;
+    zephyr_smp_transport_get_mtu_fn *zst_get_mtu;
+};
+
+void zephyr_smp_transport_init(struct zephyr_smp_transport *zst,
+                               zephyr_smp_transport_out_fn *output_func,
+                               zephyr_smp_transport_get_mtu_fn *get_mtu_func);
+
+int zephyr_smp_rx_req(struct zephyr_smp_transport *zst,
+                      struct zephyr_nmgr_pkt *pkt);
+
+#endif
diff --git a/smp/port/zephyr/src/zephyr_smp.c b/smp/port/zephyr/src/zephyr_smp.c
new file mode 100644
index 0000000..2262397
--- /dev/null
+++ b/smp/port/zephyr/src/zephyr_smp.c
@@ -0,0 +1,234 @@
+#include <zephyr.h>
+#include "mgmt/mgmt.h"
+#include "smp/smp.h"
+#include "znp/znp.h"
+#include "zephyr_smp/zephyr_smp.h"
+
+static mgmt_alloc_rsp_fn zephyr_smp_alloc_rsp;
+static mgmt_trim_front_fn zephyr_smp_trim_front;
+static mgmt_reset_buf_fn zephyr_smp_reset_buf;
+static mgmt_write_at_fn zephyr_smp_write_at;
+static mgmt_init_reader_fn zephyr_smp_init_reader;
+static mgmt_init_writer_fn zephyr_smp_init_writer;
+static mgmt_free_buf_fn zephyr_smp_free_buf;
+static smp_tx_rsp_fn zephyr_smp_tx_rsp;
+
+static const struct mgmt_streamer_cfg zephyr_smp_cbor_cfg = {
+    .alloc_rsp = zephyr_smp_alloc_rsp,
+    .trim_front = zephyr_smp_trim_front,
+    .reset_buf = zephyr_smp_reset_buf,
+    .write_at = zephyr_smp_write_at,
+    .init_reader = zephyr_smp_init_reader,
+    .init_writer = zephyr_smp_init_writer,
+    .free_buf = zephyr_smp_free_buf,
+};
+
+#if 0
+/**
+ * Allocates an mbuf to contain an outgoing response fragment.
+ */
+static struct os_mbuf *
+zephyr_smp_rsp_frag_alloc(uint16_t frag_size, void *arg)
+{
+    struct os_mbuf *src_rsp;
+    struct os_mbuf *frag;
+
+    /* We need to duplicate the user header from the source response, as that
+     * is where transport-specific information is stored.
+     */
+    src_rsp = arg;
+
+    frag = os_msys_get_pkthdr(frag_size, OS_MBUF_USRHDR_LEN(src_rsp));
+    if (frag != NULL) {
+        /* Copy the user header from the response into the fragment mbuf. */
+        memcpy(OS_MBUF_USRHDR(frag), OS_MBUF_USRHDR(src_rsp),
+               OS_MBUF_USRHDR_LEN(src_rsp));
+    }
+
+    return frag;
+}
+#endif
+
+static void *
+zephyr_smp_alloc_rsp(const void *req, void *arg)
+{
+    const struct zephyr_nmgr_pkt *req_pkt;
+    struct zephyr_nmgr_pkt *rsp_pkt;
+
+    req_pkt = req;
+
+    rsp_pkt = k_malloc(sizeof *rsp_pkt);
+    if (rsp_pkt == NULL) {
+        assert(0);
+        return NULL;
+    }
+    rsp_pkt->len = 0;
+    rsp_pkt->extra = req_pkt->extra;
+
+    return rsp_pkt;
+}
+
+static int
+zephyr_smp_trim_front(void *buf, int len, void *arg)
+{
+    struct zephyr_nmgr_pkt *pkt;
+
+    if (len > 0) {
+        pkt = buf;
+        if (len >= pkt->len) {
+            pkt->len = 0;
+        } else {
+            memmove(pkt->data, pkt->data + len, pkt->len - len);
+            pkt->len -= len;
+        }
+    }
+
+    return 0;
+}
+
+static void
+zephyr_smp_reset_buf(void *buf, void *arg)
+{
+    struct zephyr_nmgr_pkt *pkt;
+
+    pkt = buf;
+    pkt->len = 0;
+}
+
+static int
+zephyr_smp_write_at(struct cbor_encoder_writer *writer, int offset,
+                     const void *data, int len, void *arg)
+{
+    struct cbor_znp_writer *czw;
+    struct zephyr_nmgr_pkt *pkt;
+
+    czw = (struct cbor_znp_writer *)writer;
+    pkt = czw->pkt;
+
+    if (offset < 0 || offset > pkt->len) {
+        return MGMT_ERR_EINVAL;
+    }
+
+    if (offset + len > sizeof pkt->data) {
+        return MGMT_ERR_EINVAL;
+    }
+
+    memcpy(pkt->data + offset, data, len);
+    if (pkt->len < offset + len) {
+        pkt->len = offset + len;
+        writer->bytes_written = pkt->len;
+    }
+
+    return 0;
+}
+
+static int
+zephyr_smp_tx_rsp(struct smp_streamer *ns, void *rsp, void *arg)
+{
+    struct zephyr_smp_transport *zst;
+    struct zephyr_nmgr_pkt *pkt;
+    int rc;
+
+    zst = arg;
+    pkt = rsp;
+
+    rc = zst->zst_output(zst, pkt);
+    if (rc != 0) {
+        return MGMT_ERR_EUNKNOWN;
+    }
+
+    return MGMT_ERR_EOK;
+}
+
+static void
+zephyr_smp_free_buf(void *buf, void *arg)
+{
+    k_free(buf);
+}
+
+static int
+zephyr_smp_init_reader(struct cbor_decoder_reader *reader, void *buf,
+                        void *arg)
+{
+    struct cbor_znp_reader *czr;
+
+    czr = (struct cbor_znp_reader *)reader;
+    cbor_znp_reader_init(czr, buf, 0);
+
+    return 0;
+}
+
+static int
+zephyr_smp_init_writer(struct cbor_encoder_writer *writer, void *buf,
+                        void *arg)
+{
+    struct cbor_znp_writer *czw;
+
+    czw = (struct cbor_znp_writer *)writer;
+    cbor_znp_writer_init(czw, buf);
+
+    return 0;
+}
+
+static int
+zephyr_smp_process_packet(struct zephyr_smp_transport *zst,
+                          struct zephyr_nmgr_pkt *pkt)
+{
+    struct cbor_znp_reader reader;
+    struct cbor_znp_writer writer;
+    struct smp_streamer streamer;
+    int rc;
+
+    streamer = (struct smp_streamer) {
+        .ss_base = {
+            .cfg = &zephyr_smp_cbor_cfg,
+            .reader = &reader.r,
+            .writer = &writer.enc,
+            .cb_arg = zst,
+        },
+        .ss_tx_rsp = zephyr_smp_tx_rsp,
+    };
+
+    rc = smp_process_single_packet(&streamer, pkt);
+    return rc;
+}
+
+static void
+zephyr_smp_handle_reqs(struct k_work *work)
+{
+    struct zephyr_smp_transport *zst;
+    struct zephyr_nmgr_pkt *pkt;
+
+    zst = (void *)work;
+
+    while ((pkt = k_fifo_get(&zst->zst_fifo, K_NO_WAIT)) != NULL) {
+        zephyr_smp_process_packet(zst, pkt);
+    }
+}
+
+void
+zephyr_smp_transport_init(struct zephyr_smp_transport *zst,
+                          zephyr_smp_transport_out_fn *output_func,
+                          zephyr_smp_transport_get_mtu_fn *get_mtu_func)
+{
+    *zst = (struct zephyr_smp_transport) {
+        .zst_output = output_func,
+        .zst_get_mtu = get_mtu_func,
+    };
+
+    k_work_init(&zst->zst_work, zephyr_smp_handle_reqs);
+    k_fifo_init(&zst->zst_fifo);
+}
+
+/* XXX: Note: `zephyr_nmgr_pkt` is only used here during development.  Most
+ * likely, a net_buf will be used instead.
+ */
+int
+zephyr_smp_rx_req(struct zephyr_smp_transport *zst,
+                  struct zephyr_nmgr_pkt *pkt)
+{
+    k_fifo_put(&zst->zst_fifo, pkt);
+    k_work_submit(&zst->zst_work);
+
+    return 0;
+}

-- 
To stop receiving notification emails like this one, please contact
"commits@mynewt.apache.org" <commits@mynewt.apache.org>.

Mime
View raw message