xref: /btstack/src/btstack_run_loop.h (revision 86382a797521c0e8511e3614a64fa0c9a5c4c995)
1 /*
2  * Copyright (C) 2014 BlueKitchen GmbH
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the copyright holders nor the names of
14  *    contributors may be used to endorse or promote products derived
15  *    from this software without specific prior written permission.
16  * 4. Any redistribution, use, or modification is done solely for
17  *    personal benefit and not for any commercial purpose or for
18  *    monetary gain.
19  *
20  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
24  * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * Please inquire about commercial licensing options at
34  * [email protected]
35  *
36  */
37 
38 /**
39  *  BTstack Run Loop Abstraction
40  *
41  *  Provides generic functionality required by the Bluetooth stack and example applications:
42  *  - asynchronous IO for various devices Serial, USB, network sockets, console
43  *  - system time in milliseconds
44  *  - single shot timers
45  *  - integration for threaded environments
46  */
47 
48 #ifndef BTSTACK_RUN_LOOP_H
49 #define BTSTACK_RUN_LOOP_H
50 
51 #include "btstack_config.h"
52 
53 #include "btstack_bool.h"
54 #include "btstack_linked_list.h"
55 #include "btstack_defines.h"
56 
57 #include <stdint.h>
58 
59 #ifdef ENABLE_TESTING_SUPPORT
60 typedef uint64_t btstack_time_t;
61 #define PRIbtstack_time_t PRIu64
62 #else
63 typedef uint32_t btstack_time_t;
64 #define PRIbtstack_time_t PRIu32
65 #endif
66 
67 #if defined __cplusplus
68 extern "C" {
69 #endif
70 
71 /**
72  * Callback types for run loop data sources
73  */
74 typedef enum {
75     DATA_SOURCE_CALLBACK_POLL  = 1 << 0,
76     DATA_SOURCE_CALLBACK_READ  = 1 << 1,
77     DATA_SOURCE_CALLBACK_WRITE = 1 << 2,
78 } btstack_data_source_callback_type_t;
79 
80 typedef struct btstack_data_source {
81     // linked item
82     btstack_linked_item_t item;
83 
84     // item to watch in run loop
85     union {
86         // file descriptor for posix systems
87         int  fd;
88         // handle on windows
89         void * handle;
90     } source;
91 
92     // callback to call for enabled callback types
93     void (*process)(struct btstack_data_source *ds, btstack_data_source_callback_type_t callback_type);
94 
95     // flags storing enabled callback types
96     uint16_t flags;
97 
98 } btstack_data_source_t;
99 
100 typedef struct btstack_timer_source {
101     btstack_linked_item_t item;
102     // timeout in system ticks (HAVE_EMBEDDED_TICK) or milliseconds (HAVE_EMBEDDED_TIME_MS)
103     btstack_time_t timeout;
104     // will be called when timer fired
105     void  (*process)(struct btstack_timer_source *ts);
106     void * context;
107 } btstack_timer_source_t;
108 
109 typedef struct btstack_run_loop {
110 	void (*init)(void);
111 	void (*add_data_source)(btstack_data_source_t * data_source);
112 	bool (*remove_data_source)(btstack_data_source_t * data_source);
113 	void (*enable_data_source_callbacks)(btstack_data_source_t * data_source, uint16_t callbacks);
114 	void (*disable_data_source_callbacks)(btstack_data_source_t * data_source, uint16_t callbacks);
115 	void (*set_timer)(btstack_timer_source_t * timer, uint32_t timeout_in_ms);
116 	void (*add_timer)(btstack_timer_source_t *timer);
117 	bool  (*remove_timer)(btstack_timer_source_t *timer);
118 	void (*execute)(void);
119 	void (*dump_timer)(void);
120 	uint32_t (*get_time_ms)(void);
121 	void (*poll_data_sources_from_irq)(void);
122 	void (*execute_on_main_thread)(btstack_context_callback_registration_t * callback_registration);
123 	void (*trigger_exit)(void);
124 } btstack_run_loop_t;
125 
126 
127 /*
128  *  BTstack Run Loop Base Implementation
129  *  Portable implementation of timer and data source management as base for platform specific implementations
130  */
131 
132 // private data (access only by run loop implementations)
133 extern btstack_linked_list_t btstack_run_loop_base_timers;
134 extern btstack_linked_list_t btstack_run_loop_base_data_sources;
135 extern btstack_linked_list_t btstack_run_loop_base_callbacks;
136 
137 /**
138  * @brief Init
139  */
140 void btstack_run_loop_base_init(void);
141 
142 /**
143  * @brief Add timer source.
144  * @param timer to add
145  */
146 void btstack_run_loop_base_add_timer(btstack_timer_source_t * timer);
147 
148 /**
149  * @brief Remove timer source.
150  * @param timer to remove
151  * @return true if timer was removed
152  */
153 bool  btstack_run_loop_base_remove_timer(btstack_timer_source_t * timer);
154 
155 /**
156  * @brief Process timers: remove expired timers from list and call their process function
157  * @param now
158  */
159 void  btstack_run_loop_base_process_timers(uint32_t now);
160 
161 /**
162  * @brief Dump list of timers via log_info
163  */
164 void btstack_run_loop_base_dump_timer(void);
165 
166 /**
167  * @brief Get time until first timer fires
168  * @return -1 if no timers, time until next timeout otherwise
169  */
170 int32_t btstack_run_loop_base_get_time_until_timeout(uint32_t now);
171 
172 /**
173  * @brief Add data source to run loop
174  * @param data_source to add
175  */
176 void btstack_run_loop_base_add_data_source(btstack_data_source_t * data_source);
177 
178 /**
179  * @brief Remove data source from run loop
180  * @param data_source to remove
181  * @return true if data source was removed
182  */
183 bool btstack_run_loop_base_remove_data_source(btstack_data_source_t * data_source);
184 
185 /**
186  * @brief Enable callbacks for a data source
187  * @param data_source to enable
188  * @param callback_types to enable
189  */
190 void btstack_run_loop_base_enable_data_source_callbacks(btstack_data_source_t * data_source, uint16_t callback_types);
191 
192 /**
193  * @brief Disable callbacks for a data source
194  * @param data_source to disable
195  * @param callback_types to disable
196  */
197 void btstack_run_loop_base_disable_data_source_callbacks(btstack_data_source_t * data_source, uint16_t callback_types);
198 
199 /**
200  * @brief Poll data sources. It calls the process function for all data sources where DATA_SOURCE_CALLBACK_POLL is set
201  */
202 void btstack_run_loop_base_poll_data_sources(void);
203 
204 /**
205  * @bried Add Callbacks to list of callbacks to execute with btstack_run_loop_base_execute_callbacks
206  */
207 void btstack_run_loop_base_add_callback(btstack_context_callback_registration_t * callback_registration);
208 
209 /**
210  * @bried Process Callbacks: remove all callback-registrations and call the registered function with its context
211  */
212 void btstack_run_loop_base_execute_callbacks(void);
213 
214 
215 /* API_START */
216 
217 /**
218  * @brief Init main run loop. Must be called before any other run loop call.
219  *
220  * Use btstack_run_loop_$(btstack_run_loop_TYPE)_get_instance() from btstack_run_loop_$(btstack_run_loop_TYPE).h to get instance
221  */
222 void btstack_run_loop_init(const btstack_run_loop_t * run_loop);
223 
224 /**
225  * @brief Set timer based on current time in milliseconds.
226  */
227 void btstack_run_loop_set_timer(btstack_timer_source_t * timer, uint32_t timeout_in_ms);
228 
229 /**
230  * @brief Set callback that will be executed when timer expires.
231  */
232 void btstack_run_loop_set_timer_handler(btstack_timer_source_t * timer, void (*process)(btstack_timer_source_t * _timer));
233 
234 /**
235  * @brief Set context for this timer
236  */
237 void btstack_run_loop_set_timer_context(btstack_timer_source_t * timer, void * context);
238 
239 /**
240  * @brief Get context for this timer
241  */
242 void * btstack_run_loop_get_timer_context(btstack_timer_source_t * timer);
243 
244 /**
245  * @brief Add timer source.
246  */
247 void btstack_run_loop_add_timer(btstack_timer_source_t * timer);
248 
249 /**
250  * @brief Remove timer source.
251  */
252 int  btstack_run_loop_remove_timer(btstack_timer_source_t * timer);
253 
254 /**
255  * @brief Get current time in ms
256  * @note 32-bit ms counter will overflow after approx. 52 days
257  */
258 uint32_t btstack_run_loop_get_time_ms(void);
259 
260 /**
261  * @brief Dump timers using log_info
262  */
263 void btstack_run_loop_timer_dump(void);
264 
265 
266 /**
267  * @brief Set data source callback.
268  */
269 void btstack_run_loop_set_data_source_handler(btstack_data_source_t * data_source, void (*process)(btstack_data_source_t * _data_source, btstack_data_source_callback_type_t callback_type));
270 
271 /**
272  * @brief Set data source file descriptor.
273  * @param data_source
274  * @param fd file descriptor
275  * @note No effect if port doesn't have file descriptors
276  */
277 void btstack_run_loop_set_data_source_fd(btstack_data_source_t * data_source, int fd);
278 
279 /**
280  * @brief Get data source file descriptor.
281  * @param data_source
282  */
283 int btstack_run_loop_get_data_source_fd(btstack_data_source_t * data_source);
284 
285 /**
286  * @brief Set data source file descriptor.
287  * @param data_source
288  * @param handle
289  * @note No effect if port doesn't have file descriptors
290  */
291 void btstack_run_loop_set_data_source_handle(btstack_data_source_t * data_source, void * handle);
292 
293 /**
294  * @brief Get data source file descriptor.
295  * @param data_source
296  */
297 void * btstack_run_loop_get_data_source_handle(btstack_data_source_t * data_source);
298 
299 /**
300  * @brief Enable callbacks for a data source
301  * @param data_source to remove
302  * @param callback types to enable
303  */
304 void btstack_run_loop_enable_data_source_callbacks(btstack_data_source_t * data_source, uint16_t callbacks);
305 
306 /**
307  * @brief Enable callbacks for a data source
308  * @param data_source to remove
309  * @param callback types to disable
310  */
311 void btstack_run_loop_disable_data_source_callbacks(btstack_data_source_t * data_source, uint16_t callbacks);
312 
313 /**
314  * @brief Add data source to run loop
315  * @param data_source to add
316  */
317 void btstack_run_loop_add_data_source(btstack_data_source_t * data_source);
318 
319 /**
320  * @brief Remove data source from run loop
321  * @param data_source to remove
322  */
323 int btstack_run_loop_remove_data_source(btstack_data_source_t * data_source);
324 
325 /**
326  * @brief Poll data sources - called only from IRQ context
327  * @note Can be used to trigger processing of received peripheral data on the main thread
328  *       by registering a data source with DATA_SOURCE_CALLBACK_POLL and calling this
329  *       function from the IRQ handler.
330  */
331 void btstack_run_loop_poll_data_sources_from_irq(void);
332 
333 
334 /**
335  * @brief Execute configured run loop. This function does not return.
336  */
337 void btstack_run_loop_execute(void);
338 
339 /**
340  * @brief Registers callback with run loop and mark main thread as ready
341  * @note If callback is already registered, the call will be ignored.
342  *       This function allows to implement, e.g., a queue-based message passing mechanism:
343  *       The external thread puts an item into a queue and call this function to trigger
344  *       processing by the BTstack main thread. If this happens multiple times, it is
345  *       guaranteed that the callback will run at least once after the last item was added.
346  * @param callback_registration
347  */
348 void btstack_run_loop_execute_on_main_thread(btstack_context_callback_registration_t * callback_registration);
349 
350 /**
351  * @brief Trigger exit of active run loop (started via btstack_run_loop_execute) if possible
352  * @note This is only supported if there's a loop in the btstack_run_loop_execute function.
353  * It is not supported if timers and data sources are only mapped to RTOS equivalents, e.g.
354  * in the Qt or Core Foundation implementations.
355  */
356 void btstack_run_loop_trigger_exit(void);
357 
358 
359 /**
360  * @brief De-Init Run Loop
361  */
362 void btstack_run_loop_deinit(void);
363 
364 /* API_END */
365 
366 
367 #if defined __cplusplus
368 }
369 #endif
370 
371 #endif // BTSTACK_RUN_LOOP_H
372