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> 6241158b74SMatthias Ringwald #include <pthread.h> 63b7d596c1SMatthias Ringwald 64b7d596c1SMatthias Ringwald // the run loop 65*90d4f186SMatthias Ringwald static int btstack_run_loop_posix_data_sources_modified; 6692b78dd7SMatthias Ringwald 67*90d4f186SMatthias Ringwald static bool btstack_run_loop_posix_exit_requested; 682e357f7aSMatthias Ringwald 69*90d4f186SMatthias Ringwald // to trigger process callbacks other thread 70*90d4f186SMatthias Ringwald static pthread_mutex_t btstack_run_loop_posix_callbacks_mutex = PTHREAD_MUTEX_INITIALIZER; 71*90d4f186SMatthias Ringwald static int btstack_run_loop_posix_process_callbacks_fd; 72*90d4f186SMatthias Ringwald static btstack_data_source_t btstack_run_loop_posix_process_callbacks_ds; 73*90d4f186SMatthias Ringwald 74*90d4f186SMatthias Ringwald // to trigger poll data sources from irq 75*90d4f186SMatthias Ringwald static int btstack_run_loop_posix_poll_data_sources_fd; 76*90d4f186SMatthias Ringwald static btstack_data_source_t btstack_run_loop_posix_poll_data_sources_ds; 7741158b74SMatthias Ringwald 7892b78dd7SMatthias Ringwald // start time. tv_usec/tv_nsec = 0 7992b78dd7SMatthias Ringwald #ifdef _POSIX_MONOTONIC_CLOCK 8092b78dd7SMatthias Ringwald // use monotonic clock if available 8192b78dd7SMatthias Ringwald static struct timespec init_ts; 8292b78dd7SMatthias Ringwald #else 8392b78dd7SMatthias Ringwald // fallback to gettimeofday 84b7d596c1SMatthias Ringwald static struct timeval init_tv; 8592b78dd7SMatthias Ringwald #endif 86b7d596c1SMatthias Ringwald 87b7d596c1SMatthias Ringwald /** 88b7d596c1SMatthias Ringwald * Add data_source to run_loop 89b7d596c1SMatthias Ringwald */ 90ec820d77SMatthias Ringwald static void btstack_run_loop_posix_add_data_source(btstack_data_source_t *ds){ 91*90d4f186SMatthias Ringwald btstack_run_loop_posix_data_sources_modified = 1; 92075edf12SMatthias Ringwald btstack_run_loop_base_add_data_source(ds); 93b7d596c1SMatthias Ringwald } 94b7d596c1SMatthias Ringwald 95b7d596c1SMatthias Ringwald /** 96b7d596c1SMatthias Ringwald * Remove data_source from run loop 97b7d596c1SMatthias Ringwald */ 98d58a1b5fSMatthias Ringwald static bool btstack_run_loop_posix_remove_data_source(btstack_data_source_t *ds){ 99*90d4f186SMatthias Ringwald btstack_run_loop_posix_data_sources_modified = 1; 100075edf12SMatthias Ringwald return btstack_run_loop_base_remove_data_source(ds); 1019120e843SMatthias Ringwald } 1029120e843SMatthias Ringwald 10392b78dd7SMatthias Ringwald #ifdef _POSIX_MONOTONIC_CLOCK 10492b78dd7SMatthias Ringwald /** 10592b78dd7SMatthias Ringwald * @brief Returns the timespec which represents the time(stop - start). It might be negative 10692b78dd7SMatthias Ringwald */ 10792b78dd7SMatthias Ringwald static void timespec_diff(struct timespec *start, struct timespec *stop, struct timespec *result){ 10892b78dd7SMatthias Ringwald result->tv_sec = stop->tv_sec - start->tv_sec; 10992b78dd7SMatthias Ringwald if ((stop->tv_nsec - start->tv_nsec) < 0) { 11092b78dd7SMatthias Ringwald result->tv_sec = stop->tv_sec - start->tv_sec - 1; 11192b78dd7SMatthias Ringwald result->tv_nsec = stop->tv_nsec - start->tv_nsec + 1000000000; 11292b78dd7SMatthias Ringwald } else { 11392b78dd7SMatthias Ringwald result->tv_sec = stop->tv_sec - start->tv_sec; 11492b78dd7SMatthias Ringwald result->tv_nsec = stop->tv_nsec - start->tv_nsec; 11592b78dd7SMatthias Ringwald } 11692b78dd7SMatthias Ringwald } 11792b78dd7SMatthias Ringwald 11892b78dd7SMatthias Ringwald /** 11992b78dd7SMatthias Ringwald * @brief Convert timespec to miliseconds, might overflow 12092b78dd7SMatthias Ringwald */ 12192b78dd7SMatthias Ringwald static uint64_t timespec_to_milliseconds(struct timespec *a){ 12292b78dd7SMatthias Ringwald uint64_t ret = 0; 12392b78dd7SMatthias Ringwald uint64_t sec_val = (uint64_t)(a->tv_sec); 12492b78dd7SMatthias Ringwald uint64_t nsec_val = (uint64_t)(a->tv_nsec); 12592b78dd7SMatthias Ringwald ret = (sec_val*1000) + (nsec_val/1000000); 12692b78dd7SMatthias Ringwald return ret; 12792b78dd7SMatthias Ringwald } 12892b78dd7SMatthias Ringwald 12992b78dd7SMatthias Ringwald /** 13092b78dd7SMatthias Ringwald * @brief Returns the milisecond value of (stop - start). Might overflow 13192b78dd7SMatthias Ringwald */ 13292b78dd7SMatthias Ringwald static uint64_t timespec_diff_milis(struct timespec* start, struct timespec* stop){ 13392b78dd7SMatthias Ringwald struct timespec diff_ts; 13492b78dd7SMatthias Ringwald timespec_diff(start, stop, &diff_ts); 13592b78dd7SMatthias Ringwald return timespec_to_milliseconds(&diff_ts); 13692b78dd7SMatthias Ringwald } 13792b78dd7SMatthias Ringwald #endif 13892b78dd7SMatthias Ringwald 139b7d596c1SMatthias Ringwald /** 140f316a845SMatthias Ringwald * @brief Queries the current time in ms since start 141f316a845SMatthias Ringwald */ 142f316a845SMatthias Ringwald static uint32_t btstack_run_loop_posix_get_time_ms(void){ 14392b78dd7SMatthias Ringwald uint32_t time_ms; 14492b78dd7SMatthias Ringwald #ifdef _POSIX_MONOTONIC_CLOCK 14592b78dd7SMatthias Ringwald struct timespec now_ts; 14692b78dd7SMatthias Ringwald clock_gettime(CLOCK_MONOTONIC, &now_ts); 14792b78dd7SMatthias Ringwald time_ms = (uint32_t) timespec_diff_milis(&init_ts, &now_ts); 14892b78dd7SMatthias Ringwald #else 149f316a845SMatthias Ringwald struct timeval tv; 150f316a845SMatthias Ringwald gettimeofday(&tv, NULL); 15192b78dd7SMatthias Ringwald time_ms = (uint32_t) ((tv.tv_sec - init_tv.tv_sec) * 1000) + (tv.tv_usec / 1000); 15292b78dd7SMatthias Ringwald #endif 153f316a845SMatthias Ringwald return time_ms; 154f316a845SMatthias Ringwald } 155f316a845SMatthias Ringwald 156f316a845SMatthias Ringwald /** 157b7d596c1SMatthias Ringwald * Execute run_loop 158b7d596c1SMatthias Ringwald */ 159528a4a3bSMatthias Ringwald static void btstack_run_loop_posix_execute(void) { 1609120e843SMatthias Ringwald fd_set descriptors_read; 1619120e843SMatthias Ringwald fd_set descriptors_write; 162b7d596c1SMatthias Ringwald 163b7d596c1SMatthias Ringwald btstack_linked_list_iterator_t it; 164f316a845SMatthias Ringwald struct timeval * timeout; 165f316a845SMatthias Ringwald struct timeval tv; 166f316a845SMatthias Ringwald uint32_t now_ms; 167b7d596c1SMatthias Ringwald 16892b78dd7SMatthias Ringwald #ifdef _POSIX_MONOTONIC_CLOCK 16992b78dd7SMatthias Ringwald log_info("POSIX run loop with monotonic clock"); 17092b78dd7SMatthias Ringwald #else 17192b78dd7SMatthias Ringwald log_info("POSIX run loop using ettimeofday fallback."); 17292b78dd7SMatthias Ringwald #endif 17392b78dd7SMatthias Ringwald 174*90d4f186SMatthias Ringwald while (btstack_run_loop_posix_exit_requested == false) { 175b7d596c1SMatthias Ringwald // collect FDs 1769120e843SMatthias Ringwald FD_ZERO(&descriptors_read); 1779120e843SMatthias Ringwald FD_ZERO(&descriptors_write); 178f316a845SMatthias Ringwald int highest_fd = -1; 179075edf12SMatthias Ringwald btstack_linked_list_iterator_init(&it, &btstack_run_loop_base_data_sources); 180b7d596c1SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 181ec820d77SMatthias Ringwald btstack_data_source_t *ds = (btstack_data_source_t*) btstack_linked_list_iterator_next(&it); 182398a95ecSMatthias Ringwald if (ds->source.fd < 0) continue; 1839120e843SMatthias Ringwald if (ds->flags & DATA_SOURCE_CALLBACK_READ){ 184398a95ecSMatthias Ringwald FD_SET(ds->source.fd, &descriptors_read); 185398a95ecSMatthias Ringwald if (ds->source.fd > highest_fd) { 186398a95ecSMatthias Ringwald highest_fd = ds->source.fd; 1879120e843SMatthias Ringwald } 188398a95ecSMatthias Ringwald log_debug("btstack_run_loop_execute adding fd %u for read", ds->source.fd); 1899120e843SMatthias Ringwald } 1909120e843SMatthias Ringwald if (ds->flags & DATA_SOURCE_CALLBACK_WRITE){ 191398a95ecSMatthias Ringwald FD_SET(ds->source.fd, &descriptors_write); 192398a95ecSMatthias Ringwald if (ds->source.fd > highest_fd) { 193398a95ecSMatthias Ringwald highest_fd = ds->source.fd; 194b7d596c1SMatthias Ringwald } 195398a95ecSMatthias Ringwald log_debug("btstack_run_loop_execute adding fd %u for write", ds->source.fd); 196b7d596c1SMatthias Ringwald } 197b7d596c1SMatthias Ringwald } 198b7d596c1SMatthias Ringwald 199b7d596c1SMatthias Ringwald // get next timeout 200b7d596c1SMatthias Ringwald timeout = NULL; 201f316a845SMatthias Ringwald now_ms = btstack_run_loop_posix_get_time_ms(); 202075edf12SMatthias Ringwald int32_t delta_ms = btstack_run_loop_base_get_time_until_timeout(now_ms); 203075edf12SMatthias Ringwald if (delta_ms >= 0) { 204075edf12SMatthias Ringwald timeout = &tv; 205075edf12SMatthias Ringwald tv.tv_sec = delta_ms / 1000; 206075edf12SMatthias Ringwald tv.tv_usec = (int) (delta_ms - (tv.tv_sec * 1000)) * 1000; 207075edf12SMatthias Ringwald log_debug("btstack_run_loop_execute next timeout in %u ms", delta_ms); 208b7d596c1SMatthias Ringwald } 209b7d596c1SMatthias Ringwald 210b7d596c1SMatthias Ringwald // wait for ready FDs 2119120e843SMatthias Ringwald select( highest_fd+1 , &descriptors_read, &descriptors_write, NULL, timeout); 212b7d596c1SMatthias Ringwald 213*90d4f186SMatthias Ringwald btstack_run_loop_posix_data_sources_modified = 0; 214075edf12SMatthias Ringwald btstack_linked_list_iterator_init(&it, &btstack_run_loop_base_data_sources); 215*90d4f186SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it) && !btstack_run_loop_posix_data_sources_modified){ 216ec820d77SMatthias Ringwald btstack_data_source_t *ds = (btstack_data_source_t*) btstack_linked_list_iterator_next(&it); 217398a95ecSMatthias Ringwald log_debug("btstack_run_loop_posix_execute: check ds %p with fd %u\n", ds, ds->source.fd); 218398a95ecSMatthias Ringwald if (FD_ISSET(ds->source.fd, &descriptors_read)) { 219398a95ecSMatthias Ringwald log_debug("btstack_run_loop_posix_execute: process read ds %p with fd %u\n", ds, ds->source.fd); 2207cd5ef95SMatthias Ringwald ds->process(ds, DATA_SOURCE_CALLBACK_READ); 221b7d596c1SMatthias Ringwald } 222*90d4f186SMatthias Ringwald if (btstack_run_loop_posix_data_sources_modified) break; 223398a95ecSMatthias Ringwald if (FD_ISSET(ds->source.fd, &descriptors_write)) { 224398a95ecSMatthias Ringwald log_debug("btstack_run_loop_posix_execute: process write ds %p with fd %u\n", ds, ds->source.fd); 2259120e843SMatthias Ringwald ds->process(ds, DATA_SOURCE_CALLBACK_WRITE); 2269120e843SMatthias Ringwald } 227b7d596c1SMatthias Ringwald } 228f316a845SMatthias Ringwald log_debug("btstack_run_loop_posix_execute: after ds check\n"); 229b7d596c1SMatthias Ringwald 230b7d596c1SMatthias Ringwald // process timers 23188e0b3b2SMatthias Ringwald now_ms = btstack_run_loop_posix_get_time_ms(); 232075edf12SMatthias Ringwald btstack_run_loop_base_process_timers(now_ms); 233b7d596c1SMatthias Ringwald } 234b7d596c1SMatthias Ringwald } 235b7d596c1SMatthias Ringwald 2362e357f7aSMatthias Ringwald static void btstack_run_loop_posix_trigger_exit(void){ 237*90d4f186SMatthias Ringwald btstack_run_loop_posix_exit_requested = true; 2382e357f7aSMatthias Ringwald } 2392e357f7aSMatthias Ringwald 240b7d596c1SMatthias Ringwald // set timer 241ec820d77SMatthias Ringwald static void btstack_run_loop_posix_set_timer(btstack_timer_source_t *a, uint32_t timeout_in_ms){ 242f316a845SMatthias Ringwald uint32_t time_ms = btstack_run_loop_posix_get_time_ms(); 243f316a845SMatthias Ringwald a->timeout = time_ms + timeout_in_ms; 244615ae444SMatthias Ringwald log_debug("btstack_run_loop_posix_set_timer to %u ms (now %u, timeout %u)", a->timeout, time_ms, timeout_in_ms); 245b7d596c1SMatthias Ringwald } 246b7d596c1SMatthias Ringwald 247*90d4f186SMatthias Ringwald // trigger pipe 248*90d4f186SMatthias Ringwald static void btstack_run_loop_posix_trigger_pipe(int fd){ 249*90d4f186SMatthias Ringwald if (fd < 0) return; 250*90d4f186SMatthias Ringwald const uint8_t x = (uint8_t) 'x'; 251*90d4f186SMatthias Ringwald (void) write(fd, &x, 1); 252*90d4f186SMatthias Ringwald } 253*90d4f186SMatthias Ringwald 254*90d4f186SMatthias Ringwald // poll data sources from irq 255*90d4f186SMatthias Ringwald 256*90d4f186SMatthias Ringwald static void btstack_run_loop_posix_poll_data_sources_handler(btstack_data_source_t * ds, btstack_data_source_callback_type_t callback_type){ 25741158b74SMatthias Ringwald UNUSED(callback_type); 258*90d4f186SMatthias Ringwald uint8_t buffer[1]; 259*90d4f186SMatthias Ringwald (void) read(ds->source.fd, buffer, 1); 260*90d4f186SMatthias Ringwald // poll data sources 261*90d4f186SMatthias Ringwald btstack_run_loop_base_poll_data_sources(); 262*90d4f186SMatthias Ringwald } 263*90d4f186SMatthias Ringwald 264*90d4f186SMatthias Ringwald static void btstack_run_loop_posix_poll_data_sources_from_irq(void){ 265*90d4f186SMatthias Ringwald // trigger run loop 266*90d4f186SMatthias Ringwald btstack_run_loop_posix_trigger_pipe(btstack_run_loop_posix_poll_data_sources_fd); 267*90d4f186SMatthias Ringwald } 268*90d4f186SMatthias Ringwald 269*90d4f186SMatthias Ringwald // execute on main thread from same or different thread 270*90d4f186SMatthias Ringwald 271*90d4f186SMatthias Ringwald static void btstack_run_loop_posix_process_callbacks_handler(btstack_data_source_t * ds, btstack_data_source_callback_type_t callback_type){ 272*90d4f186SMatthias Ringwald UNUSED(callback_type); 27341158b74SMatthias Ringwald uint8_t buffer[1]; 27441158b74SMatthias Ringwald (void) read(ds->source.fd, buffer, 1); 27541158b74SMatthias Ringwald // execute callbacks - protect list with mutex 27641158b74SMatthias Ringwald while (1){ 277*90d4f186SMatthias Ringwald pthread_mutex_lock(&btstack_run_loop_posix_callbacks_mutex); 27841158b74SMatthias Ringwald btstack_context_callback_registration_t * callback_registration = (btstack_context_callback_registration_t *) btstack_linked_list_pop(&btstack_run_loop_base_callbacks); 279*90d4f186SMatthias Ringwald pthread_mutex_unlock(&btstack_run_loop_posix_callbacks_mutex); 28041158b74SMatthias Ringwald if (callback_registration == NULL){ 28141158b74SMatthias Ringwald break; 28241158b74SMatthias Ringwald } 28341158b74SMatthias Ringwald (*callback_registration->callback)(callback_registration->context); 28441158b74SMatthias Ringwald } 28541158b74SMatthias Ringwald } 28641158b74SMatthias Ringwald 28741158b74SMatthias Ringwald static void btstack_run_loop_posix_execute_on_main_thread(btstack_context_callback_registration_t * callback_registration){ 28841158b74SMatthias Ringwald // protect list with mutex 289*90d4f186SMatthias Ringwald pthread_mutex_lock(&btstack_run_loop_posix_callbacks_mutex); 29041158b74SMatthias Ringwald btstack_run_loop_base_add_callback(callback_registration); 291*90d4f186SMatthias Ringwald pthread_mutex_unlock(&btstack_run_loop_posix_callbacks_mutex); 29241158b74SMatthias Ringwald // trigger run loop 293*90d4f186SMatthias Ringwald btstack_run_loop_posix_trigger_pipe(btstack_run_loop_posix_process_callbacks_fd); 29441158b74SMatthias Ringwald } 295*90d4f186SMatthias Ringwald 296*90d4f186SMatthias Ringwald //init 297*90d4f186SMatthias Ringwald 298*90d4f186SMatthias Ringwald // @return fd >= 0 on success 299*90d4f186SMatthias Ringwald static int btstack_run_loop_posix_register_pipe_datasource(btstack_data_source_t * data_source){ 300*90d4f186SMatthias Ringwald int fildes[2]; // 0 = read, 1 = write 301*90d4f186SMatthias Ringwald int status = pipe(fildes); 302*90d4f186SMatthias Ringwald if (status != 0){ 303*90d4f186SMatthias Ringwald log_error("pipe() failed"); 304*90d4f186SMatthias Ringwald return -1; 305*90d4f186SMatthias Ringwald } 306*90d4f186SMatthias Ringwald data_source->source.fd = fildes[0]; 307*90d4f186SMatthias Ringwald data_source->flags = DATA_SOURCE_CALLBACK_READ; 308*90d4f186SMatthias Ringwald btstack_run_loop_base_add_data_source(data_source); 309*90d4f186SMatthias Ringwald log_info("Pipe: in %u, out %u", fildes[1], fildes[0]); 310*90d4f186SMatthias Ringwald return fildes[1]; 31141158b74SMatthias Ringwald } 31241158b74SMatthias Ringwald 313528a4a3bSMatthias Ringwald static void btstack_run_loop_posix_init(void){ 314075edf12SMatthias Ringwald btstack_run_loop_base_init(); 315075edf12SMatthias Ringwald 31692b78dd7SMatthias Ringwald #ifdef _POSIX_MONOTONIC_CLOCK 31792b78dd7SMatthias Ringwald clock_gettime(CLOCK_MONOTONIC, &init_ts); 31892b78dd7SMatthias Ringwald init_ts.tv_nsec = 0; 31992b78dd7SMatthias Ringwald #else 320f316a845SMatthias Ringwald // just assume that we started at tv_usec == 0 321b7d596c1SMatthias Ringwald gettimeofday(&init_tv, NULL); 322f316a845SMatthias Ringwald init_tv.tv_usec = 0; 32392b78dd7SMatthias Ringwald #endif 324b7d596c1SMatthias Ringwald 325*90d4f186SMatthias Ringwald // setup pipe to trigger process callbacks 326*90d4f186SMatthias Ringwald btstack_run_loop_posix_process_callbacks_ds.process = &btstack_run_loop_posix_process_callbacks_handler; 327*90d4f186SMatthias Ringwald btstack_run_loop_posix_process_callbacks_fd = btstack_run_loop_posix_register_pipe_datasource(&btstack_run_loop_posix_process_callbacks_ds); 328*90d4f186SMatthias Ringwald 329*90d4f186SMatthias Ringwald // setup pipe to poll data sources 330*90d4f186SMatthias Ringwald btstack_run_loop_posix_poll_data_sources_ds.process = &btstack_run_loop_posix_poll_data_sources_handler; 331*90d4f186SMatthias Ringwald btstack_run_loop_posix_poll_data_sources_fd = btstack_run_loop_posix_register_pipe_datasource(&btstack_run_loop_posix_poll_data_sources_ds); 33241158b74SMatthias Ringwald } 333b7d596c1SMatthias Ringwald 334528a4a3bSMatthias Ringwald static const btstack_run_loop_t btstack_run_loop_posix = { 335528a4a3bSMatthias Ringwald &btstack_run_loop_posix_init, 336528a4a3bSMatthias Ringwald &btstack_run_loop_posix_add_data_source, 337528a4a3bSMatthias Ringwald &btstack_run_loop_posix_remove_data_source, 338075edf12SMatthias Ringwald &btstack_run_loop_base_enable_data_source_callbacks, 339075edf12SMatthias Ringwald &btstack_run_loop_base_disable_data_source_callbacks, 340528a4a3bSMatthias Ringwald &btstack_run_loop_posix_set_timer, 341075edf12SMatthias Ringwald &btstack_run_loop_base_add_timer, 342075edf12SMatthias Ringwald &btstack_run_loop_base_remove_timer, 343528a4a3bSMatthias Ringwald &btstack_run_loop_posix_execute, 344075edf12SMatthias Ringwald &btstack_run_loop_base_dump_timer, 345528a4a3bSMatthias Ringwald &btstack_run_loop_posix_get_time_ms, 346*90d4f186SMatthias Ringwald &btstack_run_loop_posix_poll_data_sources_from_irq, 347*90d4f186SMatthias Ringwald &btstack_run_loop_posix_execute_on_main_thread, 3482e357f7aSMatthias Ringwald &btstack_run_loop_posix_trigger_exit, 349b7d596c1SMatthias Ringwald }; 350b7d596c1SMatthias Ringwald 351b7d596c1SMatthias Ringwald /** 352528a4a3bSMatthias Ringwald * Provide btstack_run_loop_posix instance 353b7d596c1SMatthias Ringwald */ 354528a4a3bSMatthias Ringwald const btstack_run_loop_t * btstack_run_loop_posix_get_instance(void){ 355528a4a3bSMatthias Ringwald return &btstack_run_loop_posix; 356b7d596c1SMatthias Ringwald } 357b7d596c1SMatthias Ringwald 358