Skip to content

Commit ef032dc

Browse files
committed
Binary mode: Add support for importing reports
1 parent 68c7544 commit ef032dc

File tree

6 files changed

+156
-0
lines changed

6 files changed

+156
-0
lines changed

include/thingset.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1822,6 +1822,25 @@ struct thingset_data_object *thingset_iterate_subsets(struct thingset_context *t
18221822
int thingset_import_data(struct thingset_context *ts, const uint8_t *data, size_t len,
18231823
uint8_t auth_flags, enum thingset_data_format format);
18241824

1825+
/**
1826+
* Import report into data objects.
1827+
*
1828+
* This function allows importing data objects from a received report.
1829+
*
1830+
* Unknown data items are silently ignored.
1831+
*
1832+
* @param ts Pointer to ThingSet context.
1833+
* @param data Buffer containing ID/value map that should be written to the data objects
1834+
* @param len Length of the data in the buffer
1835+
* @param auth_flags Authentication flags to be used in this function (to override auth_flags)
1836+
* @param format Protocol data format to be used (text, binary with IDs or binary with names)
1837+
* @param subset The subset associated with the published report
1838+
*
1839+
* @returns 0 for success or negative ThingSet response code in case of error
1840+
*/
1841+
int thingset_import_report(struct thingset_context *ts, const uint8_t *data, size_t len,
1842+
uint8_t auth_flags, enum thingset_data_format format, uint16_t subset);
1843+
18251844
/**
18261845
* EXPERIMENTAL
18271846
*

src/thingset.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,41 @@ int thingset_import_data(struct thingset_context *ts, const uint8_t *data, size_
349349
return err;
350350
}
351351

352+
int thingset_import_report(struct thingset_context *ts, const uint8_t *data, size_t len,
353+
uint8_t auth_flags, enum thingset_data_format format, uint16_t subset)
354+
{
355+
int err;
356+
357+
if (k_sem_take(&ts->lock, K_MSEC(THINGSET_CONTEXT_LOCK_TIMEOUT_MS)) != 0) {
358+
LOG_ERR("ThingSet context lock timed out");
359+
return -THINGSET_ERR_INTERNAL_SERVER_ERR;
360+
}
361+
362+
ts->msg = data;
363+
ts->msg_len = len;
364+
ts->msg_pos = 0;
365+
ts->rsp = NULL;
366+
ts->rsp_size = 0;
367+
ts->rsp_pos = 0;
368+
369+
switch (format) {
370+
case THINGSET_BIN_IDS_VALUES:
371+
ts->endpoint.use_ids = true;
372+
thingset_bin_setup(ts, 0);
373+
ts->decoder->elem_count = 2;
374+
ts->msg_payload = data;
375+
err = thingset_bin_import_report(ts, auth_flags, subset);
376+
break;
377+
default:
378+
err = -THINGSET_ERR_NOT_IMPLEMENTED;
379+
break;
380+
}
381+
382+
k_sem_give(&ts->lock);
383+
384+
return err;
385+
}
386+
352387
static int deserialize_value_callback(struct thingset_context *ts,
353388
const struct thingset_data_object *item_offset)
354389
{

src/thingset_bin.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -931,6 +931,22 @@ int thingset_bin_import_data(struct thingset_context *ts, uint8_t auth_flags,
931931
return ts->api->deserialize_finish(ts);
932932
}
933933

934+
int thingset_bin_import_report(struct thingset_context *ts, uint8_t auth_flags, uint16_t subset)
935+
{
936+
uint32_t id = 0;
937+
938+
if (ts->msg_payload[0] != THINGSET_BIN_REPORT) {
939+
return -THINGSET_ERR_UNSUPPORTED_FORMAT;
940+
}
941+
942+
zcbor_uint32_decode(ts->decoder, &id);
943+
if (id != subset) {
944+
return -THINGSET_ERR_NOT_FOUND;
945+
}
946+
947+
return thingset_bin_import_data(ts, auth_flags, subset);
948+
}
949+
934950
int thingset_bin_process(struct thingset_context *ts)
935951
{
936952
int ret;

src/thingset_internal.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,8 @@ void thingset_bin_setup(struct thingset_context *ts, size_t buf_offset);
377377
int thingset_bin_import_data(struct thingset_context *ts, uint8_t auth_flags,
378378
enum thingset_data_format format);
379379

380+
int thingset_bin_import_report(struct thingset_context *ts, uint8_t auth_flags, uint16_t subset);
381+
380382
int thingset_bin_import_data_progressively(struct thingset_context *ts, uint8_t auth_flags,
381383
size_t size, uint32_t *last_id, size_t *consumed);
382384

tests/common/data.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,3 +423,23 @@ struct thingset_data_object data_objects[] = {
423423
};
424424

425425
size_t data_objects_size = ARRAY_SIZE(data_objects);
426+
427+
/*
428+
* Test case for importing reports with initial zero values.
429+
*/
430+
uint32_t report_timestamp = 0;
431+
bool report_b = false;
432+
int32_t report_nested_beginning = 0;
433+
float report_nested_obj2_item2 = 0.0;
434+
435+
struct thingset_data_object report_data_objects[] = {
436+
THINGSET_ITEM_UINT32(THINGSET_ID_ROOT, 0x10, "t_s", &report_timestamp, THINGSET_ANY_RW,
437+
SUBSET_LIVE),
438+
THINGSET_ITEM_BOOL(0x200, 0x201, "wBool", &report_b, THINGSET_ANY_RW, SUBSET_LIVE),
439+
THINGSET_ITEM_INT32(0x700, 0x701, "rBeginning", &report_nested_beginning, THINGSET_ANY_RW,
440+
SUBSET_LIVE),
441+
THINGSET_ITEM_FLOAT(0x706, 0x708, "rItem2_V", &report_nested_obj2_item2, 1, THINGSET_ANY_RW,
442+
SUBSET_LIVE),
443+
};
444+
445+
size_t report_data_objects_size = ARRAY_SIZE(report_data_objects);

tests/protocol/src/bin.c

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@
1313
#include "data.h"
1414
#include "test_utils.h"
1515

16+
extern struct thingset_data_object report_data_objects[];
17+
extern size_t report_data_objects_size;
18+
extern uint32_t report_timestamp;
19+
extern bool report_b;
20+
extern int32_t report_nested_beginning;
21+
extern float report_nested_obj2_item2;
22+
1623
static struct thingset_context ts;
1724

1825
ZTEST(thingset_bin, test_get_root_ids)
@@ -985,6 +992,63 @@ ZTEST(thingset_bin, test_import_data)
985992
b = true;
986993
}
987994

995+
ZTEST(thingset_bin, test_import_report)
996+
{
997+
uint8_t data[THINGSET_TEST_BUF_SIZE];
998+
999+
const char rpt_exp_hex[] =
1000+
"1F 19 08 00 A5 "
1001+
"10 19 03 E8 " /* t_s */
1002+
"19 02 01 F5 " /* Types/wBool */
1003+
"19 06 00 02 " /* Records: 2 */
1004+
"19 07 01 01 " /* Nested/rBeginning */
1005+
"19 07 08 FA 40 0C CC CD"; /* Nested/Obj2/rItem2_V */
1006+
1007+
struct thingset_context ts_local;
1008+
thingset_init(&ts_local, report_data_objects, report_data_objects_size);
1009+
1010+
int data_len = hex2bin_spaced(rpt_exp_hex, data, sizeof(data));
1011+
1012+
int status = thingset_import_report(&ts_local, data, data_len, THINGSET_WRITE_MASK,
1013+
THINGSET_BIN_IDS_VALUES, 0x800);
1014+
1015+
zassert_equal(status, 0);
1016+
zassert_equal(report_timestamp, 1000);
1017+
zassert_equal(report_b, true);
1018+
zassert_equal(report_nested_beginning, 1);
1019+
zassert_equal(report_nested_obj2_item2, 2.2F);
1020+
}
1021+
1022+
ZTEST(thingset_bin, test_import_report_error)
1023+
{
1024+
uint8_t data[THINGSET_TEST_BUF_SIZE];
1025+
1026+
const char rpt_exp_hex[] =
1027+
"1F 19 08 00 A5 "
1028+
"10 19 03 E8 " /* t_s */
1029+
"19 02 01 F5 " /* Types/wBool */
1030+
"19 06 00 02 " /* Records: 2 */
1031+
"19 07 01 01 " /* Nested/rBeginning */
1032+
"19 07 08 FA 40 0C CC CD"; /* Nested/Obj2/rItem2_V */
1033+
1034+
struct thingset_context ts_local;
1035+
thingset_init(&ts_local, report_data_objects, report_data_objects_size);
1036+
1037+
int data_len = hex2bin_spaced(rpt_exp_hex, data, sizeof(data));
1038+
1039+
/* no report */
1040+
data[0] = 0x00;
1041+
int status = thingset_import_report(&ts_local, data, data_len, THINGSET_WRITE_MASK,
1042+
THINGSET_BIN_IDS_VALUES, 0x800);
1043+
zassert_equal(status, -THINGSET_ERR_UNSUPPORTED_FORMAT);
1044+
1045+
/* wrong subset */
1046+
data[0] = 0x1f;
1047+
status = thingset_import_report(&ts_local, data, data_len, THINGSET_WRITE_MASK,
1048+
THINGSET_BIN_IDS_VALUES, 0x0);
1049+
zassert_equal(status, -THINGSET_ERR_NOT_FOUND);
1050+
}
1051+
9881052
ZTEST(thingset_bin, test_import_record)
9891053
{
9901054
struct thingset_endpoint endpoint;

0 commit comments

Comments
 (0)