-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathusb-bt-dump.c
1505 lines (1429 loc) · 57.7 KB
/
usb-bt-dump.c
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
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright 2010 Michael Poole.
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#define _LARGEFILE64_SOURCE 1
#include <ctype.h> /* isspace() */
#include <errno.h> /* errno, EINPROGRESS */
#include <fcntl.h> /* open(), its flags, etc. */
#include <inttypes.h> /* sized integer types *and formatting* */
#include <stdio.h> /* fprintf(), stdout */
#include <stdlib.h> /* EXIT_SUCCESS, EXIT_FAILURE */
#include <string.h> /* strerror() */
#include <sys/stat.h> /* struct stat */
#include <sys/ioctl.h> /* ioctl(), etc. */
#include <unistd.h> /* getopt(), etc. */
/* Kernel-defined types and constants. */
#define XFER_ISOC 0
#define XFER_INTR 1
#define XFER_CTRL 2
#define XFER_BULK 3
#define PKT_INPUT(PKT) ((PKT)->epnum & 0x80)
/** Descriptor for a single USB transfer. */
struct mon_packet {
/** USB Request Block (URB) identifier. */
uint64_t id;
/** Event type. 'S' for submission, 'C' for callback, 'E' for
* submission error.
*/
unsigned char type;
/** Transfer type. 0 for isochronous, 1 for interrupt, 2 for
* control, 3 for bulk.
*/
unsigned char xfer_type;
/** Endpoint number. Bitwise-or'ed with 0x80 for input,
* otherwise output.
*/
unsigned char epnum;
/** Device number on the bus. */
unsigned char devnum;
/** Bus number. */
uint16_t busnum;
/** Setup packet indicator. '\0' if a setup packet, '-' if
* the setup packet could not be captured, otherwise not a
* setup packet.
*/
char flag_setup;
/** Data validity indicator, if #mon_packet::length > 0. '\0'
* if data has been captured, otherwise a flag like '<' or
* '>'.
*/
char flag_data;
/** Seconds portion of capture timestamp. */
int64_t ts_sec;
/** Microseconds portion of capture timestamp. */
int32_t ts_usec;
/** Transfer status (usually not valid for submission
* events).
*/
int status;
/** Number of data bytes associated with the event. */
unsigned int length;
/** Number of data bytes actually captured. */
unsigned int len_cap;
union {
/** Setup Data field descriptors, including
* bmRequestType (byte, 0x20), bRequest (byte, 0x00 or
* 0xE0), wValue (short, 0x00) and wIndex (short,
* 0x00), wLength (short, 0x00).
*/
unsigned char setup[8];
/** Isochronous request information. */
struct iso_rec {
int error_count;
int numdesc;
} iso;
} s;
/* Note: Only GETX populates the following fields. */
/** Bus interval number? */
int interval;
/** Start frame? */
int start_frame;
/** URB transfer flags (e.g. URB_SHORT_NOT_OK). */
unsigned int xfer_flags;
/** Number of mon_isodesc structures in the capture area
* before the actual data.
*/
unsigned int ndesc;
};
/** Description of an isochronous something or another. */
struct mon_isodesc {
int iso_stat;
unsigned int iso_off;
unsigned int iso_len;
int iso_pad;
};
/** Structure for #MON_IOCG_STATS ioctl. */
struct mon_bin_stats {
uint32_t queued;
uint32_t dropped;
};
/** Descriptor for #MON_IOCX_GET and #MON_IOCX_GETX ioctls. */
struct mon_get_arg {
/** Pointer to USB monitor packet descriptor. */
struct mon_packet *hdr;
/** Buffer to receive data. */
void *data;
/** Number of bytes in receive buffer. */
size_t alloc;
};
/** Descriptor for #MON_IOCX_MFETCH ioctl. */
struct mon_mfetch_arg {
/** Receives offsets inside buffer of fetched descriptors. */
uint32_t *offvec;
/** Maximum number of events to fetch. */
uint32_t nfetch;
/** Number of events to flush before fetch. */
uint32_t nflush;
};
/** Magic number to distinguish usbmon ioctls. */
#define MON_IOC_MAGIC 0x92
/** Return length of data in the next event (possibly zero). */
#define MON_IOCQ_URB_LEN _IO (MON_IOC_MAGIC, 1)
/** Query number of events queued in the buffer and dropped since last query. */
#define MON_IOCG_STATS _IOR (MON_IOC_MAGIC, 3, struct mon_bin_stats)
/** Set buffer size in bytes (may be rounded down). */
#define MON_IOCT_RING_SIZE _IO (MON_IOC_MAGIC, 4)
/** Get buffer size in bytes. */
#define MON_IOCQ_RING_SIZE _IO (MON_IOC_MAGIC, 5)
/** Wait for an event, and return the first one. */
#define MON_IOCX_GET _IOW (MON_IOC_MAGIC, 6, struct mon_get_arg)
#define MON_IOCX_GETX _IOW (MON_IOC_MAGIC, 10, struct mon_get_arg)
/** Used to check where events are in the mmap'ed buffer. */
#define MON_IOCX_MFETCH _IOWR(MON_IOC_MAGIC, 7, struct mon_mfetch_arg)
/** Remove events from the kernel's buffer. */
#define MON_IOCH_MFLUSH _IO (MON_IOC_MAGIC, 8)
/* Bluetooth-defined values. */
const char* lmp_features[] = {
/* Byte 0: */
"3 slot packets",
"5 slot packets",
"Encryption",
"Slot offset",
"Timing accuracy",
"Role switch",
"Hold mode",
"Sniff mode",
/* Byte 1: */
"Park state",
"Power control requests",
"Channel quality driven data rate (CQDDR)",
"SCO link",
"HV2 packets",
"HV3 packets",
"Mu-law log synchronous data",
"A-law log synchronous data",
/* Byte 2: */
"CVSD synchronous data",
"Paging parameter negotiation",
"Power control",
"Transparent synchronous data",
"Flow control lag (LSB)",
"Flow control lag (middle bit)",
"Flow control lag (MSB)",
"Broadcast encryption",
/* Byte 3: */
"Reserved (bit 24)",
"Enhanced Data Rate ACL 2 Mbps mode",
"Enhanced Data Rate ACL 3 Mbps mode",
"Enhanced inquiry scan",
"Interlaced inquiry scan",
"Interlaced page scan",
"RSSI with inquiry results",
"Extended SCO link (EV3 packets)",
/* Byte 4: */
"EV4 packets",
"EV5 packets",
"Reserved (bit 34)",
"AFH capable slave",
"AFH classification slave",
"BR/EDR Not Supported",
"LE Supported (Controller)",
"3-slot Enhanced Data Rate ACL packets",
/* Byte 5: */
"5-slot Enhanced Data Rate ACL packets",
"Sniff subrating",
"Pause encryption",
"AFH capable master",
"AFH classification master",
"Enhanced Dtaa Rate eSCO 2 Mbps mode",
"Enhanced Dtaa Rate eSCO 3 Mbps mode",
"3-slot Enhanced Data Rate eSCO packets",
/* Byte 6: */
"Extended Inquiry Response",
"Simultaneous LE and BR/EDR to Same Device Capable (Controller)",
"Reserved (bit 50)",
"Secure Simple Pairing",
"Encapsulated PDU",
"Erroneous Data Reporting",
"Non-flushable Packet Boundary Flag",
"Reserved (bit 55)",
/* Byte 7: */
"Link SUpervision Timeout Changed Event",
"Inquiry TX Power Level",
"Enhanced Power Control",
"Reserved (bit 59)",
"Reserved (bit 60)",
"Reserved (bit 61)",
"Reserved (bit 62)",
"Extended features",
NULL
};
#define L2CAP_SIGNALING 0x0001
#define L2CAP_DYNAMIC 0x0040
#define L2CAP_CMD_REJECT 0x01
#define L2CAP_CMD_CONN_REQ 0x02
#define L2CAP_CMD_CONN_RESP 0x03
#define L2CAP_CMD_CFG_REQ 0x04
#define L2CAP_CMD_CFG_RESP 0x05
#define L2CAP_CMD_DISCONN_REQ 0x06
#define L2CAP_CMD_DISCONN_RESP 0x07
#define L2CAP_CMD_INFO_REQ 0x0A
#define L2CAP_CMD_INFO_RESP 0x0B
uint16_t l2cap_psm[65536];
/* Utility functions. */
uint16_t get_be16(const unsigned char data[])
{
return (data[0] << 8) | data[1];
}
uint32_t get_be32(const unsigned char data[])
{
return (data[0] << 24) | (data[1] << 16) | (data[2] << 8) | data[3];
}
uint16_t get_le16(const unsigned char data[])
{
return data[0] | (data[1] << 8);
}
char *get_bt_addr(const unsigned char data[], unsigned int n)
{
static char buf[8][18];
if (n > 8) {
n = 8;
}
snprintf(buf[n], sizeof(buf[n]), "%02x:%02x:%02x:%02x:%02x:%02x",
data[0], data[1], data[2], data[3], data[4], data[5]);
return buf[n];
}
uint32_t get_le24(const unsigned char data[])
{
return data[0] | (data[1] << 8) | (data[2] << 16);
}
uint32_t get_le32(const unsigned char data[])
{
return data[0] | (data[1] << 8) | (data[2] << 16) | (data[3] << 24);
}
void put_le16(unsigned char out[], uint16_t data)
{
out[0] = (data >> 0) & 255;
out[1] = (data >> 8) & 255;
}
/* Formatting and parsing functions. */
unsigned char hextab[256];
void init_hex(void)
{
const char hexdigits[] = "0123456789abcdef";
int ii;
for (ii = 0; hexdigits[ii] != '\0'; ii++) {
hextab[tolower(hexdigits[ii])] = ii;
hextab[toupper(hexdigits[ii])] = ii;
}
}
unsigned char fromhex(char ch)
{
return hextab[(unsigned char)ch];
}
unsigned int min(unsigned int a, unsigned int b)
{
return (a < b) ? a : b;
}
void print_usbmon(const struct mon_packet *pkt, const unsigned char data[])
{
const const char hexdigits[] = "0123456789abcdef";
static const char xfer_types[] = "ZICB";
int xfer_type;
char direction;
xfer_type = pkt->xfer_type & 3;
direction = PKT_INPUT(pkt) ? 'i': 'o';
fprintf(stdout, "%016"PRIx64" %"PRId64".%06u %c %c%c:%u:%03u:%u",
pkt->id, pkt->ts_sec, pkt->ts_usec, pkt->type,
xfer_types[xfer_type], direction,
pkt->busnum, pkt->devnum, pkt->epnum & 127);
if (pkt->type == 'E') {
fprintf(stdout, " %d", pkt->status);
} else if (pkt->flag_setup == '\0') {
fprintf(stdout, " s %02x %02x %04x %04x %04x",
pkt->s.setup[0], pkt->s.setup[1],
get_le16(pkt->s.setup + 2),
get_le16(pkt->s.setup + 4),
get_le16(pkt->s.setup + 6));
} else if (pkt->flag_setup == '-') {
/* if (pkt->type == 'S' && pkt->status == -EINPROGRESS)
fprintf(stdout, " -");
else */
fprintf(stdout, " %d", pkt->status);
if (xfer_type == XFER_ISOC || xfer_type == XFER_INTR) {
fprintf(stdout, ":%d", pkt->interval);
}
if (xfer_type == XFER_ISOC) {
fprintf(stdout, ":%d", pkt->start_frame);
if (pkt->type == 'C') {
fprintf(stdout, ":%d", pkt->s.iso.error_count);
}
}
} else {
fprintf(stdout, " %c __ __ ____ ____ ____",
pkt->flag_setup);
}
fprintf(stdout, " %d", pkt->length);
if (pkt->length == 0) {
/* Do nothing. */
} else if (pkt->flag_data != '\0') {
fputc(' ', stdout);
fputc(pkt->flag_data, stdout);
} else {
unsigned int data_len = pkt->len_cap;
unsigned int ii;
fprintf(stdout, " =");
for (ii = 0; ii < data_len; ++ii) {
if (ii % 4 == 0) {
fputc(' ', stdout);
}
fputc(hexdigits[data[ii] >> 4], stdout);
fputc(hexdigits[data[ii] & 15], stdout);
}
if (pkt->len_cap < pkt->length) {
fprintf(stdout, " ...");
}
}
fprintf(stdout, "\n");
}
void print_hci_command(const unsigned char data[], unsigned int len)
{
unsigned int opcode = get_le16(data + 0);
unsigned int param_len = min(data[2], len - 3);
switch (opcode) {
/* No-op commands */
case 0x0000:
fprintf(stdout, " HCI_NoOp\n");
break;
/* Link Control commands (OGF = 0x01) */
case 0x0401:
fprintf(stdout, " HCI_Inquiry(LAP=%06x, Inquiry_Length=%d, Num_Responses=%d)\n",
get_le24(data + 3), data[4], data[5]);
break;
case 0x0402:
fprintf(stdout, " HCI_Inquiry_Cancel()\n");
break;
case 0x0405:
fprintf(stdout, " HCI_Create_Connection(BD_ADDR=%s, Packet_Type=%#02x, Scan=%d, Clock_Offset=%d, Allow_Role_Switch=%d)\n",
get_bt_addr(data + 3, 0), get_le16(data + 9), data[11], get_le16(data + 13), data[15]);
break;
case 0x0406:
fprintf(stdout, " HCI_Disconnect(Connection_Handle=%d, Reason=%d)\n",
get_le16(data + 3), data[5]);
break;
case 0x0409:
fprintf(stdout, " HCI_Accept_Connection_Request(BD_ADDR=%s, Role=%d)\n",
get_bt_addr(data + 3, 0), data[9]);
break;
case 0x040b:
fprintf(stdout, " HCI_Link_Key_Request_Reply(BD_ADDR=%s, Link_Key=%08x_%08x_%08x_%08x)\n",
get_bt_addr(data + 3, 0), get_le32(data + 9), get_le32(data + 13),
get_le32(data + 17), get_le32(data + 21));
break;
case 0x040c:
fprintf(stdout, " HCI_Link_Key_Request_Negative_Reply(BD_ADDR=%s)\n",
get_bt_addr(data + 3, 0));
break;
case 0x040d:
fprintf(stdout, " HCI_PIN_Code_Request_Reply(BD_ADDR=%s, PIN_Code_Length=%d, PIN_Code=%08x_%08x_%08x_%08x\n",
get_bt_addr(data + 3, 0), data[9], get_le32(data + 10), get_le32(data + 14),
get_le32(data + 18), get_le32(data + 22));
break;
case 0x0411:
fprintf(stdout, " HCI_Authentication_Requested(Connection_Handle=%d)\n",
get_le16(data + 3));
break;
case 0x0413:
fprintf(stdout, " HCI_PIN_Code_Request_Negative_Reply(BD_ADDR=%s)\n",
get_bt_addr(data + 3, 0));
break;
case 0x0419:
fprintf(stdout, " HCI_Remote_Name_Request(BD_ADDR=%s, Scan=%d, Clock_Offset=%d)\n",
get_bt_addr(data + 3, 0), data[9], get_le16(data + 11));
break;
case 0x041b:
fprintf(stdout, " HCI_Read_Remote_Supported_Features(Connection_Handle=%d)\n",
get_le16(data + 3));
break;
case 0x041d:
fprintf(stdout, " HCI_Read_Remote_Version_Information(Connection_Handle=%d)\n",
get_le16(data + 3));
break;
case 0x041f:
fprintf(stdout, " HCI_Read_Clock_Offset(Connection_Handle=%d)\n",
get_le16(data + 3));
break;
/* Link Policy commands (OGF = 0x02) */
case 0x0807:
fprintf(stdout, " HCI_QoS_Setup(Connection_Handle=%d, Flags=%#02x, Service_Type=%d, Token_Rate=%d, Peak_Bandwidth=%d, Latency=%d, Delay_Variation=%d)\n",
get_le16(data + 3), data[5], data[6], get_le32(data + 7), get_le32(data + 11),
get_le32(data + 15), get_le32(data + 19));
break;
case 0x0809:
fprintf(stdout, " HCI_Role_Discovery(Connection_Handle=%d)\n",
get_le16(data + 3));
break;
case 0x080d:
fprintf(stdout, " HCI_Write_Link_Policy_Settings(Connection_Handle=%d, Link_Policy_Settings=%#04x)\n",
get_le16(data + 3), get_le16(data + 5));
break;
case 0x080e:
fprintf(stdout, " HCI_Read_Default_Link_Policy_Settings()\n");
break;
case 0x080f:
fprintf(stdout, " HCI_Write_Default_Link_Policy_Settings(Default_Link_Policy_Settings=%#04x)\n",
get_le16(data + 3));
break;
/* Controller & Baseband Commands (OGF = 0x03) */
case 0x0c01:
fprintf(stdout, " HCI_Set_Event_Mask(Event_Mask=%08x_%08x)\n",
get_le32(data + 3), get_le32(data + 7));
break;
case 0x0c03:
fprintf(stdout, " HCI_Reset()\n");
break;
case 0x0c05:
/** \todo Correctly parse and display the condition. */
fprintf(stdout, " HCI_Set_Event_Filter(Filter_Type=%d, Filter_Condition_Type=%d, Condition=...)\n",
data[3], data[4]);
break;
case 0x0c0d:
fprintf(stdout, " HCI_Read_Stored_Link_Key(BD_ADDR=%s, Read_All_Flag=%d)\n",
get_bt_addr(data+3, 0), data[9]);
break;
case 0x0c14:
fprintf(stdout, " HCI_Read_Local_Name()\n");
break;
case 0x0c16:
fprintf(stdout, " HCI_Write_Connection_Accept_Timeout(Conn_Accept_Timeout=%d)\n",
get_le16(data + 3));
break;
case 0x0c18:
fprintf(stdout, " HCI_Write_Page_Timeout(Page_Timeout=%d)\n",
get_le16(data + 3));
break;
case 0x0c19:
fprintf(stdout, " HCI_Read_Scan_Enable()\n");
break;
case 0x0c1a: /* Did somebody say the C1A is here?! */
fprintf(stdout, " HCI_Write_Scan_Enable(Scan_Enable=%d)\n",
data[3]);
break;
case 0x0c23:
fprintf(stdout, " HCI_Read_Class_of_Device()\n");
break;
case 0x0c24:
fprintf(stdout, " HCI_Write_Class_of_Device(Class_of_Device=%#06x)\n",
get_le24(data + 3));
break;
case 0x0c25:
fprintf(stdout, " HCI_Read_Voice_Setting()\n");
break;
case 0x0c28:
fprintf(stdout, " HCI_Write_Automatic_Flush_Timeout(Connection_Handle=%d, Flush_Timeout=%d)\n",
get_le16(data + 3), get_le16(data + 5));
break;
case 0x0c2d:
fprintf(stdout, " HCI_Read_Transmit_Power_Level(Connection_Handle=%d, Type=%d)\n",
get_le16(data + 3), data[5]);
break;
case 0x0c36:
fprintf(stdout, " HCI_Read_Link_Supervision_Timeout(Handle=%d)\n",
get_le16(data + 3));
break;
case 0x0c37:
fprintf(stdout, " HCI_Write_Link_Supervision_Timeout(Handle=%d, Link_Supervision_Timeout=%d)\n",
get_le16(data + 3), get_le16(data + 5));
break;
/* Informational Parameters (OGF = 0x04) */
case 0x1001:
fprintf(stdout, " HCI_Read_Local_Version_Information()\n");
break;
case 0x1003:
fprintf(stdout, " HCI_Read_Local_Supported_Features()\n");
break;
case 0x1005:
fprintf(stdout, " HCI_Read_Buffer_Size()\n");
break;
case 0x1009:
fprintf(stdout, " HCI_Read_BD_ADDR()\n");
break;
/* HCI Status Parameters commands (OGF = 0x05) */
case 0x1403:
fprintf(stdout, " HCI_Read_Link_Quality(Handle=%d)\n",
get_le16(data + 3));
break;
case 0x1405:
fprintf(stdout, " HCI_Read_RSSI(Handle=%d)\n",
get_le16(data + 3));
break;
/* Unknown or unhandled commands */
default:
fprintf(stdout, " Unhandled HCI command with opcode %#04x (OGF %d OCF %d)\n", opcode, opcode >> 10, opcode & 1023);
}
(void)param_len;
}
void print_hci_cmd_complete(unsigned int opcode, const unsigned char data[], unsigned int len)
{
switch (opcode) {
/* Link Control commands (OGF = 0x01) */
case 0x040b:
fprintf(stdout, " HCI_Link_Key_Request_Reply: Status=%d, BD_ADDR=%s\n",
get_le16(data + 0), get_bt_addr(data + 2, 0));
break;
case 0x040c:
fprintf(stdout, " HCI_Link_Key_Request_Negative_Reply: Status=%d, BD_ADDR=%s\n",
get_le16(data + 0), get_bt_addr(data + 2, 0));
break;
case 0x040d:
fprintf(stdout, " HCI_PIN_Code_Request_Reply: Status=%d, BD_ADDR=%s\n",
get_le16(data + 0), get_bt_addr(data + 2, 0));
break;
/* Link Policy commands (OGF = 0x02) */
case 0x0809:
fprintf(stdout, " HCI_Role_Discovery: Status=%d, Connection_Handle=%d, Current_Role=%d\n",
data[0], get_le16(data + 1), data[3]);
break;
case 0x080d:
fprintf(stdout, " HCI_Write_Link_Policy_Settings: Status=%d, Connection_Handle=%d\n",
data[0], get_le16(data + 1));
break;
case 0x080e:
fprintf(stdout, " HCI_Read_Default_Link_Policy_Settings: Status=%d, Default_Link_Policy_Settings=%#04x\n",
data[0], get_le16(data + 1));
break;
case 0x080f:
fprintf(stdout, " HCI_Write_Default_Link_Policy_Settings: Status=%d\n",
data[0]);
break;
/* Controller & Baseband Commands (OGF = 0x03) */
case 0x0c01:
fprintf(stdout, " HCI_Set_Event_Mask: Status=%d\n",
data[0]);
break;
case 0x0c03:
fprintf(stdout, " HCI_Reset: Status=%d\n",
data[0]);
break;
case 0x0c05:
fprintf(stdout, " HCI_Set_Event_Filter: Status=%d\n",
data[0]);
break;
case 0x0c0d:
fprintf(stdout, " HCI_Read_Stored_Link_Key: Status=%d, Max_Num_Keys=%d, Num_Keys_Read=%d\n",
data[0], get_le16(data+1), get_le16(data+3));
break;
case 0x0c14:
fprintf(stdout, " HCI_Read_Local_Name: Status=%d, Local_Name=\"%s\"\n",
data[0], data+1);
break;
case 0x0c16:
fprintf(stdout, " HCI_Write_Connection_Accept_Timeout: Status=%d\n",
data[0]);
break;
case 0x0c18:
fprintf(stdout, " HCI_Write_Page_Timeout: Status=%d\n",
data[0]);
break;
case 0x0c19:
fprintf(stdout, " HCI_Read_Scan_Enable: Status=%d, Scan_Enable=%d\n",
data[0], data[1]);
break;
case 0x0c1a:
fprintf(stdout, " HCI_Write_Scan_Enable: Status=%d\n",
data[0]);
break;
case 0x0c23:
fprintf(stdout, " HCI_Read_Class_of_Device: Status=%d, Class_of_Device=%#06x\n",
data[0], get_le24(data + 1));
break;
case 0x0c24:
fprintf(stdout, " HCI_Write_Class_of_Device: Status=%d\n",
data[0]);
break;
case 0x0c25:
fprintf(stdout, " HCI_Read_Voice_Setting: Status=%d, Voice_Setting=%d\n",
data[0], get_le16(data + 1));
break;
case 0x0c28:
fprintf(stdout, " HCI_Write_Automatic_Flush_Timeout: Status=%d, Connection_Handle=%d\n",
data[0], get_le16(data + 1));
break;
case 0x0c2d:
fprintf(stdout, " HCI_Read_Transmit_Power_Level: Status=%d, Connection_Handle=%d, Transmit_Power_Level=%d\n",
data[0], get_le16(data + 1), (signed char)data[3]);
break;
case 0x0c36:
fprintf(stdout, " HCI_Read_Link_Supervision_Timeout: Status=%d, Connection_Handle=%d, Link_Supervision_Timeout=%d\n",
data[0], get_le16(data + 1), get_le16(data + 3));
break;
case 0x0c37:
fprintf(stdout, " HCI_Write_Link_Supervision_Timeout: Status=%d, Handle=%d\n",
data[0], get_le16(data + 1));
break;
/* Informational Parameters (OGF = 0x04) */
case 0x1001:
fprintf(stdout, " HCI_Read_Local_Version_Information: Status=%d, HCI_Version=%d, HCI_Revision=%#x, LMP/PAL_Version=%d, Manufacturer_Name=%#04x, LMP/PAL_Subversion: %#04x\n",
data[0], data[1], get_le16(data+2), data[4],
get_le16(data+5), get_le16(data+7));
break;
case 0x1003:
fprintf(stdout, " HCI_Read_Local_Supported_Features: Status=%d, LMP_Features=%08x_%08x\n",
data[0], get_le32(data + 1), get_le32(data + 5));
break;
case 0x1005:
fprintf(stdout, " HCI_Read_Buffer_Size: Status=%d, HC_ACL_Data_Packet_Length=%d, HC_Synchronous_Data_Packet_Length=%d, HC_Total_Num_ACL_Data_Packets=%d, HC_Total_Num_Synchronous_Data_Packets=%d\n",
data[0], get_le16(data+1), data[3], get_le16(data+4),
get_le16(data+6));
break;
case 0x1009:
fprintf(stdout, " HCI_Read_BD_ADDR: Status=%d, BD_ADDR=%s\n",
data[0], get_bt_addr(data+1, 0));
break;
/* HCI Status Parameters commands (OGF = 0x05) */
case 0x1403:
fprintf(stdout, " HCI_Read_Link_Quality: Status=%d, Handle=%d, Link_Quality=%d\n",
data[0], get_le16(data + 1), data[3]);
break;
case 0x1405:
fprintf(stdout, " HCI_Read_RSSI: Status=%d, Handle=%d, RSSI=%d\n",
data[0], get_le16(data + 1), (signed char)data[3]);
break;
default:
fprintf(stdout, " HCI unhandled command completion (opcode=%#04x)\n", opcode);
}
(void)len;
}
void print_hci_event(const unsigned char data[], unsigned int len)
{
unsigned int code = data[0];
unsigned int param_len = min(data[1], len - 2);
unsigned int count;
unsigned int ii;
fprintf(stdout, " HCI event: ");
switch (code) {
case 0x00:
fprintf(stdout, "Invalid/empty\n");
break;
case 0x01:
fprintf(stdout, "Inquiry Complete\n");
break;
case 0x02:
count = data[2];
fprintf(stdout, "Inquiry Result: %d responses:\n", count);
for (ii = 0; ii < count; ii++) {
fprintf(stdout, " Addr %s, page scan rep mode %d, class %#x, clock ofs %d\n",
get_bt_addr(data + 6 * ii + 3, 0), *(data + 6 * count + 3),
get_le24(data + 9 * count + 3), get_le16(data + 12 * count + 3));
}
break;
case 0x03:
fprintf(stdout, "Connection Complete: Status=%d, Connection_Handle=%d, BD_ADDR=%s, Link_Type=%d, Encryption_Enabled=%d\n",
data[2], get_le16(data+3), get_bt_addr(data+5, 0), data[11], data[12]);
break;
case 0x04:
fprintf(stdout, "Connection Request: BD_ADDR=%s, Class_of_Device=%#06x, Link_Type=%d\n",
get_bt_addr(data+2, 0), get_le24(data+8), data[10]);
break;
case 0x05:
fprintf(stdout, "Disconnection Complete: Status=%d, Connection_Handle=%d\n",
data[2], get_le16(data+ 3));
break;
case 0x06:
fprintf(stdout, "Authentication Complete: Status=%d, Connection_Handle=%d\n",
data[2], get_le16(data+3));
break;
case 0x07:
fprintf(stdout, "Remote Name Request Complete: Status=%d, BD_ADDR=%s, Remote_Name=\"%s\"\n",
data[2], get_bt_addr(data+3, 0), data+9);
break;
case 0x08:
fprintf(stdout, "Encryption Change Event: Status=%d, Connection_Handle=%d, Encryption_Enabled=%d\n",
data[2], get_le16(data+3), data[5]);
break;
case 0x0b:
fprintf(stdout, "Read Remote Supported Features Complete: Status=%d, Connection_Handle=%d, LMP_Features=%#08x_%#08x\n",
data[2], get_le16(data+3), get_le32(data+4), get_le32(data+8));
for (ii = 0; lmp_features[ii] != NULL; ii++) {
if ((data[ii/8 + 4] >> (ii%8)) & 1) {
fprintf(stdout, " %s\n", lmp_features[ii]);
}
}
if (ii != 64) {
fprintf(stdout, " .. why were there %d lmp_features[]?\n", ii);
}
break;
case 0x0c:
fprintf(stdout, "Read Remote Version Information Complete: Status=%d, Connection_Handle=%d, Version=%d, Manufacturer_Name=%#04x, Subversion=%#04x\n",
data[2], get_le16(data+3), data[5], get_le16(data+6), get_le16(data+8));
break;
case 0x0d:
fprintf(stdout, "QoS Setup Complete: Status=%d, Connection_Handle=%d, Flags=%#02x, Service_Type=%d, Token_Rate=%d, Peak_Bandwidth=%d, Latency=%d, Delay_Variation=%d\n",
data[2], get_le16(data+3), data[5], data[6], get_le32(data+7), get_le32(data+11),
get_le32(data+15), get_le32(data+19));
break;
case 0x0e:
fprintf(stdout, "Command Complete Event: Num_HCI_Command_Packets=%d, Command_Opcode=%#04x, Return_Parameters=%d bytes\n",
data[2], get_le16(data+3), data[1]-3);
print_hci_cmd_complete(get_le16(data+3), data+5, min(len-5,data[1]-3));
break;
case 0x0f:
fprintf(stdout, "Command Status: Status=%d, Num_HCI_Command_Packets=%d, Command_Opcode=%#04x\n",
data[2], data[3], get_le16(data+4));
break;
case 0x12:
fprintf(stdout, "Role Change: Status=%d, BD_ADDR=%s, New_Role=%d\n",
data[2], get_bt_addr(data+3, 0), data[9]);
break;
case 0x13:
count = data[2];
fprintf(stdout, "Number of Completed Packets %d:\n", count);
for (ii = 0; ii < count; ii++) {
fprintf(stdout, " Connection_Handle=%d, HC_Num_Of_Completed_Packets=%d\n",
get_le16(data + 2 * ii + 3), get_le16(data + 2 * count + 2 * ii + 3));
}
break;
case 0x14:
fprintf(stdout, "Mode Change: Status=%d, Connection_Handle=%d, Current_Mode=%d, Interval=%d\n",
data[2], get_le16(data+3), data[5], get_le16(data+6));
break;
case 0x16:
fprintf(stdout, "PIN Code Request: BD_ADDR=%s\n",
get_bt_addr(data+2, 0));
break;
case 0x17:
fprintf(stdout, "Link Key Request: BD_ADDR=%s\n",
get_bt_addr(data+2, 0));
break;
case 0x18:
fprintf(stdout, "Link Key Notification: BD_ADDR=%s, Link_Key=%08x_%08x_%08x_%08x, Key_Type=%d\n",
get_bt_addr(data+2, 0), get_le32(data+8), get_le32(data+12), get_le32(data+16),
get_le32(data+20), data[24]);
break;
case 0x1b:
fprintf(stdout, "Max Slots Change: Connection_Handle=%d, LMP_Max_Slots=%d\n",
get_le16(data+2), data[4]);
break;
case 0x1c:
fprintf(stdout, "Read Clock Offset Complete: Status=%d, Connection_Handle=%d, Clock_Offset=%d\n",
data[2], get_le16(data+3), get_le16(data+5));
break;
default:
fprintf(stdout, "Unhandled event %#x (%d parameter bytes)\n", code, data[1]);
}
(void)param_len;
}
void print_l2cap_config_options(const unsigned char data[])
{
fprintf(stdout, " %s ", (data[-2] & 0x80) ? "Hint" : "Reqd");
switch (data[-2] & 127) {
case 0x01:
fprintf(stdout, "MTU = %d\n", get_le16(data + 0));
break;
case 0x02:
fprintf(stdout, "Flush_Timeout = %d\n", get_le16(data + 0));
break;
case 0x03:
fprintf(stdout, "QoS: Flags=%d, Service_Type=%d, Token_Rate=%d, Token_Bucket_Size=%d, Peak_Bandwidth=%d, Latency=%d, Delay_Variation=%d\n",
data[0], data[1], get_le32(data + 2), get_le32(data + 6),
get_le32(data + 10), get_le32(data + 14), get_le32(data + 18));
break;
case 0x04:
fprintf(stdout, "Rexmit: Mode=%d, TxWindowSize=%d, MaxTx=%d, RexmitTimeout=%d, MonitorTimeout=%d, Max_PDU=%d\n",
data[0], data[1], data[2], get_le16(data + 3), get_le16(data + 5), get_le16(data + 7));
break;
default:
fprintf(stdout, "unknown option %d (%d bytes)\n", data[-2], data[-1]);
}
}
int print_sdp_data(const unsigned char data[], unsigned int *ppos, unsigned int len)
{
unsigned int pos = *ppos;
unsigned int size;
unsigned int ii;
int more;
int sub_more = 0;
if (pos < len) {
unsigned char tag = data[pos++];
/* Parse the size portion of the field. */
switch (tag & 7) {
case 0: size = 1; break;
case 1: size = 2; break;
case 2: size = 4; break;
case 3: size = 8; break;
case 4: size = 16; break;
case 5:
size = data[pos];
pos += 1;
break;
case 6:
size = get_be16(data);
pos += 2;
break;
case 7:
size = get_be32(data);
pos += 4;
break;
}
if (size > len - pos - 1) {
more = size - (len - pos - 1);
size = len - pos - 1;
} else {
more = 0;
}
/* Display according to the type portion of the field. */
switch (tag >> 3) {
case 0:
if (size == 1) size = 0;
fprintf(stdout, "nil");
break;
case 1:
fprintf(stdout, "uint%d(", size);
switch (size) {
case 1:
fprintf(stdout, "%u", data[pos]);
break;
case 2:
fprintf(stdout, "%u", get_be16(data+pos));
break;
case 4:
fprintf(stdout, "%#x", get_be32(data+pos));
break;
case 8:
fprintf(stdout, "%#x_%08x", get_be32(data+pos), get_be32(data+pos+4));
break;
case 16:
fprintf(stdout, "%#x_%08x_%08x_%08x", get_be32(data+pos), get_be32(data+pos+4), get_be32(data+pos+8), get_be32(data+pos+12));
break;
}
fputc(')', stdout);
break;
case 2:
fprintf(stdout, "int%d(", size);
switch (size) {
case 1:
fprintf(stdout, "%d", data[pos]);
break;
case 2:
fprintf(stdout, "%d", get_be16(data+pos));
break;
case 4:
fprintf(stdout, "%#x", get_be32(data+pos));
break;
case 8:
fprintf(stdout, "%#x_%08x", get_be32(data+pos), get_be32(data+pos+4));
break;
case 16:
fprintf(stdout, "%#x_%08x_%08x_%08x", get_be32(data+pos), get_be32(data+pos+4), get_be32(data+pos+8), get_be32(data+pos+12));
break;
}
fputc(')', stdout);
break;
case 3:
fprintf(stdout, "uuid%d(", size);
switch (size) {
case 2:
fprintf(stdout, "%#06x", get_be16(data+pos));
break;
case 4:
fprintf(stdout, "%#10x", get_be16(data+pos));
break;
case 16:
fprintf(stdout, "%08x-%04x-%04x-%04x-%04x%08x",
get_be32(data+pos+0),
get_be16(data+pos+4),
get_be16(data+pos+6),
get_be16(data+pos+8),
get_be16(data+pos+10),
get_be32(data+pos+12));
break;
}
fputc(')', stdout);
break;
case 8:
fputs("URL:", stdout);
/* Fall through to the string case. */
case 4:
fputc('"', stdout);
for (ii = 0; ii < size; ii++) {
if (isprint(data[pos+ii])) {
fputc(data[pos+ii], stdout);
} else {
fprintf(stdout, "\\x%02x", data[pos+ii]);
}
}
fputc('"', stdout);
break;
case 5:
fputs("bool(", stdout);
if (pos < size) {
fputs(data[pos++] ? "true" : "false", stdout);
}
fputs(")", stdout);
break;
case 6:
case 7:
fputs(((tag >> 3) == 6) ? "seq { " : "alt { ", stdout);
*ppos = pos;
do {
if (*ppos > pos) fputs(", ", stdout);
ii = print_sdp_data(data, ppos, len);
} while (ii == 0 && *ppos < pos + size);
sub_more += ii;
fputs(" }", stdout);
break;
default:
fprintf(stdout, "reserved (Type=%d, Size=%d)\n", data[pos-1] >> 3, size + more);
}
pos += size;
} else {
more = 1;
}
if (more && !sub_more) {
fprintf(stdout, " ...");
}