You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by ma...@apache.org on 2016/01/25 19:59:46 UTC

incubator-mynewt-larva git commit: Add possibility of doing file uploads.

Repository: incubator-mynewt-larva
Updated Branches:
  refs/heads/master cccb1f37f -> 7662fab94


Add possibility of doing file uploads.


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

Branch: refs/heads/master
Commit: 7662fab949a7a8ad6e2615d0957cd8001801a203
Parents: cccb1f3
Author: Marko Kiiskila <ma...@runtime.io>
Authored: Mon Jan 25 10:59:07 2016 -0800
Committer: Marko Kiiskila <ma...@runtime.io>
Committed: Mon Jan 25 10:59:07 2016 -0800

----------------------------------------------------------------------
 libs/imgmgr/include/imgmgr/imgmgr.h |  1 +
 libs/imgmgr/src/imgmgr.c            | 38 ++++++-------
 libs/imgmgr/src/imgmgr_fs.c         | 98 ++++++++++++++++++++++++++++++++
 libs/imgmgr/src/imgmgr_priv.h       | 26 +++++++++
 4 files changed, 143 insertions(+), 20 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/7662fab9/libs/imgmgr/include/imgmgr/imgmgr.h
----------------------------------------------------------------------
diff --git a/libs/imgmgr/include/imgmgr/imgmgr.h b/libs/imgmgr/include/imgmgr/imgmgr.h
index 78f4c5e..d130d7d 100644
--- a/libs/imgmgr/include/imgmgr/imgmgr.h
+++ b/libs/imgmgr/include/imgmgr/imgmgr.h
@@ -20,6 +20,7 @@
 #define IMGMGR_NMGR_OP_LIST	0
 #define IMGMGR_NMGR_OP_UPLOAD	1
 #define IMGMGR_NMGR_OP_BOOT	2
+#define IMGMGR_NMGR_OP_FILE	3
 
 #define IMGMGR_NMGR_MAX_MSG	120
 #define IMGMGR_NMGR_MAX_VER	25	/* 255.255.65535.4294967295\0 */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/7662fab9/libs/imgmgr/src/imgmgr.c
----------------------------------------------------------------------
diff --git a/libs/imgmgr/src/imgmgr.c b/libs/imgmgr/src/imgmgr.c
index 9818071..5bc690f 100644
--- a/libs/imgmgr/src/imgmgr.c
+++ b/libs/imgmgr/src/imgmgr.c
@@ -47,6 +47,10 @@ static const struct nmgr_handler imgr_nmgr_handlers[] = {
     [IMGMGR_NMGR_OP_BOOT] = {
         .nh_read = imgr_boot_read,
         .nh_write = imgr_boot_write
+    },
+    [IMGMGR_NMGR_OP_FILE] = {
+        .nh_read = imgr_noop,
+        .nh_write = imgr_file_upload
     }
 #endif
 };
@@ -56,18 +60,12 @@ static struct nmgr_group imgr_nmgr_group = {
 #ifndef FS_PRESENT
     .ng_handlers_count = 2,
 #else
-    .ng_handlers_count = 3,
+    .ng_handlers_count = 4,
 #endif
     .ng_group_id = NMGR_GROUP_ID_IMAGE,
 };
 
-static struct {
-    struct {
-        uint32_t off;
-        uint32_t size;
-        const struct flash_area *fa;
-    } upload;
-} img_state;
+struct imgr_state imgr_state;
 
 /*
  * Read version from image header from flash area 'area_id'.
@@ -196,8 +194,8 @@ imgr_upload(struct nmgr_jbuf *njb)
         /*
          * New upload.
          */
-        img_state.upload.off = 0;
-        img_state.upload.size = size;
+        imgr_state.upload.off = 0;
+        imgr_state.upload.size = size;
         active = bsp_imgr_current_slot();
         best = -1;
 
@@ -234,13 +232,13 @@ imgr_upload(struct nmgr_jbuf *njb)
             best = i;
         }
         if (best >= 0) {
-            rc = flash_area_open(best, &img_state.upload.fa);
+            rc = flash_area_open(best, &imgr_state.upload.fa);
             assert(rc == 0);
             /*
              * XXXX only erase if needed.
              */
-            rc = flash_area_erase(img_state.upload.fa, 0,
-              img_state.upload.fa->fa_size);
+            rc = flash_area_erase(imgr_state.upload.fa, 0,
+              imgr_state.upload.fa->fa_size);
         } else {
             /*
              * No slot where to upload!
@@ -248,7 +246,7 @@ imgr_upload(struct nmgr_jbuf *njb)
             assert(0);
             goto out;
         }
-    } else if (off != img_state.upload.off) {
+    } else if (off != imgr_state.upload.off) {
         /*
          * Invalid offset. Drop the data, and respond with the offset we're
          * expecting data for.
@@ -257,14 +255,14 @@ imgr_upload(struct nmgr_jbuf *njb)
         goto out;
     }
 
-    if (len && img_state.upload.fa) {
-        rc = flash_area_write(img_state.upload.fa, img_state.upload.off,
+    if (len && imgr_state.upload.fa) {
+        rc = flash_area_write(imgr_state.upload.fa, imgr_state.upload.off,
           img_data, len);
         assert(rc == 0);
-        img_state.upload.off += len;
-        if (img_state.upload.size == img_state.upload.off) {
+        imgr_state.upload.off += len;
+        if (imgr_state.upload.size == imgr_state.upload.off) {
             /* Done */
-            img_state.upload.fa = NULL;
+            imgr_state.upload.fa = NULL;
         }
     }
 out:
@@ -272,7 +270,7 @@ out:
 
     json_encode_object_start(enc);
 
-    JSON_VALUE_UINT(&jv, img_state.upload.off);
+    JSON_VALUE_UINT(&jv, imgr_state.upload.off);
     json_encode_object_entry(enc, "off", &jv);
     json_encode_object_finish(enc);
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/7662fab9/libs/imgmgr/src/imgmgr_fs.c
----------------------------------------------------------------------
diff --git a/libs/imgmgr/src/imgmgr_fs.c b/libs/imgmgr/src/imgmgr_fs.c
index 0813c68..cc5582f 100644
--- a/libs/imgmgr/src/imgmgr_fs.c
+++ b/libs/imgmgr/src/imgmgr_fs.c
@@ -16,6 +16,7 @@
 #include <os/os.h>
 #include <os/endian.h>
 
+#include <limits.h>
 #include <assert.h>
 #include <string.h>
 #include <stdio.h>
@@ -25,6 +26,7 @@
 #include <fs/fs.h>
 #include <fs/fsutil.h>
 #include <json/json.h>
+#include <util/base64.h>
 #include <bsp/bsp.h>
 
 #include "imgmgr/imgmgr.h"
@@ -143,4 +145,100 @@ imgr_boot_write(struct nmgr_jbuf *njb)
     rc = imgr_write_file(BOOT_PATH_TEST, &ver);
     return rc;
 }
+
+int
+imgr_file_upload(struct nmgr_jbuf *njb)
+{
+    char img_data[BASE64_ENCODE_SIZE(IMGMGR_NMGR_MAX_MSG)];
+    char file_name[65];
+    unsigned int off = UINT_MAX;
+    unsigned int size = UINT_MAX;
+    const struct json_attr_t off_attr[5] = {
+        [0] = {
+            .attribute = "off",
+            .type = t_uinteger,
+            .addr.uinteger = &off,
+            .nodefault = true
+        },
+        [1] = {
+            .attribute = "data",
+            .type = t_string,
+            .addr.string = img_data,
+            .len = sizeof(img_data)
+        },
+        [2] = {
+            .attribute = "len",
+            .type = t_uinteger,
+            .addr.uinteger = &size,
+            .nodefault = true
+        },
+        [3] = {
+            .attribute = "name",
+            .type = t_string,
+            .addr.string = file_name,
+            .len = sizeof(file_name)
+        }
+    };
+    struct json_encoder *enc;
+    struct json_value jv;
+    int rc;
+    int len;
+
+    rc = json_read_object(&njb->njb_buf, off_attr);
+    if (rc || off == UINT_MAX) {
+        return OS_EINVAL;
+    }
+    len = strlen(img_data);
+    if (len) {
+        len = base64_decode(img_data, img_data);
+        if (len < 0) {
+            return OS_EINVAL;
+        }
+    }
+
+    if (off == 0) {
+        /*
+         * New upload.
+         */
+        imgr_state.upload.off = 0;
+        imgr_state.upload.size = size;
+
+        if (!strlen(file_name)) {
+            return OS_EINVAL;
+        }
+        rc = fs_open(file_name, FS_ACCESS_WRITE | FS_ACCESS_TRUNCATE,
+          &imgr_state.upload.file);
+        if (rc) {
+            return OS_EINVAL;
+        }
+    } else if (off != imgr_state.upload.off) {
+        /*
+         * Invalid offset. Drop the data, and respond with the offset we're
+         * expecting data for.
+         */
+        rc = 0;
+        goto out;
+    }
+
+    if (len && imgr_state.upload.file) {
+        rc = fs_write(imgr_state.upload.file, img_data, len);
+        imgr_state.upload.off += len;
+        if (imgr_state.upload.size == imgr_state.upload.off) {
+            /* Done */
+            fs_close(imgr_state.upload.file);
+            imgr_state.upload.file = NULL;
+        }
+    }
+out:
+    enc = &njb->njb_enc;
+
+    json_encode_object_start(enc);
+
+    JSON_VALUE_UINT(&jv, imgr_state.upload.off);
+    json_encode_object_entry(enc, "off", &jv);
+    json_encode_object_finish(enc);
+
+    return 0;
+}
+
 #endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/7662fab9/libs/imgmgr/src/imgmgr_priv.h
----------------------------------------------------------------------
diff --git a/libs/imgmgr/src/imgmgr_priv.h b/libs/imgmgr/src/imgmgr_priv.h
index ff13a0c..43057ee 100644
--- a/libs/imgmgr/src/imgmgr_priv.h
+++ b/libs/imgmgr/src/imgmgr_priv.h
@@ -39,6 +39,7 @@ struct imgmgr_upload_cmd {
  *      "test":<version>
  * }
  *
+ *
  * Response to boot read:
  * {
  *	"test":<version>,
@@ -46,6 +47,7 @@ struct imgmgr_upload_cmd {
  *      "active":<version>
  * }
  *
+ *
  * Request to image upload:
  * {
  *      "off":<offset>,
@@ -53,18 +55,42 @@ struct imgmgr_upload_cmd {
  *      "data":<base64encoded binary>
  * }
  *
+ *
  * Response to upload:
  * {
  *      "off":<offset>
  * }
  *
+ *
+ * Request to image upload:
+ * {
+ *      "off":<offset>
+ *	"name":<filename>		inspected when off = 0
+ *      "len":<file_size>		inspected when off = 0
+ *      "data":<base64encoded binary>
+ * }
  */
 
 struct nmgr_hdr;
 struct os_mbuf;
+struct fs_file;
+
+struct imgr_state {
+    struct {
+        uint32_t off;
+        uint32_t size;
+        const struct flash_area *fa;
+#ifdef FS_PRESENT
+        struct fs_file *file;
+#endif
+    } upload;
+};
+
+extern struct imgr_state imgr_state;
 
 int imgr_boot_read(struct nmgr_jbuf *);
 int imgr_boot_write(struct nmgr_jbuf *);
+int imgr_file_upload(struct nmgr_jbuf *);
 
 int imgr_read_ver(int area_id, struct image_version *ver);