Skip to content

Commit

Permalink
Add some default values to generic parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
lulf committed Mar 15, 2024
1 parent 140c323 commit 79030bf
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 18 deletions.
17 changes: 15 additions & 2 deletions examples/nrf-sdc/src/bin/ble_bas_peripheral.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,18 @@ fn bd_addr() -> BdAddr {
}
}

/// 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

/// Number of packets available in the pool
const PACKET_POOL_SIZE: usize = 10;

fn build_sdc<'d, const N: usize>(
p: nrf_sdc::Peripherals<'d>,
rng: &'d RngPool<'d>,
Expand Down Expand Up @@ -100,10 +112,11 @@ async fn main(spawner: Spawner) {
unwrap!(ZephyrWriteBdAddr::new(bd_addr()).exec(&sdc).await);
Timer::after(Duration::from_millis(200)).await;

static HOST_RESOURCES: StaticCell<HostResources<NoopRawMutex, 4, 32, 27>> = StaticCell::new();
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, _, 2, 4, 1, 1> = Adapter::new(sdc, host_resources);
let adapter: Adapter<'_, NoopRawMutex, _, CONNECTIONS_MAX, L2CAP_CHANNELS_MAX> = Adapter::new(sdc, host_resources);
let config = AdvertiseConfig {
params: None,
data: &[
Expand Down
34 changes: 27 additions & 7 deletions examples/nrf-sdc/src/bin/ble_l2cap_central.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,24 @@ fn bd_addr() -> BdAddr {
}
}

/// 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
const L2CAP_MTU: usize = 27;

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

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

/// Number of packets available in the pool
const PACKET_POOL_SIZE: usize = (L2CAP_TXQ + L2CAP_RXQ) as usize;

fn build_sdc<'d, const N: usize>(
p: nrf_sdc::Peripherals<'d>,
rng: &'d RngPool<'d>,
Expand All @@ -57,7 +75,7 @@ fn build_sdc<'d, const N: usize>(
.support_scan()?
.support_central()?
.central_count(1)?
.buffer_cfg(27, 27, 20, 20)?
.buffer_cfg(L2CAP_MTU as u8, L2CAP_MTU as u8, L2CAP_TXQ, L2CAP_RXQ)?
.build(p, rng, mpsl, mem)
}

Expand Down Expand Up @@ -102,10 +120,11 @@ async fn main(spawner: Spawner) {
unwrap!(ZephyrWriteBdAddr::new(bd_addr()).exec(&sdc).await);
Timer::after(Duration::from_millis(200)).await;

static HOST_RESOURCES: StaticCell<HostResources<NoopRawMutex, 4, 32, 27>> = StaticCell::new();
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::Guaranteed(4)));

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

let config = ScanConfig { params: None };

Expand All @@ -121,19 +140,20 @@ async fn main(spawner: Spawner) {
if report.addr == target {
let conn = Connection::connect(&adapter, report.addr).await;
info!("Connected, creating l2cap channel");
let mut ch1: L2capChannel<'_, '_, 27> =
const PAYLOAD_LEN: usize = 27;
let mut ch1: L2capChannel<'_, '_, PAYLOAD_LEN> =
unwrap!(L2capChannel::create(&adapter, &conn, 0x2349).await);
info!("New l2cap channel created, sending some data!");
for i in 0..10 {
let mut tx = [i; 27];
let mut tx = [i; PAYLOAD_LEN];
let _ = unwrap!(ch1.send(&mut tx).await);
}
info!("Sent data, waiting for them to be sent back");
let mut rx = [0; 27];
let mut rx = [0; PAYLOAD_LEN];
for i in 0..10 {
let len = unwrap!(ch1.receive(&mut rx).await);
assert_eq!(len, rx.len());
assert_eq!(rx, [i; 27]);
assert_eq!(rx, [i; PAYLOAD_LEN]);
}

info!("Received successfully!");
Expand Down
37 changes: 30 additions & 7 deletions examples/nrf-sdc/src/bin/ble_l2cap_peripheral.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,24 @@ fn bd_addr() -> BdAddr {
}
}

/// 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
const L2CAP_MTU: usize = 27;

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

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

/// Number of packets available in the pool
const PACKET_POOL_SIZE: usize = (L2CAP_TXQ + L2CAP_RXQ) as usize;

fn build_sdc<'d, const N: usize>(
p: nrf_sdc::Peripherals<'d>,
rng: &'d RngPool<'d>,
Expand All @@ -56,7 +74,7 @@ fn build_sdc<'d, const N: usize>(
.support_adv()?
.support_peripheral()?
.peripheral_count(1)?
.buffer_cfg(27, 27, 20, 20)?
.buffer_cfg(L2CAP_MTU as u8, L2CAP_MTU as u8, L2CAP_TXQ, L2CAP_RXQ)?
.build(p, rng, mpsl, mem)
}

Expand Down Expand Up @@ -101,10 +119,11 @@ async fn main(spawner: Spawner) {
unwrap!(ZephyrWriteBdAddr::new(bd_addr()).exec(&sdc).await);
Timer::after(Duration::from_millis(200)).await;

static HOST_RESOURCES: StaticCell<HostResources<NoopRawMutex, 4, 32, 27>> = StaticCell::new();
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::Guaranteed(4)));

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

let config = AdvertiseConfig {
params: None,
Expand All @@ -121,20 +140,24 @@ async fn main(spawner: Spawner) {

info!("Connection established");

let mut ch1: L2capChannel<'_, '_, 27> = unwrap!(L2capChannel::accept(&adapter, &conn, 0x2349).await);
let mut ch1: L2capChannel<'_, '_, PAYLOAD_LEN> =
unwrap!(L2capChannel::accept(&adapter, &conn, 0x2349).await);

info!("L2CAP channel accepted");
let mut rx = [0; 27];

// Size of payload we're expecting
const PAYLOAD_LEN: usize = 27;
let mut rx = [0; PAYLOAD_LEN];
for i in 0..10 {
let len = unwrap!(ch1.receive(&mut rx).await);
assert_eq!(len, rx.len());
assert_eq!(rx, [i; 27]);
assert_eq!(rx, [i; PAYLOAD_LEN]);
}

info!("L2CAP data received, echoing");
Timer::after(Duration::from_secs(1)).await;
for i in 0..10 {
let mut tx = [i; 27];
let mut tx = [i; PAYLOAD_LEN];
let _ = unwrap!(ch1.send(&mut tx).await);
}
info!("L2CAP data echoed");
Expand Down
11 changes: 9 additions & 2 deletions host/src/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,15 @@ impl<M: RawMutex, const CHANNELS: usize, const PACKETS: usize, const L2CAP_MTU:
}
}

pub struct Adapter<'d, M, T, const CONNS: usize, const CHANNELS: usize, const L2CAP_TXQ: usize, const L2CAP_RXQ: usize>
where
pub struct Adapter<
'd,
M,
T,
const CONNS: usize,
const CHANNELS: usize,
const L2CAP_TXQ: usize = 1,
const L2CAP_RXQ: usize = 1,
> where
M: RawMutex,
{
pub(crate) controller: T,
Expand Down

0 comments on commit 79030bf

Please sign in to comment.