Skip to content

Commit

Permalink
nanocoap_cache: add helper function to check if entry is stale
Browse files Browse the repository at this point in the history
  • Loading branch information
miri64 committed May 13, 2022
1 parent e6a9443 commit dbed2b4
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
18 changes: 17 additions & 1 deletion sys/include/net/nanocoap/cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#define NET_NANOCOAP_CACHE_H

#include <assert.h>
#include <stdbool.h>
#include <stdint.h>
#include "clist.h"
#include "net/nanocoap.h"
Expand Down Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions sys/net/application_layer/gcoap/gcoap.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
7 changes: 5 additions & 2 deletions tests/unittests/tests-nanocoap_cache/tests-nanocoap_cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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)
Expand Down

0 comments on commit dbed2b4

Please sign in to comment.