Skip to content

Commit

Permalink
refactor: use a struct to hold the result of building the stack
Browse files Browse the repository at this point in the history
  • Loading branch information
lulf committed Jan 20, 2025
1 parent c113df2 commit 8ed4d15
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 29 deletions.
6 changes: 5 additions & 1 deletion examples/apps/src/ble_advertise_multiple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ where

let mut resources: HostResources<CONNECTIONS_MAX, L2CAP_CHANNELS_MAX, L2CAP_MTU> = HostResources::new();
let stack = trouble_host::new(controller, &mut resources).set_random_address(address);
let (mut peripheral, _, mut runner) = stack.build();
let Host {
mut peripheral,
mut runner,
..
} = stack.build();

let mut adv_data = [0; 31];
let len = AdStructure::encode_slice(
Expand Down
6 changes: 5 additions & 1 deletion examples/apps/src/ble_bas_central.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ where

let mut resources: HostResources<CONNECTIONS_MAX, L2CAP_CHANNELS_MAX, L2CAP_MTU> = HostResources::new();
let stack = trouble_host::new(controller, &mut resources).set_random_address(address);
let (_, mut central, mut runner) = stack.build();
let Host {
mut central,
mut runner,
..
} = stack.build();

// NOTE: Modify this to match the address of the peripheral you want to connect to.
// Currently it matches the address used by the peripheral examples
Expand Down
4 changes: 3 additions & 1 deletion examples/apps/src/ble_bas_peripheral.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ where

let mut resources: HostResources<CONNECTIONS_MAX, L2CAP_CHANNELS_MAX, L2CAP_MTU> = HostResources::new();
let stack = trouble_host::new(controller, &mut resources).set_random_address(address);
let (mut peripheral, _, runner) = stack.build();
let Host {
mut peripheral, runner, ..
} = stack.build();

info!("Starting advertising and GATT service");
let server = Server::new_with_config(GapConfig::Peripheral(PeripheralConfig {
Expand Down
6 changes: 5 additions & 1 deletion examples/apps/src/ble_l2cap_central.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ where

let mut resources: HostResources<CONNECTIONS_MAX, L2CAP_CHANNELS_MAX, L2CAP_MTU> = HostResources::new();
let stack = trouble_host::new(controller, &mut resources).set_random_address(address);
let (_, mut central, mut runner) = stack.build();
let Host {
mut central,
mut runner,
..
} = stack.build();

// NOTE: Modify this to match the address of the peripheral you want to connect to.
// Currently it matches the address used by the peripheral examples
Expand Down
6 changes: 5 additions & 1 deletion examples/apps/src/ble_l2cap_peripheral.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ where

let mut resources: HostResources<CONNECTIONS_MAX, L2CAP_CHANNELS_MAX, L2CAP_MTU> = HostResources::new();
let stack = trouble_host::new(controller, &mut resources).set_random_address(address);
let (mut peripheral, _, mut runner) = stack.build();
let Host {
mut peripheral,
mut runner,
..
} = stack.build();

let mut adv_data = [0; 31];
AdStructure::encode_slice(
Expand Down
2 changes: 1 addition & 1 deletion host/src/central.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use bt_hci::param::{ConnHandleCompletedPackets, ControllerToHostFlowControl};
use embassy_futures::select::{select, Either};

/// A type implementing the BLE central role.
pub struct Central<'stack, C: Controller> {
pub struct Central<'stack, C> {
pub(crate) stack: &'stack Stack<'stack, C>,
}

Expand Down
8 changes: 4 additions & 4 deletions host/src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -489,24 +489,24 @@ where
}

/// Runs the host with the given controller.
pub struct Runner<'d, C: Controller> {
pub struct Runner<'d, C> {
rx: RxRunner<'d, C>,
control: ControlRunner<'d, C>,
tx: TxRunner<'d, C>,
}

/// The receiver part of the host runner.
pub struct RxRunner<'d, C: Controller> {
pub struct RxRunner<'d, C> {
stack: &'d Stack<'d, C>,
}

/// The control part of the host runner.
pub struct ControlRunner<'d, C: Controller> {
pub struct ControlRunner<'d, C> {
stack: &'d Stack<'d, C>,
}

/// The transmit part of the host runner.
pub struct TxRunner<'d, C: Controller> {
pub struct TxRunner<'d, C> {
stack: &'d Stack<'d, C>,
}

Expand Down
40 changes: 22 additions & 18 deletions host/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ use host::{AdvHandleState, BleHost, HostMetrics, Runner};

#[allow(missing_docs)]
pub mod prelude {
pub use super::Host;
pub use bt_hci::uuid::*;
#[cfg(feature = "derive")]
pub use heapless::String as HeaplessString;
Expand Down Expand Up @@ -416,6 +417,19 @@ pub struct Stack<'stack, C> {
host: BleHost<'stack, C>,
}

/// Host components.
#[non_exhaustive]
pub struct Host<'stack, C> {
/// Central role
#[cfg(feature = "central")]
pub central: Central<'stack, C>,
/// Peripheral role
#[cfg(feature = "peripheral")]
pub peripheral: Peripheral<'stack, C>,
/// Host runner
pub runner: Runner<'stack, C>,
}

impl<'stack, C: Controller> Stack<'stack, C> {
/// Set the random address used by this host.
pub fn set_random_address(mut self, address: Address) -> Self {
Expand All @@ -424,24 +438,14 @@ impl<'stack, C: Controller> Stack<'stack, C> {
}

/// Build the stack.
#[cfg(all(feature = "central", feature = "peripheral"))]
pub fn build(&'stack self) -> (Peripheral<'stack, C>, Central<'stack, C>, Runner<'stack, C>) {
let stack = self;
(Peripheral::new(stack), Central::new(stack), Runner::new(stack))
}

/// Build the stack.
#[cfg(all(not(feature = "central"), feature = "peripheral"))]
pub fn build(&'stack self) -> (Peripheral<'stack, C>, Runner<'stack, C>) {
let stack = self;
(Peripheral::new(stack), Runner::new(stack))
}

/// Build the stack.
#[cfg(all(feature = "central", not(feature = "peripheral")))]
pub fn build(&'stack self) -> (Central<'stack, C>, Runner<'stack, C>) {
let stack = self;
(Central::new(stack), Runner::new(stack))
pub fn build(&'stack self) -> Host<'stack, C> {
Host {
#[cfg(feature = "central")]
central: Central::new(self),
#[cfg(feature = "peripheral")]
peripheral: Peripheral::new(self),
runner: Runner::new(self),
}
}

/// Run a HCI command and return the response.
Expand Down
2 changes: 1 addition & 1 deletion host/src/peripheral.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::connection::Connection;
use crate::{Address, BleHostError, Error, Stack};

/// Type which implements the BLE peripheral role.
pub struct Peripheral<'d, C: Controller> {
pub struct Peripheral<'d, C> {
stack: &'d Stack<'d, C>,
}

Expand Down

0 comments on commit 8ed4d15

Please sign in to comment.