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_util.h" 511b464e99SMatthias Ringwald #include "btstack_linked_list.h" 521b464e99SMatthias Ringwald #include "btstack_debug.h" 531b464e99SMatthias Ringwald 541b464e99SMatthias Ringwald #include <stdio.h> 551b464e99SMatthias Ringwald #include <stdlib.h> 561b464e99SMatthias Ringwald #include <sys/time.h> 571b464e99SMatthias Ringwald #include <time.h> 581b464e99SMatthias Ringwald #include <unistd.h> 591b464e99SMatthias Ringwald 60*b3483efbSMatthias Ringwald #include <QHash> 61*b3483efbSMatthias Ringwald #include <QMutex> 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 74*b3483efbSMatthias Ringwald 7584ede529SMatthias 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; 7884ede529SMatthias Ringwald #else 7984ede529SMatthias Ringwald static QHash<btstack_data_source_t *, QSocketNotifier *> read_notifiers; 8084ede529SMatthias Ringwald static QHash<btstack_data_source_t *, QSocketNotifier *> write_notifiers; 819988cd6fSMatthias Ringwald #endif 829988cd6fSMatthias Ringwald 83*b3483efbSMatthias Ringwald static QMutex run_loop_callback_mutex; 84*b3483efbSMatthias Ringwald 85*b3483efbSMatthias Ringwald static void btstack_run_loop_qt_dump_timer(void); 86*b3483efbSMatthias Ringwald 8784ede529SMatthias Ringwald static void btstack_run_loop_qt_update_data_source(btstack_data_source_t * ds){ 8884ede529SMatthias Ringwald #ifdef Q_OS_WIN 8984ede529SMatthias Ringwald QWinEventNotifier * win_notifier = win_event_notifiers.value(ds, NULL); 9084ede529SMatthias Ringwald if (win_notifier){ 9184ede529SMatthias Ringwald bool enabled = (ds->flags & (DATA_SOURCE_CALLBACK_READ | DATA_SOURCE_CALLBACK_WRITE)) != 0; 9284ede529SMatthias Ringwald win_notifier->setEnabled(enabled); 9384ede529SMatthias Ringwald } 9484ede529SMatthias Ringwald #else 9584ede529SMatthias Ringwald QSocketNotifier * read_notifier = read_notifiers.value(ds, NULL); 9684ede529SMatthias Ringwald if (read_notifier){ 9784ede529SMatthias Ringwald bool read_enabled = (ds->flags & DATA_SOURCE_CALLBACK_READ) != 0; 9884ede529SMatthias Ringwald read_notifier->setEnabled(read_enabled); 9984ede529SMatthias Ringwald } 10084ede529SMatthias Ringwald QSocketNotifier * write_notifier = write_notifiers.value(ds, NULL); 10184ede529SMatthias Ringwald if (write_notifier){ 10284ede529SMatthias Ringwald bool write_enabled = (ds->flags & DATA_SOURCE_CALLBACK_WRITE) != 0; 10384ede529SMatthias Ringwald write_notifier->setEnabled(write_enabled); 10484ede529SMatthias Ringwald } 10584ede529SMatthias Ringwald #endif 10684ede529SMatthias Ringwald } 10784ede529SMatthias Ringwald 1089988cd6fSMatthias Ringwald void btstack_run_loop_qt_add_data_source(btstack_data_source_t *ds){ 1099988cd6fSMatthias Ringwald #ifdef Q_OS_WIN 1109988cd6fSMatthias Ringwald QWinEventNotifier * win_notifier = new QWinEventNotifier(ds->source.handle); 1119988cd6fSMatthias Ringwald win_event_notifiers.insert(ds, win_notifier); 11284ede529SMatthias Ringwald btstack_run_loop_qt_update_data_source(ds); 11384ede529SMatthias Ringwald QObject::connect(win_notifier, SIGNAL(activated(HANDLE)), btstack_run_loop_object, SLOT(processDataSource(HANDLE))); 1149988cd6fSMatthias Ringwald log_debug("add data source %p with handle %p", ds, ds->source.handle); 11584ede529SMatthias Ringwald #else 11684ede529SMatthias Ringwald // POSIX Systems: macOS, Linux, .. 11784ede529SMatthias Ringwald QSocketNotifier * read_notifier = new QSocketNotifier(ds->source.fd, QSocketNotifier::Read); 11884ede529SMatthias Ringwald QSocketNotifier * write_notifier = new QSocketNotifier(ds->source.fd, QSocketNotifier::Write); 11984ede529SMatthias Ringwald read_notifiers.insert(ds, read_notifier); 12084ede529SMatthias Ringwald write_notifiers.insert(ds, write_notifier); 12184ede529SMatthias Ringwald btstack_run_loop_qt_update_data_source(ds); 12284ede529SMatthias Ringwald QObject::connect(read_notifier, SIGNAL(activated(int)), btstack_run_loop_object, SLOT(processDataSourceRead(int))); 12384ede529SMatthias Ringwald QObject::connect(write_notifier, SIGNAL(activated(int)), btstack_run_loop_object, SLOT(processDataSourceWrite(int))); 1249988cd6fSMatthias Ringwald #endif 1259988cd6fSMatthias Ringwald btstack_run_loop_base_add_data_source(ds); 1269988cd6fSMatthias Ringwald } 1279988cd6fSMatthias Ringwald 1289988cd6fSMatthias Ringwald bool btstack_run_loop_qt_remove_data_source(btstack_data_source_t *ds){ 1299988cd6fSMatthias Ringwald #ifdef Q_OS_WIN 1309988cd6fSMatthias Ringwald QWinEventNotifier * win_notifier = win_event_notifiers.value(ds, NULL); 1319988cd6fSMatthias Ringwald if (win_notifier){ 1329988cd6fSMatthias Ringwald win_event_notifiers.remove(ds); 1339988cd6fSMatthias Ringwald free(win_notifier); 1349988cd6fSMatthias Ringwald } 13584ede529SMatthias Ringwald #else 13684ede529SMatthias Ringwald QSocketNotifier * read_notifier = read_notifiers.value(ds, NULL); 13784ede529SMatthias Ringwald if (read_notifier){ 13884ede529SMatthias Ringwald read_notifiers.remove(ds); 13984ede529SMatthias Ringwald free(read_notifier); 14084ede529SMatthias Ringwald } 14184ede529SMatthias Ringwald QSocketNotifier * write_notifier = write_notifiers.value(ds, NULL); 14284ede529SMatthias Ringwald if (write_notifier){ 14384ede529SMatthias Ringwald write_notifiers.remove(ds); 14484ede529SMatthias Ringwald free(write_notifier); 14584ede529SMatthias Ringwald } 1469988cd6fSMatthias Ringwald #endif 1479988cd6fSMatthias Ringwald return btstack_run_loop_base_remove_data_source(ds); 1489988cd6fSMatthias Ringwald } 1499988cd6fSMatthias Ringwald 1509988cd6fSMatthias Ringwald void btstack_run_loop_qt_enable_data_source_callbacks(btstack_data_source_t * ds, uint16_t callback_types){ 1519988cd6fSMatthias Ringwald btstack_run_loop_base_enable_data_source_callbacks(ds, callback_types); 15284ede529SMatthias Ringwald btstack_run_loop_qt_update_data_source(ds); 1539988cd6fSMatthias Ringwald } 1549988cd6fSMatthias Ringwald 1559988cd6fSMatthias Ringwald void btstack_run_loop_qt_disable_data_source_callbacks(btstack_data_source_t * ds, uint16_t callback_types){ 1569988cd6fSMatthias Ringwald btstack_run_loop_base_disable_data_source_callbacks(ds, callback_types); 15784ede529SMatthias Ringwald btstack_run_loop_qt_update_data_source(ds); 1589988cd6fSMatthias Ringwald } 1599988cd6fSMatthias Ringwald 1601b464e99SMatthias Ringwald #ifdef _POSIX_MONOTONIC_CLOCK 1611b464e99SMatthias Ringwald /** 1621b464e99SMatthias Ringwald * @brief Returns the timespec which represents the time(stop - start). It might be negative 1631b464e99SMatthias Ringwald */ 1641b464e99SMatthias Ringwald static void timespec_diff(struct timespec *start, struct timespec *stop, struct timespec *result){ 1651b464e99SMatthias Ringwald result->tv_sec = stop->tv_sec - start->tv_sec; 1661b464e99SMatthias Ringwald if ((stop->tv_nsec - start->tv_nsec) < 0) { 1671b464e99SMatthias Ringwald result->tv_sec = stop->tv_sec - start->tv_sec - 1; 1681b464e99SMatthias Ringwald result->tv_nsec = stop->tv_nsec - start->tv_nsec + 1000000000; 1691b464e99SMatthias Ringwald } else { 1701b464e99SMatthias Ringwald result->tv_sec = stop->tv_sec - start->tv_sec; 1711b464e99SMatthias Ringwald result->tv_nsec = stop->tv_nsec - start->tv_nsec; 1721b464e99SMatthias Ringwald } 1731b464e99SMatthias Ringwald } 1741b464e99SMatthias Ringwald 1751b464e99SMatthias Ringwald /** 1761b464e99SMatthias Ringwald * @brief Convert timespec to miliseconds, might overflow 1771b464e99SMatthias Ringwald */ 1781b464e99SMatthias Ringwald static uint64_t timespec_to_milliseconds(struct timespec *a){ 1791b464e99SMatthias Ringwald uint64_t ret = 0; 1801b464e99SMatthias Ringwald uint64_t sec_val = (uint64_t)(a->tv_sec); 1811b464e99SMatthias Ringwald uint64_t nsec_val = (uint64_t)(a->tv_nsec); 1821b464e99SMatthias Ringwald ret = (sec_val*1000) + (nsec_val/1000000); 1831b464e99SMatthias Ringwald return ret; 1841b464e99SMatthias Ringwald } 1851b464e99SMatthias Ringwald 1861b464e99SMatthias Ringwald /** 1871b464e99SMatthias Ringwald * @brief Returns the milisecond value of (stop - start). Might overflow 1881b464e99SMatthias Ringwald */ 1891b464e99SMatthias Ringwald static uint64_t timespec_diff_milis(struct timespec* start, struct timespec* stop){ 1901b464e99SMatthias Ringwald struct timespec diff_ts; 1911b464e99SMatthias Ringwald timespec_diff(start, stop, &diff_ts); 1921b464e99SMatthias Ringwald return timespec_to_milliseconds(&diff_ts); 1931b464e99SMatthias Ringwald } 1941b464e99SMatthias Ringwald #endif 1951b464e99SMatthias Ringwald 1961b464e99SMatthias Ringwald /** 1971b464e99SMatthias Ringwald * @brief Queries the current time in ms since start 1981b464e99SMatthias Ringwald */ 1991b464e99SMatthias Ringwald static uint32_t btstack_run_loop_qt_get_time_ms(void){ 2001b464e99SMatthias Ringwald uint32_t time_ms; 2011b464e99SMatthias Ringwald #ifdef _POSIX_MONOTONIC_CLOCK 2021b464e99SMatthias Ringwald struct timespec now_ts; 2031b464e99SMatthias Ringwald clock_gettime(CLOCK_MONOTONIC, &now_ts); 2041b464e99SMatthias Ringwald time_ms = (uint32_t) timespec_diff_milis(&init_ts, &now_ts); 2051b464e99SMatthias Ringwald #else 2061b464e99SMatthias Ringwald struct timeval tv; 2071b464e99SMatthias Ringwald gettimeofday(&tv, NULL); 2081b464e99SMatthias Ringwald time_ms = (uint32_t) ((tv.tv_sec - init_tv.tv_sec) * 1000) + (tv.tv_usec / 1000); 2091b464e99SMatthias Ringwald #endif 2101b464e99SMatthias Ringwald return time_ms; 2111b464e99SMatthias Ringwald } 2121b464e99SMatthias Ringwald 2131b464e99SMatthias Ringwald /** 2141b464e99SMatthias Ringwald * Execute run_loop 2151b464e99SMatthias Ringwald */ 2161b464e99SMatthias Ringwald static void btstack_run_loop_qt_execute(void) { 2171b464e99SMatthias Ringwald } 2181b464e99SMatthias Ringwald 2191b464e99SMatthias Ringwald // set timer 2201b464e99SMatthias Ringwald static void btstack_run_loop_qt_set_timer(btstack_timer_source_t *a, uint32_t timeout_in_ms){ 2211b464e99SMatthias Ringwald uint32_t time_ms = btstack_run_loop_qt_get_time_ms(); 2221b464e99SMatthias Ringwald a->timeout = time_ms + timeout_in_ms; 2231b464e99SMatthias Ringwald log_debug("btstack_run_loop_qt_set_timer to %u ms (now %u, timeout %u)", a->timeout, time_ms, timeout_in_ms); 2241b464e99SMatthias Ringwald } 2251b464e99SMatthias Ringwald 2261b464e99SMatthias Ringwald /** 2271b464e99SMatthias Ringwald * Add timer to run_loop (keep list sorted) 2281b464e99SMatthias Ringwald */ 2291b464e99SMatthias Ringwald static void btstack_run_loop_qt_add_timer(btstack_timer_source_t *ts){ 2301b464e99SMatthias Ringwald uint32_t now = btstack_run_loop_qt_get_time_ms(); 2311b464e99SMatthias Ringwald int32_t next_timeout_before = btstack_run_loop_base_get_time_until_timeout(now); 2321b464e99SMatthias Ringwald btstack_run_loop_base_add_timer(ts); 2331b464e99SMatthias Ringwald int32_t next_timeout_after = btstack_run_loop_base_get_time_until_timeout(now); 2341b464e99SMatthias Ringwald if (next_timeout_after >= 0 && (next_timeout_after != next_timeout_before)){ 2351b464e99SMatthias Ringwald QTimer::singleShot((uint32_t)next_timeout_after, btstack_run_loop_object, SLOT(processTimers())); 2361b464e99SMatthias Ringwald } 2371b464e99SMatthias Ringwald } 2381b464e99SMatthias Ringwald 2391b464e99SMatthias Ringwald // BTstackRunLoopQt class implementation 240*b3483efbSMatthias Ringwald BTstackRunLoopQt::BTstackRunLoopQt(void) : QObject() { 241*b3483efbSMatthias Ringwald // allow to trigger processCallbacks both from main thread as well as other threads later 242*b3483efbSMatthias Ringwald connect(this, &BTstackRunLoopQt::callbackAdded, this, &BTstackRunLoopQt::processCallbacks, Qt::QueuedConnection); 243*b3483efbSMatthias Ringwald } 244*b3483efbSMatthias Ringwald 2451b464e99SMatthias Ringwald void BTstackRunLoopQt::processTimers(){ 2461b464e99SMatthias Ringwald uint32_t now = btstack_run_loop_qt_get_time_ms(); 2471b464e99SMatthias Ringwald int32_t next_timeout_before = btstack_run_loop_base_get_time_until_timeout(now); 2481b464e99SMatthias Ringwald btstack_run_loop_base_process_timers(btstack_run_loop_qt_get_time_ms()); 2491b464e99SMatthias Ringwald int32_t next_timeout_after = btstack_run_loop_base_get_time_until_timeout(now); 2501b464e99SMatthias Ringwald if (next_timeout_after >= 0 && (next_timeout_after != next_timeout_before)){ 2511b464e99SMatthias Ringwald QTimer::singleShot((uint32_t)next_timeout_after, this, SLOT(processTimers())); 2521b464e99SMatthias Ringwald } 2531b464e99SMatthias Ringwald } 2549988cd6fSMatthias Ringwald #ifdef Q_OS_WIN 2559988cd6fSMatthias Ringwald void BTstackRunLoopQt::processDataSource(HANDLE handle){ 2569988cd6fSMatthias Ringwald log_debug("lookup data source for handle %p, sender %p", handle, QObject::sender()); 2579988cd6fSMatthias Ringwald // find ds 2589988cd6fSMatthias Ringwald btstack_linked_list_iterator_t it; 2599988cd6fSMatthias Ringwald btstack_linked_list_iterator_init(&it, &btstack_run_loop_base_data_sources); 2609988cd6fSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 2619988cd6fSMatthias Ringwald btstack_data_source_t *ds = (btstack_data_source_t*) btstack_linked_list_iterator_next(&it); 2629988cd6fSMatthias Ringwald if (handle == ds->source.handle){ 2639988cd6fSMatthias Ringwald if (ds->flags & DATA_SOURCE_CALLBACK_READ){ 2649988cd6fSMatthias Ringwald log_debug("process read ds %p with handle %p", ds, ds->source.handle); 2659988cd6fSMatthias Ringwald ds->process(ds, DATA_SOURCE_CALLBACK_READ); 2669988cd6fSMatthias Ringwald } else if (ds->flags & DATA_SOURCE_CALLBACK_WRITE){ 2679988cd6fSMatthias Ringwald log_debug("process write ds %p with handle %p", ds, ds->source.handle); 2689988cd6fSMatthias Ringwald ds->process(ds, DATA_SOURCE_CALLBACK_WRITE); 2699988cd6fSMatthias Ringwald } 2709988cd6fSMatthias Ringwald break; 2719988cd6fSMatthias Ringwald } 2729988cd6fSMatthias Ringwald } 2739988cd6fSMatthias Ringwald } 27484ede529SMatthias Ringwald #else 27584ede529SMatthias Ringwald 276*b3483efbSMatthias Ringwald void BTstackRunLoopQt::processCallbacks(void) { 277*b3483efbSMatthias Ringwald // execute callbacks - protect list with mutex 278*b3483efbSMatthias Ringwald while (1){ 279*b3483efbSMatthias Ringwald run_loop_callback_mutex.lock(); 280*b3483efbSMatthias Ringwald btstack_context_callback_registration_t * callback_registration = (btstack_context_callback_registration_t *) btstack_linked_list_pop(&btstack_run_loop_base_callbacks); 281*b3483efbSMatthias Ringwald run_loop_callback_mutex.unlock(); 282*b3483efbSMatthias Ringwald if (callback_registration == NULL){ 283*b3483efbSMatthias Ringwald break; 284*b3483efbSMatthias Ringwald } 285*b3483efbSMatthias Ringwald (*callback_registration->callback)(callback_registration->context); 286*b3483efbSMatthias Ringwald } 287*b3483efbSMatthias Ringwald } 288*b3483efbSMatthias Ringwald 289*b3483efbSMatthias Ringwald static void btstack_run_loop_qt_execute_on_main_thread(btstack_context_callback_registration_t * callback_registration){ 290*b3483efbSMatthias Ringwald // protect list with mutex 291*b3483efbSMatthias Ringwald run_loop_callback_mutex.lock(); 292*b3483efbSMatthias Ringwald btstack_run_loop_base_add_callback(callback_registration); 293*b3483efbSMatthias Ringwald run_loop_callback_mutex.unlock(); 294*b3483efbSMatthias Ringwald btstack_run_loop_object->emit callbackAdded(); 295*b3483efbSMatthias Ringwald } 296*b3483efbSMatthias Ringwald 29784ede529SMatthias Ringwald static void btstack_run_loop_qt_process_data_source(int fd, uint16_t callback_types){ 29884ede529SMatthias Ringwald // find ds 29984ede529SMatthias Ringwald btstack_linked_list_iterator_t it; 30084ede529SMatthias Ringwald btstack_linked_list_iterator_init(&it, &btstack_run_loop_base_data_sources); 30184ede529SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 30284ede529SMatthias Ringwald btstack_data_source_t *ds = (btstack_data_source_t*) btstack_linked_list_iterator_next(&it); 30384ede529SMatthias Ringwald if (fd == ds->source.fd){ 30484ede529SMatthias Ringwald if ((callback_types & DATA_SOURCE_CALLBACK_READ) != 0){ 30584ede529SMatthias Ringwald if ((ds->flags & DATA_SOURCE_CALLBACK_READ) != 0){ 30684ede529SMatthias Ringwald ds->process(ds, DATA_SOURCE_CALLBACK_READ); 30784ede529SMatthias Ringwald } 30884ede529SMatthias Ringwald } 30984ede529SMatthias Ringwald if ((callback_types & DATA_SOURCE_CALLBACK_WRITE) != 0){ 31084ede529SMatthias Ringwald if ((ds->flags & DATA_SOURCE_CALLBACK_WRITE) != 0){ 31184ede529SMatthias Ringwald ds->process(ds, DATA_SOURCE_CALLBACK_WRITE); 31284ede529SMatthias Ringwald } 31384ede529SMatthias Ringwald } 31484ede529SMatthias Ringwald break; 31584ede529SMatthias Ringwald } 31684ede529SMatthias Ringwald } 31784ede529SMatthias Ringwald } 31884ede529SMatthias Ringwald 31984ede529SMatthias Ringwald void BTstackRunLoopQt::processDataSourceRead(int fd){ 32084ede529SMatthias Ringwald log_debug("read - lookup data source for handle %p, sender %p", fd, QObject::sender()); 32184ede529SMatthias Ringwald btstack_run_loop_qt_process_data_source(fd, DATA_SOURCE_CALLBACK_READ); 32284ede529SMatthias Ringwald } 32384ede529SMatthias Ringwald void BTstackRunLoopQt::processDataSourceWrite(int fd){ 32484ede529SMatthias Ringwald log_debug("write - lookup data source for handle %p, sender %p", fd, QObject::sender()); 32584ede529SMatthias Ringwald btstack_run_loop_qt_process_data_source(fd, DATA_SOURCE_CALLBACK_WRITE); 32684ede529SMatthias Ringwald } 3279988cd6fSMatthias Ringwald #endif 3281b464e99SMatthias Ringwald 3291b464e99SMatthias Ringwald static void btstack_run_loop_qt_init(void){ 3301b464e99SMatthias Ringwald 3311b464e99SMatthias Ringwald btstack_run_loop_base_init(); 3321b464e99SMatthias Ringwald 3331b464e99SMatthias Ringwald btstack_run_loop_object = new BTstackRunLoopQt(); 3341b464e99SMatthias Ringwald 3351b464e99SMatthias Ringwald #ifdef _POSIX_MONOTONIC_CLOCK 3361b464e99SMatthias Ringwald clock_gettime(CLOCK_MONOTONIC, &init_ts); 3371b464e99SMatthias Ringwald init_ts.tv_nsec = 0; 3381b464e99SMatthias Ringwald #else 3391b464e99SMatthias Ringwald // just assume that we started at tv_usec == 0 3401b464e99SMatthias Ringwald gettimeofday(&init_tv, NULL); 3411b464e99SMatthias Ringwald init_tv.tv_usec = 0; 3421b464e99SMatthias Ringwald #endif 3431b464e99SMatthias Ringwald } 3441b464e99SMatthias Ringwald 3451b464e99SMatthias Ringwald static void btstack_run_loop_qt_dump_timer(void){ 3461b464e99SMatthias Ringwald btstack_linked_item_t *it; 3471b464e99SMatthias Ringwald int i = 0; 3481b464e99SMatthias Ringwald for (it = (btstack_linked_item_t *) btstack_run_loop_base_timers; it ; it = it->next){ 3491b464e99SMatthias Ringwald btstack_timer_source_t *ts = (btstack_timer_source_t*) it; 3501b464e99SMatthias Ringwald log_info("timer %u (%p): timeout %u\n", i, ts, ts->timeout); 3511b464e99SMatthias Ringwald } 3521b464e99SMatthias Ringwald } 3531b464e99SMatthias Ringwald 3541b464e99SMatthias Ringwald static const btstack_run_loop_t btstack_run_loop_qt = { 3551b464e99SMatthias Ringwald &btstack_run_loop_qt_init, 3569988cd6fSMatthias Ringwald &btstack_run_loop_qt_add_data_source, 3579988cd6fSMatthias Ringwald &btstack_run_loop_qt_remove_data_source, 3589988cd6fSMatthias Ringwald &btstack_run_loop_qt_enable_data_source_callbacks, 3599988cd6fSMatthias Ringwald &btstack_run_loop_qt_disable_data_source_callbacks, 3601b464e99SMatthias Ringwald &btstack_run_loop_qt_set_timer, 3611b464e99SMatthias Ringwald &btstack_run_loop_qt_add_timer, 3621b464e99SMatthias Ringwald &btstack_run_loop_base_remove_timer, 3631b464e99SMatthias Ringwald &btstack_run_loop_qt_execute, 3641b464e99SMatthias Ringwald &btstack_run_loop_qt_dump_timer, 3651b464e99SMatthias Ringwald &btstack_run_loop_qt_get_time_ms, 366*b3483efbSMatthias Ringwald NULL, /* poll data sources from irq */ 367*b3483efbSMatthias Ringwald &btstack_run_loop_qt_execute_on_main_thread, 368*b3483efbSMatthias Ringwald NULL 3691b464e99SMatthias Ringwald }; 3701b464e99SMatthias Ringwald 3711b464e99SMatthias Ringwald /** 3721b464e99SMatthias Ringwald * Provide btstack_run_loop_posix instance 3731b464e99SMatthias Ringwald */ 3741b464e99SMatthias Ringwald const btstack_run_loop_t * btstack_run_loop_qt_get_instance(void){ 3751b464e99SMatthias Ringwald return &btstack_run_loop_qt; 3761b464e99SMatthias Ringwald } 3771b464e99SMatthias Ringwald 378