libcoap 4.3.5-develop-379c7f4
Loading...
Searching...
No Matches
coap_async.c
Go to the documentation of this file.
1/* coap_async.c -- state management for asynchronous messages
2 *
3 * Copyright (C) 2010,2011,2021-2026 Olaf Bergmann <bergmann@tzi.org>
4 *
5 * SPDX-License-Identifier: BSD-2-Clause
6 *
7 * This file is part of the CoAP library libcoap. Please see
8 * README for terms of use.
9 */
10
15
17
18COAP_API void
19coap_context_set_async_limits(coap_context_t *context, size_t max_pending,
20 size_t max_per_session) {
21#if COAP_ASYNC_SUPPORT
22 coap_lock_lock(return);
23 context->max_async = max_pending;
24 context->max_async_per_session = max_per_session;
26#else /* ! COAP_ASYNC_SUPPORT */
27 (void)context;
28 (void)max_pending;
29 (void)max_per_session;
30#endif /* ! COAP_ASYNC_SUPPORT */
31}
32
33#if COAP_ASYNC_SUPPORT
34#include <stdio.h>
35
36/* utlist-style macros for searching pairs in linked lists */
37#define SEARCH_PAIR(head,out,field1,val1,field2,val2,field3,val3) \
38 SEARCH_PAIR3(head,out,field1,val1,field2,val2,field3,val3,next)
39
40#define SEARCH_PAIR3(head,out,field1,val1,field2,val2,field3,val3,next) \
41 do { \
42 LL_FOREACH2(head,out,next) { \
43 if ((out)->field1 == (val1) && (out)->field2 == (val2) && \
44 ((val2) == 0 || memcmp((out)->field3, (val3), (val2)) == 0)) break; \
45 } \
46 } while(0)
47
48int
50 return 1;
51}
52
55 const coap_pdu_t *request, coap_tick_t delay) {
56 coap_async_t *async;
57
58 coap_lock_lock(return NULL);
59 async = coap_register_async_lkd(session, request, delay);
61 return async;
62}
63
65coap_register_async_lkd(coap_session_t *session,
66 const coap_pdu_t *request, coap_tick_t delay) {
67 coap_async_t *s;
68 coap_context_t *context = session->context;
69 size_t len;
70 const uint8_t *data;
71
73 if (!COAP_PDU_IS_REQUEST(request))
74 return NULL;
75 if ((context->max_async && context->async_count >= context->max_async) ||
76 (context->max_async_per_session && session->async_count >= context->max_async_per_session))
77 return NULL;
78
79 SEARCH_PAIR(session->context->async_state, s,
80 session, session,
81 pdu->actual_token.length, request->actual_token.length,
82 pdu->actual_token.s, request->actual_token.s);
83
84 if (s != NULL) {
85 size_t i;
86 char outbuf[2*8 + 1];
87 size_t outbuflen;
88
89 outbuf[0] = '\000';
90 for (i = 0; i < request->actual_token.length; i++) {
91 /* Output maybe truncated */
92 outbuflen = strlen(outbuf);
93 snprintf(&outbuf[outbuflen], sizeof(outbuf)-outbuflen,
94 "%02x", request->token[i]);
95 }
96 coap_log_debug("asynchronous state for token '%s' already registered\n", outbuf);
97 return NULL;
98 }
99
100 /* store information for handling the asynchronous task */
102 if (!s) {
103 coap_log_crit("coap_register_async: insufficient memory\n");
104 return NULL;
105 }
106
107 memset(s, 0, sizeof(coap_async_t));
108 s->session = coap_session_reference_lkd(session);
109 session->async_count++;
110 context->async_count++;
111 LL_PREPEND(context->async_state, s);
112
113 /* Note that this generates a new MID */
114 s->pdu = coap_pdu_duplicate_lkd(request, session, request->actual_token.length,
116 if (s->pdu == NULL) {
117 coap_free_async_lkd(session, s);
118 coap_log_crit("coap_register_async: insufficient memory\n");
119 return NULL;
120 }
121
122 if (coap_get_data(request, &len, &data)) {
123 coap_add_data(s->pdu, len, data);
124 }
125
126 /* Snapshot local address; addr_info.local may change before the async fires (multicast). */
127 coap_address_copy(&s->local_if, &session->addr_info.local);
128
129 coap_async_set_delay_lkd(s, delay);
130
131 return s;
132}
133
134COAP_API void
136 coap_lock_lock(return);
137 coap_async_trigger_lkd(async);
139}
140
141void
142coap_async_trigger_lkd(coap_async_t *async) {
143 assert(async != NULL);
145 coap_ticks(&async->delay);
146
147 coap_log_debug(" %s: Async request triggered\n",
148 coap_session_str(async->session));
149 coap_update_io_timer(async->session->context, 0);
150}
151
152COAP_API void
154 coap_lock_lock(return);
155 coap_async_set_delay_lkd(async, delay);
157}
158
159void
160coap_async_set_delay_lkd(coap_async_t *async, coap_tick_t delay) {
161 coap_tick_t now;
162
164 assert(async != NULL);
165 coap_ticks(&now);
166
167 if (delay) {
168 async->delay = now + delay;
169 coap_update_io_timer(async->session->context, delay);
170 coap_log_debug(" %s: Async request delayed for %u.%03u secs\n",
171 coap_session_str(async->session),
172 (unsigned int)(delay / COAP_TICKS_PER_SECOND),
173 (unsigned int)((delay % COAP_TICKS_PER_SECOND) *
174 1000 / COAP_TICKS_PER_SECOND));
175 } else {
176 async->delay = 0;
177 coap_log_debug(" %s: Async request indefinitely delayed\n",
178 coap_session_str(async->session));
179 }
180}
181
184 coap_async_t *tmp;
185
186 coap_lock_lock(return NULL);
187 tmp = coap_find_async_lkd(session, token);
189 return tmp;
190}
191
193coap_find_async_lkd(coap_session_t *session, coap_bin_const_t token) {
194 coap_async_t *tmp;
195
197 SEARCH_PAIR(session->context->async_state, tmp,
198 session, session,
199 pdu->actual_token.length, token.length,
200 pdu->actual_token.s, token.s);
201 return tmp;
202}
203
204static void
205coap_free_async_sub(coap_context_t *context, coap_async_t *async) {
206 if (async) {
207 LL_DELETE(context->async_state,async);
208 if (async->session) {
209 context->async_count--;
210 async->session->async_count--;
211 coap_session_release_lkd(async->session);
212 async->session = NULL;
213 }
214 if (async->pdu) {
215 coap_delete_pdu_lkd(async->pdu);
216 async->pdu = NULL;
217 }
218 if (async->app_cb && async->app_data) {
219 coap_lock_callback(async->app_cb(async->app_data));
220 }
222 }
223}
224
225COAP_API void
227 coap_lock_lock(return);
228 coap_free_async_lkd(session, async);
230}
231
232void
233coap_free_async_lkd(coap_session_t *session, coap_async_t *async) {
234 coap_free_async_sub(session->context, async);
235}
236
237void
238coap_delete_all_async(coap_context_t *context) {
239 coap_async_t *astate, *tmp;
240
241 LL_FOREACH_SAFE(context->async_state, astate, tmp) {
242 coap_free_async_sub(context, astate);
243 }
244 context->async_state = NULL;
245}
246
247COAP_API void
248coap_async_set_app_data(coap_async_t *async_entry, void *app_data) {
249 coap_lock_lock(return);
250 coap_async_set_app_data2_lkd(async_entry, app_data, NULL);
252}
253
254COAP_API void *
255coap_async_set_app_data2(coap_async_t *async_entry, void *app_data,
257 void *old_data;
258
259 coap_lock_lock(return NULL);
260 old_data = coap_async_set_app_data2_lkd(async_entry, app_data, callback);
262 return old_data;
263}
264
265void *
266coap_async_set_app_data2_lkd(coap_async_t *async_entry, void *app_data,
268 void *old_data = async_entry->app_data;
269
270 async_entry->app_data = app_data;
271 async_entry->app_cb = app_data ? callback : NULL;
272 return old_data;
273}
274
275void *
277 return async->app_data;
278}
279
280#else /* ! COAP_ASYNC_SUPPORT */
281
282int
284 return 0;
285}
286
289 const coap_pdu_t *request,
290 coap_tick_t delay) {
291 (void)session;
292 (void)request;
293 (void)delay;
294 return NULL;
295}
296
297void
299 (void)async;
300 (void)delay;
301}
302
303void
305 (void)session;
306 (void)async;
307}
308
311 coap_bin_const_t token) {
312 (void)session;
313 (void)token;
314 return NULL;
315}
316
317COAP_API void
318coap_async_set_app_data(coap_async_t *async, void *app_data) {
319 (void)async;
320 (void)app_data;
321}
322
323COAP_API void *
326 (void)async;
327 (void)app_data;
328 (void)callback;
329 return NULL;
330}
331
332void *
334 (void)async;
335 return NULL;
336}
337
338#endif /* ! COAP_ASYNC_SUPPORT */
void coap_address_copy(coap_address_t *dst, const coap_address_t *src)
struct coap_async_t coap_async_t
Async Entry information.
void coap_update_io_timer(coap_context_t *context, coap_tick_t delay)
Update when to continue with I/O processing, unless packets come in in the meantime.
Definition coap_io.c:70
Library specific build wrapper for coap_internal.h.
#define COAP_API
@ COAP_STRING
Definition coap_mem.h:33
void * coap_malloc_type(coap_memory_tag_t type, size_t size)
Allocates a chunk of size bytes and returns a pointer to the newly allocated memory.
void coap_free_type(coap_memory_tag_t type, void *p)
Releases the memory that was allocated by coap_malloc_type().
#define NULL
Definition coap_option.h:30
uint64_t coap_tick_t
This data type represents internal timer ticks with COAP_TICKS_PER_SECOND resolution.
Definition coap_time.h:149
#define COAP_TICKS_PER_SECOND
Use ms resolution on POSIX systems.
Definition coap_time.h:164
COAP_API void * coap_async_set_app_data2(coap_async_t *async, void *app_data, coap_app_data_free_callback_t callback)
Stores data with the given async, returning the previously stored value or NULL.
Definition coap_async.c:324
void coap_free_async(coap_session_t *session, coap_async_t *async)
Releases the memory that was allocated by coap_register_async() for the object async.
Definition coap_async.c:304
coap_async_t * coap_find_async(coap_session_t *session, coap_bin_const_t token)
Retrieves the object identified by token from the list of asynchronous transactions that are register...
Definition coap_async.c:310
COAP_API void coap_context_set_async_limits(coap_context_t *context, size_t max_pending, size_t max_per_session)
Set the maximum number of pending asynchronous requests for the context and for each session.
Definition coap_async.c:19
void * coap_async_get_app_data(const coap_async_t *async)
Gets the application data pointer held in async.
Definition coap_async.c:333
COAP_API void coap_async_set_app_data(coap_async_t *async, void *app_data)
Set the application data pointer held in async.
Definition coap_async.c:318
COAP_API void coap_async_trigger(coap_async_t *async)
Trigger the registered async.
coap_async_t * coap_register_async(coap_session_t *session, const coap_pdu_t *request, coap_tick_t delay)
Allocates a new coap_async_t object and fills its fields according to the given request.
Definition coap_async.c:288
void coap_async_set_delay(coap_async_t *async, coap_tick_t delay)
Update the delay timeout, so changing when the registered async triggers.
Definition coap_async.c:298
void coap_ticks(coap_tick_t *t)
Returns the current value of an internal tick counter.
Definition coap_time.c:90
#define coap_lock_callback(func)
Dummy for no thread-safe code.
#define coap_lock_unlock()
Dummy for no thread-safe code.
#define coap_lock_check_locked()
Dummy for no thread-safe code.
#define coap_lock_lock(failed)
Dummy for no thread-safe code.
#define coap_log_debug(...)
Definition coap_debug.h:126
const char * coap_session_str(const coap_session_t *session)
Get session description.
#define coap_log_crit(...)
Definition coap_debug.h:96
void coap_delete_pdu_lkd(coap_pdu_t *pdu)
Dispose of an CoAP PDU and free off associated storage.
Definition coap_pdu.c:192
coap_pdu_t * coap_pdu_duplicate_lkd(const coap_pdu_t *old_pdu, coap_session_t *session, size_t token_length, const uint8_t *token, coap_opt_filter_t *drop_options, coap_bool_t expand_opt_abb)
Duplicate an existing PDU.
Definition coap_pdu.c:232
#define COAP_PDU_IS_REQUEST(pdu)
int coap_get_data(const coap_pdu_t *pdu, size_t *len, const uint8_t **data)
Retrieves the length and data pointer of specified PDU.
Definition coap_pdu.c:961
int coap_add_data(coap_pdu_t *pdu, size_t len, const uint8_t *data)
Adds given data to the pdu that is passed as first parameter.
Definition coap_pdu.c:930
@ COAP_BOOL_FALSE
Definition coap_pdu.h:306
void coap_session_release_lkd(coap_session_t *session)
Decrement reference counter on a session.
coap_session_t * coap_session_reference_lkd(coap_session_t *session)
Increment reference counter on a session.
void(* coap_app_data_free_callback_t)(void *data)
Callback to free off the app data when the entry is being deleted / freed off.
int coap_async_is_supported(void)
Check whether ASYNC (separate responses) is available.
Definition coap_async.c:283
coap_address_t local
local address and port
Definition coap_io.h:59
CoAP binary data definition with const data.
Definition coap_str.h:65
size_t length
length of binary data
Definition coap_str.h:66
const uint8_t * s
read-only binary data
Definition coap_str.h:67
The CoAP stack's global state is stored in a coap_context_t object.
structure for CoAP PDUs
uint8_t * token
first byte of token (or extended length bytes prefix), if any, or options
coap_bin_const_t actual_token
Actual token in pdu.
Abstraction of virtual session that can be attached to coap_context_t (client) or coap_endpoint_t (se...
coap_addr_tuple_t addr_info
remote/local address info
coap_context_t * context
session's context