• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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   if (!com::android::bluetooth::flags::remove_device_in_main_thread()) {
123     bta_dm_remove_device(bd_addr);
124     return BTA_SUCCESS;
125   }
126 
127   do_in_main_thread(base::BindOnce(bta_dm_remove_device, bd_addr));
128   return BTA_SUCCESS;
129 }
130 
131 /*******************************************************************************
132  *
133  * Function         BTA_DmAddBleKey
134  *
135  * Description      Add/modify LE device information.  This function will be
136  *                  normally called during host startup to restore all required
137  *                  information stored in the NVRAM.
138  *
139  * Parameters:      bd_addr          - BD address of the peer
140  *                  p_le_key         - LE key values.
141  *                  key_type         - LE SMP key type.
142  *
143  * Returns          BTA_SUCCESS if successful
144  *                  BTA_FAIL if operation failed.
145  *
146  ******************************************************************************/
BTA_DmAddBleKey(const RawAddress & bd_addr,tBTA_LE_KEY_VALUE * p_le_key,tBTM_LE_KEY_TYPE key_type)147 void BTA_DmAddBleKey(const RawAddress& bd_addr, tBTA_LE_KEY_VALUE* p_le_key,
148                      tBTM_LE_KEY_TYPE key_type) {
149   bta_dm_add_blekey(bd_addr, *p_le_key, key_type);
150 }
151 
152 /*******************************************************************************
153  *
154  * Function         BTA_DmAddBleDevice
155  *
156  * Description      Add a BLE device.  This function will be normally called
157  *                  during host startup to restore all required information
158  *                  for a LE device stored in the NVRAM.
159  *
160  * Parameters:      bd_addr          - BD address of the peer
161  *                  dev_type         - Remote device's device type.
162  *                  addr_type        - LE device address type.
163  *
164  * Returns          void
165  *
166  ******************************************************************************/
BTA_DmAddBleDevice(const RawAddress & bd_addr,tBLE_ADDR_TYPE addr_type,tBT_DEVICE_TYPE dev_type)167 void BTA_DmAddBleDevice(const RawAddress& bd_addr, tBLE_ADDR_TYPE addr_type,
168                         tBT_DEVICE_TYPE dev_type) {
169   bta_dm_add_ble_device(bd_addr, addr_type, dev_type);
170 }
171 
172 /*******************************************************************************
173  *
174  * Function         BTA_DmBlePasskeyReply
175  *
176  * Description      Send BLE SMP passkey reply.
177  *
178  * Parameters:      bd_addr          - BD address of the peer
179  *                  accept           - passkey entry successful or declined.
180  *                  passkey          - passkey value, must be a 6 digit number,
181  *                                     can be lead by 0.
182  *
183  * Returns          void
184  *
185  ******************************************************************************/
BTA_DmBlePasskeyReply(const RawAddress & bd_addr,bool accept,uint32_t passkey)186 void BTA_DmBlePasskeyReply(const RawAddress& bd_addr, bool accept, uint32_t passkey) {
187   bta_dm_ble_passkey_reply(bd_addr, accept, accept ? passkey : 0);
188 }
189 
190 /*******************************************************************************
191  *
192  * Function         BTA_DmBleConfirmReply
193  *
194  * Description      Send BLE SMP SC user confirmation reply.
195  *
196  * Parameters:      bd_addr          - BD address of the peer
197  *                  accept           - numbers to compare are the same or
198  *                                     different.
199  *
200  * Returns          void
201  *
202  ******************************************************************************/
BTA_DmBleConfirmReply(const RawAddress & bd_addr,bool accept)203 void BTA_DmBleConfirmReply(const RawAddress& bd_addr, bool accept) {
204   bta_dm_ble_confirm_reply(bd_addr, accept);
205 }
206 
207 /*******************************************************************************
208  *
209  * Function         BTA_DmBleSecurityGrant
210  *
211  * Description      Grant security request access.
212  *
213  * Parameters:      bd_addr          - BD address of the peer
214  *                  res              - security grant status.
215  *
216  * Returns          void
217  *
218  ******************************************************************************/
BTA_DmBleSecurityGrant(const RawAddress & bd_addr,tBTA_DM_BLE_SEC_GRANT res)219 void BTA_DmBleSecurityGrant(const RawAddress& bd_addr, tBTA_DM_BLE_SEC_GRANT res) {
220   const tBTM_STATUS btm_status = [](const tBTA_DM_BLE_SEC_GRANT res) -> tBTM_STATUS {
221     switch (res) {
222       case tBTA_DM_BLE_SEC_GRANT::BTA_DM_SEC_GRANTED:
223         return tBTM_STATUS::BTM_SUCCESS;
224       case tBTA_DM_BLE_SEC_GRANT::BTA_DM_SEC_PAIR_NOT_SPT:
225         return static_cast<tBTM_STATUS>(BTA_DM_AUTH_FAIL_BASE + SMP_PAIR_NOT_SUPPORT);
226     }
227   }(res);
228 
229   BTM_SecurityGrant(bd_addr, btm_status);
230 }
231 
232 /*******************************************************************************
233  *
234  * Function         BTA_DmSetEncryption
235  *
236  * Description      This function is called to ensure that connection is
237  *                  encrypted.  Should be called only on an open connection.
238  *                  Typically only needed for connections that first want to
239  *                  bring up unencrypted links, then later encrypt them.
240  *
241  * Parameters:      bd_addr       - Address of the peer device
242  *                  transport     - transport of the link to be encruypted
243  *                  p_callback    - Pointer to callback function to indicat the
244  *                                  link encryption status
245  *                  sec_act       - This is the security action to indicate
246  *                                  what kind of BLE security level is required
247  *                                  for the BLE link if BLE is supported.
248  *                                  Note: This parameter is ignored for the
249  *                                        BR/EDR or if BLE is not supported.
250  *
251  * Returns          void
252  *
253  ******************************************************************************/
BTA_DmSetEncryption(const RawAddress & bd_addr,tBT_TRANSPORT transport,tBTA_DM_ENCRYPT_CBACK * p_callback,tBTM_BLE_SEC_ACT sec_act)254 void BTA_DmSetEncryption(const RawAddress& bd_addr, tBT_TRANSPORT transport,
255                          tBTA_DM_ENCRYPT_CBACK* p_callback, tBTM_BLE_SEC_ACT sec_act) {
256   log::verbose("");
257   bta_dm_set_encryption(bd_addr, transport, p_callback, sec_act);
258 }
259 
260 /*******************************************************************************
261  *
262  * Function         BTA_DmSirkSecCbRegister
263  *
264  * Description      This procedure registeres in requested a callback for
265  *                  verification by CSIP potential set member.
266  *
267  * Parameters       p_cback     - callback to member verificator
268  *
269  * Returns          void
270  *
271  ******************************************************************************/
BTA_DmSirkSecCbRegister(tBTA_DM_SEC_CBACK * p_cback)272 void BTA_DmSirkSecCbRegister(tBTA_DM_SEC_CBACK* p_cback) {
273   log::debug("");
274   bta_dm_ble_sirk_sec_cb_register(p_cback);
275 }
276 
277 /*******************************************************************************
278  *
279  * Function         BTA_DmSirkConfirmDeviceReply
280  *
281  * Description      This procedure confirms requested to validate set device.
282  *
283  * Parameters       bd_addr     - BD address of the peer
284  *                  accept      - True if device is authorized by CSIP, false
285  *                                otherwise.
286  *
287  * Returns          void
288  *
289  ******************************************************************************/
BTA_DmSirkConfirmDeviceReply(const RawAddress & bd_addr,bool accept)290 void BTA_DmSirkConfirmDeviceReply(const RawAddress& bd_addr, bool accept) {
291   log::debug("");
292   bta_dm_ble_sirk_confirm_device_reply(bd_addr, accept);
293 }
294