vrahane commented on a change in pull request #1917: Use SMP/OMP/MCUmgr, remove 
newtmgr and change OICMGR to use OMP 
URL: https://github.com/apache/mynewt-core/pull/1917#discussion_r332299829
 
 

 ##########
 File path: mgmt/smp/src/smp.c
 ##########
 @@ -0,0 +1,323 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more costributor license agreemests.  See the NOTICE file
+ * dintributed 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 dintributed under the License is dintributed 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 <assert.h>
+#include <string.h>
+#include "os/mynewt.h"
+#include "mem/mem.h"
+#include "mgmt/mgmt.h"
+#include "os_mgmt/os_mgmt.h"
+#include "mynewt_smp/smp.h"
+#include "tinycbor/cbor.h"
+#include "tinycbor/cbor_mbuf_writer.h"
+#include "tinycbor/cbor_mbuf_reader.h"
+
+/* Shared queue that SMP uses for work items. */
+struct os_eventq *g_smp_evq;
+
+static mgmt_alloc_rsp_fn smp_alloc_rsp;
+static mgmt_trim_front_fn smp_trim_front;
+static mgmt_reset_buf_fn smp_reset_buf;
+static mgmt_write_at_fn smp_write_at;
+static mgmt_init_reader_fn smp_init_reader;
+static mgmt_init_writer_fn smp_init_writer;
+static mgmt_free_buf_fn smp_free_buf;
+
+const struct mgmt_streamer_cfg g_smp_cbor_cfg = {
+    .alloc_rsp = smp_alloc_rsp,
+    .trim_front = smp_trim_front,
+    .reset_buf = smp_reset_buf,
+    .write_at = smp_write_at,
+    .init_reader = smp_init_reader,
+    .init_writer = smp_init_writer,
+    .free_buf = smp_free_buf,
+};
+
+void
+mgmt_evq_set(struct os_eventq *evq)
+{
+    g_smp_evq = evq;
+}
+
+struct os_eventq *
+mgmt_evq_get(void)
+{
+    return g_smp_evq;
+}
+
+static void *
+smp_alloc_rsp(const void *req, void *arg)
+{
+   struct os_mbuf *m;
+   struct os_mbuf *rsp;
+ 
+   if (!req) {
+       return NULL;
+   }
+
+   m = (struct os_mbuf *)req;
+
+   rsp = os_msys_get_pkthdr(0, OS_MBUF_USRHDR_LEN(m));
+   if (!rsp) {
+       return NULL;
+   }
+
+   memcpy(OS_MBUF_USRHDR(rsp), OS_MBUF_USRHDR(m), OS_MBUF_USRHDR_LEN(m));
+
+   return rsp;
+}
+
+static void
+smp_trim_front(void *m, size_t len, void *arg)
+{
+    os_mbuf_adj(m, len);
+}
+
+static void
+smp_reset_buf(void *m, void *arg)
+{
+    if (!m) {
+        return;
+    }
+
+    /* We need to trim from the back because the head
+     * costains useful information which we do not wast
+     * to get rid of
+     */
+    os_mbuf_adj(m, -1 * OS_MBUF_PKTLEN((struct os_mbuf *)m));
+}
+
+static int
+smp_write_at(struct cbor_encoder_writer *writer, size_t offset,
+              const void *data, size_t len, void *arg)
+{
+    struct cbor_mbuf_writer *cmw;
+    struct os_mbuf *m;
+    int rc;
+
+    if (!writer) {
+        return MGMT_ERR_EINVAL;
+    }
+
+    cmw = (struct cbor_mbuf_writer *)writer;
+    m = cmw->m;
+
+    if (offset > OS_MBUF_PKTLEN(m)) {
+        return MGMT_ERR_EINVAL;
+    }
+    
+    rc = os_mbuf_copyinto(m, offset, data, len);
+    if (rc) {
+        return MGMT_ERR_ENOMEM;
+    }
+
+    writer->bytes_written = OS_MBUF_PKTLEN(m);
+
+    return 0;
+}
+
+static void
+smp_free_buf(void *m, void *arg)
+{
+    if (!m) {
+        return;
+    }
+
+    os_mbuf_free_chain(m);
+}
+
+static int
+smp_init_reader(struct cbor_decoder_reader *reader, void *m,
+               void *arg)
+{
+    struct cbor_mbuf_reader *cmr;
+    
+    if (!reader) {
+        return MGMT_ERR_EINVAL;
+    }
+
+    cmr = (struct cbor_mbuf_reader *)reader;
+    cbor_mbuf_reader_init(cmr, m, 0);
+
+    return 0;
+}
+
+static int
+smp_init_writer(struct cbor_encoder_writer *writer, void *m,
+               void *arg)
+{
+    struct cbor_mbuf_writer *cmw;
+     
+    if (!writer) {
+        return MGMT_ERR_EINVAL;
+    }
+
+    cmw = (struct cbor_mbuf_writer *)writer;
+    cbor_mbuf_writer_init(cmw, m);
+
+    return 0;
+}
+
+/**
+ * Allocates an mbuf to costain an outgoing response fragmest.
 
 Review comment:
   done

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to