aboutsummaryrefslogtreecommitdiff
path: root/cyw43
diff options
context:
space:
mode:
authorUlf Lilleengen <[email protected]>2025-09-19 12:14:31 +0200
committerUlf Lilleengen <[email protected]>2025-09-19 12:14:31 +0200
commit6b3d2db4719046344ed93b3d0c8752ccffb2da48 (patch)
tree1ad95eb6986e7a40be1272d73488d14736f36bcf /cyw43
parente4c388c70469bd8f2f9e2072607a1cb236fd25cd (diff)
fix: add error handling for HCI transport
Diffstat (limited to 'cyw43')
-rw-r--r--cyw43/src/bluetooth.rs39
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;
3use core::mem::MaybeUninit; 3use core::mem::MaybeUninit;
4 4
5use bt_hci::transport::WithIndicator; 5use bt_hci::transport::WithIndicator;
6use bt_hci::FromHciBytesError;
6use bt_hci::{ControllerToHostPacket, FromHciBytes, HostToControllerPacket, PacketKind, WriteHci}; 7use bt_hci::{ControllerToHostPacket, FromHciBytes, HostToControllerPacket, PacketKind, WriteHci};
7use embassy_futures::yield_now; 8use embassy_futures::yield_now;
8use embassy_sync::blocking_mutex::raw::NoopRawMutex; 9use embassy_sync::blocking_mutex::raw::NoopRawMutex;
9use embassy_sync::zerocopy_channel; 10use embassy_sync::zerocopy_channel;
10use embassy_time::{Duration, Timer}; 11use embassy_time::{Duration, Timer};
11use embedded_hal_1::digital::OutputPin; 12use embedded_hal_1::digital::OutputPin;
13use embedded_io_async::ErrorKind;
12 14
13use crate::bus::Bus; 15use crate::bus::Bus;
14pub use crate::bus::SpiBusCyw43; 16pub 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)]
480pub enum Error {
481 /// I/O error.
482 Io(ErrorKind),
483}
484
485impl 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
475impl<'d> embedded_io_async::ErrorType for BtDriver<'d> { 494impl<'d> embedded_io_async::ErrorType for BtDriver<'d> {
476 type Error = core::convert::Infallible; 495 type Error = Error;
496}
497
498impl 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
479impl<'d> bt_hci::transport::Transport for BtDriver<'d> { 506impl<'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(())