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/08/31 18:32:24 UTC

[1/3] incubator-mynewt-core git commit: MYNEWT-130; imgmgr - add CLI.

Repository: incubator-mynewt-core
Updated Branches:
  refs/heads/develop e803fa2b4 -> 39e75d0e0


MYNEWT-130; imgmgr - add CLI.


Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/commit/39e75d0e
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/39e75d0e
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/39e75d0e

Branch: refs/heads/develop
Commit: 39e75d0e0b0d1a7da958f95d2c92a63641bc3df6
Parents: 1ce9264
Author: Marko Kiiskila <ma...@runtime.io>
Authored: Wed Aug 31 11:29:13 2016 -0700
Committer: Marko Kiiskila <ma...@runtime.io>
Committed: Wed Aug 31 11:31:40 2016 -0700

----------------------------------------------------------------------
 libs/imgmgr/pkg.yml           |   4 ++
 libs/imgmgr/src/imgmgr.c      |   5 ++
 libs/imgmgr/src/imgmgr_cli.c  | 139 +++++++++++++++++++++++++++++++++++++
 libs/imgmgr/src/imgmgr_priv.h |   1 +
 4 files changed, 149 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/39e75d0e/libs/imgmgr/pkg.yml
----------------------------------------------------------------------
diff --git a/libs/imgmgr/pkg.yml b/libs/imgmgr/pkg.yml
index ed02f32..5d674d0 100644
--- a/libs/imgmgr/pkg.yml
+++ b/libs/imgmgr/pkg.yml
@@ -34,3 +34,7 @@ pkg.cflags.FS: -DFS_PRESENT
 pkg.deps.COREDUMP:
     - sys/coredump
 pkg.cflags.COREDUMP: -DCOREDUMP_PRESENT
+
+pkg.deps.SHELL:
+    - libs/shell
+pkg.cflags.SHELL: -DSHELL_PRESENT

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/39e75d0e/libs/imgmgr/src/imgmgr.c
----------------------------------------------------------------------
diff --git a/libs/imgmgr/src/imgmgr.c b/libs/imgmgr/src/imgmgr.c
index 7fb5218..9a9b884 100644
--- a/libs/imgmgr/src/imgmgr.c
+++ b/libs/imgmgr/src/imgmgr.c
@@ -463,6 +463,11 @@ imgmgr_module_init(void)
     rc = nmgr_group_register(&imgr_nmgr_group);
     assert(rc == 0);
 
+#ifdef SHELL_PRESENT
+    rc = imgr_cli_register();
+    assert(rc == 0);
+#endif
+
     boot_vect_write_main();
 
     return rc;

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/39e75d0e/libs/imgmgr/src/imgmgr_cli.c
----------------------------------------------------------------------
diff --git a/libs/imgmgr/src/imgmgr_cli.c b/libs/imgmgr/src/imgmgr_cli.c
new file mode 100644
index 0000000..2b70890
--- /dev/null
+++ b/libs/imgmgr/src/imgmgr_cli.c
@@ -0,0 +1,139 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed 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 distributed under the License is distributed 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.
+ */
+
+#ifdef SHELL_PRESENT
+
+#include <string.h>
+
+#include <hal/flash_map.h>
+#include <hal/hal_bsp.h>
+
+#include <shell/shell.h>
+#include <console/console.h>
+
+#include <bootutil/image.h>
+#include <bootutil/bootutil_misc.h>
+
+#include <util/hex.h>
+
+#include "imgmgr/imgmgr.h"
+#include "imgmgr_priv.h"
+
+static int imgr_cli_cmd(int argc, char **argv);
+
+static struct shell_cmd shell_imgr_cmd = {
+    .sc_cmd = "imgr",
+    .sc_cmd_func = imgr_cli_cmd
+};
+
+static void
+imgr_cli_show_slot(int slot)
+{
+    uint8_t hash[IMGMGR_HASH_LEN]; /* SHA256 hash */
+    char hash_str[IMGMGR_HASH_LEN * 2 + 1];
+    struct image_version ver;
+    char ver_str[IMGMGR_NMGR_MAX_VER];
+    uint32_t flags;
+
+    if (imgr_read_info(slot, &ver, hash, &flags)) {
+        return;
+    }
+
+    (void)imgr_ver_str(&ver, ver_str);
+
+    console_printf("%8s: %s %c\n",
+      ver_str, hex_format(hash, IMGMGR_HASH_LEN, hash_str, sizeof(hash_str)),
+      flags & IMAGE_F_NON_BOOTABLE ? ' ' : 'b');
+}
+
+static void
+imgr_cli_boot_get(void)
+{
+    int rc;
+    int slot;
+
+    /*
+     * Display test image (if set)
+     */
+    rc = boot_vect_read_test(&slot);
+    if (rc == 0) {
+        imgr_cli_show_slot(slot);
+    } else {
+        console_printf("No test img set\n");
+        return;
+    }
+}
+
+static void
+imgr_cli_boot_set(char *hash_str)
+{
+    uint8_t hash[IMGMGR_HASH_LEN];
+    struct image_version ver;
+    int rc;
+
+    if (hex_parse(hash_str, strlen(hash_str), hash, sizeof(hash)) !=
+      sizeof(hash)) {
+        console_printf("Invalid hash %s\n", hash_str);
+        return;
+    }
+    rc = imgr_find_by_hash(hash, &ver);
+    if (rc < 0) {
+        console_printf("Unknown img\n");
+        return;
+    }
+    rc = boot_vect_write_test(rc);
+    if (rc) {
+        console_printf("Can't make img active\n");
+        return;
+    }
+}
+
+static int
+imgr_cli_cmd(int argc, char **argv)
+{
+    int i;
+
+    if (argc < 2) {
+        console_printf("Too few args\n");
+        return 0;
+    }
+    if (!strcmp(argv[1], "list")) {
+        for (i = FLASH_AREA_IMAGE_0; i <= FLASH_AREA_IMAGE_1; i++) {
+            imgr_cli_show_slot(i);
+        }
+    } else if (!strcmp(argv[1], "boot")) {
+        if (argc > 2) {
+            imgr_cli_boot_set(argv[2]);
+        } else {
+            imgr_cli_boot_get();
+        }
+    } else if (!strcmp(argv[1], "ver")) {
+        imgr_cli_show_slot(bsp_imgr_current_slot());
+    } else {
+        console_printf("Unknown cmd\n");
+    }
+    return 0;
+}
+
+int
+imgr_cli_register(void)
+{
+    return shell_cmd_register(&shell_imgr_cmd);
+}
+#endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/39e75d0e/libs/imgmgr/src/imgmgr_priv.h
----------------------------------------------------------------------
diff --git a/libs/imgmgr/src/imgmgr_priv.h b/libs/imgmgr/src/imgmgr_priv.h
index ed218c3..29bde46 100644
--- a/libs/imgmgr/src/imgmgr_priv.h
+++ b/libs/imgmgr/src/imgmgr_priv.h
@@ -107,5 +107,6 @@ int imgr_splitapp_read(struct nmgr_jbuf *);
 int imgr_splitapp_write(struct nmgr_jbuf *);
 int imgr_find_by_ver(struct image_version *find, uint8_t *hash);
 int imgr_find_by_hash(uint8_t *find, struct image_version *ver);
+int imgr_cli_register(void);
 
 #endif /* __IMGMGR_PRIV_H */


[2/3] incubator-mynewt-core git commit: imgmgr; remove obsolete 'image list', 'image boot'.

Posted by ma...@apache.org.
imgmgr; remove obsolete 'image list', 'image boot'.


Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/commit/1ce92648
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/1ce92648
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/1ce92648

Branch: refs/heads/develop
Commit: 1ce9264869fc0e9c8c7bc92943b8e94f06c5fadb
Parents: b50dca3
Author: Marko Kiiskila <ma...@runtime.io>
Authored: Wed Aug 31 10:31:46 2016 -0700
Committer: Marko Kiiskila <ma...@runtime.io>
Committed: Wed Aug 31 11:31:40 2016 -0700

----------------------------------------------------------------------
 libs/imgmgr/src/imgmgr.c      |  44 +-------------
 libs/imgmgr/src/imgmgr_boot.c | 115 -------------------------------------
 libs/imgmgr/src/imgmgr_priv.h |   3 +-
 3 files changed, 4 insertions(+), 158 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/1ce92648/libs/imgmgr/src/imgmgr.c
----------------------------------------------------------------------
diff --git a/libs/imgmgr/src/imgmgr.c b/libs/imgmgr/src/imgmgr.c
index f97b078..7fb5218 100644
--- a/libs/imgmgr/src/imgmgr.c
+++ b/libs/imgmgr/src/imgmgr.c
@@ -33,14 +33,13 @@
 #include "imgmgr/imgmgr.h"
 #include "imgmgr_priv.h"
 
-static int imgr_list(struct nmgr_jbuf *);
 static int imgr_list2(struct nmgr_jbuf *);
 static int imgr_noop(struct nmgr_jbuf *);
 static int imgr_upload(struct nmgr_jbuf *);
 
 static const struct nmgr_handler imgr_nmgr_handlers[] = {
     [IMGMGR_NMGR_OP_LIST] = {
-        .nh_read = imgr_list,
+        .nh_read = imgr_noop,
         .nh_write = imgr_noop
     },
     [IMGMGR_NMGR_OP_UPLOAD] = {
@@ -48,8 +47,8 @@ static const struct nmgr_handler imgr_nmgr_handlers[] = {
         .nh_write = imgr_upload
     },
     [IMGMGR_NMGR_OP_BOOT] = {
-        .nh_read = imgr_boot_read,
-        .nh_write = imgr_boot_write
+        .nh_read = imgr_noop,
+        .nh_write = imgr_noop
     },
     [IMGMGR_NMGR_OP_FILE] = {
 #ifdef FS_PRESENT
@@ -231,43 +230,6 @@ imgr_find_by_hash(uint8_t *find, struct image_version *ver)
 }
 
 static int
-imgr_list(struct nmgr_jbuf *njb)
-{
-    struct image_version ver;
-    int i;
-    int rc;
-    struct json_encoder *enc;
-    struct json_value array;
-    struct json_value versions[IMGMGR_MAX_IMGS];
-    struct json_value *version_ptrs[IMGMGR_MAX_IMGS];
-    char vers_str[IMGMGR_MAX_IMGS][IMGMGR_NMGR_MAX_VER];
-    int ver_len;
-    int cnt = 0;
-
-    for (i = FLASH_AREA_IMAGE_0; i <= FLASH_AREA_IMAGE_1; i++) {
-        rc = imgr_read_info(i, &ver, NULL, NULL);
-        if (rc != 0) {
-            continue;
-        }
-        ver_len = imgr_ver_str(&ver, vers_str[cnt]);
-        JSON_VALUE_STRINGN(&versions[cnt], vers_str[cnt], ver_len);
-        version_ptrs[cnt] = &versions[cnt];
-        cnt++;
-    }
-    array.jv_type = JSON_VALUE_TYPE_ARRAY;
-    array.jv_len = cnt;
-    array.jv_val.composite.values = version_ptrs;
-
-    enc = &njb->njb_enc;
-
-    json_encode_object_start(enc);
-    json_encode_object_entry(enc, "images", &array);
-    json_encode_object_finish(enc);
-
-    return 0;
-}
-
-static int
 imgr_list2(struct nmgr_jbuf *njb)
 {
     struct json_encoder *enc;

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/1ce92648/libs/imgmgr/src/imgmgr_boot.c
----------------------------------------------------------------------
diff --git a/libs/imgmgr/src/imgmgr_boot.c b/libs/imgmgr/src/imgmgr_boot.c
index ab18902..0473f25 100644
--- a/libs/imgmgr/src/imgmgr_boot.c
+++ b/libs/imgmgr/src/imgmgr_boot.c
@@ -35,19 +35,6 @@
 #include "imgmgr_priv.h"
 
 static void
-imgr_ver_jsonstr(struct json_encoder *enc, char *key,
-  struct image_version *ver)
-{
-    struct json_value jv;
-    char ver_str[IMGMGR_NMGR_MAX_VER];
-    int ver_len;
-
-    ver_len = imgr_ver_str(ver, ver_str);
-    JSON_VALUE_STRINGN(&jv, ver_str, ver_len);
-    json_encode_object_entry(enc, key, &jv);
-}
-
-static void
 imgr_hash_jsonstr(struct json_encoder *enc, char *key, uint8_t *hash)
 {
     struct json_value jv;
@@ -59,108 +46,6 @@ imgr_hash_jsonstr(struct json_encoder *enc, char *key, uint8_t *hash)
 }
 
 int
-imgr_boot_read(struct nmgr_jbuf *njb)
-{
-    int rc;
-    struct json_encoder *enc;
-    int slot;
-    struct image_version ver;
-    struct json_value jv;
-    uint8_t hash[IMGMGR_HASH_LEN];
-
-    enc = &njb->njb_enc;
-
-    json_encode_object_start(enc);
-
-    rc = boot_vect_read_test(&slot);
-    if (!rc) {
-        rc = imgr_read_info(slot, &ver, hash, NULL);
-        if (!rc) {
-            imgr_ver_jsonstr(enc, "test", &ver);
-        }
-    }
-
-    rc = boot_vect_read_main(&slot);
-    if (!rc) {
-        rc = imgr_read_info(slot, &ver, hash, NULL);
-        if (!rc) {
-            imgr_ver_jsonstr(enc, "main", &ver);
-        }
-    }
-
-    rc = imgr_read_info(bsp_imgr_current_slot(), &ver, hash, NULL);
-    if (!rc) {
-        imgr_ver_jsonstr(enc, "active", &ver);
-    }
-
-    JSON_VALUE_INT(&jv, NMGR_ERR_EOK);
-    json_encode_object_entry(enc, "rc", &jv);
-
-    json_encode_object_finish(enc);
-
-    return 0;
-}
-
-int
-imgr_boot_write(struct nmgr_jbuf *njb)
-{
-    char test_ver_str[28];
-    uint8_t hash[IMGMGR_HASH_LEN];
-    const struct json_attr_t boot_write_attr[2] = {
-        [0] = {
-            .attribute = "test",
-            .type = t_string,
-            .addr.string = test_ver_str,
-            .len = sizeof(test_ver_str),
-        },
-        [1] = {
-            .attribute = NULL
-        }
-    };
-    struct json_encoder *enc;
-    struct json_value jv;
-    int rc;
-    struct image_version ver;
-
-    rc = json_read_object(&njb->njb_buf, boot_write_attr);
-    if (rc) {
-        rc = NMGR_ERR_EINVAL;
-        goto err;
-    }
-
-    rc = imgr_ver_parse(boot_write_attr[0].addr.string, &ver);
-    if (rc) {
-        rc = NMGR_ERR_EINVAL;
-        goto err;
-    }
-
-    rc = imgr_find_by_ver(&ver, hash);
-    if (rc < 0) {
-        rc = NMGR_ERR_EINVAL;
-        goto err;
-    }
-    rc = boot_vect_write_test(rc);
-    if (rc) {
-        rc = NMGR_ERR_EINVAL;
-        goto err;
-    }
-    enc = &njb->njb_enc;
-
-    json_encode_object_start(enc);
-
-    JSON_VALUE_INT(&jv, NMGR_ERR_EOK);
-    json_encode_object_entry(enc, "rc", &jv);
-
-    json_encode_object_finish(enc);
-
-    return 0;
-
-err:
-    nmgr_jbuf_setoerr(njb, rc);
-    return 0;
-}
-
-int
 imgr_boot2_read(struct nmgr_jbuf *njb)
 {
     int rc;

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/1ce92648/libs/imgmgr/src/imgmgr_priv.h
----------------------------------------------------------------------
diff --git a/libs/imgmgr/src/imgmgr_priv.h b/libs/imgmgr/src/imgmgr_priv.h
index 987cf71..ed218c3 100644
--- a/libs/imgmgr/src/imgmgr_priv.h
+++ b/libs/imgmgr/src/imgmgr_priv.h
@@ -95,8 +95,7 @@ struct imgr_state {
 
 extern struct imgr_state imgr_state;
 
-int imgr_boot_read(struct nmgr_jbuf *);
-int imgr_boot_write(struct nmgr_jbuf *);
+struct nmgr_jbuf;
 int imgr_boot2_read(struct nmgr_jbuf *);
 int imgr_boot2_write(struct nmgr_jbuf *);
 int imgr_file_upload(struct nmgr_jbuf *);


[3/3] incubator-mynewt-core git commit: libs/util; add conversion routines from hex string to bytes and vice versa.

Posted by ma...@apache.org.
libs/util; add conversion routines from hex string to bytes and
vice versa.


Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/commit/b50dca3e
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/b50dca3e
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/b50dca3e

Branch: refs/heads/develop
Commit: b50dca3e98b7b56836b33108b5a06549d6fb332f
Parents: e803fa2
Author: Marko Kiiskila <ma...@runtime.io>
Authored: Wed Aug 31 10:28:52 2016 -0700
Committer: Marko Kiiskila <ma...@runtime.io>
Committed: Wed Aug 31 11:31:40 2016 -0700

----------------------------------------------------------------------
 libs/util/include/util/hex.h        |  25 +++++++
 libs/util/src/hex.c                 | 101 +++++++++++++++++++++++++
 libs/util/src/test/hex_test.c       | 125 +++++++++++++++++++++++++++++++
 libs/util/src/test/util_test.c      |   9 ++-
 libs/util/src/test/util_test_priv.h |   1 +
 5 files changed, 260 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/b50dca3e/libs/util/include/util/hex.h
----------------------------------------------------------------------
diff --git a/libs/util/include/util/hex.h b/libs/util/include/util/hex.h
new file mode 100644
index 0000000..1e9d8c8
--- /dev/null
+++ b/libs/util/include/util/hex.h
@@ -0,0 +1,25 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed 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 distributed under the License is distributed 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.
+ */
+#ifndef _UTIL_HEX_H_
+#define _UTIL_HEX_H_
+
+char *hex_format(void *src_v, int src_len, char *dst, int dst_len);
+int hex_parse(char *src, int src_len, void *dst_v, int dst_len);
+
+#endif /* _UTIL_HEX_H_ */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/b50dca3e/libs/util/src/hex.c
----------------------------------------------------------------------
diff --git a/libs/util/src/hex.c b/libs/util/src/hex.c
new file mode 100644
index 0000000..153f974
--- /dev/null
+++ b/libs/util/src/hex.c
@@ -0,0 +1,101 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed 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 distributed under the License is distributed 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 <inttypes.h>
+#include <ctype.h>
+#include <stddef.h>
+
+#include "util/hex.h"
+
+static const char hex_bytes[] = "0123456789abcdef";
+
+/*
+ * Turn byte array into a printable array. I.e. "\x01" -> "01"
+ *
+ * @param src_v		Data to convert
+ * @param src_len	Number of bytes of input
+ * @param dst		String where to place the results
+ * @param dst_len	Size of the target string
+ *
+ * @return		Pointer to 'dst' if successful; NULL on failure
+ */
+char *
+hex_format(void *src_v, int src_len, char *dst, int dst_len)
+{
+    int i;
+    uint8_t *src = (uint8_t *)src_v;
+    char *tgt = dst;
+
+    if (dst_len <= src_len * 2) {
+        return NULL;
+    }
+    for (i = 0; i < src_len; i++) {
+        tgt[0] = hex_bytes[(src[i] >> 4) & 0xf];
+        tgt[1] = hex_bytes[src[i] & 0xf];
+        tgt += 2;
+        dst_len -= 2;
+    }
+    *tgt = '\0';
+    return dst;
+}
+
+/*
+ * Turn string of hex decimals into a byte array. I.e. "01" -> "\x01
+ *
+ * @param src		String to convert
+ * @param src_len	Number of bytes in input string
+ * @param dst_v		Memory location to place the result
+ * @param dst_len	Amount of space for the result
+ *
+ * @return		-1 on failure; number of bytes of input
+ */
+int
+hex_parse(char *src, int src_len, void *dst_v, int dst_len)
+{
+    int i;
+    uint8_t *dst = (uint8_t *)dst_v;
+    char c;
+
+    if (src_len & 0x1) {
+        return -1;
+    }
+    if (dst_len * 2 < src_len) {
+        return -1;
+    }
+    for (i = 0; i < src_len; i++, src++) {
+        c = *src;
+        if (isdigit(c)) {
+            c -= '0';
+        } else if (c >= 'a' && c <= 'f') {
+            c -= ('a' - 10);
+        } else if (c >= 'A' && c <= 'F') {
+            c -= ('A' - 10);
+        } else {
+            return -1;
+        }
+        if (i & 1) {
+            *dst |= c;
+            dst++;
+            dst_len--;
+        } else {
+            *dst = c << 4;
+        }
+    }
+    return src_len >> 1;
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/b50dca3e/libs/util/src/test/hex_test.c
----------------------------------------------------------------------
diff --git a/libs/util/src/test/hex_test.c b/libs/util/src/test/hex_test.c
new file mode 100644
index 0000000..8183425
--- /dev/null
+++ b/libs/util/src/test/hex_test.c
@@ -0,0 +1,125 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed 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 distributed under the License is distributed 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 <stdio.h>
+#include <string.h>
+
+#include "testutil/testutil.h"
+#include "util/hex.h"
+
+TEST_CASE(hex2str)
+{
+    int i;
+    char *ret;
+    char cmp_data[8];
+
+    struct {
+        char *in;
+        int inlen;
+        char *out;
+        int outlen;
+    } test_data[] = {
+        [0] = {
+            .in = "\x01",
+            .inlen = 1,
+            .out = "01",
+            .outlen = 2,
+        },
+        [1] = {
+            .in = "\xaf\xf2",
+            .inlen = 2,
+            .out = "aff2",
+            .outlen = 4,
+        }
+    };
+
+    for (i = 0; i < sizeof(test_data) / sizeof(test_data[0]); i++) {
+        ret = hex_format(test_data[i].in, test_data[i].inlen,
+          cmp_data, sizeof(cmp_data));
+        TEST_ASSERT(ret == cmp_data);
+        TEST_ASSERT(strlen(cmp_data) == test_data[i].outlen);
+        TEST_ASSERT(!strcmp(test_data[i].out, cmp_data));
+    }
+
+    /*
+     * Test not enough space. Must have space for '\0' at the end.
+     */
+    ret = hex_format("\x01\x02", 2, cmp_data, 1);
+    TEST_ASSERT(ret == NULL);
+
+    ret = hex_format("\x01\x02", 2, cmp_data, 2);
+    TEST_ASSERT(ret == NULL);
+}
+
+TEST_CASE(str2hex)
+{
+    int i;
+    char cmp_data[8];
+    int rc;
+
+    struct {
+        char *in;
+        int inlen;
+        char *out;
+        int outlen;
+    } test_data[] = {
+        [0] = {
+            .in = "01",
+            .inlen = 2,
+            .out = "\x01",
+            .outlen = 1,
+        },
+        [1] = {
+            .in = "AfF2",
+            .inlen = 4,
+            .out = "\xaf\xf2",
+            .outlen = 2,
+        }
+    };
+
+    for (i = 0; i < sizeof(test_data) / sizeof(test_data[0]); i++) {
+        rc = hex_parse(test_data[i].in, test_data[i].inlen,
+          cmp_data, sizeof(cmp_data));
+        TEST_ASSERT(rc == test_data[i].outlen);
+        TEST_ASSERT(!memcmp(test_data[i].out, cmp_data, rc));
+    }
+
+    /*
+     * Test invalid input
+     */
+    rc = hex_parse("HJ", 2, cmp_data, sizeof(cmp_data));
+    TEST_ASSERT(rc < 0);
+
+    rc = hex_parse("a", 1, cmp_data, sizeof(cmp_data));
+    TEST_ASSERT(rc < 0);
+
+    rc = hex_parse("0102", 4, cmp_data, 1);
+    TEST_ASSERT(rc < 0);
+
+    /*
+     * This should be valid.
+     */
+    rc = hex_parse("0102", 4, cmp_data, 2);
+    TEST_ASSERT(rc == 2);
+}
+
+TEST_SUITE(hex_fmt_test_suite)
+{
+    hex2str();
+    str2hex();
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/b50dca3e/libs/util/src/test/util_test.c
----------------------------------------------------------------------
diff --git a/libs/util/src/test/util_test.c b/libs/util/src/test/util_test.c
index c236ab5..274878b 100644
--- a/libs/util/src/test/util_test.c
+++ b/libs/util/src/test/util_test.c
@@ -29,6 +29,13 @@ util_test_all(void)
     return tu_case_failed;
 }
 
+int
+hex_fmt_test_all(void)
+{
+    hex_fmt_test_suite();
+    return tu_case_failed;
+}
+
 #ifdef MYNEWT_SELFTEST
 
 int
@@ -38,7 +45,7 @@ main(int argc, char **argv)
     tu_init();
 
     util_test_all();
-
+    hex_fmt_test_all();
     return tu_any_failed;
 }
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/b50dca3e/libs/util/src/test/util_test_priv.h
----------------------------------------------------------------------
diff --git a/libs/util/src/test/util_test_priv.h b/libs/util/src/test/util_test_priv.h
index cc5533d..c82df29 100644
--- a/libs/util/src/test/util_test_priv.h
+++ b/libs/util/src/test/util_test_priv.h
@@ -21,5 +21,6 @@
 #define __UTIL_TEST_PRIV_
 
 int cbmem_test_suite(void);
+int hex_fmt_test_suite(void);
 
 #endif