Skip to content

Commit

Permalink
move traits to blocking module
Browse files Browse the repository at this point in the history
  • Loading branch information
lulf committed May 4, 2024
1 parent 8ddb4a1 commit acb9cac
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 32 deletions.
16 changes: 7 additions & 9 deletions src/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@

use core::future::Future;

use crate::controller::{
CmdError, ControllerCmdAsync, ControllerCmdSync, ErrorType, TryControllerCmdAsync, TryControllerCmdSync, TryError,
};
use crate::controller::{blocking, CmdError, ControllerCmdAsync, ControllerCmdSync, ErrorType};
use crate::param::param;
use crate::{FixedSizeValue, FromHciBytes, HostToControllerPacket, PacketKind, WriteHci};

Expand Down Expand Up @@ -106,11 +104,11 @@ pub trait AsyncCmd: Cmd {
controller.exec(self)
}

fn try_exec<C: TryControllerCmdAsync<Self>>(
fn exec_blocking<C: blocking::ControllerCmdAsync<Self>>(
&self,
controller: &C,
) -> Result<(), TryError<CmdError<<C as ErrorType>::Error>>> {
controller.try_exec(self)
) -> Result<(), blocking::TryError<CmdError<<C as ErrorType>::Error>>> {
controller.exec(self)
}
}

Expand Down Expand Up @@ -156,11 +154,11 @@ pub trait SyncCmd: Cmd {
controller.exec(self)
}

fn try_exec<C: TryControllerCmdSync<Self>>(
fn exec_blocking<C: blocking::ControllerCmdSync<Self>>(
&self,
controller: &C,
) -> Result<Self::Return, TryError<CmdError<<C as ErrorType>::Error>>> {
controller.try_exec(self)
) -> Result<Self::Return, blocking::TryError<CmdError<<C as ErrorType>::Error>>> {
controller.exec(self)
}
}

Expand Down
25 changes: 2 additions & 23 deletions src/controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ use crate::param::{RemainingBytes, Status};
use crate::transport::Transport;
use crate::{cmd, data, param, ControllerToHostPacket, FixedSizeValue, FromHciBytes, HostToControllerPacket};

pub mod blocking;

pub trait ErrorType {
type Error: embedded_io::Error;
}
Expand All @@ -27,14 +29,6 @@ pub trait Controller: ErrorType {
fn read<'a>(&self, buf: &'a mut [u8]) -> impl Future<Output = Result<ControllerToHostPacket<'a>, Self::Error>>;
}

pub trait TryController: ErrorType {
fn try_write_acl_data(&self, packet: &data::AclPacket) -> Result<(), TryError<Self::Error>>;
fn try_write_sync_data(&self, packet: &data::SyncPacket) -> Result<(), TryError<Self::Error>>;
fn try_write_iso_data(&self, packet: &data::IsoPacket) -> Result<(), TryError<Self::Error>>;

fn try_read<'a>(&self, buf: &'a mut [u8]) -> Result<ControllerToHostPacket<'a>, TryError<Self::Error>>;
}

/// An error type for Bluetooth HCI commands.
#[derive(Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
Expand All @@ -44,13 +38,6 @@ pub enum CmdError<E> {
}

/// An error type for try operations.
#[derive(Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum TryError<E> {
Error(E),
Busy,
}

impl<E> From<param::Error> for CmdError<E> {
fn from(e: param::Error) -> Self {
Self::Hci(e)
Expand All @@ -67,14 +54,6 @@ pub trait ControllerCmdAsync<C: cmd::AsyncCmd + ?Sized>: Controller {
fn exec(&self, cmd: &C) -> impl Future<Output = Result<(), CmdError<Self::Error>>>;
}

pub trait TryControllerCmdSync<C: cmd::SyncCmd + ?Sized>: TryController {
fn try_exec(&self, cmd: &C) -> Result<C::Return, TryError<CmdError<Self::Error>>>;
}

pub trait TryControllerCmdAsync<C: cmd::AsyncCmd + ?Sized>: TryController {
fn try_exec(&self, cmd: &C) -> Result<(), TryError<CmdError<Self::Error>>>;
}

/// An external Bluetooth controller with communication via [`Transport`] type `T`.
///
/// The controller state holds a number of command slots that can be used
Expand Down
33 changes: 33 additions & 0 deletions src/controller/blocking.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use crate::{
cmd,
controller::{CmdError, ErrorType},
data, ControllerToHostPacket,
};

pub trait Controller: ErrorType {
fn write_acl_data(&self, packet: &data::AclPacket) -> Result<(), Self::Error>;
fn write_sync_data(&self, packet: &data::SyncPacket) -> Result<(), Self::Error>;
fn write_iso_data(&self, packet: &data::IsoPacket) -> Result<(), Self::Error>;

fn try_write_acl_data(&self, packet: &data::AclPacket) -> Result<(), TryError<Self::Error>>;
fn try_write_sync_data(&self, packet: &data::SyncPacket) -> Result<(), TryError<Self::Error>>;
fn try_write_iso_data(&self, packet: &data::IsoPacket) -> Result<(), TryError<Self::Error>>;

fn read<'a>(&self, buf: &'a mut [u8]) -> Result<ControllerToHostPacket<'a>, Self::Error>;
fn try_read<'a>(&self, buf: &'a mut [u8]) -> Result<ControllerToHostPacket<'a>, TryError<Self::Error>>;
}

#[derive(Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum TryError<E> {
Error(E),
Busy,
}

pub trait ControllerCmdSync<C: cmd::SyncCmd + ?Sized>: Controller {
fn exec(&self, cmd: &C) -> Result<C::Return, TryError<CmdError<Self::Error>>>;
}

pub trait ControllerCmdAsync<C: cmd::AsyncCmd + ?Sized>: Controller {
fn exec(&self, cmd: &C) -> Result<(), TryError<CmdError<Self::Error>>>;
}

0 comments on commit acb9cac

Please sign in to comment.