Loading...
Searching...
No Matches
nanoCoAP-Cache implementation

A cache implementation for nanocoap response messages. More...

Detailed Description

A cache implementation for nanocoap response messages.

Files

file  cache.h
 nanocoap-cache API
 

Data Structures

struct  nanocoap_cache_entry_t
 Cache container that holds a coap_pkt_t struct. More...
 

Macros

#define CONFIG_NANOCOAP_CACHE_ENTRIES   (8)
 The number of maximum cache entries.
 
#define CONFIG_NANOCOAP_CACHE_KEY_LENGTH   (8)
 The length of the cache key in bytes.
 
#define CONFIG_NANOCOAP_CACHE_RESPONSE_SIZE   (128)
 Size of the buffer to store responses in the cache.
 

Typedefs

typedef int(* nanocoap_cache_replacement_strategy_t) (void)
 Typedef for the cache replacement strategy on full cache list.
 
typedef int(* nanocoap_cache_update_strategy_t) (clist_node_t *node)
 Typedef for the cache update strategy on element access.
 

Functions

static void nanocoap_cache_init (void)
 Initializes the internal state of the nanocoap cache.
 
size_t nanocoap_cache_used_count (void)
 Returns the number of cached entries.
 
size_t nanocoap_cache_free_count (void)
 Returns the number of unused cache entries.
 
nanocoap_cache_entry_tnanocoap_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, Section 5.9.
 
nanocoap_cache_entry_tnanocoap_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_cache_entry_tnanocoap_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_tnanocoap_cache_request_lookup (const coap_pkt_t *req)
 Performs a cache lookup based on the req.
 
nanocoap_cache_entry_tnanocoap_cache_key_lookup (const uint8_t *cache_key)
 Performs a cache lookup based on the cache key of a request.
 
int nanocoap_cache_del (const nanocoap_cache_entry_t *ce)
 Deletes the provided cache entry ce.
 
void nanocoap_cache_key_generate (const coap_pkt_t *req, uint8_t *cache_key)
 Generates a cache key based on the request req.
 
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.
 
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 in the key.
 
ssize_t nanocoap_cache_key_compare (uint8_t *cache_key1, uint8_t *cache_key2)
 Compares two cache keys.
 
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.
 

Macro Definition Documentation

◆ CONFIG_NANOCOAP_CACHE_ENTRIES

#define CONFIG_NANOCOAP_CACHE_ENTRIES   (8)

The number of maximum cache entries.

Definition at line 41 of file cache.h.

◆ CONFIG_NANOCOAP_CACHE_KEY_LENGTH

#define CONFIG_NANOCOAP_CACHE_KEY_LENGTH   (8)

The length of the cache key in bytes.

Definition at line 48 of file cache.h.

◆ CONFIG_NANOCOAP_CACHE_RESPONSE_SIZE

#define CONFIG_NANOCOAP_CACHE_RESPONSE_SIZE   (128)

Size of the buffer to store responses in the cache.

Definition at line 55 of file cache.h.

Typedef Documentation

◆ nanocoap_cache_replacement_strategy_t

typedef int(* nanocoap_cache_replacement_strategy_t) (void)

Typedef for the cache replacement strategy on full cache list.

Returns
0 on successfully replacing a cache element
-1 on error

Definition at line 102 of file cache.h.

◆ nanocoap_cache_update_strategy_t

typedef int(* nanocoap_cache_update_strategy_t) (clist_node_t *node)

Typedef for the cache update strategy on element access.

Parameters
[in]nodeThe accessed node.
Returns
0 on successfully updating the element
-1 on error

Definition at line 112 of file cache.h.

Function Documentation

◆ nanocoap_cache_add_by_key()

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.

Parameters
[in]cache_keyThe cache key of the request
[in]request_methodThe method of the initial request
[in]respThe response to add to the cache
[in]resp_lenThe actual length of the response in resp
Returns
The previously existing or newly added cache entry on success
NULL, if there is no space left

◆ nanocoap_cache_add_by_req()

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.

Parameters
[in]reqThe request to calculate the cache-key
[in]respThe response to add to the cache
[in]resp_lenThe actual length of the response message in resp
Returns
The previously existing or newly added cache entry on success
NULL, if there is no space left

◆ nanocoap_cache_del()

int nanocoap_cache_del ( const nanocoap_cache_entry_t ce)

Deletes the provided cache entry ce.

Parameters
[in]ceThe cache entry to delete
Returns
0 on success
-1 if entry is not available in the cache

◆ nanocoap_cache_entry_is_stale()

static bool nanocoap_cache_entry_is_stale ( const nanocoap_cache_entry_t ce,
uint32_t  now 
)
inlinestatic

Check if the Max-Age of a cache entry has passed.

Parameters
[in]ceA cache entry
[in]nowThe current time
Returns
true, if Max-Age of cache entry has passed.
false, if Max-Age of cache entry has not yet passed.

Definition at line 263 of file cache.h.

◆ nanocoap_cache_free_count()

size_t nanocoap_cache_free_count ( void  )

Returns the number of unused cache entries.

Returns
Number of unused cache entries

◆ nanocoap_cache_init()

static void nanocoap_cache_init ( void  )
inlinestatic

Initializes the internal state of the nanocoap cache.

Definition at line 120 of file cache.h.

◆ nanocoap_cache_key_blockreq_options_generate()

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 in the key.

This function can be used to correlate individual requests that are part of a blockwise transfer with each other.

Parameters
[in]reqThe request to generate the cache key from
[out]cache_keyThe generated cache key of SHA256_DIGEST_LENGTH bytes

◆ nanocoap_cache_key_compare()

ssize_t nanocoap_cache_key_compare ( uint8_t *  cache_key1,
uint8_t *  cache_key2 
)

Compares two cache keys.

Parameters
[in]cache_key1The first cache key in the comparison
[in]cache_key2The second cache key in the comparison
Returns
0 if cache keys are equal
<0 or 0> (see memcmp()) for unequal cache keys

◆ nanocoap_cache_key_generate()

void nanocoap_cache_key_generate ( const coap_pkt_t req,
uint8_t *  cache_key 
)

Generates a cache key based on the request req.

Parameters
[in]reqThe request to generate the cache key from
[out]cache_keyThe generated cache key

◆ nanocoap_cache_key_lookup()

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.

Parameters
[in]cache_keyThe cache key of a request
Returns
An existing cache entry on cache hit
NULL on cache miss

◆ nanocoap_cache_key_options_generate()

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.

Parameters
[in]reqThe request to generate the cache key from
[out]cache_keyThe generated cache key of SHA256_DIGEST_LENGTH bytes

◆ nanocoap_cache_process()

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, Section 5.9.

Parameters
[in]cache_keyThe cache key of the request
[in]request_methodThe method of the initial request
[in]respThe response to operate on
[in]resp_lenThe actual length of the response in resp
Returns
The cache entry on successfully handling the response
NULL on error

◆ nanocoap_cache_request_lookup()

nanocoap_cache_entry_t * nanocoap_cache_request_lookup ( const coap_pkt_t req)

Performs a cache lookup based on the req.

Parameters
[in]reqThe request to calculate the cache-key
Returns
An existing cache entry on cache hit
NULL on cache miss

◆ nanocoap_cache_used_count()

size_t nanocoap_cache_used_count ( void  )

Returns the number of cached entries.

Returns
Number of cached entries