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 cb85544 commit f2e207f
Show file tree
Hide file tree
Showing 23 changed files with 68 additions and 41 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
include `block_buffer_len` for the actual length of data and
`negotiated_block_size` to indicate the maximum block size (may be
used along with 'block_idx' to calculate a byte offset).
- Asynchronous callbacks now return error information for both the
Golioth Firmware SDK and the CoAP response code. When response->status
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. The order of the path and
response has been reversed in all async callbacks to differentiate the
new error reporting behavior.

### Fixed:

- User callbacks now run when requests are cancelled.

### Removed:

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
27 changes: 21 additions & 6 deletions include/golioth/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,15 +146,20 @@ typedef void (*golioth_client_event_cb_fn)(struct golioth_client *client,
/// The callback function should check response->status to determine which case it is (response
/// received or timeout).
///
/// When response->status is GOLIOTH_OK or GOLIOTH_ERR_COAP_RESPONSE_CODE, the
/// response->status_class and response->status_code may be read to determine the CoAP response code
/// received. All other response->status codes indicate an SDK error code and the CoAP status
/// 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 @@ -165,16 +170,21 @@ typedef void (*golioth_get_cb_fn)(struct golioth_client *client,
/// response never received). The callback function should check response->status to determine which
/// case it is (response received or timeout).
///
/// When response->status is GOLIOTH_OK or GOLIOTH_ERR_COAP_RESPONSE_CODE, the
/// response->status_class and response->status_code may be read to determine the CoAP response code
/// received. All other response->status codes indicate an SDK error code and the CoAP status
/// 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 @@ -188,13 +198,18 @@ typedef void (*golioth_get_block_cb_fn)(struct golioth_client *client,
/// received or timeout). If response->status is GOLIOTH_OK, then the set or delete request
/// was successful.
///
/// When response->status is GOLIOTH_OK or GOLIOTH_ERR_COAP_RESPONSE_CODE, the
/// response->status_class and response->status_code may be read to determine the CoAP response code
/// received. All other response->status codes indicate an SDK error code and the CoAP status
/// 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;
}

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

/* 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
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
2 changes: 1 addition & 1 deletion src/fw_update.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ static enum golioth_status golioth_fw_update_report_state_sync(struct golioth_cl
}

static void on_ota_manifest(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 src/lightdb_state.c
Original file line number Diff line number Diff line change
Expand Up @@ -312,8 +312,8 @@ enum golioth_status golioth_lightdb_set_sync(struct golioth_client *client,
}

static void on_payload(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 src/ota.c
Original file line number Diff line number Diff line change
Expand Up @@ -299,8 +299,8 @@ enum golioth_status golioth_ota_payload_as_manifest(const uint8_t *payload,
}

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
2 changes: 1 addition & 1 deletion src/rpc.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ static int params_decode(zcbor_state_t *zsd, void *value)
}

static void on_rpc(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 src/settings.c
Original file line number Diff line number Diff line change
Expand Up @@ -364,8 +364,8 @@ static int settings_decode(zcbor_state_t *zsd, void *value)
}

static void on_settings(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
Loading

0 comments on commit f2e207f

Please sign in to comment.