acl: AclHal,
rt: Arc<Runtime>,
) -> HciFacadeService {
- let (from_hci_evt_tx, to_grpc_evt_rx) = channel::<EventPacket>(10);
- let (from_hci_le_evt_tx, to_grpc_le_evt_rx) = channel::<LeMetaEventPacket>(10);
+ let (evt_tx, evt_rx) = channel::<EventPacket>(10);
+ let (le_evt_tx, le_evt_rx) = channel::<LeMetaEventPacket>(10);
HciFacadeService {
commands,
events,
acl,
rt,
- from_hci_evt_tx,
- to_grpc_evt_rx: Arc::new(Mutex::new(to_grpc_evt_rx)),
- from_hci_le_evt_tx,
- to_grpc_le_evt_rx: Arc::new(Mutex::new(to_grpc_le_evt_rx)),
+ evt_tx,
+ evt_rx: Arc::new(Mutex::new(evt_rx)),
+ le_evt_tx,
+ le_evt_rx: Arc::new(Mutex::new(le_evt_rx)),
}
}
/// HCI layer facade service
+#[allow(missing_docs)]
#[derive(Clone, Stoppable)]
pub struct HciFacadeService {
- commands: RawCommandSender,
+ pub commands: RawCommandSender,
events: EventRegistry,
- acl: AclHal,
+ pub acl: AclHal,
rt: Arc<Runtime>,
- from_hci_evt_tx: Sender<EventPacket>,
- to_grpc_evt_rx: Arc<Mutex<Receiver<EventPacket>>>,
- from_hci_le_evt_tx: Sender<LeMetaEventPacket>,
- to_grpc_le_evt_rx: Arc<Mutex<Receiver<LeMetaEventPacket>>>,
+ evt_tx: Sender<EventPacket>,
+ pub evt_rx: Arc<Mutex<Receiver<EventPacket>>>,
+ le_evt_tx: Sender<LeMetaEventPacket>,
+ pub le_evt_rx: Arc<Mutex<Receiver<LeMetaEventPacket>>>,
+}
+
+impl HciFacadeService {
+ /// Register for the event & plug in the channel to get them back on
+ pub async fn register_event(&self, code: u32) {
+ self.events.clone().register(EventCode::from_u32(code).unwrap(), self.evt_tx.clone()).await;
+ }
+
+ /// Register for the le event & plug in the channel to get them back on
+ pub async fn register_le_event(&self, code: u32) {
+ self.events
+ .clone()
+ .register_le(SubeventCode::from_u32(code).unwrap(), self.le_evt_tx.clone())
+ .await;
+ }
}
impl GrpcFacade for HciFacadeService {
}
fn request_event(&mut self, _ctx: RpcContext<'_>, req: EventRequest, sink: UnarySink<Empty>) {
- self.rt.block_on(
- self.events.register(
- EventCode::from_u32(req.get_code()).unwrap(),
- self.from_hci_evt_tx.clone(),
- ),
- );
+ self.rt.block_on(self.register_event(req.get_code()));
sink.success(Empty::default());
}
req: EventRequest,
sink: UnarySink<Empty>,
) {
- self.rt.block_on(self.events.register_le(
- SubeventCode::from_u32(req.get_code()).unwrap(),
- self.from_hci_le_evt_tx.clone(),
- ));
+ self.rt.block_on(self.register_le_event(req.get_code()));
sink.success(Empty::default());
}
_req: Empty,
mut resp: ServerStreamingSink<Data>,
) {
- let evt_rx = self.to_grpc_evt_rx.clone();
+ let evt_rx = self.evt_rx.clone();
self.rt.spawn(async move {
while let Some(event) = evt_rx.lock().await.recv().await {
_req: Empty,
mut resp: ServerStreamingSink<Data>,
) {
- let evt_rx = self.to_grpc_le_evt_rx.clone();
+ let evt_rx = self.le_evt_rx.clone();
self.rt.spawn(async move {
while let Some(event) = evt_rx.lock().await.recv().await {
//! Hci shim
-use bt_hal::AclHal;
-use bt_hci::{EventRegistry, RawCommandSender};
-use bt_packets::hci::{
- AclPacket, CommandPacket, EventCode, EventPacket, LeMetaEventPacket, SubeventCode,
-};
-use num_traits::FromPrimitive;
+use bt_hci::facade::HciFacadeService;
+use bt_packets::hci::{AclPacket, CommandPacket};
use std::sync::Arc;
use tokio::runtime::Runtime;
-use tokio::sync::mpsc::{channel, Receiver, Sender};
-use tokio::sync::Mutex;
#[cxx::bridge(namespace = bluetooth::shim::rust)]
mod ffi {
unsafe impl Send for ffi::u8SliceOnceCallback {}
pub struct Hci {
- commands: RawCommandSender,
- events: EventRegistry,
- acl: AclHal,
+ internal: HciFacadeService,
rt: Arc<Runtime>,
acl_callback_set: bool,
evt_callback_set: bool,
le_evt_callback_set: bool,
- evt_tx: Sender<EventPacket>,
- evt_rx: Arc<Mutex<Receiver<EventPacket>>>,
- le_evt_tx: Sender<LeMetaEventPacket>,
- le_evt_rx: Arc<Mutex<Receiver<LeMetaEventPacket>>>,
}
impl Hci {
- pub fn new(
- rt: Arc<Runtime>,
- commands: RawCommandSender,
- events: EventRegistry,
- acl: AclHal,
- ) -> Self {
- let (evt_tx, evt_rx) = channel::<EventPacket>(10);
- let (le_evt_tx, le_evt_rx) = channel::<LeMetaEventPacket>(10);
+ pub fn new(rt: Arc<Runtime>, internal: HciFacadeService) -> Self {
Self {
rt,
- commands,
- events,
- acl,
+ internal,
acl_callback_set: false,
evt_callback_set: false,
le_evt_callback_set: false,
- evt_tx,
- evt_rx: Arc::new(Mutex::new(evt_rx)),
- le_evt_tx,
- le_evt_rx: Arc::new(Mutex::new(le_evt_rx)),
}
}
}
callback: cxx::UniquePtr<ffi::u8SliceOnceCallback>,
) {
let packet = CommandPacket::parse(data).unwrap();
- let mut commands = hci.commands.clone();
+ let mut commands = hci.internal.commands.clone();
hci.rt.spawn(async move {
let resp = commands.send(packet).await.unwrap();
callback.Run(&resp.to_bytes());
}
pub fn hci_send_acl(hci: &mut Hci, data: &[u8]) {
- hci.rt.block_on(hci.acl.tx.send(AclPacket::parse(data).unwrap())).unwrap();
+ hci.rt.block_on(hci.internal.acl.tx.send(AclPacket::parse(data).unwrap())).unwrap();
}
pub fn hci_register_event(hci: &mut Hci, event: u8) {
- hci.rt.block_on(hci.events.register(EventCode::from_u8(event).unwrap(), hci.evt_tx.clone()));
+ hci.rt.block_on(hci.internal.register_event(event.into()));
}
pub fn hci_register_le_event(hci: &mut Hci, subevent: u8) {
- hci.rt.block_on(
- hci.events.register_le(SubeventCode::from_u8(subevent).unwrap(), hci.le_evt_tx.clone()),
- );
+ hci.rt.block_on(hci.internal.register_le_event(subevent.into()));
}
pub fn hci_set_acl_callback(hci: &mut Hci, callback: cxx::UniquePtr<ffi::u8SliceCallback>) {
assert!(!hci.acl_callback_set);
hci.acl_callback_set = true;
- let stream = hci.acl.rx.clone();
+ let stream = hci.internal.acl.rx.clone();
hci.rt.spawn(async move {
while let Some(item) = stream.lock().await.recv().await {
callback.Run(&item.to_bytes());
assert!(!hci.evt_callback_set);
hci.evt_callback_set = true;
- let stream = hci.evt_rx.clone();
+ let stream = hci.internal.evt_rx.clone();
hci.rt.spawn(async move {
while let Some(item) = stream.lock().await.recv().await {
callback.Run(&item.to_bytes());
assert!(!hci.le_evt_callback_set);
hci.le_evt_callback_set = true;
- let stream = hci.le_evt_rx.clone();
+ let stream = hci.internal.le_evt_rx.clone();
hci.rt.spawn(async move {
while let Some(item) = stream.lock().await.recv().await {
callback.Run(&item.to_bytes());