Loading system/gd/rust/hal/src/facade.rs +12 −6 Original line number Diff line number Diff line Loading @@ -35,10 +35,10 @@ async fn provide_facade(hal_exports: HalExports, rt: Arc<Runtime>) -> HciHalFaca #[derive(Clone, Stoppable)] pub struct HciHalFacadeService { rt: Arc<Runtime>, cmd_tx: mpsc::UnboundedSender<HciCommand>, evt_rx: Arc<Mutex<mpsc::UnboundedReceiver<HciEvent>>>, acl_tx: mpsc::UnboundedSender<RawPacket>, acl_rx: Arc<Mutex<mpsc::UnboundedReceiver<HciEvent>>>, cmd_tx: mpsc::Sender<HciCommand>, evt_rx: Arc<Mutex<mpsc::Receiver<HciEvent>>>, acl_tx: mpsc::Sender<RawPacket>, acl_rx: Arc<Mutex<mpsc::Receiver<HciEvent>>>, } impl GrpcFacade for HciHalFacadeService { Loading @@ -49,12 +49,18 @@ impl GrpcFacade for HciHalFacadeService { impl HciHalFacade for HciHalFacadeService { fn send_command(&mut self, _ctx: RpcContext<'_>, mut cmd: Command, sink: UnarySink<Empty>) { self.cmd_tx.send(cmd.take_payload().into()).unwrap(); let cmd_tx = self.cmd_tx.clone(); self.rt.block_on(async move { cmd_tx.send(cmd.take_payload().into()).await.unwrap(); }); sink.success(Empty::default()); } fn send_acl(&mut self, _ctx: RpcContext<'_>, mut acl: AclPacket, sink: UnarySink<Empty>) { self.acl_tx.send(acl.take_payload().into()).unwrap(); let acl_tx = self.acl_tx.clone(); self.rt.block_on(async move { acl_tx.send(acl.take_payload().into()).await.unwrap(); }); sink.success(Empty::default()); } Loading system/gd/rust/hal/src/hidl_hal.rs +3 −4 Original line number Diff line number Diff line //! Implementation of the HAl that talks to BT controller over Android's HIDL use crate::internal::Hal; use crate::HalExports; use crate::internal::{Hal, RawHalExports}; use bt_packet::{HciCommand, HciEvent, RawPacket}; use bytes::Bytes; use gddi::{module, provides}; Loading @@ -13,12 +12,12 @@ use tokio::sync::mpsc::{unbounded_channel, UnboundedReceiver, UnboundedSender}; module! { hidl_hal_module, providers { HalExports => provide_hidl_hal, RawHalExports => provide_hidl_hal, } } #[provides] async fn provide_hidl_hal(rt: Arc<Runtime>) -> HalExports { async fn provide_hidl_hal(rt: Arc<Runtime>) -> RawHalExports { let (hal_exports, hal) = Hal::new(); let (init_tx, mut init_rx) = unbounded_channel(); *CALLBACKS.lock().unwrap() = Some(Callbacks { Loading system/gd/rust/hal/src/lib.rs +20 −9 Original line number Diff line number Diff line Loading @@ -16,7 +16,7 @@ use bt_packet::{HciCommand, HciEvent, RawPacket}; use gddi::{module, Stoppable}; use std::sync::Arc; use thiserror::Error; use tokio::sync::mpsc::{UnboundedReceiver, UnboundedSender}; use tokio::sync::mpsc::{Receiver, Sender}; use tokio::sync::Mutex; #[cfg(target_os = "android")] Loading @@ -24,7 +24,8 @@ module! { hal_module, submodules { facade::hal_facade_module, hidl_hal::hidl_hal_module hidl_hal::hidl_hal_module, snoop::snoop_module, }, } Loading @@ -33,7 +34,8 @@ module! { hal_module, submodules { facade::hal_facade_module, rootcanal_hal::rootcanal_hal_module rootcanal_hal::rootcanal_hal_module, snoop::snoop_module, }, } /// H4 packet header size Loading @@ -45,21 +47,30 @@ const H4_HEADER_SIZE: usize = 1; #[derive(Clone, Stoppable)] pub struct HalExports { /// Transmit end of a channel used to send HCI commands pub cmd_tx: UnboundedSender<HciCommand>, pub cmd_tx: Sender<HciCommand>, /// Receive end of a channel used to receive HCI events pub evt_rx: Arc<Mutex<UnboundedReceiver<HciEvent>>>, pub evt_rx: Arc<Mutex<Receiver<HciEvent>>>, /// Transmit end of a channel used to send ACL data pub acl_tx: UnboundedSender<RawPacket>, pub acl_tx: Sender<RawPacket>, /// Receive end of a channel used to receive ACL data pub acl_rx: Arc<Mutex<UnboundedReceiver<RawPacket>>>, pub acl_rx: Arc<Mutex<Receiver<RawPacket>>>, } mod internal { use bt_packet::{HciCommand, HciEvent, RawPacket}; use gddi::Stoppable; use std::sync::Arc; use tokio::sync::mpsc::{unbounded_channel, UnboundedReceiver, UnboundedSender}; use tokio::sync::Mutex; #[derive(Clone, Stoppable)] pub struct RawHalExports { pub cmd_tx: UnboundedSender<HciCommand>, pub evt_rx: Arc<Mutex<UnboundedReceiver<HciEvent>>>, pub acl_tx: UnboundedSender<RawPacket>, pub acl_rx: Arc<Mutex<UnboundedReceiver<RawPacket>>>, } pub struct Hal { pub cmd_rx: UnboundedReceiver<HciCommand>, pub evt_tx: UnboundedSender<HciEvent>, Loading @@ -68,13 +79,13 @@ mod internal { } impl Hal { pub fn new() -> (super::HalExports, Self) { pub fn new() -> (RawHalExports, Self) { let (cmd_tx, cmd_rx) = unbounded_channel(); let (evt_tx, evt_rx) = unbounded_channel(); let (acl_down_tx, acl_down_rx) = unbounded_channel(); let (acl_up_tx, acl_up_rx) = unbounded_channel(); ( super::HalExports { RawHalExports { cmd_tx, evt_rx: Arc::new(Mutex::new(evt_rx)), acl_tx: acl_down_tx, Loading system/gd/rust/hal/src/rootcanal_hal.rs +4 −4 Original line number Diff line number Diff line Loading @@ -2,8 +2,8 @@ //! This connects to "rootcanal" which provides a simulated //! Bluetooth chip as well as a simulated environment. use crate::internal::Hal; use crate::{HalExports, Result, H4_HEADER_SIZE}; use crate::internal::{Hal, RawHalExports}; use crate::{Result, H4_HEADER_SIZE}; use bt_packet::{HciCommand, HciEvent, HciPacketHeaderSize, HciPacketType, RawPacket}; use bytes::{BufMut, Bytes, BytesMut}; use gddi::{module, provides, Stoppable}; Loading @@ -19,12 +19,12 @@ use tokio::sync::mpsc::{UnboundedReceiver, UnboundedSender}; module! { rootcanal_hal_module, providers { HalExports => provide_rootcanal_hal, RawHalExports => provide_rootcanal_hal, } } #[provides] async fn provide_rootcanal_hal(config: RootcanalConfig, rt: Arc<Runtime>) -> HalExports { async fn provide_rootcanal_hal(config: RootcanalConfig, rt: Arc<Runtime>) -> RawHalExports { let (hal_exports, hal) = Hal::new(); let (reader, writer) = TcpStream::connect(&config.to_socket_addr().unwrap()) .await Loading system/gd/rust/hal/src/snoop.rs +86 −1 Original line number Diff line number Diff line //! BT snoop logger use crate::internal::RawHalExports; use crate::HalExports; use bt_common::sys_prop; use gddi::Stoppable; use gddi::{module, provides, Stoppable}; use std::sync::Arc; use tokio::runtime::Runtime; use tokio::select; use tokio::sync::mpsc::{channel, UnboundedReceiver}; use tokio::sync::Mutex; /// The different modes snoop logging can be in #[derive(Clone)] Loading Loading @@ -75,3 +82,81 @@ fn get_configured_snoop_mode() -> String { String::default() }) } module! { snoop_module, providers { HalExports => provide_snooped_hal, }, } #[provides] async fn provide_snooped_hal( config: SnoopConfig, hal_exports: RawHalExports, rt: Arc<Runtime>, ) -> HalExports { let (cmd_down_tx, mut cmd_down_rx) = channel(10); let (evt_up_tx, evt_up_rx) = channel(10); let (acl_down_tx, mut acl_down_rx) = channel(10); let (acl_up_tx, acl_up_rx) = channel(10); rt.spawn(async move { let logger = SnoopLogger::new(config); loop { select! { Some(evt) = consume(&hal_exports.evt_rx) => { logger.log(Type::Evt, Direction::Up, &evt); evt_up_tx.send(evt).await.unwrap(); }, Some(cmd) = cmd_down_rx.recv() => { logger.log(Type::Cmd, Direction::Down, &cmd); hal_exports.cmd_tx.send(cmd).unwrap(); }, Some(acl) = acl_down_rx.recv() => { logger.log(Type::Acl, Direction::Down, &acl); hal_exports.acl_tx.send(acl).unwrap(); }, Some(acl) = consume(&hal_exports.acl_rx) => { logger.log(Type::Acl, Direction::Up, &acl); acl_up_tx.send(acl).await.unwrap(); } } } }); HalExports { cmd_tx: cmd_down_tx, evt_rx: Arc::new(Mutex::new(evt_up_rx)), acl_tx: acl_down_tx, acl_rx: Arc::new(Mutex::new(acl_up_rx)), } } async fn consume<T>(rx: &Arc<Mutex<UnboundedReceiver<T>>>) -> Option<T> { rx.lock().await.recv().await } #[allow(unused)] enum Type { Cmd = 1, Acl, Sco, Evt, Iso, } enum Direction { Up, Down, } struct SnoopLogger; impl SnoopLogger { fn new(_config: SnoopConfig) -> Self { Self {} } fn log(&self, _t: Type, _dir: Direction, _bytes: &bytes::Bytes) {} } Loading
system/gd/rust/hal/src/facade.rs +12 −6 Original line number Diff line number Diff line Loading @@ -35,10 +35,10 @@ async fn provide_facade(hal_exports: HalExports, rt: Arc<Runtime>) -> HciHalFaca #[derive(Clone, Stoppable)] pub struct HciHalFacadeService { rt: Arc<Runtime>, cmd_tx: mpsc::UnboundedSender<HciCommand>, evt_rx: Arc<Mutex<mpsc::UnboundedReceiver<HciEvent>>>, acl_tx: mpsc::UnboundedSender<RawPacket>, acl_rx: Arc<Mutex<mpsc::UnboundedReceiver<HciEvent>>>, cmd_tx: mpsc::Sender<HciCommand>, evt_rx: Arc<Mutex<mpsc::Receiver<HciEvent>>>, acl_tx: mpsc::Sender<RawPacket>, acl_rx: Arc<Mutex<mpsc::Receiver<HciEvent>>>, } impl GrpcFacade for HciHalFacadeService { Loading @@ -49,12 +49,18 @@ impl GrpcFacade for HciHalFacadeService { impl HciHalFacade for HciHalFacadeService { fn send_command(&mut self, _ctx: RpcContext<'_>, mut cmd: Command, sink: UnarySink<Empty>) { self.cmd_tx.send(cmd.take_payload().into()).unwrap(); let cmd_tx = self.cmd_tx.clone(); self.rt.block_on(async move { cmd_tx.send(cmd.take_payload().into()).await.unwrap(); }); sink.success(Empty::default()); } fn send_acl(&mut self, _ctx: RpcContext<'_>, mut acl: AclPacket, sink: UnarySink<Empty>) { self.acl_tx.send(acl.take_payload().into()).unwrap(); let acl_tx = self.acl_tx.clone(); self.rt.block_on(async move { acl_tx.send(acl.take_payload().into()).await.unwrap(); }); sink.success(Empty::default()); } Loading
system/gd/rust/hal/src/hidl_hal.rs +3 −4 Original line number Diff line number Diff line //! Implementation of the HAl that talks to BT controller over Android's HIDL use crate::internal::Hal; use crate::HalExports; use crate::internal::{Hal, RawHalExports}; use bt_packet::{HciCommand, HciEvent, RawPacket}; use bytes::Bytes; use gddi::{module, provides}; Loading @@ -13,12 +12,12 @@ use tokio::sync::mpsc::{unbounded_channel, UnboundedReceiver, UnboundedSender}; module! { hidl_hal_module, providers { HalExports => provide_hidl_hal, RawHalExports => provide_hidl_hal, } } #[provides] async fn provide_hidl_hal(rt: Arc<Runtime>) -> HalExports { async fn provide_hidl_hal(rt: Arc<Runtime>) -> RawHalExports { let (hal_exports, hal) = Hal::new(); let (init_tx, mut init_rx) = unbounded_channel(); *CALLBACKS.lock().unwrap() = Some(Callbacks { Loading
system/gd/rust/hal/src/lib.rs +20 −9 Original line number Diff line number Diff line Loading @@ -16,7 +16,7 @@ use bt_packet::{HciCommand, HciEvent, RawPacket}; use gddi::{module, Stoppable}; use std::sync::Arc; use thiserror::Error; use tokio::sync::mpsc::{UnboundedReceiver, UnboundedSender}; use tokio::sync::mpsc::{Receiver, Sender}; use tokio::sync::Mutex; #[cfg(target_os = "android")] Loading @@ -24,7 +24,8 @@ module! { hal_module, submodules { facade::hal_facade_module, hidl_hal::hidl_hal_module hidl_hal::hidl_hal_module, snoop::snoop_module, }, } Loading @@ -33,7 +34,8 @@ module! { hal_module, submodules { facade::hal_facade_module, rootcanal_hal::rootcanal_hal_module rootcanal_hal::rootcanal_hal_module, snoop::snoop_module, }, } /// H4 packet header size Loading @@ -45,21 +47,30 @@ const H4_HEADER_SIZE: usize = 1; #[derive(Clone, Stoppable)] pub struct HalExports { /// Transmit end of a channel used to send HCI commands pub cmd_tx: UnboundedSender<HciCommand>, pub cmd_tx: Sender<HciCommand>, /// Receive end of a channel used to receive HCI events pub evt_rx: Arc<Mutex<UnboundedReceiver<HciEvent>>>, pub evt_rx: Arc<Mutex<Receiver<HciEvent>>>, /// Transmit end of a channel used to send ACL data pub acl_tx: UnboundedSender<RawPacket>, pub acl_tx: Sender<RawPacket>, /// Receive end of a channel used to receive ACL data pub acl_rx: Arc<Mutex<UnboundedReceiver<RawPacket>>>, pub acl_rx: Arc<Mutex<Receiver<RawPacket>>>, } mod internal { use bt_packet::{HciCommand, HciEvent, RawPacket}; use gddi::Stoppable; use std::sync::Arc; use tokio::sync::mpsc::{unbounded_channel, UnboundedReceiver, UnboundedSender}; use tokio::sync::Mutex; #[derive(Clone, Stoppable)] pub struct RawHalExports { pub cmd_tx: UnboundedSender<HciCommand>, pub evt_rx: Arc<Mutex<UnboundedReceiver<HciEvent>>>, pub acl_tx: UnboundedSender<RawPacket>, pub acl_rx: Arc<Mutex<UnboundedReceiver<RawPacket>>>, } pub struct Hal { pub cmd_rx: UnboundedReceiver<HciCommand>, pub evt_tx: UnboundedSender<HciEvent>, Loading @@ -68,13 +79,13 @@ mod internal { } impl Hal { pub fn new() -> (super::HalExports, Self) { pub fn new() -> (RawHalExports, Self) { let (cmd_tx, cmd_rx) = unbounded_channel(); let (evt_tx, evt_rx) = unbounded_channel(); let (acl_down_tx, acl_down_rx) = unbounded_channel(); let (acl_up_tx, acl_up_rx) = unbounded_channel(); ( super::HalExports { RawHalExports { cmd_tx, evt_rx: Arc::new(Mutex::new(evt_rx)), acl_tx: acl_down_tx, Loading
system/gd/rust/hal/src/rootcanal_hal.rs +4 −4 Original line number Diff line number Diff line Loading @@ -2,8 +2,8 @@ //! This connects to "rootcanal" which provides a simulated //! Bluetooth chip as well as a simulated environment. use crate::internal::Hal; use crate::{HalExports, Result, H4_HEADER_SIZE}; use crate::internal::{Hal, RawHalExports}; use crate::{Result, H4_HEADER_SIZE}; use bt_packet::{HciCommand, HciEvent, HciPacketHeaderSize, HciPacketType, RawPacket}; use bytes::{BufMut, Bytes, BytesMut}; use gddi::{module, provides, Stoppable}; Loading @@ -19,12 +19,12 @@ use tokio::sync::mpsc::{UnboundedReceiver, UnboundedSender}; module! { rootcanal_hal_module, providers { HalExports => provide_rootcanal_hal, RawHalExports => provide_rootcanal_hal, } } #[provides] async fn provide_rootcanal_hal(config: RootcanalConfig, rt: Arc<Runtime>) -> HalExports { async fn provide_rootcanal_hal(config: RootcanalConfig, rt: Arc<Runtime>) -> RawHalExports { let (hal_exports, hal) = Hal::new(); let (reader, writer) = TcpStream::connect(&config.to_socket_addr().unwrap()) .await Loading
system/gd/rust/hal/src/snoop.rs +86 −1 Original line number Diff line number Diff line //! BT snoop logger use crate::internal::RawHalExports; use crate::HalExports; use bt_common::sys_prop; use gddi::Stoppable; use gddi::{module, provides, Stoppable}; use std::sync::Arc; use tokio::runtime::Runtime; use tokio::select; use tokio::sync::mpsc::{channel, UnboundedReceiver}; use tokio::sync::Mutex; /// The different modes snoop logging can be in #[derive(Clone)] Loading Loading @@ -75,3 +82,81 @@ fn get_configured_snoop_mode() -> String { String::default() }) } module! { snoop_module, providers { HalExports => provide_snooped_hal, }, } #[provides] async fn provide_snooped_hal( config: SnoopConfig, hal_exports: RawHalExports, rt: Arc<Runtime>, ) -> HalExports { let (cmd_down_tx, mut cmd_down_rx) = channel(10); let (evt_up_tx, evt_up_rx) = channel(10); let (acl_down_tx, mut acl_down_rx) = channel(10); let (acl_up_tx, acl_up_rx) = channel(10); rt.spawn(async move { let logger = SnoopLogger::new(config); loop { select! { Some(evt) = consume(&hal_exports.evt_rx) => { logger.log(Type::Evt, Direction::Up, &evt); evt_up_tx.send(evt).await.unwrap(); }, Some(cmd) = cmd_down_rx.recv() => { logger.log(Type::Cmd, Direction::Down, &cmd); hal_exports.cmd_tx.send(cmd).unwrap(); }, Some(acl) = acl_down_rx.recv() => { logger.log(Type::Acl, Direction::Down, &acl); hal_exports.acl_tx.send(acl).unwrap(); }, Some(acl) = consume(&hal_exports.acl_rx) => { logger.log(Type::Acl, Direction::Up, &acl); acl_up_tx.send(acl).await.unwrap(); } } } }); HalExports { cmd_tx: cmd_down_tx, evt_rx: Arc::new(Mutex::new(evt_up_rx)), acl_tx: acl_down_tx, acl_rx: Arc::new(Mutex::new(acl_up_rx)), } } async fn consume<T>(rx: &Arc<Mutex<UnboundedReceiver<T>>>) -> Option<T> { rx.lock().await.recv().await } #[allow(unused)] enum Type { Cmd = 1, Acl, Sco, Evt, Iso, } enum Direction { Up, Down, } struct SnoopLogger; impl SnoopLogger { fn new(_config: SnoopConfig) -> Self { Self {} } fn log(&self, _t: Type, _dir: Direction, _bytes: &bytes::Bytes) {} }