1 /*
2  * Copyright (C) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "btm_le_sec.h"
17 
18 #include "hci/hci.h"
19 #include "platform/include/allocator.h"
20 #include "platform/include/list.h"
21 #include "platform/include/mutex.h"
22 #include "smp/smp.h"
23 
24 #include "btm.h"
25 #include "btm_acl.h"
26 #include "btm_controller.h"
27 
28 #define STATUS_NONE 0
29 #define STATUS_INITIALIZED 1
30 
31 #define IS_INITIALIZED() (g_status == STATUS_INITIALIZED)
32 
33 typedef struct {
34     BtmLePairedDevice pairedInfo;
35     BtAddr currentAddr;
36     bool inResolvingList;
37 } BtmLePairedDeviceBlock;
38 
39 static BtmKey g_localIdentityResolvingKey;
40 static List *g_lePairedDevices = NULL;
41 static Mutex *g_lePairedDevicesLock = NULL;
42 static uint8_t g_resolvingListSize = 0;
43 static uint8_t g_deviceCountInResolvingList = 0;
44 static Mutex *g_ownAddressTypeLock = NULL;
45 static uint8_t g_ownAddressType = OWN_ADDRESS_TYPE_PUBLIC;
46 static BtAddr g_randomAddress;
47 static Mutex *g_randomAddressLock = NULL;
48 
49 static uint8_t g_status = STATUS_NONE;
50 
BtmAllocLePairedDeviceBlock(const BtmLePairedDevice * device)51 static BtmLePairedDeviceBlock *BtmAllocLePairedDeviceBlock(const BtmLePairedDevice *device)
52 {
53     BtmLePairedDeviceBlock *block = MEM_MALLOC.alloc(sizeof(BtmLePairedDeviceBlock));
54     if (block != NULL) {
55         block->pairedInfo = *device;
56         block->currentAddr = device->addr;
57         block->inResolvingList = false;
58     }
59     return block;
60 }
61 
BtmFreeLePairedDeviceBlock(void * device)62 static void BtmFreeLePairedDeviceBlock(void *device)
63 {
64     MEM_MALLOC.free(device);
65 }
66 
BtmEnableAddressResolution()67 static void BtmEnableAddressResolution()
68 {
69     HciLeSetAddressResolutionEnableParam hciParam = {
70         .addressResolutionEnable = ADDRESS_RESOLUTION_IN_CONTROLLER_ENABLEED,
71     };
72     HCI_LeSetAddressResolutionEnable(&hciParam);
73 }
74 
BtmDisableAddressResolution()75 static void BtmDisableAddressResolution()
76 {
77     HciLeSetAddressResolutionEnableParam hciParam = {
78         .addressResolutionEnable = ADDRESS_RESOLUTION_IN_CONTROLLER_DISABLED,
79     };
80     HCI_LeSetAddressResolutionEnable(&hciParam);
81 }
82 
BtmAddToResolvingList(const BtmLePairedDevice * device)83 static void BtmAddToResolvingList(const BtmLePairedDevice *device)
84 {
85     uint8_t peerIdentityAddrType = HCI_PEER_IDENTITY_ADDRESS_TYPE_PUBLIC;
86     if (device->remoteIdentityAddress.type == BT_RANDOM_DEVICE_ADDRESS ||
87         device->remoteIdentityAddress.type == BT_RANDOM_IDENTITY_ADDRESS) {
88         peerIdentityAddrType = HCI_PEER_IDENTITY_ADDRESS_TYPE_RANDOM;
89     }
90 
91     HciLeAddDeviceToResolvingListParam param = {
92         .peerIdentityAddrType = peerIdentityAddrType,
93         .peerIdentityAddress = {.raw = {0}},
94         .peerIrk = {0},
95         .localIrk = {0},
96     };
97     (void)memcpy_s(param.peerIdentityAddress.raw, BT_ADDRESS_SIZE, device->remoteIdentityAddress.addr, BT_ADDRESS_SIZE);
98     (void)memcpy_s(param.peerIrk, KEY_SIZE, device->remoteIdentityResolvingKey.key, KEY_SIZE);
99     (void)memcpy_s(param.localIrk, KEY_SIZE, g_localIdentityResolvingKey.key, KEY_SIZE);
100     HCI_LeAddDeviceToResolvingList(&param);
101 }
102 
BtmSetPrivacyMode(const BtmLePairedDevice * device)103 static void BtmSetPrivacyMode(const BtmLePairedDevice *device)
104 {
105     uint8_t peerIdentityAddressType = HCI_PEER_ADDR_TYPE_PUBLIC;
106     if (device->remoteIdentityAddress.type == BT_RANDOM_DEVICE_ADDRESS ||
107         device->remoteIdentityAddress.type == BT_RANDOM_IDENTITY_ADDRESS) {
108         peerIdentityAddressType = HCI_PEER_ADDR_TYPE_RANDOM;
109     }
110 
111     HciLeSetPrivacyModeParam param = {
112         .peerIdentityAddressType = peerIdentityAddressType,
113         .peerIdentityAddress = {.raw = {0}},
114         .privacyMode = HCI_DEVICE_PRIVACY_MODE,
115     };
116     (void)memcpy_s(param.peerIdentityAddress.raw, BT_ADDRESS_SIZE, device->remoteIdentityAddress.addr, BT_ADDRESS_SIZE);
117 
118     HCI_LeSetPrivacyMode(&param);
119 }
120 
BtmRemoveFromResolvingList(const BtmLePairedDevice * device)121 static void BtmRemoveFromResolvingList(const BtmLePairedDevice *device)
122 {
123     uint8_t peerIdentityAddrType = HCI_PEER_IDENTITY_ADDRESS_TYPE_PUBLIC;
124     if (device->remoteIdentityAddress.type == BT_RANDOM_DEVICE_ADDRESS ||
125         device->remoteIdentityAddress.type == BT_RANDOM_IDENTITY_ADDRESS) {
126         peerIdentityAddrType = HCI_PEER_IDENTITY_ADDRESS_TYPE_RANDOM;
127     }
128     HciLeRemoveDeviceFromResolvingListParam param = {
129         .peerIdentityAddrType = peerIdentityAddrType,
130         .peerIdentityAddress = {.raw = {0}},
131     };
132     (void)memcpy_s(param.peerIdentityAddress.raw, BT_ADDRESS_SIZE, device->remoteIdentityAddress.addr, BT_ADDRESS_SIZE);
133     HCI_LeRemoveDeviceFromResolvingList(&param);
134 }
135 
IsSameBtAddr(const BtAddr * addr1,const BtAddr * addr2)136 static bool IsSameBtAddr(const BtAddr *addr1, const BtAddr *addr2)
137 {
138     bool isSame = true;
139 
140     for (uint8_t i = 0; i < BT_ADDRESS_SIZE; i++) {
141         if (addr1->addr[i] != addr2->addr[i]) {
142             isSame = false;
143             break;
144         }
145     }
146 
147     return isSame;
148 }
149 
IsZeroAddress(const uint8_t addr[BT_ADDRESS_SIZE])150 static bool IsZeroAddress(const uint8_t addr[BT_ADDRESS_SIZE])
151 {
152     bool isZeroAddress = true;
153     for (uint8_t i = 0; i < BT_ADDRESS_SIZE; i++) {
154         if (addr[i] != 0) {
155             isZeroAddress = false;
156             break;
157         }
158     }
159     return isZeroAddress;
160 }
161 
BtmFindLePairedDeviceBlockByAddress(const BtAddr * addr)162 static BtmLePairedDeviceBlock *BtmFindLePairedDeviceBlockByAddress(const BtAddr *addr)
163 {
164     BtmLePairedDeviceBlock *block = NULL;
165 
166     ListNode *node = ListGetFirstNode(g_lePairedDevices);
167     while (node != NULL) {
168         block = ListGetNodeData(node);
169         if (IsSameBtAddr(&block->pairedInfo.addr, addr)) {
170             break;
171         } else {
172             block = NULL;
173         }
174 
175         node = ListGetNextNode(node);
176     }
177 
178     return block;
179 }
180 
BtmFindLePairedDeviceBlockByRemoteIdentityAddress(const BtAddr * addr)181 static BtmLePairedDeviceBlock *BtmFindLePairedDeviceBlockByRemoteIdentityAddress(const BtAddr *addr)
182 {
183     BtmLePairedDeviceBlock *block = NULL;
184 
185     ListNode *node = ListGetFirstNode(g_lePairedDevices);
186     while (node != NULL) {
187         block = ListGetNodeData(node);
188         if (IsSameBtAddr(&block->pairedInfo.remoteIdentityAddress, addr)) {
189             break;
190         } else {
191             block = NULL;
192         }
193 
194         node = ListGetNextNode(node);
195     }
196 
197     return block;
198 }
199 
BtmInitLeSecurity()200 void BtmInitLeSecurity()
201 {
202     g_lePairedDevices = ListCreate(BtmFreeLePairedDeviceBlock);
203     g_lePairedDevicesLock = MutexCreate();
204 
205     g_ownAddressTypeLock = MutexCreate();
206     g_randomAddressLock = MutexCreate();
207 
208     g_ownAddressType = OWN_ADDRESS_TYPE_PUBLIC;
209 
210     g_status = STATUS_INITIALIZED;
211 }
212 
BtmCloseLeSecurity()213 void BtmCloseLeSecurity()
214 {
215     g_status = STATUS_NONE;
216 
217     if (g_lePairedDevices != NULL) {
218         ListDelete(g_lePairedDevices);
219         g_lePairedDevices = NULL;
220     }
221 
222     if (g_lePairedDevicesLock != NULL) {
223         MutexDelete(g_lePairedDevicesLock);
224         g_lePairedDevicesLock = NULL;
225     }
226 
227     if (g_ownAddressTypeLock != NULL) {
228         MutexDelete(g_ownAddressTypeLock);
229         g_ownAddressTypeLock = NULL;
230     }
231 
232     if (g_randomAddressLock != NULL) {
233         MutexDelete(g_randomAddressLock);
234         g_randomAddressLock = NULL;
235     }
236 }
237 
BtmStartLeSecurity()238 void BtmStartLeSecurity()
239 {
240     g_resolvingListSize = 0;
241     if (BTM_IsControllerSupportLlPrivacy()) {
242         uint8_t resolvingListSize = 0;
243         int result = BtmGetResolvingListSize(&resolvingListSize);
244         if (result == BT_SUCCESS) {
245             g_resolvingListSize = resolvingListSize;
246         }
247 
248         HCI_LeClearResolvingList();
249         BtmEnableAddressResolution();
250     }
251 
252     g_ownAddressType = OWN_ADDRESS_TYPE_PUBLIC;
253 }
254 
BtmStopLeSecurity()255 void BtmStopLeSecurity()
256 {
257     MutexLock(g_lePairedDevicesLock);
258     ListClear(g_lePairedDevices);
259     MutexUnlock(g_lePairedDevicesLock);
260 }
261 
BTM_SetLePairedDevices(const BtmLePairedDevice * pairedDevices,uint16_t count)262 void BTM_SetLePairedDevices(const BtmLePairedDevice *pairedDevices, uint16_t count)
263 {
264     if (!IS_INITIALIZED()) {
265         return;
266     }
267 
268     MutexLock(g_lePairedDevicesLock);
269 
270     BtmStopAutoConnection();
271 
272     ListClear(g_lePairedDevices);
273 
274     if (BTM_IsControllerSupportLlPrivacy()) {
275         BtmDisableAddressResolution();
276         HCI_LeClearResolvingList();
277     }
278 
279     BtmLePairedDeviceBlock *block = NULL;
280     for (uint16_t i = 0; i < count; i++) {
281         block = BtmAllocLePairedDeviceBlock(pairedDevices + i);
282         if (block == NULL) {
283             continue;
284         }
285 
286         ListAddLast(g_lePairedDevices, block);
287 
288         if (IsZeroAddress(block->pairedInfo.remoteIdentityAddress.addr)) {
289             block->inResolvingList = false;
290             continue;
291         }
292 
293         if (BTM_IsControllerSupportLlPrivacy() && g_deviceCountInResolvingList < g_resolvingListSize) {
294             BtmAddToResolvingList(pairedDevices + i);
295             block->inResolvingList = true;
296             g_deviceCountInResolvingList++;
297 
298             if (BtmIsControllerSupportedLeSetPrivacyMode()) {
299                 BtmSetPrivacyMode(pairedDevices + i);
300             }
301         }
302     }
303 
304     if (BTM_IsControllerSupportLlPrivacy()) {
305         BtmEnableAddressResolution();
306     }
307 
308     BtmStartAutoConnection();
309 
310     MutexUnlock(g_lePairedDevicesLock);
311 }
312 
BTM_AddLePairedDevice(const BtmLePairedDevice * device)313 void BTM_AddLePairedDevice(const BtmLePairedDevice *device)
314 {
315     if (device == NULL) {
316         return;
317     }
318 
319     if (!IS_INITIALIZED()) {
320         return;
321     }
322 
323     MutexLock(g_lePairedDevicesLock);
324 
325     BtmLePairedDeviceBlock *block = BtmAllocLePairedDeviceBlock(device);
326     if (block == NULL) {
327         MutexUnlock(g_lePairedDevicesLock);
328         return;
329     }
330     ListAddLast(g_lePairedDevices, block);
331 
332     bool addToResolvingList = false;
333     if (IsZeroAddress(block->pairedInfo.remoteIdentityAddress.addr)) {
334         addToResolvingList = false;
335     } else {
336         if (g_deviceCountInResolvingList < g_resolvingListSize) {
337             addToResolvingList = true;
338         }
339     }
340 
341     if (addToResolvingList && BTM_IsControllerSupportLlPrivacy()) {
342         BtmStopAutoConnection();
343 
344         BtmDisableAddressResolution();
345 
346         BtmAddToResolvingList(device);
347         block->inResolvingList = true;
348         g_deviceCountInResolvingList++;
349 
350         if (BtmIsControllerSupportedLeSetPrivacyMode()) {
351             BtmSetPrivacyMode(device);
352         }
353 
354         BtmEnableAddressResolution();
355 
356         BtmStartAutoConnection();
357     }
358 
359     MutexUnlock(g_lePairedDevicesLock);
360 }
361 
BTM_RemoveLePairedDevice(const BtAddr * addr)362 void BTM_RemoveLePairedDevice(const BtAddr *addr)
363 {
364     if (addr == NULL) {
365         return;
366     }
367 
368     if (!IS_INITIALIZED()) {
369         return;
370     }
371 
372     MutexLock(g_lePairedDevicesLock);
373 
374     if (BTM_IsControllerSupportLlPrivacy()) {
375         BtmDisableAddressResolution();
376     }
377 
378     BtmLePairedDeviceBlock *block = BtmFindLePairedDeviceBlockByAddress(addr);
379     if (block != NULL) {
380         if (block->inResolvingList) {
381             BtmRemoveFromResolvingList(&block->pairedInfo);
382             g_deviceCountInResolvingList--;
383         }
384         ListRemoveNode(g_lePairedDevices, block);
385     }
386 
387     if (BTM_IsControllerSupportLlPrivacy()) {
388         BtmEnableAddressResolution();
389     }
390 
391     MutexUnlock(g_lePairedDevicesLock);
392 }
393 
BTM_SetLocalIdentityResolvingKey(const BtmKey * key)394 void BTM_SetLocalIdentityResolvingKey(const BtmKey *key)
395 {
396     if (key == NULL) {
397         return;
398     }
399 
400     if (!IS_INITIALIZED()) {
401         return;
402     }
403 
404     g_localIdentityResolvingKey = *key;
405 }
406 
BTM_GetLocalIdentityResolvingKey(BtmKey * irk)407 int BTM_GetLocalIdentityResolvingKey(BtmKey *irk)
408 {
409     if (irk == NULL) {
410         return BT_BAD_PARAM;
411     }
412 
413     if (!IS_INITIALIZED()) {
414         return BT_BAD_STATUS;
415     }
416 
417     *irk = g_localIdentityResolvingKey;
418     return BT_SUCCESS;
419 }
420 
BTM_GetRemoteIdentityResolvingKey(const BtAddr * addr,BtmKey * irk)421 int BTM_GetRemoteIdentityResolvingKey(const BtAddr *addr, BtmKey *irk)
422 {
423     if (addr == NULL || irk == NULL) {
424         return BT_BAD_PARAM;
425     }
426 
427     if (!IS_INITIALIZED()) {
428         return BT_BAD_STATUS;
429     }
430 
431     int result = BT_SUCCESS;
432     MutexLock(g_lePairedDevicesLock);
433 
434     BtmLePairedDeviceBlock *block = BtmFindLePairedDeviceBlockByAddress(addr);
435     if (block != NULL) {
436         *irk = block->pairedInfo.remoteIdentityResolvingKey;
437     } else {
438         result = BT_BAD_STATUS;
439     }
440 
441     MutexUnlock(g_lePairedDevicesLock);
442     return result;
443 }
444 
BTM_GetAllRemoteIdentityResolvingKey(BtmIdentityResolvingKey ** irks,uint16_t * count)445 int BTM_GetAllRemoteIdentityResolvingKey(BtmIdentityResolvingKey **irks, uint16_t *count)
446 {
447     if (irks == NULL || count == 0) {
448         return BT_BAD_PARAM;
449     }
450 
451     if (!IS_INITIALIZED()) {
452         return BT_BAD_STATUS;
453     }
454 
455     MutexLock(g_lePairedDevicesLock);
456 
457     uint8_t devCount = ListGetSize(g_lePairedDevices);
458     if (devCount) {
459         BtmIdentityResolvingKey *devIrks = MEM_MALLOC.alloc(sizeof(BtmIdentityResolvingKey) * devCount);
460         if (devIrks != NULL) {
461             uint16_t index = 0;
462             BtmLePairedDeviceBlock *block = NULL;
463             ListNode *node = ListGetFirstNode(g_lePairedDevices);
464             while (node != NULL) {
465                 block = ListGetNodeData(node);
466                 devIrks[index].addr = block->pairedInfo.addr;
467                 devIrks[index].irk = block->pairedInfo.remoteIdentityResolvingKey;
468                 node = ListGetNextNode(node);
469                 index++;
470             }
471 
472             *irks = devIrks;
473             *count = devCount;
474         }
475     } else {
476         *irks = NULL;
477         *count = 0;
478     }
479 
480     MutexUnlock(g_lePairedDevicesLock);
481     return BT_SUCCESS;
482 }
483 
BTM_GetAllPairedDevices(BtmLePairedDevice ** devices,uint16_t * count)484 int BTM_GetAllPairedDevices(BtmLePairedDevice **devices, uint16_t *count)
485 {
486     if ((devices == NULL) || (count == NULL)) {
487         return BT_BAD_PARAM;
488     }
489 
490     if (!IS_INITIALIZED()) {
491         return BT_BAD_STATUS;
492     }
493 
494     MutexLock(g_lePairedDevicesLock);
495 
496     uint8_t devCount = ListGetSize(g_lePairedDevices);
497     if (devCount) {
498         BtmLePairedDevice *devices_ = MEM_MALLOC.alloc(sizeof(BtmLePairedDevice) * devCount);
499         if (devices_ != NULL) {
500             uint16_t index = 0;
501             BtmLePairedDeviceBlock *block = NULL;
502             ListNode *node = ListGetFirstNode(g_lePairedDevices);
503             while (node != NULL) {
504                 block = ListGetNodeData(node);
505                 devices_[index] = block->pairedInfo;
506                 node = ListGetNextNode(node);
507                 index++;
508             }
509 
510             *devices = devices_;
511             *count = devCount;
512         }
513     } else {
514         *devices = NULL;
515         *count = 0;
516     }
517 
518     MutexUnlock(g_lePairedDevicesLock);
519 
520     return BT_SUCCESS;
521 }
522 
BTM_UpdateCurrentRemoteAddress(const BtAddr * pairedAddr,const BtAddr * currentAddr)523 int BTM_UpdateCurrentRemoteAddress(const BtAddr *pairedAddr, const BtAddr *currentAddr)
524 {
525     if (pairedAddr == NULL || currentAddr == NULL) {
526         return BT_BAD_PARAM;
527     }
528 
529     if (!IS_INITIALIZED()) {
530         return BT_BAD_STATUS;
531     }
532 
533     int result = BT_SUCCESS;
534 
535     MutexLock(g_lePairedDevicesLock);
536 
537     BtmLePairedDeviceBlock *block = BtmFindLePairedDeviceBlockByAddress(pairedAddr);
538     if (block != NULL) {
539         block->currentAddr = *currentAddr;
540     } else {
541         result = BT_BAD_STATUS;
542     }
543 
544     MutexUnlock(g_lePairedDevicesLock);
545 
546     return result;
547 }
548 
BTM_GetCurrentRemoteAddress(const BtAddr * pairedAddr,BtAddr * currentAddr)549 int BTM_GetCurrentRemoteAddress(const BtAddr *pairedAddr, BtAddr *currentAddr)
550 {
551     if (pairedAddr == NULL || currentAddr == NULL) {
552         return BT_BAD_PARAM;
553     }
554 
555     if (!IS_INITIALIZED()) {
556         return BT_BAD_STATUS;
557     }
558 
559     int result = BT_SUCCESS;
560 
561     MutexLock(g_lePairedDevicesLock);
562 
563     BtmLePairedDeviceBlock *block = BtmFindLePairedDeviceBlockByAddress(pairedAddr);
564     if (block != NULL) {
565         *currentAddr = block->currentAddr;
566     } else {
567         result = BT_BAD_STATUS;
568     }
569 
570     MutexUnlock(g_lePairedDevicesLock);
571 
572     return result;
573 }
574 
BTM_SetOwnAddressType(uint32_t ownAddressType)575 void BTM_SetOwnAddressType(uint32_t ownAddressType)
576 {
577     MutexLock(g_ownAddressTypeLock);
578     g_ownAddressType = ownAddressType;
579     MutexUnlock(g_ownAddressTypeLock);
580 }
581 
BTM_GetOwnAddressType()582 uint8_t BTM_GetOwnAddressType()
583 {
584     MutexLock(g_ownAddressTypeLock);
585     uint8_t type = g_ownAddressType;
586     MutexUnlock(g_ownAddressTypeLock);
587     return type;
588 }
589 
BTM_GetRemoteIdentityAddress(const BtAddr * addr,BtAddr * identityAddress)590 int BTM_GetRemoteIdentityAddress(const BtAddr *addr, BtAddr *identityAddress)
591 {
592     if (addr == NULL && identityAddress == NULL) {
593         return BT_BAD_PARAM;
594     }
595 
596     if (!IS_INITIALIZED()) {
597         return BT_BAD_STATUS;
598     }
599 
600     int result = BT_SUCCESS;
601 
602     MutexLock(g_lePairedDevicesLock);
603 
604     BtmLePairedDeviceBlock *block = BtmFindLePairedDeviceBlockByAddress(addr);
605     if (block != NULL) {
606         *identityAddress = block->pairedInfo.remoteIdentityAddress;
607     } else {
608         result = BT_BAD_STATUS;
609     }
610 
611     MutexUnlock(g_lePairedDevicesLock);
612 
613     return result;
614 }
615 
BTM_GetPairdAddressFromRemoteIdentityAddress(const BtAddr * identityAddress,BtAddr * addr)616 int BTM_GetPairdAddressFromRemoteIdentityAddress(const BtAddr *identityAddress, BtAddr *addr)
617 {
618     if (addr == NULL || identityAddress == NULL) {
619         return BT_BAD_PARAM;
620     }
621 
622     if (!IS_INITIALIZED()) {
623         return BT_BAD_STATUS;
624     }
625 
626     int result = BT_SUCCESS;
627 
628     MutexLock(g_lePairedDevicesLock);
629 
630     BtmLePairedDeviceBlock *block = BtmFindLePairedDeviceBlockByRemoteIdentityAddress(identityAddress);
631     if (block != NULL) {
632         *addr = block->pairedInfo.addr;
633     } else {
634         result = BT_BAD_STATUS;
635     }
636 
637     MutexUnlock(g_lePairedDevicesLock);
638 
639     return result;
640 }
641 
BTM_IsDeviceInResolvingList(const BtAddr * addr)642 bool BTM_IsDeviceInResolvingList(const BtAddr *addr)
643 {
644     if (addr == NULL) {
645         return false;
646     }
647 
648     if (!IS_INITIALIZED()) {
649         return false;
650     }
651 
652     bool inResolvingList = false;
653 
654     MutexLock(g_lePairedDevicesLock);
655 
656     BtmLePairedDeviceBlock *block = BtmFindLePairedDeviceBlockByAddress(addr);
657     if (block != NULL) {
658         inResolvingList = block->inResolvingList;
659     }
660 
661     MutexUnlock(g_lePairedDevicesLock);
662 
663     return inResolvingList;
664 }
665 
BTM_ConvertToPairedAddress(const BtAddr * addr,BtAddr * pairedAddress)666 int BTM_ConvertToPairedAddress(const BtAddr *addr, BtAddr *pairedAddress)
667 {
668     if (addr == NULL || pairedAddress == NULL) {
669         return BT_BAD_PARAM;
670     }
671 
672     if (!IS_INITIALIZED()) {
673         return BT_BAD_STATUS;
674     }
675 
676     int result = BT_BAD_STATUS;
677 
678     MutexLock(g_lePairedDevicesLock);
679 
680     BtmLePairedDeviceBlock *block = NULL;
681 
682     ListNode *node = ListGetFirstNode(g_lePairedDevices);
683     while (node != NULL) {
684         block = ListGetNodeData(node);
685         if (IsSameBtAddr(&block->currentAddr, addr)) {
686             *pairedAddress = block->pairedInfo.addr;
687             result = BT_SUCCESS;
688             break;
689         } else if (IsSameBtAddr(&block->pairedInfo.remoteIdentityAddress, addr)) {
690             *pairedAddress = block->pairedInfo.addr;
691             result = BT_SUCCESS;
692             break;
693         } else if (SMP_ResolveRPA(addr->addr, block->pairedInfo.remoteIdentityResolvingKey.key) ==
694                    SMP_RESOLVE_RPA_RESULT_YES) {
695             *pairedAddress = block->pairedInfo.addr;
696             result = BT_SUCCESS;
697             break;
698         }
699 
700         node = ListGetNextNode(node);
701     }
702 
703     MutexUnlock(g_lePairedDevicesLock);
704 
705     return result;
706 }
707 
BTM_SetLeRandomAddress(const BtAddr * addr)708 int BTM_SetLeRandomAddress(const BtAddr *addr)
709 {
710     if (addr == NULL) {
711         return BT_BAD_PARAM;
712     }
713 
714     if (!IS_INITIALIZED()) {
715         return BT_BAD_STATUS;
716     }
717 
718     BtmStopAutoConnection();
719 
720     HciLeSetRandomAddressParam param = {
721         .randomAddess = {0},
722     };
723     (void)memcpy_s(param.randomAddess, BT_ADDRESS_SIZE, addr->addr, BT_ADDRESS_SIZE);
724 
725     int result = HCI_LeSetRandomAddress(&param);
726     if (result == BT_SUCCESS) {
727         MutexLock(g_randomAddressLock);
728         g_randomAddress = *addr;
729         MutexUnlock(g_randomAddressLock);
730     }
731 
732     BtmStartAutoConnection();
733     return result;
734 }
735 
BTM_GetLeRandomAddress(BtAddr * addr)736 int BTM_GetLeRandomAddress(BtAddr *addr)
737 {
738     if (addr == NULL) {
739         return BT_BAD_PARAM;
740     }
741 
742     if (!IS_INITIALIZED()) {
743         return BT_BAD_STATUS;
744     }
745 
746     MutexLock(g_randomAddressLock);
747     *addr = g_randomAddress;
748     MutexUnlock(g_randomAddressLock);
749     return BT_SUCCESS;
750 }