nanocoap: add context ptr to coap_resource_t
This commit is contained in:
parent
ef20fe7bb3
commit
0729259390
@ -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 {
|
|||||||
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)
|
|||||||
* 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
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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
|
|||||||
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
|
|||||||
}
|
}
|
||||||
|
|
||||||
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;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user