diff --git a/sys/include/net/nanocoap.h b/sys/include/net/nanocoap.h index dd695b7894..0140d92e2a 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/net/application_layer/nanocoap/nanocoap.c b/sys/net/application_layer/nanocoap/nanocoap.c index 79367b7577..97d6cab9ff 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,