diff --git a/sys/include/net/nanocoap.h b/sys/include/net/nanocoap.h index dd695b78945f..0140d92e2abc 100644 --- a/sys/include/net/nanocoap.h +++ b/sys/include/net/nanocoap.h @@ -273,6 +273,14 @@ typedef struct { void *context; /**< ptr to user defined context data */ } coap_resource_t; +/** + * @brief Type for CoAP resource subtrees + */ +typedef const struct { + const coap_resource_t *resources; /**< ptr to resource array */ + const size_t resources_numof; /**< number of entries in array */ +} coap_resource_subtree_t; + /** * @brief Block1 helper struct */ @@ -1751,6 +1759,25 @@ ssize_t coap_tree_handler(coap_pkt_t *pkt, uint8_t *resp_buf, const coap_resource_t *resources, size_t resources_numof); +/** + * @brief Generic coap subtree handler + * + * This function can be used as a generic handler for resources with the + * @ref COAP_MATCH_SUBTREE where a new @ref coap_resource_t is to be parsed. + * + * @note The @p context must be of type @ref coap_resource_subtree_t. + * + * @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 + * + * @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); + /** * @brief Convert message code (request method) into a corresponding bit field * diff --git a/sys/include/suit/transport/coap.h b/sys/include/suit/transport/coap.h index b3c437205c4e..c13d30a8bb02 100644 --- a/sys/include/suit/transport/coap.h +++ b/sys/include/suit/transport/coap.h @@ -67,29 +67,6 @@ void suit_coap_run(void); */ #ifndef DOXYGEN -/** - * @brief Coap subtree handler - * - * @param[in,out] pkt Packet struct containing the request. Is reused for - * the response - * @param[in] buf Buffer to write reply to - * @param[in] len Total length of the buffer associated with the - * request - * @param[in] buf Buffer to write reply to - * - * @returns ssize_t Size of the reply - */ -ssize_t coap_subtree_handler(coap_pkt_t *pkt, uint8_t *buf, - size_t len, void *context); - -/** - * @brief Type for CoAP resource subtrees - */ -typedef const struct { - const coap_resource_t *resources; /**< ptr to resource array */ - const size_t resources_numof; /**< nr of entries in array */ -} coap_resource_subtree_t; - /** * @brief Reference to the coap resource subtree */ diff --git a/sys/net/application_layer/nanocoap/nanocoap.c b/sys/net/application_layer/nanocoap/nanocoap.c index 79367b7577d0..97d6cab9ffa6 100644 --- a/sys/net/application_layer/nanocoap/nanocoap.c +++ b/sys/net/application_layer/nanocoap/nanocoap.c @@ -424,6 +424,15 @@ ssize_t coap_handle_req(coap_pkt_t *pkt, uint8_t *resp_buf, unsigned resp_buf_le coap_resources_numof); } +ssize_t coap_subtree_handler(coap_pkt_t *pkt, uint8_t *buf, size_t len, + void *context) +{ + assert(context); + coap_resource_subtree_t *subtree = context; + return coap_tree_handler(pkt, buf, len, subtree->resources, + subtree->resources_numof); +} + ssize_t coap_tree_handler(coap_pkt_t *pkt, uint8_t *resp_buf, unsigned resp_buf_len, const coap_resource_t *resources, diff --git a/sys/suit/transport/coap.c b/sys/suit/transport/coap.c index 44f24367f742..bef335f6fc55 100644 --- a/sys/suit/transport/coap.c +++ b/sys/suit/transport/coap.c @@ -104,14 +104,6 @@ static inline void _print_download_progress(suit_manifest_t *manifest, static kernel_pid_t _suit_coap_pid; -ssize_t coap_subtree_handler(coap_pkt_t *pkt, uint8_t *buf, size_t len, - void *context) -{ - coap_resource_subtree_t *subtree = context; - return coap_tree_handler(pkt, buf, len, subtree->resources, - subtree->resources_numof); -} - static void _suit_handle_url(const char *url, coap_blksize_t blksize) { LOG_INFO("suit_coap: downloading \"%s\"\n", url);