Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into allow-getting-and-set…
Browse files Browse the repository at this point in the history
…ting-all-attributes
  • Loading branch information
petekubiak committed Jan 23, 2025
2 parents a2d3479 + e1940e8 commit 7a05271
Show file tree
Hide file tree
Showing 59 changed files with 1,525 additions and 1,881 deletions.
12 changes: 8 additions & 4 deletions ci.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ set -eo pipefail

if ! command -v cargo-batch &> /dev/null; then
mkdir -p $HOME/.cargo/bin
curl -L https://github.com/embassy-rs/cargo-batch/releases/download/batch-0.5.0/cargo-batch > $HOME/.cargo/bin/cargo-batch
curl -L https://github.com/embassy-rs/cargo-batch/releases/download/batch-0.6.0/cargo-batch > $HOME/.cargo/bin/cargo-batch
chmod +x $HOME/.cargo/bin/cargo-batch
fi

Expand All @@ -18,20 +18,24 @@ fi
cargo batch \
--- build --release --manifest-path host/Cargo.toml --no-default-features --features peripheral \
--- build --release --manifest-path host/Cargo.toml --no-default-features --features central \
--- build --release --manifest-path host/Cargo.toml --no-default-features --features central,scan \
--- build --release --manifest-path host/Cargo.toml --no-default-features --features central,peripheral \
--- build --release --manifest-path host/Cargo.toml --no-default-features --features central,peripheral,defmt \
--- build --release --manifest-path host/Cargo.toml --no-default-features --features gatt,peripheral \
--- build --release --manifest-path host/Cargo.toml --no-default-features --features gatt,central \
--- build --release --manifest-path host/Cargo.toml --no-default-features --features gatt,peripheral,central,scan \
--- build --release --manifest-path examples/nrf-sdc/Cargo.toml --target thumbv7em-none-eabihf --features nrf52840 \
--- build --release --manifest-path examples/nrf-sdc/Cargo.toml --target thumbv7em-none-eabihf --features nrf52833 --out-dir tests/nrf-sdc \
--- build --release --manifest-path examples/nrf-sdc/Cargo.toml --target thumbv7em-none-eabihf --features nrf52833 --artifact-dir tests/nrf-sdc \
--- build --release --manifest-path examples/nrf-sdc/Cargo.toml --target thumbv7em-none-eabihf --features nrf52832 \
--- build --release --manifest-path examples/esp32/Cargo.toml --target riscv32imc-unknown-none-elf --out-dir tests/esp32 \
--- 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 --lib -- --nocapture
cargo test --manifest-path ./host/Cargo.toml --no-run -- --nocapture
cargo test --manifest-path ./examples/tests/Cargo.toml --no-run -- --nocapture
2 changes: 1 addition & 1 deletion examples/apache-nimble/src/bin/ble_bas_peripheral.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ async fn main(spawner: embassy_executor::Spawner) {
// wait for RNG to calm down
Timer::after(Duration::from_secs(1)).await;

ble_bas_peripheral::run(controller).await;
ble_bas_peripheral::run::<_, 128>(controller).await;
}

#[embassy_executor::task]
Expand Down
2 changes: 0 additions & 2 deletions examples/apps/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ embassy-futures = "0.1.1"
embassy-sync = { version = "0.6" }
embassy-time = "0.4"
embedded-hal = "1.0"
embedded-hal-async = "1.0"
static_cell = "2"
embedded-io = "0.6"

Expand All @@ -28,7 +27,6 @@ defmt = [
"embedded-io/defmt-03",
"embedded-hal/defmt-03"
]
esp = []
log = [
"dep:log",
"trouble-host/log",
Expand Down
18 changes: 8 additions & 10 deletions examples/apps/src/ble_advertise_multiple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,13 @@ use embassy_futures::join::join;
use embassy_time::{Duration, Instant, Timer};
use trouble_host::prelude::*;

/// Size of L2CAP packets (ATT MTU is this - 4)
const L2CAP_MTU: usize = 27;

/// Max number of connections
const CONNECTIONS_MAX: usize = 1;

/// Max number of L2CAP channels.
const L2CAP_CHANNELS_MAX: usize = 2; // Signal + att

type Resources<C> = HostResources<C, CONNECTIONS_MAX, L2CAP_CHANNELS_MAX, L2CAP_MTU>;

pub async fn run<C>(controller: C)
pub async fn run<C, const L2CAP_MTU: usize>(controller: C)
where
C: Controller
+ for<'t> ControllerCmdSync<LeSetExtAdvData<'t>>
Expand All @@ -29,10 +24,13 @@ where
let address: Address = Address::random([0xff, 0x8f, 0x1a, 0x05, 0xe4, 0xff]);
info!("Our address = {:?}", address);

let mut resources = Resources::new(PacketQos::None);
let (_, mut peripheral, _, mut runner) = trouble_host::new(controller, &mut resources)
.set_random_address(address)
.build();
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 Host {
mut peripheral,
mut runner,
..
} = stack.build();

let mut adv_data = [0; 31];
let len = AdStructure::encode_slice(
Expand Down
27 changes: 14 additions & 13 deletions examples/apps/src/ble_bas_central.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,28 @@ use embassy_futures::join::join;
use embassy_time::{Duration, Timer};
use trouble_host::prelude::*;

/// Size of L2CAP packets
#[cfg(not(feature = "esp"))]
const L2CAP_MTU: usize = 128;
#[cfg(feature = "esp")]
// Some esp chips only accept an MTU >= 1017
const L2CAP_MTU: usize = 1017;

/// Max number of connections
const CONNECTIONS_MAX: usize = 1;

/// Max number of L2CAP channels.
const L2CAP_CHANNELS_MAX: usize = 3; // Signal + att + CoC

type Resources<C> = HostResources<C, CONNECTIONS_MAX, L2CAP_CHANNELS_MAX, L2CAP_MTU>;

pub async fn run<C>(controller: C)
pub async fn run<C, const L2CAP_MTU: usize>(controller: C)
where
C: Controller,
{
let mut resources = Resources::new(PacketQos::None);
let (stack, _, mut central, mut runner) = trouble_host::new(controller, &mut resources).build();
// Using a fixed "random" address can be useful for testing. In real scenarios, one would
// use e.g. the MAC 6 byte array as the address (how to get that varies by the platform).
let address: Address = Address::random([0xff, 0x8f, 0x1b, 0x05, 0xe4, 0xff]);
info!("Our address = {:?}", address);

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 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 All @@ -43,7 +44,7 @@ where
let conn = central.connect(&config).await.unwrap();
info!("Connected, creating gatt client");

let client = GattClient::<C, 10, 24>::new(stack, &conn).await.unwrap();
let client = GattClient::<C, 10, 24>::new(&stack, &conn).await.unwrap();

let _ = join(client.task(), async {
info!("Looking for battery service");
Expand Down
26 changes: 9 additions & 17 deletions examples/apps/src/ble_bas_peripheral.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,6 @@ use embassy_futures::{join::join, select::select};
use embassy_time::Timer;
use trouble_host::prelude::*;

/// Size of L2CAP packets
#[cfg(not(feature = "esp"))]
pub const L2CAP_MTU: usize = 251;
#[cfg(feature = "esp")]
// Some esp chips only accept an MTU >= 1017
pub const L2CAP_MTU: usize = 1017;

/// Max number of connections
const CONNECTIONS_MAX: usize = 1;

Expand All @@ -17,8 +10,6 @@ const L2CAP_CHANNELS_MAX: usize = 2; // Signal + att

const MAX_ATTRIBUTES: usize = 10;

type Resources<C> = HostResources<C, CONNECTIONS_MAX, L2CAP_CHANNELS_MAX, L2CAP_MTU>;

// GATT Server definition
#[gatt_server]
struct Server {
Expand All @@ -38,19 +29,20 @@ struct BatteryService {
}

/// Run the BLE stack.
pub async fn run<C>(controller: C)
pub async fn run<C, const L2CAP_MTU: usize>(controller: C)
where
C: Controller,
{
// Using a fixed "random" address can be useful for testing. In real scenarios, one would
// use e.g. the MAC 6 byte array as the address (how to get that varies by the platform).
let address = Address::random([0x41, 0x5A, 0xE3, 0x1E, 0x83, 0xE7]);
let address: Address = Address::random([0xff, 0x8f, 0x1a, 0x05, 0xe4, 0xff]);
info!("Our address = {:?}", address);

let mut resources = Resources::new(PacketQos::None);
let (stack, mut peripheral, _, runner) = trouble_host::new(controller, &mut resources)
.set_random_address(address)
.build();
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 Host {
mut peripheral, runner, ..
} = stack.build();

info!("Starting advertising and GATT service");
let server = Server::new_with_config(GapConfig::Peripheral(PeripheralConfig {
Expand All @@ -65,7 +57,7 @@ where
Ok(conn) => {
// set up tasks when the connection is established to a central, so they don't run when no one is connected.
let a = gatt_events_task(&server, &conn);
let b = custom_task(&server, &conn, stack);
let b = custom_task(&server, &conn, &stack);
// run until any task ends (usually because the connection has been closed),
// then return to advertising state.
select(a, b).await;
Expand Down Expand Up @@ -184,7 +176,7 @@ async fn advertise<'a, C: Controller>(
/// This task will notify the connected central of a counter value every 2 seconds.
/// It will also read the RSSI value every 2 seconds.
/// and will stop when the connection is closed by the central or an error occurs.
async fn custom_task<C: Controller>(server: &Server<'_>, conn: &Connection<'_>, stack: Stack<'_, C>) {
async fn custom_task<C: Controller>(server: &Server<'_>, conn: &Connection<'_>, stack: &Stack<'_, C>) {
let mut tick: u8 = 0;
let level = server.battery_service.level;
loop {
Expand Down
36 changes: 15 additions & 21 deletions examples/apps/src/ble_l2cap_central.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,28 @@ use embassy_futures::join::join;
use embassy_time::{Duration, Timer};
use trouble_host::prelude::*;

/// How many outgoing L2CAP buffers per link
const L2CAP_TXQ: u8 = 20;

/// How many incoming L2CAP buffers per link
const L2CAP_RXQ: u8 = 20;

/// Size of L2CAP packets
#[cfg(not(feature = "esp"))]
const L2CAP_MTU: usize = 128;
#[cfg(feature = "esp")]
// Some esp chips only accept an MTU >= 1017
const L2CAP_MTU: usize = 1017;

/// Max number of connections
const CONNECTIONS_MAX: usize = 1;

/// Max number of L2CAP channels.
const L2CAP_CHANNELS_MAX: usize = 3; // Signal + att + CoC

type Resources<C> = HostResources<C, CONNECTIONS_MAX, L2CAP_CHANNELS_MAX, L2CAP_MTU>;

pub async fn run<C>(controller: C)
pub async fn run<C, const L2CAP_MTU: usize>(controller: C)
where
C: Controller,
{
let mut resources = Resources::new(PacketQos::None);
// Using a fixed "random" address can be useful for testing. In real scenarios, one would
// use e.g. the MAC 6 byte array as the address (how to get that varies by the platform).
let address: Address = Address::random([0xff, 0x8f, 0x1b, 0x05, 0xe4, 0xff]);
info!("Our address = {:?}", address);

let (stack, _, mut central, mut runner) = trouble_host::new(controller, &mut resources).build();
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 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 All @@ -49,18 +43,18 @@ where
let conn = central.connect(&config).await.unwrap();
info!("Connected, creating l2cap channel");
const PAYLOAD_LEN: usize = 27;
let mut ch1 = L2capChannel::create(stack, &conn, 0x2349, &Default::default())
let mut ch1 = L2capChannel::create(&stack, &conn, 0x2349, &Default::default())
.await
.unwrap();
info!("New l2cap channel created, sending some data!");
for i in 0..10 {
let tx = [i; PAYLOAD_LEN];
ch1.send::<_, PAYLOAD_LEN>(stack, &tx).await.unwrap();
ch1.send::<_, PAYLOAD_LEN>(&stack, &tx).await.unwrap();
}
info!("Sent data, waiting for them to be sent back");
let mut rx = [0; PAYLOAD_LEN];
for i in 0..10 {
let len = ch1.receive(stack, &mut rx).await.unwrap();
let len = ch1.receive(&stack, &mut rx).await.unwrap();
assert_eq!(len, rx.len());
assert_eq!(rx, [i; PAYLOAD_LEN]);
}
Expand Down
33 changes: 13 additions & 20 deletions examples/apps/src/ble_l2cap_peripheral.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,27 @@ use embassy_futures::join::join;
use embassy_time::{Duration, Timer};
use trouble_host::prelude::*;

/// Size of L2CAP packets
#[cfg(not(feature = "esp"))]
pub const L2CAP_MTU: usize = 128;
#[cfg(feature = "esp")]
// Some esp chips only accept an MTU >= 1017
pub const L2CAP_MTU: usize = 1017;

/// Max number of connections
pub const CONNECTIONS_MAX: usize = 1;
const CONNECTIONS_MAX: usize = 1;

/// Max number of L2CAP channels.
pub const L2CAP_CHANNELS_MAX: usize = 3; // Signal + att + CoC

type Resources<C> = HostResources<C, CONNECTIONS_MAX, L2CAP_CHANNELS_MAX, L2CAP_MTU>;
const L2CAP_CHANNELS_MAX: usize = 3; // Signal + att + CoC

pub async fn run<C>(controller: C)
pub async fn run<C, const L2CAP_MTU: usize>(controller: C)
where
C: Controller,
{
let mut resources = Resources::new(PacketQos::None);

// Hardcoded peripheral address
let address: Address = Address::random([0xff, 0x8f, 0x1a, 0x05, 0xe4, 0xff]);
info!("Our address = {:?}", address);

let (stack, mut peripheral, _, mut runner) = trouble_host::new(controller, &mut resources)
.set_random_address(address)
.build();
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 Host {
mut peripheral,
mut runner,
..
} = stack.build();

let mut adv_data = [0; 31];
AdStructure::encode_slice(
Expand Down Expand Up @@ -58,7 +51,7 @@ where

info!("Connection established");

let mut ch1 = L2capChannel::accept(stack, &conn, &[0x2349], &Default::default())
let mut ch1 = L2capChannel::accept(&stack, &conn, &[0x2349], &Default::default())
.await
.unwrap();

Expand All @@ -68,7 +61,7 @@ where
const PAYLOAD_LEN: usize = 27;
let mut rx = [0; PAYLOAD_LEN];
for i in 0..10 {
let len = ch1.receive(stack, &mut rx).await.unwrap();
let len = ch1.receive(&stack, &mut rx).await.unwrap();
assert_eq!(len, rx.len());
assert_eq!(rx, [i; PAYLOAD_LEN]);
}
Expand All @@ -77,7 +70,7 @@ where
Timer::after(Duration::from_secs(1)).await;
for i in 0..10 {
let tx = [i; PAYLOAD_LEN];
ch1.send::<_, PAYLOAD_LEN>(stack, &tx).await.unwrap();
ch1.send::<_, PAYLOAD_LEN>(&stack, &tx).await.unwrap();
}
info!("L2CAP data echoed");

Expand Down
Loading

0 comments on commit 7a05271

Please sign in to comment.