18#if COAP_SERVER_SUPPORT
23is_cache_key(uint16_t option_type,
size_t cache_ignore_count,
24 const uint16_t *cache_ignore_options) {
28 if ((option_type & 0x1e) == 0x1c)
38 for (i = 0; i < cache_ignore_count; i++) {
39 if (cache_ignore_options[i] == option_type) {
49 const uint16_t *options,
54 ret = coap_cache_ignore_options_lkd(ctx, options, count);
61 const uint16_t *options,
64 if (ctx->cache_ignore_options) {
70 if (ctx->cache_ignore_options) {
71 memcpy(ctx->cache_ignore_options, options, count *
sizeof(options[0]));
72 ctx->cache_ignore_count = count;
78 ctx->cache_ignore_options =
NULL;
79 ctx->cache_ignore_count = count;
88 const uint16_t *cache_ignore_options,
89 size_t cache_ignore_count) {
92 coap_digest_ctx_t *dctx;
100 dctx = coap_digest_setup();
106 if (!coap_digest_update(dctx, (
const uint8_t *)&session,
sizeof(session))) {
111 if (is_cache_key(opt_iter.
number, cache_ignore_count,
112 cache_ignore_options)) {
113 if (!coap_digest_update(dctx, (
const uint8_t *)&opt_iter.
number,
114 sizeof(opt_iter.
number))) {
130 if (!coap_digest_update(dctx, data, len)) {
136 if (!coap_digest_final(dctx, &digest)) {
142 memcpy(cache_key->key, digest.key,
sizeof(cache_key->key));
146 coap_digest_free(dctx);
155 session->
context->cache_ignore_options,
156 session->
context->cache_ignore_count);
168 unsigned int idle_timeout) {
172 cache = coap_new_cache_entry_lkd(session, pdu, record_pdu, session_based,
182 unsigned int idle_timeout) {
193 entry->session = session;
203 memcpy(entry->pdu, pdu, offsetof(
coap_pdu_t, token));
206 entry->pdu->data = entry->pdu->token + (pdu->
data - pdu->
token);
210 if (!entry->cache_key) {
215 entry->idle_timeout = idle_timeout;
216 if (idle_timeout > 0) {
230 cache = coap_cache_get_by_key_lkd(ctx, cache_key);
242 HASH_FIND(hh, ctx->cache, cache_key,
sizeof(
coap_cache_key_t), cache_entry);
244 if (cache_entry && cache_entry->idle_timeout > 0) {
258 entry = coap_cache_get_by_pdu_lkd(session, request, session_based);
274 cache_entry = coap_cache_get_by_key_lkd(session->
context, cache_key);
276 if (cache_entry && cache_entry->idle_timeout > 0) {
289 HASH_DELETE(hh, ctx->cache, cache_entry);
291 if (cache_entry->pdu) {
295 if (cache_entry->app_cb && cache_entry->app_data) {
303 return cache_entry->pdu;
311 coap_cache_set_app_data2_lkd(cache_entry, data, callback);
321 old_data = coap_cache_set_app_data2_lkd(cache_entry, app_data, callback);
329 void *old_data = cache_entry->app_data;
331 cache_entry->app_data = app_data;
332 cache_entry->app_cb = app_data ? callback :
NULL;
338 return cache_entry->app_data;
347 HASH_ITER(hh, ctx->cache, cp, ctmp) {
348 if (cp->idle_timeout > 0) {
349 if (cp->expire_ticks <= now) {
struct coap_cache_key_t coap_cache_key_t
struct coap_cache_entry_t coap_cache_entry_t
Library specific build wrapper for coap_internal.h.
void * coap_malloc_type(coap_memory_tag_t type, size_t size)
Allocates a chunk of size bytes and returns a pointer to the newly allocated memory.
void coap_free_type(coap_memory_tag_t type, void *p)
Releases the memory that was allocated by coap_malloc_type().
uint8_t coap_opt_t
Use byte-oriented access methods here because sliding a complex struct coap_opt_t over the data buffe...
void coap_delete_cache_entry(coap_context_t *context, coap_cache_entry_t *cache_entry)
Remove a cache-entry from the hash list and free off all the appropriate contents apart from app_data...
COAP_API int coap_cache_ignore_options(coap_context_t *context, const uint16_t *options, size_t count)
Define the CoAP options that are not to be included when calculating the cache-key.
void coap_delete_cache_key(coap_cache_key_t *cache_key)
Delete the cache-key.
COAP_API coap_cache_entry_t * coap_cache_get_by_key(coap_context_t *context, const coap_cache_key_t *cache_key)
Searches for a cache-entry identified by cache_key.
coap_cache_key_t * coap_cache_derive_key(const coap_session_t *session, const coap_pdu_t *pdu, coap_cache_session_based_t session_based)
Calculates a cache-key for the given CoAP PDU.
COAP_API coap_cache_entry_t * coap_cache_get_by_pdu(coap_session_t *session, const coap_pdu_t *pdu, coap_cache_session_based_t session_based)
Searches for a cache-entry corresponding to pdu.
const coap_pdu_t * coap_cache_get_pdu(const coap_cache_entry_t *cache_entry)
Returns the PDU information stored in the coap_cache entry.
COAP_DEPRECATED void coap_cache_set_app_data(coap_cache_entry_t *cache_entry, void *data, coap_app_data_free_callback_t callback)
Stores data with the given cache entry.
coap_cache_key_t * coap_cache_derive_key_w_ignore(const coap_session_t *session, const coap_pdu_t *pdu, coap_cache_session_based_t session_based, const uint16_t *ignore_options, size_t ignore_count)
Calculates a cache-key for the given CoAP PDU.
void * coap_cache_get_app_data(const coap_cache_entry_t *cache_entry)
Returns any application-specific data that has been stored with cache_entry using the function coap_c...
COAP_API coap_cache_entry_t * coap_new_cache_entry(coap_session_t *session, const coap_pdu_t *pdu, coap_cache_record_pdu_t record_pdu, coap_cache_session_based_t session_based, unsigned int idle_time)
Create a new cache-entry hash keyed by cache-key derived from the PDU.
COAP_API void * coap_cache_set_app_data2(coap_cache_entry_t *cache_entry, void *data, coap_app_data_free_callback_t callback)
Stores data with the given cache_entry, returning the previously stored value or NULL.
coap_cache_session_based_t
@ COAP_CACHE_IS_SESSION_BASED
uint64_t coap_tick_t
This data type represents internal timer ticks with COAP_TICKS_PER_SECOND resolution.
#define COAP_TICKS_PER_SECOND
Use ms resolution on POSIX systems.
void coap_ticks(coap_tick_t *t)
Returns the current value of an internal tick counter.
#define coap_lock_callback(func)
Dummy for no thread-safe code.
#define coap_lock_unlock()
Dummy for no thread-safe code.
#define coap_lock_check_locked()
Dummy for no thread-safe code.
#define coap_lock_lock(failed)
Dummy for no thread-safe code.
#define coap_log_warn(...)
coap_opt_t * coap_option_next(coap_opt_iterator_t *oi)
Updates the iterator oi to point to the next option.
uint32_t coap_opt_length(const coap_opt_t *opt)
Returns the length of the given option.
coap_opt_iterator_t * coap_option_iterator_init(const coap_pdu_t *pdu, coap_opt_iterator_t *oi, const coap_opt_filter_t *filter)
Initializes the given option iterator oi to point to the beginning of the pdu's option list.
#define COAP_OPT_ALL
Pre-defined filter that includes all options.
const uint8_t * coap_opt_value(const coap_opt_t *opt)
Returns a pointer to the value of the given option.
void coap_delete_pdu_lkd(coap_pdu_t *pdu)
Dispose of an CoAP PDU and free off associated storage.
int coap_pdu_resize(coap_pdu_t *pdu, size_t new_size)
Dynamically grows the size of pdu to new_size.
int coap_get_data(const coap_pdu_t *pdu, size_t *len, const uint8_t **data)
Retrieves the length and data pointer of specified PDU.
coap_pdu_t * coap_pdu_init(coap_pdu_type_t type, coap_pdu_code_t code, coap_mid_t mid, size_t size)
Creates a new CoAP PDU with at least enough storage space for the given size maximum message size.
#define COAP_OPTION_OBSERVE
@ COAP_REQUEST_CODE_FETCH
void(* coap_app_data_free_callback_t)(void *data)
Callback to free off the app data when the entry is being deleted / freed off.
The CoAP stack's global state is stored in a coap_context_t object.
Iterator to run through PDU options.
coap_option_num_t number
decoded option number
uint8_t * token
first byte of token (or extended length bytes prefix), if any, or options
coap_pdu_code_t code
request method (value 1–31) or response code (value 64-255)
uint8_t * data
first byte of payload, if any
coap_mid_t mid
message id, if any, in regular host byte order
size_t used_size
used bytes of storage for token, options and payload
size_t alloc_size
allocated storage for token, options and payload
coap_pdu_type_t type
message type
Abstraction of virtual session that can be attached to coap_context_t (client) or coap_endpoint_t (se...
coap_context_t * context
session's context