diff --git a/sys/include/net/nanocoap.h b/sys/include/net/nanocoap.h index 5b113eedc8..4f3fa3f578 100644 --- a/sys/include/net/nanocoap.h +++ b/sys/include/net/nanocoap.h @@ -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 } diff --git a/sys/net/application_layer/nanocoap/nanocoap.c b/sys/net/application_layer/nanocoap/nanocoap.c index 5b8a60f3a1..27e810524f 100644 --- a/sys/net/application_layer/nanocoap/nanocoap.c +++ b/sys/net/application_layer/nanocoap/nanocoap.c @@ -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;