From 092f7c50c882e9409f927a8009595712d2dfee74 Mon Sep 17 00:00:00 2001 From: Jesse Melhuish Date: Wed, 2 Mar 2022 23:56:10 +0000 Subject: floss: Add set-alias to btclient and API. Adds support for changing the remote device alias. This also fixes the BluetoothProperty conversions which were being done incorrectly (off-by-one). Bug: 196887190 Tag: #floss Test: Built floss, verified alias can be updated in btclient. Change-Id: I2c21feb9bb6763c4147997f349b06904c4bff20d --- system/gd/rust/linux/stack/src/bluetooth.rs | 46 +++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) (limited to 'system/gd/rust/linux/stack/src') diff --git a/system/gd/rust/linux/stack/src/bluetooth.rs b/system/gd/rust/linux/stack/src/bluetooth.rs index f2a610d83b..3728d1a04f 100644 --- a/system/gd/rust/linux/stack/src/bluetooth.rs +++ b/system/gd/rust/linux/stack/src/bluetooth.rs @@ -131,6 +131,9 @@ pub trait IBluetooth { /// Gets the alias of the remote device. fn get_remote_alias(&self, device: BluetoothDevice) -> String; + /// Sets the alias of the remote device. + fn set_remote_alias(&mut self, device: BluetoothDevice, new_alias: String); + /// Gets the class of the remote device. fn get_remote_class(&self, device: BluetoothDevice) -> u32; @@ -399,6 +402,16 @@ impl Bluetooth { self.bonded_devices.get(&device.address).or_else(|| self.found_devices.get(&device.address)) } + fn get_remote_device_if_found_mut( + &mut self, + device: &BluetoothDevice, + ) -> Option<&mut BluetoothDeviceContext> { + match self.bonded_devices.get_mut(&device.address) { + None => self.found_devices.get_mut(&device.address), + some => some, + } + } + fn get_remote_device_property( &self, device: &BluetoothDevice, @@ -407,6 +420,31 @@ impl Bluetooth { self.get_remote_device_if_found(&device) .and_then(|d| d.properties.get(property_type).and_then(|p| Some(p.clone()))) } + + fn set_remote_device_property( + &mut self, + device: &BluetoothDevice, + property_type: BtPropertyType, + property: BluetoothProperty, + ) -> Result<(), ()> { + let mut remote_device = match self.get_remote_device_if_found_mut(&device) { + Some(d) => d, + None => { + return Err(()); + } + }; + + let mut addr = RawAddress::from_string(device.address.clone()); + if addr.is_none() { + return Err(()); + } + let addr = addr.as_mut().unwrap(); + + // TODO: Determine why a callback isn't invoked to do this. + remote_device.properties.insert(property_type, property.clone()); + self.intf.lock().unwrap().set_remote_device_property(addr, property); + Ok(()) + } } #[btif_callbacks_dispatcher(Bluetooth, dispatch_base_callbacks, BaseCallbacks)] @@ -1115,6 +1153,14 @@ impl IBluetooth for Bluetooth { } } + fn set_remote_alias(&mut self, device: BluetoothDevice, new_alias: String) { + let _ = self.set_remote_device_property( + &device, + BtPropertyType::RemoteFriendlyName, + BluetoothProperty::RemoteFriendlyName(new_alias), + ); + } + fn get_remote_class(&self, device: BluetoothDevice) -> u32 { match self.get_remote_device_property(&device, &BtPropertyType::ClassOfDevice) { Some(BluetoothProperty::ClassOfDevice(class)) => return class, -- cgit v1.2.3