libcoap  4.1.2
async.c
Go to the documentation of this file.
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 
14 #ifndef WITHOUT_ASYNC
15 
16 #include "coap_config.h"
17 #include "coap.h"
18 #include "async.h"
19 #include "debug.h"
20 #include "mem.h"
21 #include "utlist.h"
22 
25  coap_pdu_t *request, unsigned char flags, void *data) {
27  coap_tid_t id;
28 
29  coap_transaction_id(peer, request, &id);
30  LL_SEARCH_SCALAR(context->async_state,s,id,id);
31 
32  if (s != NULL) {
33  /* We must return NULL here as the caller must know that he is
34  * responsible for releasing @p data. */
35  debug("asynchronous state for transaction %d already registered\n", id);
36  return NULL;
37  }
38 
39  /* store information for handling the asynchronous task */
41  request->hdr->token_length);
42  if (!s) {
43  coap_log(LOG_CRIT, "coap_register_async: insufficient memory\n");
44  return NULL;
45  }
46 
47  memset(s, 0, sizeof(coap_async_state_t) + request->hdr->token_length);
48 
49  /* set COAP_ASYNC_CONFIRM according to request's type */
50  s->flags = flags & ~COAP_ASYNC_CONFIRM;
51  if (request->hdr->type == COAP_MESSAGE_CON)
53 
54  s->appdata = data;
55 
56  memcpy(&s->peer, peer, sizeof(coap_address_t));
57 
58  if (request->hdr->token_length) {
59  s->tokenlen = request->hdr->token_length;
60  memcpy(s->token, request->hdr->token, request->hdr->token_length);
61  }
62 
63  memcpy(&s->id, &id, sizeof(coap_tid_t));
64 
66 
67  LL_PREPEND(context->async_state, s);
68 
69  return s;
70 }
71 
74  coap_async_state_t *tmp;
75  LL_SEARCH_SCALAR(context->async_state,tmp,id,id);
76  return tmp;
77 }
78 
79 int
81  coap_async_state_t **s) {
82  coap_async_state_t *tmp = coap_find_async(context, id);
83 
84  if (tmp)
85  LL_DELETE(context->async_state,tmp);
86 
87  *s = tmp;
88  return tmp != NULL;
89 }
90 
91 void
93  if (s && (s->flags & COAP_ASYNC_RELEASE_DATA) != 0)
94  coap_free(s->appdata);
95  coap_free(s);
96 }
97 
98 #else
99 void does_not_exist(); /* make some compilers happy */
100 #endif /* WITHOUT_ASYNC */
void * appdata
This field can be used to register opaque application data with the asynchronous state object...
Definition: async.h:45
unsigned char token[]
Definition: pdu.h:192
static void * coap_malloc(size_t size)
Wrapper function to coap_malloc_type() for backwards compatibility.
Definition: mem.h:70
multi-purpose address abstraction
Definition: address.h:59
State management for asynchronous messages.
int coap_tid_t
coap_tid_t is used to store CoAP transaction id, i.e.
Definition: pdu.h:163
#define LL_PREPEND(head, add)
Definition: utlist.h:309
unsigned char flags
holds the flags to control behaviour
Definition: async.h:32
coap_address_t peer
the peer to notify
Definition: async.h:49
struct coap_async_state_t * async_state
list of asynchronous transactions
Definition: net.h:83
coap_hdr_t * hdr
Address of the first byte of the CoAP message.
Definition: pdu.h:229
void coap_transaction_id(const coap_address_t *peer, const coap_pdu_t *pdu, coap_tid_t *id)
Calculates a unique transaction id from given arguments peer and pdu.
Definition: net.c:495
#define debug(...)
Definition: debug.h:66
coap_tid_t id
transaction id
Definition: async.h:47
Header structure for CoAP PDUs.
Definition: pdu.h:227
coap_async_state_t * coap_register_async(coap_context_t *context, coap_address_t *peer, coap_pdu_t *request, unsigned char flags, void *data)
Allocates a new coap_async_state_t object and fills its fields according to the given request...
Definition: async.c:24
#define COAP_ASYNC_CONFIRM
send confirmable response
Definition: async.h:57
#define COAP_MESSAGE_CON
Definition: pdu.h:43
#define COAP_ASYNC_RELEASE_DATA
release application data on destruction
Definition: async.h:62
Definition: debug.h:29
static void coap_touch_async(coap_async_state_t *s)
Updates the time stamp of s.
Definition: async.h:140
int coap_remove_async(coap_context_t *context, coap_tid_t id, coap_async_state_t **s)
Removes the state object identified by id from context.
Definition: async.c:80
coap_async_state_t * coap_find_async(coap_context_t *context, coap_tid_t id)
Retrieves the object identified by id from the list of asynchronous transactions that are registered ...
Definition: async.c:73
void coap_free_async(coap_async_state_t *s)
Releases the memory that was allocated by coap_async_state_init() for the object s.
Definition: async.c:92
size_t tokenlen
length of the token
Definition: async.h:50
unsigned int token_length
Definition: pdu.h:186
static void coap_free(void *object)
Wrapper function to coap_free_type() for backwards compatibility.
Definition: mem.h:77
#define LL_DELETE(head, del)
Definition: utlist.h:349
unsigned char token[]
the token to use in a response
Definition: async.h:51
unsigned int type
Definition: pdu.h:187
#define LL_SEARCH_SCALAR(head, out, field, val)
Definition: utlist.h:440
#define coap_log(...)
Definition: debug.h:58
The CoAP stack&#39;s global state is stored in a coap_context_t object.
Definition: net.h:76