Skip to content

Commit

Permalink
Merge pull request #36 from embassy-rs/less-generics-2
Browse files Browse the repository at this point in the history
Less generics 2
  • Loading branch information
lulf authored May 14, 2024
2 parents f502f64 + 8856e71 commit f5b0dff
Show file tree
Hide file tree
Showing 20 changed files with 768 additions and 952 deletions.
12 changes: 11 additions & 1 deletion .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,17 @@ jobs:
run: |
cd host
cargo test --test '*' -- --nocapture
- name: Update status
- name: Update failed status
if: failure()
env:
COMMIT: ${{ steps.checkout.outputs.commit }}
GH_TOKEN: ${{ github.token }}
run: |
gh api --method POST -H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" /repos/embassy-rs/trouble/statuses/${COMMIT} \
-f "state=failure" -f "description=The build failed" -f "context=tests"
- name: Update success status
if: success()
env:
COMMIT: ${{ steps.checkout.outputs.commit }}
GH_TOKEN: ${{ github.token }}
Expand Down
8 changes: 4 additions & 4 deletions examples/nrf-sdc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ embassy-executor = { version = "0.5", default-features = false, features = ["nig
embassy-time = { version = "0.3.0", default-features = false, features = ["defmt", "defmt-timestamp-uptime"] }
embassy-nrf = { version = "0.1.0", default-features = false, features = ["defmt", "nrf52833", "time-driver-rtc1", "gpiote", "unstable-pac", "rt"] }
embassy-futures = "0.1.1"
embassy-sync = "0.5"
embassy-sync = { version = "0.5", features = ["defmt"] }

futures = { version = "0.3", default-features = false, features = ["async-await"]}
nrf-sdc = { version = "0.1.0", default-features = false, features = ["defmt", "nrf52833", "peripheral", "central"] }
Expand All @@ -36,9 +36,9 @@ embassy-futures = { git = "https://github.com/embassy-rs/embassy.git", branch =
embassy-time = { git = "https://github.com/embassy-rs/embassy.git", branch = "main" }
embassy-time-driver = { git = "https://github.com/embassy-rs/embassy.git", branch = "main" }
embassy-embedded-hal = { git = "https://github.com/embassy-rs/embassy.git", branch = "main" }
nrf-sdc = { git = "https://github.com/alexmoon/nrf-sdc.git", branch = "try-controller" }
nrf-mpsl = { git = "https://github.com/alexmoon/nrf-sdc.git", branch = "try-controler" }
bt-hci = { git = "https://github.com/alexmoon/bt-hci.git", branch = "try-controller" }
nrf-sdc = { git = "https://github.com/alexmoon/nrf-sdc.git", branch = "main" }
nrf-mpsl = { git = "https://github.com/alexmoon/nrf-sdc.git", branch = "main" }
bt-hci = { git = "https://github.com/alexmoon/bt-hci.git", branch = "main" }

#embassy-executor = {path = "../../../embassy/embassy-executor"}
#embassy-nrf = {path = "../../../embassy/embassy-nrf"}
Expand Down
36 changes: 17 additions & 19 deletions examples/nrf-sdc/src/bin/ble_bas_peripheral.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ use nrf_sdc::mpsl::MultiprotocolServiceLayer;
use nrf_sdc::{self as sdc, mpsl};
use sdc::rng_pool::RngPool;
use static_cell::StaticCell;
use trouble_host::adapter::{Adapter, HostResources};
use trouble_host::advertise::{AdStructure, Advertisement, BR_EDR_NOT_SUPPORTED, LE_GENERAL_DISCOVERABLE};
use trouble_host::attribute::{AttributeTable, CharacteristicProp, Service, Uuid};
use trouble_host::{Address, PacketQos};
use trouble_host::{Address, BleHost, BleHostResources, PacketQos};
use {defmt_rtt as _, panic_probe as _};

bind_interrupts!(struct Irqs {
Expand Down Expand Up @@ -106,13 +105,13 @@ async fn main(spawner: Spawner) {
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));
static HOST_RESOURCES: StaticCell<
BleHostResources<CONNECTIONS_MAX, L2CAP_CHANNELS_MAX, PACKET_POOL_SIZE, L2CAP_MTU>,
> = StaticCell::new();
let host_resources = HOST_RESOURCES.init(BleHostResources::new(PacketQos::None));

let mut adapter: Adapter<'_, NoopRawMutex, _, CONNECTIONS_MAX, L2CAP_CHANNELS_MAX, L2CAP_MTU> =
Adapter::new(sdc, host_resources);
adapter.set_random_address(my_addr());
let mut ble: BleHost<'_, _> = BleHost::new(sdc, host_resources);
ble.set_random_address(my_addr());

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

Expand All @@ -139,7 +138,7 @@ async fn main(spawner: Spawner) {
)
};

let server = adapter.gatt_server(&table);
let server = ble.gatt_server(&table);
let mut adv_data = [0; 31];
unwrap!(AdStructure::encode_slice(
&[
Expand All @@ -152,7 +151,7 @@ async fn main(spawner: Spawner) {

info!("Starting advertising and GATT service");
let _ = join3(
adapter.run(),
ble.run(),
async {
loop {
match server.next().await {
Expand All @@ -167,15 +166,14 @@ async fn main(spawner: Spawner) {
},
async {
let conn = unwrap!(
adapter
.advertise(
&Default::default(),
Advertisement::ConnectableScannableUndirected {
adv_data: &adv_data[..],
scan_data: &[],
}
)
.await
ble.advertise(
&Default::default(),
Advertisement::ConnectableScannableUndirected {
adv_data: &adv_data[..],
scan_data: &[],
}
)
.await
);
// Keep connection alive
let mut tick: u8 = 0;
Expand Down
39 changes: 13 additions & 26 deletions examples/nrf-sdc/src/bin/ble_l2cap_central.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,15 @@ use defmt::{info, unwrap};
use embassy_executor::Spawner;
use embassy_futures::join::join;
use embassy_nrf::{bind_interrupts, pac};
use embassy_sync::blocking_mutex::raw::NoopRawMutex;
use embassy_time::{Duration, Timer};
use nrf_sdc::mpsl::MultiprotocolServiceLayer;
use nrf_sdc::{self as sdc, mpsl};
use sdc::rng_pool::RngPool;
use static_cell::StaticCell;
use trouble_host::adapter::{Adapter, HostResources};
use trouble_host::connection::ConnectConfig;
use trouble_host::l2cap::{L2capChannel, L2capChannelConfig};
use trouble_host::l2cap::L2capChannel;
use trouble_host::scan::ScanConfig;
use trouble_host::{Address, PacketQos};
use trouble_host::{Address, BleHost, BleHostResources, PacketQos};
use {defmt_rtt as _, panic_probe as _};

bind_interrupts!(struct Irqs {
Expand Down Expand Up @@ -114,13 +112,13 @@ async fn main(spawner: Spawner) {
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::Guaranteed(4)));
static HOST_RESOURCES: StaticCell<
BleHostResources<CONNECTIONS_MAX, L2CAP_CHANNELS_MAX, PACKET_POOL_SIZE, L2CAP_MTU>,
> = StaticCell::new();
let host_resources = HOST_RESOURCES.init(BleHostResources::new(PacketQos::Guaranteed(4)));

let mut adapter: Adapter<'_, NoopRawMutex, _, CONNECTIONS_MAX, L2CAP_CHANNELS_MAX, L2CAP_MTU> =
Adapter::new(sdc, host_resources);
adapter.set_random_address(my_addr());
let mut ble: BleHost<'_, _> = BleHost::new(sdc, host_resources);
ble.set_random_address(my_addr());

// NOTE: Modify this to match the address of the peripheral you want to connect to
let target: Address = Address::random([0xf5, 0x9f, 0x1a, 0x05, 0xe4, 0xee]);
Expand All @@ -134,32 +132,21 @@ async fn main(spawner: Spawner) {
};

info!("Scanning for peripheral...");
let _ = join(adapter.run(), async {
let _ = join(ble.run(), async {
loop {
let conn = unwrap!(adapter.connect(&config).await);
let conn = unwrap!(ble.connect(&config).await);
info!("Connected, creating l2cap channel");
const PAYLOAD_LEN: usize = 27;
let mut ch1 = unwrap!(
L2capChannel::create(
&adapter,
&conn,
0x2349,
&L2capChannelConfig {
mtu: PAYLOAD_LEN as u16,
..Default::default()
}
)
.await
);
let mut ch1 = unwrap!(L2capChannel::<PAYLOAD_LEN>::create(&ble, &conn, 0x2349, &Default::default(),).await);
info!("New l2cap channel created, sending some data!");
for i in 0..10 {
let tx = [i; PAYLOAD_LEN];
unwrap!(ch1.send(&adapter, &tx).await);
unwrap!(ch1.send(&ble, &tx).await);
}
info!("Sent data, waiting for them to be sent back");
let mut rx = [0; PAYLOAD_LEN];
for i in 0..10 {
let len = unwrap!(ch1.receive(&adapter, &mut rx).await);
let len = unwrap!(ch1.receive(&ble, &mut rx).await);
assert_eq!(len, rx.len());
assert_eq!(rx, [i; PAYLOAD_LEN]);
}
Expand Down
55 changes: 21 additions & 34 deletions examples/nrf-sdc/src/bin/ble_l2cap_peripheral.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,14 @@ use defmt::{info, unwrap};
use embassy_executor::Spawner;
use embassy_futures::join::join;
use embassy_nrf::{bind_interrupts, pac};
use embassy_sync::blocking_mutex::raw::NoopRawMutex;
use embassy_time::{Duration, Timer};
use nrf_sdc::mpsl::MultiprotocolServiceLayer;
use nrf_sdc::{self as sdc, mpsl};
use sdc::rng_pool::RngPool;
use static_cell::StaticCell;
use trouble_host::adapter::{Adapter, HostResources};
use trouble_host::advertise::{AdStructure, Advertisement, BR_EDR_NOT_SUPPORTED, LE_GENERAL_DISCOVERABLE};
use trouble_host::l2cap::{L2capChannel, L2capChannelConfig};
use trouble_host::{Address, PacketQos};
use trouble_host::l2cap::L2capChannel;
use trouble_host::{Address, BleHost, BleHostResources, PacketQos};
use {defmt_rtt as _, panic_probe as _};

bind_interrupts!(struct Irqs {
Expand Down Expand Up @@ -113,13 +111,13 @@ async fn main(spawner: Spawner) {
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::Guaranteed(4)));
static HOST_RESOURCES: StaticCell<
BleHostResources<CONNECTIONS_MAX, L2CAP_CHANNELS_MAX, PACKET_POOL_SIZE, L2CAP_MTU>,
> = StaticCell::new();
let host_resources = HOST_RESOURCES.init(BleHostResources::new(PacketQos::Guaranteed(4)));

let mut adapter: Adapter<'_, NoopRawMutex, _, CONNECTIONS_MAX, L2CAP_CHANNELS_MAX, L2CAP_MTU> =
Adapter::new(sdc, host_resources);
adapter.set_random_address(my_addr());
let mut ble: BleHost<'_, _> = BleHost::new(sdc, host_resources);
ble.set_random_address(my_addr());
let mut adv_data = [0; 31];
unwrap!(AdStructure::encode_slice(
&[AdStructure::Flags(LE_GENERAL_DISCOVERABLE | BR_EDR_NOT_SUPPORTED),],
Expand All @@ -132,43 +130,32 @@ async fn main(spawner: Spawner) {
&mut scan_data[..],
));

let _ = join(adapter.run(), async {
let _ = join(ble.run(), async {
loop {
info!("Advertising, waiting for connection...");
let conn = unwrap!(
adapter
.advertise(
&Default::default(),
Advertisement::ConnectableScannableUndirected {
adv_data: &adv_data[..],
scan_data: &scan_data[..],
}
)
.await
);

info!("Connection established");

let mut ch1 = unwrap!(
L2capChannel::accept(
&adapter,
&conn,
&[0x2349],
&L2capChannelConfig {
mtu: PAYLOAD_LEN as u16,
..Default::default()
ble.advertise(
&Default::default(),
Advertisement::ConnectableScannableUndirected {
adv_data: &adv_data[..],
scan_data: &scan_data[..],
}
)
.await
);

info!("Connection established");

let mut ch1 =
unwrap!(L2capChannel::<PAYLOAD_LEN>::accept(&ble, &conn, &[0x2349], &Default::default(),).await);

info!("L2CAP channel accepted");

// 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(&adapter, &mut rx).await);
let len = unwrap!(ch1.receive(&ble, &mut rx).await);
assert_eq!(len, rx.len());
assert_eq!(rx, [i; PAYLOAD_LEN]);
}
Expand All @@ -177,7 +164,7 @@ async fn main(spawner: Spawner) {
Timer::after(Duration::from_secs(1)).await;
for i in 0..10 {
let tx = [i; PAYLOAD_LEN];
unwrap!(ch1.send(&adapter, &tx).await);
unwrap!(ch1.send(&ble, &tx).await);
}
info!("L2CAP data echoed");

Expand Down
2 changes: 1 addition & 1 deletion examples/serial-hci/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ bt-hci = { version = "0.1.0", default-features = false, features = ["log"] }
trouble-host = { version = "0.1.0", path = "../../host", features = ["log", "gatt"] }

[patch.crates-io]
bt-hci = { git = "https://github.com/alexmoon/bt-hci.git", branch = "try-controller" }
bt-hci = { git = "https://github.com/alexmoon/bt-hci.git", branch = "main" }
embassy-sync = { git = "https://github.com/embassy-rs/embassy.git", branch = "main" }
#embassy-executor = { git = "https://github.com/embassy-rs/embassy.git", branch = "main" }
#bt-hci = { path = "../../../bt-hci" }
17 changes: 8 additions & 9 deletions examples/serial-hci/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ use log::*;
use static_cell::StaticCell;
use tokio::time::Duration;
use tokio_serial::{DataBits, Parity, SerialStream, StopBits};
use trouble_host::adapter::{Adapter, HostResources};
use trouble_host::advertise::{AdStructure, Advertisement, BR_EDR_NOT_SUPPORTED, LE_GENERAL_DISCOVERABLE};
use trouble_host::attribute::{AttributeTable, CharacteristicProp, Service, Uuid};
use trouble_host::{Address, PacketQos};
use trouble_host::{Address, BleHost, BleHostResources, PacketQos};

#[tokio::main]
async fn main() {
Expand Down Expand Up @@ -55,12 +54,12 @@ async fn main() {

let driver: SerialTransport<NoopRawMutex, _, _> = SerialTransport::new(reader, writer);
let controller: ExternalController<_, 10> = ExternalController::new(driver);
static HOST_RESOURCES: StaticCell<HostResources<NoopRawMutex, 4, 32, 27>> = StaticCell::new();
let host_resources = HOST_RESOURCES.init(HostResources::new(PacketQos::None));
static HOST_RESOURCES: StaticCell<BleHostResources<2, 4, 32, 27>> = StaticCell::new();
let host_resources = HOST_RESOURCES.init(BleHostResources::new(PacketQos::None));

let mut adapter: Adapter<'_, NoopRawMutex, _, 2, 4, 27, 1, 1> = Adapter::new(controller, host_resources);
let mut ble: BleHost<'_, _> = BleHost::new(controller, host_resources);

adapter.set_random_address(Address::random([0xff, 0x9f, 0x1a, 0x05, 0xe4, 0xff]));
ble.set_random_address(Address::random([0xff, 0x9f, 0x1a, 0x05, 0xe4, 0xff]));
let mut table: AttributeTable<'_, NoopRawMutex, 10> = AttributeTable::new();

// Generic Access Service (mandatory)
Expand Down Expand Up @@ -97,11 +96,11 @@ async fn main() {
)
.unwrap();

let server = adapter.gatt_server(&table);
let server = ble.gatt_server(&table);

info!("Starting advertising and GATT service");
let _ = join3(
adapter.run(),
ble.run(),
async {
loop {
match server.next().await {
Expand All @@ -115,7 +114,7 @@ async fn main() {
}
},
async {
let conn = adapter
let conn = ble
.advertise(
&Default::default(),
Advertisement::ConnectableScannableUndirected {
Expand Down
3 changes: 2 additions & 1 deletion host/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,12 @@ embedded-io-async = { version = "0.6.1" }
tokio-serial = "5.4"
env_logger = "0.11"
critical-section = { version = "1", features = ["std"] }
static_cell = "2.1.0"

[features]
defmt = [ "dep:defmt" ]
gatt = []

[patch.crates-io]
bt-hci = { git = "https://github.com/alexmoon/bt-hci.git", branch = "try-controller" }
bt-hci = { git = "https://github.com/alexmoon/bt-hci.git", branch = "main" }
embassy-sync = { git = "https://github.com/embassy-rs/embassy.git", branch = "main" }
2 changes: 1 addition & 1 deletion host/src/attribute_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ impl<'c, 'd, M: RawMutex, const MAX: usize> AttributeServer<'c, 'd, M, MAX> {
)?)
}

/// Process an adapter event and produce a response if necessary
/// Process an event and produce a response if necessary
pub fn process(&self, conn: ConnHandle, packet: Att, rx: &mut [u8]) -> Result<Option<usize>, AttributeServerError> {
let len = match packet {
Att::ReadByTypeReq {
Expand Down
Loading

0 comments on commit f5b0dff

Please sign in to comment.