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_base.h" 46 #include "btstack_run_loop_windows.h" 47 #include "btstack_linked_list.h" 48 #include "btstack_debug.h" 49 #include "btstack_util.h" 50 51 #include <Windows.h> 52 53 #include <stdio.h> 54 #include <stdlib.h> 55 56 // start time. 57 static ULARGE_INTEGER start_time; 58 59 60 /** 61 * @brief Queries the current time in ms since start 62 */ 63 static uint32_t btstack_run_loop_windows_get_time_ms(void){ 64 65 FILETIME file_time; 66 SYSTEMTIME system_time; 67 ULARGE_INTEGER now_time; 68 GetSystemTime(&system_time); 69 SystemTimeToFileTime(&system_time, &file_time); 70 now_time.LowPart = file_time.dwLowDateTime; 71 now_time.HighPart = file_time.dwHighDateTime; 72 uint32_t time_ms = (now_time.QuadPart - start_time.QuadPart) / 10000; 73 log_debug("btstack_run_loop_windows_get_time_ms: %u", time_ms); 74 return time_ms; 75 } 76 77 /** 78 * Execute run_loop 79 */ 80 static void btstack_run_loop_windows_execute(void) { 81 82 btstack_linked_list_iterator_t it; 83 84 while (true) { 85 86 // process timers 87 uint32_t now_ms = btstack_run_loop_windows_get_time_ms(); 88 btstack_run_loop_base_process_timers(now_ms); 89 90 // get next timeout 91 int32_t timeout_ms = btstack_run_loop_base_get_time_until_timeout(now_ms); 92 if (timeout_ms < 0){ 93 timeout_ms = INFINITE; 94 } 95 96 // collect handles to wait for 97 HANDLE handles[100]; 98 memset(handles, 0, sizeof(handles)); 99 int num_handles = 0; 100 btstack_linked_list_iterator_init(&it, &btstack_run_loop_base_data_sources); 101 while (btstack_linked_list_iterator_has_next(&it)){ 102 btstack_data_source_t *ds = (btstack_data_source_t*) btstack_linked_list_iterator_next(&it); 103 if (ds->source.handle == 0) continue; 104 if (ds->flags & (DATA_SOURCE_CALLBACK_READ | DATA_SOURCE_CALLBACK_WRITE)){ 105 handles[num_handles++] = ds->source.handle; 106 log_debug("btstack_run_loop_execute adding handle %p", ds->source.handle); 107 } 108 } 109 110 // wait for timeout or data source to become ready 111 int res; 112 if (num_handles){ 113 // wait for ready Events or timeout 114 res = WaitForMultipleObjects(num_handles, &handles[0], 0, timeout_ms); 115 } else { 116 // just wait for timeout 117 Sleep(timeout_ms); 118 res = WAIT_TIMEOUT; 119 } 120 121 // process data source 122 if (WAIT_OBJECT_0 <= res && res < (WAIT_OBJECT_0 + num_handles)){ 123 void * triggered_handle = handles[res - WAIT_OBJECT_0]; 124 btstack_linked_list_iterator_init(&it, &btstack_run_loop_base_data_sources); 125 while (btstack_linked_list_iterator_has_next(&it)){ 126 btstack_data_source_t *ds = (btstack_data_source_t*) btstack_linked_list_iterator_next(&it); 127 log_debug("btstack_run_loop_windows_execute: check ds %p with handle %p\n", ds, ds->source.handle); 128 if (triggered_handle == ds->source.handle){ 129 if (ds->flags & DATA_SOURCE_CALLBACK_READ){ 130 log_debug("btstack_run_loop_windows_execute: process read ds %p with handle %p\n", ds, ds->source.handle); 131 ds->process(ds, DATA_SOURCE_CALLBACK_READ); 132 } else if (ds->flags & DATA_SOURCE_CALLBACK_WRITE){ 133 log_debug("btstack_run_loop_windows_execute: process write ds %p with handle %p\n", ds, ds->source.handle); 134 ds->process(ds, DATA_SOURCE_CALLBACK_WRITE); 135 } 136 break; 137 } 138 } 139 } 140 } 141 } 142 143 // set timer 144 static void btstack_run_loop_windows_set_timer(btstack_timer_source_t *a, uint32_t timeout_in_ms){ 145 uint32_t time_ms = btstack_run_loop_windows_get_time_ms(); 146 a->timeout = time_ms + timeout_in_ms; 147 log_debug("btstack_run_loop_windows_set_timer to %u ms (now %u, timeout %u)", a->timeout, time_ms, timeout_in_ms); 148 } 149 150 static void btstack_run_loop_windows_init(void){ 151 btstack_run_loop_base_init(); 152 153 // store start time 154 FILETIME file_time; 155 SYSTEMTIME system_time; 156 GetSystemTime(&system_time); 157 SystemTimeToFileTime(&system_time, &file_time); 158 start_time.LowPart = file_time.dwLowDateTime; 159 start_time.HighPart = file_time.dwHighDateTime; 160 161 log_debug("btstack_run_loop_windows_init"); 162 } 163 164 165 static const btstack_run_loop_t btstack_run_loop_windows = { 166 &btstack_run_loop_windows_init, 167 &btstack_run_loop_base_add_data_source, 168 &btstack_run_loop_base_remove_data_source, 169 &btstack_run_loop_base_enable_data_source_callbacks, 170 &btstack_run_loop_base_disable_data_source_callbacks, 171 &btstack_run_loop_windows_set_timer, 172 &btstack_run_loop_base_add_timer, 173 &btstack_run_loop_base_remove_timer, 174 &btstack_run_loop_windows_execute, 175 &btstack_run_loop_base_dump_timer, 176 &btstack_run_loop_windows_get_time_ms, 177 }; 178 179 /** 180 * Provide btstack_run_loop_windows instance 181 */ 182 const btstack_run_loop_t * btstack_run_loop_windows_get_instance(void){ 183 return &btstack_run_loop_windows; 184 } 185 186