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

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

Floss: Add Uuid::from([u8; 16])

It's natural to build Uuid from an array of 16 bytes. Therefore it's
good to have a standard conversion utility that does not involve vector
overhead and also callers don't need to know the internal field backing
the array.

Also includes a trivial warning fix for unnecessary mut variable.

Bug: 176837458
Tag: #floss
Test: Build Floss on Chrome OS and Linux

Change-Id: Idffeec4f55f69a6bd0769c52a5ea2a5243987417
parent 68391577
Loading
Loading
Loading
Loading
+2 −4
Original line number Diff line number Diff line
@@ -15,7 +15,6 @@ use btstack::bluetooth_gatt::{
use btstack::socket_manager::{IBluetoothSocketManager, SocketResult};
use btstack::uuid::{Profile, UuidHelper, UuidWrapper};
use manager_service::iface_bluetooth_manager::IBluetoothManager;
use std::convert::TryFrom;

const INDENT_CHAR: &str = " ";
const BAR1_CHAR: &str = "=";
@@ -961,11 +960,10 @@ impl CommandHandler {
                };

                let data = AdvertiseData {
                    service_uuids: vec![Uuid::try_from(vec![
                    service_uuids: vec![Uuid::from([
                        0x00, 0x00, 0xfe, 0xf3, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0x80,
                        0x5f, 0x9b, 0x34, 0xfb,
                    ])
                    .unwrap()],
                    ])],
                    solicit_uuids: Vec::new(),
                    transport_discovery_data: Vec::new(),
                    manufacturer_data: HashMap::from([(0, vec![0, 1, 2])]),
+2 −2
Original line number Diff line number Diff line
@@ -1323,7 +1323,7 @@ mod tests {
    const WANT_DEFAULT_ADAPTER: i32 = 0;

    fn make_state_machine(process_manager: MockProcessManager) -> StateMachineInternal {
        let mut state_machine =
        let state_machine =
            StateMachineInternal::new(Box::new(process_manager), true, WANT_DEFAULT_ADAPTER);
        state_machine
    }
@@ -1332,7 +1332,7 @@ mod tests {
    fn initial_state_is_off() {
        tokio::runtime::Runtime::new().unwrap().block_on(async {
            let process_manager = MockProcessManager::new();
            let mut state_machine = make_state_machine(process_manager);
            let state_machine = make_state_machine(process_manager);
            assert_eq!(state_machine.get_process_state(0), ProcessState::Off);
        })
    }
+1 −1
Original line number Diff line number Diff line
@@ -1517,7 +1517,7 @@ impl IBluetooth for Bluetooth {
            return false;
        }

        let uu = Uuid { uu: uuid };
        let uu = Uuid::from(uuid);
        self.sdp.as_ref().unwrap().sdp_search(&mut addr.unwrap(), &uu) == BtStatus::Success
    }

+7 −7
Original line number Diff line number Diff line
@@ -533,7 +533,7 @@ mod tests {
    fn test_append_service_uuids() {
        let mut bytes = Vec::<u8>::new();
        let uuid_16 =
            Uuid { uu: UuidHelper::from_string("0000fef3-0000-1000-8000-00805f9b34fb").unwrap() };
            Uuid::from(UuidHelper::from_string("0000fef3-0000-1000-8000-00805f9b34fb").unwrap());
        let uuids = vec![uuid_16.clone()];
        let exp_16: Vec<u8> = vec![3, 0x3, 0xf3, 0xfe];
        AdvertiseData::append_service_uuids(&mut bytes, &uuids);
@@ -541,7 +541,7 @@ mod tests {

        let mut bytes = Vec::<u8>::new();
        let uuid_32 =
            Uuid { uu: UuidHelper::from_string("00112233-0000-1000-8000-00805f9b34fb").unwrap() };
            Uuid::from(UuidHelper::from_string("00112233-0000-1000-8000-00805f9b34fb").unwrap());
        let uuids = vec![uuid_32.clone()];
        let exp_32: Vec<u8> = vec![5, 0x5, 0x33, 0x22, 0x11, 0x0];
        AdvertiseData::append_service_uuids(&mut bytes, &uuids);
@@ -549,7 +549,7 @@ mod tests {

        let mut bytes = Vec::<u8>::new();
        let uuid_128 =
            Uuid { uu: UuidHelper::from_string("00010203-0405-0607-0809-0a0b0c0d0e0f").unwrap() };
            Uuid::from(UuidHelper::from_string("00010203-0405-0607-0809-0a0b0c0d0e0f").unwrap());
        let uuids = vec![uuid_128.clone()];
        let exp_128: Vec<u8> = vec![
            17, 0x7, 0xf, 0xe, 0xd, 0xc, 0xb, 0xa, 0x9, 0x8, 0x7, 0x6, 0x5, 0x4, 0x3, 0x2, 0x1, 0x0,
@@ -567,7 +567,7 @@ mod tests {
        // Interleaved UUIDs.
        let mut bytes = Vec::<u8>::new();
        let uuid_16_2 =
            Uuid { uu: UuidHelper::from_string("0000aabb-0000-1000-8000-00805f9b34fb").unwrap() };
            Uuid::from(UuidHelper::from_string("0000aabb-0000-1000-8000-00805f9b34fb").unwrap());
        let uuids = vec![uuid_16, uuid_128, uuid_16_2, uuid_32];
        let exp_16: Vec<u8> = vec![5, 0x3, 0xf3, 0xfe, 0xbb, 0xaa];
        let exp_bytes: Vec<u8> =
@@ -580,11 +580,11 @@ mod tests {
    fn test_append_solicit_uuids() {
        let mut bytes = Vec::<u8>::new();
        let uuid_16 =
            Uuid { uu: UuidHelper::from_string("0000fef3-0000-1000-8000-00805f9b34fb").unwrap() };
            Uuid::from(UuidHelper::from_string("0000fef3-0000-1000-8000-00805f9b34fb").unwrap());
        let uuid_32 =
            Uuid { uu: UuidHelper::from_string("00112233-0000-1000-8000-00805f9b34fb").unwrap() };
            Uuid::from(UuidHelper::from_string("00112233-0000-1000-8000-00805f9b34fb").unwrap());
        let uuid_128 =
            Uuid { uu: UuidHelper::from_string("00010203-0405-0607-0809-0a0b0c0d0e0f").unwrap() };
            Uuid::from(UuidHelper::from_string("00010203-0405-0607-0809-0a0b0c0d0e0f").unwrap());
        let uuids = vec![uuid_16, uuid_32, uuid_128];
        let exp_16: Vec<u8> = vec![3, 0x14, 0xf3, 0xfe];
        let exp_32: Vec<u8> = vec![5, 0x1f, 0x33, 0x22, 0x11, 0x0];
+2 −2
Original line number Diff line number Diff line
@@ -956,7 +956,7 @@ impl IBluetoothGatt for BluetoothGatt {
    fn register_scanner(&mut self, callback_id: u32) -> Uuid128Bit {
        let mut bytes: [u8; 16] = [0; 16];
        self.small_rng.fill_bytes(&mut bytes);
        let uuid = Uuid { uu: bytes };
        let uuid = Uuid::from(bytes);

        self.scanners.insert(uuid, ScannerInfo { callback_id, scanner_id: None, is_active: false });

@@ -2674,7 +2674,7 @@ mod tests {
            0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x01, 0x23, 0x45, 0x67, 0x89, 0xab,
            0xcd, 0xef,
        ];
        assert_eq!(Uuid { uu: expected }, uuid.unwrap());
        assert_eq!(Uuid::from(expected), uuid.unwrap());
    }

    #[test]
Loading