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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
use crate::link::CompanyId;
use crate::uuid::{IsUuid, Uuid128, Uuid16, Uuid32, UuidKind};
use crate::{bytes::*, Error};
use bitflags::bitflags;
#[non_exhaustive]
#[derive(Debug, Copy, Clone)]
pub enum AdStructure<'a> {
Flags(Flags),
ServiceUuids16(ServiceUuids<'a, Uuid16>),
ServiceUuids32(ServiceUuids<'a, Uuid32>),
ServiceUuids128(ServiceUuids<'a, Uuid128>),
ServiceData16 {
uuid: u16,
data: &'a [u8],
},
CompleteLocalName(&'a str),
ShortenedLocalName(&'a str),
ManufacturerSpecificData {
company_identifier: CompanyId,
payload: &'a [u8],
},
Unknown {
ty: u8,
data: &'a [u8],
},
}
impl<'a> ToBytes for AdStructure<'a> {
fn to_bytes(&self, buf: &mut ByteWriter<'_>) -> Result<(), Error> {
let first = match buf.split_next_mut() {
None => return Err(Error::Eof),
Some(s) => s,
};
let left_before = buf.space_left();
match self {
AdStructure::Flags(flags) => {
buf.write_u8(Type::FLAGS)?;
buf.write_u8(flags.to_u8())?;
}
AdStructure::ServiceUuids16(uuids) => uuids.to_bytes(buf)?,
AdStructure::ServiceUuids32(uuids) => uuids.to_bytes(buf)?,
AdStructure::ServiceUuids128(uuids) => uuids.to_bytes(buf)?,
AdStructure::ServiceData16 { uuid, data } => {
buf.write_u8(Type::SERVICE_DATA_16BIT_UUID)?;
buf.write_u8(*uuid as u8)?;
buf.write_u8((*uuid >> 8) as u8)?;
buf.write_slice(data)?;
}
AdStructure::CompleteLocalName(name) => {
buf.write_u8(Type::COMPLETE_LOCAL_NAME)?;
buf.write_slice(name.as_bytes())?;
}
AdStructure::ShortenedLocalName(name) => {
buf.write_u8(Type::SHORTENED_LOCAL_NAME)?;
buf.write_slice(name.as_bytes())?;
}
AdStructure::ManufacturerSpecificData {
company_identifier,
payload,
} => {
buf.write_u8(Type::MANUFACTURER_SPECIFIC_DATA)?;
buf.write_u16_le(company_identifier.as_u16())?;
buf.write_slice(payload)?;
}
AdStructure::Unknown { ty, data } => {
buf.write_u8(*ty)?;
buf.write_slice(data)?;
}
}
let len = left_before - buf.space_left();
assert!(len <= 255);
*first = len as u8;
Ok(())
}
}
impl<'a> FromBytes<'a> for AdStructure<'a> {
fn from_bytes(bytes: &mut ByteReader<'a>) -> Result<Self, Error> {
let len = bytes.read_u8()?;
if len == 0 {
return Err(Error::InvalidLength);
}
let ty_and_data = bytes.read_slice(usize::from(len))?;
let ty = ty_and_data[0];
let data = &ty_and_data[1..];
Ok(match ty {
Type::FLAGS => {
if data.len() != 1 {
return Err(Error::InvalidLength);
}
let bits = data[0];
let flags = Flags::from_bits_truncate(bits);
AdStructure::Flags(flags)
}
Type::COMPLETE_LIST_OF_16BIT_SERVICE_UUIDS
| Type::INCOMPLETE_LIST_OF_16BIT_SERVICE_UUIDS => {
let uuids = ServiceUuids::<Uuid16>::from_bytes(&mut ByteReader::new(ty_and_data))?;
AdStructure::ServiceUuids16(uuids)
}
_ => AdStructure::Unknown { ty, data },
})
}
}
#[derive(Debug, Copy, Clone)]
pub struct ServiceUuids<'a, T: IsUuid> {
complete: bool,
data: BytesOr<'a, [T]>,
}
impl<'a, T: IsUuid> ServiceUuids<'a, T> {
pub fn from_uuids(complete: bool, uuids: &'a [T]) -> Self {
Self {
complete,
data: BytesOr::from_ref(uuids),
}
}
pub fn is_complete(&self) -> bool {
self.complete
}
pub fn iter(&self) -> impl Iterator<Item = T> + 'a {
self.data.iter()
}
fn type_(&self) -> u8 {
match (T::KIND, self.complete) {
(UuidKind::Uuid16, true) => Type::COMPLETE_LIST_OF_16BIT_SERVICE_UUIDS,
(UuidKind::Uuid16, false) => Type::INCOMPLETE_LIST_OF_16BIT_SERVICE_UUIDS,
(UuidKind::Uuid32, true) => Type::COMPLETE_LIST_OF_32BIT_SERVICE_UUIDS,
(UuidKind::Uuid32, false) => Type::INCOMPLETE_LIST_OF_32BIT_SERVICE_UUIDS,
(UuidKind::Uuid128, true) => Type::COMPLETE_LIST_OF_128BIT_SERVICE_UUIDS,
(UuidKind::Uuid128, false) => Type::INCOMPLETE_LIST_OF_128BIT_SERVICE_UUIDS,
}
}
}
impl<'a, T: IsUuid> FromBytes<'a> for ServiceUuids<'a, T> {
fn from_bytes(bytes: &mut ByteReader<'a>) -> Result<Self, Error> {
let (t_complete, t_incomplete) = match T::KIND {
UuidKind::Uuid16 => (
Type::COMPLETE_LIST_OF_16BIT_SERVICE_UUIDS,
Type::INCOMPLETE_LIST_OF_16BIT_SERVICE_UUIDS,
),
UuidKind::Uuid32 => (
Type::COMPLETE_LIST_OF_32BIT_SERVICE_UUIDS,
Type::INCOMPLETE_LIST_OF_32BIT_SERVICE_UUIDS,
),
UuidKind::Uuid128 => (
Type::COMPLETE_LIST_OF_128BIT_SERVICE_UUIDS,
Type::INCOMPLETE_LIST_OF_128BIT_SERVICE_UUIDS,
),
};
let ty = bytes.read_u8()?;
let complete = if ty == t_complete {
true
} else if ty == t_incomplete {
false
} else {
return Err(Error::InvalidValue);
};
Ok(Self {
complete,
data: BytesOr::from_bytes(bytes)?,
})
}
}
impl<'a, T: IsUuid> ToBytes for ServiceUuids<'a, T> {
fn to_bytes(&self, buffer: &mut ByteWriter<'_>) -> Result<(), Error> {
buffer.write_u8(self.type_())?;
self.data.to_bytes(buffer)
}
}
bitflags! {
pub struct Flags: u8 {
const LE_LIMITED_DISCOVERABLE = 0b00000001;
const LE_GENERAL_DISCOVERABLE = 0b00000010;
const BR_EDR_NOT_SUPPORTED = 0b00000100;
const SIMUL_LE_BR_CONTROLLER = 0b00001000;
const SIMUL_LE_BR_HOST = 0b00010000;
}
}
impl Flags {
pub fn discoverable() -> Flags {
Self::BR_EDR_NOT_SUPPORTED | Self::LE_GENERAL_DISCOVERABLE
}
pub fn broadcast() -> Flags {
Self::BR_EDR_NOT_SUPPORTED
}
pub fn to_u8(self) -> u8 {
self.bits()
}
pub fn supports_classic_bluetooth(&self) -> bool {
self.contains(Self::BR_EDR_NOT_SUPPORTED)
}
pub fn le_limited_discoverable(&self) -> bool {
self.contains(Self::LE_LIMITED_DISCOVERABLE)
}
pub fn le_general_discoverable(&self) -> bool {
self.contains(Self::LE_GENERAL_DISCOVERABLE)
}
}
impl<'a> From<Flags> for AdStructure<'a> {
fn from(flags: Flags) -> Self {
AdStructure::Flags(flags)
}
}
enum Type {}
#[allow(unused)]
impl Type {
const FLAGS: u8 = 0x01;
const INCOMPLETE_LIST_OF_16BIT_SERVICE_UUIDS: u8 = 0x02;
const COMPLETE_LIST_OF_16BIT_SERVICE_UUIDS: u8 = 0x03;
const INCOMPLETE_LIST_OF_32BIT_SERVICE_UUIDS: u8 = 0x04;
const COMPLETE_LIST_OF_32BIT_SERVICE_UUIDS: u8 = 0x05;
const INCOMPLETE_LIST_OF_128BIT_SERVICE_UUIDS: u8 = 0x06;
const COMPLETE_LIST_OF_128BIT_SERVICE_UUIDS: u8 = 0x07;
const SHORTENED_LOCAL_NAME: u8 = 0x08;
const COMPLETE_LOCAL_NAME: u8 = 0x09;
const TX_POWER_LEVEL: u8 = 0x0A;
const CLASS_OF_DEVICE: u8 = 0x0D;
const SIMPLE_PAIRING_HASH_C: u8 = 0x0E;
const SIMPLE_PAIRING_HASH_C192: u8 = 0x0E;
const SIMPLE_PAIRING_RANDOMIZER_R: u8 = 0x0F;
const SIMPLE_PAIRING_RANDOMIZER_R192: u8 = 0x0F;
const DEVICE_ID: u8 = 0x10;
const SECURITY_MANAGER_TK_VALUE: u8 = 0x10;
const SECURITY_MANAGER_OUT_OF_BAND_FLAGS: u8 = 0x11;
const SLAVE_CONNECTION_INTERVAL_RANGE: u8 = 0x12;
const LIST_OF_16BIT_SERVICE_SOLICITATION_UUIDS: u8 = 0x14;
const LIST_OF_128BIT_SERVICE_SOLICITATION_UUIDS: u8 = 0x15;
const SERVICE_DATA: u8 = 0x16;
const SERVICE_DATA_16BIT_UUID: u8 = 0x16;
const PUBLIC_TARGET_ADDRESS: u8 = 0x17;
const RANDOM_TARGET_ADDRESS: u8 = 0x18;
const APPEARANCE: u8 = 0x19;
const ADVERTISING_INTERVAL: u8 = 0x1A;
const LE_BLUETOOTH_DEVICE_ADDRESS: u8 = 0x1B;
const LE_ROLE: u8 = 0x1C;
const SIMPLE_PAIRING_HASH_C256: u8 = 0x1D;
const SIMPLE_PAIRING_RANDOMIZER_R256: u8 = 0x1E;
const LIST_OF_32BIT_SERVICE_SOLICITATION_UUIDS: u8 = 0x1F;
const SERVICE_DATA_32BIT_UUID: u8 = 0x20;
const SERVICE_DATA_128BIT_UUID: u8 = 0x21;
const LE_SECURE_CONNECTIONS_CONFIRMATION_VALUE: u8 = 0x22;
const LE_SECURE_CONNECTIONS_RANDOM_VALUE: u8 = 0x23;
const URI: u8 = 0x24;
const INDOOR_POSITIONING: u8 = 0x25;
const TRANSPORT_DISCOVERY_DATA: u8 = 0x26;
const LE_SUPPORTED_FEATURES: u8 = 0x27;
const CHANNEL_MAP_UPDATE_INDICATION: u8 = 0x28;
const PB_ADV: u8 = 0x29;
const MESH_MESSAGE: u8 = 0x2A;
const MESH_BEACON: u8 = 0x2B;
const THREE_D_INFORMATION_DATA: u8 = 0x3D;
const _3D_INFORMATION_DATA: u8 = 0x3D;
const MANUFACTURER_SPECIFIC_DATA: u8 = 0xFF;
}