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

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

Floss: add virtual uhid device if a wake-allowed device is bonded am: 1151d122

parents 7e98b3d2 1151d122
Loading
Loading
Loading
Loading
+23 −0
Original line number Original line Diff line number Diff line
@@ -13,6 +13,7 @@ use std::sync::{Arc, Mutex};
use tokio::sync::mpsc::Sender;
use tokio::sync::mpsc::Sender;


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


/// Defines the Suspend/Resume API.
/// Defines the Suspend/Resume API.
///
///
@@ -152,6 +153,9 @@ pub struct Suspend {
    connectable_to_restore: bool,
    connectable_to_restore: bool,
    /// Bluetooth adapter discoverable mode before suspending.
    /// Bluetooth adapter discoverable mode before suspending.
    discoverable_mode_to_restore: BtDiscMode,
    discoverable_mode_to_restore: BtDiscMode,

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


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


@@ -214,6 +219,11 @@ impl Suspend {
        let bonded_connected = self.bt.lock().unwrap().get_bonded_and_connected_devices();
        let bonded_connected = self.bt.lock().unwrap().get_bonded_and_connected_devices();
        self.media.lock().unwrap().filter_to_connected_audio_devices_from(&bonded_connected)
        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 {
impl ISuspend for Suspend {
@@ -260,6 +270,18 @@ impl ISuspend for Suspend {
            self.audio_reconnect_joinhandle = None;
            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();
        self.intf.lock().unwrap().disconnect_all_acls();


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


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


+1 −0
Original line number Original line Diff line number Diff line
@@ -25,6 +25,7 @@ log = "0.4.14"
nix = "0.23"
nix = "0.23"
num-derive = "0.3"
num-derive = "0.3"
num-traits = "0.2"
num-traits = "0.2"
uhid-virt = "0.0.6"


[lib]
[lib]
crate-type = ["rlib"]
crate-type = ["rlib"]
+1 −0
Original line number Original line Diff line number Diff line
@@ -4,4 +4,5 @@ pub mod adv_parser;
pub mod array_utils;
pub mod array_utils;
pub mod cod;
pub mod cod;
pub mod socket;
pub mod socket;
pub mod uhid;
pub mod uinput;
pub mod uinput;
+89 −0
Original line number Original line Diff line number Diff line
//! This library provides access to Linux uhid.

use log::{debug, error};
use std::fs::File;
use uhid_virt::{Bus, CreateParams, UHIDDevice};

const VID_DEFAULT: u32 = 0x0000;
const PID_DEFAULT: u32 = 0x0000;

// Report descriptor for a standard mouse
const RDESC: [u8; 50] = [
    0x05, 0x01, // USAGE_PAGE (Generic Desktop)
    0x09, 0x02, // USAGE (Mouse)
    0xa1, 0x01, // COLLECTION (Application)
    0x09, 0x01, //   USAGE (Pointer)
    0xa1, 0x00, //   COLLECTION (Physical)
    0x05, 0x09, //     USAGE_PAGE (Button)
    0x19, 0x01, //     USAGE_MINIMUM (Button 1)
    0x29, 0x03, //     USAGE_MAXIMUM (Button 3)
    0x15, 0x00, //     LOGICAL_MINIMUM (0)
    0x25, 0x01, //     LOGICAL_MAXIMUM (1)
    0x95, 0x03, //     REPORT_COUNT (3)
    0x75, 0x01, //     REPORT_SIZE (1)
    0x81, 0x02, //     INPUT (Data,Var,Abs)
    0x95, 0x01, //     REPORT_COUNT (1)
    0x75, 0x05, //     REPORT_SIZE (5)
    0x81, 0x03, //     INPUT (Cnst,Var,Abs)
    0x05, 0x01, //     USAGE_PAGE (Generic Desktop)
    0x09, 0x30, //     USAGE (X)
    0x09, 0x31, //     USAGE (Y)
    0x15, 0x81, //     LOGICAL_MINIMUM (-127)
    0x25, 0x7f, //     LOGICAL_MAXIMUM (127)
    0x75, 0x08, //     REPORT_SIZE (8)
    0x95, 0x02, //     REPORT_COUNT (2)
    0x81, 0x06, //     INPUT (Data,Var,Rel)
    0xc0, //   END_COLLECTION
    0xc0, // END_COLLECTION
];

pub struct UHid {
    /// Open UHID objects.
    devices: Vec<UHIDDevice<File>>,
}

impl Drop for UHid {
    fn drop(&mut self) {
        self.clear();
    }
}

impl UHid {
    /// Create a new UHid struct that holds a vector of uhid objects.
    pub fn new() -> Self {
        UHid { devices: Vec::<UHIDDevice<File>>::new() }
    }

    /// Initialize a uhid device with kernel.
    pub fn create(&mut self, name: String, phys: String, uniq: String) -> Result<(), String> {
        debug!("Create a UHID {} with phys: {}, uniq: {}", name, phys, uniq);
        let rd_data = RDESC.to_vec();
        let create_params = CreateParams {
            name: name,
            phys: phys,
            uniq: uniq,
            bus: Bus::BLUETOOTH,
            vendor: VID_DEFAULT,
            product: PID_DEFAULT,
            version: 0,
            country: 0,
            rd_data,
        };
        match UHIDDevice::create(create_params) {
            Ok(d) => self.devices.push(d),
            Err(e) => return Err(e.to_string()),
        }
        Ok(())
    }

    /// Destroy open UHID devices and clear the storage.
    pub fn clear(&mut self) {
        for device in self.devices.iter_mut() {
            match device.destroy() {
                Err(e) => error!("Fail to destroy uhid {}", e),
                Ok(_) => (),
            }
        }
        self.devices.clear();
    }
}