From fac670e1e26c8d3487f84fbb2e158aaef1c7c51b Mon Sep 17 00:00:00 2001 From: Pieter De Gendt Date: Mon, 9 Oct 2023 09:57:46 +0200 Subject: [PATCH] net: lib: coap: Add coap_find_observer_by_token Add a CoAP helper function to find a matching observer by token. Signed-off-by: Pieter De Gendt --- include/zephyr/net/coap.h | 15 +++++++++++++++ subsys/net/lib/coap/coap.c | 19 +++++++++++++++++++ 2 files changed, 34 insertions(+) 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. *