net: lib: coap: Add coap_find_observer

Add a function to the public CoAP API to find and return the unique
observer based on the address and token.

Signed-off-by: Pieter De Gendt <pieter.degendt@basalte.be>
This commit is contained in:
Pieter De Gendt 2023-11-10 12:21:55 +01:00 committed by Carles Cufí
parent 592292c182
commit cbf9680f96
2 changed files with 46 additions and 0 deletions

View file

@ -934,6 +934,24 @@ bool coap_register_observer(struct coap_resource *resource,
bool coap_remove_observer(struct coap_resource *resource,
struct coap_observer *observer);
/**
* @brief Returns the observer that matches address @a addr
* and has token @a token.
*
* @param observers Pointer to the array of observers
* @param len Size of the array of observers
* @param addr Address of the endpoint observing a resource
* @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(
struct coap_observer *observers, size_t len,
const struct sockaddr *addr,
const uint8_t *token, uint8_t token_len);
/**
* @brief Returns the observer that matches address @a addr.
*
@ -941,6 +959,9 @@ bool coap_remove_observer(struct coap_resource *resource,
* @param len Size of the array of observers
* @param addr Address of the endpoint observing a resource
*
* @note The function coap_find_observer() should be preferred
* if both the observer's address and token are known.
*
* @return A pointer to a observer if a match is found, NULL
* otherwise.
*/
@ -956,6 +977,9 @@ struct coap_observer *coap_find_observer_by_addr(
* @param token Pointer to the token
* @param token_len Length of valid bytes in the token
*
* @note The function coap_find_observer() should be preferred
* if both the observer's address and token are known.
*
* @return A pointer to a observer if a match is found, NULL
* otherwise.
*/

View file

@ -1913,6 +1913,28 @@ static bool sockaddr_equal(const struct sockaddr *a,
return false;
}
struct coap_observer *coap_find_observer(
struct coap_observer *observers, size_t len,
const struct sockaddr *addr,
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 &&
sockaddr_equal(&o->addr, addr)) {
return o;
}
}
return NULL;
}
struct coap_observer *coap_find_observer_by_addr(
struct coap_observer *observers, size_t len,
const struct sockaddr *addr)