You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by GitBox <gi...@apache.org> on 2018/11/08 08:27:49 UTC

[GitHub] sjanc closed pull request #16: Add event callbacks on command handlers

sjanc closed pull request #16: Add event callbacks on command handlers
URL: https://github.com/apache/mynewt-mcumgr/pull/16
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/cmd/img_mgmt/include/img_mgmt/img_mgmt.h b/cmd/img_mgmt/include/img_mgmt/img_mgmt.h
index 865964d..0f7f2bc 100644
--- a/cmd/img_mgmt/include/img_mgmt/img_mgmt.h
+++ b/cmd/img_mgmt/include/img_mgmt/img_mgmt.h
@@ -37,6 +37,13 @@ extern "C" {
 #define IMG_MGMT_ID_CORELOAD        4
 #define IMG_MGMT_ID_ERASE           5
 
+/*
+ * IMG_MGMT_ID_UPLOAD statuses.
+ */
+#define IMG_MGMT_ID_UPLOAD_STATUS_START         0
+#define IMG_MGMT_ID_UPLOAD_STATUS_ONGOING       1
+#define IMG_MGMT_ID_UPLOAD_STATUS_COMPLETE      2
+
 /**
  * @brief Registers the image management command handler group.
  */ 
diff --git a/cmd/img_mgmt/src/img_mgmt.c b/cmd/img_mgmt/src/img_mgmt.c
index 8632208..bc25d13 100644
--- a/cmd/img_mgmt/src/img_mgmt.c
+++ b/cmd/img_mgmt/src/img_mgmt.c
@@ -341,6 +341,7 @@ img_mgmt_upload_first_chunk(struct mgmt_ctxt *ctxt, const uint8_t *req_data,
 static int
 img_mgmt_upload(struct mgmt_ctxt *ctxt)
 {
+    struct mgmt_evt_op_cmd_status_arg cmd_status_arg;
     uint8_t img_mgmt_data[IMG_MGMT_UL_CHUNK_SIZE];
     uint8_t data_sha[IMG_MGMT_DATA_SHA_LEN];
     size_t data_sha_len = 0;
@@ -424,11 +425,15 @@ img_mgmt_upload(struct mgmt_ctxt *ctxt)
             return rc;
         }
         img_mgmt_ctxt.len = len;
+
+        cmd_status_arg.status = IMG_MGMT_ID_UPLOAD_STATUS_START;
     } else {
         if (!img_mgmt_ctxt.uploading) {
             return MGMT_ERR_EINVAL;
         }
 
+        cmd_status_arg.status = IMG_MGMT_ID_UPLOAD_STATUS_ONGOING;
+
         if (off != img_mgmt_ctxt.off) {
             /* Invalid offset.  Drop the data and send the expected offset. */
             return img_mgmt_encode_upload_rsp(ctxt, 0);
@@ -454,8 +459,13 @@ img_mgmt_upload(struct mgmt_ctxt *ctxt)
     if (last) {
         /* Upload complete. */
         img_mgmt_ctxt.uploading = false;
+
+        cmd_status_arg.status = IMG_MGMT_ID_UPLOAD_STATUS_COMPLETE;
     }
 
+    mgmt_evt(MGMT_EVT_OP_CMD_STATUS, MGMT_GROUP_ID_IMAGE, IMG_MGMT_ID_UPLOAD,
+             &cmd_status_arg);
+
     return img_mgmt_encode_upload_rsp(ctxt, 0);
 }
 
diff --git a/mgmt/include/mgmt/mgmt.h b/mgmt/include/mgmt/mgmt.h
index 8303d04..b17d693 100644
--- a/mgmt/include/mgmt/mgmt.h
+++ b/mgmt/include/mgmt/mgmt.h
@@ -64,6 +64,13 @@ extern "C" {
 
 #define MGMT_HDR_SIZE           8
 
+/*
+ * MGMT event opcodes.
+ */
+#define MGMT_EVT_OP_CMD_RECV            0x01
+#define MGMT_EVT_OP_CMD_STATUS          0x02
+#define MGMT_EVT_OP_CMD_DONE            0x03
+
 struct mgmt_hdr {
 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
     uint8_t  nh_op:3;           /* MGMT_OP_[...] */
@@ -80,6 +87,33 @@ struct mgmt_hdr {
     uint8_t  nh_id;             /* Message ID within group */
 };
 
+/*
+ * MGMT_EVT_OP_CMD_STATUS argument
+ */
+struct mgmt_evt_op_cmd_status_arg {
+    int status;
+};
+
+/*
+ * MGMT_EVT_OP_CMD_DONE argument
+ */
+struct mgmt_evt_op_cmd_done_arg {
+    int err;                    /* MGMT_ERR_[...] */
+};
+
+/** @typedef mgmt_on_evt_cb
+ * @brief Function to be called on MGMT event.
+ *
+ * This callback function is used to notify application about mgmt event.
+ *
+ * @param opcode                MGMT_EVT_OP_[...].
+ * @param group                 MGMT_GROUP_ID_[...].
+ * @param id                    Message ID within group.
+ * @param arg                   Optional event argument.
+ */
+typedef void mgmt_on_evt_cb(uint8_t opcode, uint16_t group, uint8_t id,
+                            void *arg);
+
 /** @typedef mgmt_alloc_rsp_fn
  * @brief Allocates a buffer suitable for holding a response.
  *
@@ -381,6 +415,23 @@ void mgmt_ntoh_hdr(struct mgmt_hdr *hdr);
  */
 void mgmt_hton_hdr(struct mgmt_hdr *hdr);
 
+/**
+ * @brief Register event callback function.
+ *
+ * @param cb                    Callback function.
+ */
+void mgmt_register_evt_cb(mgmt_on_evt_cb *cb);
+
+/**
+ * @brief This function is called to notify about mgmt event.
+ *
+ * @param opcode                MGMT_EVT_OP_[...].
+ * @param group                 MGMT_GROUP_ID_[...].
+ * @param id                    Message ID within group.
+ * @param arg                   Optional event argument.
+ */
+void mgmt_evt(uint8_t opcode, uint16_t group, uint8_t id, void *arg);
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/mgmt/src/mgmt.c b/mgmt/src/mgmt.c
index c6318ce..9287272 100644
--- a/mgmt/src/mgmt.c
+++ b/mgmt/src/mgmt.c
@@ -22,6 +22,7 @@
 #include "mgmt/endian.h"
 #include "mgmt/mgmt.h"
 
+static mgmt_on_evt_cb *evt_cb;
 static struct mgmt_group *mgmt_group_list;
 static struct mgmt_group *mgmt_group_list_end;
 
@@ -168,3 +169,17 @@ mgmt_hton_hdr(struct mgmt_hdr *hdr)
     hdr->nh_len = htons(hdr->nh_len);
     hdr->nh_group = htons(hdr->nh_group);
 }
+
+void
+mgmt_register_evt_cb(mgmt_on_evt_cb *cb)
+{
+    evt_cb = cb;
+}
+
+void
+mgmt_evt(uint8_t opcode, uint16_t group, uint8_t id, void *arg)
+{
+    if (evt_cb) {
+        evt_cb(opcode, group, id, arg);
+    }
+}
diff --git a/smp/src/smp.c b/smp/src/smp.c
index b014be5..9b929ca 100644
--- a/smp/src/smp.c
+++ b/smp/src/smp.c
@@ -153,9 +153,10 @@ smp_build_err_rsp(struct smp_streamer *streamer,
  */
 static int
 smp_handle_single_payload(struct mgmt_ctxt *cbuf,
-                          const struct mgmt_hdr *req_hdr)
+                          const struct mgmt_hdr *req_hdr, bool *handler_found)
 {
     const struct mgmt_handler *handler;
+    mgmt_handler_fn *handler_fn;
     struct CborEncoder payload_encoder;
     int rc;
 
@@ -176,25 +177,26 @@ smp_handle_single_payload(struct mgmt_ctxt *cbuf,
 
     switch (req_hdr->nh_op) {
     case MGMT_OP_READ:
-        if (handler->mh_read != NULL) {
-            rc = handler->mh_read(cbuf);
-        } else {
-            rc = MGMT_ERR_ENOTSUP;
-        }
+        handler_fn = handler->mh_read;
         break;
 
     case MGMT_OP_WRITE:
-        if (handler->mh_write != NULL) {
-            rc = handler->mh_write(cbuf);
-        } else {
-            rc = MGMT_ERR_ENOTSUP;
-        }
+        handler_fn = handler->mh_write;
         break;
 
     default:
-        rc = MGMT_ERR_EINVAL;
-        break;
+        return MGMT_ERR_EINVAL;
+    }
+
+    if (handler_fn) {
+        *handler_found = true;
+        mgmt_evt(MGMT_EVT_OP_CMD_RECV, req_hdr->nh_group, req_hdr->nh_id, NULL);
+
+        rc = handler_fn(cbuf);
+    } else {
+        rc = MGMT_ERR_ENOTSUP;
     }
+
     if (rc != 0) {
         return rc;
     }
@@ -219,7 +221,7 @@ smp_handle_single_payload(struct mgmt_ctxt *cbuf,
  */
 static int
 smp_handle_single_req(struct smp_streamer *streamer,
-                      const struct mgmt_hdr *req_hdr)
+                      const struct mgmt_hdr *req_hdr, bool *handler_found)
 {
     struct mgmt_ctxt cbuf;
     struct mgmt_hdr rsp_hdr;
@@ -240,7 +242,7 @@ smp_handle_single_req(struct smp_streamer *streamer,
     }
 
     /* Process the request and write the response payload. */
-    rc = smp_handle_single_payload(&cbuf, req_hdr);
+    rc = smp_handle_single_payload(&cbuf, req_hdr, handler_found);
     if (rc != 0) {
         return rc;
     }
@@ -316,14 +318,17 @@ int
 smp_process_request_packet(struct smp_streamer *streamer, void *req)
 {
     struct mgmt_hdr req_hdr;
+    struct mgmt_evt_op_cmd_done_arg cmd_done_arg;
     void *rsp;
-    bool valid_hdr;
+    bool valid_hdr, handler_found;
     int rc;
 
     rsp = NULL;
     valid_hdr = true;
 
     while (1) {
+        handler_found = false;
+
         rc = mgmt_streamer_init_reader(&streamer->mgmt_stmr, req);
         if (rc != 0) {
             valid_hdr = false;
@@ -351,7 +356,7 @@ smp_process_request_packet(struct smp_streamer *streamer, void *req)
         }
 
         /* Process the request payload and build the response. */
-        rc = smp_handle_single_req(streamer, &req_hdr);
+        rc = smp_handle_single_req(streamer, &req_hdr, &handler_found);
         if (rc != 0) {
             break;
         }
@@ -366,10 +371,21 @@ smp_process_request_packet(struct smp_streamer *streamer, void *req)
         /* Trim processed request to free up space for subsequent responses. */
         mgmt_streamer_trim_front(&streamer->mgmt_stmr, req,
                                  smp_align4(req_hdr.nh_len));
+
+        cmd_done_arg.err = MGMT_ERR_EOK;
+        mgmt_evt(MGMT_EVT_OP_CMD_DONE, req_hdr.nh_group, req_hdr.nh_id,
+                 &cmd_done_arg);
     }
 
     if (rc != 0 && valid_hdr) {
         smp_on_err(streamer, &req_hdr, req, rsp, rc);
+
+        if (handler_found) {
+            cmd_done_arg.err = rc;
+            mgmt_evt(MGMT_EVT_OP_CMD_DONE, req_hdr.nh_group, req_hdr.nh_id,
+                     &cmd_done_arg);
+        }
+
         return rc;
     }
 


 

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


With regards,
Apache Git Services