ronnie
2022-10-23 5a83b14855e763445ac36672c35ddb68300e4b42
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
/******************************************************************************
 *
 *  Copyright 1999-2012 Broadcom Corporation
 *
 *  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.
 *
 ******************************************************************************/
 
#ifndef BTM_API_TYPES_H
#define BTM_API_TYPES_H
 
#include "bt_target.h"
#include "device/include/esco_parameters.h"
#include "hcidefs.h"
#include "smp_api_types.h"
 
/* Maximum number of bytes allowed for vendor specific command parameters */
#define BTM_MAX_VENDOR_SPECIFIC_LEN HCI_COMMAND_SIZE
 
/* BTM application return status codes */
enum {
  BTM_SUCCESS = 0,         /* 0  Command succeeded                 */
  BTM_CMD_STARTED,         /* 1  Command started OK.               */
  BTM_BUSY,                /* 2  Device busy with another command  */
  BTM_NO_RESOURCES,        /* 3  No resources to issue command     */
  BTM_MODE_UNSUPPORTED,    /* 4  Request for 1 or more unsupported modes */
  BTM_ILLEGAL_VALUE,       /* 5  Illegal parameter value           */
  BTM_WRONG_MODE,          /* 6  Device in wrong mode for request  */
  BTM_UNKNOWN_ADDR,        /* 7  Unknown remote BD address         */
  BTM_DEVICE_TIMEOUT,      /* 8  Device timeout                    */
  BTM_BAD_VALUE_RET,       /* 9  A bad value was received from HCI */
  BTM_ERR_PROCESSING,      /* 10 Generic error                     */
  BTM_NOT_AUTHORIZED,      /* 11 Authorization failed              */
  BTM_DEV_RESET,           /* 12 Device has been reset             */
  BTM_CMD_STORED,          /* 13 request is stored in control block */
  BTM_ILLEGAL_ACTION,      /* 14 state machine gets illegal command */
  BTM_DELAY_CHECK,         /* 15 delay the check on encryption */
  BTM_SCO_BAD_LENGTH,      /* 16 Bad SCO over HCI data length */
  BTM_SUCCESS_NO_SECURITY, /* 17 security passed, no security set  */
  BTM_FAILED_ON_SECURITY,  /* 18 security failed                   */
  BTM_REPEATED_ATTEMPTS,   /* 19 repeated attempts for LE security requests */
  BTM_MODE4_LEVEL4_NOT_SUPPORTED, /* 20 Secure Connections Only Mode can't be
                                     supported */
  BTM_DEV_BLACKLISTED             /* 21 The device is Blacklisted */
};
 
typedef uint8_t tBTM_STATUS;
 
/*************************
 *  Device Control Types
 *************************/
#define BTM_DEVICE_ROLE_BR 0x01
#define BTM_DEVICE_ROLE_DUAL 0x02
#define BTM_MAX_DEVICE_ROLE BTM_DEVICE_ROLE_DUAL
typedef uint8_t tBTM_DEVICE_ROLE;
 
/* Device name of peer (may be truncated to save space in BTM database) */
typedef uint8_t tBTM_BD_NAME[BTM_MAX_REM_BD_NAME_LEN + 1];
 
/* Structure returned with local version information */
typedef struct {
  uint8_t hci_version;
  uint16_t hci_revision;
  uint8_t lmp_version;
  uint16_t manufacturer;
  uint16_t lmp_subversion;
} tBTM_VERSION_INFO;
 
/* Structure returned with Vendor Specific Command complete callback */
typedef struct {
  uint16_t opcode;
  uint16_t param_len;
  uint8_t* p_param_buf;
} tBTM_VSC_CMPL;
 
#define BTM_VSC_CMPL_DATA_SIZE \
  (BTM_MAX_VENDOR_SPECIFIC_LEN + sizeof(tBTM_VSC_CMPL))
/**************************************************
 *  Device Control and General Callback Functions
 **************************************************/
/* Callback function for when device status changes. Appl must poll for
 * what the new state is (BTM_IsDeviceUp). The event occurs whenever the stack
 * has detected that the controller status has changed. This asynchronous event
 * is enabled/disabled by calling BTM_RegisterForDeviceStatusNotif().
*/
enum { BTM_DEV_STATUS_UP, BTM_DEV_STATUS_DOWN, BTM_DEV_STATUS_CMD_TOUT };
 
typedef uint8_t tBTM_DEV_STATUS;
 
typedef void(tBTM_DEV_STATUS_CB)(tBTM_DEV_STATUS status);
 
/* Callback function for when a vendor specific event occurs. The length and
 * array of returned parameter bytes are included. This asynchronous event
 * is enabled/disabled by calling BTM_RegisterForVSEvents().
*/
typedef void(tBTM_VS_EVT_CB)(uint8_t len, uint8_t* p);
 
/* General callback function for notifying an application that a synchronous
 * BTM function is complete. The pointer contains the address of any returned
 * data.
 */
typedef void(tBTM_CMPL_CB)(void* p1);
 
/* VSC callback function for notifying an application that a synchronous
 * BTM function is complete. The pointer contains the address of any returned
 * data.
 */
typedef void(tBTM_VSC_CMPL_CB)(tBTM_VSC_CMPL* p1);
 
/* Callback for apps to check connection and inquiry filters.
 * Parameters are the BD Address of remote and the Dev Class of remote. If the
 * app returns none zero, the connection or inquiry result will be dropped.
*/
typedef uint8_t(tBTM_FILTER_CB)(const RawAddress& bd_addr, DEV_CLASS dc);
 
/*****************************************************************************
 *  DEVICE DISCOVERY - Inquiry, Remote Name, Discovery, Class of Device
 ****************************************************************************/
/*******************************
 *  Device Discovery Constants
 *******************************/
/* Discoverable modes */
#define BTM_NON_DISCOVERABLE 0
#define BTM_LIMITED_DISCOVERABLE 1
#define BTM_GENERAL_DISCOVERABLE 2
#define BTM_DISCOVERABLE_MASK \
  (BTM_LIMITED_DISCOVERABLE | BTM_GENERAL_DISCOVERABLE)
#define BTM_MAX_DISCOVERABLE BTM_GENERAL_DISCOVERABLE
/* high byte for BLE Discoverable modes */
#define BTM_BLE_NON_DISCOVERABLE 0x0000
#define BTM_BLE_LIMITED_DISCOVERABLE 0x0100
#define BTM_BLE_GENERAL_DISCOVERABLE 0x0200
#define BTM_BLE_MAX_DISCOVERABLE BTM_BLE_GENERAL_DISCOVERABLE
#define BTM_BLE_DISCOVERABLE_MASK                            \
  (BTM_BLE_NON_DISCOVERABLE | BTM_BLE_LIMITED_DISCOVERABLE | \
   BTM_BLE_GENERAL_DISCOVERABLE)
 
/* Connectable modes */
#define BTM_NON_CONNECTABLE 0
#define BTM_CONNECTABLE 1
#define BTM_CONNECTABLE_MASK (BTM_NON_CONNECTABLE | BTM_CONNECTABLE)
/* high byte for BLE Connectable modes */
#define BTM_BLE_NON_CONNECTABLE 0x0000
#define BTM_BLE_CONNECTABLE 0x0100
#define BTM_BLE_MAX_CONNECTABLE BTM_BLE_CONNECTABLE
#define BTM_BLE_CONNECTABLE_MASK (BTM_BLE_NON_CONNECTABLE | BTM_BLE_CONNECTABLE)
 
/* Inquiry modes
 * Note: These modes are associated with the inquiry active values (BTM_*ACTIVE)
 */
#define BTM_INQUIRY_NONE 0
#define BTM_GENERAL_INQUIRY 0x01
#define BTM_LIMITED_INQUIRY 0x02
#define BTM_BR_INQUIRY_MASK (BTM_GENERAL_INQUIRY | BTM_LIMITED_INQUIRY)
 
/* high byte of inquiry mode for BLE inquiry mode */
#define BTM_BLE_INQUIRY_NONE 0x00
#define BTM_BLE_GENERAL_INQUIRY 0x10
#define BTM_BLE_LIMITED_INQUIRY 0x20
#define BTM_BLE_INQUIRY_MASK (BTM_BLE_GENERAL_INQUIRY | BTM_BLE_LIMITED_INQUIRY)
 
/* BTM_IsInquiryActive return values (Bit Mask)
 * Note: These bit masks are associated with the inquiry modes (BTM_*_INQUIRY)
 */
/* no inquiry in progress */
#define BTM_INQUIRY_INACTIVE 0x0
/* a general inquiry is in progress */
#define BTM_GENERAL_INQUIRY_ACTIVE BTM_GENERAL_INQUIRY
/* a limited inquiry is in progress */
#define BTM_LIMITED_INQUIRY_ACTIVE BTM_LIMITED_INQUIRY
/* a periodic inquiry is active */
#define BTM_PERIODIC_INQUIRY_ACTIVE 0x8
/* SSP is active, so inquiry is disallowed (work around for FW bug) */
#define BTM_SSP_INQUIRY_ACTIVE 0x4
/* a general inquiry is in progress */
#define BTM_LE_GENERAL_INQUIRY_ACTIVE BTM_BLE_GENERAL_INQUIRY
/* a limited inquiry is in progress */
#define BTM_LE_LIMITED_INQUIRY_ACTIVE BTM_BLE_LIMITED_INQUIRY
 
/* inquiry activity mask */
/* BR/EDR inquiry activity mask */
#define BTM_BR_INQ_ACTIVE_MASK                               \
  (BTM_GENERAL_INQUIRY_ACTIVE | BTM_LIMITED_INQUIRY_ACTIVE | \
   BTM_PERIODIC_INQUIRY_ACTIVE)
/* LE scan activity mask */
#define BTM_BLE_SCAN_ACTIVE_MASK 0xF0
/* LE inquiry activity mask*/
#define BTM_BLE_INQ_ACTIVE_MASK \
  (BTM_LE_GENERAL_INQUIRY_ACTIVE | BTM_LE_LIMITED_INQUIRY_ACTIVE)
/* inquiry activity mask */
#define BTM_INQUIRY_ACTIVE_MASK \
  (BTM_BR_INQ_ACTIVE_MASK | BTM_BLE_INQ_ACTIVE_MASK)
 
/* Define scan types */
#define BTM_SCAN_TYPE_STANDARD 0
#define BTM_SCAN_TYPE_INTERLACED 1 /* 1.2 devices only */
 
/* Define inquiry results mode */
#define BTM_INQ_RESULT_STANDARD 0
#define BTM_INQ_RESULT_WITH_RSSI 1
#define BTM_INQ_RESULT_EXTENDED 2
/* RSSI value not supplied (ignore it) */
#define BTM_INQ_RES_IGNORE_RSSI 0x7f
 
/* Inquiry Filter Condition types (see tBTM_INQ_PARMS) */
/* Inquiry Filtering is turned off */
#define BTM_CLR_INQUIRY_FILTER 0
/* Filter on device class */
#define BTM_FILTER_COND_DEVICE_CLASS HCI_FILTER_COND_DEVICE_CLASS
/* Filter on device addr */
#define BTM_FILTER_COND_BD_ADDR HCI_FILTER_COND_BD_ADDR
 
/* State of the remote name retrieval during inquiry operations.
 * Used in the tBTM_INQ_INFO structure, and returned in the
 * BTM_InqDbRead, BTM_InqDbFirst, and BTM_InqDbNext functions.
 * The name field is valid when the state returned is
 * BTM_INQ_RMT_NAME_DONE */
#define BTM_INQ_RMT_NAME_EMPTY 0
#define BTM_INQ_RMT_NAME_PENDING 1
#define BTM_INQ_RMT_NAME_DONE 2
#define BTM_INQ_RMT_NAME_FAILED 3
 
/*********************************
 *** Class of Device constants ***
 *********************************/
#define BTM_FORMAT_TYPE_1 0x00
 
/****************************
 * minor device class field
 ****************************/
 
/* 0x00 is used as unclassified for all minor device classes */
#define BTM_COD_MINOR_UNCLASSIFIED 0x00
 
/* minor device class field for Computer Major Class */
/* #define BTM_COD_MINOR_UNCLASSIFIED       0x00    */
#define BTM_COD_MINOR_DESKTOP_WORKSTATION 0x04
#define BTM_COD_MINOR_SERVER_COMPUTER 0x08
#define BTM_COD_MINOR_LAPTOP 0x0C
#define BTM_COD_MINOR_HANDHELD_PC_PDA 0x10 /* clam shell */
#define BTM_COD_MINOR_PALM_SIZE_PC_PDA 0x14
#define BTM_COD_MINOR_WEARABLE_COMPUTER 0x18 /* watch sized */
 
/* minor device class field for Phone Major Class */
/* #define BTM_COD_MINOR_UNCLASSIFIED       0x00    */
#define BTM_COD_MINOR_CELLULAR 0x04
#define BTM_COD_MINOR_CORDLESS 0x08
#define BTM_COD_MINOR_SMART_PHONE 0x0C
/* wired modem or voice gatway */
#define BTM_COD_MINOR_WIRED_MDM_V_GTWY 0x10
#define BTM_COD_MINOR_ISDN_ACCESS 0x14
 
/* minor device class field for LAN Access Point Major Class */
/* Load Factor Field bit 5-7 */
#define BTM_COD_MINOR_FULLY_AVAILABLE 0x00
#define BTM_COD_MINOR_1_17_UTILIZED 0x20
#define BTM_COD_MINOR_17_33_UTILIZED 0x40
#define BTM_COD_MINOR_33_50_UTILIZED 0x60
#define BTM_COD_MINOR_50_67_UTILIZED 0x80
#define BTM_COD_MINOR_67_83_UTILIZED 0xA0
#define BTM_COD_MINOR_83_99_UTILIZED 0xC0
#define BTM_COD_MINOR_NO_SERVICE_AVAILABLE 0xE0
/* sub-Field bit 2-4 */
/* #define BTM_COD_MINOR_UNCLASSIFIED       0x00    */
 
/* minor device class field for Audio/Video Major Class */
/* #define BTM_COD_MINOR_UNCLASSIFIED       0x00    */
#define BTM_COD_MINOR_CONFM_HEADSET 0x04
#define BTM_COD_MINOR_CONFM_HANDSFREE 0x08
#define BTM_COD_MINOR_MICROPHONE 0x10
#define BTM_COD_MINOR_LOUDSPEAKER 0x14
#define BTM_COD_MINOR_HEADPHONES 0x18
#define BTM_COD_MINOR_PORTABLE_AUDIO 0x1C
#define BTM_COD_MINOR_CAR_AUDIO 0x20
#define BTM_COD_MINOR_SET_TOP_BOX 0x24
#define BTM_COD_MINOR_HIFI_AUDIO 0x28
#define BTM_COD_MINOR_VCR 0x2C
#define BTM_COD_MINOR_VIDEO_CAMERA 0x30
#define BTM_COD_MINOR_CAMCORDER 0x34
#define BTM_COD_MINOR_VIDEO_MONITOR 0x38
#define BTM_COD_MINOR_VIDDISP_LDSPKR 0x3C
#define BTM_COD_MINOR_VIDEO_CONFERENCING 0x40
#define BTM_COD_MINOR_GAMING_TOY 0x48
 
/* minor device class field for Peripheral Major Class */
/* Bits 6-7 independently specify mouse, keyboard, or combo mouse/keyboard */
#define BTM_COD_MINOR_KEYBOARD 0x40
#define BTM_COD_MINOR_POINTING 0x80
#define BTM_COD_MINOR_COMBO 0xC0
/* Bits 2-5 OR'd with selection from bits 6-7 */
/* #define BTM_COD_MINOR_UNCLASSIFIED       0x00    */
#define BTM_COD_MINOR_JOYSTICK 0x04
#define BTM_COD_MINOR_GAMEPAD 0x08
#define BTM_COD_MINOR_REMOTE_CONTROL 0x0C
#define BTM_COD_MINOR_SENSING_DEVICE 0x10
#define BTM_COD_MINOR_DIGITIZING_TABLET 0x14
#define BTM_COD_MINOR_CARD_READER 0x18 /* e.g. SIM card reader */
#define BTM_COD_MINOR_DIGITAL_PAN 0x1C
#define BTM_COD_MINOR_HAND_SCANNER 0x20
#define BTM_COD_MINOR_HAND_GESTURAL_INPUT 0x24
 
/* minor device class field for Imaging Major Class */
/* Bits 5-7 independently specify display, camera, scanner, or printer */
#define BTM_COD_MINOR_DISPLAY 0x10
#define BTM_COD_MINOR_CAMERA 0x20
#define BTM_COD_MINOR_SCANNER 0x40
#define BTM_COD_MINOR_PRINTER 0x80
/* Bits 2-3 Reserved */
/* #define BTM_COD_MINOR_UNCLASSIFIED       0x00    */
 
/* minor device class field for Wearable Major Class */
/* Bits 2-7 meaningful    */
#define BTM_COD_MINOR_WRIST_WATCH 0x04
#define BTM_COD_MINOR_PAGER 0x08
#define BTM_COD_MINOR_JACKET 0x0C
#define BTM_COD_MINOR_HELMET 0x10
#define BTM_COD_MINOR_GLASSES 0x14
 
/* minor device class field for Toy Major Class */
/* Bits 2-7 meaningful    */
#define BTM_COD_MINOR_ROBOT 0x04
#define BTM_COD_MINOR_VEHICLE 0x08
#define BTM_COD_MINOR_DOLL_ACTION_FIGURE 0x0C
#define BTM_COD_MINOR_CONTROLLER 0x10
#define BTM_COD_MINOR_GAME 0x14
 
/* minor device class field for Health Major Class */
/* Bits 2-7 meaningful    */
#define BTM_COD_MINOR_BLOOD_MONITOR 0x04
#define BTM_COD_MINOR_THERMOMETER 0x08
#define BTM_COD_MINOR_WEIGHING_SCALE 0x0C
#define BTM_COD_MINOR_GLUCOSE_METER 0x10
#define BTM_COD_MINOR_PULSE_OXIMETER 0x14
#define BTM_COD_MINOR_HEART_PULSE_MONITOR 0x18
#define BTM_COD_MINOR_HEALTH_DATA_DISPLAY 0x1C
#define BTM_COD_MINOR_STEP_COUNTER 0x20
#define BTM_COD_MINOR_BODY_COM_ANALYZER 0x24
#define BTM_COD_MINOR_PEAK_FLOW_MONITOR 0x28
#define BTM_COD_MINOR_MEDICATION_MONITOR 0x2C
#define BTM_COD_MINOR_KNEE_PROSTHESIS 0x30
#define BTM_COD_MINOR_ANKLE_PROSTHESIS 0x34
 
/***************************
 * major device class field
 ***************************/
#define BTM_COD_MAJOR_MISCELLANEOUS 0x00
#define BTM_COD_MAJOR_COMPUTER 0x01
#define BTM_COD_MAJOR_PHONE 0x02
#define BTM_COD_MAJOR_LAN_ACCESS_PT 0x03
#define BTM_COD_MAJOR_AUDIO 0x04
#define BTM_COD_MAJOR_PERIPHERAL 0x05
#define BTM_COD_MAJOR_IMAGING 0x06
#define BTM_COD_MAJOR_WEARABLE 0x07
#define BTM_COD_MAJOR_TOY 0x08
#define BTM_COD_MAJOR_HEALTH 0x09
#define BTM_COD_MAJOR_UNCLASSIFIED 0x1F
 
/***************************
 * service class fields
 ***************************/
#define BTM_COD_SERVICE_LMTD_DISCOVER 0x0020
#define BTM_COD_SERVICE_POSITIONING 0x0100
#define BTM_COD_SERVICE_NETWORKING 0x0200
#define BTM_COD_SERVICE_RENDERING 0x0400
#define BTM_COD_SERVICE_CAPTURING 0x0800
#define BTM_COD_SERVICE_OBJ_TRANSFER 0x1000
#define BTM_COD_SERVICE_AUDIO 0x2000
#define BTM_COD_SERVICE_TELEPHONY 0x4000
#define BTM_COD_SERVICE_INFORMATION 0x8000
 
/* class of device field macros */
#define BTM_COD_FORMAT_TYPE(u8, pd) \
  { (u8) = (pd)[2] & 0x03; }
#define BTM_COD_MINOR_CLASS(u8, pd) \
  { (u8) = (pd)[2] & 0xFC; }
#define BTM_COD_MAJOR_CLASS(u8, pd) \
  { (u8) = (pd)[1] & 0x1F; }
#define BTM_COD_SERVICE_CLASS(u16, pd) \
  {                                    \
    (u16) = (pd)[0];                   \
    (u16) <<= 8;                       \
    (u16) += (pd)[1] & 0xE0;           \
  }
 
/* to set the fields (assumes that format type is always 0) */
#define FIELDS_TO_COD(pd, mn, mj, sv)                   \
  {                                                     \
    (pd)[2] = mn;                                       \
    (pd)[1] = (mj) + ((sv)&BTM_COD_SERVICE_CLASS_LO_B); \
    (pd)[0] = (sv) >> 8;                                \
  }
 
/* the COD masks */
#define BTM_COD_FORMAT_TYPE_MASK 0x03
#define BTM_COD_MINOR_CLASS_MASK 0xFC
#define BTM_COD_MAJOR_CLASS_MASK 0x1F
#define BTM_COD_SERVICE_CLASS_LO_B 0x00E0
#define BTM_COD_SERVICE_CLASS_MASK 0xFFE0
 
/* BTM service definitions
 * Used for storing EIR data to bit mask
*/
enum {
  BTM_EIR_UUID_SERVCLASS_SERVICE_DISCOVERY_SERVER,
  /*    BTM_EIR_UUID_SERVCLASS_BROWSE_GROUP_DESCRIPTOR,   */
  /*    BTM_EIR_UUID_SERVCLASS_PUBLIC_BROWSE_GROUP,       */
  BTM_EIR_UUID_SERVCLASS_SERIAL_PORT,
  BTM_EIR_UUID_SERVCLASS_LAN_ACCESS_USING_PPP,
  BTM_EIR_UUID_SERVCLASS_DIALUP_NETWORKING,
  BTM_EIR_UUID_SERVCLASS_IRMC_SYNC,
  BTM_EIR_UUID_SERVCLASS_OBEX_OBJECT_PUSH,
  BTM_EIR_UUID_SERVCLASS_OBEX_FILE_TRANSFER,
  BTM_EIR_UUID_SERVCLASS_IRMC_SYNC_COMMAND,
  BTM_EIR_UUID_SERVCLASS_HEADSET,
  BTM_EIR_UUID_SERVCLASS_CORDLESS_TELEPHONY,
  BTM_EIR_UUID_SERVCLASS_AUDIO_SOURCE,
  BTM_EIR_UUID_SERVCLASS_AUDIO_SINK,
  BTM_EIR_UUID_SERVCLASS_AV_REM_CTRL_TARGET,
  /*    BTM_EIR_UUID_SERVCLASS_ADV_AUDIO_DISTRIBUTION,    */
  BTM_EIR_UUID_SERVCLASS_AV_REMOTE_CONTROL,
  /*    BTM_EIR_UUID_SERVCLASS_VIDEO_CONFERENCING,        */
  BTM_EIR_UUID_SERVCLASS_INTERCOM,
  BTM_EIR_UUID_SERVCLASS_FAX,
  BTM_EIR_UUID_SERVCLASS_HEADSET_AUDIO_GATEWAY,
  /*    BTM_EIR_UUID_SERVCLASS_WAP,                       */
  /*    BTM_EIR_UUID_SERVCLASS_WAP_CLIENT,                */
  BTM_EIR_UUID_SERVCLASS_PANU,
  BTM_EIR_UUID_SERVCLASS_NAP,
  BTM_EIR_UUID_SERVCLASS_GN,
  BTM_EIR_UUID_SERVCLASS_DIRECT_PRINTING,
  /*    BTM_EIR_UUID_SERVCLASS_REFERENCE_PRINTING,        */
  BTM_EIR_UUID_SERVCLASS_IMAGING,
  BTM_EIR_UUID_SERVCLASS_IMAGING_RESPONDER,
  BTM_EIR_UUID_SERVCLASS_IMAGING_AUTO_ARCHIVE,
  BTM_EIR_UUID_SERVCLASS_IMAGING_REF_OBJECTS,
  BTM_EIR_UUID_SERVCLASS_HF_HANDSFREE,
  BTM_EIR_UUID_SERVCLASS_AG_HANDSFREE,
  BTM_EIR_UUID_SERVCLASS_DIR_PRT_REF_OBJ_SERVICE,
  /*    BTM_EIR_UUID_SERVCLASS_REFLECTED_UI,              */
  BTM_EIR_UUID_SERVCLASS_BASIC_PRINTING,
  BTM_EIR_UUID_SERVCLASS_PRINTING_STATUS,
  BTM_EIR_UUID_SERVCLASS_HUMAN_INTERFACE,
  BTM_EIR_UUID_SERVCLASS_CABLE_REPLACEMENT,
  BTM_EIR_UUID_SERVCLASS_HCRP_PRINT,
  BTM_EIR_UUID_SERVCLASS_HCRP_SCAN,
  /*    BTM_EIR_UUID_SERVCLASS_COMMON_ISDN_ACCESS,        */
  /*    BTM_EIR_UUID_SERVCLASS_VIDEO_CONFERENCING_GW,     */
  /*    BTM_EIR_UUID_SERVCLASS_UDI_MT,                    */
  /*    BTM_EIR_UUID_SERVCLASS_UDI_TA,                    */
  /*    BTM_EIR_UUID_SERVCLASS_VCP,                       */
  BTM_EIR_UUID_SERVCLASS_SAP,
  BTM_EIR_UUID_SERVCLASS_PBAP_PCE,
  BTM_EIR_UUID_SERVCLASS_PBAP_PSE,
  /*    BTM_EIR_UUID_SERVCLASS_TE_PHONE_ACCESS,           */
  /*    BTM_EIR_UUID_SERVCLASS_ME_PHONE_ACCESS,           */
  BTM_EIR_UUID_SERVCLASS_PHONE_ACCESS,
  BTM_EIR_UUID_SERVCLASS_HEADSET_HS,
  BTM_EIR_UUID_SERVCLASS_PNP_INFORMATION,
  /*    BTM_EIR_UUID_SERVCLASS_GENERIC_NETWORKING,        */
  /*    BTM_EIR_UUID_SERVCLASS_GENERIC_FILETRANSFER,      */
  /*    BTM_EIR_UUID_SERVCLASS_GENERIC_AUDIO,             */
  /*    BTM_EIR_UUID_SERVCLASS_GENERIC_TELEPHONY,         */
  /*    BTM_EIR_UUID_SERVCLASS_UPNP_SERVICE,              */
  /*    BTM_EIR_UUID_SERVCLASS_UPNP_IP_SERVICE,           */
  /*    BTM_EIR_UUID_SERVCLASS_ESDP_UPNP_IP_PAN,          */
  /*    BTM_EIR_UUID_SERVCLASS_ESDP_UPNP_IP_LAP,          */
  /*    BTM_EIR_UUID_SERVCLASS_ESDP_UPNP_IP_L2CAP,        */
  BTM_EIR_UUID_SERVCLASS_VIDEO_SOURCE,
  BTM_EIR_UUID_SERVCLASS_VIDEO_SINK,
  /*    BTM_EIR_UUID_SERVCLASS_VIDEO_DISTRIBUTION         */
  /*    BTM_EIR_UUID_SERVCLASS_HDP_PROFILE                */
  BTM_EIR_UUID_SERVCLASS_MESSAGE_ACCESS,
  BTM_EIR_UUID_SERVCLASS_MESSAGE_NOTIFICATION,
  BTM_EIR_UUID_SERVCLASS_HDP_SOURCE,
  BTM_EIR_UUID_SERVCLASS_HDP_SINK,
  BTM_EIR_MAX_SERVICES
};
 
/* search result in EIR of inquiry database */
#define BTM_EIR_FOUND 0
#define BTM_EIR_NOT_FOUND 1
#define BTM_EIR_UNKNOWN 2
 
typedef uint8_t tBTM_EIR_SEARCH_RESULT;
 
/* 0x01 */
#define BTM_EIR_FLAGS_TYPE HCI_EIR_FLAGS_TYPE
/* 0x02 */
#define BTM_EIR_MORE_16BITS_UUID_TYPE HCI_EIR_MORE_16BITS_UUID_TYPE
/* 0x03 */
#define BTM_EIR_COMPLETE_16BITS_UUID_TYPE HCI_EIR_COMPLETE_16BITS_UUID_TYPE
/* 0x04 */
#define BTM_EIR_MORE_32BITS_UUID_TYPE HCI_EIR_MORE_32BITS_UUID_TYPE
/* 0x05 */
#define BTM_EIR_COMPLETE_32BITS_UUID_TYPE HCI_EIR_COMPLETE_32BITS_UUID_TYPE
/* 0x06 */
#define BTM_EIR_MORE_128BITS_UUID_TYPE HCI_EIR_MORE_128BITS_UUID_TYPE
/* 0x07 */
#define BTM_EIR_COMPLETE_128BITS_UUID_TYPE HCI_EIR_COMPLETE_128BITS_UUID_TYPE
/* 0x08 */
#define BTM_EIR_SHORTENED_LOCAL_NAME_TYPE HCI_EIR_SHORTENED_LOCAL_NAME_TYPE
/* 0x09 */
#define BTM_EIR_COMPLETE_LOCAL_NAME_TYPE HCI_EIR_COMPLETE_LOCAL_NAME_TYPE
/* 0x0A */
#define BTM_EIR_TX_POWER_LEVEL_TYPE HCI_EIR_TX_POWER_LEVEL_TYPE
/* 0xFF */
#define BTM_EIR_MANUFACTURER_SPECIFIC_TYPE HCI_EIR_MANUFACTURER_SPECIFIC_TYPE
 
/* the following EIR tags are defined to OOB, not regular EIR data */
/* 6 bytes */
#define BTM_EIR_OOB_BD_ADDR_TYPE HCI_EIR_OOB_BD_ADDR_TYPE
/* 3 bytes */
#define BTM_EIR_OOB_COD_TYPE HCI_EIR_OOB_COD_TYPE
/* 16 bytes */
#define BTM_EIR_OOB_SSP_HASH_C_TYPE HCI_EIR_OOB_SSP_HASH_C_TYPE
/* 16 bytes */
#define BTM_EIR_OOB_SSP_RAND_R_TYPE HCI_EIR_OOB_SSP_RAND_R_TYPE
 
/* include 2 bytes length & 6 bytes bd_addr */
#define BTM_OOB_MANDATORY_SIZE 8
#define BTM_OOB_DATA_LEN_SIZE 2
#define BTM_OOB_BD_ADDR_SIZE 6
#define BTM_OOB_COD_SIZE BT_OOB_COD_SIZE
#define BTM_OOB_HASH_C_SIZE BT_OOB_HASH_C_SIZE
#define BTM_OOB_RAND_R_SIZE BT_OOB_RAND_R_SIZE
 
#define BTM_BLE_SEC_NONE 0
/* encrypt the link using current key */
#define BTM_BLE_SEC_ENCRYPT 1
#define BTM_BLE_SEC_ENCRYPT_NO_MITM 2
#define BTM_BLE_SEC_ENCRYPT_MITM 3
typedef uint8_t tBTM_BLE_SEC_ACT;
 
/*******************************************************************************
 * BTM Services MACROS handle array of uint32_t bits for more than 32 services
 ******************************************************************************/
/* Determine the number of uint32_t's necessary for services */
#define BTM_EIR_ARRAY_BITS 32 /* Number of bits in each array element */
#define BTM_EIR_SERVICE_ARRAY_SIZE                         \
  (((uint32_t)BTM_EIR_MAX_SERVICES / BTM_EIR_ARRAY_BITS) + \
   (((uint32_t)BTM_EIR_MAX_SERVICES % BTM_EIR_ARRAY_BITS) ? 1 : 0))
 
/* MACRO to set the service bit mask in a bit stream */
#define BTM_EIR_SET_SERVICE(p, service)                              \
  (((uint32_t*)(p))[(((uint32_t)(service)) / BTM_EIR_ARRAY_BITS)] |= \
   ((uint32_t)1 << (((uint32_t)(service)) % BTM_EIR_ARRAY_BITS)))
 
/* MACRO to clear the service bit mask in a bit stream */
#define BTM_EIR_CLR_SERVICE(p, service)                              \
  (((uint32_t*)(p))[(((uint32_t)(service)) / BTM_EIR_ARRAY_BITS)] &= \
   ~((uint32_t)1 << (((uint32_t)(service)) % BTM_EIR_ARRAY_BITS)))
 
/* MACRO to check the service bit mask in a bit stream */
#define BTM_EIR_HAS_SERVICE(p, service)                               \
  ((((uint32_t*)(p))[(((uint32_t)(service)) / BTM_EIR_ARRAY_BITS)] &  \
    ((uint32_t)1 << (((uint32_t)(service)) % BTM_EIR_ARRAY_BITS))) >> \
   (((uint32_t)(service)) % BTM_EIR_ARRAY_BITS))
 
/* start of EIR in HCI buffer, 4 bytes = HCI Command(2) + Length(1) + FEC_Req(1)
 */
#define BTM_HCI_EIR_OFFSET (BT_HDR_SIZE + 4)
 
/***************************
 *  Device Discovery Types
 ***************************/
/* Definitions of the parameters passed to BTM_StartInquiry and
 * BTM_SetPeriodicInquiryMode.
*/
typedef struct /* contains the two device class condition fields */
{
  DEV_CLASS dev_class;
  DEV_CLASS dev_class_mask;
} tBTM_COD_COND;
 
typedef union /* contains the inquiry filter condition */
{
  RawAddress bdaddr_cond;
  tBTM_COD_COND cod_cond;
} tBTM_INQ_FILT_COND;
 
typedef struct /* contains the parameters passed to the inquiry functions */
{
  uint8_t mode;      /* general or limited */
  uint8_t duration;  /* duration of the inquiry (1.28 sec increments) */
  uint8_t max_resps; /* maximum number of responses to return */
  bool report_dup; /* report duplicated inquiry response with higher RSSI value
                      */
  uint8_t filter_cond_type; /* new devices, BD ADDR, COD, or No filtering */
  tBTM_INQ_FILT_COND filter_cond; /* filter value based on filter cond type */
} tBTM_INQ_PARMS;
 
#define BTM_INQ_RESULT_BR 0x01
#define BTM_INQ_RESULT_BLE 0x02
 
constexpr uint8_t BLE_EVT_CONNECTABLE_BIT = 0;
constexpr uint8_t BLE_EVT_SCANNABLE_BIT = 1;
constexpr uint8_t BLE_EVT_DIRECTED_BIT = 2;
constexpr uint8_t BLE_EVT_SCAN_RESPONSE_BIT = 3;
constexpr uint8_t BLE_EVT_LEGACY_BIT = 4;
 
constexpr uint8_t PHY_LE_NO_PACKET = 0x00;
constexpr uint8_t PHY_LE_1M = 0x01;
constexpr uint8_t PHY_LE_2M = 0x02;
constexpr uint8_t PHY_LE_CODED = 0x03;
 
constexpr uint8_t NO_ADI_PRESENT = 0xFF;
constexpr uint8_t TX_POWER_NOT_PRESENT = 0x7F;
 
/* These are the fields returned in each device's response to the inquiry.  It
 * is returned in the results callback if registered.
*/
typedef struct {
  uint16_t clock_offset;
  RawAddress remote_bd_addr;
  DEV_CLASS dev_class;
  uint8_t page_scan_rep_mode;
  uint8_t page_scan_per_mode;
  uint8_t page_scan_mode;
  int8_t rssi; /* Set to BTM_INQ_RES_IGNORE_RSSI if  not valid */
  uint32_t eir_uuid[BTM_EIR_SERVICE_ARRAY_SIZE];
  bool eir_complete_list;
  tBT_DEVICE_TYPE device_type;
  uint8_t inq_result_type;
  uint8_t ble_addr_type;
  uint16_t ble_evt_type;
  uint8_t ble_primary_phy;
  uint8_t ble_secondary_phy;
  uint8_t ble_advertising_sid;
  int8_t ble_tx_power;
  uint16_t ble_periodic_adv_int;
  uint8_t flag;
} tBTM_INQ_RESULTS;
 
/* This is the inquiry response information held in its database by BTM, and
 * available to applications via BTM_InqDbRead, BTM_InqDbFirst, and
 * BTM_InqDbNext.
*/
typedef struct {
  tBTM_INQ_RESULTS results;
 
  bool appl_knows_rem_name; /* set by application if it knows the remote name of
                               the peer device.
                               This is later used by application to determine if
                               remote name request is
                               required to be done. Having the flag here avoid
                               duplicate store of inquiry results */
  uint16_t remote_name_len;
  tBTM_BD_NAME remote_name;
  uint8_t remote_name_state;
  uint8_t remote_name_type;
 
} tBTM_INQ_INFO;
 
/* Structure returned with inquiry complete callback */
typedef struct {
  tBTM_STATUS status;
  uint8_t num_resp; /* Number of results from the current inquiry */
} tBTM_INQUIRY_CMPL;
 
/* Structure returned with remote name  request */
typedef struct {
  uint16_t status;
  RawAddress bd_addr;
  uint16_t length;
  BD_NAME remote_bd_name;
} tBTM_REMOTE_DEV_NAME;
 
typedef struct {
  uint8_t pcm_intf_rate; /* PCM interface rate: 0: 128kbps, 1: 256 kbps;
                             2:512 bps; 3: 1024kbps; 4: 2048kbps */
  uint8_t frame_type;    /* frame type: 0: short; 1: long */
  uint8_t sync_mode;     /* sync mode: 0: slave; 1: master */
  uint8_t clock_mode;    /* clock mode: 0: slave; 1: master */
 
} tBTM_SCO_PCM_PARAM;
 
/****************************************
 *  Device Discovery Callback Functions
 ****************************************/
/* Callback function for asynchronous notifications when the BTM inquiry DB
 * changes. First param is inquiry database, second is if added to or removed
 * from the inquiry database.
*/
typedef void(tBTM_INQ_DB_CHANGE_CB)(void* p1, bool is_new);
 
/* Callback function for notifications when the BTM gets inquiry response.
 * First param is inquiry results database, second is pointer of EIR.
*/
typedef void(tBTM_INQ_RESULTS_CB)(tBTM_INQ_RESULTS* p_inq_results,
                                  uint8_t* p_eir, uint16_t eir_len);
 
/*****************************************************************************
 *  ACL CHANNEL MANAGEMENT
 ****************************************************************************/
/******************
 *  ACL Constants
 ******************/
 
/* ACL modes */
#define BTM_ACL_MODE_NORMAL HCI_MODE_ACTIVE
#define BTM_ACL_MODE_HOLD HCI_MODE_HOLD
#define BTM_ACL_MODE_SNIFF HCI_MODE_SNIFF
#define BTM_ACL_MODE_PARK HCI_MODE_PARK
 
/* Returned with structure in role switch callback (tBTM_ROLE_SWITCH_CMPL) */
#define BTM_ROLE_MASTER HCI_ROLE_MASTER
#define BTM_ROLE_SLAVE HCI_ROLE_SLAVE
#define BTM_ROLE_UNDEFINED 0xff /* undefined value (error status) */
 
/* ACL Packet Types */
#define BTM_ACL_PKT_TYPES_MASK_DM1 HCI_PKT_TYPES_MASK_DM1
#define BTM_ACL_PKT_TYPES_MASK_DH1 HCI_PKT_TYPES_MASK_DH1
#define BTM_ACL_PKT_TYPES_MASK_DM3 HCI_PKT_TYPES_MASK_DM3
#define BTM_ACL_PKT_TYPES_MASK_DH3 HCI_PKT_TYPES_MASK_DH3
#define BTM_ACL_PKT_TYPES_MASK_DM5 HCI_PKT_TYPES_MASK_DM5
#define BTM_ACL_PKT_TYPES_MASK_DH5 HCI_PKT_TYPES_MASK_DH5
#define BTM_ACL_PKT_TYPES_MASK_NO_2_DH1 HCI_PKT_TYPES_MASK_NO_2_DH1
#define BTM_ACL_PKT_TYPES_MASK_NO_3_DH1 HCI_PKT_TYPES_MASK_NO_3_DH1
#define BTM_ACL_PKT_TYPES_MASK_NO_2_DH3 HCI_PKT_TYPES_MASK_NO_2_DH3
#define BTM_ACL_PKT_TYPES_MASK_NO_3_DH3 HCI_PKT_TYPES_MASK_NO_3_DH3
#define BTM_ACL_PKT_TYPES_MASK_NO_2_DH5 HCI_PKT_TYPES_MASK_NO_2_DH5
#define BTM_ACL_PKT_TYPES_MASK_NO_3_DH5 HCI_PKT_TYPES_MASK_NO_3_DH5
 
/***************
 *  ACL Types
 ***************/
 
/* Structure returned with Role Switch information (in tBTM_CMPL_CB callback
 * function) in response to BTM_SwitchRole call.
*/
typedef struct {
  uint8_t hci_status;     /* HCI status returned with the event */
  uint8_t role;           /* BTM_ROLE_MASTER or BTM_ROLE_SLAVE */
  RawAddress remote_bd_addr; /* Remote BD addr involved with the switch */
} tBTM_ROLE_SWITCH_CMPL;
 
/* Structure returned with QoS information (in tBTM_CMPL_CB callback function)
 * in response to BTM_SetQoS call.
*/
typedef struct {
  FLOW_SPEC flow;
  uint16_t handle;
  uint8_t status;
} tBTM_QOS_SETUP_CMPL;
 
/* Structure returned with read RSSI event (in tBTM_CMPL_CB callback function)
 * in response to BTM_ReadRSSI call.
*/
typedef struct {
  tBTM_STATUS status;
  uint8_t hci_status;
  int8_t rssi;
  RawAddress rem_bda;
} tBTM_RSSI_RESULT;
 
/* Structure returned with read failed contact counter event
 * (in tBTM_CMPL_CB callback function) in response to
 * BTM_ReadFailedContactCounter call.
 */
typedef struct {
  tBTM_STATUS status;
  uint8_t hci_status;
  uint16_t failed_contact_counter;
  RawAddress rem_bda;
} tBTM_FAILED_CONTACT_COUNTER_RESULT;
 
/* Structure returned with read automatic flush timeout event
 * (in tBTM_CMPL_CB callback function) in response to
 * BTM_ReadAutomaticFlushTimeout call.
 */
typedef struct {
  tBTM_STATUS status;
  uint8_t hci_status;
  uint16_t automatic_flush_timeout;
  RawAddress rem_bda;
} tBTM_AUTOMATIC_FLUSH_TIMEOUT_RESULT;
 
/* Structure returned with read current TX power event (in tBTM_CMPL_CB callback
 * function) in response to BTM_ReadTxPower call.
*/
typedef struct {
  tBTM_STATUS status;
  uint8_t hci_status;
  int8_t tx_power;
  RawAddress rem_bda;
} tBTM_TX_POWER_RESULT;
 
/* Structure returned with read link quality event (in tBTM_CMPL_CB callback
 * function) in response to BTM_ReadLinkQuality call.
*/
typedef struct {
  tBTM_STATUS status;
  uint8_t hci_status;
  uint8_t link_quality;
  RawAddress rem_bda;
} tBTM_LINK_QUALITY_RESULT;
 
/* Structure returned with read inq tx power quality event (in tBTM_CMPL_CB
 * callback function) in response to BTM_ReadInquiryRspTxPower call.
*/
typedef struct {
  tBTM_STATUS status;
  uint8_t hci_status;
  int8_t tx_power;
} tBTM_INQ_TXPWR_RESULT;
 
enum {
  BTM_BL_CONN_EVT,
  BTM_BL_DISCN_EVT,
  BTM_BL_UPDATE_EVT,
  BTM_BL_ROLE_CHG_EVT,
  BTM_BL_COLLISION_EVT
};
typedef uint8_t tBTM_BL_EVENT;
typedef uint16_t tBTM_BL_EVENT_MASK;
 
#define BTM_BL_CONN_MASK 0x0001
#define BTM_BL_DISCN_MASK 0x0002
#define BTM_BL_UPDATE_MASK 0x0004
#define BTM_BL_ROLE_CHG_MASK 0x0008
 
/* Device features mask definitions */
#define BTM_FEATURE_BYTES_PER_PAGE HCI_FEATURE_BYTES_PER_PAGE
#define BTM_EXT_FEATURES_PAGE_MAX HCI_EXT_FEATURES_PAGE_MAX
 
/* the data type associated with BTM_BL_CONN_EVT */
typedef struct {
  tBTM_BL_EVENT event;     /* The event reported. */
  const RawAddress* p_bda; /* The address of the newly connected device */
  DEV_CLASS_PTR p_dc;      /* The device class */
  BD_NAME_PTR p_bdn;       /* The device name */
  uint8_t* p_features;     /* pointer to the remote device's features page[0]
                              (supported features page) */
  uint16_t handle;         /* connection handle */
  tBT_TRANSPORT transport; /* link is LE or not */
} tBTM_BL_CONN_DATA;
 
/* the data type associated with BTM_BL_DISCN_EVT */
typedef struct {
  tBTM_BL_EVENT event;     /* The event reported. */
  const RawAddress* p_bda; /* The address of the disconnected device */
  uint16_t handle;         /* disconnected connection handle */
  tBT_TRANSPORT transport; /* link is LE link or not */
} tBTM_BL_DISCN_DATA;
 
/* Busy-Level shall have the inquiry_paging mask set when
 * inquiry/paging is in progress, Else the number of ACL links */
#define BTM_BL_INQUIRY_PAGING_MASK 0x10
#define BTM_BL_INQUIRY_STARTED (BTM_BL_INQUIRY_PAGING_MASK | 0x1)
#define BTM_BL_INQUIRY_CANCELLED (BTM_BL_INQUIRY_PAGING_MASK | 0x2)
#define BTM_BL_INQUIRY_COMPLETE (BTM_BL_INQUIRY_PAGING_MASK | 0x3)
#define BTM_BL_PAGING_STARTED (BTM_BL_INQUIRY_PAGING_MASK | 0x4)
#define BTM_BL_PAGING_COMPLETE (BTM_BL_INQUIRY_PAGING_MASK | 0x5)
/* the data type associated with BTM_BL_UPDATE_EVT */
typedef struct {
  tBTM_BL_EVENT event;      /* The event reported. */
  uint8_t busy_level;       /* when paging or inquiring, level is 10.
                             * Otherwise, the number of ACL links. */
  uint8_t busy_level_flags; /* Notifies actual inquiry/page activities */
} tBTM_BL_UPDATE_DATA;
 
/* the data type associated with BTM_BL_ROLE_CHG_EVT */
typedef struct {
  tBTM_BL_EVENT event; /* The event reported. */
  const RawAddress* p_bda; /* The address of the peer connected device */
  uint8_t new_role;
  uint8_t hci_status; /* HCI status returned with the event */
} tBTM_BL_ROLE_CHG_DATA;
 
typedef union {
  tBTM_BL_EVENT event;        /* The event reported. */
  tBTM_BL_CONN_DATA conn;     /* The data associated with BTM_BL_CONN_EVT */
  tBTM_BL_DISCN_DATA discn;   /* The data associated with BTM_BL_DISCN_EVT */
  tBTM_BL_UPDATE_DATA update; /* The data associated with BTM_BL_UPDATE_EVT */
  tBTM_BL_ROLE_CHG_DATA
      role_chg; /*The data associated with BTM_BL_ROLE_CHG_EVT */
} tBTM_BL_EVENT_DATA;
 
/* Callback function for notifications when the BTM busy level
 * changes.
*/
typedef void(tBTM_BL_CHANGE_CB)(tBTM_BL_EVENT_DATA* p_data);
 
/***************************
 *  ACL Callback Functions
 ***************************/
/* Callback function for notifications when the BTM ACL connection DB
 * changes. First param is BD address, second is if added or removed.
 * Registered through BTM_AclRegisterForChanges call.
*/
typedef void(tBTM_ACL_DB_CHANGE_CB)(const RawAddress& p_bda, DEV_CLASS p_dc,
                                    BD_NAME p_bdn, uint8_t* features,
                                    bool is_new, uint16_t handle,
                                    tBT_TRANSPORT transport);
/*****************************************************************************
 *  SCO CHANNEL MANAGEMENT
 ****************************************************************************/
/******************
 *  SCO Constants
 ******************/
 
/* Define an invalid SCO index and an invalid HCI handle */
#define BTM_INVALID_SCO_INDEX 0xFFFF
#define BTM_INVALID_HCI_HANDLE 0xFFFF
 
/* Define an invalid SCO disconnect reason */
#define BTM_INVALID_SCO_DISC_REASON 0xFFFF
 
/* Define first active SCO index */
#define BTM_FIRST_ACTIVE_SCO_INDEX BTM_MAX_SCO_LINKS
 
#define BTM_SCO_LINK_ONLY_MASK \
  (ESCO_PKT_TYPES_MASK_HV1 | ESCO_PKT_TYPES_MASK_HV2 | ESCO_PKT_TYPES_MASK_HV3)
 
#define BTM_ESCO_LINK_ONLY_MASK \
  (ESCO_PKT_TYPES_MASK_EV3 | ESCO_PKT_TYPES_MASK_EV4 | ESCO_PKT_TYPES_MASK_EV5)
 
#define BTM_SCO_LINK_ALL_PKT_MASK \
  (BTM_SCO_LINK_ONLY_MASK | BTM_ESCO_LINK_ONLY_MASK)
 
#define BTM_VALID_SCO_ALL_PKT_TYPE HCI_VALID_SCO_ALL_PKT_TYPE
 
/***************
 *  SCO Types
 ***************/
#define BTM_LINK_TYPE_SCO HCI_LINK_TYPE_SCO
#define BTM_LINK_TYPE_ESCO HCI_LINK_TYPE_ESCO
typedef uint8_t tBTM_SCO_TYPE;
 
/*******************
 * SCO Codec Types
 *******************/
// TODO(google) This should use common definitions
#define BTM_SCO_CODEC_NONE 0x0000
#define BTM_SCO_CODEC_CVSD 0x0001
#define BTM_SCO_CODEC_MSBC 0x0002
typedef uint16_t tBTM_SCO_CODEC_TYPE;
 
/*******************
 * SCO Voice Settings
 *******************/
#define BTM_VOICE_SETTING_CVSD                                         \
  ((uint16_t)(HCI_INP_CODING_LINEAR | HCI_INP_DATA_FMT_2S_COMPLEMENT | \
              HCI_INP_SAMPLE_SIZE_16BIT | HCI_AIR_CODING_FORMAT_CVSD))
 
#define BTM_VOICE_SETTING_TRANS                                        \
  ((uint16_t)(HCI_INP_CODING_LINEAR | HCI_INP_DATA_FMT_2S_COMPLEMENT | \
              HCI_INP_SAMPLE_SIZE_16BIT | HCI_AIR_CODING_FORMAT_TRANSPNT))
 
/*******************
 * SCO Data Status
 *******************/
enum {
  BTM_SCO_DATA_CORRECT,
  BTM_SCO_DATA_PAR_ERR,
  BTM_SCO_DATA_NONE,
  BTM_SCO_DATA_PAR_LOST
};
typedef uint8_t tBTM_SCO_DATA_FLAG;
 
/***************************
 *  SCO Callback Functions
 ***************************/
typedef void(tBTM_SCO_CB)(uint16_t sco_inx);
typedef void(tBTM_SCO_DATA_CB)(uint16_t sco_inx, BT_HDR* p_data,
                               tBTM_SCO_DATA_FLAG status);
 
/***************
 *  eSCO Types
 ***************/
/* tBTM_ESCO_CBACK event types */
#define BTM_ESCO_CHG_EVT 1
#define BTM_ESCO_CONN_REQ_EVT 2
typedef uint8_t tBTM_ESCO_EVT;
 
/* Structure passed with SCO change command and events.
 * Used by both Sync and Enhanced sync messaging
 */
typedef struct {
  uint16_t max_latency_ms;
  uint16_t packet_types;
  uint8_t retransmission_effort;
} tBTM_CHG_ESCO_PARAMS;
 
/* Returned by BTM_ReadEScoLinkParms() */
typedef struct {
  uint16_t rx_pkt_len;
  uint16_t tx_pkt_len;
  RawAddress bd_addr;
  uint8_t link_type; /* BTM_LINK_TYPE_SCO or BTM_LINK_TYPE_ESCO */
  uint8_t tx_interval;
  uint8_t retrans_window;
  uint8_t air_mode;
} tBTM_ESCO_DATA;
 
typedef struct {
  uint16_t sco_inx;
  uint16_t rx_pkt_len;
  uint16_t tx_pkt_len;
  RawAddress bd_addr;
  uint8_t hci_status;
  uint8_t tx_interval;
  uint8_t retrans_window;
} tBTM_CHG_ESCO_EVT_DATA;
 
typedef struct {
  uint16_t sco_inx;
  RawAddress bd_addr;
  DEV_CLASS dev_class;
  tBTM_SCO_TYPE link_type;
} tBTM_ESCO_CONN_REQ_EVT_DATA;
 
typedef union {
  tBTM_CHG_ESCO_EVT_DATA chg_evt;
  tBTM_ESCO_CONN_REQ_EVT_DATA conn_evt;
} tBTM_ESCO_EVT_DATA;
 
/***************************
 *  eSCO Callback Functions
 ***************************/
typedef void(tBTM_ESCO_CBACK)(tBTM_ESCO_EVT event, tBTM_ESCO_EVT_DATA* p_data);
 
/*****************************************************************************
 *  SECURITY MANAGEMENT
 ****************************************************************************/
/*******************************
 *  Security Manager Constants
 *******************************/
 
/* Security Mode (BTM_SetSecurityMode) */
#define BTM_SEC_MODE_UNDEFINED 0
#define BTM_SEC_MODE_NONE 1
#define BTM_SEC_MODE_SERVICE 2
#define BTM_SEC_MODE_LINK 3
#define BTM_SEC_MODE_SP 4
#define BTM_SEC_MODE_SP_DEBUG 5
#define BTM_SEC_MODE_SC 6
 
/* Maximum Number of BTM Security Modes */
#define BTM_SEC_MODES_MAX 7
 
/* Security Service Levels [bit mask] (BTM_SetSecurityLevel)
 * Encryption should not be used without authentication
*/
/* Nothing required */
#define BTM_SEC_NONE 0x0000
/* Inbound call requires authorization */
#define BTM_SEC_IN_AUTHORIZE 0x0001
/* Inbound call requires authentication */
#define BTM_SEC_IN_AUTHENTICATE 0x0002
/* Inbound call requires encryption */
#define BTM_SEC_IN_ENCRYPT 0x0004
/* Outbound call requires authorization */
#define BTM_SEC_OUT_AUTHORIZE 0x0008
/* Outbound call requires authentication */
#define BTM_SEC_OUT_AUTHENTICATE 0x0010
/* Outbound call requires encryption */
#define BTM_SEC_OUT_ENCRYPT 0x0020
/* Secure Connections Only Mode */
#define BTM_SEC_MODE4_LEVEL4 0x0040
/* Need to switch connection to be master */
#define BTM_SEC_FORCE_MASTER 0x0100
/* Try to switch connection to be master */
#define BTM_SEC_ATTEMPT_MASTER 0x0200
/* Need to switch connection to be master */
#define BTM_SEC_FORCE_SLAVE 0x0400
/* Try to switch connection to be slave */
#define BTM_SEC_ATTEMPT_SLAVE 0x0800
/* inbound Do man in the middle protection */
#define BTM_SEC_IN_MITM 0x1000
/* outbound Do man in the middle protection */
#define BTM_SEC_OUT_MITM 0x2000
/* enforce a minimum of 16 digit for sec mode 2 */
#define BTM_SEC_IN_MIN_16_DIGIT_PIN 0x4000
 
/* Security Flags [bit mask] (BTM_GetSecurityFlags)
*/
#define BTM_SEC_FLAG_AUTHORIZED 0x01
#define BTM_SEC_FLAG_AUTHENTICATED 0x02
#define BTM_SEC_FLAG_ENCRYPTED 0x04
#define BTM_SEC_FLAG_LKEY_KNOWN 0x10
#define BTM_SEC_FLAG_LKEY_AUTHED 0x20
 
/* PIN types */
#define BTM_PIN_TYPE_VARIABLE HCI_PIN_TYPE_VARIABLE
#define BTM_PIN_TYPE_FIXED HCI_PIN_TYPE_FIXED
 
/* Link Key types used to generate the new link key.
 * returned in link key notification callback function
*/
#define BTM_LKEY_TYPE_COMBINATION HCI_LKEY_TYPE_COMBINATION
#define BTM_LKEY_TYPE_LOCAL_UNIT HCI_LKEY_TYPE_LOCAL_UNIT
#define BTM_LKEY_TYPE_REMOTE_UNIT HCI_LKEY_TYPE_REMOTE_UNIT
#define BTM_LKEY_TYPE_DEBUG_COMB HCI_LKEY_TYPE_DEBUG_COMB
#define BTM_LKEY_TYPE_UNAUTH_COMB HCI_LKEY_TYPE_UNAUTH_COMB
#define BTM_LKEY_TYPE_AUTH_COMB HCI_LKEY_TYPE_AUTH_COMB
#define BTM_LKEY_TYPE_CHANGED_COMB HCI_LKEY_TYPE_CHANGED_COMB
 
#define BTM_LKEY_TYPE_UNAUTH_COMB_P_256 HCI_LKEY_TYPE_UNAUTH_COMB_P_256
#define BTM_LKEY_TYPE_AUTH_COMB_P_256 HCI_LKEY_TYPE_AUTH_COMB_P_256
 
/* "easy" requirements for LK derived from LTK */
#define BTM_LTK_DERIVED_LKEY_OFFSET 0x20
#define BTM_LKEY_TYPE_IGNORE               \
  0xff /* used when event is response from \
          hci return link keys request */
 
typedef uint8_t tBTM_LINK_KEY_TYPE;
 
/* Protocol level security (BTM_SetSecurityLevel) */
#define BTM_SEC_PROTO_L2CAP 0
#define BTM_SEC_PROTO_SDP 1
#define BTM_SEC_PROTO_TCS 2
#define BTM_SEC_PROTO_RFCOMM 3
#define BTM_SEC_PROTO_OBEX 4
#define BTM_SEC_PROTO_BNEP 5
#define BTM_SEC_PROTO_HID 6 /* HID      */
#define BTM_SEC_PROTO_AVDT 7
#define BTM_SEC_PROTO_MCA 8
 
/* Determine the number of uint32_t's necessary for security services */
#define BTM_SEC_ARRAY_BITS 32 /* Number of bits in each array element */
#define BTM_SEC_SERVICE_ARRAY_SIZE                         \
  (((uint32_t)BTM_SEC_MAX_SERVICES / BTM_SEC_ARRAY_BITS) + \
   (((uint32_t)BTM_SEC_MAX_SERVICES % BTM_SEC_ARRAY_BITS) ? 1 : 0))
 
/* Security service definitions (BTM_SetSecurityLevel)
 * Used for Authorization APIs
*/
#define BTM_SEC_SERVICE_SDP_SERVER 0
#define BTM_SEC_SERVICE_SERIAL_PORT 1
#define BTM_SEC_SERVICE_LAN_ACCESS 2
#define BTM_SEC_SERVICE_DUN 3
#define BTM_SEC_SERVICE_IRMC_SYNC 4
#define BTM_SEC_SERVICE_IRMC_SYNC_CMD 5
#define BTM_SEC_SERVICE_OBEX 6
#define BTM_SEC_SERVICE_OBEX_FTP 7
#define BTM_SEC_SERVICE_HEADSET 8
#define BTM_SEC_SERVICE_CORDLESS 9
#define BTM_SEC_SERVICE_INTERCOM 10
#define BTM_SEC_SERVICE_FAX 11
#define BTM_SEC_SERVICE_HEADSET_AG 12
#define BTM_SEC_SERVICE_PNP_INFO 13
#define BTM_SEC_SERVICE_GEN_NET 14
#define BTM_SEC_SERVICE_GEN_FILE 15
#define BTM_SEC_SERVICE_GEN_AUDIO 16
#define BTM_SEC_SERVICE_GEN_TEL 17
#define BTM_SEC_SERVICE_CTP_DATA 18
#define BTM_SEC_SERVICE_HCRP_CTRL 19
#define BTM_SEC_SERVICE_HCRP_DATA 20
#define BTM_SEC_SERVICE_HCRP_NOTIF 21
#define BTM_SEC_SERVICE_BPP_JOB 22
#define BTM_SEC_SERVICE_BPP_STATUS 23
#define BTM_SEC_SERVICE_BPP_REF 24
#define BTM_SEC_SERVICE_BNEP_PANU 25
#define BTM_SEC_SERVICE_BNEP_GN 26
#define BTM_SEC_SERVICE_BNEP_NAP 27
#define BTM_SEC_SERVICE_HF_HANDSFREE 28
#define BTM_SEC_SERVICE_AG_HANDSFREE 29
#define BTM_SEC_SERVICE_TE_PHONE_ACCESS 30
#define BTM_SEC_SERVICE_ME_PHONE_ACCESS 31
 
#define BTM_SEC_SERVICE_HIDH_SEC_CTRL 32
#define BTM_SEC_SERVICE_HIDH_NOSEC_CTRL 33
#define BTM_SEC_SERVICE_HIDH_INTR 34
#define BTM_SEC_SERVICE_BIP 35
#define BTM_SEC_SERVICE_BIP_REF 36
#define BTM_SEC_SERVICE_AVDTP 37
#define BTM_SEC_SERVICE_AVDTP_NOSEC 38
#define BTM_SEC_SERVICE_AVCTP 39
#define BTM_SEC_SERVICE_SAP 40
#define BTM_SEC_SERVICE_PBAP 41
#define BTM_SEC_SERVICE_RFC_MUX 42
#define BTM_SEC_SERVICE_AVCTP_BROWSE 43
#define BTM_SEC_SERVICE_MAP 44
#define BTM_SEC_SERVICE_MAP_NOTIF 45
#define BTM_SEC_SERVICE_MCAP_CTRL 46
#define BTM_SEC_SERVICE_MCAP_DATA 47
#define BTM_SEC_SERVICE_HDP_SNK 48
#define BTM_SEC_SERVICE_HDP_SRC 49
#define BTM_SEC_SERVICE_ATT 50
#define BTM_SEC_SERVICE_HIDD_SEC_CTRL 51
#define BTM_SEC_SERVICE_HIDD_NOSEC_CTRL 52
#define BTM_SEC_SERVICE_HIDD_INTR 53
#define BTM_SEC_SERVICE_HEARING_AID_LEFT 54
#define BTM_SEC_SERVICE_HEARING_AID_RIGHT 55
 
/* Update these as services are added */
#define BTM_SEC_SERVICE_FIRST_EMPTY 56
 
#ifndef BTM_SEC_MAX_SERVICES
#define BTM_SEC_MAX_SERVICES 75
#endif
 
/*******************************************************************************
 * Security Services MACROS handle array of uint32_t bits for more than 32
 * trusted services
 ******************************************************************************/
/* MACRO to set the security service bit mask in a bit stream */
#define BTM_SEC_SET_SERVICE(p, service)                              \
  (((uint32_t*)(p))[(((uint32_t)(service)) / BTM_SEC_ARRAY_BITS)] |= \
   ((uint32_t)1 << (((uint32_t)(service)) % BTM_SEC_ARRAY_BITS)))
 
/* MACRO to clear the security service bit mask in a bit stream */
#define BTM_SEC_CLR_SERVICE(p, service)                              \
  (((uint32_t*)(p))[(((uint32_t)(service)) / BTM_SEC_ARRAY_BITS)] &= \
   ~((uint32_t)1 << (((uint32_t)(service)) % BTM_SEC_ARRAY_BITS)))
 
/* MACRO to check the security service bit mask in a bit stream (Returns true or
 * false) */
#define BTM_SEC_IS_SERVICE_TRUSTED(p, service)                                 \
  (((((uint32_t*)(p))[(((uint32_t)(service)) / BTM_SEC_ARRAY_BITS)]) &         \
    (uint32_t)(((uint32_t)1 << (((uint32_t)(service)) % BTM_SEC_ARRAY_BITS)))) \
       ? true                                                                  \
       : false)
 
/* MACRO to copy two trusted device bitmask */
#define BTM_SEC_COPY_TRUSTED_DEVICE(p_src, p_dst)              \
  {                                                            \
    uint32_t trst;                                             \
    for (trst = 0; trst < BTM_SEC_SERVICE_ARRAY_SIZE; trst++)  \
      ((uint32_t*)(p_dst))[trst] = ((uint32_t*)(p_src))[trst]; \
  }
 
/* MACRO to clear two trusted device bitmask */
#define BTM_SEC_CLR_TRUSTED_DEVICE(p_dst)                     \
  {                                                           \
    uint32_t trst;                                            \
    for (trst = 0; trst < BTM_SEC_SERVICE_ARRAY_SIZE; trst++) \
      ((uint32_t*)(p_dst))[trst] = 0;                         \
  }
 
/* Following bits can be provided by host in the trusted_mask array */
/* 0..31 bits of mask[0] (Least Significant Word) */
#define BTM_SEC_TRUST_SDP_SERVER (1 << BTM_SEC_SERVICE_SDP_SERVER)
#define BTM_SEC_TRUST_SERIAL_PORT (1 << BTM_SEC_SERVICE_SERIAL_PORT)
#define BTM_SEC_TRUST_LAN_ACCESS (1 << BTM_SEC_SERVICE_LAN_ACCESS)
#define BTM_SEC_TRUST_DUN (1 << BTM_SEC_SERVICE_DUN)
#define BTM_SEC_TRUST_IRMC_SYNC (1 << BTM_SEC_SERVICE_IRMC_SYNC)
#define BTM_SEC_TRUST_IRMC_SYNC_CMD (1 << BTM_SEC_SERVICE_IRMC_SYNC_CMD)
#define BTM_SEC_TRUST_OBEX (1 << BTM_SEC_SERVICE_OBEX)
#define BTM_SEC_TRUST_OBEX_FTP (1 << BTM_SEC_SERVICE_OBEX_FTP)
#define BTM_SEC_TRUST_HEADSET (1 << BTM_SEC_SERVICE_HEADSET)
#define BTM_SEC_TRUST_CORDLESS (1 << BTM_SEC_SERVICE_CORDLESS)
#define BTM_SEC_TRUST_INTERCOM (1 << BTM_SEC_SERVICE_INTERCOM)
#define BTM_SEC_TRUST_FAX (1 << BTM_SEC_SERVICE_FAX)
#define BTM_SEC_TRUST_HEADSET_AG (1 << BTM_SEC_SERVICE_HEADSET_AG)
#define BTM_SEC_TRUST_PNP_INFO (1 << BTM_SEC_SERVICE_PNP_INFO)
#define BTM_SEC_TRUST_GEN_NET (1 << BTM_SEC_SERVICE_GEN_NET)
#define BTM_SEC_TRUST_GEN_FILE (1 << BTM_SEC_SERVICE_GEN_FILE)
#define BTM_SEC_TRUST_GEN_AUDIO (1 << BTM_SEC_SERVICE_GEN_AUDIO)
#define BTM_SEC_TRUST_GEN_TEL (1 << BTM_SEC_SERVICE_GEN_TEL)
#define BTM_SEC_TRUST_CTP_DATA (1 << BTM_SEC_SERVICE_CTP_DATA)
#define BTM_SEC_TRUST_HCRP_CTRL (1 << BTM_SEC_SERVICE_HCRP_CTRL)
#define BTM_SEC_TRUST_HCRP_DATA (1 << BTM_SEC_SERVICE_HCRP_DATA)
#define BTM_SEC_TRUST_HCRP_NOTIF (1 << BTM_SEC_SERVICE_HCRP_NOTIF)
#define BTM_SEC_TRUST_BPP_JOB (1 << BTM_SEC_SERVICE_JOB)
#define BTM_SEC_TRUST_BPP_STATUS (1 << BTM_SEC_SERVICE_STATUS)
#define BTM_SEC_TRUST_BPP_REF (1 << BTM_SEC_SERVICE_REF)
#define BTM_SEC_TRUST_BNEP_PANU (1 << BTM_SEC_SERVICE_BNEP_PANU)
#define BTM_SEC_TRUST_BNEP_GN (1 << BTM_SEC_SERVICE_BNEP_GN)
#define BTM_SEC_TRUST_BNEP_NAP (1 << BTM_SEC_SERVICE_BNEP_NAP)
#define BTM_SEC_TRUST_HFP_HF (1 << BTM_SEC_SERVICE_HF_HANDSFREE)
#define BTM_SEC_TRUST_HFP_AG (1 << BTM_SEC_SERVICE_AG_HANDSFREE)
#define BTM_SEC_TRUST_TE_PHONE_ACCESS (1 << BTM_SEC_SERVICE_TE_PHONE_ACCESS)
#define BTM_SEC_TRUST_ME_PHONE_ACCESS (1 << BTM_SEC_SERVICE_ME_PHONE_ACCESS)
 
/* 0..31 bits of mask[1] (Most Significant Word) */
#define BTM_SEC_TRUST_HIDH_CTRL (1 << (BTM_SEC_SERVICE_HIDH_SEC_CTRL - 32))
#define BTM_SEC_TRUST_HIDH_NOSEC_CTRL \
  (1 << (BTM_SEC_SERVICE_HIDH_NOSEC_CTRL - 32))
#define BTM_SEC_TRUST_HIDH_INTR (1 << (BTM_SEC_SERVICE_HIDH_INTR - 32))
#define BTM_SEC_TRUST_BIP (1 << (BTM_SEC_SERVICE_BIP - 32))
#define BTM_SEC_TRUST_BIP_REF (1 << (BTM_SEC_SERVICE_BIP_REF - 32))
#define BTM_SEC_TRUST_AVDTP (1 << (BTM_SEC_SERVICE_AVDTP - 32))
#define BTM_SEC_TRUST_AVDTP_NOSEC (1 << (BTM_SEC_SERVICE_AVDTP_NOSEC - 32))
#define BTM_SEC_TRUST_AVCTP (1 << (BTM_SEC_SERVICE_AVCTP - 32))
#define BTM_SEC_TRUST_SAP (1 << (BTM_SEC_SERVICE_SAP - 32))
#define BTM_SEC_TRUST_PBAP (1 << (BTM_SEC_SERVICE_PBAP - 32))
#define BTM_SEC_TRUST_RFC_MUX (1 << (BTM_SEC_SERVICE_RFC_MUX - 32))
#define BTM_SEC_TRUST_AVCTP_BROWSE (1 << (BTM_SEC_SERVICE_AVCTP_BROWSE - 32))
#define BTM_SEC_TRUST_MAP (1 << (BTM_SEC_SERVICE_MAP - 32))
#define BTM_SEC_TRUST_MAP_NOTIF (1 << (BTM_SEC_SERVICE_MAP_NOTIF - 32))
#define BTM_SEC_TRUST_MCAP_CTRL (1 << (BTM_SEC_SERVICE_MCAP_CTRL - 32))
#define BTM_SEC_TRUST_MCAP_DATA (1 << (BTM_SEC_SERVICE_MCAP_DATA - 32))
#define BTM_SEC_TRUST_HDP_SNK (1 << (BTM_SEC_SERVICE_HDP_SNK - 32))
#define BTM_SEC_TRUST_HDP_SRC (1 << (BTM_SEC_SERVICE_HDP_SRC - 32))
 
#define BTM_SEC_TRUST_ALL 0xFFFFFFFF /* for each array element */
 
/****************************************
 *  Security Manager Callback Functions
 ****************************************/
/* Authorize device for service.  Parameters are
 *              BD Address of remote
 *              Device Class of remote
 *              BD Name of remote
 *              Service name
 *              Service Id (NULL - unknown service or unused
 *                                 [BTM_SEC_SERVICE_NAME_LEN set to 0])
 *              Is originator of the connection
 *              Result of the operation
*/
typedef uint8_t(tBTM_AUTHORIZE_CALLBACK)(
    const RawAddress& bd_addr, DEV_CLASS dev_class, tBTM_BD_NAME bd_name,
    uint8_t* service_name, uint8_t service_id, bool is_originator);
 
/* Get PIN for the connection.  Parameters are
 *              BD Address of remote
 *              Device Class of remote
 *              BD Name of remote
 *              Flag indicating the minimum pin code length to be 16 digits
*/
typedef uint8_t(tBTM_PIN_CALLBACK)(const RawAddress& bd_addr,
                                   DEV_CLASS dev_class, tBTM_BD_NAME bd_name,
                                   bool min_16_digit);
 
/* New Link Key for the connection.  Parameters are
 *              BD Address of remote
 *              Link Key
 *              Key Type: Combination, Local Unit, or Remote Unit
*/
typedef uint8_t(tBTM_LINK_KEY_CALLBACK)(const RawAddress& bd_addr,
                                        DEV_CLASS dev_class,
                                        tBTM_BD_NAME bd_name,
                                        const LinkKey& key, uint8_t key_type);
 
/* Remote Name Resolved.  Parameters are
 *              BD Address of remote
 *              BD Name of remote
*/
typedef void(tBTM_RMT_NAME_CALLBACK)(const RawAddress& bd_addr, DEV_CLASS dc,
                                     tBTM_BD_NAME bd_name);
 
/* Authentication complete for the connection.  Parameters are
 *              BD Address of remote
 *              Device Class of remote
 *              BD Name of remote
 *
*/
typedef uint8_t(tBTM_AUTH_COMPLETE_CALLBACK)(const RawAddress& bd_addr,
                                             DEV_CLASS dev_class,
                                             tBTM_BD_NAME bd_name, int result);
 
enum {
  BTM_SP_IO_REQ_EVT,    /* received IO_CAPABILITY_REQUEST event */
  BTM_SP_IO_RSP_EVT,    /* received IO_CAPABILITY_RESPONSE event */
  BTM_SP_CFM_REQ_EVT,   /* received USER_CONFIRMATION_REQUEST event */
  BTM_SP_KEY_NOTIF_EVT, /* received USER_PASSKEY_NOTIFY event */
  BTM_SP_KEY_REQ_EVT,   /* received USER_PASSKEY_REQUEST event */
  BTM_SP_KEYPRESS_EVT,  /* received KEYPRESS_NOTIFY event */
  BTM_SP_LOC_OOB_EVT,   /* received result for READ_LOCAL_OOB_DATA command */
  BTM_SP_RMT_OOB_EVT,   /* received REMOTE_OOB_DATA_REQUEST event */
  BTM_SP_COMPLT_EVT,    /* received SIMPLE_PAIRING_COMPLETE event */
  BTM_SP_UPGRADE_EVT /* check if the application wants to upgrade the link key
                        */
};
typedef uint8_t tBTM_SP_EVT;
 
#define BTM_IO_CAP_OUT 0    /* DisplayOnly */
#define BTM_IO_CAP_IO 1     /* DisplayYesNo */
#define BTM_IO_CAP_IN 2     /* KeyboardOnly */
#define BTM_IO_CAP_NONE 3   /* NoInputNoOutput */
#define BTM_IO_CAP_KBDISP 4 /* Keyboard display */
#define BTM_IO_CAP_MAX 5
#define BTM_IO_CAP_UNKNOWN 0xFF /* Unknown value */
 
typedef uint8_t tBTM_IO_CAP;
 
#define BTM_MAX_PASSKEY_VAL (999999)
#define BTM_MIN_PASSKEY_VAL (0)
 
/* MITM Protection Not Required - Single Profile/non-bonding Numeric comparison
 * with automatic accept allowed */
#define BTM_AUTH_SP_NO 0
/* MITM Protection Required - Single Profile/non-bonding. Use IO Capabilities to
 * determine authentication procedure */
#define BTM_AUTH_SP_YES 1
/* MITM Protection Not Required - All Profiles/dedicated bonding Numeric
 * comparison with automatic accept allowed */
#define BTM_AUTH_AP_NO 2
/* MITM Protection Required - All Profiles/dedicated bonding Use IO Capabilities
 * to determine authentication procedure */
#define BTM_AUTH_AP_YES 3
/* MITM Protection Not Required - Single Profiles/general bonding Numeric
 * comparison with automatic accept allowed */
#define BTM_AUTH_SPGB_NO 4
/* MITM Protection Required - Single Profiles/general bonding Use IO
 * Capabilities to determine authentication procedure */
#define BTM_AUTH_SPGB_YES 5
 
/* this bit is ORed with BTM_AUTH_SP_* when IO exchange for dedicated bonding */
#define BTM_AUTH_DD_BOND 2
#define BTM_AUTH_GB_BIT 4 /* the genernal bonding bit */
#define BTM_AUTH_BONDS 6  /* the general/dedicated bonding bits  */
#define BTM_AUTH_YN_BIT 1 /* this is the Yes or No bit  */
 
#define BTM_BLE_INITIATOR_KEY_SIZE 15
#define BTM_BLE_RESPONDER_KEY_SIZE 15
#define BTM_BLE_MAX_KEY_SIZE 16
 
typedef uint8_t tBTM_AUTH_REQ;
 
enum { BTM_OOB_NONE, BTM_OOB_PRESENT, BTM_OOB_UNKNOWN };
typedef uint8_t tBTM_OOB_DATA;
 
/* data type for BTM_SP_IO_REQ_EVT */
typedef struct {
  RawAddress bd_addr;     /* peer address */
  tBTM_IO_CAP io_cap;     /* local IO capabilities */
  tBTM_OOB_DATA oob_data; /* OOB data present (locally) for the peer device */
  tBTM_AUTH_REQ auth_req; /* Authentication required (for local device) */
  bool is_orig;           /* true, if local device initiated the SP process */
} tBTM_SP_IO_REQ;
 
/* data type for BTM_SP_IO_RSP_EVT */
typedef struct {
  RawAddress bd_addr; /* peer address */
  tBTM_IO_CAP io_cap; /* peer IO capabilities */
  tBTM_OOB_DATA
      oob_data; /* OOB data present at peer device for the local device */
  tBTM_AUTH_REQ auth_req; /* Authentication required for peer device */
} tBTM_SP_IO_RSP;
 
/* data type for BTM_SP_CFM_REQ_EVT */
typedef struct {
  RawAddress bd_addr;   /* peer address */
  DEV_CLASS dev_class;  /* peer CoD */
  tBTM_BD_NAME bd_name; /* peer device name */
  uint32_t num_val; /* the numeric value for comparison. If just_works, do not
                       show this number to UI */
  bool just_works;  /* true, if "Just Works" association model */
  tBTM_AUTH_REQ loc_auth_req; /* Authentication required for local device */
  tBTM_AUTH_REQ rmt_auth_req; /* Authentication required for peer device */
  tBTM_IO_CAP loc_io_caps;    /* IO Capabilities of the local device */
  tBTM_IO_CAP rmt_io_caps;    /* IO Capabilities of the remot device */
} tBTM_SP_CFM_REQ;
 
/* data type for BTM_SP_KEY_REQ_EVT */
typedef struct {
  RawAddress bd_addr;   /* peer address */
  DEV_CLASS dev_class;  /* peer CoD */
  tBTM_BD_NAME bd_name; /* peer device name */
} tBTM_SP_KEY_REQ;
 
/* data type for BTM_SP_KEY_NOTIF_EVT */
typedef struct {
  RawAddress bd_addr;   /* peer address */
  DEV_CLASS dev_class;  /* peer CoD */
  tBTM_BD_NAME bd_name; /* peer device name */
  uint32_t passkey;     /* passkey */
} tBTM_SP_KEY_NOTIF;
 
enum {
  BTM_SP_KEY_STARTED,     /* 0 - passkey entry started */
  BTM_SP_KEY_ENTERED,     /* 1 - passkey digit entered */
  BTM_SP_KEY_ERASED,      /* 2 - passkey digit erased */
  BTM_SP_KEY_CLEARED,     /* 3 - passkey cleared */
  BTM_SP_KEY_COMPLT,      /* 4 - passkey entry completed */
  BTM_SP_KEY_OUT_OF_RANGE /* 5 - out of range */
};
typedef uint8_t tBTM_SP_KEY_TYPE;
 
/* data type for BTM_SP_KEYPRESS_EVT */
typedef struct {
  RawAddress bd_addr; /* peer address */
  tBTM_SP_KEY_TYPE notif_type;
} tBTM_SP_KEYPRESS;
 
/* data type for BTM_SP_LOC_OOB_EVT */
typedef struct {
  tBTM_STATUS status; /* */
  Octet16 c;          /* Simple Pairing Hash C */
  Octet16 r;          /* Simple Pairing Randomnizer R */
} tBTM_SP_LOC_OOB;
 
/* data type for BTM_SP_RMT_OOB_EVT */
typedef struct {
  RawAddress bd_addr;   /* peer address */
  DEV_CLASS dev_class;  /* peer CoD */
  tBTM_BD_NAME bd_name; /* peer device name */
} tBTM_SP_RMT_OOB;
 
/* data type for BTM_SP_COMPLT_EVT */
typedef struct {
  RawAddress bd_addr;   /* peer address */
  DEV_CLASS dev_class;  /* peer CoD */
  tBTM_BD_NAME bd_name; /* peer device name */
  tBTM_STATUS status;   /* status of the simple pairing process */
} tBTM_SP_COMPLT;
 
/* data type for BTM_SP_UPGRADE_EVT */
typedef struct {
  RawAddress bd_addr; /* peer address */
  bool upgrade;    /* true, to upgrade the link key */
} tBTM_SP_UPGRADE;
 
typedef union {
  tBTM_SP_IO_REQ io_req;       /* BTM_SP_IO_REQ_EVT      */
  tBTM_SP_IO_RSP io_rsp;       /* BTM_SP_IO_RSP_EVT      */
  tBTM_SP_CFM_REQ cfm_req;     /* BTM_SP_CFM_REQ_EVT     */
  tBTM_SP_KEY_NOTIF key_notif; /* BTM_SP_KEY_NOTIF_EVT   */
  tBTM_SP_KEY_REQ key_req;     /* BTM_SP_KEY_REQ_EVT     */
  tBTM_SP_KEYPRESS key_press;  /* BTM_SP_KEYPRESS_EVT    */
  tBTM_SP_LOC_OOB loc_oob;     /* BTM_SP_LOC_OOB_EVT     */
  tBTM_SP_RMT_OOB rmt_oob;     /* BTM_SP_RMT_OOB_EVT     */
  tBTM_SP_COMPLT complt;       /* BTM_SP_COMPLT_EVT      */
  tBTM_SP_UPGRADE upgrade;     /* BTM_SP_UPGRADE_EVT      */
} tBTM_SP_EVT_DATA;
 
/* Simple Pairing Events.  Called by the stack when Simple Pairing related
 * events occur.
*/
typedef uint8_t(tBTM_SP_CALLBACK)(tBTM_SP_EVT event, tBTM_SP_EVT_DATA* p_data);
 
typedef void(tBTM_MKEY_CALLBACK)(const RawAddress& bd_addr, uint8_t status,
                                 uint8_t key_flag);
 
/* Encryption enabled/disabled complete: Optionally passed with
 * BTM_SetEncryption.
 * Parameters are
 *              BD Address of remote
 *              optional data passed in by BTM_SetEncryption
 *              tBTM_STATUS - result of the operation
*/
typedef void(tBTM_SEC_CBACK)(const RawAddress* bd_addr, tBT_TRANSPORT trasnport,
                             void* p_ref_data, tBTM_STATUS result);
 
/* Bond Cancel complete. Parameters are
 *              Result of the cancel operation
 *
*/
typedef void(tBTM_BOND_CANCEL_CMPL_CALLBACK)(tBTM_STATUS result);
 
/* LE related event and data structure */
/* received IO_CAPABILITY_REQUEST event */
#define BTM_LE_IO_REQ_EVT SMP_IO_CAP_REQ_EVT
/* security request event */
#define BTM_LE_SEC_REQUEST_EVT SMP_SEC_REQUEST_EVT
/* received USER_PASSKEY_NOTIFY event */
#define BTM_LE_KEY_NOTIF_EVT SMP_PASSKEY_NOTIF_EVT
/* received USER_PASSKEY_REQUEST event */
#define BTM_LE_KEY_REQ_EVT SMP_PASSKEY_REQ_EVT
/* OOB data request event */
#define BTM_LE_OOB_REQ_EVT SMP_OOB_REQ_EVT
/* Numeric Comparison request event */
#define BTM_LE_NC_REQ_EVT SMP_NC_REQ_EVT
/* Peer keypress notification recd event */
#define BTM_LE_PR_KEYPR_NOT_EVT SMP_PEER_KEYPR_NOT_EVT
/* SC OOB request event (both local and peer OOB data) can be expected in
 * response */
#define BTM_LE_SC_OOB_REQ_EVT SMP_SC_OOB_REQ_EVT
/* SC OOB local data set is created (as result of SMP_CrLocScOobData(...)) */
#define BTM_LE_SC_LOC_OOB_EVT SMP_SC_LOC_OOB_DATA_UP_EVT
/* SMP over BR keys request event */
#define BTM_LE_BR_KEYS_REQ_EVT SMP_BR_KEYS_REQ_EVT
/* SMP complete event */
#define BTM_LE_COMPLT_EVT SMP_COMPLT_EVT
#define BTM_LE_LAST_FROM_SMP BTM_LE_BR_KEYS_REQ_EVT
/* KEY update event */
#define BTM_LE_KEY_EVT (BTM_LE_LAST_FROM_SMP + 1)
#define BTM_LE_CONSENT_REQ_EVT SMP_CONSENT_REQ_EVT
typedef uint8_t tBTM_LE_EVT;
 
#define BTM_LE_KEY_NONE 0
/* encryption information of peer device */
#define BTM_LE_KEY_PENC SMP_SEC_KEY_TYPE_ENC
/* identity key of the peer device */
#define BTM_LE_KEY_PID SMP_SEC_KEY_TYPE_ID
/* peer SRK */
#define BTM_LE_KEY_PCSRK SMP_SEC_KEY_TYPE_CSRK
#define BTM_LE_KEY_PLK SMP_SEC_KEY_TYPE_LK
#define BTM_LE_KEY_LLK (SMP_SEC_KEY_TYPE_LK << 4)
/* master role security information:div */
#define BTM_LE_KEY_LENC (SMP_SEC_KEY_TYPE_ENC << 4)
/* master device ID key */
#define BTM_LE_KEY_LID (SMP_SEC_KEY_TYPE_ID << 4)
/* local CSRK has been deliver to peer */
#define BTM_LE_KEY_LCSRK (SMP_SEC_KEY_TYPE_CSRK << 4)
typedef uint8_t tBTM_LE_KEY_TYPE;
 
#define BTM_LE_AUTH_REQ_NO_BOND SMP_AUTH_NO_BOND /* 0 */
#define BTM_LE_AUTH_REQ_BOND SMP_AUTH_BOND       /* 1 << 0 */
#define BTM_LE_AUTH_REQ_MITM SMP_AUTH_YN_BIT     /* 1 << 2 */
typedef uint8_t tBTM_LE_AUTH_REQ;
#define BTM_LE_SC_SUPPORT_BIT SMP_SC_SUPPORT_BIT /* (1 << 3) */
#define BTM_LE_KP_SUPPORT_BIT SMP_KP_SUPPORT_BIT /* (1 << 4) */
#define BTM_LE_H7_SUPPORT_BIT SMP_H7_SUPPORT_BIT /* (1 << 5) */
 
#define BTM_LE_AUTH_REQ_SC_ONLY SMP_AUTH_SC_ENC_ONLY     /* 00101000 */
#define BTM_LE_AUTH_REQ_SC_BOND SMP_AUTH_SC_GB           /* 00101001 */
#define BTM_LE_AUTH_REQ_SC_MITM SMP_AUTH_SC_MITM_NB      /* 00101100 */
#define BTM_LE_AUTH_REQ_SC_MITM_BOND SMP_AUTH_SC_MITM_GB /* 00101101 */
#define BTM_LE_AUTH_REQ_MASK SMP_AUTH_MASK               /* 0x3D */
 
/* LE security level */
#define BTM_LE_SEC_NONE SMP_SEC_NONE
#define BTM_LE_SEC_UNAUTHENTICATE SMP_SEC_UNAUTHENTICATE /* 1 */
#define BTM_LE_SEC_AUTHENTICATED SMP_SEC_AUTHENTICATED   /* 4 */
typedef uint8_t tBTM_LE_SEC;
 
typedef struct {
  /* local IO capabilities */
  tBTM_IO_CAP io_cap;
  /* OOB data present (locally) for the peer device */
  uint8_t oob_data;
  /* Authentication request (for local device) containing bonding and MITM
   * info */
  tBTM_LE_AUTH_REQ auth_req;
  uint8_t max_key_size;       /* max encryption key size */
  tBTM_LE_KEY_TYPE init_keys; /* keys to be distributed, bit mask */
  tBTM_LE_KEY_TYPE resp_keys; /* keys to be distributed, bit mask */
} tBTM_LE_IO_REQ;
 
/* data type for tBTM_LE_COMPLT */
typedef struct {
  uint8_t reason;
  uint8_t sec_level;
  bool is_pair_cancel;
  bool smp_over_br;
} tBTM_LE_COMPLT;
 
/* BLE encryption keys */
typedef struct {
  Octet16 ltk;
  BT_OCTET8 rand;
  uint16_t ediv;
  uint8_t sec_level;
  uint8_t key_size;
} tBTM_LE_PENC_KEYS;
 
/* BLE CSRK keys */
typedef struct {
  uint32_t counter;
  Octet16 csrk;
  uint8_t sec_level;
} tBTM_LE_PCSRK_KEYS;
 
/* BLE Encryption reproduction keys */
typedef struct {
  Octet16 ltk;
  uint16_t div;
  uint8_t key_size;
  uint8_t sec_level;
} tBTM_LE_LENC_KEYS;
 
/* BLE SRK keys */
typedef struct {
  uint32_t counter;
  uint16_t div;
  uint8_t sec_level;
  Octet16 csrk;
} tBTM_LE_LCSRK_KEYS;
 
typedef struct {
  Octet16 irk;
  tBLE_ADDR_TYPE identity_addr_type;
  RawAddress identity_addr;
} tBTM_LE_PID_KEYS;
 
typedef union {
  tBTM_LE_PENC_KEYS penc_key;   /* received peer encryption key */
  tBTM_LE_PCSRK_KEYS pcsrk_key; /* received peer device SRK */
  tBTM_LE_PID_KEYS pid_key;     /* peer device ID key */
  tBTM_LE_LENC_KEYS lenc_key;   /* local encryption reproduction keys
                                 * LTK = = d1(ER,DIV,0) */
  tBTM_LE_LCSRK_KEYS lcsrk_key; /* local device CSRK = d1(ER,DIV,1)*/
} tBTM_LE_KEY_VALUE;
 
typedef struct {
  tBTM_LE_KEY_TYPE key_type;
  tBTM_LE_KEY_VALUE* p_key_value;
} tBTM_LE_KEY;
 
typedef union {
  tBTM_LE_IO_REQ io_req; /* BTM_LE_IO_REQ_EVT      */
  uint32_t key_notif;    /* BTM_LE_KEY_NOTIF_EVT   */
                         /* BTM_LE_NC_REQ_EVT */
                         /* no callback data for
                          * BTM_LE_KEY_REQ_EVT
                          * and BTM_LE_OOB_REQ_EVT  */
  tBTM_LE_COMPLT complt; /* BTM_LE_COMPLT_EVT      */
  tSMP_OOB_DATA_TYPE req_oob_type;
  tBTM_LE_KEY key;
} tBTM_LE_EVT_DATA;
 
/* Simple Pairing Events.  Called by the stack when Simple Pairing related
 * events occur.
*/
typedef uint8_t(tBTM_LE_CALLBACK)(tBTM_LE_EVT event, const RawAddress& bda,
                                  tBTM_LE_EVT_DATA* p_data);
 
#define BTM_BLE_KEY_TYPE_ID 1
#define BTM_BLE_KEY_TYPE_ER 2
#define BTM_BLE_KEY_TYPE_COUNTER 3  // tobe obsolete
 
typedef struct {
  Octet16 ir;
  Octet16 irk;
  Octet16 dhk;
 
} tBTM_BLE_LOCAL_ID_KEYS;
 
typedef union {
  tBTM_BLE_LOCAL_ID_KEYS id_keys;
  Octet16 er;
} tBTM_BLE_LOCAL_KEYS;
 
/* New LE identity key for local device.
*/
typedef void(tBTM_LE_KEY_CALLBACK)(uint8_t key_type,
                                   tBTM_BLE_LOCAL_KEYS* p_key);
 
/***************************
 *  Security Manager Types
 ***************************/
/* Structure that applications use to register with BTM_SecRegister */
typedef struct {
  tBTM_AUTHORIZE_CALLBACK* p_authorize_callback;
  tBTM_PIN_CALLBACK* p_pin_callback;
  tBTM_LINK_KEY_CALLBACK* p_link_key_callback;
  tBTM_AUTH_COMPLETE_CALLBACK* p_auth_complete_callback;
  tBTM_BOND_CANCEL_CMPL_CALLBACK* p_bond_cancel_cmpl_callback;
  tBTM_SP_CALLBACK* p_sp_callback;
  tBTM_LE_CALLBACK* p_le_callback;
  tBTM_LE_KEY_CALLBACK* p_le_key_callback;
} tBTM_APPL_INFO;
 
/* Callback function for when a link supervision timeout event occurs.
 * This asynchronous event is enabled/disabled by calling BTM_RegForLstoEvt().
*/
typedef void(tBTM_LSTO_CBACK)(const RawAddress& remote_bda, uint16_t timeout);
 
/*****************************************************************************
 *  POWER MANAGEMENT
 ****************************************************************************/
/****************************
 *  Power Manager Constants
 ****************************/
/* BTM Power manager status codes */
enum {
  BTM_PM_STS_ACTIVE = HCI_MODE_ACTIVE,
  BTM_PM_STS_HOLD = HCI_MODE_HOLD,
  BTM_PM_STS_SNIFF = HCI_MODE_SNIFF,
  BTM_PM_STS_PARK = HCI_MODE_PARK,
  BTM_PM_STS_SSR,     /* report the SSR parameters in HCI_SNIFF_SUB_RATE_EVT */
  BTM_PM_STS_PENDING, /* when waiting for status from controller */
  BTM_PM_STS_ERROR    /* when HCI command status returns error */
};
typedef uint8_t tBTM_PM_STATUS;
 
/* BTM Power manager modes */
enum {
  BTM_PM_MD_ACTIVE = BTM_PM_STS_ACTIVE,
  BTM_PM_MD_HOLD = BTM_PM_STS_HOLD,
  BTM_PM_MD_SNIFF = BTM_PM_STS_SNIFF,
  BTM_PM_MD_PARK = BTM_PM_STS_PARK,
  BTM_PM_MD_FORCE = 0x10 /* OR this to force ACL link to a certain mode */
};
typedef uint8_t tBTM_PM_MODE;
 
#define BTM_PM_SET_ONLY_ID 0x80
 
/* Operation codes */
/* The module wants to set the desired power mode */
#define BTM_PM_REG_SET 1
/* The module wants to receive mode change event */
#define BTM_PM_REG_NOTIF 2
/* The module does not want to involve with PM anymore */
#define BTM_PM_DEREG 4
 
/************************
 *  Power Manager Types
 ************************/
typedef struct {
  uint16_t max;
  uint16_t min;
  uint16_t attempt;
  uint16_t timeout;
  tBTM_PM_MODE mode;
} tBTM_PM_PWR_MD;
 
/*************************************
 *  Power Manager Callback Functions
 *************************************/
typedef void(tBTM_PM_STATUS_CBACK)(const RawAddress& p_bda,
                                   tBTM_PM_STATUS status, uint16_t value,
                                   uint8_t hci_status);
 
/************************
 *  Stored Linkkey Types
 ************************/
#define BTM_CB_EVT_DELETE_STORED_LINK_KEYS 4
 
typedef struct {
  uint8_t event;
  uint8_t status;
  uint16_t num_keys;
 
} tBTM_DELETE_STORED_LINK_KEY_COMPLETE;
 
/* ACL link on, SCO link ongoing, sniff mode */
#define BTM_CONTRL_ACTIVE 1
/* Scan state - paging/inquiry/trying to connect*/
#define BTM_CONTRL_SCAN 2
/* Idle state - page scan, LE advt, inquiry scan */
#define BTM_CONTRL_IDLE 3
 
typedef uint8_t tBTM_CONTRL_STATE;
 
#endif  // BTM_API_TYPES_H