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

Commit 923593ad authored by Abhishek Pandit-Subedi's avatar Abhishek Pandit-Subedi Committed by Abhishek Pandit-Subedi
Browse files

floss: Implement GetConnectionState

Implement the GetConnectionState dbus api and use it in `device show`
to display the current connection state.

Bug: 196887265
Tag: #floss
Test: btclient on ChromeOS
Change-Id: Ie7dbd71499c2f14c76e5d4a0e356002c555103a0
parent 74d85f37
Loading
Loading
Loading
Loading
+12 −8
Original line number Diff line number Diff line
@@ -357,16 +357,20 @@ impl CommandHandler {
                    name: String::from("Classic Device"),
                };

                let uuids = self
                    .context
                    .lock()
                    .unwrap()
                    .adapter_dbus
                    .as_ref()
                    .unwrap()
                    .get_remote_uuids(device.clone());
                let (bonded, connected, uuids) = {
                    let ctx = self.context.lock().unwrap();
                    let adapter = ctx.adapter_dbus.as_ref().unwrap();

                    let bonded = adapter.get_bond_state(device.clone());
                    let connected = adapter.get_connection_state(device.clone());
                    let uuids = adapter.get_remote_uuids(device.clone());

                    (bonded, connected, uuids)
                };

                print_info!("Address: {}", &device.address);
                print_info!("Bonded: {}", bonded);
                print_info!("Connected: {}", connected);
                print_info!(
                    "Uuids: {}",
                    DisplayList(
+4 −0
Original line number Diff line number Diff line
@@ -335,6 +335,10 @@ impl IBluetooth for BluetoothDBus {
        self.client_proxy.method("GetBondState", (BluetoothDevice::to_dbus(device).unwrap(),))
    }

    fn get_connection_state(&self, device: BluetoothDevice) -> u32 {
        self.client_proxy.method("GetConnectionState", (BluetoothDevice::to_dbus(device).unwrap(),))
    }

    fn get_remote_uuids(&self, device: BluetoothDevice) -> Vec<Uuid128Bit> {
        let result: Vec<Vec<u8>> = self
            .client_proxy
+5 −0
Original line number Diff line number Diff line
@@ -166,6 +166,11 @@ impl IBluetooth for IBluetoothDBus {
        0
    }

    #[dbus_method("GetConnectionState")]
    fn get_connection_state(&self, _device: BluetoothDevice) -> u32 {
        0
    }

    #[dbus_method("GetRemoteUuids")]
    fn get_remote_uuids(&self, _device: BluetoothDevice) -> Vec<Uuid128Bit> {
        vec![]
+16 −2
Original line number Diff line number Diff line
@@ -2,8 +2,8 @@

use bt_topshim::btif::{
    BaseCallbacks, BaseCallbacksDispatcher, BluetoothInterface, BluetoothProperty, BtAclState,
    BtBondState, BtDiscoveryState, BtHciErrorCode, BtPropertyType, BtSspVariant, BtState, BtStatus,
    BtTransport, RawAddress, Uuid, Uuid128Bit,
    BtBondState, BtConnectionState, BtDiscoveryState, BtHciErrorCode, BtPropertyType, BtSspVariant,
    BtState, BtStatus, BtTransport, RawAddress, Uuid, Uuid128Bit,
};
use bt_topshim::{
    profiles::hid_host::{HHCallbacksDispatcher, HidHost},
@@ -87,6 +87,9 @@ pub trait IBluetooth {
    /// Gets the bond state of a single device.
    fn get_bond_state(&self, device: BluetoothDevice) -> u32;

    /// Gets the connection state of a single device.
    fn get_connection_state(&self, device: BluetoothDevice) -> u32;

    /// Returns the cached UUIDs of a remote device.
    fn get_remote_uuids(&self, device: BluetoothDevice) -> Vec<Uuid128Bit>;

@@ -851,6 +854,17 @@ impl IBluetooth for Bluetooth {
        }
    }

    fn get_connection_state(&self, device: BluetoothDevice) -> u32 {
        let addr = RawAddress::from_string(device.address.clone());

        if addr.is_none() {
            warn!("Can't check connection state. Address {} is not valid.", device.address);
            return 0;
        }

        self.intf.lock().unwrap().get_connection_state(&addr.unwrap())
    }

    fn get_remote_uuids(&self, device: BluetoothDevice) -> Vec<Uuid128Bit> {
        // Device must exist in either bonded or found list
        let found = self
+23 −2
Original line number Diff line number Diff line
@@ -81,6 +81,27 @@ impl From<bindings::bt_bond_state_t> for BtBondState {
    }
}

#[derive(Clone, Debug, FromPrimitive, ToPrimitive, PartialEq, PartialOrd)]
#[repr(u32)]
pub enum BtConnectionState {
    NotConnected = 0,
    ConnectedOnly = 1,
    EncryptedBredr = 3,
    EncryptedLe = 5,
}

impl From<i32> for BtConnectionState {
    fn from(item: i32) -> Self {
        let fallback = if item > 0 {
            BtConnectionState::ConnectedOnly
        } else {
            BtConnectionState::NotConnected
        };

        BtConnectionState::from_i32(item).unwrap_or(fallback)
    }
}

#[derive(Clone, Debug, FromPrimitive, ToPrimitive, PartialEq, PartialOrd)]
#[repr(u32)]
pub enum BtAclState {
@@ -884,9 +905,9 @@ impl BluetoothInterface {
        ccall!(self, cancel_bond, ffi_addr)
    }

    pub fn get_connection_state(&self, addr: &RawAddress) -> i32 {
    pub fn get_connection_state(&self, addr: &RawAddress) -> u32 {
        let ffi_addr = cast_to_const_ffi_address!(addr as *const RawAddress);
        ccall!(self, get_connection_state, ffi_addr)
        ccall!(self, get_connection_state, ffi_addr).to_u32().unwrap()
    }

    pub fn pin_reply(