diff --git a/include/thingset.h b/include/thingset.h index c77e272..76e48c3 100644 --- a/include/thingset.h +++ b/include/thingset.h @@ -88,13 +88,16 @@ extern "C" { (code >= 0x80 && code < 0xA0) /**< Check if provided code indicates success. */ /* Reserved data object IDs */ -#define THINGSET_ID_ROOT 0x00 /**< Root object for a node (empty path) */ -#define THINGSET_ID_TIME 0x10 /**< Unix timestamp `t_s` */ -#define THINGSET_ID_IDS 0x16 /**< `_Ids` overlay */ -#define THINGSET_ID_PATHS 0x17 /**< `_Paths` overlay */ -#define THINGSET_ID_METADATAURL 0x18 /**< URL for extended metadata information: `cMetadataURL` */ -#define THINGSET_ID_METADATA 0x19 /**< `_Metadata` overlay */ -#define THINGSET_ID_NODEID 0x1D /**< String containing the node ID: `cNodeID` */ +#define THINGSET_ID_ROOT 0x00 /**< Root object for a node (empty path) */ +#define THINGSET_ID_TIME 0x10 /**< Unix timestamp `t_s` */ +#define THINGSET_ID_IDS 0x16 /**< `_Ids` overlay */ +#define THINGSET_ID_PATHS 0x17 /**< `_Paths` overlay */ +#define THINGSET_ID_METADATAURL 0x18 /**< URL for extended metadata information: `cMetadataURL` */ +#define THINGSET_ID_METADATA 0x19 /**< `_Metadata` overlay */ +#define THINGSET_ID_METADATA_NAME 0x1A /**< `name` key in metadata overlay */ +#define THINGSET_ID_METADATA_TYPE 0x1B /**< `type` key in metadata overlay */ +#define THINGSET_ID_METADATA_ACCESS 0x1C /**< `access` key in metadata overlay */ +#define THINGSET_ID_NODEID 0x1D /**< String containing the node ID: `cNodeID` */ /* * Macros for defining data object array elements. diff --git a/src/thingset_bin.c b/src/thingset_bin.c index 0a8c401..c474546 100644 --- a/src/thingset_bin.c +++ b/src/thingset_bin.c @@ -181,8 +181,7 @@ static int bin_serialize_metadata(struct thingset_context *ts, return err; } - const char name[] = "name"; - if (!zcbor_tstr_put_lit(ts->encoder, name)) { + if (!zcbor_uint32_put(ts->encoder, THINGSET_ID_METADATA_NAME)) { return -THINGSET_ERR_RESPONSE_TOO_LARGE; } @@ -190,8 +189,7 @@ static int bin_serialize_metadata(struct thingset_context *ts, return -THINGSET_ERR_RESPONSE_TOO_LARGE; } - const char type[] = "type"; - if (!zcbor_tstr_put_lit(ts->encoder, type)) { + if (!zcbor_uint32_put(ts->encoder, THINGSET_ID_METADATA_TYPE)) { return -THINGSET_ERR_RESPONSE_TOO_LARGE; } @@ -204,6 +202,13 @@ static int bin_serialize_metadata(struct thingset_context *ts, return -THINGSET_ERR_RESPONSE_TOO_LARGE; } + if (!zcbor_uint32_put(ts->encoder, THINGSET_ID_METADATA_ACCESS)) { + return -THINGSET_ERR_RESPONSE_TOO_LARGE; + } + if (!zcbor_uint32_put(ts->encoder, object->access)) { + return -THINGSET_ERR_RESPONSE_TOO_LARGE; + } + if ((err = bin_serialize_map_end(ts))) { return err; } @@ -710,10 +715,11 @@ static int bin_deserialize_value(struct thingset_context *ts, int index = 0; do { /* using uint8_t pointer for byte-wise pointer arithmetics */ - union thingset_data_pointer data = { .u8 = array->elements.u8 + index * type_size }; + union thingset_data_pointer data = { .u8 = array->elements.u8 + + index * type_size }; - err = bin_deserialize_simple_value(ts, data, array->element_type, array->decimals, - check_only); + err = bin_deserialize_simple_value(ts, data, array->element_type, + array->decimals, check_only); if (err != 0) { break; } @@ -749,10 +755,10 @@ static int bin_deserialize_value(struct thingset_context *ts, continue; } union thingset_data_pointer data = { .u8 = ((uint8_t *)records->records) - + (i * records->record_size) - + element->data.offset }; + + (i * records->record_size) + + element->data.offset }; err = bin_deserialize_simple_value(ts, data, element->type, element->detail, - check_only); + check_only); } success = zcbor_map_end_decode(ts->decoder); diff --git a/src/thingset_txt.c b/src/thingset_txt.c index a7679d3..44be64f 100644 --- a/src/thingset_txt.c +++ b/src/thingset_txt.c @@ -400,6 +400,17 @@ static int txt_serialize_metadata(struct thingset_context *ts, return -THINGSET_ERR_RESPONSE_TOO_LARGE; } + if ((err = txt_serialize_string(ts, "access", true))) { + return -THINGSET_ERR_RESPONSE_TOO_LARGE; + } + + len = snprintf(ts->rsp + ts->rsp_pos, ts->rsp_size - ts->rsp_pos, "%d", object->access); + if (len < 0) { + return -THINGSET_ERR_RESPONSE_TOO_LARGE; + } + + ts->rsp_pos += len; + if ((err = txt_serialize_map_end(ts))) { return err; } diff --git a/tests/protocol/src/bin.c b/tests/protocol/src/bin.c index 3f51e8d..ea78e3a 100644 --- a/tests/protocol/src/bin.c +++ b/tests/protocol/src/bin.c @@ -401,12 +401,13 @@ ZTEST(thingset_bin, test_fetch_metadata) const char rsp_exp_hex[] = "85 F6 " - "84 " /* array with 4 elements */ - "a2 64 6e 61 6d 65 64 77 49 33 32 64 74 79 70 65 63 69 33 32 " /* name: wI32, type: i32 - */ - "a2 64 6e 61 6d 65 63 74 5f 73 64 74 79 70 65 63 75 33 32 " /* name: t_s, type: u32 */ - "a2 64 6e 61 6d 65 63 74 5f 73 64 74 79 70 65 63 75 33 32 " /* name: t_s, type: u32 */ - "a2 64 6e 61 6d 65 64 4f 62 6a 31 64 74 79 70 65 65 67 72 6f 75 70"; /* name: Obj1, + "84 " /* array with 4 elements */ + "a3 18 1a 64 77 49 33 32 18 1b 63 69 33 32 18 1c 18 77 " /* name: wI32, type: i32, access: + * any + */ + "a3 18 1a 63 74 5f 73 18 1b 63 75 33 32 18 1c 0f " /* name: t_s, type: u32 */ + "a3 18 1a 63 74 5f 73 18 1b 63 75 33 32 18 1c 18 77 " /* name: t_s, type: u32 */ + "a3 18 1a 64 4f 62 6a 31 18 1b 65 67 72 6f 75 70 18 1c 0f "; /* name: Obj1, type: group */ THINGSET_ASSERT_REQUEST_HEX(req_hex, rsp_exp_hex); @@ -424,7 +425,7 @@ ZTEST(thingset_bin, test_fetch_metadata_array) "85 F6 " "81 " /* array with 1 element */ /* name: wU16, type: u16[] */ - "a2 64 6e 61 6d 65 64 77 55 31 36 64 74 79 70 65 65 75 31 36 5b 5d"; + "a3 18 1a 64 77 55 31 36 18 1b 65 75 31 36 5b 5d 18 1c 18 77 "; THINGSET_ASSERT_REQUEST_HEX(req_hex, rsp_exp_hex); } @@ -441,7 +442,7 @@ ZTEST(thingset_bin, test_fetch_metadata_void_func) "85 F6 " "81 " /* array with 1 element */ /* name: xVoid, type: ()->() */ - "a2 64 6e 61 6d 65 65 78 56 6f 69 64 64 74 79 70 65 66 28 29 2d 3e 28 29 "; + "a3 18 1a 65 78 56 6f 69 64 18 1b 66 28 29 2d 3e 28 29 18 1c 18 77 "; THINGSET_ASSERT_REQUEST_HEX(req_hex, rsp_exp_hex); } @@ -458,8 +459,9 @@ ZTEST(thingset_bin, test_fetch_metadata_parameterised_func) "85 F6 " "81 " /* array with 1 element */ /* name: xVoid, type: ()->() */ - "a2 64 6e 61 6d 65 6a 78 49 33 32 50 61 72 61 6d 73 64 74 79 70 65 73 28 73 74 72 69 6e 67 " - "2c 69 33 32 29 2d 3e 28 69 33 32 29"; /* name: xI32Params, type: (string,i32)->(i32) */ + "a3 18 1a 6a 78 49 33 32 50 61 72 61 6d 73 18 1b 73 28 73 74 72 69 6e 67 " + "2c 69 33 32 29 2d 3e 28 69 33 32 29 18 1c 18 77 "; /* name: xI32Params, type: + (string,i32)->(i32) */ THINGSET_ASSERT_REQUEST_HEX(req_hex, rsp_exp_hex); } diff --git a/tests/protocol/src/txt.c b/tests/protocol/src/txt.c index 7b2b647..eb50dd6 100644 --- a/tests/protocol/src/txt.c +++ b/tests/protocol/src/txt.c @@ -241,7 +241,7 @@ ZTEST(thingset_txt, test_fetch_float_array) ZTEST(thingset_txt, test_fetch_metadata) { THINGSET_ASSERT_REQUEST_TXT("?_Metadata [\"Arrays/wF32\"]", - ":85 [{\"name\":\"wF32\",\"type\":\"f32[]\"}]"); + ":85 [{\"name\":\"wF32\",\"type\":\"f32[]\",\"access\":119}]"); } #endif /* CONFIG_THINGSET_METADATA_ENDPOINT */