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
9#pragma once
10
23
24#include <assert.h>
25#include <stdbool.h>
26#include <stdint.h>
27#include "clist.h"
28#include "net/nanocoap.h"
29#include "hashes/sha256.h"
30#include "ztimer.h"
31
32#ifdef __cplusplus
33extern "C" {
34#endif
35
39#ifndef CONFIG_NANOCOAP_CACHE_ENTRIES
40#define CONFIG_NANOCOAP_CACHE_ENTRIES (8)
41#endif
42
46#ifndef CONFIG_NANOCOAP_CACHE_KEY_LENGTH
47#define CONFIG_NANOCOAP_CACHE_KEY_LENGTH (8)
48#endif
49
53#ifndef CONFIG_NANOCOAP_CACHE_RESPONSE_SIZE
54#define CONFIG_NANOCOAP_CACHE_RESPONSE_SIZE (128)
55#endif
56
60typedef struct {
65
70
75
80
81 size_t response_len;
82
84#if IS_USED(MODULE_GCOAP) || defined(DOXYGEN)
85 bool truncated;
86#endif /* IS_USED(MODULE_GCOAP) || defined(DOXYGEN) */
87
92 uint32_t max_age;
94
102
112
116#if IS_USED(MODULE_NANOCOAP_CACHE)
117void nanocoap_cache_init(void);
118#else
119static inline void nanocoap_cache_init(void)
120{
121 return;
122}
123#endif
124
131
138
151nanocoap_cache_entry_t *nanocoap_cache_process(const uint8_t *cache_key, unsigned request_method,
152 const coap_pkt_t *resp, size_t resp_len);
165 const coap_pkt_t *resp,
166 size_t resp_len);
167
180 unsigned request_method,
181 const coap_pkt_t *resp,
182 size_t resp_len);
183
193
203
213
220void nanocoap_cache_key_generate(const coap_pkt_t *req, uint8_t *cache_key);
221
228void nanocoap_cache_key_options_generate(const coap_pkt_t *req, void *cache_key);
229
241
251ssize_t nanocoap_cache_key_compare(uint8_t *cache_key1, uint8_t *cache_key2);
252
262static inline bool nanocoap_cache_entry_is_stale(const nanocoap_cache_entry_t *ce, uint32_t now)
263{
264 /* see https://en.wikipedia.org/w/index.php?title=Serial_number_arithmetic&oldid=1085516466#General_solution */
265 return ((int)(now - ce->max_age) > 0);
266}
267
268#ifdef __cplusplus
269}
270#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:106
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:111
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:101
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:54
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:119
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:262
#define CONFIG_NANOCOAP_CACHE_KEY_LENGTH
The length of the cache key in bytes.
Definition cache.h:47
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:221
Cache container that holds a coap_pkt_t struct.
Definition cache.h:60
uint8_t request_method
the method of the initial request
Definition cache.h:83
coap_pkt_t response_pkt
packet representation of the response
Definition cache.h:74
uint32_t max_age
absolute system time in seconds until which this cache entry is considered valid.
Definition cache.h:92
uint8_t cache_key[CONFIG_NANOCOAP_CACHE_KEY_LENGTH]
the calculated cache key, see nanocoap_cache_key_generate().
Definition cache.h:69
size_t response_len
length of the message in response
Definition cache.h:81
clist_node_t node
needed for clist_t, must be the first struct member!
Definition cache.h:64
bool truncated
the cached response is truncated
Definition cache.h:85
uint8_t response_buf[CONFIG_NANOCOAP_CACHE_RESPONSE_SIZE]
buffer to hold the response message.
Definition cache.h:79
ztimer API