1 /******************************************************************************
2  *
3  *  Copyright (C) 1999-2012 Broadcom Corporation
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at:
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  ******************************************************************************/
18 
19 /******************************************************************************
20  *
21  *  This file contains functions for BLE address management.
22  *
23  ******************************************************************************/
24 
25 #include <string.h>
26 
27 #include "stack/bt_types.h"
28 #include "stack/hcimsgs.h"
29 #include "stack/btu.h"
30 #include "btm_int.h"
31 #include "stack/gap_api.h"
32 #include "device/controller.h"
33 
34 #if (defined BLE_INCLUDED && BLE_INCLUDED == TRUE)
35 #include "btm_ble_int.h"
36 #include "stack/smp_api.h"
37 
38 
39 /*******************************************************************************
40 **
41 ** Function         btm_gen_resolve_paddr_cmpl
42 **
43 ** Description      This is callback functioin when resolvable private address
44 **                  generation is complete.
45 **
46 ** Returns          void
47 **
48 *******************************************************************************/
btm_gen_resolve_paddr_cmpl(tSMP_ENC * p)49 static void btm_gen_resolve_paddr_cmpl(tSMP_ENC *p)
50 {
51     tBTM_LE_RANDOM_CB *p_cb = &btm_cb.ble_ctr_cb.addr_mgnt_cb;
52     BTM_TRACE_EVENT ("btm_gen_resolve_paddr_cmpl");
53 
54     if (p) {
55         /* set hash to be LSB of rpAddress */
56         p_cb->private_addr[5] = p->param_buf[0];
57         p_cb->private_addr[4] = p->param_buf[1];
58         p_cb->private_addr[3] = p->param_buf[2];
59         /* set it to controller */
60         btsnd_hcic_ble_set_random_addr(p_cb->private_addr);
61 
62         p_cb->exist_addr_bit |= BTM_BLE_GAP_ADDR_BIT_RESOLVABLE;
63         memcpy(p_cb->resolvale_addr, p_cb->private_addr, BD_ADDR_LEN);
64         if (p_cb->set_local_privacy_cback){
65             (*p_cb->set_local_privacy_cback)(BTM_SET_PRIVACY_SUCCESS);
66             p_cb->set_local_privacy_cback = NULL;
67         }
68 
69         if (btm_cb.ble_ctr_cb.inq_var.adv_mode == BTM_BLE_ADV_ENABLE){
70             BTM_TRACE_DEBUG("Advertise with new resolvable private address, now.");
71             /**
72              * Restart advertising, using new resolvable private address
73              */
74             btm_ble_stop_adv();
75             btm_ble_start_adv();
76         }
77         if (btm_cb.ble_ctr_cb.inq_var.state == BTM_BLE_SCANNING){
78             BTM_TRACE_DEBUG("Scan with new resolvable private address, now.");
79             /**
80              * Restart scaning, using new resolvable private address
81              */
82             btm_ble_stop_scan();
83             btm_ble_start_scan();
84         }
85 
86         /* start a periodical timer to refresh random addr */
87         btu_stop_timer_oneshot(&p_cb->raddr_timer_ent);
88 #if (BTM_BLE_CONFORMANCE_TESTING == TRUE)
89         btu_start_timer_oneshot(&p_cb->raddr_timer_ent, BTU_TTYPE_BLE_RANDOM_ADDR,
90                                 btm_cb.ble_ctr_cb.rpa_tout);
91 #else
92         btu_start_timer_oneshot(&p_cb->raddr_timer_ent, BTU_TTYPE_BLE_RANDOM_ADDR,
93                                 BTM_BLE_PRIVATE_ADDR_INT);
94 #endif
95     } else {
96         /* random address set failure */
97         BTM_TRACE_DEBUG("set random address failed");
98         if (p_cb->set_local_privacy_cback){
99             (*p_cb->set_local_privacy_cback)(BTM_SET_PRIVACY_FAIL);
100             p_cb->set_local_privacy_cback = NULL;
101         }
102     }
103 }
104 /*******************************************************************************
105 **
106 ** Function         btm_gen_resolve_paddr_low
107 **
108 ** Description      This function is called when random address has generate the
109 **                  random number base for low 3 byte bd address.
110 **
111 ** Returns          void
112 **
113 *******************************************************************************/
btm_gen_resolve_paddr_low(tBTM_RAND_ENC * p)114 void btm_gen_resolve_paddr_low(tBTM_RAND_ENC *p)
115 {
116 #if (BLE_INCLUDED == TRUE && SMP_INCLUDED == TRUE)
117     tBTM_LE_RANDOM_CB *p_cb = &btm_cb.ble_ctr_cb.addr_mgnt_cb;
118     tSMP_ENC    output;
119 
120     BTM_TRACE_EVENT ("btm_gen_resolve_paddr_low");
121     if (p) {
122         p->param_buf[2] &= (~BLE_RESOLVE_ADDR_MASK);
123         p->param_buf[2] |= BLE_RESOLVE_ADDR_MSB;
124 
125         p_cb->private_addr[2] = p->param_buf[0];
126         p_cb->private_addr[1] = p->param_buf[1];
127         p_cb->private_addr[0] = p->param_buf[2];
128 
129         /* encrypt with ur IRK */
130         if (!SMP_Encrypt(btm_cb.devcb.id_keys.irk, BT_OCTET16_LEN, p->param_buf, 3, &output)) {
131             btm_gen_resolve_paddr_cmpl(NULL);
132         } else {
133             btm_gen_resolve_paddr_cmpl(&output);
134         }
135     }
136 #endif
137 }
138 /*******************************************************************************
139 **
140 ** Function         btm_gen_resolvable_private_addr
141 **
142 ** Description      This function generate a resolvable private address.
143 **
144 ** Returns          void
145 **
146 *******************************************************************************/
btm_gen_resolvable_private_addr(void * p_cmd_cplt_cback)147 void btm_gen_resolvable_private_addr (void *p_cmd_cplt_cback)
148 {
149     BTM_TRACE_EVENT ("btm_gen_resolvable_private_addr");
150     /* generate 3B rand as BD LSB, SRK with it, get BD MSB */
151     if (!btsnd_hcic_ble_rand((void *)p_cmd_cplt_cback)) {
152         btm_gen_resolve_paddr_cmpl(NULL);
153     }
154 }
155 /*******************************************************************************
156 **
157 ** Function         btm_gen_non_resolve_paddr_cmpl
158 **
159 ** Description      This is the callback function when non-resolvable private
160 **                  function is generated and write to controller.
161 **
162 ** Returns          void
163 **
164 *******************************************************************************/
btm_gen_non_resolve_paddr_cmpl(tBTM_RAND_ENC * p)165 static void btm_gen_non_resolve_paddr_cmpl(tBTM_RAND_ENC *p)
166 {
167     tBTM_LE_RANDOM_CB *p_cb = &btm_cb.ble_ctr_cb.addr_mgnt_cb;
168     tBTM_BLE_ADDR_CBACK *p_cback = p_cb->p_generate_cback;
169     void    *p_data = p_cb->p;
170     UINT8   *pp;
171     BD_ADDR     static_random;
172 
173     BTM_TRACE_EVENT ("btm_gen_non_resolve_paddr_cmpl");
174 
175     p_cb->p_generate_cback = NULL;
176     if (p) {
177 
178         pp = p->param_buf;
179         STREAM_TO_BDADDR(static_random, pp);
180         /* mask off the 2 MSB */
181         static_random[0] &= BLE_STATIC_PRIVATE_MSB_MASK;
182 
183         /* report complete */
184         if (p_cback) {
185             (* p_cback)(static_random, p_data);
186         }
187     } else {
188         BTM_TRACE_DEBUG("btm_gen_non_resolvable_private_addr failed");
189         if (p_cback) {
190             (* p_cback)(NULL, p_data);
191         }
192     }
193 }
194 /*******************************************************************************
195 **
196 ** Function         btm_gen_non_resolvable_private_addr
197 **
198 ** Description      This function generate a non-resolvable private address.
199 **
200 **
201 ** Returns          void
202 **
203 *******************************************************************************/
btm_gen_non_resolvable_private_addr(tBTM_BLE_ADDR_CBACK * p_cback,void * p)204 void btm_gen_non_resolvable_private_addr (tBTM_BLE_ADDR_CBACK *p_cback, void *p)
205 {
206     tBTM_LE_RANDOM_CB   *p_mgnt_cb = &btm_cb.ble_ctr_cb.addr_mgnt_cb;
207 
208     BTM_TRACE_EVENT ("btm_gen_non_resolvable_private_addr");
209 
210     if (p_mgnt_cb->p_generate_cback != NULL) {
211         return;
212     }
213 
214     p_mgnt_cb->p_generate_cback = p_cback;
215     p_mgnt_cb->p                = p;
216     if (!btsnd_hcic_ble_rand((void *)btm_gen_non_resolve_paddr_cmpl)) {
217         btm_gen_non_resolve_paddr_cmpl(NULL);
218     }
219 
220 }
221 
222 /*******************************************************************************
223 **  Utility functions for Random address resolving
224 *******************************************************************************/
225 /*******************************************************************************
226 **
227 ** Function         btm_ble_resolve_address_cmpl
228 **
229 ** Description      This function sends the random address resolving complete
230 **                  callback.
231 **
232 ** Returns          None.
233 **
234 *******************************************************************************/
235 #if SMP_INCLUDED == TRUE
btm_ble_resolve_address_cmpl(void)236 static void btm_ble_resolve_address_cmpl(void)
237 {
238     tBTM_LE_RANDOM_CB   *p_mgnt_cb = &btm_cb.ble_ctr_cb.addr_mgnt_cb;
239 
240     BTM_TRACE_EVENT ("btm_ble_resolve_address_cmpl p_mgnt_cb->p_dev_rec = 0x%08x", (uint32_t)p_mgnt_cb->p_dev_rec);
241 
242     p_mgnt_cb->busy = FALSE;
243 
244     (* p_mgnt_cb->p_resolve_cback)(p_mgnt_cb->p_dev_rec, p_mgnt_cb->p);
245 }
246 /*******************************************************************************
247 **
248 ** Function         btm_ble_proc_resolve_x
249 **
250 ** Description      This function compares the X with random address 3 MSO bytes
251 **                  to find a match, if not match, continue for next record.
252 **
253 ** Returns          None.
254 **
255 *******************************************************************************/
btm_ble_proc_resolve_x(tSMP_ENC * p)256 static BOOLEAN btm_ble_proc_resolve_x(tSMP_ENC *p)
257 {
258     tBTM_LE_RANDOM_CB   *p_mgnt_cb = &btm_cb.ble_ctr_cb.addr_mgnt_cb;
259     UINT8    comp[3];
260     BTM_TRACE_EVENT ("btm_ble_proc_resolve_x");
261     /* compare the hash with 3 LSB of bd address */
262     comp[0] = p_mgnt_cb->random_bda[5];
263     comp[1] = p_mgnt_cb->random_bda[4];
264     comp[2] = p_mgnt_cb->random_bda[3];
265 
266     if (p) {
267         if (!memcmp(p->param_buf, &comp[0], 3)) {
268             /* match is found */
269             BTM_TRACE_EVENT ("match is found");
270             btm_ble_resolve_address_cmpl();
271             return TRUE;
272         }
273     }
274 
275     return FALSE;
276 }
277 #endif  ///SMP_INCLUDED == TRUE
278 
279 /*******************************************************************************
280 **
281 ** Function         btm_ble_init_pseudo_addr
282 **
283 ** Description      This function is used to initialize pseudo address.
284 **                  If pseudo address is not available, use dummy address
285 **
286 ** Returns          TRUE is updated; FALSE otherwise.
287 **
288 *******************************************************************************/
btm_ble_init_pseudo_addr(tBTM_SEC_DEV_REC * p_dev_rec,BD_ADDR new_pseudo_addr)289 BOOLEAN btm_ble_init_pseudo_addr (tBTM_SEC_DEV_REC *p_dev_rec, BD_ADDR new_pseudo_addr)
290 {
291 #if (SMP_INCLUDED == TRUE)
292     BD_ADDR dummy_bda = {0};
293 
294     if (memcmp(p_dev_rec->ble.pseudo_addr, dummy_bda, BD_ADDR_LEN) == 0) {
295         memcpy(p_dev_rec->ble.pseudo_addr, new_pseudo_addr, BD_ADDR_LEN);
296         return TRUE;
297     }
298 #endif  ///SMP_INCLUDED == TRUE
299     return FALSE;
300 }
301 
302 /*******************************************************************************
303 **
304 ** Function         btm_ble_addr_resolvable
305 **
306 ** Description      This function checks if a RPA is resolvable by the device key.
307 **
308 ** Returns          TRUE is resolvable; FALSE otherwise.
309 **
310 *******************************************************************************/
btm_ble_addr_resolvable(BD_ADDR rpa,tBTM_SEC_DEV_REC * p_dev_rec)311 BOOLEAN btm_ble_addr_resolvable (BD_ADDR rpa, tBTM_SEC_DEV_REC *p_dev_rec)
312 {
313     BOOLEAN rt = FALSE;
314 #if (SMP_INCLUDED == TRUE)
315     if (!BTM_BLE_IS_RESOLVE_BDA(rpa)) {
316         return rt;
317     }
318 
319     UINT8 rand[3];
320     tSMP_ENC output;
321     if ((p_dev_rec->device_type & BT_DEVICE_TYPE_BLE) &&
322             (p_dev_rec->ble.key_type & BTM_LE_KEY_PID)) {
323         BTM_TRACE_DEBUG("%s try to resolve", __func__);
324         /* use the 3 MSB of bd address as prand */
325         rand[0] = rpa[2];
326         rand[1] = rpa[1];
327         rand[2] = rpa[0];
328 
329         /* generate X = E irk(R0, R1, R2) and R is random address 3 LSO */
330         SMP_Encrypt(p_dev_rec->ble.keys.irk, BT_OCTET16_LEN,
331                     &rand[0], 3, &output);
332 
333         rand[0] = rpa[5];
334         rand[1] = rpa[4];
335         rand[2] = rpa[3];
336 
337         if (!memcmp(output.param_buf, &rand[0], 3)) {
338             btm_ble_init_pseudo_addr (p_dev_rec, rpa);
339             rt = TRUE;
340         }
341     }
342 #endif  ///SMP_INCLUDED == TRUE
343     return rt;
344 }
345 
346 #if (BLE_INCLUDED == TRUE && SMP_INCLUDED == TRUE)
347 /*******************************************************************************
348 **
349 ** Function         btm_ble_match_random_bda
350 **
351 ** Description      This function match the random address to the appointed device
352 **                  record, starting from calculating IRK. If record index exceed
353 **                  the maximum record number, matching failed and send callback.
354 **
355 ** Returns          None.
356 **
357 *******************************************************************************/
btm_ble_match_random_bda(tBTM_SEC_DEV_REC * p_dev_rec)358 static BOOLEAN btm_ble_match_random_bda(tBTM_SEC_DEV_REC *p_dev_rec)
359 {
360     /* use the 3 MSB of bd address as prand */
361 
362     tBTM_LE_RANDOM_CB *p_mgnt_cb = &btm_cb.ble_ctr_cb.addr_mgnt_cb;
363     UINT8 rand[3];
364     rand[0] = p_mgnt_cb->random_bda[2];
365     rand[1] = p_mgnt_cb->random_bda[1];
366     rand[2] = p_mgnt_cb->random_bda[0];
367 
368     BTM_TRACE_EVENT("%s p_dev_rec = 0x%08x", __func__, (uint32_t)p_dev_rec);
369 
370     {
371         tSMP_ENC output;
372 
373         BTM_TRACE_DEBUG("sec_flags = %02x device_type = %d", p_dev_rec->sec_flags,
374                         p_dev_rec->device_type);
375 
376         if ((p_dev_rec->device_type & BT_DEVICE_TYPE_BLE) &&
377                 (p_dev_rec->ble.key_type & BTM_LE_KEY_PID)) {
378             /* generate X = E irk(R0, R1, R2) and R is random address 3 LSO */
379             SMP_Encrypt(p_dev_rec->ble.keys.irk, BT_OCTET16_LEN,
380                         &rand[0], 3, &output);
381             return btm_ble_proc_resolve_x(&output);
382         } else {
383             // not completed
384             return FALSE;
385         }
386     }
387 }
388 #endif ///BLE_INCLUDED == TRUE && SMP_INCLUDED == TRUE
389 
390 /*******************************************************************************
391 **
392 ** Function         btm_ble_resolve_random_addr
393 **
394 ** Description      This function is called to resolve a random address.
395 **
396 ** Returns          pointer to the security record of the device whom a random
397 **                  address is matched to.
398 **
399 *******************************************************************************/
btm_ble_resolve_random_addr(BD_ADDR random_bda,tBTM_BLE_RESOLVE_CBACK * p_cback,void * p)400 void btm_ble_resolve_random_addr(BD_ADDR random_bda, tBTM_BLE_RESOLVE_CBACK *p_cback, void *p)
401 {
402 #if (SMP_INCLUDED == TRUE)
403     tBTM_LE_RANDOM_CB   *p_mgnt_cb = &btm_cb.ble_ctr_cb.addr_mgnt_cb;
404     list_node_t         *p_node    = NULL;
405     tBTM_SEC_DEV_REC *p_dev_rec    = NULL;
406 
407     BTM_TRACE_EVENT ("btm_ble_resolve_random_addr");
408     if ( !p_mgnt_cb->busy) {
409         p_mgnt_cb->p = p;
410         p_mgnt_cb->busy = TRUE;
411         p_mgnt_cb->p_dev_rec = NULL;
412         p_mgnt_cb->p_resolve_cback = p_cback;
413         memcpy(p_mgnt_cb->random_bda, random_bda, BD_ADDR_LEN);
414         /* start to resolve random address */
415         /* check for next security record */
416         for (p_node = list_begin(btm_cb.p_sec_dev_rec_list); p_node; p_node = list_next(p_node)) {
417             p_dev_rec = list_node(p_node);
418             p_mgnt_cb->p_dev_rec = p_dev_rec;
419             if (btm_ble_match_random_bda(p_dev_rec)) {
420                 break;
421             }
422             p_mgnt_cb->p_dev_rec = NULL;
423         }
424         btm_ble_resolve_address_cmpl();
425     } else {
426         (*p_cback)(NULL, p);
427     }
428 #endif
429 
430 }
431 
432 
433 /*******************************************************************************
434 **  address mapping between pseudo address and real connection address
435 *******************************************************************************/
436 /*******************************************************************************
437 **
438 ** Function         btm_find_dev_by_identity_addr
439 **
440 ** Description      find the security record whose LE static address is matching
441 **
442 *******************************************************************************/
btm_find_dev_by_identity_addr(BD_ADDR bd_addr,UINT8 addr_type)443 tBTM_SEC_DEV_REC *btm_find_dev_by_identity_addr(BD_ADDR bd_addr, UINT8 addr_type)
444 {
445 #if BLE_PRIVACY_SPT == TRUE
446     tBTM_SEC_DEV_REC *p_dev_rec = NULL;
447     list_node_t *p_node         = NULL;
448     tSecDevContext context;
449     context.type                = SEC_DEV_ID_ADDR;
450     context.context.p_bd_addr   = bd_addr;
451     context.free_check          = FALSE;
452     p_node = list_foreach(btm_cb.p_sec_dev_rec_list, btm_find_sec_dev_in_list, &context);
453     if (p_node) {
454 	p_dev_rec = list_node(p_node);
455         if ((p_dev_rec->ble.static_addr_type & (~BLE_ADDR_TYPE_ID_BIT)) !=
456                 (addr_type & (~BLE_ADDR_TYPE_ID_BIT))) {
457             BTM_TRACE_WARNING("%s find pseudo->random match with diff addr type: %d vs %d",
458                               __func__, p_dev_rec->ble.static_addr_type, addr_type);
459 	}
460     }
461     return p_dev_rec;
462 #endif
463     return NULL;
464 }
465 
466 /*******************************************************************************
467 **
468 ** Function         btm_identity_addr_to_random_pseudo
469 **
470 ** Description      This function map a static BD address to a pseudo random address
471 **                  in security database.
472 **
473 *******************************************************************************/
btm_identity_addr_to_random_pseudo(BD_ADDR bd_addr,UINT8 * p_addr_type,BOOLEAN refresh)474 BOOLEAN btm_identity_addr_to_random_pseudo(BD_ADDR bd_addr, UINT8 *p_addr_type, BOOLEAN refresh)
475 {
476 #if BLE_PRIVACY_SPT == TRUE
477     tBTM_SEC_DEV_REC    *p_dev_rec = btm_find_dev_by_identity_addr(bd_addr, *p_addr_type);
478 
479     BTM_TRACE_EVENT ("%s", __func__);
480     /* evt reported on static address, map static address to random pseudo */
481     if (p_dev_rec != NULL) {
482         /* if RPA offloading is supported, or 4.2 controller, do RPA refresh */
483         if (refresh && controller_get_interface()->get_ble_resolving_list_max_size() != 0) {
484             btm_ble_read_resolving_list_entry(p_dev_rec);
485         }
486 
487         /* assign the original address to be the current report address */
488         if (!btm_ble_init_pseudo_addr (p_dev_rec, bd_addr)) {
489             memcpy(bd_addr, p_dev_rec->ble.pseudo_addr, BD_ADDR_LEN);
490         }
491 
492         *p_addr_type = p_dev_rec->ble.ble_addr_type;
493         return TRUE;
494     }
495 #endif
496     return FALSE;
497 }
498 
499 /*******************************************************************************
500 **
501 ** Function         btm_random_pseudo_to_identity_addr
502 **
503 ** Description      This function map a random pseudo address to a public address
504 **                  random_pseudo is input and output parameter
505 **
506 *******************************************************************************/
btm_random_pseudo_to_identity_addr(BD_ADDR random_pseudo,UINT8 * p_static_addr_type)507 BOOLEAN btm_random_pseudo_to_identity_addr(BD_ADDR random_pseudo, UINT8 *p_static_addr_type)
508 {
509 #if BLE_PRIVACY_SPT == TRUE
510     tBTM_SEC_DEV_REC    *p_dev_rec = btm_find_dev (random_pseudo);
511 
512     if (p_dev_rec != NULL) {
513         if (p_dev_rec->ble.in_controller_list & BTM_RESOLVING_LIST_BIT) {
514             * p_static_addr_type = p_dev_rec->ble.static_addr_type;
515             memcpy(random_pseudo, p_dev_rec->ble.static_addr, BD_ADDR_LEN);
516             if (controller_get_interface()->supports_ble_privacy() && p_dev_rec->ble.ble_addr_type != BLE_ADDR_PUBLIC) {
517                 *p_static_addr_type |= BLE_ADDR_TYPE_ID_BIT;
518             }
519             return TRUE;
520         }
521     }
522 #endif
523     return FALSE;
524 }
525 
526 /*******************************************************************************
527 **
528 ** Function         btm_ble_refresh_peer_resolvable_private_addr
529 **
530 ** Description      This function refresh the currently used resolvable remote private address into security
531 **                  database and set active connection address.
532 **
533 *******************************************************************************/
btm_ble_refresh_peer_resolvable_private_addr(BD_ADDR pseudo_bda,BD_ADDR rpa,UINT8 rra_type)534 void btm_ble_refresh_peer_resolvable_private_addr(BD_ADDR pseudo_bda, BD_ADDR rpa,
535         UINT8 rra_type)
536 {
537 #if BLE_PRIVACY_SPT == TRUE
538     UINT8 rra_dummy = FALSE;
539     BD_ADDR dummy_bda = {0};
540 
541     if (memcmp(dummy_bda, rpa, BD_ADDR_LEN) == 0) {
542         rra_dummy = TRUE;
543     }
544 
545     /* update security record here, in adv event or connection complete process */
546     tBTM_SEC_DEV_REC *p_sec_rec = btm_find_dev(pseudo_bda);
547     if (p_sec_rec != NULL) {
548         memcpy(p_sec_rec->ble.cur_rand_addr, rpa, BD_ADDR_LEN);
549 
550         /* unknown, if dummy address, set to static */
551         if (rra_type == BTM_BLE_ADDR_PSEUDO) {
552             p_sec_rec->ble.active_addr_type = rra_dummy ? BTM_BLE_ADDR_STATIC : BTM_BLE_ADDR_RRA;
553         } else {
554             p_sec_rec->ble.active_addr_type = rra_type;
555         }
556     } else {
557         BTM_TRACE_ERROR("No matching known device in record");
558         return;
559     }
560 
561     BTM_TRACE_DEBUG("%s: active_addr_type: %d ",
562                     __func__, p_sec_rec->ble.active_addr_type);
563 
564     /* connection refresh remote address */
565     tACL_CONN *p_acl = btm_bda_to_acl(p_sec_rec->bd_addr, BT_TRANSPORT_LE);
566     if (p_acl == NULL) {
567         p_acl = btm_bda_to_acl(p_sec_rec->ble.pseudo_addr, BT_TRANSPORT_LE);
568     }
569 
570     if (p_acl != NULL) {
571         if (rra_type == BTM_BLE_ADDR_PSEUDO) {
572             /* use static address, resolvable_private_addr is empty */
573             if (rra_dummy) {
574                 p_acl->active_remote_addr_type = p_sec_rec->ble.static_addr_type;
575                 memcpy(p_acl->active_remote_addr, p_sec_rec->ble.static_addr, BD_ADDR_LEN);
576             } else {
577                 p_acl->active_remote_addr_type = BLE_ADDR_RANDOM;
578                 memcpy(p_acl->active_remote_addr, rpa, BD_ADDR_LEN);
579             }
580         } else {
581             p_acl->active_remote_addr_type = rra_type;
582             memcpy(p_acl->active_remote_addr, rpa, BD_ADDR_LEN);
583         }
584 
585         BTM_TRACE_DEBUG("p_acl->active_remote_addr_type: %d ", p_acl->active_remote_addr_type);
586         BTM_TRACE_DEBUG("%s conn_addr: %02x:%02x:%02x:%02x:%02x:%02x",
587                         __func__, p_acl->active_remote_addr[0], p_acl->active_remote_addr[1],
588                         p_acl->active_remote_addr[2], p_acl->active_remote_addr[3],
589                         p_acl->active_remote_addr[4], p_acl->active_remote_addr[5]);
590     }
591 #endif
592 }
593 
594 /*******************************************************************************
595 **
596 ** Function         btm_ble_refresh_local_resolvable_private_addr
597 **
598 ** Description      This function refresh the currently used resolvable private address for the
599 **                  active link to the remote device
600 **
601 *******************************************************************************/
btm_ble_refresh_local_resolvable_private_addr(BD_ADDR pseudo_addr,BD_ADDR local_rpa)602 void btm_ble_refresh_local_resolvable_private_addr(BD_ADDR pseudo_addr,
603         BD_ADDR local_rpa)
604 {
605 #if BLE_PRIVACY_SPT == TRUE
606     tACL_CONN *p = btm_bda_to_acl(pseudo_addr, BT_TRANSPORT_LE);
607     BD_ADDR     dummy_bda = {0};
608 
609     if (p != NULL) {
610         if (btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type == BLE_ADDR_RANDOM) {
611             p->conn_addr_type = BLE_ADDR_RANDOM;
612             if (memcmp(local_rpa, dummy_bda, BD_ADDR_LEN)) {
613                 memcpy(p->conn_addr, local_rpa, BD_ADDR_LEN);
614             } else {
615                 memcpy(p->conn_addr, btm_cb.ble_ctr_cb.addr_mgnt_cb.private_addr, BD_ADDR_LEN);
616             }
617         } else {
618             p->conn_addr_type = BLE_ADDR_PUBLIC;
619             memcpy(p->conn_addr, &controller_get_interface()->get_address()->address, BD_ADDR_LEN);
620         }
621     }
622 #endif
623 }
624 #endif
625