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

nanocoap: allow to define CoAP resources as XFA

This commit is contained in:
Benjamin Valentin 2023-02-21 21:26:20 +01:00 committed by Benjamin Valentin
parent c09ad471a1
commit 44c267c84a
3 changed files with 57 additions and 7 deletions

View File

@ -90,6 +90,7 @@
#include "bitfield.h"
#include "byteorder.h"
#include "iolist.h"
#include "macros/utils.h"
#include "net/coap.h"
#else
#include "coap.h"
@ -102,6 +103,10 @@
typedef void sock_udp_ep_t;
#endif
#if defined(MODULE_NANOCOAP_RESOURCES)
#include "xfa.h"
#endif
#ifdef __cplusplus
extern "C" {
#endif
@ -402,15 +407,29 @@ typedef struct {
uint8_t *opt; /**< Pointer to the placed option */
} coap_block_slicer_t;
#if defined(MODULE_NANOCOAP_RESOURCES) || DOXYGEN
/**
* @brief CoAP XFA resource entry
*
* @param name internal name of the resource entry, must be unique
*/
#define NANOCOAP_RESOURCE(name) \
XFA_CONST(coap_resources_xfa, 0) coap_resource_t CONCAT(coap_resource_, name) =
#else
/**
* @brief Global CoAP resource list
* @deprecated Use @ref NANOCOAP_RESOURCE instead.
* The function will be removed after the 2024.01 release.
*/
extern const coap_resource_t coap_resources[];
/**
* @brief Number of entries in global CoAP resource list
* @deprecated Use @ref NANOCOAP_RESOURCE instead.
* The function will be removed after the 2024.01 release.
*/
extern const unsigned coap_resources_numof;
#endif
/**
* @name Functions -- Header Read/Write
@ -2048,6 +2067,13 @@ extern ssize_t coap_well_known_core_default_handler(coap_pkt_t *pkt, \
coap_request_ctx_t *context);
/**@}*/
/**
* @brief Respond to `/.well-known/core` to list all resources on the server
*/
#ifndef CONFIG_NANOCOAP_SERVER_WELL_KNOWN_CORE
#define CONFIG_NANOCOAP_SERVER_WELL_KNOWN_CORE (1)
#endif
/**
* @brief Checks if a CoAP resource path matches a given URI
*

View File

@ -22,17 +22,23 @@
*
* ## Server Operation ##
*
* See the nanocoap_server example, which is built on the nanocoap_server()
* function. A server must define an array of coap_resource_t resources for
* which it responds. See the declarations of `coap_resources` and
* `coap_resources_numof`. The array contents must be ordered by the resource
* path, specifically the ASCII encoding of the path characters (digit and
* capital precede lower case). Also see _Server path matching_ in the base
* [nanocoap](group__net__nanocoap.html) documentation.
* See the nanocoap_server example, which is built on the `nanocoap_server()`
* function. A server must define CoAP resources for which it responds.
*
* Each @ref coap_resource_t is added to the XFA with NANOCOAP_RESOURCE(name)
* followed by the declaration of the CoAP resource, e.g.:
*
* ```C
* NANOCOAP_RESOURCE(board) {
* .path = "/board", .methods = COAP_GET, .handler = _board_handler,
* };
* ```
*
* nanocoap itself provides the COAP_WELL_KNOWN_CORE_DEFAULT_HANDLER entry for
* `/.well-known/core`.
*
* To use the CoAP resource XFA, enable the `nanocoap_resources` module.
*
* ### Handler functions ###
*
* For each resource, you must implement a ::coap_handler_t handler function.

View File

@ -41,6 +41,24 @@
#define COAP_RST (3)
/** @} */
#ifdef MODULE_NANOCOAP_RESOURCES
/**
* @brief CoAP resources XFA
*/
XFA_INIT_CONST(coap_resource_t, coap_resources_xfa);
/**
* @brief Add well-known .core handler
*/
#if CONFIG_NANOCOAP_SERVER_WELL_KNOWN_CORE
NANOCOAP_RESOURCE(well_known_core) COAP_WELL_KNOWN_CORE_DEFAULT_HANDLER;
#endif
/* re-define coap_resources for compatibility with non-XFA version */
#define coap_resources ((const coap_resource_t *)coap_resources_xfa)
#define coap_resources_numof XFA_LEN(coap_resource_t, coap_resources_xfa)
#endif
static int _decode_value(unsigned val, uint8_t **pkt_pos_ptr, uint8_t *pkt_end);
static uint32_t _decode_uint(uint8_t *pkt_pos, unsigned nbytes);
static size_t _encode_uint(uint32_t *val);