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

Commit 68391577 authored by Sonny Sasaka's avatar Sonny Sasaka
Browse files

Floss: Refactor UuidHelper to use static data

UuidHelper needs to create a HashMap every time it's instantiated. This
patch optimizes so that the HashMap and other constant data can be
created just once using lazy_static.

Bug: 196887265
Tag: #floss
Test: ./build.py --target test

Change-Id: I948b88e213e859bd23130a2afe2644d0859c3c79
parent e886e06c
Loading
Loading
Loading
Loading
+3 −5
Original line number Diff line number Diff line
@@ -335,8 +335,7 @@ impl CommandHandler {
                    let multi_adv_supported = adapter_dbus.is_multi_advertisement_supported();
                    let le_ext_adv_supported = adapter_dbus.is_le_extended_advertising_supported();
                    let wbs_supported = adapter_dbus.is_wbs_supported();
                    let uuid_helper = UuidHelper::new();
                    let enabled_profiles = uuid_helper.get_enabled_profiles();
                    let enabled_profiles = UuidHelper::get_enabled_profiles();
                    let connected_profiles: Vec<Profile> = enabled_profiles
                        .iter()
                        .filter(|&&prof| adapter_dbus.get_profile_connection_state(prof) > 0)
@@ -358,7 +357,7 @@ impl CommandHandler {
                        DisplayList(
                            uuids
                                .iter()
                                .map(|&x| uuid_helper.known_uuid_to_string(&x))
                                .map(|&x| UuidHelper::known_uuid_to_string(&x))
                                .collect::<Vec<String>>()
                        )
                    );
@@ -637,7 +636,6 @@ impl CommandHandler {
                        )
                    };

                    let uuid_helper = UuidHelper::new();
                    print_info!("Address: {}", &device.address);
                    print_info!("Name: {}", name);
                    print_info!("Alias: {}", alias);
@@ -652,7 +650,7 @@ impl CommandHandler {
                        DisplayList(
                            uuids
                                .iter()
                                .map(|&x| uuid_helper.known_uuid_to_string(&x))
                                .map(|&x| UuidHelper::known_uuid_to_string(&x))
                                .collect::<Vec<String>>()
                        )
                    );
+1 −0
Original line number Diff line number Diff line
@@ -12,6 +12,7 @@ btif_macros = { path = "btif_macros" }

dbus = "0.9.2"
itertools = "0.10.5"
lazy_static = "1.4"
log = "0.4.14"
nix = "0.19"
num-derive = "0.3"
+8 −11
Original line number Diff line number Diff line
@@ -349,7 +349,6 @@ pub struct Bluetooth {
    sdp: Option<Sdp>,
    state: BtState,
    tx: Sender<Message>,
    uuid_helper: UuidHelper,
    /// Used to delay connection until we have SDP results.
    wait_to_connect: bool,
    // Internal API members
@@ -388,7 +387,6 @@ impl Bluetooth {
            sdp: None,
            state: BtState::Off,
            tx,
            uuid_helper: UuidHelper::new(),
            wait_to_connect: false,
            // Internal API members
            discoverable_timeout: None,
@@ -1424,10 +1422,9 @@ impl IBluetooth for Bluetooth {
        // Wake is allowed if the device supports HIDP or HOGP only.
        match self.get_remote_device_property(&device, &BtPropertyType::Uuids) {
            Some(BluetoothProperty::Uuids(uuids)) => {
                let uu_helper = UuidHelper::new();
                return uuids.iter().any(|&x| {
                    uu_helper.is_known_profile(&x.uu).map_or(false, |profile| {
                        profile == &Profile::Hid || profile == &Profile::Hogp
                    UuidHelper::is_known_profile(&x.uu).map_or(false, |profile| {
                        profile == Profile::Hid || profile == Profile::Hogp
                    })
                });
            }
@@ -1540,15 +1537,15 @@ impl IBluetooth for Bluetooth {
        let mut has_enabled_uuids = false;
        let uuids = self.get_remote_uuids(device.clone());
        for uuid in uuids.iter() {
            match self.uuid_helper.is_known_profile(uuid) {
            match UuidHelper::is_known_profile(uuid) {
                Some(p) => {
                    if self.uuid_helper.is_profile_enabled(&p) {
                    if UuidHelper::is_profile_enabled(&p) {
                        match p {
                            Profile::Hid | Profile::Hogp => {
                                let status = self.hh.as_ref().unwrap().connect(&mut addr.unwrap());
                                metrics::profile_connection_state_changed(
                                    addr.unwrap(),
                                    *p as u32,
                                    p as u32,
                                    BtStatus::Success,
                                    BthhConnectionState::Connecting as u32,
                                );
@@ -1556,7 +1553,7 @@ impl IBluetooth for Bluetooth {
                                if status != BtStatus::Success {
                                    metrics::profile_connection_state_changed(
                                        addr.unwrap(),
                                        *p as u32,
                                        p as u32,
                                        status,
                                        BthhConnectionState::Disconnected as u32,
                                    );
@@ -1604,9 +1601,9 @@ impl IBluetooth for Bluetooth {

        let uuids = self.get_remote_uuids(device.clone());
        for uuid in uuids.iter() {
            match self.uuid_helper.is_known_profile(uuid) {
            match UuidHelper::is_known_profile(uuid) {
                Some(p) => {
                    if self.uuid_helper.is_profile_enabled(&p) {
                    if UuidHelper::is_profile_enabled(&p) {
                        match p {
                            Profile::Hid | Profile::Hogp => {
                                self.hh.as_ref().unwrap().disconnect(&mut addr.unwrap());
+2 −4
Original line number Diff line number Diff line
@@ -660,16 +660,14 @@ impl BluetoothMedia {
            let audio_profiles =
                vec![uuid::Profile::A2dpSink, uuid::Profile::Hfp, uuid::Profile::AvrcpController];

            let uuid_helper = uuid::UuidHelper::new();

            adapter
                .lock()
                .unwrap()
                .get_remote_uuids(device)
                .into_iter()
                .map(|u| uuid_helper.is_known_profile(&u))
                .map(|u| uuid::UuidHelper::is_known_profile(&u))
                .filter(|u| u.is_some())
                .map(|u| *u.unwrap())
                .map(|u| u.unwrap())
                .filter(|u| audio_profiles.contains(&u))
                .collect()
        } else {
+2 −0
Original line number Diff line number Diff line
@@ -5,6 +5,8 @@

#[macro_use]
extern crate num_derive;
#[macro_use]
extern crate lazy_static;

pub mod battery_manager;
pub mod battery_provider_manager;
Loading