cborattr; add helper routine cbor_read_flat_attrs(). This takes as argument pointer to beginning of cbor encoded data, and decodes it into an array of cbor_attr_t's.
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/9cafdcdb Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/9cafdcdb Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/9cafdcdb Branch: refs/heads/sensors_branch Commit: 9cafdcdbcd0a878232d7a7cbf29d4befa275dd48 Parents: bcc4050 Author: Marko Kiiskila <ma...@runtime.io> Authored: Tue Dec 13 10:07:02 2016 -0800 Committer: Sterling Hughes <sterl...@apache.org> Committed: Sun Dec 18 13:56:16 2016 -0800 ---------------------------------------------------------------------- encoding/cborattr/include/cborattr/cborattr.h | 2 ++ encoding/cborattr/src/cborattr.c | 28 ++++++++++++++++++++++ 2 files changed, 30 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/9cafdcdb/encoding/cborattr/include/cborattr/cborattr.h ---------------------------------------------------------------------- diff --git a/encoding/cborattr/include/cborattr/cborattr.h b/encoding/cborattr/include/cborattr/cborattr.h index d3ca2fa..d7f0e03 100644 --- a/encoding/cborattr/include/cborattr/cborattr.h +++ b/encoding/cborattr/include/cborattr/cborattr.h @@ -121,6 +121,8 @@ struct cbor_attr_t { int cbor_read_object(struct CborValue *, const struct cbor_attr_t *); int cbor_read_array(struct CborParser *, const struct cbor_array_t *); +int cbor_read_flat_attrs(const uint8_t *data, int len, + const struct cbor_attr_t *attrs); #ifdef __cplusplus } http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/9cafdcdb/encoding/cborattr/src/cborattr.c ---------------------------------------------------------------------- diff --git a/encoding/cborattr/src/cborattr.c b/encoding/cborattr/src/cborattr.c index a7ca9c0..4101646 100644 --- a/encoding/cborattr/src/cborattr.c +++ b/encoding/cborattr/src/cborattr.c @@ -19,6 +19,7 @@ #include <cborattr/cborattr.h> #include <tinycbor/cbor.h> +#include <tinycbor/cbor_buf_reader.h> /* this maps a CborType to a matching CborAtter Type. The mapping is not * one-to-one because of signedness of integers @@ -265,3 +266,30 @@ cbor_read_object(struct CborValue *value, const struct cbor_attr_t *attrs) st = cbor_internal_read_object(value, attrs, NULL, 0); return st; } + +/* + * Read in cbor key/values from flat buffer pointed by data, and fill them + * into attrs. + * + * @param data Pointer to beginning of cbor encoded data + * @param len Number of bytes in the buffer + * @param attrs Array of cbor objects to look for. + * + * @return 0 on success; non-zero on failure. + */ +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); + if (err != CborNoError) { + return -1; + } + return cbor_read_object(&value, attrs); +}