Skip to content

Commit

Permalink
Add function to export subsets in chunks
Browse files Browse the repository at this point in the history
This is useful if large amounts of data should be exported using small
buffer sizes.
  • Loading branch information
martinjaeger committed Mar 5, 2024
1 parent 257c43b commit d128284
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 6 deletions.
29 changes: 27 additions & 2 deletions include/thingset.h
Original file line number Diff line number Diff line change
Expand Up @@ -1693,6 +1693,27 @@ void thingset_init_global(struct thingset_context *ts);
int thingset_process_message(struct thingset_context *ts, const uint8_t *msg, size_t msg_len,
uint8_t *rsp, size_t rsp_size);

/**
* Retrieve data for given subset(s) in chunks.
*
* Exports object data for the given subset to the supplied buffer in the specified format starting
* at the given object index. At present, only binary formats are supported.
*
* @param ts Pointer to ThingSet context.
* @param buf Pointer to the buffer where the data should be stored
* @param buf_size Size of the buffer, i.e. maximum allowed length of the data
* @param subsets Flags to select which subset(s) of data items should be exported
* @param format Protocol data format to be used (text, binary with IDs or binary with names)
* @param index Pointer to an integer which tracks the current object being exported
*
* @retval len Length of the written chunk.
* @retval 0 When export is complete.
* @retval err Negative ThingSet response code in case of error.
*/
int thingset_export_subsets_chunks(struct thingset_context *ts, uint8_t *buf, size_t buf_size,
uint16_t subsets, enum thingset_data_format format,
uint16_t *index);

/**
* Retrieve data for given subset(s).
*
Expand All @@ -1710,8 +1731,12 @@ int thingset_process_message(struct thingset_context *ts, const uint8_t *msg, si
*
* @return Actual length of the data or negative ThingSet response code in case of error.
*/
int thingset_export_subsets(struct thingset_context *ts, uint8_t *buf, size_t buf_size,
uint16_t subsets, enum thingset_data_format format);
static inline int thingset_export_subsets(struct thingset_context *ts, uint8_t *buf,
size_t buf_size, uint16_t subsets,
enum thingset_data_format format)
{
return thingset_export_subsets_chunks(ts, buf, buf_size, subsets, format, NULL);
}

/**
* Export id, value and/or name of a single data item.
Expand Down
9 changes: 5 additions & 4 deletions src/thingset.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,9 @@ int thingset_process_message(struct thingset_context *ts, const uint8_t *msg, si
return ret;
}

int thingset_export_subsets(struct thingset_context *ts, uint8_t *buf, size_t buf_size,
uint16_t subsets, enum thingset_data_format format)
int thingset_export_subsets_chunks(struct thingset_context *ts, uint8_t *buf, size_t buf_size,
uint16_t subsets, enum thingset_data_format format,
uint16_t *index)
{
int ret;

Expand All @@ -156,11 +157,11 @@ int thingset_export_subsets(struct thingset_context *ts, uint8_t *buf, size_t bu
return -THINGSET_ERR_NOT_IMPLEMENTED;
}

ret = ts->api->serialize_subsets(ts, subsets, NULL);
ret = ts->api->serialize_subsets(ts, subsets, index);

ts->api->serialize_finish(ts);

if (ret == 0) {
if (ret == 0 || ret == 1) {
ret = ts->rsp_pos;
}

Expand Down
34 changes: 34 additions & 0 deletions tests/protocol/src/bin.c
Original file line number Diff line number Diff line change
Expand Up @@ -876,6 +876,40 @@ ZTEST(thingset_bin, test_export_item)
zassert_mem_equal(buf_exp, buf_act, 2);
}

ZTEST(thingset_bin, test_export_subsets_chunks)
{
uint8_t buf_chunk[16];
uint16_t index = 0;
int ret;

const char chunk1_exp_hex[] =
"A3 "
"10 19 03 E8 " /* t_s */
"19 02 01 F5 " /* Types/wBool */
"19 06 00 02"; /* Records: 2 */
uint8_t chunk1_exp[THINGSET_TEST_BUF_SIZE];
int chunk1_exp_len = hex2bin_spaced(chunk1_exp_hex, chunk1_exp, sizeof(chunk1_exp));

const char chunk2_exp_hex[] =
"A2 "
"19 07 01 01 " /* Nested/rBeginning */
"19 07 08 FA 40 0C CC CD"; /* Nested/Obj2/rItem2_V */
uint8_t chunk2_exp[THINGSET_TEST_BUF_SIZE];
int chunk2_exp_len = hex2bin_spaced(chunk2_exp_hex, chunk2_exp, sizeof(chunk2_exp));

ret = thingset_export_subsets_chunks(&ts, buf_chunk, sizeof(buf_chunk), SUBSET_LIVE,
THINGSET_BIN_IDS_VALUES, &index);
zassert_equal(ret, chunk1_exp_len);
zassert_not_equal(index, 0);
zassert_mem_equal(chunk1_exp, buf_chunk, chunk1_exp_len);

ret = thingset_export_subsets_chunks(&ts, buf_chunk, sizeof(buf_chunk), SUBSET_LIVE,
THINGSET_BIN_IDS_VALUES, &index);
zassert_equal(ret, chunk2_exp_len);
zassert_equal(index, 0);
zassert_mem_equal(chunk2_exp, buf_chunk, chunk2_exp_len);
}

ZTEST(thingset_bin, test_iterate_subsets)
{
struct thingset_data_object *obj = NULL;
Expand Down

0 comments on commit d128284

Please sign in to comment.