Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/connections/ble_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,13 @@ impl BleHandler {
pub async fn write_to_radio(&self, buffer: &[u8]) -> Result<(), Error> {
self.radio
// TODO: remove the skipping of the first 4 bytes
.write(&self.toradio_char, &buffer[4..], WriteType::WithResponse)
.write(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added this:

  • to check how ConnectedStreamAPI would handle it, which it does fine and avoids the panic also (tested removing the len != 0 check first)
  • for consideration as additional protection from panics due to any caller providing an invalid length buffer. Up to you to decide to include or something similar. I imagine there will be a very small runtime overhead with the check, but maybe negligible for the number of packets being sent?

&self.toradio_char,
buffer.get(4..).ok_or(Error::InvalidaDataSize {
data_length: buffer.len(),
})?,
WriteType::WithResponse,
)
.await
.map_err(|e: btleplug::Error| {
Error::InternalStreamError(InternalStreamError::StreamWriteError {
Expand Down
4 changes: 3 additions & 1 deletion src/utils_internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,9 @@ pub async fn build_ble_stream(
// Data from user, forward it to the device
from_server = server.read(&mut buf) => {
let len = from_server.map_err(duplex_write_error_fn)?;
Copy link
Contributor Author

@andrewdavidmackenzie andrewdavidmackenzie Nov 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated, but I notice on this read error you map_err using a write error fn, and the same at line 308

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, the name is wrong, I can change it later.

ble_handler.write_to_radio(&buf[..len]).await?;
if len != 0 {
ble_handler.write_to_radio(&buf[..len]).await?;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can also return Err(InternalStreamError::ConnectionLost) in case len == 0.

I can look into this later, you can merge this PR as it is an improvement.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You will have to merge it I think.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the result of an explicit disconnect call, strictly speaking not an error - but will let you decide.

}
},
event = adapter_events.next() => {
if Some(AdapterEvent::Disconnected) == event {
Expand Down
Loading