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