xref: /btstack/src/btstack_run_loop.c (revision da8e14c5aa3783b6bb7dd63e71572a901bcf168b)
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 #define BTSTACK_FILE__ "btstack_run_loop.c"
39 
40 /*
41  *  run_loop.c
42  *
43  *  Created by Matthias Ringwald on 6/6/09.
44  */
45 
46 #include "btstack_run_loop.h"
47 
48 #include "btstack_debug.h"
49 #include "btstack_config.h"
50 #include "btstack_util.h"
51 
52 #include <inttypes.h>
53 
54 static const btstack_run_loop_t * the_run_loop = NULL;
55 
56 extern const btstack_run_loop_t btstack_run_loop_embedded;
57 
58 /*
59  *  Portable implementation of timer and data source management as base for platform specific implementations
60  */
61 
62 // private data (access only by run loop implementations)
63 btstack_linked_list_t  btstack_run_loop_base_timers;
64 btstack_linked_list_t  btstack_run_loop_base_data_sources;
65 btstack_linked_list_t  btstack_run_loop_base_callbacks;
66 
67 void btstack_run_loop_base_init(void){
68     btstack_run_loop_base_timers = NULL;
69     btstack_run_loop_base_data_sources = NULL;
70     btstack_run_loop_base_callbacks = NULL;
71 }
72 
73 void btstack_run_loop_base_add_data_source(btstack_data_source_t * data_source){
74     btstack_linked_list_add(&btstack_run_loop_base_data_sources, (btstack_linked_item_t *) data_source);
75 }
76 
77 bool btstack_run_loop_base_remove_data_source(btstack_data_source_t * data_source){
78     return btstack_linked_list_remove(&btstack_run_loop_base_data_sources, (btstack_linked_item_t *) data_source);
79 }
80 
81 void btstack_run_loop_base_enable_data_source_callbacks(btstack_data_source_t * data_source, uint16_t callback_types){
82     data_source->flags |= callback_types;
83 }
84 
85 void btstack_run_loop_base_disable_data_source_callbacks(btstack_data_source_t * data_source, uint16_t callback_types){
86     data_source->flags &= ~callback_types;
87 }
88 
89 bool btstack_run_loop_base_remove_timer(btstack_timer_source_t * timer){
90     return btstack_linked_list_remove(&btstack_run_loop_base_timers, (btstack_linked_item_t *) timer);
91 }
92 
93 void btstack_run_loop_base_add_timer(btstack_timer_source_t * timer){
94     btstack_linked_item_t *it;
95     for (it = (btstack_linked_item_t *) &btstack_run_loop_base_timers; it->next ; it = it->next){
96         btstack_timer_source_t * next = (btstack_timer_source_t *) it->next;
97         btstack_assert(next != timer);
98         int32_t delta = btstack_time_delta(timer->timeout, next->timeout);
99         if (delta < 0) break;
100     }
101     timer->item.next = it->next;
102     it->next = (btstack_linked_item_t *) timer;
103 }
104 
105 void btstack_run_loop_base_process_timers(uint32_t now){
106     // process timers, exit when timeout is in the future
107     while (btstack_run_loop_base_timers) {
108         btstack_timer_source_t * timer = (btstack_timer_source_t *) btstack_run_loop_base_timers;
109         int32_t delta = btstack_time_delta(timer->timeout, now);
110         if (delta > 0) break;
111         btstack_run_loop_base_remove_timer(timer);
112         timer->process(timer);
113     }
114 }
115 
116 void btstack_run_loop_base_dump_timer(void){
117 #ifdef ENABLE_LOG_INFO
118     btstack_linked_item_t *it;
119     uint16_t i = 0;
120     for (it = (btstack_linked_item_t *) btstack_run_loop_base_timers; it ; it = it->next){
121         btstack_timer_source_t * timer = (btstack_timer_source_t*) it;
122         log_info("timer %u (%p): timeout %" PRIbtstack_time_t "\n", i, (void *) timer, timer->timeout);
123     }
124 #endif
125 
126 }
127 /**
128  * @brief Get time until first timer fires
129  * @return -1 if no timers, time until next timeout otherwise
130  */
131 int32_t btstack_run_loop_base_get_time_until_timeout(uint32_t now){
132     if (btstack_run_loop_base_timers == NULL) return -1;
133     btstack_timer_source_t * timer = (btstack_timer_source_t *) btstack_run_loop_base_timers;
134     uint32_t list_timeout  = timer->timeout;
135     int32_t delta = btstack_time_delta(list_timeout, now);
136     if (delta < 0){
137         delta = 0;
138     }
139     return delta;
140 }
141 
142 void btstack_run_loop_base_poll_data_sources(void){
143     // poll data sources
144     btstack_data_source_t *ds;
145     btstack_data_source_t *next;
146     for (ds = (btstack_data_source_t *) btstack_run_loop_base_data_sources; ds != NULL ; ds = next){
147         next = (btstack_data_source_t *) ds->item.next; // cache pointer to next data_source to allow data source to remove itself
148         if (ds->flags & DATA_SOURCE_CALLBACK_POLL){
149             ds->process(ds, DATA_SOURCE_CALLBACK_POLL);
150         }
151     }
152 }
153 
154 void btstack_run_loop_base_add_callback(btstack_context_callback_registration_t * callback_registration){
155     btstack_linked_list_add_tail(&btstack_run_loop_base_callbacks, (btstack_linked_item_t *) callback_registration);
156 }
157 
158 
159 void btstack_run_loop_base_execute_callbacks(void){
160     while (1){
161         btstack_context_callback_registration_t * callback_registration = (btstack_context_callback_registration_t *) btstack_linked_list_pop(&btstack_run_loop_base_callbacks);
162         if (callback_registration == NULL){
163             break;
164         }
165         (*callback_registration->callback)(callback_registration->context);
166     }
167 }
168 
169 
170 /**
171  * BTstack Run Loop Implementation, mainly dispatches to port-specific implementation
172  */
173 
174 // main implementation
175 
176 void btstack_run_loop_set_timer_handler(btstack_timer_source_t * timer, void (*process)(btstack_timer_source_t * _timer)){
177     timer->process = process;
178 }
179 
180 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)){
181     data_source->process = process;
182 }
183 
184 void btstack_run_loop_set_data_source_fd(btstack_data_source_t * data_source, int fd){
185     data_source->source.fd = fd;
186 }
187 
188 int btstack_run_loop_get_data_source_fd(btstack_data_source_t * data_source){
189     return data_source->source.fd;
190 }
191 
192 void btstack_run_loop_set_data_source_handle(btstack_data_source_t * data_source, void * handle){
193     data_source->source.handle = handle;
194 }
195 
196 void * btstack_run_loop_get_data_source_handle(btstack_data_source_t * data_source){
197     return data_source->source.handle;
198 }
199 
200 void btstack_run_loop_enable_data_source_callbacks(btstack_data_source_t * data_source, uint16_t callbacks){
201     btstack_assert(the_run_loop != NULL);
202     btstack_assert(the_run_loop->enable_data_source_callbacks != NULL);
203     the_run_loop->enable_data_source_callbacks(data_source, callbacks);
204 }
205 
206 void btstack_run_loop_disable_data_source_callbacks(btstack_data_source_t * data_source, uint16_t callbacks){
207     btstack_assert(the_run_loop != NULL);
208     btstack_assert(the_run_loop->enable_data_source_callbacks != NULL);
209     the_run_loop->disable_data_source_callbacks(data_source, callbacks);
210 }
211 
212 /**
213  * Add data_source to run_loop
214  */
215 void btstack_run_loop_add_data_source(btstack_data_source_t * data_source){
216     btstack_assert(the_run_loop != NULL);
217     btstack_assert(the_run_loop->enable_data_source_callbacks != NULL);
218     btstack_assert(data_source->process != NULL);
219     the_run_loop->add_data_source(data_source);
220 }
221 
222 /**
223  * Remove data_source from run loop
224  */
225 int btstack_run_loop_remove_data_source(btstack_data_source_t * data_source){
226     btstack_assert(the_run_loop != NULL);
227     btstack_assert(the_run_loop->disable_data_source_callbacks != NULL);
228     btstack_assert(data_source->process != NULL);
229     return the_run_loop->remove_data_source(data_source);
230 }
231 
232 void btstack_run_loop_poll_data_sources_from_irq(void){
233     btstack_assert(the_run_loop != NULL);
234     btstack_assert(the_run_loop->poll_data_sources_from_irq != NULL);
235     the_run_loop->poll_data_sources_from_irq();
236 }
237 
238 void btstack_run_loop_set_timer(btstack_timer_source_t *timer, uint32_t timeout_in_ms){
239     btstack_assert(the_run_loop != NULL);
240     the_run_loop->set_timer(timer, timeout_in_ms);
241 }
242 
243 /**
244  * @brief Set context for this timer
245  */
246 void btstack_run_loop_set_timer_context(btstack_timer_source_t * timer, void * context){
247     timer->context = context;
248 }
249 
250 /**
251  * @brief Get context for this timer
252  */
253 void * btstack_run_loop_get_timer_context(btstack_timer_source_t * timer){
254     return timer->context;
255 }
256 
257 /**
258  * Add timer to run_loop (keep list sorted)
259  */
260 void btstack_run_loop_add_timer(btstack_timer_source_t * timer){
261     btstack_assert(the_run_loop != NULL);
262     btstack_assert(timer->process != NULL);
263     the_run_loop->add_timer(timer);
264 }
265 
266 /**
267  * Remove timer from run loop
268  */
269 int btstack_run_loop_remove_timer(btstack_timer_source_t * timer){
270     btstack_assert(the_run_loop != NULL);
271     return the_run_loop->remove_timer(timer);
272 }
273 
274 /**
275  * @brief Get current time in ms
276  */
277 uint32_t btstack_run_loop_get_time_ms(void){
278     btstack_assert(the_run_loop != NULL);
279     return the_run_loop->get_time_ms();
280 }
281 
282 
283 void btstack_run_loop_timer_dump(void){
284     btstack_assert(the_run_loop != NULL);
285     the_run_loop->dump_timer();
286 }
287 
288 /**
289  * Execute run_loop
290  */
291 void btstack_run_loop_execute(void){
292     btstack_assert(the_run_loop != NULL);
293     the_run_loop->execute();
294 }
295 
296 void btstack_run_loop_trigger_exit(void){
297     btstack_assert(the_run_loop != NULL);
298     btstack_assert(the_run_loop->trigger_exit != NULL);
299     the_run_loop->trigger_exit();
300 }
301 
302 void btstack_run_loop_execute_on_main_thread(btstack_context_callback_registration_t * callback_registration){
303     btstack_assert(the_run_loop != NULL);
304     btstack_assert(the_run_loop->execute_on_main_thread != NULL);
305     the_run_loop->execute_on_main_thread(callback_registration);
306 }
307 
308 // init must be called before any other run_loop call
309 void btstack_run_loop_init(const btstack_run_loop_t * run_loop){
310     btstack_assert(the_run_loop == NULL);
311     the_run_loop = run_loop;
312     the_run_loop->init();
313 }
314 
315 void btstack_run_loop_deinit(void){
316     the_run_loop = NULL;
317 }
318 
319