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" 52*075edf12SMatthias Ringwald #include "btstack_run_loop_base.h" 53b6fc147fSMatthias Ringwald #include "btstack_util.h" 54b7d596c1SMatthias Ringwald #include "btstack_linked_list.h" 55b7d596c1SMatthias Ringwald #include "btstack_debug.h" 56b7d596c1SMatthias Ringwald 57b7d596c1SMatthias Ringwald #include <stdio.h> 58b7d596c1SMatthias Ringwald #include <stdlib.h> 59b6fc147fSMatthias Ringwald #include <sys/select.h> 60f316a845SMatthias Ringwald #include <sys/time.h> 6192b78dd7SMatthias Ringwald #include <time.h> 6292b78dd7SMatthias Ringwald #include <unistd.h> 63b7d596c1SMatthias Ringwald 64b7d596c1SMatthias Ringwald // the run loop 65b7d596c1SMatthias Ringwald static int data_sources_modified; 6692b78dd7SMatthias Ringwald 6792b78dd7SMatthias Ringwald // start time. tv_usec/tv_nsec = 0 6892b78dd7SMatthias Ringwald #ifdef _POSIX_MONOTONIC_CLOCK 6992b78dd7SMatthias Ringwald // use monotonic clock if available 7092b78dd7SMatthias Ringwald static struct timespec init_ts; 7192b78dd7SMatthias Ringwald #else 7292b78dd7SMatthias Ringwald // fallback to gettimeofday 73b7d596c1SMatthias Ringwald static struct timeval init_tv; 7492b78dd7SMatthias Ringwald #endif 75b7d596c1SMatthias Ringwald 76b7d596c1SMatthias Ringwald /** 77b7d596c1SMatthias Ringwald * Add data_source to run_loop 78b7d596c1SMatthias Ringwald */ 79ec820d77SMatthias Ringwald static void btstack_run_loop_posix_add_data_source(btstack_data_source_t *ds){ 80b7d596c1SMatthias Ringwald data_sources_modified = 1; 81*075edf12SMatthias Ringwald btstack_run_loop_base_add_data_source(ds); 82b7d596c1SMatthias Ringwald } 83b7d596c1SMatthias Ringwald 84b7d596c1SMatthias Ringwald /** 85b7d596c1SMatthias Ringwald * Remove data_source from run loop 86b7d596c1SMatthias Ringwald */ 87d58a1b5fSMatthias Ringwald static bool btstack_run_loop_posix_remove_data_source(btstack_data_source_t *ds){ 88b7d596c1SMatthias Ringwald data_sources_modified = 1; 89*075edf12SMatthias Ringwald return btstack_run_loop_base_remove_data_source(ds); 909120e843SMatthias Ringwald } 919120e843SMatthias Ringwald 9292b78dd7SMatthias Ringwald #ifdef _POSIX_MONOTONIC_CLOCK 9392b78dd7SMatthias Ringwald /** 9492b78dd7SMatthias Ringwald * @brief Returns the timespec which represents the time(stop - start). It might be negative 9592b78dd7SMatthias Ringwald */ 9692b78dd7SMatthias Ringwald static void timespec_diff(struct timespec *start, struct timespec *stop, struct timespec *result){ 9792b78dd7SMatthias Ringwald result->tv_sec = stop->tv_sec - start->tv_sec; 9892b78dd7SMatthias Ringwald if ((stop->tv_nsec - start->tv_nsec) < 0) { 9992b78dd7SMatthias Ringwald result->tv_sec = stop->tv_sec - start->tv_sec - 1; 10092b78dd7SMatthias Ringwald result->tv_nsec = stop->tv_nsec - start->tv_nsec + 1000000000; 10192b78dd7SMatthias Ringwald } else { 10292b78dd7SMatthias Ringwald result->tv_sec = stop->tv_sec - start->tv_sec; 10392b78dd7SMatthias Ringwald result->tv_nsec = stop->tv_nsec - start->tv_nsec; 10492b78dd7SMatthias Ringwald } 10592b78dd7SMatthias Ringwald } 10692b78dd7SMatthias Ringwald 10792b78dd7SMatthias Ringwald /** 10892b78dd7SMatthias Ringwald * @brief Convert timespec to miliseconds, might overflow 10992b78dd7SMatthias Ringwald */ 11092b78dd7SMatthias Ringwald static uint64_t timespec_to_milliseconds(struct timespec *a){ 11192b78dd7SMatthias Ringwald uint64_t ret = 0; 11292b78dd7SMatthias Ringwald uint64_t sec_val = (uint64_t)(a->tv_sec); 11392b78dd7SMatthias Ringwald uint64_t nsec_val = (uint64_t)(a->tv_nsec); 11492b78dd7SMatthias Ringwald ret = (sec_val*1000) + (nsec_val/1000000); 11592b78dd7SMatthias Ringwald return ret; 11692b78dd7SMatthias Ringwald } 11792b78dd7SMatthias Ringwald 11892b78dd7SMatthias Ringwald /** 11992b78dd7SMatthias Ringwald * @brief Returns the milisecond value of (stop - start). Might overflow 12092b78dd7SMatthias Ringwald */ 12192b78dd7SMatthias Ringwald static uint64_t timespec_diff_milis(struct timespec* start, struct timespec* stop){ 12292b78dd7SMatthias Ringwald struct timespec diff_ts; 12392b78dd7SMatthias Ringwald timespec_diff(start, stop, &diff_ts); 12492b78dd7SMatthias Ringwald return timespec_to_milliseconds(&diff_ts); 12592b78dd7SMatthias Ringwald } 12692b78dd7SMatthias Ringwald #endif 12792b78dd7SMatthias Ringwald 128b7d596c1SMatthias Ringwald /** 129f316a845SMatthias Ringwald * @brief Queries the current time in ms since start 130f316a845SMatthias Ringwald */ 131f316a845SMatthias Ringwald static uint32_t btstack_run_loop_posix_get_time_ms(void){ 13292b78dd7SMatthias Ringwald uint32_t time_ms; 13392b78dd7SMatthias Ringwald #ifdef _POSIX_MONOTONIC_CLOCK 13492b78dd7SMatthias Ringwald struct timespec now_ts; 13592b78dd7SMatthias Ringwald clock_gettime(CLOCK_MONOTONIC, &now_ts); 13692b78dd7SMatthias Ringwald time_ms = (uint32_t) timespec_diff_milis(&init_ts, &now_ts); 13792b78dd7SMatthias Ringwald #else 138f316a845SMatthias Ringwald struct timeval tv; 139f316a845SMatthias Ringwald gettimeofday(&tv, NULL); 14092b78dd7SMatthias Ringwald time_ms = (uint32_t) ((tv.tv_sec - init_tv.tv_sec) * 1000) + (tv.tv_usec / 1000); 14192b78dd7SMatthias Ringwald #endif 142f316a845SMatthias Ringwald return time_ms; 143f316a845SMatthias Ringwald } 144f316a845SMatthias Ringwald 145f316a845SMatthias Ringwald /** 146b7d596c1SMatthias Ringwald * Execute run_loop 147b7d596c1SMatthias Ringwald */ 148528a4a3bSMatthias Ringwald static void btstack_run_loop_posix_execute(void) { 1499120e843SMatthias Ringwald fd_set descriptors_read; 1509120e843SMatthias Ringwald fd_set descriptors_write; 151b7d596c1SMatthias Ringwald 152b7d596c1SMatthias Ringwald btstack_linked_list_iterator_t it; 153f316a845SMatthias Ringwald struct timeval * timeout; 154f316a845SMatthias Ringwald struct timeval tv; 155f316a845SMatthias Ringwald uint32_t now_ms; 156b7d596c1SMatthias Ringwald 15792b78dd7SMatthias Ringwald #ifdef _POSIX_MONOTONIC_CLOCK 15892b78dd7SMatthias Ringwald log_info("POSIX run loop with monotonic clock"); 15992b78dd7SMatthias Ringwald #else 16092b78dd7SMatthias Ringwald log_info("POSIX run loop using ettimeofday fallback."); 16192b78dd7SMatthias Ringwald #endif 16292b78dd7SMatthias Ringwald 163ff3cc4a5SMatthias Ringwald while (true) { 164b7d596c1SMatthias Ringwald // collect FDs 1659120e843SMatthias Ringwald FD_ZERO(&descriptors_read); 1669120e843SMatthias Ringwald FD_ZERO(&descriptors_write); 167f316a845SMatthias Ringwald int highest_fd = -1; 168*075edf12SMatthias Ringwald btstack_linked_list_iterator_init(&it, &btstack_run_loop_base_data_sources); 169b7d596c1SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 170ec820d77SMatthias Ringwald btstack_data_source_t *ds = (btstack_data_source_t*) btstack_linked_list_iterator_next(&it); 171398a95ecSMatthias Ringwald if (ds->source.fd < 0) continue; 1729120e843SMatthias Ringwald if (ds->flags & DATA_SOURCE_CALLBACK_READ){ 173398a95ecSMatthias Ringwald FD_SET(ds->source.fd, &descriptors_read); 174398a95ecSMatthias Ringwald if (ds->source.fd > highest_fd) { 175398a95ecSMatthias Ringwald highest_fd = ds->source.fd; 1769120e843SMatthias Ringwald } 177398a95ecSMatthias Ringwald log_debug("btstack_run_loop_execute adding fd %u for read", ds->source.fd); 1789120e843SMatthias Ringwald } 1799120e843SMatthias Ringwald if (ds->flags & DATA_SOURCE_CALLBACK_WRITE){ 180398a95ecSMatthias Ringwald FD_SET(ds->source.fd, &descriptors_write); 181398a95ecSMatthias Ringwald if (ds->source.fd > highest_fd) { 182398a95ecSMatthias Ringwald highest_fd = ds->source.fd; 183b7d596c1SMatthias Ringwald } 184398a95ecSMatthias Ringwald log_debug("btstack_run_loop_execute adding fd %u for write", ds->source.fd); 185b7d596c1SMatthias Ringwald } 186b7d596c1SMatthias Ringwald } 187b7d596c1SMatthias Ringwald 188b7d596c1SMatthias Ringwald // get next timeout 189b7d596c1SMatthias Ringwald timeout = NULL; 190f316a845SMatthias Ringwald now_ms = btstack_run_loop_posix_get_time_ms(); 191*075edf12SMatthias Ringwald int32_t delta_ms = btstack_run_loop_base_get_time_until_timeout(now_ms); 192*075edf12SMatthias Ringwald if (delta_ms >= 0) { 193*075edf12SMatthias Ringwald timeout = &tv; 194*075edf12SMatthias Ringwald tv.tv_sec = delta_ms / 1000; 195*075edf12SMatthias Ringwald tv.tv_usec = (int) (delta_ms - (tv.tv_sec * 1000)) * 1000; 196*075edf12SMatthias Ringwald log_debug("btstack_run_loop_execute next timeout in %u ms", delta_ms); 197b7d596c1SMatthias Ringwald } 198b7d596c1SMatthias Ringwald 199b7d596c1SMatthias Ringwald // wait for ready FDs 2009120e843SMatthias Ringwald select( highest_fd+1 , &descriptors_read, &descriptors_write, NULL, timeout); 201b7d596c1SMatthias Ringwald 20288e0b3b2SMatthias Ringwald 203b7d596c1SMatthias Ringwald data_sources_modified = 0; 204*075edf12SMatthias Ringwald btstack_linked_list_iterator_init(&it, &btstack_run_loop_base_data_sources); 205b7d596c1SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it) && !data_sources_modified){ 206ec820d77SMatthias Ringwald btstack_data_source_t *ds = (btstack_data_source_t*) btstack_linked_list_iterator_next(&it); 207398a95ecSMatthias Ringwald log_debug("btstack_run_loop_posix_execute: check ds %p with fd %u\n", ds, ds->source.fd); 208398a95ecSMatthias Ringwald if (FD_ISSET(ds->source.fd, &descriptors_read)) { 209398a95ecSMatthias Ringwald log_debug("btstack_run_loop_posix_execute: process read ds %p with fd %u\n", ds, ds->source.fd); 2107cd5ef95SMatthias Ringwald ds->process(ds, DATA_SOURCE_CALLBACK_READ); 211b7d596c1SMatthias Ringwald } 2124b7565a2SMatthias Ringwald if (data_sources_modified) break; 213398a95ecSMatthias Ringwald if (FD_ISSET(ds->source.fd, &descriptors_write)) { 214398a95ecSMatthias Ringwald log_debug("btstack_run_loop_posix_execute: process write ds %p with fd %u\n", ds, ds->source.fd); 2159120e843SMatthias Ringwald ds->process(ds, DATA_SOURCE_CALLBACK_WRITE); 2169120e843SMatthias Ringwald } 217b7d596c1SMatthias Ringwald } 218f316a845SMatthias Ringwald log_debug("btstack_run_loop_posix_execute: after ds check\n"); 219b7d596c1SMatthias Ringwald 220b7d596c1SMatthias Ringwald // process timers 22188e0b3b2SMatthias Ringwald now_ms = btstack_run_loop_posix_get_time_ms(); 222*075edf12SMatthias Ringwald btstack_run_loop_base_process_timers(now_ms); 223b7d596c1SMatthias Ringwald } 224b7d596c1SMatthias Ringwald } 225b7d596c1SMatthias Ringwald 226b7d596c1SMatthias Ringwald // set timer 227ec820d77SMatthias Ringwald static void btstack_run_loop_posix_set_timer(btstack_timer_source_t *a, uint32_t timeout_in_ms){ 228f316a845SMatthias Ringwald uint32_t time_ms = btstack_run_loop_posix_get_time_ms(); 229f316a845SMatthias Ringwald a->timeout = time_ms + timeout_in_ms; 230615ae444SMatthias Ringwald log_debug("btstack_run_loop_posix_set_timer to %u ms (now %u, timeout %u)", a->timeout, time_ms, timeout_in_ms); 231b7d596c1SMatthias Ringwald } 232b7d596c1SMatthias Ringwald 233528a4a3bSMatthias Ringwald static void btstack_run_loop_posix_init(void){ 234*075edf12SMatthias Ringwald btstack_run_loop_base_init(); 235*075edf12SMatthias Ringwald 23692b78dd7SMatthias Ringwald #ifdef _POSIX_MONOTONIC_CLOCK 23792b78dd7SMatthias Ringwald clock_gettime(CLOCK_MONOTONIC, &init_ts); 23892b78dd7SMatthias Ringwald init_ts.tv_nsec = 0; 23992b78dd7SMatthias Ringwald #else 240f316a845SMatthias Ringwald // just assume that we started at tv_usec == 0 241b7d596c1SMatthias Ringwald gettimeofday(&init_tv, NULL); 242f316a845SMatthias Ringwald init_tv.tv_usec = 0; 24392b78dd7SMatthias Ringwald #endif 244b7d596c1SMatthias Ringwald } 245b7d596c1SMatthias Ringwald 246b7d596c1SMatthias Ringwald 247528a4a3bSMatthias Ringwald static const btstack_run_loop_t btstack_run_loop_posix = { 248528a4a3bSMatthias Ringwald &btstack_run_loop_posix_init, 249528a4a3bSMatthias Ringwald &btstack_run_loop_posix_add_data_source, 250528a4a3bSMatthias Ringwald &btstack_run_loop_posix_remove_data_source, 251*075edf12SMatthias Ringwald &btstack_run_loop_base_enable_data_source_callbacks, 252*075edf12SMatthias Ringwald &btstack_run_loop_base_disable_data_source_callbacks, 253528a4a3bSMatthias Ringwald &btstack_run_loop_posix_set_timer, 254*075edf12SMatthias Ringwald &btstack_run_loop_base_add_timer, 255*075edf12SMatthias Ringwald &btstack_run_loop_base_remove_timer, 256528a4a3bSMatthias Ringwald &btstack_run_loop_posix_execute, 257*075edf12SMatthias Ringwald &btstack_run_loop_base_dump_timer, 258528a4a3bSMatthias Ringwald &btstack_run_loop_posix_get_time_ms, 259b7d596c1SMatthias Ringwald }; 260b7d596c1SMatthias Ringwald 261b7d596c1SMatthias Ringwald /** 262528a4a3bSMatthias Ringwald * Provide btstack_run_loop_posix instance 263b7d596c1SMatthias Ringwald */ 264528a4a3bSMatthias Ringwald const btstack_run_loop_t * btstack_run_loop_posix_get_instance(void){ 265528a4a3bSMatthias Ringwald return &btstack_run_loop_posix; 266b7d596c1SMatthias Ringwald } 267b7d596c1SMatthias Ringwald 268