aboutsummaryrefslogtreecommitdiff
path: root/platform/atm2/ATM22xx-x1x/lib/ble_gap/ble_gap.h
blob: 867123a38450d53be1957c1ee6f26d18d5c4dcc2 (plain)
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
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
/**
 *******************************************************************************
 *
 * @file ble_gap.h
 *
 * @brief Header file - BLE GAP API.
 *
 * Copyright (C) Atmosic 2020-2023
 *
 *
 *******************************************************************************
 */
#pragma once

/**
 *******************************************************************************
 * @defgroup ATM_BTFM_BLE_GAP GAP API
 * @ingroup ATM_BTFM_API
 * @brief ATM bluetooth framework GAP API
 *
 * This module contains the necessary API to deal with the GAP messages.
 *
 * @{
 *******************************************************************************
 */


/*
 * INCLUDE FILES
 *******************************************************************************
 */

#include "co_bt.h"     // Common BT Definitions
#include "gap.h"       // GAP Definition
#include "gapm.h"
#include "gapc.h"
#include "ble_common.h"
#include "ble_err.h"

#ifdef __cplusplus
extern "C" {
#endif

/// Key length
#define BLE_GAP_KEY_LEN 16
/// Random number number
#define BLE_GAP_RAND_NB_LEN 8
/// Maximum test TX power level
#define BLE_GAP_MAX_TEST_TX_PWR_LVL 0x7F
/// Minimum switching pattern length
#define BLE_GAP_MIN_SWITCHING_PATTERN_LEN 2
/// Maximum switching pattern length
#define BLE_GAP_MAX_SWITCHING_PATTERN_LEN 0x4B
/// LE Feature Flags Length
#define BLE_GAP_LE_FEATS_LEN 8
/// LE Channel map length
#define BLE_GAP_LE_CHNL_MAP_LEN 5
/// Scanning window (in 625us slot)
#define BLE_SCAN_WINDOW_DFT 0x0010
/// Scanning interval (in 625us slot)
#define BLE_SCAN_INTERVAL_DFT 0x0010
/// Database hash length
#define BLE_GAP_DATABASE_HASH_LEN 16

/*
 * TYPEDEFINE
 *******************************************************************************
 */

/// Random Address type
typedef enum {
    /// Static random address
    BLE_GAP_STATIC_ADDR = 0xc0,
    /// Private non-resolvable address
    BLE_GAP_NON_RSLV_ADDR = 0x00,
    /// Private resolvable address
    BLE_GAP_RSLV_ADDR = 0x40,
} ble_gapm_rand_addr_type_t;

/// BLE ADV data type
typedef enum {
    /// Advertising data
    BLE_GAPM_ADV_DATA,
    /// Scan response data
    BLE_GAPM_SCAN_RSP_DATA,
    /// Periodic advertising data
    BLE_GAPM_PERIOD_ADV_DATA
} ble_gapm_data_type_t;

typedef enum {
    /// Extended advertising report
    BLE_GAP_REPORT_TYPE_ADV_EXT,
    /// Legacy advertising report
    BLE_GAP_REPORT_TYPE_ADV_LEG,
    /// Extended scan response report
    BLE_GAP_REPORT_TYPE_SCAN_RSP_EXT,
    /// Legacy scan response report
    BLE_GAP_REPORT_TYPE_SCAN_RSP_LEG,
    /// Periodic advertising report
    BLE_GAP_REPORT_TYPE_PER_ADV,
} ble_gap_adv_report_type_t;

typedef enum {
    /// Report Type (see ble_gap_adv_report_type_t)
    BLE_GAP_REPORT_INFO_REPORT_TYPE_MASK = 0x07,
    /// Report is complete
    BLE_GAP_REPORT_INFO_COMPLETE_BIT = (1 << 3),
    /// Connectable advertising
    BLE_GAP_REPORT_INFO_CONN_ADV_BIT = (1 << 4),
    /// Scannable advertising
    BLE_GAP_REPORT_INFO_SCAN_ADV_BIT = (1 << 5),
    /// Directed advertising
    BLE_GAP_REPORT_INFO_DIR_ADV_BIT = (1 << 6),
} ble_gap_adv_report_info_t;

/// Packet payload type for test mode
typedef enum {
    /// PRBS9 sequence "11111111100000111101..." (in transmission order)
    BLE_GAP_PKT_PLD_PRBS9,
    /// Repeated "11110000" (in transmission order)
    BLE_GAP_PKT_PLD_REPEATED_11110000,
    /// Repeated "10101010" (in transmission order)
    BLE_GAP_PKT_PLD_REPEATED_10101010,
    /// PRBS15 sequence
    BLE_GAP_PKT_PLD_PRBS15,
    /// Repeated "11111111" (in transmission order) sequence
    BLE_GAP_PKT_PLD_REPEATED_11111111,
    /// Repeated "00000000" (in transmission order) sequence
    BLE_GAP_PKT_PLD_REPEATED_00000000,
    /// Repeated "00001111" (in transmission order) sequence
    BLE_GAP_PKT_PLD_REPEATED_00001111,
    /// Repeated "01010101" (in transmission order) sequence
    BLE_GAP_PKT_PLD_REPEATED_01010101,
} ble_gap_pkt_pld_type_t;

/// Enumeration of TX PHY values
typedef enum {
    /// LE 1M PHY
    BLE_GAP_TX_PHY_1MBPS = 1,
    /// LE 2M PHY
    BLE_GAP_TX_PHY_2MBPS = 2,
    /// LE Coded PHY with S=8 data coding
    BLE_GAP_TX_PHY_125KBPS = 3,
    /// LE Coded PHY with S=2 data coding
    BLE_GAP_TX_PHY_500KBPS = 4,
} ble_gap_tx_phy_val_t;

/// Enumeration of RX PHY values
typedef enum {
    /// LE 1M PHY
    BLE_GAP_RX_PHY_1MBPS = 1,
    /// LE 2M PHY
    BLE_GAP_RX_PHY_2MBPS = 2,
    /// LE Coded PHY
    BLE_GAP_RX_PHY_CODED = 3,
} ble_gap_rx_phy_val_t;

/// Enumeration of PHY values
typedef enum {
    /// LE 1M PHY
    BLE_GAP_PHY_1MBPS = 1,
    /// LE 2M PHY
    BLE_GAP_PHY_2MBPS = 2,
    /// LE Coded PHY
    BLE_GAP_PHY_CODED = 3,
} ble_gap_phy_val_t;

/// Constant tone extension type
typedef enum {
    /// AoA constant tone extension
    BLE_GAP_CTE_AOA,
    /// AoD constant tone extension with 1 us slots
    BLE_GAP_CTE_AOD_1US,
    /// AoD constant tone extension with 2 us slots
    BLE_GAP_CTE_AOD_2US,
} ble_gap_cte_type_t;

/// LE transmitter test parameters
typedef struct {
    /// TX channel (range: 0x00 to 0x27)
    uint8_t ch;
    /// Length in bytes of payload data in each packet
    uint8_t test_data_len;
    /// Packet payload type
    ble_gap_pkt_pld_type_t pkt_pld_type;
    /// PHY
    ble_gap_tx_phy_val_t phy;
    /// CTE length
    uint8_t cte_len;
    /// CTE type
    ble_gap_cte_type_t cte_type;
    /// Transmit power level in dBm (0x7E: minimum | 0x7F: maximum | range: -127
    /// to +20)
    int8_t tx_pwr_lvl;
    /// The number of antenna IDs in the pattern (range: 0x02 to 0x4B)
    uint8_t switching_pattern_len;
    /// Antenna IDs
    uint8_t antenna_id[__ARRAY_EMPTY];
} ble_gap_test_tx_param_t;

/// Modulation index
typedef enum {
    /// Assume transmitter will have a standard modulation index
    BLE_GAP_MODULATION_STANDARD,
    /// Assume transmitter will have a stable modulation index
    BLE_GAP_MODULATION_STABLE,
} ble_gap_mod_idx_t;

/// Slot durations
typedef enum {
    /// Switching and sampling slots are 1 μs each
    BLE_GAP_SLOT_DUR_1US = 1,
    /// Switching and sampling slots are 2 μs each
    BLE_GAP_SLOT_DUR_2US,
} ble_gap_slot_dur_t;

/// LE receiver test parameters
typedef struct {
    /// RX channel (range: 0x00 to 0x27)
    uint8_t ch;
    /// PHY
    ble_gap_rx_phy_val_t phy;
    /// Modulation index
    ble_gap_mod_idx_t mod_idx;
    /// Expected CTE length
    uint8_t exp_cte_len;
    /// Expected CTE type
    ble_gap_cte_type_t exp_cte_type;
    /// Slot durations
    ble_gap_slot_dur_t slot_dur;
    /// The number of antenna IDs in the pattern (range: 0x02 to 0x4B)
    uint8_t switching_pattern_len;
    /// Antenna IDs
    uint8_t antenna_id[__ARRAY_EMPTY];
} ble_gap_test_rx_param_t;

/// BLE ADV channel selection
typedef enum {
    /// Channel 37
    ADV_37_CHNL = (1 << 0),
    /// Channel 38
    ADV_38_CHNL = (1 << 1),
    /// Channel 39
    ADV_39_CHNL = (1 << 2),
    /// Channel 37, 38 and 39
    ADV_ALL_CHNLS = ADV_37_CHNL | ADV_38_CHNL | ADV_39_CHNL
} ble_adv_chnl_t;

/// Advertising properties bit field bit value
typedef enum {
    /// Indicate that advertising is connectable, reception of
    /// CONNECT_IND or AUX_CONNECT_REQ PDUs is accepted. Not applicable
    /// for periodic advertising.
    ADV_PROP_CONNECTABLE_BIT = (1 << 0),
    /// Indicate that advertising is scannable, reception of SCAN_REQ or
    /// AUX_SCAN_REQ PDUs is accepted
    ADV_PROP_SCANNABLE_BIT = (1 << 1),
    /// Indicate that advertising targets a specific device. Only apply
    /// in
    /// following cases:
    ///   - Legacy advertising: if connectable
    ///   - Extended advertising: connectable or (non connectable and
    /// non discoverable)
    ADV_PROP_DIRECTED_BIT = (1 << 2),
    /// Indicate that High Duty Cycle has to be used for advertising on
    /// primary channel Apply only if created advertising is not an
    /// extended advertising
    ADV_PROP_HDC_BIT = (1 << 3),
    /// Reserved for bit 4
    ADV_PROP_RESERVED_4_BIT = (1 << 4),
    /// Enable anonymous mode. Device address won't appear in send PDUs
    /// Valid only if created advertising is an extended advertising
    ADV_PROP_ANONYMOUS_BIT = (1 << 5),
    /// Include TX Power in the extended header of the advertising PDU.
    /// Valid only if created advertising is not a legacy advertising
    ADV_PROP_TX_PWR_BIT = (1 << 6),
    /// Include TX Power in the periodic advertising PDU.
    /// Valid only if created advertising is a periodic advertising
    ADV_PROP_PER_TX_PWR_BIT = (1 << 7),
    /// Indicate if application must be informed about received scan
    /// requests PDUs
    ADV_PROP_SCAN_REQ_NTF_EN_BIT = (1 << 8),
} ble_adv_prop_t;

/// Leggacy advertising properties. Used when type is ADV_TYPE_LEGACY
enum adv_crt_leg_prop {
    /// Non connectable non scannable advertising
    ADV_LEGACY_NON_CONN_NON_SCAN_MASK,
    /// Broadcast non scannable advertising - Discovery mode must be
    /// Non Discoverable
    ADV_LEGACY_BROADCAST_NON_SCAN_MASK =
	ADV_LEGACY_NON_CONN_NON_SCAN_MASK,
    /// Non connectable scannable advertising
    ADV_LEGACY_NON_CONN_SCAN_MASK = ADV_PROP_SCANNABLE_BIT,
    /// Broadcast non scannable advertising - Discovery mode must be Non
    /// Discoverable
    ADV_LEGACY_BROADCAST_SCAN_MASK = ADV_LEGACY_NON_CONN_SCAN_MASK,
    /// Undirected connectable advertising
    ADV_LEGACY_UNDIR_CONN_MASK = ADV_PROP_CONNECTABLE_BIT |
	ADV_PROP_SCANNABLE_BIT,
    /// Directed connectable advertising
    ADV_LEGACY_DIR_CONN_MASK = ADV_PROP_DIRECTED_BIT |
	ADV_PROP_CONNECTABLE_BIT,
    /// Directed connectable with Low Duty Cycle
    ADV_LEGACY_DIR_CONN_LDC_MASK = ADV_LEGACY_DIR_CONN_MASK,
    /// Directed connectable with High Duty Cycle
    ADV_LEGACY_DIR_CONN_HDC_MASK = ADV_LEGACY_DIR_CONN_MASK |
	ADV_PROP_HDC_BIT,
};

/// Leggacy advertising properties. Used when type is ADV_TYPE_EXTENDED
enum adv_crt_ext_prop {
    /// Non connectable non scannable extended advertising
    ADV_EXT_NON_CONN_NON_SCAN_MASK,
    /// Non connectable scannable extended advertising
    ADV_EXT_NON_CONN_SCAN_MASK = ADV_PROP_SCANNABLE_BIT,
    /// Non connectable scannable directed extended advertising
    ADV_EXT_NON_CONN_SCAN_DIR_MASK = ADV_PROP_SCANNABLE_BIT |
	ADV_PROP_DIRECTED_BIT,
    /// Non connectable anonymous directed extended advertising
    ADV_EXT_ANONYM_DIR_MASK = ADV_PROP_ANONYMOUS_BIT |
	ADV_PROP_DIRECTED_BIT,
    /// Undirected connectable extended advertising
    ADV_EXT_UNDIR_CONN_MASK = ADV_PROP_CONNECTABLE_BIT,
    /// Directed connectable extended advertising
    ADV_EXT_DIR_CONN_MASK = ADV_PROP_CONNECTABLE_BIT |
	ADV_PROP_DIRECTED_BIT,
};

/// Periodic advertising properties. Used when type is ADV_TYPE_PERIODIC
enum adv_crt_per_prop {
    /// Undirected periodic advertising
    ADV_PER_UNDIR_MASK,
    /// Directed periodic advertising
    ADV_PER_DIR_MASK = ADV_PROP_DIRECTED_BIT,
};

/// Advertising parameters for advertising creation
typedef struct {
    /// Advertising type
    enum adv_crt_type {
	/// Legacy advertising
	ADV_TYPE_LEGACY,
	/// Extended advertising
	ADV_TYPE_EXTENDED,
	/// Periodic advertising
	ADV_TYPE_PERIODIC,
    } type;

    /// Advertising discovery mode
    enum adv_crt_mode {
	/// Mode in non-discov rable
	ADV_MODE_NON_DISC,
	/// Mode in general discoverable
	ADV_MODE_GEN_DISC,
	/// Mode in limited discoverable
	ADV_MODE_LIM_DISC,
	/// Broadcast mode without presence of AD_TYPE_FLAG in advertising data
	ADV_MODE_BEACON,
	/// Number of mode
	ADV_MODE_MAX,
    } disc_mode;

    /// Advertising property
    /// This may come from OR operation of values of ble_adv_prop_t
    /// with one of value from @see enum adv_crt_leg_prop,
    /// @see enum adv_crt_ext_prop or @see enum adv_crt_per_prop.
    uint16_t prop;

    /// Maximum power level at which the advertising packets have to be
    /// transmitted (between -127 and 126 dBm)
    int8_t max_tx_pwr;
    /// Advertising filter policy
    enum adv_crt_filter_policy {
	/// Allow both scan and connection requests from anyone
	FILTER_SCAN_ANY_CON_ANY,
	/// Allow both scan req from White List devices only and connection req
	/// from anyone
	FILTER_SCAN_WLST_CON_ANY,
	/// Allow both scan req from anyone and connection req from White List
	/// devices only
	FILTER_SCAN_ANY_CON_WLST,
	/// Allow scan and connection requests from White List devices only
	FILTER_SCAN_WLST_CON_WLST,
    } filter_pol;
    /// Peer address configuration (only used in case of directed advertising)
    ble_gap_bdaddr_t peer_addr;
    /// Configuration for advertising on primary channel
    struct primary_config {
	/// Minimum advertising interval (in unit of 625us). Must be greater
	/// than 20ms
	uint32_t adv_intv_min;
	/// Maximum advertising interval (in unit of 625us). Must be greater
	/// than 20ms
	uint32_t adv_intv_max;
	/// Bit field indicating the channel mapping. Bit number 0/1/2 is
	/// channel number 37/38/39.
	ble_adv_chnl_t chnl_map;
	/// Note that LE 2M PHY is not allowed and that legacy advertising only
	/// support LE 1M PHY
	ble_gap_phy_val_t phy;
    } prim_cfg;
    /// Configuration for secondary advertising (valid only if advertising type
    /// is ADV_TYPE_EXTENDED or ADV_TYPE_PERIODIC)
    struct second_config {
	/// Maximum number of advertising events the controller can skip before
	/// sending the
	/// AUX_ADV_IND packets. 0 means that AUX_ADV_IND PDUs shall be sent
	/// prior each
	/// advertising events
	uint8_t max_skip;
	/// Indicate on which PHY secondary advertising has to be performed
	ble_gap_phy_val_t phy;
	/// Advertising SID
	uint8_t adv_sid;
    } second_cfg;
    /// Configuration for periodic advertising (valid only if advertising type
    /// is ADV_TYPE_PERIODIC)
    struct periodic_config {
	/// Minimum advertising interval (in unit of 1.25ms). Must be greater
	/// than 20ms
	uint16_t adv_intv_min;
	/// Maximum advertising interval (in unit of 1.25ms). Must be greater
	/// than 20ms
	uint16_t adv_intv_max;
    } period_cfg;
} ble_gapm_adv_create_param_t;

/// Generic key structure
typedef struct {
    /// Key value MSB -> LSB
    uint8_t key[BLE_GAP_KEY_LEN];
} ble_gap_key_t;

typedef struct {
    /// Identity Resolving Key
    ble_gap_key_t irk;
    /// Device BD Identity Address
    ble_gap_bdaddr_t addr;
} ble_gap_irk_t;

/// Bit field use to select the preferred TX or RX LE PHY. 0 means no
/// preferences.
typedef enum {
    /// No preferred PHY.
    BLE_GAP_PHY_ANY = 0x00,
    /// LE 1M PHY preferred for an active link.
    BLE_GAP_PHY_LE_1MBPS = (1 << 0),
    /// LE 2M PHY preferred for an active link.
    BLE_GAP_PHY_LE_2MBPS = (1 << 1),
    /// LE Coded PHY preferred for an active link.
    BLE_GAP_PHY_LE_CODED = (1 << 2),
} ble_gap_phy_mode_t;

/// Specify what rate Host prefers to use in transmission on coded PHY. refer to
/// HCI: 7.8.49
typedef enum {
    /// No preferred coding when transmitting on the LE Coded PHY.
    BLE_GAP_PHY_OPT_NO_LE_CODED_TX_PREF,
    /// Prefers that S=2 coding be used when transmitting on the LE Coded PHY.
    BLE_GAP_PHY_OPT_S2_LE_CODED_TX_PREF,
    /// Prefers that S=8 coding be used when transmitting on the LE Coded PHY.
    BLE_GAP_PHY_OPT_S8_LE_CODED_TX_PREF,
} ble_gap_phy_opt_t;

typedef enum {
    /// No role set yet
    BLE_GAP_ROLE_NONE = 0x00,
    /// Observer role
    BLE_GAP_ROLE_OBSERVER = 0x01,
    /// Broadcaster role
    BLE_GAP_ROLE_BROADCASTER = 0x02,
    /// Master/Central role
    BLE_GAP_ROLE_CENTRAL = (0x04 | BLE_GAP_ROLE_OBSERVER),
    /// Peripheral/Slave role
    BLE_GAP_ROLE_PERIPHERAL = (0x08 | BLE_GAP_ROLE_BROADCASTER),
    /// Device has all role, both peripheral and central
    BLE_GAP_ROLE_ALL = (BLE_GAP_ROLE_CENTRAL | BLE_GAP_ROLE_PERIPHERAL),
} ble_gap_role_t;

/// Privacy configuration
typedef enum {
    /// Identity address type. Public address:0, Static private random
    /// address:1
    BLE_GAP_PRIV_CFG_PRIV_ADDR_BIT = (1 << 0),
    /// Reserved
    BLE_GAP_PRIV_CFG_RSVD = (1 << 1),
    /// Enable controller privacy
    BLE_GAP_PRIV_CFG_PRIV_EN_BIT = (1 << 2),
} ble_gap_priv_cfg_t;

/// Pairing Mode
typedef enum {
    /// No pairing authorized
    BLE_GAP_PAIRING_DISABLE = 0,
    /// Legacy pairing Authorized
    BLE_GAP_PAIRING_LEGACY = (1 << 0),
    /// Secure Connection pairing Authorized
    BLE_GAP_PAIRING_SEC_CON = (1 << 1),
} ble_gap_pairing_mode_t;

/// Device Attribute write permission requirement
typedef enum {
    /// Disable write access
    BLE_GAP_WRITE_DISABLE,
    /// Enable write access - no authentication required
    BLE_GAP_WRITE_NO_AUTH,
    /// Write access requires unauthenticated link
    BLE_GAP_WRITE_UNAUTH,
    /// Write access requires authenticated link
    BLE_GAP_WRITE_AUTH,
    /// Write access requires secure connected link
    BLE_GAP_WRITE_SEC_CON,
} ble_gap_write_att_perm_t;

/// Attribute database configuration
///    8    7     6    5    4    3    2    1    0
/// +-----+----+-----+----+----+----+----+----+----+
/// | MTU | SC | PCP |   APP_PERM   |   NAME_PERM  |
/// +-----+----+-----+----+----+----+----+----+----+
///   15   14   13   12    11     10    9
/// +----+----+----+----+------+------+----+
/// |        RFU        | RPAO | EATT | FE |
/// +----+----+----+----+------+------+----+
/// - Bit [0-2]  : Device Name write permission requirements for peer device.
///                (@see ble_gap_write_att_perm_t)
/// - Bit [3-5]  : Device Appearance write permission requirements for peer
///                device (@see ble_gap_write_att_perm_t)
/// - Bit [6]    : Peripheral Preferred Connection Parameters present.
/// - Bit [7]    : Service change feature present in GATT attribute database.
/// - Bit [8]    : Enable automatic MTU exchange at connection establishment.
/// - Bit [9]    : Enable automatic client feature enable setup at connection
///                establishment.
/// - Bit [10]   : Enable automatic establishment of Enhanced ATT bearers.
/// - Bit [11]   : Enable presence of Resolvable private address only.
/// - Bit [12-15]: Reserve for future use.
typedef enum {
    /// Device Name write permission requirements for peer device
    /// (@see ble_gap_write_att_perm_t)
    BLE_GAP_ATT_NAME_PERM_MASK = 0x0007,
    BLE_GAP_ATT_NAME_PERM_POS = 0,
    /// Device Appearance write permission requirements for peer device
    /// (@see ble_gap_write_att_perm_t)
    BLE_GAP_ATT_APPEARENCE_PERM_MASK = 0x0038,
    BLE_GAP_ATT_APPEARENCE_PERM_POS = 3,
    /// Peripheral Preferred Connection Parameters present in GAP attribute
    /// database.
    BLE_GAP_ATT_PERIPH_PREF_CON_PAR_EN_MASK  = 0x0040,
    BLE_GAP_ATT_PERIPH_PREF_CON_PAR_EN_POS  = 6,
    /// Service change feature present in GATT attribute database.
    BLE_GAP_ATT_SVC_CHG_EN_MASK = 0x0080,
    BLE_GAP_ATT_SVC_CHG_EN_POS = 7,
    /// Automatic MTU exchange at connection establishment.
    BLE_GAP_ATT_CLI_AUTO_MTU_EXCH_EN_MASK = 0x0100,
    BLE_GAP_ATT_CLI_AUTO_MTU_EXCH_EN_POS = 8,
    /// Automatic client feature enable
    BLE_GAP_ATT_CLI_AUTO_FEAT_EN_MASK = 0x0200,
    BLE_GAP_ATT_CLI_AUTO_FEAT_EN_POS = 9,
    /// Automatic establishment of Enhanced ATT bearers.
    BLE_GAP_ATT_CLI_AUTO_EATT_EN_MASK = 0x0400,
    BLE_GAP_ATT_CLI_AUTO_EATT_POS = 10,
    /// Presence of Resolvable private address only.
    BLE_GAP_ATT_RSLV_PRIV_ADDR_ONLY_MASK = 0x0800,
    BLE_GAP_ATT_RSLV_PRIV_ADDR_ONLY_POS = 11,
} ble_gap_att_cfg_flag_t;

typedef struct {
    /// Device Role: Central, Peripheral, Observer, Broadcaster or All roles.
    ble_gap_role_t role;

    /// Privacy Config
    /// Duration before regenerate device address when privacy is enabled.
    /// unit:sec
    uint16_t renew_dur;
    /// Provided own static private random address
    ble_bdaddr_t addr;
    /// Device IRK used for resolvable random BD address generation (LSB first)
    ble_gap_key_t irk;
    /// Privacy configuration bit field
    ble_gap_priv_cfg_t privacy_cfg;

    /// Security Config
    /// Pairing mode authorized
    ble_gap_pairing_mode_t pairing_mode;

    /// ATT Database Config
    /// GAP service start handle
    uint16_t gap_start_hdl;
    /// GATT service start handle
    uint16_t gatt_start_hdl;
    /// Attribute database configuration
    ble_gap_att_cfg_flag_t att_cfg;

    /// LE Data Length Extension
    /// Suggested value for the Controller's maximum transmitted number of
    /// payload octets to be used
    uint16_t sugg_max_tx_octets;
    /// Suggested value for the Controller's maximum packet transmission time to
    /// be used (unit: micro sec)
    uint16_t sugg_max_tx_time;

    /// L2CAP Configuration
    /// Maximal MTU size
    uint16_t max_mtu;
    /// Maximal MPS Packet size
    uint16_t max_mps;
    /// Maximum number of LE Credit based connection that can be established
    uint8_t max_nb_lecb;

    /// LE PHY Management
    /// Preferred LE PHY for data transmission
    ble_gap_phy_mode_t tx_pref_phy;
    /// Preferred LE PHY for data reception
    ble_gap_phy_mode_t rx_pref_phy;

    /// Other settings
    /// RF TX Path Compensation value (from -128dB to 128dB, unit is 0.1dB)
    uint16_t tx_path_comp;
    /// RF RX Path Compensation value (from -128dB to 128dB, unit is 0.1dB)
    uint16_t rx_path_comp;
} ble_gap_set_dev_config_t;

/// BLE peer info type
typedef enum {
    /// Retrieve name of peer device.
    BLE_GAP_GET_PEER_NAME,
    /// Retrieve peer device version info.
    BLE_GAP_GET_PEER_VERSION,
    /// Retrieve peer device features.
    BLE_GAP_GET_PEER_FEATURES,
    /// Retrieve connection RSSI.
    BLE_GAP_GET_CON_RSSI,
    /// Retrieve connection channel map.
    BLE_GAP_GET_CON_CHANNEL_MAP,
    /// Get peer device appearance.
    BLE_GAP_GET_PEER_APPEARANCE,
    /// Get peer device peripheral preferred parameters.
    BLE_GAP_GET_PEER_PERIPH_PREF_PARAMS,
    /// Get timer timeout value.
    BLE_GAP_GET_LE_PING_TO,
    /// Central address resolution supported.
    BLE_GAP_GET_ADDR_RESOL_SUPP,
    /// Retrieve PHY configuration of active link.
    BLE_GAP_GET_PHY,
    /// Retrieve channel selection algorithm.
    BLE_GAP_GET_CHAN_SEL_ALGO,
} ble_gap_link_info_type_t;

/// Peer irk structure
typedef struct {
    /// irk information
    ble_gap_irk_t irk;
    /// Bonding Index
    int idx;
} ble_gap_irk_idx_t;

/// Resolving address command complete event data structure
typedef struct {
    /// Index of irk
    uint8_t index;
    /// Number of irk
    uint8_t nb;
    /// Irk
    ble_gap_irk_idx_t irk[__ARRAY_EMPTY];
} resl_addr_t;

/// Connection parameter
typedef struct {
    /// Connection interval minimum
    uint16_t intv_min;
    /// Connection interval maximum
    uint16_t intv_max;
    /// Latency
    uint16_t latency;
    /// Supervision timeout
    uint16_t time_out;
} ble_gap_conn_param_t;

typedef struct {
    /// GAP requested operation
    uint8_t operation;
    /// Status of the request
    uint8_t status;
} gapm_cmp_evt_t;

/// The list of names for registered gap event function.
typedef enum {
    /// Handler in atm_gap.
    GAP_EVT_NAME_ATM_GAP,
    /// Handler in ble_gap_sec.
    GAP_EVT_NAME_BLE_GAP_SEC,
    /// Handler in test.
    GAP_EVT_NAME_TEST,
    /// Max number of this enumerations.
    GAP_EVT_NAME_MAX,
} ble_gap_evt_reg_name_t;

typedef struct {
    /// 8-byte array for random number
    uint8_t nb[BLE_GAP_RAND_NB_LEN];
} ble_rand_nb_t;

typedef struct ble_gap_ext_adv_report_ind {
    /// Bit field info
    ble_gap_adv_report_info_t info;
    /// Transmitter device address
    ble_gap_bdaddr_t trans_addr;
    /// Target device address
    /// Valid only for directed advertising report
    ble_gap_bdaddr_t target_addr;
    /// TX power (in dBm)
    int8_t tx_pwr;
    /// RSSI (between -127 and +20 dBm)
    int8_t rssi;
    /// Primary PHY (1:1M, 3:Coded)
    uint8_t phy_prim;
    /// Secondary PHY (0:No packet on secondary phy, 1:1M, 2:2M, 3:Coded)
    uint8_t phy_second;
    /// Advertising SID
    /// Valid only for periodic advertising report
    uint8_t adv_sid;
    /// Periodic advertising interval (in unit of 1.25ms, min is 7.5ms)
    /// Valid only for periodic advertising report
    uint16_t period_adv_intv;
    /// Report length
    uint16_t length;
    /// Report
    uint8_t data[__ARRAY_EMPTY];
} ble_gap_ind_ext_adv_report_t;

/// Device name
typedef struct {
    /// Name length
    uint16_t value_length;
    /// Name value (starting from offset to maximum length for BLE_GAP_DEV_NAME
    /// request)
    uint8_t value[__ARRAY_EMPTY];
} ble_gap_dev_name_t;

typedef struct {
    uint8_t con_idx;
    ble_gap_dev_name_t param;
} ble_gap_le_dev_name_t;

typedef struct {
    uint8_t con_idx;
    /// Appearance Icon
    uint16_t appearance;
} ble_gap_le_appearance_t;

/// Peripheral preference parameters
typedef struct {
    /// Connection interval minimum
    uint16_t con_intv_min;
    /// Connection interval maximum
    uint16_t con_intv_max;
    /// Peripheral latency
    uint16_t periph_latency;
    /// Connection supervision timeout multiplier
    uint16_t con_timeout;
} ble_gap_periph_pref_t;

typedef struct {
    uint8_t con_idx;
    ble_gap_periph_pref_t param;
} ble_gap_le_periph_pref_t;

typedef struct {
    uint8_t con_idx;
    /// Resolvable Private address only
    uint8_t rslv_priv_addr_only;
} ble_gap_le_rslv_priv_addr_only_t;

/// Peer version info
typedef struct {
    /// Manufacturer name
    uint16_t comp_id;
    /// LMP subversion
    uint16_t lmp_subvers;
    /// LMP version
    uint8_t lmp_vers;
} ble_gap_peer_version_t;

typedef struct {
    uint8_t con_idx;
    ble_gap_peer_version_t param;
} ble_gap_le_peer_version_t;

/// Peer features info
typedef struct {
    /// 8-byte array for LE features
    uint8_t features[BLE_GAP_LE_FEATS_LEN];
} ble_gap_peer_features_t;

typedef struct {
    uint8_t con_idx;
    ble_gap_peer_features_t param;
} ble_gap_le_peer_features_t;

/// Ongoing connection RSSI
typedef struct {
    /// RSSI value
    int8_t rssi;
} ble_gap_con_rssi_t;

typedef struct {
    uint8_t con_idx;
    ble_gap_con_rssi_t param;
} ble_gap_le_con_rssi_t;

/// Channel map structure
typedef struct {
    /// 5-byte channel map array
    uint8_t map[BLE_GAP_LE_CHNL_MAP_LEN];
} ble_chnl_map_t;

/// Ongoing connection Channel Map
typedef struct {
    /// Channel map value
    ble_chnl_map_t ch_map;
} ble_gap_con_channel_map_t;

typedef struct {
    uint8_t con_idx;
    ble_gap_con_channel_map_t param;
} ble_gap_le_con_channel_map_t;

/// LE Ping
typedef struct {
    /// Authenticated payload timeout
    uint16_t timeout;
} ble_gap_le_ping_to_val_t;

typedef struct {
    uint8_t con_idx;
    ble_gap_le_ping_to_val_t param;
} ble_gap_le_le_ping_to_val_t;

/// Active link PHY configuration. Triggered when configuration is read or
/// during an update.
typedef struct {
    /// LE PHY for data transmission.
    ble_gap_phy_val_t tx_phy;
    /// LE PHY for data reception.
    ble_gap_phy_val_t rx_phy;
} ble_gap_le_phy_t;

typedef struct {
    uint8_t con_idx;
    ble_gap_le_phy_t param;
} ble_gap_le_le_phy_t;

/// Channel selection algorithm
typedef struct {
    /// Used channel selection algorithm
    uint8_t chan_sel_algo;
} ble_gap_chan_sel_algo_t;

typedef struct {
    uint8_t con_idx;
    ble_gap_chan_sel_algo_t param;
} ble_gap_le_chan_sel_algo_t;

/// Response data of @ref ble_gapc_get_link_info
typedef struct {
    /// Get information type.
    ble_gap_link_info_type_t type;
    union {
	/// Data for BLE_GAP_GET_PEER_NAME type
	ble_gap_le_dev_name_t name;
	/// Data for BLE_GAP_GET_PEER_APPEARANCE type
	ble_gap_le_appearance_t appearance;
	/// Data for BLE_GAP_GET_PEER_PERIPH_PREF_PARAMS type
	ble_gap_le_periph_pref_t periph_pref;
	/// Data for BLE_GAP_GET_ADDR_RESOL_SUPP type
	ble_gap_le_rslv_priv_addr_only_t rslv_priv_addr_only;
	/// Data for BLE_GAP_GET_PEER_VERSION type
	ble_gap_le_peer_version_t peer_ver;
	/// Data for BLE_GAP_GET_PEER_FEATURES type
	ble_gap_le_peer_features_t peer_feat;
	/// Data for BLE_GAP_GET_CON_RSSI type
	ble_gap_le_con_rssi_t con_rssi;
	/// Data for BLE_GAP_GET_CON_CHANNEL_MAP type
	ble_gap_le_con_channel_map_t con_chnl_map;
	/// Data for BLE_GAP_GET_LE_PING_TO type
	ble_gap_le_le_ping_to_val_t le_ping_to;
	/// Data for BLE_GAP_GET_PHY type
	ble_gap_le_le_phy_t le_phy;
	/// Data for BLE_GAP_GET_CHAN_SEL_ALGO type
	ble_gap_le_chan_sel_algo_t chan_sel_algo;
    };
} ble_gap_link_info_t;

typedef struct {
    /// Activity identifier
    uint8_t actv_idx;
    /// Activity stop reason
    ble_err_code_t reason;
    /// In case of periodic advertising, indicate if periodic advertising
    /// has been stopped
    uint8_t per_adv_stop;
} ble_gap_ind_stop_t;

typedef struct {
    /// Connection handle
    uint16_t conhdl;
    /// Connection interval
    uint16_t con_interval;
    /// Connection latency
    uint16_t con_latency;
    /// Supervision timeout
    uint16_t sup_to;
    /// Clock accuracy
    uint8_t clk_accuracy;
    /// Peer address type
    uint8_t peer_addr_type;
    /// Peer BT address
    ble_bdaddr_t peer_addr;
    /// Role of device in connection (0 = Central / 1 = Peripheral)
    uint8_t role;
} ble_gap_ind_con_est_t;

typedef struct {
    /// Connection index
    uint8_t conidx;
    /// Connection parameter
    ble_gap_ind_con_est_t param;
} ble_gap_ind_le_con_est_t;

/// Long Term Key information
typedef struct {
    /// Long Term Key
    ble_gap_key_t ltk;
    /// Encryption Diversifier
    uint16_t ediv;
    /// Random Number
    ble_rand_nb_t randnb;
    /// Encryption key size (7 to 16)
    uint8_t key_size;
} ble_gap_ltk_t;

typedef struct {
    /// Connection index
    uint8_t conidx;
    /// Long Term Key information
    ble_gap_ltk_t ltk;
} ble_gap_ind_le_ltk_t;

typedef struct {
    /// Connection index
    uint8_t conidx;
} ble_gap_ind_le_repeated_attempt_t;

/// Authentication mask
typedef enum {
    /// No Flag set
    BLE_GAP_AUTH_NONE = 0,
    /// Bond authentication
    BLE_GAP_AUTH_BOND = (1 << 0),
    /// Man In the middle protection
    BLE_GAP_AUTH_MITM = (1 << 2),
    /// Secure Connection
    BLE_GAP_AUTH_SEC_CON = (1 << 3),
    /// Key Notification
    BLE_GAP_AUTH_KEY_NOTIF = (1 << 4),
} ble_gap_auth_mask_t;

/// Authentication Requirements
typedef enum {
    /// No MITM No Bonding
    BLE_GAP_AUTH_REQ_NO_MITM_NO_BOND = BLE_GAP_AUTH_NONE,
    /// No MITM Bonding
    BLE_GAP_AUTH_REQ_NO_MITM_BOND = BLE_GAP_AUTH_BOND,
    /// MITM No Bonding
    BLE_GAP_AUTH_REQ_MITM_NO_BOND = BLE_GAP_AUTH_MITM,
    /// MITM and Bonding
    BLE_GAP_AUTH_REQ_MITM_BOND = BLE_GAP_AUTH_MITM | BLE_GAP_AUTH_BOND,
    /// SEC_CON, No MITM and No Bonding
    BLE_GAP_AUTH_REQ_NO_MITM_SEC_CON_NO_BOND = BLE_GAP_AUTH_SEC_CON,
    /// SEC_CON, No MITM and Bonding
    BLE_GAP_AUTH_REQ_NO_MITM_SEC_CON_BOND = BLE_GAP_AUTH_SEC_CON |
	BLE_GAP_AUTH_BOND,
    /// SEC_CON, MITM and No Bonding
    BLE_GAP_AUTH_REQ_MITM_SEC_CON_NO_BOND = BLE_GAP_AUTH_SEC_CON |
	BLE_GAP_AUTH_MITM,
    /// SEC_CON, MITM and Bonding
    BLE_GAP_AUTH_REQ_MITM_SEC_CON_BOND = BLE_GAP_AUTH_SEC_CON |
	BLE_GAP_AUTH_MITM | BLE_GAP_AUTH_BOND,

    /// Mask of  authentication features without reserved flag
    BLE_GAP_AUTH_REQ_MASK = 0x1F
} ble_gap_auth_t;

typedef enum {
    /// Unauthenticated pairing achieved but without bond data
    /// (meaning-less for connection confirmation)
    BLE_GAP_PAIRING_UNAUTH = BLE_GAP_AUTH_NONE,
    /// Authenticated pairing achieved but without bond data
    /// (meaning-less for connection confirmation)
    BLE_GAP_PAIRING_AUTH = BLE_GAP_AUTH_MITM,
    /// Secure connection pairing achieved but without bond data
    /// (meaning-less for connection confirmation)
    BLE_GAP_PAIRING_SECURE_CON = BLE_GAP_AUTH_SEC_CON | BLE_GAP_AUTH_MITM,

    /// No pairing performed with peer device
    /// (meaning-less for connection confirmation)
    BLE_GAP_PAIRING_NO_BOND = BLE_GAP_AUTH_NONE,
    /// Peer device bonded through an unauthenticated pairing.
    BLE_GAP_PAIRING_BOND_UNAUTH = BLE_GAP_AUTH_BOND,
    /// Peer device bonded through an authenticated pairing.
    BLE_GAP_PAIRING_BOND_AUTH = BLE_GAP_AUTH_MITM | BLE_GAP_AUTH_BOND,
    /// Peer device bonded through a secure connection pairing pairing.
    BLE_GAP_PAIRING_BOND_SECURE_CON = BLE_GAP_AUTH_SEC_CON | BLE_GAP_AUTH_MITM |
	BLE_GAP_AUTH_BOND,

    /// Pairing with bond data present Bit
    BLE_GAP_PAIRING_BOND_PRESENT_BIT = 0x01,
    BLE_GAP_PAIRING_BOND_PRESENT_POS = 0x00,
} ble_gap_pairing_level_t;

typedef struct {
    /// Connection handle
    uint8_t conidx;
    /// Error code
    ble_err_code_t reason;
    /// Pairing security level. (Valid if reason == BLE_ERR_NO_ERROR)
    ble_gap_pairing_level_t pairing_lvl;
    /// LTK is exchanged. (Valid if reason == BLE_ERR_NO_ERROR)
    bool ltk_present;
} ble_gap_ind_le_pair_end_t;

typedef struct {
    /// Connection handle
    uint16_t conhdl;
    /// Reason of disconnection
    ble_err_code_t reason;
} ble_gap_ind_discon_t;

typedef struct {
    uint8_t conidx;
    ble_gap_ind_discon_t param;
} ble_gap_ind_le_discon_t;

/// Privacy mode type
typedef enum {
    /// Network privacy mode
    BLE_GAP_PRIV_TYPE_NETWORK,
    /// Device privacy mode
    BLE_GAP_PRIV_TYPE_DEVICE,
} ble_gap_priv_type_t;

/// Resolving list device information
typedef struct {
    /// Device identity
    ble_gap_bdaddr_t addr;
    /// Privacy Mode
    ble_gap_priv_type_t priv_mode;
    /// Peer IRK
    uint8_t peer_irk[BLE_GAP_KEY_LEN];
    /// Local IRK
    uint8_t local_irk[BLE_GAP_KEY_LEN];
} ble_gap_ral_dev_info_t;

typedef struct {
    /// Connection interval used on this connection, unit:1.25 ms
    uint16_t con_interval;
    /// Peripheral latency for the connection in number of connection events,
    /// range: 0x0000 to 0x01F3
    uint16_t con_latency;
    /// Supervision timeout for this connection, unit: 10ms
    uint16_t sup_to;
} ble_gap_ind_con_update_t;

typedef struct {
    uint8_t conidx;
    ble_gap_ind_con_update_t param;
} ble_gap_ind_le_con_update_t;

typedef enum {
    /// Device name
    BLE_GAP_DEV_NAME,
    /// Device external appearance
    BLE_GAP_DEV_APPEARANCE,
    /// Peripheral preferred connection parameters
    BLE_GAP_DEV_PERIPH_PREF_PARAMS,
    /// Central address resolution supported
    BLE_GAP_DEV_CTL_ADDR_RESOL,
    /// Device database hash value
    BLE_GAP_DEV_DB_HASH,
    /// Resolvable private addresss as local addresss only
    BLE_GAP_DEV_RSLV_PRIV_ADDR_ONLY,
    /// Maximum device info parameter
    BLE_GAP_DEV_INFO_MAX,
} ble_gap_dev_info_t;

typedef union {
    /// Device name
    ble_gap_dev_name_t name;
    /// Appearance Icon
    uint16_t appearance;
} ble_gap_set_dev_info_t;

typedef struct {
    /// Requested device information
    ble_gap_dev_info_t req_type;
    /// Token value
    uint16_t token;
    /// Device information data
    ble_gap_set_dev_info_t info;
} ble_gap_ind_set_dev_req_t;

typedef struct {
    uint8_t conidx;
    ble_gap_ind_set_dev_req_t param;
} ble_gap_ind_le_set_dev_req_t;

typedef struct {
    /// true to accept connection parameters, false else.
    bool accept;
    /// Minimum length of connection event needed for this LE connection in
    /// 0.625 ms.
    uint16_t ce_len_min;
    /// Maximum length of connection event needed for this LE connection in
    /// 0.625 ms.
    uint16_t ce_len_max;
} ble_gapc_param_update_cfm_t;

typedef struct {
    /// Connection interval minimum in 1.25 ms
    uint16_t intv_min;
    /// Connection interval maximum in 1.25 ms
    uint16_t intv_max;
    /// Latency
    uint16_t latency;
    /// Supervision timeout in 10 ms
    uint16_t time_out;
} ble_gap_ind_param_update_req_t;

typedef struct {
    uint8_t conidx;
    ble_gap_ind_param_update_req_t param;
} ble_gap_ind_le_param_update_req_t;

typedef struct {
    /// PHY updated
    ble_gap_le_phy_t phy;
} ble_gap_ind_le_phy_t;

typedef struct {
    uint8_t con_idx;
    ble_gap_ind_le_phy_t param;
} ble_gap_ind_le_le_phy_t;

typedef struct {
    /// Channel selection algorithm updated
    ble_gap_chan_sel_algo_t algo;
} ble_gap_ind_chan_sel_algo_t;

typedef struct {
    uint8_t con_idx;
    ble_gap_ind_chan_sel_algo_t param;
} ble_gap_ind_le_chan_sel_algo_t;

/// Additional advertising parameters
typedef struct {
    /// Advertising duration (in unit of 10ms). 0 means that advertising
    /// continues until the host disable it
    uint16_t duration;
    /// Maximum number of extended advertising events the controller shall
    /// attempt to send prior to terminating the extending advertising Valid
    /// only if extended advertising
    uint8_t max_adv_evt;
} ble_gapm_adv_param_t;

/// Scan window operation parameters
typedef struct {
    /// Scan interval
    uint16_t scan_intv;
    /// Scan window
    uint16_t scan_wd;
} ble_gapm_scan_wd_op_param_t;

/// Scanning types
typedef enum {
    /// General discovery
    BLE_GAPM_SCAN_TYPE_GEN_DISC,
    /// Limited discovery
    BLE_GAPM_SCAN_TYPE_LIM_DISC,
    /// Observer
    BLE_GAPM_SCAN_TYPE_OBSERVER,
    /// Selective observer
    BLE_GAPM_SCAN_TYPE_SEL_OBSERVER,
    /// Connectable discovery
    BLE_GAPM_SCAN_TYPE_CONN_DISC,
    /// Selective connectable discovery
    BLE_GAPM_SCAN_TYPE_SEL_CONN_DISC,
} ble_gapm_scan_type_t;

/// Scanning properties bit field bit value
typedef enum {
    /// Scan advertisement on the LE 1M PHY
    BLE_GAPM_SCAN_PROP_PHY_1M_BIT = (1 << 0),
    /// Scan advertisement on the LE Coded PHY
    BLE_GAPM_SCAN_PROP_PHY_CODED_BIT = (1 << 1),
    /// Active scan on LE 1M PHY (Scan Request PDUs may be sent)
    BLE_GAPM_SCAN_PROP_ACTIVE_1M_BIT = (1 << 2),
    /// Active scan on LE Coded PHY (Scan Request PDUs may be sent)
    BLE_GAPM_SCAN_PROP_ACTIVE_CODED_BIT = (1 << 3),
    /// Accept directed advertising packets if we use a RPA and target address
    /// cannot be solved by the controller
    BLE_GAPM_SCAN_PROP_ACCEPT_RPA_BIT = (1 << 4),
    /// Filter truncated advertising or scan response reports
    BLE_GAPM_SCAN_PROP_FILT_TRUNC_BIT = (1 << 5),
} ble_gapm_scan_prop_t;

/// Filtering policy for duplicated packets
typedef enum {
    /// Disable filtering of duplicated packets
    BLE_GAPM_DUP_FILT_DIS,
    /// Enable filtering of duplicated packets
    BLE_GAPM_DUP_FILT_EN,
    /// Enable filtering of duplicated packets, reset for each scan period
    BLE_GAPM_DUP_FILT_EN_PERIOD,
} ble_gapm_dup_filter_pol_t;

/// Scanning parameters
typedef struct {
    /// Type of scanning to be started
    ble_gapm_scan_type_t type;
    /// Properties for the scan procedure
    ble_gapm_scan_prop_t prop;
    /// Duplicate packet filtering policy
    ble_gapm_dup_filter_pol_t dup_filt_pol;
    /// Reserved for future use
    uint8_t rsvd;
    /// Scan window opening parameters for LE 1M PHY
    ble_gapm_scan_wd_op_param_t scan_param_1m;
    /// Scan window opening parameters for LE Coded PHY
    ble_gapm_scan_wd_op_param_t scan_param_coded;
    /// Scan duration (in unit of 10ms). 0 means that the controller will scan
    /// continuously until reception of a stop command from the application
    uint16_t duration;
    /// Scan period (in unit of 1.28s). Time interval between two consequent
    /// starts of a scan duration by the controller. 0 means that the scan
    /// procedure is not periodic
    uint16_t period;
} ble_gapm_scan_param_t;

/// Initiating types
typedef enum {
    /// Direct connection establishment, establish a connection with an
    /// indicated device
    BLE_GAPM_INIT_TYPE_DIRECT_CONN_EST,
    /// Automatic connection establishment, establish a connection with all
    /// devices whose address is present in the white list
    BLE_GAPM_INIT_TYPE_AUTO_CONN_EST,
    /// Name discovery, Establish a connection with an indicated device in order
    /// to read content of its Device Name characteristic. Connection is closed
    /// once this operation is stopped.
    BLE_GAPM_INIT_TYPE_NAME_DISC,
} ble_gapm_init_type_t;

/// Initiating properties
typedef enum {
    /// Scan connectable advertisements on the LE 1M PHY. Connection parameters
    /// for the LE 1M PHY are provided
    BLE_GAPM_INIT_PROP_1M_BIT = (1 << 0),
    /// Connection parameters for the LE 2M PHY are provided
    BLE_GAPM_INIT_PROP_2M_BIT = (1 << 1),
    /// Scan connectable advertisements on the LE Coded PHY. Connection
    /// parameters for the LE Coded PHY are provided
    BLE_GAPM_INIT_PROP_CODED_BIT = (1 << 2),
} ble_gapm_init_prop_t;

/// Connection parameters
typedef struct {
    /// Minimum value for the connection interval (in unit of 1.25ms). Shall be
    /// less than or equal to conn_intv_max value. Allowed range is 7.5ms to 4s.
    uint16_t conn_intv_min;
    /// Maximum value for the connection interval (in unit of 1.25ms). Shall be
    /// greater than or equal to conn_intv_min value. Allowed range is 7.5ms to
    /// 4s.
    uint16_t conn_intv_max;
    /// Slave latency. Number of events that can be missed by a connected slave
    /// device
    uint16_t conn_latency;
    /// Link supervision timeout (in unit of 10ms). Allowed range is 100ms to
    /// 32s
    uint16_t supervision_to;
    /// Recommended minimum duration of connection events (in unit of 625us)
    uint16_t ce_len_min;
    /// Recommended maximum duration of connection events (in unit of 625us)
    uint16_t ce_len_max;
} ble_gapm_conn_param_t;

/// Initiating parameters
typedef struct {
    /// Initiating type
    ble_gapm_init_type_t type;
    /// Properties for the initiating procedure
    ble_gapm_init_prop_t prop;
    /// Timeout for automatic connection establishment (in unit of 10ms). Cancel
    /// the procedure if not all indicated devices have been connected when the
    /// timeout occurs. 0 means there is no timeout
    uint16_t conn_to;
    /// Scan window opening parameters for LE 1M PHY
    ble_gapm_scan_wd_op_param_t scan_param_1m;
    /// Scan window opening parameters for LE Coded PHY
    ble_gapm_scan_wd_op_param_t scan_param_coded;
    /// Connection parameters for LE 1M PHY
    ble_gapm_conn_param_t conn_param_1m;
    /// Connection parameters for LE 2M PHY
    ble_gapm_conn_param_t conn_param_2m;
    /// Connection parameters for LE Coded PHY
    ble_gapm_conn_param_t conn_param_coded;
    /// Address of peer device in case white list is not used for connection
    ble_gap_bdaddr_t peer_addr;
} ble_gapm_init_param_t;

/// Periodic advertising information
typedef struct {
    /// Advertiser address information
    ble_gap_bdaddr_t addr;
    /// Advertising SID
    uint8_t adv_sid;
} ble_gapm_period_adv_addr_t;

/// Periodic synchronization types
typedef enum {
    /// Do not use periodic advertiser list for synchronization. Use advertiser
    /// information provided in the ble_gapm_start_per_sync().
    BLE_GAPM_PER_SYNC_TYPE_GENERAL,
    /// Use periodic advertiser list for synchronization
    BLE_GAPM_PER_SYNC_TYPE_SELECTIVE,
    /// Use Periodic advertising sync transfer information send through
    /// connection for synchronization
    BLE_GAPM_PER_SYNC_TYPE_PAST,
} ble_gapm_per_sync_type_t;

/// Bit field of enabled advertising reports
typedef enum {
    /// Periodic advertising reports reception enabled
    BLE_GAPM_REPORT_ADV_EN_BIT = 0x01,
    BLE_GAPM_REPORT_ADV_EN_POS = 0,
    /// BIG Info advertising reports reception enabled
    BLE_GAPM_REPORT_BIGINFO_EN_BIT = 0x02,
    BLE_GAPM_REPORT_BIGINFO_EN_POS = 1,
    /// Periodic advertising reports reception enabled with duplicate filtering
    /// enabled.
    BLE_GAPM_REPORT_ADV_DUP_FIL_EN_BIT = 0x04,
    BLE_GAPM_REPORT_ADV_DUP_FIL_EN_POS = 2,
} ble_gapm_report_en_bf_t;

/// Constant Tone Extension sync filtering type
typedef enum {
    /// Do not sync to packets with an AoA Constant Tone Extension
    BLE_GAPM_CTE_NO_SYNC_WITH_AOA = (1 << 0),
    /// Do not sync to packets with an AoD Constant Tone Extension with 1 us
    /// slots
    BLE_GAPM_CTE_NO_SYNC_WITH_AOD_1US_SLOT = (1 << 1),
    /// Do not sync to packets with an AoD Constant Tone Extension with 2 us
    /// slots
    BLE_GAPM_CTE_NO_SYNC_WITH_AOD_2US_SLOT = (1 << 2),
    /// Do not sync to packets with a type 3 Constant Tone Extension (currently
    /// reserved for future use)
    BLE_GAPM_CTE_NO_SYNC_WITH_TYPE_3 = (1 << 3),
    /// Do not sync to packets without a Constant Tone Extension
    BLE_GAPM_CTE_NO_SYNC_WITHOUT_CTE = (1 << 4),
} ble_gapm_sync_cte_type_t;

/// Periodic synchronization parameters
typedef struct {
    /// Number of periodic advertising that can be skipped after a successful
    /// receive. Maximum authorized value is 499.
    uint16_t skip;
    /// Synchronization timeout for the periodic advertising (in unit of 10ms
    /// between 100ms and 163.84s)
    uint16_t sync_to;
    /// Periodic synchronization type
    ble_gapm_per_sync_type_t type;
    /// Connection index used for periodic sync info reception (only valid for
    /// BLE_GAPM_PER_SYNC_TYPE_PAST)
    uint8_t conidx;
    /// Address of advertiser with which synchronization has to be established
    /// (used only if type is BLE_GAPM_PER_SYNC_TYPE_GENERAL)
    ble_gapm_period_adv_addr_t adv_addr;
    /// Bit field that contains list of reports that are enabled or not.
    ble_gapm_report_en_bf_t report_en_bf;
    /// Type of Constant Tone Extension device should sync on.
    ble_gapm_sync_cte_type_t cte_type;
} ble_gapm_per_sync_param_t;

/// Type of activities that can be created
typedef enum {
    /// Advertising activity
    BLE_GAPM_ACTV_TYPE_ADV,
    /// Scanning activity
    BLE_GAPM_ACTV_TYPE_SCAN,
    /// Initiating activity
    BLE_GAPM_ACTV_TYPE_INIT,
    /// Periodic synchronization activity
    BLE_GAPM_ACTV_TYPE_PER_SYNC,
} ble_gapm_actv_type_t;

typedef struct {
    /// The maximum Tx payload size
    uint16_t max_tx_octets;
    /// The maximum Tx time (unit: us)
    uint16_t max_tx_time;
    /// The maximum Rx payload size
    uint16_t max_rx_octets;
    /// The maximum Rx time (unit: us)
    uint16_t max_rx_time;
} ble_gap_ind_pkt_size_t;

typedef struct {
    uint8_t conidx;
    ble_gap_ind_pkt_size_t param;
} ble_gap_ind_le_pkt_size_t;

typedef struct {
    /// Requested information
    ble_gap_dev_info_t req;
    /// Token value that must be returned in confirmation
    uint16_t token;
    /// Device name data offset
    uint16_t name_offset;
    /// Maximum name length (starting from offset)
    uint16_t max_name_length;
} ble_gap_ind_get_dev_info_req_t;

typedef struct {
    uint8_t conidx;
    ble_gap_ind_get_dev_info_req_t param;
} ble_gap_ind_le_get_dev_info_req_t;

typedef struct {
    /// Encryption status
    ble_err_code_t status;
    /// Pairing security level
    ble_gap_pairing_level_t pairing_lvl;
} ble_gap_ind_encrypt_t;

typedef struct {
    uint8_t conidx;
    ble_gap_ind_encrypt_t param;
} ble_gap_ind_le_encrypt_t;

typedef struct {
    /// Pairing security level
    ble_gap_pairing_level_t pairing_lvl;
} ble_gap_encrypt_t;

typedef struct {
    uint8_t conidx;
    ble_gap_encrypt_t param;
} ble_gap_le_encrypt_t;

typedef enum {
    /// Passkey entry started
    BLE_GAP_PASSKEY_ENTRY_STARTED,
    /// Passkey digit entered
    BLE_GAP_PASSKEY_DIGIT_ENTERED,
    /// Passkey digit erased
    BLE_GAP_PASSKEY_DIGIT_ERASED,
    /// Passkey cleared
    BLE_GAP_PASSKEY_CLEARED,
    /// Passkey entry completed
    BLE_GAP_PASSKEY_ENTRY_COMPLETED,
} ble_gap_notif_type_t;

typedef struct {
    /// Keypress notification type
    ble_gap_notif_type_t keypress_notif_type;
} ble_gap_ind_keypress_notif_t;

typedef struct {
    uint8_t conidx;
    ble_gap_ind_keypress_notif_t param;
} ble_gap_ind_le_keypress_notif_t;

typedef struct {
    /// Requested information
    ble_gap_dev_info_t req;
    /// Status of the confirmation
    ble_err_code_t status;
    /// Token value provided in request indication
    uint16_t token;
    /// Complete value length including offset
    uint16_t complete_length;
    union {
	/// Data for BLE_GAP_DEV_NAME request
	ble_gap_dev_name_t name;
	/// Data for BLE_GAP_DEV_APPEARANCE request
	uint16_t appearance;
	/// Data for BLE_GAP_DEV_PERIPH_PREF_PARAMS request
	ble_gap_periph_pref_t periph_pref;
	/// Data for BLE_GAP_DEV_CTL_ADDR_RESOL request
	uint8_t ctl_addr_resol;
	/// Data for BLE_GAP_DEV_DB_HASH request
	uint8_t hash[BLE_GAP_DATABASE_HASH_LEN];
	/// Data for BLE_GAP_DEV_RSLV_PRIV_ADDR_ONLY request
	/// 0: Only Resolvable Private Addresses will be used as local addresses
	/// after bonding; 1-255: Reserved for future use.
	uint8_t rslv_priv_addr_only;
    };
} ble_gap_get_dev_info_cfm_t;

typedef struct {
    /// Internally used
    uint8_t operation;
    /// Status of the request
    ble_err_code_t status;
    /// Connection index
    uint8_t con_idx;
    union {
	/// Data for operation (@see enum ble_gap_link_info_type_t)
	ble_gap_link_info_t info;
	/// Authentication. Data in callback of @ref ble_gap_sec_encrypt_req
	ble_gap_le_encrypt_t encrypt;
    };
} ble_gapc_cmpl_t;

typedef struct {
    /// Internally used
    uint8_t operation;
    /// Status of the request
    ble_err_code_t status;
    /// Extra data
    union {
	/// Random number. Data in callback of @ref ble_gapm_gen_rand.
	ble_rand_nb_t randnb;
	/// Resolved addr
	resl_addr_t resladr;
	/// Activity index. Data in callback of @ref ble_gapm_stop_activity
	uint8_t actv_stop_idx;
	/// Generated address which is returned data of
	/// @ref ble_gapm_gen_rand_addr
	ble_bdaddr_t randadr;
	/// Generated encrypted data which is returned data of
	/// @ref ble_gapm_enc_block
	uint8_t enc_result[BLE_GAP_KEY_LEN];
	/// Number of received packets. Data in callback of
	/// @ref ble_gapm_le_test_stop
	uint16_t nb_packet_received;
	/// Periodic sync activity index which is returned data of
	/// @ref ble_gapm_create_persync_activity
	uint8_t persync_act_idx;
	/// Scan activity index which is returned data of
	/// @ref ble_gapm_create_scan_activity
	uint8_t scan_act_idx;
	/// Initiating activity index which is returned data of
	/// @ref ble_gapm_create_init_activity
	uint8_t init_act_idx;
	/// ADV creation information. Data in callback of
	/// @ref ble_gapm_create_adv_activity.
	struct {
	    /// ADV identifier
	    uint8_t adv_idx;
	    /// Selected TX power for ADV activity
	    int8_t tx_pwr;
	} adv_created;
    };
} ble_gapm_cmpl_t;

/// Invalid connection index
#define BLE_GAP_INVALID_CONIDX 0xFF

/// GAP event
typedef enum {
    /// Indication of ADV stop
    BLE_GAP_EID_IND_ADV_STOP,
    /// Indication of Scan stop
    BLE_GAP_EID_IND_SCAN_STOP,
    /// Indication of initiating stop
    BLE_GAP_EID_IND_INIT_STOP,
    /// Indication of periodic sync stop
    BLE_GAP_EID_IND_PER_SYNC_STOP,
    /// Indication of extended advertising report
    BLE_GAP_EID_IND_EXT_ADV_REPORT,
    /// Indication of connection established
    BLE_GAP_EID_IND_CON_EST,
    /// Indication of connection disconnected
    BLE_GAP_EID_IND_DISCON,
    /// Indication of LTK
    BLE_GAP_EID_IND_LTK,
    /// Indication of connection parameter updated
    BLE_GAP_EID_IND_CON_UPDATE,
    /// Indication of set device information request
    BLE_GAP_EID_IND_SET_DEV_INFO_REQ,
    /// Pairing end
    BLE_GAP_EID_IND_PAIR_END,
    /// Indication of parameter updating request
    BLE_GAP_EID_IND_PARAM_UPDATE_REQ,
    /// Indication of LE PHY
    BLE_GAP_EID_IND_LE_PHY,
    /// Indication of channel selection algorithm
    BLE_GAP_EID_IND_CHAN_SEL_ALGO,
    /// Indication of LE packet size
    BLE_GAP_EID_IND_PKT_SIZE,
    /// Indication of getting device information request
    BLE_GAP_EID_IND_GET_DEV_INFO_REQ,
    /// Indication of encrypt
    BLE_GAP_EID_IND_ENCRYPT,
    /// Indicatin of repeated attempt
    BLE_GAP_EID_IND_REPEATED_ATTEMPT,
    /// Indicatin of keypress notification
    BLE_GAP_EID_IND_KEYPRESS_NOTIFICATION,
} ble_gap_eid_t;

typedef struct {
    /// Event ID
    ble_gap_eid_t eid;
    union {
	/// Data for BLE_GAP_EID_IND_ADV_STOP event.
	ble_gap_ind_stop_t ind_adv_stop;
	/// Data for BLE_GAP_EID_IND_SCAN_STOP event.
	ble_gap_ind_stop_t ind_scan_stop;
	/// Data for BLE_GAP_EID_IND_INIT_STOP event.
	ble_gap_ind_stop_t ind_init_stop;
	/// Data for BLE_GAP_EID_IND_PER_SYNC_STOP event.
	ble_gap_ind_stop_t ind_per_sync_stop;
	/// Data for BLE_GAP_EID_IND_EXT_ADV_REPORT event.
	ble_gap_ind_ext_adv_report_t ind_ext_adv_rpt;
	/// Data for BLE_GAP_EID_IND_CON_EST event.
	ble_gap_ind_le_con_est_t ind_con_est;
	/// Data for BLE_GAP_EID_IND_DISCON event.
	ble_gap_ind_le_discon_t ind_discon;
	/// Data for BLE_GAP_EID_IND_LTK event.
	ble_gap_ind_le_ltk_t ind_ltk;
	/// Data for BLE_GAP_EID_IND_CON_UPDATE event.
	ble_gap_ind_le_con_update_t ind_con_update;
	// Data for BLE_GAP_EID_IND_SET_DEV_INFO_REQ event.
	ble_gap_ind_le_set_dev_req_t ind_set_dev_req;
	/// Data for BLE_GAP_EDI_IND_PAIR_END
	ble_gap_ind_le_pair_end_t ind_pair_end;
	/// Data for BLE_GAP_EID_IND_PARAM_UPDATE_REQ event.
	ble_gap_ind_le_param_update_req_t ind_param_update_req;
	/// Data for BLE_GAP_EID_IND_LE_PHY event.
	ble_gap_ind_le_le_phy_t ind_le_phy;
	/// Data for BLE_GAP_EID_IND_CHAN_SEL_ALGO event.
	ble_gap_ind_le_chan_sel_algo_t ind_sel_algo;
	/// Data for BLE_GAP_EID_IND_PKT_SIZE event.
	ble_gap_ind_le_pkt_size_t ind_pkt_size;
	/// Data for BLE_GAP_EID_IND_GET_DEV_INFO_REQ event.
	ble_gap_ind_le_get_dev_info_req_t ind_get_dev_info_req;
	/// Data for BLE_GAP_EID_IND_ENCRYPT event.
	ble_gap_ind_le_encrypt_t ind_encrypt;
	/// Data for BLE_GAP_EID_IND_REPEATED_ATTEMPT event.
	ble_gap_ind_le_repeated_attempt_t ind_repeated_attempt;
	/// Data for BLE_GAP_EID_IND_KEYPRESS_NOTIFICATION event.
	ble_gap_ind_le_keypress_notif_t ind_keypress_notif;
    };
} ble_gap_evt_t;

/// Own BD address source of the device
typedef enum {
    /// Public or Private Static Address
    BLE_OWN_STATIC_ADDR,
    /// Generated resolvable private random address
    BLE_OWN_GEN_RSLV_ADDR,
    /// Generated non-resolvable private random address
    BLE_OWN_GEN_NON_RSLV_ADDR,
} ble_own_addr_t;

/// GAPC command complete callback prototype
/// @param[in] conidx Connection index.
/// @param[in] parm Parameter returned.
/// @param[in] ctx Context provided from calling API.
typedef void (*gapc_cmpl_cb)(uint8_t conidx, struct gapc_cmp_evt const *parm,
    void const *ctx);

/// GAPC message
/// @param[in] id message id
/// @param[in] conidx The connection index
/// @param[in] is_bcast Specify the message is broadcast or not
/// @param[in] parm message parameter
typedef void (*gapc_ind)(enum gapc_msg_id id, uint8_t conidx, bool is_bcast,
    void const *parm);

/// GAPM message callback
/// @param[in] id message id
/// @param[in] parm message parameter
typedef void (*gapm_ind)(enum gapm_msg_id id, void const *parm);

/// GAPM add profile callback prototype
/// @param[in] parm Parameter returned.
/// @param[in] ctx Context provided from calling API.
typedef void (*gapm_profile_add_cb)(void *parm, void const *ctx);

// FIXME: please remove comment below after coverting process is done.
// Coverting mapping for process of cleaning up violation of abstraction:
//| incument            | succeesor            |
//|=====================+======================|
//| gapc_cmpl_cb        | ble_gapc_cmd_cb      |
//| gapm_cmp_evt_t      | ble_gapm_cmpl_t      |
//| struct gapc_cmp_evt | ble_gapc_cmpl_t      |
//| ble_gapm_reg_ind    | ble_gap_evt_func_reg |
//| ble_gapc_reg_ind    | ble_gap_evt_func_reg |
//| gapc_ind            | ble_gap_evt_fn       |
//| gapm_ind            | ble_gap_evt_fn       |
// The incumbent functions(structs) would be totally replaced finally.

/// GAPC command complete callback prototype
/// @param[in] conidx Connection index.
/// @param[in] parm Parameter returned.
/// @param[in] ctx Context provided from calling API.
typedef void (*ble_gapc_cmd_cb)(uint8_t conidx, ble_gapc_cmpl_t const *parm,
    void const *ctx);

/// GAPM command complete callback prototype
/// @param[in] parm Parameter returned.
/// @param[in] ctx Context provided from calling API.
typedef void (*ble_gapm_cmd_cb)(ble_gapm_cmpl_t const *parm, void const *ctx);

/// GAP event callback function
/// @param[in] event Event parameter
/// @return True if processed. If return value is false, the event will be sent
/// to next registered function.
typedef bool (*ble_gap_evt_fn)(ble_gap_evt_t const *event);

/*
 * MACROS
 *******************************************************************************
 */

/// Invalid bond index
#define BLE_GAP_INVALID_BOND (-1)


/*
 * GLOBAL C API
 *******************************************************************************
 */
/**
 *******************************************************************************
 * @brief Register ble gap event callback
 *
 * @param[in] pri Priority for service order. 0 is the highest.
 * @param[in] name Name of this event func.
 * @param[in] fn Event function
 *******************************************************************************
 */
void ble_gap_evt_func_reg(uint8_t pri, ble_gap_evt_reg_name_t name,
    ble_gap_evt_fn fn);

/**
 *******************************************************************************
 * @brief Get gap event handler
 *
 * @param[in] name Name string for event handler.
 * @return Event function. NULL if not found.
 *******************************************************************************
 */
ble_gap_evt_fn ble_gap_evt_func_get(ble_gap_evt_reg_name_t name);

/**
 *******************************************************************************
 * @brief Set device configuration
 *
 * @param[in] cfg device configuration
 * @param[in] cb Complete callback function.
 * @param[in] ctx Context data.
 *******************************************************************************
 */
__NONNULL(1)
void ble_gapm_set_device_config(ble_gap_set_dev_config_t const *cfg,
    ble_gapm_cmd_cb cb, void const *ctx);

/**
 *******************************************************************************
 * @brief Remove gap event handler
 *
 * @param[in] name Name string for event handler.
 * @return Removed callback function. NULL if not found.
 *******************************************************************************
 */
ble_gap_evt_fn ble_gap_evt_func_rm(ble_gap_evt_reg_name_t name);

/**
 *******************************************************************************
 * @brief Generate random number
 *
 * @param[in] cb Complete callback function. The random number will be return in
 * randnb data field of ble_gapm_cmpl_t which is passed as a argument of cb.
 * @param[in] ctx Context data.
 *******************************************************************************
 */
void ble_gapm_gen_rand(ble_gapm_cmd_cb cb, void const *ctx);

/**
 *******************************************************************************
 * @brief Set content of the white list
 * @param[in] count Number of entries to be added in the list. 0 means that list
 * content has to be cleared.
 * @param[in] addrs List of entries to be added in the list.
 * @param[in] cb Command complete callback. NULL means that the caller does not
 * need to be informed after the command completion.
 * @param[in] ctx Context data.
 *******************************************************************************
 */
__NONNULL(2)
void ble_gapm_set_white_list(uint8_t count, ble_gap_bdaddr_t const addrs[],
    ble_gapm_cmd_cb cb, void const *ctx);

/**
 *******************************************************************************
 * @brief Set content of the resolving list
 * @param[in] count Number of entries to be added in the list. 0 means that list
 * content has to be cleared.
 * @param[in] rals List of entries to be added in the list.
 * @param[in] cb Command complete callback. NULL means that the caller does not
 * need to be informed after the command completion.
 * @param[in] ctx Context data.
 *******************************************************************************
 */
void ble_gapm_set_resolving_list(uint8_t count,
    ble_gap_ral_dev_info_t const rals[], ble_gapm_cmd_cb cb, void const *ctx);

/**
 *******************************************************************************
 * @brief Lower slave latency locally
 * @param[in] conidx Connection index.
 * @param[in] latency
 * @param[in] cb Command complete callback
 * @param[in] ctx Context data.
 *******************************************************************************
 */
void ble_gapc_lower_slave_latency_locally(uint8_t conidx, uint16_t latency,
    ble_gapc_cmd_cb cb, void const *ctx);

/**
 *******************************************************************************
 * @brief Set the preferred slave event duration (for slave role configuration)
 * @param[in] conidx Connection index.
 * @param[in] dur Preferred event duration that the controller should use on a
 * connection (N * 0.625 ms)
 * @param[in] single_tx Slave transmits a single packet per connection event
 * (False/True)
 * @param[in] cb Command complete callback
 * @param[in] ctx Context data.
 *******************************************************************************
 */
void ble_gapc_set_prefer_slave_event_duration(uint8_t conidx, uint16_t dur,
    bool single_tx, ble_gapc_cmd_cb cb, void const *ctx);

/**
 *******************************************************************************
 * @brief Start TX direct test mode
 * @param[in] param Test TX parameters.
 * @param[in] cb Command complete callback. NULL means that the caller does not
 * need to be informed after the command completion.
 *******************************************************************************
 */
__NONNULL(1)
void ble_gapm_le_test_tx(ble_gap_test_tx_param_t const *param,
    ble_gapm_cmd_cb cb);

/**
 *******************************************************************************
 * @brief Start RX direct test mode.
 * @param[in] param Test RX parameters.
 * @param[in] cb Command complete callback. NULL means that the caller does not
 * need to be informed after the command completion.
 *******************************************************************************
 */
__NONNULL(1)
void ble_gapm_le_test_rx(ble_gap_test_rx_param_t const *param,
    ble_gapm_cmd_cb cb);

/**
 *******************************************************************************
 * @brief Stop the direct test mode.
 * @param[in] cb Command complete callback.
 *******************************************************************************
 */
__NONNULL_ALL
void ble_gapm_le_test_stop(ble_gapm_cmd_cb cb);

/**
 *******************************************************************************
 * @brief Resolve provided random address using array of Identity Resolution Key
 * (IRK) exchanged.
 * @param[in] addr Resolvable random address to solve.
 * @param[in] nb Number of provided IRK.
 * @param[in] irkarr peer IRK
 * @param[in] cb Command complete callback.
 * @param[in] ctx Context data.
 *******************************************************************************
 */
void ble_gapm_resolv_addr(ble_bdaddr_t addr, uint8_t nb, ble_gap_irk_idx_t irkarr[],
    ble_gapm_cmd_cb cb, void const *ctx);

/**
 *******************************************************************************
 * @brief Generate random address
 * @param[in] addr_type random address type
 * @param[in] cb Command complete or indication callback.
 * @param[in] ctx Context data.
 *******************************************************************************
 */
void ble_gapm_gen_rand_addr(ble_gapm_rand_addr_type_t addr_type,
    ble_gapm_cmd_cb cb, void const *ctx);

/**
 *******************************************************************************
 * @brief Encrypt data
 * @param[in] key 128 bit operand (key)
 * @param[in] data 128 bit operand (data)
 * @param[in] cb Command complete callback.
 * @param[in] ctx Context data.
 *******************************************************************************
 */
void ble_gapm_enc_block(uint8_t const key[BLE_GAP_KEY_LEN],
    uint8_t const data[BLE_GAP_KEY_LEN], ble_gapm_cmd_cb cb, void const *ctx);

/**
 *******************************************************************************
 * @brief Stop an activity
 * @param[in] idx Activity identifier
 * @param[in] ctx Context data.
 * @param[in] cb Command complete callback.
 *******************************************************************************
 */
void ble_gapm_stop_activity(uint8_t idx, void const *ctx, ble_gapm_cmd_cb cb);

/**
 *******************************************************************************
 * @brief Delete activity
 * @param[in] idx Activity identifier.
 * @param[in] ctx Context data.
 * @param[in] cb Command complete callback.
 *******************************************************************************
 */
void ble_gapm_delete_activity(uint8_t idx, void const *ctx, ble_gapm_cmd_cb cb);

/**
 *******************************************************************************
 * @brief Set advertising data
 * @param[in] data_type  ADV data type
 * @param[in] idx Activity identifier.
 * @param[in] len Length of ADV data.
 * @param[in] data ADV data array.
 * @param[in] ctx Context data.
 * @param[in] cb Command complete callback.
 *****************************************************************************
 */
void ble_gapm_set_adv_data(ble_gapm_data_type_t data_type, uint8_t idx,
    uint16_t len, uint8_t const *data, void const *ctx, ble_gapm_cmd_cb cb);

/**
 *******************************************************************************
 * @brief Get link information
 * @param[in] info Operation correspond to information.
 * @param[in] conidx Connection index.
 * @param[in] cb Complete callback function. NULL means that the caller does not
 * need to be informed after the command completion.
 * @param[in] ctx Context data.
 *******************************************************************************
 */
void ble_gapc_get_link_info(uint8_t conidx, ble_gap_link_info_type_t info,
    ble_gapc_cmd_cb cb, void const *ctx);

/**
 *******************************************************************************
 * @brief Set phy. rate
 * @param[in] conidx Connection index.
 * @param[in] tx_phy Tx phy.
 * @param[in] rx_phy Rx phy.
 * @param[in] phy_opt Coded phy. opt.
 * @param[in] cb Complete callback function. NULL means that the caller does not
 * need to be informed after the command completion.
 * @param[in] ctx Context data.
 *******************************************************************************
 */
void ble_gapc_set_phy(uint8_t conidx, ble_gap_phy_mode_t tx_phy,
    ble_gap_phy_mode_t rx_phy, ble_gap_phy_opt_t phy_opt, ble_gapc_cmd_cb cb,
    void const *ctx);

/**
 *******************************************************************************
 * @brief Update connection parameters
 * @param[in] conidx Connection index.
 * @param[in] param Connection parameters.
 * @param[in] cb Complete callback function.
 * @param[in] ctx Context data.
 *******************************************************************************
 */
__NONNULL(2)
void ble_gapc_update_param(uint8_t conidx, ble_gap_conn_param_t const *param,
    ble_gapc_cmd_cb cb, void const *ctx);

/**
 *******************************************************************************
 * @brief Disconnect specific link
 * @param[in] conidx Connection index.
 * @param[in] reason Reason for disconnection. Only allow BLE_HCI_MODULE and
 * BLE_SMP_MODULE type.
 * @param[in] cb Complete callback function.
 * @param[in] ctx Context data.
 *******************************************************************************
 */
void ble_gapc_disconnect(uint8_t conidx, ble_err_code_t reason, ble_gapc_cmd_cb cb,
    void const *ctx);

/**
 *******************************************************************************
 * @brief Set device IRK
 * @param[in] irk IRK array data. Size is BLE_GAP_KEY_LEN(16).
 * @param[in] cb Complete callback function.
 * @param[in] ctx Context data.
 *******************************************************************************
 */
__NONNULL(1)
void ble_gapm_set_irk(uint8_t const irk[BLE_GAP_KEY_LEN], ble_gapm_cmd_cb cb,
    void const *ctx);

/**
 *******************************************************************************
 * @brief Set device information confirmation (corresponding to
 * BLE_GAP_EID_IND_SET_DEV_INFO_REQ event)
 * @param[in] conidx Connection index.
 * @param[in] req_type Request type
 * @param[in] status Status of the request
 * @param[in] token Token value
 *******************************************************************************
 */
void ble_gapc_set_dev_info_cfm(uint8_t conidx, ble_gap_dev_info_t req_type,
    ble_err_code_t status, uint16_t token);

/**
 *******************************************************************************
 * @brief Retrieve connection address information
 * @param[in] conidx Connection index
 * @param[out] addr Pointer to peer addr.
 * @return True if connection exists.
 *******************************************************************************
 */
__NONNULL(2)
bool ble_gapc_get_peer_addr(uint8_t conidx, ble_gap_bdaddr_t *addr);

/**
 *******************************************************************************
 * @brief Confirm reception of BLE_GAP_EID_IND_PARAM_UPDATE_REQ event
 * @param[in] conidx Connection index.
 * @param[in] param Confirmation parameters.
 *******************************************************************************
 */
__NONNULL(2)
void ble_gapc_param_update_cfm(uint8_t conidx,
    ble_gapc_param_update_cfm_t const *param);

/**
 *******************************************************************************
 * @brief Confirm reception of BLE_GAP_EID_IND_GET_DEV_INFO_REQ event
 * @param[in] conidx Connection index.
 * @param[in] param Confirmation parameters.
 *******************************************************************************
 */
__NONNULL(2)
void ble_gapc_get_dev_info_cfm(uint8_t conidx,
    ble_gap_get_dev_info_cfm_t const *param);

/**
 *******************************************************************************
 * @brief Create periodic sync activity
 * @param[in] own_addr_type Own address type
 * @param[in] ctx Context data.
 * @param[in] cb Command complete callback.
 *******************************************************************************
 */
__NONNULL(3)
void ble_gapm_create_persync_activity(ble_own_addr_t own_addr_type,
    void const *ctx, ble_gapm_cmd_cb cb);

/**
 *******************************************************************************
 * @brief Create scan activity
 * @param[in] own_addr_type Own address type
 * @param[in] ctx Context data.
 * @param[in] cb Command complete callback.
 *******************************************************************************
 */
__NONNULL(3)
void ble_gapm_create_scan_activity(ble_own_addr_t own_addr_type,
    void const *ctx, ble_gapm_cmd_cb cb);

/**
 *******************************************************************************
 * @brief Create initiating activity
 * @param[in] own_addr_type Own address type
 * @param[in] ctx Context data.
 * @param[in] cb Command complete callback.
 *******************************************************************************
 */
__NONNULL(3)
void ble_gapm_create_init_activity(ble_own_addr_t own_addr_type,
    void const *ctx, ble_gapm_cmd_cb cb);

/**
 *******************************************************************************
 * @brief Start advertising
 * @param[in] idx Activity identifier.
 * @param[in] dur Advertising duration (in unit of 10ms). 0 means that
 * advertising continues until the application stop actively.
 * @param[in] max_evt  Maximum number of extended advertising events the
 * controller shall attempt to send prior to terminating the extending
 * advertising. Valid only if extended advertising.
 * @param[in] ctx Context data.
 * @param[in] cb Command complete callback. NULL means that the caller does not
 * need to be informed after the command completion.
 *******************************************************************************
 */
void ble_gapm_start_adv(uint8_t idx, uint16_t dur, uint8_t max_evt,
    void const *ctx, ble_gapm_cmd_cb cb);

/**
 *******************************************************************************
 * @brief Start scanning
 * @param[in] idx Activity identifier.
 * @param[in] param Scanning parameters.
 * @param[in] ctx Context data.
 * @param[in] cb Command complete callback. NULL means that the caller does not
 * need to be informed after the command completion.
 *******************************************************************************
 */
__NONNULL(2)
void ble_gapm_start_scan(uint8_t idx, ble_gapm_scan_param_t const *param,
    void const *ctx, ble_gapm_cmd_cb cb);

/**
 *******************************************************************************
 * @brief Start Initiating
 * @param[in] idx Activity identifier.
 * @param[in] param Initiating parameters.
 * @param[in] ctx Context data.
 * @param[in] cb Command complete callback. NULL means that the caller does not
 * need to be informed after the command completion.
 *******************************************************************************
 */
__NONNULL(2)
void ble_gapm_start_init(uint8_t idx, ble_gapm_init_param_t const *param,
    void const *ctx, ble_gapm_cmd_cb cb);

/**
 *******************************************************************************
 * @brief Start periodic synchronization
 * @param[in] idx Activity identifier.
 * @param[in] param Periodic synchronization parameters.
 * @param[in] ctx Context data.
 * @param[in] cb Command complete callback. NULL means that the caller does not
 * need to be informed after the command completion.
 *******************************************************************************
 */
__NONNULL(2)
void ble_gapm_start_per_sync(uint8_t idx,
    ble_gapm_per_sync_param_t const *param, void const *ctx,
    ble_gapm_cmd_cb cb);

/**
 *******************************************************************************
 * @brief Create ADV activity
 * @param[in] own_addr_type Own address type
 * @param[in] param Advertising parameters for advertising creation.
 * @param[in] ctx Context data
 * @param[in] cb Command complete callback.
 *******************************************************************************
 */
__NONNULL(2)
void ble_gapm_create_adv_activity(ble_own_addr_t own_addr_type,
    ble_gapm_adv_create_param_t const *param, void const *ctx,
    ble_gapm_cmd_cb cb);

/**
 *******************************************************************************
 * @brief Get advertising create parameters from Flash NVDS.
 * @param[in] is_wurx True if the adv is wurx.
 * @param[in,out] own_addr_type Own addr type.
 * @param[in,out] create Create parameter
 * @return true for success
 *******************************************************************************
 */
__NONNULL_ALL
bool ble_gapm_get_adv_create_nvds(bool is_wurx, ble_own_addr_t *own_addr_type,
    ble_gapm_adv_create_param_t *create);

#ifdef __cplusplus
}
#endif

///@} ATM_BTFM_BLE_GAP