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

Commit 25b37aa1 authored by Abhishek Pandit-Subedi's avatar Abhishek Pandit-Subedi Committed by Abhishek Pandit-Subedi
Browse files

floss: Check adapter enabled on init

btclient should check if the default adapter is already enabled on init
and initialize the dbus object if it is.

Bug: 199213563
Tag: #floss
Test: Enable adapter, close btclient, open btclient and verify adapter
      is still enabled.
Change-Id: I3c39022756b74f80577d7b8da2d824583b15c2bc
parent 94dc00f0
Loading
Loading
Loading
Loading
+1 −23
Original line number Diff line number Diff line
@@ -32,29 +32,7 @@ impl IBluetoothManagerCallback for BtManagerCallback {
    }

    fn on_hci_enabled_changed(&self, hci_interface: i32, enabled: bool) {
        print_info!("hci{} enabled = {}", hci_interface, enabled);

        self.context
            .lock()
            .unwrap()
            .adapters
            .entry(hci_interface)
            .and_modify(|v| *v = enabled)
            .or_insert(enabled);

        // When the default adapter's state is updated, we need to modify a few more things.
        // Only do this if we're not repeating the previous state.
        let prev_enabled = self.context.lock().unwrap().enabled;
        let default_adapter = self.context.lock().unwrap().default_adapter;
        if hci_interface == default_adapter && prev_enabled != enabled {
            self.context.lock().unwrap().enabled = enabled;
            self.context.lock().unwrap().adapter_ready = false;
            if enabled {
                self.context.lock().unwrap().create_adapter_proxy(hci_interface);
            } else {
                self.context.lock().unwrap().adapter_dbus = None;
            }
        }
        self.context.lock().unwrap().set_adapter_enabled(hci_interface, enabled);
    }
}

+1 −3
Original line number Diff line number Diff line
@@ -239,10 +239,8 @@ impl CommandHandler {
            return;
        }

        let address = self.context.lock().unwrap().adapter_dbus.as_ref().unwrap().get_address();
        let address = self.context.lock().unwrap().update_adapter_address();
        print_info!("Local address = {}", &address);
        // Cache address for adapter show
        self.context.lock().unwrap().adapter_address = Some(address);
    }

    fn cmd_discovery(&mut self, args: &Vec<String>) {
+38 −0
Original line number Diff line number Diff line
@@ -99,6 +99,27 @@ impl ClientContext {
        }
    }

    // Sets required values for the adapter when enabling or disabling
    fn set_adapter_enabled(&mut self, hci_interface: i32, enabled: bool) {
        print_info!("hci{} enabled = {}", hci_interface, enabled);

        self.adapters.entry(hci_interface).and_modify(|v| *v = enabled).or_insert(enabled);

        // When the default adapter's state is updated, we need to modify a few more things.
        // Only do this if we're not repeating the previous state.
        let prev_enabled = self.enabled;
        let default_adapter = self.default_adapter;
        if hci_interface == default_adapter && prev_enabled != enabled {
            self.enabled = enabled;
            self.adapter_ready = false;
            if enabled {
                self.create_adapter_proxy(hci_interface);
            } else {
                self.adapter_dbus = None;
            }
        }
    }

    // Creates adapter proxy, registers callbacks and initializes address.
    fn create_adapter_proxy(&mut self, idx: i32) {
        let conn = self.dbus_connection.clone();
@@ -117,6 +138,14 @@ impl ClientContext {
            let _ = fg.send(ForegroundActions::RegisterAdapterCallback(objpath)).await;
        });
    }

    // Foreground-only: Updates the adapter address.
    fn update_adapter_address(&mut self) -> String {
        let address = self.adapter_dbus.as_ref().unwrap().get_address();
        self.adapter_address = Some(address.clone());

        address
    }
}

/// Actions to take on the foreground loop. This allows us to queue actions in
@@ -171,6 +200,13 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
            context.clone(),
        )));

        // Check if the default adapter is enabled. If yes, we should create the adapter proxy
        // right away.
        let default_adapter = context.lock().unwrap().default_adapter;
        if context.lock().unwrap().manager_dbus.get_adapter_enabled(default_adapter) {
            context.lock().unwrap().set_adapter_enabled(default_adapter, true);
        }

        let mut handler = CommandHandler::new(context.clone());

        let args: Vec<String> = std::env::args().collect();
@@ -233,6 +269,8 @@ async fn start_interactive_shell(
                    .unwrap()
                    .register_callback(Box::new(BtCallback::new(objpath, context.clone())));
                context.lock().unwrap().adapter_ready = true;
                let adapter_address = context.lock().unwrap().update_adapter_address();
                print_info!("Adapter {} is ready", adapter_address);
            }
            ForegroundActions::Readline(result) => match result {
                Err(_err) => {