mirror of
https://github.com/RIOT-OS/RIOT.git
synced 2025-01-18 12:52:44 +01:00
nanocoap: request context to coap_handle_req()
This commit is contained in:
parent
ed1d8e0a3d
commit
c6f9654461
@ -353,8 +353,6 @@ uint32_t coap_request_ctx_get_tl_type(const coap_request_ctx_t *ctx);
|
||||
/**
|
||||
* @brief Get the remote endpoint from which the request was received
|
||||
*
|
||||
* @note This is currently only implemented for GCoAP
|
||||
*
|
||||
* @param[in] ctx The request context
|
||||
*
|
||||
* @return Remote endpoint from which the request originated
|
||||
@ -1846,11 +1844,13 @@ ssize_t coap_build_reply(coap_pkt_t *pkt, unsigned code,
|
||||
* @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] ctx CoAP request context information
|
||||
*
|
||||
* @returns size of reply packet on success
|
||||
* @returns <0 on error
|
||||
*/
|
||||
ssize_t coap_handle_req(coap_pkt_t *pkt, uint8_t *resp_buf, unsigned resp_buf_len);
|
||||
ssize_t coap_handle_req(coap_pkt_t *pkt, uint8_t *resp_buf, unsigned resp_buf_len,
|
||||
coap_request_ctx_t *ctx);
|
||||
|
||||
/**
|
||||
* @brief Pass a coap request to a matching handler
|
||||
@ -1861,6 +1861,7 @@ ssize_t coap_handle_req(coap_pkt_t *pkt, uint8_t *resp_buf, unsigned resp_buf_le
|
||||
* @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] ctx CoAP request context information
|
||||
* @param[in] resources Array of coap endpoint resources
|
||||
* @param[in] resources_numof length of the coap endpoint resources
|
||||
*
|
||||
@ -1868,7 +1869,7 @@ ssize_t coap_handle_req(coap_pkt_t *pkt, uint8_t *resp_buf, unsigned resp_buf_le
|
||||
* @returns <0 on error
|
||||
*/
|
||||
ssize_t coap_tree_handler(coap_pkt_t *pkt, uint8_t *resp_buf,
|
||||
unsigned resp_buf_len,
|
||||
unsigned resp_buf_len, coap_request_ctx_t *ctx,
|
||||
const coap_resource_t *resources,
|
||||
size_t resources_numof);
|
||||
|
||||
|
@ -414,8 +414,11 @@ bool coap_has_unprocessed_critical_options(const coap_pkt_t *pkt)
|
||||
return false;
|
||||
}
|
||||
|
||||
ssize_t coap_handle_req(coap_pkt_t *pkt, uint8_t *resp_buf, unsigned resp_buf_len)
|
||||
ssize_t coap_handle_req(coap_pkt_t *pkt, uint8_t *resp_buf, unsigned resp_buf_len,
|
||||
coap_request_ctx_t *ctx)
|
||||
{
|
||||
assert(ctx);
|
||||
|
||||
if (coap_get_code_class(pkt) != COAP_REQ) {
|
||||
DEBUG("coap_handle_req(): not a request.\n");
|
||||
return -EBADMSG;
|
||||
@ -424,8 +427,8 @@ 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);
|
||||
return coap_tree_handler(pkt, resp_buf, resp_buf_len, ctx,
|
||||
coap_resources, coap_resources_numof);
|
||||
}
|
||||
|
||||
ssize_t coap_subtree_handler(coap_pkt_t *pkt, uint8_t *buf, size_t len,
|
||||
@ -433,13 +436,12 @@ ssize_t coap_subtree_handler(coap_pkt_t *pkt, uint8_t *buf, size_t len,
|
||||
{
|
||||
assert(context);
|
||||
coap_resource_subtree_t *subtree = coap_request_ctx_get_context(context);
|
||||
return coap_tree_handler(pkt, buf, len, subtree->resources,
|
||||
return coap_tree_handler(pkt, buf, len, context, 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,
|
||||
ssize_t coap_tree_handler(coap_pkt_t *pkt, uint8_t *resp_buf, unsigned resp_buf_len,
|
||||
coap_request_ctx_t *ctx, const coap_resource_t *resources,
|
||||
size_t resources_numof)
|
||||
{
|
||||
coap_method_flags_t method_flag = coap_method2flag(coap_get_code_detail(pkt));
|
||||
@ -461,10 +463,8 @@ ssize_t coap_tree_handler(coap_pkt_t *pkt, uint8_t *resp_buf,
|
||||
continue;
|
||||
}
|
||||
|
||||
coap_request_ctx_t ctx = {
|
||||
.resource = resource,
|
||||
};
|
||||
return resource->handler(pkt, resp_buf, resp_buf_len, &ctx);
|
||||
ctx->resource = resource;
|
||||
return resource->handler(pkt, resp_buf, resp_buf_len, ctx);
|
||||
}
|
||||
|
||||
return coap_build_reply(pkt, COAP_CODE_404, resp_buf, resp_buf_len, 0);
|
||||
@ -1282,10 +1282,5 @@ uint32_t coap_request_ctx_get_tl_type(const coap_request_ctx_t *ctx)
|
||||
|
||||
const sock_udp_ep_t *coap_request_ctx_get_remote_udp(const coap_request_ctx_t *ctx)
|
||||
{
|
||||
#ifdef MODULE_GCOAP
|
||||
return ctx->remote;
|
||||
#else
|
||||
(void)ctx;
|
||||
return NULL;
|
||||
#endif
|
||||
}
|
||||
|
@ -35,6 +35,7 @@ extern "C" {
|
||||
*/
|
||||
struct _coap_request_ctx {
|
||||
const coap_resource_t *resource; /**< resource of the request */
|
||||
sock_udp_ep_t *remote; /**< remote endpoint of the request */
|
||||
#if defined(MODULE_GCOAP) || DOXYGEN
|
||||
/**
|
||||
* @brief transport the packet was received over
|
||||
@ -43,7 +44,6 @@ struct _coap_request_ctx {
|
||||
* cyclically include the @ref net_gcoap header.
|
||||
*/
|
||||
uint32_t tl_type;
|
||||
sock_udp_ep_t *remote; /**< remote endpoint of the request */
|
||||
#endif
|
||||
};
|
||||
|
||||
|
@ -26,6 +26,7 @@
|
||||
#include <stdio.h>
|
||||
|
||||
#include "atomic_utils.h"
|
||||
#include "nanocoap_internal.h"
|
||||
#include "net/nanocoap_sock.h"
|
||||
#include "net/sock/util.h"
|
||||
#include "net/sock/udp.h"
|
||||
@ -643,6 +644,9 @@ int nanocoap_server(sock_udp_ep_t *local, uint8_t *buf, size_t bufsize)
|
||||
{
|
||||
nanocoap_sock_t sock;
|
||||
sock_udp_ep_t remote;
|
||||
coap_request_ctx_t ctx = {
|
||||
.remote = &remote,
|
||||
};
|
||||
|
||||
if (!local->port) {
|
||||
local->port = COAP_PORT;
|
||||
@ -664,7 +668,7 @@ int nanocoap_server(sock_udp_ep_t *local, uint8_t *buf, size_t bufsize)
|
||||
DEBUG("error parsing packet\n");
|
||||
continue;
|
||||
}
|
||||
if ((res = coap_handle_req(&pkt, buf, bufsize)) > 0) {
|
||||
if ((res = coap_handle_req(&pkt, buf, bufsize, &ctx)) > 0) {
|
||||
sock_udp_send(&sock, buf, res, &remote);
|
||||
}
|
||||
else {
|
||||
|
Loading…
Reference in New Issue
Block a user