IOT-1072 Unfork libcoap: update forked headers
[platform/upstream/iotivity.git] / resource / csdk / connectivity / lib / libcoap-4.1.1 / async.c
1 /* async.c -- state management for asynchronous messages
2  *
3  * Copyright (C) 2010,2011 Olaf Bergmann <bergmann@tzi.org>
4  *
5  * This file is part of the CoAP library libcoap. Please see
6  * README for terms of use.
7  */
8
9 /**
10  * @file async.c
11  * @brief state management for asynchronous messages
12  */
13
14 #ifndef WITHOUT_ASYNC
15
16 #include "include/coap/config.h"
17 #include "include/coap/utlist.h"
18 #include "include/coap/mem.h"
19 #include "include/coap/debug.h"
20 #include "include/coap/async.h"
21
22 coap_async_state_t *
23 coap_register_async(coap_context_t *context, coap_address_t *peer,
24         coap_pdu_t *request, unsigned char flags, void *data) {
25     coap_async_state_t *s;
26     coap_tid_t id;
27
28     coap_transaction_id(peer, request, &id);
29     LL_SEARCH_SCALAR(context->async_state, s, id, id);
30
31     if (s != NULL) {
32         /* We must return NULL here as the caller must know that he is
33          * responsible for releasing @p data. */
34         debug("asynchronous state for transaction %d already registered\n", id);
35         return NULL;
36     }
37
38     /* store information for handling the asynchronous task */
39     s = (coap_async_state_t *) coap_malloc(sizeof(coap_async_state_t) +
40             request->transport_hdr->udp.token_length);
41     if (!s) {
42         coap_log(LOG_CRIT, "coap_register_async: insufficient memory\n");
43         return NULL;
44     }
45
46     memset(s, 0, sizeof(coap_async_state_t) + request->transport_hdr->udp.token_length);
47
48     /* set COAP_ASYNC_CONFIRM according to request's type */
49     s->flags = flags & ~COAP_ASYNC_CONFIRM;
50     if (request->transport_hdr->udp.type == COAP_MESSAGE_CON)
51         s->flags |= COAP_ASYNC_CONFIRM;
52
53     s->appdata = data;
54
55     memcpy(&s->peer, peer, sizeof(coap_address_t));
56
57     if (request->transport_hdr->udp.token_length) {
58         s->tokenlen = request->transport_hdr->udp.token_length;
59         memcpy(s->token, request->transport_hdr->udp.token, request->transport_hdr->udp.token_length);
60     }
61
62     memcpy(&s->id, &id, sizeof(coap_tid_t));
63
64     coap_touch_async(s);
65
66     LL_PREPEND(context->async_state, s);
67
68     return s;
69 }
70
71 coap_async_state_t *
72 coap_find_async(coap_context_t *context, coap_tid_t id) {
73     coap_async_state_t *tmp;
74     LL_SEARCH_SCALAR(context->async_state, tmp, id, id);
75     return tmp;
76 }
77
78 int
79 coap_remove_async(coap_context_t *context, coap_tid_t id, coap_async_state_t **s) {
80     coap_async_state_t *tmp = coap_find_async(context, id);
81
82     if (tmp)
83         LL_DELETE(context->async_state, tmp);
84
85     *s = tmp;
86     return tmp != NULL;
87 }
88
89 void
90 coap_free_async(coap_async_state_t *s) {
91     if (s && (s->flags & COAP_ASYNC_RELEASE_DATA) != 0)
92         coap_free(s->appdata);
93     coap_free(s);
94 }
95
96 #else
97 void does_not_exist(); /* make some compilers happy */
98 #endif /* WITHOUT_ASYNC */