mynewt-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ma...@apache.org
Subject [2/3] incubator-mynewt-larva git commit: Start encoding imgmgr newt command payloads in json.
Date Mon, 18 Jan 2016 23:26:10 GMT
Start encoding imgmgr newt command payloads in json.


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

Branch: refs/heads/master
Commit: a519dacc55be535813be9cab96cc4942cdc492f5
Parents: 344782a
Author: Marko Kiiskila <marko@runtime.io>
Authored: Mon Jan 18 15:23:34 2016 -0800
Committer: Marko Kiiskila <marko@runtime.io>
Committed: Mon Jan 18 15:23:34 2016 -0800

----------------------------------------------------------------------
 libs/imgmgr/include/imgmgr/imgmgr.h |  3 +
 libs/imgmgr/src/imgmgr.c            | 98 ++++++++++++++++++++++++++------
 libs/imgmgr/src/imgmgr_fs.c         | 82 +++++++++++++++++++-------
 libs/imgmgr/src/imgmgr_priv.h       | 26 ++++++---
 4 files changed, 162 insertions(+), 47 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/a519dacc/libs/imgmgr/include/imgmgr/imgmgr.h
----------------------------------------------------------------------
diff --git a/libs/imgmgr/include/imgmgr/imgmgr.h b/libs/imgmgr/include/imgmgr/imgmgr.h
index 3e602ca..7fa580f 100644
--- a/libs/imgmgr/include/imgmgr/imgmgr.h
+++ b/libs/imgmgr/include/imgmgr/imgmgr.h
@@ -25,4 +25,7 @@
 
 int imgmgr_module_init(void);
 
+struct image_version;
+int imgr_ver_parse(char *src, struct image_version *ver);
+
 #endif /* _IMGMGR_H */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/a519dacc/libs/imgmgr/src/imgmgr.c
----------------------------------------------------------------------
diff --git a/libs/imgmgr/src/imgmgr.c b/libs/imgmgr/src/imgmgr.c
index 8368216..1df0a36 100644
--- a/libs/imgmgr/src/imgmgr.c
+++ b/libs/imgmgr/src/imgmgr.c
@@ -19,6 +19,7 @@
 #include <limits.h>
 #include <assert.h>
 #include <string.h>
+#include <stdio.h>
 #include <hal/flash_map.h>
 #include <newtmgr/newtmgr.h>
 
@@ -42,11 +43,16 @@ static const struct nmgr_handler imgr_nmgr_handlers[] = {
     [IMGMGR_NMGR_OP_UPLOAD] = {
         .nh_read = imgr_noop,
         .nh_write = imgr_upload
-    },
+    }
+#ifdef FS_PRESENT
+    ,
     [IMGMGR_NMGR_OP_BOOT] = {
         .nh_read = imgr_boot_read,
         .nh_write = imgr_boot_write
     }
+#else
+    goob
+#endif
 };
 
 static struct nmgr_group imgr_nmgr_group = {
@@ -95,23 +101,73 @@ imgr_read_ver(int area_id, struct image_version *ver)
     return rc;
 }
 
-void
-imgr_ver_hton(struct image_version *ver)
+int
+imgr_ver_parse(char *src, struct image_version *ver)
 {
-    ver->iv_revision = htons(ver->iv_revision);
-    ver->iv_build_num = htonl(ver->iv_build_num);
+    unsigned long ul;
+    char *tok;
+    char *nxt;
+    char *ep;
+
+    memset(ver, 0, sizeof(*ver));
+
+    nxt = src;
+    tok = strsep(&nxt, ".");
+    ul = strtoul(tok, &ep, 10);
+    if (tok[0] == '\0' || ep[0] != '\0' || ul > UINT8_MAX) {
+        return -1;
+    }
+    ver->iv_major = ul;
+    if (nxt == NULL) {
+        return 0;
+    }
+    tok = strsep(&nxt, ".");
+    ul = strtoul(tok, &ep, 10);
+    if (tok[0] == '\0' || ep[0] != '\0' || ul > UINT8_MAX) {
+        return -1;
+    }
+    ver->iv_minor = ul;
+    if (nxt == NULL) {
+        return 0;
+    }
+
+    tok = strsep(&nxt, ".");
+    ul = strtoul(tok, &ep, 10);
+    if (tok[0] == '\0' || ep[0] != '\0' || ul > UINT16_MAX) {
+        return -1;
+    }
+    ver->iv_revision = ul;
+    if (nxt == NULL) {
+        return 0;
+    }
+
+    tok = nxt;
+    ul = strtoul(tok, &ep, 10);
+    if (tok[0] == '\0' || ep[0] != '\0' || ul > UINT32_MAX) {
+        return -1;
+    }
+    ver->iv_build_num = ul;
+
+    return 0;
 }
 
-static int
-imgr_nmgr_append_ver(struct nmgr_hdr *rsp_hdr, struct os_mbuf *rsp,
-  struct image_version *to_write)
+int
+imgr_ver_jsonstr(char *dst, int dstlen, char *key, struct image_version *ver)
 {
-    struct image_version ver;
-
-    ver = *to_write;
-    imgr_ver_hton(&ver);
+    int off = 0;
 
-    return (nmgr_rsp_extend(rsp_hdr, rsp, &ver, sizeof(ver)));
+    if (key) {
+        off = snprintf(dst, dstlen, "\"%s\":", key);
+    }
+    if (ver->iv_build_num) {
+        off += snprintf(dst + off, dstlen - off, "\"%u.%u.%u.%lu\"",
+          ver->iv_major, ver->iv_minor, ver->iv_revision,
+          (unsigned long)ver->iv_build_num);
+    } else {
+        off += snprintf(dst + off, dstlen - off, "\"%u.%u.%u\"",
+          ver->iv_major, ver->iv_minor, ver->iv_revision);
+    }
+    return off;
 }
 
 static int
@@ -119,21 +175,27 @@ imgr_list(struct nmgr_hdr *nmr, struct os_mbuf *req, uint16_t srcoff,
   struct nmgr_hdr *rsp_hdr, struct os_mbuf *rsp)
 {
     struct image_version ver;
+    char str[128];
+    int off;
     int rc;
     int i;
+    int put_comma = 0;
 
+    off = snprintf(str, sizeof(str), "{\"images\":[");
     for (i = FLASH_AREA_IMAGE_0; i <= FLASH_AREA_IMAGE_1; i++) {
         rc = imgr_read_ver(i, &ver);
         if (rc < 0) {
             continue;
         }
-        rc = imgr_nmgr_append_ver(rsp_hdr, rsp, &ver);
-        if (rc) {
-            goto err;
+        if (put_comma) {
+            off += snprintf(str + off, sizeof(str) - off, ",");
         }
+        put_comma = 1;
+        off += imgr_ver_jsonstr(str + off, sizeof(str) - off, NULL, &ver);
     }
-    return 0;
-err:
+    off += snprintf(str + off, sizeof(str) - off, "]}");
+
+    rc = nmgr_rsp_extend(rsp_hdr, rsp, str, off);
     return rc;
 }
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/a519dacc/libs/imgmgr/src/imgmgr_fs.c
----------------------------------------------------------------------
diff --git a/libs/imgmgr/src/imgmgr_fs.c b/libs/imgmgr/src/imgmgr_fs.c
index ec3e6fa..06980fb 100644
--- a/libs/imgmgr/src/imgmgr_fs.c
+++ b/libs/imgmgr/src/imgmgr_fs.c
@@ -18,13 +18,16 @@
 
 #include <assert.h>
 #include <string.h>
+#include <stdio.h>
 
 #include <newtmgr/newtmgr.h>
 #include <bootutil/image.h>
 #include <fs/fs.h>
 #include <fs/fsutil.h>
+#include <json/json.h>
 #include <bsp/bsp.h>
 
+#include "imgmgr/imgmgr.h"
 #include "imgmgr_priv.h"
 
 /* XXX share with bootutil */
@@ -32,6 +35,7 @@
 #define BOOT_PATH_MAIN          "/boot/main"
 #define BOOT_PATH_TEST          "/boot/test"
 
+#ifdef FS_PRESENT
 static int
 imgr_read_file(const char *path, struct image_version *ver)
 {
@@ -67,29 +71,39 @@ int
 imgr_boot_read(struct nmgr_hdr *nmr, struct os_mbuf *req,
   uint16_t srcoff, struct nmgr_hdr *rsp_hdr, struct os_mbuf *rsp)
 {
-    struct imgmgr_bootrsp ibr;
     int rc;
-
-    rc = imgr_read_test(&ibr.ibr_test);
-    if (rc) {
-        memset(&ibr.ibr_test, 0xff, sizeof(ibr.ibr_test));
+    int prev_set = 0;
+    struct image_version ver;
+    char str[128];
+    int off;
+
+    off = snprintf(str, sizeof(str), "{");
+    rc = imgr_read_test(&ver);
+    if (!rc) {
+        off += imgr_ver_jsonstr(str + off, sizeof(str) - off, "test", &ver);
+        prev_set = 1;
     }
 
-    rc = imgr_read_main(&ibr.ibr_main);
-    if (rc) {
-        memset(&ibr.ibr_main, 0xff, sizeof(ibr.ibr_main));
+    rc = imgr_read_main(&ver);
+    if (!rc) {
+        if (prev_set) {
+            off += snprintf(str + off, sizeof(str) - off, ",");
+        }
+        off += imgr_ver_jsonstr(str + off, sizeof(str) - off, "main", &ver);
+        prev_set = 1;
     }
 
-    rc = imgr_read_ver(bsp_imgr_current_slot(), &ibr.ibr_active);
-    if (rc) {
-        memset(&ibr.ibr_active, 0xff, sizeof(ibr.ibr_active));
+    rc = imgr_read_ver(bsp_imgr_current_slot(), &ver);
+    if (!rc) {
+        if (prev_set) {
+            off += snprintf(str + off, sizeof(str) - off, ",");
+        }
+        off += imgr_ver_jsonstr(str + off, sizeof(str) - off, "active", &ver);
     }
 
-    imgr_ver_hton(&ibr.ibr_test);
-    imgr_ver_hton(&ibr.ibr_main);
-    imgr_ver_hton(&ibr.ibr_active);
+    off += snprintf(str + off, sizeof(str) - off, "}");
 
-    rc = nmgr_rsp_extend(rsp_hdr, rsp, &ibr, sizeof(ibr));
+    rc = nmgr_rsp_extend(rsp_hdr, rsp, str, off);
     if (rc) {
         goto err;
     }
@@ -103,19 +117,43 @@ int
 imgr_boot_write(struct nmgr_hdr *nmr, struct os_mbuf *req,
   uint16_t srcoff, struct nmgr_hdr *rsp_hdr, struct os_mbuf *rsp)
 {
-    struct image_version ver;
+    char incoming[64];
+    char test_ver_str[28];
+    const struct json_attr_t boot_write_attr[2] = {
+        [0] = {
+            .attribute = "test",
+            .type = JSON_VALUE_TYPE_STRING,
+            .addr.string = test_ver_str,
+            .len = sizeof(test_ver_str),
+        },
+        [1] = {
+            .attribute = NULL
+        }
+    };
     int rc;
-    int len;
+    const char *end;
+    struct image_version ver;
+
+    if (nmr->nh_len > sizeof(incoming)) {
+        return OS_EINVAL;
+    }
+    rc = os_mbuf_copydata(req, srcoff + sizeof(*nmr), nmr->nh_len, incoming);
+    if (rc) {
+        return OS_EINVAL;
+    }
 
-    len = min(nmr->nh_len, sizeof(ver));
-    rc = os_mbuf_copydata(req, srcoff + sizeof(*nmr), len, &ver);
-    if (rc || len < sizeof(ver)) {
+    rc = json_read_object(incoming, boot_write_attr, &end);
+    if (rc) {
+        return OS_EINVAL;
+    }
+
+    rc = imgr_ver_parse(boot_write_attr[0].addr.string, &ver);
+    if (rc) {
         return OS_EINVAL;
     }
 
-    ver.iv_revision = ntohs(ver.iv_revision);
-    ver.iv_build_num = ntohl(ver.iv_build_num);
     fs_mkdir(BOOT_PATH);
     rc = imgr_write_file(BOOT_PATH_TEST, &ver);
     return rc;
 }
+#endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/a519dacc/libs/imgmgr/src/imgmgr_priv.h
----------------------------------------------------------------------
diff --git a/libs/imgmgr/src/imgmgr_priv.h b/libs/imgmgr/src/imgmgr_priv.h
index 7308aec..49b1716 100644
--- a/libs/imgmgr/src/imgmgr_priv.h
+++ b/libs/imgmgr/src/imgmgr_priv.h
@@ -28,13 +28,24 @@ struct imgmgr_upload_cmd {
 };
 
 /*
- * Response to boot read.
+ * Response to list:
+ * {
+ *      "list":[ <version1>, <version2>]
+ * }
+ *
+ *
+ * Request to boot to version:
+ * {
+ *      "test":<version>
+ * }
+ *
+ * Response to boot read:
+ * {
+ *	"test":<version>,
+ *	"main":<version>,
+ *      "active":<version>
+ * }
  */
-struct imgmgr_bootrsp {
-    struct image_version ibr_test;	/* /boot/test */
-    struct image_version ibr_main;	/* /boot/main */
-    struct image_version ibr_active;	/* currently running image */
-};
 
 struct nmgr_hdr;
 struct os_mbuf;
@@ -45,6 +56,7 @@ int imgr_boot_write(struct nmgr_hdr *nmr, struct os_mbuf *req,
   uint16_t srcoff, struct nmgr_hdr *rsp_hdr, struct os_mbuf *rsp);
 
 int imgr_read_ver(int area_id, struct image_version *ver);
-void imgr_ver_hton(struct image_version *ver);
+int imgr_ver_jsonstr(char *dst, int dstlen, char *key,
+  struct image_version *ver);
 
 #endif /* __IMGMGR_PRIV_H */


Mime
View raw message