Skip to content

Commit 09e0315

Browse files
committed
feat: allow configuring custom pool allocator
* Add Packet trait which constrains the packet allocator. * Use Packet trait in all places needed. * Provide out of the box configuration-based packet pool. * Adjust host buffer settings according to the ACL buffer instead of the l2cap buffer.
1 parent 563f28f commit 09e0315

File tree

75 files changed

+911
-1125
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

75 files changed

+911
-1125
lines changed

benchmarks/nrf-sdc/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ embassy-time = { version = "0.4", default-features = false, features = ["defmt",
1010
embassy-nrf = { version = "0.3", default-features = false, features = ["defmt", "time-driver-rtc1", "gpiote", "unstable-pac", "rt"] }
1111
embassy-futures = "0.1.1"
1212
embassy-sync = { version = "0.6", features = ["defmt"] }
13-
trouble-host = { path = "../../host", default-features = false, features = ["defmt", "l2cap-rx-queue-size-4", "l2cap-rx-packet-pool-size-16", "l2cap-tx-queue-size-4", "central", "peripheral", "scan", "gatt", "controller-host-flow-control"] }
13+
trouble-host = { path = "../../host", default-features = false, features = ["defmt", "l2cap-rx-queue-size-4", "l2cap-tx-queue-size-4", "central", "peripheral", "scan", "gatt", "default-packet-pool"] }
1414

1515
futures = { version = "0.3", default-features = false, features = ["async-await"]}
1616
nrf-sdc = { version = "0.1.0", default-features = false, features = ["defmt", "peripheral", "central"] }

benchmarks/nrf-sdc/src/bin/l2cap_benchmark_central.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ async fn main(spawner: Spawner) {
7676
Timer::after(Duration::from_millis(200)).await;
7777

7878
let address: Address = Address::random([0xff, 0x8f, 0x1b, 0x05, 0xe4, 0xff]);
79-
let mut resources: HostResources<CONNECTIONS_MAX, L2CAP_CHANNELS_MAX, L2CAP_MTU> = HostResources::new();
79+
let mut resources: HostResources<DefaultPacketPool, CONNECTIONS_MAX, L2CAP_CHANNELS_MAX> = HostResources::new();
8080
let stack = trouble_host::new(sdc, &mut resources).set_random_address(address);
8181
let Host {
8282
mut central,

benchmarks/nrf-sdc/src/bin/l2cap_benchmark_peripheral.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ async fn main(spawner: Spawner) {
7373
let sdc = unwrap!(build_sdc(sdc_p, &mut rng, mpsl, &mut sdc_mem));
7474

7575
let address: Address = Address::random([0xff, 0x8f, 0x1a, 0x05, 0xe4, 0xff]);
76-
let mut resources: HostResources<CONNECTIONS_MAX, L2CAP_CHANNELS_MAX, L2CAP_MTU> = HostResources::new();
76+
let mut resources: HostResources<DefaultPacketPool, CONNECTIONS_MAX, L2CAP_CHANNELS_MAX> = HostResources::new();
7777
let stack = trouble_host::new(sdc, &mut resources).set_random_address(address);
7878
let Host {
7979
mut peripheral,

examples/apps/src/ble_advertise.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ where
1818
let address: Address = Address::random([0xff, 0x8f, 0x1a, 0x05, 0xe4, 0xff]);
1919
info!("Our address = {:?}", address);
2020

21-
let mut resources: HostResources<0, 0, 27> = HostResources::new();
21+
let mut resources: HostResources<DefaultPacketPool, 0, 0> = HostResources::new();
2222
let stack = trouble_host::new(controller, &mut resources).set_random_address(address);
2323
let Host {
2424
mut peripheral,

examples/apps/src/ble_advertise_multiple.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ where
1818
let address: Address = Address::random([0xff, 0x8f, 0x1a, 0x05, 0xe4, 0xff]);
1919
info!("Our address = {:?}", address);
2020

21-
let mut resources: HostResources<0, 0, 27, 2> = HostResources::new();
21+
let mut resources: HostResources<DefaultPacketPool, 0, 0, 2> = HostResources::new();
2222
let stack = trouble_host::new(controller, &mut resources).set_random_address(address);
2323
let Host {
2424
mut peripheral,

examples/apps/src/ble_bas_central.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const CONNECTIONS_MAX: usize = 1;
88
/// Max number of L2CAP channels.
99
const L2CAP_CHANNELS_MAX: usize = 3; // Signal + att + CoC
1010

11-
pub async fn run<C, const L2CAP_MTU: usize>(controller: C)
11+
pub async fn run<C>(controller: C)
1212
where
1313
C: Controller,
1414
{
@@ -17,7 +17,7 @@ where
1717
let address: Address = Address::random([0xff, 0x8f, 0x1b, 0x05, 0xe4, 0xff]);
1818
info!("Our address = {:?}", address);
1919

20-
let mut resources: HostResources<CONNECTIONS_MAX, L2CAP_CHANNELS_MAX, L2CAP_MTU> = HostResources::new();
20+
let mut resources: HostResources<DefaultPacketPool, CONNECTIONS_MAX, L2CAP_CHANNELS_MAX> = HostResources::new();
2121
let stack = trouble_host::new(controller, &mut resources).set_random_address(address);
2222
let Host {
2323
mut central,
@@ -44,7 +44,9 @@ where
4444
let conn = central.connect(&config).await.unwrap();
4545
info!("Connected, creating gatt client");
4646

47-
let client = GattClient::<C, 10, 24>::new(&stack, &conn).await.unwrap();
47+
let client = GattClient::<C, DefaultPacketPool, 10>::new(&stack, &conn)
48+
.await
49+
.unwrap();
4850

4951
let _ = join(client.task(), async {
5052
info!("Looking for battery service");

examples/apps/src/ble_bas_central_sec.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const CONNECTIONS_MAX: usize = 1;
99
/// Max number of L2CAP channels.
1010
const L2CAP_CHANNELS_MAX: usize = 3; // Signal + att + CoC
1111

12-
pub async fn run<C, RNG, const L2CAP_MTU: usize>(controller: C, random_generator: &mut RNG)
12+
pub async fn run<C, RNG>(controller: C, random_generator: &mut RNG)
1313
where
1414
C: Controller,
1515
RNG: RngCore + CryptoRng,
@@ -19,7 +19,7 @@ where
1919
let address: Address = Address::random([0xff, 0x8f, 0x1b, 0x05, 0xe4, 0xff]);
2020
info!("Our address = {:?}", address);
2121

22-
let mut resources: HostResources<CONNECTIONS_MAX, L2CAP_CHANNELS_MAX, L2CAP_MTU> = HostResources::new();
22+
let mut resources: HostResources<DefaultPacketPool, CONNECTIONS_MAX, L2CAP_CHANNELS_MAX> = HostResources::new();
2323
let stack = trouble_host::new(controller, &mut resources)
2424
.set_random_address(address)
2525
.set_random_generator_seed(random_generator);
@@ -56,7 +56,9 @@ where
5656
}
5757
}
5858

59-
let client = GattClient::<C, 10, 24>::new(&stack, &conn).await.unwrap();
59+
let client = GattClient::<C, DefaultPacketPool, 10>::new(&stack, &conn)
60+
.await
61+
.unwrap();
6062

6163
let _ = join(client.task(), async {
6264
info!("Looking for battery service");

examples/apps/src/ble_bas_peripheral.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ struct BatteryService {
2828
}
2929

3030
/// Run the BLE stack.
31-
pub async fn run<C, const L2CAP_MTU: usize>(controller: C)
31+
pub async fn run<C>(controller: C)
3232
where
3333
C: Controller,
3434
{
@@ -37,7 +37,7 @@ where
3737
let address: Address = Address::random([0xff, 0x8f, 0x1a, 0x05, 0xe4, 0xff]);
3838
info!("Our address = {:?}", address);
3939

40-
let mut resources: HostResources<CONNECTIONS_MAX, L2CAP_CHANNELS_MAX, L2CAP_MTU> = HostResources::new();
40+
let mut resources: HostResources<DefaultPacketPool, CONNECTIONS_MAX, L2CAP_CHANNELS_MAX> = HostResources::new();
4141
let stack = trouble_host::new(controller, &mut resources).set_random_address(address);
4242
let Host {
4343
mut peripheral, runner, ..
@@ -87,7 +87,7 @@ where
8787
///
8888
/// spawner.must_spawn(ble_task(runner));
8989
/// ```
90-
async fn ble_task<C: Controller>(mut runner: Runner<'_, C>) {
90+
async fn ble_task<C: Controller, P: PacketPool>(mut runner: Runner<'_, C, P>) {
9191
loop {
9292
if let Err(e) = runner.run().await {
9393
#[cfg(feature = "defmt")]
@@ -101,7 +101,7 @@ async fn ble_task<C: Controller>(mut runner: Runner<'_, C>) {
101101
///
102102
/// This function will handle the GATT events and process them.
103103
/// This is how we interact with read and write requests.
104-
async fn gatt_events_task(server: &Server<'_>, conn: &GattConnection<'_, '_>) -> Result<(), Error> {
104+
async fn gatt_events_task<P: PacketPool>(server: &Server<'_>, conn: &GattConnection<'_, '_, P>) -> Result<(), Error> {
105105
let level = server.battery_service.level;
106106
loop {
107107
match conn.next().await {
@@ -146,9 +146,9 @@ async fn gatt_events_task(server: &Server<'_>, conn: &GattConnection<'_, '_>) ->
146146
/// Create an advertiser to use to connect to a BLE Central, and wait for it to connect.
147147
async fn advertise<'a, 'b, C: Controller>(
148148
name: &'a str,
149-
peripheral: &mut Peripheral<'a, C>,
149+
peripheral: &mut Peripheral<'a, C, DefaultPacketPool>,
150150
server: &'b Server<'_>,
151-
) -> Result<GattConnection<'a, 'b>, BleHostError<C::Error>> {
151+
) -> Result<GattConnection<'a, 'b, DefaultPacketPool>, BleHostError<C::Error>> {
152152
let mut advertiser_data = [0; 31];
153153
AdStructure::encode_slice(
154154
&[
@@ -177,7 +177,11 @@ async fn advertise<'a, 'b, C: Controller>(
177177
/// This task will notify the connected central of a counter value every 2 seconds.
178178
/// It will also read the RSSI value every 2 seconds.
179179
/// and will stop when the connection is closed by the central or an error occurs.
180-
async fn custom_task<C: Controller>(server: &Server<'_>, conn: &GattConnection<'_, '_>, stack: &Stack<'_, C>) {
180+
async fn custom_task<C: Controller, P: PacketPool>(
181+
server: &Server<'_>,
182+
conn: &GattConnection<'_, '_, P>,
183+
stack: &Stack<'_, C, P>,
184+
) {
181185
let mut tick: u8 = 0;
182186
let level = server.battery_service.level;
183187
loop {

examples/apps/src/ble_bas_peripheral_sec.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ struct BatteryService {
2929
}
3030

3131
/// Run the BLE stack.
32-
pub async fn run<C, RNG, const L2CAP_MTU: usize>(controller: C, random_generator: &mut RNG)
32+
pub async fn run<C, RNG>(controller: C, random_generator: &mut RNG)
3333
where
3434
C: Controller,
3535
RNG: RngCore + CryptoRng,
@@ -39,7 +39,7 @@ where
3939
let address: Address = Address::random([0xff, 0x8f, 0x1a, 0x05, 0xe4, 0xff]);
4040
info!("Our address = {}", address);
4141

42-
let mut resources: HostResources<CONNECTIONS_MAX, L2CAP_CHANNELS_MAX, L2CAP_MTU> = HostResources::new();
42+
let mut resources: HostResources<DefaultPacketPool, CONNECTIONS_MAX, L2CAP_CHANNELS_MAX> = HostResources::new();
4343
let stack = trouble_host::new(controller, &mut resources)
4444
.set_random_address(address)
4545
.set_random_generator_seed(random_generator);
@@ -91,7 +91,7 @@ where
9191
///
9292
/// spawner.must_spawn(ble_task(runner));
9393
/// ```
94-
async fn ble_task<C: Controller>(mut runner: Runner<'_, C>) {
94+
async fn ble_task<C: Controller, P: PacketPool>(mut runner: Runner<'_, C, P>) {
9595
loop {
9696
if let Err(e) = runner.run().await {
9797
#[cfg(feature = "defmt")]
@@ -105,7 +105,7 @@ async fn ble_task<C: Controller>(mut runner: Runner<'_, C>) {
105105
///
106106
/// This function will handle the GATT events and process them.
107107
/// This is how we interact with read and write requests.
108-
async fn gatt_events_task(server: &Server<'_>, conn: &GattConnection<'_, '_>) -> Result<(), Error> {
108+
async fn gatt_events_task(server: &Server<'_>, conn: &GattConnection<'_, '_, DefaultPacketPool>) -> Result<(), Error> {
109109
let level = server.battery_service.level;
110110
loop {
111111
match conn.next().await {
@@ -173,9 +173,9 @@ async fn gatt_events_task(server: &Server<'_>, conn: &GattConnection<'_, '_>) ->
173173
/// Create an advertiser to use to connect to a BLE Central, and wait for it to connect.
174174
async fn advertise<'a, 'b, C: Controller>(
175175
name: &'a str,
176-
peripheral: &mut Peripheral<'a, C>,
176+
peripheral: &mut Peripheral<'a, C, DefaultPacketPool>,
177177
server: &'b Server<'_>,
178-
) -> Result<GattConnection<'a, 'b>, BleHostError<C::Error>> {
178+
) -> Result<GattConnection<'a, 'b, DefaultPacketPool>, BleHostError<C::Error>> {
179179
let mut advertiser_data = [0; 31];
180180
AdStructure::encode_slice(
181181
&[
@@ -204,7 +204,11 @@ async fn advertise<'a, 'b, C: Controller>(
204204
/// This task will notify the connected central of a counter value every 2 seconds.
205205
/// It will also read the RSSI value every 2 seconds.
206206
/// and will stop when the connection is closed by the central or an error occurs.
207-
async fn custom_task<C: Controller>(server: &Server<'_>, conn: &GattConnection<'_, '_>, stack: &Stack<'_, C>) {
207+
async fn custom_task<C: Controller, P: PacketPool>(
208+
server: &Server<'_>,
209+
conn: &GattConnection<'_, '_, P>,
210+
stack: &Stack<'_, C, P>,
211+
) {
208212
let mut tick: u8 = 0;
209213
let level = server.battery_service.level;
210214
loop {

examples/apps/src/ble_l2cap_central.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const CONNECTIONS_MAX: usize = 1;
88
/// Max number of L2CAP channels.
99
const L2CAP_CHANNELS_MAX: usize = 3; // Signal + att + CoC
1010

11-
pub async fn run<C, const L2CAP_MTU: usize>(controller: C)
11+
pub async fn run<C>(controller: C)
1212
where
1313
C: Controller,
1414
{
@@ -17,7 +17,7 @@ where
1717
let address: Address = Address::random([0xff, 0x8f, 0x1b, 0x05, 0xe4, 0xff]);
1818
info!("Our address = {:?}", address);
1919

20-
let mut resources: HostResources<CONNECTIONS_MAX, L2CAP_CHANNELS_MAX, L2CAP_MTU> = HostResources::new();
20+
let mut resources: HostResources<DefaultPacketPool, CONNECTIONS_MAX, L2CAP_CHANNELS_MAX> = HostResources::new();
2121
let stack = trouble_host::new(controller, &mut resources).set_random_address(address);
2222
let Host {
2323
mut central,
@@ -43,6 +43,7 @@ where
4343
let conn = central.connect(&config).await.unwrap();
4444
info!("Connected, creating l2cap channel");
4545
const PAYLOAD_LEN: usize = 27;
46+
const L2CAP_MTU: usize = DefaultPacketPool::MTU;
4647
let mut ch1 = L2capChannel::create(&stack, &conn, 0x2349, &Default::default())
4748
.await
4849
.unwrap();

0 commit comments

Comments
 (0)