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