1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
use uuid::Uuid;

const BLUETOOTH_BASE_UUID: u128 = 0x00000000_0000_1000_8000_00805f9b34fb;
const BLUETOOTH_BASE_MASK: u128 = 0x00000000_ffff_ffff_ffff_ffffffffffff;
const BLUETOOTH_BASE_MASK_16: u128 = 0xffff0000_ffff_ffff_ffff_ffffffffffff;

/// Convert a 32-bit BLE short UUID to a full 128-bit UUID by filling in the standard Bluetooth Base
/// UUID.
pub const fn uuid_from_u32(short: u32) -> Uuid {
    Uuid::from_u128(BLUETOOTH_BASE_UUID | ((short as u128) << 96))
}

/// Convert a 16-bit BLE short UUID to a full 128-bit UUID by filling in the standard Bluetooth Base
/// UUID.
pub const fn uuid_from_u16(short: u16) -> Uuid {
    uuid_from_u32(short as u32)
}

/// An extension trait for `Uuid` which provides BLE-specific methods.
pub trait BleUuid {
    /// If the UUID is a valid BLE short UUID then return its short form, otherwise return `None`.
    fn to_ble_u32(&self) -> Option<u32>;

    /// If the UUID is a valid 16-bit BLE short UUID then return its short form, otherwise return
    /// `None`.
    fn to_ble_u16(&self) -> Option<u16>;

    /// Convert the UUID to a string, using short format if applicable.
    fn succinctly(&self) -> String;
}

impl BleUuid for Uuid {
    fn to_ble_u32(&self) -> Option<u32> {
        let value = self.as_u128();
        if value & BLUETOOTH_BASE_MASK == BLUETOOTH_BASE_UUID {
            Some((value >> 96) as u32)
        } else {
            None
        }
    }

    fn to_ble_u16(&self) -> Option<u16> {
        let value = self.as_u128();
        if value & BLUETOOTH_BASE_MASK_16 == BLUETOOTH_BASE_UUID {
            Some((value >> 96) as u16)
        } else {
            None
        }
    }

    fn succinctly(&self) -> String {
        if let Some(uuid16) = self.to_ble_u16() {
            format!("{:#04x}", uuid16)
        } else if let Some(uuid32) = self.to_ble_u32() {
            format!("{:#06x}", uuid32)
        } else {
            self.to_string()
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn uuid_from_u32_test() {
        assert_eq!(
            uuid_from_u32(0x11223344),
            Uuid::parse_str("11223344-0000-1000-8000-00805f9b34fb").unwrap()
        );
    }

    #[test]
    fn uuid_from_u16_test() {
        assert_eq!(
            uuid_from_u16(0x1122),
            Uuid::parse_str("00001122-0000-1000-8000-00805f9b34fb").unwrap()
        );
    }

    #[test]
    fn uuid_to_from_u16_success() {
        let uuid = Uuid::parse_str("00001234-0000-1000-8000-00805f9b34fb").unwrap();
        assert_eq!(uuid_from_u16(uuid.to_ble_u16().unwrap()), uuid);
    }

    #[test]
    fn uuid_to_from_u32_success() {
        let uuid = Uuid::parse_str("12345678-0000-1000-8000-00805f9b34fb").unwrap();
        assert_eq!(uuid_from_u32(uuid.to_ble_u32().unwrap()), uuid);
    }

    #[test]
    fn uuid_to_u16_fail() {
        assert_eq!(
            Uuid::parse_str("12345678-0000-1000-8000-00805f9b34fb")
                .unwrap()
                .to_ble_u16(),
            None
        );
        assert_eq!(
            Uuid::parse_str("12340000-0000-1000-8000-00805f9b34fb")
                .unwrap()
                .to_ble_u16(),
            None
        );
        assert_eq!(Uuid::nil().to_ble_u16(), None);
    }

    #[test]
    fn uuid_to_u32_fail() {
        assert_eq!(
            Uuid::parse_str("12345678-9000-1000-8000-00805f9b34fb")
                .unwrap()
                .to_ble_u32(),
            None
        );
        assert_eq!(Uuid::nil().to_ble_u32(), None);
    }

    #[test]
    fn succinctly_u16() {
        let uuid = uuid_from_u16(0x1122);
        assert_eq!(uuid.succinctly(), "0x1122");
    }

    #[test]
    fn succinctly_u32() {
        let uuid = uuid_from_u32(0x11223344);
        assert_eq!(uuid.succinctly(), "0x11223344");
    }

    #[test]
    fn succinctly_long() {
        let uuid_str = "12345678-9000-1000-8000-00805f9b34fb";
        let uuid = Uuid::parse_str(uuid_str).unwrap();
        assert_eq!(uuid.succinctly(), uuid_str);
    }
}