From cafa37c8219a37302fd4233e0c9e16260ffdf6c3 Mon Sep 17 00:00:00 2001 From: Ethan Duckett Date: Thu, 26 Sep 2024 15:13:04 +0000 Subject: [PATCH 1/2] Rework callback mechanism --- include/thingset.h | 4 ++-- src/thingset_common.c | 10 ++++++++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/include/thingset.h b/include/thingset.h index f6eae95..67a53f6 100644 --- a/include/thingset.h +++ b/include/thingset.h @@ -1365,10 +1365,10 @@ enum thingset_callback_reason }; /** 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); /** 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); /** @cond INTERNAL_HIDDEN */ diff --git a/src/thingset_common.c b/src/thingset_common.c index 30cdc97..409dd07 100644 --- a/src/thingset_common.c +++ b/src/thingset_common.c @@ -346,7 +346,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 ret = ts->endpoint.object->data.group_callback(THINGSET_CALLBACK_PRE_WRITE); + if (ret < 0) { + return ts->api->serialize_response(ts, THINGSET_ERR_BAD_REQUEST, "Invalid parameters"); + } } /* actually write data */ @@ -364,7 +367,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 ret = ts->endpoint.object->data.group_callback(THINGSET_CALLBACK_POST_WRITE); + if (ret < 0) { + return ts->api->serialize_response(ts, THINGSET_ERR_BAD_REQUEST, "Invalid parameters"); + } } /* From 959f7a091971f665130f5a21ddb4530ef4839d51 Mon Sep 17 00:00:00 2001 From: Ethan Duckett Date: Wed, 2 Oct 2024 13:00:26 +0000 Subject: [PATCH 2/2] Add error code to error response --- src/thingset_common.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/thingset_common.c b/src/thingset_common.c index 409dd07..8bd089d 100644 --- a/src/thingset_common.c +++ b/src/thingset_common.c @@ -348,7 +348,7 @@ int thingset_common_update(struct thingset_context *ts) if (ts->endpoint.object->data.group_callback != NULL) { int ret = ts->endpoint.object->data.group_callback(THINGSET_CALLBACK_PRE_WRITE); if (ret < 0) { - return ts->api->serialize_response(ts, THINGSET_ERR_BAD_REQUEST, "Invalid parameters"); + return ts->api->serialize_response(ts, THINGSET_ERR_BAD_REQUEST, "Callback error %i", ret); } } @@ -369,7 +369,7 @@ int thingset_common_update(struct thingset_context *ts) if (ts->endpoint.object->data.group_callback != NULL) { int ret = ts->endpoint.object->data.group_callback(THINGSET_CALLBACK_POST_WRITE); if (ret < 0) { - return ts->api->serialize_response(ts, THINGSET_ERR_BAD_REQUEST, "Invalid parameters"); + return ts->api->serialize_response(ts, THINGSET_ERR_BAD_REQUEST, "Callback error %i", ret); } }