Skip to content

Commit

Permalink
drv/bluetooth_stm32_bluenrg: fix lost data in send
Browse files Browse the repository at this point in the history
When sending large amounts of data quickly, some data was being lost.
Although we had a retry when BLE_STATUS_INSUFFICIENT_RESOURCES was
received, it seems that repeated calls immediately after that lead to
lost data (and sometimes duplicated data).

It turns out, the BlueNRG chip has an event that says when it is ready
again after a BLE_STATUS_INSUFFICIENT_RESOURCES, so we can use that
event to wait instead of doing multiple failed retries.

There are still some issues with data corruption/loss but this is a
significant improvement nonetheless.

Issue: pybricks/support#324
  • Loading branch information
dlech committed Mar 21, 2023
1 parent d8ace75 commit 014140a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
### Fixed
- Fixed allocator interfering with motor control when memory usage is high ([support#956]).
- Fixed `Stop.NONE` not working properly for some drivebase geometries ([support#972]).
- Reduced data loss when printing in tight loop on Move hub ([support#324]).

### Changed
- Methods like `control.limits()` now check the user input and raise a
Expand All @@ -28,6 +29,7 @@
### Removed
- Removed `DriveBase.left` and `DriveBase.right` properties ([support#910]).

[support#324]: https://github.com/pybricks/support/issues/324
[support#484]: https://github.com/pybricks/support/issues/484
[support#830]: https://github.com/pybricks/support/issues/830
[support#989]: https://github.com/pybricks/support/issues/881
Expand Down
14 changes: 13 additions & 1 deletion lib/pbio/drv/bluetooth/bluetooth_stm32_bluenrg.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: MIT
// Copyright (c) 2018-2022 The Pybricks Authors
// Copyright (c) 2018-2023 The Pybricks Authors

// Bluetooth for STM32 MCU with STMicro BlueNRG-MS

Expand Down Expand Up @@ -94,6 +94,8 @@ static uint16_t conn_handle;
static uint16_t remote_handle;
// handle to LWP3 characteristic on remote
static uint16_t remote_lwp3_char_handle;
// used to wait for Evt_Blue_Gatt_Tx_Pool_Available
static bool tx_pool_available;

// Pybricks GATT service handles
static uint16_t pybricks_service_handle;
Expand Down Expand Up @@ -364,6 +366,8 @@ static PT_THREAD(send_value_notification(struct pt *pt, pbio_task_t *task))
if (ret == BLE_STATUS_INSUFFICIENT_RESOURCES) {
// this will happen if notifications are enabled and the previous
// changes haven't been sent over the air yet
tx_pool_available = false;
PT_WAIT_UNTIL(pt, tx_pool_available);
goto retry;
}

Expand Down Expand Up @@ -948,6 +952,14 @@ static void handle_event(hci_event_pckt *event) {
aci_gatt_write_response_begin(subevt->conn_handle, subevt->attr_handle, !!err, err, subevt->data_length, subevt->data);
}
break;

case EVT_BLUE_GATT_TX_POOL_AVAILABLE: {
// REVISIT: We might need to look at the event args for
// connection handle if we need to handle this in multiple
// places, e.g. for notifications and write without response.
tx_pool_available = true;
}
break;
}
}
break;
Expand Down

0 comments on commit 014140a

Please sign in to comment.