1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
114
117
118
119
120
124
125
126
127
128
129
130
131
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
173
174
175
176
177
178
179
180
181
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
225
226
227
228
229
230
231
232
233
234
235
236
237
242
243
244
245
246
247
248
249
250
251
252
253
254
260
261
262
263
264
265
266
267
268
269
270
271
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
357
358
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
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
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
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
555
556
557
558
559
560
561
562
563
564
565
566
567
568
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
634
635
640
641
646
647
652
653
654
655
658
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
743
744
755
756
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
808
809
810
811
812
813
814
815
816
817
818
826
827
830
831
832
833
834
835
836
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
934
935
936
940
941
946
947
948
949
950
951
952
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
991
1003
1004
1005
1006
1007
1008
1009
1012
1013
1016
1017
1018
1019
1020
1021
1022
1032
1033
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
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1100
1105
1110
1115
1118
1119
1120
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
1165
1166
1167
1168
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
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
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
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
1307
1308
1309
1310
1311
1312
1313
1314
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1386
1389
1390
1391
1392
1393
1394
1395
1397
1403
1404
1405
1406
1407
1408
1409
1410
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1477
1478
1479
1483
1484
1487
1488
1489
1490
1491
1505
1506
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
1551
1552
1553
1554
1557
1558
1559
1568
1569
1570
1571
1572
1573
1574
1578
1579
1580
1587
1591
1592
1593
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
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1651
1652
1653
1654
1655
1656
1657
1661
1662
1666
1667
1668
1669
1681
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1721
1722
1723
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1757
1758
1759
1760
1761
1762
1763
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1821
1822
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1840
1841
1842
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
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
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1934
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
1968
1969
1970
1971
1972
1973
1974
1975
1976
1981
1982
1983
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2003
2004
2005
2013
2014
2015
2016
2017
2018
2019
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2059
2060
2061
2062
2063
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
2105
2106
2121
2122
2123
2124
2125
2126
2127
2129
2130
2131
2132
2133
2134
2135
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2155
2156
2157
2158
2159
2165
2166
2167
2168
2169
2170
2171
2180
2181
2182
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2202
2206
2207
2208
2209
2210
2214
2215
2219
2220
2221
2222
2223
2224
2225
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2243
2244
2254
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2273
2274
2281
2282
2283
2284
2285
2286
2291
2292
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2337
2338
2339
2340
2341
2342
2343
2347
2348
2349
2350
2351
2352
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2526
2527
2531
2532
2533
2534
2535
2539
2540
2541
2542
2543
2544
2545
2546
2547
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2665
2668
2669
2670
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2706
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2861
2862
2863
2864
2865
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2907
2908
2909
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2958
2959
2960
2961
2962
2963
2964
2970
2971
/* ... */
/* ... */
#include "common/bt_target.h"
#include <string.h>
#include "stack/bt_types.h"
#include "stack/hcimsgs.h"
#include "stack/btu.h"
#include "btm_int.h"
#include "stack/btm_ble_api.h"
#include "stack/smp_api.h"
#include "l2c_int.h"
#include "stack/gap_api.h"
#include "device/controller.h"11 includes
#if BLE_INCLUDED == TRUE
extern void BTM_UpdateAddrInfor(uint8_t addr_type, BD_ADDR bda);
#if SMP_INCLUDED == TRUE
static BOOLEAN temp_enhanced = FALSE;
extern BOOLEAN aes_cipher_msg_auth_code(BT_OCTET16 key, UINT8 *input, UINT16 length,
UINT16 tlen, UINT8 *p_signature);
extern void smp_link_encrypted(BD_ADDR bda, UINT8 encr_enable);
extern BOOLEAN smp_proc_ltk_request(BD_ADDR bda);/* ... */
#endif
extern void gatt_notify_enc_cmpl(BD_ADDR bd_addr);
/* ... */
#if (SMP_INCLUDED == TRUE)
BOOLEAN BTM_SecAddBleDevice (BD_ADDR bd_addr, BD_NAME bd_name, tBT_DEVICE_TYPE dev_type,
tBLE_ADDR_TYPE addr_type, UINT32 auth_mode)
{
tBTM_SEC_DEV_REC *p_dev_rec;
tBTM_INQ_INFO *p_info = NULL;
BTM_TRACE_DEBUG ("BTM_SecAddBleDevice dev_type=0x%x", dev_type);
p_dev_rec = btm_find_dev (bd_addr);
if (!p_dev_rec) {
BTM_TRACE_DEBUG("Add a new device");
/* ... */
if (list_length(btm_cb.p_sec_dev_rec_list) < BTM_SEC_MAX_DEVICE_RECORDS) {
p_dev_rec = (tBTM_SEC_DEV_REC *)osi_malloc(sizeof(tBTM_SEC_DEV_REC));
if(p_dev_rec) {
list_append(btm_cb.p_sec_dev_rec_list, p_dev_rec);
BTM_TRACE_DEBUG ("allocate a new dev rec idx=0x%x\n", list_length(btm_cb.p_sec_dev_rec_list));
memset (p_dev_rec, 0, sizeof (tBTM_SEC_DEV_REC));
p_dev_rec->sec_flags = BTM_SEC_IN_USE;
memcpy (p_dev_rec->bd_addr, bd_addr, BD_ADDR_LEN);
p_dev_rec->hci_handle = BTM_GetHCIConnHandle (bd_addr, BT_TRANSPORT_BR_EDR);
p_dev_rec->ble_hci_handle = BTM_GetHCIConnHandle (bd_addr, BT_TRANSPORT_LE);
p_dev_rec->conn_params.min_conn_int =
p_dev_rec->conn_params.max_conn_int =
p_dev_rec->conn_params.supervision_tout =
p_dev_rec->conn_params.slave_latency = BTM_BLE_CONN_PARAM_UNDEF;
BTM_TRACE_DEBUG ("hci_handl=0x%x ", p_dev_rec->ble_hci_handle );
}{...}
}{...}
if (!p_dev_rec) {
return (FALSE);
}{...}
}{...} else {
BTM_TRACE_DEBUG("Device already exist");
}{...}
memset(p_dev_rec->sec_bd_name, 0, sizeof(tBTM_BD_NAME));
if (bd_name && bd_name[0]) {
p_dev_rec->sec_flags |= BTM_SEC_NAME_KNOWN;
BCM_STRNCPY_S ((char *)p_dev_rec->sec_bd_name, (char *)bd_name, BTM_MAX_REM_BD_NAME_LEN);
}{...}
p_dev_rec->device_type |= dev_type;
p_dev_rec->ble.ble_addr_type = addr_type;
p_dev_rec->ble.auth_mode = auth_mode;
memcpy (p_dev_rec->ble.pseudo_addr, bd_addr, BD_ADDR_LEN);
p_info = BTM_InqDbRead(bd_addr);
if (p_info) {
p_info->results.ble_addr_type = p_dev_rec->ble.ble_addr_type ;
p_info->results.device_type = p_dev_rec->device_type;
BTM_TRACE_DEBUG ("InqDb device_type =0x%x addr_type=0x%x",
p_info->results.device_type, p_info->results.ble_addr_type);
}{...}
return (TRUE);
}{ ... }
/* ... */#endif
/* ... */
#if SMP_INCLUDED == TRUE
BOOLEAN BTM_SecAddBleKey (BD_ADDR bd_addr, tBTM_LE_KEY_VALUE *p_le_key, tBTM_LE_KEY_TYPE key_type)
{
tBTM_SEC_DEV_REC *p_dev_rec;
BTM_TRACE_DEBUG ("BTM_SecAddBleKey");
p_dev_rec = btm_find_dev (bd_addr);
if (!p_dev_rec || !p_le_key ||
(key_type != BTM_LE_KEY_PENC && key_type != BTM_LE_KEY_PID &&
key_type != BTM_LE_KEY_PCSRK && key_type != BTM_LE_KEY_LENC &&
key_type != BTM_LE_KEY_LCSRK && key_type != BTM_LE_KEY_LID)) {
BTM_TRACE_WARNING ("BTM_SecAddBleKey() Wrong Type, or No Device record \
for bdaddr: %08x%04x, Type: %d",
(bd_addr[0] << 24) + (bd_addr[1] << 16) + (bd_addr[2] << 8) + bd_addr[3],
(bd_addr[4] << 8) + bd_addr[5], key_type);
return (FALSE);
}{...}
BTM_TRACE_DEBUG ("BTM_SecAddLeKey() BDA: %08x%04x, Type: 0x%02x",
(bd_addr[0] << 24) + (bd_addr[1] << 16) + (bd_addr[2] << 8) + bd_addr[3],
(bd_addr[4] << 8) + bd_addr[5], key_type);
btm_sec_save_le_key (bd_addr, key_type, p_le_key, FALSE);
#if (BLE_PRIVACY_SPT == TRUE)
if (key_type == BTM_LE_KEY_PID || key_type == BTM_LE_KEY_LID) {
btm_ble_resolving_list_load_dev (p_dev_rec);
}{...}
#endif/* ... */
return (TRUE);
}{ ... }
/* ... */#endif
/* ... */
void BTM_BleLoadLocalKeys(UINT8 key_type, tBTM_BLE_LOCAL_KEYS *p_key)
{
tBTM_DEVCB *p_devcb = &btm_cb.devcb;
BTM_TRACE_DEBUG ("%s", __func__);
if (p_key != NULL) {
switch (key_type) {
case BTM_BLE_KEY_TYPE_ID:
memcpy(&p_devcb->id_keys, &p_key->id_keys, sizeof(tBTM_BLE_LOCAL_ID_KEYS));
break;
...
case BTM_BLE_KEY_TYPE_ER:
memcpy(p_devcb->ble_encryption_key_value, p_key->er, sizeof(BT_OCTET16));
break;
...
default:
BTM_TRACE_ERROR("unknown local key type: %d", key_type);
break;...
}{...}
}{...}
}{ ... }
/* ... */
void BTM_GetDeviceEncRoot (BT_OCTET16 ble_encr_key_value)
{
BTM_TRACE_DEBUG ("%s", __func__);
memcpy (ble_encr_key_value, btm_cb.devcb.ble_encryption_key_value, BT_OCTET16_LEN);
}{ ... }
/* ... */
void BTM_GetDeviceIDRoot (BT_OCTET16 irk)
{
BTM_TRACE_DEBUG ("BTM_GetDeviceIDRoot ");
memcpy (irk, btm_cb.devcb.id_keys.irk, BT_OCTET16_LEN);
}{ ... }
/* ... */
void BTM_GetDeviceDHK (BT_OCTET16 dhk)
{
BTM_TRACE_DEBUG ("BTM_GetDeviceDHK");
memcpy (dhk, btm_cb.devcb.id_keys.dhk, BT_OCTET16_LEN);
}{ ... }
/* ... */
void BTM_ReadConnectionAddr (BD_ADDR remote_bda, BD_ADDR local_conn_addr, tBLE_ADDR_TYPE *p_addr_type)
{
tACL_CONN *p_acl = btm_bda_to_acl(remote_bda, BT_TRANSPORT_LE);
if (p_acl == NULL) {
BTM_TRACE_ERROR("No connection exist!");
return;
}{...}
memcpy(local_conn_addr, p_acl->conn_addr, BD_ADDR_LEN);
* p_addr_type = p_acl->conn_addr_type;
BTM_TRACE_DEBUG ("BTM_ReadConnectionAddr address type: %d addr: 0x%02x",
p_acl->conn_addr_type, p_acl->conn_addr[0]);
}{ ... }
/* ... */
BOOLEAN BTM_IsBleConnection (UINT16 conn_handle)
{
#if (BLE_INCLUDED == TRUE)
tACL_CONN *p;
BTM_TRACE_API ("BTM_IsBleConnection: conn_handle: %d", conn_handle);
p = btm_handle_to_acl(conn_handle);
if (!p) {
return FALSE;
}{...}
return (p->transport == BT_TRANSPORT_LE);/* ... */
#else
return FALSE;
#endif
}{ ... }
/* ... */
BOOLEAN BTM_ReadRemoteConnectionAddr(BD_ADDR pseudo_addr, BD_ADDR conn_addr,
tBLE_ADDR_TYPE *p_addr_type)
{
BOOLEAN st = TRUE;
#if (BLE_PRIVACY_SPT == TRUE)
tACL_CONN *p = btm_bda_to_acl (pseudo_addr, BT_TRANSPORT_LE);
if (p == NULL) {
BTM_TRACE_ERROR("BTM_ReadRemoteConnectionAddr can not find connection"
" with matching address");
return FALSE;
}{...}
memcpy(conn_addr, p->active_remote_addr, BD_ADDR_LEN);
*p_addr_type = p->active_remote_addr_type;/* ... */
#else
tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev(pseudo_addr);
memcpy(conn_addr, pseudo_addr, BD_ADDR_LEN);
if (p_dev_rec != NULL) {
*p_addr_type = p_dev_rec->ble.ble_addr_type;
}{...}
#endif/* ... */
return st;
}{ ... }
/* ... */
void BTM_SecurityGrant(BD_ADDR bd_addr, UINT8 res)
{
#if SMP_INCLUDED == TRUE
tSMP_STATUS res_smp = (res == BTM_SUCCESS) ? SMP_SUCCESS : SMP_REPEATED_ATTEMPTS;
BTM_TRACE_DEBUG ("BTM_SecurityGrant");
SMP_SecurityGrant(bd_addr, res_smp);/* ... */
#endif
}{ ... }
/* ... */
void BTM_BlePasskeyReply (BD_ADDR bd_addr, UINT8 res, UINT32 passkey)
{
#if SMP_INCLUDED == TRUE
tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev (bd_addr);
tSMP_STATUS res_smp = (res == BTM_SUCCESS) ? SMP_SUCCESS : SMP_PASSKEY_ENTRY_FAIL;
if (p_dev_rec == NULL) {
BTM_TRACE_ERROR("Passkey reply to Unknown device");
return;
}{...}
p_dev_rec->sec_flags |= BTM_SEC_LE_AUTHENTICATED;
BTM_TRACE_DEBUG ("BTM_BlePasskeyReply");
SMP_PasskeyReply(bd_addr, res_smp, passkey);/* ... */
#endif
}{ ... }
/* ... */
void BTM_BleSetStaticPasskey(BOOLEAN add, UINT32 passkey)
{
#if SMP_INCLUDED == TRUE
SMP_SetStaticPasskey(add, passkey);
#endif
}{ ... }
/* ... */
#if (SMP_INCLUDED == TRUE)
void BTM_BleConfirmReply (BD_ADDR bd_addr, UINT8 res)
{
tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev (bd_addr);
tSMP_STATUS res_smp = (res == BTM_SUCCESS) ? SMP_SUCCESS : SMP_PASSKEY_ENTRY_FAIL;
if (p_dev_rec == NULL) {
BTM_TRACE_ERROR("Passkey reply to Unknown device");
return;
}{...}
p_dev_rec->sec_flags |= BTM_SEC_LE_AUTHENTICATED;
BTM_TRACE_DEBUG ("%s\n", __func__);
SMP_ConfirmReply(bd_addr, res_smp);
}{ ... }
/* ... */#endif
/* ... */
void BTM_BleOobDataReply(BD_ADDR bd_addr, UINT8 res, UINT8 len, UINT8 *p_data)
{
#if SMP_INCLUDED == TRUE
tSMP_STATUS res_smp = (res == BTM_SUCCESS) ? SMP_SUCCESS : SMP_OOB_FAIL;
tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev (bd_addr);
BTM_TRACE_DEBUG ("BTM_BleOobDataReply");
if (p_dev_rec == NULL) {
BTM_TRACE_ERROR("BTM_BleOobDataReply() to Unknown device");
return;
}{...}
p_dev_rec->sec_flags |= BTM_SEC_LE_AUTHENTICATED;
SMP_OobDataReply(bd_addr, res_smp, len, p_data);/* ... */
#endif
}{ ... }
/* ... */
void BTM_BleSecureConnectionOobDataReply(BD_ADDR bd_addr, UINT8 *p_c, UINT8 *p_r)
{
#if SMP_INCLUDED == TRUE
tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev (bd_addr);
BTM_TRACE_DEBUG ("%s", __func__);
if (p_dev_rec == NULL) {
BTM_TRACE_ERROR("%s Unknown device", __func__);
return;
}{...}
p_dev_rec->sec_flags |= BTM_SEC_LE_AUTHENTICATED;
tSMP_SC_OOB_DATA oob;
memset(&oob, 0, sizeof(tSMP_SC_OOB_DATA));
oob.peer_oob_data.present = true;
memcpy(&oob.peer_oob_data.commitment, p_c, BT_OCTET16_LEN);
memcpy(&oob.peer_oob_data.randomizer, p_r, BT_OCTET16_LEN);
oob.peer_oob_data.addr_rcvd_from.type = p_dev_rec->ble.ble_addr_type;
memcpy(oob.peer_oob_data.addr_rcvd_from.bda, bd_addr, BD_ADDR_LEN);
SMP_SecureConnectionOobDataReply((UINT8 *)&oob);/* ... */
#endif
}{ ... }
/* ... */
void BTM_BleSecureConnectionCreateOobData(void)
{
#if SMP_INCLUDED == TRUE
BTM_TRACE_DEBUG ("%s", __func__);
SMP_CreateLocalSecureConnectionsOobData();/* ... */
#endif
}{ ... }
/* ... */
void BTM_BleSetConnScanParams (UINT32 scan_interval, UINT32 scan_window)
{
#if SMP_INCLUDED == TRUE
tBTM_BLE_CB *p_ble_cb = &btm_cb.ble_ctr_cb;
BOOLEAN new_param = FALSE;
if (BTM_BLE_ISVALID_PARAM(scan_interval, BTM_BLE_SCAN_INT_MIN, BTM_BLE_SCAN_INT_MAX) &&
BTM_BLE_ISVALID_PARAM(scan_window, BTM_BLE_SCAN_WIN_MIN, BTM_BLE_SCAN_WIN_MAX)) {
if (p_ble_cb->scan_int != scan_interval) {
p_ble_cb->scan_int = scan_interval;
new_param = TRUE;
}{...}
if (p_ble_cb->scan_win != scan_window) {
p_ble_cb->scan_win = scan_window;
new_param = TRUE;
}{...}
if (new_param && p_ble_cb->conn_state == BLE_BG_CONN) {
btm_ble_suspend_bg_conn();
}{...}
}{...} else {
BTM_TRACE_ERROR("Illegal Connection Scan Parameters");
}{...}
/* ... */#endif
}{ ... }
/* ... */
void BTM_BleSetPrefConnParams (BD_ADDR bd_addr,
UINT16 min_conn_int, UINT16 max_conn_int,
UINT16 slave_latency, UINT16 supervision_tout)
{
tBTM_SEC_DEV_REC *p_dev_rec = btm_find_or_alloc_dev (bd_addr);
BTM_TRACE_API ("BTM_BleSetPrefConnParams min: %u max: %u latency: %u \
tout: %u",
min_conn_int, max_conn_int, slave_latency, supervision_tout);
if (BTM_BLE_ISVALID_PARAM(min_conn_int, BTM_BLE_CONN_INT_MIN, BTM_BLE_CONN_INT_MAX) &&
BTM_BLE_ISVALID_PARAM(max_conn_int, BTM_BLE_CONN_INT_MIN, BTM_BLE_CONN_INT_MAX) &&
BTM_BLE_ISVALID_PARAM(supervision_tout, BTM_BLE_CONN_SUP_TOUT_MIN, BTM_BLE_CONN_SUP_TOUT_MAX) &&
(slave_latency <= BTM_BLE_CONN_LATENCY_MAX || slave_latency == BTM_BLE_CONN_PARAM_UNDEF)) {
if (p_dev_rec) {
if (min_conn_int != BTM_BLE_CONN_PARAM_UNDEF || max_conn_int != BTM_BLE_CONN_PARAM_UNDEF) {
if (min_conn_int != BTM_BLE_CONN_PARAM_UNDEF) {
p_dev_rec->conn_params.min_conn_int = min_conn_int;
}{...} else {
p_dev_rec->conn_params.min_conn_int = max_conn_int;
}{...}
if (max_conn_int != BTM_BLE_CONN_PARAM_UNDEF) {
p_dev_rec->conn_params.max_conn_int = max_conn_int;
}{...} else {
p_dev_rec->conn_params.max_conn_int = min_conn_int;
}{...}
if (slave_latency != BTM_BLE_CONN_PARAM_UNDEF) {
p_dev_rec->conn_params.slave_latency = slave_latency;
}{...} else {
p_dev_rec->conn_params.slave_latency = BTM_BLE_CONN_SLAVE_LATENCY_DEF;
}{...}
if (supervision_tout != BTM_BLE_CONN_PARAM_UNDEF) {
p_dev_rec->conn_params.supervision_tout = supervision_tout;
}{...} else {
p_dev_rec->conn_params.supervision_tout = BTM_BLE_CONN_TIMEOUT_DEF;
}{...}
}{...}
}{...} else {
BTM_TRACE_ERROR("Unknown Device, setting rejected");
}{...}
}{...} else {
BTM_TRACE_ERROR("Illegal Connection Parameters");
}{...}
}{ ... }
/* ... */
void BTM_ReadDevInfo (BD_ADDR remote_bda, tBT_DEVICE_TYPE *p_dev_type, tBLE_ADDR_TYPE *p_addr_type)
{
tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev (remote_bda);
tBTM_INQ_INFO *p_inq_info = BTM_InqDbRead(remote_bda);
tBLE_ADDR_TYPE temp_addr_type = (*p_addr_type);
*p_addr_type = BLE_ADDR_PUBLIC;
if (!p_dev_rec) {
*p_dev_type = BT_DEVICE_TYPE_BREDR;
if (p_inq_info != NULL) {
*p_dev_type = p_inq_info->results.device_type ;
*p_addr_type = p_inq_info->results.ble_addr_type;
}{...} else {
if(temp_addr_type <= BLE_ADDR_TYPE_MAX) {
*p_addr_type = temp_addr_type;
}{...} else {
BTM_TRACE_DEBUG ("btm_find_dev_type - unknown device, BR/EDR assumed");
}{...}
}{...}
}{...} else {
if (p_inq_info) {
p_dev_rec->device_type = p_inq_info->results.device_type;
p_dev_rec->ble.ble_addr_type = p_inq_info->results.ble_addr_type;
}{...}
if (memcmp(p_dev_rec->bd_addr, remote_bda, BD_ADDR_LEN) == 0 &&
memcmp(p_dev_rec->ble.pseudo_addr, remote_bda, BD_ADDR_LEN) == 0) {
*p_dev_type = p_dev_rec->device_type;
*p_addr_type = p_dev_rec->ble.ble_addr_type;
}{...} else if (memcmp(p_dev_rec->ble.pseudo_addr, remote_bda, BD_ADDR_LEN) == 0) {
*p_dev_type = BT_DEVICE_TYPE_BLE;
*p_addr_type = p_dev_rec->ble.ble_addr_type;
}{...} else {
*p_dev_type = BT_DEVICE_TYPE_BREDR;
*p_addr_type = BLE_ADDR_PUBLIC;
}{...}
}{...}
BTM_TRACE_DEBUG ("btm_find_dev_type - device_type = %d addr_type = %d", *p_dev_type , *p_addr_type);
}{ ... }
#endif/* ... */
/* ... */
BOOLEAN BTM_ReadConnectedTransportAddress(BD_ADDR remote_bda, tBT_TRANSPORT transport)
{
tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev(remote_bda);
if (p_dev_rec == NULL) {
memset(remote_bda, 0, BD_ADDR_LEN);
return FALSE;
}{...}
if (transport == BT_TRANSPORT_BR_EDR) {
if (btm_bda_to_acl(p_dev_rec->bd_addr, transport) != NULL) {
memcpy(remote_bda, p_dev_rec->bd_addr, BD_ADDR_LEN);
return TRUE;
}{...} else if (p_dev_rec->device_type & BT_DEVICE_TYPE_BREDR) {
memcpy(remote_bda, p_dev_rec->bd_addr, BD_ADDR_LEN);
}{...} else {
memset(remote_bda, 0, BD_ADDR_LEN);
}{...}
return FALSE;
}{...}
#if (BLE_INCLUDED == TRUE)
if (transport == BT_TRANSPORT_LE) {
memcpy(remote_bda, p_dev_rec->ble.pseudo_addr, BD_ADDR_LEN);
if (btm_bda_to_acl(p_dev_rec->ble.pseudo_addr, transport) != NULL) {
return TRUE;
}{...} else {
return FALSE;
}{...}
}{...}
#endif/* ... */
return FALSE;
}{ ... }
#if (BLE_INCLUDED == TRUE)
/* ... */
void BTM_BleReceiverTest(UINT8 rx_freq, tBTM_CMPL_CB *p_cmd_cmpl_cback)
{
btm_cb.devcb.p_le_test_cmd_cmpl_cb = p_cmd_cmpl_cback;
if (btsnd_hcic_ble_receiver_test(rx_freq) == FALSE) {
BTM_TRACE_ERROR("%s: Unable to Trigger LE receiver test", __FUNCTION__);
}{...}
}{ ... }
/* ... */
void BTM_BleTransmitterTest(UINT8 tx_freq, UINT8 test_data_len,
UINT8 packet_payload, tBTM_CMPL_CB *p_cmd_cmpl_cback)
{
btm_cb.devcb.p_le_test_cmd_cmpl_cb = p_cmd_cmpl_cback;
if (btsnd_hcic_ble_transmitter_test(tx_freq, test_data_len, packet_payload) == FALSE) {
BTM_TRACE_ERROR("%s: Unable to Trigger LE transmitter test", __FUNCTION__);
}{...}
}{ ... }
/* ... */
void BTM_BleTestEnd(tBTM_CMPL_CB *p_cmd_cmpl_cback)
{
btm_cb.devcb.p_le_test_cmd_cmpl_cb = p_cmd_cmpl_cback;
if (btsnd_hcic_ble_test_end() == FALSE) {
BTM_TRACE_ERROR("%s: Unable to End the LE TX/RX test", __FUNCTION__);
}{...}
}{ ... }
/* ... */
void btm_ble_test_command_complete(UINT8 *p)
{
tBTM_CMPL_CB *p_cb = btm_cb.devcb.p_le_test_cmd_cmpl_cb;
btm_cb.devcb.p_le_test_cmd_cmpl_cb = NULL;
if (p_cb) {
(*p_cb)(p);
}{...}
}{ ... }
#if (BLE_50_FEATURE_SUPPORT == TRUE)
/* ... */
void BTM_BleEnhancedReceiverTest(UINT8 rx_freq, UINT8 phy, UINT8 modulation_index, tBTM_CMPL_CB *p_cmd_cmpl_cback)
{
btm_cb.devcb.p_le_test_cmd_cmpl_cb = p_cmd_cmpl_cback;
if (btsnd_hcic_ble_enhand_rx_test(rx_freq, phy, modulation_index) == FALSE) {
BTM_TRACE_ERROR("%s: Unable to Trigger LE enhanced receiver test", __FUNCTION__);
}{...}
}{...}
/* ... */
void BTM_BleEnhancedTransmitterTest(UINT8 tx_freq, UINT8 test_data_len,
UINT8 packet_payload, UINT8 phy, tBTM_CMPL_CB *p_cmd_cmpl_cback)
{
btm_cb.devcb.p_le_test_cmd_cmpl_cb = p_cmd_cmpl_cback;
if (btsnd_hcic_ble_enhand_tx_test(tx_freq, test_data_len, packet_payload, phy) == FALSE) {
BTM_TRACE_ERROR("%s: Unable to Trigger LE enhanced transmitter test", __FUNCTION__);
}{...}
}{...}
/* ... */#endif
/* ... */
BOOLEAN BTM_UseLeLink (BD_ADDR bd_addr)
{
tACL_CONN *p;
tBT_DEVICE_TYPE dev_type;
tBLE_ADDR_TYPE addr_type = 0;
BOOLEAN use_le = FALSE;
if ((p = btm_bda_to_acl(bd_addr, BT_TRANSPORT_BR_EDR)) != NULL) {
return use_le;
}{...} else if ((p = btm_bda_to_acl(bd_addr, BT_TRANSPORT_LE)) != NULL) {
use_le = TRUE;
}{...} else {
BTM_ReadDevInfo(bd_addr, &dev_type, &addr_type);
use_le = (dev_type == BT_DEVICE_TYPE_BLE);
}{...}
return use_le;
}{ ... }
/* ... */
tBTM_STATUS BTM_SetBleDataLength(BD_ADDR bd_addr, UINT16 tx_pdu_length)
{
tACL_CONN *p_acl = btm_bda_to_acl(bd_addr, BT_TRANSPORT_LE);
BTM_TRACE_DEBUG("%s: tx_pdu_length =%d", __FUNCTION__, tx_pdu_length);
if (!controller_get_interface()->supports_ble_packet_extension()) {
BTM_TRACE_ERROR("%s failed, request not supported", __FUNCTION__);
return BTM_CONTROL_LE_DATA_LEN_UNSUPPORTED;
}{...}
if (p_acl != NULL) {
if (!HCI_LE_DATA_LEN_EXT_SUPPORTED(p_acl->peer_le_features)) {
BTM_TRACE_ERROR("%s failed, peer does not support request", __FUNCTION__);
return BTM_PEER_LE_DATA_LEN_UNSUPPORTED;
}{...}
if (tx_pdu_length > BTM_BLE_DATA_SIZE_MAX) {
tx_pdu_length = BTM_BLE_DATA_SIZE_MAX;
}{...} else if (tx_pdu_length < BTM_BLE_DATA_SIZE_MIN) {
tx_pdu_length = BTM_BLE_DATA_SIZE_MIN;
}{...}
btsnd_hcic_ble_set_data_length(p_acl->hci_handle, tx_pdu_length,
BTM_BLE_DATA_TX_TIME_MAX);
return BTM_SUCCESS;
}{...} else {
BTM_TRACE_ERROR("%s: Wrong mode: no LE link exist or LE not supported", __FUNCTION__);
return BTM_WRONG_MODE;
}{...}
}{ ... }
#if (SMP_INCLUDED == TRUE)
/* ... */
tBTM_SEC_ACTION btm_ble_determine_security_act(BOOLEAN is_originator, BD_ADDR bdaddr, UINT16 security_required)
{
tBTM_LE_AUTH_REQ auth_req = 0x00;
if (is_originator)
{
if ((security_required & BTM_SEC_OUT_FLAGS) == 0 &&
(security_required & BTM_SEC_OUT_MITM) == 0)
{
BTM_TRACE_DEBUG ("%s No security required for outgoing connection", __func__);
return BTM_SEC_OK;
}{...}
if (security_required & BTM_SEC_OUT_MITM) {
auth_req |= BTM_LE_AUTH_REQ_MITM;
}{...}
}{...}
else
{
if ((security_required & BTM_SEC_IN_FLAGS) == 0&& (security_required & BTM_SEC_IN_MITM) == 0)
{
BTM_TRACE_DEBUG ("%s No security required for incoming connection", __func__);
return BTM_SEC_OK;
}{...}
if (security_required & BTM_SEC_IN_MITM) {
auth_req |= BTM_LE_AUTH_REQ_MITM;
}{...}
}{...}
tBTM_BLE_SEC_REQ_ACT ble_sec_act = BTM_BLE_SEC_REQ_ACT_NONE;
btm_ble_link_sec_check(bdaddr, auth_req, &ble_sec_act);
BTM_TRACE_DEBUG ("%s ble_sec_act %d", __func__ , ble_sec_act);
if (ble_sec_act == BTM_BLE_SEC_REQ_ACT_DISCARD) {
return BTM_SEC_ENC_PENDING;
}{...}
if (ble_sec_act == BTM_BLE_SEC_REQ_ACT_NONE) {
return BTM_SEC_OK;
}{...}
UINT8 sec_flag = 0;
BTM_GetSecurityFlagsByTransport(bdaddr, &sec_flag, BT_TRANSPORT_LE);
BOOLEAN is_link_encrypted = FALSE;
BOOLEAN is_key_mitm = FALSE;
if (sec_flag & (BTM_SEC_FLAG_ENCRYPTED| BTM_SEC_FLAG_LKEY_KNOWN))
{
if (sec_flag & BTM_SEC_FLAG_ENCRYPTED) {
is_link_encrypted = TRUE;
}{...}
if (sec_flag & BTM_SEC_FLAG_LKEY_AUTHED) {
is_key_mitm = TRUE;
}{...}
}{...}
if (auth_req & BTM_LE_AUTH_REQ_MITM)
{
if (!is_key_mitm)
{
return BTM_SEC_ENCRYPT_MITM;
}{...} else {
if (is_link_encrypted) {
return BTM_SEC_OK;
}{...} else {
return BTM_SEC_ENCRYPT;
}{...}
}{...}
}{...} else {
if (is_link_encrypted) {
return BTM_SEC_OK;
}{...} else {
return BTM_SEC_ENCRYPT_NO_MITM;
}{...}
}{...}
return BTM_SEC_OK;
}{ ... }
/* ... */
BOOLEAN btm_ble_start_sec_check(BD_ADDR bd_addr, UINT16 psm, BOOLEAN is_originator,
tBTM_SEC_CALLBACK *p_callback, void *p_ref_data)
{
tBTM_SEC_SERV_REC *p_serv_rec = btm_sec_find_first_serv (is_originator, psm);
if (!p_serv_rec)
{
BTM_TRACE_WARNING ("%s PSM: %d no application registered", __func__, psm);
(*p_callback) (bd_addr, BT_TRANSPORT_LE, p_ref_data, BTM_MODE_UNSUPPORTED);
return FALSE;
}{...}
tBTM_SEC_ACTION sec_act = btm_ble_determine_security_act(is_originator,
bd_addr, p_serv_rec->security_flags);
tBTM_BLE_SEC_ACT ble_sec_act = BTM_BLE_SEC_NONE;
BOOLEAN status = FALSE;
switch (sec_act)
{
case BTM_SEC_OK:
BTM_TRACE_DEBUG ("%s Security met", __func__);
p_callback(bd_addr, BT_TRANSPORT_LE, p_ref_data, BTM_SUCCESS);
status = TRUE;
break;
...
case BTM_SEC_ENCRYPT:
BTM_TRACE_DEBUG ("%s Encryption needs to be done", __func__);
ble_sec_act = BTM_BLE_SEC_ENCRYPT;
break;
...
case BTM_SEC_ENCRYPT_MITM:
BTM_TRACE_DEBUG ("%s Pairing with MITM needs to be done", __func__);
ble_sec_act = BTM_BLE_SEC_ENCRYPT_MITM;
break;
...
case BTM_SEC_ENCRYPT_NO_MITM:
BTM_TRACE_DEBUG ("%s Pairing with No MITM needs to be done", __func__);
ble_sec_act = BTM_BLE_SEC_ENCRYPT_NO_MITM;
break;
...
case BTM_SEC_ENC_PENDING:
BTM_TRACE_DEBUG ("%s Ecryption pending", __func__);
break;...
}{...}
if (ble_sec_act == BTM_BLE_SEC_NONE) {
return status;
}{...}
tL2C_LCB *p_lcb = l2cu_find_lcb_by_bd_addr(bd_addr, BT_TRANSPORT_LE);
p_lcb->sec_act = sec_act;
BTM_SetEncryption(bd_addr, BT_TRANSPORT_LE, p_callback, p_ref_data);
return FALSE;
}{ ... }
/* ... */
void btm_ble_rand_enc_complete (UINT8 *p, UINT16 op_code, tBTM_RAND_ENC_CB *p_enc_cplt_cback)
{
tBTM_RAND_ENC params;
UINT8 *p_dest = params.param_buf;
BTM_TRACE_DEBUG ("btm_ble_rand_enc_complete");
memset(¶ms, 0, sizeof(tBTM_RAND_ENC));
if (p_enc_cplt_cback && p) {
STREAM_TO_UINT8(params.status, p);
if (params.status == HCI_SUCCESS) {
params.opcode = op_code;
if (op_code == HCI_BLE_RAND) {
params.param_len = BT_OCTET8_LEN;
}{...} else {
params.param_len = BT_OCTET16_LEN;
}{...}
memcpy(p_dest, p, params.param_len);
}{...}
if (p_enc_cplt_cback) {
(*p_enc_cplt_cback)(¶ms);
}{...}
}{...}
}{ ... }
#endif/* ... */
/* ... */
#if (SMP_INCLUDED == TRUE)
void btm_ble_increment_sign_ctr(BD_ADDR bd_addr, BOOLEAN is_local )
{
tBTM_SEC_DEV_REC *p_dev_rec;
BTM_TRACE_DEBUG ("btm_ble_increment_sign_ctr is_local=%d", is_local);
if ((p_dev_rec = btm_find_dev (bd_addr)) != NULL) {
if (is_local) {
p_dev_rec->ble.keys.local_counter++;
}{...} else {
p_dev_rec->ble.keys.counter++;
}{...}
BTM_TRACE_DEBUG ("is_local=%d local sign counter=%d peer sign counter=%d",
is_local,
p_dev_rec->ble.keys.local_counter,
p_dev_rec->ble.keys.counter);
}{...}
}{ ... }
/* ... */#endif /* ... */
#endif
/* ... */
#if (SMP_INCLUDED == TRUE)
#if (BLE_INCLUDED == TRUE)
BOOLEAN btm_ble_get_enc_key_type(BD_ADDR bd_addr, UINT8 *p_key_types)
{
tBTM_SEC_DEV_REC *p_dev_rec;
BTM_TRACE_DEBUG ("btm_ble_get_enc_key_type");
if ((p_dev_rec = btm_find_dev (bd_addr)) != NULL) {
*p_key_types = p_dev_rec->ble.key_type;
return TRUE;
}{...}
return FALSE;
}{ ... }
/* ... */
BOOLEAN btm_get_local_div (BD_ADDR bd_addr, UINT16 *p_div)
{
tBTM_SEC_DEV_REC *p_dev_rec;
BOOLEAN status = FALSE;
BTM_TRACE_DEBUG ("btm_get_local_div");
BTM_TRACE_DEBUG("bd_addr:%02x-%02x-%02x-%02x-%02x-%02x",
bd_addr[0], bd_addr[1],
bd_addr[2], bd_addr[3],
bd_addr[4], bd_addr[5]);
*p_div = 0;
p_dev_rec = btm_find_dev (bd_addr);
if (p_dev_rec && p_dev_rec->ble.keys.div) {
status = TRUE;
*p_div = p_dev_rec->ble.keys.div;
}{...}
BTM_TRACE_DEBUG ("btm_get_local_div status=%d (1-OK) DIV=0x%x", status, *p_div);
return status;
}{ ... }
/* ... */
void btm_sec_save_le_key(BD_ADDR bd_addr, tBTM_LE_KEY_TYPE key_type, tBTM_LE_KEY_VALUE *p_keys,
BOOLEAN pass_to_application)
{
tBTM_SEC_DEV_REC *p_rec;
tBTM_LE_EVT_DATA cb_data;
UINT8 i;
BTM_TRACE_DEBUG ("btm_sec_save_le_key key_type=0x%x pass_to_application=%d", key_type, pass_to_application);
BTM_TRACE_DEBUG("bd_addr:%02x-%02x-%02x-%02x-%02x-%02x",
bd_addr[0], bd_addr[1],
bd_addr[2], bd_addr[3],
bd_addr[4], bd_addr[5]);
if ((p_rec = btm_find_dev (bd_addr)) != NULL && (p_keys || key_type == BTM_LE_KEY_LID)) {
btm_ble_init_pseudo_addr (p_rec, bd_addr);
switch (key_type) {
case BTM_LE_KEY_PENC:
memcpy(p_rec->ble.keys.pltk, p_keys->penc_key.ltk, BT_OCTET16_LEN);
memcpy(p_rec->ble.keys.rand, p_keys->penc_key.rand, BT_OCTET8_LEN);
p_rec->ble.keys.sec_level = p_keys->penc_key.sec_level;
p_rec->ble.keys.ediv = p_keys->penc_key.ediv;
p_rec->ble.keys.key_size = p_keys->penc_key.key_size;
p_rec->ble.key_type |= BTM_LE_KEY_PENC;
p_rec->sec_flags |= BTM_SEC_LE_LINK_KEY_KNOWN;
if (p_keys->penc_key.sec_level == SMP_SEC_AUTHENTICATED) {
p_rec->sec_flags |= BTM_SEC_LE_LINK_KEY_AUTHED;
}{...} else {
p_rec->sec_flags &= ~BTM_SEC_LE_LINK_KEY_AUTHED;
}{...}
BTM_TRACE_DEBUG("BTM_LE_KEY_PENC key_type=0x%x sec_flags=0x%x sec_leve=0x%x",
p_rec->ble.key_type,
p_rec->sec_flags,
p_rec->ble.keys.sec_level);
break;
...
case BTM_LE_KEY_PID:
for (i = 0; i < BT_OCTET16_LEN; i++) {
p_rec->ble.keys.irk[i] = p_keys->pid_key.irk[i];
}{...}
memcpy(p_rec->ble.static_addr, p_keys->pid_key.static_addr, BD_ADDR_LEN);
p_rec->ble.static_addr_type = p_keys->pid_key.addr_type;
p_rec->ble.key_type |= BTM_LE_KEY_PID;
BTM_TRACE_DEBUG("BTM_LE_KEY_PID key_type=0x%x save peer IRK", p_rec->ble.key_type);
memcpy(p_rec->bd_addr, p_keys->pid_key.static_addr, BD_ADDR_LEN);
btm_consolidate_dev(p_rec);
break;
...
case BTM_LE_KEY_PCSRK:
memcpy(p_rec->ble.keys.pcsrk, p_keys->pcsrk_key.csrk, BT_OCTET16_LEN);
p_rec->ble.keys.srk_sec_level = p_keys->pcsrk_key.sec_level;
p_rec->ble.keys.counter = p_keys->pcsrk_key.counter;
p_rec->ble.key_type |= BTM_LE_KEY_PCSRK;
p_rec->sec_flags |= BTM_SEC_LE_LINK_KEY_KNOWN;
if ( p_keys->pcsrk_key.sec_level == SMP_SEC_AUTHENTICATED) {
p_rec->sec_flags |= BTM_SEC_LE_LINK_KEY_AUTHED;
}{...} else {
p_rec->sec_flags &= ~BTM_SEC_LE_LINK_KEY_AUTHED;
}{...}
BTM_TRACE_DEBUG("BTM_LE_KEY_PCSRK key_type=0x%x sec_flags=0x%x sec_level=0x%x peer_counter=%d",
p_rec->ble.key_type,
p_rec->sec_flags,
p_rec->ble.keys.srk_sec_level,
p_rec->ble.keys.counter );
break;
...
case BTM_LE_KEY_LENC:
memcpy(p_rec->ble.keys.lltk, p_keys->lenc_key.ltk, BT_OCTET16_LEN);
p_rec->ble.keys.div = p_keys->lenc_key.div;
p_rec->ble.keys.sec_level = p_keys->lenc_key.sec_level;
p_rec->ble.keys.key_size = p_keys->lenc_key.key_size;
p_rec->ble.key_type |= BTM_LE_KEY_LENC;
p_rec->sec_flags |= BTM_SEC_LE_LINK_KEY_KNOWN;
if ( p_keys->lenc_key.sec_level == SMP_SEC_AUTHENTICATED) {
p_rec->sec_flags |= BTM_SEC_LE_LINK_KEY_AUTHED;
}{...} else {
p_rec->sec_flags &= ~BTM_SEC_LE_LINK_KEY_AUTHED;
}{...}
BTM_TRACE_DEBUG("BTM_LE_KEY_LENC key_type=0x%x DIV=0x%x key_size=0x%x sec_level=0x%x",
p_rec->ble.key_type,
p_rec->ble.keys.div,
p_rec->ble.keys.key_size,
p_rec->ble.keys.sec_level );
break;
...
case BTM_LE_KEY_LCSRK:
memcpy (p_rec->ble.keys.lcsrk, p_keys->lcsrk_key.csrk, BT_OCTET16_LEN);
p_rec->ble.keys.div = p_keys->lcsrk_key.div;
p_rec->ble.keys.local_csrk_sec_level = p_keys->lcsrk_key.sec_level;
p_rec->ble.keys.local_counter = p_keys->lcsrk_key.counter;
p_rec->ble.key_type |= BTM_LE_KEY_LCSRK;
BTM_TRACE_DEBUG("BTM_LE_KEY_LCSRK key_type=0x%x DIV=0x%x scrk_sec_level=0x%x local_counter=%d",
p_rec->ble.key_type,
p_rec->ble.keys.div,
p_rec->ble.keys.local_csrk_sec_level,
p_rec->ble.keys.local_counter );
break;
...
case BTM_LE_KEY_LID:
p_rec->ble.key_type |= BTM_LE_KEY_LID;
break;...
default:
BTM_TRACE_WARNING("btm_sec_save_le_key (Bad key_type 0x%02x)", key_type);
return;...
}{...}
BTM_TRACE_DEBUG ("BLE key type 0x%02x updated for BDA: %08x%04x (btm_sec_save_le_key)", key_type,
(bd_addr[0] << 24) + (bd_addr[1] << 16) + (bd_addr[2] << 8) + bd_addr[3],
(bd_addr[4] << 8) + bd_addr[5]);
/* ... */
if (pass_to_application && btm_cb.api.p_le_callback) {
cb_data.key.p_key_value = p_keys;
cb_data.key.key_type = key_type;
(*btm_cb.api.p_le_callback) (BTM_LE_KEY_EVT, bd_addr, &cb_data);
}{...}
return;
}{...}
BTM_TRACE_WARNING ("BLE key type 0x%02x called for Unknown BDA or type: %08x%04x !! (btm_sec_save_le_key)", key_type,
(bd_addr[0] << 24) + (bd_addr[1] << 16) + (bd_addr[2] << 8) + bd_addr[3],
(bd_addr[4] << 8) + bd_addr[5]);
if (p_rec) {
BTM_TRACE_DEBUG ("sec_flags=0x%x", p_rec->sec_flags);
}{...}
}{ ... }
/* ... */
void btm_ble_update_sec_key_size(BD_ADDR bd_addr, UINT8 enc_key_size)
{
tBTM_SEC_DEV_REC *p_rec;
BTM_TRACE_DEBUG("btm_ble_update_sec_key_size enc_key_size = %d", enc_key_size);
if ((p_rec = btm_find_dev (bd_addr)) != NULL ) {
p_rec->enc_key_size = enc_key_size;
}{...}
}{ ... }
/* ... */
UINT8 btm_ble_read_sec_key_size(BD_ADDR bd_addr)
{
tBTM_SEC_DEV_REC *p_rec;
if ((p_rec = btm_find_dev (bd_addr)) != NULL ) {
return p_rec->enc_key_size;
}{...} else {
return 0;
}{...}
return 0;
}{ ... }
/* ... */
void btm_ble_link_sec_check(BD_ADDR bd_addr, tBTM_LE_AUTH_REQ auth_req, tBTM_BLE_SEC_REQ_ACT *p_sec_req_act)
{
tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev (bd_addr);
UINT8 req_sec_level = BTM_LE_SEC_NONE, cur_sec_level = BTM_LE_SEC_NONE;
BTM_TRACE_DEBUG ("btm_ble_link_sec_check auth_req =0x%x", auth_req);
if (p_dev_rec == NULL) {
BTM_TRACE_ERROR ("btm_ble_link_sec_check received for unknown device");
return;
}{...}
if (p_dev_rec->sec_state == BTM_SEC_STATE_ENCRYPTING ||
p_dev_rec->sec_state == BTM_SEC_STATE_AUTHENTICATING) {
*p_sec_req_act = BTM_BLE_SEC_REQ_ACT_DISCARD;
}{...} else {
req_sec_level = BTM_LE_SEC_UNAUTHENTICATE;
if (auth_req & BTM_LE_AUTH_REQ_MITM) {
req_sec_level = BTM_LE_SEC_AUTHENTICATED;
}{...}
BTM_TRACE_DEBUG ("dev_rec sec_flags=0x%x", p_dev_rec->sec_flags);
if (p_dev_rec->sec_flags & BTM_SEC_LE_ENCRYPTED) {
if (p_dev_rec->sec_flags & BTM_SEC_LE_AUTHENTICATED) {
cur_sec_level = BTM_LE_SEC_AUTHENTICATED;
}{...} else {
cur_sec_level = BTM_LE_SEC_UNAUTHENTICATE;
}{...}
}{...} else {
if (p_dev_rec->ble.key_type & BTM_LE_KEY_PENC) {
cur_sec_level = p_dev_rec->ble.keys.sec_level;
}{...} else {
cur_sec_level = BTM_LE_SEC_NONE;
}{...}
}{...}
if (cur_sec_level >= req_sec_level) {
*p_sec_req_act = BTM_BLE_SEC_REQ_ACT_ENCRYPT;
}{...} else {
*p_sec_req_act = BTM_BLE_SEC_REQ_ACT_PAIR;
}{...}
}{...}
BTM_TRACE_DEBUG("cur_sec_level=%d req_sec_level=%d sec_req_act=%d",
cur_sec_level,
req_sec_level,
*p_sec_req_act);
}{ ... }
#endif/* ... */ /* ... */
#endif
#if (BLE_INCLUDED == TRUE)
/* ... */
tBTM_STATUS btm_ble_set_encryption (BD_ADDR bd_addr, void *p_ref_data, UINT8 link_role)
{
tBTM_STATUS cmd = BTM_NO_RESOURCES;
#if (SMP_INCLUDED == TRUE)
tBTM_BLE_SEC_ACT sec_act = *(tBTM_BLE_SEC_ACT *)p_ref_data ;
tBTM_SEC_DEV_REC *p_rec = btm_find_dev (bd_addr);
tBTM_BLE_SEC_REQ_ACT sec_req_act;
tBTM_LE_AUTH_REQ auth_req;
if (p_rec == NULL) {
BTM_TRACE_WARNING ("btm_ble_set_encryption (NULL device record!! sec_act=0x%x", sec_act);
return (BTM_WRONG_MODE);
}{...}
BTM_TRACE_DEBUG ("btm_ble_set_encryption sec_act=0x%x role_master=%d", sec_act, p_rec->role_master);
if (sec_act == BTM_BLE_SEC_ENCRYPT_MITM) {
p_rec->security_required |= BTM_SEC_IN_MITM;
}{...}
switch (sec_act) {
case BTM_BLE_SEC_ENCRYPT:
if (link_role == BTM_ROLE_MASTER && (p_rec->ble.key_type & BTM_LE_KEY_PENC)) {
cmd = btm_ble_start_encrypt(bd_addr, FALSE, NULL);
break;
}{...}
/* ... */
... case BTM_BLE_SEC_ENCRYPT_NO_MITM:
case BTM_BLE_SEC_ENCRYPT_MITM:
if ((link_role == BTM_ROLE_MASTER) && (sec_act != BTM_BLE_SEC_ENCRYPT)) {
auth_req = (sec_act == BTM_BLE_SEC_ENCRYPT_NO_MITM)
? SMP_AUTH_GEN_BOND : (SMP_AUTH_GEN_BOND | SMP_AUTH_YN_BIT);
btm_ble_link_sec_check (bd_addr, auth_req, &sec_req_act);
if (sec_req_act == BTM_BLE_SEC_REQ_ACT_ENCRYPT) {
cmd = btm_ble_start_encrypt(bd_addr, FALSE, NULL);
break;
}{...}
}{...}
#if (SMP_SLAVE_CON_PARAMS_UPD_ENABLE == TRUE)
if(link_role == BTM_ROLE_SLAVE && (p_rec->ble.key_type & BTM_LE_KEY_PENC)) {
p_rec->ble.skip_update_conn_param = true;
}{...} else {
p_rec->ble.skip_update_conn_param = false;
}{...}
#endif/* ... */
if (SMP_Pair(bd_addr) == SMP_STARTED) {
cmd = BTM_CMD_STARTED;
p_rec->sec_state = BTM_SEC_STATE_AUTHENTICATING;
}{...}
break;
...
default:
cmd = BTM_WRONG_MODE;
break;...
}{...}
/* ... */#endif
return cmd;
}{ ... }
/* ... */
#if (SMP_INCLUDED == TRUE)
void btm_ble_ltk_request(UINT16 handle, UINT8 rand[8], UINT16 ediv)
{
tBTM_CB *p_cb = &btm_cb;
tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev_by_handle (handle);
BT_OCTET16 dummy_stk = {0};
BTM_TRACE_DEBUG ("btm_ble_ltk_request");
p_cb->ediv = ediv;
memcpy(p_cb->enc_rand, rand, BT_OCTET8_LEN);
if (p_dev_rec != NULL) {
if (!smp_proc_ltk_request(p_dev_rec->bd_addr)) {
btm_ble_ltk_request_reply(p_dev_rec->bd_addr, FALSE, dummy_stk);
}{...}
}{...}
}{ ... }
/* ... */#endif
/* ... */
tBTM_STATUS btm_ble_start_encrypt(BD_ADDR bda, BOOLEAN use_stk, BT_OCTET16 stk)
{
#if (SMP_INCLUDED == TRUE)
tBTM_CB *p_cb = &btm_cb;
tBTM_SEC_DEV_REC *p_rec = btm_find_dev (bda);
BT_OCTET8 dummy_rand = {0};/* ... */
#endif
tBTM_STATUS rt = BTM_NO_RESOURCES;
#if (SMP_INCLUDED == TRUE)
BTM_TRACE_DEBUG ("btm_ble_start_encrypt");
if (!p_rec ) {
BTM_TRACE_ERROR("Link is not active, can not encrypt!");
return BTM_WRONG_MODE;
}{...}
if (p_rec->sec_state == BTM_SEC_STATE_ENCRYPTING) {
BTM_TRACE_WARNING("Link Encryption is active, Busy!");
return BTM_BUSY;
}{...}
p_cb->enc_handle = p_rec->ble_hci_handle;
if (use_stk) {
if (btsnd_hcic_ble_start_enc(p_rec->ble_hci_handle, dummy_rand, 0, stk)) {
rt = BTM_CMD_STARTED;
}{...}
}{...} else if (p_rec->ble.key_type & BTM_LE_KEY_PENC) {
if (btsnd_hcic_ble_start_enc(p_rec->ble_hci_handle, p_rec->ble.keys.rand,
p_rec->ble.keys.ediv, p_rec->ble.keys.pltk)) {
rt = BTM_CMD_STARTED;
}{...}
}{...} else {
BTM_TRACE_ERROR("No key available to encrypt the link");
}{...}
if (rt == BTM_CMD_STARTED) {
if (p_rec->sec_state == BTM_SEC_STATE_IDLE) {
p_rec->sec_state = BTM_SEC_STATE_ENCRYPTING;
}{...}
}{...}
#endif/* ... */
return rt;
}{ ... }
/* ... */
#if (SMP_INCLUDED == TRUE)
void btm_ble_link_encrypted(BD_ADDR bd_addr, UINT8 encr_enable)
{
tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev (bd_addr);
BOOLEAN enc_cback;
if (!p_dev_rec) {
BTM_TRACE_WARNING ("btm_ble_link_encrypted (No Device Found!) encr_enable=%d", encr_enable);
return;
}{...}
BTM_TRACE_DEBUG ("btm_ble_link_encrypted encr_enable=%d", encr_enable);
enc_cback = (p_dev_rec->sec_state == BTM_SEC_STATE_ENCRYPTING);
smp_link_encrypted(bd_addr, encr_enable);
BTM_TRACE_DEBUG(" p_dev_rec->sec_flags=0x%x", p_dev_rec->sec_flags);
if (encr_enable && p_dev_rec->enc_key_size == 0) {
p_dev_rec->enc_key_size = p_dev_rec->ble.keys.key_size;
}{...}
p_dev_rec->sec_state = BTM_SEC_STATE_IDLE;
if (p_dev_rec->p_callback && enc_cback) {
if (encr_enable) {
btm_sec_dev_rec_cback_event(p_dev_rec, BTM_SUCCESS, TRUE);
}{...} else if (p_dev_rec->role_master) {
btm_sec_dev_rec_cback_event(p_dev_rec, BTM_ERR_PROCESSING, TRUE);
}{...}
}{...}
gatt_notify_enc_cmpl(p_dev_rec->ble.pseudo_addr);
}{ ... }
/* ... */#endif
/* ... */
#if (SMP_INCLUDED == TRUE)
void btm_ble_ltk_request_reply(BD_ADDR bda, BOOLEAN use_stk, BT_OCTET16 stk)
{
tBTM_SEC_DEV_REC *p_rec = btm_find_dev (bda);
tBTM_CB *p_cb = &btm_cb;
if (p_rec == NULL) {
BTM_TRACE_ERROR("btm_ble_ltk_request_reply received for unknown device");
return;
}{...}
BTM_TRACE_DEBUG ("btm_ble_ltk_request_reply");
p_cb->enc_handle = p_rec->ble_hci_handle;
p_cb->key_size = p_rec->ble.keys.key_size;
BTM_TRACE_DEBUG("key size = %d", p_rec->ble.keys.key_size);
if (use_stk) {
btsnd_hcic_ble_ltk_req_reply(btm_cb.enc_handle, stk);
}{...} else {
if (p_rec->ble.key_type & BTM_LE_KEY_LENC) {
btsnd_hcic_ble_ltk_req_reply(btm_cb.enc_handle, p_rec->ble.keys.lltk);
}{...} else {
btsnd_hcic_ble_ltk_req_neg_reply(btm_cb.enc_handle);
}{...}
}{...}
}{ ... }
/* ... */
UINT8 btm_ble_io_capabilities_req(tBTM_SEC_DEV_REC *p_dev_rec, tBTM_LE_IO_REQ *p_data)
{
UINT8 callback_rc = BTM_SUCCESS;
BTM_TRACE_DEBUG ("btm_ble_io_capabilities_req");
if (btm_cb.api.p_le_callback) {
callback_rc = (*btm_cb.api.p_le_callback) (BTM_LE_IO_REQ_EVT, p_dev_rec->bd_addr, (tBTM_LE_EVT_DATA *)p_data);
}{...}
#if BTM_OOB_INCLUDED == TRUE
if ((callback_rc == BTM_SUCCESS) || (BTM_OOB_UNKNOWN != p_data->oob_data))
#else
if (callback_rc == BTM_SUCCESS)
#endif
{
#if BTM_BLE_CONFORMANCE_TESTING == TRUE
if (btm_cb.devcb.keep_rfu_in_auth_req) {
BTM_TRACE_DEBUG ("btm_ble_io_capabilities_req keep_rfu_in_auth_req = %u",
btm_cb.devcb.keep_rfu_in_auth_req);
p_data->auth_req &= BTM_LE_AUTH_REQ_MASK_KEEP_RFU;
btm_cb.devcb.keep_rfu_in_auth_req = FALSE;
}{...} else {
p_data->auth_req &= BTM_LE_AUTH_REQ_MASK;
}{...}
#else/* ... */
p_data->auth_req &= BTM_LE_AUTH_REQ_MASK;
#endif
BTM_TRACE_DEBUG ("btm_ble_io_capabilities_req 1: p_dev_rec->security_required = %d auth_req:%d",
p_dev_rec->security_required, p_data->auth_req);
BTM_TRACE_DEBUG ("btm_ble_io_capabilities_req 2: i_keys=0x%x r_keys=0x%x (bit 0-LTK 1-IRK 2-CSRK)",
p_data->init_keys,
p_data->resp_keys);
if (p_dev_rec->security_required & BTM_SEC_IN_MITM) {
p_data->auth_req |= BTM_LE_AUTH_REQ_MITM;
}{...}
if (!(p_data->auth_req & SMP_AUTH_BOND)) {
BTM_TRACE_DEBUG("Non bonding: No keys should be exchanged");
p_data->init_keys = 0;
p_data->resp_keys = 0;
}{...}
BTM_TRACE_DEBUG ("btm_ble_io_capabilities_req 3: auth_req:%d\n", p_data->auth_req);
BTM_TRACE_DEBUG ("btm_ble_io_capabilities_req 4: i_keys=0x%x r_keys=0x%x\n",
p_data->init_keys,
p_data->resp_keys);
BTM_TRACE_DEBUG ("btm_ble_io_capabilities_req 5: p_data->io_cap = %d auth_req:%d\n",
p_data->io_cap, p_data->auth_req);
if ((p_data->io_cap == BTM_IO_CAP_NONE) && p_data->oob_data == SMP_OOB_NONE) {
p_data->auth_req &= ~BTM_LE_AUTH_REQ_MITM;
}{...}
if (!(p_data->auth_req & SMP_SC_SUPPORT_BIT)) {
/* ... */
BTM_TRACE_DEBUG("%s-SC not supported -> No LK derivation, no keypress notifications",
__func__);
p_data->auth_req &= ~SMP_KP_SUPPORT_BIT;
p_data->init_keys &= ~SMP_SEC_KEY_TYPE_LK;
p_data->resp_keys &= ~SMP_SEC_KEY_TYPE_LK;
}{...}
BTM_TRACE_DEBUG ("btm_ble_io_capabilities_req 6: IO_CAP:%d oob_data:%d auth_req:0x%02x\n",
p_data->io_cap, p_data->oob_data, p_data->auth_req);
}{...}
return callback_rc;
}{ ... }
#endif/* ... */
/* ... */
#if (SMP_INCLUDED == TRUE)
UINT8 btm_ble_br_keys_req(tBTM_SEC_DEV_REC *p_dev_rec, tBTM_LE_IO_REQ *p_data)
{
UINT8 callback_rc = BTM_SUCCESS;
BTM_TRACE_DEBUG ("%s\n", __func__);
if (btm_cb.api.p_le_callback) {
callback_rc = (*btm_cb.api.p_le_callback) (BTM_LE_IO_REQ_EVT, p_dev_rec->bd_addr,
(tBTM_LE_EVT_DATA *)p_data);
}{...}
return callback_rc;
}{ ... }
/* ... */#endif
#if (BLE_PRIVACY_SPT == TRUE )
/* ... */
static void btm_ble_resolve_random_addr_on_conn_cmpl(void *p_rec, void *p_data)
{
UINT8 *p = (UINT8 *)p_data;
tBTM_SEC_DEV_REC *match_rec = (tBTM_SEC_DEV_REC *) p_rec;
UINT8 role, bda_type;
UINT16 handle;
BD_ADDR bda, local_rpa, peer_rpa;
UINT16 conn_interval, conn_latency, conn_timeout;
BOOLEAN match = FALSE;
++p;
STREAM_TO_UINT16 (handle, p);
STREAM_TO_UINT8 (role, p);
STREAM_TO_UINT8 (bda_type, p);
STREAM_TO_BDADDR (bda, p);
if(temp_enhanced) {
STREAM_TO_BDADDR(local_rpa, p);
STREAM_TO_BDADDR(peer_rpa, p);
}{...}
STREAM_TO_UINT16 (conn_interval, p);
STREAM_TO_UINT16 (conn_latency, p);
STREAM_TO_UINT16 (conn_timeout, p);
handle = HCID_GET_HANDLE (handle);
BTM_TRACE_EVENT ("%s\n", __func__);
if (match_rec) {
BTM_TRACE_DEBUG("%s matched and resolved random address", __func__);
match = TRUE;
match_rec->ble.active_addr_type = BTM_BLE_ADDR_RRA;
memcpy(match_rec->ble.cur_rand_addr, bda, BD_ADDR_LEN);
if (!btm_ble_init_pseudo_addr (match_rec, bda)) {
memcpy(bda, match_rec->ble.pseudo_addr, BD_ADDR_LEN);
}{...} else {
memcpy(bda, match_rec->bd_addr, BD_ADDR_LEN);
}{...}
}{...} else {
BTM_TRACE_DEBUG("%s unable to match and resolve random address", __func__);
}{...}
btm_ble_connected(bda, handle, HCI_ENCRYPT_MODE_DISABLED, role, bda_type, match);
l2cble_conn_comp (handle, role, bda, bda_type, conn_interval,
conn_latency, conn_timeout);
return;
}{ ... }
#endif/* ... */
/* ... */
void btm_ble_connected (UINT8 *bda, UINT16 handle, UINT8 enc_mode, UINT8 role,
tBLE_ADDR_TYPE addr_type, BOOLEAN addr_matched)
{
tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev (bda);
tBTM_BLE_CB *p_cb = &btm_cb.ble_ctr_cb;
UNUSED(addr_matched);
BTM_TRACE_EVENT ("btm_ble_connected");
/* ... */
#if (BT_USE_TRACES == TRUE)
if (p_dev_rec) {
BTM_TRACE_EVENT ("Security Manager: btm_ble_connected : handle:%d enc_mode:%d bda:%x RName:%s",
handle, enc_mode,
(bda[2] << 24) + (bda[3] << 16) + (bda[4] << 8) + bda[5],
p_dev_rec->sec_bd_name);
BTM_TRACE_DEBUG ("btm_ble_connected sec_flags=0x%x", p_dev_rec->sec_flags);
}{...} else {
BTM_TRACE_EVENT ("Security Manager: btm_ble_connected: handle:%d enc_mode:%d bda:%x ",
handle, enc_mode,
(bda[2] << 24) + (bda[3] << 16) + (bda[4] << 8) + bda[5]);
}{...}
#endif/* ... */
if (!p_dev_rec) {
if ((p_dev_rec = btm_sec_alloc_dev (bda)) == NULL) {
return;
}{...}
}{...} else {
p_dev_rec->timestamp = btm_cb.dev_rec_count++;
}{...}
p_dev_rec->device_type |= BT_DEVICE_TYPE_BLE;
p_dev_rec->ble_hci_handle = handle;
p_dev_rec->ble.ble_addr_type = addr_type;
memcpy(p_dev_rec->ble.pseudo_addr, bda, BD_ADDR_LEN);
p_dev_rec->role_master = FALSE;
if (role == HCI_ROLE_MASTER) {
p_dev_rec->role_master = TRUE;
}{...}
#if (defined BLE_PRIVACY_SPT && BLE_PRIVACY_SPT == TRUE)
if (!addr_matched) {
p_dev_rec->ble.active_addr_type = BTM_BLE_ADDR_PSEUDO;
}{...}
if (p_dev_rec->ble.ble_addr_type == BLE_ADDR_RANDOM && !addr_matched) {
memcpy(p_dev_rec->ble.cur_rand_addr, bda, BD_ADDR_LEN);
}{...}
#endif/* ... */
p_cb->inq_var.directed_conn = BTM_BLE_CONNECT_EVT;
return;
}{ ... }
/* ... */
void btm_ble_conn_complete(UINT8 *p, UINT16 evt_len, BOOLEAN enhanced)
{
#if (BLE_PRIVACY_SPT == TRUE )
UINT8 *p_data = p, peer_addr_type;
#endif
UINT8 role, status, bda_type;
UINT16 handle;
BD_ADDR bda;
BD_ADDR local_rpa, peer_rpa;
UINT16 conn_interval, conn_latency, conn_timeout;
BOOLEAN match = FALSE;
UNUSED(evt_len);
STREAM_TO_UINT8 (status, p);
STREAM_TO_UINT16 (handle, p);
STREAM_TO_UINT8 (role, p);
STREAM_TO_UINT8 (bda_type, p);
STREAM_TO_BDADDR (bda, p);
BTM_TRACE_DEBUG("status = %d, handle = %d, role = %d, bda_type = %d",status,handle,role,bda_type);
if (status == 0) {
if (enhanced) {
STREAM_TO_BDADDR (local_rpa, p);
STREAM_TO_BDADDR (peer_rpa, p);
#if (CONTROLLER_RPA_LIST_ENABLE == TRUE)
BD_ADDR dummy_bda = {0};
/* ... */
if (memcmp(local_rpa, dummy_bda, BD_ADDR_LEN)) {
btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type |= (BLE_ADDR_TYPE_ID_BIT);
BTM_UpdateAddrInfor(btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type, local_rpa);
}{...} else {
btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type &= (~BLE_ADDR_TYPE_ID_BIT);
}{...}
#endif/* ... */
}{...}
#if (BLE_PRIVACY_SPT == TRUE )
peer_addr_type = bda_type;
match = btm_identity_addr_to_random_pseudo (bda, &bda_type, FALSE);
/* ... */
/* ... */
#if CONTROLLER_RPA_LIST_ENABLE
if (!match && role == HCI_ROLE_SLAVE && bda_type != BLE_ADDR_PUBLIC && BTM_BLE_IS_RESOLVE_BDA(bda)) {
#else
if (!match && bda_type != BLE_ADDR_PUBLIC && BTM_BLE_IS_RESOLVE_BDA(bda)) {
#endif
temp_enhanced = enhanced;
btm_ble_resolve_random_addr(bda, btm_ble_resolve_random_addr_on_conn_cmpl, p_data);
temp_enhanced = FALSE;
}{...} else
#endif
{
STREAM_TO_UINT16 (conn_interval, p);
STREAM_TO_UINT16 (conn_latency, p);
STREAM_TO_UINT16 (conn_timeout, p);
handle = HCID_GET_HANDLE (handle);
btm_ble_connected(bda, handle, HCI_ENCRYPT_MODE_DISABLED, role, bda_type, match);
l2cble_conn_comp (handle, role, bda, bda_type, conn_interval,
conn_latency, conn_timeout);
#if (BLE_PRIVACY_SPT == TRUE)
if (enhanced) {
btm_ble_refresh_local_resolvable_private_addr(bda, local_rpa);
if (peer_addr_type & BLE_ADDR_TYPE_ID_BIT) {
btm_ble_refresh_peer_resolvable_private_addr(bda, peer_rpa, BLE_ADDR_RANDOM);
}{...}
}{...}
#endif/* ... */
}{...}
}{...} else {
role = HCI_ROLE_UNKNOWN;
if (status != HCI_ERR_DIRECTED_ADVERTISING_TIMEOUT) {
btm_ble_set_conn_st(BLE_CONN_IDLE);
#if (defined BLE_PRIVACY_SPT && BLE_PRIVACY_SPT == TRUE)
btm_ble_disable_resolving_list(BTM_BLE_RL_INIT, TRUE);
#endif
}{...} else {
#if (defined BLE_PRIVACY_SPT && BLE_PRIVACY_SPT == TRUE)
btm_cb.ble_ctr_cb.inq_var.adv_mode = BTM_BLE_ADV_DISABLE;
btm_ble_disable_resolving_list(BTM_BLE_RL_ADV, TRUE);/* ... */
#endif
}{...}
}{...}
BOOLEAN bg_con = btm_ble_update_mode_operation(role, bda, status);
if (status != HCI_SUCCESS && !bg_con) {
l2c_link_hci_disc_comp (handle, status);
#if (SMP_INCLUDED == TRUE)
btm_sec_disconnected (handle, status);/* ... */
#endif
}{...}
}{ ... }
/* ... */
void btm_ble_create_ll_conn_complete (UINT8 status)
{
if (status != HCI_SUCCESS) {
btm_ble_set_conn_st(BLE_CONN_IDLE);
btm_ble_update_mode_operation(HCI_ROLE_UNKNOWN, NULL, status);
if(l2cb.is_ble_connecting) {
tL2C_LCB *p_lcb = l2cu_find_lcb_by_bd_addr(l2cb.ble_connecting_bda, BT_TRANSPORT_LE);
if (p_lcb != NULL &&
!(p_lcb->link_role == HCI_ROLE_SLAVE && BTM_LE_ACL_IS_CONNECTED(l2cb.ble_connecting_bda))) {
p_lcb->disc_reason = L2CAP_CONN_CANCEL;
l2cu_release_lcb (p_lcb);
}{...}
}{...}
}{...}
}{ ... }
/* ... */
void btm_ble_create_conn_cancel_complete (UINT8 *p)
{
UINT8 status;
STREAM_TO_UINT8 (status, p);
switch (status) {
case HCI_SUCCESS:
if (btm_ble_get_conn_st() == BLE_CONN_CANCEL) {
btm_ble_set_conn_st (BLE_CONN_IDLE);
}{...}
break;...
default:
break;...
}{...}
}{ ... }
/* ... */
#if (SMP_INCLUDED == TRUE)
UINT8 btm_proc_smp_cback(tSMP_EVT event, BD_ADDR bd_addr, tSMP_EVT_DATA *p_data)
{
tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev (bd_addr);
UINT8 res = 0;
BTM_TRACE_DEBUG ("btm_proc_smp_cback event = %d", event);
if (p_dev_rec != NULL) {
switch (event) {
case SMP_IO_CAP_REQ_EVT:
btm_ble_io_capabilities_req(p_dev_rec, (tBTM_LE_IO_REQ *)&p_data->io_req);
break;
...
case SMP_BR_KEYS_REQ_EVT:
btm_ble_br_keys_req(p_dev_rec, (tBTM_LE_IO_REQ *)&p_data->io_req);
break;
...
case SMP_PASSKEY_REQ_EVT:
case SMP_PASSKEY_NOTIF_EVT:
case SMP_OOB_REQ_EVT:
case SMP_NC_REQ_EVT:
case SMP_SC_OOB_REQ_EVT:
p_dev_rec->sec_flags |= BTM_SEC_LE_AUTHENTICATED;
...
case SMP_SEC_REQUEST_EVT:
if (event == SMP_SEC_REQUEST_EVT && btm_cb.pairing_state != BTM_PAIR_STATE_IDLE) {
BTM_TRACE_DEBUG("%s: Ignoring SMP Security request", __func__);
break;
}{...}
memcpy (btm_cb.pairing_bda, bd_addr, BD_ADDR_LEN);
p_dev_rec->sec_state = BTM_SEC_STATE_AUTHENTICATING;
btm_cb.pairing_flags |= BTM_PAIR_FLAGS_LE_ACTIVE;
...
case SMP_COMPLT_EVT:
if (btm_cb.api.p_le_callback) {
BTM_TRACE_DEBUG ("btm_cb.api.p_le_callback=%p", btm_cb.api.p_le_callback );
(*btm_cb.api.p_le_callback) (event, bd_addr, (tBTM_LE_EVT_DATA *)p_data);
}{...}
if (event == SMP_COMPLT_EVT) {
BTM_TRACE_DEBUG ("evt=SMP_COMPLT_EVT before update sec_level=0x%x sec_flags=0x%x", p_data->cmplt.sec_level , p_dev_rec->sec_flags );
res = (p_data->cmplt.reason == SMP_SUCCESS) ? BTM_SUCCESS : BTM_ERR_PROCESSING;
BTM_TRACE_DEBUG ("after update result=%d sec_level=0x%x sec_flags=0x%x",
res, p_data->cmplt.sec_level , p_dev_rec->sec_flags );
if (p_data->cmplt.is_pair_cancel && btm_cb.api.p_bond_cancel_cmpl_callback ) {
BTM_TRACE_DEBUG ("Pairing Cancel completed");
(*btm_cb.api.p_bond_cancel_cmpl_callback)(BTM_SUCCESS);
}{...}
#if BTM_BLE_CONFORMANCE_TESTING == TRUE
if (res != BTM_SUCCESS) {
if (!btm_cb.devcb.no_disc_if_pair_fail && p_data->cmplt.reason != SMP_CONN_TOUT) {
BTM_TRACE_DEBUG ("Pairing failed - prepare to remove ACL");
l2cu_start_post_bond_timer(p_dev_rec->ble_hci_handle);
}{...} else {
BTM_TRACE_DEBUG ("Pairing failed - Not Removing ACL");
p_dev_rec->sec_state = BTM_SEC_STATE_IDLE;
}{...}
}{...}
#else/* ... */
if (res != BTM_SUCCESS && p_data->cmplt.reason != SMP_CONN_TOUT) {
BTM_TRACE_DEBUG ("Pairing failed - prepare to remove ACL");
l2cu_start_post_bond_timer(p_dev_rec->ble_hci_handle);
}{...}
#endif/* ... */
BTM_TRACE_DEBUG ("btm_cb pairing_state=%x pairing_flags=%x",
btm_cb.pairing_state,
btm_cb.pairing_flags);
BTM_TRACE_DEBUG ("btm_cb.pairing_bda %02x:%02x:%02x:%02x:%02x:%02x",
btm_cb.pairing_bda[0], btm_cb.pairing_bda[1], btm_cb.pairing_bda[2],
btm_cb.pairing_bda[3], btm_cb.pairing_bda[4], btm_cb.pairing_bda[5]);
if (memcmp(bd_addr, btm_cb.pairing_bda, BD_ADDR_LEN) == 0) {
memset (btm_cb.pairing_bda, 0xff, BD_ADDR_LEN);
btm_cb.pairing_state = BTM_PAIR_STATE_IDLE;
btm_cb.pairing_flags = 0;
}{...}
if (res == BTM_SUCCESS) {
p_dev_rec->sec_state = BTM_SEC_STATE_IDLE;
#if (defined BLE_PRIVACY_SPT && BLE_PRIVACY_SPT == TRUE)
btm_ble_resolving_list_load_dev(p_dev_rec);/* ... */
#endif
}{...}
btm_sec_dev_rec_cback_event(p_dev_rec, res, TRUE);
}{...}
break;
...
default:
BTM_TRACE_DEBUG ("unknown event = %d", event);
break;
...
}{...}
}{...} else {
if (event == SMP_SC_LOC_OOB_DATA_UP_EVT) {
tBTM_LE_EVT_DATA evt_data;
memcpy(&evt_data.local_oob_data, &p_data->loc_oob_data, sizeof(tSMP_LOC_OOB_DATA));
if (btm_cb.api.p_le_callback) {
(*btm_cb.api.p_le_callback)(event, bd_addr, &evt_data);
}{...}
}{...} else {
BTM_TRACE_ERROR("btm_proc_smp_cback received for unknown device");
}{...}
}{...}
return 0;
}{ ... }
/* ... */#endif
/* ... */
#if (SMP_INCLUDED == TRUE)
BOOLEAN BTM_BleDataSignature (BD_ADDR bd_addr, UINT8 *p_text, UINT16 len,
BLE_SIGNATURE signature)
{
tBTM_SEC_DEV_REC *p_rec = btm_find_dev (bd_addr);
BTM_TRACE_DEBUG ("%s", __func__);
#endif
BOOLEAN ret = FALSE;
#if (SMP_INCLUDED == TRUE)
if (p_rec == NULL) {
BTM_TRACE_ERROR("%s-data signing can not be done from unknown device", __func__);
}{...} else {
UINT8 *p_mac = (UINT8 *)signature;
UINT8 *p_buf, *pp;
if ((p_buf = (UINT8 *)osi_malloc((UINT16)(len + 4))) != NULL) {
BTM_TRACE_DEBUG("%s-Start to generate Local CSRK", __func__);
pp = p_buf;
if (p_text) {
memcpy(p_buf, p_text, len);
pp = (p_buf + len);
}{...}
UINT32_TO_STREAM(pp, p_rec->ble.keys.local_counter);
UINT32_TO_STREAM(p_mac, p_rec->ble.keys.local_counter);
if ((ret = aes_cipher_msg_auth_code(p_rec->ble.keys.lcsrk, p_buf, (UINT16)(len + 4),
BTM_CMAC_TLEN_SIZE, p_mac)) == TRUE) {
btm_ble_increment_sign_ctr(bd_addr, TRUE);
}{...}
BTM_TRACE_DEBUG("%s p_mac = %p", __func__, p_mac);
BTM_TRACE_DEBUG("p_mac[0] = 0x%02x p_mac[1] = 0x%02x p_mac[2] = 0x%02x p_mac[3] = 0x%02x",
*p_mac, *(p_mac + 1), *(p_mac + 2), *(p_mac + 3));
BTM_TRACE_DEBUG("p_mac[4] = 0x%02x p_mac[5] = 0x%02x p_mac[6] = 0x%02x p_mac[7] = 0x%02x",
*(p_mac + 4), *(p_mac + 5), *(p_mac + 6), *(p_mac + 7));
osi_free(p_buf);
}{...}
}{...}
return ret;
}{ ... }
/* ... */
BOOLEAN BTM_BleVerifySignature (BD_ADDR bd_addr, UINT8 *p_orig, UINT16 len, UINT32 counter,
UINT8 *p_comp)
{
BOOLEAN verified = FALSE;
tBTM_SEC_DEV_REC *p_rec = btm_find_dev (bd_addr);
UINT8 p_mac[BTM_CMAC_TLEN_SIZE];
if (p_rec == NULL || (p_rec && !(p_rec->ble.key_type & BTM_LE_KEY_PCSRK))) {
BTM_TRACE_ERROR("can not verify signature for unknown device");
}{...} else if (counter < p_rec->ble.keys.counter) {
BTM_TRACE_ERROR("signature received with out dated sign counter");
}{...} else if (p_orig == NULL) {
BTM_TRACE_ERROR("No signature to verify");
}{...} else {
BTM_TRACE_DEBUG ("%s rcv_cnt=%d >= expected_cnt=%d", __func__, counter,
p_rec->ble.keys.counter);
if (aes_cipher_msg_auth_code(p_rec->ble.keys.pcsrk, p_orig, len, BTM_CMAC_TLEN_SIZE, p_mac)) {
if (memcmp(p_mac, p_comp, BTM_CMAC_TLEN_SIZE) == 0) {
btm_ble_increment_sign_ctr(bd_addr, FALSE);
verified = TRUE;
}{...}
}{...}
}{...}
return verified;
}{ ... }
#endif
/* ... */
BOOLEAN BTM_GetLeSecurityState (BD_ADDR bd_addr, UINT8 *p_le_dev_sec_flags, UINT8 *p_le_key_size)
{
#if (BLE_INCLUDED == TRUE && SMP_INCLUDED == TRUE)
tBTM_SEC_DEV_REC *p_dev_rec;
UINT16 dev_rec_sec_flags;/* ... */
#endif
*p_le_dev_sec_flags = 0;
*p_le_key_size = 0;
#if (BLE_INCLUDED == TRUE && SMP_INCLUDED == TRUE)
if ((p_dev_rec = btm_find_dev (bd_addr)) == NULL) {
BTM_TRACE_ERROR ("%s fails", __func__);
return (FALSE);
}{...}
if (p_dev_rec->ble_hci_handle == BTM_SEC_INVALID_HANDLE) {
BTM_TRACE_ERROR ("%s-this is not LE device", __func__);
return (FALSE);
}{...}
dev_rec_sec_flags = p_dev_rec->sec_flags;
if (dev_rec_sec_flags & BTM_SEC_LE_ENCRYPTED) {
*p_le_key_size = p_dev_rec->enc_key_size;
*p_le_dev_sec_flags |= BTM_SEC_LE_LINK_ENCRYPTED;
*p_le_dev_sec_flags |= (dev_rec_sec_flags & BTM_SEC_LE_AUTHENTICATED)
? BTM_SEC_LE_LINK_PAIRED_WITH_MITM
: BTM_SEC_LE_LINK_PAIRED_WITHOUT_MITM;
}{...} else if (p_dev_rec->ble.key_type & BTM_LE_KEY_PENC) {
*p_le_key_size = p_dev_rec->ble.keys.key_size;
*p_le_dev_sec_flags |= (dev_rec_sec_flags & BTM_SEC_LE_LINK_KEY_AUTHED)
? BTM_SEC_LE_LINK_PAIRED_WITH_MITM
: BTM_SEC_LE_LINK_PAIRED_WITHOUT_MITM;
}{...}
BTM_TRACE_DEBUG ("%s - le_dev_sec_flags: 0x%02x, le_key_size: %d",
__func__, *p_le_dev_sec_flags, *p_le_key_size);
return TRUE;/* ... */
#else
return FALSE;
#endif
}{ ... }
/* ... */
BOOLEAN BTM_BleSecurityProcedureIsRunning(BD_ADDR bd_addr)
{
#if (BLE_INCLUDED == TRUE)
tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev (bd_addr);
if (p_dev_rec == NULL) {
BTM_TRACE_ERROR ("%s device with BDA: %08x%04x is not found",
__func__, (bd_addr[0] << 24) + (bd_addr[1] << 16) + (bd_addr[2] << 8) + bd_addr[3],
(bd_addr[4] << 8) + bd_addr[5]);
return FALSE;
}{...}
return (p_dev_rec->sec_state == BTM_SEC_STATE_ENCRYPTING ||
p_dev_rec->sec_state == BTM_SEC_STATE_AUTHENTICATING);/* ... */
#else
return FALSE;
#endif
}{ ... }
/* ... */
extern UINT8 BTM_BleGetSupportedKeySize (BD_ADDR bd_addr)
{
#ifndef L2CAP_LE_COC_INCLUDED
#define L2CAP_LE_COC_INCLUDED FALSE
#endif
#if ((BLE_INCLUDED == TRUE) && (L2CAP_LE_COC_INCLUDED == TRUE))
tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev (bd_addr);
tBTM_LE_IO_REQ dev_io_cfg;
UINT8 callback_rc;
if (!p_dev_rec) {
BTM_TRACE_ERROR ("%s device with BDA: %08x%04x is not found",
__func__, (bd_addr[0] << 24) + (bd_addr[1] << 16) + (bd_addr[2] << 8) + bd_addr[3],
(bd_addr[4] << 8) + bd_addr[5]);
return 0;
}{...}
if (btm_cb.api.p_le_callback == NULL) {
BTM_TRACE_ERROR ("%s can't access supported key size", __func__);
return 0;
}{...}
callback_rc = (*btm_cb.api.p_le_callback) (BTM_LE_IO_REQ_EVT, p_dev_rec->bd_addr,
(tBTM_LE_EVT_DATA *) &dev_io_cfg);
if (callback_rc != BTM_SUCCESS) {
BTM_TRACE_ERROR ("%s can't access supported key size", __func__);
return 0;
}{...}
BTM_TRACE_DEBUG ("%s device supports key size = %d", __func__, dev_io_cfg.max_key_size);
return (dev_io_cfg.max_key_size);/* ... */
#else
return 0;
#endif
}{ ... }
/* ... */
/* ... */
#if (SMP_INCLUDED == TRUE)
static void btm_notify_new_key(UINT8 key_type)
{
tBTM_BLE_LOCAL_KEYS *p_locak_keys = NULL;
BTM_TRACE_DEBUG ("btm_notify_new_key key_type=%d", key_type);
if (btm_cb.api.p_le_key_callback) {
switch (key_type) {
case BTM_BLE_KEY_TYPE_ID:
BTM_TRACE_DEBUG ("BTM_BLE_KEY_TYPE_ID");
p_locak_keys = (tBTM_BLE_LOCAL_KEYS *)&btm_cb.devcb.id_keys;
break;
...
case BTM_BLE_KEY_TYPE_ER:
BTM_TRACE_DEBUG ("BTM_BLE_KEY_TYPE_ER");
p_locak_keys = (tBTM_BLE_LOCAL_KEYS *)&btm_cb.devcb.ble_encryption_key_value;
break;
...
default:
BTM_TRACE_ERROR("unknown key type: %d", key_type);
break;...
}{...}
if (p_locak_keys != NULL) {
(*btm_cb.api.p_le_key_callback) (key_type, p_locak_keys);
}{...}
}{...}
}{ ... }
/* ... */
static void btm_ble_process_er2(tBTM_RAND_ENC *p)
{
BTM_TRACE_DEBUG ("btm_ble_process_er2");
if (p && p->opcode == HCI_BLE_RAND) {
memcpy(&btm_cb.devcb.ble_encryption_key_value[8], p->param_buf, BT_OCTET8_LEN);
btm_notify_new_key(BTM_BLE_KEY_TYPE_ER);
}{...} else {
BTM_TRACE_ERROR("Generating ER2 exception.");
memset(&btm_cb.devcb.ble_encryption_key_value, 0, sizeof(BT_OCTET16));
}{...}
}{ ... }
/* ... */
static void btm_ble_process_er(tBTM_RAND_ENC *p)
{
BTM_TRACE_DEBUG ("btm_ble_process_er");
if (p && p->opcode == HCI_BLE_RAND) {
memcpy(&btm_cb.devcb.ble_encryption_key_value[0], p->param_buf, BT_OCTET8_LEN);
if (!btsnd_hcic_ble_rand((void *)btm_ble_process_er2)) {
memset(&btm_cb.devcb.ble_encryption_key_value, 0, sizeof(BT_OCTET16));
BTM_TRACE_ERROR("Generating ER2 failed.");
}{...}
}{...} else {
BTM_TRACE_ERROR("Generating ER1 exception.");
}{...}
}{ ... }
/* ... */
static void btm_ble_process_irk(tSMP_ENC *p)
{
BTM_TRACE_DEBUG ("btm_ble_process_irk");
if (p && p->opcode == HCI_BLE_ENCRYPT) {
memcpy(btm_cb.devcb.id_keys.irk, p->param_buf, BT_OCTET16_LEN);
btm_notify_new_key(BTM_BLE_KEY_TYPE_ID);
#if (CONTROLLER_RPA_LIST_ENABLE == TRUE)
btm_ble_add_default_entry_to_resolving_list();
#endif
#if (BLE_PRIVACY_SPT == TRUE) && (CONTROLLER_RPA_LIST_ENABLE == FALSE)
if (btm_cb.ble_ctr_cb.privacy_mode != BTM_PRIVACY_NONE) {
btm_gen_resolvable_private_addr((void *)btm_gen_resolve_paddr_low);
}{...}
#endif/* ... */
}{...} else {
BTM_TRACE_ERROR("Generating IRK exception.");
}{...}
if (!btsnd_hcic_ble_rand((void *)btm_ble_process_er)) {
BTM_TRACE_ERROR("Generating ER failed.");
}{...}
}{ ... }
/* ... */
static void btm_ble_process_dhk(tSMP_ENC *p)
{
UINT8 btm_ble_irk_pt = 0x01;
tSMP_ENC output;
BTM_TRACE_DEBUG ("btm_ble_process_dhk");
if (p && p->opcode == HCI_BLE_ENCRYPT) {
memcpy(btm_cb.devcb.id_keys.dhk, p->param_buf, BT_OCTET16_LEN);
BTM_TRACE_DEBUG("BLE DHK generated.");
if (!SMP_Encrypt(btm_cb.devcb.id_keys.ir, BT_OCTET16_LEN, &btm_ble_irk_pt,
1, &output)) {
memset(&btm_cb.devcb.id_keys, 0, sizeof(tBTM_BLE_LOCAL_ID_KEYS));
}{...} else {
btm_ble_process_irk(&output);
}{...}
}{...} else {
memset(&btm_cb.devcb.id_keys, 0, sizeof(tBTM_BLE_LOCAL_ID_KEYS));
}{...}
}{ ... }
/* ... */
static void btm_ble_process_ir2(tBTM_RAND_ENC *p)
{
UINT8 btm_ble_dhk_pt = 0x03;
tSMP_ENC output;
BTM_TRACE_DEBUG ("btm_ble_process_ir2");
if (p && p->opcode == HCI_BLE_RAND) {
memcpy(&btm_cb.devcb.id_keys.ir[8], p->param_buf, BT_OCTET8_LEN);
SMP_Encrypt(btm_cb.devcb.id_keys.ir, BT_OCTET16_LEN, &btm_ble_dhk_pt,
1, &output);
btm_ble_process_dhk(&output);
BTM_TRACE_DEBUG("BLE IR generated.");
}{...} else {
memset(&btm_cb.devcb.id_keys, 0, sizeof(tBTM_BLE_LOCAL_ID_KEYS));
}{...}
}{ ... }
/* ... */
static void btm_ble_process_ir(tBTM_RAND_ENC *p)
{
BTM_TRACE_DEBUG ("btm_ble_process_ir");
if (p && p->opcode == HCI_BLE_RAND) {
memcpy(btm_cb.devcb.id_keys.ir, p->param_buf, BT_OCTET8_LEN);
if (!btsnd_hcic_ble_rand((void *)btm_ble_process_ir2)) {
BTM_TRACE_ERROR("Generating IR2 failed.");
memset(&btm_cb.devcb.id_keys, 0, sizeof(tBTM_BLE_LOCAL_ID_KEYS));
}{...}
}{...}
}{ ... }
/* ... */
void btm_ble_reset_id( void )
{
BTM_TRACE_DEBUG ("btm_ble_reset_id");
if (!btsnd_hcic_ble_rand((void *)btm_ble_process_ir)) {
BTM_TRACE_DEBUG("Generating IR failed.");
}{...}
}{ ... }
#endif/* ... */
#if BTM_BLE_CONFORMANCE_TESTING == TRUE
/* ... */
void btm_ble_set_no_disc_if_pair_fail(BOOLEAN disable_disc )
{
BTM_TRACE_DEBUG ("btm_ble_set_disc_enable_if_pair_fail disable_disc=%d", disable_disc);
btm_cb.devcb.no_disc_if_pair_fail = disable_disc;
}{...}
/* ... */
void btm_ble_set_test_mac_value(BOOLEAN enable, UINT8 *p_test_mac_val )
{
BTM_TRACE_DEBUG ("btm_ble_set_test_mac_value enable=%d", enable);
btm_cb.devcb.enable_test_mac_val = enable;
memcpy(btm_cb.devcb.test_mac, p_test_mac_val, BT_OCTET8_LEN);
}{...}
/* ... */
void btm_ble_set_test_local_sign_cntr_value(BOOLEAN enable, UINT32 test_local_sign_cntr )
{
BTM_TRACE_DEBUG ("btm_ble_set_test_local_sign_cntr_value enable=%d local_sign_cntr=%d",
enable, test_local_sign_cntr);
btm_cb.devcb.enable_test_local_sign_cntr = enable;
btm_cb.devcb.test_local_sign_cntr = test_local_sign_cntr;
}{...}
/* ... */
void btm_set_random_address(BD_ADDR random_bda)
{
tBTM_LE_RANDOM_CB *p_cb = &btm_cb.ble_ctr_cb.addr_mgnt_cb;
BOOLEAN adv_mode = btm_cb.ble_ctr_cb.inq_var.adv_mode ;
BTM_TRACE_DEBUG ("btm_set_random_address");
if (adv_mode == BTM_BLE_ADV_ENABLE) {
btsnd_hcic_ble_set_adv_enable (BTM_BLE_ADV_DISABLE);
}{...}
memcpy(p_cb->private_addr, random_bda, BD_ADDR_LEN);
btsnd_hcic_ble_set_random_addr(p_cb->private_addr);
if (adv_mode == BTM_BLE_ADV_ENABLE) {
btsnd_hcic_ble_set_adv_enable (BTM_BLE_ADV_ENABLE);
}{...}
}{...}
/* ... */
void btm_ble_set_keep_rfu_in_auth_req(BOOLEAN keep_rfu)
{
BTM_TRACE_DEBUG ("btm_ble_set_keep_rfu_in_auth_req keep_rfus=%d", keep_rfu);
btm_cb.devcb.keep_rfu_in_auth_req = keep_rfu;
}{...}
/* ... */
#endif
/* ... */
BOOLEAN btm_get_current_conn_params(BD_ADDR bda, UINT16 *interval, UINT16 *latency, UINT16 *timeout)
{
if( (interval == NULL) || (latency == NULL) || (timeout == NULL) ) {
BTM_TRACE_ERROR("%s invalid parameters ", __func__);
return FALSE;
}{...}
tL2C_LCB *p_lcb = l2cu_find_lcb_by_bd_addr(bda, BT_TRANSPORT_LE);
if(p_lcb != NULL) {
(*interval) = p_lcb->current_used_conn_interval;
(*latency) = p_lcb->current_used_conn_latency;
(*timeout) = p_lcb->current_used_conn_timeout;
return TRUE;
}{...}
BTM_TRACE_WARNING("%s Device is not connected", __func__);
return FALSE;
}{ ... }
uint8_t btm_ble_adv_active_count(void)
{
uint8_t count = 0;
tBTM_BLE_INQ_CB *p_cb = &btm_cb.ble_ctr_cb.inq_var;
if (p_cb->state & BTM_BLE_ADVERTISING) {
count++;
}{...}
return count;
}{ ... }
uint8_t btm_ble_scan_active_count(void)
{
uint8_t count = 0;
tBTM_BLE_INQ_CB *p_cb = &btm_cb.ble_ctr_cb.inq_var;
if (p_cb->state & BTM_BLE_SCANNING) {
count++;
}{...}
return count;
}{ ... }
#if (SMP_INCLUDED == TRUE)
uint8_t btm_ble_sec_dev_active_count(void)
{
tBTM_SEC_DEV_REC *p_dev_rec = NULL;
list_node_t *p_node = NULL;
uint8_t count = 0;
for (p_node = list_begin(btm_cb.p_sec_dev_rec_list); p_node; p_node = list_next(p_node)) {
p_dev_rec = list_node(p_node);
if (p_dev_rec && (p_dev_rec->sec_flags & BTM_SEC_IN_USE) && (p_dev_rec->ble.key_type != BTM_LE_KEY_NONE)) {
count++;
}{...}
}{...}
return count;
}{ ... }
/* ... */#endif
#if (BLE_PRIVACY_SPT == TRUE)
uint8_t btm_ble_privacy_is_enabled(void)
{
tBTM_BLE_CB *p_cb = &btm_cb.ble_ctr_cb;
return (p_cb->privacy_mode != BTM_PRIVACY_NONE);
}{ ... }
/* ... */#endif
/* ... */
#endif