-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhci.c
4012 lines (3427 loc) · 75.7 KB
/
hci.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
/**
* @file hci.c
* @brief hci functions
* @author Gilbert Brault
* @copyright Gilbert Brault 2015
* the original work comes from bluez v5.39
* value add: documenting main features
*
*/
/*
*
* BlueZ - Bluetooth protocol stack for Linux
*
* Copyright (C) 2000-2001 Qualcomm Incorporated
* Copyright (C) 2002-2003 Maxim Krasnyansky <[email protected]>
* Copyright (C) 2002-2010 Marcel Holtmann <[email protected]>
*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <poll.h>
#include <sys/param.h>
#include <sys/uio.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include "bluetooth.h"
#include "hci.h"
#include "hci_lib.h"
#ifndef MIN
#define MIN(x, y) ((x) < (y) ? (x) : (y))
#endif
typedef struct {
char *str;
unsigned int val;
} hci_map;
/**
* lookup the numeric code val into the hci_map m and return the matching string
*
* @param m pointer to the hci_map
* @param val code to match
* @return string matching the code (val) from the hci_map
*/
static char *hci_bit2str(hci_map *m, unsigned int val)
{
char *str = malloc(120);
char *ptr = str;
if (!str)
return NULL;
*ptr = 0;
while (m->str) {
if ((unsigned int) m->val & val)
ptr += sprintf(ptr, "%s ", m->str);
m++;
}
return str;
}
/**
* ored bits corresponding to comma separated commands matching the hci_map array
*
* @param map pointer to the hci_map array
* @param str commands, separated by commas for match in the hci_map array
* @param val ored bits (from the hci_map) of maching commands
*
* @return 1 val is set, 0
*/static int hci_str2bit(hci_map *map, char *str, unsigned int *val)
{
char *t, *ptr;
hci_map *m;
int set;
if (!str || !(str = ptr = strdup(str)))
return 0;
*val = set = 0;
while ((t = strsep(&ptr, ","))) {
for (m = map; m->str; m++) {
if (!strcasecmp(m->str, t)) {
*val |= (unsigned int) m->val;
set = 1;
}
}
}
free(str);
return set;
}
/**
* lookup the hci_map and find entry corresponding to val and return the corresponding string
*
* @param m hci_map pointer
* @param val val to match in the hci_map
* @return string mapped to the matching value or NULL if no matching
*/
static char *hci_uint2str(hci_map *m, unsigned int val)
{
char *str = malloc(50);
char *ptr = str;
if (!str)
return NULL;
*ptr = 0;
while (m->str) {
if ((unsigned int) m->val == val) {
ptr += sprintf(ptr, "%s", m->str);
break;
}
m++;
}
return str;
}
/**
* lookup a command string in the hci_map array and return the corresponding code
*
* @param map pointer to the hci_map array
* @param str command to match
* @param val returned corresponding code
*
* @return 1 val is set 0 if no matching string
*/
static int hci_str2uint(hci_map *map, char *str, unsigned int *val)
{
char *t, *ptr;
hci_map *m;
int set = 0;
if (!str)
return 0;
str = ptr = strdup(str);
while ((t = strsep(&ptr, ","))) {
for (m = map; m->str; m++) {
if (!strcasecmp(m->str,t)) {
*val = (unsigned int) m->val;
set = 1;
break;
}
}
}
free(str);
return set;
}
/**
* return a string corresponding to the bus code (see HCI bus types in hci.h)
*
* @param bus bus code
* @return string corresponding to the bus code (NULL if error)
*/
char *hci_bustostr(int bus)
{
switch (bus) {
case HCI_VIRTUAL:
return "VIRTUAL";
case HCI_USB:
return "USB";
case HCI_PCCARD:
return "PCCARD";
case HCI_UART:
return "UART";
case HCI_RS232:
return "RS232";
case HCI_PCI:
return "PCI";
case HCI_SDIO:
return "SDIO";
default:
return "UNKNOWN";
}
}
/**
* return the bus string according to the bus code (type)
*
* @param type bus code
* @return bus string (NULL if error)
*/
char *hci_dtypetostr(int type)
{
return hci_bustostr(type & 0x0f);
}
/**
* return the bluetooth type string give the code (see HCI controller types in hci.h)
*
* @param type HCI controller types
* @return corresponding string (NULL if error)
*/
char *hci_typetostr(int type)
{
switch (type) {
case HCI_BREDR:
return "BR/EDR";
case HCI_AMP:
return "AMP";
default:
return "UNKNOWN";
}
}
/* HCI dev flags mapping */
static hci_map dev_flags_map[] = {
{ "UP", HCI_UP },
{ "INIT", HCI_INIT },
{ "RUNNING", HCI_RUNNING },
{ "RAW", HCI_RAW },
{ "PSCAN", HCI_PSCAN },
{ "ISCAN", HCI_ISCAN },
{ "INQUIRY", HCI_INQUIRY },
{ "AUTH", HCI_AUTH },
{ "ENCRYPT", HCI_ENCRYPT },
{ NULL }
};
/**
* return the HCI device flags string given its code
*
* @param flags HCI device flags code
* @return HCI device flags string (NULL if error)
*/
char *hci_dflagstostr(uint32_t flags)
{
char *str = bt_malloc(50);
char *ptr = str;
hci_map *m = dev_flags_map;
if (!str)
return NULL;
*ptr = 0;
if (!hci_test_bit(HCI_UP, &flags))
ptr += sprintf(ptr, "DOWN ");
while (m->str) {
if (hci_test_bit(m->val, &flags))
ptr += sprintf(ptr, "%s ", m->str);
m++;
}
return str;
}
/* HCI packet type mapping */
static hci_map pkt_type_map[] = {
{ "DM1", HCI_DM1 },
{ "DM3", HCI_DM3 },
{ "DM5", HCI_DM5 },
{ "DH1", HCI_DH1 },
{ "DH3", HCI_DH3 },
{ "DH5", HCI_DH5 },
{ "HV1", HCI_HV1 },
{ "HV2", HCI_HV2 },
{ "HV3", HCI_HV3 },
{ "2-DH1", HCI_2DH1 },
{ "2-DH3", HCI_2DH3 },
{ "2-DH5", HCI_2DH5 },
{ "3-DH1", HCI_3DH1 },
{ "3-DH3", HCI_3DH3 },
{ "3-DH5", HCI_3DH5 },
{ NULL }
};
/* SCO (synchronous connection orientated) links */
static hci_map sco_ptype_map[] = {
{ "HV1", 0x0001 },
{ "HV2", 0x0002 },
{ "HV3", 0x0004 },
{ "EV3", HCI_EV3 },
{ "EV4", HCI_EV4 },
{ "EV5", HCI_EV5 },
{ "2-EV3", HCI_2EV3 },
{ "2-EV5", HCI_2EV5 },
{ "3-EV3", HCI_3EV3 },
{ "3-EV5", HCI_3EV5 },
{ NULL }
};
/**
* return the HCI packet type string given the code
*
* @param ptype HCI packet type
* @return HCI packet type string (NULL if error)
*/
char *hci_ptypetostr(unsigned int ptype)
{
return hci_bit2str(pkt_type_map, ptype);
}
/**
* return the HCI packet type code given the string
*
* @param str HCI packet type string
* @param val HCI packet type code
* @return 1 if found 0 error
*/
int hci_strtoptype(char *str, unsigned int *val)
{
return hci_str2bit(pkt_type_map, str, val);
}
/**
* return the SCO (synchronous connection orientated) links string given the code
*
* @param ptype SCO type code
* @return SCO string (NULL if error)
*/
char *hci_scoptypetostr(unsigned int ptype)
{
return hci_bit2str(sco_ptype_map, ptype);
}
/**
* return the SCO (synchronous connection orientated) links code given the string
*
* @param str SCO (synchronous connection orientated) links string
* @param val SCO (synchronous connection orientated) links code
* @return 1 match found 0 error
*/
int hci_strtoscoptype(char *str, unsigned int *val)
{
return hci_str2bit(sco_ptype_map, str, val);
}
/* Link policy mapping */
static hci_map link_policy_map[] = {
{ "NONE", 0 },
{ "RSWITCH", HCI_LP_RSWITCH },
{ "HOLD", HCI_LP_HOLD },
{ "SNIFF", HCI_LP_SNIFF },
{ "PARK", HCI_LP_PARK },
{ NULL }
};
/**
* return the Link policy mapping string given the code
*
* @param lp Link policy mapping code
* @return Link policy mapping string (NULL if error)
*/
char *hci_lptostr(unsigned int lp)
{
return hci_bit2str(link_policy_map, lp);
}
/**
* return Link policy mapping code given the string
*
* @param str Link policy mapping string
* @param val Link policy mapping code
* @return 1 match found 0 error
*/
int hci_strtolp(char *str, unsigned int *val)
{
return hci_str2bit(link_policy_map, str, val);
}
/* Link mode mapping */
static hci_map link_mode_map[] = {
{ "NONE", 0 },
{ "ACCEPT", HCI_LM_ACCEPT },
{ "MASTER", HCI_LM_MASTER },
{ "AUTH", HCI_LM_AUTH },
{ "ENCRYPT", HCI_LM_ENCRYPT },
{ "TRUSTED", HCI_LM_TRUSTED },
{ "RELIABLE", HCI_LM_RELIABLE },
{ "SECURE", HCI_LM_SECURE },
{ NULL }
};
/**
* return the Link mode mapping string given the code
*
* @param lm Link mode mapping code
* @return Link mode mapping string (NULL if error)
*/
char *hci_lmtostr(unsigned int lm)
{
char *s, *str = bt_malloc(50);
if (!str)
return NULL;
*str = 0;
if (!(lm & HCI_LM_MASTER))
strcpy(str, "SLAVE ");
s = hci_bit2str(link_mode_map, lm);
if (!s) {
bt_free(str);
return NULL;
}
strcat(str, s);
free(s);
return str;
}
/**
* retrun Link mode mapping code given the tring
*
* @param str Link mode mapping string
* @param val Link mode mapping code
* @return 1 match found, 0 error
*/
int hci_strtolm(char *str, unsigned int *val)
{
return hci_str2bit(link_mode_map, str, val);
}
/* Command mapping */
static hci_map commands_map[] = {
{ "Inquiry", 0 },
{ "Inquiry Cancel", 1 },
{ "Periodic Inquiry Mode", 2 },
{ "Exit Periodic Inquiry Mode", 3 },
{ "Create Connection", 4 },
{ "Disconnect", 5 },
{ "Add SCO Connection", 6 },
{ "Cancel Create Connection", 7 },
{ "Accept Connection Request", 8 },
{ "Reject Connection Request", 9 },
{ "Link Key Request Reply", 10 },
{ "Link Key Request Negative Reply", 11 },
{ "PIN Code Request Reply", 12 },
{ "PIN Code Request Negative Reply", 13 },
{ "Change Connection Packet Type", 14 },
{ "Authentication Requested", 15 },
{ "Set Connection Encryption", 16 },
{ "Change Connection Link Key", 17 },
{ "Master Link Key", 18 },
{ "Remote Name Request", 19 },
{ "Cancel Remote Name Request", 20 },
{ "Read Remote Supported Features", 21 },
{ "Read Remote Extended Features", 22 },
{ "Read Remote Version Information", 23 },
{ "Read Clock Offset", 24 },
{ "Read LMP Handle", 25 },
{ "Reserved", 26 },
{ "Reserved", 27 },
{ "Reserved", 28 },
{ "Reserved", 29 },
{ "Reserved", 30 },
{ "Reserved", 31 },
{ "Reserved", 32 },
{ "Hold Mode", 33 },
{ "Sniff Mode", 34 },
{ "Exit Sniff Mode", 35 },
{ "Park State", 36 },
{ "Exit Park State", 37 },
{ "QoS Setup", 38 },
{ "Role Discovery", 39 },
{ "Switch Role", 40 },
{ "Read Link Policy Settings", 41 },
{ "Write Link Policy Settings", 42 },
{ "Read Default Link Policy Settings", 43 },
{ "Write Default Link Policy Settings", 44 },
{ "Flow Specification", 45 },
{ "Set Event Mask", 46 },
{ "Reset", 47 },
{ "Set Event Filter", 48 },
{ "Flush", 49 },
{ "Read PIN Type", 50 },
{ "Write PIN Type", 51 },
{ "Create New Unit Key", 52 },
{ "Read Stored Link Key", 53 },
{ "Write Stored Link Key", 54 },
{ "Delete Stored Link Key", 55 },
{ "Write Local Name", 56 },
{ "Read Local Name", 57 },
{ "Read Connection Accept Timeout", 58 },
{ "Write Connection Accept Timeout", 59 },
{ "Read Page Timeout", 60 },
{ "Write Page Timeout", 61 },
{ "Read Scan Enable", 62 },
{ "Write Scan Enable", 63 },
{ "Read Page Scan Activity", 64 },
{ "Write Page Scan Activity", 65 },
{ "Read Inquiry Scan Activity", 66 },
{ "Write Inquiry Scan Activity", 67 },
{ "Read Authentication Enable", 68 },
{ "Write Authentication Enable", 69 },
{ "Read Encryption Mode", 70 },
{ "Write Encryption Mode", 71 },
{ "Read Class Of Device", 72 },
{ "Write Class Of Device", 73 },
{ "Read Voice Setting", 74 },
{ "Write Voice Setting", 75 },
{ "Read Automatic Flush Timeout", 76 },
{ "Write Automatic Flush Timeout", 77 },
{ "Read Num Broadcast Retransmissions", 78 },
{ "Write Num Broadcast Retransmissions", 79 },
{ "Read Hold Mode Activity", 80 },
{ "Write Hold Mode Activity", 81 },
{ "Read Transmit Power Level", 82 },
{ "Read Synchronous Flow Control Enable", 83 },
{ "Write Synchronous Flow Control Enable", 84 },
{ "Set Host Controller To Host Flow Control", 85 },
{ "Host Buffer Size", 86 },
{ "Host Number Of Completed Packets", 87 },
{ "Read Link Supervision Timeout", 88 },
{ "Write Link Supervision Timeout", 89 },
{ "Read Number of Supported IAC", 90 },
{ "Read Current IAC LAP", 91 },
{ "Write Current IAC LAP", 92 },
{ "Read Page Scan Period Mode", 93 },
{ "Write Page Scan Period Mode", 94 },
{ "Read Page Scan Mode", 95 },
{ "Write Page Scan Mode", 96 },
{ "Set AFH Channel Classification", 97 },
{ "Reserved", 98 },
{ "Reserved", 99 },
{ "Read Inquiry Scan Type", 100 },
{ "Write Inquiry Scan Type", 101 },
{ "Read Inquiry Mode", 102 },
{ "Write Inquiry Mode", 103 },
{ "Read Page Scan Type", 104 },
{ "Write Page Scan Type", 105 },
{ "Read AFH Channel Assessment Mode", 106 },
{ "Write AFH Channel Assessment Mode", 107 },
{ "Reserved", 108 },
{ "Reserved", 109 },
{ "Reserved", 110 },
{ "Reserved", 111 },
{ "Reserved", 112 },
{ "Reserved", 113 },
{ "Reserved", 114 },
{ "Read Local Version Information", 115 },
{ "Read Local Supported Commands", 116 },
{ "Read Local Supported Features", 117 },
{ "Read Local Extended Features", 118 },
{ "Read Buffer Size", 119 },
{ "Read Country Code", 120 },
{ "Read BD ADDR", 121 },
{ "Read Failed Contact Counter", 122 },
{ "Reset Failed Contact Counter", 123 },
{ "Get Link Quality", 124 },
{ "Read RSSI", 125 },
{ "Read AFH Channel Map", 126 },
{ "Read BD Clock", 127 },
{ "Read Loopback Mode", 128 },
{ "Write Loopback Mode", 129 },
{ "Enable Device Under Test Mode", 130 },
{ "Setup Synchronous Connection", 131 },
{ "Accept Synchronous Connection", 132 },
{ "Reject Synchronous Connection", 133 },
{ "Reserved", 134 },
{ "Reserved", 135 },
{ "Read Extended Inquiry Response", 136 },
{ "Write Extended Inquiry Response", 137 },
{ "Refresh Encryption Key", 138 },
{ "Reserved", 139 },
{ "Sniff Subrating", 140 },
{ "Read Simple Pairing Mode", 141 },
{ "Write Simple Pairing Mode", 142 },
{ "Read Local OOB Data", 143 },
{ "Read Inquiry Response Transmit Power Level", 144 },
{ "Write Inquiry Transmit Power Level", 145 },
{ "Read Default Erroneous Data Reporting", 146 },
{ "Write Default Erroneous Data Reporting", 147 },
{ "Reserved", 148 },
{ "Reserved", 149 },
{ "Reserved", 150 },
{ "IO Capability Request Reply", 151 },
{ "User Confirmation Request Reply", 152 },
{ "User Confirmation Request Negative Reply", 153 },
{ "User Passkey Request Reply", 154 },
{ "User Passkey Request Negative Reply", 155 },
{ "Remote OOB Data Request Reply", 156 },
{ "Write Simple Pairing Debug Mode", 157 },
{ "Enhanced Flush", 158 },
{ "Remote OOB Data Request Negative Reply", 159 },
{ "Reserved", 160 },
{ "Reserved", 161 },
{ "Send Keypress Notification", 162 },
{ "IO Capability Request Negative Reply", 163 },
{ "Read Encryption Key Size", 164 },
{ "Reserved", 165 },
{ "Reserved", 166 },
{ "Reserved", 167 },
{ "Create Physical Link", 168 },
{ "Accept Physical Link", 169 },
{ "Disconnect Physical Link", 170 },
{ "Create Logical Link", 171 },
{ "Accept Logical Link", 172 },
{ "Disconnect Logical Link", 173 },
{ "Logical Link Cancel", 174 },
{ "Flow Specification Modify", 175 },
{ "Read Logical Link Accept Timeout", 176 },
{ "Write Logical Link Accept Timeout", 177 },
{ "Set Event Mask Page 2", 178 },
{ "Read Location Data", 179 },
{ "Write Location Data", 180 },
{ "Read Local AMP Info", 181 },
{ "Read Local AMP_ASSOC", 182 },
{ "Write Remote AMP_ASSOC", 183 },
{ "Read Flow Control Mode", 184 },
{ "Write Flow Control Mode", 185 },
{ "Read Data Block Size", 186 },
{ "Reserved", 187 },
{ "Reserved", 188 },
{ "Enable AMP Receiver Reports", 189 },
{ "AMP Test End", 190 },
{ "AMP Test Command", 191 },
{ "Read Enhanced Transmit Power Level", 192 },
{ "Reserved", 193 },
{ "Read Best Effort Flush Timeout", 194 },
{ "Write Best Effort Flush Timeout", 195 },
{ "Short Range Mode", 196 },
{ "Read LE Host Support", 197 },
{ "Write LE Host Support", 198 },
{ "Reserved", 199 },
{ "LE Set Event Mask", 200 },
{ "LE Read Buffer Size", 201 },
{ "LE Read Local Supported Features", 202 },
{ "Reserved", 203 },
{ "LE Set Random Address", 204 },
{ "LE Set Advertising Parameters", 205 },
{ "LE Read Advertising Channel TX Power", 206 },
{ "LE Set Advertising Data", 207 },
{ "LE Set Scan Response Data", 208 },
{ "LE Set Advertise Enable", 209 },
{ "LE Set Scan Parameters", 210 },
{ "LE Set Scan Enable", 211 },
{ "LE Create Connection", 212 },
{ "LE Create Connection Cancel", 213 },
{ "LE Read White List Size", 214 },
{ "LE Clear White List", 215 },
{ "LE Add Device To White List", 216 },
{ "LE Remove Device From White List", 217 },
{ "LE Connection Update", 218 },
{ "LE Set Host Channel Classification", 219 },
{ "LE Read Channel Map", 220 },
{ "LE Read Remote Used Features", 221 },
{ "LE Encrypt", 222 },
{ "LE Rand", 223 },
{ "LE Start Encryption", 224 },
{ "LE Long Term Key Request Reply", 225 },
{ "LE Long Term Key Request Negative Reply", 226 },
{ "LE Read Supported States", 227 },
{ "LE Receiver Test", 228 },
{ "LE Transmitter Test", 229 },
{ "LE Test End", 230 },
{ "Reserved", 231 },
{ NULL }
};
/**
* translate a command code into an english sentence
*
* use the commands_map table which is an array of structures { "english sentence", command code }
*
* @param cmd command code value
* @return english sentence corresponding to the command code (NULL if error)
*/
char *hci_cmdtostr(unsigned int cmd)
{
return hci_uint2str(commands_map, cmd);
}
/**
* translate a byte array of bit "commands" into a series of corresponding english strings which is returned
*
* @param commands an array of bytes corresponding to a list of encoded commands
* @param pref prefix to add to str (if pref !=NULL pref will be the first concatenated string)
* @param width
* @return NULL if error else str which is a string of concatenated commands separated by /0
*/
char *hci_commandstostr(uint8_t *commands, char *pref, int width)
{
unsigned int maxwidth = width - 3;
hci_map *m;
char *off, *ptr, *str;
int size = 10;
m = commands_map;
while (m->str) {
if (commands[m->val / 8] & (1 << (m->val % 8)))
size += strlen(m->str) + (pref ? strlen(pref) : 0) + 3;
m++;
}
str = bt_malloc(size);
if (!str)
return NULL;
ptr = str; *ptr = '\0';
if (pref)
ptr += sprintf(ptr, "%s", pref);
off = ptr;
m = commands_map;
while (m->str) {
if (commands[m->val / 8] & (1 << (m->val % 8))) {
if (strlen(off) + strlen(m->str) > maxwidth) {
ptr += sprintf(ptr, "\n%s", pref ? pref : "");
off = ptr;
}
ptr += sprintf(ptr, "'%s' ", m->str);
}
m++;
}
return str;
}
/* Version mapping */
static hci_map ver_map[] = {
{ "1.0b", 0x00 },
{ "1.1", 0x01 },
{ "1.2", 0x02 },
{ "2.0", 0x03 },
{ "2.1", 0x04 },
{ "3.0", 0x05 },
{ "4.0", 0x06 },
{ "4.1", 0x07 },
{ "4.2", 0x08 },
{ NULL }
};
/**
* translate a version code into a mapping string
*
* @param ver version code
* @return mapping string (NULL if error)
*/
char *hci_vertostr(unsigned int ver)
{
return hci_uint2str(ver_map, ver);
}
/**
* return the version code given the string
*
* @param str version string
* @param ver version code
* @return 1 of match found, 0 error
*/
int hci_strtover(char *str, unsigned int *ver)
{
return hci_str2uint(ver_map, str, ver);
}
/**
* Link Manager Protocol version (same as hci) string given the code
*
* @param ver version code
* @return mapping string (NULL if error)
*/
char *lmp_vertostr(unsigned int ver)
{
return hci_uint2str(ver_map, ver);
}
/**
* Link Manager Protocol code given the string
*
* @param str version string
* @param ver version code
* @return 1 of match found, 0 error
*/
int lmp_strtover(char *str, unsigned int *ver)
{
return hci_str2uint(ver_map, str, ver);
}
static hci_map pal_map[] = {
{ "3.0", 0x01 },
{ NULL }
};
/**
* pal version string given code
*
* @param ver pal version code
* @return pal version string or NULL if error
*/
char *pal_vertostr(unsigned int ver)
{
return hci_uint2str(pal_map, ver);
}
/**
* pal version code given string
*
* @param str pal version string
* @param ver pal version code
* @return 1 pal version code is set 0 error
*/
int pal_strtover(char *str, unsigned int *ver)
{
return hci_str2uint(pal_map, str, ver);
}
/* LMP features mapping */
static hci_map lmp_features_map[8][9] = {
{ /* Byte 0 */
{ "<3-slot packets>", LMP_3SLOT }, /* Bit 0 */
{ "<5-slot packets>", LMP_5SLOT }, /* Bit 1 */
{ "<encryption>", LMP_ENCRYPT }, /* Bit 2 */
{ "<slot offset>", LMP_SOFFSET }, /* Bit 3 */
{ "<timing accuracy>", LMP_TACCURACY }, /* Bit 4 */
{ "<role switch>", LMP_RSWITCH }, /* Bit 5 */
{ "<hold mode>", LMP_HOLD }, /* Bit 6 */
{ "<sniff mode>", LMP_SNIFF }, /* Bit 7 */
{ NULL }
},
{ /* Byte 1 */
{ "<park state>", LMP_PARK }, /* Bit 0 */
{ "<RSSI>", LMP_RSSI }, /* Bit 1 */
{ "<channel quality>", LMP_QUALITY }, /* Bit 2 */
{ "<SCO link>", LMP_SCO }, /* Bit 3 */
{ "<HV2 packets>", LMP_HV2 }, /* Bit 4 */
{ "<HV3 packets>", LMP_HV3 }, /* Bit 5 */
{ "<u-law log>", LMP_ULAW }, /* Bit 6 */
{ "<A-law log>", LMP_ALAW }, /* Bit 7 */
{ NULL }
},
{ /* Byte 2 */
{ "<CVSD>", LMP_CVSD }, /* Bit 0 */
{ "<paging scheme>", LMP_PSCHEME }, /* Bit 1 */
{ "<power control>", LMP_PCONTROL }, /* Bit 2 */
{ "<transparent SCO>", LMP_TRSP_SCO }, /* Bit 3 */
{ "<broadcast encrypt>",LMP_BCAST_ENC }, /* Bit 7 */
{ NULL }
},
{ /* Byte 3 */
{ "<no. 24>", 0x01 }, /* Bit 0 */
{ "<EDR ACL 2 Mbps>", LMP_EDR_ACL_2M }, /* Bit 1 */
{ "<EDR ACL 3 Mbps>", LMP_EDR_ACL_3M }, /* Bit 2 */
{ "<enhanced iscan>", LMP_ENH_ISCAN }, /* Bit 3 */
{ "<interlaced iscan>", LMP_ILACE_ISCAN }, /* Bit 4 */
{ "<interlaced pscan>", LMP_ILACE_PSCAN }, /* Bit 5 */
{ "<inquiry with RSSI>",LMP_RSSI_INQ }, /* Bit 6 */
{ "<extended SCO>", LMP_ESCO }, /* Bit 7 */
{ NULL }
},
{ /* Byte 4 */
{ "<EV4 packets>", LMP_EV4 }, /* Bit 0 */
{ "<EV5 packets>", LMP_EV5 }, /* Bit 1 */
{ "<no. 34>", 0x04 }, /* Bit 2 */
{ "<AFH cap. slave>", LMP_AFH_CAP_SLV }, /* Bit 3 */
{ "<AFH class. slave>", LMP_AFH_CLS_SLV }, /* Bit 4 */
{ "<BR/EDR not supp.>", LMP_NO_BREDR }, /* Bit 5 */
{ "<LE support>", LMP_LE }, /* Bit 6 */
{ "<3-slot EDR ACL>", LMP_EDR_3SLOT }, /* Bit 7 */
{ NULL }
},
{ /* Byte 5 */
{ "<5-slot EDR ACL>", LMP_EDR_5SLOT }, /* Bit 0 */
{ "<sniff subrating>", LMP_SNIFF_SUBR }, /* Bit 1 */
{ "<pause encryption>", LMP_PAUSE_ENC }, /* Bit 2 */
{ "<AFH cap. master>", LMP_AFH_CAP_MST }, /* Bit 3 */
{ "<AFH class. master>",LMP_AFH_CLS_MST }, /* Bit 4 */
{ "<EDR eSCO 2 Mbps>", LMP_EDR_ESCO_2M }, /* Bit 5 */
{ "<EDR eSCO 3 Mbps>", LMP_EDR_ESCO_3M }, /* Bit 6 */
{ "<3-slot EDR eSCO>", LMP_EDR_3S_ESCO }, /* Bit 7 */
{ NULL }
},
{ /* Byte 6 */
{ "<extended inquiry>", LMP_EXT_INQ }, /* Bit 0 */
{ "<LE and BR/EDR>", LMP_LE_BREDR }, /* Bit 1 */
{ "<no. 50>", 0x04 }, /* Bit 2 */
{ "<simple pairing>", LMP_SIMPLE_PAIR }, /* Bit 3 */
{ "<encapsulated PDU>", LMP_ENCAPS_PDU }, /* Bit 4 */
{ "<err. data report>", LMP_ERR_DAT_REP }, /* Bit 5 */
{ "<non-flush flag>", LMP_NFLUSH_PKTS }, /* Bit 6 */
{ "<no. 55>", 0x80 }, /* Bit 7 */
{ NULL }
},
{ /* Byte 7 */
{ "<LSTO>", LMP_LSTO }, /* Bit 1 */
{ "<inquiry TX power>", LMP_INQ_TX_PWR }, /* Bit 1 */
{ "<EPC>", LMP_EPC }, /* Bit 2 */
{ "<no. 59>", 0x08 }, /* Bit 3 */
{ "<no. 60>", 0x10 }, /* Bit 4 */
{ "<no. 61>", 0x20 }, /* Bit 5 */
{ "<no. 62>", 0x40 }, /* Bit 6 */
{ "<extended features>",LMP_EXT_FEAT }, /* Bit 7 */
{ NULL }
},
};
/**
* Link Manager Protocol feature string given code
*
* @param features Link Manager Protocol feature code
* @param pref prefix
* @param width
* @return Link Manager Protocol string or NULL if error
*/
char *lmp_featurestostr(uint8_t *features, char *pref, int width)
{
unsigned int maxwidth = width - 1;
char *off, *ptr, *str;
int i, size = 10;
for (i = 0; i < 8; i++) {
hci_map *m = lmp_features_map[i];
while (m->str) {
if (m->val & features[i])
size += strlen(m->str) +
(pref ? strlen(pref) : 0) + 1;
m++;
}
}
str = bt_malloc(size);
if (!str)
return NULL;
ptr = str; *ptr = '\0';
if (pref)
ptr += sprintf(ptr, "%s", pref);
off = ptr;
for (i = 0; i < 8; i++) {
hci_map *m = lmp_features_map[i];
while (m->str) {
if (m->val & features[i]) {
if (strlen(off) + strlen(m->str) > maxwidth) {
ptr += sprintf(ptr, "\n%s",
pref ? pref : "");
off = ptr;
}
ptr += sprintf(ptr, "%s ", m->str);