1 /******************************************************************************
2 *
3 * Copyright 2023 The Android Open Source Project
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 is the API implementation file for the BTA device manager.
22 *
23 ******************************************************************************/
24
25 #include <base/functional/bind.h>
26 #include <bluetooth/log.h>
27 #include <com_android_bluetooth_flags.h>
28
29 #include "bta/dm/bta_dm_sec_int.h"
30 #include "stack/btm/btm_sec.h"
31 #include "stack/include/bt_octets.h"
32 #include "stack/include/btm_ble_sec_api.h"
33 #include "stack/include/btm_client_interface.h"
34 #include "stack/include/btm_status.h"
35 #include "stack/include/main_thread.h"
36 #include "types/raw_address.h"
37
38 using namespace bluetooth;
39
40 /** This function initiates a bonding procedure with a peer device */
BTA_DmBond(const RawAddress & bd_addr,tBLE_ADDR_TYPE addr_type,tBT_TRANSPORT transport,tBT_DEVICE_TYPE device_type)41 void BTA_DmBond(const RawAddress& bd_addr, tBLE_ADDR_TYPE addr_type, tBT_TRANSPORT transport,
42 tBT_DEVICE_TYPE device_type) {
43 bta_dm_bond(bd_addr, addr_type, transport, device_type);
44 }
45
46 /** This function cancels the bonding procedure with a peer device
47 */
BTA_DmBondCancel(const RawAddress & bd_addr)48 void BTA_DmBondCancel(const RawAddress& bd_addr) { bta_dm_bond_cancel(bd_addr); }
49
50 /*******************************************************************************
51 *
52 * Function BTA_DmPinReply
53 *
54 * Description This function provides a pincode for a remote device when
55 * one is requested by DM through BTA_DM_PIN_REQ_EVT
56 *
57 *
58 * Returns void
59 *
60 ******************************************************************************/
BTA_DmPinReply(const RawAddress & bd_addr,bool accept,uint8_t pin_len,uint8_t * p_pin)61 void BTA_DmPinReply(const RawAddress& bd_addr, bool accept, uint8_t pin_len, uint8_t* p_pin) {
62 std::unique_ptr<tBTA_DM_API_PIN_REPLY> msg = std::make_unique<tBTA_DM_API_PIN_REPLY>();
63
64 msg->bd_addr = bd_addr;
65 msg->accept = accept;
66 if (accept) {
67 msg->pin_len = pin_len;
68 memcpy(msg->p_pin, p_pin, pin_len);
69 }
70
71 bta_dm_pin_reply(std::move(msg));
72 }
73
74 /*******************************************************************************
75 *
76 * Function BTA_DmLocalOob
77 *
78 * Description This function retrieves the OOB data from local controller.
79 * The result is reported by:
80 * - bta_dm_co_loc_oob_ext() if device supports secure
81 * connections (SC)
82 * - bta_dm_co_loc_oob() if device doesn't support SC
83 *
84 * Returns void
85 *
86 ******************************************************************************/
BTA_DmLocalOob(void)87 void BTA_DmLocalOob(void) { BTM_ReadLocalOobData(); }
88
89 /*******************************************************************************
90 *
91 * Function BTA_DmConfirm
92 *
93 * Description This function accepts or rejects the numerical value of the
94 * Simple Pairing process on BTA_DM_SP_CFM_REQ_EVT
95 *
96 * Returns void
97 *
98 ******************************************************************************/
BTA_DmConfirm(const RawAddress & bd_addr,bool accept)99 void BTA_DmConfirm(const RawAddress& bd_addr, bool accept) { bta_dm_confirm(bd_addr, accept); }
100
101 /*******************************************************************************
102 *
103 * Function BTA_DmAddDevice
104 *
105 * Description This function adds a device to the security database list of
106 * peer device
107 *
108 * Returns void
109 *
110 ******************************************************************************/
BTA_DmAddDevice(RawAddress bd_addr,DEV_CLASS dev_class,LinkKey link_key,uint8_t key_type,uint8_t pin_length)111 void BTA_DmAddDevice(RawAddress bd_addr, DEV_CLASS dev_class, LinkKey link_key, uint8_t key_type,
112 uint8_t pin_length) {
113 auto closure = base::Bind(get_btm_client_interface().security.BTM_SecAddDevice, bd_addr,
114 dev_class, link_key, key_type, pin_length);
115
116 closure.Run();
117 }
118
119 /** This function removes a device from the security database list of peer
120 * device. It manages unpairing even while connected */
BTA_DmRemoveDevice(const RawAddress & bd_addr)121 tBTA_STATUS BTA_DmRemoveDevice(const RawAddress& bd_addr) {
122 bta_dm_remove_device(bd_addr);
123 return BTA_SUCCESS;
124 }
125
126 /*******************************************************************************
127 *
128 * Function BTA_DmAddBleKey
129 *
130 * Description Add/modify LE device information. This function will be
131 * normally called during host startup to restore all required
132 * information stored in the NVRAM.
133 *
134 * Parameters: bd_addr - BD address of the peer
135 * p_le_key - LE key values.
136 * key_type - LE SMP key type.
137 *
138 * Returns BTA_SUCCESS if successful
139 * BTA_FAIL if operation failed.
140 *
141 ******************************************************************************/
BTA_DmAddBleKey(const RawAddress & bd_addr,tBTA_LE_KEY_VALUE * p_le_key,tBTM_LE_KEY_TYPE key_type)142 void BTA_DmAddBleKey(const RawAddress& bd_addr, tBTA_LE_KEY_VALUE* p_le_key,
143 tBTM_LE_KEY_TYPE key_type) {
144 bta_dm_add_blekey(bd_addr, *p_le_key, key_type);
145 }
146
147 /*******************************************************************************
148 *
149 * Function BTA_DmAddBleDevice
150 *
151 * Description Add a BLE device. This function will be normally called
152 * during host startup to restore all required information
153 * for a LE device stored in the NVRAM.
154 *
155 * Parameters: bd_addr - BD address of the peer
156 * dev_type - Remote device's device type.
157 * addr_type - LE device address type.
158 *
159 * Returns void
160 *
161 ******************************************************************************/
BTA_DmAddBleDevice(const RawAddress & bd_addr,tBLE_ADDR_TYPE addr_type,tBT_DEVICE_TYPE dev_type)162 void BTA_DmAddBleDevice(const RawAddress& bd_addr, tBLE_ADDR_TYPE addr_type,
163 tBT_DEVICE_TYPE dev_type) {
164 bta_dm_add_ble_device(bd_addr, addr_type, dev_type);
165 }
166
167 /*******************************************************************************
168 *
169 * Function BTA_DmBlePasskeyReply
170 *
171 * Description Send BLE SMP passkey reply.
172 *
173 * Parameters: bd_addr - BD address of the peer
174 * accept - passkey entry successful or declined.
175 * passkey - passkey value, must be a 6 digit number,
176 * can be lead by 0.
177 *
178 * Returns void
179 *
180 ******************************************************************************/
BTA_DmBlePasskeyReply(const RawAddress & bd_addr,bool accept,uint32_t passkey)181 void BTA_DmBlePasskeyReply(const RawAddress& bd_addr, bool accept, uint32_t passkey) {
182 bta_dm_ble_passkey_reply(bd_addr, accept, accept ? passkey : 0);
183 }
184
185 /*******************************************************************************
186 *
187 * Function BTA_DmBleConfirmReply
188 *
189 * Description Send BLE SMP SC user confirmation reply.
190 *
191 * Parameters: bd_addr - BD address of the peer
192 * accept - numbers to compare are the same or
193 * different.
194 *
195 * Returns void
196 *
197 ******************************************************************************/
BTA_DmBleConfirmReply(const RawAddress & bd_addr,bool accept)198 void BTA_DmBleConfirmReply(const RawAddress& bd_addr, bool accept) {
199 bta_dm_ble_confirm_reply(bd_addr, accept);
200 }
201
202 /*******************************************************************************
203 *
204 * Function BTA_DmBleSecurityGrant
205 *
206 * Description Grant security request access.
207 *
208 * Parameters: bd_addr - BD address of the peer
209 * res - security grant status.
210 *
211 * Returns void
212 *
213 ******************************************************************************/
BTA_DmBleSecurityGrant(const RawAddress & bd_addr,tBTA_DM_BLE_SEC_GRANT res)214 void BTA_DmBleSecurityGrant(const RawAddress& bd_addr, tBTA_DM_BLE_SEC_GRANT res) {
215 const tBTM_STATUS btm_status = [](const tBTA_DM_BLE_SEC_GRANT res) -> tBTM_STATUS {
216 switch (res) {
217 case tBTA_DM_BLE_SEC_GRANT::BTA_DM_SEC_GRANTED:
218 return tBTM_STATUS::BTM_SUCCESS;
219 case tBTA_DM_BLE_SEC_GRANT::BTA_DM_SEC_PAIR_NOT_SPT:
220 return static_cast<tBTM_STATUS>(BTA_DM_AUTH_FAIL_BASE + SMP_PAIR_NOT_SUPPORT);
221 }
222 }(res);
223
224 BTM_SecurityGrant(bd_addr, btm_status);
225 }
226
227 /*******************************************************************************
228 *
229 * Function BTA_DmSetEncryption
230 *
231 * Description This function is called to ensure that connection is
232 * encrypted. Should be called only on an open connection.
233 * Typically only needed for connections that first want to
234 * bring up unencrypted links, then later encrypt them.
235 *
236 * Parameters: bd_addr - Address of the peer device
237 * transport - transport of the link to be encruypted
238 * p_callback - Pointer to callback function to indicat the
239 * link encryption status
240 * sec_act - This is the security action to indicate
241 * what kind of BLE security level is required
242 * for the BLE link if BLE is supported.
243 * Note: This parameter is ignored for the
244 * BR/EDR or if BLE is not supported.
245 *
246 * Returns void
247 *
248 ******************************************************************************/
BTA_DmSetEncryption(const RawAddress & bd_addr,tBT_TRANSPORT transport,tBTA_DM_ENCRYPT_CBACK * p_callback,tBTM_BLE_SEC_ACT sec_act)249 void BTA_DmSetEncryption(const RawAddress& bd_addr, tBT_TRANSPORT transport,
250 tBTA_DM_ENCRYPT_CBACK* p_callback, tBTM_BLE_SEC_ACT sec_act) {
251 log::verbose("");
252 bta_dm_set_encryption(bd_addr, transport, p_callback, sec_act);
253 }
254
255 /*******************************************************************************
256 *
257 * Function BTA_DmSirkSecCbRegister
258 *
259 * Description This procedure registeres in requested a callback for
260 * verification by CSIP potential set member.
261 *
262 * Parameters p_cback - callback to member verificator
263 *
264 * Returns void
265 *
266 ******************************************************************************/
BTA_DmSirkSecCbRegister(tBTA_DM_SEC_CBACK * p_cback)267 void BTA_DmSirkSecCbRegister(tBTA_DM_SEC_CBACK* p_cback) {
268 log::debug("");
269 bta_dm_ble_sirk_sec_cb_register(p_cback);
270 }
271
272 /*******************************************************************************
273 *
274 * Function BTA_DmSirkConfirmDeviceReply
275 *
276 * Description This procedure confirms requested to validate set device.
277 *
278 * Parameters bd_addr - BD address of the peer
279 * accept - True if device is authorized by CSIP, false
280 * otherwise.
281 *
282 * Returns void
283 *
284 ******************************************************************************/
BTA_DmSirkConfirmDeviceReply(const RawAddress & bd_addr,bool accept)285 void BTA_DmSirkConfirmDeviceReply(const RawAddress& bd_addr, bool accept) {
286 log::debug("");
287 bta_dm_ble_sirk_confirm_device_reply(bd_addr, accept);
288 }
289