windows: support timers

This commit is contained in:
Matthias Ringwald 2016-11-27 21:54:01 +01:00
parent 0ad2d5db00
commit 27ff675be7
2 changed files with 68 additions and 4 deletions

View File

@ -144,6 +144,49 @@ static uint32_t btstack_run_loop_windows_get_time_ms(void){
* Execute run_loop
*/
static void btstack_run_loop_windows_execute(void) {
while (1) {
// collect handles to wait for
HANDLE handles[20];
memset(handles, 0, sizeof(handles));
int num_handles = 0;
// get next timeout
btstack_timer_source_t *ts;
uint32_t timeout_ms = INFINITE;
if (timers) {
ts = (btstack_timer_source_t *) timers;
uint32_t now_ms = btstack_run_loop_windows_get_time_ms();
timeout_ms = ts->timeout - now_ms;
if (timeout_ms < 0){
timeout_ms = 0;
}
log_debug("btstack_run_loop_execute next timeout in %u ms", timeout_ms);
}
if (num_handles){
// wait for ready Events or timeout
WaitForMultipleObjects(num_handles, &handles[0], 0, timeout_ms);
} else {
// just wait for timeout
Sleep(timeout_ms);
}
// process timers
uint32_t now_ms = btstack_run_loop_windows_get_time_ms();
while (timers) {
ts = (btstack_timer_source_t *) timers;
if (ts->timeout > now_ms) break;
log_debug("btstack_run_loop_windows_execute: process timer %p\n", ts);
// remove timer before processing it to allow handler to re-register with run loop
btstack_run_loop_remove_timer(ts);
ts->process(ts);
}
}
#if 0
fd_set descriptors_read;
fd_set descriptors_write;

View File

@ -17,21 +17,42 @@
// #include "btstack_link_key_db_fs.h"
// #include "stdin_support.h"
int btstack_main(int argc, const char * argv[]);
static int led_state = 0;
void hal_led_toggle(void){
led_state = 1 - led_state;
printf("LED State %u\n", led_state);
}
int main(int argc, char * argv[]){
static void sigint_handler(int param){
#ifndef _WIN32
// reset anyway
btstack_stdin_reset();
#endif
log_info(" <= SIGINT received, shutting down..\n");
// hci_power_control(HCI_POWER_OFF);
// hci_close();
log_info("Good bye, see you.\n");
exit(0);
}
int main(int argc, const char * argv[]){
printf("BTstack on windows booting up\n");
/// GET STARTED with BTstack ///
btstack_memory_init();
btstack_run_loop_init(btstack_run_loop_windows_get_instance());
while(1){
printf("time ms %u\n", btstack_run_loop_get_time_ms());
}
// handle CTRL-c
signal(SIGINT, sigint_handler);
// setup app
btstack_main(argc, argv);
// go
btstack_run_loop_execute();
return 0;
}