Skip to content

Commit c252e84

Browse files
committed
add access to metadata endpoint and use IDs in binary mode to reduce size
1 parent 6033e55 commit c252e84

File tree

5 files changed

+50
-28
lines changed

5 files changed

+50
-28
lines changed

include/thingset.h

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,16 @@ extern "C" {
8888
(code >= 0x80 && code < 0xA0) /**< Check if provided code indicates success. */
8989

9090
/* Reserved data object IDs */
91-
#define THINGSET_ID_ROOT 0x00 /**< Root object for a node (empty path) */
92-
#define THINGSET_ID_TIME 0x10 /**< Unix timestamp `t_s` */
93-
#define THINGSET_ID_IDS 0x16 /**< `_Ids` overlay */
94-
#define THINGSET_ID_PATHS 0x17 /**< `_Paths` overlay */
95-
#define THINGSET_ID_METADATAURL 0x18 /**< URL for extended metadata information: `cMetadataURL` */
96-
#define THINGSET_ID_METADATA 0x19 /**< `_Metadata` overlay */
97-
#define THINGSET_ID_NODEID 0x1D /**< String containing the node ID: `cNodeID` */
91+
#define THINGSET_ID_ROOT 0x00 /**< Root object for a node (empty path) */
92+
#define THINGSET_ID_TIME 0x10 /**< Unix timestamp `t_s` */
93+
#define THINGSET_ID_IDS 0x16 /**< `_Ids` overlay */
94+
#define THINGSET_ID_PATHS 0x17 /**< `_Paths` overlay */
95+
#define THINGSET_ID_METADATAURL 0x18 /**< URL for extended metadata information: `cMetadataURL` */
96+
#define THINGSET_ID_METADATA 0x19 /**< `_Metadata` overlay */
97+
#define THINGSET_ID_METADATA_NAME 0x1A /**< `name` key in metadata overlay */
98+
#define THINGSET_ID_METADATA_TYPE 0x1B /**< `type` key in metadata overlay */
99+
#define THINGSET_ID_METADATA_ACCESS 0x1C /**< `access` key in metadata overlay */
100+
#define THINGSET_ID_NODEID 0x1D /**< String containing the node ID: `cNodeID` */
98101

99102
/*
100103
* Macros for defining data object array elements.

src/thingset_bin.c

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -181,17 +181,15 @@ static int bin_serialize_metadata(struct thingset_context *ts,
181181
return err;
182182
}
183183

184-
const char name[] = "name";
185-
if (!zcbor_tstr_put_lit(ts->encoder, name)) {
184+
if (!zcbor_uint32_put(ts->encoder, THINGSET_ID_METADATA_NAME)) {
186185
return -THINGSET_ERR_RESPONSE_TOO_LARGE;
187186
}
188187

189188
if (!zcbor_tstr_encode_ptr(ts->encoder, object->name, strlen(object->name))) {
190189
return -THINGSET_ERR_RESPONSE_TOO_LARGE;
191190
}
192191

193-
const char type[] = "type";
194-
if (!zcbor_tstr_put_lit(ts->encoder, type)) {
192+
if (!zcbor_uint32_put(ts->encoder, THINGSET_ID_METADATA_TYPE)) {
195193
return -THINGSET_ERR_RESPONSE_TOO_LARGE;
196194
}
197195

@@ -204,6 +202,13 @@ static int bin_serialize_metadata(struct thingset_context *ts,
204202
return -THINGSET_ERR_RESPONSE_TOO_LARGE;
205203
}
206204

205+
if (!zcbor_uint32_put(ts->encoder, THINGSET_ID_METADATA_ACCESS)) {
206+
return -THINGSET_ERR_RESPONSE_TOO_LARGE;
207+
}
208+
if (!zcbor_uint32_put(ts->encoder, object->access)) {
209+
return -THINGSET_ERR_RESPONSE_TOO_LARGE;
210+
}
211+
207212
if ((err = bin_serialize_map_end(ts))) {
208213
return err;
209214
}
@@ -710,10 +715,11 @@ static int bin_deserialize_value(struct thingset_context *ts,
710715
int index = 0;
711716
do {
712717
/* using uint8_t pointer for byte-wise pointer arithmetics */
713-
union thingset_data_pointer data = { .u8 = array->elements.u8 + index * type_size };
718+
union thingset_data_pointer data = { .u8 = array->elements.u8
719+
+ index * type_size };
714720

715-
err = bin_deserialize_simple_value(ts, data, array->element_type, array->decimals,
716-
check_only);
721+
err = bin_deserialize_simple_value(ts, data, array->element_type,
722+
array->decimals, check_only);
717723
if (err != 0) {
718724
break;
719725
}
@@ -749,10 +755,10 @@ static int bin_deserialize_value(struct thingset_context *ts,
749755
continue;
750756
}
751757
union thingset_data_pointer data = { .u8 = ((uint8_t *)records->records)
752-
+ (i * records->record_size)
753-
+ element->data.offset };
758+
+ (i * records->record_size)
759+
+ element->data.offset };
754760
err = bin_deserialize_simple_value(ts, data, element->type, element->detail,
755-
check_only);
761+
check_only);
756762
}
757763

758764
success = zcbor_map_end_decode(ts->decoder);

src/thingset_txt.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,17 @@ static int txt_serialize_metadata(struct thingset_context *ts,
400400
return -THINGSET_ERR_RESPONSE_TOO_LARGE;
401401
}
402402

403+
if ((err = txt_serialize_string(ts, "access", true))) {
404+
return -THINGSET_ERR_RESPONSE_TOO_LARGE;
405+
}
406+
407+
len = snprintf(ts->rsp + ts->rsp_pos, ts->rsp_size - ts->rsp_pos, "%d", object->access);
408+
if (len < 0) {
409+
return -THINGSET_ERR_RESPONSE_TOO_LARGE;
410+
}
411+
412+
ts->rsp_pos += len;
413+
403414
if ((err = txt_serialize_map_end(ts))) {
404415
return err;
405416
}

tests/protocol/src/bin.c

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -401,12 +401,13 @@ ZTEST(thingset_bin, test_fetch_metadata)
401401

402402
const char rsp_exp_hex[] =
403403
"85 F6 "
404-
"84 " /* array with 4 elements */
405-
"a2 64 6e 61 6d 65 64 77 49 33 32 64 74 79 70 65 63 69 33 32 " /* name: wI32, type: i32
406-
*/
407-
"a2 64 6e 61 6d 65 63 74 5f 73 64 74 79 70 65 63 75 33 32 " /* name: t_s, type: u32 */
408-
"a2 64 6e 61 6d 65 63 74 5f 73 64 74 79 70 65 63 75 33 32 " /* name: t_s, type: u32 */
409-
"a2 64 6e 61 6d 65 64 4f 62 6a 31 64 74 79 70 65 65 67 72 6f 75 70"; /* name: Obj1,
404+
"84 " /* array with 4 elements */
405+
"a3 18 1a 64 77 49 33 32 18 1b 63 69 33 32 18 1c 18 77 " /* name: wI32, type: i32, access:
406+
* any
407+
*/
408+
"a3 18 1a 63 74 5f 73 18 1b 63 75 33 32 18 1c 0f " /* name: t_s, type: u32 */
409+
"a3 18 1a 63 74 5f 73 18 1b 63 75 33 32 18 1c 18 77 " /* name: t_s, type: u32 */
410+
"a3 18 1a 64 4f 62 6a 31 18 1b 65 67 72 6f 75 70 18 1c 0f "; /* name: Obj1,
410411
type: group */
411412

412413
THINGSET_ASSERT_REQUEST_HEX(req_hex, rsp_exp_hex);
@@ -424,7 +425,7 @@ ZTEST(thingset_bin, test_fetch_metadata_array)
424425
"85 F6 "
425426
"81 " /* array with 1 element */
426427
/* name: wU16, type: u16[] */
427-
"a2 64 6e 61 6d 65 64 77 55 31 36 64 74 79 70 65 65 75 31 36 5b 5d";
428+
"a3 18 1a 64 77 55 31 36 18 1b 65 75 31 36 5b 5d 18 1c 18 77 ";
428429

429430
THINGSET_ASSERT_REQUEST_HEX(req_hex, rsp_exp_hex);
430431
}
@@ -441,7 +442,7 @@ ZTEST(thingset_bin, test_fetch_metadata_void_func)
441442
"85 F6 "
442443
"81 " /* array with 1 element */
443444
/* name: xVoid, type: ()->() */
444-
"a2 64 6e 61 6d 65 65 78 56 6f 69 64 64 74 79 70 65 66 28 29 2d 3e 28 29 ";
445+
"a3 18 1a 65 78 56 6f 69 64 18 1b 66 28 29 2d 3e 28 29 18 1c 18 77 ";
445446

446447
THINGSET_ASSERT_REQUEST_HEX(req_hex, rsp_exp_hex);
447448
}
@@ -458,8 +459,9 @@ ZTEST(thingset_bin, test_fetch_metadata_parameterised_func)
458459
"85 F6 "
459460
"81 " /* array with 1 element */
460461
/* name: xVoid, type: ()->() */
461-
"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 "
462-
"2c 69 33 32 29 2d 3e 28 69 33 32 29"; /* name: xI32Params, type: (string,i32)->(i32) */
462+
"a3 18 1a 6a 78 49 33 32 50 61 72 61 6d 73 18 1b 73 28 73 74 72 69 6e 67 "
463+
"2c 69 33 32 29 2d 3e 28 69 33 32 29 18 1c 18 77 "; /* name: xI32Params, type:
464+
(string,i32)->(i32) */
463465

464466
THINGSET_ASSERT_REQUEST_HEX(req_hex, rsp_exp_hex);
465467
}

tests/protocol/src/txt.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ ZTEST(thingset_txt, test_fetch_float_array)
241241
ZTEST(thingset_txt, test_fetch_metadata)
242242
{
243243
THINGSET_ASSERT_REQUEST_TXT("?_Metadata [\"Arrays/wF32\"]",
244-
":85 [{\"name\":\"wF32\",\"type\":\"f32[]\"}]");
244+
":85 [{\"name\":\"wF32\",\"type\":\"f32[]\",\"access\":119}]");
245245
}
246246

247247
#endif /* CONFIG_THINGSET_METADATA_ENDPOINT */

0 commit comments

Comments
 (0)