refactor newtmgr to use cbor instead of json
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/7f299efb Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/7f299efb Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/7f299efb Branch: refs/heads/develop Commit: 7f299efb657dccedbebe7254fbd495ae80409cb0 Parents: adbb00e Author: Paul Dietrich <paulfdietr...@yahoo.com> Authored: Tue Oct 11 16:51:50 2016 -0700 Committer: Paul Dietrich <paulfdietr...@yahoo.com> Committed: Thu Oct 13 11:08:40 2016 -0700 ---------------------------------------------------------------------- boot/split/src/split_netmgr.c | 60 ++-- encoding/cborattr/include/cborattr/cborattr.h | 130 ++++++++ encoding/cborattr/pkg.yml | 27 ++ encoding/cborattr/src/cborattr.c | 257 ++++++++++++++++ encoding/tinycbor/include/tinycbor/cbor.h | 20 +- .../tinycbor/include/tinycbor/cbor_buf_writer.h | 4 +- .../tinycbor/include/tinycbor/cbor_cnt_writer.h | 21 +- .../include/tinycbor/cbor_mbuf_writer.h | 9 +- encoding/tinycbor/src/cbor_buf_writer.c | 17 +- encoding/tinycbor/src/cbor_mbuf_reader.c | 10 +- encoding/tinycbor/src/cbor_mbuf_writer.c | 21 +- encoding/tinycbor/src/cborencoder.c | 7 +- encoding/tinycbor/src/cborparser.c | 13 +- mgmt/imgmgr/src/imgmgr.c | 129 ++++---- mgmt/imgmgr/src/imgmgr_boot.c | 71 ++--- mgmt/imgmgr/src/imgmgr_coredump.c | 59 ++-- mgmt/imgmgr/src/imgmgr_fs.c | 129 ++++---- mgmt/imgmgr/src/imgmgr_priv.h | 24 +- mgmt/imgmgr/src/imgmgr_state.c | 91 +++--- mgmt/mgmt/include/mgmt/mgmt.h | 13 +- mgmt/mgmt/src/mgmt.c | 17 +- mgmt/newtmgr/include/newtmgr/newtmgr.h | 4 +- mgmt/newtmgr/nmgr_os/src/newtmgr_os.c | 214 ++++++------- mgmt/newtmgr/pkg.yml | 2 +- mgmt/newtmgr/src/newtmgr.c | 168 +++------- sys/config/src/config_json_line.c | 14 +- sys/config/src/config_nmgr.c | 32 +- sys/config/src/config_priv.h | 6 +- sys/log/src/log_nmgr.c | 303 ++++++++++--------- sys/stats/src/stats_nmgr.c | 113 ++++--- test/crash_test/src/crash_nmgr.c | 14 +- 31 files changed, 1124 insertions(+), 875 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/7f299efb/boot/split/src/split_netmgr.c ---------------------------------------------------------------------- diff --git a/boot/split/src/split_netmgr.c b/boot/split/src/split_netmgr.c index 50c21f5..ae22554 100644 --- a/boot/split/src/split_netmgr.c +++ b/boot/split/src/split_netmgr.c @@ -17,7 +17,8 @@ * under the License. */ -#include <json/json.h> +#include <tinycbor/cbor.h> +#include <cborattr/cborattr.h> #include <mgmt/mgmt.h> #include <bootutil/bootutil_misc.h> #include <bootutil/image.h> @@ -25,8 +26,8 @@ #include <split_priv.h> -static int imgr_splitapp_read(struct mgmt_jbuf *njb); -static int imgr_splitapp_write(struct mgmt_jbuf *njb); +static int imgr_splitapp_read(struct mgmt_cbuf *cb); +static int imgr_splitapp_write(struct mgmt_cbuf *cb); static const struct mgmt_handler split_nmgr_handlers[] = { [SPLIT_NMGR_OP_SPLIT] = { @@ -51,56 +52,56 @@ split_nmgr_register(void) } int -imgr_splitapp_read(struct mgmt_jbuf *njb) +imgr_splitapp_read(struct mgmt_cbuf *cb) { int x; - struct json_encoder *enc; - struct json_value jv; + CborError g_err = CborNoError; + CborEncoder *penc = &cb->encoder; + CborEncoder rsp; - enc = &njb->mjb_enc; - - json_encode_object_start(enc); + g_err |= cbor_encoder_create_map(penc, &rsp, CborIndefiniteLength); x = split_mode_get(); - JSON_VALUE_INT(&jv, x) - json_encode_object_entry(enc, "splitMode", &jv); + g_err |= cbor_encode_text_stringz(&rsp, "splitMode"); + g_err |= cbor_encode_int(&rsp, x); - JSON_VALUE_INT(&jv, split_check_status()) - json_encode_object_entry(enc, "splitStatus", &jv); + x = split_check_status(); + g_err |= cbor_encode_text_stringz(&rsp, "splitStatus"); + g_err |= cbor_encode_int(&rsp, x); - JSON_VALUE_INT(&jv, MGMT_ERR_EOK); - json_encode_object_entry(enc, "rc", &jv); + g_err |= cbor_encode_text_stringz(&rsp, "rc"); + g_err |= cbor_encode_int(&rsp, MGMT_ERR_EOK); - json_encode_object_finish(enc); + g_err |= cbor_encoder_close_container(penc, &rsp); return 0; } int -imgr_splitapp_write(struct mgmt_jbuf *njb) +imgr_splitapp_write(struct mgmt_cbuf *cb) { long long int split_mode; long long int send_split_status; /* ignored */ long long int sent_rc; /* ignored */ - const struct json_attr_t split_write_attr[4] = { + const struct cbor_attr_t split_write_attr[4] = { [0] = { .attribute = "splitMode", - .type = t_integer, + .type = CborAttrIntegerType, .addr.integer = &split_mode, .nodefault = true, }, [1] = { .attribute = "splitStatus", - .type = t_integer, + .type = CborAttrIntegerType, .addr.integer = &send_split_status, .nodefault = true, }, [2] = { .attribute = "rc", - .type = t_integer, + .type = CborAttrIntegerType, .addr.integer = &sent_rc, .nodefault = true, }, @@ -109,11 +110,9 @@ imgr_splitapp_write(struct mgmt_jbuf *njb) .attribute = NULL } }; - struct json_encoder *enc; - struct json_value jv; int rc; - rc = json_read_object(&njb->mjb_buf, split_write_attr); + rc = cbor_read_object(&cb->it, split_write_attr); if (rc) { rc = MGMT_ERR_EINVAL; goto err; @@ -125,17 +124,8 @@ imgr_splitapp_write(struct mgmt_jbuf *njb) goto err; } - enc = &njb->mjb_enc; - - json_encode_object_start(enc); - - JSON_VALUE_INT(&jv, MGMT_ERR_EOK); - json_encode_object_entry(enc, "rc", &jv); - - json_encode_object_finish(enc); - - return 0; + rc = MGMT_ERR_EOK; err: - mgmt_jbuf_setoerr(njb, rc); + mgmt_cbuf_setoerr(cb, rc); return 0; } http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/7f299efb/encoding/cborattr/include/cborattr/cborattr.h ---------------------------------------------------------------------- diff --git a/encoding/cborattr/include/cborattr/cborattr.h b/encoding/cborattr/include/cborattr/cborattr.h new file mode 100644 index 0000000..bc343be --- /dev/null +++ b/encoding/cborattr/include/cborattr/cborattr.h @@ -0,0 +1,130 @@ +/** + * 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 CBORATTR_H +#define CBORATTR_H + +#include <stdbool.h> +#include <stdint.h> +#include <ctype.h> +#include <stdio.h> +#include <sys/types.h> +#include <tinycbor/cbor.h> + +#ifdef __cplusplus +extern "C" { +#endif + +/* This library wraps the tinycbor decoder with a attribute based decoder + * suitable for decoding a binary version of json. Specificallly, the + * contents of the cbor contains pairs of attributes. where the attribute + * is a key/value pair. keys are always text strings, but values can be + * many different things (enumerated below) */ + + /* maximum size of the string attribute name for this decoder */ +#define CBOR_ATTR_MAX (128) + +typedef enum CborAttrType { + CborAttrIntegerType = 1, + CborAttrUnsignedIntegerType, + CborAttrByteStringType, + CborAttrTextStringType, + CborAttrBooleanType, + CborAttrFloatType, + CborAttrDoubleType, + CborAttrArrayType, + CborAttrNullType, +} CborAttrType; + +struct cbor_attr_t; + +struct cbor_enum_t { + char *name; + long long int value; +}; + +struct cbor_array_t { + CborAttrType element_type; + union { + struct { + const struct cbor_attr_t *subtype; + char *base; + size_t stride; + } objects; + struct { + char **ptrs; + char *store; + int storelen; + } strings; + struct { + long long int *store; + } integers; + struct { + long long unsigned int *store; + } uintegers; + struct { + double *store; + } reals; + struct { + bool *store; + } booleans; + } arr; + int *count; + int maxlen; +}; + +struct cbor_attr_t { + char *attribute; + CborAttrType type; + union { + long long int *integer; + long long unsigned int *uinteger; + double *real; + float *fval; + char *string; + bool *boolean; + struct byte_string { + uint8_t *data; + size_t *len; + } bytestring; + struct cbor_array_t array; + size_t offset; + } addr; + union { + long long int integer; + double real; + bool boolean; + float fval; + } dflt; + size_t len; + const struct json_enum_t *map; + bool nodefault; +}; + +int cbor_read_object(struct CborValue *, const struct cbor_attr_t *); +int cbor_read_array(struct CborParser *, const struct cbor_array_t *); + + +#ifdef __cplusplus +} +#endif + +#endif /* CBORATTR_H */ + http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/7f299efb/encoding/cborattr/pkg.yml ---------------------------------------------------------------------- diff --git a/encoding/cborattr/pkg.yml b/encoding/cborattr/pkg.yml new file mode 100644 index 0000000..b443d8e --- /dev/null +++ b/encoding/cborattr/pkg.yml @@ -0,0 +1,27 @@ +# +# 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. +# + +pkg.name: encoding/cborattr +pkg.description: CBOR encoding/decoding library +pkg.author: "Apache Mynewt <d...@mynewt.incubator.apache.org>" +pkg.homepage: "http://mynewt.apache.org/" +pkg.keywords: + +pkg.deps: + - "@apache-mynewt-core/encoding/tinycbor" \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/7f299efb/encoding/cborattr/src/cborattr.c ---------------------------------------------------------------------- diff --git a/encoding/cborattr/src/cborattr.c b/encoding/cborattr/src/cborattr.c new file mode 100644 index 0000000..5dd5308 --- /dev/null +++ b/encoding/cborattr/src/cborattr.c @@ -0,0 +1,257 @@ +/** + * 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 <cborattr/cborattr.h> +#include <tinycbor/cbor.h> + +/* this maps a CborType to a matching CborAtter Type. The mapping is not + * one-to-one because of signedness of integers + * and therefore we need a function to do this trickery */ +static int +valid_attr_type(CborType ct, CborAttrType at) { + switch (at) { + case CborAttrIntegerType: + case CborAttrUnsignedIntegerType: + if(ct == CborIntegerType) { + return 1; + } + break; + case CborAttrByteStringType: + if (ct == CborByteStringType) { + return 1; + } + break; + case CborAttrTextStringType: + if (ct == CborTextStringType) { + return 1; + } + break; + case CborAttrBooleanType: + if (ct == CborBooleanType) { + return 1; + } + case CborAttrFloatType: + if (ct == CborFloatType) { + return 1; + } + break; + case CborAttrDoubleType: + if (ct == CborDoubleType) { + return 1; + } + break; + case CborAttrArrayType: + if (ct == CborArrayType) { + return 1; + } + break; + case CborAttrNullType: + if (ct == CborNullType) { + return 1; + } + break; + } + return 0; +} + +/* this function find the pointer to the memory location to + * write or read and attribute from the cbor_attr_r structure */ +static char * +cbor_target_address(const struct cbor_attr_t *cursor, + const struct cbor_array_t *parent, int offset) +{ + char *targetaddr = NULL; + if (parent == NULL || parent->element_type != CborAttrArrayType) { + /* ordinary case - use the address in the cursor structure */ + switch (cursor->type) { + case CborAttrNullType: + targetaddr = NULL; + break; + case CborAttrIntegerType: + targetaddr = (char *)&cursor->addr.integer[offset]; + break; + case CborAttrUnsignedIntegerType: + targetaddr = (char *)&cursor->addr.uinteger[offset]; + break; + case CborAttrFloatType: + targetaddr = (char *)&cursor->addr.fval[offset]; + break; + case CborAttrDoubleType: + targetaddr = (char *)&cursor->addr.real[offset]; + break; + case CborAttrByteStringType: + targetaddr = (char *) cursor->addr.bytestring.data; + break; + case CborAttrTextStringType: + targetaddr = cursor->addr.string; + break; + case CborAttrBooleanType: + targetaddr = (char *)&cursor->addr.boolean[offset]; + break; + default: + targetaddr = NULL; + break; + } + } else { + /* tricky case - hacking a member in an array of structures */ + targetaddr = + parent->arr.objects.base + (offset * parent->arr.objects.stride) + + cursor->addr.offset; + } + return targetaddr; +} + +static int +cbor_internal_read_object(CborValue *root_value, + const struct cbor_attr_t *attrs, + const struct cbor_array_t *parent, + int offset) { + const struct cbor_attr_t *cursor; + char attrbuf[CBOR_ATTR_MAX + 1]; + char *lptr; + CborValue cur_value; + CborError g_err = 0; + size_t len; + CborType type = CborInvalidType; + + /* stuff fields with defaults in case they're omitted in the JSON input */ + for (cursor = attrs; cursor->attribute != NULL; cursor++) { + if (!cursor->nodefault) { + lptr = cbor_target_address(cursor, parent, offset); + if (lptr != NULL) { + switch (cursor->type) { + case CborAttrIntegerType: + memcpy(lptr, &cursor->dflt.integer, sizeof(long long int)); + break; + case CborAttrUnsignedIntegerType: + memcpy(lptr, &cursor->dflt.integer, sizeof(long long unsigned int)); + break; + case CborAttrBooleanType: + memcpy(lptr, &cursor->dflt.boolean, sizeof(bool)); + break; + case CborAttrFloatType: + memcpy(lptr, &cursor->dflt.fval, sizeof(float)); + break; + case CborAttrDoubleType: + memcpy(lptr, &cursor->dflt.real, sizeof(double)); + break; + default: + break; + } + } + } + } + + if (cbor_value_is_map(root_value)) { + g_err |= cbor_value_enter_container(root_value, &cur_value); + } else { + g_err |= CborErrorIllegalType; + return g_err; + } + + /* contains key value pairs */ + while (cbor_value_is_valid(&cur_value)) { + /* get the attribute */ + if (cbor_value_is_text_string(&cur_value)) { + if (cbor_value_calculate_string_length(&cur_value, &len) == 0) { + if (len > CBOR_ATTR_MAX) { + g_err |= CborErrorDataTooLarge; + goto g_err_return; + } + g_err |= cbor_value_copy_text_string(&cur_value, attrbuf, &len, NULL); + } + } else { + g_err |= CborErrorIllegalType; + goto g_err_return; + } + + /* at least get the type of the next value so we can match the + * attribute name and type for a perfect match */ + g_err |= cbor_value_advance(&cur_value); + if(cbor_value_is_valid(&cur_value)) { + type = cbor_value_get_type(&cur_value); + } else { + g_err |= CborErrorIllegalType; + goto g_err_return; + } + + /* find this attribute in our list */ + for (cursor = attrs; cursor->attribute != NULL; cursor++) { + if ( valid_attr_type(type, cursor->type) && + (memcmp(cursor->attribute, attrbuf, len) == 0 )) { + break; + } + } + + /* we found a match */ + if ( cursor->attribute != NULL ) { + lptr = cbor_target_address(cursor, parent, offset); + switch (cursor->type) { + case CborAttrNullType: + /* nothing to do */ + break; + case CborAttrBooleanType: + g_err |= cbor_value_get_boolean(&cur_value, (bool *) lptr); + break; + case CborAttrIntegerType: + g_err |= cbor_value_get_int64(&cur_value, (long long int *) lptr); + break; + case CborAttrUnsignedIntegerType: + g_err |= cbor_value_get_uint64(&cur_value, (long long unsigned int *) lptr); + break; + case CborAttrFloatType: + g_err |= cbor_value_get_float(&cur_value, (float *) lptr); + break; + case CborAttrDoubleType: + g_err |= cbor_value_get_double(&cur_value, (double *) lptr); + break; + case CborAttrByteStringType: + { + size_t len = cursor->len; + g_err |= cbor_value_copy_byte_string(&cur_value, (unsigned char *) lptr, &len, NULL); + *cursor->addr.bytestring.len = len; + break; + } + case CborAttrTextStringType: + { + size_t len = cursor->len; + g_err |= cbor_value_copy_text_string(&cur_value, (char *) lptr, &len, NULL); + break; + } + /* TODO array */ + default: + g_err |= CborErrorIllegalType; + } + } + cbor_value_advance(&cur_value); + } +g_err_return: + /* that should be it for this container */ + g_err |= cbor_value_leave_container(root_value, &cur_value); + return g_err; +} + +int +cbor_read_object(struct CborValue *value, const struct cbor_attr_t *attrs) +{ + int st; + + st = cbor_internal_read_object(value, attrs, NULL, 0); + return st; +} http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/7f299efb/encoding/tinycbor/include/tinycbor/cbor.h ---------------------------------------------------------------------- diff --git a/encoding/tinycbor/include/tinycbor/cbor.h b/encoding/tinycbor/include/tinycbor/cbor.h index 6ec5ce0..0d280ba 100644 --- a/encoding/tinycbor/include/tinycbor/cbor.h +++ b/encoding/tinycbor/include/tinycbor/cbor.h @@ -157,8 +157,15 @@ typedef enum CborError { CBOR_API const char *cbor_error_string(CborError error); +struct cbor_encoder_writer; + +typedef int (cbor_encoder_write)(struct cbor_encoder_writer *, const char *data, int len); + +typedef struct cbor_encoder_writer { + cbor_encoder_write *write; + int bytes_written; +} cbor_encoder_writer; -typedef int (cbor_encoder_writer)(void *arg, const char *data, int len); /* Encoder API */ struct CborEncoder @@ -173,7 +180,7 @@ typedef struct CborEncoder CborEncoder; static const size_t CborIndefiniteLength = SIZE_MAX; -CBOR_API void cbor_encoder_init(CborEncoder *encoder, cbor_encoder_writer *pwriter, void *writer_arg, int flags); +CBOR_API void cbor_encoder_init(CborEncoder *encoder, cbor_encoder_writer *pwriter, int flags); CBOR_API CborError cbor_encode_uint(CborEncoder *encoder, uint64_t value); CBOR_API CborError cbor_encode_int(CborEncoder *encoder, int64_t value); CBOR_API CborError cbor_encode_negative_int(CborEncoder *encoder, uint64_t absolute_value); @@ -184,7 +191,8 @@ CBOR_INLINE_API CborError cbor_encode_text_stringz(CborEncoder *encoder, const c { return cbor_encode_text_string(encoder, string, strlen(string)); } CBOR_API CborError cbor_encode_byte_string(CborEncoder *encoder, const uint8_t *string, size_t length); CBOR_API CborError cbor_encode_floating_point(CborEncoder *encoder, CborType fpType, const void *value); - +CBOR_INLINE_API CborError cbor_encode_bytes_written(CborEncoder *encoder) +{ return encoder->writer->bytes_written; } CBOR_INLINE_API CborError cbor_encode_boolean(CborEncoder *encoder, bool value) { return cbor_encode_simple_value(encoder, (int)value - 1 + (CborBooleanType & 0x1f)); } CBOR_INLINE_API CborError cbor_encode_null(CborEncoder *encoder) @@ -480,6 +488,12 @@ CBOR_INLINE_API CborError cbor_value_to_pretty(FILE *out, const CborValue *value return cbor_value_to_pretty_advance(out, ©); } +struct mgmt_cbuf { + struct CborParser parser; + struct CborEncoder encoder; + struct CborValue it; +}; + #ifdef __cplusplus } #endif http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/7f299efb/encoding/tinycbor/include/tinycbor/cbor_buf_writer.h ---------------------------------------------------------------------- diff --git a/encoding/tinycbor/include/tinycbor/cbor_buf_writer.h b/encoding/tinycbor/include/tinycbor/cbor_buf_writer.h index 2f98154..1e66fff 100644 --- a/encoding/tinycbor/include/tinycbor/cbor_buf_writer.h +++ b/encoding/tinycbor/include/tinycbor/cbor_buf_writer.h @@ -25,6 +25,7 @@ extern "C" { #endif struct CborBufWriter { + struct cbor_encoder_writer enc; uint8_t *ptr; const uint8_t *end; }; @@ -32,9 +33,6 @@ struct CborBufWriter { void cbor_buf_writer_init(struct CborBufWriter *cb, uint8_t *buffer, size_t data); -int -cbor_buf_writer(void *arg, const char *data, int len); - size_t cbor_buf_writer_buffer_size(struct CborBufWriter *cb, const uint8_t *buffer); http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/7f299efb/encoding/tinycbor/include/tinycbor/cbor_cnt_writer.h ---------------------------------------------------------------------- diff --git a/encoding/tinycbor/include/tinycbor/cbor_cnt_writer.h b/encoding/tinycbor/include/tinycbor/cbor_cnt_writer.h index 1db57e6..deb8f89 100644 --- a/encoding/tinycbor/include/tinycbor/cbor_cnt_writer.h +++ b/encoding/tinycbor/include/tinycbor/cbor_cnt_writer.h @@ -20,6 +20,9 @@ #ifndef CBOR_CNT_WRITER_H #define CBOR_CNT_WRITER_H +#include "cbor.h" + + #ifdef __cplusplus extern "C" { #endif @@ -31,24 +34,20 @@ extern "C" { */ struct CborCntWriter { - int bytes_written; + struct cbor_encoder_writer enc; }; -inline void -cbor_cnt_writer_init(struct CborCntWriter *cb) { - cb->bytes_written = 0; -} - inline int -cbor_cnt_writer(void *arg, char *data, int len) { +cbor_cnt_writer(struct cbor_encoder_writer *arg, const char *data, int len) { struct CborCntWriter *cb = (struct CborCntWriter *) arg; - cb->bytes_written += len; + cb->enc.bytes_written += len; return CborNoError; } -inline int -cbor_cnt_writer_length(struct CborCntWriter *cb) { - return cb->bytes_written; +inline void +cbor_cnt_writer_init(struct CborCntWriter *cb) { + cb->enc.bytes_written = 0; + cb->enc.write = &cbor_cnt_writer; } #ifdef __cplusplus http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/7f299efb/encoding/tinycbor/include/tinycbor/cbor_mbuf_writer.h ---------------------------------------------------------------------- diff --git a/encoding/tinycbor/include/tinycbor/cbor_mbuf_writer.h b/encoding/tinycbor/include/tinycbor/cbor_mbuf_writer.h index 1373846..1304c70 100644 --- a/encoding/tinycbor/include/tinycbor/cbor_mbuf_writer.h +++ b/encoding/tinycbor/include/tinycbor/cbor_mbuf_writer.h @@ -26,20 +26,13 @@ extern "C" { struct CborMbufWriter { - int bytes_written; + struct cbor_encoder_writer enc; struct os_mbuf *m; }; void cbor_mbuf_writer_init(struct CborMbufWriter *cb, struct os_mbuf *m); -int -cbor_mbuf_writer(void *arg, const char *data, int len); - - -int -cbor_mbuf_bytes_written(struct CborMbufWriter *cb); - #ifdef __cplusplus } #endif http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/7f299efb/encoding/tinycbor/src/cbor_buf_writer.c ---------------------------------------------------------------------- diff --git a/encoding/tinycbor/src/cbor_buf_writer.c b/encoding/tinycbor/src/cbor_buf_writer.c index b734f81..c5097e9 100644 --- a/encoding/tinycbor/src/cbor_buf_writer.c +++ b/encoding/tinycbor/src/cbor_buf_writer.c @@ -20,12 +20,6 @@ #include <tinycbor/cbor.h> #include <tinycbor/cbor_buf_writer.h> -void cbor_buf_writer_init(struct CborBufWriter *cb, uint8_t *buffer, size_t size) -{ - cb->ptr = buffer; - cb->end = buffer + size; -} - static inline int would_overflow(struct CborBufWriter *cb, size_t len) { ptrdiff_t remaining = (ptrdiff_t)cb->end; @@ -34,7 +28,7 @@ static inline int would_overflow(struct CborBufWriter *cb, size_t len) return (remaining < 0); } -int cbor_buf_writer(void *arg, const char *data, int len) { +int cbor_buf_writer(struct cbor_encoder_writer *arg, const char *data, int len) { struct CborBufWriter *cb = (struct CborBufWriter *) arg; if (would_overflow(cb, len)) { @@ -43,9 +37,18 @@ int cbor_buf_writer(void *arg, const char *data, int len) { memcpy(cb->ptr, data, len); cb->ptr += len; + cb->enc.bytes_written += len; return CborNoError; } +void cbor_buf_writer_init(struct CborBufWriter *cb, uint8_t *buffer, size_t size) +{ + cb->ptr = buffer; + cb->end = buffer + size; + cb->enc.bytes_written = 0; + cb->enc.write = cbor_buf_writer; +} + size_t cbor_buf_writer_buffer_size(struct CborBufWriter *cb, const uint8_t *buffer) { http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/7f299efb/encoding/tinycbor/src/cbor_mbuf_reader.c ---------------------------------------------------------------------- diff --git a/encoding/tinycbor/src/cbor_mbuf_reader.c b/encoding/tinycbor/src/cbor_mbuf_reader.c index f37908b..c097a49 100644 --- a/encoding/tinycbor/src/cbor_mbuf_reader.c +++ b/encoding/tinycbor/src/cbor_mbuf_reader.c @@ -61,8 +61,14 @@ cbor_mbuf_reader_cmp(struct cbor_decoder_reader *d, char *buf, int offset, size_ static uintptr_t cbor_mbuf_reader_cpy(struct cbor_decoder_reader *d, char *dst, int offset, size_t len) { + int rc; struct CborMbufReader *cb = (struct CborMbufReader *) d; - return !os_mbuf_copydata(cb->m, offset + cb->init_off, len, dst); + rc = os_mbuf_copydata(cb->m, offset + cb->init_off, len, dst); + + if(rc == 0) { + return true; + } + return false; } void @@ -81,5 +87,5 @@ cbor_mbuf_reader_init(struct CborMbufReader *cb, struct os_mbuf *m, hdr = OS_MBUF_PKTHDR(m); cb->m = m; cb->init_off = initial_offset; - cb->r.message_size = hdr->omp_len; + cb->r.message_size = hdr->omp_len - initial_offset; } http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/7f299efb/encoding/tinycbor/src/cbor_mbuf_writer.c ---------------------------------------------------------------------- diff --git a/encoding/tinycbor/src/cbor_mbuf_writer.c b/encoding/tinycbor/src/cbor_mbuf_writer.c index f40d0a3..030951c 100644 --- a/encoding/tinycbor/src/cbor_mbuf_writer.c +++ b/encoding/tinycbor/src/cbor_mbuf_writer.c @@ -19,16 +19,11 @@ #include <tinycbor/cbor.h> #include <os/os_mbuf.h> +#include <tinycbor/cbor.h> #include <tinycbor/cbor_mbuf_writer.h> -void -cbor_mbuf_writer_init(struct CborMbufWriter *cb, struct os_mbuf *m) { - cb->m = m; - cb->bytes_written = 0; -} - int -cbor_mbuf_writer(void *arg, const char *data, int len) { +cbor_mbuf_writer(struct cbor_encoder_writer *arg, const char *data, int len) { int rc; struct CborMbufWriter *cb = (struct CborMbufWriter *) arg; rc = os_mbuf_append(cb->m, data, len); @@ -37,11 +32,15 @@ cbor_mbuf_writer(void *arg, const char *data, int len) { return CborErrorOutOfMemory; } - cb->bytes_written += len; + cb->enc.bytes_written += len; return CborNoError; } -int -cbor_mbuf_bytes_written(struct CborMbufWriter *cb) { - return cb->bytes_written; + +void +cbor_mbuf_writer_init(struct CborMbufWriter *cb, struct os_mbuf *m) { + cb->m = m; + cb->enc.bytes_written = 0; + cb->enc.write = &cbor_mbuf_writer; } + http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/7f299efb/encoding/tinycbor/src/cborencoder.c ---------------------------------------------------------------------- diff --git a/encoding/tinycbor/src/cborencoder.c b/encoding/tinycbor/src/cborencoder.c index 0c71d26..b510662 100644 --- a/encoding/tinycbor/src/cborencoder.c +++ b/encoding/tinycbor/src/cborencoder.c @@ -199,10 +199,9 @@ * buffer of size \a size. The \a flags field is currently unused and must be * zero. */ -void cbor_encoder_init(CborEncoder *encoder, cbor_encoder_writer *writer, void* writer_arg, int flags) +void cbor_encoder_init(CborEncoder *encoder, cbor_encoder_writer *writer, int flags) { encoder->writer = writer; - encoder->writer_arg = writer_arg; encoder->added = 0; encoder->flags = flags; } @@ -238,7 +237,7 @@ static inline void put64(void *where, uint64_t v) static inline CborError append_to_buffer(CborEncoder *encoder, const void *data, size_t len) { - return encoder->writer(encoder->writer_arg, data, len); + return encoder->writer->write(encoder->writer, data, len); } static inline CborError append_byte_to_buffer(CborEncoder *encoder, uint8_t byte) @@ -423,7 +422,6 @@ static CborError create_container(CborEncoder *encoder, CborEncoder *container, { CborError err; container->writer = encoder->writer; - container->writer_arg = encoder->writer_arg; ++encoder->added; container->added = 0; @@ -502,7 +500,6 @@ CborError cbor_encoder_create_map(CborEncoder *encoder, CborEncoder *mapEncoder, CborError cbor_encoder_close_container(CborEncoder *encoder, const CborEncoder *containerEncoder) { encoder->writer = containerEncoder->writer; - encoder->writer_arg = containerEncoder->writer_arg; if (containerEncoder->flags & CborIteratorFlag_UnknownLength) return append_byte_to_buffer(encoder, BreakByte); http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/7f299efb/encoding/tinycbor/src/cborparser.c ---------------------------------------------------------------------- diff --git a/encoding/tinycbor/src/cborparser.c b/encoding/tinycbor/src/cborparser.c index 3b91448..ca7ddb7 100644 --- a/encoding/tinycbor/src/cborparser.c +++ b/encoding/tinycbor/src/cborparser.c @@ -984,12 +984,13 @@ static CborError iterate_string_chunks(const CborValue *value, char *buffer, siz /* is there enough room for the ending NUL byte? */ if (*result && *buflen > total) { - /* we are just trying to write a NULL byte here, but we have - * to create this decoder since we may be calling different - * functions through the pointer below */ - struct cbor_buf_reader cb; - cbor_buf_reader_init(&cb, (const uint8_t *) "", 1); - *result = !!func(&cb.r, buffer + total, 0, 1); + /* we are just trying to write a NULL byte here,, but this is hard + * because this is called by function pointer with an abstract + * reader. Since this is the output buffer, we can assume that if + * we have a valid buffer its ok to write a NULL here */ + if(buffer) { + *(buffer + total) = '\0'; + } } *buflen = total; http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/7f299efb/mgmt/imgmgr/src/imgmgr.c ---------------------------------------------------------------------- diff --git a/mgmt/imgmgr/src/imgmgr.c b/mgmt/imgmgr/src/imgmgr.c index 20c93af..52ff282 100644 --- a/mgmt/imgmgr/src/imgmgr.c +++ b/mgmt/imgmgr/src/imgmgr.c @@ -26,8 +26,7 @@ #include "sysflash/sysflash.h" #include "hal/hal_bsp.h" #include "flash_map/flash_map.h" -#include "json/json.h" -#include "base64/base64.h" +#include "cborattr/cborattr.h" #include "bootutil/image.h" #include "bootutil/bootutil_misc.h" #include "mgmt/mgmt.h" @@ -35,8 +34,8 @@ #include "imgmgr/imgmgr.h" #include "imgmgr_priv.h" -static int imgr_list2(struct mgmt_jbuf *); -static int imgr_upload(struct mgmt_jbuf *); +static int imgr_list2(struct mgmt_cbuf *); +static int imgr_upload(struct mgmt_cbuf *); static const struct mgmt_handler imgr_nmgr_handlers[] = { [IMGMGR_NMGR_OP_LIST] = { @@ -250,106 +249,91 @@ imgr_find_by_hash(uint8_t *find, struct image_version *ver) } static int -imgr_list2(struct mgmt_jbuf *njb) +imgr_list2(struct mgmt_cbuf *cb) { - struct json_encoder *enc; int i; int rc; uint32_t flags; struct image_version ver; uint8_t hash[IMGMGR_HASH_LEN]; /* SHA256 hash */ - struct json_value jv; char vers_str[IMGMGR_NMGR_MAX_VER]; - char hash_str[IMGMGR_HASH_STR + 1]; int ver_len; + CborEncoder *penc = &cb->encoder; + CborError g_err = CborNoError; + CborEncoder rsp, images, image; - enc = &njb->mjb_enc; + g_err |= cbor_encoder_create_map(penc, &rsp, CborIndefiniteLength); + g_err |= cbor_encode_text_stringz(&rsp, "images"); + g_err |= cbor_encoder_create_array(&rsp, &images, CborIndefiniteLength); - json_encode_object_start(enc); - json_encode_array_name(enc, "images"); - json_encode_array_start(enc); for (i = 0; i < 2; i++) { rc = imgr_read_info(i, &ver, hash, &flags); if (rc != 0) { continue; } - json_encode_object_start(enc); - - JSON_VALUE_INT(&jv, i); - json_encode_object_entry(enc, "slot", &jv); - + g_err |= cbor_encoder_create_map(&images, &image, CborIndefiniteLength); + g_err |= cbor_encode_text_stringz(&image, "slot"); + g_err |= cbor_encode_int(&image, i); + g_err |= cbor_encode_text_stringz(&image, "version"); ver_len = imgr_ver_str(&ver, vers_str); - JSON_VALUE_STRINGN(&jv, vers_str, ver_len); - json_encode_object_entry(enc, "version", &jv); - - base64_encode(hash, IMGMGR_HASH_LEN, hash_str, 1); - JSON_VALUE_STRING(&jv, hash_str); - json_encode_object_entry(enc, "hash", &jv); - - JSON_VALUE_BOOL(&jv, !(flags & IMAGE_F_NON_BOOTABLE)); - json_encode_object_entry(enc, "bootable", &jv); - - json_encode_object_finish(enc); + g_err |= cbor_encode_text_string(&image, vers_str, ver_len); + g_err |= cbor_encode_text_stringz(&image, "hash"); + g_err |= cbor_encode_byte_string(&image, hash, IMGMGR_HASH_LEN); + g_err |= cbor_encode_text_stringz(&image, "bootable"); + g_err |= cbor_encode_boolean(&image, !(flags & IMAGE_F_NON_BOOTABLE)); + g_err |= cbor_encoder_close_container(&images, &image); } - json_encode_array_finish(enc); - json_encode_object_finish(enc); + g_err |= cbor_encoder_close_container(&rsp, &images); + g_err |= cbor_encoder_close_container(penc, &rsp); - return 0; + return g_err; } static int -imgr_upload(struct mgmt_jbuf *njb) +imgr_upload(struct mgmt_cbuf *cb) { - char img_data[BASE64_ENCODE_SIZE(IMGMGR_NMGR_MAX_MSG)]; + uint8_t img_data[IMGMGR_NMGR_MAX_MSG]; long long unsigned int off = UINT_MAX; long long unsigned int size = UINT_MAX; - const struct json_attr_t off_attr[4] = { + size_t data_len = 0; + const struct cbor_attr_t off_attr[4] = { [0] = { - .attribute = "off", - .type = t_uinteger, - .addr.uinteger = &off, - .nodefault = true - }, - [1] = { .attribute = "data", - .type = t_string, - .addr.string = img_data, + .type = CborAttrByteStringType, + .addr.bytestring.data = img_data, + .addr.bytestring.len = &data_len, .len = sizeof(img_data) }, - [2] = { + [1] = { .attribute = "len", - .type = t_uinteger, + .type = CborAttrUnsignedIntegerType, .addr.uinteger = &size, .nodefault = true }, + [2] = { + .attribute = "off", + .type = CborAttrUnsignedIntegerType, + .addr.uinteger = &off, + .nodefault = true + }, [3] = { 0 }, }; struct image_version ver; struct image_header *hdr; - struct json_encoder *enc; - struct json_value jv; int area_id; + int best; int rc; - int len; int i; - rc = json_read_object(&njb->mjb_buf, off_attr); + rc = cbor_read_object(&cb->it, off_attr); if (rc || off == UINT_MAX) { rc = MGMT_ERR_EINVAL; goto err; } - len = strlen(img_data); - if (len) { - len = base64_decode(img_data, img_data); - if (len < 0) { - rc = MGMT_ERR_EINVAL; - goto err; - } - } if (off == 0) { - if (len < sizeof(struct image_header)) { + if (data_len < sizeof(struct image_header)) { /* * Image header is the first thing in the image. */ @@ -430,14 +414,14 @@ imgr_upload(struct mgmt_jbuf *njb) rc = MGMT_ERR_EINVAL; goto err; } - if (len) { + if (data_len) { rc = flash_area_write(imgr_state.upload.fa, imgr_state.upload.off, - img_data, len); + img_data, data_len); if (rc) { rc = MGMT_ERR_EINVAL; goto err_close; } - imgr_state.upload.off += len; + imgr_state.upload.off += data_len; if (imgr_state.upload.size == imgr_state.upload.off) { /* Done */ flash_area_close(imgr_state.upload.fa); @@ -445,24 +429,23 @@ imgr_upload(struct mgmt_jbuf *njb) } } out: - enc = &njb->mjb_enc; - - json_encode_object_start(enc); - - JSON_VALUE_INT(&jv, MGMT_ERR_EOK); - json_encode_object_entry(enc, "rc", &jv); - - JSON_VALUE_UINT(&jv, imgr_state.upload.off); - json_encode_object_entry(enc, "off", &jv); - - json_encode_object_finish(enc); - + { + CborError g_err = CborNoError; + CborEncoder *penc = &cb->encoder; + CborEncoder rsp; + g_err |= cbor_encoder_create_map(penc, &rsp, CborIndefiniteLength); + g_err |= cbor_encode_text_stringz(&rsp, "rc"); + g_err |= cbor_encode_int(&rsp, MGMT_ERR_EOK); + g_err |= cbor_encode_text_stringz(&rsp, "off"); + g_err |= cbor_encode_int(&rsp, imgr_state.upload.off); + g_err |= cbor_encoder_close_container(penc, &rsp); + } return 0; err_close: flash_area_close(imgr_state.upload.fa); imgr_state.upload.fa = NULL; err: - mgmt_jbuf_setoerr(njb, rc); + mgmt_cbuf_setoerr(cb, rc); return 0; } http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/7f299efb/mgmt/imgmgr/src/imgmgr_boot.c ---------------------------------------------------------------------- diff --git a/mgmt/imgmgr/src/imgmgr_boot.c b/mgmt/imgmgr/src/imgmgr_boot.c index d21b009..34c3bb2 100644 --- a/mgmt/imgmgr/src/imgmgr_boot.c +++ b/mgmt/imgmgr/src/imgmgr_boot.c @@ -27,40 +27,28 @@ #include <mgmt/mgmt.h> #include <bootutil/image.h> #include <bootutil/bootutil_misc.h> -#include <json/json.h> -#include <base64/base64.h> +#include <cborattr/cborattr.h> #include <hal/hal_bsp.h> #include "split/split.h" #include "imgmgr/imgmgr.h" #include "imgmgr_priv.h" -static void -imgr_hash_jsonstr(struct json_encoder *enc, char *key, uint8_t *hash) -{ - struct json_value jv; - char hash_str[IMGMGR_HASH_STR + 1]; - - base64_encode(hash, IMGMGR_HASH_LEN, hash_str, 1); - JSON_VALUE_STRING(&jv, hash_str); - json_encode_object_entry(enc, key, &jv); -} int -imgr_boot2_read(struct mgmt_jbuf *njb) +imgr_boot2_read(struct mgmt_cbuf *cb) { int rc; - struct json_encoder *enc; struct image_version ver; - struct json_value jv; uint8_t hash[IMGMGR_HASH_LEN]; int test_slot; int main_slot; int active_slot; + CborEncoder *penc = &cb->encoder; + CborError g_err = CborNoError; + CborEncoder rsp; - enc = &njb->mjb_enc; - - json_encode_object_start(enc); + g_err |= cbor_encoder_create_map(penc, &rsp, CborIndefiniteLength); test_slot = -1; main_slot = -1; @@ -82,60 +70,58 @@ imgr_boot2_read(struct mgmt_jbuf *njb) if (test_slot != -1) { rc = imgr_read_info(test_slot, &ver, hash, NULL); if (rc >= 0) { - imgr_hash_jsonstr(enc, "test", hash); + g_err |= cbor_encode_text_stringz(&rsp, "test"); + g_err |= cbor_encode_byte_string(&rsp, hash, IMGMGR_HASH_LEN); } } if (main_slot != -1) { rc = imgr_read_info(main_slot, &ver, hash, NULL); if (rc >= 0) { - imgr_hash_jsonstr(enc, "main", hash); + g_err |= cbor_encode_text_stringz(&rsp, "main"); + g_err |= cbor_encode_byte_string(&rsp, hash, IMGMGR_HASH_LEN); } } if (active_slot != -1) { rc = imgr_read_info(active_slot, &ver, hash, NULL); if (rc >= 0) { - imgr_hash_jsonstr(enc, "active", hash); + g_err |= cbor_encode_text_stringz(&rsp, "active"); + g_err |= cbor_encode_byte_string(&rsp, hash, IMGMGR_HASH_LEN); } } - JSON_VALUE_INT(&jv, MGMT_ERR_EOK); - json_encode_object_entry(enc, "rc", &jv); - - json_encode_object_finish(enc); + g_err |= cbor_encode_text_stringz(&rsp, "rc"); + g_err |= cbor_encode_int(&rsp, MGMT_ERR_EOK); + g_err |= cbor_encoder_close_container(penc, &rsp); return 0; } int -imgr_boot2_write(struct mgmt_jbuf *njb) +imgr_boot2_write(struct mgmt_cbuf *cb) { - char hash_str[IMGMGR_HASH_STR + 1]; + int rc; uint8_t hash[IMGMGR_HASH_LEN]; - const struct json_attr_t boot_write_attr[2] = { + const struct cbor_attr_t boot_write_attr[2] = { [0] = { .attribute = "test", - .type = t_string, - .addr.string = hash_str, - .len = sizeof(hash_str), + .type = CborAttrByteStringType, + .addr.bytestring.data = hash, + .len = sizeof(hash), }, [1] = { .attribute = NULL } }; - struct json_encoder *enc; - struct json_value jv; - int rc; struct image_version ver; - rc = json_read_object(&njb->mjb_buf, boot_write_attr); + rc = cbor_read_object(&cb->it, boot_write_attr); if (rc) { rc = MGMT_ERR_EINVAL; goto err; } - base64_decode(hash_str, hash); rc = imgr_find_by_hash(hash, &ver); if (rc >= 0) { rc = boot_vect_write_test(rc); @@ -149,19 +135,10 @@ imgr_boot2_write(struct mgmt_jbuf *njb) goto err; } - enc = &njb->mjb_enc; - - json_encode_object_start(enc); - - JSON_VALUE_INT(&jv, MGMT_ERR_EOK); - json_encode_object_entry(&njb->mjb_enc, "rc", &jv); - - json_encode_object_finish(enc); - - return 0; + rc = MGMT_ERR_EOK; err: - mgmt_jbuf_setoerr(njb, rc); + mgmt_cbuf_setoerr(cb, rc); return 0; } http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/7f299efb/mgmt/imgmgr/src/imgmgr_coredump.c ---------------------------------------------------------------------- diff --git a/mgmt/imgmgr/src/imgmgr_coredump.c b/mgmt/imgmgr/src/imgmgr_coredump.c index 72ae7c5..352171f 100644 --- a/mgmt/imgmgr/src/imgmgr_coredump.c +++ b/mgmt/imgmgr/src/imgmgr_coredump.c @@ -27,18 +27,16 @@ #include "flash_map/flash_map.h" #include "mgmt/mgmt.h" #include "coredump/coredump.h" -#include "base64/base64.h" +#include "cborattr/cborattr.h" #include "imgmgr/imgmgr.h" #include "imgmgr_priv.h" int -imgr_core_list(struct mgmt_jbuf *njb) +imgr_core_list(struct mgmt_cbuf *cb) { const struct flash_area *fa; struct coredump_header hdr; - struct json_encoder *enc; - struct json_value jv; int rc; rc = flash_area_open(MYNEWT_VAL(COREDUMP_FLASH_AREA), &fa); @@ -55,24 +53,18 @@ imgr_core_list(struct mgmt_jbuf *njb) } } - enc = &njb->mjb_enc; - - json_encode_object_start(enc); - JSON_VALUE_INT(&jv, rc); - json_encode_object_entry(enc, "rc", &jv); - json_encode_object_finish(enc); - + mgmt_cbuf_setoerr(cb, rc); return 0; } int -imgr_core_load(struct mgmt_jbuf *njb) +imgr_core_load(struct mgmt_cbuf *cb) { unsigned long long off = UINT_MAX; - const struct json_attr_t dload_attr[2] = { + const struct cbor_attr_t dload_attr[2] = { [0] = { .attribute = "off", - .type = t_uinteger, + .type = CborAttrUnsignedIntegerType, .addr.uinteger = &off }, [1] = { 0 }, @@ -80,15 +72,11 @@ imgr_core_load(struct mgmt_jbuf *njb) int rc; int sz; const struct flash_area *fa; - char data[IMGMGR_NMGR_MAX_MSG]; - char encoded[BASE64_ENCODE_SIZE(IMGMGR_NMGR_MAX_MSG)]; + uint8_t data[IMGMGR_NMGR_MAX_MSG]; struct coredump_header *hdr; - struct json_encoder *enc; - struct json_value jv; - hdr = (struct coredump_header *)data; - rc = json_read_object(&njb->mjb_buf, dload_attr); + rc = cbor_read_object(&cb->it, dload_attr); if (rc || off == UINT_MAX) { rc = MGMT_ERR_EINVAL; goto err; @@ -123,20 +111,17 @@ imgr_core_load(struct mgmt_jbuf *njb) goto err_close; } - sz = base64_encode(data, sz, encoded, 1); - - enc = &njb->mjb_enc; - - json_encode_object_start(enc); - JSON_VALUE_INT(&jv, 0); - json_encode_object_entry(enc, "rc", &jv); - - JSON_VALUE_INT(&jv, off); - json_encode_object_entry(enc, "off", &jv); - - JSON_VALUE_STRINGN(&jv, encoded, sz); - json_encode_object_entry(enc, "data", &jv); - json_encode_object_finish(enc); + CborError g_err = CborNoError; + CborEncoder *penc = &cb->encoder; + CborEncoder rsp; + g_err |= cbor_encoder_create_map(penc, &rsp, CborIndefiniteLength); + g_err |= cbor_encode_text_stringz(&rsp, "rc"); + g_err |= cbor_encode_int(&rsp, MGMT_ERR_EOK); + g_err |= cbor_encode_text_stringz(&rsp, "off"); + g_err |= cbor_encode_int(&rsp, off); + g_err |= cbor_encode_text_stringz(&rsp, "data"); + g_err |= cbor_encode_byte_string(&rsp, data, sz); + g_err |= cbor_encoder_close_container(penc, &rsp); flash_area_close(fa); return 0; @@ -144,7 +129,7 @@ imgr_core_load(struct mgmt_jbuf *njb) err_close: flash_area_close(fa); err: - mgmt_jbuf_setoerr(njb, rc); + mgmt_cbuf_setoerr(cb, rc); return 0; } @@ -152,7 +137,7 @@ err: * Erase the area if it has a coredump, or the header is empty. */ int -imgr_core_erase(struct mgmt_jbuf *njb) +imgr_core_erase(struct mgmt_cbuf *cb) { struct coredump_header hdr; const struct flash_area *fa; @@ -176,7 +161,7 @@ imgr_core_erase(struct mgmt_jbuf *njb) flash_area_close(fa); err: - mgmt_jbuf_setoerr(njb, rc); + mgmt_cbuf_setoerr(cb, rc); return 0; } http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/7f299efb/mgmt/imgmgr/src/imgmgr_fs.c ---------------------------------------------------------------------- diff --git a/mgmt/imgmgr/src/imgmgr_fs.c b/mgmt/imgmgr/src/imgmgr_fs.c index f7d603b..eadbfb5 100644 --- a/mgmt/imgmgr/src/imgmgr_fs.c +++ b/mgmt/imgmgr/src/imgmgr_fs.c @@ -31,28 +31,28 @@ #include "newtmgr/newtmgr.h" #include "bootutil/image.h" #include "fs/fs.h" -#include "json/json.h" -#include "base64/base64.h" +#include "cborattr/cborattr.h" #include "bsp/bsp.h" +#include "mgmt/mgmt.h" #include "imgmgr/imgmgr.h" #include "imgmgr_priv.h" int -imgr_file_download(struct nmgr_jbuf *njb) +imgr_file_download(struct mgmt_cbuf *cb) { long long unsigned int off = UINT_MAX; char tmp_str[IMGMGR_NMGR_MAX_NAME + 1]; - char img_data[BASE64_ENCODE_SIZE(IMGMGR_NMGR_MAX_MSG)]; - const struct json_attr_t dload_attr[3] = { + uint8_t img_data[IMGMGR_NMGR_MAX_MSG]; + const struct cbor_attr_t dload_attr[3] = { [0] = { .attribute = "off", - .type = t_uinteger, + .type = CborAttrUnsignedIntegerType, .addr.uinteger = &off }, [1] = { .attribute = "name", - .type = t_string, + .type = CborAttrTextStringType, .addr.string = tmp_str, .len = sizeof(tmp_str) }, @@ -61,115 +61,106 @@ imgr_file_download(struct nmgr_jbuf *njb) int rc; uint32_t out_len; struct fs_file *file; - struct json_encoder *enc; - struct json_value jv; + CborError g_err = CborNoError; + CborEncoder *penc = &cb->encoder; + CborEncoder rsp; - rc = json_read_object(&njb->njb_buf, dload_attr); + rc = cbor_read_object(&cb->it, dload_attr); if (rc || off == UINT_MAX) { - rc = NMGR_ERR_EINVAL; + rc = MGMT_ERR_EINVAL; goto err; } rc = fs_open(tmp_str, FS_ACCESS_READ, &file); if (rc || !file) { - rc = NMGR_ERR_ENOMEM; + rc = MGMT_ERR_ENOMEM; goto err; } rc = fs_seek(file, off); if (rc) { - rc = NMGR_ERR_EUNKNOWN; + rc = MGMT_ERR_EUNKNOWN; goto err_close; } - rc = fs_read(file, 32, tmp_str, &out_len); + rc = fs_read(file, 32, img_data, &out_len); if (rc) { - rc = NMGR_ERR_EUNKNOWN; + rc = MGMT_ERR_EUNKNOWN; goto err_close; } - out_len = base64_encode(tmp_str, out_len, img_data, 1); + g_err |= cbor_encoder_create_map(penc, &rsp, CborIndefiniteLength); - enc = &njb->njb_enc; + g_err |= cbor_encode_text_stringz(&rsp, "off"); + g_err |= cbor_encode_uint(&rsp, off); - json_encode_object_start(enc); + g_err |= cbor_encode_text_stringz(&rsp, "data"); + g_err |= cbor_encode_byte_string(&rsp, img_data, out_len); - JSON_VALUE_UINT(&jv, off); - json_encode_object_entry(enc, "off", &jv); - JSON_VALUE_STRINGN(&jv, img_data, out_len); - json_encode_object_entry(enc, "data", &jv); + g_err |= cbor_encode_text_stringz(&rsp, "rc"); + g_err |= cbor_encode_int(&rsp, MGMT_ERR_EOK); if (off == 0) { rc = fs_filelen(file, &out_len); - JSON_VALUE_UINT(&jv, out_len); - json_encode_object_entry(enc, "len", &jv); + g_err |= cbor_encode_text_stringz(&rsp, "len"); + g_err |= cbor_encode_uint(&rsp, out_len); } - fs_close(file); - - JSON_VALUE_INT(&jv, NMGR_ERR_EOK); - json_encode_object_entry(&njb->njb_enc, "rc", &jv); - - json_encode_object_finish(enc); + g_err |= cbor_encoder_close_container(penc, &rsp); + fs_close(file); return 0; err_close: fs_close(file); err: - nmgr_jbuf_setoerr(njb, rc); + mgmt_cbuf_setoerr(cb, rc); return 0; } int -imgr_file_upload(struct nmgr_jbuf *njb) +imgr_file_upload(struct mgmt_cbuf *cb) { - char img_data[BASE64_ENCODE_SIZE(IMGMGR_NMGR_MAX_MSG)]; + uint8_t img_data[IMGMGR_NMGR_MAX_MSG]; char file_name[IMGMGR_NMGR_MAX_NAME + 1]; + size_t img_len; long long unsigned int off = UINT_MAX; long long unsigned int size = UINT_MAX; - const struct json_attr_t off_attr[5] = { + const struct cbor_attr_t off_attr[5] = { [0] = { .attribute = "off", - .type = t_uinteger, + .type = CborAttrUnsignedIntegerType, .addr.uinteger = &off, .nodefault = true }, [1] = { .attribute = "data", - .type = t_string, - .addr.string = img_data, + .type = CborAttrByteStringType, + .addr.bytestring.data = img_data, + .addr.bytestring.len = &img_len, .len = sizeof(img_data) }, [2] = { .attribute = "len", - .type = t_uinteger, + .type = CborAttrUnsignedIntegerType, .addr.uinteger = &size, .nodefault = true }, [3] = { .attribute = "name", - .type = t_string, + .type = CborAttrTextStringType, .addr.string = file_name, .len = sizeof(file_name) }, [4] = { 0 }, }; - struct json_encoder *enc; - struct json_value jv; + CborError g_err = CborNoError; + CborEncoder *penc = &cb->encoder; + CborEncoder rsp; int rc; - int len; - rc = json_read_object(&njb->njb_buf, off_attr); + rc = cbor_read_object(&cb->it, off_attr); if (rc || off == UINT_MAX) { - rc = NMGR_ERR_EINVAL; + rc = MGMT_ERR_EINVAL; goto err; } - len = strlen(img_data); - if (len) { - len = base64_decode(img_data, img_data); - if (len < 0) { - rc = NMGR_ERR_EINVAL; - goto err; - } - } if (off == 0) { /* @@ -179,7 +170,7 @@ imgr_file_upload(struct nmgr_jbuf *njb) imgr_state.upload.size = size; if (!strlen(file_name)) { - rc = NMGR_ERR_EINVAL; + rc = MGMT_ERR_EINVAL; goto err; } if (imgr_state.upload.file) { @@ -189,7 +180,7 @@ imgr_file_upload(struct nmgr_jbuf *njb) rc = fs_open(file_name, FS_ACCESS_WRITE | FS_ACCESS_TRUNCATE, &imgr_state.upload.file); if (rc) { - rc = NMGR_ERR_EINVAL; + rc = MGMT_ERR_EINVAL; goto err; } } else if (off != imgr_state.upload.off) { @@ -201,16 +192,16 @@ imgr_file_upload(struct nmgr_jbuf *njb) } if (!imgr_state.upload.file) { - rc = NMGR_ERR_EINVAL; + rc = MGMT_ERR_EINVAL; goto err; } - if (len) { - rc = fs_write(imgr_state.upload.file, img_data, len); + if (img_len) { + rc = fs_write(imgr_state.upload.file, img_data, img_len); if (rc) { - rc = NMGR_ERR_EINVAL; + rc = MGMT_ERR_EINVAL; goto err_close; } - imgr_state.upload.off += len; + imgr_state.upload.off += img_len; if (imgr_state.upload.size == imgr_state.upload.off) { /* Done */ fs_close(imgr_state.upload.file); @@ -218,25 +209,19 @@ imgr_file_upload(struct nmgr_jbuf *njb) } } out: - enc = &njb->njb_enc; - - json_encode_object_start(enc); - - JSON_VALUE_INT(&jv, NMGR_ERR_EOK); - json_encode_object_entry(&njb->njb_enc, "rc", &jv); - - JSON_VALUE_UINT(&jv, imgr_state.upload.off); - json_encode_object_entry(enc, "off", &jv); - - json_encode_object_finish(enc); - + g_err |= cbor_encoder_create_map(penc, &rsp, CborIndefiniteLength); + g_err |= cbor_encode_text_stringz(&rsp, "rc"); + g_err |= cbor_encode_int(&rsp, MGMT_ERR_EOK); + g_err |= cbor_encode_text_stringz(&rsp, "off"); + g_err |= cbor_encode_uint(&rsp, imgr_state.upload.off); + g_err |= cbor_encoder_close_container(penc, &rsp); return 0; err_close: fs_close(imgr_state.upload.file); imgr_state.upload.file = NULL; err: - nmgr_jbuf_setoerr(njb, rc); + mgmt_cbuf_setoerr(cb, rc); return 0; } #endif http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/7f299efb/mgmt/imgmgr/src/imgmgr_priv.h ---------------------------------------------------------------------- diff --git a/mgmt/imgmgr/src/imgmgr_priv.h b/mgmt/imgmgr/src/imgmgr_priv.h index 6642bc6..1c31ad5 100644 --- a/mgmt/imgmgr/src/imgmgr_priv.h +++ b/mgmt/imgmgr/src/imgmgr_priv.h @@ -87,6 +87,7 @@ struct mgmt_jbuf; struct nmgr_hdr; struct os_mbuf; struct fs_file; +struct mgmt_cbuf; struct imgr_state { struct { @@ -102,17 +103,18 @@ struct imgr_state { extern struct imgr_state imgr_state; struct nmgr_jbuf; -int imgr_boot2_read(struct mgmt_jbuf *); -int imgr_boot2_write(struct mgmt_jbuf *); -int imgr_file_upload(struct mgmt_jbuf *); -int imgr_file_download(struct mgmt_jbuf *); -int imgr_core_list(struct mgmt_jbuf *); -int imgr_core_load(struct mgmt_jbuf *); -int imgr_core_erase(struct mgmt_jbuf *); -int imgr_splitapp_read(struct mgmt_jbuf *); -int imgr_splitapp_write(struct mgmt_jbuf *); -int imgmgr_state_read(struct mgmt_jbuf *njb); -int imgmgr_state_write(struct mgmt_jbuf *njb); + +int imgr_boot2_read(struct mgmt_cbuf *); +int imgr_boot2_write(struct mgmt_cbuf *); +int imgr_file_upload(struct mgmt_cbuf *); +int imgr_file_download(struct mgmt_cbuf *); +int imgr_core_list(struct mgmt_cbuf *); +int imgr_core_load(struct mgmt_cbuf *); +int imgr_core_erase(struct mgmt_cbuf *); +int imgr_splitapp_read(struct mgmt_cbuf *); +int imgr_splitapp_write(struct mgmt_cbuf *); +int imgmgr_state_read(struct mgmt_cbuf *cb); +int imgmgr_state_write(struct mgmt_cbuf *njb); 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); http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/7f299efb/mgmt/imgmgr/src/imgmgr_state.c ---------------------------------------------------------------------- diff --git a/mgmt/imgmgr/src/imgmgr_state.c b/mgmt/imgmgr/src/imgmgr_state.c index ca8a46a..ac62d71 100644 --- a/mgmt/imgmgr/src/imgmgr_state.c +++ b/mgmt/imgmgr/src/imgmgr_state.c @@ -21,7 +21,8 @@ #include "bootutil/image.h" #include "bootutil/bootutil_misc.h" -#include "base64/base64.h" +#include "cborattr/cborattr.h" +#include "tinycbor/cbor.h" #include "split/split.h" #include "mgmt/mgmt.h" #include "imgmgr/imgmgr.h" @@ -195,29 +196,27 @@ imgmgr_state_confirm(void) } int -imgmgr_state_read(struct mgmt_jbuf *njb) +imgmgr_state_read(struct mgmt_cbuf *cb) { - struct json_encoder *enc; int i; int rc; uint32_t flags; struct image_version ver; uint8_t hash[IMGMGR_HASH_LEN]; /* SHA256 hash */ - struct json_value jv; char vers_str[IMGMGR_NMGR_MAX_VER]; - char hash_str[IMGMGR_HASH_STR + 1]; - int ver_len; int any_non_bootable; int split_status; uint8_t state_flags; + CborError g_err = CborNoError; + CborEncoder *penc = &cb->encoder; + CborEncoder rsp, images, image; any_non_bootable = 0; - enc = &njb->mjb_enc; - json_encode_object_start(enc); + g_err |= cbor_encoder_create_map(penc, &rsp, CborIndefiniteLength); + g_err |= cbor_encode_text_stringz(&rsp, "images"); - json_encode_array_name(enc, "images"); - json_encode_array_start(enc); + g_err |= cbor_encoder_create_array(&rsp, &images, CborIndefiniteLength); for (i = 0; i < 2; i++) { rc = imgr_read_info(i, &ver, hash, &flags); if (rc != 0) { @@ -230,92 +229,90 @@ imgmgr_state_read(struct mgmt_jbuf *njb) state_flags = imgmgr_state_flags(i); - json_encode_object_start(enc); + g_err |= cbor_encoder_create_map(&images, &image, CborIndefiniteLength); + g_err |= cbor_encode_text_stringz(&image, "slot"); + g_err |= cbor_encode_int(&rsp, i); - JSON_VALUE_INT(&jv, i); - json_encode_object_entry(enc, "slot", &jv); + g_err |= cbor_encode_text_stringz(&image, "version"); + g_err |= cbor_encode_text_stringz(&image, vers_str); - ver_len = imgr_ver_str(&ver, vers_str); - JSON_VALUE_STRINGN(&jv, vers_str, ver_len); - json_encode_object_entry(enc, "version", &jv); + g_err |= cbor_encode_text_stringz(&image, "hash"); + g_err |= cbor_encode_byte_string(&image, hash, IMGMGR_HASH_LEN); - base64_encode(hash, IMGMGR_HASH_LEN, hash_str, 1); - JSON_VALUE_STRING(&jv, hash_str); - json_encode_object_entry(enc, "hash", &jv); + g_err |= cbor_encode_text_stringz(&image, "bootable"); + g_err |= cbor_encode_boolean(&image, !(flags & IMAGE_F_NON_BOOTABLE)); - JSON_VALUE_BOOL(&jv, !(flags & IMAGE_F_NON_BOOTABLE)); - json_encode_object_entry(enc, "bootable", &jv); + g_err |= cbor_encode_text_stringz(&image, "pending"); + g_err |= cbor_encode_boolean(&image, state_flags & IMGMGR_STATE_F_PENDING); - JSON_VALUE_BOOL(&jv, state_flags & IMGMGR_STATE_F_PENDING); - json_encode_object_entry(enc, "pending", &jv); + g_err |= cbor_encode_text_stringz(&image, "confirmed"); + g_err |= cbor_encode_boolean(&image, state_flags & IMGMGR_STATE_F_CONFIRMED); - JSON_VALUE_BOOL(&jv, state_flags & IMGMGR_STATE_F_CONFIRMED); - json_encode_object_entry(enc, "confirmed", &jv); + g_err |= cbor_encode_text_stringz(&image, "active"); + g_err |= cbor_encode_boolean(&image, state_flags & IMGMGR_STATE_F_ACTIVE); - JSON_VALUE_BOOL(&jv, state_flags & IMGMGR_STATE_F_ACTIVE); - json_encode_object_entry(enc, "active", &jv); - - json_encode_object_finish(enc); + g_err |= cbor_encoder_close_container(&images, &image); } - json_encode_array_finish(enc); + + g_err |= cbor_encoder_close_container(&rsp, &images); if (any_non_bootable) { split_status = split_check_status(); } else { split_status = SPLIT_STATUS_INVALID; } - JSON_VALUE_INT(&jv, split_status); - json_encode_object_entry(enc, "splitStatus", &jv); - json_encode_object_finish(enc); + g_err |= cbor_encode_text_stringz(&image, "splitStatus"); + g_err |= cbor_encode_int(&rsp, split_status); + + g_err |= cbor_encoder_close_container(penc, &rsp); return 0; } int -imgmgr_state_write(struct mgmt_jbuf *njb) +imgmgr_state_write(struct mgmt_cbuf *cb) { - char hash_str[IMGMGR_HASH_STR + 1]; uint8_t hash[IMGMGR_HASH_LEN]; + size_t hash_len = 0; bool confirm; int slot; int rc; - const struct json_attr_t write_attr[] = { + const struct cbor_attr_t write_attr[] = { [0] = { .attribute = "hash", - .type = t_string, - .addr.string = hash_str, - .len = sizeof(hash_str), + .type = CborAttrByteStringType, + .addr.bytestring.data = hash, + .addr.bytestring.len = &hash_len, + .len = sizeof(hash), }, [1] = { .attribute = "confirm", - .type = t_boolean, + .type = CborAttrBooleanType, .addr.boolean = &confirm, .dflt.boolean = false, }, [2] = { 0 }, }; - rc = json_read_object(&njb->mjb_buf, write_attr); + rc = cbor_read_object(&cb->it, write_attr); if (rc != 0) { rc = MGMT_ERR_EINVAL; goto err; } /* Validate arguments. */ - if (hash_str[0] == '\0' && !confirm) { + if ((hash_len == 0) && !confirm) { rc = MGMT_ERR_EINVAL; goto err; } - if (hash_str[0] != '\0' && confirm) { + if ((hash_len != 0) && confirm) { rc = MGMT_ERR_EINVAL; goto err; } if (!confirm) { - /* Test image with specified hash. */ - base64_decode(hash_str, hash); slot = imgr_find_by_hash(hash, NULL); if (slot < 0) { rc = MGMT_ERR_EINVAL; @@ -335,7 +332,7 @@ imgmgr_state_write(struct mgmt_jbuf *njb) } /* Send the current image state in the response. */ - rc = imgmgr_state_read(njb); + rc = imgmgr_state_read(cb); if (rc != 0) { goto err; } @@ -343,7 +340,7 @@ imgmgr_state_write(struct mgmt_jbuf *njb) return 0; err: - mgmt_jbuf_setoerr(njb, rc); + mgmt_cbuf_setoerr(cb, rc); return 0; } http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/7f299efb/mgmt/mgmt/include/mgmt/mgmt.h ---------------------------------------------------------------------- diff --git a/mgmt/mgmt/include/mgmt/mgmt.h b/mgmt/mgmt/include/mgmt/mgmt.h index f87d57b..cd5c4f5 100644 --- a/mgmt/mgmt/include/mgmt/mgmt.h +++ b/mgmt/mgmt/include/mgmt/mgmt.h @@ -21,7 +21,6 @@ #define _MGMT_MGMT_H_ #include <inttypes.h> -#include <json/json.h> #include <os/queue.h> @@ -51,7 +50,7 @@ extern "C" { #define MGMT_GROUP_ID_PERUSER (64) /** - * Newtmgr JSON error codes + * Newtmgr error codes */ #define MGMT_ERR_EOK (0) #define MGMT_ERR_EUNKNOWN (1) @@ -61,13 +60,9 @@ extern "C" { #define MGMT_ERR_ENOENT (5) #define MGMT_ERR_EPERUSER (256) -struct mgmt_jbuf { - /* json_buffer must be first element in the structure */ - struct json_buffer mjb_buf; /* for input */ - struct json_encoder mjb_enc; /* for output */ -}; +struct mgmt_cbuf; -typedef int (*mgmt_handler_func_t)(struct mgmt_jbuf *); +typedef int (*mgmt_handler_func_t)(struct mgmt_cbuf *); struct mgmt_handler { mgmt_handler_func_t mh_read; @@ -87,7 +82,7 @@ struct mgmt_group { sizeof(struct mgmt_handler)); int mgmt_group_register(struct mgmt_group *group); -void mgmt_jbuf_setoerr(struct mgmt_jbuf *njb, int errcode); +void mgmt_cbuf_setoerr(struct mgmt_cbuf *njb, int errcode); const struct mgmt_handler *mgmt_find_handler(uint16_t group_id, uint16_t handler_id); http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/7f299efb/mgmt/mgmt/src/mgmt.c ---------------------------------------------------------------------- diff --git a/mgmt/mgmt/src/mgmt.c b/mgmt/mgmt/src/mgmt.c index be77279..59c4fe4 100644 --- a/mgmt/mgmt/src/mgmt.c +++ b/mgmt/mgmt/src/mgmt.c @@ -19,7 +19,7 @@ #include <string.h> #include <os/os.h> - +#include <tinycbor/cbor.h> #include "mgmt/mgmt.h" static struct os_mutex mgmt_group_lock; @@ -139,14 +139,15 @@ err: } void -mgmt_jbuf_setoerr(struct mgmt_jbuf *njb, int errcode) +mgmt_cbuf_setoerr(struct mgmt_cbuf *cb, int errcode) { - struct json_value jv; - - json_encode_object_start(&njb->mjb_enc); - JSON_VALUE_INT(&jv, errcode); - json_encode_object_entry(&njb->mjb_enc, "rc", &jv); - json_encode_object_finish(&njb->mjb_enc); + CborEncoder *penc = &cb->encoder; + CborError g_err = CborNoError; + CborEncoder rsp; + g_err |= cbor_encoder_create_map(penc, &rsp, CborIndefiniteLength); + g_err |= cbor_encode_text_stringz(&rsp, "rc"); + g_err |= cbor_encode_int(&rsp, errcode); + g_err |= cbor_encoder_close_container(penc, &rsp); } void http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/7f299efb/mgmt/newtmgr/include/newtmgr/newtmgr.h ---------------------------------------------------------------------- diff --git a/mgmt/newtmgr/include/newtmgr/newtmgr.h b/mgmt/newtmgr/include/newtmgr/newtmgr.h index 8d6aaee..170486f 100644 --- a/mgmt/newtmgr/include/newtmgr/newtmgr.h +++ b/mgmt/newtmgr/include/newtmgr/newtmgr.h @@ -20,7 +20,7 @@ #ifndef _NEWTMGR_H_ #define _NEWTMGR_H_ -#include <json/json.h> +#include <tinycbor/cbor.h> #include <inttypes.h> #include <os/os.h> @@ -45,7 +45,7 @@ extern "C" { #define NMGR_OP_WRITE (2) #define NMGR_OP_WRITE_RSP (3) -#define NMGR_F_JSON_RSP_COMPLETE (0x0001) +#define NMGR_F_CBOR_RSP_COMPLETE (0x0001) struct nmgr_hdr { uint8_t nh_op; /* NMGR_OP_XXX */ http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/7f299efb/mgmt/newtmgr/nmgr_os/src/newtmgr_os.c ---------------------------------------------------------------------- diff --git a/mgmt/newtmgr/nmgr_os/src/newtmgr_os.c b/mgmt/newtmgr/nmgr_os/src/newtmgr_os.c index 4e772af..9fbc294 100644 --- a/mgmt/newtmgr/nmgr_os/src/newtmgr_os.c +++ b/mgmt/newtmgr/nmgr_os/src/newtmgr_os.c @@ -33,15 +33,18 @@ #include "nmgr_os/nmgr_os.h" +#include <tinycbor/cbor.h> +#include <cborattr/cborattr.h> + static struct os_callout_func nmgr_reset_callout; -static int nmgr_def_echo(struct mgmt_jbuf *); -static int nmgr_def_console_echo(struct mgmt_jbuf *); -static int nmgr_def_taskstat_read(struct mgmt_jbuf *njb); -static int nmgr_def_mpstat_read(struct mgmt_jbuf *njb); -static int nmgr_datetime_get(struct mgmt_jbuf *njb); -static int nmgr_datetime_set(struct mgmt_jbuf *njb); -static int nmgr_reset(struct mgmt_jbuf *njb); +static int nmgr_def_echo(struct mgmt_cbuf *); +static int nmgr_def_console_echo(struct mgmt_cbuf *); +static int nmgr_def_taskstat_read(struct mgmt_cbuf *njb); +static int nmgr_def_mpstat_read(struct mgmt_cbuf *njb); +static int nmgr_datetime_get(struct mgmt_cbuf *njb); +static int nmgr_datetime_set(struct mgmt_cbuf *njb); +static int nmgr_reset(struct mgmt_cbuf *njb); static const struct mgmt_handler nmgr_def_group_handlers[] = { [NMGR_ID_ECHO] = { @@ -74,41 +77,44 @@ static struct mgmt_group nmgr_def_group = { }; static int -nmgr_def_echo(struct mgmt_jbuf *njb) +nmgr_def_echo(struct mgmt_cbuf *cb) { - uint8_t echo_buf[128]; - struct json_attr_t attrs[] = { - { "d", t_string, .addr.string = (char *) &echo_buf[0], - .len = sizeof(echo_buf) }, - { NULL }, - }; - struct json_value jv; - int rc; + char echo_buf[128] = {'\0'}; + CborEncoder *penc = &cb->encoder; + CborError g_err = CborNoError; + CborEncoder rsp; - rc = json_read_object((struct json_buffer *) njb, attrs); - if (rc != 0) { - goto err; - } + struct cbor_attr_t attrs[3] = { + [0] = { + .attribute = "d", + .type = CborAttrTextStringType, + .addr.string = echo_buf, + .nodefault = 1, + .len = sizeof(echo_buf), + }, + [1] = { + .attribute = NULL + } + }; - json_encode_object_start(&njb->mjb_enc); - JSON_VALUE_STRINGN(&jv, (char *) echo_buf, strlen((char *) echo_buf)); - json_encode_object_entry(&njb->mjb_enc, "r", &jv); - json_encode_object_finish(&njb->mjb_enc); + g_err |= cbor_encoder_create_array(penc, &rsp, CborIndefiniteLength); + g_err |= cbor_encode_text_stringz(&rsp, "r"); + g_err |= cbor_read_object(&cb->it, attrs); + g_err |= cbor_encode_text_string(&rsp, echo_buf, strlen(echo_buf)); + g_err |= cbor_encoder_close_container(penc, &rsp); - return (0); -err: - return (rc); + return (g_err); } static int -nmgr_def_console_echo(struct mgmt_jbuf *njb) +nmgr_def_console_echo(struct mgmt_cbuf *cb) { long long int echo_on = 1; int rc; - struct json_attr_t attrs[3] = { + struct cbor_attr_t attrs[3] = { [0] = { .attribute = "echo", - .type = t_integer, + .type = CborAttrTextStringType, .addr.integer = &echo_on, .nodefault = 1 }, @@ -118,7 +124,7 @@ nmgr_def_console_echo(struct mgmt_jbuf *njb) [2] = { 0 }, }; - rc = json_read_object(&njb->mjb_buf, attrs); + rc = cbor_read_object(&cb->it, attrs); if (rc) { return OS_EINVAL; } @@ -132,68 +138,69 @@ nmgr_def_console_echo(struct mgmt_jbuf *njb) } static int -nmgr_def_taskstat_read(struct mgmt_jbuf *njb) +nmgr_def_taskstat_read(struct mgmt_cbuf *cb) { struct os_task *prev_task; struct os_task_info oti; - struct json_value jv; - json_encode_object_start(&njb->mjb_enc); - JSON_VALUE_INT(&jv, MGMT_ERR_EOK); - json_encode_object_entry(&njb->mjb_enc, "rc", &jv); + CborError g_err = CborNoError; + CborEncoder rsp, tasks, task; - json_encode_object_key(&njb->mjb_enc, "tasks"); - json_encode_object_start(&njb->mjb_enc); + g_err |= cbor_encoder_create_map(&cb->encoder, &rsp, CborIndefiniteLength); + g_err |= cbor_encode_text_stringz(&rsp, "rc"); + g_err |= cbor_encode_int(&rsp, MGMT_ERR_EOK); + g_err |= cbor_encode_text_stringz(&rsp, "tasks"); + g_err |= cbor_encoder_create_map(&rsp, &tasks, CborIndefiniteLength); prev_task = NULL; while (1) { + prev_task = os_task_info_get_next(prev_task, &oti); if (prev_task == NULL) { break; } - json_encode_object_key(&njb->mjb_enc, oti.oti_name); - - json_encode_object_start(&njb->mjb_enc); - JSON_VALUE_UINT(&jv, oti.oti_prio); - json_encode_object_entry(&njb->mjb_enc, "prio", &jv); - JSON_VALUE_UINT(&jv, oti.oti_taskid); - json_encode_object_entry(&njb->mjb_enc, "tid", &jv); - JSON_VALUE_UINT(&jv, oti.oti_state); - json_encode_object_entry(&njb->mjb_enc, "state", &jv); - JSON_VALUE_UINT(&jv, oti.oti_stkusage); - json_encode_object_entry(&njb->mjb_enc, "stkuse", &jv); - JSON_VALUE_UINT(&jv, oti.oti_stksize); - json_encode_object_entry(&njb->mjb_enc, "stksiz", &jv); - JSON_VALUE_UINT(&jv, oti.oti_cswcnt); - json_encode_object_entry(&njb->mjb_enc, "cswcnt", &jv); - JSON_VALUE_UINT(&jv, oti.oti_runtime); - json_encode_object_entry(&njb->mjb_enc, "runtime", &jv); - JSON_VALUE_UINT(&jv, oti.oti_last_checkin); - json_encode_object_entry(&njb->mjb_enc, "last_checkin", &jv); - JSON_VALUE_UINT(&jv, oti.oti_next_checkin); - json_encode_object_entry(&njb->mjb_enc, "next_checkin", &jv); - json_encode_object_finish(&njb->mjb_enc); + g_err |= cbor_encode_text_stringz(&tasks, oti.oti_name); + g_err |= cbor_encoder_create_map(&tasks, &task, CborIndefiniteLength); + g_err |= cbor_encode_text_stringz(&rsp, "prio"); + g_err |= cbor_encode_uint(&rsp, oti.oti_prio); + g_err |= cbor_encode_text_stringz(&rsp, "tid"); + g_err |= cbor_encode_uint(&rsp, oti.oti_taskid); + g_err |= cbor_encode_text_stringz(&rsp, "state"); + g_err |= cbor_encode_uint(&rsp, oti.oti_state); + g_err |= cbor_encode_text_stringz(&rsp, "stkuse"); + g_err |= cbor_encode_uint(&rsp, oti.oti_stkusage); + g_err |= cbor_encode_text_stringz(&rsp, "stksiz"); + g_err |= cbor_encode_uint(&rsp, oti.oti_stksize); + g_err |= cbor_encode_text_stringz(&rsp, "cswcnt"); + g_err |= cbor_encode_uint(&rsp, oti.oti_cswcnt); + g_err |= cbor_encode_text_stringz(&rsp, "runtime"); + g_err |= cbor_encode_uint(&rsp, oti.oti_runtime); + g_err |= cbor_encode_text_stringz(&rsp, "last_checkin"); + g_err |= cbor_encode_uint(&rsp, oti.oti_last_checkin); + g_err |= cbor_encode_text_stringz(&rsp, "next_checkin"); + g_err |= cbor_encode_uint(&rsp, oti.oti_next_checkin); + g_err |= cbor_encoder_close_container(&tasks, &task); } - json_encode_object_finish(&njb->mjb_enc); - json_encode_object_finish(&njb->mjb_enc); + g_err |= cbor_encoder_close_container(&rsp, &tasks); + g_err |= cbor_encoder_close_container(&cb->encoder, &rsp); return (0); } static int -nmgr_def_mpstat_read(struct mgmt_jbuf *njb) +nmgr_def_mpstat_read(struct mgmt_cbuf *cb) { struct os_mempool *prev_mp; struct os_mempool_info omi; - struct json_value jv; - - json_encode_object_start(&njb->mjb_enc); - JSON_VALUE_INT(&jv, MGMT_ERR_EOK); - json_encode_object_entry(&njb->mjb_enc, "rc", &jv); + CborError g_err = CborNoError; + CborEncoder rsp, pools, pool; - json_encode_object_key(&njb->mjb_enc, "mpools"); - json_encode_object_start(&njb->mjb_enc); + g_err |= cbor_encoder_create_map(&cb->encoder, &rsp, CborIndefiniteLength); + g_err |= cbor_encode_text_stringz(&rsp, "rc"); + g_err |= cbor_encode_int(&rsp, MGMT_ERR_EOK); + g_err |= cbor_encode_text_stringz(&rsp, "mpools"); + g_err |= cbor_encoder_create_map(&rsp, &pools, CborIndefiniteLength); prev_mp = NULL; while (1) { @@ -202,36 +209,36 @@ nmgr_def_mpstat_read(struct mgmt_jbuf *njb) break; } - json_encode_object_key(&njb->mjb_enc, omi.omi_name); - - json_encode_object_start(&njb->mjb_enc); - JSON_VALUE_UINT(&jv, omi.omi_block_size); - json_encode_object_entry(&njb->mjb_enc, "blksiz", &jv); - JSON_VALUE_UINT(&jv, omi.omi_num_blocks); - json_encode_object_entry(&njb->mjb_enc, "nblks", &jv); - JSON_VALUE_UINT(&jv, omi.omi_num_free); - json_encode_object_entry(&njb->mjb_enc, "nfree", &jv); - json_encode_object_finish(&njb->mjb_enc); + g_err |= cbor_encode_text_stringz(&pools, omi.omi_name); + g_err |= cbor_encoder_create_map(&pools, &pool, CborIndefiniteLength); + g_err |= cbor_encode_text_stringz(&rsp, "blksiz"); + g_err |= cbor_encode_uint(&rsp, omi.omi_block_size); + g_err |= cbor_encode_text_stringz(&rsp, "nblks"); + g_err |= cbor_encode_uint(&rsp, omi.omi_num_blocks); + g_err |= cbor_encode_text_stringz(&rsp, "nfree"); + g_err |= cbor_encode_uint(&rsp, omi.omi_num_free); + g_err |= cbor_encoder_close_container(&pools, &pool); } - json_encode_object_finish(&njb->mjb_enc); - json_encode_object_finish(&njb->mjb_enc); + g_err |= cbor_encoder_close_container(&rsp, &pools); + g_err |= cbor_encoder_close_container(&cb->encoder, &rsp); return (0); } static int -nmgr_datetime_get(struct mgmt_jbuf *njb) +nmgr_datetime_get(struct mgmt_cbuf *cb) { struct os_timeval tv; struct os_timezone tz; char buf[DATETIME_BUFSIZE]; - struct json_value jv; int rc; - json_encode_object_start(&njb->mjb_enc); - JSON_VALUE_INT(&jv, MGMT_ERR_EOK); - json_encode_object_entry(&njb->mjb_enc, "rc", &jv); + CborError g_err = CborNoError; + CborEncoder rsp; + g_err |= cbor_encoder_create_map(&cb->encoder, &rsp, CborIndefiniteLength); + g_err |= cbor_encode_text_stringz(&rsp, "rc"); + g_err |= cbor_encode_int(&rsp, MGMT_ERR_EOK); /* Display the current datetime */ rc = os_gettimeofday(&tv, &tz); @@ -241,39 +248,38 @@ nmgr_datetime_get(struct mgmt_jbuf *njb) rc = OS_EINVAL; goto err; } - - JSON_VALUE_STRING(&jv, buf) - json_encode_object_entry(&njb->mjb_enc, "datetime", &jv); - json_encode_object_finish(&njb->mjb_enc); - + g_err |= cbor_encode_text_stringz(&rsp, "datetime"); + g_err |= cbor_encode_text_stringz(&rsp, buf); + g_err |= cbor_encoder_close_container(&cb->encoder, &rsp); return OS_OK; + err: return (rc); } static int -nmgr_datetime_set(struct mgmt_jbuf *njb) +nmgr_datetime_set(struct mgmt_cbuf *cb) { struct os_timeval tv; struct os_timezone tz; - struct json_value jv; char buf[DATETIME_BUFSIZE]; int rc = OS_OK; - const struct json_attr_t datetime_write_attr[3] = { + + const struct cbor_attr_t datetime_write_attr[3] = { [0] = { .attribute = "datetime", - .type = t_string, + .type = CborAttrTextStringType, .addr.string = buf, .len = sizeof(buf), }, [1] = { .attribute = "rc", - .type = t_uinteger, + .type = CborAttrIntegerType, }, { 0 }, }; - rc = json_read_object(&njb->mjb_buf, datetime_write_attr); + rc = cbor_read_object(&cb->it, datetime_write_attr); if (rc) { rc = OS_EINVAL; goto out; @@ -292,12 +298,10 @@ nmgr_datetime_set(struct mgmt_jbuf *njb) goto out; } + rc = OS_OK; out: - json_encode_object_start(&njb->mjb_enc); - JSON_VALUE_INT(&jv, rc); - json_encode_object_entry(&njb->mjb_enc, "rc", &jv); - json_encode_object_finish(&njb->mjb_enc); - return OS_OK; + mgmt_cbuf_setoerr(cb, rc); + return rc; } static void @@ -307,12 +311,12 @@ nmgr_reset_tmo(void *arg) } static int -nmgr_reset(struct mgmt_jbuf *njb) +nmgr_reset(struct mgmt_cbuf *cb) { log_reboot(SOFT_REBOOT); os_callout_reset(&nmgr_reset_callout.cf_c, OS_TICKS_PER_SEC / 4); - mgmt_jbuf_setoerr(njb, 0); + mgmt_cbuf_setoerr(cb, OS_OK); return OS_OK; } http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/7f299efb/mgmt/newtmgr/pkg.yml ---------------------------------------------------------------------- diff --git a/mgmt/newtmgr/pkg.yml b/mgmt/newtmgr/pkg.yml index a472cb9..c5b0581 100644 --- a/mgmt/newtmgr/pkg.yml +++ b/mgmt/newtmgr/pkg.yml @@ -25,7 +25,7 @@ pkg.keywords: pkg.deps: - kernel/os - - encoding/json + - encoding/cborattr - mgmt/newtmgr/nmgr_os pkg.deps.NEWTMGR_BLE_HOST: