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

Commit 6328874f authored by Abhishek Pandit-Subedi's avatar Abhishek Pandit-Subedi Committed by Abhishek Pandit-Subedi
Browse files

floss: Handle register_disconnect for btmanagerd

Handle dbus client disconnection in btmanagerd using the
register_disconnect RPCProxy method. This makes sure that old callbacks
that are no longer connected get removed once the dbus client is gone.

Bug: 193061373
Test: Debug log in btmanagerd and open + quit btclient
Tag: #floss
Change-Id: Id26bbbb20def9332f5e3704c88a862ee901008df
parent c6901cb0
Loading
Loading
Loading
Loading
+26 −8
Original line number Diff line number Diff line
use log::error;
use log::{error, info};

use manager_service::iface_bluetooth_manager::{IBluetoothManager, IBluetoothManagerCallback};

@@ -12,29 +12,35 @@ const BLUEZ_INIT_TARGET: &str = "bluetoothd";
/// Implementation of IBluetoothManager.
pub struct BluetoothManager {
    manager_context: ManagerContext,
    callbacks: Vec<Box<dyn IBluetoothManagerCallback + Send>>,
    callbacks: Vec<(u32, Box<dyn IBluetoothManagerCallback + Send>)>,
    callbacks_last_id: u32,
}

impl BluetoothManager {
    pub(crate) fn new(manager_context: ManagerContext) -> BluetoothManager {
        BluetoothManager { manager_context, callbacks: vec![] }
        BluetoothManager { manager_context, callbacks: vec![], callbacks_last_id: 0 }
    }

    pub(crate) fn callback_hci_device_change(&self, hci_device: i32, present: bool) {
        for callback in &self.callbacks {
        for (_, callback) in &self.callbacks {
            callback.on_hci_device_changed(hci_device, present);
        }
    }

    pub(crate) fn callback_hci_enabled_change(&self, hci_device: i32, enabled: bool) {
        for callback in &self.callbacks {
        for (_, callback) in &self.callbacks {
            callback.on_hci_enabled_changed(hci_device, enabled);
        }
    }

    pub(crate) fn callback_disconnected(&mut self, id: u32) {
        self.callbacks.retain(|x| x.0 != id);
    }
}

impl IBluetoothManager for BluetoothManager {
    fn start(&mut self, hci_interface: i32) {
        info!("Starting {}", hci_interface);
        if !config_util::modify_hci_n_enabled(hci_interface, true) {
            error!("Config is not successfully modified");
        }
@@ -42,6 +48,7 @@ impl IBluetoothManager for BluetoothManager {
    }

    fn stop(&mut self, hci_interface: i32) {
        info!("Stopping {}", hci_interface);
        if !config_util::modify_hci_n_enabled(hci_interface, false) {
            error!("Config is not successfully modified");
        }
@@ -55,9 +62,20 @@ impl IBluetoothManager for BluetoothManager {
        result
    }

    fn register_callback(&mut self, callback: Box<dyn IBluetoothManagerCallback + Send>) {
        // TODO: Handle callback disconnects.
        self.callbacks.push(callback);
    fn register_callback(&mut self, mut callback: Box<dyn IBluetoothManagerCallback + Send>) {
        let tx = self.manager_context.proxy.get_tx();

        self.callbacks_last_id += 1;
        let id = self.callbacks_last_id;

        callback.register_disconnect(Box::new(move || {
            let tx = tx.clone();
            tokio::spawn(async move {
                let _result = tx.send(state_machine::Message::CallbackDisconnected(id)).await;
            });
        }));

        self.callbacks.push((id, callback));
    }

    fn get_floss_enabled(&mut self) -> bool {
+5 −0
Original line number Diff line number Diff line
@@ -395,6 +395,11 @@ pub async fn mainloop<PM>(
                _ => debug!("Ignored event {:?} - {:?}", mask, &filename),
            },

            // Callback client has disconnected
            Message::CallbackDisconnected(id) => {
                bluetooth_manager.lock().unwrap().callback_disconnected(id);
            }

            // Handle command timeouts
            Message::CommandTimeout() => {
                // Hold state lock for short duration