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