Skip to content

Commit cbef867

Browse files
pdgendthenrikbrixandersen
authored andcommitted
net: lib: coap: Convert client response callback arguments to struct
Make it easier to modify the response callback data by passing it as a struct pointer rather than a long list of arguments. Signed-off-by: Pieter De Gendt <[email protected]>
1 parent c0a421c commit cbef867

File tree

5 files changed

+55
-33
lines changed

5 files changed

+55
-33
lines changed

doc/connectivity/networking/api/coap_client.rst

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,15 @@ The following is an example of a very simple response handling function:
6666

6767
.. code-block:: c
6868
69-
void response_cb(int16_t code, size_t offset, const uint8_t *payload, size_t len,
70-
bool last_block, void *user_data)
69+
void response_cb(const struct coap_client_response_data *data, void *user_data)
7170
{
72-
if (code >= 0) {
73-
LOG_INF("CoAP response from server %d", code);
74-
if (last_block) {
71+
if (data->result_code >= 0) {
72+
LOG_INF("CoAP response from server %d", data->result_code);
73+
if (data->last_block) {
7574
LOG_INF("Last packet received");
7675
}
7776
} else {
78-
LOG_ERR("Error in sending request %d", code);
77+
LOG_ERR("Error in sending request %d", data->result_code);
7978
}
8079
}
8180

include/zephyr/net/coap_client.h

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,25 @@ extern "C" {
3232
#define MAX_COAP_MSG_LEN (CONFIG_COAP_CLIENT_MESSAGE_HEADER_SIZE + \
3333
CONFIG_COAP_CLIENT_MESSAGE_SIZE)
3434

35+
/**
36+
* @brief Representation for CoAP client response data.
37+
*/
38+
struct coap_client_response_data {
39+
/**
40+
* Result code of the response. Negative if there was a failure in send.
41+
* @ref coap_response_code for positive.
42+
*/
43+
int16_t result_code;
44+
/** Payload offset from the beginning of a blockwise transfer. */
45+
size_t offset;
46+
/** Buffer containing the payload from the response. NULL for empty payload. */
47+
const uint8_t *payload;
48+
/** Size of the payload. */
49+
size_t payload_len;
50+
/** Indicates the last block of the response. */
51+
bool last_block;
52+
};
53+
3554
/**
3655
* @typedef coap_client_response_cb_t
3756
* @brief Callback for CoAP request.
@@ -41,17 +60,11 @@ extern "C" {
4160
* Blockwise transfers cause this callback to be called sequentially with increasing payload offset
4261
* and only partial content in buffer pointed by payload parameter.
4362
*
44-
* @param result_code Result code of the response. Negative if there was a failure in send.
45-
* @ref coap_response_code for positive.
46-
* @param offset Payload offset from the beginning of a blockwise transfer.
47-
* @param payload Buffer containing the payload from the response. NULL for empty payload.
48-
* @param len Size of the payload.
49-
* @param last_block Indicates the last block of the response.
63+
* @param data The CoAP response data.
5064
* @param user_data User provided context.
5165
*/
52-
typedef void (*coap_client_response_cb_t)(int16_t result_code,
53-
size_t offset, const uint8_t *payload, size_t len,
54-
bool last_block, void *user_data);
66+
typedef void (*coap_client_response_cb_t)(const struct coap_client_response_data *data,
67+
void *user_data);
5568

5669
/**
5770
* @brief Representation of a CoAP client request.

samples/net/sockets/coap_download/src/main.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,20 @@ static void net_event_handler(uint64_t mgmt_event, struct net_if *iface, void *i
3131

3232
NET_MGMT_REGISTER_EVENT_HANDLER(l4_conn_handler, NET_EVENT_L4_CONNECTED, net_event_handler, NULL);
3333

34-
static void on_coap_response(int16_t result_code, size_t offset, const uint8_t *payload, size_t len,
35-
bool last_block, void *user_data)
34+
static void on_coap_response(const struct coap_client_response_data *data, void *user_data)
3635
{
37-
LOG_INF("CoAP response, result_code=%d, offset=%u, len=%u", result_code, offset, len);
36+
LOG_INF("CoAP response, result_code=%d, offset=%u, len=%u", data->result_code, data->offset,
37+
data->payload_len);
3838

39-
if ((COAP_RESPONSE_CODE_CONTENT == result_code) && last_block) {
39+
if ((COAP_RESPONSE_CODE_CONTENT == data->result_code) && data->last_block) {
4040
int64_t elapsed_time = k_uptime_delta(&start_time);
41-
size_t total_size = offset + len;
41+
size_t total_size = data->offset + data->payload_len;
4242

4343
LOG_INF("CoAP download done, got %u bytes in %" PRId64 " ms", total_size,
4444
elapsed_time);
4545
k_sem_give(&coap_done_sem);
46-
} else if (COAP_RESPONSE_CODE_CONTENT != result_code) {
47-
LOG_ERR("Error during CoAP download, result_code=%d", result_code);
46+
} else if (COAP_RESPONSE_CODE_CONTENT != data->result_code) {
47+
LOG_ERR("Error during CoAP download, result_code=%d", data->result_code);
4848
k_sem_give(&coap_done_sem);
4949
}
5050
}

subsys/net/lib/coap/coap_client.c

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -459,9 +459,14 @@ int coap_client_req(struct coap_client *client, int sock, const struct sockaddr
459459

460460
static void report_callback_error(struct coap_client_internal_request *internal_req, int error_code)
461461
{
462-
if (internal_req->coap_request.cb) {
462+
if (internal_req->coap_request.cb != NULL) {
463463
if (!atomic_set(&internal_req->in_callback, 1)) {
464-
internal_req->coap_request.cb(error_code, 0, NULL, 0, true,
464+
const struct coap_client_response_data resp_data = {
465+
.result_code = error_code,
466+
.last_block = true,
467+
};
468+
469+
internal_req->coap_request.cb(&resp_data,
465470
internal_req->coap_request.user_data);
466471
atomic_clear(&internal_req->in_callback);
467472
} else {
@@ -947,10 +952,17 @@ static int handle_response(struct coap_client *client, const struct coap_packet
947952
}
948953

949954
/* Call user callback */
950-
if (internal_req->coap_request.cb) {
955+
if (internal_req->coap_request.cb != NULL) {
951956
if (!atomic_set(&internal_req->in_callback, 1)) {
952-
internal_req->coap_request.cb(response_code, internal_req->offset, payload,
953-
payload_len, last_block,
957+
const struct coap_client_response_data resp_data = {
958+
.result_code = response_code,
959+
.offset = internal_req->offset,
960+
.payload = payload,
961+
.payload_len = payload_len,
962+
.last_block = last_block,
963+
};
964+
965+
internal_req->coap_request.cb(&resp_data,
954966
internal_req->coap_request.user_data);
955967
atomic_clear(&internal_req->in_callback);
956968
}

tests/net/lib/coap_client/src/main.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ DEFINE_FFF_GLOBALS;
2727
#define VALID_MESSAGE_ID BIT(31)
2828
#define TOKEN_OFFSET 4
2929

30-
void coap_callback(int16_t code, size_t offset, const uint8_t *payload, size_t len, bool last_block,
31-
void *user_data);
30+
void coap_callback(const struct coap_client_response_data *data, void *user_data);
3231

3332
static int16_t last_response_code;
3433
static const char test_path[] = "test";
@@ -451,11 +450,10 @@ static ssize_t z_impl_zsock_recvfrom_custom_fake_observe(int sock, void *buf, si
451450
return ret;
452451
}
453452

454-
void coap_callback(int16_t code, size_t offset, const uint8_t *payload, size_t len, bool last_block,
455-
void *user_data)
453+
void coap_callback(const struct coap_client_response_data *data, void *user_data)
456454
{
457-
LOG_INF("CoAP response callback, %d", code);
458-
last_response_code = code;
455+
LOG_INF("CoAP response callback, %d", data->result_code);
456+
last_response_code = data->result_code;
459457
if (user_data) {
460458
k_sem_give((struct k_sem *) user_data);
461459
}

0 commit comments

Comments
 (0)