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
8 changes: 6 additions & 2 deletions include/thingset.h
Original file line number Diff line number Diff line change
Expand Up @@ -1364,11 +1364,15 @@ enum thingset_callback_reason
THINGSET_CALLBACK_POST_WRITE, /**< Function was called after deserializing data of the group */
};

struct thingset_data_object;

/** Function to be called before/after read/write operations to groups. */
typedef void (*thingset_group_callback_t)(enum thingset_callback_reason cb_reason);
typedef int (*thingset_group_callback_t)(enum thingset_callback_reason cb_reason,
const struct thingset_data_object *obj);

/** Function to be called before/after read/write operations to records. */
typedef void (*thingset_records_callback_t)(enum thingset_callback_reason cb_reason, int index);
typedef int (*thingset_records_callback_t)(enum thingset_callback_reason cb_reason, int index,
const struct thingset_data_object *obj);

/** @cond INTERNAL_HIDDEN */

Expand Down
32 changes: 20 additions & 12 deletions src/thingset_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ int thingset_common_serialize_group(struct thingset_context *ts,
}

if (object->data.group_callback != NULL) {
object->data.group_callback(THINGSET_CALLBACK_PRE_READ);
object->data.group_callback(THINGSET_CALLBACK_PRE_READ, object);
}

for (unsigned int i = 0; i < ts->num_objects; i++) {
Expand All @@ -42,7 +42,7 @@ int thingset_common_serialize_group(struct thingset_context *ts,
}

if (object->data.group_callback != NULL) {
object->data.group_callback(THINGSET_CALLBACK_POST_READ);
object->data.group_callback(THINGSET_CALLBACK_POST_READ, object);
}

return ts->api->serialize_map_end(ts);
Expand Down Expand Up @@ -128,7 +128,7 @@ int thingset_common_serialize_record(struct thingset_context *ts,
}

if (records->callback != NULL) {
records->callback(THINGSET_CALLBACK_PRE_READ, record_index);
records->callback(THINGSET_CALLBACK_PRE_READ, record_index, object);
}

const struct thingset_data_object *item = thingset_get_object_by_id(ts, object->id) + 1;
Expand All @@ -151,7 +151,7 @@ int thingset_common_serialize_record(struct thingset_context *ts,
}

if (records->callback != NULL) {
records->callback(THINGSET_CALLBACK_POST_READ, record_index);
records->callback(THINGSET_CALLBACK_POST_READ, record_index, object);
}

return ts->api->serialize_map_end(ts);
Expand Down Expand Up @@ -184,13 +184,13 @@ int thingset_common_get(struct thingset_context *ts)
parent = thingset_get_object_by_id(ts, ts->endpoint.object->parent_id);

if (parent != NULL && parent->data.group_callback != NULL) {
parent->data.group_callback(THINGSET_CALLBACK_PRE_READ);
parent->data.group_callback(THINGSET_CALLBACK_PRE_READ, ts->endpoint.object);
}

err = ts->api->serialize_value(ts, ts->endpoint.object);

if (parent != NULL && parent->data.group_callback != NULL) {
parent->data.group_callback(THINGSET_CALLBACK_POST_READ);
parent->data.group_callback(THINGSET_CALLBACK_POST_READ, ts->endpoint.object);
}
break;
}
Expand Down Expand Up @@ -232,14 +232,16 @@ int thingset_common_fetch(struct thingset_context *ts)
}

/* fetch values */
if (ts->endpoint.object->data.group_callback != NULL) {
ts->endpoint.object->data.group_callback(THINGSET_CALLBACK_PRE_READ);
}

const struct thingset_data_object *object;
while ((err = ts->api->deserialize_child(ts, &object))
!= -THINGSET_ERR_DESERIALIZATION_FINISHED)
{

if (ts->endpoint.object->data.group_callback != NULL) {
ts->endpoint.object->data.group_callback(THINGSET_CALLBACK_PRE_READ, object);
}

if (err != 0) {
return ts->api->serialize_response(ts, -err, NULL);
}
Expand Down Expand Up @@ -281,7 +283,7 @@ int thingset_common_fetch(struct thingset_context *ts)
}

if (ts->endpoint.object->data.group_callback != NULL) {
ts->endpoint.object->data.group_callback(THINGSET_CALLBACK_POST_READ);
ts->endpoint.object->data.group_callback(THINGSET_CALLBACK_POST_READ, object);
}
}
else {
Expand Down Expand Up @@ -346,7 +348,10 @@ int thingset_common_update(struct thingset_context *ts)
ts->api->deserialize_map_start(ts);

if (ts->endpoint.object->data.group_callback != NULL) {
ts->endpoint.object->data.group_callback(THINGSET_CALLBACK_PRE_WRITE);
int err = ts->endpoint.object->data.group_callback(THINGSET_CALLBACK_PRE_WRITE, object);
if (err < 0) {
return ts->api->serialize_response(ts, -err, NULL);
}
}

/* actually write data */
Expand All @@ -364,7 +369,10 @@ int thingset_common_update(struct thingset_context *ts)
}

if (ts->endpoint.object->data.group_callback != NULL) {
ts->endpoint.object->data.group_callback(THINGSET_CALLBACK_POST_WRITE);
int err = ts->endpoint.object->data.group_callback(THINGSET_CALLBACK_POST_WRITE, object);
if (err < 0) {
return ts->api->serialize_response(ts, -err, NULL);
}
}

/*
Expand Down
10 changes: 7 additions & 3 deletions tests/common/data.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ int group_callback_pre_read_count;
int group_callback_post_read_count;
int group_callback_pre_write_count;
int group_callback_post_write_count;
static void group_callback(enum thingset_callback_reason reason)
static int group_callback(enum thingset_callback_reason reason,
const struct thingset_data_object *obj)
{
switch (reason) {
case THINGSET_CALLBACK_PRE_READ:
Expand All @@ -119,6 +120,7 @@ static void group_callback(enum thingset_callback_reason reason)
group_callback_post_write_count++;
break;
}
return 0;
}

/* Records */
Expand Down Expand Up @@ -164,7 +166,8 @@ static struct test_dyn_struct dyn_records = {
int dyn_records_callback_pre_read_count;
int dyn_records_callback_post_read_count;
int dyn_records_callback_index;
static void dyn_records_callback(enum thingset_callback_reason reason, int index)
static int dyn_records_callback(enum thingset_callback_reason reason, int index,
const struct thingset_data_object *object)
{
switch (reason) {
case THINGSET_CALLBACK_PRE_READ:
Expand All @@ -180,9 +183,10 @@ static void dyn_records_callback(enum thingset_callback_reason reason, int index
dyn_records_callback_post_read_count++;
break;
default:
return;
return 0;
}
dyn_records_callback_index = index;
return 0;
}

THINGSET_DEFINE_DYN_RECORDS(dyn_records_obj, &dyn_records, 10, dyn_records_callback);
Expand Down