diff --git a/include/zephyr/net/coap.h b/include/zephyr/net/coap.h index 67a70cbc43a..42eee01cfc1 100644 --- a/include/zephyr/net/coap.h +++ b/include/zephyr/net/coap.h @@ -916,6 +916,21 @@ struct coap_observer *coap_find_observer_by_addr( struct coap_observer *observers, size_t len, const struct sockaddr *addr); +/** + * @brief Returns the observer that has token @a token. + * + * @param observers Pointer to the array of observers + * @param len Size of the array of observers + * @param token Pointer to the token + * @param token_len Length of valid bytes in the token + * + * @return A pointer to a observer if a match is found, NULL + * otherwise. + */ +struct coap_observer *coap_find_observer_by_token( + struct coap_observer *observers, size_t len, + const uint8_t *token, uint8_t token_len); + /** * @brief Returns the next available observer representation. * diff --git a/subsys/net/lib/coap/coap.c b/subsys/net/lib/coap/coap.c index bd266360f98..a7dd9302ba2 100644 --- a/subsys/net/lib/coap/coap.c +++ b/subsys/net/lib/coap/coap.c @@ -1915,6 +1915,25 @@ struct coap_observer *coap_find_observer_by_addr( return NULL; } +struct coap_observer *coap_find_observer_by_token( + struct coap_observer *observers, size_t len, + const uint8_t *token, uint8_t token_len) +{ + if (token_len == 0U || token_len > COAP_TOKEN_MAX_LEN) { + return NULL; + } + + for (size_t i = 0; i < len; i++) { + struct coap_observer *o = &observers[i]; + + if (o->tkl == token_len && memcmp(o->token, token, token_len) == 0) { + return o; + } + } + + return NULL; +} + /** * @brief Internal initialization function for CoAP library. *