From 55e13a9d611feb85567612bcc610fd23fcae5518 Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Fri, 28 Oct 2022 14:16:13 +0200 Subject: [PATCH] nanocoap: add coap_request_ctx_init() --- sys/include/net/nanocoap.h | 27 +++++++++++++++++++ sys/net/application_layer/nanocoap/nanocoap.c | 9 +++++++ 2 files changed, 36 insertions(+) diff --git a/sys/include/net/nanocoap.h b/sys/include/net/nanocoap.h index 19298a3d65..8588d9249e 100644 --- a/sys/include/net/nanocoap.h +++ b/sys/include/net/nanocoap.h @@ -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 * diff --git a/sys/net/application_layer/nanocoap/nanocoap.c b/sys/net/application_layer/nanocoap/nanocoap.c index e877ce8ccf..c9944dcf12 100644 --- a/sys/net/application_layer/nanocoap/nanocoap.c +++ b/sys/net/application_layer/nanocoap/nanocoap.c @@ -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;