xref: /btstack/platform/windows/btstack_run_loop_windows.c (revision f9f2075ceac5e9dc08e9abea437e43d733a3a0ea)
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 /*
39  *  btstack_run_loop_windows.c
40  */
41 
42 #include "btstack_run_loop.h"
43 #include "btstack_run_loop_windows.h"
44 #include "btstack_linked_list.h"
45 #include "btstack_debug.h"
46 #include <Windows.h>
47 
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <sys/time.h>
51 
52 static void btstack_run_loop_windows_dump_timer(void);
53 
54 // the run loop
55 static btstack_linked_list_t data_sources;
56 static int data_sources_modified;
57 static btstack_linked_list_t timers;
58 // start time.
59 static ULARGE_INTEGER start_time;
60 
61 /**
62  * Add data_source to run_loop
63  */
64 static void btstack_run_loop_windows_add_data_source(btstack_data_source_t *ds){
65     data_sources_modified = 1;
66     // log_info("btstack_run_loop_windows_add_data_source %x with fd %u\n", (int) ds, ds->fd);
67     btstack_linked_list_add(&data_sources, (btstack_linked_item_t *) ds);
68 }
69 
70 /**
71  * Remove data_source from run loop
72  */
73 static int btstack_run_loop_windows_remove_data_source(btstack_data_source_t *ds){
74     data_sources_modified = 1;
75     // log_info("btstack_run_loop_windows_remove_data_source %x\n", (int) ds);
76     return btstack_linked_list_remove(&data_sources, (btstack_linked_item_t *) ds);
77 }
78 
79 /**
80  * Add timer to run_loop (keep list sorted)
81  */
82 static void btstack_run_loop_windows_add_timer(btstack_timer_source_t *ts){
83     btstack_linked_item_t *it;
84     for (it = (btstack_linked_item_t *) &timers; it->next ; it = it->next){
85         btstack_timer_source_t * next = (btstack_timer_source_t *) it->next;
86         if (next == ts){
87             log_error( "btstack_run_loop_timer_add error: timer to add already in list!");
88             return;
89         }
90         if (next->timeout > ts->timeout) {
91             break;
92         }
93     }
94     ts->item.next = it->next;
95     it->next = (btstack_linked_item_t *) ts;
96     log_debug("Added timer %p at %u\n", ts, ts->timeout);
97     // btstack_run_loop_windows_dump_timer();
98 }
99 
100 /**
101  * Remove timer from run loop
102  */
103 static int btstack_run_loop_windows_remove_timer(btstack_timer_source_t *ts){
104     // log_info("Removed timer %x at %u\n", (int) ts, (unsigned int) ts->timeout.tv_sec);
105     // btstack_run_loop_windows_dump_timer();
106     return btstack_linked_list_remove(&timers, (btstack_linked_item_t *) ts);
107 }
108 
109 static void btstack_run_loop_windows_dump_timer(void){
110     btstack_linked_item_t *it;
111     int i = 0;
112     for (it = (btstack_linked_item_t *) timers; it ; it = it->next){
113         btstack_timer_source_t *ts = (btstack_timer_source_t*) it;
114         log_info("timer %u, timeout %u\n", i, ts->timeout);
115     }
116 }
117 
118 static void btstack_run_loop_windows_enable_data_source_callbacks(btstack_data_source_t * ds, uint16_t callback_types){
119     ds->flags |= callback_types;
120 }
121 
122 static void btstack_run_loop_windows_disable_data_source_callbacks(btstack_data_source_t * ds, uint16_t callback_types){
123     ds->flags &= ~callback_types;
124 }
125 
126 /**
127  * @brief Queries the current time in ms since start
128  */
129 static uint32_t btstack_run_loop_windows_get_time_ms(void){
130 
131     FILETIME    file_time;
132     SYSTEMTIME  system_time;
133     ULARGE_INTEGER now_time;
134     GetSystemTime(&system_time);
135     SystemTimeToFileTime(&system_time, &file_time);
136     now_time.LowPart =  file_time.dwLowDateTime;
137     now_time.HighPart = file_time.dwHighDateTime;
138     uint32_t time_ms = (now_time.QuadPart - start_time.QuadPart) / 10000;
139     log_debug("btstack_run_loop_windows_get_time_ms: %u", time_ms);
140     return time_ms;
141 }
142 
143 /**
144  * Execute run_loop
145  */
146 static void btstack_run_loop_windows_execute(void) {
147 
148     btstack_timer_source_t *ts;
149     btstack_linked_list_iterator_t it;
150 
151     while (1) {
152 
153         // collect handles to wait for
154         HANDLE handles[100];
155         memset(handles, 0, sizeof(handles));
156         int num_handles = 0;
157         btstack_linked_list_iterator_init(&it, &data_sources);
158         while (btstack_linked_list_iterator_has_next(&it)){
159             btstack_data_source_t *ds = (btstack_data_source_t*) btstack_linked_list_iterator_next(&it);
160             if (ds->handle == 0) continue;
161             if (ds->flags & (DATA_SOURCE_CALLBACK_READ | DATA_SOURCE_CALLBACK_WRITE)){
162                 handles[num_handles++] = ds->handle;
163                 log_debug("btstack_run_loop_execute adding handle %p", ds->handle);
164             }
165         }
166 
167         // get next timeout
168         uint32_t timeout_ms = INFINITE;
169         if (timers) {
170             ts = (btstack_timer_source_t *) timers;
171             uint32_t now_ms = btstack_run_loop_windows_get_time_ms();
172             timeout_ms = ts->timeout - now_ms;
173             if (timeout_ms < 0){
174                 timeout_ms = 0;
175             }
176             log_debug("btstack_run_loop_execute next timeout in %u ms", timeout_ms);
177         }
178 
179         int res;
180         if (num_handles){
181             // wait for ready Events or timeout
182             res = WaitForMultipleObjects(num_handles, &handles[0], 0, timeout_ms);
183         } else {
184             // just wait for timeout
185             Sleep(timeout_ms);
186             res = WAIT_TIMEOUT;
187         }
188 
189         // process data source
190         if (WAIT_OBJECT_0 <= res && res < (WAIT_OBJECT_0 + num_handles)){
191             void * triggered_handle = handles[res - WAIT_OBJECT_0];
192             btstack_linked_list_iterator_init(&it, &data_sources);
193             while (btstack_linked_list_iterator_has_next(&it)){
194                 btstack_data_source_t *ds = (btstack_data_source_t*) btstack_linked_list_iterator_next(&it);
195                 log_debug("btstack_run_loop_windows_execute: check ds %p with handle %p\n", ds, ds->handle);
196                 if (triggered_handle == ds->handle){
197                     if (ds->flags & DATA_SOURCE_CALLBACK_READ){
198                         log_debug("btstack_run_loop_windows_execute: process read ds %p with handle %p\n", ds, ds->handle);
199                         ds->process(ds, DATA_SOURCE_CALLBACK_READ);
200                     } else if (ds->flags & DATA_SOURCE_CALLBACK_WRITE){
201                         log_debug("btstack_run_loop_windows_execute: process write ds %p with handle %p\n", ds, ds->handle);
202                         ds->process(ds, DATA_SOURCE_CALLBACK_WRITE);
203                     }
204                     break;
205                 }
206             }
207         }
208 
209         // process timers
210         uint32_t now_ms = btstack_run_loop_windows_get_time_ms();
211         while (timers) {
212             ts = (btstack_timer_source_t *) timers;
213             if (ts->timeout > now_ms) break;
214             log_debug("btstack_run_loop_windows_execute: process timer %p\n", ts);
215 
216             // remove timer before processing it to allow handler to re-register with run loop
217             btstack_run_loop_remove_timer(ts);
218             ts->process(ts);
219         }
220     }
221 }
222 
223 // set timer
224 static void btstack_run_loop_windows_set_timer(btstack_timer_source_t *a, uint32_t timeout_in_ms){
225     uint32_t time_ms = btstack_run_loop_windows_get_time_ms();
226     a->timeout = time_ms + timeout_in_ms;
227     log_debug("btstack_run_loop_windows_set_timer to %u ms (now %u, timeout %u)", a->timeout, time_ms, timeout_in_ms);
228 }
229 
230 static void btstack_run_loop_windows_init(void){
231     data_sources = NULL;
232     timers = NULL;
233 
234     // store start time
235     FILETIME    file_time;
236     SYSTEMTIME  system_time;
237     GetSystemTime(&system_time);
238     SystemTimeToFileTime(&system_time, &file_time);
239     start_time.LowPart =  file_time.dwLowDateTime;
240     start_time.HighPart = file_time.dwHighDateTime;
241 
242     log_debug("btstack_run_loop_windows_init");
243 }
244 
245 
246 static const btstack_run_loop_t btstack_run_loop_windows = {
247     &btstack_run_loop_windows_init,
248     &btstack_run_loop_windows_add_data_source,
249     &btstack_run_loop_windows_remove_data_source,
250     &btstack_run_loop_windows_enable_data_source_callbacks,
251     &btstack_run_loop_windows_disable_data_source_callbacks,
252     &btstack_run_loop_windows_set_timer,
253     &btstack_run_loop_windows_add_timer,
254     &btstack_run_loop_windows_remove_timer,
255     &btstack_run_loop_windows_execute,
256     &btstack_run_loop_windows_dump_timer,
257     &btstack_run_loop_windows_get_time_ms,
258 };
259 
260 /**
261  * Provide btstack_run_loop_windows instance
262  */
263 const btstack_run_loop_t * btstack_run_loop_windows_get_instance(void){
264     return &btstack_run_loop_windows;
265 }
266 
267