This is an automated email from the ASF dual-hosted git repository. sjanc pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/mynewt-core.git
commit fa94da4cc164df2ba4a892a5b60a2b81c265a371 Author: Szymon Janc <[email protected]> AuthorDate: Fri Oct 9 10:49:12 2026 +0200 all: Use tinycbor7 package Convert all users of tinycbor package to tinycbor7. This needs to be done in single change since both packages provide the same symbols and cannot be linked together, and some APIs (e.g. cborattr, mgmt) pass TinyCBOR types between packages. - flat buffers are handled with TinyCBOR buffer API - mbuf readers and writers are used with cbor_reader_parser_init() and cbor_writer_encoder_init() - strings from mbuf chains are copied and skipped with mbuf helpers - log_cbor_reader is reimplemented as Mynewt CBOR reader, with string buffer for pretty printing (LOG_CBOR_READER_STRING_BUF_SIZE) - metrics event dump uses string buffer for pretty printing - mgmt streamer uses Mynewt CBOR reader and writer types Map with odd number of items is not accepted by TinyCBOR parser, so cborattr unnamed attribute test is updated to expect error. Assisted-by: Claude:claude-opus-5-5 --- apps/metrics/src/main.c | 4 +- encoding/cborattr/include/cborattr/cborattr.h | 2 +- encoding/cborattr/pkg.yml | 2 +- encoding/cborattr/src/cborattr.c | 31 +++---- encoding/cborattr/test/pkg.yml | 2 +- encoding/cborattr/test/src/test_cborattr.h | 2 +- .../src/testcases/cborattr_decode_bool_array.c | 12 +-- .../test/src/testcases/cborattr_decode_int_array.c | 12 +-- .../test/src/testcases/cborattr_decode_obj_array.c | 12 +-- .../test/src/testcases/cborattr_decode_object.c | 14 ++- .../src/testcases/cborattr_decode_object_array.c | 12 +-- .../test/src/testcases/cborattr_decode_simple.c | 12 +-- .../src/testcases/cborattr_decode_string_array.c | 14 ++- .../cborattr_decode_string_array_bounds.c | 9 +- .../src/testcases/cborattr_decode_string_bounds.c | 9 +- .../src/testcases/cborattr_decode_substring_key.c | 12 +-- .../src/testcases/cborattr_decode_unnamed_array.c | 22 ++--- mgmt/mcumgr/cmd/img_mgmt/src/img_mgmt_state.c | 2 +- mgmt/mcumgr/cmd/log_mgmt/src/log_mgmt.c | 10 +- mgmt/mcumgr/cmd/os_mgmt/src/os_mgmt.c | 2 +- mgmt/mcumgr/mgmt/include/mgmt/mgmt.h | 7 +- mgmt/mcumgr/mgmt/pkg.yml | 2 +- mgmt/mcumgr/mgmt/src/mgmt.c | 10 +- mgmt/mcumgr/omp/include/omp/omp.h | 2 +- mgmt/mcumgr/omp/src/omp.c | 4 +- mgmt/mcumgr/smp/include/smp/smp.h | 4 +- mgmt/mcumgr/smp/src/smp.c | 8 +- mgmt/mgmt/pkg.yml | 2 +- mgmt/oicmgr/src/oicmgr.c | 9 +- mgmt/smp/include/mynewt_smp/smp.h | 2 +- mgmt/smp/smp_os/pkg.yml | 2 +- mgmt/smp/smp_os/src/smp_os.c | 2 +- mgmt/smp/src/smp.c | 6 +- mgmt/smp/transport/smp_shell/pkg.yml | 2 +- mgmt/smp/transport/smp_shell/src/smp_shell.c | 4 +- net/oic/include/oic/oc_rep.h | 2 +- net/oic/pkg.yml | 2 +- net/oic/selftest/pkg.yml | 2 +- net/oic/src/api/oc_rep.c | 48 +++++----- sys/log/full/pkg.yml | 6 +- sys/log/full/src/log_console.c | 7 +- sys/log/full/src/log_shell.c | 5 +- .../include/log_cbor_reader/log_cbor_reader.h | 34 +++++-- sys/log/util/log_cbor_reader/pkg.yml | 2 +- sys/log/util/log_cbor_reader/src/log_cbor_reader.c | 102 +++++---------------- .../log/util/log_cbor_reader/syscfg.yml | 18 ++-- sys/metrics/pkg.yml | 2 +- sys/metrics/src/cli.c | 10 +- sys/metrics/src/metrics.c | 6 +- sys/reboot/pkg.yml | 2 +- sys/reboot/src/log_reboot.c | 13 +-- sys/shell/pkg.yml | 2 +- sys/shell/src/shell_bridge.c | 2 +- sys/shell/src/shell_bridge_streamer.c | 2 +- 54 files changed, 235 insertions(+), 295 deletions(-) diff --git a/apps/metrics/src/main.c b/apps/metrics/src/main.c index efaf144ea..a4d160a87 100755 --- a/apps/metrics/src/main.c +++ b/apps/metrics/src/main.c @@ -24,8 +24,8 @@ #include "os/os.h" #include "sysinit/sysinit.h" #include "metrics/metrics.h" -#include "tinycbor/cbor_mbuf_reader.h" -#include "tinycbor/cbor.h" +#include "tinycbor7/cbor_mbuf_reader.h" +#include "tinycbor7/cbor.h" #include "log/log.h" #include "sysflash/sysflash.h" #include "fcb/fcb.h" diff --git a/encoding/cborattr/include/cborattr/cborattr.h b/encoding/cborattr/include/cborattr/cborattr.h index 083fea8f9..01bcc36d4 100644 --- a/encoding/cborattr/include/cborattr/cborattr.h +++ b/encoding/cborattr/include/cborattr/cborattr.h @@ -25,7 +25,7 @@ #include <ctype.h> #include <stdio.h> #include <sys/types.h> -#include "tinycbor/cbor.h" +#include "tinycbor7/cbor.h" #include <os/os_mbuf.h> diff --git a/encoding/cborattr/pkg.yml b/encoding/cborattr/pkg.yml index 6f19678b5..0547f106f 100644 --- a/encoding/cborattr/pkg.yml +++ b/encoding/cborattr/pkg.yml @@ -24,6 +24,6 @@ pkg.homepage: "http://mynewt.apache.org/" pkg.keywords: pkg.deps: - - "@apache-mynewt-core/encoding/tinycbor" + - "@apache-mynewt-core/encoding/tinycbor7" pkg.cflags.FLOAT_USER: -DFLOAT_SUPPORT diff --git a/encoding/cborattr/src/cborattr.c b/encoding/cborattr/src/cborattr.c index 4f980ae4f..df2bb0ac0 100644 --- a/encoding/cborattr/src/cborattr.c +++ b/encoding/cborattr/src/cborattr.c @@ -18,11 +18,10 @@ */ #include "cborattr/cborattr.h" -#include "tinycbor/cbor.h" -#include "tinycbor/cbor_buf_reader.h" +#include "tinycbor7/cbor.h" +#include "tinycbor7/cbor_mbuf_reader.h" #include "syscfg/syscfg.h" -#include "tinycbor/cbor_mbuf_reader.h" -#include "tinycbor/cbor_mbuf_writer.h" +#include "tinycbor7/cbor_mbuf_writer.h" #include "os/os_mbuf.h" #define CBORATTR_MAX_SIZE MYNEWT_VAL(CBORATTR_MAX_SIZE) #define CBORATTR_MAX_NESTING MYNEWT_VAL(CBORATTR_MAX_NESTING) @@ -108,7 +107,7 @@ cborattr_advance(CborValue *it) int depth; if (!cbor_value_is_container(it)) { - return cbor_value_advance(it); + return cbor_mbuf_value_advance(it); } err = cbor_value_enter_container(it, &stack[0]); @@ -136,7 +135,7 @@ cborattr_advance(CborValue *it) } depth++; } else { - err = cbor_value_advance(cur); + err = cbor_mbuf_value_advance(cur); if (err) { return err; } @@ -265,7 +264,7 @@ cbor_internal_read_object(CborValue *root_value, const struct cbor_attr_t *attrs while (cbor_value_is_valid(&cur_value) && !err) { /* get the attribute */ if (cbor_value_is_text_string(&cur_value)) { - err |= cbor_value_calculate_string_length(&cur_value, &len); + err |= cbor_mbuf_value_calculate_string_length(&cur_value, &len); if (err) { break; } @@ -273,7 +272,7 @@ cbor_internal_read_object(CborValue *root_value, const struct cbor_attr_t *attrs err |= CborErrorDataTooLarge; break; } - err |= cbor_value_copy_text_string(&cur_value, attrbuf, &len, NULL); + err |= cbor_mbuf_value_copy_text_string(&cur_value, attrbuf, &len, NULL); /* at least get the type of the next value so we can match the * attribute name and type for a perfect match */ @@ -335,7 +334,8 @@ cbor_internal_read_object(CborValue *root_value, const struct cbor_attr_t *attrs size_t len = cursor->len; CborError copy_err; - copy_err = cbor_value_copy_byte_string(&cur_value, lptr, &len, NULL); + copy_err = + cbor_mbuf_value_copy_byte_string(&cur_value, lptr, &len, NULL); err |= copy_err; /* On error len is set to string length, not copied length */ *cursor->addr.bytestring.len = copy_err ? 0 : len; @@ -351,7 +351,8 @@ cbor_internal_read_object(CborValue *root_value, const struct cbor_attr_t *attrs } /* Reserve space for terminating NUL */ len = cursor->len - 1; - copy_err = cbor_value_copy_text_string(&cur_value, lptr, &len, NULL); + copy_err = + cbor_mbuf_value_copy_text_string(&cur_value, lptr, &len, NULL); err |= copy_err; ((char *)lptr)[copy_err ? 0 : len] = '\0'; break; @@ -426,7 +427,7 @@ cbor_read_array(struct CborValue *value, const struct cbor_array_t *arr) } /* Reserve space for terminating NUL */ len--; - err |= cbor_value_copy_text_string(&elem, tp, &len, NULL); + err |= cbor_mbuf_value_copy_text_string(&elem, tp, &len, NULL); if (err) { break; } @@ -496,13 +497,11 @@ cbor_read_object(struct CborValue *value, const struct cbor_attr_t *attrs) int cbor_read_flat_attrs(const uint8_t *data, int len, const struct cbor_attr_t *attrs) { - struct cbor_buf_reader reader; struct CborParser parser; struct CborValue value; CborError err; - cbor_buf_reader_init(&reader, data, len); - err = cbor_parser_init(&reader.r, 0, &parser, &value); + err = cbor_parser_init(data, len, 0, &parser, &value); if (err != CborNoError) { return -1; } @@ -532,7 +531,7 @@ cbor_read_mbuf_attrs(struct os_mbuf *m, uint16_t off, uint16_t len, CborError err; cbor_mbuf_reader_init(&cmr, m, off); - err = cbor_parser_init(&cmr.r, 0, &parser, &value); + err = cbor_reader_parser_init(&cmr.r, &parser, &value); if (err != CborNoError) { return -1; } @@ -709,7 +708,7 @@ cbor_write_object_msys(const struct cbor_out_attr_t *attrs, struct os_mbuf **out } cbor_mbuf_writer_init(&writer, *out_om); - cbor_encoder_init(&encoder, &writer.enc, 0); + cbor_writer_encoder_init(&writer.enc, &encoder); rc = cbor_write_object(&encoder, attrs); if (rc != 0) { diff --git a/encoding/cborattr/test/pkg.yml b/encoding/cborattr/test/pkg.yml index df0c50a8b..2129c8493 100644 --- a/encoding/cborattr/test/pkg.yml +++ b/encoding/cborattr/test/pkg.yml @@ -23,7 +23,7 @@ pkg.homepage: "http://mynewt.apache.org/" pkg.keywords: pkg.deps: - - '@apache-mynewt-core/encoding/tinycbor' + - '@apache-mynewt-core/encoding/tinycbor7' - '@apache-mynewt-core/encoding/cborattr' pkg.deps.SELFTEST: diff --git a/encoding/cborattr/test/src/test_cborattr.h b/encoding/cborattr/test/src/test_cborattr.h index b7168fad9..48d04f823 100644 --- a/encoding/cborattr/test/src/test_cborattr.h +++ b/encoding/cborattr/test/src/test_cborattr.h @@ -23,7 +23,7 @@ #include <string.h> #include "testutil/testutil.h" #include "test_cborattr.h" -#include "tinycbor/cbor.h" +#include "tinycbor7/cbor.h" #include "cborattr/cborattr.h" #ifdef __cplusplus diff --git a/encoding/cborattr/test/src/testcases/cborattr_decode_bool_array.c b/encoding/cborattr/test/src/testcases/cborattr_decode_bool_array.c index a2e858ce6..4b28f9f69 100644 --- a/encoding/cborattr/test/src/testcases/cborattr_decode_bool_array.c +++ b/encoding/cborattr/test/src/testcases/cborattr_decode_bool_array.c @@ -27,18 +27,16 @@ static int test_cbor_len; /* * CBOR encoder data structures. */ -static int test_cbor_wr(struct cbor_encoder_writer *, const char *, int); +static CborError test_cbor_wr(void *, const void *, size_t, CborEncoderAppendType); static CborEncoder test_encoder; -static struct cbor_encoder_writer test_writer = { .write = test_cbor_wr }; - -static int -test_cbor_wr(struct cbor_encoder_writer *cew, const char *data, int len) +static CborError +test_cbor_wr(void *token, const void *data, size_t len, CborEncoderAppendType append) { memcpy(test_cbor_buf + test_cbor_len, data, len); test_cbor_len += len; assert(test_cbor_len < sizeof(test_cbor_buf)); - return 0; + return CborNoError; } static void @@ -47,7 +45,7 @@ test_encode_bool_array(void) CborEncoder data; CborEncoder array; - cbor_encoder_init(&test_encoder, &test_writer, 0); + cbor_encoder_init_writer(&test_encoder, test_cbor_wr, NULL); cbor_encoder_create_map(&test_encoder, &data, CborIndefiniteLength); diff --git a/encoding/cborattr/test/src/testcases/cborattr_decode_int_array.c b/encoding/cborattr/test/src/testcases/cborattr_decode_int_array.c index c48ae7026..c082a8820 100644 --- a/encoding/cborattr/test/src/testcases/cborattr_decode_int_array.c +++ b/encoding/cborattr/test/src/testcases/cborattr_decode_int_array.c @@ -27,18 +27,16 @@ static int test_cbor_len; /* * CBOR encoder data structures. */ -static int test_cbor_wr(struct cbor_encoder_writer *, const char *, int); +static CborError test_cbor_wr(void *, const void *, size_t, CborEncoderAppendType); static CborEncoder test_encoder; -static struct cbor_encoder_writer test_writer = { .write = test_cbor_wr }; - -static int -test_cbor_wr(struct cbor_encoder_writer *cew, const char *data, int len) +static CborError +test_cbor_wr(void *token, const void *data, size_t len, CborEncoderAppendType append) { memcpy(test_cbor_buf + test_cbor_len, data, len); test_cbor_len += len; assert(test_cbor_len < sizeof(test_cbor_buf)); - return 0; + return CborNoError; } static void @@ -47,7 +45,7 @@ test_encode_int_array(void) CborEncoder data; CborEncoder array; - cbor_encoder_init(&test_encoder, &test_writer, 0); + cbor_encoder_init_writer(&test_encoder, test_cbor_wr, NULL); cbor_encoder_create_map(&test_encoder, &data, CborIndefiniteLength); diff --git a/encoding/cborattr/test/src/testcases/cborattr_decode_obj_array.c b/encoding/cborattr/test/src/testcases/cborattr_decode_obj_array.c index 9295cfcae..ef4394dae 100644 --- a/encoding/cborattr/test/src/testcases/cborattr_decode_obj_array.c +++ b/encoding/cborattr/test/src/testcases/cborattr_decode_obj_array.c @@ -27,18 +27,16 @@ static int test_cbor_len; /* * CBOR encoder data structures. */ -static int test_cbor_wr(struct cbor_encoder_writer *, const char *, int); +static CborError test_cbor_wr(void *, const void *, size_t, CborEncoderAppendType); static CborEncoder test_encoder; -static struct cbor_encoder_writer test_writer = { .write = test_cbor_wr }; - -static int -test_cbor_wr(struct cbor_encoder_writer *cew, const char *data, int len) +static CborError +test_cbor_wr(void *token, const void *data, size_t len, CborEncoderAppendType append) { memcpy(test_cbor_buf + test_cbor_len, data, len); test_cbor_len += len; assert(test_cbor_len < sizeof(test_cbor_buf)); - return 0; + return CborNoError; } static void @@ -48,7 +46,7 @@ test_encode_obj_array(void) CborEncoder array; CborEncoder obj; - cbor_encoder_init(&test_encoder, &test_writer, 0); + cbor_encoder_init_writer(&test_encoder, test_cbor_wr, NULL); cbor_encoder_create_map(&test_encoder, &data, CborIndefiniteLength); diff --git a/encoding/cborattr/test/src/testcases/cborattr_decode_object.c b/encoding/cborattr/test/src/testcases/cborattr_decode_object.c index 69008b417..26e354545 100644 --- a/encoding/cborattr/test/src/testcases/cborattr_decode_object.c +++ b/encoding/cborattr/test/src/testcases/cborattr_decode_object.c @@ -27,18 +27,16 @@ static int test_cbor_len; /* * CBOR encoder data structures. */ -static int test_cbor_wr(struct cbor_encoder_writer *, const char *, int); +static CborError test_cbor_wr(void *, const void *, size_t, CborEncoderAppendType); static CborEncoder test_encoder; -static struct cbor_encoder_writer test_writer = { .write = test_cbor_wr }; - -static int -test_cbor_wr(struct cbor_encoder_writer *cew, const char *data, int len) +static CborError +test_cbor_wr(void *token, const void *data, size_t len, CborEncoderAppendType append) { memcpy(test_cbor_buf + test_cbor_len, data, len); test_cbor_len += len; assert(test_cbor_len < sizeof(test_cbor_buf)); - return 0; + return CborNoError; } static void @@ -48,7 +46,7 @@ test_encode_data(void) CborEncoder sub_obj; test_cbor_len = 0; - cbor_encoder_init(&test_encoder, &test_writer, 0); + cbor_encoder_init_writer(&test_encoder, test_cbor_wr, NULL); cbor_encoder_create_map(&test_encoder, &test_data, CborIndefiniteLength); /* @@ -73,7 +71,7 @@ test_encode_data_complex(void) test_cbor_len = 0; - cbor_encoder_init(&test_encoder, &test_writer, 0); + cbor_encoder_init_writer(&test_encoder, test_cbor_wr, NULL); cbor_encoder_create_map(&test_encoder, &test_data, CborIndefiniteLength); /* diff --git a/encoding/cborattr/test/src/testcases/cborattr_decode_object_array.c b/encoding/cborattr/test/src/testcases/cborattr_decode_object_array.c index ab5fc9b9c..ca2c5d400 100644 --- a/encoding/cborattr/test/src/testcases/cborattr_decode_object_array.c +++ b/encoding/cborattr/test/src/testcases/cborattr_decode_object_array.c @@ -27,18 +27,16 @@ static int test_cbor_len; /* * CBOR encoder data structures. */ -static int test_cbor_wr(struct cbor_encoder_writer *, const char *, int); +static CborError test_cbor_wr(void *, const void *, size_t, CborEncoderAppendType); static CborEncoder test_encoder; -static struct cbor_encoder_writer test_writer = { .write = test_cbor_wr }; - -static int -test_cbor_wr(struct cbor_encoder_writer *cew, const char *data, int len) +static CborError +test_cbor_wr(void *token, const void *data, size_t len, CborEncoderAppendType append) { memcpy(test_cbor_buf + test_cbor_len, data, len); test_cbor_len += len; assert(test_cbor_len < sizeof(test_cbor_buf)); - return 0; + return CborNoError; } static void @@ -49,7 +47,7 @@ test_encode_object_array(void) CborEncoder sub_obj; test_cbor_len = 0; - cbor_encoder_init(&test_encoder, &test_writer, 0); + cbor_encoder_init_writer(&test_encoder, test_cbor_wr, NULL); cbor_encoder_create_map(&test_encoder, &data, CborIndefiniteLength); diff --git a/encoding/cborattr/test/src/testcases/cborattr_decode_simple.c b/encoding/cborattr/test/src/testcases/cborattr_decode_simple.c index 46bdf8d64..b7868d311 100644 --- a/encoding/cborattr/test/src/testcases/cborattr_decode_simple.c +++ b/encoding/cborattr/test/src/testcases/cborattr_decode_simple.c @@ -27,18 +27,16 @@ static int test_cbor_len; /* * CBOR encoder data structures. */ -static int test_cbor_wr(struct cbor_encoder_writer *, const char *, int); +static CborError test_cbor_wr(void *, const void *, size_t, CborEncoderAppendType); static CborEncoder test_encoder; -static struct cbor_encoder_writer test_writer = { .write = test_cbor_wr }; - -static int -test_cbor_wr(struct cbor_encoder_writer *cew, const char *data, int len) +static CborError +test_cbor_wr(void *token, const void *data, size_t len, CborEncoderAppendType append) { memcpy(test_cbor_buf + test_cbor_len, data, len); test_cbor_len += len; assert(test_cbor_len < sizeof(test_cbor_buf)); - return 0; + return CborNoError; } static void @@ -47,7 +45,7 @@ test_encode_data(void) CborEncoder test_data; uint8_t data[4] = { 0, 1, 2 }; - cbor_encoder_init(&test_encoder, &test_writer, 0); + cbor_encoder_init_writer(&test_encoder, test_cbor_wr, NULL); cbor_encoder_create_map(&test_encoder, &test_data, CborIndefiniteLength); /* diff --git a/encoding/cborattr/test/src/testcases/cborattr_decode_string_array.c b/encoding/cborattr/test/src/testcases/cborattr_decode_string_array.c index bf74135ad..f952611a7 100644 --- a/encoding/cborattr/test/src/testcases/cborattr_decode_string_array.c +++ b/encoding/cborattr/test/src/testcases/cborattr_decode_string_array.c @@ -27,18 +27,16 @@ static int test_cbor_len; /* * CBOR encoder data structures. */ -static int test_cbor_wr(struct cbor_encoder_writer *, const char *, int); +static CborError test_cbor_wr(void *, const void *, size_t, CborEncoderAppendType); static CborEncoder test_encoder; -static struct cbor_encoder_writer test_writer = { .write = test_cbor_wr }; - -static int -test_cbor_wr(struct cbor_encoder_writer *cew, const char *data, int len) +static CborError +test_cbor_wr(void *token, const void *data, size_t len, CborEncoderAppendType append) { memcpy(test_cbor_buf + test_cbor_len, data, len); test_cbor_len += len; assert(test_cbor_len < sizeof(test_cbor_buf)); - return 0; + return CborNoError; } static void @@ -48,7 +46,7 @@ test_encode_string_array_one(void) CborEncoder array; test_cbor_len = 0; - cbor_encoder_init(&test_encoder, &test_writer, 0); + cbor_encoder_init_writer(&test_encoder, test_cbor_wr, NULL); cbor_encoder_create_map(&test_encoder, &data, CborIndefiniteLength); @@ -71,7 +69,7 @@ test_encode_string_array_three(void) CborEncoder array; test_cbor_len = 0; - cbor_encoder_init(&test_encoder, &test_writer, 0); + cbor_encoder_init_writer(&test_encoder, test_cbor_wr, NULL); cbor_encoder_create_map(&test_encoder, &data, CborIndefiniteLength); diff --git a/encoding/cborattr/test/src/testcases/cborattr_decode_string_array_bounds.c b/encoding/cborattr/test/src/testcases/cborattr_decode_string_array_bounds.c index 4e4430ecd..2670f5888 100644 --- a/encoding/cborattr/test/src/testcases/cborattr_decode_string_array_bounds.c +++ b/encoding/cborattr/test/src/testcases/cborattr_decode_string_array_bounds.c @@ -22,27 +22,26 @@ static uint8_t test_arr_buf[64]; static int test_arr_len; -static int -test_arr_wr(struct cbor_encoder_writer *cew, const char *data, int len) +static CborError +test_arr_wr(void *token, const void *data, size_t len, CborEncoderAppendType append) { memcpy(test_arr_buf + test_arr_len, data, len); test_arr_len += len; assert(test_arr_len < sizeof(test_arr_buf)); - return 0; + return CborNoError; } /* Encodes {"a": [strs...]} */ static void test_arr_encode(const char **strs, int cnt) { - struct cbor_encoder_writer writer = { .write = test_arr_wr }; CborEncoder enc; CborEncoder map; CborEncoder arr; int i; test_arr_len = 0; - cbor_encoder_init(&enc, &writer, 0); + cbor_encoder_init_writer(&enc, test_arr_wr, NULL); cbor_encoder_create_map(&enc, &map, CborIndefiniteLength); cbor_encode_text_stringz(&map, "a"); cbor_encoder_create_array(&map, &arr, CborIndefiniteLength); diff --git a/encoding/cborattr/test/src/testcases/cborattr_decode_string_bounds.c b/encoding/cborattr/test/src/testcases/cborattr_decode_string_bounds.c index 0d4d99f7a..2166be9a5 100644 --- a/encoding/cborattr/test/src/testcases/cborattr_decode_string_bounds.c +++ b/encoding/cborattr/test/src/testcases/cborattr_decode_string_bounds.c @@ -22,25 +22,24 @@ static uint8_t test_str_buf[64]; static int test_str_len; -static int -test_str_wr(struct cbor_encoder_writer *cew, const char *data, int len) +static CborError +test_str_wr(void *token, const void *data, size_t len, CborEncoderAppendType append) { memcpy(test_str_buf + test_str_len, data, len); test_str_len += len; assert(test_str_len < sizeof(test_str_buf)); - return 0; + return CborNoError; } /* Encodes {"s": <text>, "b": <bytes>, "i": 1}, omitting NULL members. */ static void test_str_encode(const char *s, const uint8_t *b, size_t b_len) { - struct cbor_encoder_writer writer = { .write = test_str_wr }; CborEncoder enc; CborEncoder map; test_str_len = 0; - cbor_encoder_init(&enc, &writer, 0); + cbor_encoder_init_writer(&enc, test_str_wr, NULL); cbor_encoder_create_map(&enc, &map, CborIndefiniteLength); if (s) { cbor_encode_text_stringz(&map, "s"); diff --git a/encoding/cborattr/test/src/testcases/cborattr_decode_substring_key.c b/encoding/cborattr/test/src/testcases/cborattr_decode_substring_key.c index c73611fad..47912f0df 100644 --- a/encoding/cborattr/test/src/testcases/cborattr_decode_substring_key.c +++ b/encoding/cborattr/test/src/testcases/cborattr_decode_substring_key.c @@ -27,18 +27,16 @@ static int test_cbor_len; /* * CBOR encoder data structures. */ -static int test_cbor_wr(struct cbor_encoder_writer *, const char *, int); +static CborError test_cbor_wr(void *, const void *, size_t, CborEncoderAppendType); static CborEncoder test_encoder; -static struct cbor_encoder_writer test_writer = { .write = test_cbor_wr }; - -static int -test_cbor_wr(struct cbor_encoder_writer *cew, const char *data, int len) +static CborError +test_cbor_wr(void *token, const void *data, size_t len, CborEncoderAppendType append) { memcpy(test_cbor_buf + test_cbor_len, data, len); test_cbor_len += len; assert(test_cbor_len < sizeof(test_cbor_buf)); - return 0; + return CborNoError; } static void @@ -46,7 +44,7 @@ test_encode_substring_key(void) { CborEncoder data; - cbor_encoder_init(&test_encoder, &test_writer, 0); + cbor_encoder_init_writer(&test_encoder, test_cbor_wr, NULL); /* * { "a": "A", "aa": "AA", "aaa" : "AAA" } diff --git a/encoding/cborattr/test/src/testcases/cborattr_decode_unnamed_array.c b/encoding/cborattr/test/src/testcases/cborattr_decode_unnamed_array.c index a17617213..83098e6ab 100644 --- a/encoding/cborattr/test/src/testcases/cborattr_decode_unnamed_array.c +++ b/encoding/cborattr/test/src/testcases/cborattr_decode_unnamed_array.c @@ -27,18 +27,16 @@ static int test_cbor_len; /* * CBOR encoder data structures. */ -static int test_cbor_wr(struct cbor_encoder_writer *, const char *, int); +static CborError test_cbor_wr(void *, const void *, size_t, CborEncoderAppendType); static CborEncoder test_encoder; -static struct cbor_encoder_writer test_writer = { .write = test_cbor_wr }; - -static int -test_cbor_wr(struct cbor_encoder_writer *cew, const char *data, int len) +static CborError +test_cbor_wr(void *token, const void *data, size_t len, CborEncoderAppendType append) { memcpy(test_cbor_buf + test_cbor_len, data, len); test_cbor_len += len; assert(test_cbor_len < sizeof(test_cbor_buf)); - return 0; + return CborNoError; } static void @@ -47,7 +45,7 @@ test_encode_unnamed_array(void) CborEncoder data; CborEncoder array; - cbor_encoder_init(&test_encoder, &test_writer, 0); + cbor_encoder_init_writer(&test_encoder, test_cbor_wr, NULL); cbor_encoder_create_map(&test_encoder, &data, CborIndefiniteLength); @@ -84,10 +82,10 @@ TEST_CASE(test_cborattr_decode_unnamed_array) test_encode_unnamed_array(); + /* + * Map with odd number of items is not valid CBOR and is rejected by + * TinyCBOR parser. + */ rc = cbor_read_flat_attrs(test_cbor_buf, test_cbor_len, test_attrs); - TEST_ASSERT(rc == 0); - TEST_ASSERT(arr_cnt == 3); - TEST_ASSERT(arr_data[0] == 1); - TEST_ASSERT(arr_data[1] == 2); - TEST_ASSERT(arr_data[2] == 33); + TEST_ASSERT(rc != 0); } diff --git a/mgmt/mcumgr/cmd/img_mgmt/src/img_mgmt_state.c b/mgmt/mcumgr/cmd/img_mgmt/src/img_mgmt_state.c index 7d8d5e9f0..3f687d8da 100644 --- a/mgmt/mcumgr/cmd/img_mgmt/src/img_mgmt_state.c +++ b/mgmt/mcumgr/cmd/img_mgmt/src/img_mgmt_state.c @@ -19,7 +19,7 @@ #include <assert.h> -#include "tinycbor/cbor.h" +#include "tinycbor7/cbor.h" #include "cborattr/cborattr.h" #include "mgmt/mgmt.h" #include "img_mgmt/img_mgmt.h" diff --git a/mgmt/mcumgr/cmd/log_mgmt/src/log_mgmt.c b/mgmt/mcumgr/cmd/log_mgmt/src/log_mgmt.c index 3f6bd46a6..d74e42ca7 100644 --- a/mgmt/mcumgr/cmd/log_mgmt/src/log_mgmt.c +++ b/mgmt/mcumgr/cmd/log_mgmt/src/log_mgmt.c @@ -23,7 +23,7 @@ #include "os/mynewt.h" #include "mgmt/mgmt.h" #include "cborattr/cborattr.h" -#include "tinycbor/cbor_cnt_writer.h" +#include "tinycbor7/cbor_cnt_writer.h" #include "log_mgmt/log_mgmt.h" #include "log/log.h" @@ -444,7 +444,7 @@ log_mgmt_encode_entry(CborEncoder *enc, const struct log_mgmt_entry *entry, static int log_mgmt_cb_encode(struct log_mgmt_entry *entry, void *arg) { - struct CborCntWriter cnt_writer; + struct cbor_cnt_writer cnt_writer; struct log_mgmt_enc_ctxt *lmec; struct log_walk_ctxt *ctxt; CborEncoder cnt_encoder; @@ -461,7 +461,7 @@ log_mgmt_cb_encode(struct log_mgmt_entry *entry, void *arg) */ cbor_cnt_writer_init(&cnt_writer); - cbor_encoder_init(&cnt_encoder, &cnt_writer.enc, 0); + cbor_writer_encoder_init(&cnt_writer.enc, &cnt_encoder); rc = log_mgmt_encode_entry(&cnt_encoder, entry, &entry_len, lmec); if (rc != 0) { return rc; @@ -506,7 +506,7 @@ static int log_encode_entries(const struct log_mgmt_log *log, CborEncoder *enc, int64_t timestamp, uint32_t index) { - struct CborCntWriter cnt_writer; + struct cbor_cnt_writer cnt_writer; struct log_mgmt_filter filter; struct log_walk_ctxt ctxt = { 0 }; CborEncoder cnt_encoder; @@ -520,7 +520,7 @@ log_encode_entries(const struct log_mgmt_log *log, CborEncoder *enc, /* this code counts how long the message would be if we encoded * this outer structure using cbor. */ cbor_cnt_writer_init(&cnt_writer); - cbor_encoder_init(&cnt_encoder, &cnt_writer.enc, 0); + cbor_writer_encoder_init(&cnt_writer.enc, &cnt_encoder); err |= cbor_encode_text_stringz(&cnt_encoder, "entries"); err |= cbor_encoder_create_array(&cnt_encoder, &entries, CborIndefiniteLength); err |= cbor_encoder_close_container(&cnt_encoder, &entries); diff --git a/mgmt/mcumgr/cmd/os_mgmt/src/os_mgmt.c b/mgmt/mcumgr/cmd/os_mgmt/src/os_mgmt.c index 22ea4b42d..d1df8f699 100644 --- a/mgmt/mcumgr/cmd/os_mgmt/src/os_mgmt.c +++ b/mgmt/mcumgr/cmd/os_mgmt/src/os_mgmt.c @@ -22,7 +22,7 @@ #include "os/mynewt.h" #include "hal/hal_system.h" #include "hal/hal_watchdog.h" -#include "tinycbor/cbor.h" +#include "tinycbor7/cbor.h" #include "cborattr/cborattr.h" #include "mgmt/mgmt.h" #include "os_mgmt/os_mgmt.h" diff --git a/mgmt/mcumgr/mgmt/include/mgmt/mgmt.h b/mgmt/mcumgr/mgmt/include/mgmt/mgmt.h index bf43453ed..727f0d8b2 100644 --- a/mgmt/mcumgr/mgmt/include/mgmt/mgmt.h +++ b/mgmt/mcumgr/mgmt/include/mgmt/mgmt.h @@ -21,7 +21,8 @@ #define H_MGMT_MGMT_ #include <inttypes.h> -#include "tinycbor/cbor.h" +#include "tinycbor7/cbor.h" +#include "tinycbor7/cbor_reader.h" #ifdef __cplusplus extern "C" { @@ -210,8 +211,8 @@ int mgmt_write_rsp_status(struct mgmt_ctxt *ctxt, int status); * * @return 0 on success, MGMT_ERR_[...] code on failure. */ -int mgmt_ctxt_init(struct mgmt_ctxt *ctxt, struct cbor_decoder_reader *reader, - struct cbor_encoder_writer *writer); +int mgmt_ctxt_init(struct mgmt_ctxt *ctxt, struct cbor_reader *reader, + struct cbor_writer *writer); /** * @brief Converts a CBOR status code to a MGMT_ERR_[...] code. diff --git a/mgmt/mcumgr/mgmt/pkg.yml b/mgmt/mcumgr/mgmt/pkg.yml index 565510f5f..61cdbeb3e 100644 --- a/mgmt/mcumgr/mgmt/pkg.yml +++ b/mgmt/mcumgr/mgmt/pkg.yml @@ -24,5 +24,5 @@ pkg.homepage: "http://mynewt.apache.org/" pkg.keywords: pkg.deps: - - '@apache-mynewt-core/encoding/tinycbor' + - '@apache-mynewt-core/encoding/tinycbor7' - '@apache-mynewt-core/kernel/os' diff --git a/mgmt/mcumgr/mgmt/src/mgmt.c b/mgmt/mcumgr/mgmt/src/mgmt.c index 9ef93a245..072024aae 100644 --- a/mgmt/mcumgr/mgmt/src/mgmt.c +++ b/mgmt/mcumgr/mgmt/src/mgmt.c @@ -19,7 +19,7 @@ #include <string.h> -#include "tinycbor/cbor.h" +#include "tinycbor7/cbor.h" #include "mgmt/endian.h" #include "mgmt/mgmt.h" @@ -143,17 +143,17 @@ mgmt_err_from_cbor(int cbor_status) } int -mgmt_ctxt_init(struct mgmt_ctxt *ctxt, struct cbor_decoder_reader *reader, - struct cbor_encoder_writer *writer) +mgmt_ctxt_init(struct mgmt_ctxt *ctxt, struct cbor_reader *reader, + struct cbor_writer *writer) { int rc; - rc = cbor_parser_init(reader, 0, &ctxt->parser, &ctxt->it); + rc = cbor_reader_parser_init(reader, &ctxt->parser, &ctxt->it); if (rc != CborNoError) { return mgmt_err_from_cbor(rc); } - cbor_encoder_init(&ctxt->encoder, writer, 0); + cbor_writer_encoder_init(writer, &ctxt->encoder); return 0; } diff --git a/mgmt/mcumgr/omp/include/omp/omp.h b/mgmt/mcumgr/omp/include/omp/omp.h index c290a4c21..c5fb17c62 100644 --- a/mgmt/mcumgr/omp/include/omp/omp.h +++ b/mgmt/mcumgr/omp/include/omp/omp.h @@ -38,7 +38,7 @@ #ifndef H_OMP_ #define H_OMP_ -#include "tinycbor/cbor_mbuf_reader.h" +#include "tinycbor7/cbor_mbuf_reader.h" #include "mgmt/mgmt.h" #ifdef __cplusplus diff --git a/mgmt/mcumgr/omp/src/omp.c b/mgmt/mcumgr/omp/src/omp.c index e0bc33c6f..ddef42396 100644 --- a/mgmt/mcumgr/omp/src/omp.c +++ b/mgmt/mcumgr/omp/src/omp.c @@ -24,7 +24,7 @@ #include <mgmt/mgmt.h> #include <cborattr/cborattr.h> -#include <tinycbor/cbor.h> +#include <tinycbor7/cbor.h> #include "omp/omp.h" #include "omp/omp_priv.h" @@ -160,7 +160,7 @@ static void omp_init_reader(struct omp_streamer *streamer, struct os_mbuf *m, struct mgmt_ctxt *ctxt) { cbor_mbuf_reader_init(streamer->reader, m, streamer->req_off); - cbor_parser_init(&streamer->reader->r, 0, &ctxt->parser, &ctxt->it); + cbor_reader_parser_init(&streamer->reader->r, &ctxt->parser, &ctxt->it); } int diff --git a/mgmt/mcumgr/smp/include/smp/smp.h b/mgmt/mcumgr/smp/include/smp/smp.h index 5a285aacf..860c741ba 100644 --- a/mgmt/mcumgr/smp/include/smp/smp.h +++ b/mgmt/mcumgr/smp/include/smp/smp.h @@ -39,8 +39,8 @@ #define H_SMP_ #include "os/os_mbuf.h" -#include "tinycbor/cbor_mbuf_reader.h" -#include "tinycbor/cbor_mbuf_writer.h" +#include "tinycbor7/cbor_mbuf_reader.h" +#include "tinycbor7/cbor_mbuf_writer.h" #include "mgmt/mgmt.h" #ifdef __cplusplus diff --git a/mgmt/mcumgr/smp/src/smp.c b/mgmt/mcumgr/smp/src/smp.c index 76eb976cd..090a1587f 100644 --- a/mgmt/mcumgr/smp/src/smp.c +++ b/mgmt/mcumgr/smp/src/smp.c @@ -22,7 +22,7 @@ #include <assert.h> #include <string.h> -#include "tinycbor/cbor.h" +#include "tinycbor7/cbor.h" #include "mgmt/endian.h" #include "mgmt/mgmt.h" #include "smp/smp.h" @@ -134,15 +134,15 @@ smp_write_at(struct cbor_mbuf_writer *cmw, size_t offset, const void *data, size static int smp_read_hdr(struct smp_streamer *streamer, struct mgmt_hdr *dst_hdr) { - struct cbor_decoder_reader *reader; + struct cbor_reader *reader; reader = &streamer->reader->r; - if (reader->message_size < sizeof *dst_hdr) { + if (!reader->ops->can_read(reader, 0, sizeof(*dst_hdr)) || + !reader->ops->read(reader, dst_hdr, 0, sizeof(*dst_hdr))) { return MGMT_ERR_EINVAL; } - reader->cpy(reader, (char *)dst_hdr, 0, sizeof *dst_hdr); return 0; } diff --git a/mgmt/mgmt/pkg.yml b/mgmt/mgmt/pkg.yml index 4cde92d39..731751327 100644 --- a/mgmt/mgmt/pkg.yml +++ b/mgmt/mgmt/pkg.yml @@ -25,5 +25,5 @@ pkg.keywords: pkg.deps: - "@apache-mynewt-core/kernel/os" - - "@apache-mynewt-core/encoding/tinycbor" + - "@apache-mynewt-core/encoding/tinycbor7" - "@apache-mynewt-core/mgmt/mcumgr/mgmt" diff --git a/mgmt/oicmgr/src/oicmgr.c b/mgmt/oicmgr/src/oicmgr.c index 801d095f2..2710e3246 100644 --- a/mgmt/oicmgr/src/oicmgr.c +++ b/mgmt/oicmgr/src/oicmgr.c @@ -24,9 +24,9 @@ #include "mgmt/mgmt.h" #include "omp/omp.h" -#include "tinycbor/cbor.h" -#include "tinycbor/cbor_mbuf_writer.h" -#include "tinycbor/cbor_mbuf_reader.h" +#include "tinycbor7/cbor.h" +#include "tinycbor7/cbor_mbuf_writer.h" +#include "tinycbor7/cbor_mbuf_reader.h" #include "cborattr/cborattr.h" #include "omp/omp.h" @@ -137,8 +137,7 @@ omgr_extract_req_hdr(oc_request_t *req, struct nmgr_hdr *out_hdr) coap_get_payload(req->packet, &m, &data_off); cbor_mbuf_reader_init(&reader, m, data_off); - cbor_parser_init(&reader.r, 0, &parser, - &it); + cbor_reader_parser_init(&reader.r, &parser, &it); rc = omp_read_hdr(&it, out_hdr); if (rc != 0) { diff --git a/mgmt/smp/include/mynewt_smp/smp.h b/mgmt/smp/include/mynewt_smp/smp.h index e07a9bbc1..b1109aa73 100644 --- a/mgmt/smp/include/mynewt_smp/smp.h +++ b/mgmt/smp/include/mynewt_smp/smp.h @@ -25,7 +25,7 @@ * @{ */ -#include <tinycbor/cbor.h> +#include <tinycbor7/cbor.h> #include <inttypes.h> #include "os/mynewt.h" #include "smp/smp.h" diff --git a/mgmt/smp/smp_os/pkg.yml b/mgmt/smp/smp_os/pkg.yml index 310eadd34..76f93da23 100644 --- a/mgmt/smp/smp_os/pkg.yml +++ b/mgmt/smp/smp_os/pkg.yml @@ -29,7 +29,7 @@ pkg.deps: - "@apache-mynewt-core/kernel/os" - "@apache-mynewt-core/mgmt/mcumgr/cmd/os_mgmt" - "@apache-mynewt-core/mgmt/mcumgr/mgmt" - - "@apache-mynewt-core/encoding/tinycbor" + - "@apache-mynewt-core/encoding/tinycbor7" - "@apache-mynewt-core/encoding/cborattr" pkg.deps.TIMEPERSIST: diff --git a/mgmt/smp/smp_os/src/smp_os.c b/mgmt/smp/smp_os/src/smp_os.c index b73ec82b4..cac474643 100644 --- a/mgmt/smp/smp_os/src/smp_os.c +++ b/mgmt/smp/smp_os/src/smp_os.c @@ -36,7 +36,7 @@ #include "smp_os/smp_os.h" -#include <tinycbor/cbor.h> +#include <tinycbor7/cbor.h> #include <cborattr/cborattr.h> static int smp_def_console_echo(struct mgmt_ctxt *cb); diff --git a/mgmt/smp/src/smp.c b/mgmt/smp/src/smp.c index 76e474ffd..b04c0af22 100644 --- a/mgmt/smp/src/smp.c +++ b/mgmt/smp/src/smp.c @@ -24,9 +24,9 @@ #include "mgmt/mgmt.h" #include "os_mgmt/os_mgmt.h" #include "mynewt_smp/smp.h" -#include "tinycbor/cbor.h" -#include "tinycbor/cbor_mbuf_writer.h" -#include "tinycbor/cbor_mbuf_reader.h" +#include "tinycbor7/cbor.h" +#include "tinycbor7/cbor_mbuf_writer.h" +#include "tinycbor7/cbor_mbuf_reader.h" /* Shared queue that SMP uses for work items. */ struct os_eventq *g_smp_evq; diff --git a/mgmt/smp/transport/smp_shell/pkg.yml b/mgmt/smp/transport/smp_shell/pkg.yml index 9c1bf16c1..e59b356d9 100644 --- a/mgmt/smp/transport/smp_shell/pkg.yml +++ b/mgmt/smp/transport/smp_shell/pkg.yml @@ -30,7 +30,7 @@ pkg.deps: - "@apache-mynewt-core/sys/shell" - "@apache-mynewt-core/mgmt/smp" - "@apache-mynewt-core/mgmt/mcumgr/mgmt" - - "@apache-mynewt-core/encoding/tinycbor" + - "@apache-mynewt-core/encoding/tinycbor7" pkg.init: smp_shell_pkg_init: 'MYNEWT_VAL(SMP_SHELL_SYSINIT_STAGE)' diff --git a/mgmt/smp/transport/smp_shell/src/smp_shell.c b/mgmt/smp/transport/smp_shell/src/smp_shell.c index 2258d8c7e..80a8e41cd 100644 --- a/mgmt/smp/transport/smp_shell/src/smp_shell.c +++ b/mgmt/smp/transport/smp_shell/src/smp_shell.c @@ -24,8 +24,8 @@ #include <shell/shell.h> #include <mgmt/mgmt.h> #include <mynewt_smp/smp.h> -#include "tinycbor/cbor_mbuf_reader.h" -#include "tinycbor/cbor_mbuf_writer.h" +#include "tinycbor7/cbor_mbuf_reader.h" +#include "tinycbor7/cbor_mbuf_writer.h" static struct smp_transport g_smp_shell_transport; diff --git a/net/oic/include/oic/oc_rep.h b/net/oic/include/oic/oc_rep.h index 87efd91dd..353abe849 100644 --- a/net/oic/include/oic/oc_rep.h +++ b/net/oic/include/oic/oc_rep.h @@ -20,7 +20,7 @@ #include <stdbool.h> #include <stdint.h> -#include <tinycbor/cbor.h> +#include <tinycbor7/cbor.h> #include "oic/oc_constants.h" #include "oic/oc_helpers.h" #include "oic/port/mynewt/config.h" diff --git a/net/oic/pkg.yml b/net/oic/pkg.yml index b938f1680..6862a9323 100644 --- a/net/oic/pkg.yml +++ b/net/oic/pkg.yml @@ -25,7 +25,7 @@ pkg.keywords: pkg.deps: - "@apache-mynewt-core/encoding/cborattr" - - "@apache-mynewt-core/encoding/tinycbor" + - "@apache-mynewt-core/encoding/tinycbor7" - "@apache-mynewt-core/kernel/os" - "@apache-mynewt-core/sys/log/modlog" diff --git a/net/oic/selftest/pkg.yml b/net/oic/selftest/pkg.yml index 7d20aac26..437547991 100644 --- a/net/oic/selftest/pkg.yml +++ b/net/oic/selftest/pkg.yml @@ -24,7 +24,7 @@ pkg.keywords: pkg.deps: - "@apache-mynewt-core/encoding/cborattr" - - "@apache-mynewt-core/encoding/tinycbor" + - "@apache-mynewt-core/encoding/tinycbor7" - "@apache-mynewt-core/net/oic" - "@apache-mynewt-core/sys/console/stub" - "@apache-mynewt-core/sys/log/full" diff --git a/net/oic/src/api/oc_rep.c b/net/oic/src/api/oc_rep.c index 111322031..52e1a4259 100644 --- a/net/oic/src/api/oc_rep.c +++ b/net/oic/src/api/oc_rep.c @@ -18,8 +18,8 @@ #include "os/mynewt.h" -#include <tinycbor/cbor_mbuf_writer.h> -#include <tinycbor/cbor_mbuf_reader.h> +#include <tinycbor7/cbor_mbuf_writer.h> +#include <tinycbor7/cbor_mbuf_reader.h> #include "oic/port/mynewt/config.h" #include "oic/oc_rep.h" @@ -44,7 +44,7 @@ oc_rep_new(struct os_mbuf *m) g_err = CborNoError; g_outm = m; cbor_mbuf_writer_init(&g_buf_writer, m); - cbor_encoder_init(&g_encoder, &g_buf_writer.enc, 0); + cbor_writer_encoder_init(&g_buf_writer.enc, &g_encoder); } int @@ -145,11 +145,12 @@ oc_parse_rep_value(CborValue *value, oc_rep_t **rep, CborError *err) cur->next = 0; cur->value_object_array = 0; /* key */ - *err |= cbor_value_calculate_string_length(value, &len); + *err |= cbor_mbuf_value_calculate_string_length(value, &len); len++; oc_alloc_string(&cur->name, len); - *err |= cbor_value_copy_text_string(value, oc_string(cur->name), &len, NULL); - *err |= cbor_value_advance(value); + *err |= cbor_mbuf_value_copy_text_string(value, oc_string(cur->name), &len, + NULL); + *err |= cbor_mbuf_value_advance(value); /* value */ switch (value->type) { case CborIntegerType: @@ -165,20 +166,19 @@ oc_parse_rep_value(CborValue *value, oc_rep_t **rep, CborError *err) cur->type = DOUBLE; break; case CborByteStringType: - *err |= cbor_value_calculate_string_length(value, &len); + *err |= cbor_mbuf_value_calculate_string_length(value, &len); len++; oc_alloc_string(&cur->value_string, len); - *err |= cbor_value_copy_byte_string(value, - (uint8_t *)oc_string(cur->value_string), - &len, NULL); + *err |= cbor_mbuf_value_copy_byte_string( + value, (uint8_t *)oc_string(cur->value_string), &len, NULL); cur->type = BYTE_STRING; break; case CborTextStringType: - *err |= cbor_value_calculate_string_length(value, &len); + *err |= cbor_mbuf_value_calculate_string_length(value, &len); len++; oc_alloc_string(&cur->value_string, len); - *err |= cbor_value_copy_text_string(value, oc_string(cur->value_string), - &len, NULL); + *err |= cbor_mbuf_value_copy_text_string( + value, oc_string(cur->value_string), &len, NULL); cur->type = STRING; break; case CborMapType: /* when value is a map/object */ { @@ -188,7 +188,7 @@ oc_parse_rep_value(CborValue *value, oc_rep_t **rep, CborError *err) oc_parse_rep_value(&map, obj, err); (*obj)->next = 0; obj = &(*obj)->next; - *err |= cbor_value_advance(&map); + *err |= cbor_mbuf_value_advance(&map); } cur->type = OBJECT; } break; @@ -200,7 +200,7 @@ oc_parse_rep_value(CborValue *value, oc_rep_t **rep, CborError *err) CborValue t = array; while (!cbor_value_at_end(&t)) { len++; - cbor_value_advance(&t); + cbor_mbuf_value_advance(&t); } } k = 0; @@ -235,9 +235,9 @@ oc_parse_rep_value(CborValue *value, oc_rep_t **rep, CborError *err) oc_new_string_array(&cur->value_array, len); cur->type = BYTE_STRING | ARRAY; } - *err |= cbor_value_calculate_string_length(&array, &len); + *err |= cbor_mbuf_value_calculate_string_length(&array, &len); len++; - *err |= cbor_value_copy_byte_string( + *err |= cbor_mbuf_value_copy_byte_string( &array, (uint8_t *)oc_string_array_get_item(cur->value_array, k), &len, NULL); break; @@ -246,9 +246,9 @@ oc_parse_rep_value(CborValue *value, oc_rep_t **rep, CborError *err) oc_new_string_array(&cur->value_array, len); cur->type = STRING | ARRAY; } - *err |= cbor_value_calculate_string_length(&array, &len); + *err |= cbor_mbuf_value_calculate_string_length(&array, &len); len++; - *err |= cbor_value_copy_text_string( + *err |= cbor_mbuf_value_copy_text_string( &array, (char *)oc_string_array_get_item(cur->value_array, k), &len, NULL); break; @@ -269,14 +269,14 @@ oc_parse_rep_value(CborValue *value, oc_rep_t **rep, CborError *err) while (!cbor_value_at_end(&map)) { oc_parse_rep_value(&map, obj, err); obj = &(*obj)->next; - *err |= cbor_value_advance(&map); + *err |= cbor_mbuf_value_advance(&map); } break; default: break; } k++; - *err |= cbor_value_advance(&array); + *err |= cbor_mbuf_value_advance(&array); } break; default: @@ -294,14 +294,14 @@ oc_parse_rep(struct os_mbuf *m, uint16_t payload_off, struct cbor_mbuf_reader br; cbor_mbuf_reader_init(&br, m, payload_off); - err |= cbor_parser_init(&br.r, 0, &parser, &root_value); + err |= cbor_reader_parser_init(&br.r, &parser, &root_value); if (cbor_value_is_map(&root_value)) { err |= cbor_value_enter_container(&root_value, &cur_value); *out_rep = 0; oc_rep_t **cur = out_rep; while (cbor_value_is_valid(&cur_value)) { oc_parse_rep_value(&cur_value, cur, &err); - err |= cbor_value_advance(&cur_value); + err |= cbor_mbuf_value_advance(&cur_value); cur = &(*cur)->next; } } else if (cbor_value_is_array(&root_value)) { @@ -313,7 +313,7 @@ oc_parse_rep(struct os_mbuf *m, uint16_t payload_off, *cur = _alloc_rep(); (*cur)->type = OBJECT; oc_parse_rep_value(&cur_value, &(*cur)->value_object, &err); - err |= cbor_value_advance(&cur_value); + err |= cbor_mbuf_value_advance(&cur_value); (*cur)->next = 0; cur = &(*cur)->next; } diff --git a/sys/log/full/pkg.yml b/sys/log/full/pkg.yml index 06bb9dbea..5dd68aa60 100644 --- a/sys/log/full/pkg.yml +++ b/sys/log/full/pkg.yml @@ -40,15 +40,15 @@ pkg.deps.LOG_CLI: - "@apache-mynewt-core/sys/log/util/log_cbor_reader" - "@apache-mynewt-core/sys/shell" - "@apache-mynewt-core/encoding/base64" - - "@apache-mynewt-core/encoding/tinycbor" + - "@apache-mynewt-core/encoding/tinycbor7" - "@apache-mynewt-core/util/parse" pkg.deps.LOG_CONSOLE: - - "@apache-mynewt-core/encoding/tinycbor" + - "@apache-mynewt-core/encoding/tinycbor7" pkg.deps.LOG_MGMT: - "@apache-mynewt-core/mgmt/mcumgr/cmd/log_mgmt" - - "@apache-mynewt-core/encoding/tinycbor" + - "@apache-mynewt-core/encoding/tinycbor7" pkg.deps.LOG_FLAGS_IMAGE_HASH: - "@apache-mynewt-core/mgmt/imgmgr" diff --git a/sys/log/full/src/log_console.c b/sys/log/full/src/log_console.c index d836a413f..d8cda2265 100644 --- a/sys/log/full/src/log_console.c +++ b/sys/log/full/src/log_console.c @@ -24,8 +24,7 @@ #include <cbmem/cbmem.h> #include <console/console.h> #include "log/log.h" -#include "tinycbor/cbor.h" -#include "tinycbor/cbor_buf_reader.h" +#include "tinycbor7/cbor.h" #include "datetime/datetime.h" static int @@ -33,10 +32,8 @@ log_console_dump_cbor_entry(const void *dptr, uint16_t len) { struct CborParser cbor_parser; struct CborValue cbor_value; - struct cbor_buf_reader cbor_buf_reader; - cbor_buf_reader_init(&cbor_buf_reader, dptr, len); - cbor_parser_init(&cbor_buf_reader.r, 0, &cbor_parser, &cbor_value); + cbor_parser_init(dptr, len, 0, &cbor_parser, &cbor_value); cbor_value_to_pretty(stdout, &cbor_value); console_write("\n", 1); diff --git a/sys/log/full/src/log_shell.c b/sys/log/full/src/log_shell.c index a031ed997..8c71ba16d 100644 --- a/sys/log/full/src/log_shell.c +++ b/sys/log/full/src/log_shell.c @@ -35,8 +35,7 @@ #include "shell/shell.h" #include "console/console.h" #include "base64/hex.h" -#include "tinycbor/cbor.h" -#include "tinycbor/compilersupport_p.h" +#include "tinycbor7/cbor.h" #include "log_cbor_reader/log_cbor_reader.h" void log_console_print_hdr(const struct log_entry_hdr *hdr); @@ -143,7 +142,7 @@ shell_log_dump_entry(struct log *log, struct log_offset *log_offset, break; case LOG_ETYPE_CBOR: log_cbor_reader_init(&cbor_reader, log, dptr, len); - cbor_parser_init(&cbor_reader.r, 0, &cbor_parser, &cbor_value); + cbor_reader_parser_init(&cbor_reader.r, &cbor_parser, &cbor_value); cbor_value_to_pretty(stdout, &cbor_value); break; default: diff --git a/sys/log/util/log_cbor_reader/include/log_cbor_reader/log_cbor_reader.h b/sys/log/util/log_cbor_reader/include/log_cbor_reader/log_cbor_reader.h index 9a1770645..fd45edced 100644 --- a/sys/log/util/log_cbor_reader/include/log_cbor_reader/log_cbor_reader.h +++ b/sys/log/util/log_cbor_reader/include/log_cbor_reader/log_cbor_reader.h @@ -17,17 +17,39 @@ * under the License. */ +#ifndef H_LOG_CBOR_READER_ +#define H_LOG_CBOR_READER_ + #include "os/mynewt.h" #include "log/log.h" -#include "tinycbor/cbor.h" -#include "tinycbor/compilersupport_p.h" +#include "tinycbor7/cbor.h" +#include "tinycbor7/cbor_reader.h" + +#ifdef __cplusplus +extern "C" { +#endif +/** + * CBOR reader for log entry body. Parser is initialized with + * cbor_reader_parser_init(&reader->r, ...). + * + * Strings are not contiguous in memory so TinyCBOR string functions (used + * e.g. by cbor_value_to_pretty()) can only access strings that fit in reader + * string buffer. + */ struct log_cbor_reader { - struct cbor_decoder_reader r; + struct cbor_reader r; struct log *log; const void *dptr; + uint16_t len; + uint8_t str_buf[MYNEWT_VAL(LOG_CBOR_READER_STRING_BUF_SIZE)]; }; -void -log_cbor_reader_init(struct log_cbor_reader *cbr, struct log *log, - const void *dptr, uint16_t len); +void log_cbor_reader_init(struct log_cbor_reader *cbr, struct log *log, + const void *dptr, uint16_t len); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/sys/log/util/log_cbor_reader/pkg.yml b/sys/log/util/log_cbor_reader/pkg.yml index 903b1d3ea..b4e7b47ca 100644 --- a/sys/log/util/log_cbor_reader/pkg.yml +++ b/sys/log/util/log_cbor_reader/pkg.yml @@ -27,7 +27,7 @@ pkg.keywords: - reader pkg.deps: - - "@apache-mynewt-core/encoding/tinycbor" + - "@apache-mynewt-core/encoding/tinycbor7" - "@apache-mynewt-core/sys/log/common" pkg.req_apis: diff --git a/sys/log/util/log_cbor_reader/src/log_cbor_reader.c b/sys/log/util/log_cbor_reader/src/log_cbor_reader.c index 99705b08c..3770a5bff 100644 --- a/sys/log/util/log_cbor_reader/src/log_cbor_reader.c +++ b/sys/log/util/log_cbor_reader/src/log_cbor_reader.c @@ -19,104 +19,48 @@ #include "os/mynewt.h" #include "log/log.h" -#include "tinycbor/cbor.h" #include "log_cbor_reader/log_cbor_reader.h" -#include "tinycbor/compilersupport_p.h" -static uint8_t -log_cbor_reader_get8(struct cbor_decoder_reader *d, int offset) +static bool +log_cbor_reader_can_read(const struct cbor_reader *r, size_t offset, size_t len) { - struct log_cbor_reader *cbr = (struct log_cbor_reader *)d; - uint8_t val = 0; + const struct log_cbor_reader *cbr = (const struct log_cbor_reader *)r; - (void)log_read_body(cbr->log, cbr->dptr, &val, offset, sizeof(val)); - - return val; -} - -static uint16_t -log_cbor_reader_get16(struct cbor_decoder_reader *d, int offset) -{ - struct log_cbor_reader *cbr = (struct log_cbor_reader *)d; - uint16_t val = 0; - - (void)log_read_body(cbr->log, cbr->dptr, &val, offset, sizeof(val)); - - return cbor_ntohs(val); -} - -static uint32_t -log_cbor_reader_get32(struct cbor_decoder_reader *d, int offset) -{ - struct log_cbor_reader *cbr = (struct log_cbor_reader *)d; - uint32_t val = 0; - - (void)log_read_body(cbr->log, cbr->dptr, &val, offset, sizeof(val)); - - return cbor_ntohl(val); -} - -static uint64_t -log_cbor_reader_get64(struct cbor_decoder_reader *d, int offset) -{ - struct log_cbor_reader *cbr = (struct log_cbor_reader *)d; - uint64_t val = 0; - - (void)log_read_body(cbr->log, cbr->dptr, &val, offset, sizeof(val)); - - return cbor_ntohll(val); + return offset <= cbr->len && len <= cbr->len - offset; } -static uintptr_t -log_cbor_reader_cmp(struct cbor_decoder_reader *d, char *dst, - int src_offset, size_t len) +static void * +log_cbor_reader_read(const struct cbor_reader *r, void *dst, size_t offset, size_t len) { - struct log_cbor_reader *cbr = (struct log_cbor_reader *)d; - uint8_t buf[16]; - int chunk_len; - int offset; + const struct log_cbor_reader *cbr = (const struct log_cbor_reader *)r; int rc; - offset = 0; - - while (offset < len) { - chunk_len = min(len - offset, sizeof(buf)); - - log_read_body(cbr->log, cbr->dptr, buf, src_offset + offset, chunk_len); - - rc = memcmp(&dst[offset], buf, chunk_len); - if (rc) { - return rc; - } + if (!log_cbor_reader_can_read(r, offset, len)) { + return NULL; + } - offset += chunk_len; + rc = log_read_body(cbr->log, cbr->dptr, dst, offset, len); + if (rc != len) { + return NULL; } - return 0; + return dst; } -static uintptr_t -log_cbor_reader_cpy(struct cbor_decoder_reader *d, char *dst, - int src_offset, size_t len) -{ - struct log_cbor_reader *cbr = (struct log_cbor_reader *)d; - - log_read_body(cbr->log, cbr->dptr, dst, src_offset, len); - - return (uintptr_t)dst; -} +static const struct cbor_reader_ops log_cbor_reader_ops = { + .can_read = log_cbor_reader_can_read, + .read = log_cbor_reader_read, +}; void log_cbor_reader_init(struct log_cbor_reader *cbr, struct log *log, const void *dptr, uint16_t len) { - cbr->r.get8 = &log_cbor_reader_get8; - cbr->r.get16 = &log_cbor_reader_get16; - cbr->r.get32 = &log_cbor_reader_get32; - cbr->r.get64 = &log_cbor_reader_get64; - cbr->r.cmp = &log_cbor_reader_cmp; - cbr->r.cpy = &log_cbor_reader_cpy; - cbr->r.message_size = len; + memset(&cbr->r, 0, sizeof(cbr->r)); + cbr->r.ops = &log_cbor_reader_ops; + cbr->r.scratch = cbr->str_buf; + cbr->r.scratch_size = sizeof(cbr->str_buf); cbr->log = log; cbr->dptr = dptr; + cbr->len = len; } diff --git a/encoding/cborattr/pkg.yml b/sys/log/util/log_cbor_reader/syscfg.yml similarity index 72% copy from encoding/cborattr/pkg.yml copy to sys/log/util/log_cbor_reader/syscfg.yml index 6f19678b5..afd1ed256 100644 --- a/encoding/cborattr/pkg.yml +++ b/sys/log/util/log_cbor_reader/syscfg.yml @@ -6,7 +6,7 @@ # 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, @@ -17,13 +17,11 @@ # under the License. # -pkg.name: encoding/cborattr -pkg.description: CBOR encoding/decoding library -pkg.author: "Apache Mynewt <[email protected]>" -pkg.homepage: "http://mynewt.apache.org/" -pkg.keywords: - -pkg.deps: - - "@apache-mynewt-core/encoding/tinycbor" -pkg.cflags.FLOAT_USER: -DFLOAT_SUPPORT +syscfg.defs: + LOG_CBOR_READER_STRING_BUF_SIZE: + description: > + Size of buffer in log CBOR reader used to read strings from log + entry body (e.g. when pretty printing entry). Longer strings + cannot be read with TinyCBOR string functions. + value: 128 diff --git a/sys/metrics/pkg.yml b/sys/metrics/pkg.yml index 2ef83451b..ae7263e92 100644 --- a/sys/metrics/pkg.yml +++ b/sys/metrics/pkg.yml @@ -27,7 +27,7 @@ pkg.keywords: pkg.deps: - kernel/os - sys/log/full - - "@apache-mynewt-core/encoding/tinycbor" + - "@apache-mynewt-core/encoding/tinycbor7" pkg.apis: - metrics pkg.deps.METRICS_CLI: diff --git a/sys/metrics/src/cli.c b/sys/metrics/src/cli.c index 1ee316517..30cf6f2fc 100644 --- a/sys/metrics/src/cli.c +++ b/sys/metrics/src/cli.c @@ -27,8 +27,8 @@ #include "shell/shell.h" #include "console/console.h" #include "metrics/metrics.h" -#include "tinycbor/cbor.h" -#include "tinycbor/cbor_mbuf_reader.h" +#include "tinycbor7/cbor.h" +#include "tinycbor7/cbor_mbuf_reader.h" static STAILQ_HEAD(, metrics_event_hdr) g_event_list = STAILQ_HEAD_INITIALIZER(g_event_list); @@ -172,6 +172,7 @@ static int cmd_event_dump(int argc, char **argv) { struct cbor_mbuf_reader reader; + uint8_t str_buf[64]; struct CborParser parser; struct CborValue value; struct metrics_event_hdr *hdr; @@ -192,7 +193,10 @@ cmd_event_dump(int argc, char **argv) om = os_msys_get_pkthdr(50, 0); metrics_event_to_cbor(hdr, om); cbor_mbuf_reader_init(&reader, om, 0); - cbor_parser_init(&reader.r, 0, &parser, &value); + /* Strings may span multiple mbufs, pretty printer needs them contiguous */ + reader.r.scratch = str_buf; + reader.r.scratch_size = sizeof(str_buf); + cbor_reader_parser_init(&reader.r, &parser, &value); cbor_value_to_pretty(stdout, &value); os_mbuf_free_chain(om); diff --git a/sys/metrics/src/metrics.c b/sys/metrics/src/metrics.c index 9f387cc27..720ef6796 100644 --- a/sys/metrics/src/metrics.c +++ b/sys/metrics/src/metrics.c @@ -25,8 +25,8 @@ #include "syscfg/syscfg.h" #include "os/os.h" #include "metrics/metrics.h" -#include "tinycbor/cbor.h" -#include "tinycbor/cbor_mbuf_writer.h" +#include "tinycbor7/cbor.h" +#include "tinycbor7/cbor_mbuf_writer.h" #include "log/log.h" #include "metrics_priv.h" @@ -444,7 +444,7 @@ metrics_event_to_cbor(struct metrics_event_hdr *hdr, struct os_mbuf *om) int rc; cbor_mbuf_writer_init(&writer, om); - cbor_encoder_init(&encoder, &writer.enc, 0); + cbor_writer_encoder_init(&writer.enc, &encoder); rc = cbor_encoder_create_map(&encoder, &map, CborIndefiniteLength); if (rc != 0) { diff --git a/sys/reboot/pkg.yml b/sys/reboot/pkg.yml index 6df8c4baa..0c3ebf99c 100644 --- a/sys/reboot/pkg.yml +++ b/sys/reboot/pkg.yml @@ -30,7 +30,7 @@ pkg.deps: - "@apache-mynewt-core/sys/config" - "@apache-mynewt-core/sys/flash_map" - "@apache-mynewt-core/sys/log/modlog" - - "@apache-mynewt-core/encoding/tinycbor" + - "@apache-mynewt-core/encoding/tinycbor7" - "@apache-mynewt-core/mgmt/mcumgr/cmd/img_mgmt" - "@mcuboot/boot/bootutil" diff --git a/sys/reboot/src/log_reboot.c b/sys/reboot/src/log_reboot.c index 121d594c2..6ea439d13 100644 --- a/sys/reboot/src/log_reboot.c +++ b/sys/reboot/src/log_reboot.c @@ -29,8 +29,7 @@ #include "reboot/log_reboot.h" #include "bsp/bsp.h" #include "flash_map/flash_map.h" -#include "tinycbor/cbor.h" -#include "tinycbor/cbor_buf_writer.h" +#include "tinycbor7/cbor.h" uint16_t reboot_cnt; static int8_t log_reboot_written; @@ -176,7 +175,6 @@ log_reboot_write(const struct log_reboot_info *info) int off; int rc; int i; - struct cbor_buf_writer writer; struct CborEncoder enc; struct CborEncoder map; size_t cbor_buf_len; @@ -199,8 +197,7 @@ log_reboot_write(const struct log_reboot_info *info) memset(cbor_enc_buf, 0, sizeof cbor_enc_buf); - cbor_buf_writer_init(&writer, cbor_enc_buf, sizeof cbor_enc_buf); - cbor_encoder_init(&enc, &writer.enc, 0); + cbor_encoder_init(&enc, cbor_enc_buf, sizeof(cbor_enc_buf), 0); rc = cbor_encoder_create_map(&enc, &map, CborIndefiniteLength); if (rc != 0) { return rc; @@ -267,14 +264,14 @@ log_reboot_write(const struct log_reboot_info *info) } cbor_encode_text_stringz(&map, buf); - /* Find length of the CBOR encoded log entry. */ - cbor_buf_len = cbor_buf_writer_buffer_size(&writer, cbor_enc_buf) + 1; - rc = cbor_encoder_close_container(&enc, &map); if (rc != 0) { return SYS_ENOMEM; } + /* Find length of the CBOR encoded log entry. */ + cbor_buf_len = cbor_encoder_get_buffer_size(&enc, cbor_enc_buf); + /* Log a CBOR encoded reboot record. */ modlog_append(LOG_MODULE_REBOOT, LOG_LEVEL_CRITICAL, LOG_ETYPE_CBOR, cbor_enc_buf, cbor_buf_len); diff --git a/sys/shell/pkg.yml b/sys/shell/pkg.yml index e4856a52e..05a9b5e70 100644 --- a/sys/shell/pkg.yml +++ b/sys/shell/pkg.yml @@ -34,7 +34,7 @@ pkg.deps.SHELL_MGMT: - "@apache-mynewt-core/util/crc" pkg.deps.SHELL_BRIDGE: - - "@apache-mynewt-core/encoding/tinycbor" + - "@apache-mynewt-core/encoding/tinycbor7" pkg.req_apis: - console diff --git a/sys/shell/src/shell_bridge.c b/sys/shell/src/shell_bridge.c index ff5304c91..00c1dacdb 100644 --- a/sys/shell/src/shell_bridge.c +++ b/sys/shell/src/shell_bridge.c @@ -24,7 +24,7 @@ #include "mgmt/mgmt.h" #include "cborattr/cborattr.h" #include "streamer/streamer.h" -#include "tinycbor/cbor_mbuf_writer.h" +#include "tinycbor7/cbor_mbuf_writer.h" #include "shell_priv.h" static int shell_bridge_exec(struct mgmt_ctxt *cb); diff --git a/sys/shell/src/shell_bridge_streamer.c b/sys/shell/src/shell_bridge_streamer.c index d9df4ddd6..63e62ba94 100644 --- a/sys/shell/src/shell_bridge_streamer.c +++ b/sys/shell/src/shell_bridge_streamer.c @@ -21,7 +21,7 @@ #if MYNEWT_VAL(SHELL_BRIDGE) -#include "tinycbor/cbor.h" +#include "tinycbor7/cbor.h" #include "shell_priv.h" static int
