From 01568b573a93bf55ecd7da62ce1cab08746c9392 Mon Sep 17 00:00:00 2001 From: Seppo Takalo Date: Mon, 18 Dec 2023 13:37:33 +0200 Subject: [PATCH] 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 --- include/zephyr/net/coap.h | 9 +++++++++ subsys/net/lib/coap/coap.c | 13 +++++++++++++ 2 files changed, 22 insertions(+) diff --git a/include/zephyr/net/coap.h b/include/zephyr/net/coap.h index e303d5c8ad..cd57c560be 100644 --- a/include/zephyr/net/coap.h +++ b/include/zephyr/net/coap.h @@ -1112,6 +1112,15 @@ void coap_pending_clear(struct coap_pending *pending); */ 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 * again. User responsibility to free the memory associated with data. diff --git a/subsys/net/lib/coap/coap.c b/subsys/net/lib/coap/coap.c index bfb643a8e8..d22ee29bea 100644 --- a/subsys/net/lib/coap/coap.c +++ b/subsys/net/lib/coap/coap.c @@ -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 */ static inline bool is_newer(int v1, int v2) {