From 8e9a89b484d98adf26c306f2a8bb0d6750767b5a Mon Sep 17 00:00:00 2001 From: Ulf Lilleengen Date: Mon, 20 Jan 2025 18:28:04 +0100 Subject: [PATCH] refactor: use a struct to hold the result of building the stack * Update examples to use destructuring. * Update tests to use destructuring * Add test compilation to ci.sh --- ci.sh | 3 ++ examples/apps/src/ble_advertise_multiple.rs | 6 ++- examples/apps/src/ble_bas_central.rs | 6 ++- examples/apps/src/ble_bas_peripheral.rs | 4 +- examples/apps/src/ble_l2cap_central.rs | 6 ++- examples/apps/src/ble_l2cap_peripheral.rs | 6 ++- examples/tests/tests/ble_l2cap_central.rs | 6 ++- examples/tests/tests/ble_l2cap_peripheral.rs | 6 ++- host/src/central.rs | 2 +- host/src/host.rs | 8 ++-- host/src/lib.rs | 40 +++++++++++--------- host/src/peripheral.rs | 2 +- host/tests/gatt.rs | 12 +++++- host/tests/gatt_derive.rs | 12 +++++- host/tests/l2cap.rs | 13 ++++++- 15 files changed, 95 insertions(+), 37 deletions(-) diff --git a/ci.sh b/ci.sh index 049dcfa..f536188 100755 --- a/ci.sh +++ b/ci.sh @@ -29,6 +29,7 @@ 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 @@ -36,3 +37,5 @@ cargo batch \ 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 diff --git a/examples/apps/src/ble_advertise_multiple.rs b/examples/apps/src/ble_advertise_multiple.rs index 874ab6e..557d553 100644 --- a/examples/apps/src/ble_advertise_multiple.rs +++ b/examples/apps/src/ble_advertise_multiple.rs @@ -26,7 +26,11 @@ where let mut resources: HostResources = 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( diff --git a/examples/apps/src/ble_bas_central.rs b/examples/apps/src/ble_bas_central.rs index 2b26056..9b605e1 100644 --- a/examples/apps/src/ble_bas_central.rs +++ b/examples/apps/src/ble_bas_central.rs @@ -19,7 +19,11 @@ where let mut resources: HostResources = 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 diff --git a/examples/apps/src/ble_bas_peripheral.rs b/examples/apps/src/ble_bas_peripheral.rs index fab0cd0..b3663b2 100644 --- a/examples/apps/src/ble_bas_peripheral.rs +++ b/examples/apps/src/ble_bas_peripheral.rs @@ -40,7 +40,9 @@ where let mut resources: HostResources = 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 { diff --git a/examples/apps/src/ble_l2cap_central.rs b/examples/apps/src/ble_l2cap_central.rs index 96e8b47..03919bd 100644 --- a/examples/apps/src/ble_l2cap_central.rs +++ b/examples/apps/src/ble_l2cap_central.rs @@ -19,7 +19,11 @@ where let mut resources: HostResources = 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 diff --git a/examples/apps/src/ble_l2cap_peripheral.rs b/examples/apps/src/ble_l2cap_peripheral.rs index 483f5ce..f4c0de7 100644 --- a/examples/apps/src/ble_l2cap_peripheral.rs +++ b/examples/apps/src/ble_l2cap_peripheral.rs @@ -18,7 +18,11 @@ where let mut resources: HostResources = 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( diff --git a/examples/tests/tests/ble_l2cap_central.rs b/examples/tests/tests/ble_l2cap_central.rs index c734acc..ccc620c 100644 --- a/examples/tests/tests/ble_l2cap_central.rs +++ b/examples/tests/tests/ble_l2cap_central.rs @@ -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() => { diff --git a/examples/tests/tests/ble_l2cap_peripheral.rs b/examples/tests/tests/ble_l2cap_peripheral.rs index 05fede8..4601c0b 100644 --- a/examples/tests/tests/ble_l2cap_peripheral.rs +++ b/examples/tests/tests/ble_l2cap_peripheral.rs @@ -47,7 +47,11 @@ async fn run_l2cap_peripheral_test(labels: &[(&str, &str)], firmware: &str) { let controller_central = serial::create_controller(¢ral).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 diff --git a/host/src/central.rs b/host/src/central.rs index 446c1ce..5c8ca41 100644 --- a/host/src/central.rs +++ b/host/src/central.rs @@ -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>, } diff --git a/host/src/host.rs b/host/src/host.rs index f2d747f..7a647d4 100644 --- a/host/src/host.rs +++ b/host/src/host.rs @@ -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>, } diff --git a/host/src/lib.rs b/host/src/lib.rs index f01cab2..9a0b0c3 100644 --- a/host/src/lib.rs +++ b/host/src/lib.rs @@ -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; @@ -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 { @@ -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. diff --git a/host/src/peripheral.rs b/host/src/peripheral.rs index 788ef3a..3f9d120 100644 --- a/host/src/peripheral.rs +++ b/host/src/peripheral.rs @@ -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>, } diff --git a/host/tests/gatt.rs b/host/tests/gatt.rs index f3c2946..6e3b48b 100644 --- a/host/tests/gatt.rs +++ b/host/tests/gatt.rs @@ -34,7 +34,11 @@ async fn gatt_client_server() { let mut resources: HostResources = 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]; @@ -129,7 +133,11 @@ async fn gatt_client_server() { let controller_central = common::create_controller(¢ral).await; let mut resources: HostResources = 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() => { diff --git a/host/tests/gatt_derive.rs b/host/tests/gatt_derive.rs index 5ebf44c..2b989bf 100644 --- a/host/tests/gatt_derive.rs +++ b/host/tests/gatt_derive.rs @@ -74,7 +74,11 @@ async fn gatt_client_server() { let mut resources: HostResources = 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, @@ -158,7 +162,11 @@ async fn gatt_client_server() { let controller_central = common::create_controller(¢ral).await; let mut resources: HostResources = 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() => { diff --git a/host/tests/l2cap.rs b/host/tests/l2cap.rs index 1a39a58..f50e9b0 100644 --- a/host/tests/l2cap.rs +++ b/host/tests/l2cap.rs @@ -29,7 +29,12 @@ async fn l2cap_connection_oriented_channels() { let mut resources: HostResources = 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() => { @@ -90,7 +95,11 @@ async fn l2cap_connection_oriented_channels() { let mut resources: HostResources = 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() => {