Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit b6974bd6 authored by Zach Johnson's avatar Zach Johnson
Browse files

rusty-gd: refine stack ffi a bit

Bug: 171749953
Tag: #gd-refactor
Test: gd/cert/run --rhost SimpleHalTest
Change-Id: If1e078bdb64a41ee0a77e352617f39a0ab3155d5
parent c577a5f2
Loading
Loading
Loading
Loading
+12 −0
Original line number Diff line number Diff line
@@ -99,6 +99,18 @@ fluoride_defaults {
            // libstatslog -> libbinder doesn't build on mac
            enabled: false,
        },
        android: {
            shared_libs: [
                "android.hardware.bluetooth@1.0",
                "libhidlbase",
                "libutils",
                "libcutils",
            ],
            static_libs: [
                "libbt_common_sys_prop_cxx",
                "libbt_hidl_hal_cxx",
            ],
        },
    },
}

+4 −0
Original line number Diff line number Diff line
@@ -130,6 +130,10 @@ cc_defaults {
                "libutils",
                "libcutils",
            ],
            static_libs: [
                "libbt_common_sys_prop_cxx",
                "libbt_hidl_hal_cxx",
            ],
        },
    },
    srcs: [
+19 −2
Original line number Diff line number Diff line
@@ -18,15 +18,16 @@ module! {
/// Central state manager
pub struct Stack {
    registry: Arc<Registry>,
    rt: Arc<Runtime>,
}

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;
        registry.inject(rt.clone()).await;

        Self { registry }
        Self { registry, rt }
    }

    /// Helper to set the rootcanal port
@@ -36,6 +37,11 @@ impl Stack {
        }
    }

    /// Configures snoop with defaults
    pub async fn use_default_snoop(&self) {
        self.configure_snoop(None).await;
    }

    /// Configures snoop. If the path is provided, full logging is turned on
    pub async fn configure_snoop(&self, path: Option<String>) {
        let mut config = SnoopConfig::default();
@@ -51,6 +57,11 @@ impl Stack {
        self.registry.get::<T>().await
    }

    /// Get, but blocks the current thread.
    pub fn get_blocking<T: 'static + Clone + Send + Sync + Stoppable>(&self) -> T {
        self.rt.block_on(self.get::<T>())
    }

    /// Helper to get a grpc service
    pub async fn get_grpc<T: 'static + Clone + Send + Sync + GrpcFacade + Stoppable>(
        &self,
@@ -62,4 +73,10 @@ impl Stack {
    pub async fn stop(&mut self) {
        self.registry.stop_all().await;
    }

    /// Stop, but blocks the current thread.
    pub fn stop_blocking(&mut self) {
        let rt = self.rt.clone();
        rt.block_on(self.stop());
    }
}
+2 −0
Original line number Diff line number Diff line
@@ -27,6 +27,8 @@ rust_ffi_static {
        "libbt_hci",
        "libbt_common",
        "libcxx",
        "libtokio",
        "libbt_main",
    ],
    static_libs: [
        "libbt_callbacks_cxx",
+33 −4
Original line number Diff line number Diff line
//! Stack management

use bt_common::init_flags;
use bt_main::Stack;
use std::sync::Arc;
use tokio::runtime::Runtime;

#[cxx::bridge(namespace = bluetooth::rust::stack)]
mod ffi {
    extern "Rust" {
        fn start();
        fn stop();
        type Stack;

        fn create() -> Box<Stack>;
        fn start(stack: &mut Stack);
        fn stop(stack: &mut Stack);
    }
}

pub fn start() {
pub fn create() -> Box<Stack> {
    assert!(init_flags::gd_rust_is_enabled());

    let rt = Arc::new(Runtime::new().unwrap());
    let local_rt = rt.clone();
    rt.block_on(async move {
        let stack = Stack::new(local_rt).await;
        stack.use_default_snoop().await;

        Box::new(stack)
    })
}

pub fn stop() {
pub fn start(stack: &mut Stack) {
    assert!(init_flags::gd_rust_is_enabled());

    if init_flags::gd_hci_is_enabled() {
        stack.get_blocking::<bt_hci::HciExports>();
    }
}

pub fn stop(stack: &mut Stack) {
    assert!(init_flags::gd_rust_is_enabled());

    stack.stop_blocking();
}
Loading