btm_dev.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  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 the Bluetooth Device Manager
  21. *
  22. ******************************************************************************/
  23. #include <stdlib.h>
  24. #include <string.h>
  25. //#include <stdio.h>
  26. #include <stddef.h>
  27. #include "stack/bt_types.h"
  28. #include "device/controller.h"
  29. #include "stack/hcimsgs.h"
  30. #include "stack/btu.h"
  31. #include "stack/btm_api.h"
  32. #include "btm_int.h"
  33. #include "stack/hcidefs.h"
  34. #include "stack/l2c_api.h"
  35. static tBTM_SEC_DEV_REC *btm_find_oldest_dev (void);
  36. /*******************************************************************************
  37. **
  38. ** Function BTM_SecAddDevice
  39. **
  40. ** Description Add/modify device. This function will be normally called
  41. ** during host startup to restore all required information
  42. ** stored in the NVRAM.
  43. **
  44. ** Parameters: bd_addr - BD address of the peer
  45. ** dev_class - Device Class
  46. ** bd_name - Name of the peer device. NULL if unknown.
  47. ** features - Remote device's features (up to 3 pages). NULL if not known
  48. ** trusted_mask - Bitwise OR of services that do not
  49. ** require authorization. (array of UINT32)
  50. ** link_key - Connection link key. NULL if unknown.
  51. **
  52. ** Returns TRUE if added OK, else FALSE
  53. **
  54. *******************************************************************************/
  55. BOOLEAN BTM_SecAddDevice (BD_ADDR bd_addr, DEV_CLASS dev_class, BD_NAME bd_name,
  56. UINT8 *features, UINT32 trusted_mask[],
  57. LINK_KEY link_key, UINT8 key_type, tBTM_IO_CAP io_cap,
  58. UINT8 pin_length, UINT8 sc_support)
  59. {
  60. #if (SMP_INCLUDED == TRUE)
  61. tBTM_SEC_DEV_REC *p_dev_rec;
  62. int i, j;
  63. BOOLEAN found = FALSE;
  64. BTM_TRACE_API("%s, link key type:%x\n", __FUNCTION__, key_type);
  65. p_dev_rec = btm_find_dev (bd_addr);
  66. if (!p_dev_rec) {
  67. /* There is no device record, allocate one.
  68. * If we can not find an empty spot for this one, let it fail. */
  69. if (list_length(btm_cb.p_sec_dev_rec_list) < BTM_SEC_MAX_DEVICE_RECORDS) {
  70. p_dev_rec = (tBTM_SEC_DEV_REC *)osi_malloc(sizeof(tBTM_SEC_DEV_REC));
  71. if(p_dev_rec) {
  72. list_append(btm_cb.p_sec_dev_rec_list, p_dev_rec);
  73. /* Mark this record as in use and initialize */
  74. memset (p_dev_rec, 0, sizeof (tBTM_SEC_DEV_REC));
  75. p_dev_rec->sec_flags = BTM_SEC_IN_USE;
  76. memcpy (p_dev_rec->bd_addr, bd_addr, BD_ADDR_LEN);
  77. p_dev_rec->hci_handle = BTM_GetHCIConnHandle (bd_addr, BT_TRANSPORT_BR_EDR);
  78. p_dev_rec->ble_hci_handle = BTM_GetHCIConnHandle (bd_addr, BT_TRANSPORT_LE);
  79. #if BLE_INCLUDED == TRUE
  80. /* use default value for background connection params */
  81. /* update conn params, use default value for background connection params */
  82. memset(&p_dev_rec->conn_params, 0xff, sizeof(tBTM_LE_CONN_PRAMS));
  83. #endif
  84. }
  85. }
  86. if (!p_dev_rec) {
  87. return (FALSE);
  88. }
  89. }
  90. p_dev_rec->bond_type = BOND_TYPE_UNKNOWN; /* Default value */
  91. p_dev_rec->timestamp = btm_cb.dev_rec_count++;
  92. p_dev_rec->remote_secure_connection_previous_state = sc_support;
  93. if (dev_class) {
  94. memcpy (p_dev_rec->dev_class, dev_class, DEV_CLASS_LEN);
  95. }
  96. memset(p_dev_rec->sec_bd_name, 0, sizeof(tBTM_BD_NAME));
  97. if (bd_name && bd_name[0]) {
  98. p_dev_rec->sec_flags |= BTM_SEC_NAME_KNOWN;
  99. BCM_STRNCPY_S ((char *)p_dev_rec->sec_bd_name, (char *)bd_name, BTM_MAX_REM_BD_NAME_LEN);
  100. }
  101. p_dev_rec->num_read_pages = 0;
  102. if (features) {
  103. memcpy (p_dev_rec->features, features, sizeof (p_dev_rec->features));
  104. for (i = HCI_EXT_FEATURES_PAGE_MAX; i >= 0; i--) {
  105. for (j = 0; j < HCI_FEATURE_BYTES_PER_PAGE; j++) {
  106. if (p_dev_rec->features[i][j] != 0) {
  107. found = TRUE;
  108. break;
  109. }
  110. }
  111. if (found) {
  112. p_dev_rec->num_read_pages = i + 1;
  113. break;
  114. }
  115. }
  116. } else {
  117. memset (p_dev_rec->features, 0, sizeof (p_dev_rec->features));
  118. }
  119. BTM_SEC_COPY_TRUSTED_DEVICE(trusted_mask, p_dev_rec->trusted_mask);
  120. if (link_key) {
  121. BTM_TRACE_EVENT ("BTM_SecAddDevice() BDA: %02x:%02x:%02x:%02x:%02x:%02x\n",
  122. bd_addr[0], bd_addr[1], bd_addr[2],
  123. bd_addr[3], bd_addr[4], bd_addr[5]);
  124. p_dev_rec->sec_flags |= BTM_SEC_LINK_KEY_KNOWN;
  125. memcpy (p_dev_rec->link_key, link_key, LINK_KEY_LEN);
  126. p_dev_rec->link_key_type = key_type;
  127. p_dev_rec->pin_code_length = pin_length;
  128. if (pin_length >= 16 ||
  129. key_type == BTM_LKEY_TYPE_AUTH_COMB ||
  130. key_type == BTM_LKEY_TYPE_AUTH_COMB_P_256) {
  131. // Set the fiag if the link key was made by using either a 16 digit
  132. // pin or MITM.
  133. p_dev_rec->sec_flags |= BTM_SEC_16_DIGIT_PIN_AUTHED;
  134. }
  135. }
  136. #if defined(BTIF_MIXED_MODE_INCLUDED) && (BTIF_MIXED_MODE_INCLUDED == TRUE)
  137. if (key_type < BTM_MAX_PRE_SM4_LKEY_TYPE) {
  138. p_dev_rec->sm4 = BTM_SM4_KNOWN;
  139. } else {
  140. p_dev_rec->sm4 = BTM_SM4_TRUE;
  141. }
  142. #endif
  143. p_dev_rec->rmt_io_caps = io_cap;
  144. p_dev_rec->device_type |= BT_DEVICE_TYPE_BREDR;
  145. #endif ///SMP_INCLUDED == TRUE
  146. return (TRUE);
  147. }
  148. /*******************************************************************************
  149. **
  150. ** Function BTM_SecDeleteDevice
  151. **
  152. ** Description Free resources associated with the device.
  153. **
  154. ** Parameters: bd_addr - BD address of the peer
  155. ** transport - BT_TRANSPORT_BR_EDR or BT_TRANSPORT_LE
  156. **
  157. ** Returns TRUE if removed OK, FALSE if not found or ACL link is active
  158. **
  159. *******************************************************************************/
  160. BOOLEAN BTM_SecDeleteDevice (BD_ADDR bd_addr, tBT_TRANSPORT transport)
  161. {
  162. tBTM_SEC_DEV_REC *p_dev_rec;
  163. if (BTM_IsAclConnectionUp(bd_addr, transport)) {
  164. BTM_TRACE_WARNING("%s FAILED: Cannot Delete when connection is active\n", __func__);
  165. return FALSE;
  166. }
  167. if ((p_dev_rec = btm_find_dev(bd_addr)) != NULL) {
  168. /* Tell controller to get rid of the link key, if it has one stored */
  169. BTM_DeleteStoredLinkKey (p_dev_rec->bd_addr, NULL);
  170. btm_sec_free_dev(p_dev_rec, transport);
  171. }
  172. return TRUE;
  173. }
  174. /*******************************************************************************
  175. **
  176. ** Function BTM_SecClearSecurityFlags
  177. **
  178. ** Description Reset the security flags (mark as not-paired) for a given
  179. ** remove device.
  180. **
  181. *******************************************************************************/
  182. extern void BTM_SecClearSecurityFlags (BD_ADDR bd_addr)
  183. {
  184. tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev(bd_addr);
  185. if (p_dev_rec == NULL) {
  186. return;
  187. }
  188. p_dev_rec->sec_flags = 0;
  189. p_dev_rec->sec_state = BTM_SEC_STATE_IDLE;
  190. p_dev_rec->sm4 = BTM_SM4_UNKNOWN;
  191. }
  192. /*******************************************************************************
  193. **
  194. ** Function BTM_SecReadDevName
  195. **
  196. ** Description Looks for the device name in the security database for the
  197. ** specified BD address.
  198. **
  199. ** Returns Pointer to the name or NULL
  200. **
  201. *******************************************************************************/
  202. char *BTM_SecReadDevName (BD_ADDR bd_addr)
  203. {
  204. char *p_name = NULL;
  205. tBTM_SEC_DEV_REC *p_srec;
  206. if ((p_srec = btm_find_dev(bd_addr)) != NULL) {
  207. p_name = (char *)p_srec->sec_bd_name;
  208. }
  209. return (p_name);
  210. }
  211. /*******************************************************************************
  212. **
  213. ** Function btm_find_sec_dev_in_list
  214. **
  215. ** Description Look for the record in the device database for the record
  216. ** with specified address
  217. **
  218. ** Returns Pointer to the record or NULL
  219. **
  220. *******************************************************************************/
  221. BOOLEAN btm_find_sec_dev_in_list (void *p_node_data, void *context)
  222. {
  223. tBTM_SEC_DEV_REC *p_sec_dev = (tBTM_SEC_DEV_REC *)p_node_data;
  224. BOOLEAN ret = TRUE;
  225. BOOLEAN dev_free = !(p_sec_dev->sec_flags & BTM_SEC_IN_USE);
  226. tSecDevContext *p_context = (tSecDevContext *)context;
  227. if (dev_free == p_context->free_check) {
  228. switch (p_context->type) {
  229. case SEC_DEV_BDA:
  230. if (!memcmp(p_context->context.p_bd_addr, p_sec_dev->bd_addr, BD_ADDR_LEN)) {
  231. ret = FALSE;
  232. }
  233. break;
  234. case SEC_DEV_HDL:
  235. if (p_context->context.handle == p_sec_dev->hci_handle
  236. #if BLE_INCLUDED == TRUE
  237. || (p_context->context.handle == p_sec_dev->ble_hci_handle)
  238. #endif
  239. ) {
  240. ret = FALSE;
  241. }
  242. break;
  243. #if BLE_PRIVACY_SPT == TRUE
  244. case SEC_DEV_ID_ADDR:
  245. if (!memcmp(p_context->context.p_bd_addr, p_sec_dev->ble.static_addr, BD_ADDR_LEN)) {
  246. ret = FALSE;
  247. }
  248. break;
  249. #endif //BLE_PRIVACY_SPT == TRUE
  250. case SEC_DEV_BTDM_BDA:
  251. if (!memcmp(p_context->context.p_bd_addr, p_sec_dev->bd_addr, BD_ADDR_LEN)) {
  252. ret = FALSE;
  253. }
  254. #if BLE_INCLUDED == TRUE
  255. // If a LE random address is looking for device record
  256. if (!memcmp(p_sec_dev->ble.pseudo_addr, p_context->context.p_bd_addr, BD_ADDR_LEN)) {
  257. ret = FALSE;
  258. }
  259. if (btm_ble_addr_resolvable(p_context->context.p_bd_addr, p_sec_dev)) {
  260. ret = FALSE;
  261. }
  262. #endif
  263. break;
  264. default:
  265. break;
  266. }
  267. }
  268. return ret;
  269. }
  270. /*******************************************************************************
  271. **
  272. ** Function btm_sec_alloc_dev
  273. **
  274. ** Description Look for the record in the device database for the record
  275. ** with specified address
  276. **
  277. ** Returns Pointer to the record or NULL
  278. **
  279. *******************************************************************************/
  280. tBTM_SEC_DEV_REC *btm_sec_alloc_dev (BD_ADDR bd_addr)
  281. {
  282. tBTM_SEC_DEV_REC *p_dev_rec = NULL;
  283. tBTM_INQ_INFO *p_inq_info;
  284. BTM_TRACE_EVENT ("btm_sec_alloc_dev\n");
  285. /* Old devices which are not in use are deleted already */
  286. /* Allocate new device or reuse the oldest device */
  287. if (list_length(btm_cb.p_sec_dev_rec_list) < BTM_SEC_MAX_DEVICE_RECORDS) {
  288. //Max number of devices is not exceeded, allocate new device
  289. p_dev_rec = (tBTM_SEC_DEV_REC *)osi_malloc(sizeof(tBTM_SEC_DEV_REC));
  290. if (p_dev_rec) {
  291. list_append(btm_cb.p_sec_dev_rec_list, p_dev_rec);
  292. } else {
  293. return NULL;
  294. }
  295. }
  296. else {
  297. //Find and reuse the oldest device
  298. p_dev_rec = btm_find_oldest_dev();
  299. }
  300. memset (p_dev_rec, 0, sizeof (tBTM_SEC_DEV_REC));
  301. p_dev_rec->bond_type = BOND_TYPE_UNKNOWN; /* Default value */
  302. p_dev_rec->sec_flags = BTM_SEC_IN_USE;
  303. /* Check with the BT manager if details about remote device are known */
  304. /* outgoing connection */
  305. if ((p_inq_info = BTM_InqDbRead(bd_addr)) != NULL) {
  306. memcpy (p_dev_rec->dev_class, p_inq_info->results.dev_class, DEV_CLASS_LEN);
  307. #if BLE_INCLUDED == TRUE
  308. p_dev_rec->device_type = p_inq_info->results.device_type;
  309. p_dev_rec->ble.ble_addr_type = p_inq_info->results.ble_addr_type;
  310. /* update conn params, use default value for background connection params */
  311. memset(&p_dev_rec->conn_params, 0xff, sizeof(tBTM_LE_CONN_PRAMS));
  312. #endif
  313. } else {
  314. #if BLE_INCLUDED == TRUE
  315. /* update conn params, use default value for background connection params */
  316. memset(&p_dev_rec->conn_params, 0xff, sizeof(tBTM_LE_CONN_PRAMS));
  317. #endif
  318. if (!memcmp (bd_addr, btm_cb.connecting_bda, BD_ADDR_LEN)) {
  319. memcpy (p_dev_rec->dev_class, btm_cb.connecting_dc, DEV_CLASS_LEN);
  320. }
  321. }
  322. memcpy (p_dev_rec->bd_addr, bd_addr, BD_ADDR_LEN);
  323. #if BLE_INCLUDED == TRUE
  324. p_dev_rec->ble_hci_handle = BTM_GetHCIConnHandle (bd_addr, BT_TRANSPORT_LE);
  325. #endif
  326. p_dev_rec->hci_handle = BTM_GetHCIConnHandle (bd_addr, BT_TRANSPORT_BR_EDR);
  327. p_dev_rec->timestamp = btm_cb.dev_rec_count++;
  328. return (p_dev_rec);
  329. }
  330. /*******************************************************************************
  331. **
  332. ** Function btm_sec_free_dev
  333. **
  334. ** Description Mark device record as not used
  335. **
  336. *******************************************************************************/
  337. void btm_sec_free_dev (tBTM_SEC_DEV_REC *p_dev_rec, tBT_TRANSPORT transport)
  338. {
  339. if (transport == BT_TRANSPORT_BR_EDR) {
  340. memset(p_dev_rec->link_key, 0, LINK_KEY_LEN);
  341. p_dev_rec->sec_flags &= ~(BTM_SEC_AUTHORIZED | BTM_SEC_AUTHENTICATED
  342. | BTM_SEC_ENCRYPTED | BTM_SEC_NAME_KNOWN
  343. | BTM_SEC_LINK_KEY_KNOWN | BTM_SEC_LINK_KEY_AUTHED
  344. | BTM_SEC_ROLE_SWITCHED | BTM_SEC_16_DIGIT_PIN_AUTHED);
  345. } else if (transport == BT_TRANSPORT_LE) {
  346. p_dev_rec->bond_type = BOND_TYPE_UNKNOWN;
  347. p_dev_rec->sec_flags &= ~(BTM_SEC_LE_AUTHENTICATED | BTM_SEC_LE_ENCRYPTED
  348. | BTM_SEC_LE_NAME_KNOWN | BTM_SEC_LE_LINK_KEY_KNOWN
  349. | BTM_SEC_LE_LINK_KEY_AUTHED | BTM_SEC_ROLE_SWITCHED);
  350. #if BLE_INCLUDED == TRUE
  351. /* Clear out any saved BLE keys */
  352. btm_sec_clear_ble_keys (p_dev_rec);
  353. #endif
  354. } else {
  355. p_dev_rec->bond_type = BOND_TYPE_UNKNOWN;
  356. memset(p_dev_rec->link_key, 0, LINK_KEY_LEN);
  357. p_dev_rec->sec_flags = 0;
  358. #if BLE_INCLUDED == TRUE
  359. /* Clear out any saved BLE keys */
  360. btm_sec_clear_ble_keys (p_dev_rec);
  361. #endif
  362. }
  363. /* No BLE keys and BT keys, clear the sec_flags */
  364. if(p_dev_rec->sec_flags == BTM_SEC_IN_USE) {
  365. p_dev_rec->sec_flags = 0;
  366. }
  367. list_remove(btm_cb.p_sec_dev_rec_list, p_dev_rec);
  368. }
  369. /*******************************************************************************
  370. **
  371. ** Function btm_dev_support_switch
  372. **
  373. ** Description This function is called by the L2CAP to check if remote
  374. ** device supports role switch
  375. **
  376. ** Parameters: bd_addr - Address of the peer device
  377. **
  378. ** Returns TRUE if device is known and role switch is supported
  379. **
  380. *******************************************************************************/
  381. BOOLEAN btm_dev_support_switch (BD_ADDR bd_addr)
  382. {
  383. tBTM_SEC_DEV_REC *p_dev_rec;
  384. UINT8 xx;
  385. BOOLEAN feature_empty = TRUE;
  386. #if BTM_SCO_INCLUDED == TRUE
  387. /* Role switch is not allowed if a SCO is up */
  388. if (btm_is_sco_active_by_bdaddr(bd_addr)) {
  389. return (FALSE);
  390. }
  391. #endif
  392. p_dev_rec = btm_find_dev (bd_addr);
  393. if (p_dev_rec && controller_get_interface()->supports_master_slave_role_switch()) {
  394. if (HCI_SWITCH_SUPPORTED(p_dev_rec->features[HCI_EXT_FEATURES_PAGE_0])) {
  395. BTM_TRACE_DEBUG("btm_dev_support_switch return TRUE (feature found)\n");
  396. return (TRUE);
  397. }
  398. /* If the feature field is all zero, we never received them */
  399. for (xx = 0 ; xx < BD_FEATURES_LEN ; xx++) {
  400. if (p_dev_rec->features[HCI_EXT_FEATURES_PAGE_0][xx] != 0x00) {
  401. feature_empty = FALSE; /* at least one is != 0 */
  402. break;
  403. }
  404. }
  405. /* If we don't know peer's capabilities, assume it supports Role-switch */
  406. if (feature_empty) {
  407. BTM_TRACE_DEBUG("btm_dev_support_switch return TRUE (feature empty)\n");
  408. return (TRUE);
  409. }
  410. }
  411. BTM_TRACE_DEBUG("btm_dev_support_switch return FALSE\n");
  412. return (FALSE);
  413. }
  414. /*******************************************************************************
  415. **
  416. ** Function btm_find_dev_by_handle
  417. **
  418. ** Description Look for the record in the device database for the record
  419. ** with specified handle
  420. **
  421. ** Returns Pointer to the record or NULL
  422. **
  423. *******************************************************************************/
  424. tBTM_SEC_DEV_REC *btm_find_dev_by_handle (UINT16 handle)
  425. {
  426. tBTM_SEC_DEV_REC *p_dev_rec = NULL;
  427. list_node_t *p_node = NULL;
  428. tSecDevContext context;
  429. context.type = SEC_DEV_HDL;
  430. context.context.handle = handle;
  431. context.free_check = FALSE;
  432. p_node = list_foreach(btm_cb.p_sec_dev_rec_list, btm_find_sec_dev_in_list, &context);
  433. if (p_node) {
  434. p_dev_rec = list_node(p_node);
  435. }
  436. return (p_dev_rec);
  437. }
  438. /*******************************************************************************
  439. **
  440. ** Function btm_find_dev
  441. **
  442. ** Description Look for the record in the device database for the record
  443. ** with specified BD address
  444. **
  445. ** Returns Pointer to the record or NULL
  446. **
  447. *******************************************************************************/
  448. tBTM_SEC_DEV_REC *btm_find_dev(BD_ADDR bd_addr)
  449. {
  450. if(bd_addr) {
  451. list_node_t *p_node = NULL;
  452. tSecDevContext context;
  453. context.type = SEC_DEV_BTDM_BDA;
  454. context.context.p_bd_addr = bd_addr;
  455. context.free_check = FALSE;
  456. p_node = list_foreach(btm_cb.p_sec_dev_rec_list, btm_find_sec_dev_in_list, &context);
  457. if (p_node) {
  458. return(list_node(p_node));
  459. }
  460. }
  461. return (NULL);
  462. }
  463. /*******************************************************************************
  464. **
  465. ** Function btm_consolidate_dev
  466. **
  467. ** Description combine security records if identified as same peer
  468. **
  469. ** Returns none
  470. **
  471. *******************************************************************************/
  472. void btm_consolidate_dev(tBTM_SEC_DEV_REC *p_target_rec)
  473. {
  474. #if BLE_INCLUDED == TRUE && SMP_INCLUDED == TRUE
  475. tBTM_SEC_DEV_REC *p_dev_rec = NULL;
  476. tBTM_SEC_DEV_REC temp_rec = *p_target_rec;
  477. list_node_t *p_node = NULL;
  478. BTM_TRACE_DEBUG("%s\n", __func__);
  479. for (p_node = list_begin(btm_cb.p_sec_dev_rec_list); p_node; p_node = list_next(p_node)) {
  480. p_dev_rec = list_node(p_node);
  481. if (p_target_rec != p_dev_rec && p_dev_rec->sec_flags & BTM_SEC_IN_USE) {
  482. if (!memcmp (p_dev_rec->bd_addr, p_target_rec->bd_addr, BD_ADDR_LEN)) {
  483. memcpy(p_target_rec, p_dev_rec, sizeof(tBTM_SEC_DEV_REC));
  484. p_target_rec->ble = temp_rec.ble;
  485. p_target_rec->ble_hci_handle = temp_rec.ble_hci_handle;
  486. p_target_rec->enc_key_size = temp_rec.enc_key_size;
  487. p_target_rec->conn_params = temp_rec.conn_params;
  488. p_target_rec->device_type |= temp_rec.device_type;
  489. p_target_rec->sec_flags |= temp_rec.sec_flags;
  490. p_target_rec->new_encryption_key_is_p256 = temp_rec.new_encryption_key_is_p256;
  491. p_target_rec->no_smp_on_br = temp_rec.no_smp_on_br;
  492. p_target_rec->bond_type = temp_rec.bond_type;
  493. /* Remove the unused device from the list */
  494. list_remove(btm_cb.p_sec_dev_rec_list, p_dev_rec);
  495. break;
  496. }
  497. /* an RPA device entry is a duplicate of the target record */
  498. if (btm_ble_addr_resolvable(p_dev_rec->bd_addr, p_target_rec)) {
  499. if (memcmp(p_target_rec->ble.pseudo_addr, p_dev_rec->bd_addr, BD_ADDR_LEN) == 0) {
  500. p_target_rec->ble.ble_addr_type = p_dev_rec->ble.ble_addr_type;
  501. p_target_rec->device_type |= p_dev_rec->device_type;
  502. /* Remove the unused device from the list */
  503. list_remove(btm_cb.p_sec_dev_rec_list, p_dev_rec);
  504. }
  505. break;
  506. }
  507. }
  508. }
  509. #endif
  510. }
  511. /*******************************************************************************
  512. **
  513. ** Function btm_find_or_alloc_dev
  514. **
  515. ** Description Look for the record in the device database for the record
  516. ** with specified BD address
  517. **
  518. ** Returns Pointer to the record or NULL
  519. **
  520. *******************************************************************************/
  521. tBTM_SEC_DEV_REC *btm_find_or_alloc_dev (BD_ADDR bd_addr)
  522. {
  523. tBTM_SEC_DEV_REC *p_dev_rec;
  524. BTM_TRACE_EVENT ("btm_find_or_alloc_dev\n");
  525. if ((p_dev_rec = btm_find_dev (bd_addr)) == NULL) {
  526. /* Allocate a new device record or reuse the oldest one */
  527. p_dev_rec = btm_sec_alloc_dev (bd_addr);
  528. }
  529. return (p_dev_rec);
  530. }
  531. /*******************************************************************************
  532. **
  533. ** Function btm_find_oldest_dev
  534. **
  535. ** Description Locates the oldest device in use. It first looks for
  536. ** the oldest non-paired device. If all devices are paired it
  537. ** deletes the oldest paired device.
  538. **
  539. ** Returns Pointer to the record or NULL
  540. **
  541. *******************************************************************************/
  542. tBTM_SEC_DEV_REC *btm_find_oldest_dev (void)
  543. {
  544. tBTM_SEC_DEV_REC *p_dev_rec = NULL;
  545. tBTM_SEC_DEV_REC *p_oldest = NULL;
  546. list_node_t *p_node = NULL;
  547. UINT32 ot = 0xFFFFFFFF;
  548. /* First look for the non-paired devices for the oldest entry */
  549. for (p_node = list_begin(btm_cb.p_sec_dev_rec_list); p_node; p_node = list_next(p_node)) {
  550. p_dev_rec = list_node(p_node);
  551. if (((p_dev_rec->sec_flags & BTM_SEC_IN_USE) == 0)
  552. || ((p_dev_rec->sec_flags & (BTM_SEC_LINK_KEY_KNOWN | BTM_SEC_LE_LINK_KEY_KNOWN)) != 0)) {
  553. continue; /* Device is paired so skip it */
  554. }
  555. if (p_dev_rec->timestamp < ot) {
  556. p_oldest = p_dev_rec;
  557. ot = p_dev_rec->timestamp;
  558. }
  559. }
  560. if (ot != 0xFFFFFFFF) {
  561. return (p_oldest);
  562. }
  563. /* All devices are paired; find the oldest */
  564. for (p_node = list_begin(btm_cb.p_sec_dev_rec_list); p_node; p_node = list_next(p_node)) {
  565. if ((p_dev_rec->sec_flags & BTM_SEC_IN_USE) == 0) {
  566. continue;
  567. }
  568. if (p_dev_rec->timestamp < ot) {
  569. p_oldest = p_dev_rec;
  570. ot = p_dev_rec->timestamp;
  571. }
  572. }
  573. return (p_oldest);
  574. }
  575. /*******************************************************************************
  576. **
  577. ** Function btm_get_bond_type_dev
  578. **
  579. ** Description Get the bond type for a device in the device database
  580. ** with specified BD address
  581. **
  582. ** Returns The device bond type if known, otherwise BOND_TYPE_UNKNOWN
  583. **
  584. *******************************************************************************/
  585. tBTM_BOND_TYPE btm_get_bond_type_dev(BD_ADDR bd_addr)
  586. {
  587. tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev(bd_addr);
  588. if (p_dev_rec == NULL) {
  589. return BOND_TYPE_UNKNOWN;
  590. }
  591. return p_dev_rec->bond_type;
  592. }
  593. /*******************************************************************************
  594. **
  595. ** Function btm_set_bond_type_dev
  596. **
  597. ** Description Set the bond type for a device in the device database
  598. ** with specified BD address
  599. **
  600. ** Returns TRUE on success, otherwise FALSE
  601. **
  602. *******************************************************************************/
  603. BOOLEAN btm_set_bond_type_dev(BD_ADDR bd_addr, tBTM_BOND_TYPE bond_type)
  604. {
  605. tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev(bd_addr);
  606. if (p_dev_rec == NULL) {
  607. return FALSE;
  608. }
  609. p_dev_rec->bond_type = bond_type;
  610. return TRUE;
  611. }
  612. /*******************************************************************************
  613. **
  614. ** Function btm_sec_dev_init
  615. **
  616. ** Description Create new linked list for dynamic allocation on sec_dev_rec
  617. **
  618. *******************************************************************************/
  619. void btm_sec_dev_init(void)
  620. {
  621. btm_cb.p_sec_dev_rec_list = list_new(osi_free_func);
  622. }
  623. /*******************************************************************************
  624. **
  625. ** Function btm_sec_dev_free
  626. **
  627. ** Description Delete sec_dev_rec list when btm_cb is being released
  628. **
  629. *******************************************************************************/
  630. void btm_sec_dev_free(void)
  631. {
  632. list_free(btm_cb.p_sec_dev_rec_list);
  633. }