From acfab72296a00e5680eb7dd1c0ee36c3b6ace14a Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Fri, 15 Apr 2022 21:17:24 +0200 Subject: [PATCH] nanocoap: make use of coap_request_ctx_t --- examples/suit_update/coap_handler.c | 3 ++- sys/include/net/nanocoap.h | 5 +++-- sys/net/application_layer/nanocoap/nanocoap.c | 12 ++++++++---- sys/suit/transport/coap.c | 8 ++++---- tests/nanocoap_cli/request_handlers.c | 2 +- 5 files changed, 18 insertions(+), 12 deletions(-) diff --git a/examples/suit_update/coap_handler.c b/examples/suit_update/coap_handler.c index e985ce6b2e..5d31e76fa8 100644 --- a/examples/suit_update/coap_handler.c +++ b/examples/suit_update/coap_handler.c @@ -14,7 +14,8 @@ #include "suit/transport/coap.h" #include "kernel_defines.h" -static ssize_t _riot_board_handler(coap_pkt_t *pkt, uint8_t *buf, size_t len, void *context) +static ssize_t _riot_board_handler(coap_pkt_t *pkt, uint8_t *buf, size_t len, + coap_request_ctx_t *context) { (void)context; return coap_reply_simple(pkt, COAP_CODE_205, buf, len, diff --git a/sys/include/net/nanocoap.h b/sys/include/net/nanocoap.h index d28e747faa..d8d7a88f56 100644 --- a/sys/include/net/nanocoap.h +++ b/sys/include/net/nanocoap.h @@ -1849,13 +1849,14 @@ ssize_t coap_tree_handler(coap_pkt_t *pkt, uint8_t *resp_buf, * @param[in] pkt pointer to (parsed) CoAP packet * @param[out] resp_buf buffer for response * @param[in] resp_buf_len size of response buffer - * @param[in] context ptr to a @ref coap_resource_subtree_t instance + * @param[in] context pointer to request context, must contain context + * to @ref coap_resource_subtree_t instance * * @returns size of the reply packet on success * @returns <0 on error */ ssize_t coap_subtree_handler(coap_pkt_t *pkt, uint8_t *resp_buf, - size_t resp_buf_len, void *context); + size_t resp_buf_len, coap_request_ctx_t *context); /** * @brief Convert message code (request method) into a corresponding bit field diff --git a/sys/net/application_layer/nanocoap/nanocoap.c b/sys/net/application_layer/nanocoap/nanocoap.c index 62b848928e..389833e5c8 100644 --- a/sys/net/application_layer/nanocoap/nanocoap.c +++ b/sys/net/application_layer/nanocoap/nanocoap.c @@ -426,10 +426,10 @@ ssize_t coap_handle_req(coap_pkt_t *pkt, uint8_t *resp_buf, unsigned resp_buf_le } ssize_t coap_subtree_handler(coap_pkt_t *pkt, uint8_t *buf, size_t len, - void *context) + coap_request_ctx_t *context) { assert(context); - coap_resource_subtree_t *subtree = context; + coap_resource_subtree_t *subtree = context->context; return coap_tree_handler(pkt, buf, len, subtree->resources, subtree->resources_numof); } @@ -461,7 +461,11 @@ ssize_t coap_tree_handler(coap_pkt_t *pkt, uint8_t *resp_buf, break; } else { - return resource->handler(pkt, resp_buf, resp_buf_len, resource->context); + coap_request_ctx_t ctx = { + .resource = resource, + .context = resource->context, + }; + return resource->handler(pkt, resp_buf, resp_buf_len, &ctx); } } @@ -1196,7 +1200,7 @@ size_t coap_blockwise_put_bytes(coap_block_slicer_t *slicer, uint8_t *bufpos, } ssize_t coap_well_known_core_default_handler(coap_pkt_t *pkt, uint8_t *buf, \ - size_t len, void *context) + size_t len, coap_request_ctx_t *context) { (void)context; coap_block_slicer_t slicer; diff --git a/sys/suit/transport/coap.c b/sys/suit/transport/coap.c index 8ed74793d0..0ef417844c 100644 --- a/sys/suit/transport/coap.c +++ b/sys/suit/transport/coap.c @@ -33,7 +33,7 @@ #endif static ssize_t _version_handler(coap_pkt_t *pkt, uint8_t *buf, size_t len, - void *context) + coap_request_ctx_t *context) { (void)context; return coap_reply_simple(pkt, COAP_CODE_205, buf, len, @@ -41,7 +41,7 @@ static ssize_t _version_handler(coap_pkt_t *pkt, uint8_t *buf, size_t len, } static ssize_t _trigger_handler(coap_pkt_t *pkt, uint8_t *buf, size_t len, - void *context) + coap_request_ctx_t *context) { (void)context; unsigned code; @@ -66,12 +66,12 @@ static ssize_t _trigger_handler(coap_pkt_t *pkt, uint8_t *buf, size_t len, #ifdef MODULE_RIOTBOOT_SLOT static ssize_t _slot_handler(coap_pkt_t *pkt, uint8_t *buf, size_t len, - void *context) + coap_request_ctx_t *context) { /* context is passed either as NULL or 0x1 for /active or /inactive */ char c = '0'; - if (context) { + if (coap_request_ctx_get_context(context)) { c += riotboot_slot_other(); } else { diff --git a/tests/nanocoap_cli/request_handlers.c b/tests/nanocoap_cli/request_handlers.c index 32b78254b4..4690dde838 100644 --- a/tests/nanocoap_cli/request_handlers.c +++ b/tests/nanocoap_cli/request_handlers.c @@ -32,7 +32,7 @@ /* internal value that can be read/written via CoAP */ static uint8_t internal_value = 0; -static ssize_t _value_handler(coap_pkt_t *pkt, uint8_t *buf, size_t len, void *context) +static ssize_t _value_handler(coap_pkt_t *pkt, uint8_t *buf, size_t len, coap_request_ctx_t *context) { (void) context;