diff --git a/include/btstack/run_loop.h b/include/btstack/run_loop.h index 077e8db03..d7df0367e 100644 --- a/include/btstack/run_loop.h +++ b/include/btstack/run_loop.h @@ -113,7 +113,7 @@ uint32_t embedded_get_ticks(void); // just before entering sleep mode. Has to be called by the interupt // handler of a data source to signal the run loop that a new data // is available. -void embedded_trigger(void); +void embedded_trigger(void); #endif #if defined __cplusplus } diff --git a/src/run_loop.c b/src/run_loop.c index 513ea496b..8ade4085b 100644 --- a/src/run_loop.c +++ b/src/run_loop.c @@ -98,6 +98,11 @@ int run_loop_remove_data_source(data_source_t *ds){ return the_run_loop->remove_data_source(ds); } +void run_loop_set_timer(timer_source_t *a, uint32_t timeout_in_ms){ + run_loop_assert(); + the_run_loop->set_timer(a, timeout_in_ms); +} + /** * Add timer to run_loop (keep list sorted) */ diff --git a/src/run_loop_cocoa.m b/src/run_loop_cocoa.m index 93009522a..f87181b82 100644 --- a/src/run_loop_cocoa.m +++ b/src/run_loop_cocoa.m @@ -138,6 +138,17 @@ int cocoa_remove_timer(timer_source_t * ts){ return 0; } +// set timer +static void cocoa_set_timer(timer_source_t *a, uint32_t timeout_in_ms){ + gettimeofday(&a->timeout, NULL); + a->timeout.tv_sec += timeout_in_ms / 1000; + a->timeout.tv_usec += (timeout_in_ms % 1000) * 1000; + if (a->timeout.tv_usec > 1000000) { + a->timeout.tv_usec -= 1000000; + a->timeout.tv_sec++; + } +} + void cocoa_init(void){ } @@ -155,9 +166,10 @@ run_loop_t run_loop_cocoa = { &cocoa_init, &cocoa_add_data_source, &cocoa_remove_data_source, + &cocoa_set_timer, &cocoa_add_timer, &cocoa_remove_timer, &cocoa_execute, - &cocoa_dump_timer + &cocoa_dump_timer, }; diff --git a/src/run_loop_embedded.c b/src/run_loop_embedded.c index 456985f14..93d309bf4 100644 --- a/src/run_loop_embedded.c +++ b/src/run_loop_embedded.c @@ -73,31 +73,31 @@ static uint32_t system_ticks; static int trigger_event_received = 0; -/** - * trigger run loop iteration - */ -void embedded_trigger(void){ - trigger_event_received = 1; -} - /** * Add data_source to run_loop */ -void embedded_add_data_source(data_source_t *ds){ +static void embedded_add_data_source(data_source_t *ds){ linked_list_add(&data_sources, (linked_item_t *) ds); } /** * Remove data_source from run loop */ -int embedded_remove_data_source(data_source_t *ds){ +static int embedded_remove_data_source(data_source_t *ds){ return linked_list_remove(&data_sources, (linked_item_t *) ds); } +// set timer +static void embedded_set_timer(timer_source_t *ts, uint32_t timeout_in_ms){ + uint32_t ticks = embedded_ticks_for_ms(timeout_in_ms); + if (ticks == 0) ticks++; + ts->timeout = system_ticks + ticks; +} + /** * Add timer to run_loop (keep list sorted) */ -void embedded_add_timer(timer_source_t *ts){ +static void embedded_add_timer(timer_source_t *ts){ #ifdef HAVE_TICK linked_item_t *it; for (it = (linked_item_t *) &timers; it->next ; it = it->next){ @@ -115,7 +115,7 @@ void embedded_add_timer(timer_source_t *ts){ /** * Remove timer from run loop */ -int embedded_remove_timer(timer_source_t *ts){ +static int embedded_remove_timer(timer_source_t *ts){ #ifdef HAVE_TICK // log_info("Removed timer %x at %u\n", (int) ts, (unsigned int) ts->timeout.tv_sec); return linked_list_remove(&timers, (linked_item_t *) ts); @@ -124,7 +124,7 @@ int embedded_remove_timer(timer_source_t *ts){ #endif } -void embedded_dump_timer(void){ +static void embedded_dump_timer(void){ #ifdef HAVE_TICK #ifdef ENABLE_LOG_INFO linked_item_t *it; @@ -140,7 +140,7 @@ void embedded_dump_timer(void){ /** * Execute run_loop once */ -void embedded_execute_once(void) { +static void embedded_execute_once(void) { data_source_t *ds; // process data sources @@ -173,7 +173,7 @@ void embedded_execute_once(void) { /** * Execute run_loop */ -void embedded_execute(void) { +static void embedded_execute(void) { while (1) { embedded_execute_once(); } @@ -192,16 +192,16 @@ uint32_t embedded_get_ticks(void){ uint32_t embedded_ticks_for_ms(uint32_t time_in_ms){ return time_in_ms / hal_tick_get_tick_period_in_ms(); } - -// set timer -void run_loop_set_timer(timer_source_t *ts, uint32_t timeout_in_ms){ - uint32_t ticks = embedded_ticks_for_ms(timeout_in_ms); - if (ticks == 0) ticks++; - ts->timeout = system_ticks + ticks; -} #endif -void embedded_init(void){ +/** + * trigger run loop iteration + */ +void embedded_trigger(void){ + trigger_event_received = 1; +} + +static void embedded_init(void){ data_sources = NULL; @@ -217,8 +217,9 @@ const run_loop_t run_loop_embedded = { &embedded_init, &embedded_add_data_source, &embedded_remove_data_source, + &embedded_set_timer, &embedded_add_timer, &embedded_remove_timer, &embedded_execute, - &embedded_dump_timer + &embedded_dump_timer, }; diff --git a/src/run_loop_posix.c b/src/run_loop_posix.c index 6cbb5045d..4aa0a707b 100644 --- a/src/run_loop_posix.c +++ b/src/run_loop_posix.c @@ -50,7 +50,9 @@ #include #include -void posix_dump_timer(void); +static void posix_dump_timer(void); +static int posix_timeval_compare(struct timeval *a, struct timeval *b); +static int posix_timer_compare(timer_source_t *a, timer_source_t *b); // the run loop static linked_list_t data_sources; @@ -60,7 +62,7 @@ static linked_list_t timers; /** * Add data_source to run_loop */ -void posix_add_data_source(data_source_t *ds){ +static void posix_add_data_source(data_source_t *ds){ data_sources_modified = 1; // log_info("posix_add_data_source %x with fd %u\n", (int) ds, ds->fd); linked_list_add(&data_sources, (linked_item_t *) ds); @@ -69,7 +71,7 @@ void posix_add_data_source(data_source_t *ds){ /** * Remove data_source from run loop */ -int posix_remove_data_source(data_source_t *ds){ +static int posix_remove_data_source(data_source_t *ds){ data_sources_modified = 1; // log_info("posix_remove_data_source %x\n", (int) ds); return linked_list_remove(&data_sources, (linked_item_t *) ds); @@ -78,14 +80,14 @@ int posix_remove_data_source(data_source_t *ds){ /** * Add timer to run_loop (keep list sorted) */ -void posix_add_timer(timer_source_t *ts){ +static void posix_add_timer(timer_source_t *ts){ linked_item_t *it; for (it = (linked_item_t *) &timers; it->next ; it = it->next){ if ((timer_source_t *) it->next == ts){ log_error( "run_loop_timer_add error: timer to add already in list!\n"); return; } - if (run_loop_timer_compare( (timer_source_t *) it->next, ts) > 0) { + if (posix_timer_compare( (timer_source_t *) it->next, ts) > 0) { break; } } @@ -98,13 +100,13 @@ void posix_add_timer(timer_source_t *ts){ /** * Remove timer from run loop */ -int posix_remove_timer(timer_source_t *ts){ +static int posix_remove_timer(timer_source_t *ts){ // log_info("Removed timer %x at %u\n", (int) ts, (unsigned int) ts->timeout.tv_sec); // posix_dump_timer(); return linked_list_remove(&timers, (linked_item_t *) ts); } -void posix_dump_timer(void){ +static void posix_dump_timer(void){ linked_item_t *it; int i = 0; for (it = (linked_item_t *) timers; it ; it = it->next){ @@ -116,7 +118,7 @@ void posix_dump_timer(void){ /** * Execute run_loop */ -void posix_execute(void) { +static void posix_execute(void) { fd_set descriptors; data_source_t *ds; timer_source_t *ts; @@ -190,7 +192,7 @@ void posix_execute(void) { } // set timer -void run_loop_set_timer(timer_source_t *a, uint32_t timeout_in_ms){ +static void posix_set_timer(timer_source_t *a, uint32_t timeout_in_ms){ gettimeofday(&a->timeout, NULL); a->timeout.tv_sec += timeout_in_ms / 1000; a->timeout.tv_usec += (timeout_in_ms % 1000) * 1000; @@ -202,7 +204,7 @@ void run_loop_set_timer(timer_source_t *a, uint32_t timeout_in_ms){ // compare timers - NULL is assumed to be before the Big Bang // pre: 0 <= tv_usec < 1000000 -int run_loop_timeval_compare(struct timeval *a, struct timeval *b){ +static int posix_timeval_compare(struct timeval *a, struct timeval *b){ if (!a && !b) return 0; if (!a) return -1; if (!b) return 1; @@ -227,14 +229,14 @@ int run_loop_timeval_compare(struct timeval *a, struct timeval *b){ // compare timers - NULL is assumed to be before the Big Bang // pre: 0 <= tv_usec < 1000000 -int run_loop_timer_compare(timer_source_t *a, timer_source_t *b){ +static int posix_timer_compare(timer_source_t *a, timer_source_t *b){ if (!a && !b) return 0; if (!a) return -1; if (!b) return 1; - return run_loop_timeval_compare(&a->timeout, &b->timeout); + return posix_timeval_compare(&a->timeout, &b->timeout); } -void posix_init(void){ +static void posix_init(void){ data_sources = NULL; timers = NULL; } @@ -243,8 +245,9 @@ run_loop_t run_loop_posix = { &posix_init, &posix_add_data_source, &posix_remove_data_source, + &posix_set_timer, &posix_add_timer, &posix_remove_timer, &posix_execute, - &posix_dump_timer + &posix_dump_timer, }; diff --git a/src/run_loop_private.h b/src/run_loop_private.h index 61d382344..8a49b503c 100644 --- a/src/run_loop_private.h +++ b/src/run_loop_private.h @@ -52,12 +52,6 @@ extern "C" { #endif -#ifdef HAVE_TIME -// compare timeval or timers - NULL is assumed to be before the Big Bang -int run_loop_timeval_compare(struct timeval *a, struct timeval *b); -int run_loop_timer_compare(timer_source_t *a, timer_source_t *b); -#endif - // void run_loop_timer_dump(void); @@ -66,6 +60,7 @@ typedef struct { void (*init)(void); void (*add_data_source)(data_source_t *dataSource); int (*remove_data_source)(data_source_t *dataSource); + void (*set_timer)(timer_source_t * timer, uint32_t timeout_in_ms); void (*add_timer)(timer_source_t *timer); int (*remove_timer)(timer_source_t *timer); void (*execute)(void);