Skip to content

Commit

Permalink
Merge pull request #20 from embassy-rs/signal-refactor
Browse files Browse the repository at this point in the history
Signal refactor
  • Loading branch information
lulf authored Apr 22, 2024
2 parents 3aa84ad + 9ee4cb8 commit fd8ecc7
Show file tree
Hide file tree
Showing 8 changed files with 280 additions and 322 deletions.
16 changes: 13 additions & 3 deletions examples/nrf-sdc/src/bin/ble_l2cap_central.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ 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;
use trouble_host::l2cap::{L2capChannel, L2capChannelConfig};
use trouble_host::scan::ScanConfig;
use trouble_host::{Address, PacketQos};
use {defmt_rtt as _, panic_probe as _};
Expand Down Expand Up @@ -139,8 +139,18 @@ async fn main(spawner: Spawner) {
let conn = unwrap!(adapter.connect(&config).await);
info!("Connected, creating l2cap channel");
const PAYLOAD_LEN: usize = 27;
let mut ch1 =
unwrap!(L2capChannel::create(&adapter, &conn, 0x2349, PAYLOAD_LEN as u16, Default::default()).await);
let mut ch1 = unwrap!(
L2capChannel::create(
&adapter,
&conn,
0x2349,
&L2capChannelConfig {
mtu: PAYLOAD_LEN as u16,
..Default::default()
}
)
.await
);
info!("New l2cap channel created, sending some data!");
for i in 0..10 {
let tx = [i; PAYLOAD_LEN];
Expand Down
16 changes: 13 additions & 3 deletions examples/nrf-sdc/src/bin/ble_l2cap_peripheral.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ 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;
use trouble_host::l2cap::{L2capChannel, L2capChannelConfig};
use trouble_host::{Address, PacketQos};
use {defmt_rtt as _, panic_probe as _};

Expand Down Expand Up @@ -149,8 +149,18 @@ async fn main(spawner: Spawner) {

info!("Connection established");

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

info!("L2CAP channel accepted");

Expand Down
53 changes: 27 additions & 26 deletions host/src/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use bt_hci::param::{
AddrKind, AdvChannelMap, AdvHandle, AdvKind, BdAddr, ConnHandle, DisconnectReason, EventMask, FilterDuplicates,
InitiatingPhy, LeEventMask, Operation, PhyParams, ScanningPhy,
};
use bt_hci::ControllerToHostPacket;
use bt_hci::{ControllerToHostPacket, FromHciBytes, WriteHci};
use embassy_futures::select::{select, Either};
use embassy_sync::blocking_mutex::raw::RawMutex;
use embassy_sync::channel::Channel;
Expand All @@ -29,12 +29,14 @@ use crate::advertise::{Advertisement, AdvertisementConfig, RawAdvertisement};
use crate::channel_manager::ChannelManager;
use crate::connection::{ConnectConfig, Connection};
use crate::connection_manager::{ConnectionInfo, ConnectionManager};
use crate::cursor::{ReadCursor, WriteCursor};
use crate::cursor::WriteCursor;
use crate::l2cap::sar::PacketReassembly;
use crate::packet_pool::{AllocId, DynamicPacketPool, PacketPool, Qos};
use crate::pdu::Pdu;
use crate::scan::{PhySet, ScanConfig, ScanReport};
use crate::types::l2cap::{L2capHeader, L2capLeSignal, L2CAP_CID_ATT, L2CAP_CID_DYN_START, L2CAP_CID_LE_U_SIGNAL};
use crate::types::l2cap::{
L2capHeader, L2capSignal, L2capSignalHeader, L2CAP_CID_ATT, L2CAP_CID_DYN_START, L2CAP_CID_LE_U_SIGNAL,
};
#[cfg(feature = "gatt")]
use crate::{attribute::AttributeTable, gatt::GattServer};
use crate::{AdapterError, Address, Error};
Expand Down Expand Up @@ -530,14 +532,12 @@ where
async fn handle_acl(&self, acl: AclPacket<'_>) -> Result<(), Error> {
let (header, packet) = match acl.boundary_flag() {
AclPacketBoundary::FirstFlushable => {
let (header, data) = L2capHeader::decode(&acl)?;
let (header, data) = L2capHeader::from_hci_bytes(acl.data())?;

// Avoids using the packet buffer for signalling packets
if header.channel == L2CAP_CID_LE_U_SIGNAL {
assert!(data.len() == header.length as usize);
let mut r = ReadCursor::new(data);
let signal: L2capLeSignal = r.read()?;
self.channels.control(acl.handle(), signal).await?;
self.channels.control(acl.handle(), &data).await?;
return Ok(());
}

Expand Down Expand Up @@ -851,28 +851,29 @@ impl<'d, T: Controller> HciController<'d, T> {
Ok(())
}

pub(crate) async fn signal(
pub(crate) async fn signal<D: L2capSignal>(
&self,
handle: ConnHandle,
response: &L2capLeSignal,
identifier: u8,
signal: &D,
p_buf: &mut [u8],
) -> Result<(), AdapterError<T::Error>> {
// TODO: Refactor signal to avoid encode/decode
// info!("[{}] sending signal: {:?}", handle, response);
let mut tx = [0; 32];
let mut w = WriteCursor::new(&mut tx);
let (mut header, mut body) = w.split(4)?;

body.write_ref(response)?;

// TODO: Move into l2cap packet type
header.write(body.len() as u16)?;
header.write(L2CAP_CID_LE_U_SIGNAL)?;
let len = header.len() + body.len();

header.finish();
body.finish();
w.finish();
self.send(handle, &tx[..len]).await?;
let header = L2capSignalHeader {
identifier,
code: D::code(),
length: signal.size() as u16,
};
let l2cap = L2capHeader {
channel: D::channel(),
length: header.size() as u16 + header.length,
};

let mut w = WriteCursor::new(p_buf);
w.write_hci(&l2cap)?;
w.write_hci(&header)?;
w.write_hci(signal)?;

self.send(handle, w.finish()).await?;

Ok(())
}
Expand Down
Loading

0 comments on commit fd8ecc7

Please sign in to comment.