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

Commit c01e1876 authored by Treehugger Robot's avatar Treehugger Robot Committed by Gerrit Code Review
Browse files

Merge "floss: Fix AvrcpDeviceConnected race"

parents f7d86132 91868c52
Loading
Loading
Loading
Loading
+35 −6
Original line number Original line Diff line number Diff line
@@ -29,7 +29,10 @@ use crate::bluetooth::{Bluetooth, BluetoothDevice, IBluetooth};
use crate::callbacks::Callbacks;
use crate::callbacks::Callbacks;
use crate::{Message, RPCProxy};
use crate::{Message, RPCProxy};


const DEFAULT_PROFILE_DISCOVERY_TIMEOUT_SEC: u64 = 5;
// The timeout we have to wait for all supported profiles to connect after we
// receive the first profile connected event. In the worst scenario, we'll have
// 2 * PROFILE_DISCOVERY_TIMEOUT_SEC of waiting time.
const PROFILE_DISCOVERY_TIMEOUT_SEC: u64 = 5;


pub trait IBluetoothMedia {
pub trait IBluetoothMedia {
    ///
    ///
@@ -214,11 +217,37 @@ impl BluetoothMedia {


    pub fn dispatch_avrcp_callbacks(&mut self, cb: AvrcpCallbacks) {
    pub fn dispatch_avrcp_callbacks(&mut self, cb: AvrcpCallbacks) {
        match cb {
        match cb {
            AvrcpCallbacks::AvrcpDeviceConnected(_addr, supported) => {
            AvrcpCallbacks::AvrcpDeviceConnected(addr, supported) => {
                if self.absolute_volume == supported {
                    return;
                }

                self.absolute_volume = supported;
                self.absolute_volume = supported;
                self.callbacks.lock().unwrap().for_all_callbacks(|callback| {
                let mut guard = self.device_added_tasks.lock().unwrap();
                if let Some(task) = guard.get(&addr) {
                    match task {
                        // There is a device added event waiting for other
                        // profiles (A2DP or HFP) to connect. We need to cancel
                        // the pending event to update the absolute volume
                        // capability.
                        // This refreshes the timeout waiting for potential
                        // profile connection and makes the worst case total
                        // waiting time to 2 * PROFILE_DISCOVERY_TIMEOUT_SEC.
                        Some(handler) => {
                            handler.abort();
                            guard.remove(&addr);
                            drop(guard);
                            self.notify_media_capability_added(addr);
                        }
                        // This addr has been added so trigger the absolute
                        // volume supported changed callback.
                        None => self.callbacks.lock().unwrap().for_all_callbacks(|callback| {
                            callback.on_absolute_volume_supported_changed(supported);
                            callback.on_absolute_volume_supported_changed(supported);
                });
                        }),
                    }
                } else {
                    info!("[{}]: Device's avrcp connected before a2dp and hfp", addr.to_string());
                }
            }
            }
            AvrcpCallbacks::AvrcpDeviceDisconnected(_addr) => {}
            AvrcpCallbacks::AvrcpDeviceDisconnected(_addr) => {}
            AvrcpCallbacks::AvrcpAbsoluteVolumeUpdate(volume) => {
            AvrcpCallbacks::AvrcpAbsoluteVolumeUpdate(volume) => {
@@ -409,7 +438,7 @@ impl BluetoothMedia {
                        absolute_volume,
                        absolute_volume,
                    );
                    );
                    let task = topstack::get_runtime().spawn(async move {
                    let task = topstack::get_runtime().spawn(async move {
                        sleep(Duration::from_secs(DEFAULT_PROFILE_DISCOVERY_TIMEOUT_SEC)).await;
                        sleep(Duration::from_secs(PROFILE_DISCOVERY_TIMEOUT_SEC)).await;
                        if dedup_added_cb(device_added_tasks, addr, callbacks, device, true) {
                        if dedup_added_cb(device_added_tasks, addr, callbacks, device, true) {
                            warn!(
                            warn!(
                                "[{}]: Add a device with only hfp or a2dp capability after timeout.",
                                "[{}]: Add a device with only hfp or a2dp capability after timeout.",