Skip to content

Commit

Permalink
change async callback syntax to indicate new error reporting behavior
Browse files Browse the repository at this point in the history
Make a breaking change in the order of params for async callbacks to make
it clear that something fundamental has changed in the callback.

This, along with documentation in Doxygen and CHANGELOG is meant to raise
user attention to new behavior in the golioth_response status usage.

Signed-off-by: Mike Szczys <[email protected]>
  • Loading branch information
szczys committed Nov 13, 2024
1 parent 033b19d commit 2b12954
Show file tree
Hide file tree
Showing 25 changed files with 67 additions and 65 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
is GOLIOTH_OK or GOLIOTH_ERR_COAP_REPONSE_CODE, read the
response->status_class and response->status_code for the CoAP response
code. All other response->status codes indicate an SDK error and the
CoAP response codes are invalid/undefined.
CoAP response codes are invalid/undefined. The order of the path and
response has been reversed in all async callbacks to differentiate the
new error reporting behavior.

### Fixed:

Expand Down
4 changes: 2 additions & 2 deletions examples/common/golioth_basics.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ static enum golioth_rpc_status on_multiply(zcbor_state_t *request_params_array,

// Callback function for asynchronous get request of LightDB path "my_int"
static void on_get_my_int(struct golioth_client *client,
const struct golioth_response *response,
const char *path,
const struct golioth_response *response,
const uint8_t *payload,
size_t payload_size,
void *arg)
Expand All @@ -105,8 +105,8 @@ static void on_get_my_int(struct golioth_client *client,

// Callback function for asynchronous observation of LightDB path "desired/my_config"
static void on_my_config(struct golioth_client *client,
const struct golioth_response *response,
const char *path,
const struct golioth_response *response,
const uint8_t *payload,
size_t payload_size,
void *arg)
Expand Down
2 changes: 1 addition & 1 deletion examples/esp_idf/lightdb/delete/main/app_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ static void on_client_event(struct golioth_client *client,
}

static void counter_handler(struct golioth_client *client,
const struct golioth_response *response,
const char *path,
const struct golioth_response *response,
void *arg)
{
if (response->status != GOLIOTH_OK)
Expand Down
4 changes: 2 additions & 2 deletions examples/esp_idf/lightdb/get/main/app_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ static void on_client_event(struct golioth_client *client,
}

static void counter_get_handler(struct golioth_client *client,
const struct golioth_response *response,
const char *path,
const struct golioth_response *response,
const uint8_t *payload,
size_t payload_size,
void *arg)
Expand Down Expand Up @@ -103,8 +103,8 @@ static void counter_get_json_sync(struct golioth_client *client)
}

static void counter_get_cbor_handler(struct golioth_client *client,
const struct golioth_response *response,
const char *path,
const struct golioth_response *response,
const uint8_t *payload,
size_t payload_size,
void *arg)
Expand Down
2 changes: 1 addition & 1 deletion examples/esp_idf/lightdb/observe/main/app_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ static void on_client_event(struct golioth_client *client,
}

static void counter_observe_handler(struct golioth_client *client,
const struct golioth_response *response,
const char *path,
const struct golioth_response *response,
const uint8_t *payload,
size_t payload_size,
void *arg)
Expand Down
2 changes: 1 addition & 1 deletion examples/esp_idf/lightdb/set/main/app_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ static void on_client_event(struct golioth_client *client,
}

static void counter_set_handler(struct golioth_client *client,
const struct golioth_response *response,
const char *path,
const struct golioth_response *response,
void *arg)
{
if (response->status != GOLIOTH_OK)
Expand Down
2 changes: 1 addition & 1 deletion examples/esp_idf/stream/main/app_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ static float get_temperature()
}

static void temperature_push_handler(struct golioth_client *client,
const struct golioth_response *response,
const char *path,
const struct golioth_response *response,
void *arg)
{
if (response->status != GOLIOTH_OK)
Expand Down
2 changes: 1 addition & 1 deletion examples/zephyr/lightdb/delete/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ static void on_client_event(struct golioth_client *client,
}

static void counter_handler(struct golioth_client *client,
const struct golioth_response *response,
const char *path,
const struct golioth_response *response,
void *arg)
{
if (response->status != GOLIOTH_OK)
Expand Down
4 changes: 2 additions & 2 deletions examples/zephyr/lightdb/get/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ static void on_client_event(struct golioth_client *client,
}

static void counter_get_handler(struct golioth_client *client,
const struct golioth_response *response,
const char *path,
const struct golioth_response *response,
const uint8_t *payload,
size_t payload_size,
void *arg)
Expand Down Expand Up @@ -103,8 +103,8 @@ static void counter_get_json_sync(struct golioth_client *client)
}

static void counter_get_cbor_handler(struct golioth_client *client,
const struct golioth_response *response,
const char *path,
const struct golioth_response *response,
const uint8_t *payload,
size_t payload_size,
void *arg)
Expand Down
2 changes: 1 addition & 1 deletion examples/zephyr/lightdb/observe/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ static void on_client_event(struct golioth_client *client,
}

static void counter_observe_handler(struct golioth_client *client,
const struct golioth_response *response,
const char *path,
const struct golioth_response *response,
const uint8_t *payload,
size_t payload_size,
void *arg)
Expand Down
2 changes: 1 addition & 1 deletion examples/zephyr/lightdb/set/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ static void on_client_event(struct golioth_client *client,
}

static void counter_set_handler(struct golioth_client *client,
const struct golioth_response *response,
const char *path,
const struct golioth_response *response,
void *arg)
{
if (response->status != GOLIOTH_OK)
Expand Down
2 changes: 1 addition & 1 deletion examples/zephyr/stream/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ static int get_temperature(struct sensor_value *val)
#endif

static void temperature_async_push_handler(struct golioth_client *client,
const struct golioth_response *response,
const char *path,
const struct golioth_response *response,
void *arg)
{
if (response->status != GOLIOTH_OK)
Expand Down
12 changes: 6 additions & 6 deletions include/golioth/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,14 +152,14 @@ typedef void (*golioth_client_event_cb_fn)(struct golioth_client *client,
/// members are invalid/undefined.
///
/// @param client The client handle from the original request.
/// @param response Response status and class/code
/// @param path The path from the original request
/// @param response Response status and class/code
/// @param payload The application layer payload in the response packet. Can be NULL.
/// @param payload_size The size of payload, in bytes
/// @param arg User argument, copied from the original request. Can be NULL.
typedef void (*golioth_get_cb_fn)(struct golioth_client *client,
const struct golioth_response *response,
const char *path,
const struct golioth_response *response,
const uint8_t *payload,
size_t payload_size,
void *arg);
Expand All @@ -176,15 +176,15 @@ typedef void (*golioth_get_cb_fn)(struct golioth_client *client,
/// members are invalid/undefined.
///
/// @param client The client handle from the original request.
/// @param response Response status and class/code
/// @param path The path from the original request
/// @param response Response status and class/code
/// @param payload The application layer payload in the response packet. Can be NULL.
/// @param payload_size The size of payload, in bytes
/// @param is_last True if this is the final block of the get request
/// @param arg User argument, copied from the original request. Can be NULL.
typedef void (*golioth_get_block_cb_fn)(struct golioth_client *client,
const struct golioth_response *response,
const char *path,
const struct golioth_response *response,
const uint8_t *payload,
size_t payload_size,
bool is_last,
Expand All @@ -204,12 +204,12 @@ typedef void (*golioth_get_block_cb_fn)(struct golioth_client *client,
/// members are invalid/undefined.
///
/// @param client The client handle from the original request.
/// @param response Response status and class/code
/// @param path The path from the original request
/// @param response Response status and class/code
/// @param arg User argument, copied from the original request. Can be NULL.
typedef void (*golioth_set_cb_fn)(struct golioth_client *client,
const struct golioth_response *response,
const char *path,
const struct golioth_response *response,
void *arg);

/// Create a Golioth client
Expand Down
6 changes: 3 additions & 3 deletions src/coap_blockwise.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ static void blockwise_upload_init(struct blockwise_transfer *ctx,

// Blockwise upload's internal callback function that the COAP client calls
static void on_block_sent(struct golioth_client *client,
const struct golioth_response *response,
const char *path,
const struct golioth_response *response,
size_t block_szx,
void *arg)
{
Expand Down Expand Up @@ -251,7 +251,7 @@ enum golioth_status golioth_blockwise_post(struct golioth_client *client,
ctx->response.status = status;

Check warning on line 251 in src/coap_blockwise.c

View check run for this annotation

Codecov / codecov/patch

src/coap_blockwise.c#L251

Added line #L251 was not covered by tests
}

set_cb(client, &ctx->response, path, callback_arg);
set_cb(client, path, &ctx->response, callback_arg);

Check warning on line 254 in src/coap_blockwise.c

View check run for this annotation

Codecov / codecov/patch

src/coap_blockwise.c#L254

Added line #L254 was not covered by tests
}

/* Upload complete, clean up allocated resources */
Expand Down Expand Up @@ -314,8 +314,8 @@ static enum golioth_status call_write_block_callback(struct blockwise_transfer *

// Blockwise download's internal callback function that the COAP client calls
static void on_block_rcvd(struct golioth_client *client,
const struct golioth_response *response,
const char *path,
const struct golioth_response *response,
const uint8_t *payload,
size_t payload_size,
bool is_last,
Expand Down
4 changes: 2 additions & 2 deletions src/coap_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@
/// Callback function type for blockwise uploads that also returns the blocksize in szx format
///
/// @param client The client handle from the original request.
/// @param response Response status and class/code
/// @param path The path from the original request
/// @param response Response status and class/code
/// @param block_szx The block size from the server in coap SZX format
/// @param arg User argument, copied from the original request. Can be NULL.
typedef void (*golioth_set_block_cb_fn)(struct golioth_client *client,
const struct golioth_response *response,
const char *path,
const struct golioth_response *response,
size_t block_szx,
void *arg);

Expand Down
22 changes: 11 additions & 11 deletions src/coap_client_libcoap.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ static void notify_observers(const coap_pdu_t *received,
&& (0 == memcmp(rcvd_token.s, obs_info->req.token, obs_info->req.token_len)))
{
callback(client,
response,
obs_info->req.path,
response,
data,
data_len,
obs_info->req.observe.arg);
Expand Down Expand Up @@ -158,7 +158,7 @@ static coap_response_t coap_response_handler(coap_session_t *session,
{
if (req->get.callback)
{
req->get.callback(client, &response, req->path, data, data_len, req->get.arg);
req->get.callback(client, req->path, &response, data, data_len, req->get.arg);
}
}
else if (req->type == GOLIOTH_COAP_REQUEST_GET_BLOCK)
Expand Down Expand Up @@ -186,8 +186,8 @@ static coap_response_t coap_response_handler(coap_session_t *session,
if (req->get_block.callback)
{
req->get_block.callback(client,
&response,
req->path,
&response,
data,
data_len,
is_last,
Expand All @@ -198,7 +198,7 @@ static coap_response_t coap_response_handler(coap_session_t *session,
{
if (req->post.callback)
{
req->post.callback(client, &response, req->path, req->post.arg);
req->post.callback(client, req->path, &response, req->post.arg);
}
}
else if (req->type == GOLIOTH_COAP_REQUEST_POST_BLOCK)
Expand All @@ -213,8 +213,8 @@ static coap_response_t coap_response_handler(coap_session_t *session,
if (req->post_block.callback)
{
req->post_block.callback(client,
&response,
req->path,
&response,
server_requested_szx,
req->post_block.arg);
}
Expand All @@ -223,7 +223,7 @@ static coap_response_t coap_response_handler(coap_session_t *session,
{
if (req->delete.callback)
{
req->delete.callback(client, &response, req->path, req->delete.arg);
req->delete.callback(client, req->path, &response, req->delete.arg);
}
}
}
Expand Down Expand Up @@ -1085,37 +1085,37 @@ static enum golioth_status coap_io_loop_once(struct golioth_client *client,
if (request_msg.type == GOLIOTH_COAP_REQUEST_GET && request_msg.get.callback)
{
request_msg.get
.callback(client, &response, request_msg.path, NULL, 0, request_msg.get.arg);
.callback(client, request_msg.path, &response, NULL, 0, request_msg.get.arg);

Check warning on line 1088 in src/coap_client_libcoap.c

View check run for this annotation

Codecov / codecov/patch

src/coap_client_libcoap.c#L1088

Added line #L1088 was not covered by tests
}
else if (request_msg.type == GOLIOTH_COAP_REQUEST_GET_BLOCK
&& request_msg.get_block.callback)
{
request_msg.get_block.callback(client,
&response,
request_msg.path,
&response,
NULL,
0,
false,
request_msg.get_block.arg);
}
else if (request_msg.type == GOLIOTH_COAP_REQUEST_POST && request_msg.post.callback)
{
request_msg.post.callback(client, &response, request_msg.path, request_msg.post.arg);
request_msg.post.callback(client, request_msg.path, &response, request_msg.post.arg);

Check warning on line 1103 in src/coap_client_libcoap.c

View check run for this annotation

Codecov / codecov/patch

src/coap_client_libcoap.c#L1103

Added line #L1103 was not covered by tests
}
else if (request_msg.type == GOLIOTH_COAP_REQUEST_POST_BLOCK
&& request_msg.post_block.callback)
{
request_msg.post_block.callback(client,
&response,
request_msg.path,
&response,
request_msg.post_block.block_szx,
request_msg.post_block.arg);
}
else if (request_msg.type == GOLIOTH_COAP_REQUEST_DELETE && request_msg.delete.callback)
{
request_msg.delete.callback(client,
&response,
request_msg.path,
&response,
request_msg.delete.arg);
}

Expand Down
12 changes: 6 additions & 6 deletions src/coap_client_zephyr.c
Original file line number Diff line number Diff line change
Expand Up @@ -267,15 +267,15 @@ static int golioth_coap_cb(struct golioth_req_rsp *rsp)
if (req->get.callback)
{
req->get
.callback(client, &rsp->codes, req->path, rsp->data, rsp->len, req->get.arg);
.callback(client, req->path, &rsp->codes, rsp->data, rsp->len, req->get.arg);
}
break;
case GOLIOTH_COAP_REQUEST_GET_BLOCK:
if (req->get_block.callback)
{
req->get_block.callback(client,
&rsp->codes,
req->path,
&rsp->codes,
rsp->data,
rsp->len,
rsp->is_last,
Expand All @@ -285,31 +285,31 @@ static int golioth_coap_cb(struct golioth_req_rsp *rsp)
case GOLIOTH_COAP_REQUEST_POST:
if (req->post.callback)
{
req->post.callback(client, &rsp->codes, req->path, req->post.arg);
req->post.callback(client, req->path, &rsp->codes, req->post.arg);
}
break;
case GOLIOTH_COAP_REQUEST_POST_BLOCK:
if (req->post_block.callback)
{
req->post_block.callback(client,
&rsp->codes,
req->path,
&rsp->codes,
req->post_block.block_szx,
req->post_block.arg);
}
break;
case GOLIOTH_COAP_REQUEST_DELETE:
if (req->delete.callback)
{
req->delete.callback(client, &rsp->codes, req->path, req->delete.arg);
req->delete.callback(client, req->path, &rsp->codes, req->delete.arg);
}
break;
case GOLIOTH_COAP_REQUEST_OBSERVE:
if (req->observe.callback)
{
req->observe.callback(client,
&rsp->codes,
req->path,
&rsp->codes,
rsp->data,
rsp->len,
req->observe.arg);
Expand Down
Loading

0 comments on commit 2b12954

Please sign in to comment.