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

Commit 4c54cf6f authored by Hsin-chen Chuang's avatar Hsin-chen Chuang
Browse files

floss: Bluetooth/QA DBus: Change address type to RawAddress

This brings 2 benefits:
- Make it possible to mask the address in the DBus debugging log
- Don't need to convert the type and handle error in every DBus function

Note that this doesn't really change the DBus API. From the clients'
perspective these DBus args are still String.

Bug: 342337056
Tag: #floss
Test: mmm packages/modules/Bluetooth
Test: Call GetHIDReport, verified masked address in the log
Test: bluetooth_AdapterQuickHealth.AVL.all_floss
Test: NearbyShare / PhoneHub / FastPair
Flag: EXEMPT, Floss-only changes
Change-Id: Id1152119b51de039fd22848cb1e21f2158ae54b6
parent d5eeba38
Loading
Loading
Loading
Loading
+16 −9
Original line number Diff line number Diff line
@@ -10,7 +10,9 @@ use crate::dbus_iface::{
};
use crate::{console_red, console_yellow, print_error, print_info};
use crate::{ClientContext, GattRequest};
use bt_topshim::btif::{BtBondState, BtPropertyType, BtSspVariant, BtStatus, Uuid128Bit};
use bt_topshim::btif::{
    BtBondState, BtPropertyType, BtSspVariant, BtStatus, RawAddress, Uuid128Bit,
};
use bt_topshim::profiles::gatt::{AdvertisingStatus, GattStatus, LePhy};
use bt_topshim::profiles::hfp::HfpCodecId;
use bt_topshim::profiles::le_audio::{
@@ -143,8 +145,8 @@ impl IBluetoothCallback for BtCallback {
        print_info!("Bluetooth properties {:?} changed for {:?}", props, remote_device);
    }

    fn on_address_changed(&mut self, addr: String) {
        print_info!("Address changed to {}", &addr);
    fn on_address_changed(&mut self, addr: RawAddress) {
        print_info!("Address changed to {}", addr.to_string());
        self.context.lock().unwrap().adapter_address = Some(addr);
    }

@@ -247,8 +249,13 @@ impl IBluetoothCallback for BtCallback {
        );
    }

    fn on_bond_state_changed(&mut self, status: u32, address: String, state: u32) {
        print_info!("Bonding state changed: [{}] state: {}, Status = {}", address, state, status);
    fn on_bond_state_changed(&mut self, status: u32, address: RawAddress, state: u32) {
        print_info!(
            "Bonding state changed: [{}] state: {}, Status = {}",
            address.to_string(),
            state,
            status
        );

        // Clear bonding attempt if bonding fails or succeeds
        match BtBondState::from(state) {
@@ -257,7 +264,7 @@ impl IBluetoothCallback for BtCallback {
                    self.context.lock().unwrap().bonding_attempt.as_ref().cloned();
                match bonding_attempt {
                    Some(bd) => {
                        if &address == &bd.address {
                        if address.to_string() == bd.address {
                            self.context.lock().unwrap().bonding_attempt = None;
                        }
                    }
@@ -268,16 +275,16 @@ impl IBluetoothCallback for BtCallback {
        }

        let device =
            BluetoothDevice { address: address.clone(), name: String::from("Classic device") };
            BluetoothDevice { address: address.to_string(), name: String::from("Classic device") };

        // If bonded, we should also automatically connect all enabled profiles
        if BtBondState::Bonded == state.into() {
            self.context.lock().unwrap().bonded_devices.insert(address.clone(), device.clone());
            self.context.lock().unwrap().bonded_devices.insert(address.to_string(), device.clone());
            self.context.lock().unwrap().connect_all_enabled_profiles(device.clone());
        }

        if BtBondState::NotBonded == state.into() {
            self.context.lock().unwrap().bonded_devices.remove(&address);
            self.context.lock().unwrap().bonded_devices.remove(&address.to_string());
        }
    }

+10 −11
Original line number Diff line number Diff line
@@ -9,7 +9,9 @@ use crate::bt_gatt::AuthReq;
use crate::callbacks::{BtGattCallback, BtGattServerCallback};
use crate::ClientContext;
use crate::{console_red, console_yellow, print_error, print_info};
use bt_topshim::btif::{BtConnectionState, BtDiscMode, BtStatus, BtTransport, Uuid, INVALID_RSSI};
use bt_topshim::btif::{
    BtConnectionState, BtDiscMode, BtStatus, BtTransport, RawAddress, Uuid, INVALID_RSSI,
};
use bt_topshim::profiles::gatt::{GattStatus, LePhy};
use bt_topshim::profiles::hid_host::BthhReportType;
use bt_topshim::profiles::sdp::{BtSdpMpsRecord, BtSdpRecord};
@@ -532,10 +534,7 @@ impl CommandHandler {
                }

                let enabled = self.lock_context().enabled;
                let address = match self.lock_context().adapter_address.as_ref() {
                    Some(x) => x.clone(),
                    None => String::from(""),
                };
                let address = self.lock_context().adapter_address.unwrap_or_default();
                let context = self.lock_context();
                let adapter_dbus = context.adapter_dbus.as_ref().unwrap();
                let qa_dbus = context.qa_dbus.as_ref().unwrap();
@@ -563,7 +562,7 @@ impl CommandHandler {
                qa_dbus.fetch_connectable();
                qa_dbus.fetch_alias();
                qa_dbus.fetch_discoverable_mode();
                print_info!("Address: {}", address);
                print_info!("Address: {}", address.to_string());
                print_info!("Name: {}", name);
                print_info!("Modalias: {}", modalias);
                print_info!("State: {}", if enabled { "enabled" } else { "disabled" });
@@ -662,7 +661,7 @@ impl CommandHandler {
        }

        let address = self.lock_context().update_adapter_address();
        print_info!("Local address = {}", &address);
        print_info!("Local address = {}", address.to_string());
        Ok(())
    }

@@ -1795,7 +1794,7 @@ impl CommandHandler {
            "send-msc" => {
                let dlci =
                    String::from(get_arg(args, 1)?).parse::<u8>().or(Err("Failed parsing DLCI"))?;
                let addr = String::from(get_arg(args, 2)?);
                let addr = RawAddress::from_string(get_arg(args, 2)?).ok_or("Invalid Address")?;
                self.context.lock().unwrap().qa_dbus.as_mut().unwrap().rfcomm_send_msc(dlci, addr);
            }
            "listen-rfcomm" => {
@@ -1985,7 +1984,7 @@ impl CommandHandler {

        match &command[..] {
            "get-report" => {
                let addr = String::from(get_arg(args, 1)?);
                let addr = RawAddress::from_string(get_arg(args, 1)?).ok_or("Invalid Address")?;
                let report_type = match &get_arg(args, 2)?[..] {
                    "Input" => BthhReportType::InputReport,
                    "Output" => BthhReportType::OutputReport,
@@ -2005,7 +2004,7 @@ impl CommandHandler {
                );
            }
            "set-report" => {
                let addr = String::from(get_arg(args, 1)?);
                let addr = RawAddress::from_string(get_arg(args, 1)?).ok_or("Invalid Address")?;
                let report_type = match &get_arg(args, 2)?[..] {
                    "Input" => BthhReportType::InputReport,
                    "Output" => BthhReportType::OutputReport,
@@ -2023,7 +2022,7 @@ impl CommandHandler {
                );
            }
            "send-data" => {
                let addr = String::from(get_arg(args, 1)?);
                let addr = RawAddress::from_string(get_arg(args, 1)?).ok_or("Invalid Address")?;
                let data = String::from(get_arg(args, 2)?);

                self.context.lock().unwrap().qa_dbus.as_mut().unwrap().send_hid_data(addr, data);
+39 −11
Original line number Diff line number Diff line
@@ -2,7 +2,8 @@

use bt_topshim::btif::{
    BtAddrType, BtBondState, BtConnectionState, BtDeviceType, BtDiscMode, BtPropertyType,
    BtSspVariant, BtStatus, BtTransport, BtVendorProductInfo, Uuid, Uuid128Bit,
    BtSspVariant, BtStatus, BtTransport, BtVendorProductInfo, DisplayAddress, RawAddress, Uuid,
    Uuid128Bit,
};
use bt_topshim::profiles::a2dp::{
    A2dpCodecBitsPerSample, A2dpCodecChannelMode, A2dpCodecConfig, A2dpCodecIndex,
@@ -384,6 +385,33 @@ impl DBusArg for BtSdpRecord {
    }
}

impl DBusArg for RawAddress {
    type DBusType = String;
    fn from_dbus(
        data: String,
        _conn: Option<std::sync::Arc<dbus::nonblock::SyncConnection>>,
        _remote: Option<dbus::strings::BusName<'static>>,
        _disconnect_watcher: Option<
            std::sync::Arc<std::sync::Mutex<dbus_projection::DisconnectWatcher>>,
        >,
    ) -> Result<RawAddress, Box<dyn std::error::Error>> {
        Ok(RawAddress::from_string(data.clone()).ok_or_else(|| {
            format!(
                "Invalid Address: last 6 chars=\"{}\"",
                data.chars().rev().take(6).collect::<String>().chars().rev().collect::<String>()
            )
        })?)
    }

    fn to_dbus(addr: RawAddress) -> Result<String, Box<dyn std::error::Error>> {
        Ok(addr.to_string())
    }

    fn log(addr: &RawAddress) -> String {
        format!("{}", DisplayAddress(addr))
    }
}

#[dbus_propmap(BluetoothGattDescriptor)]
pub struct BluetoothGattDescriptorDBus {
    uuid: Uuid128Bit,
@@ -617,7 +645,7 @@ impl IBluetoothCallback for IBluetoothCallbackDBus {
    }

    #[dbus_method("OnAddressChanged", DBusLog::Disable)]
    fn on_address_changed(&mut self, addr: String) {}
    fn on_address_changed(&mut self, addr: RawAddress) {}

    #[dbus_method("OnNameChanged", DBusLog::Disable)]
    fn on_name_changed(&mut self, name: String) {}
@@ -651,7 +679,7 @@ impl IBluetoothCallback for IBluetoothCallbackDBus {
    fn on_pin_display(&mut self, remote_device: BluetoothDevice, pincode: String) {}

    #[dbus_method("OnBondStateChanged", DBusLog::Disable)]
    fn on_bond_state_changed(&mut self, status: u32, address: String, state: u32) {}
    fn on_bond_state_changed(&mut self, status: u32, address: RawAddress, state: u32) {}

    #[dbus_method("OnSdpSearchComplete", DBusLog::Disable)]
    fn on_sdp_search_complete(
@@ -793,7 +821,7 @@ impl IBluetooth for BluetoothDBus {
    }

    #[dbus_method("GetAddress")]
    fn get_address(&self) -> String {
    fn get_address(&self) -> RawAddress {
        dbus_generated!()
    }

@@ -1075,7 +1103,7 @@ impl IBluetoothQALegacy for BluetoothQALegacyDBus {
    #[dbus_method("GetHIDReport")]
    fn get_hid_report(
        &mut self,
        addr: String,
        addr: RawAddress,
        report_type: BthhReportType,
        report_id: u8,
    ) -> BtStatus {
@@ -1085,7 +1113,7 @@ impl IBluetoothQALegacy for BluetoothQALegacyDBus {
    #[dbus_method("SetHIDReport")]
    fn set_hid_report(
        &mut self,
        addr: String,
        addr: RawAddress,
        report_type: BthhReportType,
        report: String,
    ) -> BtStatus {
@@ -1093,7 +1121,7 @@ impl IBluetoothQALegacy for BluetoothQALegacyDBus {
    }

    #[dbus_method("SendHIDData")]
    fn send_hid_data(&mut self, addr: String, data: String) -> BtStatus;
    fn send_hid_data(&mut self, addr: RawAddress, data: String) -> BtStatus;
}

#[dbus_propmap(AdapterWithEnabled)]
@@ -2497,7 +2525,7 @@ impl IBluetoothQA for BluetoothQADBus {
        dbus_generated!()
    }
    #[dbus_method("RfcommSendMsc")]
    fn rfcomm_send_msc(&self, dlci: u8, addr: String) {
    fn rfcomm_send_msc(&self, dlci: u8, addr: RawAddress) {
        dbus_generated!()
    }
    #[dbus_method("FetchDiscoverableMode")]
@@ -2521,15 +2549,15 @@ impl IBluetoothQA for BluetoothQADBus {
        dbus_generated!()
    }
    #[dbus_method("GetHIDReport")]
    fn get_hid_report(&self, addr: String, report_type: BthhReportType, report_id: u8) {
    fn get_hid_report(&self, addr: RawAddress, report_type: BthhReportType, report_id: u8) {
        dbus_generated!()
    }
    #[dbus_method("SetHIDReport")]
    fn set_hid_report(&self, addr: String, report_type: BthhReportType, report: String) {
    fn set_hid_report(&self, addr: RawAddress, report_type: BthhReportType, report: String) {
        dbus_generated!()
    }
    #[dbus_method("SendHIDData")]
    fn send_hid_data(&self, addr: String, data: String) {
    fn send_hid_data(&self, addr: RawAddress, data: String) {
        dbus_generated!()
    }
}
+4 −4
Original line number Diff line number Diff line
@@ -25,7 +25,7 @@ use crate::dbus_iface::{
    BluetoothTelephonyDBus, SuspendDBus,
};
use crate::editor::AsyncEditor;
use bt_topshim::topstack;
use bt_topshim::{btif::RawAddress, topstack};
use btstack::bluetooth::{BluetoothDevice, IBluetooth};
use btstack::suspend::ISuspend;
use manager_service::iface_bluetooth_manager::IBluetoothManager;
@@ -64,7 +64,7 @@ pub(crate) struct ClientContext {
    pub(crate) adapter_ready: bool,

    /// Current adapter address if known.
    pub(crate) adapter_address: Option<String>,
    pub(crate) adapter_address: Option<RawAddress>,

    /// Currently active bonding attempt. If it is not none, we are currently attempting to bond
    /// this device.
@@ -281,7 +281,7 @@ impl ClientContext {
    }

    // Foreground-only: Updates the adapter address.
    fn update_adapter_address(&mut self) -> String {
    fn update_adapter_address(&mut self) -> RawAddress {
        let address = self.adapter_dbus.as_ref().unwrap().get_address();
        self.adapter_address = Some(address.clone());

@@ -759,7 +759,7 @@ async fn handle_client_command(
                let adapter_address = context.lock().unwrap().update_adapter_address();
                context.lock().unwrap().update_bonded_devices();

                print_info!("Adapter {} is ready", adapter_address);
                print_info!("Adapter {} is ready", adapter_address.to_string());

                // Run the command with the command arguments as the client is
                // non-interactive.
+35 −7
Original line number Diff line number Diff line
use bt_topshim::btif::{
    BtAddrType, BtBondState, BtConnectionState, BtDeviceType, BtDiscMode, BtPropertyType,
    BtSspVariant, BtStatus, BtTransport, BtVendorProductInfo, Uuid, Uuid128Bit,
    BtSspVariant, BtStatus, BtTransport, BtVendorProductInfo, DisplayAddress, RawAddress, Uuid,
    Uuid128Bit,
};
use bt_topshim::profiles::socket::SocketType;
use bt_topshim::profiles::ProfileConnectionState;
@@ -90,7 +91,7 @@ impl IBluetoothCallback for BluetoothCallbackDBus {
        dbus_generated!()
    }
    #[dbus_method("OnAddressChanged")]
    fn on_address_changed(&mut self, addr: String) {
    fn on_address_changed(&mut self, addr: RawAddress) {
        dbus_generated!()
    }
    #[dbus_method("OnNameChanged")]
@@ -138,7 +139,7 @@ impl IBluetoothCallback for BluetoothCallbackDBus {
        "OnBondStateChanged",
        DBusLog::Enable(DBusLogOptions::LogAll, DBusLogVerbosity::Verbose)
    )]
    fn on_bond_state_changed(&mut self, status: u32, address: String, state: u32) {
    fn on_bond_state_changed(&mut self, status: u32, address: RawAddress, state: u32) {
        dbus_generated!()
    }
    #[dbus_method("OnSdpSearchComplete")]
@@ -425,6 +426,33 @@ impl DBusArg for BtSdpRecord {
    }
}

impl DBusArg for RawAddress {
    type DBusType = String;
    fn from_dbus(
        data: String,
        _conn: Option<std::sync::Arc<dbus::nonblock::SyncConnection>>,
        _remote: Option<dbus::strings::BusName<'static>>,
        _disconnect_watcher: Option<
            std::sync::Arc<std::sync::Mutex<dbus_projection::DisconnectWatcher>>,
        >,
    ) -> Result<RawAddress, Box<dyn std::error::Error>> {
        Ok(RawAddress::from_string(data.clone()).ok_or_else(|| {
            format!(
                "Invalid Address: last 6 chars=\"{}\"",
                data.chars().rev().take(6).collect::<String>().chars().rev().collect::<String>()
            )
        })?)
    }

    fn to_dbus(addr: RawAddress) -> Result<String, Box<dyn std::error::Error>> {
        Ok(addr.to_string())
    }

    fn log(addr: &RawAddress) -> String {
        format!("{}", DisplayAddress(addr))
    }
}

impl_dbus_arg_enum!(BtDiscMode);
impl_dbus_arg_from_into!(EscoCodingFormat, u8);

@@ -482,7 +510,7 @@ impl IBluetooth for IBluetoothDBus {
    }

    #[dbus_method("GetAddress", DBusLog::Disable)]
    fn get_address(&self) -> String {
    fn get_address(&self) -> RawAddress {
        dbus_generated!()
    }

@@ -1020,7 +1048,7 @@ impl IBluetoothQALegacy for IBluetoothQALegacyDBus {
    #[dbus_method("GetHIDReport")]
    fn get_hid_report(
        &mut self,
        addr: String,
        addr: RawAddress,
        report_type: BthhReportType,
        report_id: u8,
    ) -> BtStatus {
@@ -1030,7 +1058,7 @@ impl IBluetoothQALegacy for IBluetoothQALegacyDBus {
    #[dbus_method("SetHIDReport")]
    fn set_hid_report(
        &mut self,
        addr: String,
        addr: RawAddress,
        report_type: BthhReportType,
        report: String,
    ) -> BtStatus {
@@ -1038,7 +1066,7 @@ impl IBluetoothQALegacy for IBluetoothQALegacyDBus {
    }

    #[dbus_method("SendHIDData")]
    fn send_hid_data(&mut self, addr: String, data: String) -> BtStatus {
    fn send_hid_data(&mut self, addr: RawAddress, data: String) -> BtStatus {
        dbus_generated!()
    }
}
Loading