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

Commit afc8b647 authored by Jesse Melhuish's avatar Jesse Melhuish
Browse files

floss: Refactor BluetoothDevice checks into helper

Mostly useful for later work fetching properties.

Bug: 196887190
Tag: #floss
Test: Built floss, verified UUIDs can be fetched in btclient.

Change-Id: I30913432e499747ee6c6199416292b3074b2787f
parent e976dc12
Loading
Loading
Loading
Loading
+23 −26
Original line number Diff line number Diff line
@@ -365,6 +365,22 @@ impl Bluetooth {
            }
        };
    }

    fn get_remote_device_if_found(
        &self,
        device: &BluetoothDevice,
    ) -> Option<&BluetoothDeviceContext> {
        self.bonded_devices.get(&device.address).or_else(|| self.found_devices.get(&device.address))
    }

    fn get_remote_device_property(
        &self,
        device: &BluetoothDevice,
        property_type: &BtPropertyType,
    ) -> Option<BluetoothProperty> {
        self.get_remote_device_if_found(&device)
            .and_then(|d| d.properties.get(property_type).and_then(|p| Some(p.clone())))
    }
}

#[btif_callbacks_dispatcher(Bluetooth, dispatch_base_callbacks, BaseCallbacks)]
@@ -1025,35 +1041,16 @@ impl IBluetooth for Bluetooth {
    }

    fn get_remote_uuids(&self, device: BluetoothDevice) -> Vec<Uuid128Bit> {
        // Device must exist in either bonded or found list
        let found = self
            .bonded_devices
            .get(&device.address)
            .or_else(|| self.found_devices.get(&device.address));

        // Extract property from the device
        return found
            .and_then(|d| {
                if let Some(u) = d.properties.get(&BtPropertyType::Uuids) {
                    match u {
                        BluetoothProperty::Uuids(uuids) => {
                            return Some(
                                uuids.iter().map(|&x| x.uu.clone()).collect::<Vec<Uuid128Bit>>(),
                            );
                        }
                        _ => (),
        match self.get_remote_device_property(&device, &BtPropertyType::Uuids) {
            Some(BluetoothProperty::Uuids(uuids)) => {
                return uuids.iter().map(|&x| x.uu.clone()).collect::<Vec<Uuid128Bit>>()
            }
            _ => return vec![],
        }

                None
            })
            .unwrap_or(vec![]);
    }

    fn fetch_remote_uuids(&self, device: BluetoothDevice) -> bool {
        if !self.bonded_devices.contains_key(&device.address)
            && !self.found_devices.contains_key(&device.address)
        {
        if self.get_remote_device_if_found(&device).is_none() {
            warn!("Won't fetch UUIDs on unknown device {}", device.address);
            return false;
        }