xref: /btstack/platform/posix/btstack_run_loop_posix.c (revision 2e357f7a2f48c682ad306483a500aae0cceb7e31)
1b7d596c1SMatthias Ringwald /*
2b7d596c1SMatthias Ringwald  * Copyright (C) 2014 BlueKitchen GmbH
3b7d596c1SMatthias Ringwald  *
4b7d596c1SMatthias Ringwald  * Redistribution and use in source and binary forms, with or without
5b7d596c1SMatthias Ringwald  * modification, are permitted provided that the following conditions
6b7d596c1SMatthias Ringwald  * are met:
7b7d596c1SMatthias Ringwald  *
8b7d596c1SMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright
9b7d596c1SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer.
10b7d596c1SMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
11b7d596c1SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer in the
12b7d596c1SMatthias Ringwald  *    documentation and/or other materials provided with the distribution.
13b7d596c1SMatthias Ringwald  * 3. Neither the name of the copyright holders nor the names of
14b7d596c1SMatthias Ringwald  *    contributors may be used to endorse or promote products derived
15b7d596c1SMatthias Ringwald  *    from this software without specific prior written permission.
16b7d596c1SMatthias Ringwald  * 4. Any redistribution, use, or modification is done solely for
17b7d596c1SMatthias Ringwald  *    personal benefit and not for any commercial purpose or for
18b7d596c1SMatthias Ringwald  *    monetary gain.
19b7d596c1SMatthias Ringwald  *
20b7d596c1SMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21b7d596c1SMatthias Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22b7d596c1SMatthias Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23b7d596c1SMatthias Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
24b7d596c1SMatthias Ringwald  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25b7d596c1SMatthias Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26b7d596c1SMatthias Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27b7d596c1SMatthias Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28b7d596c1SMatthias Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29b7d596c1SMatthias Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30b7d596c1SMatthias Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31b7d596c1SMatthias Ringwald  * SUCH DAMAGE.
32b7d596c1SMatthias Ringwald  *
33b7d596c1SMatthias Ringwald  * Please inquire about commercial licensing options at
34b7d596c1SMatthias Ringwald  * [email protected]
35b7d596c1SMatthias Ringwald  *
36b7d596c1SMatthias Ringwald  */
37b7d596c1SMatthias Ringwald 
38e501bae0SMatthias Ringwald #define BTSTACK_FILE__ "btstack_run_loop_posix.c"
39ab2c6ae4SMatthias Ringwald 
40b7d596c1SMatthias Ringwald /*
414ab92137SMatthias Ringwald  *  btstack_run_loop.c
42b7d596c1SMatthias Ringwald  *
43b7d596c1SMatthias Ringwald  *  Created by Matthias Ringwald on 6/6/09.
44b7d596c1SMatthias Ringwald  */
45b7d596c1SMatthias Ringwald 
4692b78dd7SMatthias Ringwald // enable POSIX functions (needed for -std=c99)
4792b78dd7SMatthias Ringwald #define _POSIX_C_SOURCE 200809
4892b78dd7SMatthias Ringwald 
498f2a52f4SMatthias Ringwald #include "btstack_run_loop_posix.h"
50b6fc147fSMatthias Ringwald 
51b6fc147fSMatthias Ringwald #include "btstack_run_loop.h"
52b6fc147fSMatthias Ringwald #include "btstack_util.h"
53b7d596c1SMatthias Ringwald #include "btstack_linked_list.h"
54b7d596c1SMatthias Ringwald #include "btstack_debug.h"
55b7d596c1SMatthias Ringwald 
56b7d596c1SMatthias Ringwald #include <stdio.h>
57b7d596c1SMatthias Ringwald #include <stdlib.h>
58b6fc147fSMatthias Ringwald #include <sys/select.h>
59f316a845SMatthias Ringwald #include <sys/time.h>
6092b78dd7SMatthias Ringwald #include <time.h>
6192b78dd7SMatthias Ringwald #include <unistd.h>
62b7d596c1SMatthias Ringwald 
63b7d596c1SMatthias Ringwald // the run loop
64b7d596c1SMatthias Ringwald static int data_sources_modified;
6592b78dd7SMatthias Ringwald 
66*2e357f7aSMatthias Ringwald static bool run_loop_exit_requested;
67*2e357f7aSMatthias Ringwald 
6892b78dd7SMatthias Ringwald // start time. tv_usec/tv_nsec = 0
6992b78dd7SMatthias Ringwald #ifdef _POSIX_MONOTONIC_CLOCK
7092b78dd7SMatthias Ringwald // use monotonic clock if available
7192b78dd7SMatthias Ringwald static struct timespec init_ts;
7292b78dd7SMatthias Ringwald #else
7392b78dd7SMatthias Ringwald // fallback to gettimeofday
74b7d596c1SMatthias Ringwald static struct timeval init_tv;
7592b78dd7SMatthias Ringwald #endif
76b7d596c1SMatthias Ringwald 
77b7d596c1SMatthias Ringwald /**
78b7d596c1SMatthias Ringwald  * Add data_source to run_loop
79b7d596c1SMatthias Ringwald  */
80ec820d77SMatthias Ringwald static void btstack_run_loop_posix_add_data_source(btstack_data_source_t *ds){
81b7d596c1SMatthias Ringwald     data_sources_modified = 1;
82075edf12SMatthias Ringwald     btstack_run_loop_base_add_data_source(ds);
83b7d596c1SMatthias Ringwald }
84b7d596c1SMatthias Ringwald 
85b7d596c1SMatthias Ringwald /**
86b7d596c1SMatthias Ringwald  * Remove data_source from run loop
87b7d596c1SMatthias Ringwald  */
88d58a1b5fSMatthias Ringwald static bool btstack_run_loop_posix_remove_data_source(btstack_data_source_t *ds){
89b7d596c1SMatthias Ringwald     data_sources_modified = 1;
90075edf12SMatthias Ringwald     return btstack_run_loop_base_remove_data_source(ds);
919120e843SMatthias Ringwald }
929120e843SMatthias Ringwald 
9392b78dd7SMatthias Ringwald #ifdef _POSIX_MONOTONIC_CLOCK
9492b78dd7SMatthias Ringwald /**
9592b78dd7SMatthias Ringwald  * @brief Returns the timespec which represents the time(stop - start). It might be negative
9692b78dd7SMatthias Ringwald  */
9792b78dd7SMatthias Ringwald static void timespec_diff(struct timespec *start, struct timespec *stop, struct timespec *result){
9892b78dd7SMatthias Ringwald     result->tv_sec = stop->tv_sec - start->tv_sec;
9992b78dd7SMatthias Ringwald     if ((stop->tv_nsec - start->tv_nsec) < 0) {
10092b78dd7SMatthias Ringwald         result->tv_sec = stop->tv_sec - start->tv_sec - 1;
10192b78dd7SMatthias Ringwald         result->tv_nsec = stop->tv_nsec - start->tv_nsec + 1000000000;
10292b78dd7SMatthias Ringwald     } else {
10392b78dd7SMatthias Ringwald         result->tv_sec = stop->tv_sec - start->tv_sec;
10492b78dd7SMatthias Ringwald         result->tv_nsec = stop->tv_nsec - start->tv_nsec;
10592b78dd7SMatthias Ringwald     }
10692b78dd7SMatthias Ringwald }
10792b78dd7SMatthias Ringwald 
10892b78dd7SMatthias Ringwald /**
10992b78dd7SMatthias Ringwald  * @brief Convert timespec to miliseconds, might overflow
11092b78dd7SMatthias Ringwald  */
11192b78dd7SMatthias Ringwald static uint64_t timespec_to_milliseconds(struct timespec *a){
11292b78dd7SMatthias Ringwald     uint64_t ret = 0;
11392b78dd7SMatthias Ringwald     uint64_t sec_val = (uint64_t)(a->tv_sec);
11492b78dd7SMatthias Ringwald     uint64_t nsec_val = (uint64_t)(a->tv_nsec);
11592b78dd7SMatthias Ringwald     ret = (sec_val*1000) + (nsec_val/1000000);
11692b78dd7SMatthias Ringwald     return ret;
11792b78dd7SMatthias Ringwald }
11892b78dd7SMatthias Ringwald 
11992b78dd7SMatthias Ringwald /**
12092b78dd7SMatthias Ringwald  * @brief Returns the milisecond value of (stop - start). Might overflow
12192b78dd7SMatthias Ringwald  */
12292b78dd7SMatthias Ringwald static uint64_t timespec_diff_milis(struct timespec* start, struct timespec* stop){
12392b78dd7SMatthias Ringwald     struct timespec diff_ts;
12492b78dd7SMatthias Ringwald     timespec_diff(start, stop, &diff_ts);
12592b78dd7SMatthias Ringwald     return timespec_to_milliseconds(&diff_ts);
12692b78dd7SMatthias Ringwald }
12792b78dd7SMatthias Ringwald #endif
12892b78dd7SMatthias Ringwald 
129b7d596c1SMatthias Ringwald /**
130f316a845SMatthias Ringwald  * @brief Queries the current time in ms since start
131f316a845SMatthias Ringwald  */
132f316a845SMatthias Ringwald static uint32_t btstack_run_loop_posix_get_time_ms(void){
13392b78dd7SMatthias Ringwald     uint32_t time_ms;
13492b78dd7SMatthias Ringwald #ifdef _POSIX_MONOTONIC_CLOCK
13592b78dd7SMatthias Ringwald     struct timespec now_ts;
13692b78dd7SMatthias Ringwald     clock_gettime(CLOCK_MONOTONIC, &now_ts);
13792b78dd7SMatthias Ringwald     time_ms = (uint32_t) timespec_diff_milis(&init_ts, &now_ts);
13892b78dd7SMatthias Ringwald #else
139f316a845SMatthias Ringwald     struct timeval tv;
140f316a845SMatthias Ringwald     gettimeofday(&tv, NULL);
14192b78dd7SMatthias Ringwald     time_ms = (uint32_t) ((tv.tv_sec  - init_tv.tv_sec) * 1000) + (tv.tv_usec / 1000);
14292b78dd7SMatthias Ringwald #endif
143f316a845SMatthias Ringwald     return time_ms;
144f316a845SMatthias Ringwald }
145f316a845SMatthias Ringwald 
146f316a845SMatthias Ringwald /**
147b7d596c1SMatthias Ringwald  * Execute run_loop
148b7d596c1SMatthias Ringwald  */
149528a4a3bSMatthias Ringwald static void btstack_run_loop_posix_execute(void) {
1509120e843SMatthias Ringwald     fd_set descriptors_read;
1519120e843SMatthias Ringwald     fd_set descriptors_write;
152b7d596c1SMatthias Ringwald 
153b7d596c1SMatthias Ringwald     btstack_linked_list_iterator_t it;
154f316a845SMatthias Ringwald     struct timeval * timeout;
155f316a845SMatthias Ringwald     struct timeval tv;
156f316a845SMatthias Ringwald     uint32_t now_ms;
157b7d596c1SMatthias Ringwald 
15892b78dd7SMatthias Ringwald #ifdef _POSIX_MONOTONIC_CLOCK
15992b78dd7SMatthias Ringwald     log_info("POSIX run loop with monotonic clock");
16092b78dd7SMatthias Ringwald #else
16192b78dd7SMatthias Ringwald     log_info("POSIX run loop using ettimeofday fallback.");
16292b78dd7SMatthias Ringwald #endif
16392b78dd7SMatthias Ringwald 
164*2e357f7aSMatthias Ringwald     while (run_loop_exit_requested == false) {
165b7d596c1SMatthias Ringwald         // collect FDs
1669120e843SMatthias Ringwald         FD_ZERO(&descriptors_read);
1679120e843SMatthias Ringwald         FD_ZERO(&descriptors_write);
168f316a845SMatthias Ringwald         int highest_fd = -1;
169075edf12SMatthias Ringwald         btstack_linked_list_iterator_init(&it, &btstack_run_loop_base_data_sources);
170b7d596c1SMatthias Ringwald         while (btstack_linked_list_iterator_has_next(&it)){
171ec820d77SMatthias Ringwald             btstack_data_source_t *ds = (btstack_data_source_t*) btstack_linked_list_iterator_next(&it);
172398a95ecSMatthias Ringwald             if (ds->source.fd < 0) continue;
1739120e843SMatthias Ringwald             if (ds->flags & DATA_SOURCE_CALLBACK_READ){
174398a95ecSMatthias Ringwald                 FD_SET(ds->source.fd, &descriptors_read);
175398a95ecSMatthias Ringwald                 if (ds->source.fd > highest_fd) {
176398a95ecSMatthias Ringwald                     highest_fd = ds->source.fd;
1779120e843SMatthias Ringwald                 }
178398a95ecSMatthias Ringwald                 log_debug("btstack_run_loop_execute adding fd %u for read", ds->source.fd);
1799120e843SMatthias Ringwald             }
1809120e843SMatthias Ringwald             if (ds->flags & DATA_SOURCE_CALLBACK_WRITE){
181398a95ecSMatthias Ringwald                 FD_SET(ds->source.fd, &descriptors_write);
182398a95ecSMatthias Ringwald                 if (ds->source.fd > highest_fd) {
183398a95ecSMatthias Ringwald                     highest_fd = ds->source.fd;
184b7d596c1SMatthias Ringwald                 }
185398a95ecSMatthias Ringwald                 log_debug("btstack_run_loop_execute adding fd %u for write", ds->source.fd);
186b7d596c1SMatthias Ringwald             }
187b7d596c1SMatthias Ringwald         }
188b7d596c1SMatthias Ringwald 
189b7d596c1SMatthias Ringwald         // get next timeout
190b7d596c1SMatthias Ringwald         timeout = NULL;
191f316a845SMatthias Ringwald         now_ms = btstack_run_loop_posix_get_time_ms();
192075edf12SMatthias Ringwald         int32_t delta_ms = btstack_run_loop_base_get_time_until_timeout(now_ms);
193075edf12SMatthias Ringwald         if (delta_ms >= 0) {
194075edf12SMatthias Ringwald             timeout = &tv;
195075edf12SMatthias Ringwald             tv.tv_sec  = delta_ms / 1000;
196075edf12SMatthias Ringwald             tv.tv_usec = (int) (delta_ms - (tv.tv_sec * 1000)) * 1000;
197075edf12SMatthias Ringwald             log_debug("btstack_run_loop_execute next timeout in %u ms", delta_ms);
198b7d596c1SMatthias Ringwald         }
199b7d596c1SMatthias Ringwald 
200b7d596c1SMatthias Ringwald         // wait for ready FDs
2019120e843SMatthias Ringwald         select( highest_fd+1 , &descriptors_read, &descriptors_write, NULL, timeout);
202b7d596c1SMatthias Ringwald 
20388e0b3b2SMatthias Ringwald 
204b7d596c1SMatthias Ringwald         data_sources_modified = 0;
205075edf12SMatthias Ringwald         btstack_linked_list_iterator_init(&it, &btstack_run_loop_base_data_sources);
206b7d596c1SMatthias Ringwald         while (btstack_linked_list_iterator_has_next(&it) && !data_sources_modified){
207ec820d77SMatthias Ringwald             btstack_data_source_t *ds = (btstack_data_source_t*) btstack_linked_list_iterator_next(&it);
208398a95ecSMatthias Ringwald             log_debug("btstack_run_loop_posix_execute: check ds %p with fd %u\n", ds, ds->source.fd);
209398a95ecSMatthias Ringwald             if (FD_ISSET(ds->source.fd, &descriptors_read)) {
210398a95ecSMatthias Ringwald                 log_debug("btstack_run_loop_posix_execute: process read ds %p with fd %u\n", ds, ds->source.fd);
2117cd5ef95SMatthias Ringwald                 ds->process(ds, DATA_SOURCE_CALLBACK_READ);
212b7d596c1SMatthias Ringwald             }
2134b7565a2SMatthias Ringwald             if (data_sources_modified) break;
214398a95ecSMatthias Ringwald             if (FD_ISSET(ds->source.fd, &descriptors_write)) {
215398a95ecSMatthias Ringwald                 log_debug("btstack_run_loop_posix_execute: process write ds %p with fd %u\n", ds, ds->source.fd);
2169120e843SMatthias Ringwald                 ds->process(ds, DATA_SOURCE_CALLBACK_WRITE);
2179120e843SMatthias Ringwald             }
218b7d596c1SMatthias Ringwald         }
219f316a845SMatthias Ringwald         log_debug("btstack_run_loop_posix_execute: after ds check\n");
220b7d596c1SMatthias Ringwald 
221b7d596c1SMatthias Ringwald         // process timers
22288e0b3b2SMatthias Ringwald         now_ms = btstack_run_loop_posix_get_time_ms();
223075edf12SMatthias Ringwald         btstack_run_loop_base_process_timers(now_ms);
224b7d596c1SMatthias Ringwald     }
225b7d596c1SMatthias Ringwald }
226b7d596c1SMatthias Ringwald 
227*2e357f7aSMatthias Ringwald static void btstack_run_loop_posix_trigger_exit(void){
228*2e357f7aSMatthias Ringwald     run_loop_exit_requested = true;
229*2e357f7aSMatthias Ringwald }
230*2e357f7aSMatthias Ringwald 
231b7d596c1SMatthias Ringwald // set timer
232ec820d77SMatthias Ringwald static void btstack_run_loop_posix_set_timer(btstack_timer_source_t *a, uint32_t timeout_in_ms){
233f316a845SMatthias Ringwald     uint32_t time_ms = btstack_run_loop_posix_get_time_ms();
234f316a845SMatthias Ringwald     a->timeout = time_ms + timeout_in_ms;
235615ae444SMatthias Ringwald     log_debug("btstack_run_loop_posix_set_timer to %u ms (now %u, timeout %u)", a->timeout, time_ms, timeout_in_ms);
236b7d596c1SMatthias Ringwald }
237b7d596c1SMatthias Ringwald 
238528a4a3bSMatthias Ringwald static void btstack_run_loop_posix_init(void){
239075edf12SMatthias Ringwald     btstack_run_loop_base_init();
240075edf12SMatthias Ringwald 
24192b78dd7SMatthias Ringwald #ifdef _POSIX_MONOTONIC_CLOCK
24292b78dd7SMatthias Ringwald     clock_gettime(CLOCK_MONOTONIC, &init_ts);
24392b78dd7SMatthias Ringwald     init_ts.tv_nsec = 0;
24492b78dd7SMatthias Ringwald #else
245f316a845SMatthias Ringwald     // just assume that we started at tv_usec == 0
246b7d596c1SMatthias Ringwald     gettimeofday(&init_tv, NULL);
247f316a845SMatthias Ringwald     init_tv.tv_usec = 0;
24892b78dd7SMatthias Ringwald #endif
249b7d596c1SMatthias Ringwald }
250b7d596c1SMatthias Ringwald 
251b7d596c1SMatthias Ringwald 
252528a4a3bSMatthias Ringwald static const btstack_run_loop_t btstack_run_loop_posix = {
253528a4a3bSMatthias Ringwald     &btstack_run_loop_posix_init,
254528a4a3bSMatthias Ringwald     &btstack_run_loop_posix_add_data_source,
255528a4a3bSMatthias Ringwald     &btstack_run_loop_posix_remove_data_source,
256075edf12SMatthias Ringwald     &btstack_run_loop_base_enable_data_source_callbacks,
257075edf12SMatthias Ringwald     &btstack_run_loop_base_disable_data_source_callbacks,
258528a4a3bSMatthias Ringwald     &btstack_run_loop_posix_set_timer,
259075edf12SMatthias Ringwald     &btstack_run_loop_base_add_timer,
260075edf12SMatthias Ringwald     &btstack_run_loop_base_remove_timer,
261528a4a3bSMatthias Ringwald     &btstack_run_loop_posix_execute,
262075edf12SMatthias Ringwald     &btstack_run_loop_base_dump_timer,
263528a4a3bSMatthias Ringwald     &btstack_run_loop_posix_get_time_ms,
264*2e357f7aSMatthias Ringwald     NULL, /* poll data sources from irq */
265*2e357f7aSMatthias Ringwald     NULL,
266*2e357f7aSMatthias Ringwald     &btstack_run_loop_posix_trigger_exit,
267b7d596c1SMatthias Ringwald };
268b7d596c1SMatthias Ringwald 
269b7d596c1SMatthias Ringwald /**
270528a4a3bSMatthias Ringwald  * Provide btstack_run_loop_posix instance
271b7d596c1SMatthias Ringwald  */
272528a4a3bSMatthias Ringwald const btstack_run_loop_t * btstack_run_loop_posix_get_instance(void){
273528a4a3bSMatthias Ringwald     return &btstack_run_loop_posix;
274b7d596c1SMatthias Ringwald }
275b7d596c1SMatthias Ringwald 
276