mirror of
https://github.com/RIOT-OS/RIOT.git
synced 2025-01-18 12:52:44 +01:00
nanocoap_cache: add helper function to check if entry is stale
This commit is contained in:
parent
e6a9443932
commit
dbed2b48ea
@ -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"
|
||||
@ -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
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user