11b464e99SMatthias Ringwald /* 21b464e99SMatthias Ringwald * Copyright (C) 2019 BlueKitchen GmbH 31b464e99SMatthias Ringwald * 41b464e99SMatthias Ringwald * Redistribution and use in source and binary forms, with or without 51b464e99SMatthias Ringwald * modification, are permitted provided that the following conditions 61b464e99SMatthias Ringwald * are met: 71b464e99SMatthias Ringwald * 81b464e99SMatthias Ringwald * 1. Redistributions of source code must retain the above copyright 91b464e99SMatthias Ringwald * notice, this list of conditions and the following disclaimer. 101b464e99SMatthias Ringwald * 2. Redistributions in binary form must reproduce the above copyright 111b464e99SMatthias Ringwald * notice, this list of conditions and the following disclaimer in the 121b464e99SMatthias Ringwald * documentation and/or other materials provided with the distribution. 131b464e99SMatthias Ringwald * 3. Neither the name of the copyright holders nor the names of 141b464e99SMatthias Ringwald * contributors may be used to endorse or promote products derived 151b464e99SMatthias Ringwald * from this software without specific prior written permission. 161b464e99SMatthias Ringwald * 4. Any redistribution, use, or modification is done solely for 171b464e99SMatthias Ringwald * personal benefit and not for any commercial purpose or for 181b464e99SMatthias Ringwald * monetary gain. 191b464e99SMatthias Ringwald * 201b464e99SMatthias Ringwald * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 211b464e99SMatthias Ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 221b464e99SMatthias Ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 231b464e99SMatthias Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 241b464e99SMatthias Ringwald * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 251b464e99SMatthias Ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 261b464e99SMatthias Ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 271b464e99SMatthias Ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 281b464e99SMatthias Ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 291b464e99SMatthias Ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 301b464e99SMatthias Ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 311b464e99SMatthias Ringwald * SUCH DAMAGE. 321b464e99SMatthias Ringwald * 331b464e99SMatthias Ringwald * Please inquire about commercial licensing options at 341b464e99SMatthias Ringwald * [email protected] 351b464e99SMatthias Ringwald * 361b464e99SMatthias Ringwald */ 371b464e99SMatthias Ringwald 381b464e99SMatthias Ringwald #define BTSTACK_FILE__ "btstack_run_loop_qt.c" 391b464e99SMatthias Ringwald 401b464e99SMatthias Ringwald /* 411b464e99SMatthias Ringwald * btstack_run_loop_qt.c 421b464e99SMatthias Ringwald */ 431b464e99SMatthias Ringwald 441b464e99SMatthias Ringwald // enable POSIX functions (needed for -std=c99) 451b464e99SMatthias Ringwald #define _POSIX_C_SOURCE 200809 461b464e99SMatthias Ringwald 471b464e99SMatthias Ringwald #include "btstack_run_loop_qt.h" 481b464e99SMatthias Ringwald 491b464e99SMatthias Ringwald #include "btstack_run_loop.h" 501b464e99SMatthias Ringwald #include "btstack_run_loop_base.h" 511b464e99SMatthias Ringwald #include "btstack_util.h" 521b464e99SMatthias Ringwald #include "btstack_linked_list.h" 531b464e99SMatthias Ringwald #include "btstack_debug.h" 541b464e99SMatthias Ringwald 551b464e99SMatthias Ringwald #include <stdio.h> 561b464e99SMatthias Ringwald #include <stdlib.h> 571b464e99SMatthias Ringwald #include <sys/time.h> 581b464e99SMatthias Ringwald #include <time.h> 591b464e99SMatthias Ringwald #include <unistd.h> 601b464e99SMatthias Ringwald 611b464e99SMatthias Ringwald static void btstack_run_loop_qt_dump_timer(void); 621b464e99SMatthias Ringwald 631b464e99SMatthias Ringwald // start time. tv_usec/tv_nsec = 0 641b464e99SMatthias Ringwald #ifdef _POSIX_MONOTONIC_CLOCK 651b464e99SMatthias Ringwald // use monotonic clock if available 661b464e99SMatthias Ringwald static struct timespec init_ts; 671b464e99SMatthias Ringwald #else 681b464e99SMatthias Ringwald // fallback to gettimeofday 691b464e99SMatthias Ringwald static struct timeval init_tv; 701b464e99SMatthias Ringwald #endif 711b464e99SMatthias Ringwald 721b464e99SMatthias Ringwald static BTstackRunLoopQt * btstack_run_loop_object; 731b464e99SMatthias Ringwald 749988cd6fSMatthias Ringwald #include <QHash> 75*84ede529SMatthias Ringwald #ifdef Q_OS_WIN 769988cd6fSMatthias Ringwald // associate each data source with QWinEventNotifier 779988cd6fSMatthias Ringwald static QHash<btstack_data_source_t *, QWinEventNotifier *> win_event_notifiers; 78*84ede529SMatthias Ringwald #else 79*84ede529SMatthias Ringwald static QHash<btstack_data_source_t *, QSocketNotifier *> read_notifiers; 80*84ede529SMatthias Ringwald static QHash<btstack_data_source_t *, QSocketNotifier *> write_notifiers; 819988cd6fSMatthias Ringwald #endif 829988cd6fSMatthias Ringwald 83*84ede529SMatthias Ringwald static void btstack_run_loop_qt_update_data_source(btstack_data_source_t * ds){ 84*84ede529SMatthias Ringwald #ifdef Q_OS_WIN 85*84ede529SMatthias Ringwald QWinEventNotifier * win_notifier = win_event_notifiers.value(ds, NULL); 86*84ede529SMatthias Ringwald if (win_notifier){ 87*84ede529SMatthias Ringwald bool enabled = (ds->flags & (DATA_SOURCE_CALLBACK_READ | DATA_SOURCE_CALLBACK_WRITE)) != 0; 88*84ede529SMatthias Ringwald win_notifier->setEnabled(enabled); 89*84ede529SMatthias Ringwald } 90*84ede529SMatthias Ringwald #else 91*84ede529SMatthias Ringwald QSocketNotifier * read_notifier = read_notifiers.value(ds, NULL); 92*84ede529SMatthias Ringwald if (read_notifier){ 93*84ede529SMatthias Ringwald bool read_enabled = (ds->flags & DATA_SOURCE_CALLBACK_READ) != 0; 94*84ede529SMatthias Ringwald read_notifier->setEnabled(read_enabled); 95*84ede529SMatthias Ringwald } 96*84ede529SMatthias Ringwald QSocketNotifier * write_notifier = write_notifiers.value(ds, NULL); 97*84ede529SMatthias Ringwald if (write_notifier){ 98*84ede529SMatthias Ringwald bool write_enabled = (ds->flags & DATA_SOURCE_CALLBACK_WRITE) != 0; 99*84ede529SMatthias Ringwald write_notifier->setEnabled(write_enabled); 100*84ede529SMatthias Ringwald } 101*84ede529SMatthias Ringwald #endif 102*84ede529SMatthias Ringwald } 103*84ede529SMatthias Ringwald 1049988cd6fSMatthias Ringwald void btstack_run_loop_qt_add_data_source(btstack_data_source_t *ds){ 1059988cd6fSMatthias Ringwald #ifdef Q_OS_WIN 1069988cd6fSMatthias Ringwald QWinEventNotifier * win_notifier = new QWinEventNotifier(ds->source.handle); 1079988cd6fSMatthias Ringwald win_event_notifiers.insert(ds, win_notifier); 108*84ede529SMatthias Ringwald btstack_run_loop_qt_update_data_source(ds); 109*84ede529SMatthias Ringwald QObject::connect(win_notifier, SIGNAL(activated(HANDLE)), btstack_run_loop_object, SLOT(processDataSource(HANDLE))); 1109988cd6fSMatthias Ringwald log_debug("add data source %p with handle %p", ds, ds->source.handle); 111*84ede529SMatthias Ringwald #else 112*84ede529SMatthias Ringwald // POSIX Systems: macOS, Linux, .. 113*84ede529SMatthias Ringwald QSocketNotifier * read_notifier = new QSocketNotifier(ds->source.fd, QSocketNotifier::Read); 114*84ede529SMatthias Ringwald QSocketNotifier * write_notifier = new QSocketNotifier(ds->source.fd, QSocketNotifier::Write); 115*84ede529SMatthias Ringwald read_notifiers.insert(ds, read_notifier); 116*84ede529SMatthias Ringwald write_notifiers.insert(ds, write_notifier); 117*84ede529SMatthias Ringwald btstack_run_loop_qt_update_data_source(ds); 118*84ede529SMatthias Ringwald QObject::connect(read_notifier, SIGNAL(activated(int)), btstack_run_loop_object, SLOT(processDataSourceRead(int))); 119*84ede529SMatthias Ringwald QObject::connect(write_notifier, SIGNAL(activated(int)), btstack_run_loop_object, SLOT(processDataSourceWrite(int))); 1209988cd6fSMatthias Ringwald #endif 1219988cd6fSMatthias Ringwald btstack_run_loop_base_add_data_source(ds); 1229988cd6fSMatthias Ringwald } 1239988cd6fSMatthias Ringwald 1249988cd6fSMatthias Ringwald bool btstack_run_loop_qt_remove_data_source(btstack_data_source_t *ds){ 1259988cd6fSMatthias Ringwald #ifdef Q_OS_WIN 1269988cd6fSMatthias Ringwald QWinEventNotifier * win_notifier = win_event_notifiers.value(ds, NULL); 1279988cd6fSMatthias Ringwald if (win_notifier){ 1289988cd6fSMatthias Ringwald win_event_notifiers.remove(ds); 1299988cd6fSMatthias Ringwald free(win_notifier); 1309988cd6fSMatthias Ringwald } 131*84ede529SMatthias Ringwald #else 132*84ede529SMatthias Ringwald QSocketNotifier * read_notifier = read_notifiers.value(ds, NULL); 133*84ede529SMatthias Ringwald if (read_notifier){ 134*84ede529SMatthias Ringwald read_notifiers.remove(ds); 135*84ede529SMatthias Ringwald free(read_notifier); 136*84ede529SMatthias Ringwald } 137*84ede529SMatthias Ringwald QSocketNotifier * write_notifier = write_notifiers.value(ds, NULL); 138*84ede529SMatthias Ringwald if (write_notifier){ 139*84ede529SMatthias Ringwald write_notifiers.remove(ds); 140*84ede529SMatthias Ringwald free(write_notifier); 141*84ede529SMatthias Ringwald } 1429988cd6fSMatthias Ringwald #endif 1439988cd6fSMatthias Ringwald return btstack_run_loop_base_remove_data_source(ds); 1449988cd6fSMatthias Ringwald } 1459988cd6fSMatthias Ringwald 1469988cd6fSMatthias Ringwald void btstack_run_loop_qt_enable_data_source_callbacks(btstack_data_source_t * ds, uint16_t callback_types){ 1479988cd6fSMatthias Ringwald btstack_run_loop_base_enable_data_source_callbacks(ds, callback_types); 148*84ede529SMatthias Ringwald btstack_run_loop_qt_update_data_source(ds); 1499988cd6fSMatthias Ringwald } 1509988cd6fSMatthias Ringwald 1519988cd6fSMatthias Ringwald void btstack_run_loop_qt_disable_data_source_callbacks(btstack_data_source_t * ds, uint16_t callback_types){ 1529988cd6fSMatthias Ringwald btstack_run_loop_base_disable_data_source_callbacks(ds, callback_types); 153*84ede529SMatthias Ringwald btstack_run_loop_qt_update_data_source(ds); 1549988cd6fSMatthias Ringwald } 1559988cd6fSMatthias Ringwald 1561b464e99SMatthias Ringwald #ifdef _POSIX_MONOTONIC_CLOCK 1571b464e99SMatthias Ringwald /** 1581b464e99SMatthias Ringwald * @brief Returns the timespec which represents the time(stop - start). It might be negative 1591b464e99SMatthias Ringwald */ 1601b464e99SMatthias Ringwald static void timespec_diff(struct timespec *start, struct timespec *stop, struct timespec *result){ 1611b464e99SMatthias Ringwald result->tv_sec = stop->tv_sec - start->tv_sec; 1621b464e99SMatthias Ringwald if ((stop->tv_nsec - start->tv_nsec) < 0) { 1631b464e99SMatthias Ringwald result->tv_sec = stop->tv_sec - start->tv_sec - 1; 1641b464e99SMatthias Ringwald result->tv_nsec = stop->tv_nsec - start->tv_nsec + 1000000000; 1651b464e99SMatthias Ringwald } else { 1661b464e99SMatthias Ringwald result->tv_sec = stop->tv_sec - start->tv_sec; 1671b464e99SMatthias Ringwald result->tv_nsec = stop->tv_nsec - start->tv_nsec; 1681b464e99SMatthias Ringwald } 1691b464e99SMatthias Ringwald } 1701b464e99SMatthias Ringwald 1711b464e99SMatthias Ringwald /** 1721b464e99SMatthias Ringwald * @brief Convert timespec to miliseconds, might overflow 1731b464e99SMatthias Ringwald */ 1741b464e99SMatthias Ringwald static uint64_t timespec_to_milliseconds(struct timespec *a){ 1751b464e99SMatthias Ringwald uint64_t ret = 0; 1761b464e99SMatthias Ringwald uint64_t sec_val = (uint64_t)(a->tv_sec); 1771b464e99SMatthias Ringwald uint64_t nsec_val = (uint64_t)(a->tv_nsec); 1781b464e99SMatthias Ringwald ret = (sec_val*1000) + (nsec_val/1000000); 1791b464e99SMatthias Ringwald return ret; 1801b464e99SMatthias Ringwald } 1811b464e99SMatthias Ringwald 1821b464e99SMatthias Ringwald /** 1831b464e99SMatthias Ringwald * @brief Returns the milisecond value of (stop - start). Might overflow 1841b464e99SMatthias Ringwald */ 1851b464e99SMatthias Ringwald static uint64_t timespec_diff_milis(struct timespec* start, struct timespec* stop){ 1861b464e99SMatthias Ringwald struct timespec diff_ts; 1871b464e99SMatthias Ringwald timespec_diff(start, stop, &diff_ts); 1881b464e99SMatthias Ringwald return timespec_to_milliseconds(&diff_ts); 1891b464e99SMatthias Ringwald } 1901b464e99SMatthias Ringwald #endif 1911b464e99SMatthias Ringwald 1921b464e99SMatthias Ringwald /** 1931b464e99SMatthias Ringwald * @brief Queries the current time in ms since start 1941b464e99SMatthias Ringwald */ 1951b464e99SMatthias Ringwald static uint32_t btstack_run_loop_qt_get_time_ms(void){ 1961b464e99SMatthias Ringwald uint32_t time_ms; 1971b464e99SMatthias Ringwald #ifdef _POSIX_MONOTONIC_CLOCK 1981b464e99SMatthias Ringwald struct timespec now_ts; 1991b464e99SMatthias Ringwald clock_gettime(CLOCK_MONOTONIC, &now_ts); 2001b464e99SMatthias Ringwald time_ms = (uint32_t) timespec_diff_milis(&init_ts, &now_ts); 2011b464e99SMatthias Ringwald #else 2021b464e99SMatthias Ringwald struct timeval tv; 2031b464e99SMatthias Ringwald gettimeofday(&tv, NULL); 2041b464e99SMatthias Ringwald time_ms = (uint32_t) ((tv.tv_sec - init_tv.tv_sec) * 1000) + (tv.tv_usec / 1000); 2051b464e99SMatthias Ringwald #endif 2061b464e99SMatthias Ringwald return time_ms; 2071b464e99SMatthias Ringwald } 2081b464e99SMatthias Ringwald 2091b464e99SMatthias Ringwald /** 2101b464e99SMatthias Ringwald * Execute run_loop 2111b464e99SMatthias Ringwald */ 2121b464e99SMatthias Ringwald static void btstack_run_loop_qt_execute(void) { 2131b464e99SMatthias Ringwald } 2141b464e99SMatthias Ringwald 2151b464e99SMatthias Ringwald // set timer 2161b464e99SMatthias Ringwald static void btstack_run_loop_qt_set_timer(btstack_timer_source_t *a, uint32_t timeout_in_ms){ 2171b464e99SMatthias Ringwald uint32_t time_ms = btstack_run_loop_qt_get_time_ms(); 2181b464e99SMatthias Ringwald a->timeout = time_ms + timeout_in_ms; 2191b464e99SMatthias Ringwald log_debug("btstack_run_loop_qt_set_timer to %u ms (now %u, timeout %u)", a->timeout, time_ms, timeout_in_ms); 2201b464e99SMatthias Ringwald } 2211b464e99SMatthias Ringwald 2221b464e99SMatthias Ringwald /** 2231b464e99SMatthias Ringwald * Add timer to run_loop (keep list sorted) 2241b464e99SMatthias Ringwald */ 2251b464e99SMatthias Ringwald static void btstack_run_loop_qt_add_timer(btstack_timer_source_t *ts){ 2261b464e99SMatthias Ringwald uint32_t now = btstack_run_loop_qt_get_time_ms(); 2271b464e99SMatthias Ringwald int32_t next_timeout_before = btstack_run_loop_base_get_time_until_timeout(now); 2281b464e99SMatthias Ringwald btstack_run_loop_base_add_timer(ts); 2291b464e99SMatthias Ringwald int32_t next_timeout_after = btstack_run_loop_base_get_time_until_timeout(now); 2301b464e99SMatthias Ringwald if (next_timeout_after >= 0 && (next_timeout_after != next_timeout_before)){ 2311b464e99SMatthias Ringwald QTimer::singleShot((uint32_t)next_timeout_after, btstack_run_loop_object, SLOT(processTimers())); 2321b464e99SMatthias Ringwald } 2331b464e99SMatthias Ringwald } 2341b464e99SMatthias Ringwald 2351b464e99SMatthias Ringwald // BTstackRunLoopQt class implementation 2361b464e99SMatthias Ringwald void BTstackRunLoopQt::processTimers(){ 2371b464e99SMatthias Ringwald uint32_t now = btstack_run_loop_qt_get_time_ms(); 2381b464e99SMatthias Ringwald int32_t next_timeout_before = btstack_run_loop_base_get_time_until_timeout(now); 2391b464e99SMatthias Ringwald btstack_run_loop_base_process_timers(btstack_run_loop_qt_get_time_ms()); 2401b464e99SMatthias Ringwald int32_t next_timeout_after = btstack_run_loop_base_get_time_until_timeout(now); 2411b464e99SMatthias Ringwald if (next_timeout_after >= 0 && (next_timeout_after != next_timeout_before)){ 2421b464e99SMatthias Ringwald QTimer::singleShot((uint32_t)next_timeout_after, this, SLOT(processTimers())); 2431b464e99SMatthias Ringwald } 2441b464e99SMatthias Ringwald } 2459988cd6fSMatthias Ringwald #ifdef Q_OS_WIN 2469988cd6fSMatthias Ringwald void BTstackRunLoopQt::processDataSource(HANDLE handle){ 2479988cd6fSMatthias Ringwald log_debug("lookup data source for handle %p, sender %p", handle, QObject::sender()); 2489988cd6fSMatthias Ringwald // find ds 2499988cd6fSMatthias Ringwald btstack_linked_list_iterator_t it; 2509988cd6fSMatthias Ringwald btstack_linked_list_iterator_init(&it, &btstack_run_loop_base_data_sources); 2519988cd6fSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 2529988cd6fSMatthias Ringwald btstack_data_source_t *ds = (btstack_data_source_t*) btstack_linked_list_iterator_next(&it); 2539988cd6fSMatthias Ringwald if (handle == ds->source.handle){ 2549988cd6fSMatthias Ringwald if (ds->flags & DATA_SOURCE_CALLBACK_READ){ 2559988cd6fSMatthias Ringwald log_debug("process read ds %p with handle %p", ds, ds->source.handle); 2569988cd6fSMatthias Ringwald ds->process(ds, DATA_SOURCE_CALLBACK_READ); 2579988cd6fSMatthias Ringwald } else if (ds->flags & DATA_SOURCE_CALLBACK_WRITE){ 2589988cd6fSMatthias Ringwald log_debug("process write ds %p with handle %p", ds, ds->source.handle); 2599988cd6fSMatthias Ringwald ds->process(ds, DATA_SOURCE_CALLBACK_WRITE); 2609988cd6fSMatthias Ringwald } 2619988cd6fSMatthias Ringwald break; 2629988cd6fSMatthias Ringwald } 2639988cd6fSMatthias Ringwald } 2649988cd6fSMatthias Ringwald } 265*84ede529SMatthias Ringwald #else 266*84ede529SMatthias Ringwald 267*84ede529SMatthias Ringwald static void btstack_run_loop_qt_process_data_source(int fd, uint16_t callback_types){ 268*84ede529SMatthias Ringwald // find ds 269*84ede529SMatthias Ringwald btstack_linked_list_iterator_t it; 270*84ede529SMatthias Ringwald btstack_linked_list_iterator_init(&it, &btstack_run_loop_base_data_sources); 271*84ede529SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 272*84ede529SMatthias Ringwald btstack_data_source_t *ds = (btstack_data_source_t*) btstack_linked_list_iterator_next(&it); 273*84ede529SMatthias Ringwald if (fd == ds->source.fd){ 274*84ede529SMatthias Ringwald if ((callback_types & DATA_SOURCE_CALLBACK_READ) != 0){ 275*84ede529SMatthias Ringwald if ((ds->flags & DATA_SOURCE_CALLBACK_READ) != 0){ 276*84ede529SMatthias Ringwald ds->process(ds, DATA_SOURCE_CALLBACK_READ); 277*84ede529SMatthias Ringwald } 278*84ede529SMatthias Ringwald } 279*84ede529SMatthias Ringwald if ((callback_types & DATA_SOURCE_CALLBACK_WRITE) != 0){ 280*84ede529SMatthias Ringwald if ((ds->flags & DATA_SOURCE_CALLBACK_WRITE) != 0){ 281*84ede529SMatthias Ringwald ds->process(ds, DATA_SOURCE_CALLBACK_WRITE); 282*84ede529SMatthias Ringwald } 283*84ede529SMatthias Ringwald } 284*84ede529SMatthias Ringwald break; 285*84ede529SMatthias Ringwald } 286*84ede529SMatthias Ringwald } 287*84ede529SMatthias Ringwald } 288*84ede529SMatthias Ringwald 289*84ede529SMatthias Ringwald void BTstackRunLoopQt::processDataSourceRead(int fd){ 290*84ede529SMatthias Ringwald log_debug("read - lookup data source for handle %p, sender %p", fd, QObject::sender()); 291*84ede529SMatthias Ringwald btstack_run_loop_qt_process_data_source(fd, DATA_SOURCE_CALLBACK_READ); 292*84ede529SMatthias Ringwald } 293*84ede529SMatthias Ringwald void BTstackRunLoopQt::processDataSourceWrite(int fd){ 294*84ede529SMatthias Ringwald log_debug("write - lookup data source for handle %p, sender %p", fd, QObject::sender()); 295*84ede529SMatthias Ringwald btstack_run_loop_qt_process_data_source(fd, DATA_SOURCE_CALLBACK_WRITE); 296*84ede529SMatthias Ringwald } 2979988cd6fSMatthias Ringwald #endif 2981b464e99SMatthias Ringwald 2991b464e99SMatthias Ringwald static void btstack_run_loop_qt_init(void){ 3001b464e99SMatthias Ringwald 3011b464e99SMatthias Ringwald btstack_run_loop_base_init(); 3021b464e99SMatthias Ringwald 3031b464e99SMatthias Ringwald btstack_run_loop_object = new BTstackRunLoopQt(); 3041b464e99SMatthias Ringwald 3051b464e99SMatthias Ringwald #ifdef _POSIX_MONOTONIC_CLOCK 3061b464e99SMatthias Ringwald clock_gettime(CLOCK_MONOTONIC, &init_ts); 3071b464e99SMatthias Ringwald init_ts.tv_nsec = 0; 3081b464e99SMatthias Ringwald #else 3091b464e99SMatthias Ringwald // just assume that we started at tv_usec == 0 3101b464e99SMatthias Ringwald gettimeofday(&init_tv, NULL); 3111b464e99SMatthias Ringwald init_tv.tv_usec = 0; 3121b464e99SMatthias Ringwald #endif 3131b464e99SMatthias Ringwald } 3141b464e99SMatthias Ringwald 3151b464e99SMatthias Ringwald static void btstack_run_loop_qt_dump_timer(void){ 3161b464e99SMatthias Ringwald btstack_linked_item_t *it; 3171b464e99SMatthias Ringwald int i = 0; 3181b464e99SMatthias Ringwald for (it = (btstack_linked_item_t *) btstack_run_loop_base_timers; it ; it = it->next){ 3191b464e99SMatthias Ringwald btstack_timer_source_t *ts = (btstack_timer_source_t*) it; 3201b464e99SMatthias Ringwald log_info("timer %u (%p): timeout %u\n", i, ts, ts->timeout); 3211b464e99SMatthias Ringwald } 3221b464e99SMatthias Ringwald } 3231b464e99SMatthias Ringwald 3241b464e99SMatthias Ringwald static const btstack_run_loop_t btstack_run_loop_qt = { 3251b464e99SMatthias Ringwald &btstack_run_loop_qt_init, 3269988cd6fSMatthias Ringwald &btstack_run_loop_qt_add_data_source, 3279988cd6fSMatthias Ringwald &btstack_run_loop_qt_remove_data_source, 3289988cd6fSMatthias Ringwald &btstack_run_loop_qt_enable_data_source_callbacks, 3299988cd6fSMatthias Ringwald &btstack_run_loop_qt_disable_data_source_callbacks, 3301b464e99SMatthias Ringwald &btstack_run_loop_qt_set_timer, 3311b464e99SMatthias Ringwald &btstack_run_loop_qt_add_timer, 3321b464e99SMatthias Ringwald &btstack_run_loop_base_remove_timer, 3331b464e99SMatthias Ringwald &btstack_run_loop_qt_execute, 3341b464e99SMatthias Ringwald &btstack_run_loop_qt_dump_timer, 3351b464e99SMatthias Ringwald &btstack_run_loop_qt_get_time_ms, 3361b464e99SMatthias Ringwald }; 3371b464e99SMatthias Ringwald 3381b464e99SMatthias Ringwald /** 3391b464e99SMatthias Ringwald * Provide btstack_run_loop_posix instance 3401b464e99SMatthias Ringwald */ 3411b464e99SMatthias Ringwald const btstack_run_loop_t * btstack_run_loop_qt_get_instance(void){ 3421b464e99SMatthias Ringwald return &btstack_run_loop_qt; 3431b464e99SMatthias Ringwald } 3441b464e99SMatthias Ringwald 345