diff options
author | Scott Lobdell <slobdell@google.com> | 2022-03-11 19:27:17 +0000 |
---|---|---|
committer | Scott Lobdell <slobdell@google.com> | 2022-03-11 19:57:09 +0000 |
commit | c9218ef1b82430a07d94f74c212a30e7ccc52975 (patch) | |
tree | 241b7fdeb6bdf1cf3af925ba8996f18faa8973d9 /system/gd/rust/linux/stack/src/bluetooth.rs | |
parent | a26bda4d37221f2f7ef750b413502091e3bcddd4 (diff) | |
parent | 480d2270b269a0e47bf475eb439111f3f966e2a9 (diff) |
Merge TP1A.220225.003
Change-Id: Id71ac466dbfe3707fe2e544ce22b1da8f474ec2b
Diffstat (limited to 'system/gd/rust/linux/stack/src/bluetooth.rs')
-rw-r--r-- | system/gd/rust/linux/stack/src/bluetooth.rs | 117 |
1 files changed, 101 insertions, 16 deletions
diff --git a/system/gd/rust/linux/stack/src/bluetooth.rs b/system/gd/rust/linux/stack/src/bluetooth.rs index ee0578c958..c3b1d3218f 100644 --- a/system/gd/rust/linux/stack/src/bluetooth.rs +++ b/system/gd/rust/linux/stack/src/bluetooth.rs @@ -2,8 +2,8 @@ use bt_topshim::btif::{ BaseCallbacks, BaseCallbacksDispatcher, BluetoothInterface, BluetoothProperty, BtAclState, - BtBondState, BtDiscoveryState, BtHciErrorCode, BtPinCode, BtPropertyType, BtSspVariant, - BtState, BtStatus, BtTransport, RawAddress, Uuid, Uuid128Bit, + BtBondState, BtDiscoveryState, BtHciErrorCode, BtLocalLeFeatures, BtPinCode, BtPropertyType, + BtScanMode, BtSspVariant, BtState, BtStatus, BtTransport, RawAddress, Uuid, Uuid128Bit, }; use bt_topshim::{ profiles::hid_host::{HHCallbacksDispatcher, HidHost}, @@ -25,6 +25,9 @@ use crate::bluetooth_media::{BluetoothMedia, IBluetoothMedia, MediaActions}; use crate::uuid::{Profile, UuidHelper}; use crate::{BluetoothCallbackType, Message, RPCProxy}; +const DEFAULT_DISCOVERY_TIMEOUT_MS: u64 = 12800; +const MIN_ADV_INSTANCES_FOR_MULTI_ADV: u8 = 5; + /// Defines the adapter API. pub trait IBluetooth { /// Adds a callback from a client who wishes to observe adapter events. @@ -67,6 +70,19 @@ pub trait IBluetooth { /// Sets the bluetooth class. fn set_bluetooth_class(&self, cod: u32) -> bool; + /// Returns whether the adapter is discoverable. + fn get_discoverable(&self) -> bool; + + /// Sets discoverability. If discoverable, limits the duration with given value. + fn set_discoverable(&self, mode: bool, duration: u32) -> bool; + + /// Returns whether multi-advertisement is supported. + /// A minimum number of 5 advertising instances is required for multi-advertisment support. + fn is_multi_advertisement_supported(&self) -> bool; + + /// Returns whether LE extended advertising is supported. + fn is_le_extended_advertising_supported(&self) -> bool; + /// Starts BREDR Inquiry. fn start_discovery(&self) -> bool; @@ -243,6 +259,7 @@ pub struct Bluetooth { state: BtState, tx: Sender<Message>, uuid_helper: UuidHelper, + is_connectable: bool, } impl Bluetooth { @@ -269,6 +286,7 @@ impl Bluetooth { state: BtState::Off, tx, uuid_helper: UuidHelper::new(), + is_connectable: false, } } @@ -322,6 +340,29 @@ impl Bluetooth { } } + fn get_connectable(&self) -> bool { + match self.properties.get(&BtPropertyType::AdapterScanMode) { + Some(prop) => match prop { + BluetoothProperty::AdapterScanMode(mode) => match *mode { + BtScanMode::Connectable | BtScanMode::ConnectableDiscoverable => true, + _ => false, + }, + _ => false, + }, + _ => false, + } + } + + fn set_connectable(&mut self, mode: bool) -> bool { + self.is_connectable = mode; + if mode && self.get_discoverable() { + return true; + } + self.intf.lock().unwrap().set_adapter_property(BluetoothProperty::AdapterScanMode( + if mode { BtScanMode::Connectable } else { BtScanMode::None_ }, + )) == 0 + } + pub(crate) fn callback_disconnected(&mut self, id: u32, cb_type: BluetoothCallbackType) { match cb_type { BluetoothCallbackType::Adapter => { @@ -786,6 +827,59 @@ impl IBluetooth for Bluetooth { self.intf.lock().unwrap().set_adapter_property(BluetoothProperty::ClassOfDevice(cod)) == 0 } + fn get_discoverable(&self) -> bool { + match self.properties.get(&BtPropertyType::AdapterScanMode) { + Some(prop) => match prop { + BluetoothProperty::AdapterScanMode(mode) => match mode { + BtScanMode::ConnectableDiscoverable => true, + _ => false, + }, + _ => false, + }, + _ => false, + } + } + + fn set_discoverable(&self, mode: bool, duration: u32) -> bool { + self.intf + .lock() + .unwrap() + .set_adapter_property(BluetoothProperty::AdapterDiscoverableTimeout(duration)); + self.intf.lock().unwrap().set_adapter_property(BluetoothProperty::AdapterScanMode( + if mode { + BtScanMode::ConnectableDiscoverable + } else { + if self.is_connectable { + BtScanMode::Connectable + } else { + BtScanMode::None_ + } + }, + )) == 0 + } + + fn is_multi_advertisement_supported(&self) -> bool { + match self.properties.get(&BtPropertyType::LocalLeFeatures) { + Some(prop) => match prop { + BluetoothProperty::LocalLeFeatures(llf) => { + llf.max_adv_instance >= MIN_ADV_INSTANCES_FOR_MULTI_ADV + } + _ => false, + }, + _ => false, + } + } + + fn is_le_extended_advertising_supported(&self) -> bool { + match self.properties.get(&BtPropertyType::LocalLeFeatures) { + Some(prop) => match prop { + BluetoothProperty::LocalLeFeatures(llf) => llf.le_extended_advertising_supported, + _ => false, + }, + _ => false, + } + } + fn start_discovery(&self) -> bool { self.intf.lock().unwrap().start_discovery() == 0 } @@ -803,20 +897,11 @@ impl IBluetooth for Bluetooth { return 0; } - match self.properties.get(&BtPropertyType::AdapterDiscoveryTimeout) { - Some(variant) => match variant { - BluetoothProperty::AdapterDiscoveryTimeout(timeout) => { - let seconds: u64 = (*timeout).into(); - let elapsed = self.discovering_started.elapsed(); - if elapsed.as_secs() >= seconds { - 0 - } else { - seconds * 1000 - elapsed.as_millis() as u64 - } - } - _ => 0, - }, - _ => 0, + let elapsed_ms = self.discovering_started.elapsed().as_millis() as u64; + if elapsed_ms >= DEFAULT_DISCOVERY_TIMEOUT_MS { + 0 + } else { + DEFAULT_DISCOVERY_TIMEOUT_MS - elapsed_ms } } |