From d0ef2e953780ebac7274ba4e4d8cdd7c116bd16c Mon Sep 17 00:00:00 2001 From: Andrew Mackenzie Date: Thu, 6 Nov 2025 14:26:30 +0100 Subject: [PATCH 1/5] Handle EOF when DuplexStream is closed, avoiding a panic. Fixes #75 --- src/utils_internal.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/utils_internal.rs b/src/utils_internal.rs index 17dd032..067a1b9 100644 --- a/src/utils_internal.rs +++ b/src/utils_internal.rs @@ -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)?; - ble_handler.write_to_radio(&buf[..len]).await?; + if len != 0 { + ble_handler.write_to_radio(&buf[..len]).await?; + } }, event = adapter_events.next() => { if Some(AdapterEvent::Disconnected) == event { From 45c7ce0c8e7f8f519fdf6930e25373d5514ae5bf Mon Sep 17 00:00:00 2001 From: Andrew Mackenzie Date: Fri, 7 Nov 2025 13:03:38 +0100 Subject: [PATCH 2/5] Additional protection from panics on invalid input buffer lengths. --- src/connections/ble_handler.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/connections/ble_handler.rs b/src/connections/ble_handler.rs index 1d5d195..c3ea085 100644 --- a/src/connections/ble_handler.rs +++ b/src/connections/ble_handler.rs @@ -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( + &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 { From a21b5850aaaa5addb55a62d6f71c9d0502d4b228 Mon Sep 17 00:00:00 2001 From: Andrew Mackenzie Date: Sun, 23 Nov 2025 11:26:55 +0100 Subject: [PATCH 3/5] Derive Hash for BleDevice to allow it to be added to HashSets and HashMaps --- src/connections/ble_handler.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/connections/ble_handler.rs b/src/connections/ble_handler.rs index d0412f6..8229ca5 100644 --- a/src/connections/ble_handler.rs +++ b/src/connections/ble_handler.rs @@ -102,7 +102,7 @@ impl Display for BleId { } /// A Meshtastic device discovered via Bluetooth LE. -#[derive(Clone, PartialEq, Eq, Debug, Serialize, Deserialize)] +#[derive(Clone, PartialEq, Eq, Hash, Debug, Serialize, Deserialize)] pub struct BleDevice { /// The broadcast name of the device. pub name: Option, From 0b9076f5cc3e38b8ab07d730994449577175e262 Mon Sep 17 00:00:00 2001 From: Andrew Mackenzie Date: Sun, 23 Nov 2025 11:27:48 +0100 Subject: [PATCH 4/5] Revert "Derive Hash for BleDevice to allow it to be added to HashSets and HashMaps" This reverts commit a21b5850aaaa5addb55a62d6f71c9d0502d4b228. --- src/connections/ble_handler.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/connections/ble_handler.rs b/src/connections/ble_handler.rs index 8229ca5..d0412f6 100644 --- a/src/connections/ble_handler.rs +++ b/src/connections/ble_handler.rs @@ -102,7 +102,7 @@ impl Display for BleId { } /// A Meshtastic device discovered via Bluetooth LE. -#[derive(Clone, PartialEq, Eq, Hash, Debug, Serialize, Deserialize)] +#[derive(Clone, PartialEq, Eq, Debug, Serialize, Deserialize)] pub struct BleDevice { /// The broadcast name of the device. pub name: Option, From 954644a9072b9edddb95371d00b9466690b87b12 Mon Sep 17 00:00:00 2001 From: Andrew Mackenzie Date: Sun, 23 Nov 2025 11:28:37 +0100 Subject: [PATCH 5/5] Derive Hash for BleDevice to allow it to be added to HashSets and HashMaps --- src/connections/ble_handler.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/connections/ble_handler.rs b/src/connections/ble_handler.rs index d0412f6..8229ca5 100644 --- a/src/connections/ble_handler.rs +++ b/src/connections/ble_handler.rs @@ -102,7 +102,7 @@ impl Display for BleId { } /// A Meshtastic device discovered via Bluetooth LE. -#[derive(Clone, PartialEq, Eq, Debug, Serialize, Deserialize)] +#[derive(Clone, PartialEq, Eq, Hash, Debug, Serialize, Deserialize)] pub struct BleDevice { /// The broadcast name of the device. pub name: Option,