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

Commit b65670a4 authored by Abhishek Pandit-Subedi's avatar Abhishek Pandit-Subedi Committed by Gerrit Code Review
Browse files

Merge "floss: Refactor GetProfileConnectionState"

parents 9bde803d ac69da6c
Loading
Loading
Loading
Loading
+10 −4
Original line number Diff line number Diff line
@@ -9,7 +9,7 @@ use crate::callbacks::{BtGattCallback, BtGattServerCallback};
use crate::ClientContext;
use crate::{console_red, console_yellow, print_error, print_info};
use bt_topshim::btif::{BtConnectionState, BtStatus, BtTransport};
use bt_topshim::profiles::gatt::LePhy;
use bt_topshim::profiles::{gatt::LePhy, ProfileConnectionState};
use btstack::bluetooth::{BluetoothDevice, IBluetooth, IBluetoothQA};
use btstack::bluetooth_gatt::{GattWriteType, IBluetoothGatt, ScanSettings, ScanType};
use btstack::socket_manager::{IBluetoothSocketManager, SocketResult};
@@ -401,10 +401,16 @@ impl CommandHandler {
                let le_ext_adv_supported = adapter_dbus.is_le_extended_advertising_supported();
                let wbs_supported = adapter_dbus.is_wbs_supported();
                let supported_profiles = UuidHelper::get_supported_profiles();
                let connected_profiles: Vec<Profile> = supported_profiles
                let connected_profiles: Vec<(Profile, ProfileConnectionState)> = supported_profiles
                    .iter()
                    .filter(|&&prof| adapter_dbus.get_profile_connection_state(prof) > 0)
                    .cloned()
                    .map(|&prof| {
                        if let Some(uuid) = UuidHelper::get_profile_uuid(&prof) {
                            (prof, adapter_dbus.get_profile_connection_state(uuid.clone()))
                        } else {
                            (prof, ProfileConnectionState::Disconnected)
                        }
                    })
                    .filter(|(_prof, state)| state != &ProfileConnectionState::Disconnected)
                    .collect();
                print_info!("Address: {}", address);
                print_info!("Name: {}", name);
+3 −3
Original line number Diff line number Diff line
@@ -6,6 +6,7 @@ use bt_topshim::btif::{
};
use bt_topshim::profiles::gatt::{AdvertisingStatus, GattStatus, LePhy};
use bt_topshim::profiles::socket::SocketType;
use bt_topshim::profiles::ProfileConnectionState;

use btstack::bluetooth::{
    BluetoothDevice, IBluetooth, IBluetoothCallback, IBluetoothConnectionCallback, IBluetoothQA,
@@ -29,7 +30,6 @@ use btstack::{RPCProxy, SuspendMode};

use btstack::suspend::{ISuspend, ISuspendCallback, SuspendType};

use btstack::uuid::Profile;
use dbus::arg::RefArg;
use dbus::nonblock::SyncConnection;

@@ -69,7 +69,7 @@ impl_dbus_arg_enum!(GattStatus);
impl_dbus_arg_enum!(GattWriteRequestStatus);
impl_dbus_arg_enum!(GattWriteType);
impl_dbus_arg_enum!(LePhy);
impl_dbus_arg_enum!(Profile);
impl_dbus_arg_enum!(ProfileConnectionState);
impl_dbus_arg_enum!(ScanType);
impl_dbus_arg_enum!(SocketType);
impl_dbus_arg_enum!(SuspendMode);
@@ -572,7 +572,7 @@ impl IBluetooth for BluetoothDBus {
    }

    #[dbus_method("GetProfileConnectionState")]
    fn get_profile_connection_state(&self, profile: Profile) -> u32 {
    fn get_profile_connection_state(&self, profile: Uuid128Bit) -> ProfileConnectionState {
        dbus_generated!()
    }

+3 −3
Original line number Diff line number Diff line
@@ -3,6 +3,7 @@ use bt_topshim::btif::{
    BtTransport, Uuid, Uuid128Bit,
};
use bt_topshim::profiles::socket::SocketType;
use bt_topshim::profiles::ProfileConnectionState;

use btstack::bluetooth::{
    Bluetooth, BluetoothDevice, IBluetooth, IBluetoothCallback, IBluetoothConnectionCallback,
@@ -13,7 +14,6 @@ use btstack::socket_manager::{
    IBluetoothSocketManager, IBluetoothSocketManagerCallbacks, SocketId, SocketResult,
};
use btstack::suspend::{ISuspend, ISuspendCallback, Suspend, SuspendType};
use btstack::uuid::Profile;
use btstack::RPCProxy;

use dbus::arg::RefArg;
@@ -117,7 +117,7 @@ impl_dbus_arg_enum!(BtDeviceType);
impl_dbus_arg_enum!(BtPropertyType);
impl_dbus_arg_enum!(BtSspVariant);
impl_dbus_arg_enum!(BtTransport);
impl_dbus_arg_enum!(Profile);
impl_dbus_arg_enum!(ProfileConnectionState);

#[allow(dead_code)]
struct BluetoothConnectionCallbackDBus {}
@@ -340,7 +340,7 @@ impl IBluetooth for IBluetoothDBus {
    }

    #[dbus_method("GetProfileConnectionState")]
    fn get_profile_connection_state(&self, profile: Profile) -> u32 {
    fn get_profile_connection_state(&self, profile: Uuid128Bit) -> ProfileConnectionState {
        dbus_generated!()
    }

+15 −10
Original line number Diff line number Diff line
@@ -14,6 +14,7 @@ use bt_topshim::{
        HHCallbacksDispatcher, HidHost,
    },
    profiles::sdp::{BtSdpRecord, Sdp, SdpCallbacks, SdpCallbacksDispatcher},
    profiles::ProfileConnectionState,
    topstack,
};

@@ -174,7 +175,7 @@ pub trait IBluetooth {
    fn get_connection_state(&self, device: BluetoothDevice) -> BtConnectionState;

    /// Gets the connection state of a specific profile.
    fn get_profile_connection_state(&self, profile: Profile) -> u32;
    fn get_profile_connection_state(&self, profile: Uuid128Bit) -> ProfileConnectionState;

    /// Returns the cached UUIDs of a remote device.
    fn get_remote_uuids(&self, device: BluetoothDevice) -> Vec<Uuid128Bit>;
@@ -1693,8 +1694,9 @@ impl IBluetooth for Bluetooth {
        self.intf.lock().unwrap().get_connection_state(&addr.unwrap())
    }

    fn get_profile_connection_state(&self, profile: Profile) -> u32 {
        match profile {
    fn get_profile_connection_state(&self, profile: Uuid128Bit) -> ProfileConnectionState {
        if let Some(known) = UuidHelper::is_known_profile(&profile) {
            match known {
                Profile::A2dpSink | Profile::A2dpSource => {
                    self.bluetooth_media.lock().unwrap().get_a2dp_connection_state()
                }
@@ -1702,7 +1704,10 @@ impl IBluetooth for Bluetooth {
                    self.bluetooth_media.lock().unwrap().get_hfp_connection_state()
                }
                // TODO: (b/223431229) Profile::Hid and Profile::Hogp
            _ => 0,
                _ => ProfileConnectionState::Disconnected,
            }
        } else {
            ProfileConnectionState::Disconnected
        }
    }

+62 −9
Original line number Diff line number Diff line
@@ -15,11 +15,11 @@ use bt_topshim::profiles::hfp::{
    BthfAudioState, BthfConnectionState, Hfp, HfpCallbacks, HfpCallbacksDispatcher,
    HfpCodecCapability,
};
use bt_topshim::profiles::ProfileConnectionState;
use bt_topshim::{metrics, topstack};
use bt_utils::uinput::UInput;

use log::{debug, info, warn};
use num_traits::cast::ToPrimitive;
use std::collections::{HashMap, HashSet};
use std::convert::TryFrom;
use std::sync::Arc;
@@ -806,18 +806,71 @@ impl BluetoothMedia {
        }
    }

    pub fn get_hfp_connection_state(&self) -> u32 {
    pub fn get_hfp_connection_state(&self) -> ProfileConnectionState {
        if self.hfp_audio_state.values().any(|state| *state == BthfAudioState::Connected) {
            ProfileConnectionState::Active
        } else {
            let mut winning_state = ProfileConnectionState::Disconnected;
            for state in self.hfp_states.values() {
            return BthfConnectionState::to_u32(state).unwrap_or(0);
                // Grab any state higher than the current state.
                match state {
                    // Any SLC completed state means the profile is connected.
                    BthfConnectionState::SlcConnected => {
                        winning_state = ProfileConnectionState::Connected;
                    }

                    // Connecting or Connected are both counted as connecting for profile state
                    // since it's not a complete connection.
                    BthfConnectionState::Connecting | BthfConnectionState::Connected
                        if winning_state != ProfileConnectionState::Connected =>
                    {
                        winning_state = ProfileConnectionState::Connecting;
                    }

                    BthfConnectionState::Disconnecting
                        if winning_state == ProfileConnectionState::Disconnected =>
                    {
                        winning_state = ProfileConnectionState::Disconnecting;
                    }

                    _ => (),
                }
            }

            winning_state
        }
        0
    }

    pub fn get_a2dp_connection_state(&self) -> u32 {
    pub fn get_a2dp_connection_state(&self) -> ProfileConnectionState {
        if self.a2dp_audio_state.values().any(|state| *state == BtavAudioState::Started) {
            ProfileConnectionState::Active
        } else {
            let mut winning_state = ProfileConnectionState::Disconnected;
            for state in self.a2dp_states.values() {
            return BtavConnectionState::to_u32(state).unwrap_or(0);
                // Grab any state higher than the current state.
                match state {
                    BtavConnectionState::Connected => {
                        winning_state = ProfileConnectionState::Connected;
                    }

                    BtavConnectionState::Connecting
                        if winning_state != ProfileConnectionState::Connected =>
                    {
                        winning_state = ProfileConnectionState::Connecting;
                    }

                    BtavConnectionState::Disconnecting
                        if winning_state == ProfileConnectionState::Disconnected =>
                    {
                        winning_state = ProfileConnectionState::Disconnecting;
                    }

                    _ => (),
                }
            }

            winning_state
        }
        0
    }
}

Loading