Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix interaction between central and peripheral in nrf #246

Merged
merged 5 commits into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 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 @@ -25,9 +25,9 @@ cargo batch \
--- 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 --target riscv32imc-unknown-none-elf --artifact-dir tests/esp32 \
--- build --release --manifest-path examples/serial-hci/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
Expand Down
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
1 change: 0 additions & 1 deletion examples/apps/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ defmt = [
"embedded-io/defmt-03",
"embedded-hal/defmt-03"
]
esp = []
log = [
"dep:log",
"trouble-host/log",
Expand Down
10 changes: 3 additions & 7 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,7 +24,8 @@ 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 resources: HostResources<C, CONNECTIONS_MAX, L2CAP_CHANNELS_MAX, L2CAP_MTU> =
HostResources::new(PacketQos::None);
let (_, mut peripheral, _, mut runner) = trouble_host::new(controller, &mut resources)
.set_random_address(address)
.build();
Expand Down
23 changes: 11 additions & 12 deletions examples/apps/src/ble_bas_central.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,26 @@ 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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is in an example for central. I thought it was on purpose that the central can have whatever address, whereas the peripheral's needs to be fixed. Does this not cause two nodes, both with the same BLE address? 😐

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, it's different from the peripheral's fixed address:

Address::random([0x41, 0x5A, 0xE3, 0x1E, 0x83, 0xE7]);

a) What's the purpose of fixing the central's?
b) It worked before.. so why change?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: since it doesn't matter what the bytes are - does it? - how about creating them from some wonky string?

b"centra"
b"periph"
  • more wonky/funny

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea. I will do it in a follow up pr if that's ok

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also while you're doing this how about printing the address in hex with info!("Our address = {:x?}", address);


let mut resources: HostResources<C, CONNECTIONS_MAX, L2CAP_CHANNELS_MAX, L2CAP_MTU> =
HostResources::new(PacketQos::None);
let (stack, _, mut central, mut runner) = trouble_host::new(controller, &mut resources)
.set_random_address(address)
.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
16 changes: 4 additions & 12 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,16 +29,17 @@ 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 mut resources: HostResources<C, CONNECTIONS_MAX, L2CAP_CHANNELS_MAX, L2CAP_MTU> =
HostResources::new(PacketQos::None);
let (stack, mut peripheral, _, runner) = trouble_host::new(controller, &mut resources)
.set_random_address(address)
.build();
Expand Down
29 changes: 11 additions & 18 deletions examples/apps/src/ble_l2cap_central.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::*;

/// 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 mut resources: HostResources<C, CONNECTIONS_MAX, L2CAP_CHANNELS_MAX, L2CAP_MTU> =
HostResources::new(PacketQos::None);

let (stack, _, mut central, mut runner) = trouble_host::new(controller, &mut resources).build();
let (stack, _, mut central, mut runner) = trouble_host::new(controller, &mut resources)
.set_random_address(address)
.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
18 changes: 5 additions & 13 deletions examples/apps/src/ble_l2cap_peripheral.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,18 @@ 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);
let mut resources: HostResources<C, CONNECTIONS_MAX, L2CAP_CHANNELS_MAX, L2CAP_MTU> =
HostResources::new(PacketQos::None);

// Hardcoded peripheral address
let address: Address = Address::random([0xff, 0x8f, 0x1a, 0x05, 0xe4, 0xff]);
Expand Down
6 changes: 3 additions & 3 deletions examples/esp32/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ default = ["esp32c3"]
# [1]: https://github.com/embassy-rs/trouble/pull/236#issuecomment-2586457641
#
esp32 = ["esp-hal/esp32", "esp-backtrace/esp32", "esp-hal-embassy/esp32", "esp-println/esp32", "esp-wifi/esp32"]
esp32c2 = ["esp-hal/esp32c2", "esp-backtrace/esp32c2", "esp-hal-embassy/esp32c2", "esp-println/esp32c2", "esp-wifi/esp32c2", "trouble-example-apps/esp"]
esp32c2 = ["esp-hal/esp32c2", "esp-backtrace/esp32c2", "esp-hal-embassy/esp32c2", "esp-println/esp32c2", "esp-wifi/esp32c2"]
esp32c3 = ["esp-hal/esp32c3", "esp-backtrace/esp32c3", "esp-hal-embassy/esp32c3", "esp-println/esp32c3", "esp-wifi/esp32c3"]
esp32c6 = ["esp-hal/esp32c6", "esp-backtrace/esp32c6", "esp-hal-embassy/esp32c6", "esp-println/esp32c6", "esp-wifi/esp32c6", "trouble-example-apps/esp"]
esp32h2 = ["esp-hal/esp32h2", "esp-backtrace/esp32h2", "esp-hal-embassy/esp32h2", "esp-println/esp32h2", "esp-wifi/esp32h2", "trouble-example-apps/esp"]
esp32c6 = ["esp-hal/esp32c6", "esp-backtrace/esp32c6", "esp-hal-embassy/esp32c6", "esp-println/esp32c6", "esp-wifi/esp32c6"]
esp32h2 = ["esp-hal/esp32h2", "esp-backtrace/esp32h2", "esp-hal-embassy/esp32h2", "esp-println/esp32h2", "esp-wifi/esp32h2"]
esp32s3 = ["esp-hal/esp32s3", "esp-backtrace/esp32s3", "esp-hal-embassy/esp32s3", "esp-println/esp32s3", "esp-wifi/esp32s3"]

[profile.dev]
Expand Down
5 changes: 4 additions & 1 deletion examples/esp32/src/bin/ble_bas_central.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ use esp_wifi::ble::controller::BleConnector;
use trouble_example_apps::ble_bas_central;
use {esp_alloc as _, esp_backtrace as _};

#[path = "../consts.rs"]
mod consts;

#[esp_hal_embassy::main]
async fn main(_s: Spawner) {
esp_println::logger::init_logger_from_env();
Expand Down Expand Up @@ -40,5 +43,5 @@ async fn main(_s: Spawner) {
let connector = BleConnector::new(&init, bluetooth);
let controller: ExternalController<_, 20> = ExternalController::new(connector);

ble_bas_central::run(controller).await;
ble_bas_central::run::<_, { consts::L2CAP_MTU }>(controller).await;
}
5 changes: 4 additions & 1 deletion examples/esp32/src/bin/ble_bas_peripheral.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ use esp_wifi::ble::controller::BleConnector;
use trouble_example_apps::ble_bas_peripheral;
use {esp_alloc as _, esp_backtrace as _};

#[path = "../consts.rs"]
mod consts;

#[esp_hal_embassy::main]
async fn main(_s: Spawner) {
esp_println::logger::init_logger_from_env();
Expand Down Expand Up @@ -40,5 +43,5 @@ async fn main(_s: Spawner) {
let connector = BleConnector::new(&init, bluetooth);
let controller: ExternalController<_, 20> = ExternalController::new(connector);

ble_bas_peripheral::run(controller).await;
ble_bas_peripheral::run::<_, { consts::L2CAP_MTU }>(controller).await;
}
5 changes: 4 additions & 1 deletion examples/esp32/src/bin/ble_l2cap_central.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ use esp_wifi::ble::controller::BleConnector;
use trouble_example_apps::ble_l2cap_central;
use {esp_alloc as _, esp_backtrace as _};

#[path = "../consts.rs"]
mod consts;

#[esp_hal_embassy::main]
async fn main(_s: Spawner) {
esp_println::logger::init_logger_from_env();
Expand Down Expand Up @@ -40,5 +43,5 @@ async fn main(_s: Spawner) {
let connector = BleConnector::new(&init, bluetooth);
let controller: ExternalController<_, 20> = ExternalController::new(connector);

ble_l2cap_central::run(controller).await;
ble_l2cap_central::run::<_, { consts::L2CAP_MTU }>(controller).await;
}
5 changes: 4 additions & 1 deletion examples/esp32/src/bin/ble_l2cap_peripheral.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ use esp_wifi::ble::controller::BleConnector;
use trouble_example_apps::ble_l2cap_peripheral;
use {esp_alloc as _, esp_backtrace as _};

#[path = "../consts.rs"]
mod consts;

#[esp_hal_embassy::main]
async fn main(_s: Spawner) {
esp_println::logger::init_logger_from_env();
Expand Down Expand Up @@ -40,5 +43,5 @@ async fn main(_s: Spawner) {
let connector = BleConnector::new(&init, bluetooth);
let controller: ExternalController<_, 20> = ExternalController::new(connector);

ble_l2cap_peripheral::run(controller).await;
ble_l2cap_peripheral::run::<_, { consts::L2CAP_MTU }>(controller).await;
}
6 changes: 6 additions & 0 deletions examples/esp32/src/consts.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/// Size of L2CAP packets
#[cfg(not(any(feature = "esp32c2", feature = "esp32c6", feature = "esp32h2")))]
pub const L2CAP_MTU: usize = 128;
// Some esp chips only accept an MTU >= 1017
#[cfg(any(feature = "esp32c2", feature = "esp32c6", feature = "esp32h2"))]
pub const L2CAP_MTU: usize = 1017;
lulf marked this conversation as resolved.
Show resolved Hide resolved
1 change: 0 additions & 1 deletion examples/nrf-sdc/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/nrf-sdc/src/bin/ble_bas_central.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,5 @@ async fn main(spawner: Spawner) {
let mut sdc_mem = sdc::Mem::<6544>::new();
let sdc = unwrap!(build_sdc(sdc_p, &mut rng, mpsl, &mut sdc_mem));

ble_bas_central::run(sdc).await;
ble_bas_central::run::<_, L2CAP_MTU>(sdc).await;
}
12 changes: 11 additions & 1 deletion examples/nrf-sdc/src/bin/ble_bas_peripheral.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ async fn mpsl_task(mpsl: &'static MultiprotocolServiceLayer<'static>) -> ! {
mpsl.run().await
}

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

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

/// Size of L2CAP packets
const L2CAP_MTU: usize = 27;

fn build_sdc<'d, const N: usize>(
p: nrf_sdc::Peripherals<'d>,
rng: &'d mut rng::Rng<RNG>,
Expand All @@ -35,6 +44,7 @@ fn build_sdc<'d, const N: usize>(
.support_adv()?
.support_peripheral()?
.peripheral_count(1)?
.buffer_cfg(L2CAP_MTU as u8, L2CAP_MTU as u8, L2CAP_TXQ, L2CAP_RXQ)?
.build(p, rng, mpsl, mem)
}

Expand Down Expand Up @@ -63,5 +73,5 @@ async fn main(spawner: Spawner) {
let mut sdc_mem = sdc::Mem::<3312>::new();
let sdc = unwrap!(build_sdc(sdc_p, &mut rng, mpsl, &mut sdc_mem));

ble_bas_peripheral::run(sdc).await;
ble_bas_peripheral::run::<_, L2CAP_MTU>(sdc).await;
}
2 changes: 1 addition & 1 deletion examples/nrf-sdc/src/bin/ble_l2cap_central.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,5 @@ async fn main(spawner: Spawner) {

Timer::after(Duration::from_millis(200)).await;

ble_l2cap_central::run(sdc).await;
ble_l2cap_central::run::<_, L2CAP_MTU>(sdc).await;
}
5 changes: 2 additions & 3 deletions examples/nrf-sdc/src/bin/ble_l2cap_peripheral.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ const L2CAP_TXQ: u8 = 20;
/// How many incoming L2CAP buffers per link
const L2CAP_RXQ: u8 = 20;

/// Size of L2CAP packets
const L2CAP_MTU: usize = ble_l2cap_peripheral::L2CAP_MTU;
const L2CAP_MTU: usize = 27;

fn build_sdc<'d, const N: usize>(
p: nrf_sdc::Peripherals<'d>,
Expand Down Expand Up @@ -73,5 +72,5 @@ async fn main(spawner: Spawner) {
let mut sdc_mem = sdc::Mem::<12848>::new();
let sdc = unwrap!(build_sdc(sdc_p, &mut rng, mpsl, &mut sdc_mem));

ble_l2cap_peripheral::run(sdc).await;
ble_l2cap_peripheral::run::<_, L2CAP_MTU>(sdc).await;
}
2 changes: 1 addition & 1 deletion examples/rp-pico-2-w/src/bin/ble_bas_central.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,5 @@ async fn main(spawner: Spawner) {

let controller: ExternalController<_, 10> = ExternalController::new(bt_device);

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