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/11/09 07:30:33 UTC
[1/3] incubator-mynewt-core git commit: boot_serial;
first take at converting from JSON to cbor encoded data.
Repository: incubator-mynewt-core
Updated Branches:
refs/heads/develop de6978257 -> 0c51338ed
boot_serial; first take at converting from JSON to cbor encoded
data.
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/39a34a25
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/39a34a25
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/39a34a25
Branch: refs/heads/develop
Commit: 39a34a25d1e657c2405196865ed50a53a5e62a8a
Parents: de69782
Author: Marko Kiiskila <ma...@runtime.io>
Authored: Tue Nov 8 23:27:16 2016 -0800
Committer: Marko Kiiskila <ma...@runtime.io>
Committed: Tue Nov 8 23:27:16 2016 -0800
----------------------------------------------------------------------
.../include/boot_serial/boot_serial.h | 9 +-
boot/boot_serial/pkg.yml | 2 +
boot/boot_serial/src/boot_serial.c | 228 ++++++++++---------
boot/boot_serial/src/boot_serial_priv.h | 12 +-
4 files changed, 133 insertions(+), 118 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/39a34a25/boot/boot_serial/include/boot_serial/boot_serial.h
----------------------------------------------------------------------
diff --git a/boot/boot_serial/include/boot_serial/boot_serial.h b/boot/boot_serial/include/boot_serial/boot_serial.h
index b9f6f6f..b93c28f 100644
--- a/boot/boot_serial/include/boot_serial/boot_serial.h
+++ b/boot/boot_serial/include/boot_serial/boot_serial.h
@@ -25,14 +25,11 @@ extern "C" {
#endif
/*
- * Create a task for uploading image0 over serial.
+ * Start processing newtmgr commands for uploading image0 over serial.
*
- * Task opens console serial port and waits for download command.
- * Return code 0 means new image was uploaded, non-zero means that
- * there was an error.
+ * Open console serial port and wait for download command.
*/
-int boot_serial_task_init(struct os_task *task, uint8_t prio,
- os_stack_t *stack, uint16_t stack_size, int max_input);
+void boot_serial_start(int max_input);
#ifdef __cplusplus
}
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/39a34a25/boot/boot_serial/pkg.yml
----------------------------------------------------------------------
diff --git a/boot/boot_serial/pkg.yml b/boot/boot_serial/pkg.yml
index 6cf45ec..5913877 100644
--- a/boot/boot_serial/pkg.yml
+++ b/boot/boot_serial/pkg.yml
@@ -29,6 +29,8 @@ pkg.deps:
- hw/hal
- kernel/os
- boot/bootutil
+ - encoding/tinycbor
+ - encoding/cborattr
- encoding/base64
- sys/flash_map
- util/crc
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/39a34a25/boot/boot_serial/src/boot_serial.c
----------------------------------------------------------------------
diff --git a/boot/boot_serial/src/boot_serial.c b/boot/boot_serial/src/boot_serial.c
index d9a8ece..ac935c1 100644
--- a/boot/boot_serial/src/boot_serial.c
+++ b/boot/boot_serial/src/boot_serial.c
@@ -33,9 +33,13 @@
#include <os/endian.h>
#include <os/os.h>
#include <os/os_malloc.h>
+#include <os/os_cputime.h>
#include <console/console.h>
+#include <tinycbor/cbor.h>
+#include <tinycbor/cbor_buf_reader.h>
+#include <cborattr/cborattr.h>
#include <base64/base64.h>
#include <crc/crc16.h>
@@ -50,7 +54,26 @@ static uint32_t curr_off;
static uint32_t img_size;
static struct nmgr_hdr *bs_hdr;
-static void boot_serial_output(char *data, int len);
+static char bs_obuf[BOOT_SERIAL_OUT_MAX];
+
+static int bs_cbor_writer(struct cbor_encoder_writer *, const char *data,
+ int len);
+static void boot_serial_output(void);
+
+static struct cbor_encoder_writer bs_writer = {
+ .write = bs_cbor_writer
+};
+static CborEncoder bs_root;
+static CborEncoder bs_rsp;
+
+int
+bs_cbor_writer(struct cbor_encoder_writer *cew, const char *data, int len)
+{
+ memcpy(&bs_obuf[cew->bytes_written], data, len);
+ cew->bytes_written += len;
+
+ return 0;
+}
/*
* Looks for 'name' from NULL-terminated json data in buf.
@@ -86,48 +109,46 @@ bs_find_val(char *buf, char *name)
static void
bs_list(char *buf, int len)
{
- char *ptr;
+ CborEncoder images;
+ CborEncoder image;
struct image_header hdr;
uint8_t tmpbuf[64];
- const struct flash_area *fap = NULL;
- int good_img, need_comma = 0;
- int area_id;
- int rc;
- int i;
+ int i, area_id;
+ const struct flash_area *fap;
- ptr = os_malloc(BOOT_SERIAL_OUT_MAX);
- if (!ptr) {
- return;
- }
- len = snprintf(ptr, BOOT_SERIAL_OUT_MAX, "{\"images\":[");
+ cbor_encoder_create_map(&bs_root, &bs_rsp, CborIndefiniteLength);
+ cbor_encode_text_stringz(&bs_rsp, "images");
+ cbor_encoder_create_array(&bs_rsp, &images, CborIndefiniteLength);
for (i = 0; i < 2; i++) {
area_id = flash_area_id_from_image_slot(i);
- rc = flash_area_open(area_id, &fap);
- if (rc) {
+ if (flash_area_open(area_id, &fap)) {
continue;
}
flash_area_read(fap, 0, &hdr, sizeof(hdr));
- if (hdr.ih_magic == IMAGE_MAGIC &&
+ if (hdr.ih_magic != IMAGE_MAGIC ||
bootutil_img_validate(&hdr, fap, tmpbuf, sizeof(tmpbuf),
- NULL, 0, NULL) == 0) {
- good_img = 1;
- } else {
- good_img = 0;
- }
- if (good_img) {
- len += snprintf(ptr + len, BOOT_SERIAL_OUT_MAX - len,
- "%c\"%u.%u.%u.%u\"", need_comma ? ',' : ' ',
- hdr.ih_ver.iv_major, hdr.ih_ver.iv_minor, hdr.ih_ver.iv_revision,
- (unsigned int)hdr.ih_ver.iv_build_num);
+ NULL, 0, NULL)) {
+ flash_area_close(fap);
+ continue;
}
flash_area_close(fap);
- need_comma = 1;
+
+ cbor_encoder_create_map(&images, &image, CborIndefiniteLength);
+ cbor_encode_text_stringz(&image, "slot");
+ cbor_encode_int(&image, i);
+ cbor_encode_text_stringz(&image, "version");
+
+ len = snprintf((char *)tmpbuf, sizeof(tmpbuf),
+ "%u.%u.%u.%u", hdr.ih_ver.iv_major, hdr.ih_ver.iv_minor,
+ hdr.ih_ver.iv_revision, (unsigned int)hdr.ih_ver.iv_build_num);
+ cbor_encode_text_stringz(&image, (char *)tmpbuf);
+ cbor_encoder_close_container(&images, &image);
}
- len += snprintf(ptr + len, BOOT_SERIAL_OUT_MAX - len, "]}");
- boot_serial_output(ptr, len);
- os_free(ptr);
+ cbor_encoder_close_container(&bs_rsp, &images);
+ cbor_encoder_close_container(&bs_root, &bs_rsp);
+ boot_serial_output();
}
/*
@@ -136,68 +157,62 @@ bs_list(char *buf, int len)
static void
bs_upload(char *buf, int len)
{
- char *ptr;
- char *data_ptr;
- uint32_t off, data_len = 0;
+ CborParser parser;
+ struct cbor_buf_reader reader;
+ struct CborValue value;
+ uint8_t img_data[400];
+ long long unsigned int off = UINT_MAX;
+ size_t img_blen = 0;
+ long long unsigned int data_len = UINT_MAX;
+ const struct cbor_attr_t attr[4] = {
+ [0] = {
+ .attribute = "data",
+ .type = CborAttrByteStringType,
+ .addr.bytestring.data = img_data,
+ .addr.bytestring.len = &img_blen,
+ .len = sizeof(img_data)
+ },
+ [1] = {
+ .attribute = "off",
+ .type = CborAttrUnsignedIntegerType,
+ .addr.uinteger = &off,
+ .nodefault = true
+ },
+ [2] = {
+ .attribute = "len",
+ .type = CborAttrUnsignedIntegerType,
+ .addr.uinteger = &data_len,
+ .nodefault = true
+ }
+ };
const struct flash_area *fap = NULL;
int rc;
- /*
- * should be json inside
- */
- ptr = bs_find_val(buf, "\"off\"");
- if (!ptr) {
- rc = NMGR_ERR_EINVAL;
- goto out;
- }
- off = strtoul(ptr, NULL, 10);
-
- if (off == 0) {
- ptr = bs_find_val(buf, "\"len\"");
- if (!ptr) {
- rc = NMGR_ERR_EINVAL;
- goto out;
- }
- data_len = strtoul(ptr, NULL, 10);
-
- }
- data_ptr = bs_find_val(buf, "\"data\"");
- if (!data_ptr) {
- rc = NMGR_ERR_EINVAL;
- goto out;
- }
- if (*data_ptr != '"') {
- rc = NMGR_ERR_EINVAL;
- goto out;
- }
- ++data_ptr;
- data_ptr = strsep(&data_ptr, "\"");
- if (!data_ptr) {
- rc = NMGR_ERR_EINVAL;
+ memset(img_data, 0, sizeof(img_data));
+ cbor_buf_reader_init(&reader, (uint8_t *)buf, len);
+ cbor_parser_init(&reader.r, 0, &parser, &value);
+ rc = cbor_read_object(&value, attr);
+ if (rc || off == UINT_MAX) {
+ rc = MGMT_ERR_EINVAL;
goto out;
}
- len = base64_decode(data_ptr, data_ptr);
- if (len <= 0) {
- rc = NMGR_ERR_EINVAL;
- goto out;
- }
- rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap);
+ rc = flash_area_open(flash_area_id_from_image_slot(0), &fap);
if (rc) {
- rc = NMGR_ERR_EINVAL;
+ rc = MGMT_ERR_EINVAL;
goto out;
}
if (off == 0) {
curr_off = 0;
if (data_len > fap->fa_size) {
- rc = NMGR_ERR_EINVAL;
+ rc = MGMT_ERR_EINVAL;
goto out;
}
rc = flash_area_erase(fap, 0, fap->fa_size);
if (rc) {
- rc = NMGR_ERR_EINVAL;
+ rc = MGMT_ERR_EINVAL;
goto out;
}
img_size = data_len;
@@ -206,26 +221,24 @@ bs_upload(char *buf, int len)
rc = 0;
goto out;
}
- rc = flash_area_write(fap, curr_off, data_ptr, len);
+ rc = flash_area_write(fap, curr_off, img_data, img_blen);
if (rc) {
- rc = NMGR_ERR_EINVAL;
+ rc = MGMT_ERR_EINVAL;
goto out;
}
- curr_off += len;
+ curr_off += img_blen;
out:
- ptr = os_malloc(BOOT_SERIAL_OUT_MAX);
- if (!ptr) {
- return;
- }
+ cbor_encoder_create_map(&bs_root, &bs_rsp, CborIndefiniteLength);
+ cbor_encode_text_stringz(&bs_rsp, "rc");
+ cbor_encode_int(&bs_rsp, rc);
if (rc == 0) {
- len = snprintf(ptr, BOOT_SERIAL_OUT_MAX, "{\"rc\":%d,\"off\":%u}",
- rc, (int)curr_off);
- } else {
- len = snprintf(ptr, BOOT_SERIAL_OUT_MAX, "{\"rc\":%d}", rc);
+ cbor_encode_text_stringz(&bs_rsp, "off");
+ cbor_encode_uint(&bs_rsp, curr_off);
}
- boot_serial_output(ptr, len);
- os_free(ptr);
+ cbor_encoder_close_container(&bs_root, &bs_rsp);
+
+ boot_serial_output();
flash_area_close(fap);
}
@@ -235,20 +248,23 @@ out:
static void
bs_echo_ctl(char *buf, int len)
{
- boot_serial_output(NULL, 0);
+ boot_serial_output();
}
/*
* Reset, and (presumably) boot to newly uploaded image. Flush console
* before restarting.
*/
-static void
+static int
bs_reset(char *buf, int len)
{
- char msg[] = "{\"rc\":0}";
+ cbor_encoder_create_map(&bs_root, &bs_rsp, CborIndefiniteLength);
+ cbor_encode_text_stringz(&bs_rsp, "rc");
+ cbor_encode_int(&bs_rsp, 0);
+ cbor_encoder_close_container(&bs_root, &bs_rsp);
- boot_serial_output(msg, strlen(msg));
- os_time_delay(250);
+ boot_serial_output();
+ os_cputime_delay_usecs(250000);
hal_system_reset();
}
@@ -273,10 +289,13 @@ boot_serial_input(char *buf, int len)
buf += sizeof(*hdr);
len -= sizeof(*hdr);
+ bs_writer.bytes_written = 0;
+ cbor_encoder_init(&bs_root, &bs_writer, 0);
+
/*
* Limited support for commands.
*/
- if (hdr->nh_group == NMGR_GROUP_ID_IMAGE) {
+ if (hdr->nh_group == MGMT_GROUP_ID_IMAGE) {
switch (hdr->nh_id) {
case IMGMGR_NMGR_OP_STATE:
bs_list(buf, len);
@@ -287,7 +306,7 @@ boot_serial_input(char *buf, int len)
default:
break;
}
- } else if (hdr->nh_group == NMGR_GROUP_ID_DEFAULT) {
+ } else if (hdr->nh_group == MGMT_GROUP_ID_DEFAULT) {
switch (hdr->nh_id) {
case NMGR_ID_CONS_ECHO_CTRL:
bs_echo_ctl(buf, len);
@@ -302,15 +321,21 @@ boot_serial_input(char *buf, int len)
}
static void
-boot_serial_output(char *data, int len)
+boot_serial_output(void)
{
+ char *data;
+ int len;
uint16_t crc;
uint16_t totlen;
char pkt_start[2] = { SHELL_NLIP_PKT_START1, SHELL_NLIP_PKT_START2 };
char buf[BOOT_SERIAL_OUT_MAX];
char encoded_buf[BASE64_ENCODE_SIZE(BOOT_SERIAL_OUT_MAX)];
+ data = bs_obuf;
+ len = bs_writer.bytes_written;
+
bs_hdr->nh_op++;
+ bs_hdr->nh_flags = NMGR_F_CBOR_RSP_COMPLETE;
bs_hdr->nh_len = htons(len);
bs_hdr->nh_group = htons(bs_hdr->nh_group);
@@ -376,9 +401,8 @@ boot_serial_in_dec(char *in, int inlen, char *out, int *out_off, int maxout)
* Task which waits reading console, expecting to get image over
* serial port.
*/
-int cont;
-static void
-boot_serial(void *arg)
+void
+boot_serial_start(int max_input)
{
int rc;
int off;
@@ -386,7 +410,6 @@ boot_serial(void *arg)
char *dec;
int dec_off;
int full_line;
- int max_input = (int)arg;
rc = console_init(NULL);
assert(rc == 0);
@@ -412,7 +435,6 @@ boot_serial(void *arg)
rc = boot_serial_in_dec(&buf[2], off - 2, dec, &dec_off, max_input);
} else if (buf[0] == SHELL_NLIP_DATA_START1 &&
buf[1] == SHELL_NLIP_DATA_START2) {
- ++cont;
rc = boot_serial_in_dec(&buf[2], off - 2, dec, &dec_off, max_input);
}
if (rc == 1) {
@@ -421,11 +443,3 @@ boot_serial(void *arg)
off = 0;
}
}
-
-int
-boot_serial_task_init(struct os_task *task, uint8_t prio, os_stack_t *stack,
- uint16_t stack_size, int max_input)
-{
- return os_task_init(task, "boot", boot_serial, (void *)max_input,
- prio, OS_WAIT_FOREVER, stack, stack_size);
-}
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/39a34a25/boot/boot_serial/src/boot_serial_priv.h
----------------------------------------------------------------------
diff --git a/boot/boot_serial/src/boot_serial_priv.h b/boot/boot_serial/src/boot_serial_priv.h
index b350173..146aa6a 100644
--- a/boot/boot_serial/src/boot_serial_priv.h
+++ b/boot/boot_serial/src/boot_serial_priv.h
@@ -30,19 +30,21 @@ extern "C" {
#define SHELL_NLIP_PKT_START1 6
#define SHELL_NLIP_PKT_START2 9
-#define SHELL_NLIP_DATA_START1 4
-#define SHELL_NLIP_DATA_START2 20
+#define SHELL_NLIP_DATA_START1 4
+#define SHELL_NLIP_DATA_START2 20
/*
* From newtmgr.h
*/
-#define NMGR_ERR_EINVAL 3
+#define MGMT_ERR_EINVAL 3
#define NMGR_OP_READ 0
#define NMGR_OP_WRITE 2
-#define NMGR_GROUP_ID_DEFAULT 0
-#define NMGR_GROUP_ID_IMAGE 1
+#define NMGR_F_CBOR_RSP_COMPLETE 0x01
+
+#define MGMT_GROUP_ID_DEFAULT 0
+#define MGMT_GROUP_ID_IMAGE 1
#define NMGR_ID_CONS_ECHO_CTRL 1
#define NMGR_ID_RESET 5
[2/3] incubator-mynewt-core git commit: imgmgr;
fix use of incorrect cbor encoder.
Posted by ma...@apache.org.
imgmgr; fix use of incorrect cbor encoder.
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/41e4670a
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/41e4670a
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/41e4670a
Branch: refs/heads/develop
Commit: 41e4670a896ce2087e4e5ae1354dc40c74a0e4ce
Parents: 39a34a2
Author: Marko Kiiskila <ma...@runtime.io>
Authored: Tue Nov 8 23:27:50 2016 -0800
Committer: Marko Kiiskila <ma...@runtime.io>
Committed: Tue Nov 8 23:27:50 2016 -0800
----------------------------------------------------------------------
mgmt/imgmgr/src/imgmgr_state.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/41e4670a/mgmt/imgmgr/src/imgmgr_state.c
----------------------------------------------------------------------
diff --git a/mgmt/imgmgr/src/imgmgr_state.c b/mgmt/imgmgr/src/imgmgr_state.c
index d3adf39..99bcb31 100644
--- a/mgmt/imgmgr/src/imgmgr_state.c
+++ b/mgmt/imgmgr/src/imgmgr_state.c
@@ -248,7 +248,7 @@ imgmgr_state_read(struct mgmt_cbuf *cb)
g_err |= cbor_encoder_create_map(&images, &image, CborIndefiniteLength);
g_err |= cbor_encode_text_stringz(&image, "slot");
- g_err |= cbor_encode_int(&rsp, i);
+ g_err |= cbor_encode_int(&image, i);
g_err |= cbor_encode_text_stringz(&image, "version");
imgr_ver_str(&ver, vers_str);
[3/3] incubator-mynewt-core git commit: boot;
don't start OS even when boot_serial is not defined.
Posted by ma...@apache.org.
boot; don't start OS even when boot_serial is not defined.
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/0c51338e
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/0c51338e
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/0c51338e
Branch: refs/heads/develop
Commit: 0c51338ed56eee51d3c1b76f90db842eae1cac44
Parents: 41e4670
Author: Marko Kiiskila <ma...@runtime.io>
Authored: Tue Nov 8 23:29:14 2016 -0800
Committer: Marko Kiiskila <ma...@runtime.io>
Committed: Tue Nov 8 23:29:14 2016 -0800
----------------------------------------------------------------------
apps/boot/src/boot.c | 12 +++---------
1 file changed, 3 insertions(+), 9 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/0c51338e/apps/boot/src/boot.c
----------------------------------------------------------------------
diff --git a/apps/boot/src/boot.c b/apps/boot/src/boot.c
index 43280c1..fbc4b7d 100755
--- a/apps/boot/src/boot.c
+++ b/apps/boot/src/boot.c
@@ -30,6 +30,7 @@
#if MYNEWT_VAL(BOOT_SERIAL)
#include <hal/hal_gpio.h>
#include <boot_serial/boot_serial.h>
+#include <sysinit/sysinit.h>
#endif
#include <console/console.h>
#include "bootutil/image.h"
@@ -39,12 +40,7 @@
#define AREA_DESC_MAX (BOOT_AREA_DESC_MAX)
#if MYNEWT_VAL(BOOT_SERIAL)
-#define BOOT_SER_PRIO_TASK 1
-#define BOOT_SER_STACK_SZ 512
#define BOOT_SER_CONS_INPUT 128
-
-static struct os_task boot_ser_task;
-static os_stack_t boot_ser_stack[BOOT_SER_STACK_SZ];
#endif
int
@@ -67,10 +63,8 @@ main(void)
*/
hal_gpio_init_in(BOOT_SERIAL_DETECT_PIN, BOOT_SERIAL_DETECT_PIN_CFG);
if (hal_gpio_read(BOOT_SERIAL_DETECT_PIN) == BOOT_SERIAL_DETECT_PIN_VAL) {
- rc = boot_serial_task_init(&boot_ser_task, BOOT_SER_PRIO_TASK,
- boot_ser_stack, BOOT_SER_STACK_SZ, BOOT_SER_CONS_INPUT);
- assert(rc == 0);
- os_start();
+ boot_serial_start(BOOT_SER_CONS_INPUT);
+ assert(0);
}
#endif
rc = boot_go(&rsp);