From 27ff675be7aa3e1e0ae9e68c256fc30005e5c641 Mon Sep 17 00:00:00 2001 From: Matthias Ringwald Date: Sun, 27 Nov 2016 21:54:01 +0100 Subject: [PATCH] windows: support timers --- port/windows-h4/btstack_run_loop_windows.c | 43 ++++++++++++++++++++++ port/windows-h4/main.c | 29 +++++++++++++-- 2 files changed, 68 insertions(+), 4 deletions(-) diff --git a/port/windows-h4/btstack_run_loop_windows.c b/port/windows-h4/btstack_run_loop_windows.c index 23fa11542..579cbfdd5 100644 --- a/port/windows-h4/btstack_run_loop_windows.c +++ b/port/windows-h4/btstack_run_loop_windows.c @@ -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; diff --git a/port/windows-h4/main.c b/port/windows-h4/main.c index ba061509d..96283ef15 100644 --- a/port/windows-h4/main.c +++ b/port/windows-h4/main.c @@ -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; }