libcoap 4.3.4
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-2023 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
16#include "coap3/coap_internal.h"
17
18#if COAP_ASYNC_SUPPORT
19#include <stdio.h>
20
21/* utlist-style macros for searching pairs in linked lists */
22#define SEARCH_PAIR(head,out,field1,val1,field2,val2,field3,val3) \
23 SEARCH_PAIR3(head,out,field1,val1,field2,val2,field3,val3,next)
24
25#define SEARCH_PAIR3(head,out,field1,val1,field2,val2,field3,val3,next) \
26 do { \
27 LL_FOREACH2(head,out,next) { \
28 if ((out)->field1 == (val1) && (out)->field2 == (val2) && \
29 ((val2) == 0 || memcmp((out)->field3, (val3), (val2)) == 0)) break; \
30 } \
31 } while(0)
32
33int
35 return 1;
36}
37
40 const coap_pdu_t *request, coap_tick_t delay) {
41 coap_async_t *s;
42 size_t len;
43 const uint8_t *data;
44
45 if (!COAP_PDU_IS_REQUEST(request))
46 return NULL;
47
48 SEARCH_PAIR(session->context->async_state, s,
49 session, session,
50 pdu->actual_token.length, request->actual_token.length,
51 pdu->actual_token.s, request->actual_token.s);
52
53 if (s != NULL) {
54 size_t i;
55 char outbuf[2*8 + 1];
56 size_t outbuflen;
57
58 outbuf[0] = '\000';
59 for (i = 0; i < request->actual_token.length; i++) {
60 /* Output maybe truncated */
61 outbuflen = strlen(outbuf);
62 snprintf(&outbuf[outbuflen], sizeof(outbuf)-outbuflen,
63 "%02x", request->token[i]);
64 }
65 coap_log_debug("asynchronous state for token '%s' already registered\n", outbuf);
66 return NULL;
67 }
68
69 /* store information for handling the asynchronous task */
71 if (!s) {
72 coap_log_crit("coap_register_async: insufficient memory\n");
73 return NULL;
74 }
75
76 memset(s, 0, sizeof(coap_async_t));
77 LL_PREPEND(session->context->async_state, s);
78
79 /* Note that this generates a new MID */
80 s->pdu = coap_pdu_duplicate(request, session, request->actual_token.length,
81 request->actual_token.s, NULL);
82 if (s->pdu == NULL) {
83 coap_free_async(session, s);
84 coap_log_crit("coap_register_async: insufficient memory\n");
85 return NULL;
86 }
87
88 if (coap_get_data(request, &len, &data)) {
89 coap_add_data(s->pdu, len, data);
90 }
91
92 s->session = coap_session_reference(session);
93
94 coap_async_set_delay(s, delay);
95
96 return s;
97}
98
99void
101 assert(async != NULL);
102 coap_ticks(&async->delay);
103
104 coap_log_debug(" %s: Async request triggered\n",
105 coap_session_str(async->session));
106#ifdef COAP_EPOLL_SUPPORT
107 coap_update_epoll_timer(async->session->context, 0);
108#endif /* COAP_EPOLL_SUPPORT */
109}
110
111
112void
114 coap_tick_t now;
115
116 assert(async != NULL);
117 coap_ticks(&now);
118
119 if (delay) {
120 async->delay = now + delay;
121#ifdef COAP_EPOLL_SUPPORT
122 coap_update_epoll_timer(async->session->context, delay);
123#endif /* COAP_EPOLL_SUPPORT */
124 coap_log_debug(" %s: Async request delayed for %u.%03u secs\n",
125 coap_session_str(async->session),
126 (unsigned int)(delay / COAP_TICKS_PER_SECOND),
127 (unsigned int)((delay % COAP_TICKS_PER_SECOND) *
128 1000 / COAP_TICKS_PER_SECOND));
129 } else {
130 async->delay = 0;
131 coap_log_debug(" %s: Async request indefinately delayed\n",
132 coap_session_str(async->session));
133 }
134}
135
138 coap_async_t *tmp;
139
140 SEARCH_PAIR(session->context->async_state, tmp,
141 session, session,
142 pdu->actual_token.length, token.length,
143 pdu->actual_token.s, token.s);
144 return tmp;
145}
146
147static void
148coap_free_async_sub(coap_context_t *context, coap_async_t *s) {
149 if (s) {
150 LL_DELETE(context->async_state,s);
151 if (s->session) {
152 coap_session_release(s->session);
153 }
154 if (s->pdu) {
155 coap_delete_pdu(s->pdu);
156 s->pdu = NULL;
157 }
159 }
160}
161
162void
164 coap_free_async_sub(session->context, s);
165}
166
167void
168coap_delete_all_async(coap_context_t *context) {
169 coap_async_t *astate, *tmp;
170
171 LL_FOREACH_SAFE(context->async_state, astate, tmp) {
172 coap_free_async_sub(context, astate);
173 }
174 context->async_state = NULL;
175}
176
177void
178coap_async_set_app_data(coap_async_t *async, void *app_data) {
179 async->appdata = app_data;
180}
181
182void *
184 return async->appdata;
185}
186
187#else /* ! COAP_ASYNC_SUPPORT */
188
189int
191 return 0;
192}
193
196 const coap_pdu_t *request,
197 coap_tick_t delay) {
198 (void)session;
199 (void)request;
200 (void)delay;
201 return NULL;
202}
203
204void
206 (void)async;
207 (void)delay;
208}
209
210void
212 (void)session;
213 (void)async;
214}
215
218 coap_bin_const_t token) {
219 (void)session;
220 (void)token;
221 return NULL;
222}
223
224void
225coap_async_set_app_data(coap_async_t *async, void *app_data) {
226 (void)async;
227 (void)app_data;
228}
229
230void *
232 (void)async;
233 return NULL;
234}
235
236#endif /* ! COAP_ASYNC_SUPPORT */
struct coap_async_t coap_async_t
Async Entry information.
Pulls together all the internal only header files.
void coap_update_epoll_timer(coap_context_t *context, coap_tick_t delay)
Update the epoll timer fd as to when it is to trigger.
@ COAP_STRING
Definition: coap_mem.h:38
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().
uint64_t coap_tick_t
This data type represents internal timer ticks with COAP_TICKS_PER_SECOND resolution.
Definition: coap_time.h:144
#define COAP_TICKS_PER_SECOND
Use ms resolution on POSIX systems.
Definition: coap_time.h:159
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:211
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:195
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:205
void * coap_async_get_app_data(const coap_async_t *async)
Gets the application data pointer held in async.
Definition: coap_async.c:231
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:217
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:225
int coap_async_is_supported(void)
Returns 1 if libcoap was built with separate messages enabled, 0 otherwise.
Definition: coap_async.c:190
void coap_ticks(coap_tick_t *)
Returns the current value of an internal tick counter.
#define coap_log_debug(...)
Definition: coap_debug.h:120
const char * coap_session_str(const coap_session_t *session)
Get session description.
#define coap_log_crit(...)
Definition: coap_debug.h:90
#define COAP_PDU_IS_REQUEST(pdu)
void coap_delete_pdu(coap_pdu_t *pdu)
Dispose of an CoAP PDU and frees associated storage.
Definition: coap_pdu.c:163
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:797
coap_pdu_t * coap_pdu_duplicate(const coap_pdu_t *old_pdu, coap_session_t *session, size_t token_length, const uint8_t *token, coap_opt_filter_t *drop_options)
Duplicate an existing PDU.
Definition: coap_pdu.c:179
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:766
void coap_session_release(coap_session_t *session)
Decrement reference counter on a session.
Definition: coap_session.c:354
coap_session_t * coap_session_reference(coap_session_t *session)
Increment reference counter on a session.
Definition: coap_session.c:348
CoAP binary data definition with const data.
Definition: coap_str.h:64
size_t length
length of binary data
Definition: coap_str.h:65
const uint8_t * s
read-only binary data
Definition: coap_str.h:66
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_context_t * context
session's context