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

Commit e7808609 authored by Sonny Sasaka's avatar Sonny Sasaka
Browse files

Floss: Move parse_uuid_string to UuidHelper

This consolidates a related utility function into an umbrella struct
UuidHelper.

Bug: 176837458
Tag: #floss
Test: Build Floss on Linux

Change-Id: I1be91d1e71980ea2f5942eb0b7f5e333a9813138
parent db5053d2
Loading
Loading
Loading
Loading
+3 −3
Original line number Diff line number Diff line
@@ -3,7 +3,7 @@ use crate::bluetooth_gatt::{
};
use crate::callbacks::Callbacks;
use crate::uuid;
use crate::uuid::parse_uuid_string;
use crate::uuid::UuidHelper;
use crate::Message;
use crate::RPCProxy;
use bt_topshim::btif::BtTransport;
@@ -144,8 +144,8 @@ impl BatteryService {
                    return;
                }
                let (bas_uuid, battery_level_uuid) = match (
                    parse_uuid_string(uuid::BAS),
                    parse_uuid_string(CHARACTERISTIC_BATTERY_LEVEL),
                    UuidHelper::parse_string(uuid::BAS),
                    UuidHelper::parse_string(CHARACTERISTIC_BATTERY_LEVEL),
                ) {
                    (Some(bas_uuid), Some(battery_level_uuid)) => (bas_uuid, battery_level_uuid),
                    _ => return,
+2 −2
Original line number Diff line number Diff line
@@ -11,7 +11,7 @@ use std::sync::atomic::{AtomicIsize, Ordering};
use tokio::sync::mpsc::Sender;

use crate::callbacks::Callbacks;
use crate::uuid::{parse_uuid_string, UuidHelper};
use crate::uuid::UuidHelper;
use crate::{Message, RPCProxy};

pub type AdvertiserId = i32;
@@ -262,7 +262,7 @@ impl AdvertiseData {

    fn append_service_data(dest: &mut Vec<u8>, service_data: &HashMap<String, Vec<u8>>) {
        for (uuid, data) in
            service_data.iter().filter_map(|(s, d)| parse_uuid_string(s).map(|s| (s, d)))
            service_data.iter().filter_map(|(s, d)| UuidHelper::parse_string(s).map(|s| (s, d)))
        {
            let uuid_slice = UuidHelper::get_shortest_slice(&uuid.uu);
            let concated: Vec<u8> = uuid_slice.iter().rev().chain(data).cloned().collect();
+8 −8
Original line number Diff line number Diff line
@@ -20,7 +20,7 @@ use crate::bluetooth_adv::{
    IAdvertisingSetCallback, PeriodicAdvertisingParameters,
};
use crate::callbacks::Callbacks;
use crate::uuid::parse_uuid_string;
use crate::uuid::UuidHelper;
use crate::{Message, RPCProxy, SuspendMode};
use log::{debug, warn};
use num_traits::cast::{FromPrimitive, ToPrimitive};
@@ -1186,7 +1186,7 @@ impl IBluetoothGatt for BluetoothGatt {
        callback: Box<dyn IBluetoothGattCallback + Send>,
        eatt_support: bool,
    ) {
        let uuid = match parse_uuid_string(&app_uuid) {
        let uuid = match UuidHelper::parse_string(&app_uuid) {
            Some(id) => id,
            None => {
                log::info!("Uuid is malformed: {}", app_uuid);
@@ -1266,7 +1266,7 @@ impl IBluetoothGatt for BluetoothGatt {
            return;
        }

        let uuid = parse_uuid_string(uuid);
        let uuid = UuidHelper::parse_string(uuid);
        if uuid.is_none() {
            return;
        }
@@ -1303,7 +1303,7 @@ impl IBluetoothGatt for BluetoothGatt {
            return;
        }

        let uuid = parse_uuid_string(uuid);
        let uuid = UuidHelper::parse_string(uuid);
        if uuid.is_none() {
            return;
        }
@@ -2665,10 +2665,10 @@ mod tests {

    #[test]
    fn test_uuid_from_string() {
        let uuid = parse_uuid_string("abcdef");
        let uuid = UuidHelper::parse_string("abcdef");
        assert!(uuid.is_none());

        let uuid = parse_uuid_string("0123456789abcdef0123456789abcdef");
        let uuid = UuidHelper::parse_string("0123456789abcdef0123456789abcdef");
        assert!(uuid.is_some());
        let expected: [u8; 16] = [
            0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x01, 0x23, 0x45, 0x67, 0x89, 0xab,
@@ -2684,7 +2684,7 @@ mod tests {

        // Add client 1.
        let callback1 = Box::new(TestBluetoothGattCallback::new(String::from("Callback 1")));
        let uuid1 = parse_uuid_string("00000000000000000000000000000001").unwrap().uu;
        let uuid1 = UuidHelper::parse_string("00000000000000000000000000000001").unwrap().uu;
        map.add(&uuid1, callback1);
        let found = map.get_by_uuid(&uuid1);
        assert!(found.is_some());
@@ -2704,7 +2704,7 @@ mod tests {

        // Add client 2.
        let callback2 = Box::new(TestBluetoothGattCallback::new(String::from("Callback 2")));
        let uuid2 = parse_uuid_string("00000000000000000000000000000002").unwrap().uu;
        let uuid2 = UuidHelper::parse_string("00000000000000000000000000000002").unwrap().uu;
        map.add(&uuid2, callback2);
        let found = map.get_by_uuid(&uuid2);
        assert!(found.is_some());
+19 −19
Original line number Diff line number Diff line
@@ -237,11 +237,10 @@ impl UuidHelper {
        let num = u128::from_be_bytes(*uuid);
        (num & BASE_UUID_MASK) == BASE_UUID_NUM
    }
}

    // Temporary util that covers only basic string conversion.
    // TODO(b/193685325): Implement more UUID utils by using Uuid from gd/hci/uuid.h with cxx.
pub fn parse_uuid_string<T: Into<String>>(uuid: T) -> Option<Uuid> {
    pub fn parse_string<T: Into<String>>(uuid: T) -> Option<Uuid> {
        let uuid = uuid.into();

        // Strip un-needed characters before parsing to handle the common
@@ -261,6 +260,7 @@ pub fn parse_uuid_string<T: Into<String>>(uuid: T) -> Option<Uuid> {

        Some(Uuid::from(raw))
    }
}

#[cfg(test)]
mod tests {