aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorUlf Lilleengen <[email protected]>2025-09-19 10:25:19 +0000
committerGitHub <[email protected]>2025-09-19 10:25:19 +0000
commit8394e1320844f6a1342f1bff10215cfd4643fe3a (patch)
tree5fda1a870fb00357ca218acbdba2017fc58e5335
parente4c388c70469bd8f2f9e2072607a1cb236fd25cd (diff)
parente46f3af64ff0c94e80cb1536ac42bbea12810653 (diff)
Merge pull request #4684 from embassy-rs/cyw43-transport-error
fix: add error handling for HCI transport
-rw-r--r--cyw43/CHANGELOG.md3
-rw-r--r--cyw43/src/bluetooth.rs40
2 files changed, 36 insertions, 7 deletions
diff --git a/cyw43/CHANGELOG.md b/cyw43/CHANGELOG.md
index 58a440335..dcf84b9ba 100644
--- a/cyw43/CHANGELOG.md
+++ b/cyw43/CHANGELOG.md
@@ -9,7 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
9## Unreleased - ReleaseDate 9## Unreleased - ReleaseDate
10 10
11- Updated documentation for Control::join() #4678 11- Updated documentation for Control::join() #4678
12- Bump bt-hci to 0.5.0 12- Bump bt-hci to 0.5.0.
13- Add error handling to HCI transport implementation.
13 14
14## 0.5.0 - 2025-08-28 15## 0.5.0 - 2025-08-28
15 16
diff --git a/cyw43/src/bluetooth.rs b/cyw43/src/bluetooth.rs
index f617a8c7e..d176c4b09 100644
--- a/cyw43/src/bluetooth.rs
+++ b/cyw43/src/bluetooth.rs
@@ -3,12 +3,13 @@ use core::future::Future;
3use core::mem::MaybeUninit; 3use core::mem::MaybeUninit;
4 4
5use bt_hci::transport::WithIndicator; 5use bt_hci::transport::WithIndicator;
6use bt_hci::{ControllerToHostPacket, FromHciBytes, HostToControllerPacket, PacketKind, WriteHci}; 6use bt_hci::{ControllerToHostPacket, FromHciBytes, FromHciBytesError, HostToControllerPacket, PacketKind, WriteHci};
7use embassy_futures::yield_now; 7use embassy_futures::yield_now;
8use embassy_sync::blocking_mutex::raw::NoopRawMutex; 8use embassy_sync::blocking_mutex::raw::NoopRawMutex;
9use embassy_sync::zerocopy_channel; 9use embassy_sync::zerocopy_channel;
10use embassy_time::{Duration, Timer}; 10use embassy_time::{Duration, Timer};
11use embedded_hal_1::digital::OutputPin; 11use embedded_hal_1::digital::OutputPin;
12use embedded_io_async::ErrorKind;
12 13
13use crate::bus::Bus; 14use crate::bus::Bus;
14pub use crate::bus::SpiBusCyw43; 15pub use crate::bus::SpiBusCyw43;
@@ -472,8 +473,33 @@ impl<'a> BtRunner<'a> {
472 } 473 }
473} 474}
474 475
476/// HCI transport error.
477#[cfg_attr(feature = "defmt", derive(defmt::Format))]
478#[derive(Debug)]
479pub enum Error {
480 /// I/O error.
481 Io(ErrorKind),
482}
483
484impl From<FromHciBytesError> for Error {
485 fn from(e: FromHciBytesError) -> Self {
486 match e {
487 FromHciBytesError::InvalidSize => Error::Io(ErrorKind::InvalidInput),
488 FromHciBytesError::InvalidValue => Error::Io(ErrorKind::InvalidData),
489 }
490 }
491}
492
475impl<'d> embedded_io_async::ErrorType for BtDriver<'d> { 493impl<'d> embedded_io_async::ErrorType for BtDriver<'d> {
476 type Error = core::convert::Infallible; 494 type Error = Error;
495}
496
497impl embedded_io_async::Error for Error {
498 fn kind(&self) -> ErrorKind {
499 match self {
500 Self::Io(e) => *e,
501 }
502 }
477} 503}
478 504
479impl<'d> bt_hci::transport::Transport for BtDriver<'d> { 505impl<'d> bt_hci::transport::Transport for BtDriver<'d> {
@@ -486,9 +512,9 @@ impl<'d> bt_hci::transport::Transport for BtDriver<'d> {
486 rx[..n].copy_from_slice(&buf.buf[..n]); 512 rx[..n].copy_from_slice(&buf.buf[..n]);
487 ch.receive_done(); 513 ch.receive_done();
488 514
489 let kind = PacketKind::from_hci_bytes_complete(&rx[..1]).unwrap(); 515 let kind = PacketKind::from_hci_bytes_complete(&rx[..1])?;
490 let (res, _) = ControllerToHostPacket::from_hci_bytes_with_kind(kind, &rx[1..n]).unwrap(); 516 let (pkt, _) = ControllerToHostPacket::from_hci_bytes_with_kind(kind, &rx[1..n])?;
491 Ok(res) 517 Ok(pkt)
492 } 518 }
493 } 519 }
494 520
@@ -499,7 +525,9 @@ impl<'d> bt_hci::transport::Transport for BtDriver<'d> {
499 let buf = ch.send().await; 525 let buf = ch.send().await;
500 let buf_len = buf.buf.len(); 526 let buf_len = buf.buf.len();
501 let mut slice = &mut buf.buf[..]; 527 let mut slice = &mut buf.buf[..];
502 WithIndicator::new(val).write_hci(&mut slice).unwrap(); 528 WithIndicator::new(val)
529 .write_hci(&mut slice)
530 .map_err(|_| Error::Io(ErrorKind::Other))?;
503 buf.len = buf_len - slice.len(); 531 buf.len = buf_len - slice.len();
504 ch.send_done(); 532 ch.send_done();
505 Ok(()) 533 Ok(())