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

Commit e8e15b0a authored by Abhishek Pandit-Subedi's avatar Abhishek Pandit-Subedi Committed by Abhishek Pandit-Subedi
Browse files

floss: Replace BtProperty with BluetoothProperty

Replace all instances of topshim::btif::BtProperty with
BluetoothProperty, which is an enum variant. This will make it easier
for the stack to deal with properties.

Bug: 197021037
Tag: #floss
Test: Test btclient on ChromeOS
Change-Id: I12b8af74169960e0576e39771dc67dfafbd53e8a
parent 4ccc335e
Loading
Loading
Loading
Loading
+17 −19
Original line number Diff line number Diff line
//! Anything related to the adapter API (IBluetooth).

use bt_topshim::btif::{
    BaseCallbacks, BaseCallbacksDispatcher, BluetoothInterface, BtBondState, BtDiscoveryState,
    BtProperty, BtPropertyType, BtSspVariant, BtState, BtStatus, BtTransport, RawAddress,
    BaseCallbacks, BaseCallbacksDispatcher, BluetoothInterface, BluetoothProperty, BtBondState,
    BtDiscoveryState, BtSspVariant, BtState, BtStatus, BtTransport, RawAddress,
};
use bt_topshim::profiles::hid_host::{HHCallbacksDispatcher, HidHost};
use bt_topshim::topstack;
@@ -62,19 +62,17 @@ pub struct BluetoothDevice {
}

impl BluetoothDevice {
    pub(crate) fn from_properties(properties: &Vec<BtProperty>) -> BluetoothDevice {
    pub(crate) fn from_properties(properties: &Vec<BluetoothProperty>) -> BluetoothDevice {
        let mut address = String::from("");
        let mut name = String::from("");

        for prop in properties {
            match prop.prop_type {
                BtPropertyType::BdAddr => {
                    if let Some(addr) = RawAddress::from_bytes(&prop.val) {
                        address = addr.to_string();
            match prop {
                BluetoothProperty::BdAddr(bdaddr) => {
                    address = bdaddr.to_string();
                }
                }
                BtPropertyType::BdName => {
                    name = String::from_utf8(prop.val.clone()).unwrap();
                BluetoothProperty::BdName(bdname) => {
                    name = bdname.clone();
                }
                _ => {}
            }
@@ -146,8 +144,8 @@ impl Bluetooth {
        });
    }

    fn update_local_address(&mut self, raw: &Vec<u8>) {
        self.local_address = RawAddress::from_bytes(raw);
    fn update_local_address(&mut self, addr: &RawAddress) {
        self.local_address = Some(*addr);

        self.for_all_callbacks(|callback| {
            callback.on_address_changed(self.local_address.unwrap().to_string());
@@ -175,11 +173,11 @@ pub(crate) trait BtifBluetoothCallbacks {
        &mut self,
        status: BtStatus,
        num_properties: i32,
        properties: Vec<BtProperty>,
        properties: Vec<BluetoothProperty>,
    );

    #[btif_callback(DeviceFound)]
    fn device_found(&mut self, n: i32, properties: Vec<BtProperty>);
    fn device_found(&mut self, n: i32, properties: Vec<BluetoothProperty>);

    #[btif_callback(DiscoveryState)]
    fn discovery_state(&mut self, state: BtDiscoveryState);
@@ -229,23 +227,23 @@ impl BtifBluetoothCallbacks for Bluetooth {
        &mut self,
        status: BtStatus,
        num_properties: i32,
        properties: Vec<BtProperty>,
        properties: Vec<BluetoothProperty>,
    ) {
        if status != BtStatus::Success {
            return;
        }

        for prop in properties {
            match prop.prop_type {
                BtPropertyType::BdAddr => {
                    self.update_local_address(&prop.val);
            match prop {
                BluetoothProperty::BdAddr(bdaddr) => {
                    self.update_local_address(&bdaddr);
                }
                _ => {}
            }
        }
    }

    fn device_found(&mut self, _n: i32, properties: Vec<BtProperty>) {
    fn device_found(&mut self, _n: i32, properties: Vec<BluetoothProperty>) {
        self.for_all_callbacks(|callback| {
            callback.on_device_found(BluetoothDevice::from_properties(&properties));
        });
+3 −5
Original line number Diff line number Diff line
@@ -108,11 +108,9 @@ impl AdapterService for AdapterServiceImpl {
        _req: SetDiscoveryModeRequest,
        sink: UnarySink<SetDiscoveryModeResponse>,
    ) {
        self.btif_intf.lock().unwrap().set_adapter_property(btif::BtProperty {
            prop_type: btif::BtPropertyType::AdapterScanMode,
            len: 1,
            val: vec![1_u8],
        });
        self.btif_intf.lock().unwrap().set_adapter_property(
            btif::BluetoothProperty::AdapterScanMode(btif::BtScanMode::Connectable),
        );

        ctx.spawn(async move {
            sink.success(SetDiscoveryModeResponse::default()).await.unwrap();
+4 −33
Original line number Diff line number Diff line
@@ -202,36 +202,6 @@ impl From<bindings::bt_status_t> for BtStatus {
    }
}

#[derive(Debug, Clone)]
pub struct BtProperty {
    pub prop_type: BtPropertyType,
    pub len: i32,
    pub val: Vec<u8>,
}

impl From<bindings::bt_property_t> for BtProperty {
    fn from(item: bindings::bt_property_t) -> Self {
        let slice: &[u8] =
            unsafe { std::slice::from_raw_parts(item.val as *mut u8, item.len as usize) };
        let mut val = Vec::new();
        val.extend_from_slice(slice);

        BtProperty { prop_type: BtPropertyType::from(item.type_), len: item.len, val }
    }
}

impl From<BtProperty> for bindings::bt_property_t {
    fn from(item: BtProperty) -> Self {
        // This is probably very unsafe
        let mut foo = item.clone();
        bindings::bt_property_t {
            type_: item.prop_type.to_u32().unwrap(),
            len: foo.val.len() as i32,
            val: foo.val.as_mut_ptr() as *mut std::os::raw::c_void,
        }
    }
}

impl From<bindings::bt_bdname_t> for String {
    fn from(item: bindings::bt_bdname_t) -> Self {
        ascii_to_string(&item.name, item.name.len())
@@ -296,6 +266,7 @@ pub type BtRemoteVersion = bindings::bt_remote_version_t;
pub type Uuid = bindings::bluetooth::Uuid;

/// All supported Bluetooth properties after conversion.
#[derive(Debug, Clone)]
pub enum BluetoothProperty {
    BdName(String),
    BdAddr(RawAddress),
@@ -677,9 +648,9 @@ macro_rules! cast_to_const_ffi_address {
#[derive(Clone, Debug)]
pub enum BaseCallbacks {
    AdapterState(BtState),
    AdapterProperties(BtStatus, i32, Vec<BtProperty>),
    RemoteDeviceProperties(BtStatus, RawAddress, i32, Vec<BtProperty>),
    DeviceFound(i32, Vec<BtProperty>),
    AdapterProperties(BtStatus, i32, Vec<BluetoothProperty>),
    RemoteDeviceProperties(BtStatus, RawAddress, i32, Vec<BluetoothProperty>),
    DeviceFound(i32, Vec<BluetoothProperty>),
    DiscoveryState(BtDiscoveryState),
    PinRequest(RawAddress, String, u32, bool),
    SspRequest(RawAddress, String, u32, BtSspVariant, u32),