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

Commit 66d4f381 authored by Treehugger Robot's avatar Treehugger Robot Committed by Gerrit Code Review
Browse files

Merge "floss: Add BtAddrType and BtTransport parameters to HID host" into main

parents ea6ebccd 5b303cc6
Loading
Loading
Loading
Loading
+153 −26
Original line number Diff line number Diff line
@@ -860,6 +860,7 @@ impl Bluetooth {
        }
    }

    // TODO(b/328675014): Add BtAddrType and BtTransport parameters
    pub(crate) fn get_hid_report_internal(
        &mut self,
        addr: String,
@@ -867,12 +868,20 @@ impl Bluetooth {
        report_id: u8,
    ) -> BtStatus {
        if let Some(mut addr) = RawAddress::from_string(addr) {
            self.hh.as_mut().unwrap().get_report(&mut addr, report_type, report_id, 128)
            self.hh.as_mut().unwrap().get_report(
                &mut addr,
                BtAddrType::Public,
                BtTransport::Auto,
                report_type,
                report_id,
                128,
            )
        } else {
            BtStatus::InvalidParam
        }
    }

    // TODO(b/328675014): Add BtAddrType and BtTransport parameters
    pub(crate) fn set_hid_report_internal(
        &mut self,
        addr: String,
@@ -881,16 +890,28 @@ impl Bluetooth {
    ) -> BtStatus {
        if let Some(mut addr) = RawAddress::from_string(addr) {
            let mut rb = report.clone().into_bytes();
            self.hh.as_mut().unwrap().set_report(&mut addr, report_type, rb.as_mut_slice())
            self.hh.as_mut().unwrap().set_report(
                &mut addr,
                BtAddrType::Public,
                BtTransport::Auto,
                report_type,
                rb.as_mut_slice(),
            )
        } else {
            BtStatus::InvalidParam
        }
    }

    // TODO(b/328675014): Add BtAddrType and BtTransport parameters
    pub(crate) fn send_hid_data_internal(&mut self, addr: String, data: String) -> BtStatus {
        if let Some(mut addr) = RawAddress::from_string(addr) {
            let mut rb = data.clone().into_bytes();
            self.hh.as_mut().unwrap().send_data(&mut addr, rb.as_mut_slice())
            self.hh.as_mut().unwrap().send_data(
                &mut addr,
                BtAddrType::Public,
                BtTransport::Auto,
                rb.as_mut_slice(),
            )
        } else {
            BtStatus::InvalidParam
        }
@@ -1250,22 +1271,62 @@ pub(crate) trait BtifBluetoothCallbacks {
#[btif_callbacks_dispatcher(dispatch_hid_host_callbacks, HHCallbacks)]
pub(crate) trait BtifHHCallbacks {
    #[btif_callback(ConnectionState)]
    fn connection_state(&mut self, address: RawAddress, state: BthhConnectionState);
    fn connection_state(
        &mut self,
        address: RawAddress,
        address_type: BtAddrType,
        transport: BtTransport,
        state: BthhConnectionState,
    );

    #[btif_callback(HidInfo)]
    fn hid_info(&mut self, address: RawAddress, info: BthhHidInfo);
    fn hid_info(
        &mut self,
        address: RawAddress,
        address_type: BtAddrType,
        transport: BtTransport,
        info: BthhHidInfo,
    );

    #[btif_callback(ProtocolMode)]
    fn protocol_mode(&mut self, address: RawAddress, status: BthhStatus, mode: BthhProtocolMode);
    fn protocol_mode(
        &mut self,
        address: RawAddress,
        address_type: BtAddrType,
        transport: BtTransport,
        status: BthhStatus,
        mode: BthhProtocolMode,
    );

    #[btif_callback(IdleTime)]
    fn idle_time(&mut self, address: RawAddress, status: BthhStatus, idle_rate: i32);
    fn idle_time(
        &mut self,
        address: RawAddress,
        address_type: BtAddrType,
        transport: BtTransport,
        status: BthhStatus,
        idle_rate: i32,
    );

    #[btif_callback(GetReport)]
    fn get_report(&mut self, address: RawAddress, status: BthhStatus, data: Vec<u8>, size: i32);
    fn get_report(
        &mut self,
        address: RawAddress,
        address_type: BtAddrType,
        transport: BtTransport,
        status: BthhStatus,
        data: Vec<u8>,
        size: i32,
    );

    #[btif_callback(Handshake)]
    fn handshake(&mut self, address: RawAddress, status: BthhStatus);
    fn handshake(
        &mut self,
        address: RawAddress,
        address_type: BtAddrType,
        transport: BtTransport,
        status: BthhStatus,
    );
}

#[btif_callbacks_dispatcher(dispatch_sdp_callbacks, SdpCallbacks)]
@@ -2602,7 +2663,14 @@ impl IBluetooth for Bluetooth {
                        match p {
                            Profile::Hid | Profile::Hogp => {
                                has_supported_profile = true;
                                let status = self.hh.as_ref().unwrap().connect(&mut addr);
                                // TODO(b/328675014): Use BtAddrType
                                // and BtTransport from
                                // BluetoothDevice instead of default
                                let status = self.hh.as_ref().unwrap().connect(
                                    &mut addr,
                                    BtAddrType::Public,
                                    BtTransport::Auto,
                                );
                                metrics::profile_connection_state_changed(
                                    addr,
                                    p as u32,
@@ -2718,7 +2786,14 @@ impl IBluetooth for Bluetooth {
                    if UuidHelper::is_profile_supported(&p) {
                        match p {
                            Profile::Hid | Profile::Hogp => {
                                self.hh.as_ref().unwrap().disconnect(&mut addr.unwrap());
                                // TODO(b/328675014): Use BtAddrType
                                // and BtTransport from
                                // BluetoothDevice instead of default
                                self.hh.as_ref().unwrap().disconnect(
                                    &mut addr.unwrap(),
                                    BtAddrType::Public,
                                    BtTransport::Auto,
                                );
                            }

                            Profile::A2dpSink
@@ -2854,7 +2929,13 @@ impl BtifSdpCallbacks for Bluetooth {
}

impl BtifHHCallbacks for Bluetooth {
    fn connection_state(&mut self, mut address: RawAddress, state: BthhConnectionState) {
    fn connection_state(
        &mut self,
        mut address: RawAddress,
        address_type: BtAddrType,
        transport: BtTransport,
        state: BthhConnectionState,
    ) {
        debug!(
            "Hid host connection state updated: Address({}) State({:?})",
            DisplayAddress(&address),
@@ -2889,43 +2970,89 @@ impl BtifHHCallbacks for Bluetooth {
                "[{}]: Rejecting a unbonded device's attempt to connect to HID/HOG profiles",
                DisplayAddress(&address)
            );
            self.hh.as_ref().unwrap().disconnect(&mut address);
            self.hh.as_ref().unwrap().disconnect(&mut address, address_type, transport);
        }
    }

    fn hid_info(&mut self, address: RawAddress, info: BthhHidInfo) {
        debug!("Hid host info updated: Address({}) Info({:?})", DisplayAddress(&address), info);
    fn hid_info(
        &mut self,
        address: RawAddress,
        address_type: BtAddrType,
        transport: BtTransport,
        info: BthhHidInfo,
    ) {
        debug!(
            "Hid host info updated: Address({}) AddressType({:?}) Transport({:?}) Info({:?})",
            DisplayAddress(&address),
            address_type,
            transport,
            info
        );
    }

    fn protocol_mode(&mut self, address: RawAddress, status: BthhStatus, mode: BthhProtocolMode) {
    fn protocol_mode(
        &mut self,
        address: RawAddress,
        address_type: BtAddrType,
        transport: BtTransport,
        status: BthhStatus,
        mode: BthhProtocolMode,
    ) {
        debug!(
            "Hid host protocol mode updated: Address({}) Status({:?}) Mode({:?})",
            DisplayAddress(&address),
            "Hid host protocol mode updated: Address({}) AddressType({:?}) Transport({:?}) Status({:?}) Mode({:?})",
            DisplayAddress(&address), address_type, transport,
            status,
            mode
        );
    }

    fn idle_time(&mut self, address: RawAddress, status: BthhStatus, idle_rate: i32) {
    fn idle_time(
        &mut self,
        address: RawAddress,
        address_type: BtAddrType,
        transport: BtTransport,
        status: BthhStatus,
        idle_rate: i32,
    ) {
        debug!(
            "Hid host idle time updated: Address({}) Status({:?}) Idle Rate({:?})",
            DisplayAddress(&address),
            "Hid host idle time updated: Address({}) AddressType({:?}) Transport({:?}) Status({:?}) Idle Rate({:?})",
            DisplayAddress(&address), address_type, transport,
            status,
            idle_rate
        );
    }

    fn get_report(&mut self, address: RawAddress, status: BthhStatus, _data: Vec<u8>, size: i32) {
    fn get_report(
        &mut self,
        address: RawAddress,
        address_type: BtAddrType,
        transport: BtTransport,
        status: BthhStatus,
        _data: Vec<u8>,
        size: i32,
    ) {
        debug!(
            "Hid host got report: Address({}) Status({:?}) Report Size({:?})",
            DisplayAddress(&address),
            "Hid host got report: Address({}) AddressType({:?}) Transport({:?}) Status({:?}) Report Size({:?})",
            DisplayAddress(&address), address_type, transport,
            status,
            size
        );
    }

    fn handshake(&mut self, address: RawAddress, status: BthhStatus) {
        debug!("Hid host handshake: Address({}) Status({:?})", DisplayAddress(&address), status);
    fn handshake(
        &mut self,
        address: RawAddress,
        address_type: BtAddrType,
        transport: BtTransport,
        status: BthhStatus,
    ) {
        debug!(
            "Hid host handshake: Address({}) AddressType({:?}) Transport({:?}) Status({:?})",
            DisplayAddress(&address),
            address_type,
            transport,
            status
        );
    }
}

+24 −0
Original line number Diff line number Diff line
@@ -52,6 +52,18 @@ impl From<BtTransport> for i32 {
    }
}

impl From<u8> for BtTransport {
    fn from(transport: u8) -> Self {
        BtTransport::from_u8(transport).unwrap_or(BtTransport::Auto)
    }
}

impl Into<u8> for BtTransport {
    fn into(self) -> u8 {
        self.to_u8().unwrap_or(0)
    }
}

#[derive(Clone, Debug, FromPrimitive, ToPrimitive, PartialEq, PartialOrd)]
#[repr(u32)]
pub enum BtSspVariant {
@@ -390,6 +402,18 @@ impl Into<u32> for BtAddrType {
    }
}

impl From<u8> for BtAddrType {
    fn from(address_type: u8) -> Self {
        BtAddrType::from_u8(address_type).unwrap_or(BtAddrType::Unknown)
    }
}

impl Into<u8> for BtAddrType {
    fn into(self) -> u8 {
        self.to_u8().unwrap_or(0)
    }
}

pub type BtHciErrorCode = u8;
pub type BtLocalLeFeatures = bindings::bt_local_le_features_t;
pub type BtPinCode = bindings::bt_pin_code_t;
+136 −31
Original line number Diff line number Diff line
use crate::bindings::root as bindings;
use crate::btif::{BluetoothInterface, BtStatus, RawAddress, SupportedProfiles, ToggleableProfile};
use crate::btif::{
    BluetoothInterface, BtAddrType, BtStatus, BtTransport, RawAddress, SupportedProfiles,
    ToggleableProfile,
};
use crate::ccall;
use crate::profiles::hid_host::bindings::bthh_interface_t;
use crate::topstack::get_dispatchers;
@@ -103,13 +106,13 @@ fn convert_report(count: i32, raw: *mut u8) -> Vec<u8> {

#[derive(Debug)]
pub enum HHCallbacks {
    ConnectionState(RawAddress, BthhConnectionState),
    VirtualUnplug(RawAddress, BthhStatus),
    HidInfo(RawAddress, BthhHidInfo),
    ProtocolMode(RawAddress, BthhStatus, BthhProtocolMode),
    IdleTime(RawAddress, BthhStatus, i32),
    GetReport(RawAddress, BthhStatus, Vec<u8>, i32),
    Handshake(RawAddress, BthhStatus),
    ConnectionState(RawAddress, BtAddrType, BtTransport, BthhConnectionState),
    VirtualUnplug(RawAddress, BtAddrType, BtTransport, BthhStatus),
    HidInfo(RawAddress, BtAddrType, BtTransport, BthhHidInfo),
    ProtocolMode(RawAddress, BtAddrType, BtTransport, BthhStatus, BthhProtocolMode),
    IdleTime(RawAddress, BtAddrType, BtTransport, BthhStatus, i32),
    GetReport(RawAddress, BtAddrType, BtTransport, BthhStatus, Vec<u8>, i32),
    Handshake(RawAddress, BtAddrType, BtTransport, BthhStatus),
}

pub struct HHCallbacksDispatcher {
@@ -119,33 +122,33 @@ pub struct HHCallbacksDispatcher {
type HHCb = Arc<Mutex<HHCallbacksDispatcher>>;

cb_variant!(HHCb, connection_state_cb -> HHCallbacks::ConnectionState,
*mut RawAddress, bindings::bthh_connection_state_t -> BthhConnectionState, {
*mut RawAddress, u8 -> BtAddrType, u8 -> BtTransport, bindings::bthh_connection_state_t -> BthhConnectionState, {
    let _0 = unsafe { *_0 };
});
cb_variant!(HHCb, virtual_unplug_cb -> HHCallbacks::VirtualUnplug,
*mut RawAddress, bindings::bthh_status_t -> BthhStatus, {
*mut RawAddress, u8 -> BtAddrType, u8 -> BtTransport, bindings::bthh_status_t -> BthhStatus, {
    let _0 = unsafe { *_0 };
});
cb_variant!(HHCb, hid_info_cb -> HHCallbacks::HidInfo,
*mut RawAddress, bindings::bthh_hid_info_t -> BthhHidInfo, {
*mut RawAddress, u8 -> BtAddrType, u8 -> BtTransport, bindings::bthh_hid_info_t -> BthhHidInfo, {
    let _0 = unsafe { *_0 };
});
cb_variant!(HHCb, protocol_mode_cb -> HHCallbacks::ProtocolMode,
*mut RawAddress, bindings::bthh_status_t -> BthhStatus,
*mut RawAddress, u8 -> BtAddrType, u8 -> BtTransport, bindings::bthh_status_t -> BthhStatus,
bindings::bthh_protocol_mode_t -> BthhProtocolMode, {
    let _0 = unsafe { *_0 };
});
cb_variant!(HHCb, idle_time_cb -> HHCallbacks::IdleTime,
*mut RawAddress, bindings::bthh_status_t -> BthhStatus, i32, {
*mut RawAddress, u8 -> BtAddrType, u8 -> BtTransport, bindings::bthh_status_t -> BthhStatus, i32, {
    let _0 = unsafe { *_0 };
});
cb_variant!(HHCb, get_report_cb -> HHCallbacks::GetReport,
*mut RawAddress, bindings::bthh_status_t -> BthhStatus, *mut u8, i32, {
*mut RawAddress, u8 -> BtAddrType, u8 -> BtTransport, bindings::bthh_status_t -> BthhStatus, *mut u8, i32, {
    let _0 = unsafe { *_0 };
    let _2 = convert_report(_3, _2);
    let _4 = convert_report(_5, _4);
});
cb_variant!(HHCb, handshake_cb -> HHCallbacks::Handshake,
*mut RawAddress, bindings::bthh_status_t -> BthhStatus, {
*mut RawAddress, u8 -> BtAddrType, u8 -> BtTransport, bindings::bthh_status_t -> BthhStatus, {
    let _0 = unsafe { *_0 };
});

@@ -230,67 +233,155 @@ impl HidHost {
    }

    #[profile_enabled_or(BtStatus::NotReady)]
    pub fn connect(&self, addr: &mut RawAddress) -> BtStatus {
    pub fn connect(
        &self,
        addr: &mut RawAddress,
        address_type: BtAddrType,
        transport: BtTransport,
    ) -> BtStatus {
        let addr_ptr = LTCheckedPtrMut::from_ref(addr);
        BtStatus::from(ccall!(self, connect, addr_ptr.into()))
        BtStatus::from(ccall!(
            self,
            connect,
            addr_ptr.into(),
            address_type.into(),
            transport.into()
        ))
    }

    #[profile_enabled_or(BtStatus::NotReady)]
    pub fn disconnect(&self, addr: &mut RawAddress) -> BtStatus {
    pub fn disconnect(
        &self,
        addr: &mut RawAddress,
        address_type: BtAddrType,
        transport: BtTransport,
    ) -> BtStatus {
        let addr_ptr = LTCheckedPtrMut::from_ref(addr);
        BtStatus::from(ccall!(self, disconnect, addr_ptr.into()))
        BtStatus::from(ccall!(
            self,
            disconnect,
            addr_ptr.into(),
            address_type.into(),
            transport.into()
        ))
    }

    #[profile_enabled_or(BtStatus::NotReady)]
    pub fn virtual_unplug(&self, addr: &mut RawAddress) -> BtStatus {
    pub fn virtual_unplug(
        &self,
        addr: &mut RawAddress,
        address_type: BtAddrType,
        transport: BtTransport,
    ) -> BtStatus {
        let addr_ptr = LTCheckedPtrMut::from_ref(addr);
        BtStatus::from(ccall!(self, virtual_unplug, addr_ptr.into()))
        BtStatus::from(ccall!(
            self,
            virtual_unplug,
            addr_ptr.into(),
            address_type.into(),
            transport.into()
        ))
    }

    #[profile_enabled_or(BtStatus::NotReady)]
    pub fn set_info(&self, addr: &mut RawAddress, info: BthhHidInfo) -> BtStatus {
    pub fn set_info(
        &self,
        addr: &mut RawAddress,
        address_type: BtAddrType,
        transport: BtTransport,
        info: BthhHidInfo,
    ) -> BtStatus {
        let addr_ptr = LTCheckedPtrMut::from_ref(addr);
        BtStatus::from(ccall!(self, set_info, addr_ptr.into(), info))
        BtStatus::from(ccall!(
            self,
            set_info,
            addr_ptr.into(),
            address_type.into(),
            transport.into(),
            info
        ))
    }

    #[profile_enabled_or(BtStatus::NotReady)]
    pub fn get_protocol(&self, addr: &mut RawAddress, mode: BthhProtocolMode) -> BtStatus {
    pub fn get_protocol(
        &self,
        addr: &mut RawAddress,
        address_type: BtAddrType,
        transport: BtTransport,
        mode: BthhProtocolMode,
    ) -> BtStatus {
        let addr_ptr = LTCheckedPtrMut::from_ref(addr);
        BtStatus::from(ccall!(
            self,
            get_protocol,
            addr_ptr.into(),
            address_type.into(),
            transport.into(),
            bindings::bthh_protocol_mode_t::from(mode)
        ))
    }

    #[profile_enabled_or(BtStatus::NotReady)]
    pub fn set_protocol(&self, addr: &mut RawAddress, mode: BthhProtocolMode) -> BtStatus {
    pub fn set_protocol(
        &self,
        addr: &mut RawAddress,
        address_type: BtAddrType,
        transport: BtTransport,
        mode: BthhProtocolMode,
    ) -> BtStatus {
        let addr_ptr = LTCheckedPtrMut::from_ref(addr);
        BtStatus::from(ccall!(
            self,
            set_protocol,
            addr_ptr.into(),
            address_type.into(),
            transport.into(),
            bindings::bthh_protocol_mode_t::from(mode)
        ))
    }

    #[profile_enabled_or(BtStatus::NotReady)]
    pub fn get_idle_time(&self, addr: &mut RawAddress) -> BtStatus {
    pub fn get_idle_time(
        &self,
        addr: &mut RawAddress,
        address_type: BtAddrType,
        transport: BtTransport,
    ) -> BtStatus {
        let addr_ptr = LTCheckedPtrMut::from_ref(addr);
        BtStatus::from(ccall!(self, get_idle_time, addr_ptr.into()))
        BtStatus::from(ccall!(
            self,
            get_idle_time,
            addr_ptr.into(),
            address_type.into(),
            transport.into()
        ))
    }

    #[profile_enabled_or(BtStatus::NotReady)]
    pub fn set_idle_time(&self, addr: &mut RawAddress, idle_time: u8) -> BtStatus {
    pub fn set_idle_time(
        &self,
        addr: &mut RawAddress,
        address_type: BtAddrType,
        transport: BtTransport,
        idle_time: u8,
    ) -> BtStatus {
        let addr_ptr = LTCheckedPtrMut::from_ref(addr);
        BtStatus::from(ccall!(self, set_idle_time, addr_ptr.into(), idle_time))
        BtStatus::from(ccall!(
            self,
            set_idle_time,
            addr_ptr.into(),
            address_type.into(),
            transport.into(),
            idle_time
        ))
    }

    #[profile_enabled_or(BtStatus::NotReady)]
    pub fn get_report(
        &self,
        addr: &mut RawAddress,
        address_type: BtAddrType,
        transport: BtTransport,
        report_type: BthhReportType,
        report_id: u8,
        buffer_size: i32,
@@ -300,6 +391,8 @@ impl HidHost {
            self,
            get_report,
            addr_ptr.into(),
            address_type.into(),
            transport.into(),
            bindings::bthh_report_type_t::from(report_type),
            report_id,
            buffer_size
@@ -310,6 +403,8 @@ impl HidHost {
    pub fn set_report(
        &self,
        addr: &mut RawAddress,
        address_type: BtAddrType,
        transport: BtTransport,
        report_type: BthhReportType,
        report: &mut [u8],
    ) -> BtStatus {
@@ -319,19 +414,29 @@ impl HidHost {
            self,
            set_report,
            addr_ptr.into(),
            address_type.into(),
            transport.into(),
            bindings::bthh_report_type_t::from(report_type),
            report_ptr.cast_into::<std::os::raw::c_char>()
        ))
    }

    #[profile_enabled_or(BtStatus::NotReady)]
    pub fn send_data(&mut self, addr: &mut RawAddress, data: &mut [u8]) -> BtStatus {
    pub fn send_data(
        &mut self,
        addr: &mut RawAddress,
        address_type: BtAddrType,
        transport: BtTransport,
        data: &mut [u8],
    ) -> BtStatus {
        let addr_ptr = LTCheckedPtrMut::from_ref(addr);
        let data_ptr = LTCheckedPtrMut::from(data);
        BtStatus::from(ccall!(
            self,
            send_data,
            addr_ptr.into(),
            address_type.into(),
            transport.into(),
            data_ptr.cast_into::<std::os::raw::c_char>()
        ))
    }