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 132891327f894432af948c1325fa6ab5f62230eb Mon Sep 17 00:00:00 2001 From: Andrew Mackenzie Date: Fri, 14 Nov 2025 19:15:11 +0100 Subject: [PATCH 3/5] Only echo a mesh packet back to the PacketRouter if the sending was actually successful. If sending failed, the call to send_* will return an Error as before --- src/connections/stream_api.rs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/connections/stream_api.rs b/src/connections/stream_api.rs index e960c4b..f09cb34 100644 --- a/src/connections/stream_api.rs +++ b/src/connections/stream_api.rs @@ -210,18 +210,20 @@ impl ConnectedStreamApi { ..Default::default() }; + mesh_packet.rx_time = current_epoch_secs_u32(); + + let payload_variant = Some(protobufs::to_radio::PayloadVariant::Packet(mesh_packet.clone())); + self.send_to_radio_packet(payload_variant).await?; + + // If the sending was successful, echo it back to the client via the `PacketRouter` if echo_response { - mesh_packet.rx_time = current_epoch_secs_u32(); packet_router - .handle_mesh_packet(mesh_packet.clone()) + .handle_mesh_packet(mesh_packet) .map_err(|e| Error::PacketHandlerFailure { source: Box::new(e), })?; } - let payload_variant = Some(protobufs::to_radio::PayloadVariant::Packet(mesh_packet)); - self.send_to_radio_packet(payload_variant).await?; - Ok(()) } From d36461c7b2fb9708afe6a43956e9a42b64c1a5ee Mon Sep 17 00:00:00 2001 From: Andrew Mackenzie Date: Fri, 14 Nov 2025 19:19:00 +0100 Subject: [PATCH 4/5] Modify comments slightly --- src/connections/stream_api.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/connections/stream_api.rs b/src/connections/stream_api.rs index f09cb34..e5be8fc 100644 --- a/src/connections/stream_api.rs +++ b/src/connections/stream_api.rs @@ -118,7 +118,7 @@ impl ConnectedStreamApi { /// /// # Arguments /// - /// * `packet_router` - A generic packet router field that implements the `PacketRouter` trait. + /// * `packet_router` - A struct that implements the `PacketRouter` trait. /// * `byte_data` - A `Vec` containing the byte data to send. /// * `port_num` - A `PortNum` enum that specifies the port number to send the packet on. /// * `destination` - A `PacketDestination` enum that specifies the destination of the packet. From 94a0f9480a2ad60d9919aaa201982410f6284eac Mon Sep 17 00:00:00 2001 From: Andrew Mackenzie Date: Fri, 14 Nov 2025 19:22:36 +0100 Subject: [PATCH 5/5] cargo fmt --- src/connections/stream_api.rs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/connections/stream_api.rs b/src/connections/stream_api.rs index e5be8fc..a329b12 100644 --- a/src/connections/stream_api.rs +++ b/src/connections/stream_api.rs @@ -212,16 +212,18 @@ impl ConnectedStreamApi { mesh_packet.rx_time = current_epoch_secs_u32(); - let payload_variant = Some(protobufs::to_radio::PayloadVariant::Packet(mesh_packet.clone())); + let payload_variant = Some(protobufs::to_radio::PayloadVariant::Packet( + mesh_packet.clone(), + )); self.send_to_radio_packet(payload_variant).await?; // If the sending was successful, echo it back to the client via the `PacketRouter` if echo_response { - packet_router - .handle_mesh_packet(mesh_packet) - .map_err(|e| Error::PacketHandlerFailure { + packet_router.handle_mesh_packet(mesh_packet).map_err(|e| { + Error::PacketHandlerFailure { source: Box::new(e), - })?; + } + })?; } Ok(())