"libnix",
"liblog_rust",
"libcxx",
+ "libgrpcio",
],
target: {
android: {
"liblog_rust",
"libenv_logger",
"libcxx",
+ "libgrpcio",
],
proc_macros: [
"libpaste",
.try_init()
.ok();
}
+
+/// Indicates the object can be converted to a GRPC service
+pub trait GrpcFacade {
+ /// Convert the object into the service
+ fn into_grpc(self) -> grpcio::Service;
+}
"libprotobuf",
"libtokio",
"libgddi",
+ "libbt_main",
],
host_supported: true,
}
use bt_facade_common_proto::common;
use bt_facade_rootservice_proto::rootservice;
use bt_hal::facade::HciHalFacadeService;
-use bt_hal::hal_module;
-use bt_hal::rootcanal_hal::RootcanalConfig;
use bt_hci::facade::HciLayerFacadeService;
-use bt_hci::hci_module;
use futures::executor::block_on;
-use gddi::{module, Registry, RegistryBuilder};
use grpcio::*;
use rootservice::*;
use rootservice_grpc::{create_root_facade, RootFacade};
use tokio::runtime::Runtime;
use tokio::sync::mpsc::{channel, Sender};
use tokio::sync::oneshot;
-
-module! {
- stack_module,
- submodules {
- hal_module,
- hci_module,
- }
-}
+use bt_main::Stack;
/// Bluetooth testing root facade service
#[derive(Clone)]
while let Some(cmd) = rx.recv().await {
match cmd {
LifecycleCommand::Start { req, done } => {
- let registry = Arc::new(RegistryBuilder::new().register_module(stack_module).build());
-
- registry.inject(local_rt.clone()).await;
- if let Some(rc_port) = rootcanal_port {
- registry
- .inject(RootcanalConfig::new("127.0.0.1", rc_port))
- .await;
- }
-
- server = Some(Self::start_internal(®istry, req, grpc_port).await);
+ let stack = Stack::new(local_rt.clone()).await;
+ stack.set_rootcanal_port(rootcanal_port).await;
+ server = Some(Self::start_internal(&stack, req, grpc_port).await);
done.send(()).unwrap();
}
LifecycleCommand::Stop { done } => {
}
async fn start_internal(
- registry: &Arc<Registry>,
+ stack: &Stack,
req: StartStackRequest,
grpc_port: u16,
) -> Server {
let mut services = Vec::new();
match req.get_module_under_test() {
BluetoothModule::HAL => {
- services.push(registry.get::<HciHalFacadeService>().await.create_grpc());
+ services.push(stack.get_grpc::<HciHalFacadeService>().await);
}
BluetoothModule::HCI => {
- services.push(registry.get::<HciLayerFacadeService>().await.create_grpc());
+ services.push(stack.get_grpc::<HciLayerFacadeService>().await);
}
_ => unimplemented!(),
}
"libcxx",
"liblazy_static",
"liblog_rust",
+ "libbt_common",
],
host_supported: true,
target: {
//! BT HCI HAL facade
use crate::HalExports;
+use bt_common::GrpcFacade;
use bt_hal_proto::empty::Empty;
use bt_hal_proto::facade::*;
use bt_hal_proto::facade_grpc::{create_hci_hal_facade, HciHalFacade};
acl_rx: Arc<Mutex<mpsc::UnboundedReceiver<HciEvent>>>,
}
-impl HciHalFacadeService {
- /// Convert to a grpc service
- pub fn create_grpc(self) -> grpcio::Service {
+impl GrpcFacade for HciHalFacadeService {
+ fn into_grpc(self) -> grpcio::Service {
create_hci_hal_facade(self)
}
}
"libprotobuf",
"libgddi",
"liblog_rust",
+ "libbt_common",
],
proc_macros: [
"libnum_derive",
//! HCI layer facade
use crate::HciExports;
+use bt_common::GrpcFacade;
use bt_hci_proto::empty::Empty;
use bt_hci_proto::facade::*;
use bt_hci_proto::facade_grpc::{create_hci_layer_facade, HciLayerFacade};
use futures::sink::SinkExt;
use gddi::{module, provides};
use grpcio::*;
+use log::error;
use std::sync::Arc;
use tokio::runtime::Runtime;
-use log::error;
module! {
facade_module,
rt: Arc<Runtime>,
}
-impl HciLayerFacadeService {
- /// Convert to a grpc service
- pub fn create_grpc(self) -> grpcio::Service {
+impl GrpcFacade for HciLayerFacadeService {
+ fn into_grpc(self) -> grpcio::Service {
create_hci_layer_facade(self)
}
}
--- /dev/null
+rust_library {
+ name: "libbt_main",
+ crate_name: "bt_main",
+ srcs: ["src/lib.rs"],
+ edition: "2018",
+ rustlibs: [
+ "libbt_hal",
+ "libbt_hci",
+ "libtokio",
+ "libgddi",
+ "libbt_common",
+ "libgrpcio",
+ ],
+ host_supported: true,
+}
--- /dev/null
+//! Main BT lifecycle support
+
+use bt_hal::hal_module;
+use bt_hci::hci_module;
+use gddi::{module, Registry, RegistryBuilder};
+use bt_hal::rootcanal_hal::RootcanalConfig;
+use std::sync::Arc;
+use tokio::runtime::Runtime;
+use bt_common::GrpcFacade;
+
+module! {
+ stack_module,
+ submodules {
+ hal_module,
+ hci_module,
+ }
+}
+
+/// Central state manager
+pub struct Stack {
+ registry: Arc<Registry>,
+}
+
+impl Stack {
+ /// Construct a new Stack
+ pub async fn new(rt: Arc<Runtime>) -> Self {
+ let registry = Arc::new(RegistryBuilder::new().register_module(stack_module).build());
+ registry.inject(rt).await;
+
+ Self { registry }
+ }
+
+ /// Helper to set the rootcanal port
+ pub async fn set_rootcanal_port(&self, port: Option<u16>) {
+ if let Some(port) = port {
+ self.registry
+ .inject(RootcanalConfig::new("127.0.0.1", port))
+ .await;
+ }
+ }
+
+ /// Helper forwarding to underlying registry
+ pub async fn get<T: 'static + Clone + Send + Sync>(&self) -> T {
+ self.registry.get::<T>().await
+ }
+
+ /// Helper to get a grpc service
+ pub async fn get_grpc<T: 'static + Clone + Send + Sync + GrpcFacade>(&self) -> grpcio::Service {
+ self.get::<T>().await.into_grpc()
+ }
+}