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

Commit c1759f29 authored by Sonny Sasaka's avatar Sonny Sasaka
Browse files

Floss: Refactor le_rand async mechanism

A public facing API should not block, so this patch removes block_on and
instead uses the le_rand callback to wait before firing callbacks.

Bug: 232547719
Tag: #floss
Test: Manual - Build Floss on Linux

Change-Id: Ifed5d215953a485c2ae37627b47f8b298fbe2b46
parent e04eac40
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -1442,7 +1442,7 @@ impl ISuspend for SuspendDBus {
    }

    #[dbus_method("Suspend")]
    fn suspend(&self, _suspend_type: SuspendType) {
    fn suspend(&mut self, _suspend_type: SuspendType) {
        dbus_generated!()
    }

+1 −1
Original line number Diff line number Diff line
@@ -42,7 +42,7 @@ impl ISuspend for SuspendDBus {
    }

    #[dbus_method("Suspend")]
    fn suspend(&self, _suspend_type: SuspendType) {
    fn suspend(&mut self, _suspend_type: SuspendType) {
        dbus_generated!()
    }

+2 −2
Original line number Diff line number Diff line
@@ -441,8 +441,8 @@ impl PowerdSuspendManager {
        {
            // Anonymous block to contain locked `self.context` which needs to be called multiple
            // times in the `if let` block below. Prevent deadlock by locking only once.
            let context_locked = self.context.lock().unwrap();
            if let Some(adapter_suspend_dbus) = &context_locked.adapter_suspend_dbus {
            let mut context_locked = self.context.lock().unwrap();
            if let Some(adapter_suspend_dbus) = &mut context_locked.adapter_suspend_dbus {
                adapter_suspend_dbus.suspend(match suspend_imminent.get_reason() {
                    SuspendImminent_Reason::IDLE => SuspendType::AllowWakeFromHid,
                    SuspendImminent_Reason::LID_CLOSED => SuspendType::NoWakesAllowed,
+1 −1
Original line number Diff line number Diff line
@@ -559,7 +559,7 @@ impl ISuspend for ISuspendDBus {
    }

    #[dbus_method("Suspend")]
    fn suspend(&self, suspend_type: SuspendType) {
    fn suspend(&mut self, suspend_type: SuspendType) {
        dbus_generated!()
    }

+19 −33
Original line number Diff line number Diff line
@@ -18,15 +18,13 @@ use bt_topshim::{

use btif_macros::{btif_callback, btif_callbacks_dispatcher};

use log::{debug, error, warn};
use log::{debug, warn};
use num_traits::cast::ToPrimitive;
use std::collections::HashMap;
use std::collections::VecDeque;
use std::sync::{Arc, Condvar, Mutex};
use std::time::Duration;
use std::time::Instant;
use tokio::sync::mpsc::Sender;
use tokio::sync::oneshot::Sender as OneShotSender;
use tokio::task::JoinHandle;
use tokio::time;

@@ -352,7 +350,6 @@ pub struct Bluetooth {
    /// Used to delay connection until we have SDP results.
    wait_to_connect: bool,
    // Internal API members
    internal_le_rand_queue: VecDeque<OneShotSender<u64>>,
    discoverable_timeout: Option<JoinHandle<()>>,

    /// Used to notify signal handler that we have turned off the stack.
@@ -391,7 +388,6 @@ impl Bluetooth {
            uuid_helper: UuidHelper::new(),
            wait_to_connect: false,
            // Internal API members
            internal_le_rand_queue: VecDeque::<OneShotSender<u64>>::new(),
            discoverable_timeout: None,
            sig_notifier,
        }
@@ -533,17 +529,9 @@ impl Bluetooth {
        }
    }

    /// Makes an LE_RAND call to the Bluetooth interface.  This call is asynchronous, and uses an
    /// asynchronous callback, but is synchonized on a |OneShotSender|
    /// If generating a random isn't successful, a 0 will be sent to the callback.
    pub fn le_rand(&mut self, promise: OneShotSender<u64>) {
        if self.intf.lock().unwrap().le_rand() != BTM_SUCCESS {
            debug!("Call to BTIF failed.");
            // Send 0 to caller indicating failure to generate a RANDOM
            let _ = promise.send(0);
        } else {
            self.internal_le_rand_queue.push_back(promise);
        }
    /// Makes an LE_RAND call to the Bluetooth interface.
    pub fn le_rand(&mut self) -> bool {
        self.intf.lock().unwrap().le_rand() == BTM_SUCCESS
    }

    fn send_metrics_remote_device_info(device: &BluetoothDeviceContext) {
@@ -582,9 +570,10 @@ impl Bluetooth {
}

#[btif_callbacks_dispatcher(dispatch_base_callbacks, BaseCallbacks)]
#[allow(unused_variables)]
pub(crate) trait BtifBluetoothCallbacks {
    #[btif_callback(AdapterState)]
    fn adapter_state_changed(&mut self, state: BtState);
    fn adapter_state_changed(&mut self, state: BtState) {}

    #[btif_callback(AdapterProperties)]
    fn adapter_properties_changed(
@@ -592,13 +581,14 @@ pub(crate) trait BtifBluetoothCallbacks {
        status: BtStatus,
        num_properties: i32,
        properties: Vec<BluetoothProperty>,
    );
    ) {
    }

    #[btif_callback(DeviceFound)]
    fn device_found(&mut self, n: i32, properties: Vec<BluetoothProperty>);
    fn device_found(&mut self, n: i32, properties: Vec<BluetoothProperty>) {}

    #[btif_callback(DiscoveryState)]
    fn discovery_state(&mut self, state: BtDiscoveryState);
    fn discovery_state(&mut self, state: BtDiscoveryState) {}

    #[btif_callback(SspRequest)]
    fn ssp_request(
@@ -608,7 +598,8 @@ pub(crate) trait BtifBluetoothCallbacks {
        cod: u32,
        variant: BtSspVariant,
        passkey: u32,
    );
    ) {
    }

    #[btif_callback(BondState)]
    fn bond_state(
@@ -617,7 +608,8 @@ pub(crate) trait BtifBluetoothCallbacks {
        addr: RawAddress,
        bond_state: BtBondState,
        fail_reason: i32,
    );
    ) {
    }

    #[btif_callback(RemoteDeviceProperties)]
    fn remote_device_properties_changed(
@@ -626,7 +618,8 @@ pub(crate) trait BtifBluetoothCallbacks {
        addr: RawAddress,
        num_properties: i32,
        properties: Vec<BluetoothProperty>,
    );
    ) {
    }

    #[btif_callback(AclState)]
    fn acl_state(
@@ -636,10 +629,11 @@ pub(crate) trait BtifBluetoothCallbacks {
        state: BtAclState,
        link_type: BtTransport,
        hci_reason: BtHciErrorCode,
    );
    ) {
    }

    #[btif_callback(LeRandCallback)]
    fn le_rand_cb(&mut self, random: u64);
    fn le_rand_cb(&mut self, random: u64) {}
}

#[btif_callbacks_dispatcher(dispatch_hid_host_callbacks, HHCallbacks)]
@@ -1021,14 +1015,6 @@ impl BtifBluetoothCallbacks for Bluetooth {
            None => (),
        };
    }

    fn le_rand_cb(&mut self, random: u64) {
        debug!("Random: {:?}", random);
        let _ = match self.internal_le_rand_queue.pop_back() {
            Some(promise) => promise.send(random),
            None => Ok(error!("LE Rand callback received but no sender available!")),
        };
    }
}

// TODO: Add unit tests for this implementation
Loading