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

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

Floss: Refactor formatting util of Uuid

This adds `Display` implementation for `Uuid` so that `Uuid`s can be
conveniently displayed in logs in human readable format.

This patch also refactors UuidHelper to reuse the same implementation
rather than duplicating it.

Bug: 217273154
Tag: #floss
Test: Manual - Build floss on Linux

Change-Id: Ib99c89721770bc9a6e38c329d21534584089b477
parent e83f46d1
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -989,7 +989,7 @@ impl BtifGattClientCallbacks for BluetoothGatt {

        let client = self.context_map.get_by_uuid(&app_uuid.uu);
        if client.is_none() {
            warn!("Warning: Client not registered for UUID {:?}", app_uuid.uu);
            warn!("Warning: Client not registered for UUID {}", app_uuid);
            return;
        }

+14 −7
Original line number Diff line number Diff line
//! Collection of Profile UUIDs and helpers to use them.

use std::collections::{HashMap, HashSet};
use std::fmt::{Display, Formatter};

use bt_topshim::btif::Uuid128Bit;
use bt_topshim::btif::{Uuid, Uuid128Bit};

// List of profile uuids
pub const A2DP_SINK: &str = "0000110B-0000-1000-8000-00805F9B34FB";
@@ -69,6 +70,17 @@ pub enum Profile {
    CoordinatedSet,
}

/// Wraps a reference of Uuid128Bit, which is the raw array of bytes of UUID.
/// This is useful in implementing standard Rust traits which can't be implemented directly on
/// built-in types (Rust's Orphan Rule).
pub struct UuidWrapper<'a>(pub &'a Uuid128Bit);

impl<'a> Display for UuidWrapper<'a> {
    fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
        Uuid::format(&self.0, f)
    }
}

pub struct UuidHelper {
    /// A list of enabled profiles on the system. These may be modified by policy.
    pub enabled_profiles: HashSet<Profile>,
@@ -150,12 +162,7 @@ impl UuidHelper {

    /// Converts a UUID byte array into a formatted string.
    pub fn to_string(uuid: &Uuid128Bit) -> String {
        return String::from(format!("{:02x}{:02x}{:02x}{:02x}-{:02x}{:02x}-{:02x}{:02x}-{:02x}{:02x}-{:02X}{:02X}{:02X}{:02X}{:02X}{:02X}",
            uuid[0], uuid[1], uuid[2], uuid[3],
            uuid[4], uuid[5],
            uuid[6], uuid[7],
            uuid[8], uuid[9],
            uuid[10], uuid[11], uuid[12], uuid[13], uuid[14], uuid[15]));
        UuidWrapper(&uuid).to_string()
    }

    /// Converts a well-formatted UUID string to a UUID byte array.
+19 −1
Original line number Diff line number Diff line
@@ -7,7 +7,7 @@ use crate::topstack::get_dispatchers;
use num_traits::cast::{FromPrimitive, ToPrimitive};
use std::cmp;
use std::convert::TryFrom;
use std::fmt::{Debug, Formatter, Result};
use std::fmt::{Debug, Display, Formatter, Result};
use std::mem;
use std::os::raw::c_char;
use std::sync::{Arc, Mutex};
@@ -312,6 +312,24 @@ impl TryFrom<Vec<u8>> for Uuid {
    }
}

impl Uuid {
    /// Formats this UUID to a human-readable representation.
    pub fn format(uuid: &Uuid128Bit, f: &mut Formatter) -> Result {
        write!(f, "{:02x}{:02x}{:02x}{:02x}-{:02x}{:02x}-{:02x}{:02x}-{:02x}{:02x}-{:02X}{:02X}{:02X}{:02X}{:02X}{:02X}",
            uuid[0], uuid[1], uuid[2], uuid[3],
            uuid[4], uuid[5],
            uuid[6], uuid[7],
            uuid[8], uuid[9],
            uuid[10], uuid[11], uuid[12], uuid[13], uuid[14], uuid[15])
    }
}

impl Display for Uuid {
    fn fmt(&self, f: &mut Formatter) -> Result {
        Uuid::format(&self.uu, f)
    }
}

/// All supported Bluetooth properties after conversion.
#[derive(Debug, Clone)]
pub enum BluetoothProperty {