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 <pieter.degendt@basalte.be>
This commit is contained in:
Pieter De Gendt 2023-10-09 09:57:46 +02:00 committed by Carles Cufí
parent cc89338888
commit fac670e1e2
2 changed files with 34 additions and 0 deletions

View File

@ -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.
*

View File

@ -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.
*