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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
|
// Copyright 2024, The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use crate::derive::{Read, Write};
use crate::reader::{Read, Reader};
use crate::writer::{pack, Write, Writer};
/// HCI Command, as defined in Part E - 5.4.1
#[derive(Debug)]
pub enum Command {
/// 7.3.2 Reset Command
Reset(Reset),
/// 7.8.97 LE Set CIG Parameters
LeSetCigParameters(LeSetCigParameters),
/// 7.8.99 LE Create CIS
LeCreateCis(LeCreateCis),
/// 7.8.100 LE Remove CIG
LeRemoveCig(LeRemoveCig),
/// 7.8.103 LE Create BIG
LeCreateBig(LeCreateBig),
/// 7.8.109 LE Setup ISO Data Path
LeSetupIsoDataPath(LeSetupIsoDataPath),
/// 7.8.110 LE Remove ISO Data Path
LeRemoveIsoDataPath(LeRemoveIsoDataPath),
/// Unknown command
Unknown(OpCode),
}
/// HCI Command Return Parameters
#[derive(Debug, Read, Write)]
pub enum ReturnParameters {
/// 7.3.2 Reset Command
Reset(ResetComplete),
/// 7.8.2 LE Read Buffer Size [V1]
LeReadBufferSizeV1(LeReadBufferSizeV1Complete),
/// 7.8.2 LE Read Buffer Size [V2]
LeReadBufferSizeV2(LeReadBufferSizeV2Complete),
/// 7.8.97 LE Set CIG Parameters
LeSetCigParameters(LeSetCigParametersComplete),
/// 7.8.100 LE Remove CIG
LeRemoveCig(LeRemoveCigComplete),
/// 7.8.109 LE Setup ISO Data Path
LeSetupIsoDataPath(LeIsoDataPathComplete),
/// 7.8.110 LE Remove ISO Data Path
LeRemoveIsoDataPath(LeIsoDataPathComplete),
/// Unknown command
Unknown(OpCode),
}
impl Command {
/// Read an HCI Command packet
pub fn from_bytes(data: &[u8]) -> Result<Self, Option<OpCode>> {
fn parse_packet(data: &[u8]) -> Option<(OpCode, Reader)> {
let mut r = Reader::new(data);
let opcode = r.read()?;
let len = r.read_u8()? as usize;
Some((opcode, Reader::new(r.get(len)?)))
}
let Some((opcode, mut r)) = parse_packet(data) else {
return Err(None);
};
Self::dispatch_read(opcode, &mut r).ok_or(Some(opcode))
}
fn dispatch_read(opcode: OpCode, r: &mut Reader) -> Option<Command> {
Some(match opcode {
Reset::OPCODE => Self::Reset(r.read()?),
LeSetCigParameters::OPCODE => Self::LeSetCigParameters(r.read()?),
LeCreateCis::OPCODE => Self::LeCreateCis(r.read()?),
LeRemoveCig::OPCODE => Self::LeRemoveCig(r.read()?),
LeCreateBig::OPCODE => Self::LeCreateBig(r.read()?),
LeSetupIsoDataPath::OPCODE => Self::LeSetupIsoDataPath(r.read()?),
LeRemoveIsoDataPath::OPCODE => Self::LeRemoveIsoDataPath(r.read()?),
opcode => Self::Unknown(opcode),
})
}
fn to_bytes<T: CommandOpCode + Write>(command: &T) -> Vec<u8> {
let mut w = Writer::new(Vec::with_capacity(3 + 255));
w.write(&T::OPCODE);
w.write_u8(0);
w.write(command);
let mut vec = w.into_vec();
vec[2] = (vec.len() - 3).try_into().unwrap();
vec
}
}
/// OpCode of HCI Command, as defined in Part E - 5.4.1
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct OpCode(u16);
impl OpCode {
/// OpCode from OpCode Group Field (OGF) and OpCode Command Field (OCF).
pub const fn from(ogf: u16, ocf: u16) -> Self {
Self(pack!((ocf, 10), (ogf, 6)))
}
}
impl From<u16> for OpCode {
fn from(v: u16) -> Self {
OpCode(v)
}
}
impl Read for OpCode {
fn read(r: &mut Reader) -> Option<Self> {
Some(r.read_u16()?.into())
}
}
impl Write for OpCode {
fn write(&self, w: &mut Writer) {
w.write_u16(self.0)
}
}
/// Define command OpCode
pub trait CommandOpCode {
/// OpCode of the command
const OPCODE: OpCode;
}
/// Build command from definition
pub trait CommandToBytes: CommandOpCode + Write {
/// Output the HCI Command packet
fn to_bytes(&self) -> Vec<u8>
where
Self: Sized + CommandOpCode + Write;
}
pub use defs::*;
#[allow(missing_docs)]
#[rustfmt::skip]
mod defs {
use super::*;
use crate::derive::CommandToBytes;
use crate::status::*;
#[cfg(test)]
use crate::{Event, EventToBytes};
// 7.3.2 Reset Command
impl CommandOpCode for Reset {
const OPCODE: OpCode = OpCode::from(0x03, 0x003);
}
#[derive(Debug, Read, Write, CommandToBytes)]
pub struct Reset {}
#[derive(Debug, Read, Write)]
pub struct ResetComplete {
pub status: Status,
}
#[test]
fn test_reset() {
let dump = [0x03, 0x0c, 0x00];
let Ok(Command::Reset(c)) = Command::from_bytes(&dump) else { panic!() };
assert_eq!(c.to_bytes(), &dump[..]);
}
#[test]
fn test_reset_complete() {
let dump = [0x0e, 0x04, 0x01, 0x03, 0x0c, 0x00];
let Ok(Event::CommandComplete(e)) = Event::from_bytes(&dump) else { panic!() };
let ReturnParameters::Reset(ref p) = e.return_parameters else { panic!() };
assert_eq!(p.status, Status::Success);
assert_eq!(e.to_bytes(), &dump[..]);
}
// 7.8.2 LE Read Buffer Size
impl CommandOpCode for LeReadBufferSizeV1 {
const OPCODE: OpCode = OpCode::from(0x08, 0x002);
}
#[derive(Debug)]
pub struct LeReadBufferSizeV1;
#[derive(Debug, Read, Write)]
pub struct LeReadBufferSizeV1Complete {
pub status: Status,
pub le_acl_data_packet_length: u16,
pub total_num_le_acl_data_packets: u8,
}
#[test]
fn test_le_read_buffer_size_v1_complete() {
let dump = [0x0e, 0x07, 0x01, 0x02, 0x20, 0x00, 0xfb, 0x00, 0x0f];
let Ok(Event::CommandComplete(e)) = Event::from_bytes(&dump) else { panic!() };
let ReturnParameters::LeReadBufferSizeV1(ref p) = e.return_parameters else { panic!() };
assert_eq!(p.status, Status::Success);
assert_eq!(p.le_acl_data_packet_length, 251);
assert_eq!(p.total_num_le_acl_data_packets, 15);
assert_eq!(e.to_bytes(), &dump[..]);
}
impl CommandOpCode for LeReadBufferSizeV2 {
const OPCODE: OpCode = OpCode::from(0x08, 0x060);
}
#[derive(Debug)]
pub struct LeReadBufferSizeV2;
#[derive(Debug, Read, Write)]
pub struct LeReadBufferSizeV2Complete {
pub status: Status,
pub le_acl_data_packet_length: u16,
pub total_num_le_acl_data_packets: u8,
pub iso_data_packet_length: u16,
pub total_num_iso_data_packets: u8,
}
#[test]
fn test_le_read_buffer_size_v2_complete() {
let dump = [0x0e, 0x0a, 0x01, 0x60, 0x20, 0x00, 0xfb, 0x00, 0x0f, 0xfd, 0x03, 0x18];
let Ok(Event::CommandComplete(e)) = Event::from_bytes(&dump) else { panic!() };
let ReturnParameters::LeReadBufferSizeV2(ref p) = e.return_parameters else { panic!() };
assert_eq!(p.status, Status::Success);
assert_eq!(p.le_acl_data_packet_length, 251);
assert_eq!(p.total_num_le_acl_data_packets, 15);
assert_eq!(p.iso_data_packet_length, 1021);
assert_eq!(p.total_num_iso_data_packets, 24);
assert_eq!(e.to_bytes(), &dump[..]);
}
// 7.8.97 LE Set CIG Parameters
impl CommandOpCode for LeSetCigParameters {
const OPCODE: OpCode = OpCode::from(0x08, 0x062);
}
#[derive(Debug, Read, Write, CommandToBytes)]
pub struct LeSetCigParameters {
pub cig_id: u8,
#[N(3)] pub sdu_interval_c_to_p: u32,
#[N(3)] pub sdu_interval_p_to_c: u32,
pub worst_case_sca: u8,
pub packing: u8,
pub framing: u8,
pub max_transport_latency_c_to_p: u16,
pub max_transport_latency_p_to_c: u16,
pub cis: Vec<LeCisInCigParameters>,
}
#[derive(Debug, Read, Write)]
pub struct LeCisInCigParameters {
pub cis_id: u8,
pub max_sdu_c_to_p: u16,
pub max_sdu_p_to_c: u16,
pub phy_c_to_p: u8,
pub phy_p_to_c: u8,
pub rtn_c_to_p: u8,
pub rtn_p_to_c: u8,
}
#[test]
fn test_le_set_cig_parameters() {
let dump = [
0x62, 0x20, 0x21, 0x01, 0x10, 0x27, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x64, 0x00, 0x05,
0x00, 0x02, 0x00, 0x78, 0x00, 0x00, 0x00, 0x02, 0x03, 0x0d, 0x00, 0x01, 0x78, 0x00, 0x00, 0x00,
0x02, 0x03, 0x0d, 0x00
];
let Ok(Command::LeSetCigParameters(c)) = Command::from_bytes(&dump) else { panic!() };
assert_eq!(c.cig_id, 0x01);
assert_eq!(c.sdu_interval_c_to_p, 10_000);
assert_eq!(c.sdu_interval_p_to_c, 0);
assert_eq!(c.worst_case_sca, 1);
assert_eq!(c.packing, 0);
assert_eq!(c.framing, 0);
assert_eq!(c.max_transport_latency_c_to_p, 100);
assert_eq!(c.max_transport_latency_p_to_c, 5);
assert_eq!(c.cis.len(), 2);
assert_eq!(c.cis[0].cis_id, 0);
assert_eq!(c.cis[0].max_sdu_c_to_p, 120);
assert_eq!(c.cis[0].max_sdu_p_to_c, 0);
assert_eq!(c.cis[0].phy_c_to_p, 0x02);
assert_eq!(c.cis[0].phy_p_to_c, 0x03);
assert_eq!(c.cis[0].rtn_c_to_p, 13);
assert_eq!(c.cis[0].rtn_p_to_c, 0);
assert_eq!(c.cis[1].cis_id, 1);
assert_eq!(c.cis[1].max_sdu_c_to_p, 120);
assert_eq!(c.cis[1].max_sdu_p_to_c, 0);
assert_eq!(c.cis[1].phy_c_to_p, 0x02);
assert_eq!(c.cis[1].phy_p_to_c, 0x03);
assert_eq!(c.cis[1].rtn_c_to_p, 13);
assert_eq!(c.cis[1].rtn_p_to_c, 0);
assert_eq!(c.to_bytes(), &dump[..]);
}
#[derive(Debug, Read, Write)]
pub struct LeSetCigParametersComplete {
pub status: Status,
pub cig_id: u8,
pub connection_handles: Vec<u16>,
}
#[test]
fn test_le_set_cig_parameters_complete() {
let dump = [0x0e, 0x0a, 0x01, 0x62, 0x20, 0x00, 0x01, 0x02, 0x60, 0x00, 0x61, 0x00];
let Ok(Event::CommandComplete(e)) = Event::from_bytes(&dump) else { panic!() };
let ReturnParameters::LeSetCigParameters(ref p) = e.return_parameters else { panic!() };
assert_eq!(p.status, Status::Success);
assert_eq!(p.cig_id, 1);
assert_eq!(p.connection_handles.len(), 2);
assert_eq!(p.connection_handles[0], 0x60);
assert_eq!(p.connection_handles[1], 0x61);
assert_eq!(e.to_bytes(), &dump[..]);
}
// 7.8.99 LE Create CIS
impl CommandOpCode for LeCreateCis {
const OPCODE: OpCode = OpCode::from(0x08, 0x064);
}
#[derive(Debug, Read, Write, CommandToBytes)]
pub struct LeCreateCis {
pub connection_handles: Vec<CisAclConnectionHandle>,
}
#[derive(Debug, Read, Write)]
pub struct CisAclConnectionHandle {
pub cis: u16,
pub acl: u16,
}
#[test]
fn test_le_create_cis () {
let dump = [0x64, 0x20, 0x09, 0x02, 0x60, 0x00, 0x40, 0x00, 0x61, 0x00, 0x41, 0x00];
let Ok(Command::LeCreateCis(c)) = Command::from_bytes(&dump) else { panic!() };
assert_eq!(c.connection_handles.len(), 2);
assert_eq!(c.connection_handles[0].cis, 0x60);
assert_eq!(c.connection_handles[0].acl, 0x40);
assert_eq!(c.connection_handles[1].cis, 0x61);
assert_eq!(c.connection_handles[1].acl, 0x41);
assert_eq!(c.to_bytes(), &dump[..]);
}
// 7.8.100 LE Remove CIG
impl CommandOpCode for LeRemoveCig {
const OPCODE: OpCode = OpCode::from(0x08, 0x065);
}
#[derive(Debug, Read, Write, CommandToBytes)]
pub struct LeRemoveCig {
pub cig_id: u8,
}
#[derive(Debug, Read, Write)]
pub struct LeRemoveCigComplete {
pub status: Status,
pub cig_id: u8,
}
#[test]
fn test_le_remove_cig() {
let dump = [0x65, 0x20, 0x01, 0x01];
let Ok(Command::LeRemoveCig(c)) = Command::from_bytes(&dump) else { panic!() };
assert_eq!(c.cig_id, 0x01);
assert_eq!(c.to_bytes(), &dump[..]);
}
#[test]
fn test_le_remove_cig_complete() {
let dump = [0x0e, 0x05, 0x01, 0x65, 0x20, 0x00, 0x01];
let Ok(Event::CommandComplete(e)) = Event::from_bytes(&dump) else { panic!() };
let ReturnParameters::LeRemoveCig(ref p) = e.return_parameters else { panic!() };
assert_eq!(p.status, Status::Success);
assert_eq!(p.cig_id, 0x01);
assert_eq!(e.to_bytes(), &dump[..]);
}
// 7.8.103 LE Create BIG
impl CommandOpCode for LeCreateBig {
const OPCODE: OpCode = OpCode::from(0x08, 0x068);
}
#[derive(Debug, Read, Write, CommandToBytes)]
pub struct LeCreateBig {
pub big_handle: u8,
pub advertising_handle: u8,
pub num_bis: u8,
#[N(3)] pub sdu_interval: u32,
pub max_sdu: u16,
pub max_transport_latency: u16,
pub rtn: u8,
pub phy: u8,
pub packing: u8,
pub framing: u8,
pub encryption: u8,
pub broadcast_code: [u8; 16],
}
#[test]
fn test_le_create_big() {
let dump = [
0x68, 0x20, 0x1f, 0x00, 0x00, 0x02, 0x10, 0x27, 0x00, 0x78, 0x00, 0x3c, 0x00, 0x04, 0x02, 0x00,
0x00, 0x01, 0x31, 0x32, 0x33, 0x34, 0x31, 0x32, 0x33, 0x34, 0x31, 0x32, 0x33, 0x34, 0x31, 0x32,
0x33, 0x34
];
let Ok(Command::LeCreateBig(c)) = Command::from_bytes(&dump) else { panic!() };
assert_eq!(c.big_handle, 0x00);
assert_eq!(c.advertising_handle, 0x00);
assert_eq!(c.num_bis, 2);
assert_eq!(c.sdu_interval, 10_000);
assert_eq!(c.max_sdu, 120);
assert_eq!(c.max_transport_latency, 60);
assert_eq!(c.rtn, 4);
assert_eq!(c.phy, 0x02);
assert_eq!(c.packing, 0x00);
assert_eq!(c.framing, 0x00);
assert_eq!(c.encryption, 1);
assert_eq!(c.broadcast_code, [
0x31, 0x32, 0x33, 0x34, 0x31, 0x32, 0x33, 0x34,
0x31, 0x32, 0x33, 0x34, 0x31, 0x32, 0x33, 0x34
]);
assert_eq!(c.to_bytes(), &dump[..]);
}
// 7.8.109 LE Setup ISO Data Path
impl CommandOpCode for LeSetupIsoDataPath {
const OPCODE: OpCode = OpCode::from(0x08, 0x06e);
}
#[derive(Clone, Debug, Read, Write, CommandToBytes)]
pub struct LeSetupIsoDataPath {
pub connection_handle: u16,
pub data_path_direction: LeDataPathDirection,
pub data_path_id: u8,
pub codec_id: LeCodecId,
#[N(3)] pub controller_delay: u32,
pub codec_configuration: Vec<u8>,
}
#[derive(Clone, Debug, PartialEq, Read, Write)]
pub enum LeDataPathDirection {
Input = 0x00,
Output = 0x01,
}
#[derive(Clone, Debug, Read, Write)]
pub struct LeCodecId {
pub coding_format: CodingFormat,
pub company_id: u16,
pub vendor_id: u16,
}
#[derive(Clone, Debug, PartialEq, Read, Write)]
pub enum CodingFormat {
ULawLog = 0x00,
ALawLog = 0x01,
Cvsd = 0x02,
Transparent = 0x03,
LinearPcm = 0x04,
MSbc = 0x05,
Lc3 = 0x06,
G729A = 0x07,
VendorSpecific = 0xff,
}
#[derive(Debug, Read, Write)]
pub struct LeIsoDataPathComplete {
pub status: Status,
pub connection_handle: u16,
}
#[test]
fn test_le_setup_iso_data_path() {
let dump = [
0x6e, 0x20, 0x0d, 0x60, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
];
let Ok(Command::LeSetupIsoDataPath(c)) = Command::from_bytes(&dump) else { panic!() };
assert_eq!(c.connection_handle, 0x60);
assert_eq!(c.data_path_direction, LeDataPathDirection::Input);
assert_eq!(c.data_path_id, 0x00);
assert_eq!(c.codec_id.coding_format, CodingFormat::Transparent);
assert_eq!(c.codec_id.company_id, 0);
assert_eq!(c.codec_id.vendor_id, 0);
assert_eq!(c.controller_delay, 0);
assert_eq!(c.codec_configuration.len(), 0);
assert_eq!(c.to_bytes(), &dump[..]);
}
#[test]
fn test_le_setup_iso_data_path_complete() {
let dump = [0x0e, 0x06, 0x01, 0x6e, 0x20, 0x00, 0x60, 0x00];
let Ok(Event::CommandComplete(e)) = Event::from_bytes(&dump) else { panic!() };
let ReturnParameters::LeSetupIsoDataPath(ref p) = e.return_parameters else { panic!() };
assert_eq!(p.status, Status::Success);
assert_eq!(p.connection_handle, 0x60);
assert_eq!(e.to_bytes(), &dump[..]);
}
// 7.8.110 LE Remove ISO Data Path
impl CommandOpCode for LeRemoveIsoDataPath {
const OPCODE: OpCode = OpCode::from(0x08, 0x06f);
}
#[derive(Debug, Read, Write, CommandToBytes)]
pub struct LeRemoveIsoDataPath {
pub connection_handle: u16,
pub data_path_direction: u8,
}
#[test]
fn test_le_remove_iso_data_path() {
let dump = [0x6f, 0x20, 0x03, 0x60, 0x00, 0x01];
let Ok(Command::LeRemoveIsoDataPath(c)) = Command::from_bytes(&dump) else { panic!() };
assert_eq!(c.connection_handle, 0x60);
assert_eq!(c.data_path_direction, 0x01);
assert_eq!(c.to_bytes(), &dump[..]);
}
}
|