diff options
| author | Ulf Lilleengen <[email protected]> | 2025-09-19 12:14:31 +0200 |
|---|---|---|
| committer | Ulf Lilleengen <[email protected]> | 2025-09-19 12:14:31 +0200 |
| commit | 6b3d2db4719046344ed93b3d0c8752ccffb2da48 (patch) | |
| tree | 1ad95eb6986e7a40be1272d73488d14736f36bcf | |
| parent | e4c388c70469bd8f2f9e2072607a1cb236fd25cd (diff) | |
fix: add error handling for HCI transport
| -rw-r--r-- | cyw43/src/bluetooth.rs | 39 |
1 files changed, 34 insertions, 5 deletions
diff --git a/cyw43/src/bluetooth.rs b/cyw43/src/bluetooth.rs index f617a8c7e..e530326db 100644 --- a/cyw43/src/bluetooth.rs +++ b/cyw43/src/bluetooth.rs | |||
| @@ -3,12 +3,14 @@ use core::future::Future; | |||
| 3 | use core::mem::MaybeUninit; | 3 | use core::mem::MaybeUninit; |
| 4 | 4 | ||
| 5 | use bt_hci::transport::WithIndicator; | 5 | use bt_hci::transport::WithIndicator; |
| 6 | use bt_hci::FromHciBytesError; | ||
| 6 | use bt_hci::{ControllerToHostPacket, FromHciBytes, HostToControllerPacket, PacketKind, WriteHci}; | 7 | use bt_hci::{ControllerToHostPacket, FromHciBytes, HostToControllerPacket, PacketKind, WriteHci}; |
| 7 | use embassy_futures::yield_now; | 8 | use embassy_futures::yield_now; |
| 8 | use embassy_sync::blocking_mutex::raw::NoopRawMutex; | 9 | use embassy_sync::blocking_mutex::raw::NoopRawMutex; |
| 9 | use embassy_sync::zerocopy_channel; | 10 | use embassy_sync::zerocopy_channel; |
| 10 | use embassy_time::{Duration, Timer}; | 11 | use embassy_time::{Duration, Timer}; |
| 11 | use embedded_hal_1::digital::OutputPin; | 12 | use embedded_hal_1::digital::OutputPin; |
| 13 | use embedded_io_async::ErrorKind; | ||
| 12 | 14 | ||
| 13 | use crate::bus::Bus; | 15 | use crate::bus::Bus; |
| 14 | pub use crate::bus::SpiBusCyw43; | 16 | pub use crate::bus::SpiBusCyw43; |
| @@ -472,8 +474,33 @@ impl<'a> BtRunner<'a> { | |||
| 472 | } | 474 | } |
| 473 | } | 475 | } |
| 474 | 476 | ||
| 477 | /// HCI transport error. | ||
| 478 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] | ||
| 479 | #[derive(Debug)] | ||
| 480 | pub enum Error { | ||
| 481 | /// I/O error. | ||
| 482 | Io(ErrorKind), | ||
| 483 | } | ||
| 484 | |||
| 485 | impl From<FromHciBytesError> for Error { | ||
| 486 | fn from(e: FromHciBytesError) -> Self { | ||
| 487 | match e { | ||
| 488 | FromHciBytesError::InvalidSize => Error::Io(ErrorKind::InvalidInput), | ||
| 489 | FromHciBytesError::InvalidValue => Error::Io(ErrorKind::InvalidData), | ||
| 490 | } | ||
| 491 | } | ||
| 492 | } | ||
| 493 | |||
| 475 | impl<'d> embedded_io_async::ErrorType for BtDriver<'d> { | 494 | impl<'d> embedded_io_async::ErrorType for BtDriver<'d> { |
| 476 | type Error = core::convert::Infallible; | 495 | type Error = Error; |
| 496 | } | ||
| 497 | |||
| 498 | impl embedded_io_async::Error for Error { | ||
| 499 | fn kind(&self) -> ErrorKind { | ||
| 500 | match self { | ||
| 501 | Self::Io(e) => *e, | ||
| 502 | } | ||
| 503 | } | ||
| 477 | } | 504 | } |
| 478 | 505 | ||
| 479 | impl<'d> bt_hci::transport::Transport for BtDriver<'d> { | 506 | impl<'d> bt_hci::transport::Transport for BtDriver<'d> { |
| @@ -486,9 +513,9 @@ impl<'d> bt_hci::transport::Transport for BtDriver<'d> { | |||
| 486 | rx[..n].copy_from_slice(&buf.buf[..n]); | 513 | rx[..n].copy_from_slice(&buf.buf[..n]); |
| 487 | ch.receive_done(); | 514 | ch.receive_done(); |
| 488 | 515 | ||
| 489 | let kind = PacketKind::from_hci_bytes_complete(&rx[..1]).unwrap(); | 516 | let kind = PacketKind::from_hci_bytes_complete(&rx[..1])?; |
| 490 | let (res, _) = ControllerToHostPacket::from_hci_bytes_with_kind(kind, &rx[1..n]).unwrap(); | 517 | let (pkt, _) = ControllerToHostPacket::from_hci_bytes_with_kind(kind, &rx[1..n])?; |
| 491 | Ok(res) | 518 | Ok(pkt) |
| 492 | } | 519 | } |
| 493 | } | 520 | } |
| 494 | 521 | ||
| @@ -499,7 +526,9 @@ impl<'d> bt_hci::transport::Transport for BtDriver<'d> { | |||
| 499 | let buf = ch.send().await; | 526 | let buf = ch.send().await; |
| 500 | let buf_len = buf.buf.len(); | 527 | let buf_len = buf.buf.len(); |
| 501 | let mut slice = &mut buf.buf[..]; | 528 | let mut slice = &mut buf.buf[..]; |
| 502 | WithIndicator::new(val).write_hci(&mut slice).unwrap(); | 529 | WithIndicator::new(val) |
| 530 | .write_hci(&mut slice) | ||
| 531 | .map_err(|_| Error::Io(ErrorKind::Other))?; | ||
| 503 | buf.len = buf_len - slice.len(); | 532 | buf.len = buf_len - slice.len(); |
| 504 | ch.send_done(); | 533 | ch.send_done(); |
| 505 | Ok(()) | 534 | Ok(()) |
