OSDN Git Service

rusty-gd: move common stack logic to bt_main
authorZach Johnson <zachoverflow@google.com>
Tue, 1 Dec 2020 21:08:36 +0000 (13:08 -0800)
committerZach Johnson <zachoverflow@google.com>
Thu, 10 Dec 2020 17:37:55 +0000 (09:37 -0800)
prep for sharing with shim

Bug: 171749953
Tag: #gd-refactor
Test: gd/cert/run --rhost SimpleHalTest
Change-Id: I20f897a894973506cd777a5164c268db8fb2fc7d

gd/rust/common/Android.bp
gd/rust/common/src/lib.rs
gd/rust/facade/Android.bp
gd/rust/facade/src/lib.rs
gd/rust/hal/Android.bp
gd/rust/hal/src/facade.rs
gd/rust/hci/Android.bp
gd/rust/hci/src/facade.rs
gd/rust/main/Android.bp [new file with mode: 0644]
gd/rust/main/src/lib.rs [new file with mode: 0644]

index 040b269..0fd9f7b 100644 (file)
@@ -8,6 +8,7 @@ rust_library {
         "libnix",
         "liblog_rust",
         "libcxx",
+        "libgrpcio",
     ],
     target: {
         android: {
@@ -38,6 +39,7 @@ rust_test_host {
         "liblog_rust",
         "libenv_logger",
         "libcxx",
+        "libgrpcio",
     ],
     proc_macros: [
         "libpaste",
index 051bf10..4b2734a 100644 (file)
@@ -34,3 +34,9 @@ pub fn init_logging() {
         .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;
+}
index c45e45a..7cb2f97 100644 (file)
@@ -13,6 +13,7 @@ rust_library {
       "libprotobuf",
       "libtokio",
       "libgddi",
+      "libbt_main",
     ],
     host_supported: true,
 }
index a2ace15..845a3c9 100644 (file)
@@ -11,12 +11,8 @@ pub mod empty {
 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};
@@ -24,14 +20,7 @@ use std::sync::Arc;
 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)]
@@ -104,16 +93,9 @@ impl FacadeServiceManager {
             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(&registry, 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 } => {
@@ -149,17 +131,17 @@ impl FacadeServiceManager {
     }
 
     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!(),
         }
index c24dd44..293baf1 100644 (file)
@@ -17,6 +17,7 @@ rust_library {
         "libcxx",
         "liblazy_static",
         "liblog_rust",
+        "libbt_common",
     ],
     host_supported: true,
     target: {
index 4982a6c..6d344f4 100644 (file)
@@ -1,6 +1,7 @@
 //! 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};
@@ -40,9 +41,8 @@ pub struct HciHalFacadeService {
     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)
     }
 }
index 3ea030f..16f1403 100644 (file)
@@ -16,6 +16,7 @@ rust_library {
         "libprotobuf",
         "libgddi",
         "liblog_rust",
+        "libbt_common",
     ],
     proc_macros: [
         "libnum_derive",
index 4afe625..4667da4 100644 (file)
@@ -1,6 +1,7 @@
 //! 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};
@@ -8,9 +9,9 @@ use futures::prelude::*;
 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,
@@ -31,9 +32,8 @@ pub struct HciLayerFacadeService {
     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)
     }
 }
diff --git a/gd/rust/main/Android.bp b/gd/rust/main/Android.bp
new file mode 100644 (file)
index 0000000..2e27ecf
--- /dev/null
@@ -0,0 +1,15 @@
+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,
+}
diff --git a/gd/rust/main/src/lib.rs b/gd/rust/main/src/lib.rs
new file mode 100644 (file)
index 0000000..97a5c75
--- /dev/null
@@ -0,0 +1,51 @@
+//! 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()
+    }
+}