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

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

Floss: introduce |CreateBondWithRetry| to |Stack::Message|

In some contexts, the media policy (|bluetooth_media|) may need to
invoke |CreateBond|, and retry that multiple times when |PairingIsBusy|.

In this CL, we bridge the two APIs and expose a new type of
|Stack::Message| that will attempt to |CreateBond| and retry with the
specified parameters until |PairingIsBusy| is not.

Bug: 317682584
Test: m Bluetooth
Change-Id: I7b77b8fbc7bd4a4376bfc85ca5bc05f280d3875d
parent 4651f216
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -199,6 +199,7 @@ fn main() -> Result<(), Box<dyn Error>> {
        // Run the stack main dispatch loop.
        topstack::get_runtime().spawn(Stack::dispatch(
            rx,
            tx.clone(),
            api_tx.clone(),
            bluetooth.clone(),
            bluetooth_gatt.clone(),
+6 −1
Original line number Diff line number Diff line
@@ -1302,6 +1302,11 @@ impl Bluetooth {
    fn clear_uhid(&mut self) {
        self.uhid_wakeup_source.clear();
    }

    /// Checks whether pairing is busy.
    pub fn is_pairing_busy(&self) -> bool {
        self.intf.lock().unwrap().pairing_is_busy()
    }
}

#[btif_callbacks_dispatcher(dispatch_base_callbacks, BaseCallbacks)]
@@ -2821,7 +2826,7 @@ impl IBluetooth for Bluetooth {

        let addr = RawAddress::from_string(device.address.clone());
        if addr.is_none() {
            warn!("Can't connect profiles on invalid address [{}]", &device.address);
            warn!("Can't disconnect profiles on invalid address [{}]", &device.address);
            return false;
        }

+32 −2
Original line number Diff line number Diff line
@@ -26,6 +26,7 @@ use num_derive::{FromPrimitive, ToPrimitive};
use std::sync::{Arc, Mutex};
use tokio::sync::mpsc::channel;
use tokio::sync::mpsc::{Receiver, Sender};
use tokio::time::{sleep, Duration};

use crate::battery_manager::{BatteryManager, BatterySet};
use crate::battery_provider_manager::BatteryProviderManager;
@@ -86,6 +87,7 @@ pub enum Message {
    HidHost(HHCallbacks),
    Hfp(HfpCallbacks),
    Sdp(SdpCallbacks),
    CreateBondWithRetry(BluetoothDevice, BtTransport, u32, Duration),

    // Actions within the stack
    Media(MediaActions),
@@ -201,6 +203,7 @@ impl Stack {
    /// Runs the main dispatch loop.
    pub async fn dispatch(
        mut rx: Receiver<Message>,
        tx: Sender<Message>,
        api_tx: Sender<APIMessage>,
        bluetooth: Arc<Mutex<Box<Bluetooth>>>,
        bluetooth_gatt: Arc<Mutex<Box<BluetoothGatt>>>,
@@ -224,9 +227,9 @@ impl Stack {

            match m.unwrap() {
                Message::InterfaceShutdown => {
                    let tx = api_tx.clone();
                    let txl = api_tx.clone();
                    tokio::spawn(async move {
                        let _ = tx.send(APIMessage::ShutDown).await;
                        let _ = txl.send(APIMessage::ShutDown).await;
                    });
                }

@@ -259,6 +262,33 @@ impl Stack {
                    dispatch_base_callbacks(suspend.lock().unwrap().as_mut(), b);
                }

                // When pairing is busy for any reason, the bond cannot be created.
                // Allow retries until it is ready for bonding.
                Message::CreateBondWithRetry(device, bt_transport, num_attempts, retry_delay) => {
                    if num_attempts <= 0 {
                        continue;
                    }

                    let mut bt = bluetooth.lock().unwrap();
                    if !bt.is_pairing_busy() {
                        bt.create_bond(device, bt_transport);
                        continue;
                    }

                    let txl = tx.clone();
                    tokio::spawn(async move {
                        sleep(retry_delay).await;
                        let _ = txl
                            .send(Message::CreateBondWithRetry(
                                device,
                                bt_transport,
                                num_attempts - 1,
                                retry_delay,
                            ))
                            .await;
                    });
                }

                Message::GattClient(m) => {
                    dispatch_gatt_client_callbacks(bluetooth_gatt.lock().unwrap().as_mut(), m);
                }
+4 −0
Original line number Diff line number Diff line
@@ -1281,6 +1281,10 @@ impl BluetoothInterface {
        ccall!(self, cancel_discovery)
    }

    pub fn pairing_is_busy(&self) -> bool {
        ccall!(self, pairing_is_busy)
    }

    pub fn create_bond(&self, addr: &RawAddress, transport: BtTransport) -> i32 {
        let ctransport: i32 = transport.into();
        let addr_ptr = LTCheckedPtr::from_ref(addr);