Skip to content
Snippets Groups Projects
Commit 8f08aedb authored by JohnLai's avatar JohnLai
Browse files

Floss: Caches the discoverable mode

This change caches the discoverable mode in BluetoothQA for later use.

Bug: 278653204
Test: Manually test with btclient:
        adapter discoverable limited 60
        adapter show
Tag: #floss
Change-Id: I8c6de804f12acc88a0545d4182fd653cd6df901b
parent 61a05dc6
No related branches found
No related tags found
No related merge requests found
......@@ -180,7 +180,8 @@ fn main() -> Result<(), Box<dyn Error>> {
tx.clone(),
bluetooth_admin.clone(),
))));
let qa = Arc::new(Mutex::new(Box::new(BluetoothQA::new(tx.clone()))));
let bluetooth_qa = Arc::new(Mutex::new(Box::new(BluetoothQA::new(tx.clone()))));
let dis =
Arc::new(Mutex::new(Box::new(DeviceInformation::new(bluetooth_gatt.clone(), tx.clone()))));
......@@ -226,6 +227,7 @@ fn main() -> Result<(), Box<dyn Error>> {
bt_sock_mgr.clone(),
bluetooth_admin.clone(),
dis.clone(),
bluetooth_qa.clone(),
));
// Set up the disconnect watcher to monitor client disconnects.
......@@ -370,7 +372,11 @@ fn main() -> Result<(), Box<dyn Error>> {
logging.clone(),
);
cr.lock().unwrap().insert(make_object_name(adapter_index, "qa"), &[qa_iface], qa.clone());
cr.lock().unwrap().insert(
make_object_name(adapter_index, "qa"),
&[qa_iface],
bluetooth_qa.clone(),
);
// Hold locks and initialize all interfaces. This must be done AFTER DBus is
// initialized so DBus can properly enforce user policies.
......@@ -410,6 +416,9 @@ fn main() -> Result<(), Box<dyn Error>> {
}
}
// Initialize the bluetooth_qa
bluetooth.lock().unwrap().cache_discoverable_mode_into_qa();
// Serve clients forever.
future::pending::<()>().await;
unreachable!()
......
......@@ -745,7 +745,7 @@ impl Bluetooth {
}
/// Returns adapter's discoverable mode.
pub(crate) fn get_discoverable_mode(&self) -> BtDiscMode {
pub fn get_discoverable_mode(&self) -> BtDiscMode {
let off_mode = BtDiscMode::NonDiscoverable;
match self.properties.get(&BtPropertyType::AdapterScanMode) {
......@@ -761,6 +761,16 @@ impl Bluetooth {
}
}
/// Caches the discoverable mode into BluetoothQA.
pub fn cache_discoverable_mode_into_qa(&self) {
let disc_mode = self.get_discoverable_mode();
let txl = self.tx.clone();
tokio::spawn(async move {
let _ = txl.send(Message::QaOnDiscoverableModeChanged(disc_mode)).await;
});
}
/// Returns all bonded and connected devices.
pub(crate) fn get_bonded_and_connected_devices(&mut self) -> Vec<BluetoothDevice> {
self.bonded_devices
......@@ -1213,6 +1223,8 @@ impl BtifBluetoothCallbacks for Bluetooth {
// Update local property cache
for prop in properties {
self.properties.insert(prop.get_type(), prop.clone());
match &prop {
BluetoothProperty::BdAddr(bdaddr) => {
self.update_local_address(&bdaddr);
......@@ -1241,6 +1253,8 @@ impl BtifBluetoothCallbacks for Bluetooth {
});
}
BluetoothProperty::AdapterScanMode(mode) => {
self.cache_discoverable_mode_into_qa();
self.callbacks.for_all_callbacks(|callback| {
callback
.on_discoverable_changed(*mode == BtScanMode::ConnectableDiscoverable);
......@@ -1249,8 +1263,6 @@ impl BtifBluetoothCallbacks for Bluetooth {
_ => {}
}
self.properties.insert(prop.get_type(), prop.clone());
self.callbacks.for_all_callbacks(|callback| {
callback.on_adapter_property_changed(prop.get_type());
});
......
//! Anything related to the Qualification API (IBluetoothQA).
use crate::Message;
use bt_topshim::btif::BtDiscMode;
use tokio::sync::mpsc::Sender;
/// Defines the Qualification API
......@@ -11,11 +12,16 @@ pub trait IBluetoothQA {
pub struct BluetoothQA {
tx: Sender<Message>,
disc_mode: BtDiscMode,
}
impl BluetoothQA {
pub fn new(tx: Sender<Message>) -> BluetoothQA {
BluetoothQA { tx }
BluetoothQA { tx, disc_mode: BtDiscMode::NonDiscoverable }
}
pub fn handle_discoverable_mode_changed(&mut self, mode: BtDiscMode) {
self.disc_mode = mode;
}
}
......
......@@ -20,6 +20,8 @@ pub mod socket_manager;
pub mod suspend;
pub mod uuid;
use bluetooth_qa::BluetoothQA;
use bt_topshim::btif::BtDiscMode;
use log::debug;
use num_derive::{FromPrimitive, ToPrimitive};
use std::sync::{Arc, Mutex};
......@@ -128,6 +130,7 @@ pub enum Message {
// Qualification Only
QaAddMediaPlayer(String, bool),
QaRfcommSendMsc(u8, String),
QaOnDiscoverableModeChanged(BtDiscMode),
}
/// Represents suspend mode of a module.
......@@ -164,6 +167,7 @@ impl Stack {
bluetooth_socketmgr: Arc<Mutex<Box<BluetoothSocketManager>>>,
bluetooth_admin: Arc<Mutex<Box<BluetoothAdmin>>>,
bluetooth_dis: Arc<Mutex<Box<DeviceInformation>>>,
bluetooth_qa: Arc<Mutex<Box<BluetoothQA>>>,
) {
loop {
let m = rx.recv().await;
......@@ -357,6 +361,9 @@ impl Stack {
Message::QaRfcommSendMsc(dlci, addr) => {
bluetooth_socketmgr.lock().unwrap().rfcomm_send_msc(dlci, addr);
}
Message::QaOnDiscoverableModeChanged(mode) => {
bluetooth_qa.lock().unwrap().handle_discoverable_mode_changed(mode);
}
}
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment