From 6d6ba56e98ad9c5d15d75cb24d7e88f80c259b45 Mon Sep 17 00:00:00 2001 From: Ulf Lilleengen Date: Thu, 18 Apr 2024 16:29:28 +0200 Subject: [PATCH] Support propagating vendor events to an external handler --- .../nrf-sdc/src/bin/ble_bas_peripheral.rs | 2 +- examples/nrf-sdc/src/bin/ble_l2cap_central.rs | 2 +- .../nrf-sdc/src/bin/ble_l2cap_peripheral.rs | 2 +- host/src/adapter.rs | 48 ++++++++++++++----- host/src/advertise.rs | 4 +- host/src/channel_manager.rs | 2 +- host/src/gatt.rs | 4 +- 7 files changed, 45 insertions(+), 19 deletions(-) diff --git a/examples/nrf-sdc/src/bin/ble_bas_peripheral.rs b/examples/nrf-sdc/src/bin/ble_bas_peripheral.rs index 19bbdc3..c930332 100644 --- a/examples/nrf-sdc/src/bin/ble_bas_peripheral.rs +++ b/examples/nrf-sdc/src/bin/ble_bas_peripheral.rs @@ -114,7 +114,7 @@ async fn main(spawner: Spawner) { let mut adapter: Adapter<'_, NoopRawMutex, _, CONNECTIONS_MAX, L2CAP_CHANNELS_MAX> = Adapter::new(sdc, host_resources); - unwrap!(adapter.set_random_address(my_addr()).await); + adapter.set_random_address(my_addr()); let mut table: AttributeTable<'_, NoopRawMutex, 10> = AttributeTable::new(); diff --git a/examples/nrf-sdc/src/bin/ble_l2cap_central.rs b/examples/nrf-sdc/src/bin/ble_l2cap_central.rs index e48687d..c95c2d4 100644 --- a/examples/nrf-sdc/src/bin/ble_l2cap_central.rs +++ b/examples/nrf-sdc/src/bin/ble_l2cap_central.rs @@ -122,7 +122,7 @@ async fn main(spawner: Spawner) { let mut adapter: Adapter<'_, NoopRawMutex, _, CONNECTIONS_MAX, L2CAP_CHANNELS_MAX> = Adapter::new(sdc, host_resources); - unwrap!(adapter.set_random_address(my_addr()).await); + adapter.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]); diff --git a/examples/nrf-sdc/src/bin/ble_l2cap_peripheral.rs b/examples/nrf-sdc/src/bin/ble_l2cap_peripheral.rs index ee061e8..a8ca0e8 100644 --- a/examples/nrf-sdc/src/bin/ble_l2cap_peripheral.rs +++ b/examples/nrf-sdc/src/bin/ble_l2cap_peripheral.rs @@ -121,7 +121,7 @@ async fn main(spawner: Spawner) { let mut adapter: Adapter<'_, NoopRawMutex, _, CONNECTIONS_MAX, L2CAP_CHANNELS_MAX> = Adapter::new(sdc, host_resources); - unwrap!(adapter.set_random_address(my_addr()).await); + adapter.set_random_address(my_addr()); let mut adv_data = [0; 31]; unwrap!(AdStructure::encode_slice( &[AdStructure::Flags(LE_GENERAL_DISCOVERABLE | BR_EDR_NOT_SUPPORTED),], diff --git a/host/src/adapter.rs b/host/src/adapter.rs index 137e58c..7cb8802 100644 --- a/host/src/adapter.rs +++ b/host/src/adapter.rs @@ -23,10 +23,10 @@ use bt_hci::controller::{CmdError, Controller}; use bt_hci::controller::{ControllerCmdAsync, ControllerCmdSync}; use bt_hci::data::{AclBroadcastFlag, AclPacket, AclPacketBoundary}; use bt_hci::event::le::LeEvent; -use bt_hci::event::Event; +use bt_hci::event::{Event, Vendor}; use bt_hci::param::{ - AddrKind, AdvHandle, AdvKind, BdAddr, ConnHandle, DisconnectReason, EventMask, FilterDuplicates, InitiatingPhy, - LeEventMask, Operation, PhyParams, ScanningPhy, + AddrKind, AdvChannelMap, AdvHandle, AdvKind, BdAddr, ConnHandle, DisconnectReason, EventMask, FilterDuplicates, + InitiatingPhy, LeEventMask, Operation, PhyParams, ScanningPhy, }; use bt_hci::ControllerToHostPacket; use core::future::pending; @@ -54,6 +54,11 @@ impl); +} + pub struct Adapter< 'd, M, @@ -188,8 +193,7 @@ where .exec(&self.controller) .await?; let info = self.connections.accept(config.scan_config.filter_accept_list).await; - let _ = LeCreateConnCancel::new().exec(&self.controller).await; - return Ok(Connection { info }); + Ok(Connection { info }) } else { LeCreateConn::new( config.scan_config.interval.into(), @@ -208,8 +212,7 @@ where .exec(&self.controller) .await?; let info = self.connections.accept(config.scan_config.filter_accept_list).await; - let _ = LeCreateConnCancel::new().exec(&self.controller).await; - return Ok(Connection { info }); + Ok(Connection { info }) } } @@ -377,7 +380,7 @@ where self.address.map(|a| a.kind).unwrap_or(AddrKind::RANDOM), peer.kind, peer.addr, - config.channel_map, + config.channel_map.unwrap_or(AdvChannelMap::ALL), config.filter_policy, ) .exec(&self.controller) @@ -446,7 +449,7 @@ where params.props, config.interval_min.into(), config.interval_max.into(), - config.channel_map, + config.channel_map.unwrap_or(AdvChannelMap::ALL), self.address.map(|a| a.kind).unwrap_or(AddrKind::RANDOM), peer.kind, peer.addr, @@ -571,6 +574,24 @@ where } pub async fn run(&self) -> Result<(), AdapterError> + where + T: ControllerCmdSync + + ControllerCmdSync + + ControllerCmdSync + + ControllerCmdSync + + ControllerCmdSync + + ControllerCmdSync + // + ControllerCmdSync + // + ControllerCmdSync + + ControllerCmdSync, + { + self.run_with_handler(None).await + } + + pub async fn run_with_handler( + &self, + vendor_handler: Option<&dyn VendorEventHandler>, + ) -> Result<(), AdapterError> where T: ControllerCmdSync + ControllerCmdSync @@ -696,7 +717,7 @@ where .await; } _ => { - error!("Unknown event: {:?}", event); + warn!("Unknown LE event!"); } }, Event::DisconnectionComplete(e) => { @@ -709,8 +730,13 @@ where // trace!("Confirmed {} packets sent", c.completed_packets.len()); self.permits.release(c.completed_packets.len()); } + Event::Vendor(vendor) => { + if let Some(handler) = vendor_handler { + handler.on_event(&vendor); + } + } _ => { - warn!("Unknown event: {:?}", event); + warn!("Unknown event"); } }, Ok(p) => { diff --git a/host/src/advertise.rs b/host/src/advertise.rs index b18fa2c..8ff6281 100644 --- a/host/src/advertise.rs +++ b/host/src/advertise.rs @@ -43,7 +43,7 @@ pub struct AdvertisementConfig { pub interval_min: Duration, pub interval_max: Duration, - pub channel_map: AdvChannelMap, + pub channel_map: Option, pub filter_policy: AdvFilterPolicy, } @@ -58,7 +58,7 @@ impl Default for AdvertisementConfig { interval_min: Duration::from_millis(250), interval_max: Duration::from_millis(250), filter_policy: AdvFilterPolicy::default(), - channel_map: AdvChannelMap::ALL, + channel_map: None, } } } diff --git a/host/src/channel_manager.rs b/host/src/channel_manager.rs index 64041b7..88a08d6 100644 --- a/host/src/channel_manager.rs +++ b/host/src/channel_manager.rs @@ -73,7 +73,7 @@ impl<'d, M: RawMutex, const CHANNELS: usize, const L2CAP_TXQ: usize, const L2CAP let mut state = state.borrow_mut(); // 0 is an invalid identifier if state.next_req_id == 0 { - state.next_req_id = state.next_req_id + 1; + state.next_req_id += 1; } let next = state.next_req_id; state.next_req_id = state.next_req_id.wrapping_add(1); diff --git a/host/src/gatt.rs b/host/src/gatt.rs index 33e9799..a1ad684 100644 --- a/host/src/gatt.rs +++ b/host/src/gatt.rs @@ -83,7 +83,7 @@ impl<'reference, 'values, 'resources, M: RawMutex, T: Controller, const MAX: usi pub async fn notify( &self, handle: CharacteristicHandle, - connection: &Connection<'_>, + connection: &Connection, value: &[u8], ) -> Result<(), AdapterError> { let conn = connection.handle(); @@ -116,7 +116,7 @@ impl<'reference, 'values, 'resources, M: RawMutex, T: Controller, const MAX: usi #[derive(Clone)] pub enum GattEvent<'reference, 'values> { Write { - connection: Connection<'reference>, + connection: &'reference Connection, handle: CharacteristicHandle, value: &'values [u8], },