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

Commit eaf94cf7 authored by Jeremy Wu's avatar Jeremy Wu
Browse files

Floss: disconnect all profiles when media profiles are cleared

We used to disconnect only media profiles when there is an error during
media connection.

In this CL, we disconnect all enabled profiles when a critical media
profile is disconnected.

This also makes the `disconnect` function call-able as an entry point
because while it may not disconnect all media profiles at once, it
will disconnect all profiles in the end after some iterations.

Bug: 285391400
Tag: #floss
Test: cq

Change-Id: I2edb9f74a7b71800178336b6dae80a80d3e5db5a
parent 9e9b9300
Loading
Loading
Loading
Loading
+15 −9
Original line number Diff line number Diff line
@@ -73,6 +73,9 @@ pub trait IBluetoothMedia {

    /// connect to available but missing media profiles
    fn connect(&mut self, address: String);

    /// disconnect all profiles from the device
    /// NOTE: do not call this function from outside unless `is_complete_profiles_required`
    fn disconnect(&mut self, address: String);

    // Set the device as the active A2DP device
@@ -367,6 +370,7 @@ impl BluetoothMedia {
        self.delay_volume_update.remove(&profile);

        if is_profile_critical && self.is_complete_profiles_required() {
            BluetoothMedia::disconnect_device(self.tx.clone(), addr);
            self.notify_critical_profile_disconnected(addr);
        }

@@ -475,9 +479,6 @@ impl BluetoothMedia {
                        self.a2dp_caps.remove(&addr);
                        self.a2dp_audio_state.remove(&addr);
                        self.rm_connected_profile(addr, uuid::Profile::A2dpSink, true);
                        if self.is_complete_profiles_required() {
                            self.disconnect(addr.to_string());
                        }
                    }
                    _ => {
                        self.a2dp_states.insert(addr, state);
@@ -495,6 +496,13 @@ impl BluetoothMedia {
        }
    }

    fn disconnect_device(txl: Sender<Message>, addr: RawAddress) {
        let device = BluetoothDevice::new(addr.to_string(), "".to_string());
        topstack::get_runtime().spawn(async move {
            let _ = txl.send(Message::DisconnectDevice(device)).await;
        });
    }

    pub fn dispatch_avrcp_callbacks(&mut self, cb: AvrcpCallbacks) {
        match cb {
            AvrcpCallbacks::AvrcpDeviceConnected(addr, supported) => {
@@ -672,9 +680,6 @@ impl BluetoothMedia {
                        self.hfp_cap.remove(&addr);
                        self.hfp_audio_state.remove(&addr);
                        self.rm_connected_profile(addr, uuid::Profile::Hfp, true);
                        if self.is_complete_profiles_required() {
                            self.disconnect(addr.to_string());
                        }
                    }
                    BthfConnectionState::Connecting => {
                        info!("[{}]: hfp connecting.", DisplayAddress(&addr));
@@ -1919,9 +1924,9 @@ impl IBluetoothMedia for BluetoothMedia {
        true
    }

    // TODO(b/278963515): Currently this is designed to be called from both the
    // UI and via disconnection callbacks. Remove this workaround once the
    // proper fix has landed.
    // This may not disconnect all media profiles at once, but once the stack
    // is notified of the disconnection callback, `disconnect_device` will be
    // invoked as necessary to ensure the device is removed.
    fn disconnect(&mut self, address: String) {
        let addr = match RawAddress::from_string(address.clone()) {
            None => {
@@ -1947,6 +1952,7 @@ impl IBluetoothMedia for BluetoothMedia {
                uuid::Profile::A2dpSink => {
                    // Some headsets (b/278963515) will try reconnecting to A2DP
                    // when HFP is running but (requested to be) disconnected.
                    // TODO: Remove this workaround once proper fix lands.
                    if connected_profiles.contains(&Profile::Hfp) {
                        continue;
                    }
+6 −0
Original line number Diff line number Diff line
@@ -133,6 +133,9 @@ pub enum Message {
    // Dis callbacks
    Dis(ServiceCallbacks),

    // Device removal
    DisconnectDevice(BluetoothDevice),

    // Qualification Only
    QaCallbackDisconnected(u32),
    QaAddMediaPlayer(String, bool),
@@ -367,6 +370,9 @@ impl Stack {
                Message::Dis(callback) => {
                    bluetooth_dis.lock().unwrap().handle_callbacks(&callback);
                }
                Message::DisconnectDevice(addr) => {
                    bluetooth.lock().unwrap().disconnect_all_enabled_profiles(addr);
                }
                // Qualification Only
                Message::QaAddMediaPlayer(name, browsing_supported) => {
                    bluetooth_media.lock().unwrap().add_player(name, browsing_supported);