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. More...
 
#define CONFIG_NANOCOAP_CACHE_KEY_LENGTH   (8)
 The length of the cache key in bytes. More...
 
#define CONFIG_NANOCOAP_CACHE_RESPONSE_SIZE   (128)
 Size of the buffer to store responses in the cache. More...
 

Typedefs

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

Functions

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

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 243 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_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_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