-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathuuid.rs
145 lines (125 loc) · 3.38 KB
/
uuid.rs
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
141
142
143
144
145
//! UUID types.
use bt_hci::uuid::BluetoothUuid16;
use crate::codec::{Decode, Encode, Error, Type};
/// A 16-bit or 128-bit UUID.
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[derive(Debug, PartialEq, Clone)]
pub enum Uuid {
/// 16-bit UUID
Uuid16([u8; 2]),
/// 128-bit UUID
Uuid128([u8; 16]),
}
impl From<BluetoothUuid16> for Uuid {
fn from(data: bt_hci::uuid::BluetoothUuid16) -> Self {
Uuid::Uuid16(data.into())
}
}
impl From<u128> for Uuid {
fn from(data: u128) -> Self {
Uuid::Uuid128(data.to_le_bytes())
}
}
impl From<[u8; 16]> for Uuid {
fn from(data: [u8; 16]) -> Self {
Uuid::Uuid128(data)
}
}
impl From<[u8; 2]> for Uuid {
fn from(data: [u8; 2]) -> Self {
Uuid::Uuid16(data)
}
}
impl From<u16> for Uuid {
fn from(data: u16) -> Self {
Uuid::Uuid16(data.to_le_bytes())
}
}
impl Uuid {
/// Create a new 16-bit UUID.
pub const fn new_short(val: u16) -> Self {
Self::Uuid16(val.to_le_bytes())
}
/// Create a new 128-bit UUID.
pub const fn new_long(val: [u8; 16]) -> Self {
Self::Uuid128(val)
}
/// Copy the UUID bytes into a slice.
pub fn bytes(&self, data: &mut [u8]) {
match self {
Uuid::Uuid16(uuid) => data.copy_from_slice(uuid),
Uuid::Uuid128(uuid) => data.copy_from_slice(uuid),
}
}
/// Get the UUID type.
pub fn get_type(&self) -> u8 {
match self {
Uuid::Uuid16(_) => 0x01,
Uuid::Uuid128(_) => 0x02,
}
}
pub(crate) fn size(&self) -> usize {
match self {
Uuid::Uuid16(_) => 6,
Uuid::Uuid128(_) => 20,
}
}
/// Get the 16-bit UUID value.
pub fn as_short(&self) -> u16 {
match self {
Uuid::Uuid16(data) => u16::from_le_bytes([data[0], data[1]]),
_ => panic!("wrong type"),
}
}
/// Get the 128-bit UUID value.
pub fn as_raw(&self) -> &[u8] {
match self {
Uuid::Uuid16(uuid) => uuid,
Uuid::Uuid128(uuid) => uuid,
}
}
}
impl TryFrom<&[u8]> for Uuid {
type Error = crate::Error;
fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
match value.len() {
// Slice length has already been verified, so unwrap can be used
2 => Ok(Uuid::Uuid16(value.try_into().unwrap())),
16 => {
let mut bytes = [0; 16];
bytes.copy_from_slice(value);
Ok(Uuid::Uuid128(bytes))
}
_ => Err(crate::Error::InvalidValue),
}
}
}
impl Type for Uuid {
fn size(&self) -> usize {
self.as_raw().len()
}
}
impl Decode<'_> for Uuid {
fn decode(src: &[u8]) -> Result<Self, Error> {
if src.len() < 2 {
Err(Error::InvalidValue)
} else {
let val: u16 = u16::from_le_bytes([src[0], src[1]]);
// Must be a long id
if val == 0 {
if src.len() < 16 {
return Err(Error::InvalidValue);
}
Ok(Uuid::Uuid128(src[0..16].try_into().map_err(|_| Error::InvalidValue)?))
} else {
Ok(Uuid::new_short(val))
}
}
}
}
impl Encode for Uuid {
fn encode(&self, dest: &mut [u8]) -> Result<(), Error> {
self.bytes(dest);
Ok(())
}
}