Skip to content

Commit f3f6ccb

Browse files
jaguilardlech
andauthored
pbio/drv/bluetooth_btstack: Simplify runloop code
Most runloop code can simply use the btstack_run_loop_base functions, with the notable exception being our mechanism for executing the runloop, and for setting timers and getting the current time. Co-authored-by: David Lechner <[email protected]>
1 parent 16e461e commit f3f6ccb

File tree

1 file changed

+11
-85
lines changed

1 file changed

+11
-85
lines changed

lib/pbio/drv/bluetooth/bluetooth_btstack.c

Lines changed: 11 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <ble/gatt-service/device_information_service_server.h>
1414
#include <ble/gatt-service/nordic_spp_service_server.h>
1515
#include <btstack.h>
16+
#include <btstack_run_loop.h>
1617

1718
#include <pbdrv/bluetooth.h>
1819
#include <pbdrv/clock.h>
@@ -990,81 +991,25 @@ pbio_error_t pbdrv_bluetooth_controller_initialize(pbio_os_state_t *state, pbio_
990991
PBIO_OS_ASYNC_END(PBIO_SUCCESS);
991992
}
992993

993-
static void bluetooth_btstack_run_loop_init(void) {
994-
// Not used. Bluetooth process is started like a regular pbdrv process.
995-
}
996-
997-
static btstack_linked_list_t data_sources;
998-
999-
static void bluetooth_btstack_run_loop_add_data_source(btstack_data_source_t *ds) {
1000-
btstack_linked_list_add(&data_sources, &ds->item);
1001-
}
1002-
1003-
static bool bluetooth_btstack_run_loop_remove_data_source(btstack_data_source_t *ds) {
1004-
return btstack_linked_list_remove(&data_sources, &ds->item);
1005-
}
1006-
1007-
static void bluetooth_btstack_run_loop_enable_data_source_callbacks(btstack_data_source_t *ds, uint16_t callback_types) {
1008-
ds->flags |= callback_types;
1009-
}
1010-
1011-
static void bluetooth_btstack_run_loop_disable_data_source_callbacks(btstack_data_source_t *ds, uint16_t callback_types) {
1012-
ds->flags &= ~callback_types;
1013-
}
1014-
1015-
static btstack_linked_list_t timers;
1016-
1017994
static void bluetooth_btstack_run_loop_set_timer(btstack_timer_source_t *ts, uint32_t timeout_in_ms) {
1018995
ts->timeout = pbdrv_clock_get_ms() + timeout_in_ms;
1019996
}
1020997

1021-
static void bluetooth_btstack_run_loop_add_timer(btstack_timer_source_t *ts) {
1022-
btstack_linked_item_t *it;
1023-
for (it = (void *)&timers; it->next; it = it->next) {
1024-
// don't add timer that's already in there
1025-
btstack_timer_source_t *next = (void *)it->next;
1026-
if (next == ts) {
1027-
// timer was already in the list!
1028-
assert(0);
1029-
return;
1030-
}
1031-
// exit if new timeout before list timeout
1032-
int32_t delta = btstack_time_delta(ts->timeout, next->timeout);
1033-
if (delta < 0) {
1034-
break;
1035-
}
1036-
}
1037-
1038-
ts->item.next = it->next;
1039-
it->next = &ts->item;
1040-
}
1041-
1042-
static bool bluetooth_btstack_run_loop_remove_timer(btstack_timer_source_t *ts) {
1043-
if (btstack_linked_list_remove(&timers, &ts->item)) {
1044-
return true;
1045-
}
1046-
return false;
1047-
}
1048-
1049998
static void bluetooth_btstack_run_loop_execute(void) {
1050999
// not used
10511000
}
10521001

1053-
static void bluetooth_btstack_run_loop_dump_timer(void) {
1054-
// not used
1055-
}
1056-
10571002
static const btstack_run_loop_t bluetooth_btstack_run_loop = {
1058-
.init = bluetooth_btstack_run_loop_init,
1059-
.add_data_source = bluetooth_btstack_run_loop_add_data_source,
1060-
.remove_data_source = bluetooth_btstack_run_loop_remove_data_source,
1061-
.enable_data_source_callbacks = bluetooth_btstack_run_loop_enable_data_source_callbacks,
1062-
.disable_data_source_callbacks = bluetooth_btstack_run_loop_disable_data_source_callbacks,
1003+
.init = btstack_run_loop_base_init,
1004+
.add_data_source = btstack_run_loop_base_add_data_source,
1005+
.remove_data_source = btstack_run_loop_base_remove_data_source,
1006+
.enable_data_source_callbacks = btstack_run_loop_base_enable_data_source_callbacks,
1007+
.disable_data_source_callbacks = btstack_run_loop_base_disable_data_source_callbacks,
10631008
.set_timer = bluetooth_btstack_run_loop_set_timer,
1064-
.add_timer = bluetooth_btstack_run_loop_add_timer,
1065-
.remove_timer = bluetooth_btstack_run_loop_remove_timer,
1009+
.add_timer = btstack_run_loop_base_add_timer,
1010+
.remove_timer = btstack_run_loop_base_remove_timer,
10661011
.execute = bluetooth_btstack_run_loop_execute,
1067-
.dump_timer = bluetooth_btstack_run_loop_dump_timer,
1012+
.dump_timer = btstack_run_loop_base_dump_timer,
10681013
.get_time_ms = pbdrv_clock_get_ms,
10691014
};
10701015

@@ -1085,15 +1030,7 @@ static pbio_error_t pbdrv_bluetooth_hci_process_thread(pbio_os_state_t *state, v
10851030

10861031
if (do_poll_handler) {
10871032
do_poll_handler = false;
1088-
1089-
btstack_data_source_t *ds, *next;
1090-
for (ds = (void *)data_sources; ds != NULL; ds = next) {
1091-
// cache pointer to next data_source to allow data source to remove itself
1092-
next = (void *)ds->item.next;
1093-
if (ds->flags & DATA_SOURCE_CALLBACK_POLL) {
1094-
ds->process(ds, DATA_SOURCE_CALLBACK_POLL);
1095-
}
1096-
}
1033+
btstack_run_loop_base_poll_data_sources();
10971034
}
10981035

10991036
static pbio_os_timer_t btstack_timer = {
@@ -1102,18 +1039,7 @@ static pbio_error_t pbdrv_bluetooth_hci_process_thread(pbio_os_state_t *state, v
11021039

11031040
if (pbio_os_timer_is_expired(&btstack_timer)) {
11041041
pbio_os_timer_extend(&btstack_timer);
1105-
1106-
// process all BTStack timers in list that have expired
1107-
while (timers) {
1108-
btstack_timer_source_t *ts = (void *)timers;
1109-
int32_t delta = btstack_time_delta(ts->timeout, pbdrv_clock_get_ms());
1110-
if (delta > 0) {
1111-
// we have reached unexpired timers
1112-
break;
1113-
}
1114-
btstack_run_loop_remove_timer(ts);
1115-
ts->process(ts);
1116-
}
1042+
btstack_run_loop_base_process_timers(pbdrv_clock_get_ms());
11171043
}
11181044

11191045
// Also propagate non-btstack events like polls or timers.

0 commit comments

Comments
 (0)