async.c
Go to the documentation of this file.
/* async.c -- state management for asynchronous messages
*
* Copyright (C) 2010,2011,2021 Olaf Bergmann <bergmann@tzi.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*
* This file is part of the CoAP library libcoap. Please see
* README for terms of use.
*/
#include "coap3/coap_internal.h"
#ifndef WITHOUT_ASYNC
/* utlist-style macros for searching pairs in linked lists */
#define SEARCH_PAIR(head,out,field1,val1,field2,val2,field3,val3) \
SEARCH_PAIR3(head,out,field1,val1,field2,val2,field3,val3,next)
#define SEARCH_PAIR3(head,out,field1,val1,field2,val2,field3,val3,next) \
do { \
LL_FOREACH2(head,out,next) { \
if ((out)->field1 == (val1) && (out)->field2 == (val2) && \
((val2) == 0 || memcmp((out)->field3, (val3), (val2)) == 0)) break; \
} \
} while(0)
int
coap_async_is_supported(void) {
return 1;
}
coap_async_t *
coap_register_async(coap_session_t *session,
const coap_pdu_t *request, coap_tick_t delay) {
coap_async_t *s;
coap_mid_t mid = request->mid;
size_t len;
const uint8_t *data;
if (!COAP_PDU_IS_REQUEST(request))
return NULL;
SEARCH_PAIR(session->context->async_state, s,
session, session,
pdu->token_length, request->token_length,
pdu->token, request->token);
if (s != NULL) {
coap_log(LOG_DEBUG,
"asynchronous state for mid=0x%x already registered\n", mid);
return NULL;
}
/* store information for handling the asynchronous task */
s = (coap_async_t *)coap_malloc(sizeof(coap_async_t));
if (!s) {
coap_log(LOG_CRIT, "coap_register_async: insufficient memory\n");
return NULL;
}
memset(s, 0, sizeof(coap_async_t));
s->pdu = coap_pdu_duplicate(request, session, request->token_length,
request->token, NULL);
if (s->pdu == NULL) {
coap_free_async(session, s);
coap_log(LOG_CRIT, "coap_register_async: insufficient memory\n");
return NULL;
}
s->pdu->mid = mid; /* coap_pdu_duplicate() created one */
if (coap_get_data(request, &len, &data)) {
coap_add_data(s->pdu, len, data);
}
s->session = coap_session_reference( session );
coap_async_set_delay(s, delay);
LL_PREPEND(session->context->async_state, s);
return s;
}
void
coap_async_set_delay(coap_async_t *async, coap_tick_t delay) {
assert(async != NULL);
if (delay) {
coap_ticks(&async->delay);
async->delay += delay;
}
else
async->delay = 0;
coap_log(LOG_DEBUG, " %s: Request for delayed for %u.%03u secs\n",
coap_session_str(async->session),
(unsigned int)(delay / COAP_TICKS_PER_SECOND),
(unsigned int)((delay % COAP_TICKS_PER_SECOND) *
1000 / COAP_TICKS_PER_SECOND));
}
coap_async_t *
coap_find_async(coap_session_t *session, coap_bin_const_t token) {
coap_async_t *tmp;
SEARCH_PAIR(session->context->async_state, tmp,
session, session,
pdu->token_length, token.length,
pdu->token, token.s);
return tmp;
}
static void
coap_free_async_sub(coap_context_t *context, coap_async_t *s) {
if (s) {
LL_DELETE(context->async_state,s);
if (s->session) {
coap_session_release(s->session);
}
if (s->pdu) {
coap_delete_pdu(s->pdu);
s->pdu = NULL;
}
coap_free(s);
}
}
void
coap_free_async(coap_session_t *session, coap_async_t *s) {
coap_free_async_sub(session->context, s);
}
void
coap_delete_all_async(coap_context_t *context) {
coap_async_t *astate, *tmp;
LL_FOREACH_SAFE(context->async_state, astate, tmp) {
coap_free_async_sub(context, astate);
}
context->async_state = NULL;
}
void
coap_async_set_app_data(coap_async_t *async, void *app_data) {
async->appdata = app_data;
}
void *
coap_async_get_app_data(const coap_async_t *async) {
return async->appdata;
}
#else
int
coap_async_is_supported(void) {
return 0;
}
coap_async_t *
coap_register_async(coap_session_t *session,
const coap_pdu_t *request,
coap_tick_t delay) {
(void)session;
(void)request;
(void)delay;
return NULL;
}
void
coap_async_set_delay(coap_async_t *async, coap_tick_t delay) {
(void)async;
(void)delay;
}
void
coap_free_async(coap_session_t *session, coap_async_t *async) {
(void)session;
(void)async;
}
coap_async_t *
coap_find_async(coap_session_t *session,
coap_bin_const_t token) {
(void)session;
(void)token;
return NULL;
}
void
coap_async_set_app_data(coap_async_t *async, void *app_data) {
(void)async;
(void)app_data;
}
void *
coap_async_get_app_data(const coap_async_t *async) {
(void)async;
return NULL;
}
#endif /* WITHOUT_ASYNC */
SEARCH_PAIR
#define SEARCH_PAIR(head, out, field1, val1, field2, val2, field3, val3)
Definition: async.c:21
coap_free_async_sub
static void coap_free_async_sub(coap_context_t *context, coap_async_t *s)
Definition: async.c:119
coap_internal.h
Pulls together all the internal only header files.
coap_ticks
void coap_ticks(coap_tick_t *t)
Sets t to the internal time with COAP_TICKS_PER_SECOND resolution.
coap_tick_t
uint64_t coap_tick_t
This data type represents internal timer ticks with COAP_TICKS_PER_SECOND resolution.
Definition: coap_time.h:122
COAP_TICKS_PER_SECOND
#define COAP_TICKS_PER_SECOND
Use ms resolution on POSIX systems.
Definition: coap_time.h:137
coap_delete_all_async
void coap_delete_all_async(coap_context_t *context)
Removes and frees off all of the async entries for the given context.
Definition: async.c:139
coap_free_async
void coap_free_async(coap_session_t *session, coap_async_t *s)
Releases the memory that was allocated by coap_register_async() for the object async.
Definition: async.c:134
coap_register_async
coap_async_t * coap_register_async(coap_session_t *session, const coap_pdu_t *request, coap_tick_t delay)
Allocates a new coap_async_t object and fills its fields according to the given request.
Definition: async.c:38
coap_async_get_app_data
void * coap_async_get_app_data(const coap_async_t *async)
Gets the application data pointer held in async.
Definition: async.c:154
coap_async_set_delay
void coap_async_set_delay(coap_async_t *async, coap_tick_t delay)
Update the delay timeout, so changing when the registered async triggers.
Definition: async.c:91
coap_async_set_app_data
void coap_async_set_app_data(coap_async_t *async, void *app_data)
Set the application data pointer held in async.
Definition: async.c:149
coap_async_is_supported
int coap_async_is_supported(void)
Returns 1 if libcoap was built with separate messages enabled, 0 otherwise.
Definition: async.c:33
coap_find_async
coap_async_t * coap_find_async(coap_session_t *session, coap_bin_const_t token)
Retrieves the object identified by token from the list of asynchronous transactions that are register...
Definition: async.c:109
coap_session_str
const char * coap_session_str(const coap_session_t *session)
Get session description.
Definition: coap_session.c:1380
coap_log
#define coap_log(level,...)
Logging function.
Definition: coap_debug.h:152
LOG_CRIT
@ LOG_CRIT
Critical.
Definition: coap_debug.h:54
LOG_DEBUG
@ LOG_DEBUG
Debug.
Definition: coap_debug.h:59
COAP_PDU_IS_REQUEST
#define COAP_PDU_IS_REQUEST(pdu)
Definition: coap_pdu_internal.h:75
coap_pdu_duplicate
coap_pdu_t * coap_pdu_duplicate(const coap_pdu_t *old_pdu, coap_session_t *session, size_t token_length, const uint8_t *token, coap_opt_filter_t *drop_options)
Duplicate an existing PDU.
Definition: pdu.c:155
coap_delete_pdu
void coap_delete_pdu(coap_pdu_t *pdu)
Dispose of an CoAP PDU and frees associated storage.
Definition: pdu.c:142
coap_mid_t
int coap_mid_t
coap_mid_t is used to store the CoAP Message ID of a CoAP PDU.
Definition: pdu.h:231
coap_get_data
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.
Definition: pdu.c:654
coap_add_data
int coap_add_data(coap_pdu_t *pdu, size_t len, const uint8_t *data)
Adds given data to the pdu that is passed as first parameter.
Definition: pdu.c:624
coap_session_reference
coap_session_t * coap_session_reference(coap_session_t *session)
Increment reference counter on a session.
Definition: coap_session.c:70
coap_session_release
void coap_session_release(coap_session_t *session)
Decrement reference counter on a session.
Definition: coap_session.c:76
coap_malloc
COAP_STATIC_INLINE void * coap_malloc(size_t size)
Wrapper function to coap_malloc_type() for backwards compatibility.
Definition: mem.h:98
coap_free
COAP_STATIC_INLINE void coap_free(void *object)
Wrapper function to coap_free_type() for backwards compatibility.
Definition: mem.h:105
coap_async_t
Definition: coap_async_internal.h:33
coap_async_t::appdata
void * appdata
Definition: coap_async_internal.h:39
coap_async_t::session
coap_session_t * session
transaction session
Definition: coap_async_internal.h:37
coap_async_t::pdu
coap_pdu_t * pdu
copy of request pdu
Definition: coap_async_internal.h:38
coap_async_t::delay
coap_tick_t delay
When to delay to before triggering the response 0 indicates never trigger.
Definition: coap_async_internal.h:35
coap_bin_const_t
CoAP binary data definition with const data.
Definition: str.h:58
coap_bin_const_t::length
size_t length
length of binary data
Definition: str.h:59
coap_bin_const_t::s
const uint8_t * s
read-only binary data
Definition: str.h:60
coap_context_t
The CoAP stack's global state is stored in a coap_context_t object.
Definition: coap_net_internal.h:45
coap_context_t::async_state
coap_async_t * async_state
list of asynchronous message ids
Definition: coap_net_internal.h:60
coap_pdu_t
structure for CoAP PDUs token, if any, follows the fixed size header, then options until payload mark...
Definition: coap_pdu_internal.h:94
coap_pdu_t::token
uint8_t * token
first byte of token, if any, or options
Definition: coap_pdu_internal.h:111
coap_pdu_t::token_length
uint8_t token_length
length of Token
Definition: coap_pdu_internal.h:103
coap_pdu_t::mid
coap_mid_t mid
message id, if any, in regular host byte order
Definition: coap_pdu_internal.h:98
coap_session_t
Abstraction of virtual session that can be attached to coap_context_t (client) or coap_endpoint_t (se...
Definition: coap_session_internal.h:48
coap_session_t::context
coap_context_t * context
session's context
Definition: coap_session_internal.h:63