Skip to content

Commit

Permalink
Refactor advertisement configuration
Browse files Browse the repository at this point in the history
* Create a high level API for configuring advertisments
* Encapsulate bt-hci in a higher level api
  • Loading branch information
lulf committed Apr 6, 2024
1 parent acf3417 commit a3517d6
Show file tree
Hide file tree
Showing 8 changed files with 441 additions and 126 deletions.
42 changes: 22 additions & 20 deletions examples/nrf-sdc/src/bin/ble_bas_peripheral.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
#![no_main]
#![feature(impl_trait_in_assoc_type)]

use bt_hci::cmd::le::LeSetRandomAddr;
use bt_hci::cmd::SyncCmd;
use bt_hci::param::BdAddr;
use defmt::{error, info, unwrap};
use embassy_executor::Spawner;
use embassy_futures::join::join3;
Expand All @@ -16,9 +13,9 @@ use sdc::rng_pool::RngPool;
use static_cell::StaticCell;
use trouble_host::{
adapter::{Adapter, HostResources},
advertise::{AdStructure, AdvertiseConfig, BR_EDR_NOT_SUPPORTED, LE_GENERAL_DISCOVERABLE},
advertise::{AdStructure, Advertisement, BR_EDR_NOT_SUPPORTED, LE_GENERAL_DISCOVERABLE},
attribute::{AttributeTable, CharacteristicProp, Service, Uuid},
PacketQos,
Address, PacketQos,
};

use {defmt_rtt as _, panic_probe as _};
Expand All @@ -37,12 +34,12 @@ async fn mpsl_task(mpsl: &'static MultiprotocolServiceLayer<'static>) -> ! {
mpsl.run().await
}

fn bd_addr() -> BdAddr {
fn my_addr() -> Address {
unsafe {
let ficr = &*pac::FICR::ptr();
let high = u64::from((ficr.deviceaddr[1].read().bits() & 0x0000ffff) | 0x0000c000);
let addr = high << 32 | u64::from(ficr.deviceaddr[0].read().bits());
BdAddr::new(unwrap!(addr.to_le_bytes()[..6].try_into()))
Address::random(unwrap!(addr.to_le_bytes()[..6].try_into()))
}
}

Expand Down Expand Up @@ -98,8 +95,8 @@ async fn main(spawner: Spawner) {
spawner.must_spawn(mpsl_task(&*mpsl));

let sdc_p = sdc::Peripherals::new(
pac_p.ECB, pac_p.AAR, p.NVMC, p.PPI_CH17, p.PPI_CH18, p.PPI_CH20, p.PPI_CH21, p.PPI_CH22, p.PPI_CH23,
p.PPI_CH24, p.PPI_CH25, p.PPI_CH26, p.PPI_CH27, p.PPI_CH28, p.PPI_CH29,
pac_p.ECB, pac_p.AAR, p.PPI_CH17, p.PPI_CH18, p.PPI_CH20, p.PPI_CH21, p.PPI_CH22, p.PPI_CH23, p.PPI_CH24,
p.PPI_CH25, p.PPI_CH26, p.PPI_CH27, p.PPI_CH28, p.PPI_CH29,
);

let mut pool = [0; 256];
Expand All @@ -108,23 +105,14 @@ async fn main(spawner: Spawner) {
let mut sdc_mem = sdc::Mem::<3312>::new();
let sdc = unwrap!(build_sdc(sdc_p, &rng, mpsl, &mut sdc_mem));

info!("Our address = {:02x}", bd_addr());
unwrap!(LeSetRandomAddr::new(bd_addr()).exec(&sdc).await);
info!("Our address = {:02x}", my_addr());
Timer::after(Duration::from_millis(200)).await;

static HOST_RESOURCES: StaticCell<HostResources<NoopRawMutex, L2CAP_CHANNELS_MAX, PACKET_POOL_SIZE, L2CAP_MTU>> =
StaticCell::new();
let host_resources = HOST_RESOURCES.init(HostResources::new(PacketQos::None));

let adapter: Adapter<'_, NoopRawMutex, _, CONNECTIONS_MAX, L2CAP_CHANNELS_MAX> = Adapter::new(sdc, host_resources);
let config = AdvertiseConfig {
params: None,
adv_data: &[
AdStructure::Flags(LE_GENERAL_DISCOVERABLE | BR_EDR_NOT_SUPPORTED),
AdStructure::ServiceUuids16(&[Uuid::Uuid16([0x0f, 0x18])]),
],
scan_data: &[AdStructure::CompleteLocalName(b"Trouble")],
};

let mut table: AttributeTable<'_, NoopRawMutex, 10> = AttributeTable::new();

Expand Down Expand Up @@ -152,6 +140,7 @@ async fn main(spawner: Spawner) {
};

let server = adapter.gatt_server(&table);
unwrap!(adapter.set_random_address(my_addr()).await);

info!("Starting advertising and GATT service");
let _ = join3(
Expand All @@ -169,7 +158,20 @@ async fn main(spawner: Spawner) {
}
},
async {
let conn = unwrap!(adapter.advertise(&config).await);
let conn = unwrap!(
adapter
.advertise(
&Default::default(),
Advertisement::ConnectableScannableUndirected {
adv_data: &[
AdStructure::Flags(LE_GENERAL_DISCOVERABLE | BR_EDR_NOT_SUPPORTED),
AdStructure::ServiceUuids16(&[Uuid::Uuid16([0x0f, 0x18])]),
],
scan_data: &[AdStructure::CompleteLocalName(b"Trouble")],
}
)
.await
);
// Keep connection alive
let mut tick: u8 = 0;
loop {
Expand Down
21 changes: 9 additions & 12 deletions examples/nrf-sdc/src/bin/ble_l2cap_central.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
#![no_main]
#![feature(impl_trait_in_assoc_type)]

use bt_hci::cmd::le::LeSetRandomAddr;
use bt_hci::cmd::SyncCmd;
use bt_hci::param::BdAddr;
use defmt::{info, unwrap};
use embassy_executor::Spawner;
use embassy_futures::join::join;
Expand All @@ -19,7 +16,7 @@ use trouble_host::{
connection::Connection,
l2cap::L2capChannel,
scan::ScanConfig,
PacketQos,
Address, PacketQos,
};

use {defmt_rtt as _, panic_probe as _};
Expand All @@ -38,12 +35,12 @@ async fn mpsl_task(mpsl: &'static MultiprotocolServiceLayer<'static>) -> ! {
mpsl.run().await
}

fn bd_addr() -> BdAddr {
fn my_addr() -> Address {
unsafe {
let ficr = &*pac::FICR::ptr();
let high = u64::from((ficr.deviceaddr[1].read().bits() & 0x0000ffff) | 0x0000c000);
let addr = high << 32 | u64::from(ficr.deviceaddr[0].read().bits());
BdAddr::new(unwrap!(addr.to_le_bytes()[..6].try_into()))
Address::random(unwrap!(addr.to_le_bytes()[..6].try_into()))
}
}

Expand Down Expand Up @@ -106,8 +103,8 @@ async fn main(spawner: Spawner) {
spawner.must_spawn(mpsl_task(&*mpsl));

let sdc_p = sdc::Peripherals::new(
pac_p.ECB, pac_p.AAR, p.NVMC, p.PPI_CH17, p.PPI_CH18, p.PPI_CH20, p.PPI_CH21, p.PPI_CH22, p.PPI_CH23,
p.PPI_CH24, p.PPI_CH25, p.PPI_CH26, p.PPI_CH27, p.PPI_CH28, p.PPI_CH29,
pac_p.ECB, pac_p.AAR, p.PPI_CH17, p.PPI_CH18, p.PPI_CH20, p.PPI_CH21, p.PPI_CH22, p.PPI_CH23, p.PPI_CH24,
p.PPI_CH25, p.PPI_CH26, p.PPI_CH27, p.PPI_CH28, p.PPI_CH29,
);

let mut pool = [0; 256];
Expand All @@ -116,8 +113,7 @@ async fn main(spawner: Spawner) {
let mut sdc_mem = sdc::Mem::<6544>::new();
let sdc = unwrap!(build_sdc(sdc_p, &rng, mpsl, &mut sdc_mem));

info!("Our address = {:02x}", bd_addr());
unwrap!(LeSetRandomAddr::new(bd_addr()).exec(&sdc).await);
info!("Our address = {:02x}", my_addr());
Timer::after(Duration::from_millis(200)).await;

static HOST_RESOURCES: StaticCell<HostResources<NoopRawMutex, L2CAP_CHANNELS_MAX, PACKET_POOL_SIZE, L2CAP_MTU>> =
Expand All @@ -132,15 +128,16 @@ async fn main(spawner: Spawner) {
};

// NOTE: Modify this to match the address of the peripheral you want to connect to
let target: BdAddr = BdAddr::new([0xf5, 0x9f, 0x1a, 0x05, 0xe4, 0xee]);
let target: Address = Address::random([0xf5, 0x9f, 0x1a, 0x05, 0xe4, 0xee]);
unwrap!(adapter.set_random_address(my_addr()).await);

info!("Scanning for peripheral...");
let _ = join(adapter.run(), async {
loop {
let reports = unwrap!(adapter.scan(&config).await);
for report in reports.iter() {
let report = report.unwrap();
if report.addr == target {
if report.addr == target.addr {
let conn = Connection::connect(&adapter, report.addr).await;
info!("Connected, creating l2cap channel");
const PAYLOAD_LEN: usize = 27;
Expand Down
36 changes: 19 additions & 17 deletions examples/nrf-sdc/src/bin/ble_l2cap_peripheral.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
#![no_main]
#![feature(impl_trait_in_assoc_type)]

use bt_hci::cmd::le::LeSetRandomAddr;
use bt_hci::cmd::SyncCmd;
use bt_hci::param::BdAddr;
use defmt::{info, unwrap};
use embassy_executor::Spawner;
use embassy_futures::join::join;
Expand All @@ -16,9 +13,9 @@ use sdc::rng_pool::RngPool;
use static_cell::StaticCell;
use trouble_host::{
adapter::{Adapter, HostResources},
advertise::{AdStructure, AdvertiseConfig, BR_EDR_NOT_SUPPORTED, LE_GENERAL_DISCOVERABLE},
advertise::{AdStructure, Advertisement, BR_EDR_NOT_SUPPORTED, LE_GENERAL_DISCOVERABLE},
l2cap::L2capChannel,
PacketQos,
Address, PacketQos,
};

use {defmt_rtt as _, panic_probe as _};
Expand All @@ -37,12 +34,12 @@ async fn mpsl_task(mpsl: &'static MultiprotocolServiceLayer<'static>) -> ! {
mpsl.run().await
}

fn bd_addr() -> BdAddr {
fn my_addr() -> Address {
unsafe {
let ficr = &*pac::FICR::ptr();
let high = u64::from((ficr.deviceaddr[1].read().bits() & 0x0000ffff) | 0x0000c000);
let addr = high << 32 | u64::from(ficr.deviceaddr[0].read().bits());
BdAddr::new(unwrap!(addr.to_le_bytes()[..6].try_into()))
Address::random(unwrap!(addr.to_le_bytes()[..6].try_into()))
}
}

Expand Down Expand Up @@ -105,8 +102,8 @@ async fn main(spawner: Spawner) {
spawner.must_spawn(mpsl_task(&*mpsl));

let sdc_p = sdc::Peripherals::new(
pac_p.ECB, pac_p.AAR, p.NVMC, p.PPI_CH17, p.PPI_CH18, p.PPI_CH20, p.PPI_CH21, p.PPI_CH22, p.PPI_CH23,
p.PPI_CH24, p.PPI_CH25, p.PPI_CH26, p.PPI_CH27, p.PPI_CH28, p.PPI_CH29,
pac_p.ECB, pac_p.AAR, p.PPI_CH17, p.PPI_CH18, p.PPI_CH20, p.PPI_CH21, p.PPI_CH22, p.PPI_CH23, p.PPI_CH24,
p.PPI_CH25, p.PPI_CH26, p.PPI_CH27, p.PPI_CH28, p.PPI_CH29,
);

let mut pool = [0; 256];
Expand All @@ -115,8 +112,7 @@ async fn main(spawner: Spawner) {
let mut sdc_mem = sdc::Mem::<6224>::new();
let sdc = unwrap!(build_sdc(sdc_p, &rng, mpsl, &mut sdc_mem));

info!("Our address = {:02x}", bd_addr());
unwrap!(LeSetRandomAddr::new(bd_addr()).exec(&sdc).await);
info!("Our address = {:02x}", my_addr());
Timer::after(Duration::from_millis(200)).await;

static HOST_RESOURCES: StaticCell<HostResources<NoopRawMutex, L2CAP_CHANNELS_MAX, PACKET_POOL_SIZE, L2CAP_MTU>> =
Expand All @@ -125,16 +121,22 @@ async fn main(spawner: Spawner) {

let adapter: Adapter<'_, NoopRawMutex, _, CONNECTIONS_MAX, L2CAP_CHANNELS_MAX> = Adapter::new(sdc, host_resources);

let config = AdvertiseConfig {
params: None,
adv_data: &[AdStructure::Flags(LE_GENERAL_DISCOVERABLE | BR_EDR_NOT_SUPPORTED)],
scan_data: &[AdStructure::CompleteLocalName(b"Trouble")],
};
unwrap!(adapter.set_random_address(my_addr()).await);

let _ = join(adapter.run(), async {
loop {
info!("Advertising, waiting for connection...");
let conn = unwrap!(adapter.advertise(&config).await);
let conn = unwrap!(
adapter
.advertise(
&Default::default(),
Advertisement::ConnectableScannableUndirected {
adv_data: &[AdStructure::Flags(LE_GENERAL_DISCOVERABLE | BR_EDR_NOT_SUPPORTED),],
scan_data: &[AdStructure::CompleteLocalName(b"Trouble")],
}
)
.await
);

info!("Connection established");

Expand Down
24 changes: 14 additions & 10 deletions examples/serial-hci/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use tokio_serial::SerialStream;
use tokio_serial::{DataBits, Parity, StopBits};
use trouble_host::{
adapter::{Adapter, HostResources},
advertise::{AdStructure, AdvertiseConfig, BR_EDR_NOT_SUPPORTED, LE_GENERAL_DISCOVERABLE},
advertise::{AdStructure, Advertisement, BR_EDR_NOT_SUPPORTED, LE_GENERAL_DISCOVERABLE},
attribute::{AttributeTable, CharacteristicProp, Service, Uuid},
PacketQos,
};
Expand Down Expand Up @@ -62,14 +62,6 @@ async fn main() {
let host_resources = HOST_RESOURCES.init(HostResources::new(PacketQos::None));

let adapter: Adapter<'_, NoopRawMutex, _, 2, 4, 1, 1> = Adapter::new(controller, host_resources);
let config = AdvertiseConfig {
params: None,
adv_data: &[
AdStructure::Flags(LE_GENERAL_DISCOVERABLE | BR_EDR_NOT_SUPPORTED),
AdStructure::ServiceUuids16(&[Uuid::Uuid16([0x0f, 0x18])]),
],
scan_data: &[AdStructure::CompleteLocalName(b"Trouble HCI")],
};

let mut table: AttributeTable<'_, NoopRawMutex, 10> = AttributeTable::new();

Expand Down Expand Up @@ -114,7 +106,19 @@ async fn main() {
}
},
async {
let conn = adapter.advertise(&config).await.unwrap();
let conn = adapter
.advertise(
&Default::default(),
Advertisement::ConnectableScannableUndirected {
adv_data: &[
AdStructure::Flags(LE_GENERAL_DISCOVERABLE | BR_EDR_NOT_SUPPORTED),
AdStructure::ServiceUuids16(&[Uuid::Uuid16([0x0f, 0x18])]),
],
scan_data: &[AdStructure::CompleteLocalName(b"Trouble HCI")],
},
)
.await
.unwrap();
// Keep connection alive
let mut tick: u8 = 0;
loop {
Expand Down
Loading

0 comments on commit a3517d6

Please sign in to comment.