Skip to content

Commit

Permalink
drv/bluetooth_stm32_cc2640: Work around scanning stopping.
Browse files Browse the repository at this point in the history
  • Loading branch information
laurensvalk committed Nov 20, 2023
1 parent cf36ff1 commit 4f52a58
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
14 changes: 14 additions & 0 deletions lib/pbio/drv/bluetooth/bluetooth_stm32_cc2640.c
Original file line number Diff line number Diff line change
Expand Up @@ -954,6 +954,20 @@ void pbdrv_bluetooth_stop_broadcasting(void) {
static PT_THREAD(observe_task(struct pt *pt, pbio_task_t *task)) {
PT_BEGIN(pt);

// HACK: Always stop observing first so that this task always does a clean
// restart of observing. This clears the size limited buffer of discovered
// devices. Should be replaced by a generalized scan process that only
// restarts scanning when needed. https://github.com/pybricks/support/issues/1096
if (is_observing) {
PT_WAIT_WHILE(pt, write_xfer_size);
GAP_DeviceDiscoveryCancel();
PT_WAIT_UNTIL(pt, hci_command_status);
if (read_buf[8] == bleSUCCESS) {
PT_WAIT_UNTIL(pt, device_discovery_done);
}
is_observing = false;
}

if (!is_observing) {
PT_WAIT_WHILE(pt, write_xfer_size);
GAP_DeviceDiscoveryRequest(GAP_DEVICE_DISCOVERY_MODE_NONDISCOVERABLE, 0, GAP_FILTER_POLICY_SCAN_ANY_CONNECT_ANY);
Expand Down
16 changes: 16 additions & 0 deletions pybricks/common/pb_type_ble.c
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,22 @@ STATIC mp_obj_t pb_module_ble_observe(mp_obj_t self_in, mp_obj_t channel_in) {

// Have not received data yet or timed out.
if (ch_data.rssi == INT8_MIN) {

// HACK: Work around observing eventually stopping on the CC2640 due to
// full buffer of discovered devices. Needs to be fixed at the driver
// level with a scan process that restarts automatically.
// See https://github.com/pybricks/support/issues/1096
#if PBDRV_CONFIG_BLUETOOTH_STM32_CC2640
static uint32_t time_restart = 0;
uint32_t time_now = mp_hal_ticks_ms();
if (time_now - time_restart > OBSERVED_DATA_TIMEOUT_MS) {
pbio_task_t task;
pbdrv_bluetooth_start_observing(&task, handle_observe_event);
pb_module_tools_pbio_task_do_blocking(&task, -1);
time_restart = time_now;
}
#endif

return mp_const_none;
}

Expand Down

0 comments on commit 4f52a58

Please sign in to comment.