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

Commit c8e5e571 authored by Zhengping Jiang's avatar Zhengping Jiang Committed by Automerger Merge Worker
Browse files

Merge "floss: Keep uhid when wake-allowed hid is bonded to fix wakeup" into main am: e7a0e357

parents 491dec48 e7a0e357
Loading
Loading
Loading
Loading
+48 −0
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@ use bt_topshim::{

use bt_utils::array_utils;
use bt_utils::cod::{is_cod_hid_combo, is_cod_hid_keyboard};
use bt_utils::uhid::{UHid, BD_ADDR_DEFAULT};
use btif_macros::{btif_callback, btif_callbacks_dispatcher};

use log::{debug, warn};
@@ -512,6 +513,9 @@ pub struct Bluetooth {

    /// Used to notify signal handler that we have turned off the stack.
    sig_notifier: Arc<SigData>,

    /// Virtual uhid device created to keep bluetooth as a wakeup source.
    uhid_wakeup_source: UHid,
}

impl Bluetooth {
@@ -561,6 +565,7 @@ impl Bluetooth {
            discoverable_timeout: None,
            cancelling_devices: HashSet::new(),
            sig_notifier,
            uhid_wakeup_source: UHid::new(),
        }
    }

@@ -1106,6 +1111,38 @@ impl Bluetooth {
            self.start_discovery();
        }
    }

    /// Return if there are wake-allowed device in bonded status.
    fn get_wake_allowed_device_bonded(&self) -> bool {
        self.get_bonded_devices().into_iter().any(|d| self.get_remote_wake_allowed(d))
    }

    /// Powerd recognizes bluetooth activities as valid wakeup sources if powerd keeps bluetooth in
    /// the monitored path. This only happens if there is at least one valid wake-allowed BT device
    /// connected during the suspending process. If there is no BT devices connected at any time
    /// during the suspending process, the wakeup count will be lost, and system goes to dark
    /// resume instead of full resume.
    /// Bluetooth stack disconnects all physical bluetooth HID devices for suspend, so a virtual
    /// uhid device is necessary to keep bluetooth as a valid wakeup source.
    fn create_uhid_for_suspend_wakesource(&mut self) {
        if !self.uhid_wakeup_source.is_empty() {
            return;
        }
        let adapter_addr = self.get_address().to_lowercase();
        match self.uhid_wakeup_source.create(
            "suspend uhid".to_string(),
            adapter_addr,
            String::from(BD_ADDR_DEFAULT),
        ) {
            Err(e) => log::error!("Fail to create uhid {}", e),
            Ok(_) => (),
        }
    }

    /// Clear the UHID device.
    fn clear_uhid(&mut self) {
        self.uhid_wakeup_source.clear();
    }
}

#[btif_callbacks_dispatcher(dispatch_base_callbacks, BaseCallbacks)]
@@ -1254,6 +1291,8 @@ impl BtifBluetoothCallbacks for Bluetooth {
                    _ => (),
                }

                self.clear_uhid();

                // Let the signal notifier know we are turned off.
                *self.sig_notifier.enabled.lock().unwrap() = false;
                self.sig_notifier.enabled_notify.notify_all();
@@ -1282,6 +1321,9 @@ impl BtifBluetoothCallbacks for Bluetooth {
                // Ensure device is connectable so that disconnected device can reconnect
                self.set_connectable(true);

                if self.get_wake_allowed_device_bonded() {
                    self.create_uhid_for_suspend_wakesource();
                }
                // Notify the signal notifier that we are turned on.
                *self.sig_notifier.enabled.lock().unwrap() = true;
                self.sig_notifier.enabled_notify.notify_all();
@@ -1513,6 +1555,9 @@ impl BtifBluetoothCallbacks for Bluetooth {
            self.found_devices
                .entry(address.clone())
                .and_modify(|d| d.bond_state = bond_state.clone());
            if !self.get_wake_allowed_device_bonded() {
                self.clear_uhid();
            }
        }
        // We will only insert into the bonded list after bonding is complete
        else if &bond_state == &BtBondState::Bonded && !self.bonded_devices.contains_key(&address)
@@ -1539,6 +1584,9 @@ impl BtifBluetoothCallbacks for Bluetooth {
            device.services_resolved = false;
            self.bonded_devices.insert(address.clone(), device);
            self.fetch_remote_uuids(device_info);
            if self.get_wake_allowed_device_bonded() {
                self.create_uhid_for_suspend_wakesource();
            }
        } else {
            // If we're bonding, we need to update the found devices list
            self.found_devices
+0 −26
Original line number Diff line number Diff line
@@ -13,7 +13,6 @@ use std::sync::{Arc, Mutex};
use tokio::sync::mpsc::Sender;

use bt_utils::socket::{BtSocket, HciChannels, MgmtCommand, HCI_DEV_NONE};
use bt_utils::uhid::UHid;

/// Defines the Suspend/Resume API.
///
@@ -66,9 +65,6 @@ const MASKED_EVENTS_FOR_SUSPEND: u64 = (1u64 << 4) | (1u64 << 19);
/// However, we will need to delay a few seconds to avoid co-ex issues with Wi-Fi reconnection.
const RECONNECT_AUDIO_ON_RESUME_DELAY_MS: u64 = 3000;

/// Default address for a virtual uhid device.
const BD_ADDR_DEFAULT: &str = "00:00:00:00:00:00";

/// TODO(b/286268874) Remove after the synchronization issue is resolved.
/// Delay sending suspend ready signal by some time.
const LE_RAND_CB_SUSPEND_READY_DELAY_MS: u64 = 100;
@@ -153,9 +149,6 @@ pub struct Suspend {
    connectable_to_restore: bool,
    /// Bluetooth adapter discoverable mode before suspending.
    discoverable_mode_to_restore: BtDiscMode,

    /// Virtual uhid device created during suspend.
    uhid: UHid,
}

impl Suspend {
@@ -179,7 +172,6 @@ impl Suspend {
            suspend_state: Arc::new(Mutex::new(SuspendState::new())),
            connectable_to_restore: false,
            discoverable_mode_to_restore: BtDiscMode::NonDiscoverable,
            uhid: UHid::new(),
        }
    }

@@ -219,11 +211,6 @@ impl Suspend {
        let bonded_connected = self.bt.lock().unwrap().get_bonded_and_connected_devices();
        self.media.lock().unwrap().filter_to_connected_audio_devices_from(&bonded_connected)
    }

    fn get_wake_allowed_device_bonded(&self) -> bool {
        let bt = self.bt.lock().unwrap();
        bt.get_bonded_devices().into_iter().any(|d| bt.get_remote_wake_allowed(d))
    }
}

impl ISuspend for Suspend {
@@ -270,18 +257,6 @@ impl ISuspend for Suspend {
            self.audio_reconnect_joinhandle = None;
        }

        if self.get_wake_allowed_device_bonded() {
            let adapter_addr = self.bt.lock().unwrap().get_address().to_lowercase();
            match self.uhid.create(
                "suspend uhid".to_string(),
                adapter_addr,
                String::from(BD_ADDR_DEFAULT),
            ) {
                Err(e) => log::error!("Fail to create uhid {}", e),
                Ok(_) => (),
            }
        }

        self.intf.lock().unwrap().disconnect_all_acls();

        // Handle wakeful cases (Connected/Other)
@@ -319,7 +294,6 @@ impl ISuspend for Suspend {
    fn resume(&mut self) -> bool {
        let hci_index = self.bt.lock().unwrap().get_hci_index();
        notify_suspend_state(hci_index, false);
        self.uhid.clear();

        self.intf.lock().unwrap().set_default_event_mask_except(0u64, 0u64);

+8 −0
Original line number Diff line number Diff line
@@ -7,6 +7,9 @@ use uhid_virt::{Bus, CreateParams, UHIDDevice};
const VID_DEFAULT: u32 = 0x0000;
const PID_DEFAULT: u32 = 0x0000;

/// Default address for a virtual uhid device.
pub const BD_ADDR_DEFAULT: &str = "00:00:00:00:00:00";

// Report descriptor for a standard mouse
const RDESC: [u8; 50] = [
    0x05, 0x01, // USAGE_PAGE (Generic Desktop)
@@ -86,4 +89,9 @@ impl UHid {
        }
        self.devices.clear();
    }

    /// Return if the UHID vector is empty.
    pub fn is_empty(&self) -> bool {
        self.devices.is_empty()
    }
}