btm_dev.c 21 KB

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