summaryrefslogtreecommitdiff
path: root/system/gd/rust/linux/stack/src
diff options
context:
space:
mode:
Diffstat (limited to 'system/gd/rust/linux/stack/src')
-rw-r--r--system/gd/rust/linux/stack/src/bluetooth.rs117
-rw-r--r--system/gd/rust/linux/stack/src/lib.rs9
2 files changed, 104 insertions, 22 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
}
}
diff --git a/system/gd/rust/linux/stack/src/lib.rs b/system/gd/rust/linux/stack/src/lib.rs
index 160363e88a..ad21e6e617 100644
--- a/system/gd/rust/linux/stack/src/lib.rs
+++ b/system/gd/rust/linux/stack/src/lib.rs
@@ -1,7 +1,7 @@
-//! Fluoride/GD Bluetooth stack.
+//! Floss Bluetooth stack.
//!
-//! This crate provides the API implementation of the Fluoride/GD Bluetooth stack, independent of
-//! any RPC projection.
+//! This crate provides the API implementation of the Fluoride/GD Bluetooth
+//! stack, independent of any RPC projection.
#[macro_use]
extern crate num_derive;
@@ -27,9 +27,6 @@ use bt_topshim::{
},
};
-/// Represents a Bluetooth address.
-// TODO: Add support for LE random addresses.
-
#[derive(Clone, Debug)]
pub enum BluetoothCallbackType {
Adapter,