libcoap  4.2.0
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 "coap_debug.h"
20 #include "mem.h"
21 #include "utlist.h"
22 
23 /* utlist-style macros for searching pairs in linked lists */
24 #define SEARCH_PAIR(head,out,field1,val1,field2,val2) \
25  SEARCH_PAIR2(head,out,field1,val1,field2,val2,next)
26 
27 #define SEARCH_PAIR2(head,out,field1,val1,field2,val2,next) \
28  do { \
29  LL_FOREACH2(head,out,next) { \
30  if ((out)->field1 == (val1) && (out)->field2 == (val2)) break; \
31  } \
32 } while(0)
33 
36  coap_pdu_t *request, unsigned char flags, void *data) {
38  coap_tid_t id = request->tid;
39 
40  SEARCH_PAIR(context->async_state,s,session,session,id,id);
41 
42  if (s != NULL) {
43  /* We must return NULL here as the caller must know that he is
44  * responsible for releasing @p data. */
46  "asynchronous state for transaction %d already registered\n", id);
47  return NULL;
48  }
49 
50  /* store information for handling the asynchronous task */
52  if (!s) {
53  coap_log(LOG_CRIT, "coap_register_async: insufficient memory\n");
54  return NULL;
55  }
56 
57  memset(s, 0, sizeof(coap_async_state_t));
58 
59  /* set COAP_ASYNC_CONFIRM according to request's type */
60  s->flags = flags & ~COAP_ASYNC_CONFIRM;
61  if (request->type == COAP_MESSAGE_CON)
63 
64  s->appdata = data;
65  s->session = coap_session_reference( session );
66  s->id = id;
67 
68  if (request->token_length) {
69  /* A token can be up to 8 bytes */
70  s->tokenlen = (request->token_length > 8) ? 8 : request->token_length;
71  memcpy(s->token, request->token, s->tokenlen);
72  }
73 
75 
76  LL_PREPEND(context->async_state, s);
77 
78  return s;
79 }
80 
83  coap_async_state_t *tmp;
84  SEARCH_PAIR(context->async_state,tmp,session,session,id,id);
85  return tmp;
86 }
87 
88 int
91  coap_async_state_t *tmp = coap_find_async(context, session, id);
92 
93  if (tmp)
94  LL_DELETE(context->async_state,tmp);
95 
96  *s = tmp;
97  return tmp != NULL;
98 }
99 
100 void
102  if (s) {
103  if (s->session) {
105  }
106  if ((s->flags & COAP_ASYNC_RELEASE_DATA) != 0) {
107  coap_free(s->appdata);
108  }
109  coap_free(s);
110  }
111 }
112 
113 #else
114 void does_not_exist(void); /* make some compilers happy */
115 #endif /* WITHOUT_ASYNC */
uint8_t type
message type
Definition: pdu.h:288
void * appdata
This field can be used to register opaque application data with the asynchronous state object...
Definition: async.h:45
State management for asynchronous messages.
int coap_tid_t
coap_tid_t is used to store CoAP transaction id, i.e.
Definition: pdu.h:238
#define LL_PREPEND(head, add)
Definition: utlist.h:314
unsigned char flags
holds the flags to control behaviour
Definition: async.h:32
Debug.
Definition: coap_debug.h:49
struct coap_async_state_t * async_state
list of asynchronous transactions
Definition: net.h:158
coap_session_t * coap_session_reference(coap_session_t *session)
Increment reference counter on a session.
Definition: coap_session.c:71
#define SEARCH_PAIR(head, out, field1, val1, field2, val2)
Definition: async.c:24
COAP_STATIC_INLINE void coap_touch_async(coap_async_state_t *s)
Updates the time stamp of s.
Definition: async.h:143
COAP_STATIC_INLINE void * coap_malloc(size_t size)
Wrapper function to coap_malloc_type() for backwards compatibility.
Definition: mem.h:75
coap_tid_t id
transaction id
Definition: async.h:48
coap_session_t * session
transaction session
Definition: async.h:47
structure for CoAP PDUs token, if any, follows the fixed size header, then options until payload mark...
Definition: pdu.h:287
coap_async_state_t * coap_register_async(coap_context_t *context, coap_session_t *session, 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:35
#define COAP_ASYNC_CONFIRM
send confirmable response
Definition: async.h:57
uint8_t * token
first byte of token, if any, or options
Definition: pdu.h:298
int coap_remove_async(coap_context_t *context, coap_session_t *session, coap_tid_t id, coap_async_state_t **s)
Removes the state object identified by id from context.
Definition: async.c:89
#define COAP_MESSAGE_CON
Definition: pdu.h:72
COAP_STATIC_INLINE void coap_free(void *object)
Wrapper function to coap_free_type() for backwards compatibility.
Definition: mem.h:82
#define COAP_ASYNC_RELEASE_DATA
release application data on destruction
Definition: async.h:62
void coap_session_release(coap_session_t *session)
Decrement reference counter on a session.
Definition: coap_session.c:77
coap_async_state_t * coap_find_async(coap_context_t *context, coap_session_t *session, coap_tid_t id)
Retrieves the object identified by id from the list of asynchronous transactions that are registered ...
Definition: async.c:82
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:101
uint8_t token[8]
the token to use in a response
Definition: async.h:51
size_t tokenlen
length of the token
Definition: async.h:50
#define LL_DELETE(head, del)
Definition: utlist.h:385
#define coap_log(level,...)
Logging function.
Definition: coap_debug.h:122
uint8_t token_length
length of Token
Definition: pdu.h:292
Critical.
Definition: coap_debug.h:44
The CoAP stack&#39;s global state is stored in a coap_context_t object.
Definition: net.h:148
uint16_t tid
transaction id, if any, in regular host byte order
Definition: pdu.h:293