Loading...
Searching...
No Matches
cache.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2020 HAW Hamburg
3 *
4 * This file is subject to the terms and conditions of the GNU Lesser
5 * General Public License v2.1. See the file LICENSE in the top level
6 * directory for more details.
7 */
8
22#ifndef NET_NANOCOAP_CACHE_H
23#define NET_NANOCOAP_CACHE_H
24
25#include <assert.h>
26#include <stdbool.h>
27#include <stdint.h>
28#include "clist.h"
29#include "net/nanocoap.h"
30#include "hashes/sha256.h"
31#include "ztimer.h"
32
33#ifdef __cplusplus
34extern "C" {
35#endif
36
40#ifndef CONFIG_NANOCOAP_CACHE_ENTRIES
41#define CONFIG_NANOCOAP_CACHE_ENTRIES (8)
42#endif
43
47#ifndef CONFIG_NANOCOAP_CACHE_KEY_LENGTH
48#define CONFIG_NANOCOAP_CACHE_KEY_LENGTH (8)
49#endif
50
54#ifndef CONFIG_NANOCOAP_CACHE_RESPONSE_SIZE
55#define CONFIG_NANOCOAP_CACHE_RESPONSE_SIZE (128)
56#endif
57
61typedef struct {
66
71
76
81
82 size_t response_len;
85#if IS_USED(MODULE_GCOAP) || defined(DOXYGEN)
86 bool truncated;
87#endif /* IS_USED(MODULE_GCOAP) || defined(DOXYGEN) */
88
93 uint32_t max_age;
95
103
113
117#if IS_USED(MODULE_NANOCOAP_CACHE)
118void nanocoap_cache_init(void);
119#else
120static inline void nanocoap_cache_init(void)
121{
122 return;
123}
124#endif
125
132
139
152nanocoap_cache_entry_t *nanocoap_cache_process(const uint8_t *cache_key, unsigned request_method,
153 const coap_pkt_t *resp, size_t resp_len);
166 const coap_pkt_t *resp,
167 size_t resp_len);
168
181 unsigned request_method,
182 const coap_pkt_t *resp,
183 size_t resp_len);
184
194
204
214
221void nanocoap_cache_key_generate(const coap_pkt_t *req, uint8_t *cache_key);
222
229void nanocoap_cache_key_options_generate(const coap_pkt_t *req, void *cache_key);
230
242
252ssize_t nanocoap_cache_key_compare(uint8_t *cache_key1, uint8_t *cache_key2);
253
263static inline bool nanocoap_cache_entry_is_stale(const nanocoap_cache_entry_t *ce, uint32_t now)
264{
265 /* see https://en.wikipedia.org/w/index.php?title=Serial_number_arithmetic&oldid=1085516466#General_solution */
266 return ((int)(now - ce->max_age) > 0);
267}
268
269#ifdef __cplusplus
270}
271#endif
272#endif /* NET_NANOCOAP_CACHE_H */
POSIX.1-2008 compliant version of the assert macro.
Circular linked list.
size_t nanocoap_cache_used_count(void)
Returns the number of cached entries.
nanocoap_cache_entry_t * nanocoap_cache_request_lookup(const coap_pkt_t *req)
Performs a cache lookup based on the req.
int(* nanocoap_cache_update_strategy_t)(clist_node_t *node)
Typedef for the cache update strategy on element access.
Definition cache.h:112
nanocoap_cache_entry_t * nanocoap_cache_key_lookup(const uint8_t *cache_key)
Performs a cache lookup based on the cache key of a request.
void nanocoap_cache_key_blockreq_options_generate(const coap_pkt_t *req, void *cache_key)
Generates a cache key based on only the options in req without any of the blockwise options included ...
int(* nanocoap_cache_replacement_strategy_t)(void)
Typedef for the cache replacement strategy on full cache list.
Definition cache.h:102
nanocoap_cache_entry_t * nanocoap_cache_process(const uint8_t *cache_key, unsigned request_method, const coap_pkt_t *resp, size_t resp_len)
Determines if a response is cacheable and modifies the cache as reflected in RFC7252,...
int nanocoap_cache_del(const nanocoap_cache_entry_t *ce)
Deletes the provided cache entry ce.
void nanocoap_cache_key_options_generate(const coap_pkt_t *req, void *cache_key)
Generates a cache key based on only the options in req.
#define CONFIG_NANOCOAP_CACHE_RESPONSE_SIZE
Size of the buffer to store responses in the cache.
Definition cache.h:55
size_t nanocoap_cache_free_count(void)
Returns the number of unused cache entries.
void nanocoap_cache_key_generate(const coap_pkt_t *req, uint8_t *cache_key)
Generates a cache key based on the request req.
ssize_t nanocoap_cache_key_compare(uint8_t *cache_key1, uint8_t *cache_key2)
Compares two cache keys.
static void nanocoap_cache_init(void)
Initializes the internal state of the nanocoap cache.
Definition cache.h:120
static bool nanocoap_cache_entry_is_stale(const nanocoap_cache_entry_t *ce, uint32_t now)
Check if the Max-Age of a cache entry has passed.
Definition cache.h:263
#define CONFIG_NANOCOAP_CACHE_KEY_LENGTH
The length of the cache key in bytes.
Definition cache.h:48
nanocoap_cache_entry_t * nanocoap_cache_add_by_key(const uint8_t *cache_key, unsigned request_method, const coap_pkt_t *resp, size_t resp_len)
Creates a new or gets an existing cache entry using the cache key.
nanocoap_cache_entry_t * nanocoap_cache_add_by_req(const coap_pkt_t *req, const coap_pkt_t *resp, size_t resp_len)
Creates a new or gets an existing cache entry using the request packet.
nanocoap API
Header definitions for the SHA256 hash function.
CoAP PDU parsing context structure.
Definition nanocoap.h:232
List node structure.
Definition list.h:40
Cache container that holds a coap_pkt_t struct.
Definition cache.h:61
uint8_t request_method
the method of the initial request
Definition cache.h:84
coap_pkt_t response_pkt
packet representation of the response
Definition cache.h:75
uint32_t max_age
absolute system time in seconds until which this cache entry is considered valid.
Definition cache.h:93
size_t response_len
length of the message in response
Definition cache.h:82
clist_node_t node
needed for clist_t, must be the first struct member!
Definition cache.h:65
bool truncated
the cached response is truncated
Definition cache.h:86
ztimer API