net: coap: Add API to count number of pending requests

Add coap_pendings_count() that return number of waiting
requests on the pendings array.

Signed-off-by: Seppo Takalo <seppo.takalo@nordicsemi.no>
This commit is contained in:
Seppo Takalo 2023-12-18 13:37:33 +02:00 committed by Fabio Baltieri
parent a4356da484
commit 01568b573a
2 changed files with 22 additions and 0 deletions

View file

@ -1112,6 +1112,15 @@ void coap_pending_clear(struct coap_pending *pending);
*/ */
void coap_pendings_clear(struct coap_pending *pendings, size_t len); void coap_pendings_clear(struct coap_pending *pendings, size_t len);
/**
* @brief Count number of pending requests.
*
* @param len Number of elements in array.
* @param pendings Array of pending requests.
* @return count of elements where timeout is not zero.
*/
size_t coap_pendings_count(struct coap_pending *pendings, size_t len);
/** /**
* @brief Cancels awaiting for this reply, so it becomes available * @brief Cancels awaiting for this reply, so it becomes available
* again. User responsibility to free the memory associated with data. * again. User responsibility to free the memory associated with data.

View file

@ -1734,6 +1734,19 @@ void coap_pendings_clear(struct coap_pending *pendings, size_t len)
} }
} }
size_t coap_pendings_count(struct coap_pending *pendings, size_t len)
{
struct coap_pending *p = pendings;
size_t c = 0;
for (size_t i = 0; i < len && p; i++, p++) {
if (p->timeout) {
c++;
}
}
return c;
}
/* Reordering according to RFC7641 section 3.4 but without timestamp comparison */ /* Reordering according to RFC7641 section 3.4 but without timestamp comparison */
static inline bool is_newer(int v1, int v2) static inline bool is_newer(int v1, int v2)
{ {