1 /*
2  * Copyright 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <vector>
20 
21 #include "stack/btm/security_device_record.h"
22 #include "types/ble_address_with_type.h"
23 #include "types/raw_address.h"
24 
25 /** Free resources associated with the device associated with |bd_addr| address.
26  *
27  * *** WARNING ***
28  * tBTM_SEC_DEV_REC associated with bd_addr becomes invalid after this function
29  * is called, also any of it's fields. i.e. if you use p_dev_rec->bd_addr, it is
30  * no longer valid!
31  * *** WARNING ***
32  *
33  * Returns true if removed OK, false if not found or ACL link is active.
34  */
35 bool BTM_SecDeleteDevice(const RawAddress& bd_addr);
36 
37 /*******************************************************************************
38  *
39  * Function         BTM_SecClearSecurityFlags
40  *
41  * Description      Reset the security flags (mark as not-paired) for a given
42  *                  remove device.
43  *
44  ******************************************************************************/
45 void BTM_SecClearSecurityFlags(const RawAddress& bd_addr);
46 
47 /*******************************************************************************
48  *
49  * Function         BTM_SecReadDevName
50  *
51  * Description      Looks for the device name in the security database for the
52  *                  specified BD address.
53  *
54  * Returns          Pointer to the name or NULL
55  *
56  ******************************************************************************/
57 const char* BTM_SecReadDevName(const RawAddress& bd_addr);
58 
59 /*******************************************************************************
60  *
61  * Function         btm_sec_alloc_dev
62  *
63  * Description      Allocate a record in the device database
64  *                  with specified address
65  *
66  * Returns          Pointer to the record or NULL
67  *
68  ******************************************************************************/
69 tBTM_SEC_DEV_REC* btm_sec_alloc_dev(const RawAddress& bd_addr);
70 
71 /*******************************************************************************
72  *
73  * Function         btm_find_dev_by_handle
74  *
75  * Description      Look for the record in the device database for the record
76  *                  with specified handle
77  *
78  * Returns          Pointer to the record or NULL
79  *
80  ******************************************************************************/
81 tBTM_SEC_DEV_REC* btm_find_dev_by_handle(uint16_t handle);
82 
83 /*******************************************************************************
84  *
85  * Function         btm_find_dev
86  *
87  * Description      Look for the record in the device database for the record
88  *                  with specified BD address
89  *
90  * Returns          Pointer to the record or NULL
91  *
92  ******************************************************************************/
93 tBTM_SEC_DEV_REC* btm_find_dev(const RawAddress& bd_addr);
94 
95 /*******************************************************************************
96  *
97  * Function         btm_find_dev_with_lenc
98  *
99  * Description      Look for the record in the device database with LTK and
100  *                  specified BD address
101  *
102  * Returns          Pointer to the record or NULL
103  *
104  ******************************************************************************/
105 tBTM_SEC_DEV_REC* btm_find_dev_with_lenc(const RawAddress& bd_addr);
106 
107 /*******************************************************************************
108  *
109  * Function         btm_consolidate_dev
110  *
111  * Description      combine security records if identified as same peer
112  *
113  * Returns          none
114  *
115  ******************************************************************************/
116 void btm_consolidate_dev(tBTM_SEC_DEV_REC* p_target_rec);
117 
118 /*******************************************************************************
119  *
120  * Function         btm_consolidate_dev
121  *
122  * Description      When pairing is finished (i.e. on BR/EDR), this function
123  *                  checks if there are existing LE connections to same device
124  *                  that can now be encrypted and used for profiles requiring
125  *                  encryption.
126  *
127  * Returns          none
128  *
129  ******************************************************************************/
130 void btm_dev_consolidate_existing_connections(const RawAddress& bd_addr);
131 
132 /*******************************************************************************
133  *
134  * Function         btm_find_or_alloc_dev
135  *
136  * Description      Look for the record in the device database for the record
137  *                  with specified BD address
138  *
139  * Returns          Pointer to the record or NULL
140  *
141  ******************************************************************************/
142 tBTM_SEC_DEV_REC* btm_find_or_alloc_dev(const RawAddress& bd_addr);
143 
144 /*******************************************************************************
145  *
146  * Function         btm_sec_allocate_dev_rec
147  *
148  * Description      Attempts to allocate a new device record. If we have
149  *                  exceeded the maximum number of allowable records to
150  *                  allocate, the oldest record will be deleted to make room
151  *                  for the new record.
152  *
153  * Returns          Pointer to the newly allocated record
154  *
155  ******************************************************************************/
156 tBTM_SEC_DEV_REC* btm_sec_allocate_dev_rec(void);
157 
158 /*******************************************************************************
159  *
160  * Function         btm_get_bond_type_dev
161  *
162  * Description      Get the bond type for a device in the device database
163  *                  with specified BD address
164  *
165  * Returns          The device bond type if known, otherwise BOND_TYPE_UNKNOWN
166  *
167  ******************************************************************************/
168 tBTM_BOND_TYPE btm_get_bond_type_dev(const RawAddress& bd_addr);
169 
170 /*******************************************************************************
171  *
172  * Function         btm_set_bond_type_dev
173  *
174  * Description      Set the bond type for a device in the device database
175  *                  with specified BD address
176  *
177  * Returns          true on success, otherwise false
178  *
179  ******************************************************************************/
180 bool btm_set_bond_type_dev(const RawAddress& bd_addr, tBTM_BOND_TYPE bond_type);
181 
182 /*******************************************************************************
183  *
184  * Function         btm_get_sec_dev_rec
185  *
186  * Description      Get security device records satisfying given filter
187  *
188  * Returns          A vector containing pointers of security device records
189  *
190  ******************************************************************************/
191 std::vector<tBTM_SEC_DEV_REC*> btm_get_sec_dev_rec();
192 
193 bool BTM_Sec_AddressKnown(const RawAddress& address);
194 const tBLE_BD_ADDR BTM_Sec_GetAddressWithType(const RawAddress& bd_addr);
195 
196 bool BTM_IsRemoteNameKnown(const RawAddress& bd_addr, tBT_TRANSPORT transport);
197