Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nanocoap: Add handler for resource-based subtrees #13698

Merged
merged 2 commits into from
Apr 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions sys/include/net/nanocoap.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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
*
Expand Down
23 changes: 0 additions & 23 deletions sys/include/suit/transport/coap.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
9 changes: 9 additions & 0 deletions sys/net/application_layer/nanocoap/nanocoap.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 0 additions & 8 deletions sys/suit/transport/coap.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down