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

Commit db821d33 authored by Michael Sun's avatar Michael Sun
Browse files

floss: rejecting the unbonded media profile connections

Rejecting the unbonded connection after we finished our profile
reconnecting logic to avoid a collision.

BUG: 259745456
Tag: #floss
Test: forget and reconnect device
Change-Id: I19d5e7c3f698fda4968eb87a3b10b5201ae66adc
parent b1ad7a8c
Loading
Loading
Loading
Loading
+9 −4
Original line number Diff line number Diff line
@@ -638,6 +638,14 @@ impl Bluetooth {
            .collect()
    }

    /// Gets the bond state of a single device with its address.
    pub fn get_bond_state_by_addr(&self, addr: &String) -> BtBondState {
        match self.bonded_devices.get(addr) {
            Some(device) => device.bond_state.clone(),
            None => BtBondState::NotBonded,
        }
    }

    /// Check whether found devices are still fresh. If they're outside the
    /// freshness window, send a notification to clear the device from clients.
    fn trigger_freshness_check(&mut self) {
@@ -1575,10 +1583,7 @@ impl IBluetooth for Bluetooth {
    }

    fn get_bond_state(&self, device: BluetoothDevice) -> BtBondState {
        match self.bonded_devices.get(&device.address) {
            Some(device) => device.bond_state.clone(),
            None => BtBondState::NotBonded,
        }
        self.get_bond_state_by_addr(&device.address)
    }

    fn set_pin(&self, device: BluetoothDevice, accept: bool, pin_code: Vec<u8>) -> bool {
+37 −1
Original line number Diff line number Diff line
//! Anything related to audio and media API.

use bt_topshim::btif::{
    BluetoothInterface, BtConnectionDirection, BtStatus, DisplayAddress, RawAddress,
    BluetoothInterface, BtBondState, BtConnectionDirection, BtStatus, DisplayAddress, RawAddress,
    ToggleableProfile,
};
use bt_topshim::profiles::a2dp::{
@@ -990,6 +990,42 @@ impl BluetoothMedia {
                guard.insert(addr, Some((task, first_conn_ts)));
            }
            DeviceConnectionStates::FullyConnected => {
                // Rejecting the unbonded connection after we finished our profile
                // reconnectinglogic to avoid a collision.
                if let Some(adapter) = &self.adapter {
                    if BtBondState::Bonded
                        != adapter.lock().unwrap().get_bond_state_by_addr(&addr.to_string())
                    {
                        warn!(
                            "[{}]: Rejecting a unbonded device's attempt to connect to media profiles",
                            DisplayAddress(addr));
                        let fallback_tasks = self.fallback_tasks.clone();
                        let device_states = self.device_states.clone();
                        let txl = self.tx.clone();
                        let task = topstack::get_runtime().spawn(async move {
                            {
                                device_states
                                    .lock()
                                    .unwrap()
                                    .insert(addr, DeviceConnectionStates::Disconnecting);
                                fallback_tasks.lock().unwrap().insert(addr, None);
                            }

                            debug!(
                                "[{}]: Device connection state: {:?}.",
                                DisplayAddress(addr),
                                DeviceConnectionStates::Disconnecting
                            );

                            let _ = txl
                                .send(Message::Media(MediaActions::Disconnect(addr.to_string())))
                                .await;
                        });
                        guard.insert(addr, Some((task, first_conn_ts)));
                        return;
                    }
                }

                let cur_a2dp_caps = self.a2dp_caps.get(&addr);
                let cur_hfp_cap = self.hfp_cap.get(&addr);
                let name = self.adapter_get_remote_name(addr);