libcoap  4.2.1
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 #include "coap_internal.h"
15 
16 #ifndef WITHOUT_ASYNC
17 
18 /* utlist-style macros for searching pairs in linked lists */
19 #define SEARCH_PAIR(head,out,field1,val1,field2,val2) \
20  SEARCH_PAIR2(head,out,field1,val1,field2,val2,next)
21 
22 #define SEARCH_PAIR2(head,out,field1,val1,field2,val2,next) \
23  do { \
24  LL_FOREACH2(head,out,next) { \
25  if ((out)->field1 == (val1) && (out)->field2 == (val2)) break; \
26  } \
27 } while(0)
28 
31  coap_pdu_t *request, unsigned char flags, void *data) {
33  coap_tid_t id = request->tid;
34 
35  SEARCH_PAIR(context->async_state,s,session,session,id,id);
36 
37  if (s != NULL) {
38  /* We must return NULL here as the caller must know that he is
39  * responsible for releasing @p data. */
41  "asynchronous state for transaction %d already registered\n", id);
42  return NULL;
43  }
44 
45  /* store information for handling the asynchronous task */
47  if (!s) {
48  coap_log(LOG_CRIT, "coap_register_async: insufficient memory\n");
49  return NULL;
50  }
51 
52  memset(s, 0, sizeof(coap_async_state_t));
53 
54  /* set COAP_ASYNC_CONFIRM according to request's type */
55  s->flags = flags & ~COAP_ASYNC_CONFIRM;
56  if (request->type == COAP_MESSAGE_CON)
58 
59  s->appdata = data;
60  s->session = coap_session_reference( session );
61  s->id = id;
62 
63  if (request->token_length) {
64  /* A token can be up to 8 bytes */
65  s->tokenlen = (request->token_length > 8) ? 8 : request->token_length;
66  memcpy(s->token, request->token, s->tokenlen);
67  }
68 
70 
71  LL_PREPEND(context->async_state, s);
72 
73  return s;
74 }
75 
78  coap_async_state_t *tmp;
79  SEARCH_PAIR(context->async_state,tmp,session,session,id,id);
80  return tmp;
81 }
82 
83 int
86  coap_async_state_t *tmp = coap_find_async(context, session, id);
87 
88  if (tmp)
89  LL_DELETE(context->async_state,tmp);
90 
91  *s = tmp;
92  return tmp != NULL;
93 }
94 
95 void
97  if (s) {
98  if (s->session) {
100  }
101  if ((s->flags & COAP_ASYNC_RELEASE_DATA) != 0) {
102  coap_free(s->appdata);
103  }
104  coap_free(s);
105  }
106 }
107 
108 #else
109 void does_not_exist(void); /* make some compilers happy */
110 #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
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:55
struct coap_async_state_t * async_state
list of asynchronous transactions
Definition: net.h:157
coap_session_t * coap_session_reference(coap_session_t *session)
Increment reference counter on a session.
Definition: coap_session.c:68
#define SEARCH_PAIR(head, out, field1, val1, field2, val2)
Definition: async.c:19
COAP_STATIC_INLINE void coap_touch_async(coap_async_state_t *s)
Updates the time stamp of s.
Definition: async.h:142
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:47
coap_session_t * session
transaction session
Definition: async.h:46
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:30
#define COAP_ASYNC_CONFIRM
send confirmable response
Definition: async.h:56
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:84
#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:61
void coap_session_release(coap_session_t *session)
Decrement reference counter on a session.
Definition: coap_session.c:74
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:77
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:96
uint8_t token[8]
the token to use in a response
Definition: async.h:50
size_t tokenlen
length of the token
Definition: async.h:49
#define LL_DELETE(head, del)
Definition: utlist.h:385
#define coap_log(level,...)
Logging function.
Definition: coap_debug.h:129
uint8_t token_length
length of Token
Definition: pdu.h:292
Critical.
Definition: coap_debug.h:50
The CoAP stack&#39;s global state is stored in a coap_context_t object.
Definition: net.h:147
uint16_t tid
transaction id, if any, in regular host byte order
Definition: pdu.h:293
Pulls together all the internal only header files.