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

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

floss: GetProperties on enable and cache results

When the adapter is enabled, trigger a GetProperties call and cache the
results from |AdapterPropertiesChanged| callback.

Bug: 197021037
Test: Build + verify on ChromeOS
Tag: #floss
Change-Id: I54343e46f745944b4ee959910265b021f8b0f3ff
parent e8e15b0a
Loading
Loading
Loading
Loading
+30 −10
Original line number Diff line number Diff line
@@ -2,7 +2,7 @@

use bt_topshim::btif::{
    BaseCallbacks, BaseCallbacksDispatcher, BluetoothInterface, BluetoothProperty, BtBondState,
    BtDiscoveryState, BtSspVariant, BtState, BtStatus, BtTransport, RawAddress,
    BtDiscoveryState, BtPropertyType, BtSspVariant, BtState, BtStatus, BtTransport, RawAddress,
};
use bt_topshim::profiles::hid_host::{HHCallbacksDispatcher, HidHost};
use bt_topshim::topstack;
@@ -11,6 +11,7 @@ use btif_macros::{btif_callback, btif_callbacks_dispatcher};

use num_traits::cast::ToPrimitive;

use std::collections::HashMap;
use std::sync::Arc;
use std::sync::Mutex;

@@ -106,13 +107,15 @@ pub trait IBluetoothCallback: RPCProxy {
/// Implementation of the adapter API.
pub struct Bluetooth {
    intf: Arc<Mutex<BluetoothInterface>>,
    state: BtState,

    bluetooth_media: Arc<Mutex<Box<BluetoothMedia>>>,
    callbacks: Vec<(u32, Box<dyn IBluetoothCallback + Send>)>,
    callbacks_last_id: u32,
    tx: Sender<Message>,
    local_address: Option<RawAddress>,
    hh: Option<HidHost>,
    bluetooth_media: Arc<Mutex<Box<BluetoothMedia>>>,
    local_address: Option<RawAddress>,
    properties: HashMap<BtPropertyType, BluetoothProperty>,
    state: BtState,
    tx: Sender<Message>,
}

impl Bluetooth {
@@ -123,14 +126,15 @@ impl Bluetooth {
        bluetooth_media: Arc<Mutex<Box<BluetoothMedia>>>,
    ) -> Bluetooth {
        Bluetooth {
            tx,
            intf,
            state: BtState::Off,
            callbacks: vec![],
            callbacks_last_id: 0,
            local_address: None,
            hh: None,
            bluetooth_media,
            intf,
            local_address: None,
            properties: HashMap::new(),
            state: BtState::Off,
            tx,
        }
    }

@@ -215,11 +219,24 @@ pub fn get_bt_dispatcher(tx: Sender<Message>) -> BaseCallbacksDispatcher {

impl BtifBluetoothCallbacks for Bluetooth {
    fn adapter_state_changed(&mut self, state: BtState) {
        let prev_state = self.state.clone();
        self.state = state;

        // If it's the same state as before, no further action
        if self.state == prev_state {
            return;
        }

        if self.state == BtState::On {
            self.bluetooth_media.lock().unwrap().initialize();
        }

        if self.state == BtState::Off {
            self.properties.clear();
        } else {
            // Trigger properties update
            self.intf.lock().unwrap().get_adapter_properties();
        }
    }

    #[allow(unused_variables)]
@@ -233,13 +250,16 @@ impl BtifBluetoothCallbacks for Bluetooth {
            return;
        }

        // Update local property cache
        for prop in properties {
            match prop {
            match &prop {
                BluetoothProperty::BdAddr(bdaddr) => {
                    self.update_local_address(&bdaddr);
                }
                _ => {}
            }

            self.properties.insert(prop.get_type(), prop);
        }
    }

+1 −1
Original line number Diff line number Diff line
@@ -102,7 +102,7 @@ pub enum BtDeviceType {
    Dual,
}

#[derive(Clone, Debug, FromPrimitive, ToPrimitive, PartialEq, PartialOrd)]
#[derive(Clone, Debug, Eq, Hash, FromPrimitive, ToPrimitive, PartialEq, PartialOrd)]
#[repr(u32)]
pub enum BtPropertyType {
    BdName = 0x1,