1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

nanocoap: add coap_request_ctx_init()

This commit is contained in:
Benjamin Valentin 2022-10-28 14:16:13 +02:00
parent c6f9654461
commit 55e13a9d61
2 changed files with 36 additions and 0 deletions

View File

@ -322,6 +322,33 @@ typedef const struct {
const size_t resources_numof; /**< number of entries in array */
} coap_resource_subtree_t;
/**
* @brief Size of the CoAP request context struct
*/
#define COAP_REQUEST_CTX_SIZE (2 * sizeof(void *) + \
IS_USED(MODULE_GCOAP) * sizeof(uint32_t))
/**
* @brief Define and initialize CoAP request context struct
*
* @param[in] ctx Name of the request context variable
* @param[in] remote Remote endpoint that made the request
*/
#define COAP_REQUEST_CTX_INIT(ctx, remote) \
uint8_t ctx ## _buffer[COAP_REQUEST_CTX_SIZE]; \
coap_request_ctx_t *ctx = (void *)ctx ## _buffer; \
coap_request_ctx_init(ctx, remote)
/**
* @brief Initialize CoAP request context
* Called by @ref COAP_REQUEST_CTX_INIT
* @internal
*
* @param[in] ctx Pointer to the request context to initialize
* @param[in] remote Remote endpoint that made the request
*/
void coap_request_ctx_init(coap_request_ctx_t *ctx, sock_udp_ep_t *remote);
/**
* @brief Get resource path associated with a CoAP request
*

View File

@ -1260,6 +1260,15 @@ unsigned coap_get_len(coap_pkt_t *pkt)
return pktlen;
}
void coap_request_ctx_init(coap_request_ctx_t *ctx, sock_udp_ep_t *remote)
{
static_assert(COAP_REQUEST_CTX_SIZE == sizeof(coap_request_ctx_t),
"COAP_REQUEST_CTX_SIZE define does not match actual size");
memset(ctx, 0, sizeof(*ctx));
ctx->remote = remote;
}
const char *coap_request_ctx_get_path(const coap_request_ctx_t *ctx)
{
return ctx->resource->path;