1 /* 2 * Copyright (C) 2014 BlueKitchen GmbH 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. Neither the name of the copyright holders nor the names of 14 * contributors may be used to endorse or promote products derived 15 * from this software without specific prior written permission. 16 * 4. Any redistribution, use, or modification is done solely for 17 * personal benefit and not for any commercial purpose or for 18 * monetary gain. 19 * 20 * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 24 * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 27 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 30 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * Please inquire about commercial licensing options at 34 * [email protected] 35 * 36 */ 37 38 #define BTSTACK_FILE__ "btstack_run_loop_windows.c" 39 40 /* 41 * btstack_run_loop_windows.c 42 */ 43 44 #include "btstack_run_loop.h" 45 #include "btstack_run_loop_windows.h" 46 #include "btstack_linked_list.h" 47 #include "btstack_debug.h" 48 #include "btstack_util.h" 49 50 #include <Windows.h> 51 52 #include <stdio.h> 53 #include <stdlib.h> 54 55 // start time. 56 static ULARGE_INTEGER start_time; 57 58 59 /** 60 * @brief Queries the current time in ms since start 61 */ 62 static uint32_t btstack_run_loop_windows_get_time_ms(void){ 63 64 FILETIME file_time; 65 SYSTEMTIME system_time; 66 ULARGE_INTEGER now_time; 67 GetSystemTime(&system_time); 68 SystemTimeToFileTime(&system_time, &file_time); 69 now_time.LowPart = file_time.dwLowDateTime; 70 now_time.HighPart = file_time.dwHighDateTime; 71 uint32_t time_ms = (now_time.QuadPart - start_time.QuadPart) / 10000; 72 log_debug("btstack_run_loop_windows_get_time_ms: %u", time_ms); 73 return time_ms; 74 } 75 76 /** 77 * Execute run_loop 78 */ 79 static void btstack_run_loop_windows_execute(void) { 80 81 btstack_linked_list_iterator_t it; 82 83 while (true) { 84 85 // process timers 86 uint32_t now_ms = btstack_run_loop_windows_get_time_ms(); 87 btstack_run_loop_base_process_timers(now_ms); 88 89 // get next timeout 90 int32_t timeout_ms = btstack_run_loop_base_get_time_until_timeout(now_ms); 91 if (timeout_ms < 0){ 92 timeout_ms = INFINITE; 93 } 94 95 // collect handles to wait for 96 HANDLE handles[100]; 97 memset(handles, 0, sizeof(handles)); 98 int num_handles = 0; 99 btstack_linked_list_iterator_init(&it, &btstack_run_loop_base_data_sources); 100 while (btstack_linked_list_iterator_has_next(&it)){ 101 btstack_data_source_t *ds = (btstack_data_source_t*) btstack_linked_list_iterator_next(&it); 102 if (ds->source.handle == 0) continue; 103 if (ds->flags & (DATA_SOURCE_CALLBACK_READ | DATA_SOURCE_CALLBACK_WRITE)){ 104 handles[num_handles++] = ds->source.handle; 105 log_debug("btstack_run_loop_execute adding handle %p", ds->source.handle); 106 } 107 } 108 109 // wait for timeout or data source to become ready 110 int res; 111 if (num_handles){ 112 // wait for ready Events or timeout 113 res = WaitForMultipleObjects(num_handles, &handles[0], 0, timeout_ms); 114 } else { 115 // just wait for timeout 116 Sleep(timeout_ms); 117 res = WAIT_TIMEOUT; 118 } 119 120 // process data source 121 if (WAIT_OBJECT_0 <= res && res < (WAIT_OBJECT_0 + num_handles)){ 122 void * triggered_handle = handles[res - WAIT_OBJECT_0]; 123 btstack_linked_list_iterator_init(&it, &btstack_run_loop_base_data_sources); 124 while (btstack_linked_list_iterator_has_next(&it)){ 125 btstack_data_source_t *ds = (btstack_data_source_t*) btstack_linked_list_iterator_next(&it); 126 log_debug("btstack_run_loop_windows_execute: check ds %p with handle %p\n", ds, ds->source.handle); 127 if (triggered_handle == ds->source.handle){ 128 if (ds->flags & DATA_SOURCE_CALLBACK_READ){ 129 log_debug("btstack_run_loop_windows_execute: process read ds %p with handle %p\n", ds, ds->source.handle); 130 ds->process(ds, DATA_SOURCE_CALLBACK_READ); 131 } else if (ds->flags & DATA_SOURCE_CALLBACK_WRITE){ 132 log_debug("btstack_run_loop_windows_execute: process write ds %p with handle %p\n", ds, ds->source.handle); 133 ds->process(ds, DATA_SOURCE_CALLBACK_WRITE); 134 } 135 break; 136 } 137 } 138 } 139 } 140 } 141 142 // set timer 143 static void btstack_run_loop_windows_set_timer(btstack_timer_source_t *a, uint32_t timeout_in_ms){ 144 uint32_t time_ms = btstack_run_loop_windows_get_time_ms(); 145 a->timeout = time_ms + timeout_in_ms; 146 log_debug("btstack_run_loop_windows_set_timer to %u ms (now %u, timeout %u)", a->timeout, time_ms, timeout_in_ms); 147 } 148 149 static void btstack_run_loop_windows_init(void){ 150 btstack_run_loop_base_init(); 151 152 // store start time 153 FILETIME file_time; 154 SYSTEMTIME system_time; 155 GetSystemTime(&system_time); 156 SystemTimeToFileTime(&system_time, &file_time); 157 start_time.LowPart = file_time.dwLowDateTime; 158 start_time.HighPart = file_time.dwHighDateTime; 159 160 log_debug("btstack_run_loop_windows_init"); 161 } 162 163 164 static const btstack_run_loop_t btstack_run_loop_windows = { 165 &btstack_run_loop_windows_init, 166 &btstack_run_loop_base_add_data_source, 167 &btstack_run_loop_base_remove_data_source, 168 &btstack_run_loop_base_enable_data_source_callbacks, 169 &btstack_run_loop_base_disable_data_source_callbacks, 170 &btstack_run_loop_windows_set_timer, 171 &btstack_run_loop_base_add_timer, 172 &btstack_run_loop_base_remove_timer, 173 &btstack_run_loop_windows_execute, 174 &btstack_run_loop_base_dump_timer, 175 &btstack_run_loop_windows_get_time_ms, 176 }; 177 178 /** 179 * Provide btstack_run_loop_windows instance 180 */ 181 const btstack_run_loop_t * btstack_run_loop_windows_get_instance(void){ 182 return &btstack_run_loop_windows; 183 } 184 185