Loading...
Searching...
No Matches
cache.h
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2020 HAW Hamburg
3 * SPDX-License-Identifier: LGPL-2.1-only
4 */
5
6#pragma once
7
20
21#include <assert.h>
22#include <stdbool.h>
23#include <stdint.h>
24#include "clist.h"
25#include "net/nanocoap.h"
26#include "hashes/sha256.h"
27#include "ztimer.h"
28
29#ifdef __cplusplus
30extern "C" {
31#endif
32
36#ifndef CONFIG_NANOCOAP_CACHE_ENTRIES
37#define CONFIG_NANOCOAP_CACHE_ENTRIES (8)
38#endif
39
43#ifndef CONFIG_NANOCOAP_CACHE_KEY_LENGTH
44#define CONFIG_NANOCOAP_CACHE_KEY_LENGTH (8)
45#endif
46
50#ifndef CONFIG_NANOCOAP_CACHE_RESPONSE_SIZE
51#define CONFIG_NANOCOAP_CACHE_RESPONSE_SIZE (128)
52#endif
53
57typedef struct {
62
67
72
77
78 size_t response_len;
79
81#if IS_USED(MODULE_GCOAP) || defined(DOXYGEN)
82 bool truncated;
83#endif /* IS_USED(MODULE_GCOAP) || defined(DOXYGEN) */
84
89 uint32_t max_age;
91
99
109
113#if IS_USED(MODULE_NANOCOAP_CACHE)
114void nanocoap_cache_init(void);
115#else
116static inline void nanocoap_cache_init(void)
117{
118 return;
119}
120#endif
121
128
135
148nanocoap_cache_entry_t *nanocoap_cache_process(const uint8_t *cache_key, unsigned request_method,
149 const coap_pkt_t *resp, size_t resp_len);
162 const coap_pkt_t *resp,
163 size_t resp_len);
164
177 unsigned request_method,
178 const coap_pkt_t *resp,
179 size_t resp_len);
180
190
200
210
217void nanocoap_cache_key_generate(const coap_pkt_t *req, uint8_t *cache_key);
218
225void nanocoap_cache_key_options_generate(const coap_pkt_t *req, void *cache_key);
226
238
248ssize_t nanocoap_cache_key_compare(uint8_t *cache_key1, uint8_t *cache_key2);
249
259static inline bool nanocoap_cache_entry_is_stale(const nanocoap_cache_entry_t *ce, uint32_t now)
260{
261 /* see https://en.wikipedia.org/w/index.php?title=Serial_number_arithmetic&oldid=1085516466#General_solution */
262 return ((int)(now - ce->max_age) > 0);
263}
264
265#ifdef __cplusplus
266}
267#endif
POSIX.1-2008 compliant version of the assert macro.
Circular linked list.
list_node_t clist_node_t
List node structure.
Definition clist.h:104
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:108
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:98
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:51
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:116
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:259
#define CONFIG_NANOCOAP_CACHE_KEY_LENGTH
The length of the cache key in bytes.
Definition cache.h:44
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:226
Cache container that holds a coap_pkt_t struct.
Definition cache.h:57
uint8_t request_method
the method of the initial request
Definition cache.h:80
coap_pkt_t response_pkt
packet representation of the response
Definition cache.h:71
uint32_t max_age
absolute system time in seconds until which this cache entry is considered valid.
Definition cache.h:89
uint8_t cache_key[CONFIG_NANOCOAP_CACHE_KEY_LENGTH]
the calculated cache key, see nanocoap_cache_key_generate().
Definition cache.h:66
size_t response_len
length of the message in response
Definition cache.h:78
clist_node_t node
needed for clist_t, must be the first struct member!
Definition cache.h:61
bool truncated
the cached response is truncated
Definition cache.h:82
uint8_t response_buf[CONFIG_NANOCOAP_CACHE_RESPONSE_SIZE]
buffer to hold the response message.
Definition cache.h:76
ztimer API