diff --git a/platform/embedded/btstack_run_loop_embedded.c b/platform/embedded/btstack_run_loop_embedded.c index daa891206..2303f9ef9 100644 --- a/platform/embedded/btstack_run_loop_embedded.c +++ b/platform/embedded/btstack_run_loop_embedded.c @@ -55,8 +55,10 @@ */ -#include "btstack_run_loop.h" #include "btstack_run_loop_embedded.h" + +#include "btstack_run_loop.h" +#include "btstack_run_loop_base.h" #include "btstack_linked_list.h" #include "btstack_util.h" #include "hal_tick.h" @@ -79,11 +81,6 @@ #endif // the run loop -static btstack_linked_list_t data_sources; - -#ifdef TIMER_SUPPORT -static btstack_linked_list_t timers; -#endif #ifdef HAVE_EMBEDDED_TICK static volatile uint32_t system_ticks; @@ -91,20 +88,6 @@ static volatile uint32_t system_ticks; static int trigger_event_received = 0; -/** - * Add data_source to run_loop - */ -static void btstack_run_loop_embedded_add_data_source(btstack_data_source_t *ds){ - btstack_linked_list_add(&data_sources, (btstack_linked_item_t *) ds); -} - -/** - * Remove data_source from run loop - */ -static bool btstack_run_loop_embedded_remove_data_source(btstack_data_source_t *ds){ - return btstack_linked_list_remove(&data_sources, (btstack_linked_item_t *) ds); -} - // set timer static void btstack_run_loop_embedded_set_timer(btstack_timer_source_t *ts, uint32_t timeout_in_ms){ #ifdef HAVE_EMBEDDED_TICK @@ -118,76 +101,14 @@ static void btstack_run_loop_embedded_set_timer(btstack_timer_source_t *ts, uint #endif } -/** - * Add timer to run_loop (keep list sorted) - */ -static void btstack_run_loop_embedded_add_timer(btstack_timer_source_t *ts){ -#ifdef TIMER_SUPPORT - btstack_linked_item_t *it; - for (it = (btstack_linked_item_t *) &timers; it->next ; it = it->next){ - // don't add timer that's already in there - btstack_timer_source_t * next = (btstack_timer_source_t *) it->next; - if (next == ts){ - log_error( "btstack_run_loop_timer_add error: timer to add already in list!"); - return; - } - // exit if new timeout before list timeout - int32_t delta = btstack_time_delta(ts->timeout, next->timeout); - if (delta < 0) break; - } - - ts->item.next = it->next; - it->next = (btstack_linked_item_t *) ts; -#endif -} - -/** - * Remove timer from run loop - */ -static bool btstack_run_loop_embedded_remove_timer(btstack_timer_source_t *ts){ -#ifdef TIMER_SUPPORT - return btstack_linked_list_remove(&timers, (btstack_linked_item_t *) ts); -#else - return 0; -#endif -} - -static void btstack_run_loop_embedded_dump_timer(void){ -#ifdef TIMER_SUPPORT -#ifdef ENABLE_LOG_INFO - btstack_linked_item_t *it; - int i = 0; - for (it = (btstack_linked_item_t *) timers; it ; it = it->next){ - btstack_timer_source_t *ts = (btstack_timer_source_t*) it; - log_info("timer %u, timeout %u\n", i, (unsigned int) ts->timeout); - } -#endif -#endif -} - -static void btstack_run_loop_embedded_enable_data_source_callbacks(btstack_data_source_t * ds, uint16_t callback_types){ - ds->flags |= callback_types; -} - -static void btstack_run_loop_embedded_disable_data_source_callbacks(btstack_data_source_t * ds, uint16_t callback_types){ - ds->flags &= ~callback_types; -} - /** * Execute run_loop once */ void btstack_run_loop_embedded_execute_once(void) { - btstack_data_source_t *ds; - // process data sources - btstack_data_source_t *next; - for (ds = (btstack_data_source_t *) data_sources; ds != NULL ; ds = next){ - next = (btstack_data_source_t *) ds->item.next; // cache pointer to next data_source to allow data source to remove itself - if (ds->flags & DATA_SOURCE_CALLBACK_POLL){ - ds->process(ds, DATA_SOURCE_CALLBACK_POLL); - } - } - + // poll data sources + btstack_run_loop_base_poll_data_sources(); + #ifdef TIMER_SUPPORT #ifdef HAVE_EMBEDDED_TICK @@ -198,14 +119,7 @@ void btstack_run_loop_embedded_execute_once(void) { #endif // process timers - while (timers) { - btstack_timer_source_t * ts = (btstack_timer_source_t *) timers; - int32_t delta = btstack_time_delta(ts->timeout, now); - if (delta > 0) break; - - btstack_run_loop_embedded_remove_timer(ts); - ts->process(ts); - } + btstack_run_loop_base_process_timers(now); #endif // disable IRQs and check if run loop iteration has been requested. if not, go to sleep @@ -261,11 +175,7 @@ void btstack_run_loop_embedded_trigger(void){ } static void btstack_run_loop_embedded_init(void){ - data_sources = NULL; - -#ifdef TIMER_SUPPORT - timers = NULL; -#endif + btstack_run_loop_base_init(); #ifdef HAVE_EMBEDDED_TICK system_ticks = 0; @@ -280,15 +190,15 @@ static void btstack_run_loop_embedded_init(void){ static const btstack_run_loop_t btstack_run_loop_embedded = { &btstack_run_loop_embedded_init, - &btstack_run_loop_embedded_add_data_source, - &btstack_run_loop_embedded_remove_data_source, - &btstack_run_loop_embedded_enable_data_source_callbacks, - &btstack_run_loop_embedded_disable_data_source_callbacks, + &btstack_run_loop_base_add_data_source, + &btstack_run_loop_base_remove_data_source, + &btstack_run_loop_base_enable_data_source_callbacks, + &btstack_run_loop_base_disable_data_source_callbacks, &btstack_run_loop_embedded_set_timer, - &btstack_run_loop_embedded_add_timer, - &btstack_run_loop_embedded_remove_timer, + &btstack_run_loop_base_add_timer, + &btstack_run_loop_base_remove_timer, &btstack_run_loop_embedded_execute, - &btstack_run_loop_embedded_dump_timer, + &btstack_run_loop_base_dump_timer, &btstack_run_loop_embedded_get_time_ms, };