mirror of
https://github.com/RIOT-OS/RIOT.git
synced 2025-12-24 14:03:55 +01:00
Merge pull request #17957 from benpicco/coap_resource_ctx_t
nanocoap: define and use coap_request_ctx_t for request handlers
This commit is contained in:
commit
eb12f44331
@ -50,7 +50,7 @@ static void _on_ep_event(cord_ep_standalone_event_t event)
|
||||
|
||||
/* define some dummy CoAP resources */
|
||||
static ssize_t _handler_dummy(coap_pkt_t *pdu,
|
||||
uint8_t *buf, size_t len, void *ctx)
|
||||
uint8_t *buf, size_t len, coap_request_ctx_t *ctx)
|
||||
{
|
||||
(void)ctx;
|
||||
|
||||
@ -64,7 +64,7 @@ static ssize_t _handler_dummy(coap_pkt_t *pdu,
|
||||
}
|
||||
|
||||
static ssize_t _handler_info(coap_pkt_t *pdu,
|
||||
uint8_t *buf, size_t len, void *ctx)
|
||||
uint8_t *buf, size_t len, coap_request_ctx_t *ctx)
|
||||
{
|
||||
(void)ctx;
|
||||
|
||||
|
||||
@ -46,15 +46,15 @@ static ssize_t text_resp(coap_pkt_t *pdu, uint8_t *buf, size_t len,
|
||||
return resp_len + slen;
|
||||
}
|
||||
|
||||
static ssize_t handler_info(coap_pkt_t *pdu, uint8_t *buf, size_t len, void *ctx)
|
||||
static ssize_t handler_info(coap_pkt_t *pdu, uint8_t *buf, size_t len, coap_request_ctx_t *ctx)
|
||||
{
|
||||
(void)ctx;
|
||||
return text_resp(pdu, buf, len, riot_info, COAP_FORMAT_JSON);
|
||||
}
|
||||
|
||||
static ssize_t handler_text(coap_pkt_t *pdu, uint8_t *buf, size_t len, void *ctx)
|
||||
static ssize_t handler_text(coap_pkt_t *pdu, uint8_t *buf, size_t len, coap_request_ctx_t *ctx)
|
||||
{
|
||||
return text_resp(pdu, buf, len, (char *)ctx, COAP_FORMAT_TEXT);
|
||||
return text_resp(pdu, buf, len, coap_request_ctx_get_context(ctx), COAP_FORMAT_TEXT);
|
||||
}
|
||||
|
||||
static const coap_resource_t resources[] = {
|
||||
|
||||
@ -58,8 +58,8 @@ static const credman_credential_t credential = {
|
||||
|
||||
static ssize_t _encode_link(const coap_resource_t *resource, char *buf,
|
||||
size_t maxlen, coap_link_encoder_ctx_t *context);
|
||||
static ssize_t _stats_handler(coap_pkt_t* pdu, uint8_t *buf, size_t len, void *ctx);
|
||||
static ssize_t _riot_board_handler(coap_pkt_t* pdu, uint8_t *buf, size_t len, void *ctx);
|
||||
static ssize_t _stats_handler(coap_pkt_t* pdu, uint8_t *buf, size_t len, coap_request_ctx_t *ctx);
|
||||
static ssize_t _riot_board_handler(coap_pkt_t* pdu, uint8_t *buf, size_t len, coap_request_ctx_t *ctx);
|
||||
|
||||
/* CoAP resources. Must be sorted by path (ASCII order). */
|
||||
static const coap_resource_t _resources[] = {
|
||||
@ -108,7 +108,7 @@ static ssize_t _encode_link(const coap_resource_t *resource, char *buf,
|
||||
* allows any two byte value for example purposes. Semantically, the only
|
||||
* valid action is to set the value to 0.
|
||||
*/
|
||||
static ssize_t _stats_handler(coap_pkt_t* pdu, uint8_t *buf, size_t len, void *ctx)
|
||||
static ssize_t _stats_handler(coap_pkt_t* pdu, uint8_t *buf, size_t len, coap_request_ctx_t *ctx)
|
||||
{
|
||||
(void)ctx;
|
||||
|
||||
@ -142,7 +142,7 @@ static ssize_t _stats_handler(coap_pkt_t* pdu, uint8_t *buf, size_t len, void *c
|
||||
return 0;
|
||||
}
|
||||
|
||||
static ssize_t _riot_board_handler(coap_pkt_t *pdu, uint8_t *buf, size_t len, void *ctx)
|
||||
static ssize_t _riot_board_handler(coap_pkt_t *pdu, uint8_t *buf, size_t len, coap_request_ctx_t *ctx)
|
||||
{
|
||||
(void)ctx;
|
||||
gcoap_resp_init(pdu, buf, len, COAP_CODE_CONTENT);
|
||||
|
||||
@ -28,8 +28,8 @@
|
||||
#define ENABLE_DEBUG (0)
|
||||
#include "debug.h"
|
||||
|
||||
static ssize_t _sha256_handler(coap_pkt_t* pdu, uint8_t *buf, size_t len, void *ctx);
|
||||
static ssize_t _riot_block2_handler(coap_pkt_t *pdu, uint8_t *buf, size_t len, void *ctx);
|
||||
static ssize_t _sha256_handler(coap_pkt_t* pdu, uint8_t *buf, size_t len, coap_request_ctx_t *ctx);
|
||||
static ssize_t _riot_block2_handler(coap_pkt_t *pdu, uint8_t *buf, size_t len, coap_request_ctx_t *ctx);
|
||||
|
||||
/* CoAP resources */
|
||||
static const coap_resource_t _resources[] = {
|
||||
@ -47,7 +47,7 @@ static const uint8_t block2_intro[] = "This is RIOT (Version: ";
|
||||
static const uint8_t block2_board[] = " running on a ";
|
||||
static const uint8_t block2_mcu[] = " board with a ";
|
||||
|
||||
static ssize_t _riot_block2_handler(coap_pkt_t *pdu, uint8_t *buf, size_t len, void *ctx)
|
||||
static ssize_t _riot_block2_handler(coap_pkt_t *pdu, uint8_t *buf, size_t len, coap_request_ctx_t *ctx)
|
||||
{
|
||||
(void)ctx;
|
||||
coap_block_slicer_t slicer;
|
||||
@ -80,7 +80,7 @@ static ssize_t _riot_block2_handler(coap_pkt_t *pdu, uint8_t *buf, size_t len, v
|
||||
|
||||
/*
|
||||
* Uses block1 POSTs to generate an sha256 digest. */
|
||||
static ssize_t _sha256_handler(coap_pkt_t* pdu, uint8_t *buf, size_t len, void *ctx)
|
||||
static ssize_t _sha256_handler(coap_pkt_t* pdu, uint8_t *buf, size_t len, coap_request_ctx_t *ctx)
|
||||
{
|
||||
(void)ctx;
|
||||
|
||||
|
||||
@ -25,14 +25,9 @@
|
||||
#define MAIN_QUEUE_SIZE (4)
|
||||
static msg_t _main_msg_queue[MAIN_QUEUE_SIZE];
|
||||
|
||||
static const gcoap_fileserver_entry_t _vfs_entry = {
|
||||
.root = VFS_DEFAULT_DATA,
|
||||
.resource = "/vfs",
|
||||
};
|
||||
|
||||
/* CoAP resources. Must be sorted by path (ASCII order). */
|
||||
static const coap_resource_t _resources[] = {
|
||||
{ "/vfs", COAP_GET | COAP_MATCH_SUBTREE, gcoap_fileserver_handler, (void *)&_vfs_entry },
|
||||
{ "/vfs", COAP_GET | COAP_MATCH_SUBTREE, gcoap_fileserver_handler, VFS_DEFAULT_DATA },
|
||||
};
|
||||
|
||||
static gcoap_listener_t _listener = {
|
||||
|
||||
@ -22,7 +22,7 @@ static const uint8_t block2_intro[] = "This is RIOT (Version: ";
|
||||
static const uint8_t block2_board[] = " running on a ";
|
||||
static const uint8_t block2_mcu[] = " board with a ";
|
||||
|
||||
static ssize_t _echo_handler(coap_pkt_t *pkt, uint8_t *buf, size_t len, void *context)
|
||||
static ssize_t _echo_handler(coap_pkt_t *pkt, uint8_t *buf, size_t len, coap_request_ctx_t *context)
|
||||
{
|
||||
(void)context;
|
||||
char uri[CONFIG_NANOCOAP_URI_MAX];
|
||||
@ -37,14 +37,14 @@ static ssize_t _echo_handler(coap_pkt_t *pkt, uint8_t *buf, size_t len, void *co
|
||||
(uint8_t *)sub_uri, sub_uri_len);
|
||||
}
|
||||
|
||||
static ssize_t _riot_board_handler(coap_pkt_t *pkt, uint8_t *buf, size_t len, void *context)
|
||||
static ssize_t _riot_board_handler(coap_pkt_t *pkt, uint8_t *buf, size_t len, coap_request_ctx_t *context)
|
||||
{
|
||||
(void)context;
|
||||
return coap_reply_simple(pkt, COAP_CODE_205, buf, len,
|
||||
COAP_FORMAT_TEXT, (uint8_t*)RIOT_BOARD, strlen(RIOT_BOARD));
|
||||
}
|
||||
|
||||
static ssize_t _riot_block2_handler(coap_pkt_t *pkt, uint8_t *buf, size_t len, void *context)
|
||||
static ssize_t _riot_block2_handler(coap_pkt_t *pkt, uint8_t *buf, size_t len, coap_request_ctx_t *context)
|
||||
{
|
||||
(void)context;
|
||||
coap_block_slicer_t slicer;
|
||||
@ -77,7 +77,7 @@ static ssize_t _riot_block2_handler(coap_pkt_t *pkt, uint8_t *buf, size_t len, v
|
||||
buf, len, payload_len, &slicer);
|
||||
}
|
||||
|
||||
static ssize_t _riot_value_handler(coap_pkt_t *pkt, uint8_t *buf, size_t len, void *context)
|
||||
static ssize_t _riot_value_handler(coap_pkt_t *pkt, uint8_t *buf, size_t len, coap_request_ctx_t *context)
|
||||
{
|
||||
(void) context;
|
||||
|
||||
@ -112,7 +112,7 @@ static ssize_t _riot_value_handler(coap_pkt_t *pkt, uint8_t *buf, size_t len, vo
|
||||
COAP_FORMAT_TEXT, (uint8_t*)rsp, p);
|
||||
}
|
||||
|
||||
ssize_t _sha256_handler(coap_pkt_t* pkt, uint8_t *buf, size_t len, void *context)
|
||||
ssize_t _sha256_handler(coap_pkt_t* pkt, uint8_t *buf, size_t len, coap_request_ctx_t *context)
|
||||
{
|
||||
(void)context;
|
||||
|
||||
|
||||
@ -14,7 +14,8 @@
|
||||
#include "suit/transport/coap.h"
|
||||
#include "kernel_defines.h"
|
||||
|
||||
static ssize_t _riot_board_handler(coap_pkt_t *pkt, uint8_t *buf, size_t len, void *context)
|
||||
static ssize_t _riot_board_handler(coap_pkt_t *pkt, uint8_t *buf, size_t len,
|
||||
coap_request_ctx_t *context)
|
||||
{
|
||||
(void)context;
|
||||
return coap_reply_simple(pkt, COAP_CODE_205, buf, len,
|
||||
|
||||
@ -36,27 +36,25 @@
|
||||
*
|
||||
* * ``USEMODULE += gcoap_fileserver``
|
||||
*
|
||||
* * Have a @ref gcoap_fileserver_entry_t populated with the path you want to serve,
|
||||
* and the number of path components to strip from incoming requests:
|
||||
*
|
||||
* ```
|
||||
* static const gcoap_fileserver_entry_t files_sd = {
|
||||
* .root = "/sd0",
|
||||
* .resource = "/files/sd"
|
||||
* };
|
||||
* ```
|
||||
*
|
||||
* * Enter a @ref gcoap_fileserver_handler handler into your CoAP server's
|
||||
* resource list like this:
|
||||
*
|
||||
* ```
|
||||
* static const coap_resource_t _resources[] = {
|
||||
* ...
|
||||
* { "/files/sd", COAP_GET | COAP_MATCH_SUBTREE, gcoap_fileserver_handler, (void*)&files_sd },
|
||||
* {
|
||||
* .path = "/files/sd",
|
||||
* .methods = COAP_GET | COAP_MATCH_SUBTREE,
|
||||
* .handler = gcoap_fileserver_handler,
|
||||
* .context = "/sd0"
|
||||
* },
|
||||
* ...
|
||||
* }
|
||||
* ```
|
||||
*
|
||||
* The path argument specifies under which path the folder is served via CoAP while
|
||||
* the context argument contains the path on the local filesystem that will be served.
|
||||
*
|
||||
* The allowed methods dictate whether it's read-only (``COAP_GET``) or (in the
|
||||
* future<!-- WRITESUPPORT -->) read-write (``COAP_GET | COAP_PUT | COAP_DELETE``).
|
||||
*
|
||||
@ -77,26 +75,6 @@ extern "C" {
|
||||
|
||||
#include "net/nanocoap.h"
|
||||
|
||||
/**
|
||||
* @brief File server starting point
|
||||
*
|
||||
* This struct needs to be present at the ctx of a gcoap_fileserver_handler entry
|
||||
* in a resource list.
|
||||
*
|
||||
*/
|
||||
typedef struct {
|
||||
/**
|
||||
* @brief Path in the VFS that should be served.
|
||||
*
|
||||
* This does not need a trailing slash.
|
||||
*/
|
||||
const char *root;
|
||||
/**
|
||||
* @brief The associated CoAP resource path
|
||||
*/
|
||||
const char *resource;
|
||||
} gcoap_fileserver_entry_t;
|
||||
|
||||
/**
|
||||
* @brief File server handler
|
||||
*
|
||||
@ -106,12 +84,12 @@ typedef struct {
|
||||
* @param[in] pdu CoAP request package
|
||||
* @param[out] buf Buffer for the response
|
||||
* @param[in] len Response buffer length
|
||||
* @param[in] ctx pointer to a @ref gcoap_fileserver_entry_t
|
||||
* @param[in] ctx pointer to a @ref coap_request_ctx_t
|
||||
*
|
||||
* @return size of the response on success
|
||||
* negative error
|
||||
*/
|
||||
ssize_t gcoap_fileserver_handler(coap_pkt_t *pdu, uint8_t *buf, size_t len, void *ctx);
|
||||
ssize_t gcoap_fileserver_handler(coap_pkt_t *pdu, uint8_t *buf, size_t len, coap_request_ctx_t *ctx);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
||||
@ -240,6 +240,16 @@ typedef struct {
|
||||
#endif
|
||||
} coap_pkt_t;
|
||||
|
||||
/**
|
||||
* @brief Forward declaration of internal CoAP resource request handler context
|
||||
*/
|
||||
struct _coap_request_ctx;
|
||||
|
||||
/**
|
||||
* @brief CoAP resource request handler context
|
||||
*/
|
||||
typedef struct _coap_request_ctx coap_request_ctx_t;
|
||||
|
||||
/**
|
||||
* @brief Resource handler type
|
||||
*
|
||||
@ -253,8 +263,17 @@ typedef struct {
|
||||
*
|
||||
* For POST, PATCH and other non-idempotent methods, this is an additional
|
||||
* requirement introduced by the contract of this type.
|
||||
*
|
||||
* @param[in] pkt The request packet
|
||||
* @param[out] buf Buffer for the response
|
||||
* @param[in] len Size of the response buffer
|
||||
* @param[in] context Request context
|
||||
*
|
||||
* @return Number of response bytes written on success
|
||||
* Negative error on failure
|
||||
*/
|
||||
typedef ssize_t (*coap_handler_t)(coap_pkt_t *pkt, uint8_t *buf, size_t len, void *context);
|
||||
typedef ssize_t (*coap_handler_t)(coap_pkt_t *pkt, uint8_t *buf, size_t len,
|
||||
coap_request_ctx_t *context);
|
||||
|
||||
/**
|
||||
* @brief Coap blockwise request callback descriptor
|
||||
@ -307,6 +326,24 @@ typedef const struct {
|
||||
const size_t resources_numof; /**< number of entries in array */
|
||||
} coap_resource_subtree_t;
|
||||
|
||||
/**
|
||||
* @brief Get resource path associated with a CoAP request
|
||||
*
|
||||
* @param[in] ctx The request context
|
||||
*
|
||||
* @return Resource path of the request
|
||||
*/
|
||||
const char *coap_request_ctx_get_path(const coap_request_ctx_t *ctx);
|
||||
|
||||
/**
|
||||
* @brief Get resource context associated with a CoAP request
|
||||
*
|
||||
* @param[in] ctx The request context
|
||||
*
|
||||
* @return Resource context of the request
|
||||
*/
|
||||
void *coap_request_ctx_get_context(const coap_request_ctx_t *ctx);
|
||||
|
||||
/**
|
||||
* @brief Block1 helper struct
|
||||
*/
|
||||
@ -1811,13 +1848,14 @@ ssize_t coap_tree_handler(coap_pkt_t *pkt, uint8_t *resp_buf,
|
||||
* @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
|
||||
* @param[in] context pointer to request context, must contain context
|
||||
* to @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);
|
||||
size_t resp_buf_len, coap_request_ctx_t *context);
|
||||
|
||||
/**
|
||||
* @brief Convert message code (request method) into a corresponding bit field
|
||||
@ -1953,7 +1991,7 @@ ssize_t coap_reply_simple(coap_pkt_t *pkt,
|
||||
*/
|
||||
extern ssize_t coap_well_known_core_default_handler(coap_pkt_t *pkt, \
|
||||
uint8_t *buf, size_t len,
|
||||
void *context);
|
||||
coap_request_ctx_t *context);
|
||||
/**@}*/
|
||||
|
||||
/**
|
||||
|
||||
@ -2,4 +2,7 @@ SRC := gcoap.c
|
||||
|
||||
SUBMODULES := 1
|
||||
|
||||
# Since gcoap extends nanocoap, it shall be provided with nanocoap internals
|
||||
INCLUDES += -I$(RIOTBASE)/sys/net/application_layer/nanocoap
|
||||
|
||||
include $(RIOTBASE)/Makefile.base
|
||||
|
||||
@ -207,7 +207,7 @@ late_err:
|
||||
|
||||
static ssize_t gcoap_fileserver_directory_handler(coap_pkt_t *pdu, uint8_t *buf, size_t len,
|
||||
struct requestdata *request,
|
||||
gcoap_fileserver_entry_t *resource)
|
||||
const char *root, const char* resource_dir)
|
||||
{
|
||||
vfs_DIR dir;
|
||||
coap_block_slicer_t slicer;
|
||||
@ -227,9 +227,8 @@ static ssize_t gcoap_fileserver_directory_handler(coap_pkt_t *pdu, uint8_t *buf,
|
||||
coap_opt_add_block2(pdu, &slicer, true);
|
||||
buf += coap_opt_finish(pdu, COAP_OPT_FINISH_PAYLOAD);
|
||||
|
||||
size_t root_len = resource->root ? strlen(resource->root) : 0;
|
||||
size_t root_len = root ? strlen(root) : 0;
|
||||
const char *root_dir = &request->namebuf[root_len];
|
||||
const char *resource_dir = resource->resource;
|
||||
size_t root_dir_len = strlen(root_dir);
|
||||
size_t resource_dir_len = strlen(resource_dir);
|
||||
|
||||
@ -264,8 +263,10 @@ static ssize_t gcoap_fileserver_directory_handler(coap_pkt_t *pdu, uint8_t *buf,
|
||||
return (uintptr_t)buf - (uintptr_t)pdu->hdr;
|
||||
}
|
||||
|
||||
ssize_t gcoap_fileserver_handler(coap_pkt_t *pdu, uint8_t *buf, size_t len, void *ctx) {
|
||||
gcoap_fileserver_entry_t *entry = ctx;
|
||||
ssize_t gcoap_fileserver_handler(coap_pkt_t *pdu, uint8_t *buf, size_t len,
|
||||
coap_request_ctx_t *ctx) {
|
||||
const char *root = coap_request_ctx_get_context(ctx);
|
||||
const char *resource = coap_request_ctx_get_path(ctx);
|
||||
struct requestdata request = {
|
||||
.etag_sent = false,
|
||||
.blocknum2 = 0,
|
||||
@ -276,12 +277,12 @@ ssize_t gcoap_fileserver_handler(coap_pkt_t *pdu, uint8_t *buf, size_t len, void
|
||||
* zeroed to get a 0-terminated string. */
|
||||
size_t namelength = 0;
|
||||
uint8_t errorcode = COAP_CODE_INTERNAL_SERVER_ERROR;
|
||||
uint8_t strip_remaining = _count_char(entry->resource, '/');
|
||||
uint8_t strip_remaining = _count_char(resource, '/');
|
||||
|
||||
/* If a root directory for the server was specified, use that */
|
||||
if (entry->root && strlen(entry->root) > 1) {
|
||||
strncpy(request.namebuf, entry->root, sizeof(request.namebuf));
|
||||
namelength = strlen(entry->root);
|
||||
if (root && strlen(root) > 1) {
|
||||
strncpy(request.namebuf, root, sizeof(request.namebuf));
|
||||
namelength = strlen(root);
|
||||
}
|
||||
|
||||
bool is_directory = true; /* either no path component at all or trailing '/' */
|
||||
@ -360,7 +361,7 @@ ssize_t gcoap_fileserver_handler(coap_pkt_t *pdu, uint8_t *buf, size_t len, void
|
||||
* pass a struct pointer later. So far, those could even be hooked into the
|
||||
* resource list, but that'll go away once we parse more options */
|
||||
if (is_directory) {
|
||||
return gcoap_fileserver_directory_handler(pdu, buf, len, &request, entry);
|
||||
return gcoap_fileserver_directory_handler(pdu, buf, len, &request, root, resource);
|
||||
}
|
||||
else {
|
||||
return gcoap_fileserver_file_handler(pdu, buf, len, &request);
|
||||
|
||||
@ -41,7 +41,7 @@ static int _request_matcher_forward_proxy(gcoap_listener_t *listener,
|
||||
const coap_resource_t **resource,
|
||||
coap_pkt_t *pdu);
|
||||
static ssize_t _forward_proxy_handler(coap_pkt_t* pdu, uint8_t *buf,
|
||||
size_t len, void *ctx);
|
||||
size_t len, coap_request_ctx_t *ctx);
|
||||
|
||||
const coap_resource_t forward_proxy_resources[] = {
|
||||
{ "/", COAP_IGNORE, _forward_proxy_handler, NULL },
|
||||
@ -99,10 +99,10 @@ static int _request_matcher_forward_proxy(gcoap_listener_t *listener,
|
||||
}
|
||||
|
||||
static ssize_t _forward_proxy_handler(coap_pkt_t *pdu, uint8_t *buf,
|
||||
size_t len, void *ctx)
|
||||
size_t len, coap_request_ctx_t *ctx)
|
||||
{
|
||||
int pdu_len = 0;
|
||||
sock_udp_ep_t *remote = (sock_udp_ep_t *)ctx;
|
||||
int pdu_len;
|
||||
sock_udp_ep_t *remote = coap_request_ctx_get_context(ctx);
|
||||
|
||||
pdu_len = gcoap_forward_proxy_request_process(pdu, remote);
|
||||
|
||||
|
||||
@ -28,6 +28,8 @@
|
||||
#include "assert.h"
|
||||
#include "net/coap.h"
|
||||
#include "net/gcoap.h"
|
||||
#include "net/gcoap/forward_proxy.h"
|
||||
#include "nanocoap_internal.h"
|
||||
#include "net/nanocoap/cache.h"
|
||||
#include "net/sock/async/event.h"
|
||||
#include "net/sock/util.h"
|
||||
@ -42,8 +44,6 @@
|
||||
#include "net/dsm.h"
|
||||
#endif
|
||||
|
||||
#include "net/gcoap/forward_proxy.h"
|
||||
|
||||
#define ENABLE_DEBUG 0
|
||||
#include "debug.h"
|
||||
|
||||
@ -63,7 +63,8 @@ static ssize_t _tl_send(gcoap_socket_t *sock, const void *data, size_t len,
|
||||
const sock_udp_ep_t *remote, sock_udp_aux_tx_t *aux);
|
||||
static ssize_t _tl_authenticate(gcoap_socket_t *sock, const sock_udp_ep_t *remote,
|
||||
uint32_t timeout);
|
||||
static ssize_t _well_known_core_handler(coap_pkt_t* pdu, uint8_t *buf, size_t len, void *ctx);
|
||||
static ssize_t _well_known_core_handler(coap_pkt_t* pdu, uint8_t *buf, size_t len,
|
||||
coap_request_ctx_t *ctx);
|
||||
static void _cease_retransmission(gcoap_request_memo_t *memo);
|
||||
static size_t _handle_req(gcoap_socket_t *sock, coap_pkt_t *pdu, uint8_t *buf,
|
||||
size_t len, sock_udp_ep_t *remote);
|
||||
@ -688,13 +689,18 @@ static size_t _handle_req(gcoap_socket_t *sock, coap_pkt_t *pdu, uint8_t *buf,
|
||||
ssize_t pdu_len;
|
||||
char *offset;
|
||||
|
||||
coap_request_ctx_t ctx = {
|
||||
.resource = resource,
|
||||
};
|
||||
|
||||
if (coap_get_proxy_uri(pdu, &offset) > 0) {
|
||||
pdu_len = resource->handler(pdu, buf, len, remote);
|
||||
ctx.context = remote;
|
||||
}
|
||||
else {
|
||||
pdu_len = resource->handler(pdu, buf, len, resource->context);
|
||||
ctx.context = resource->context;
|
||||
}
|
||||
|
||||
pdu_len = resource->handler(pdu, buf, len, &ctx);
|
||||
if (pdu_len < 0) {
|
||||
pdu_len = gcoap_response(pdu, buf, len,
|
||||
COAP_CODE_INTERNAL_SERVER_ERROR);
|
||||
@ -883,7 +889,7 @@ static void _expire_request(gcoap_request_memo_t *memo)
|
||||
* /.well-known/core itself.
|
||||
*/
|
||||
static ssize_t _well_known_core_handler(coap_pkt_t* pdu, uint8_t *buf, size_t len,
|
||||
void *ctx)
|
||||
coap_request_ctx_t *ctx)
|
||||
{
|
||||
(void)ctx;
|
||||
|
||||
|
||||
@ -27,7 +27,7 @@
|
||||
#include <string.h>
|
||||
|
||||
#include "bitarithm.h"
|
||||
#include "net/nanocoap.h"
|
||||
#include "nanocoap_internal.h"
|
||||
|
||||
#define ENABLE_DEBUG 0
|
||||
#include "debug.h"
|
||||
@ -426,10 +426,10 @@ ssize_t coap_handle_req(coap_pkt_t *pkt, uint8_t *resp_buf, unsigned resp_buf_le
|
||||
}
|
||||
|
||||
ssize_t coap_subtree_handler(coap_pkt_t *pkt, uint8_t *buf, size_t len,
|
||||
void *context)
|
||||
coap_request_ctx_t *context)
|
||||
{
|
||||
assert(context);
|
||||
coap_resource_subtree_t *subtree = context;
|
||||
coap_resource_subtree_t *subtree = coap_request_ctx_get_context(context);
|
||||
return coap_tree_handler(pkt, buf, len, subtree->resources,
|
||||
subtree->resources_numof);
|
||||
}
|
||||
@ -461,7 +461,11 @@ ssize_t coap_tree_handler(coap_pkt_t *pkt, uint8_t *resp_buf,
|
||||
break;
|
||||
}
|
||||
else {
|
||||
return resource->handler(pkt, resp_buf, resp_buf_len, resource->context);
|
||||
coap_request_ctx_t ctx = {
|
||||
.resource = resource,
|
||||
.context = resource->context,
|
||||
};
|
||||
return resource->handler(pkt, resp_buf, resp_buf_len, &ctx);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1196,7 +1200,7 @@ size_t coap_blockwise_put_bytes(coap_block_slicer_t *slicer, uint8_t *bufpos,
|
||||
}
|
||||
|
||||
ssize_t coap_well_known_core_default_handler(coap_pkt_t *pkt, uint8_t *buf, \
|
||||
size_t len, void *context)
|
||||
size_t len, coap_request_ctx_t *context)
|
||||
{
|
||||
(void)context;
|
||||
coap_block_slicer_t slicer;
|
||||
@ -1232,3 +1236,13 @@ unsigned coap_get_len(coap_pkt_t *pkt)
|
||||
}
|
||||
return pktlen;
|
||||
}
|
||||
|
||||
const char *coap_request_ctx_get_path(const coap_request_ctx_t *ctx)
|
||||
{
|
||||
return ctx->resource->path;
|
||||
}
|
||||
|
||||
void *coap_request_ctx_get_context(const coap_request_ctx_t *ctx)
|
||||
{
|
||||
return ctx->context;
|
||||
}
|
||||
|
||||
41
sys/net/application_layer/nanocoap/nanocoap_internal.h
Normal file
41
sys/net/application_layer/nanocoap/nanocoap_internal.h
Normal file
@ -0,0 +1,41 @@
|
||||
/*
|
||||
* Copyright (C) 2022 ML!PA Consulting GmbH
|
||||
*
|
||||
* This file is subject to the terms and conditions of the GNU Lesser
|
||||
* General Public License v2.1. See the file LICENSE in the top level
|
||||
* directory for more details.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @ingroup net_nanocoap
|
||||
* @{
|
||||
*
|
||||
* @file
|
||||
* @brief NanoCoAP internals
|
||||
*
|
||||
* @author Benjamin Valentin <benjamin.valentin@ml-pa.com>
|
||||
*/
|
||||
|
||||
#ifndef NANOCOAP_INTERNAL_H
|
||||
#define NANOCOAP_INTERNAL_H
|
||||
|
||||
#include "net/nanocoap.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief Internal CoAP resource request handler context
|
||||
*/
|
||||
struct _coap_request_ctx {
|
||||
const coap_resource_t *resource; /**< resource of the request */
|
||||
void *context; /**< request context, needed to supply
|
||||
the remote for the forward proxy */
|
||||
};
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif /* NANOCOAP_INTERNAL_H */
|
||||
/** @} */
|
||||
@ -33,7 +33,7 @@
|
||||
#endif
|
||||
|
||||
static ssize_t _version_handler(coap_pkt_t *pkt, uint8_t *buf, size_t len,
|
||||
void *context)
|
||||
coap_request_ctx_t *context)
|
||||
{
|
||||
(void)context;
|
||||
return coap_reply_simple(pkt, COAP_CODE_205, buf, len,
|
||||
@ -41,7 +41,7 @@ static ssize_t _version_handler(coap_pkt_t *pkt, uint8_t *buf, size_t len,
|
||||
}
|
||||
|
||||
static ssize_t _trigger_handler(coap_pkt_t *pkt, uint8_t *buf, size_t len,
|
||||
void *context)
|
||||
coap_request_ctx_t *context)
|
||||
{
|
||||
(void)context;
|
||||
unsigned code;
|
||||
@ -66,12 +66,12 @@ static ssize_t _trigger_handler(coap_pkt_t *pkt, uint8_t *buf, size_t len,
|
||||
|
||||
#ifdef MODULE_RIOTBOOT_SLOT
|
||||
static ssize_t _slot_handler(coap_pkt_t *pkt, uint8_t *buf, size_t len,
|
||||
void *context)
|
||||
coap_request_ctx_t *context)
|
||||
{
|
||||
/* context is passed either as NULL or 0x1 for /active or /inactive */
|
||||
char c = '0';
|
||||
|
||||
if (context) {
|
||||
if (coap_request_ctx_get_context(context)) {
|
||||
c += riotboot_slot_other();
|
||||
}
|
||||
else {
|
||||
|
||||
@ -239,7 +239,7 @@ static int _unittests(int argc, char **argv)
|
||||
return 0;
|
||||
}
|
||||
|
||||
static ssize_t _mock_dns_server(coap_pkt_t *pdu, uint8_t *buf, size_t len, void *ctx)
|
||||
static ssize_t _mock_dns_server(coap_pkt_t *pdu, uint8_t *buf, size_t len, coap_request_ctx_t *ctx)
|
||||
{
|
||||
(void)ctx;
|
||||
if ((_resp_code >> 5) == COAP_CLASS_SUCCESS) {
|
||||
|
||||
@ -32,7 +32,7 @@
|
||||
/* internal value that can be read/written via CoAP */
|
||||
static uint8_t internal_value = 0;
|
||||
|
||||
static ssize_t _value_handler(coap_pkt_t *pkt, uint8_t *buf, size_t len, void *context)
|
||||
static ssize_t _value_handler(coap_pkt_t *pkt, uint8_t *buf, size_t len, coap_request_ctx_t *context)
|
||||
{
|
||||
(void) context;
|
||||
|
||||
|
||||
@ -58,7 +58,7 @@ static wc_Sha256 _sha_r;
|
||||
struct tc_sha256_state_struct _sha_r;
|
||||
#endif
|
||||
|
||||
ssize_t _edhoc_handler(coap_pkt_t *pkt, uint8_t *buf, size_t len, void *context)
|
||||
ssize_t _edhoc_handler(coap_pkt_t *pkt, uint8_t *buf, size_t len, coap_request_ctx_t *context)
|
||||
{
|
||||
(void)context;
|
||||
ssize_t msg_len = 0;
|
||||
|
||||
@ -15,9 +15,9 @@
|
||||
|
||||
static riotboot_flashwrite_t _writer;
|
||||
|
||||
ssize_t _flashwrite_handler(coap_pkt_t* pkt, uint8_t *buf, size_t len, void *context)
|
||||
ssize_t _flashwrite_handler(coap_pkt_t* pkt, uint8_t *buf, size_t len, coap_request_ctx_t *ctx)
|
||||
{
|
||||
riotboot_flashwrite_t *writer = context;
|
||||
riotboot_flashwrite_t *writer = coap_request_ctx_get_context(ctx);
|
||||
|
||||
uint32_t result = COAP_CODE_204;
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user