btm_ble_addr.c 22 KB

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