xref: /btstack/src/btstack_run_loop.h (revision 6f122aa37274ae04a0ab6be052e905e4fd6dcde9)
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 MATTHIAS
24  * RINGWALD 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  *  run_loop.h
40  *
41  *  Created by Matthias Ringwald on 6/6/09.
42  */
43 
44 #ifndef btstack_run_loop_H
45 #define btstack_run_loop_H
46 
47 #include "btstack_config.h"
48 
49 #include "btstack_bool.h"
50 #include "btstack_linked_list.h"
51 
52 #include <stdint.h>
53 
54 #if defined __cplusplus
55 extern "C" {
56 #endif
57 
58 
59 /**
60  * Callback types for run loop data sources
61  */
62 typedef enum {
63 	DATA_SOURCE_CALLBACK_POLL  = 1 << 0,
64 	DATA_SOURCE_CALLBACK_READ  = 1 << 1,
65 	DATA_SOURCE_CALLBACK_WRITE = 1 << 2,
66 } btstack_data_source_callback_type_t;
67 
68 typedef struct btstack_data_source {
69 	// linked item
70     btstack_linked_item_t item;
71 
72     // item to watch in run loop
73     union {
74 	    // file descriptor for posix systems
75 	    int  fd;
76     	// handle on windows
77     	void * handle;
78     } source;
79 
80     // callback to call for enabled callback types
81     void  (*process)(struct btstack_data_source *ds, btstack_data_source_callback_type_t callback_type);
82 
83     // flags storing enabled callback types
84     uint16_t flags;
85 
86 } btstack_data_source_t;
87 
88 typedef struct btstack_timer_source {
89     btstack_linked_item_t item;
90     // timeout in system ticks (HAVE_EMBEDDED_TICK) or milliseconds (HAVE_EMBEDDED_TIME_MS)
91     uint32_t timeout;
92     // will be called when timer fired
93     void  (*process)(struct btstack_timer_source *ts);
94     void * context;
95 } btstack_timer_source_t;
96 
97 typedef struct btstack_run_loop {
98 	void (*init)(void);
99 	void (*add_data_source)(btstack_data_source_t * data_source);
100 	bool (*remove_data_source)(btstack_data_source_t * data_source);
101 	void (*enable_data_source_callbacks)(btstack_data_source_t * data_source, uint16_t callbacks);
102 	void (*disable_data_source_callbacks)(btstack_data_source_t * data_source, uint16_t callbacks);
103 	void (*set_timer)(btstack_timer_source_t * timer, uint32_t timeout_in_ms);
104 	void (*add_timer)(btstack_timer_source_t *timer);
105 	bool  (*remove_timer)(btstack_timer_source_t *timer);
106 	void (*execute)(void);
107 	void (*dump_timer)(void);
108 	uint32_t (*get_time_ms)(void);
109 } btstack_run_loop_t;
110 
111 
112 /*
113  *  BTstack Run Loop Base Implementation
114  *  Portable implementation of timer and data source management as base for platform specific implementations
115  */
116 
117 // private data (access only by run loop implementations)
118 extern btstack_linked_list_t btstack_run_loop_base_timers;
119 extern btstack_linked_list_t btstack_run_loop_base_data_sources;
120 
121 /**
122  * @brief Init
123  */
124 void btstack_run_loop_base_init(void);
125 
126 /**
127  * @brief Add timer source.
128  * @param timer to add
129  */
130 void btstack_run_loop_base_add_timer(btstack_timer_source_t * timer);
131 
132 /**
133  * @brief Remove timer source.
134  * @param timer to remove
135  * @returns true if timer was removed
136  */
137 bool  btstack_run_loop_base_remove_timer(btstack_timer_source_t * timer);
138 
139 /**
140  * @brief Process timers: remove expired timers from list and call their process function
141  * @param now
142  */
143 void  btstack_run_loop_base_process_timers(uint32_t now);
144 
145 /**
146  * @brief Dump list of timers via log_info
147  */
148 void btstack_run_loop_base_dump_timer(void);
149 
150 /**
151  * @brief Get time until first timer fires
152  * @returns -1 if no timers, time until next timeout otherwise
153  */
154 int32_t btstack_run_loop_base_get_time_until_timeout(uint32_t now);
155 
156 /**
157  * @brief Add data source to run loop
158  * @param data_source to add
159  */
160 void btstack_run_loop_base_add_data_source(btstack_data_source_t * data_source);
161 
162 /**
163  * @brief Remove data source from run loop
164  * @param data_source to remove
165  * @returns true if data srouce was removed
166  */
167 bool btstack_run_loop_base_remove_data_source(btstack_data_source_t * data_source);
168 
169 /**
170  * @brief Enable callbacks for a data source
171  * @param data_source to remove
172  * @param callback types to enable
173  */
174 void btstack_run_loop_base_enable_data_source_callbacks(btstack_data_source_t * data_source, uint16_t callbacks);
175 
176 /**
177  * @brief Enable callbacks for a data source
178  * @param data_source to remove
179  * @param callback types to disable
180  */
181 void btstack_run_loop_base_disable_data_source_callbacks(btstack_data_source_t * data_source, uint16_t callbacks);
182 
183 /**
184  * @brief Poll data sources. It calls the procss function for all data sources where DATA_SOURCE_CALLBACK_POLL is set
185  */
186 void btstack_run_loop_base_poll_data_sources(void);
187 
188 
189 /* API_START */
190 
191 /**
192  * @brief Init main run loop. Must be called before any other run loop call.
193  *
194  * Use btstack_run_loop_$(btstack_run_loop_TYPE)_get_instance() from btstack_run_loop_$(btstack_run_loop_TYPE).h to get instance
195  */
196 void btstack_run_loop_init(const btstack_run_loop_t * run_loop);
197 
198 /**
199  * @brief Set timer based on current time in milliseconds.
200  */
201 void btstack_run_loop_set_timer(btstack_timer_source_t * ts, uint32_t timeout_in_ms);
202 
203 /**
204  * @brief Set callback that will be executed when timer expires.
205  */
206 void btstack_run_loop_set_timer_handler(btstack_timer_source_t * ts, void (*process)(btstack_timer_source_t *_ts));
207 
208 /**
209  * @brief Set context for this timer
210  */
211 void btstack_run_loop_set_timer_context(btstack_timer_source_t * ts, void * context);
212 
213 /**
214  * @brief Get context for this timer
215  */
216 void * btstack_run_loop_get_timer_context(btstack_timer_source_t * ts);
217 
218 /**
219  * @brief Add timer source.
220  */
221 void btstack_run_loop_add_timer(btstack_timer_source_t * timer);
222 
223 /**
224  * @brief Remove timer source.
225  */
226 int  btstack_run_loop_remove_timer(btstack_timer_source_t * timer);
227 
228 /**
229  * @brief Get current time in ms
230  * @note 32-bit ms counter will overflow after approx. 52 days
231  */
232 uint32_t btstack_run_loop_get_time_ms(void);
233 
234 /**
235  * @brief Dump timers using log_info
236  */
237 void btstack_run_loop_timer_dump(void);
238 
239 
240 /**
241  * @brief Set data source callback.
242  */
243 void btstack_run_loop_set_data_source_handler(btstack_data_source_t * data_source, void (*process)(btstack_data_source_t *_ds, btstack_data_source_callback_type_t callback_type));
244 
245 /**
246  * @brief Set data source file descriptor.
247  * @param data_source
248  * @param fd file descriptor
249  * @note No effect if port doensn't have file descriptors
250  */
251 void btstack_run_loop_set_data_source_fd(btstack_data_source_t * data_source, int fd);
252 
253 /**
254  * @brief Get data source file descriptor.
255  * @param data_source
256  */
257 int btstack_run_loop_get_data_source_fd(btstack_data_source_t * data_source);
258 
259 /**
260  * @brief Set data source file descriptor.
261  * @param data_source
262  * @param handle
263  * @note No effect if port doensn't have file descriptors
264  */
265 void btstack_run_loop_set_data_source_handle(btstack_data_source_t * data_source, void * handle);
266 
267 /**
268  * @brief Get data source file descriptor.
269  * @param data_source
270  */
271 void * btstack_run_loop_get_data_source_handle(btstack_data_source_t * data_source);
272 
273 /**
274  * @brief Enable callbacks for a data source
275  * @param data_source to remove
276  * @param callback types to enable
277  */
278 void btstack_run_loop_enable_data_source_callbacks(btstack_data_source_t * data_source, uint16_t callbacks);
279 
280 /**
281  * @brief Enable callbacks for a data source
282  * @param data_source to remove
283  * @param callback types to disable
284  */
285 void btstack_run_loop_disable_data_source_callbacks(btstack_data_source_t * data_source, uint16_t callbacks);
286 
287 /**
288  * @brief Add data source to run loop
289  * @param data_source to add
290  */
291 void btstack_run_loop_add_data_source(btstack_data_source_t * data_source);
292 
293 /**
294  * @brief Remove data source from run loop
295  * @param data_source to remove
296  */
297 int btstack_run_loop_remove_data_source(btstack_data_source_t * data_source);
298 
299 /**
300  * @brief Execute configured run loop. This function does not return.
301  */
302 void btstack_run_loop_execute(void);
303 
304 /**
305  * @brief De-Init Run Loop
306  */
307 void btstack_run_loop_deinit(void);
308 
309 /* API_END */
310 
311 
312 
313 #if defined __cplusplus
314 }
315 #endif
316 
317 #endif // btstack_run_loop_H
318