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