• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2019 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 #define LOG_TAG "bt_shim_btm"
18 
19 #include "main/shim/btm_api.h"
20 
21 #include <base/functional/callback.h>
22 #include <base/logging.h>
23 
24 #include <mutex>
25 
26 #include "common/metric_id_allocator.h"
27 #include "common/time_util.h"
28 #include "gd/common/callback.h"
29 #include "gd/os/log.h"
30 #include "gd/security/security_module.h"
31 #include "gd/security/ui.h"
32 #include "main/shim/btm.h"
33 #include "main/shim/controller.h"
34 #include "main/shim/helpers.h"
35 #include "main/shim/metric_id_api.h"
36 #include "main/shim/shim.h"
37 #include "main/shim/stack.h"
38 #include "osi/include/allocator.h"
39 #include "osi/include/osi.h"  // UNUSED_ATTR
40 #include "osi/include/properties.h"
41 #include "stack/btm/btm_ble_int.h"
42 #include "stack/btm/btm_int_types.h"
43 #include "stack/btm/btm_sec.h"
44 #include "stack/include/bt_hdr.h"
45 #include "stack/include/bt_octets.h"
46 #include "stack/include/hci_error_code.h"
47 #include "types/ble_address_with_type.h"
48 #include "types/bluetooth/uuid.h"
49 #include "types/raw_address.h"
50 
51 using bluetooth::common::MetricIdAllocator;
52 
53 #define BTIF_DM_DEFAULT_INQ_MAX_RESULTS 0
54 #define BTIF_DM_DEFAULT_INQ_MAX_DURATION 10
55 
56 /**
57  * Legacy bluetooth module global control block state
58  *
59  * Mutex is used to synchronize access from the shim
60  * layer into the global control block.  This is used
61  * by the shim despite potentially arbitrary
62  * unsynchronized access by the legacy stack.
63  */
64 extern tBTM_CB btm_cb;
65 std::mutex btm_cb_mutex_;
66 
67 bool btm_inq_find_bdaddr(const RawAddress& p_bda);
68 extern tINQ_DB_ENT* btm_inq_db_find(const RawAddress& raw_address);
69 extern tINQ_DB_ENT* btm_inq_db_new(const RawAddress& p_bda);
70 
71 /**
72  * Legacy bluetooth btm stack entry points
73  */
74 void btm_acl_update_inquiry_status(uint8_t status);
75 void btm_clear_all_pending_le_entry(void);
76 void btm_clr_inq_result_flt(void);
77 void btm_set_eir_uuid(const uint8_t* p_eir, tBTM_INQ_RESULTS* p_results);
78 void btm_sort_inq_result(void);
79 void btm_process_inq_complete(tHCI_STATUS status, uint8_t result_type);
80 
is_classic_device(tBT_DEVICE_TYPE device_type)81 static bool is_classic_device(tBT_DEVICE_TYPE device_type) {
82   return device_type == BT_DEVICE_TYPE_BREDR;
83 }
84 
has_classic_device(tBT_DEVICE_TYPE device_type)85 static bool has_classic_device(tBT_DEVICE_TYPE device_type) {
86   return device_type & BT_DEVICE_TYPE_BREDR;
87 }
88 
btm_api_process_inquiry_result(const RawAddress & raw_address,uint8_t page_scan_rep_mode,DEV_CLASS device_class,uint16_t clock_offset)89 void btm_api_process_inquiry_result(const RawAddress& raw_address,
90                                     uint8_t page_scan_rep_mode,
91                                     DEV_CLASS device_class,
92                                     uint16_t clock_offset) {
93   tINQ_DB_ENT* p_i = btm_inq_db_find(raw_address);
94 
95   if (p_i == nullptr) {
96     p_i = btm_inq_db_new(raw_address);
97     CHECK(p_i != nullptr);
98   } else if (p_i->inq_count == btm_cb.btm_inq_vars.inq_counter &&
99              is_classic_device(p_i->inq_info.results.device_type)) {
100     return;
101   }
102 
103   p_i->inq_info.results.page_scan_rep_mode = page_scan_rep_mode;
104   p_i->inq_info.results.page_scan_per_mode = 0;  // RESERVED
105   p_i->inq_info.results.page_scan_mode = 0;      // RESERVED
106   p_i->inq_info.results.dev_class[0] = device_class[0];
107   p_i->inq_info.results.dev_class[1] = device_class[1];
108   p_i->inq_info.results.dev_class[2] = device_class[2];
109   p_i->inq_info.results.clock_offset = clock_offset | BTM_CLOCK_OFFSET_VALID;
110   p_i->inq_info.results.inq_result_type = BTM_INQ_RESULT_BR;
111   p_i->inq_info.results.rssi = BTM_INQ_RES_IGNORE_RSSI;
112 
113   p_i->time_of_resp = bluetooth::common::time_get_os_boottime_ms();
114   p_i->inq_count = btm_cb.btm_inq_vars.inq_counter;
115   p_i->inq_info.appl_knows_rem_name = false;
116 
117   if (p_i->inq_count != btm_cb.btm_inq_vars.inq_counter) {
118     p_i->inq_info.results.device_type = BT_DEVICE_TYPE_BREDR;
119     btm_cb.btm_inq_vars.inq_cmpl_info.num_resp++;
120     p_i->scan_rsp = false;
121   } else {
122     p_i->inq_info.results.device_type |= BT_DEVICE_TYPE_BREDR;
123   }
124 
125   if (btm_cb.btm_inq_vars.p_inq_results_cb == nullptr) {
126     return;
127   }
128 
129   (btm_cb.btm_inq_vars.p_inq_results_cb)(&p_i->inq_info.results, nullptr, 0);
130 }
131 
btm_api_process_inquiry_result_with_rssi(RawAddress raw_address,uint8_t page_scan_rep_mode,DEV_CLASS device_class,uint16_t clock_offset,int8_t rssi)132 void btm_api_process_inquiry_result_with_rssi(RawAddress raw_address,
133                                               uint8_t page_scan_rep_mode,
134                                               DEV_CLASS device_class,
135                                               uint16_t clock_offset,
136                                               int8_t rssi) {
137   tINQ_DB_ENT* p_i = btm_inq_db_find(raw_address);
138 
139   bool update = false;
140   if (btm_inq_find_bdaddr(raw_address)) {
141     if (p_i != nullptr &&
142         (rssi > p_i->inq_info.results.rssi || p_i->inq_info.results.rssi == 0 ||
143          has_classic_device(p_i->inq_info.results.device_type))) {
144       update = true;
145     }
146   }
147 
148   bool is_new = true;
149   if (p_i == nullptr) {
150     p_i = btm_inq_db_new(raw_address);
151     CHECK(p_i != nullptr);
152   } else if (p_i->inq_count == btm_cb.btm_inq_vars.inq_counter &&
153              is_classic_device(p_i->inq_info.results.device_type)) {
154     is_new = false;
155   }
156 
157   p_i->inq_info.results.rssi = rssi;
158 
159   if (is_new) {
160     p_i->inq_info.results.page_scan_rep_mode = page_scan_rep_mode;
161     p_i->inq_info.results.page_scan_per_mode = 0;  // RESERVED
162     p_i->inq_info.results.page_scan_mode = 0;      // RESERVED
163     p_i->inq_info.results.dev_class[0] = device_class[0];
164     p_i->inq_info.results.dev_class[1] = device_class[1];
165     p_i->inq_info.results.dev_class[2] = device_class[2];
166     p_i->inq_info.results.clock_offset = clock_offset | BTM_CLOCK_OFFSET_VALID;
167     p_i->inq_info.results.inq_result_type = BTM_INQ_RESULT_BR;
168 
169     p_i->time_of_resp = bluetooth::common::time_get_os_boottime_ms();
170     p_i->inq_count = btm_cb.btm_inq_vars.inq_counter;
171     p_i->inq_info.appl_knows_rem_name = false;
172 
173     if (p_i->inq_count != btm_cb.btm_inq_vars.inq_counter) {
174       p_i->inq_info.results.device_type = BT_DEVICE_TYPE_BREDR;
175       btm_cb.btm_inq_vars.inq_cmpl_info.num_resp++;
176       p_i->scan_rsp = false;
177     } else {
178       p_i->inq_info.results.device_type |= BT_DEVICE_TYPE_BREDR;
179     }
180   }
181 
182   if (btm_cb.btm_inq_vars.p_inq_results_cb == nullptr) {
183     return;
184   }
185 
186   if (is_new || update) {
187     (btm_cb.btm_inq_vars.p_inq_results_cb)(&p_i->inq_info.results, nullptr, 0);
188   }
189 }
btm_api_process_extended_inquiry_result(RawAddress raw_address,uint8_t page_scan_rep_mode,DEV_CLASS device_class,uint16_t clock_offset,int8_t rssi,const uint8_t * eir_data,size_t eir_len)190 void btm_api_process_extended_inquiry_result(RawAddress raw_address,
191                                              uint8_t page_scan_rep_mode,
192                                              DEV_CLASS device_class,
193                                              uint16_t clock_offset, int8_t rssi,
194                                              const uint8_t* eir_data,
195                                              size_t eir_len) {
196   tINQ_DB_ENT* p_i = btm_inq_db_find(raw_address);
197 
198   bool update = false;
199   if (btm_inq_find_bdaddr(raw_address) && p_i != nullptr) {
200     update = true;
201   }
202 
203   bool is_new = true;
204   if (p_i == nullptr) {
205     p_i = btm_inq_db_new(raw_address);
206   } else if (p_i->inq_count == btm_cb.btm_inq_vars.inq_counter &&
207              (p_i->inq_info.results.device_type == BT_DEVICE_TYPE_BREDR)) {
208     is_new = false;
209   }
210 
211   p_i->inq_info.results.rssi = rssi;
212 
213   if (is_new) {
214     p_i->inq_info.results.page_scan_rep_mode = page_scan_rep_mode;
215     p_i->inq_info.results.page_scan_per_mode = 0;  // RESERVED
216     p_i->inq_info.results.page_scan_mode = 0;      // RESERVED
217     p_i->inq_info.results.dev_class[0] = device_class[0];
218     p_i->inq_info.results.dev_class[1] = device_class[1];
219     p_i->inq_info.results.dev_class[2] = device_class[2];
220     p_i->inq_info.results.clock_offset = clock_offset | BTM_CLOCK_OFFSET_VALID;
221     p_i->inq_info.results.inq_result_type = BTM_INQ_RESULT_BR;
222 
223     p_i->time_of_resp = bluetooth::common::time_get_os_boottime_ms();
224     p_i->inq_count = btm_cb.btm_inq_vars.inq_counter;
225     p_i->inq_info.appl_knows_rem_name = false;
226 
227     if (p_i->inq_count != btm_cb.btm_inq_vars.inq_counter) {
228       p_i->inq_info.results.device_type = BT_DEVICE_TYPE_BREDR;
229       btm_cb.btm_inq_vars.inq_cmpl_info.num_resp++;
230       p_i->scan_rsp = false;
231     } else {
232       p_i->inq_info.results.device_type |= BT_DEVICE_TYPE_BREDR;
233     }
234   }
235 
236   if (btm_cb.btm_inq_vars.p_inq_results_cb == nullptr) {
237     return;
238   }
239 
240   if (is_new || update) {
241     memset(p_i->inq_info.results.eir_uuid, 0,
242            BTM_EIR_SERVICE_ARRAY_SIZE * (BTM_EIR_ARRAY_BITS / 8));
243     btm_set_eir_uuid(const_cast<uint8_t*>(eir_data), &p_i->inq_info.results);
244     uint8_t* p_eir_data = const_cast<uint8_t*>(eir_data);
245     (btm_cb.btm_inq_vars.p_inq_results_cb)(&p_i->inq_info.results, p_eir_data,
246                                            eir_len);
247   }
248 }
249 
250 namespace {
251 std::unordered_map<bluetooth::hci::AddressWithType, bt_bdname_t>
252     address_name_map_;
253 
254 std::unordered_map<bluetooth::hci::IoCapability, int> gd_legacy_io_caps_map_ = {
255     {bluetooth::hci::IoCapability::DISPLAY_ONLY, BTM_IO_CAP_OUT},
256     {bluetooth::hci::IoCapability::DISPLAY_YES_NO, BTM_IO_CAP_IO},
257     {bluetooth::hci::IoCapability::KEYBOARD_ONLY, BTM_IO_CAP_IN},
258     {bluetooth::hci::IoCapability::NO_INPUT_NO_OUTPUT, BTM_IO_CAP_NONE},
259 };
260 
261 std::unordered_map<bluetooth::hci::AuthenticationRequirements, int>
262     gd_legacy_auth_reqs_map_ = {
263         {bluetooth::hci::AuthenticationRequirements::NO_BONDING,
264          BTM_AUTH_SP_NO},
265         {bluetooth::hci::AuthenticationRequirements::NO_BONDING_MITM_PROTECTION,
266          BTM_AUTH_SP_YES},
267         {bluetooth::hci::AuthenticationRequirements::DEDICATED_BONDING,
268          BTM_AUTH_AP_NO},
269         {bluetooth::hci::AuthenticationRequirements::
270              DEDICATED_BONDING_MITM_PROTECTION,
271          BTM_AUTH_AP_YES},
272         {bluetooth::hci::AuthenticationRequirements::GENERAL_BONDING,
273          BTM_AUTH_SPGB_NO},
274         {bluetooth::hci::AuthenticationRequirements::
275              GENERAL_BONDING_MITM_PROTECTION,
276          BTM_AUTH_SPGB_YES},
277 };
278 }
279 
280 class ShimUi : public bluetooth::security::UI {
281  public:
GetInstance()282   static ShimUi* GetInstance() {
283     static ShimUi instance;
284     return &instance;
285   }
286 
287   ShimUi(const ShimUi&) = delete;
288   ShimUi& operator=(const ShimUi&) = delete;
289 
SetBtaCallbacks(const tBTM_APPL_INFO * bta_callbacks)290   void SetBtaCallbacks(const tBTM_APPL_INFO* bta_callbacks) {
291     bta_callbacks_ = bta_callbacks;
292     if (bta_callbacks->p_pin_callback == nullptr) {
293       LOG_INFO("UNIMPLEMENTED %s pin_callback", __func__);
294     }
295 
296     if (bta_callbacks->p_link_key_callback == nullptr) {
297       LOG_INFO("UNIMPLEMENTED %s link_key_callback", __func__);
298     }
299 
300     if (bta_callbacks->p_auth_complete_callback == nullptr) {
301       LOG_INFO("UNIMPLEMENTED %s auth_complete_callback", __func__);
302     }
303 
304     if (bta_callbacks->p_bond_cancel_cmpl_callback == nullptr) {
305       LOG_INFO("UNIMPLEMENTED %s bond_cancel_complete_callback", __func__);
306     }
307 
308     if (bta_callbacks->p_le_callback == nullptr) {
309       LOG_INFO("UNIMPLEMENTED %s le_callback", __func__);
310     }
311 
312     if (bta_callbacks->p_le_key_callback == nullptr) {
313       LOG_INFO("UNIMPLEMENTED %s le_key_callback", __func__);
314     }
315   }
316 
DisplayPairingPrompt(const bluetooth::hci::AddressWithType & address,std::string name)317   void DisplayPairingPrompt(const bluetooth::hci::AddressWithType& address,
318                             std::string name) {
319     waiting_for_pairing_prompt_ = true;
320     bt_bdname_t legacy_name{0};
321     memcpy(legacy_name.name, name.data(), name.length());
322     // TODO(optedoblivion): Handle callback to BTA for BLE
323   }
324 
Cancel(const bluetooth::hci::AddressWithType & address)325   void Cancel(const bluetooth::hci::AddressWithType& address) {
326     LOG(WARNING) << " ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ " << __func__;
327   }
328 
HandleConfirm(bluetooth::security::ConfirmationData data)329   void HandleConfirm(bluetooth::security::ConfirmationData data) {
330     const bluetooth::hci::AddressWithType& address = data.GetAddressWithType();
331     uint32_t numeric_value = data.GetNumericValue();
332     bt_bdname_t legacy_name{0};
333     memcpy(legacy_name.name, data.GetName().data(), data.GetName().length());
334 
335     if (bta_callbacks_->p_sp_callback) {
336       // Call sp_cback for IO_REQ
337       tBTM_SP_IO_REQ io_req_evt_data;
338       io_req_evt_data.bd_addr = bluetooth::ToRawAddress(address.GetAddress());
339       // Local IO Caps (Phone is always DisplayYesNo)
340       io_req_evt_data.io_cap = BTM_IO_CAP_IO;
341       // Local Auth Reqs (Phone is always DEDICATED_BONDING)
342       io_req_evt_data.auth_req = BTM_AUTH_AP_NO;
343       io_req_evt_data.oob_data = BTM_OOB_NONE;
344       (*bta_callbacks_->p_sp_callback)(BTM_SP_IO_REQ_EVT,
345                                        (tBTM_SP_EVT_DATA*)&io_req_evt_data);
346 
347       // Call sp_cback for IO_RSP
348       tBTM_SP_IO_RSP io_rsp_evt_data;
349       io_rsp_evt_data.bd_addr = bluetooth::ToRawAddress(address.GetAddress());
350       io_rsp_evt_data.io_cap = gd_legacy_io_caps_map_[data.GetRemoteIoCaps()];
351       io_rsp_evt_data.auth_req =
352           gd_legacy_auth_reqs_map_[data.GetRemoteAuthReqs()];
353       io_rsp_evt_data.auth_req = BTM_AUTH_AP_YES;
354       io_rsp_evt_data.oob_data = BTM_OOB_NONE;
355       (*bta_callbacks_->p_sp_callback)(BTM_SP_IO_RSP_EVT,
356                                        (tBTM_SP_EVT_DATA*)&io_rsp_evt_data);
357 
358       // Call sp_cback for USER_CONFIRMATION
359       tBTM_SP_EVT_DATA user_cfm_req_evt_data;
360       user_cfm_req_evt_data.cfm_req.bd_addr =
361           bluetooth::ToRawAddress(address.GetAddress());
362       user_cfm_req_evt_data.cfm_req.num_val = numeric_value;
363       // If we pop a dialog then it isn't just_works
364       user_cfm_req_evt_data.cfm_req.just_works = data.IsJustWorks();
365 
366       address_name_map_.emplace(address, legacy_name);
367       memcpy((char*)user_cfm_req_evt_data.cfm_req.bd_name, legacy_name.name,
368              BD_NAME_LEN);
369 
370       (*bta_callbacks_->p_sp_callback)(BTM_SP_CFM_REQ_EVT,
371                                        &user_cfm_req_evt_data);
372     }
373   }
374 
DisplayConfirmValue(bluetooth::security::ConfirmationData data)375   void DisplayConfirmValue(bluetooth::security::ConfirmationData data) {
376     waiting_for_pairing_prompt_ = false;
377     data.SetJustWorks(false);
378     HandleConfirm(data);
379   }
380 
DisplayYesNoDialog(bluetooth::security::ConfirmationData data)381   void DisplayYesNoDialog(bluetooth::security::ConfirmationData data) {
382     waiting_for_pairing_prompt_ = false;
383     data.SetJustWorks(true);
384     HandleConfirm(data);
385   }
386 
DisplayEnterPasskeyDialog(bluetooth::security::ConfirmationData data)387   void DisplayEnterPasskeyDialog(bluetooth::security::ConfirmationData data) {
388     waiting_for_pairing_prompt_ = false;
389     LOG_WARN("UNIMPLEMENTED, Passkey not supported in GD");
390   }
391 
DisplayPasskey(bluetooth::security::ConfirmationData data)392   void DisplayPasskey(bluetooth::security::ConfirmationData data) {
393     waiting_for_pairing_prompt_ = false;
394     LOG_WARN("UNIMPLEMENTED, Passkey not supported in GD");
395   }
396 
DisplayEnterPinDialog(bluetooth::security::ConfirmationData data)397   void DisplayEnterPinDialog(bluetooth::security::ConfirmationData data) {
398     waiting_for_pairing_prompt_ = false;
399     LOG_WARN("UNIMPLEMENTED, PIN not supported in GD");
400   }
401 
402   bool waiting_for_pairing_prompt_ = false;
403 
404  private:
ShimUi()405   ShimUi() : bta_callbacks_(nullptr) {}
~ShimUi()406   ~ShimUi() {}
407   const tBTM_APPL_INFO* bta_callbacks_;
408 };
409 
410 ShimUi* shim_ui_ = nullptr;
411 
412 class ShimBondListener : public bluetooth::security::ISecurityManagerListener {
413  public:
GetInstance()414   static ShimBondListener* GetInstance() {
415     static ShimBondListener instance;
416     return &instance;
417   }
418 
419   ShimBondListener(const ShimBondListener&) = delete;
420   ShimBondListener& operator=(const ShimBondListener&) = delete;
421 
SetBtaCallbacks(const tBTM_APPL_INFO * bta_callbacks)422   void SetBtaCallbacks(const tBTM_APPL_INFO* bta_callbacks) {
423     bta_callbacks_ = bta_callbacks;
424     if (bta_callbacks->p_pin_callback == nullptr) {
425       LOG_INFO("UNIMPLEMENTED %s pin_callback", __func__);
426     }
427 
428     if (bta_callbacks->p_link_key_callback == nullptr) {
429       LOG_INFO("UNIMPLEMENTED %s link_key_callback", __func__);
430     }
431 
432     if (bta_callbacks->p_auth_complete_callback == nullptr) {
433       LOG_INFO("UNIMPLEMENTED %s auth_complete_callback", __func__);
434     }
435 
436     if (bta_callbacks->p_bond_cancel_cmpl_callback == nullptr) {
437       LOG_INFO("UNIMPLEMENTED %s bond_cancel_complete_callback", __func__);
438     }
439 
440     if (bta_callbacks->p_le_callback == nullptr) {
441       LOG_INFO("UNIMPLEMENTED %s le_callback", __func__);
442     }
443 
444     if (bta_callbacks->p_le_key_callback == nullptr) {
445       LOG_INFO("UNIMPLEMENTED %s le_key_callback", __func__);
446     }
447   }
448 
OnDeviceBonded(bluetooth::hci::AddressWithType device)449   void OnDeviceBonded(bluetooth::hci::AddressWithType device) override {
450     // Call sp_cback for LINK_KEY_NOTIFICATION
451     // Call AUTHENTICATION_COMPLETE callback
452     if (device.GetAddressType() ==
453         bluetooth::hci::AddressType::PUBLIC_DEVICE_ADDRESS) {
454       auto it = address_name_map_.find(device);
455       bt_bdname_t tmp_name;
456       if (it != address_name_map_.end()) {
457         tmp_name = it->second;
458       }
459       BD_NAME name;
460       memcpy((char*)name, tmp_name.name, BD_NAME_LEN);
461 
462       if (*bta_callbacks_->p_link_key_callback) {
463         LinkKey key;  // Never want to send the key to the stack
464         (*bta_callbacks_->p_link_key_callback)(
465             bluetooth::ToRawAddress(device.GetAddress()), 0, name, key,
466             BTM_LKEY_TYPE_COMBINATION, false /* is_ctkd */);
467       }
468       if (*bta_callbacks_->p_auth_complete_callback) {
469         (*bta_callbacks_->p_auth_complete_callback)(
470             bluetooth::ToRawAddress(device.GetAddress()), 0, name, HCI_SUCCESS);
471       }
472     }
473     bluetooth::shim::AllocateIdFromMetricIdAllocator(
474         bluetooth::ToRawAddress(device.GetAddress()));
475     bool is_saving_successful = bluetooth::shim::SaveDeviceOnMetricIdAllocator(
476         bluetooth::ToRawAddress(device.GetAddress()));
477     if (!is_saving_successful) {
478       LOG(FATAL) << __func__ << ": Fail to save metric id for device "
479                  << bluetooth::ToRawAddress(device.GetAddress());
480     }
481   }
482 
OnDeviceUnbonded(bluetooth::hci::AddressWithType device)483   void OnDeviceUnbonded(bluetooth::hci::AddressWithType device) override {
484     if (bta_callbacks_->p_bond_cancel_cmpl_callback) {
485       (*bta_callbacks_->p_bond_cancel_cmpl_callback)(BTM_SUCCESS);
486     }
487     bluetooth::shim::ForgetDeviceFromMetricIdAllocator(
488         bluetooth::ToRawAddress(device.GetAddress()));
489   }
490 
OnDeviceBondFailed(bluetooth::hci::AddressWithType device,bluetooth::security::PairingFailure status)491   void OnDeviceBondFailed(bluetooth::hci::AddressWithType device,
492                           bluetooth::security::PairingFailure status) override {
493     auto it = address_name_map_.find(device);
494     bt_bdname_t tmp_name;
495     if (it != address_name_map_.end()) {
496       tmp_name = it->second;
497     }
498     BD_NAME name;
499     memcpy((char*)name, tmp_name.name, BD_NAME_LEN);
500 
501     if (bta_callbacks_->p_auth_complete_callback) {
502       (*bta_callbacks_->p_auth_complete_callback)(
503           bluetooth::ToRawAddress(device.GetAddress()), 0, name,
504           HCI_ERR_AUTH_FAILURE);
505     }
506   }
507 
OnEncryptionStateChanged(bluetooth::hci::EncryptionChangeView encryption_change_view)508   void OnEncryptionStateChanged(
509       bluetooth::hci::EncryptionChangeView encryption_change_view) override {
510     // TODO(optedoblivion): Find BTA callback for this to call
511   }
512 
513  private:
ShimBondListener()514   ShimBondListener() : bta_callbacks_(nullptr) {}
~ShimBondListener()515   ~ShimBondListener() {}
516   const tBTM_APPL_INFO* bta_callbacks_;
517 };
518 
BTM_CancelRemoteDeviceName(void)519 tBTM_STATUS bluetooth::shim::BTM_CancelRemoteDeviceName(void) {
520   return Stack::GetInstance()->GetBtm()->CancelAllReadRemoteDeviceName();
521 }
522 
BTM_InqDbRead(const RawAddress & p_bda)523 tBTM_INQ_INFO* bluetooth::shim::BTM_InqDbRead(const RawAddress& p_bda) {
524   LOG_INFO("UNIMPLEMENTED %s", __func__);
525   return nullptr;
526 }
527 
BTM_InqDbFirst(void)528 tBTM_INQ_INFO* bluetooth::shim::BTM_InqDbFirst(void) {
529   LOG_INFO("UNIMPLEMENTED %s", __func__);
530   return nullptr;
531 }
532 
BTM_InqDbNext(tBTM_INQ_INFO * p_cur)533 tBTM_INQ_INFO* bluetooth::shim::BTM_InqDbNext(tBTM_INQ_INFO* p_cur) {
534   LOG_INFO("UNIMPLEMENTED %s", __func__);
535   CHECK(p_cur != nullptr);
536   return nullptr;
537 }
538 
BTM_ClearInqDb(const RawAddress * p_bda)539 tBTM_STATUS bluetooth::shim::BTM_ClearInqDb(const RawAddress* p_bda) {
540   LOG_INFO("UNIMPLEMENTED %s", __func__);
541   if (p_bda == nullptr) {
542     // clear all entries
543   } else {
544     // clear specific entry
545   }
546   return BTM_NO_RESOURCES;
547 }
548 
BTM_HasEirService(const uint32_t * p_eir_uuid,uint16_t uuid16)549 bool bluetooth::shim::BTM_HasEirService(const uint32_t* p_eir_uuid,
550                                         uint16_t uuid16) {
551   LOG_INFO("UNIMPLEMENTED %s", __func__);
552   CHECK(p_eir_uuid != nullptr);
553   return false;
554 }
555 
BTM_HasInquiryEirService(tBTM_INQ_RESULTS * p_results,uint16_t uuid16)556 tBTM_EIR_SEARCH_RESULT bluetooth::shim::BTM_HasInquiryEirService(
557     tBTM_INQ_RESULTS* p_results, uint16_t uuid16) {
558   LOG_INFO("UNIMPLEMENTED %s", __func__);
559   CHECK(p_results != nullptr);
560   return BTM_EIR_UNKNOWN;
561 }
562 
BTM_AddEirService(uint32_t * p_eir_uuid,uint16_t uuid16)563 void bluetooth::shim::BTM_AddEirService(uint32_t* p_eir_uuid, uint16_t uuid16) {
564   LOG_INFO("UNIMPLEMENTED %s", __func__);
565   CHECK(p_eir_uuid != nullptr);
566 }
567 
BTM_ReadConnectionAddr(const RawAddress & remote_bda,RawAddress & local_conn_addr,tBLE_ADDR_TYPE * p_addr_type)568 void bluetooth::shim::BTM_ReadConnectionAddr(const RawAddress& remote_bda,
569                                              RawAddress& local_conn_addr,
570                                              tBLE_ADDR_TYPE* p_addr_type) {
571   LOG_INFO("UNIMPLEMENTED %s", __func__);
572   CHECK(p_addr_type != nullptr);
573 }
574 
BTM_ReadRemoteConnectionAddr(const RawAddress & pseudo_addr,RawAddress & conn_addr,tBLE_ADDR_TYPE * p_addr_type)575 bool bluetooth::shim::BTM_ReadRemoteConnectionAddr(
576     const RawAddress& pseudo_addr, RawAddress& conn_addr,
577     tBLE_ADDR_TYPE* p_addr_type) {
578   LOG_INFO("UNIMPLEMENTED %s", __func__);
579   CHECK(p_addr_type != nullptr);
580   return false;
581 }
582 
BTM_BleSetConnScanParams(uint32_t scan_interval,uint32_t scan_window)583 void bluetooth::shim::BTM_BleSetConnScanParams(uint32_t scan_interval,
584                                                uint32_t scan_window) {
585   LOG_INFO("UNIMPLEMENTED %s", __func__);
586 }
587 
BTM_ReadDevInfo(const RawAddress & remote_bda,tBT_DEVICE_TYPE * p_dev_type,tBLE_ADDR_TYPE * p_addr_type)588 void bluetooth::shim::BTM_ReadDevInfo(const RawAddress& remote_bda,
589                                       tBT_DEVICE_TYPE* p_dev_type,
590                                       tBLE_ADDR_TYPE* p_addr_type) {
591   LOG_INFO("UNIMPLEMENTED %s", __func__);
592   CHECK(p_dev_type != nullptr);
593   CHECK(p_addr_type != nullptr);
594 }
595 
BTM_ReadConnectedTransportAddress(RawAddress * remote_bda,tBT_TRANSPORT transport)596 bool bluetooth::shim::BTM_ReadConnectedTransportAddress(
597     RawAddress* remote_bda, tBT_TRANSPORT transport) {
598   LOG_INFO("UNIMPLEMENTED %s", __func__);
599   CHECK(remote_bda != nullptr);
600   return false;
601 }
602 
BTM_GetLeSecurityState(const RawAddress & bd_addr,uint8_t * p_le_dev_sec_flags,uint8_t * p_le_key_size)603 bool bluetooth::shim::BTM_GetLeSecurityState(const RawAddress& bd_addr,
604                                              uint8_t* p_le_dev_sec_flags,
605                                              uint8_t* p_le_key_size) {
606   LOG_INFO("UNIMPLEMENTED %s", __func__);
607   CHECK(p_le_dev_sec_flags != nullptr);
608   CHECK(p_le_key_size != nullptr);
609   return false;
610 }
611 
BTM_BleSecurityProcedureIsRunning(const RawAddress & bd_addr)612 bool bluetooth::shim::BTM_BleSecurityProcedureIsRunning(
613     const RawAddress& bd_addr) {
614   LOG_INFO("UNIMPLEMENTED %s", __func__);
615   return false;
616 }
617 
BTM_BleGetSupportedKeySize(const RawAddress & bd_addr)618 uint8_t bluetooth::shim::BTM_BleGetSupportedKeySize(const RawAddress& bd_addr) {
619   LOG_INFO("UNIMPLEMENTED %s", __func__);
620   return 0;
621 }
622 
623 /**
624  * This function update(add,delete or clear) the adv local name filtering
625  * condition.
626  */
BTM_LE_PF_local_name(tBTM_BLE_SCAN_COND_OP action,tBTM_BLE_PF_FILT_INDEX filt_index,std::vector<uint8_t> name,tBTM_BLE_PF_CFG_CBACK cb)627 void bluetooth::shim::BTM_LE_PF_local_name(tBTM_BLE_SCAN_COND_OP action,
628                                            tBTM_BLE_PF_FILT_INDEX filt_index,
629                                            std::vector<uint8_t> name,
630                                            tBTM_BLE_PF_CFG_CBACK cb) {
631   LOG_INFO("UNIMPLEMENTED %s", __func__);
632 }
633 
BTM_LE_PF_srvc_data(tBTM_BLE_SCAN_COND_OP action,tBTM_BLE_PF_FILT_INDEX filt_index)634 void bluetooth::shim::BTM_LE_PF_srvc_data(tBTM_BLE_SCAN_COND_OP action,
635                                           tBTM_BLE_PF_FILT_INDEX filt_index) {
636   LOG_INFO("UNIMPLEMENTED %s", __func__);
637 }
638 
BTM_LE_PF_manu_data(tBTM_BLE_SCAN_COND_OP action,tBTM_BLE_PF_FILT_INDEX filt_index,uint16_t company_id,uint16_t company_id_mask,std::vector<uint8_t> data,std::vector<uint8_t> data_mask,tBTM_BLE_PF_CFG_CBACK cb)639 void bluetooth::shim::BTM_LE_PF_manu_data(
640     tBTM_BLE_SCAN_COND_OP action, tBTM_BLE_PF_FILT_INDEX filt_index,
641     uint16_t company_id, uint16_t company_id_mask, std::vector<uint8_t> data,
642     std::vector<uint8_t> data_mask, tBTM_BLE_PF_CFG_CBACK cb) {
643   LOG_INFO("UNIMPLEMENTED %s", __func__);
644 }
645 
BTM_LE_PF_srvc_data_pattern(tBTM_BLE_SCAN_COND_OP action,tBTM_BLE_PF_FILT_INDEX filt_index,std::vector<uint8_t> data,std::vector<uint8_t> data_mask,tBTM_BLE_PF_CFG_CBACK cb)646 void bluetooth::shim::BTM_LE_PF_srvc_data_pattern(
647     tBTM_BLE_SCAN_COND_OP action, tBTM_BLE_PF_FILT_INDEX filt_index,
648     std::vector<uint8_t> data, std::vector<uint8_t> data_mask,
649     tBTM_BLE_PF_CFG_CBACK cb) {
650   LOG_INFO("UNIMPLEMENTED %s", __func__);
651 }
652 
BTM_LE_PF_addr_filter(tBTM_BLE_SCAN_COND_OP action,tBTM_BLE_PF_FILT_INDEX filt_index,tBLE_BD_ADDR addr,tBTM_BLE_PF_CFG_CBACK cb)653 void bluetooth::shim::BTM_LE_PF_addr_filter(tBTM_BLE_SCAN_COND_OP action,
654                                             tBTM_BLE_PF_FILT_INDEX filt_index,
655                                             tBLE_BD_ADDR addr,
656                                             tBTM_BLE_PF_CFG_CBACK cb) {
657   LOG_INFO("UNIMPLEMENTED %s", __func__);
658 }
659 
BTM_LE_PF_uuid_filter(tBTM_BLE_SCAN_COND_OP action,tBTM_BLE_PF_FILT_INDEX filt_index,tBTM_BLE_PF_COND_TYPE filter_type,const bluetooth::Uuid & uuid,tBTM_BLE_PF_LOGIC_TYPE cond_logic,const bluetooth::Uuid & uuid_mask,tBTM_BLE_PF_CFG_CBACK cb)660 void bluetooth::shim::BTM_LE_PF_uuid_filter(tBTM_BLE_SCAN_COND_OP action,
661                                             tBTM_BLE_PF_FILT_INDEX filt_index,
662                                             tBTM_BLE_PF_COND_TYPE filter_type,
663                                             const bluetooth::Uuid& uuid,
664                                             tBTM_BLE_PF_LOGIC_TYPE cond_logic,
665                                             const bluetooth::Uuid& uuid_mask,
666                                             tBTM_BLE_PF_CFG_CBACK cb) {
667   LOG_INFO("UNIMPLEMENTED %s", __func__);
668 }
669 
BTM_LE_PF_set(tBTM_BLE_PF_FILT_INDEX filt_index,std::vector<ApcfCommand> commands,tBTM_BLE_PF_CFG_CBACK cb)670 void bluetooth::shim::BTM_LE_PF_set(tBTM_BLE_PF_FILT_INDEX filt_index,
671                                     std::vector<ApcfCommand> commands,
672                                     tBTM_BLE_PF_CFG_CBACK cb) {
673   LOG_INFO("UNIMPLEMENTED %s", __func__);
674 }
675 
BTM_LE_PF_clear(tBTM_BLE_PF_FILT_INDEX filt_index,tBTM_BLE_PF_CFG_CBACK cb)676 void bluetooth::shim::BTM_LE_PF_clear(tBTM_BLE_PF_FILT_INDEX filt_index,
677                                       tBTM_BLE_PF_CFG_CBACK cb) {
678   LOG_INFO("UNIMPLEMENTED %s", __func__);
679 }
680 
BTM_BleAdvFilterParamSetup(tBTM_BLE_SCAN_COND_OP action,tBTM_BLE_PF_FILT_INDEX filt_index,std::unique_ptr<btgatt_filt_param_setup_t> p_filt_params,tBTM_BLE_PF_PARAM_CB cb)681 void bluetooth::shim::BTM_BleAdvFilterParamSetup(
682     tBTM_BLE_SCAN_COND_OP action, tBTM_BLE_PF_FILT_INDEX filt_index,
683     std::unique_ptr<btgatt_filt_param_setup_t> p_filt_params,
684     tBTM_BLE_PF_PARAM_CB cb) {
685   LOG_INFO("UNIMPLEMENTED %s", __func__);
686 }
687 
BTM_BleUpdateAdvFilterPolicy(tBTM_BLE_AFP adv_policy)688 void bluetooth::shim::BTM_BleUpdateAdvFilterPolicy(tBTM_BLE_AFP adv_policy) {
689   LOG_INFO("UNIMPLEMENTED %s", __func__);
690 }
691 
BTM_BleEnableDisableFilterFeature(uint8_t enable,tBTM_BLE_PF_STATUS_CBACK p_stat_cback)692 void bluetooth::shim::BTM_BleEnableDisableFilterFeature(
693     uint8_t enable, tBTM_BLE_PF_STATUS_CBACK p_stat_cback) {
694   LOG_INFO("UNIMPLEMENTED %s", __func__);
695 }
696 
BTM_SecBond(const RawAddress & bd_addr,tBLE_ADDR_TYPE addr_type,tBT_TRANSPORT transport,tBT_DEVICE_TYPE device_type)697 tBTM_STATUS bluetooth::shim::BTM_SecBond(const RawAddress& bd_addr,
698                                          tBLE_ADDR_TYPE addr_type,
699                                          tBT_TRANSPORT transport,
700                                          tBT_DEVICE_TYPE device_type) {
701   return Stack::GetInstance()->GetBtm()->CreateBond(bd_addr, addr_type,
702                                                     transport, device_type);
703 }
704 
BTM_SecAddDevice(const RawAddress & bd_addr,DEV_CLASS dev_class,const BD_NAME & bd_name,uint8_t * features,LinkKey * link_key,uint8_t key_type,uint8_t pin_length)705 bool bluetooth::shim::BTM_SecAddDevice(const RawAddress& bd_addr,
706                                        DEV_CLASS dev_class,
707                                        const BD_NAME& bd_name,
708                                        uint8_t* features, LinkKey* link_key,
709                                        uint8_t key_type, uint8_t pin_length) {
710   // Check if GD has a security record for the device
711   return BTM_SUCCESS;
712 }
713 
BTM_ConfirmReqReply(tBTM_STATUS res,const RawAddress & bd_addr)714 void bluetooth::shim::BTM_ConfirmReqReply(tBTM_STATUS res,
715                                           const RawAddress& bd_addr) {
716   // Send for both Classic and LE until we can determine the type
717   bool accept = res == BTM_SUCCESS;
718   hci::AddressWithType address = ToAddressWithType(bd_addr, BLE_ADDR_PUBLIC);
719   hci::AddressWithType address2 = ToAddressWithType(bd_addr, BLE_ADDR_RANDOM);
720   auto security_manager =
721       bluetooth::shim::GetSecurityModule()->GetSecurityManager();
722   if (ShimUi::GetInstance()->waiting_for_pairing_prompt_) {
723     LOG(INFO) << "interpreting confirmation as pairing accept " << address;
724     security_manager->OnPairingPromptAccepted(address, accept);
725     security_manager->OnPairingPromptAccepted(address2, accept);
726     ShimUi::GetInstance()->waiting_for_pairing_prompt_ = false;
727   } else {
728     LOG(INFO) << "interpreting confirmation as yes/no confirmation " << address;
729     security_manager->OnConfirmYesNo(address, accept);
730     security_manager->OnConfirmYesNo(address2, accept);
731   }
732 }
733 
BTM_GetHCIConnHandle(const RawAddress & remote_bda,tBT_TRANSPORT transport)734 uint16_t bluetooth::shim::BTM_GetHCIConnHandle(const RawAddress& remote_bda,
735                                                tBT_TRANSPORT transport) {
736   return Stack::GetInstance()->GetBtm()->GetAclHandle(remote_bda, transport);
737 }
738 
BTM_SecClearSecurityFlags(const RawAddress & bd_addr)739 void bluetooth::shim::BTM_SecClearSecurityFlags(const RawAddress& bd_addr) {
740   // TODO(optedoblivion): Call RemoveBond on device address
741 }
742 
BTM_SecReadDevName(const RawAddress & address)743 char* bluetooth::shim::BTM_SecReadDevName(const RawAddress& address) {
744   static char name[] = "TODO: See if this is needed";
745   return name;
746 }
747 
BTM_SecAddRmtNameNotifyCallback(tBTM_RMT_NAME_CALLBACK * p_callback)748 bool bluetooth::shim::BTM_SecAddRmtNameNotifyCallback(
749     tBTM_RMT_NAME_CALLBACK* p_callback) {
750   // TODO(optedoblivion): keep track of callback
751   LOG_WARN("Unimplemented");
752   return true;
753 }
754 
BTM_SecDeleteRmtNameNotifyCallback(tBTM_RMT_NAME_CALLBACK * p_callback)755 bool bluetooth::shim::BTM_SecDeleteRmtNameNotifyCallback(
756     tBTM_RMT_NAME_CALLBACK* p_callback) {
757   // TODO(optedoblivion): stop keeping track of callback
758   LOG_WARN("Unimplemented");
759   return true;
760 }
761 
BTM_PINCodeReply(const RawAddress & bd_addr,tBTM_STATUS res,uint8_t pin_len,uint8_t * p_pin)762 void bluetooth::shim::BTM_PINCodeReply(const RawAddress& bd_addr,
763                                        tBTM_STATUS res, uint8_t pin_len,
764                                        uint8_t* p_pin) {
765   ASSERT_LOG(!bluetooth::shim::is_gd_shim_enabled(), "Unreachable code path");
766 }
767 
BTM_RemoteOobDataReply(tBTM_STATUS res,const RawAddress & bd_addr,const Octet16 & c,const Octet16 & r)768 void bluetooth::shim::BTM_RemoteOobDataReply(tBTM_STATUS res,
769                                              const RawAddress& bd_addr,
770                                              const Octet16& c,
771                                              const Octet16& r) {
772   ASSERT_LOG(!bluetooth::shim::is_gd_shim_enabled(), "Unreachable code path");
773 }
774 
BTM_SetDeviceClass(DEV_CLASS dev_class)775 tBTM_STATUS bluetooth::shim::BTM_SetDeviceClass(DEV_CLASS dev_class) {
776   // TODO(optedoblivion): see if we need this, I don't think we do
777   LOG_WARN("Unimplemented");
778   return BTM_SUCCESS;
779 }
780 
BTM_ClearEventFilter()781 tBTM_STATUS bluetooth::shim::BTM_ClearEventFilter() {
782   controller_get_interface()->clear_event_filter();
783   return BTM_SUCCESS;
784 }
785 
BTM_ClearEventMask()786 tBTM_STATUS bluetooth::shim::BTM_ClearEventMask() {
787   controller_get_interface()->clear_event_mask();
788   return BTM_SUCCESS;
789 }
790 
BTM_ClearFilterAcceptList()791 tBTM_STATUS bluetooth::shim::BTM_ClearFilterAcceptList() {
792   Stack::GetInstance()->GetAcl()->ClearFilterAcceptList();
793   return BTM_SUCCESS;
794 }
795 
BTM_DisconnectAllAcls()796 tBTM_STATUS bluetooth::shim::BTM_DisconnectAllAcls() {
797   Stack::GetInstance()->GetAcl()->DisconnectAllForSuspend();
798 //  Stack::GetInstance()->GetAcl()->Shutdown();
799   return BTM_SUCCESS;
800 }
801 
BTM_LeRand(LeRandCallback cb)802 tBTM_STATUS bluetooth::shim::BTM_LeRand(LeRandCallback cb) {
803   Stack::GetInstance()->GetAcl()->LeRand(cb);
804   return BTM_SUCCESS;
805 }
806 
BTM_SetEventFilterConnectionSetupAllDevices()807 tBTM_STATUS bluetooth::shim::BTM_SetEventFilterConnectionSetupAllDevices() {
808   // Autoplumbed
809   controller_get_interface()->set_event_filter_connection_setup_all_devices();
810   return BTM_SUCCESS;
811 }
812 
BTM_AllowWakeByHid(std::vector<RawAddress> classic_hid_devices,std::vector<std::pair<RawAddress,uint8_t>> le_hid_devices)813 tBTM_STATUS bluetooth::shim::BTM_AllowWakeByHid(
814     std::vector<RawAddress> classic_hid_devices,
815     std::vector<std::pair<RawAddress, uint8_t>> le_hid_devices) {
816   // First set ACL to suspended state.
817   Stack::GetInstance()->GetAcl()->SetSystemSuspendState(/*suspended=*/true);
818 
819   // Allow classic HID wake.
820   controller_get_interface()->set_event_filter_allow_device_connection(
821       std::move(classic_hid_devices));
822 
823   // Allow BLE HID
824   for (auto hid_address : le_hid_devices) {
825     std::promise<bool> accept_promise;
826     auto accept_future = accept_promise.get_future();
827 
828     Stack::GetInstance()->GetAcl()->AcceptLeConnectionFrom(
829         ToAddressWithType(hid_address.first, hid_address.second),
830         /*is_direct=*/false, std::move(accept_promise));
831 
832     accept_future.wait();
833   }
834 
835   return BTM_SUCCESS;
836 }
837 
BTM_RestoreFilterAcceptList(std::vector<std::pair<RawAddress,uint8_t>> le_devices)838 tBTM_STATUS bluetooth::shim::BTM_RestoreFilterAcceptList(
839     std::vector<std::pair<RawAddress, uint8_t>> le_devices) {
840   // First, mark ACL as no longer suspended.
841   Stack::GetInstance()->GetAcl()->SetSystemSuspendState(/*suspended=*/false);
842 
843   // Next, Allow BLE connection from all devices that need to be restored.
844   // This will also re-arm the LE connection.
845   for (auto address_pair : le_devices) {
846     std::promise<bool> accept_promise;
847     auto accept_future = accept_promise.get_future();
848 
849     Stack::GetInstance()->GetAcl()->AcceptLeConnectionFrom(
850         ToAddressWithType(address_pair.first, address_pair.second),
851         /*is_direct=*/false, std::move(accept_promise));
852 
853     accept_future.wait();
854   }
855 
856   return BTM_SUCCESS;
857 }
858 
BTM_SetDefaultEventMaskExcept(uint64_t mask,uint64_t le_mask)859 tBTM_STATUS bluetooth::shim::BTM_SetDefaultEventMaskExcept(uint64_t mask,
860                                                            uint64_t le_mask) {
861   // Autoplumbed
862   controller_get_interface()->set_default_event_mask_except(mask, le_mask);
863   return BTM_SUCCESS;
864 }
865 
BTM_SetEventFilterInquiryResultAllDevices()866 tBTM_STATUS bluetooth::shim::BTM_SetEventFilterInquiryResultAllDevices() {
867   // Autoplumbed
868   controller_get_interface()->set_event_filter_inquiry_result_all_devices();
869   return BTM_SUCCESS;
870 }
871 
BTM_BleResetId()872 tBTM_STATUS bluetooth::shim::BTM_BleResetId() {
873   btm_ble_reset_id();
874   return BTM_SUCCESS;
875 }
876