Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 20 additions & 10 deletions src/thingset_bin.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
#include <stdio.h>
#include <string.h>

#define THINGSET_METADATA_KEY_NAME 0x1A /**< `name` key in metadata overlay */
#define THINGSET_METADATA_KEY_TYPE 0x1B /**< `type` key in metadata overlay */
#define THINGSET_METADATA_KEY_ACCESS 0x1C /**< `access` key in metadata overlay */

static void bin_decoder_init(struct thingset_context *ts, const uint8_t *payload,
size_t payload_len)
{
Expand Down Expand Up @@ -181,17 +185,15 @@ 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_METADATA_KEY_NAME)) {
return -THINGSET_ERR_RESPONSE_TOO_LARGE;
}

if (!zcbor_tstr_encode_ptr(ts->encoder, object->name, strlen(object->name))) {
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_METADATA_KEY_TYPE)) {
return -THINGSET_ERR_RESPONSE_TOO_LARGE;
}

Expand All @@ -204,6 +206,13 @@ static int bin_serialize_metadata(struct thingset_context *ts,
return -THINGSET_ERR_RESPONSE_TOO_LARGE;
}

if (!zcbor_uint32_put(ts->encoder, THINGSET_METADATA_KEY_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;
}
Expand Down Expand Up @@ -710,10 +719,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;
}
Expand Down Expand Up @@ -749,10 +759,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);
Expand Down
11 changes: 11 additions & 0 deletions src/thingset_txt.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
22 changes: 12 additions & 10 deletions tests/protocol/src/bin.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/protocol/src/txt.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down