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

Commit 1ef05f42 authored by Sonny Sasaka's avatar Sonny Sasaka
Browse files

Refactor `dyn I...` to be inside `Box<dyn I...>`

Many IBluetooth... interface accepts Box<dyn I...> as a callback.
This refactors the D-Bus projection utilities to put I... inside
Box<dyn I...> to accommodate passing the callback objects.

Additional refactor to accept String instead of &'static str as the
object path.

Bug: 188718349
Tag: #floss
Test: manual - Build floss on Linux

Change-Id: I40ec2aae30172704c5e4963173b752aea7b67a9f
parent e07e8765
Loading
Loading
Loading
Loading
+4 −4
Original line number Diff line number Diff line
@@ -8,12 +8,12 @@ use crate::console_yellow;
use crate::print_info;

/// Handles string command entered from command line.
pub struct CommandHandler {
    bluetooth: Arc<Mutex<dyn IBluetooth>>,
pub struct CommandHandler<T: IBluetooth> {
    bluetooth: Arc<Mutex<Box<T>>>,
}

impl CommandHandler {
    pub fn new(bluetooth: Arc<Mutex<dyn IBluetooth>>) -> CommandHandler {
impl<T: IBluetooth> CommandHandler<T> {
    pub fn new(bluetooth: Arc<Mutex<Box<T>>>) -> CommandHandler<T> {
        CommandHandler { bluetooth }
    }

+11 −13
Original line number Diff line number Diff line
@@ -50,16 +50,19 @@ impl RPCProxy for BtCallback {
    }
}

struct API {
    bluetooth: Arc<Mutex<dyn IBluetooth>>,
struct API<T: IBluetooth> {
    bluetooth: Arc<Mutex<Box<T>>>,
}

// This creates the API implementations directly embedded to this client.
fn create_api_embedded() -> API {
// TODO: Remove when D-Bus client is completed since this is only useful while D-Bus client is
// under development.
#[allow(dead_code)]
fn create_api_embedded() -> API<Bluetooth> {
    let (tx, rx) = Stack::create_channel();

    let intf = Arc::new(Mutex::new(get_btinterface().unwrap()));
    let bluetooth = Arc::new(Mutex::new(Bluetooth::new(tx.clone(), intf.clone())));
    let bluetooth = Arc::new(Mutex::new(Box::new(Bluetooth::new(tx.clone(), intf.clone()))));

    intf.lock().unwrap().initialize(get_bt_dispatcher(tx), vec![]);

@@ -71,8 +74,8 @@ fn create_api_embedded() -> API {
}

// This creates the API implementations over D-Bus.
fn create_api_dbus(conn: Arc<SyncConnection>) -> API {
    let bluetooth = Arc::new(Mutex::new(BluetoothDBus::new(conn.clone())));
fn create_api_dbus(conn: Arc<SyncConnection>) -> API<BluetoothDBus> {
    let bluetooth = Arc::new(Mutex::new(Box::new(BluetoothDBus::new(conn.clone()))));

    API { bluetooth }
}
@@ -92,12 +95,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
            panic!("Lost connection to D-Bus: {}", err);
        });

        // TODO: Separate the embedded mode into its own binary.
        let api = if std::env::var("EMBEDDED_STACK").is_ok() {
            create_api_embedded()
        } else {
            create_api_dbus(conn)
        };
        let api = create_api_dbus(conn);

        let dc_callbacks = Arc::new(Mutex::new(vec![]));
        api.bluetooth
@@ -105,7 +103,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
            .unwrap()
            .register_callback(Box::new(BtCallback { disconnect_callbacks: dc_callbacks.clone() }));

        let handler = CommandHandler::new(api.bluetooth.clone());
        let handler = CommandHandler::<BluetoothDBus>::new(api.bluetooth.clone());

        let simulate_disconnect = move |_cmd| {
            for callback in &*dc_callbacks.lock().unwrap() {
+8 −8
Original line number Diff line number Diff line
@@ -54,6 +54,8 @@ pub fn generate_dbus_exporter(attr: TokenStream, item: TokenStream) -> TokenStre

    let mut register_methods = quote! {};

    let obj_type = quote! { std::sync::Arc<std::sync::Mutex<Box<T>>> };

    for item in ast.items {
        if let ImplItem::Method(method) = item {
            if method.attrs.len() != 1 {
@@ -151,7 +153,7 @@ pub fn generate_dbus_exporter(attr: TokenStream, item: TokenStream) -> TokenStre
                let conn_clone = conn.clone();
                let dc_watcher_clone = disconnect_watcher.clone();
                let handle_method = move |ctx: &mut dbus_crossroads::Context,
                                          obj: &mut ObjType,
                                          obj: &mut #obj_type,
                                          #dbus_input_args |
                      -> Result<(#output_type), dbus_crossroads::MethodErr> {
                    #make_args
@@ -171,20 +173,18 @@ pub fn generate_dbus_exporter(attr: TokenStream, item: TokenStream) -> TokenStre
    let gen = quote! {
        #ori_item

        type ObjType = std::sync::Arc<std::sync::Mutex<dyn #api_iface_ident + Send>>;

        pub fn #fn_ident(
            path: &'static str,
        pub fn #fn_ident<T: 'static + #api_iface_ident + Send + ?Sized>(
            path: String,
            conn: std::sync::Arc<SyncConnection>,
            cr: &mut dbus_crossroads::Crossroads,
            obj: ObjType,
            obj: #obj_type,
            disconnect_watcher: std::sync::Arc<std::sync::Mutex<dbus_projection::DisconnectWatcher>>,
        ) {
            fn get_iface_token(
            fn get_iface_token<T: #api_iface_ident + Send + ?Sized>(
                conn: std::sync::Arc<SyncConnection>,
                cr: &mut dbus_crossroads::Crossroads,
                disconnect_watcher: std::sync::Arc<std::sync::Mutex<dbus_projection::DisconnectWatcher>>,
            ) -> dbus_crossroads::IfaceToken<ObjType> {
            ) -> dbus_crossroads::IfaceToken<#obj_type> {
                cr.register(#dbus_iface_name, |ibuilder| {
                    #register_methods
                })
+4 −4
Original line number Diff line number Diff line
@@ -33,8 +33,8 @@ fn main() -> Result<(), Box<dyn Error>> {
    let (tx, rx) = Stack::create_channel();

    let intf = Arc::new(Mutex::new(get_btinterface().unwrap()));
    let bluetooth = Arc::new(Mutex::new(Bluetooth::new(tx.clone(), intf.clone())));
    let bluetooth_gatt = Arc::new(Mutex::new(BluetoothGatt::new(intf.clone())));
    let bluetooth = Arc::new(Mutex::new(Box::new(Bluetooth::new(tx.clone(), intf.clone()))));
    let bluetooth_gatt = Arc::new(Mutex::new(Box::new(BluetoothGatt::new(intf.clone()))));

    // Args don't include arg[0] which is the binary name
    let all_args = std::env::args().collect::<Vec<String>>();
@@ -76,7 +76,7 @@ fn main() -> Result<(), Box<dyn Error>> {

        // Register D-Bus method handlers of IBluetooth.
        iface_bluetooth::export_bluetooth_dbus_obj(
            OBJECT_BLUETOOTH,
            String::from(OBJECT_BLUETOOTH),
            conn.clone(),
            &mut cr,
            bluetooth,
@@ -84,7 +84,7 @@ fn main() -> Result<(), Box<dyn Error>> {
        );
        // Register D-Bus method handlers of IBluetoothGatt.
        iface_bluetooth_gatt::export_bluetooth_gatt_dbus_obj(
            OBJECT_BLUETOOTH_GATT,
            String::from(OBJECT_BLUETOOTH_GATT),
            conn.clone(),
            &mut cr,
            bluetooth_gatt,
+1 −1
Original line number Diff line number Diff line
@@ -97,7 +97,7 @@ impl Stack {
    }

    /// Runs the main dispatch loop.
    pub async fn dispatch(mut rx: Receiver<Message>, bluetooth: Arc<Mutex<Bluetooth>>) {
    pub async fn dispatch(mut rx: Receiver<Message>, bluetooth: Arc<Mutex<Box<Bluetooth>>>) {
        loop {
            let m = rx.recv().await;