Skip to content

Commit

Permalink
nanocoap: make separate tree handling function
Browse files Browse the repository at this point in the history
This refactors nanocoap to seperate out the resource tree parsing. It
allows for calling the tree handler with custom resource trees. The
advantage is that a resource with COAP_MATCH_SUBTREE can parse a new
separate resource tree.
  • Loading branch information
bergzand committed Mar 23, 2020
1 parent 5ef0cb9 commit 1d5010e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
20 changes: 20 additions & 0 deletions sys/include/net/nanocoap.h
Original file line number Diff line number Diff line change
Expand Up @@ -1405,6 +1405,26 @@ ssize_t coap_build_reply(coap_pkt_t *pkt, unsigned code,
*/
ssize_t coap_handle_req(coap_pkt_t *pkt, uint8_t *resp_buf, unsigned resp_buf_len);

/**
* @brief Pass a coap request to a matching handler
*
* This function will try to find a matching handler in @p resources and call
* the handler.
*
* @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] resources Array of coap endpoint resources
* @param[in] resources_numof length of the coap endpoint resources
*
* @returns size of the reply packet on success
* @returns <0 on error
*/
ssize_t coap_tree_handler(coap_pkt_t *pkt, uint8_t *resp_buf,
unsigned resp_buf_len,
const coap_resource_t *resources,
size_t resources_numof);

/**
* @brief Convert message code (request method) into a corresponding bit field
*
Expand Down
12 changes: 10 additions & 2 deletions sys/net/application_layer/nanocoap/nanocoap.c
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,15 @@ ssize_t coap_handle_req(coap_pkt_t *pkt, uint8_t *resp_buf, unsigned resp_buf_le
if (pkt->hdr->code == 0) {
return coap_build_reply(pkt, COAP_CODE_EMPTY, resp_buf, resp_buf_len, 0);
}
return coap_tree_handler(pkt, resp_buf, resp_buf_len, coap_resources,
coap_resources_numof);
}

ssize_t coap_tree_handler(coap_pkt_t *pkt, uint8_t *resp_buf,
unsigned resp_buf_len,
const coap_resource_t *resources,
size_t resources_numof)
{
coap_method_flags_t method_flag = coap_method2flag(coap_get_code_detail(pkt));

uint8_t uri[NANOCOAP_URI_MAX];
Expand All @@ -393,8 +401,8 @@ ssize_t coap_handle_req(coap_pkt_t *pkt, uint8_t *resp_buf, unsigned resp_buf_le
}
DEBUG("nanocoap: URI path: \"%s\"\n", uri);

for (unsigned i = 0; i < coap_resources_numof; i++) {
const coap_resource_t *resource = &coap_resources[i];
for (unsigned i = 0; i < resources_numof; i++) {
const coap_resource_t *resource = &resources[i];
if (!(resource->methods & method_flag)) {
continue;
}
Expand Down

0 comments on commit 1d5010e

Please sign in to comment.