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