Skip to content
Snippets Groups Projects
Unverified Commit d7004ebc authored by Ken Bannister's avatar Ken Bannister Committed by GitHub
Browse files

Merge pull request #8513 from kaspar030/nanocoap_add_context_to_resources

nanocoap: add context pointer to resources
parents ea7e0860 c2fd0fb3
No related branches found
No related tags found
No related merge requests found
...@@ -31,13 +31,13 @@ ...@@ -31,13 +31,13 @@
static void _resp_handler(unsigned req_state, coap_pkt_t* pdu, static void _resp_handler(unsigned req_state, coap_pkt_t* pdu,
sock_udp_ep_t *remote); sock_udp_ep_t *remote);
static ssize_t _stats_handler(coap_pkt_t* pdu, uint8_t *buf, size_t len); 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); static ssize_t _riot_board_handler(coap_pkt_t* pdu, uint8_t *buf, size_t len, void *ctx);
/* CoAP resources */ /* CoAP resources */
static const coap_resource_t _resources[] = { static const coap_resource_t _resources[] = {
{ "/cli/stats", COAP_GET | COAP_PUT, _stats_handler }, { "/cli/stats", COAP_GET | COAP_PUT, _stats_handler, NULL },
{ "/riot/board", COAP_GET, _riot_board_handler }, { "/riot/board", COAP_GET, _riot_board_handler, NULL },
}; };
static gcoap_listener_t _listener = { static gcoap_listener_t _listener = {
...@@ -98,8 +98,10 @@ static void _resp_handler(unsigned req_state, coap_pkt_t* pdu, ...@@ -98,8 +98,10 @@ static void _resp_handler(unsigned req_state, coap_pkt_t* pdu,
* allows any two byte value for example purposes. Semantically, the only * allows any two byte value for example purposes. Semantically, the only
* valid action is to set the value to 0. * valid action is to set the value to 0.
*/ */
static ssize_t _stats_handler(coap_pkt_t* pdu, uint8_t *buf, size_t len) static ssize_t _stats_handler(coap_pkt_t* pdu, uint8_t *buf, size_t len, void *ctx)
{ {
(void)ctx;
/* read coap method type in packet */ /* read coap method type in packet */
unsigned method_flag = coap_method2flag(coap_get_code_detail(pdu)); unsigned method_flag = coap_method2flag(coap_get_code_detail(pdu));
...@@ -129,8 +131,9 @@ static ssize_t _stats_handler(coap_pkt_t* pdu, uint8_t *buf, size_t len) ...@@ -129,8 +131,9 @@ static ssize_t _stats_handler(coap_pkt_t* pdu, uint8_t *buf, size_t len)
return 0; return 0;
} }
static ssize_t _riot_board_handler(coap_pkt_t *pdu, uint8_t *buf, size_t len) static ssize_t _riot_board_handler(coap_pkt_t *pdu, uint8_t *buf, size_t len, void *ctx)
{ {
(void)ctx;
gcoap_resp_init(pdu, buf, len, COAP_CODE_CONTENT); gcoap_resp_init(pdu, buf, len, COAP_CODE_CONTENT);
/* write the RIOT board name in the response buffer */ /* write the RIOT board name in the response buffer */
memcpy(pdu->payload, RIOT_BOARD, strlen(RIOT_BOARD)); memcpy(pdu->payload, RIOT_BOARD, strlen(RIOT_BOARD));
......
...@@ -16,14 +16,17 @@ ...@@ -16,14 +16,17 @@
/* internal value that can be read/written via CoAP */ /* internal value that can be read/written via CoAP */
static uint8_t internal_value = 0; static uint8_t internal_value = 0;
static ssize_t _riot_board_handler(coap_pkt_t *pkt, uint8_t *buf, size_t len) static ssize_t _riot_board_handler(coap_pkt_t *pkt, uint8_t *buf, size_t len, void *context)
{ {
(void)context;
return coap_reply_simple(pkt, COAP_CODE_205, buf, len, return coap_reply_simple(pkt, COAP_CODE_205, buf, len,
COAP_FORMAT_TEXT, (uint8_t*)RIOT_BOARD, strlen(RIOT_BOARD)); COAP_FORMAT_TEXT, (uint8_t*)RIOT_BOARD, strlen(RIOT_BOARD));
} }
static ssize_t _riot_value_handler(coap_pkt_t *pkt, uint8_t *buf, size_t len) static ssize_t _riot_value_handler(coap_pkt_t *pkt, uint8_t *buf, size_t len, void *context)
{ {
(void) context;
ssize_t p = 0; ssize_t p = 0;
char rsp[16]; char rsp[16];
unsigned code = COAP_CODE_EMPTY; unsigned code = COAP_CODE_EMPTY;
...@@ -55,8 +58,8 @@ static ssize_t _riot_value_handler(coap_pkt_t *pkt, uint8_t *buf, size_t len) ...@@ -55,8 +58,8 @@ static ssize_t _riot_value_handler(coap_pkt_t *pkt, uint8_t *buf, size_t len)
/* must be sorted by path (alphabetically) */ /* must be sorted by path (alphabetically) */
const coap_resource_t coap_resources[] = { const coap_resource_t coap_resources[] = {
COAP_WELL_KNOWN_CORE_DEFAULT_HANDLER, COAP_WELL_KNOWN_CORE_DEFAULT_HANDLER,
{ "/riot/board", COAP_GET, _riot_board_handler }, { "/riot/board", COAP_GET, _riot_board_handler, NULL },
{ "/riot/value", COAP_GET | COAP_PUT | COAP_POST, _riot_value_handler }, { "/riot/value", COAP_GET | COAP_PUT | COAP_POST, _riot_value_handler, NULL },
}; };
const unsigned coap_resources_numof = sizeof(coap_resources) / sizeof(coap_resources[0]); const unsigned coap_resources_numof = sizeof(coap_resources) / sizeof(coap_resources[0]);
...@@ -233,7 +233,7 @@ typedef struct { ...@@ -233,7 +233,7 @@ typedef struct {
/** /**
* @brief Resource handler type * @brief Resource handler type
*/ */
typedef ssize_t (*coap_handler_t)(coap_pkt_t *pkt, uint8_t *buf, size_t len); typedef ssize_t (*coap_handler_t)(coap_pkt_t *pkt, uint8_t *buf, size_t len, void *context);
/** /**
* @brief Type for CoAP resource entry * @brief Type for CoAP resource entry
...@@ -242,6 +242,7 @@ typedef struct { ...@@ -242,6 +242,7 @@ typedef struct {
const char *path; /**< URI path of resource */ const char *path; /**< URI path of resource */
unsigned methods; /**< OR'ed methods this resource allows */ unsigned methods; /**< OR'ed methods this resource allows */
coap_handler_t handler; /**< ptr to resource handler */ coap_handler_t handler; /**< ptr to resource handler */
void *context; /**< ptr to user defined context data */
} coap_resource_t; } coap_resource_t;
/** /**
...@@ -595,13 +596,18 @@ static inline uint32_t coap_get_observe(coap_pkt_t *pkt) ...@@ -595,13 +596,18 @@ static inline uint32_t coap_get_observe(coap_pkt_t *pkt)
* application * application
*/ */
extern ssize_t coap_well_known_core_default_handler(coap_pkt_t *pkt, \ extern ssize_t coap_well_known_core_default_handler(coap_pkt_t *pkt, \
uint8_t *buf, size_t len); uint8_t *buf, size_t len,
void *context);
/** /**
* @brief Resource definition for the default .well-known/core handler * @brief Resource definition for the default .well-known/core handler
*/ */
#define COAP_WELL_KNOWN_CORE_DEFAULT_HANDLER \ #define COAP_WELL_KNOWN_CORE_DEFAULT_HANDLER \
{ "/.well-known/core", COAP_GET, coap_well_known_core_default_handler } { \
.path = "/.well-known/core", \
.methods = COAP_GET, \
.handler = coap_well_known_core_default_handler \
}
#ifdef __cplusplus #ifdef __cplusplus
} }
......
...@@ -29,7 +29,7 @@ ...@@ -29,7 +29,7 @@
/* Internal functions */ /* Internal functions */
static void *_event_loop(void *arg); static void *_event_loop(void *arg);
static void _listen(sock_udp_t *sock); static void _listen(sock_udp_t *sock);
static ssize_t _well_known_core_handler(coap_pkt_t* pdu, uint8_t *buf, size_t len); static ssize_t _well_known_core_handler(coap_pkt_t* pdu, uint8_t *buf, size_t len, void *ctx);
static ssize_t _write_options(coap_pkt_t *pdu, uint8_t *buf, size_t len); static ssize_t _write_options(coap_pkt_t *pdu, uint8_t *buf, size_t len);
static size_t _handle_req(coap_pkt_t *pdu, uint8_t *buf, size_t len, static size_t _handle_req(coap_pkt_t *pdu, uint8_t *buf, size_t len,
sock_udp_ep_t *remote); sock_udp_ep_t *remote);
...@@ -48,7 +48,7 @@ static void _find_obs_memo_resource(gcoap_observe_memo_t **memo, ...@@ -48,7 +48,7 @@ static void _find_obs_memo_resource(gcoap_observe_memo_t **memo,
/* Internal variables */ /* Internal variables */
const coap_resource_t _default_resources[] = { const coap_resource_t _default_resources[] = {
{ "/.well-known/core", COAP_GET, _well_known_core_handler }, { "/.well-known/core", COAP_GET, _well_known_core_handler, NULL },
}; };
static gcoap_listener_t _default_listener = { static gcoap_listener_t _default_listener = {
...@@ -310,7 +310,7 @@ static size_t _handle_req(coap_pkt_t *pdu, uint8_t *buf, size_t len, ...@@ -310,7 +310,7 @@ static size_t _handle_req(coap_pkt_t *pdu, uint8_t *buf, size_t len,
return -1; return -1;
} }
ssize_t pdu_len = resource->handler(pdu, buf, len); ssize_t pdu_len = resource->handler(pdu, buf, len, resource->context);
if (pdu_len < 0) { if (pdu_len < 0) {
pdu_len = gcoap_response(pdu, buf, len, pdu_len = gcoap_response(pdu, buf, len,
COAP_CODE_INTERNAL_SERVER_ERROR); COAP_CODE_INTERNAL_SERVER_ERROR);
...@@ -457,8 +457,9 @@ static void _expire_request(gcoap_request_memo_t *memo) ...@@ -457,8 +457,9 @@ static void _expire_request(gcoap_request_memo_t *memo)
* Handler for /.well-known/core. Lists registered handlers, except for * Handler for /.well-known/core. Lists registered handlers, except for
* /.well-known/core itself. * /.well-known/core itself.
*/ */
static ssize_t _well_known_core_handler(coap_pkt_t* pdu, uint8_t *buf, size_t len) static ssize_t _well_known_core_handler(coap_pkt_t* pdu, uint8_t *buf, size_t len, void *ctx)
{ {
(void)ctx;
/* write header */ /* write header */
gcoap_resp_init(pdu, buf, len, COAP_CODE_CONTENT); gcoap_resp_init(pdu, buf, len, COAP_CODE_CONTENT);
int plen = gcoap_get_resource_list(pdu->payload, (size_t)pdu->payload_len, int plen = gcoap_get_resource_list(pdu->payload, (size_t)pdu->payload_len,
......
...@@ -155,11 +155,12 @@ ssize_t coap_handle_req(coap_pkt_t *pkt, uint8_t *resp_buf, unsigned resp_buf_le ...@@ -155,11 +155,12 @@ ssize_t coap_handle_req(coap_pkt_t *pkt, uint8_t *resp_buf, unsigned resp_buf_le
unsigned method_flag = coap_method2flag(coap_get_code_detail(pkt)); unsigned method_flag = coap_method2flag(coap_get_code_detail(pkt));
for (unsigned i = 0; i < coap_resources_numof; i++) { for (unsigned i = 0; i < coap_resources_numof; i++) {
if (!(coap_resources[i].methods & method_flag)) { const coap_resource_t *resource = &coap_resources[i];
if (!(resource->methods & method_flag)) {
continue; continue;
} }
int res = strcmp((char *)pkt->url, coap_resources[i].path); int res = strcmp((char *)pkt->url, resource->path);
if (res > 0) { if (res > 0) {
continue; continue;
} }
...@@ -167,7 +168,7 @@ ssize_t coap_handle_req(coap_pkt_t *pkt, uint8_t *resp_buf, unsigned resp_buf_le ...@@ -167,7 +168,7 @@ ssize_t coap_handle_req(coap_pkt_t *pkt, uint8_t *resp_buf, unsigned resp_buf_le
break; break;
} }
else { else {
return coap_resources[i].handler(pkt, resp_buf, resp_buf_len); return resource->handler(pkt, resp_buf, resp_buf_len, resource->context);
} }
} }
...@@ -381,8 +382,10 @@ size_t coap_put_option_uri(uint8_t *buf, uint16_t lastonum, const char *uri, uin ...@@ -381,8 +382,10 @@ size_t coap_put_option_uri(uint8_t *buf, uint16_t lastonum, const char *uri, uin
} }
ssize_t coap_well_known_core_default_handler(coap_pkt_t *pkt, uint8_t *buf, \ ssize_t coap_well_known_core_default_handler(coap_pkt_t *pkt, uint8_t *buf, \
size_t len) size_t len, void *context)
{ {
(void)context;
uint8_t *payload = buf + coap_get_total_hdr_len(pkt); uint8_t *payload = buf + coap_get_total_hdr_len(pkt);
uint8_t *bufpos = payload; uint8_t *bufpos = payload;
......
...@@ -26,13 +26,13 @@ ...@@ -26,13 +26,13 @@
* A test set of dummy resources. The resource handlers are set to NULL. * A test set of dummy resources. The resource handlers are set to NULL.
*/ */
static const coap_resource_t resources[] = { static const coap_resource_t resources[] = {
{ "/act/switch", (COAP_GET | COAP_POST), NULL }, { .path = "/act/switch", .methods = (COAP_GET | COAP_POST) },
{ "/sensor/temp", (COAP_GET), NULL }, { .path = "/sensor/temp", .methods = (COAP_GET) },
{ "/test/info/all", (COAP_GET), NULL }, { .path = "/test/info/all", .methods = (COAP_GET) },
}; };
static const coap_resource_t resources_second[] = { static const coap_resource_t resources_second[] = {
{ "/second/part", (COAP_GET), NULL }, { .path = "/second/part", .methods = (COAP_GET)},
}; };
static gcoap_listener_t listener = { static gcoap_listener_t listener = {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment