libcoap  4.1.1
 All Data Structures Files Functions Variables Typedefs Macros Groups Pages
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 "config.h"
17 
18 #include "utlist.h"
19 
20 #include "mem.h"
21 #include "debug.h"
22 #include "async.h"
23 
25 coap_register_async(coap_context_t *context, coap_address_t *peer,
26  coap_pdu_t *request, unsigned char flags, void *data) {
28  coap_tid_t id;
29 
30  coap_transaction_id(peer, request, &id);
31  LL_SEARCH_SCALAR(context->async_state,s,id,id);
32 
33  if (s != NULL) {
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);
37  return NULL;
38  }
39 
40  /* store information for handling the asynchronous task */
42  request->hdr->token_length);
43  if (!s) {
44  coap_log(LOG_CRIT, "coap_register_async: insufficient memory\n");
45  return NULL;
46  }
47 
48  memset(s, 0, sizeof(coap_async_state_t) + request->hdr->token_length);
49 
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)
54 
55  s->appdata = data;
56 
57  memcpy(&s->peer, peer, sizeof(coap_address_t));
58 
59  if (request->hdr->token_length) {
60  s->tokenlen = request->hdr->token_length;
61  memcpy(s->token, request->hdr->token, request->hdr->token_length);
62  }
63 
64  memcpy(&s->id, &id, sizeof(coap_tid_t));
65 
67 
68  LL_PREPEND(context->async_state, s);
69 
70  return s;
71 }
72 
75  coap_async_state_t *tmp;
76  LL_SEARCH_SCALAR(context->async_state,tmp,id,id);
77  return tmp;
78 }
79 
80 int
82  coap_async_state_t **s) {
83  coap_async_state_t *tmp = coap_find_async(context, id);
84 
85  if (tmp)
86  LL_DELETE(context->async_state,tmp);
87 
88  *s = tmp;
89  return tmp != NULL;
90 }
91 
92 void
94  if (s && (s->flags & COAP_ASYNC_RELEASE_DATA) != 0)
95  coap_free(s->appdata);
96  coap_free(s);
97 }
98 
99 #else
100 void does_not_exist(); /* make some compilers happy */
101 #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:174
state management for asynchronous messages
int coap_tid_t
Definition: pdu.h:155
#define LL_PREPEND(head, add)
Definition: utlist.h:287
unsigned char flags
holds the flags to control behaviour
Definition: async.h:33
coap_address_t peer
the peer to notify
Definition: async.h:52
#define coap_malloc(size)
Definition: mem.h:15
struct coap_async_state_t * async_state
list of asynchronous transactions
Definition: net.h:104
coap_hdr_t * hdr
Definition: pdu.h:209
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:498
#define debug(...)
Definition: debug.h:55
coap_tid_t id
transaction id
Definition: async.h:48
#define coap_free(size)
Definition: mem.h:16
Header structure for CoAP PDUs.
Definition: pdu.h:206
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:25
static coap_tid_t id
Definition: tiny.c:23
#define COAP_ASYNC_CONFIRM
send confirmable response
Definition: async.h:59
#define COAP_MESSAGE_CON
Definition: pdu.h:43
#define COAP_ASYNC_RELEASE_DATA
release application data on destruction
Definition: async.h:64
static void coap_touch_async(coap_async_state_t *s)
Updates the time stamp of s.
Definition: async.h:141
int flags
Definition: client.c:26
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:81
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:74
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:93
size_t tokenlen
length of the token
Definition: async.h:53
unsigned int token_length
Definition: pdu.h:169
#define LL_DELETE(head, del)
Definition: utlist.h:306
unsigned char token[]
the token to use in a response
Definition: async.h:54
unsigned int type
Definition: pdu.h:170
#define LL_SEARCH_SCALAR(head, out, field, val)
Definition: utlist.h:367
#define coap_log(...)
Definition: debug.h:47
The CoAP stack's global state is stored in a coap_context_t object.
Definition: net.h:97