diff --git a/platform/freertos/btstack_run_loop_freertos.c b/platform/freertos/btstack_run_loop_freertos.c index ecbb87c4a..2e4d2435e 100644 --- a/platform/freertos/btstack_run_loop_freertos.c +++ b/platform/freertos/btstack_run_loop_freertos.c @@ -49,6 +49,7 @@ #include "btstack_run_loop_freertos.h" +#include "btstack_run_loop_base.h" #include "btstack_linked_list.h" #include "btstack_debug.h" #include "btstack_util.h" @@ -103,8 +104,6 @@ static EventGroupHandle_t btstack_run_loop_event_group; #define EVENT_GROUP_FLAG_RUN_LOOP 1 // the run loop -static btstack_linked_list_t timers; -static btstack_linked_list_t data_sources; static bool run_loop_exit_requested; static uint32_t btstack_run_loop_freertos_get_time_ms(void){ @@ -116,44 +115,6 @@ static void btstack_run_loop_freertos_set_timer(btstack_timer_source_t *ts, uint ts->timeout = btstack_run_loop_freertos_get_time_ms() + timeout_in_ms + 1; } -/** - * Add timer to run_loop (keep list sorted) - */ -static void btstack_run_loop_freertos_add_timer(btstack_timer_source_t *ts){ - 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; -} - -/** - * Remove timer from run loop - */ -static bool btstack_run_loop_freertos_remove_timer(btstack_timer_source_t *ts){ - return btstack_linked_list_remove(&timers, (btstack_linked_item_t *) ts); -} - -static void btstack_run_loop_freertos_dump_timer(void){ -#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 -} - // schedules execution from regular thread void btstack_run_loop_freertos_trigger(void){ #ifdef HAVE_FREERTOS_TASK_NOTIFICATIONS @@ -223,14 +184,7 @@ static void btstack_run_loop_freertos_execute(void) { while (true) { // process data sources - btstack_data_source_t *ds; - 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); - } - } + btstack_run_loop_base_poll_data_sources(); // process registered function calls on run loop thread while (true){ @@ -242,28 +196,21 @@ static void btstack_run_loop_freertos_execute(void) { } } - // process timers and get next timeout - uint32_t timeout_ms = portMAX_DELAY; - log_debug("RL: portMAX_DELAY %u", portMAX_DELAY); - while (timers) { - btstack_timer_source_t * ts = (btstack_timer_source_t *) timers; - uint32_t now = btstack_run_loop_freertos_get_time_ms(); - int32_t delta_ms = btstack_time_delta(ts->timeout, now); - log_debug("RL: now %u, expires %u -> delta %d", now, ts->timeout, delta_ms); - if (delta_ms > 0){ - timeout_ms = delta_ms; - break; - } - // remove timer before processing it to allow handler to re-register with run loop - btstack_run_loop_freertos_remove_timer(ts); - log_debug("RL: first timer %p", ts->process); - ts->process(ts); - } + // process timers + uint32_t now = btstack_run_loop_freertos_get_time_ms(); + btstack_run_loop_base_process_timers(now); // exit triggered by btstack_run_loop_freertos_trigger_exit (from data source, timer, run on main thread) if (run_loop_exit_requested) break; // wait for timeout or event group/task notification + int32_t timeout_next_timer_ms = btstack_run_loop_base_get_time_until_timeout(now); + + uint32_t timeout_ms = portMAX_DELAY; + if (timeout_next_timer_ms >= 0){ + timeout_ms = (uint32_t) timeout_next_timer_ms; + } + log_debug("RL: wait with timeout %u", (int) timeout_ms); #ifdef HAVE_FREERTOS_TASK_NOTIFICATIONS xTaskNotifyWait(pdFALSE, 0xffffffff, NULL, pdMS_TO_TICKS(timeout_ms)); @@ -273,24 +220,8 @@ static void btstack_run_loop_freertos_execute(void) { } } -static void btstack_run_loop_freertos_add_data_source(btstack_data_source_t *ds){ - btstack_linked_list_add(&data_sources, (btstack_linked_item_t *) ds); -} - -static bool btstack_run_loop_freertos_remove_data_source(btstack_data_source_t *ds){ - return btstack_linked_list_remove(&data_sources, (btstack_linked_item_t *) ds); -} - -static void btstack_run_loop_freertos_enable_data_source_callbacks(btstack_data_source_t * ds, uint16_t callback_types){ - ds->flags |= callback_types; -} - -static void btstack_run_loop_freertos_disable_data_source_callbacks(btstack_data_source_t * ds, uint16_t callback_types){ - ds->flags &= ~callback_types; -} - static void btstack_run_loop_freertos_init(void){ - timers = NULL; + btstack_run_loop_base_init(); #ifdef USE_STATIC_ALLOC btstack_run_loop_queue = xQueueCreateStatic(RUN_LOOP_QUEUE_LENGTH, RUN_LOOP_QUEUE_ITEM_SIZE, btstack_run_loop_queue_storage, &btstack_run_loop_queue_object); @@ -315,15 +246,15 @@ static void btstack_run_loop_freertos_init(void){ static const btstack_run_loop_t btstack_run_loop_freertos = { &btstack_run_loop_freertos_init, - &btstack_run_loop_freertos_add_data_source, - &btstack_run_loop_freertos_remove_data_source, - &btstack_run_loop_freertos_enable_data_source_callbacks, - &btstack_run_loop_freertos_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_freertos_set_timer, - &btstack_run_loop_freertos_add_timer, - &btstack_run_loop_freertos_remove_timer, + &btstack_run_loop_base_add_timer, + &btstack_run_loop_base_remove_timer, &btstack_run_loop_freertos_execute, - &btstack_run_loop_freertos_dump_timer, + &btstack_run_loop_base_dump_timer, &btstack_run_loop_freertos_get_time_ms, };