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
* Update examples to use destructuring.
* Update tests to use destructuring
* Add test compilation to ci.sh
  • Loading branch information
lulf committed Jan 21, 2025
1 parent c113df2 commit 51d9928
Show file tree
Hide file tree
Showing 15 changed files with 95 additions and 37 deletions.
3 changes: 3 additions & 0 deletions ci.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,13 @@ cargo batch \
--- build --release --manifest-path examples/nrf-sdc/Cargo.toml --target thumbv7em-none-eabihf --features nrf52832 \
--- build --release --manifest-path examples/esp32/Cargo.toml --features esp32c3 --target riscv32imc-unknown-none-elf --artifact-dir tests/esp32 \
--- build --release --manifest-path examples/serial-hci/Cargo.toml \
--- build --release --manifest-path examples/tests/Cargo.toml \
--- build --release --manifest-path examples/rp-pico-w/Cargo.toml --target thumbv6m-none-eabi --features skip-cyw43-firmware \
--- build --release --manifest-path examples/rp-pico-2-w/Cargo.toml --target thumbv8m.main-none-eabihf --features skip-cyw43-firmware
# --- build --release --manifest-path examples/apache-nimble/Cargo.toml --target thumbv7em-none-eabihf

cargo fmt --check --manifest-path ./host/Cargo.toml
cargo clippy --manifest-path ./host/Cargo.toml --features gatt,peripheral,central
cargo test --manifest-path ./host/Cargo.toml --lib -- --nocapture
cargo test --manifest-path ./host/Cargo.toml --no-run -- --nocapture
cargo test --manifest-path ./examples/tests/Cargo.toml --no-run -- --nocapture
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
6 changes: 5 additions & 1 deletion examples/tests/tests/ble_l2cap_central.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@ async fn run_l2cap_central_test(labels: &[(&str, &str)], firmware: &str) {

let mut resources: HostResources<2, 4, 27> = HostResources::new();
let stack = trouble_host::new(controller_peripheral, &mut resources).set_random_address(peripheral_address);
let (mut peripheral, _central, mut runner) = stack.build();
let Host {
mut peripheral,
mut runner,
..
} = stack.build();

select! {
r = runner.run() => {
Expand Down
6 changes: 5 additions & 1 deletion examples/tests/tests/ble_l2cap_peripheral.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ async fn run_l2cap_peripheral_test(labels: &[(&str, &str)], firmware: &str) {
let controller_central = serial::create_controller(&central).await;
let mut resources: HostResources<2, 4, 27> = HostResources::new();
let stack = trouble_host::new(controller_central, &mut resources);
let (_peripheral, mut central, mut runner) = stack.build();
let Host {
mut central,
mut runner,
..
} = stack.build();
select! {
r = runner.run() => {
r
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
12 changes: 10 additions & 2 deletions host/tests/gatt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ async fn gatt_client_server() {
let mut resources: HostResources<CONNECTIONS_MAX, L2CAP_CHANNELS_MAX, 27> = HostResources::new();
let stack = trouble_host::new(controller_peripheral, &mut resources)
.set_random_address(peripheral_address);
let (mut peripheral, _, mut runner) = stack.build();
let Host {
mut peripheral,
mut runner,
..
} = stack.build();

let id = b"Trouble";
let appearance = [0x80, 0x07];
Expand Down Expand Up @@ -129,7 +133,11 @@ async fn gatt_client_server() {
let controller_central = common::create_controller(&central).await;
let mut resources: HostResources<CONNECTIONS_MAX, L2CAP_CHANNELS_MAX, 27> = HostResources::new();
let stack = trouble_host::new(controller_central, &mut resources);
let (_, mut central, mut runner) = stack.build();
let Host {
mut central,
mut runner,
..
} = stack.build();

select! {
r = runner.run() => {
Expand Down
12 changes: 10 additions & 2 deletions host/tests/gatt_derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,11 @@ async fn gatt_client_server() {
let mut resources: HostResources<CONNECTIONS_MAX, L2CAP_CHANNELS_MAX, 27> = HostResources::new();
let stack = trouble_host::new(controller_peripheral, &mut resources)
.set_random_address(peripheral_address);
let (mut peripheral, _, mut runner) = stack.build();
let Host {
mut peripheral,
mut runner,
..
} = stack.build();

let gap = GapConfig::Peripheral(PeripheralConfig {
name: &name,
Expand Down Expand Up @@ -158,7 +162,11 @@ async fn gatt_client_server() {
let controller_central = common::create_controller(&central).await;
let mut resources: HostResources<CONNECTIONS_MAX, L2CAP_CHANNELS_MAX, 27> = HostResources::new();
let stack = trouble_host::new(controller_central, &mut resources);
let (_, mut central, mut runner) = stack.build();
let Host {
mut central,
mut runner,
..
} = stack.build();

select! {
r = runner.run() => {
Expand Down
13 changes: 11 additions & 2 deletions host/tests/l2cap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,12 @@ async fn l2cap_connection_oriented_channels() {
let mut resources: HostResources<CONNECTIONS_MAX, L2CAP_CHANNELS_MAX, 27> = HostResources::new();
let stack = trouble_host::new(controller_peripheral, &mut resources)
.set_random_address(peripheral_address);
let (mut peripheral, _, mut runner) = stack.build();
let Host {
mut peripheral,
mut runner,
..
} = stack.build();


select! {
r = runner.run() => {
Expand Down Expand Up @@ -90,7 +95,11 @@ async fn l2cap_connection_oriented_channels() {
let mut resources: HostResources<CONNECTIONS_MAX, L2CAP_CHANNELS_MAX, 27> = HostResources::new();

let stack = trouble_host::new(controller_central, &mut resources);
let (_, mut central, mut runner) = stack.build();
let Host {
mut central,
mut runner,
..
} = stack.build();

select! {
r = runner.run() => {
Expand Down

0 comments on commit 51d9928

Please sign in to comment.