xref: /btstack/platform/posix/btstack_run_loop_posix.c (revision b6fc147f78a37f3479b846963ca40c9b931f6e2f)
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 #define BTSTACK_FILE__ "btstack_run_loop_posix.c"
39 
40 /*
41  *  btstack_run_loop.c
42  *
43  *  Created by Matthias Ringwald on 6/6/09.
44  */
45 
46 #include "btstack_run_loop_posix.h"
47 
48 #include "btstack_run_loop.h"
49 #include "btstack_util.h"
50 #include "btstack_linked_list.h"
51 #include "btstack_debug.h"
52 
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <sys/select.h>
56 #include <sys/time.h>
57 
58 static void btstack_run_loop_posix_dump_timer(void);
59 
60 // the run loop
61 static btstack_linked_list_t data_sources;
62 static int data_sources_modified;
63 static btstack_linked_list_t timers;
64 // start time. tv_usec = 0
65 static struct timeval init_tv;
66 
67 /**
68  * Add data_source to run_loop
69  */
70 static void btstack_run_loop_posix_add_data_source(btstack_data_source_t *ds){
71     data_sources_modified = 1;
72     // log_info("btstack_run_loop_posix_add_data_source %x with fd %u\n", (int) ds, ds->source.fd);
73     btstack_linked_list_add(&data_sources, (btstack_linked_item_t *) ds);
74 }
75 
76 /**
77  * Remove data_source from run loop
78  */
79 static int btstack_run_loop_posix_remove_data_source(btstack_data_source_t *ds){
80     data_sources_modified = 1;
81     // log_info("btstack_run_loop_posix_remove_data_source %x\n", (int) ds);
82     return btstack_linked_list_remove(&data_sources, (btstack_linked_item_t *) ds);
83 }
84 
85 /**
86  * Add timer to run_loop (keep list sorted)
87  */
88 static void btstack_run_loop_posix_add_timer(btstack_timer_source_t *ts){
89     btstack_linked_item_t *it;
90     for (it = (btstack_linked_item_t *) &timers; it->next ; it = it->next){
91         btstack_timer_source_t * next = (btstack_timer_source_t *) it->next;
92         if (next == ts){
93             log_error( "btstack_run_loop_timer_add error: timer to add already in list!");
94             return;
95         }
96         // exit if new timeout before list timeout
97         int32_t delta = btstack_time_delta(ts->timeout, next->timeout);
98         if (delta < 0) break;
99     }
100     ts->item.next = it->next;
101     it->next = (btstack_linked_item_t *) ts;
102     log_debug("Added timer %p at %u\n", ts, ts->timeout);
103     // btstack_run_loop_posix_dump_timer();
104 }
105 
106 /**
107  * Remove timer from run loop
108  */
109 static int btstack_run_loop_posix_remove_timer(btstack_timer_source_t *ts){
110     // log_info("Removed timer %x at %u\n", (int) ts, (unsigned int) ts->timeout.tv_sec);
111     // btstack_run_loop_posix_dump_timer();
112     return btstack_linked_list_remove(&timers, (btstack_linked_item_t *) ts);
113 }
114 
115 static void btstack_run_loop_posix_dump_timer(void){
116     btstack_linked_item_t *it;
117     int i = 0;
118     for (it = (btstack_linked_item_t *) timers; it ; it = it->next){
119         btstack_timer_source_t *ts = (btstack_timer_source_t*) it;
120         log_info("timer %u, timeout %u\n", i, ts->timeout);
121     }
122 }
123 
124 static void btstack_run_loop_posix_enable_data_source_callbacks(btstack_data_source_t * ds, uint16_t callback_types){
125     ds->flags |= callback_types;
126 }
127 
128 static void btstack_run_loop_posix_disable_data_source_callbacks(btstack_data_source_t * ds, uint16_t callback_types){
129     ds->flags &= ~callback_types;
130 }
131 
132 /**
133  * @brief Queries the current time in ms since start
134  */
135 static uint32_t btstack_run_loop_posix_get_time_ms(void){
136     struct timeval tv;
137     gettimeofday(&tv, NULL);
138     uint32_t time_ms = (uint32_t)((tv.tv_sec  - init_tv.tv_sec) * 1000) + (tv.tv_usec / 1000);
139     log_debug("btstack_run_loop_posix_get_time_ms: %u <- %u / %u", time_ms, (int) tv.tv_sec, (int) tv.tv_usec);
140     return time_ms;
141 }
142 
143 /**
144  * Execute run_loop
145  */
146 static void btstack_run_loop_posix_execute(void) {
147     fd_set descriptors_read;
148     fd_set descriptors_write;
149 
150     btstack_timer_source_t       *ts;
151     btstack_linked_list_iterator_t it;
152     struct timeval * timeout;
153     struct timeval tv;
154     uint32_t now_ms;
155 
156     while (1) {
157         // collect FDs
158         FD_ZERO(&descriptors_read);
159         FD_ZERO(&descriptors_write);
160         int highest_fd = -1;
161         btstack_linked_list_iterator_init(&it, &data_sources);
162         while (btstack_linked_list_iterator_has_next(&it)){
163             btstack_data_source_t *ds = (btstack_data_source_t*) btstack_linked_list_iterator_next(&it);
164             if (ds->source.fd < 0) continue;
165             if (ds->flags & DATA_SOURCE_CALLBACK_READ){
166                 FD_SET(ds->source.fd, &descriptors_read);
167                 if (ds->source.fd > highest_fd) {
168                     highest_fd = ds->source.fd;
169                 }
170                 log_debug("btstack_run_loop_execute adding fd %u for read", ds->source.fd);
171             }
172             if (ds->flags & DATA_SOURCE_CALLBACK_WRITE){
173                 FD_SET(ds->source.fd, &descriptors_write);
174                 if (ds->source.fd > highest_fd) {
175                     highest_fd = ds->source.fd;
176                 }
177                 log_debug("btstack_run_loop_execute adding fd %u for write", ds->source.fd);
178             }
179         }
180 
181         // get next timeout
182         timeout = NULL;
183         if (timers) {
184             ts = (btstack_timer_source_t *) timers;
185             timeout = &tv;
186             uint32_t list_timeout  = ts->timeout;
187             now_ms = btstack_run_loop_posix_get_time_ms();
188             int32_t delta = btstack_time_delta(list_timeout, now_ms);
189             if (delta < 0){
190                 delta = 0;
191             }
192             tv.tv_sec  = delta / 1000;
193             tv.tv_usec = (int) (delta - (tv.tv_sec * 1000)) * 1000;
194             log_debug("btstack_run_loop_execute next timeout in %u ms", delta);
195         }
196 
197         // wait for ready FDs
198         select( highest_fd+1 , &descriptors_read, &descriptors_write, NULL, timeout);
199 
200 
201         data_sources_modified = 0;
202         btstack_linked_list_iterator_init(&it, &data_sources);
203         while (btstack_linked_list_iterator_has_next(&it) && !data_sources_modified){
204             btstack_data_source_t *ds = (btstack_data_source_t*) btstack_linked_list_iterator_next(&it);
205             log_debug("btstack_run_loop_posix_execute: check ds %p with fd %u\n", ds, ds->source.fd);
206             if (FD_ISSET(ds->source.fd, &descriptors_read)) {
207                 log_debug("btstack_run_loop_posix_execute: process read ds %p with fd %u\n", ds, ds->source.fd);
208                 ds->process(ds, DATA_SOURCE_CALLBACK_READ);
209             }
210             if (data_sources_modified) break;
211             if (FD_ISSET(ds->source.fd, &descriptors_write)) {
212                 log_debug("btstack_run_loop_posix_execute: process write ds %p with fd %u\n", ds, ds->source.fd);
213                 ds->process(ds, DATA_SOURCE_CALLBACK_WRITE);
214             }
215         }
216         log_debug("btstack_run_loop_posix_execute: after ds check\n");
217 
218         // process timers
219         now_ms = btstack_run_loop_posix_get_time_ms();
220         while (timers) {
221             ts = (btstack_timer_source_t *) timers;
222             int32_t delta = btstack_time_delta(ts->timeout, now_ms);
223             if (delta > 0) break;
224             log_debug("btstack_run_loop_posix_execute: process timer %p\n", ts);
225 
226             // remove timer before processing it to allow handler to re-register with run loop
227             btstack_run_loop_posix_remove_timer(ts);
228             ts->process(ts);
229         }
230     }
231 }
232 
233 // set timer
234 static void btstack_run_loop_posix_set_timer(btstack_timer_source_t *a, uint32_t timeout_in_ms){
235     uint32_t time_ms = btstack_run_loop_posix_get_time_ms();
236     a->timeout = time_ms + timeout_in_ms;
237     log_debug("btstack_run_loop_posix_set_timer to %u ms (now %u, timeout %u)", a->timeout, time_ms, timeout_in_ms);
238 }
239 
240 static void btstack_run_loop_posix_init(void){
241     data_sources = NULL;
242     timers = NULL;
243     // just assume that we started at tv_usec == 0
244     gettimeofday(&init_tv, NULL);
245     init_tv.tv_usec = 0;
246     log_debug("btstack_run_loop_posix_init at %u/%u", (int) init_tv.tv_sec, 0);
247 }
248 
249 
250 static const btstack_run_loop_t btstack_run_loop_posix = {
251     &btstack_run_loop_posix_init,
252     &btstack_run_loop_posix_add_data_source,
253     &btstack_run_loop_posix_remove_data_source,
254     &btstack_run_loop_posix_enable_data_source_callbacks,
255     &btstack_run_loop_posix_disable_data_source_callbacks,
256     &btstack_run_loop_posix_set_timer,
257     &btstack_run_loop_posix_add_timer,
258     &btstack_run_loop_posix_remove_timer,
259     &btstack_run_loop_posix_execute,
260     &btstack_run_loop_posix_dump_timer,
261     &btstack_run_loop_posix_get_time_ms,
262 };
263 
264 /**
265  * Provide btstack_run_loop_posix instance
266  */
267 const btstack_run_loop_t * btstack_run_loop_posix_get_instance(void){
268     return &btstack_run_loop_posix;
269 }
270 
271