From dbed2b48eaa405bed5305f4e0af4cde1ea571392 Mon Sep 17 00:00:00 2001 From: Martine Lenders Date: Fri, 13 May 2022 11:58:31 +0200 Subject: [PATCH] nanocoap_cache: add helper function to check if entry is stale --- sys/include/net/nanocoap/cache.h | 18 +++++++++++++++++- sys/net/application_layer/gcoap/gcoap.c | 3 +-- .../tests-nanocoap_cache.c | 7 +++++-- 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/sys/include/net/nanocoap/cache.h b/sys/include/net/nanocoap/cache.h index eb76820dd46a..64721b44f4b5 100644 --- a/sys/include/net/nanocoap/cache.h +++ b/sys/include/net/nanocoap/cache.h @@ -23,6 +23,7 @@ #define NET_NANOCOAP_CACHE_H #include +#include #include #include "clist.h" #include "net/nanocoap.h" @@ -89,7 +90,7 @@ typedef struct { * @brief absolute system time in seconds until which this cache entry * is considered valid. */ - ztimer_now_t max_age; + uint32_t max_age; } nanocoap_cache_entry_t; /** @@ -230,6 +231,21 @@ void nanocoap_cache_key_generate(const coap_pkt_t *req, uint8_t *cache_key); */ ssize_t nanocoap_cache_key_compare(uint8_t *cache_key1, uint8_t *cache_key2); +/** + * @brief Check if the Max-Age of a cache entry has passed + * + * @param[in] ce A cache entry + * @param[in] now The current time + * + * @return true, if Max-Age of cache entry has passed. + * @return false, if Max-Age of cache entry has not yet passed. + */ +static inline bool nanocoap_cache_entry_is_stale(const nanocoap_cache_entry_t *ce, uint32_t now) +{ + /* see https://en.wikipedia.org/w/index.php?title=Serial_number_arithmetic&oldid=1085516466#General_solution */ + return ((int)(now - ce->max_age) > 0); +} + #ifdef __cplusplus } #endif diff --git a/sys/net/application_layer/gcoap/gcoap.c b/sys/net/application_layer/gcoap/gcoap.c index db8581f16cc2..3d8ddb77519a 100644 --- a/sys/net/application_layer/gcoap/gcoap.c +++ b/sys/net/application_layer/gcoap/gcoap.c @@ -1253,8 +1253,7 @@ static bool _cache_lookup(gcoap_request_memo_t *memo, /* cache hit, methods are equal, and cache entry is not stale */ if (*ce && ((*ce)->request_method == coap_get_code(pdu)) && - /* if now < (*ce)->max_age we have an overflow => result large as (*ce)->max_age */ - ((unsigned)(now - (*ce)->max_age) > (*ce)->max_age)) { + !nanocoap_cache_entry_is_stale(*ce, now)) { return true; } } diff --git a/tests/unittests/tests-nanocoap_cache/tests-nanocoap_cache.c b/tests/unittests/tests-nanocoap_cache/tests-nanocoap_cache.c index f00eb80e1aa9..8545a34caa75 100644 --- a/tests/unittests/tests-nanocoap_cache/tests-nanocoap_cache.c +++ b/tests/unittests/tests-nanocoap_cache/tests-nanocoap_cache.c @@ -231,7 +231,7 @@ static void test_nanocoap_cache__max_age(void) /* the absolute time of max-age should be at approx. now + 30 sec (1 sec buffer) */ now = ztimer_now(ZTIMER_SEC); - TEST_ASSERT(c->max_age < (now + 31)); + TEST_ASSERT(nanocoap_cache_entry_is_stale(c, now + 31)); /* delete previously added cache entry */ nanocoap_cache_del(c); @@ -251,7 +251,10 @@ static void test_nanocoap_cache__max_age(void) /* the absolute time of max-age should be at approx. now + 60 sec (1 sec buffer) */ now = ztimer_now(ZTIMER_SEC); - TEST_ASSERT(c->max_age < (now + 61)); + TEST_ASSERT(nanocoap_cache_entry_is_stale(c, now + 61)); + /* check overflow cases */ + c->max_age = UINT32_MAX - 40; + TEST_ASSERT(nanocoap_cache_entry_is_stale(c, 20)); } Test *tests_nanocoap_cache_tests(void)