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

nanocoap: add context ptr to coap_resource_t

This commit is contained in:
Kaspar Schleiser 2018-02-02 18:54:29 +01:00
parent ef20fe7bb3
commit 0729259390
2 changed files with 16 additions and 7 deletions

View File

@ -233,7 +233,7 @@ typedef struct {
/**
* @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
@ -242,6 +242,7 @@ typedef struct {
const char *path; /**< URI path of resource */
unsigned methods; /**< OR'ed methods this resource allows */
coap_handler_t handler; /**< ptr to resource handler */
void *context; /**< ptr to user defined context data */
} coap_resource_t;
/**
@ -595,13 +596,18 @@ static inline uint32_t coap_get_observe(coap_pkt_t *pkt)
* application
*/
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
*/
#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
}

View File

@ -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));
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;
}
int res = strcmp((char *)pkt->url, coap_resources[i].path);
int res = strcmp((char *)pkt->url, resource->path);
if (res > 0) {
continue;
}
@ -167,7 +168,7 @@ ssize_t coap_handle_req(coap_pkt_t *pkt, uint8_t *resp_buf, unsigned resp_buf_le
break;
}
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
}
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 *bufpos = payload;