1 /* async.c -- state management for asynchronous messages
3 * Copyright (C) 2010,2011 Olaf Bergmann <bergmann@tzi.org>
5 * This file is part of the CoAP library libcoap. Please see
6 * README for terms of use.
11 * @brief state management for asynchronous messages
25 coap_register_async(coap_context_t *context, coap_address_t *peer,
26 coap_pdu_t *request, unsigned char flags, void *data) {
27 coap_async_state_t *s;
30 coap_transaction_id(peer, request, &id);
31 LL_SEARCH_SCALAR(context->async_state,s,id,id);
34 /* We must return NULL here as the caller must know that he is
35 * responsible for releasing @p data. */
36 debug("asynchronous state for transaction %d already registered\n", id);
40 /* store information for handling the asynchronous task */
41 s = (coap_async_state_t *)coap_malloc(sizeof(coap_async_state_t) +
42 request->hdr->token_length);
44 coap_log(LOG_CRIT, "coap_register_async: insufficient memory\n");
48 memset(s, 0, sizeof(coap_async_state_t) + request->hdr->token_length);
50 /* set COAP_ASYNC_CONFIRM according to request's type */
51 s->flags = flags & ~COAP_ASYNC_CONFIRM;
52 if (request->hdr->type == COAP_MESSAGE_CON)
53 s->flags |= COAP_ASYNC_CONFIRM;
57 memcpy(&s->peer, peer, sizeof(coap_address_t));
59 if (request->hdr->token_length) {
60 s->tokenlen = request->hdr->token_length;
61 memcpy(s->token, request->hdr->token, request->hdr->token_length);
64 memcpy(&s->id, &id, sizeof(coap_tid_t));
68 LL_PREPEND(context->async_state, s);
74 coap_find_async(coap_context_t *context, coap_tid_t id) {
75 coap_async_state_t *tmp;
76 LL_SEARCH_SCALAR(context->async_state,tmp,id,id);
81 coap_remove_async(coap_context_t *context, coap_tid_t id,
82 coap_async_state_t **s) {
83 coap_async_state_t *tmp = coap_find_async(context, id);
86 LL_DELETE(context->async_state,tmp);
93 coap_free_async(coap_async_state_t *s) {
94 if (s && (s->flags & COAP_ASYNC_RELEASE_DATA) != 0)
95 coap_free(s->appdata);
100 void does_not_exist(); /* make some compilers happy */
101 #endif /* WITHOUT_ASYNC */