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 <base/callback.h>
20 
21 #include <mutex>
22 
23 #include "common/time_util.h"
24 #include "device/include/controller.h"
25 #include "gd/common/callback.h"
26 #include "gd/neighbor/name.h"
27 #include "main/shim/btm.h"
28 #include "main/shim/btm_api.h"
29 #include "main/shim/controller.h"
30 #include "main/shim/helpers.h"
31 #include "main/shim/shim.h"
32 #include "main/shim/stack.h"
33 #include "stack/btm/btm_int_types.h"
34 #include "types/raw_address.h"
35 
36 /**
37  * Legacy bluetooth module global control block state
38  *
39  * Mutex is used to synchronize access from the shim
40  * layer into the global control block.  This is used
41  * by the shim despite potentially arbitrary
42  * unsynchronized access by the legacy stack.
43  */
44 extern tBTM_CB btm_cb;
45 std::mutex btm_cb_mutex_;
46 
47 extern bool btm_inq_find_bdaddr(const RawAddress& p_bda);
48 extern tINQ_DB_ENT* btm_inq_db_find(const RawAddress& raw_address);
49 extern tINQ_DB_ENT* btm_inq_db_new(const RawAddress& p_bda);
50 
51 /**
52  * Legacy bluetooth btm stack entry points
53  */
54 extern void btm_acl_update_busy_level(tBTM_BLI_EVENT event);
55 extern void btm_clear_all_pending_le_entry(void);
56 extern void btm_clr_inq_result_flt(void);
57 extern void btm_set_eir_uuid(uint8_t* p_eir, tBTM_INQ_RESULTS* p_results);
58 extern void btm_sort_inq_result(void);
59 extern void btm_process_inq_complete(uint8_t status, uint8_t result_type);
60 
max_responses_reached()61 static bool max_responses_reached() {
62   return (btm_cb.btm_inq_vars.inqparms.max_resps &&
63           btm_cb.btm_inq_vars.inq_cmpl_info.num_resp >=
64               btm_cb.btm_inq_vars.inqparms.max_resps);
65 }
66 
is_periodic_inquiry_active()67 static bool is_periodic_inquiry_active() {
68   return btm_cb.btm_inq_vars.inq_active & BTM_PERIODIC_INQUIRY_ACTIVE;
69 }
70 
has_le_device(tBT_DEVICE_TYPE device_type)71 static bool has_le_device(tBT_DEVICE_TYPE device_type) {
72   return device_type & BT_DEVICE_TYPE_BLE;
73 }
74 
is_classic_device(tBT_DEVICE_TYPE device_type)75 static bool is_classic_device(tBT_DEVICE_TYPE device_type) {
76   return device_type == BT_DEVICE_TYPE_BREDR;
77 }
78 
has_classic_device(tBT_DEVICE_TYPE device_type)79 static bool has_classic_device(tBT_DEVICE_TYPE device_type) {
80   return device_type & BT_DEVICE_TYPE_BREDR;
81 }
82 
is_dual_mode_device(tBT_DEVICE_TYPE device_type)83 static bool is_dual_mode_device(tBT_DEVICE_TYPE device_type) {
84   return device_type == BT_DEVICE_TYPE_DUMO;
85 }
86 
is_observing_or_active_scanning()87 static bool is_observing_or_active_scanning() {
88   return btm_cb.btm_inq_vars.inqparms.mode & BTM_BLE_INQUIRY_MASK;
89 }
90 
check_exceeded_responses(tBT_DEVICE_TYPE device_type,bool scan_rsp)91 static void check_exceeded_responses(tBT_DEVICE_TYPE device_type,
92                                      bool scan_rsp) {
93   if (!is_periodic_inquiry_active() && max_responses_reached() &&
94       ((is_observing_or_active_scanning() && is_dual_mode_device(device_type) &&
95         scan_rsp) ||
96        (is_observing_or_active_scanning()))) {
97     LOG_INFO("UNIMPLEMENTED %s Device max responses found...cancelling inquiry",
98              __func__);
99   }
100 }
101 
btm_api_process_inquiry_result(const RawAddress & raw_address,uint8_t page_scan_rep_mode,DEV_CLASS device_class,uint16_t clock_offset)102 void btm_api_process_inquiry_result(const RawAddress& raw_address,
103                                     uint8_t page_scan_rep_mode,
104                                     DEV_CLASS device_class,
105                                     uint16_t clock_offset) {
106   tINQ_DB_ENT* p_i = btm_inq_db_find(raw_address);
107   if (max_responses_reached()) {
108     if (p_i == nullptr || !has_le_device(p_i->inq_info.results.device_type)) {
109       return;
110     }
111   }
112 
113   if (p_i == nullptr) {
114     p_i = btm_inq_db_new(raw_address);
115     CHECK(p_i != nullptr);
116   } else if (p_i->inq_count == btm_cb.btm_inq_vars.inq_counter &&
117              is_classic_device(p_i->inq_info.results.device_type)) {
118     return;
119   }
120 
121   p_i->inq_info.results.page_scan_rep_mode = page_scan_rep_mode;
122   p_i->inq_info.results.page_scan_per_mode = 0;  // RESERVED
123   p_i->inq_info.results.page_scan_mode = 0;      // RESERVED
124   p_i->inq_info.results.dev_class[0] = device_class[0];
125   p_i->inq_info.results.dev_class[1] = device_class[1];
126   p_i->inq_info.results.dev_class[2] = device_class[2];
127   p_i->inq_info.results.clock_offset = clock_offset | BTM_CLOCK_OFFSET_VALID;
128   p_i->inq_info.results.inq_result_type = BTM_INQ_RESULT_BR;
129   p_i->inq_info.results.rssi = BTM_INQ_RES_IGNORE_RSSI;
130 
131   p_i->time_of_resp = bluetooth::common::time_get_os_boottime_ms();
132   p_i->inq_count = btm_cb.btm_inq_vars.inq_counter;
133   p_i->inq_info.appl_knows_rem_name = false;
134 
135   if (p_i->inq_count != btm_cb.btm_inq_vars.inq_counter) {
136     p_i->inq_info.results.device_type = BT_DEVICE_TYPE_BREDR;
137     btm_cb.btm_inq_vars.inq_cmpl_info.num_resp++;
138     p_i->scan_rsp = false;
139   } else {
140     p_i->inq_info.results.device_type |= BT_DEVICE_TYPE_BREDR;
141   }
142 
143   check_exceeded_responses(p_i->inq_info.results.device_type, p_i->scan_rsp);
144   if (btm_cb.btm_inq_vars.p_inq_results_cb == nullptr) {
145     return;
146   }
147 
148   (btm_cb.btm_inq_vars.p_inq_results_cb)(&p_i->inq_info.results, nullptr, 0);
149 }
150 
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)151 void btm_api_process_inquiry_result_with_rssi(RawAddress raw_address,
152                                               uint8_t page_scan_rep_mode,
153                                               DEV_CLASS device_class,
154                                               uint16_t clock_offset,
155                                               int8_t rssi) {
156   tINQ_DB_ENT* p_i = btm_inq_db_find(raw_address);
157   if (max_responses_reached()) {
158     if (p_i == nullptr || !has_le_device(p_i->inq_info.results.device_type)) {
159       return;
160     }
161   }
162 
163   bool update = false;
164   if (btm_inq_find_bdaddr(raw_address)) {
165     if (btm_cb.btm_inq_vars.inqparms.report_dup && p_i != nullptr &&
166         (rssi > p_i->inq_info.results.rssi || p_i->inq_info.results.rssi == 0 ||
167          has_classic_device(p_i->inq_info.results.device_type))) {
168       update = true;
169     }
170   }
171 
172   bool is_new = true;
173   if (p_i == nullptr) {
174     p_i = btm_inq_db_new(raw_address);
175     CHECK(p_i != nullptr);
176   } else if (p_i->inq_count == btm_cb.btm_inq_vars.inq_counter &&
177              is_classic_device(p_i->inq_info.results.device_type)) {
178     is_new = false;
179   }
180 
181   p_i->inq_info.results.rssi = rssi;
182 
183   if (is_new) {
184     p_i->inq_info.results.page_scan_rep_mode = page_scan_rep_mode;
185     p_i->inq_info.results.page_scan_per_mode = 0;  // RESERVED
186     p_i->inq_info.results.page_scan_mode = 0;      // RESERVED
187     p_i->inq_info.results.dev_class[0] = device_class[0];
188     p_i->inq_info.results.dev_class[1] = device_class[1];
189     p_i->inq_info.results.dev_class[2] = device_class[2];
190     p_i->inq_info.results.clock_offset = clock_offset | BTM_CLOCK_OFFSET_VALID;
191     p_i->inq_info.results.inq_result_type = BTM_INQ_RESULT_BR;
192 
193     p_i->time_of_resp = bluetooth::common::time_get_os_boottime_ms();
194     p_i->inq_count = btm_cb.btm_inq_vars.inq_counter;
195     p_i->inq_info.appl_knows_rem_name = false;
196 
197     if (p_i->inq_count != btm_cb.btm_inq_vars.inq_counter) {
198       p_i->inq_info.results.device_type = BT_DEVICE_TYPE_BREDR;
199       btm_cb.btm_inq_vars.inq_cmpl_info.num_resp++;
200       p_i->scan_rsp = false;
201     } else {
202       p_i->inq_info.results.device_type |= BT_DEVICE_TYPE_BREDR;
203     }
204   }
205 
206   check_exceeded_responses(p_i->inq_info.results.device_type, p_i->scan_rsp);
207   if (btm_cb.btm_inq_vars.p_inq_results_cb == nullptr) {
208     return;
209   }
210 
211   if (is_new || update) {
212     (btm_cb.btm_inq_vars.p_inq_results_cb)(&p_i->inq_info.results, nullptr, 0);
213   }
214 }
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)215 void btm_api_process_extended_inquiry_result(RawAddress raw_address,
216                                              uint8_t page_scan_rep_mode,
217                                              DEV_CLASS device_class,
218                                              uint16_t clock_offset, int8_t rssi,
219                                              const uint8_t* eir_data,
220                                              size_t eir_len) {
221   tINQ_DB_ENT* p_i = btm_inq_db_find(raw_address);
222   if (max_responses_reached()) {
223     if (p_i == nullptr || !has_le_device(p_i->inq_info.results.device_type)) {
224       return;
225     }
226   }
227 
228   bool update = false;
229   if (btm_inq_find_bdaddr(raw_address) && p_i != nullptr) {
230     update = true;
231   }
232 
233   bool is_new = true;
234   if (p_i == nullptr) {
235     p_i = btm_inq_db_new(raw_address);
236   } else if (p_i->inq_count == btm_cb.btm_inq_vars.inq_counter &&
237              (p_i->inq_info.results.device_type == BT_DEVICE_TYPE_BREDR)) {
238     is_new = false;
239   }
240 
241   p_i->inq_info.results.rssi = rssi;
242 
243   if (is_new) {
244     p_i->inq_info.results.page_scan_rep_mode = page_scan_rep_mode;
245     p_i->inq_info.results.page_scan_per_mode = 0;  // RESERVED
246     p_i->inq_info.results.page_scan_mode = 0;      // RESERVED
247     p_i->inq_info.results.dev_class[0] = device_class[0];
248     p_i->inq_info.results.dev_class[1] = device_class[1];
249     p_i->inq_info.results.dev_class[2] = device_class[2];
250     p_i->inq_info.results.clock_offset = clock_offset | BTM_CLOCK_OFFSET_VALID;
251     p_i->inq_info.results.inq_result_type = BTM_INQ_RESULT_BR;
252 
253     p_i->time_of_resp = bluetooth::common::time_get_os_boottime_ms();
254     p_i->inq_count = btm_cb.btm_inq_vars.inq_counter;
255     p_i->inq_info.appl_knows_rem_name = false;
256 
257     if (p_i->inq_count != btm_cb.btm_inq_vars.inq_counter) {
258       p_i->inq_info.results.device_type = BT_DEVICE_TYPE_BREDR;
259       btm_cb.btm_inq_vars.inq_cmpl_info.num_resp++;
260       p_i->scan_rsp = false;
261     } else {
262       p_i->inq_info.results.device_type |= BT_DEVICE_TYPE_BREDR;
263     }
264   }
265 
266   check_exceeded_responses(p_i->inq_info.results.device_type, p_i->scan_rsp);
267   if (btm_cb.btm_inq_vars.p_inq_results_cb == nullptr) {
268     return;
269   }
270 
271   if (is_new || update) {
272     memset(p_i->inq_info.results.eir_uuid, 0,
273            BTM_EIR_SERVICE_ARRAY_SIZE * (BTM_EIR_ARRAY_BITS / 8));
274     btm_set_eir_uuid(const_cast<uint8_t*>(eir_data), &p_i->inq_info.results);
275     uint8_t* p_eir_data = const_cast<uint8_t*>(eir_data);
276     (btm_cb.btm_inq_vars.p_inq_results_cb)(&p_i->inq_info.results, p_eir_data,
277                                            eir_len);
278   }
279 }
280 
BTM_StartInquiry(tBTM_INQ_PARMS * p_inqparms,tBTM_INQ_RESULTS_CB * p_results_cb,tBTM_CMPL_CB * p_cmpl_cb)281 tBTM_STATUS bluetooth::shim::BTM_StartInquiry(tBTM_INQ_PARMS* p_inqparms,
282                                               tBTM_INQ_RESULTS_CB* p_results_cb,
283                                               tBTM_CMPL_CB* p_cmpl_cb) {
284   CHECK(p_inqparms != nullptr);
285   CHECK(p_results_cb != nullptr);
286   CHECK(p_cmpl_cb != nullptr);
287 
288   std::lock_guard<std::mutex> lock(btm_cb_mutex_);
289 
290   btm_cb.btm_inq_vars.inq_cmpl_info.num_resp = 0;
291   btm_cb.btm_inq_vars.scan_type = INQ_GENERAL;
292 
293   Stack::GetInstance()->GetBtm()->StartActiveScanning();
294   if (p_inqparms->duration != 0) {
295     Stack::GetInstance()->GetBtm()->SetScanningTimer(
296         p_inqparms->duration * 1000, common::BindOnce([]() {
297           LOG_INFO("%s scanning timeout popped", __func__);
298           std::lock_guard<std::mutex> lock(btm_cb_mutex_);
299           Stack::GetInstance()->GetBtm()->StopActiveScanning();
300         }));
301   }
302 
303   Stack::GetInstance()->GetBtm()->StartActiveScanning();
304 
305   uint8_t classic_mode = p_inqparms->mode & 0x0f;
306   if (!Stack::GetInstance()->GetBtm()->SetInquiryFilter(
307           classic_mode, p_inqparms->filter_cond_type,
308           p_inqparms->filter_cond)) {
309     LOG_WARN("%s Unable to set inquiry filter", __func__);
310     return BTM_ERR_PROCESSING;
311   }
312 
313   if (!Stack::GetInstance()->GetBtm()->StartInquiry(
314           classic_mode, p_inqparms->duration, p_inqparms->max_resps,
315           [](uint16_t status, uint8_t inquiry_mode) {
316             LOG_DEBUG("%s Inquiry is complete status:%hd inquiry_mode:%hhd",
317                       __func__, status, inquiry_mode);
318             btm_cb.btm_inq_vars.inqparms.mode &= ~(inquiry_mode);
319 
320             btm_acl_update_busy_level(BTM_BLI_INQ_DONE_EVT);
321             if (btm_cb.btm_inq_vars.inq_active) {
322               btm_cb.btm_inq_vars.inq_cmpl_info.status = status;
323               btm_clear_all_pending_le_entry();
324               btm_cb.btm_inq_vars.state = BTM_INQ_INACTIVE_STATE;
325 
326               /* Increment so the start of a next inquiry has a new count */
327               btm_cb.btm_inq_vars.inq_counter++;
328 
329               btm_clr_inq_result_flt();
330 
331               if ((status == BTM_SUCCESS) &&
332                   controller_get_interface()
333                       ->supports_rssi_with_inquiry_results()) {
334                 btm_sort_inq_result();
335               }
336 
337               btm_cb.btm_inq_vars.inq_active = BTM_INQUIRY_INACTIVE;
338               btm_cb.btm_inq_vars.p_inq_results_cb = nullptr;
339               btm_cb.btm_inq_vars.p_inq_cmpl_cb = nullptr;
340 
341               if (btm_cb.btm_inq_vars.p_inq_cmpl_cb != nullptr) {
342                 LOG_DEBUG("%s Sending inquiry completion to upper layer",
343                           __func__);
344                 (btm_cb.btm_inq_vars.p_inq_cmpl_cb)(
345                     (tBTM_INQUIRY_CMPL*)&btm_cb.btm_inq_vars.inq_cmpl_info);
346                 btm_cb.btm_inq_vars.p_inq_cmpl_cb = nullptr;
347               }
348             }
349             if (btm_cb.btm_inq_vars.inqparms.mode == BTM_INQUIRY_NONE &&
350                 btm_cb.btm_inq_vars.scan_type == INQ_GENERAL) {
351               btm_cb.btm_inq_vars.scan_type = INQ_NONE;
352             }
353           })) {
354     LOG_WARN("%s Unable to start inquiry", __func__);
355     return BTM_ERR_PROCESSING;
356   }
357 
358   btm_cb.btm_inq_vars.state = BTM_INQ_ACTIVE_STATE;
359   btm_cb.btm_inq_vars.p_inq_cmpl_cb = p_cmpl_cb;
360   btm_cb.btm_inq_vars.p_inq_results_cb = p_results_cb;
361   btm_cb.btm_inq_vars.inq_active = p_inqparms->mode;
362 
363   btm_acl_update_busy_level(BTM_BLI_INQ_EVT);
364 
365   return BTM_CMD_STARTED;
366 }
367 
BTM_SetDiscoverability(uint16_t discoverable_mode,uint16_t window,uint16_t interval)368 tBTM_STATUS bluetooth::shim::BTM_SetDiscoverability(uint16_t discoverable_mode,
369                                                     uint16_t window,
370                                                     uint16_t interval) {
371   uint16_t classic_discoverable_mode = discoverable_mode & 0xff;
372   uint16_t le_discoverable_mode = discoverable_mode >> 8;
373 
374   if (window == 0) window = BTM_DEFAULT_DISC_WINDOW;
375   if (interval == 0) interval = BTM_DEFAULT_DISC_INTERVAL;
376 
377   switch (le_discoverable_mode) {
378     case kDiscoverableModeOff:
379       Stack::GetInstance()->GetBtm()->StopAdvertising();
380       break;
381     case kLimitedDiscoverableMode:
382     case kGeneralDiscoverableMode:
383       Stack::GetInstance()->GetBtm()->StartAdvertising();
384       break;
385     default:
386       LOG_WARN("%s Unexpected le discoverability mode:%d", __func__,
387                le_discoverable_mode);
388   }
389 
390   switch (classic_discoverable_mode) {
391     case kDiscoverableModeOff:
392       Stack::GetInstance()->GetBtm()->SetClassicDiscoverabilityOff();
393       break;
394     case kLimitedDiscoverableMode:
395       Stack::GetInstance()->GetBtm()->SetClassicLimitedDiscoverability(
396           window, interval);
397       break;
398     case kGeneralDiscoverableMode:
399       Stack::GetInstance()->GetBtm()->SetClassicGeneralDiscoverability(
400           window, interval);
401       break;
402     default:
403       LOG_WARN("%s Unexpected classic discoverability mode:%d", __func__,
404                classic_discoverable_mode);
405   }
406   return BTM_SUCCESS;
407 }
408 
BTM_SetInquiryScanType(uint16_t scan_type)409 tBTM_STATUS bluetooth::shim::BTM_SetInquiryScanType(uint16_t scan_type) {
410   switch (scan_type) {
411     case kInterlacedScanType:
412       Stack::GetInstance()->GetBtm()->SetInterlacedInquiryScan();
413       return BTM_SUCCESS;
414       break;
415     case kStandardScanType:
416       Stack::GetInstance()->GetBtm()->SetStandardInquiryScan();
417       return BTM_SUCCESS;
418       break;
419     default:
420       return BTM_ILLEGAL_VALUE;
421   }
422   return BTM_WRONG_MODE;
423 }
424 
BTM_BleObserve(bool start,uint8_t duration_sec,tBTM_INQ_RESULTS_CB * p_results_cb,tBTM_CMPL_CB * p_cmpl_cb)425 tBTM_STATUS bluetooth::shim::BTM_BleObserve(bool start, uint8_t duration_sec,
426                                             tBTM_INQ_RESULTS_CB* p_results_cb,
427                                             tBTM_CMPL_CB* p_cmpl_cb) {
428   if (start) {
429     CHECK(p_results_cb != nullptr);
430     CHECK(p_cmpl_cb != nullptr);
431 
432     std::lock_guard<std::mutex> lock(btm_cb_mutex_);
433 
434     if (btm_cb.ble_ctr_cb.scan_activity & BTM_LE_OBSERVE_ACTIVE) {
435       LOG_WARN("%s Observing already active", __func__);
436       return BTM_WRONG_MODE;
437     }
438 
439     btm_cb.ble_ctr_cb.p_obs_results_cb = p_results_cb;
440     btm_cb.ble_ctr_cb.p_obs_cmpl_cb = p_cmpl_cb;
441     Stack::GetInstance()->GetBtm()->StartObserving();
442     btm_cb.ble_ctr_cb.scan_activity |= BTM_LE_OBSERVE_ACTIVE;
443 
444     if (duration_sec != 0) {
445       Stack::GetInstance()->GetBtm()->SetObservingTimer(
446           duration_sec * 1000, common::BindOnce([]() {
447             LOG_DEBUG("%s observing timeout popped", __func__);
448 
449             Stack::GetInstance()->GetBtm()->CancelObservingTimer();
450             Stack::GetInstance()->GetBtm()->StopObserving();
451 
452             std::lock_guard<std::mutex> lock(btm_cb_mutex_);
453             btm_cb.ble_ctr_cb.scan_activity &= ~BTM_LE_OBSERVE_ACTIVE;
454 
455             if (btm_cb.ble_ctr_cb.p_obs_cmpl_cb) {
456               (btm_cb.ble_ctr_cb.p_obs_cmpl_cb)(
457                   &btm_cb.btm_inq_vars.inq_cmpl_info);
458             }
459             btm_cb.ble_ctr_cb.p_obs_results_cb = nullptr;
460             btm_cb.ble_ctr_cb.p_obs_cmpl_cb = nullptr;
461 
462             btm_cb.btm_inq_vars.inqparms.mode &= ~(BTM_BLE_INQUIRY_MASK);
463             btm_cb.btm_inq_vars.scan_type = INQ_NONE;
464 
465             btm_acl_update_busy_level(BTM_BLI_INQ_DONE_EVT);
466 
467             btm_clear_all_pending_le_entry();
468             btm_cb.btm_inq_vars.state = BTM_INQ_INACTIVE_STATE;
469 
470             btm_cb.btm_inq_vars.inq_counter++;
471             btm_clr_inq_result_flt();
472             btm_sort_inq_result();
473 
474             btm_cb.btm_inq_vars.inq_active = BTM_INQUIRY_INACTIVE;
475             btm_cb.btm_inq_vars.p_inq_results_cb = NULL;
476             btm_cb.btm_inq_vars.p_inq_cmpl_cb = NULL;
477 
478             if (btm_cb.btm_inq_vars.p_inq_cmpl_cb) {
479               (btm_cb.btm_inq_vars.p_inq_cmpl_cb)(
480                   (tBTM_INQUIRY_CMPL*)&btm_cb.btm_inq_vars.inq_cmpl_info);
481               btm_cb.btm_inq_vars.p_inq_cmpl_cb = nullptr;
482             }
483           }));
484     }
485   } else {
486     std::lock_guard<std::mutex> lock(btm_cb_mutex_);
487 
488     if (!(btm_cb.ble_ctr_cb.scan_activity & BTM_LE_OBSERVE_ACTIVE)) {
489       LOG_WARN("%s Observing already inactive", __func__);
490     }
491     Stack::GetInstance()->GetBtm()->CancelObservingTimer();
492     Stack::GetInstance()->GetBtm()->StopObserving();
493     btm_cb.ble_ctr_cb.scan_activity &= ~BTM_LE_OBSERVE_ACTIVE;
494     Stack::GetInstance()->GetBtm()->StopObserving();
495     if (btm_cb.ble_ctr_cb.p_obs_cmpl_cb) {
496       (btm_cb.ble_ctr_cb.p_obs_cmpl_cb)(&btm_cb.btm_inq_vars.inq_cmpl_info);
497     }
498     btm_cb.ble_ctr_cb.p_obs_results_cb = nullptr;
499     btm_cb.ble_ctr_cb.p_obs_cmpl_cb = nullptr;
500   }
501   return BTM_CMD_STARTED;
502 }
503 
BTM_SetPageScanType(uint16_t scan_type)504 tBTM_STATUS bluetooth::shim::BTM_SetPageScanType(uint16_t scan_type) {
505   switch (scan_type) {
506     case kInterlacedScanType:
507       if (!Stack::GetInstance()->GetBtm()->IsInterlacedScanSupported()) {
508         return BTM_MODE_UNSUPPORTED;
509       }
510       Stack::GetInstance()->GetBtm()->SetInterlacedPageScan();
511       return BTM_SUCCESS;
512       break;
513     case kStandardScanType:
514       Stack::GetInstance()->GetBtm()->SetStandardPageScan();
515       return BTM_SUCCESS;
516       break;
517     default:
518       return BTM_ILLEGAL_VALUE;
519   }
520   return BTM_WRONG_MODE;
521 }
522 
BTM_SetInquiryMode(uint8_t inquiry_mode)523 tBTM_STATUS bluetooth::shim::BTM_SetInquiryMode(uint8_t inquiry_mode) {
524   switch (inquiry_mode) {
525     case kStandardInquiryResult:
526       Stack::GetInstance()->GetBtm()->SetStandardInquiryResultMode();
527       break;
528     case kInquiryResultWithRssi:
529       Stack::GetInstance()->GetBtm()->SetInquiryWithRssiResultMode();
530       break;
531     case kExtendedInquiryResult:
532       Stack::GetInstance()->GetBtm()->SetExtendedInquiryResultMode();
533       break;
534     default:
535       return BTM_ILLEGAL_VALUE;
536   }
537   return BTM_SUCCESS;
538 }
539 
BTM_ReadDiscoverability(uint16_t * p_window,uint16_t * p_interval)540 uint16_t bluetooth::shim::BTM_ReadDiscoverability(uint16_t* p_window,
541                                                   uint16_t* p_interval) {
542   DiscoverabilityState state =
543       Stack::GetInstance()->GetBtm()->GetClassicDiscoverabilityState();
544 
545   if (p_interval) *p_interval = state.interval;
546   if (p_window) *p_window = state.window;
547 
548   return state.mode;
549 }
550 
BTM_CancelPeriodicInquiry(void)551 tBTM_STATUS bluetooth::shim::BTM_CancelPeriodicInquiry(void) {
552   Stack::GetInstance()->GetBtm()->CancelPeriodicInquiry();
553   return BTM_SUCCESS;
554 }
555 
BTM_SetConnectability(uint16_t page_mode,uint16_t window,uint16_t interval)556 tBTM_STATUS bluetooth::shim::BTM_SetConnectability(uint16_t page_mode,
557                                                    uint16_t window,
558                                                    uint16_t interval) {
559   uint16_t classic_connectible_mode = page_mode & 0xff;
560   uint16_t le_connectible_mode = page_mode >> 8;
561 
562   if (!window) window = BTM_DEFAULT_CONN_WINDOW;
563   if (!interval) interval = BTM_DEFAULT_CONN_INTERVAL;
564 
565   switch (le_connectible_mode) {
566     case kConnectibleModeOff:
567       Stack::GetInstance()->GetBtm()->StopConnectability();
568       break;
569     case kConnectibleModeOn:
570       Stack::GetInstance()->GetBtm()->StartConnectability();
571       break;
572     default:
573       return BTM_ILLEGAL_VALUE;
574       break;
575   }
576 
577   switch (classic_connectible_mode) {
578     case kConnectibleModeOff:
579       Stack::GetInstance()->GetBtm()->SetClassicConnectibleOff();
580       break;
581     case kConnectibleModeOn:
582       Stack::GetInstance()->GetBtm()->SetClassicConnectibleOn();
583       break;
584     default:
585       return BTM_ILLEGAL_VALUE;
586       break;
587   }
588   return BTM_SUCCESS;
589 }
590 
BTM_ReadConnectability(uint16_t * p_window,uint16_t * p_interval)591 uint16_t bluetooth::shim::BTM_ReadConnectability(uint16_t* p_window,
592                                                  uint16_t* p_interval) {
593   ConnectabilityState state =
594       Stack::GetInstance()->GetBtm()->GetClassicConnectabilityState();
595 
596   if (p_window) *p_window = state.window;
597   if (p_interval) *p_interval = state.interval;
598 
599   return state.mode;
600 }
601 
BTM_IsInquiryActive(void)602 uint16_t bluetooth::shim::BTM_IsInquiryActive(void) {
603   if (Stack::GetInstance()->GetBtm()->IsLimitedInquiryActive()) {
604     return BTM_LIMITED_INQUIRY_ACTIVE;
605   } else if (Stack::GetInstance()->GetBtm()->IsGeneralInquiryActive()) {
606     return BTM_GENERAL_INQUIRY_ACTIVE;
607   } else if (Stack::GetInstance()->GetBtm()->IsGeneralPeriodicInquiryActive() ||
608              Stack::GetInstance()->GetBtm()->IsLimitedPeriodicInquiryActive()) {
609     return BTM_PERIODIC_INQUIRY_ACTIVE;
610   }
611   return BTM_INQUIRY_INACTIVE;
612 }
613 
BTM_CancelInquiry(void)614 tBTM_STATUS bluetooth::shim::BTM_CancelInquiry(void) {
615   LOG_DEBUG("%s Cancel inquiry", __func__);
616   Stack::GetInstance()->GetBtm()->CancelInquiry();
617 
618   btm_cb.btm_inq_vars.state = BTM_INQ_INACTIVE_STATE;
619   btm_clr_inq_result_flt();
620 
621   Stack::GetInstance()->GetBtm()->CancelScanningTimer();
622   Stack::GetInstance()->GetBtm()->StopActiveScanning();
623 
624   btm_cb.ble_ctr_cb.scan_activity &= ~BTM_BLE_INQUIRY_MASK;
625 
626   btm_cb.btm_inq_vars.inqparms.mode &=
627       ~(btm_cb.btm_inq_vars.inqparms.mode & BTM_BLE_INQUIRY_MASK);
628 
629   btm_acl_update_busy_level(BTM_BLI_INQ_DONE_EVT);
630   /* Ignore any stray or late complete messages if the inquiry is not active */
631   if (btm_cb.btm_inq_vars.inq_active) {
632     btm_cb.btm_inq_vars.inq_cmpl_info.status = BTM_SUCCESS;
633     btm_clear_all_pending_le_entry();
634 
635     if (controller_get_interface()->supports_rssi_with_inquiry_results()) {
636       btm_sort_inq_result();
637     }
638 
639     btm_cb.btm_inq_vars.inq_active = BTM_INQUIRY_INACTIVE;
640     btm_cb.btm_inq_vars.p_inq_results_cb = nullptr;
641     btm_cb.btm_inq_vars.p_inq_cmpl_cb = nullptr;
642     btm_cb.btm_inq_vars.inq_counter++;
643 
644     if (btm_cb.btm_inq_vars.p_inq_cmpl_cb != nullptr) {
645       LOG_DEBUG("%s Sending cancel inquiry completion to upper layer",
646                 __func__);
647       (btm_cb.btm_inq_vars.p_inq_cmpl_cb)(
648           (tBTM_INQUIRY_CMPL*)&btm_cb.btm_inq_vars.inq_cmpl_info);
649       btm_cb.btm_inq_vars.p_inq_cmpl_cb = nullptr;
650     }
651   }
652   if (btm_cb.btm_inq_vars.inqparms.mode == BTM_INQUIRY_NONE &&
653       btm_cb.btm_inq_vars.scan_type == INQ_GENERAL) {
654     btm_cb.btm_inq_vars.scan_type = INQ_NONE;
655   }
656   return BTM_SUCCESS;
657 }
658 
BTM_ReadRemoteDeviceName(const RawAddress & raw_address,tBTM_CMPL_CB * callback,tBT_TRANSPORT transport)659 tBTM_STATUS bluetooth::shim::BTM_ReadRemoteDeviceName(
660     const RawAddress& raw_address, tBTM_CMPL_CB* callback,
661     tBT_TRANSPORT transport) {
662   CHECK(callback != nullptr);
663   tBTM_STATUS status = BTM_NO_RESOURCES;
664 
665   switch (transport) {
666     case BT_TRANSPORT_LE:
667       status = Stack::GetInstance()->GetBtm()->ReadLeRemoteDeviceName(
668           raw_address, callback);
669       break;
670     case BT_TRANSPORT_BR_EDR:
671       status = Stack::GetInstance()->GetBtm()->ReadClassicRemoteDeviceName(
672           raw_address, callback);
673       break;
674     default:
675       LOG_WARN("%s Unspecified transport:%d", __func__, transport);
676       break;
677   }
678   return status;
679 }
680 
BTM_CancelRemoteDeviceName(void)681 tBTM_STATUS bluetooth::shim::BTM_CancelRemoteDeviceName(void) {
682   return Stack::GetInstance()->GetBtm()->CancelAllReadRemoteDeviceName();
683 }
684 
BTM_InqDbRead(const RawAddress & p_bda)685 tBTM_INQ_INFO* bluetooth::shim::BTM_InqDbRead(const RawAddress& p_bda) {
686   LOG_INFO("UNIMPLEMENTED %s", __func__);
687   return nullptr;
688 }
689 
BTM_InqDbFirst(void)690 tBTM_INQ_INFO* bluetooth::shim::BTM_InqDbFirst(void) {
691   LOG_INFO("UNIMPLEMENTED %s", __func__);
692   return nullptr;
693 }
694 
BTM_InqDbNext(tBTM_INQ_INFO * p_cur)695 tBTM_INQ_INFO* bluetooth::shim::BTM_InqDbNext(tBTM_INQ_INFO* p_cur) {
696   LOG_INFO("UNIMPLEMENTED %s", __func__);
697   CHECK(p_cur != nullptr);
698   return nullptr;
699 }
700 
BTM_ClearInqDb(const RawAddress * p_bda)701 tBTM_STATUS bluetooth::shim::BTM_ClearInqDb(const RawAddress* p_bda) {
702   LOG_INFO("UNIMPLEMENTED %s", __func__);
703   if (p_bda == nullptr) {
704     // clear all entries
705   } else {
706     // clear specific entry
707   }
708   return BTM_NO_RESOURCES;
709 }
710 
BTM_WriteEIR(BT_HDR * p_buff)711 tBTM_STATUS bluetooth::shim::BTM_WriteEIR(BT_HDR* p_buff) {
712   LOG_INFO("UNIMPLEMENTED %s", __func__);
713   CHECK(p_buff != nullptr);
714   return BTM_NO_RESOURCES;
715 }
716 
BTM_HasEirService(const uint32_t * p_eir_uuid,uint16_t uuid16)717 bool bluetooth::shim::BTM_HasEirService(const uint32_t* p_eir_uuid,
718                                         uint16_t uuid16) {
719   LOG_INFO("UNIMPLEMENTED %s", __func__);
720   CHECK(p_eir_uuid != nullptr);
721   return false;
722 }
723 
BTM_HasInquiryEirService(tBTM_INQ_RESULTS * p_results,uint16_t uuid16)724 tBTM_EIR_SEARCH_RESULT bluetooth::shim::BTM_HasInquiryEirService(
725     tBTM_INQ_RESULTS* p_results, uint16_t uuid16) {
726   LOG_INFO("UNIMPLEMENTED %s", __func__);
727   CHECK(p_results != nullptr);
728   return BTM_EIR_UNKNOWN;
729 }
730 
BTM_AddEirService(uint32_t * p_eir_uuid,uint16_t uuid16)731 void bluetooth::shim::BTM_AddEirService(uint32_t* p_eir_uuid, uint16_t uuid16) {
732   LOG_INFO("UNIMPLEMENTED %s", __func__);
733   CHECK(p_eir_uuid != nullptr);
734 }
735 
BTM_RemoveEirService(uint32_t * p_eir_uuid,uint16_t uuid16)736 void bluetooth::shim::BTM_RemoveEirService(uint32_t* p_eir_uuid,
737                                            uint16_t uuid16) {
738   LOG_INFO("UNIMPLEMENTED %s", __func__);
739   CHECK(p_eir_uuid != nullptr);
740 }
741 
BTM_GetEirSupportedServices(uint32_t * p_eir_uuid,uint8_t ** p,uint8_t max_num_uuid16,uint8_t * p_num_uuid16)742 uint8_t bluetooth::shim::BTM_GetEirSupportedServices(uint32_t* p_eir_uuid,
743                                                      uint8_t** p,
744                                                      uint8_t max_num_uuid16,
745                                                      uint8_t* p_num_uuid16) {
746   LOG_INFO("UNIMPLEMENTED %s", __func__);
747   CHECK(p_eir_uuid != nullptr);
748   CHECK(p != nullptr);
749   CHECK(*p != nullptr);
750   CHECK(p_num_uuid16 != nullptr);
751   return BTM_NO_RESOURCES;
752 }
753 
BTM_GetEirUuidList(uint8_t * p_eir,size_t eir_len,uint8_t uuid_size,uint8_t * p_num_uuid,uint8_t * p_uuid_list,uint8_t max_num_uuid)754 uint8_t bluetooth::shim::BTM_GetEirUuidList(uint8_t* p_eir, size_t eir_len,
755                                             uint8_t uuid_size,
756                                             uint8_t* p_num_uuid,
757                                             uint8_t* p_uuid_list,
758                                             uint8_t max_num_uuid) {
759   LOG_INFO("UNIMPLEMENTED %s", __func__);
760   CHECK(p_eir != nullptr);
761   CHECK(p_num_uuid != nullptr);
762   CHECK(p_uuid_list != nullptr);
763   return 0;
764 }
765 
BTM_SecAddBleDevice(const RawAddress & bd_addr,BD_NAME bd_name,tBT_DEVICE_TYPE dev_type,tBLE_ADDR_TYPE addr_type)766 bool bluetooth::shim::BTM_SecAddBleDevice(const RawAddress& bd_addr,
767                                           BD_NAME bd_name,
768                                           tBT_DEVICE_TYPE dev_type,
769                                           tBLE_ADDR_TYPE addr_type) {
770   LOG_INFO("UNIMPLEMENTED %s", __func__);
771   return false;
772 }
773 
BTM_SecAddBleKey(const RawAddress & bd_addr,tBTM_LE_KEY_VALUE * p_le_key,tBTM_LE_KEY_TYPE key_type)774 bool bluetooth::shim::BTM_SecAddBleKey(const RawAddress& bd_addr,
775                                        tBTM_LE_KEY_VALUE* p_le_key,
776                                        tBTM_LE_KEY_TYPE key_type) {
777   LOG_INFO("UNIMPLEMENTED %s", __func__);
778   CHECK(p_le_key != nullptr);
779   return false;
780 }
781 
BTM_BleLoadLocalKeys(uint8_t key_type,tBTM_BLE_LOCAL_KEYS * p_key)782 void bluetooth::shim::BTM_BleLoadLocalKeys(uint8_t key_type,
783                                            tBTM_BLE_LOCAL_KEYS* p_key) {
784   LOG_INFO("UNIMPLEMENTED %s", __func__);
785   CHECK(p_key != nullptr);
786 }
787 
788 static Octet16 bogus_root;
789 
790 /** Returns local device encryption root (ER) */
BTM_GetDeviceEncRoot()791 const Octet16& bluetooth::shim::BTM_GetDeviceEncRoot() {
792   LOG_INFO("UNIMPLEMENTED %s", __func__);
793   return bogus_root;
794 }
795 
796 /** Returns local device identity root (IR). */
BTM_GetDeviceIDRoot()797 const Octet16& bluetooth::shim::BTM_GetDeviceIDRoot() {
798   LOG_INFO("UNIMPLEMENTED %s", __func__);
799   return bogus_root;
800 }
801 
802 /** Return local device DHK. */
BTM_GetDeviceDHK()803 const Octet16& bluetooth::shim::BTM_GetDeviceDHK() {
804   LOG_INFO("UNIMPLEMENTED %s", __func__);
805   return bogus_root;
806 }
807 
BTM_ReadConnectionAddr(const RawAddress & remote_bda,RawAddress & local_conn_addr,tBLE_ADDR_TYPE * p_addr_type)808 void bluetooth::shim::BTM_ReadConnectionAddr(const RawAddress& remote_bda,
809                                              RawAddress& local_conn_addr,
810                                              tBLE_ADDR_TYPE* p_addr_type) {
811   LOG_INFO("UNIMPLEMENTED %s", __func__);
812   CHECK(p_addr_type != nullptr);
813 }
814 
BTM_ReadRemoteConnectionAddr(const RawAddress & pseudo_addr,RawAddress & conn_addr,tBLE_ADDR_TYPE * p_addr_type)815 bool bluetooth::shim::BTM_ReadRemoteConnectionAddr(
816     const RawAddress& pseudo_addr, RawAddress& conn_addr,
817     tBLE_ADDR_TYPE* p_addr_type) {
818   LOG_INFO("UNIMPLEMENTED %s", __func__);
819   CHECK(p_addr_type != nullptr);
820   return false;
821 }
822 
BTM_SecurityGrant(const RawAddress & bd_addr,uint8_t res)823 void bluetooth::shim::BTM_SecurityGrant(const RawAddress& bd_addr,
824                                         uint8_t res) {
825   LOG_INFO("UNIMPLEMENTED %s", __func__);
826 }
827 
BTM_BleOobDataReply(const RawAddress & bd_addr,uint8_t res,uint8_t len,uint8_t * p_data)828 void bluetooth::shim::BTM_BleOobDataReply(const RawAddress& bd_addr,
829                                           uint8_t res, uint8_t len,
830                                           uint8_t* p_data) {
831   LOG_INFO("UNIMPLEMENTED %s", __func__);
832   CHECK(p_data != nullptr);
833 }
834 
BTM_BleSecureConnectionOobDataReply(const RawAddress & bd_addr,uint8_t * p_c,uint8_t * p_r)835 void bluetooth::shim::BTM_BleSecureConnectionOobDataReply(
836     const RawAddress& bd_addr, uint8_t* p_c, uint8_t* p_r) {
837   LOG_INFO("UNIMPLEMENTED %s", __func__);
838   CHECK(p_c != nullptr);
839   CHECK(p_r != nullptr);
840 }
841 
BTM_BleSetConnScanParams(uint32_t scan_interval,uint32_t scan_window)842 void bluetooth::shim::BTM_BleSetConnScanParams(uint32_t scan_interval,
843                                                uint32_t scan_window) {
844   LOG_INFO("UNIMPLEMENTED %s", __func__);
845 }
846 
BTM_BleSetPrefConnParams(const RawAddress & bd_addr,uint16_t min_conn_int,uint16_t max_conn_int,uint16_t slave_latency,uint16_t supervision_tout)847 void bluetooth::shim::BTM_BleSetPrefConnParams(const RawAddress& bd_addr,
848                                                uint16_t min_conn_int,
849                                                uint16_t max_conn_int,
850                                                uint16_t slave_latency,
851                                                uint16_t supervision_tout) {
852   LOG_INFO("UNIMPLEMENTED %s", __func__);
853 }
854 
BTM_ReadDevInfo(const RawAddress & remote_bda,tBT_DEVICE_TYPE * p_dev_type,tBLE_ADDR_TYPE * p_addr_type)855 void bluetooth::shim::BTM_ReadDevInfo(const RawAddress& remote_bda,
856                                       tBT_DEVICE_TYPE* p_dev_type,
857                                       tBLE_ADDR_TYPE* p_addr_type) {
858   LOG_INFO("UNIMPLEMENTED %s", __func__);
859   CHECK(p_dev_type != nullptr);
860   CHECK(p_addr_type != nullptr);
861 }
862 
BTM_ReadConnectedTransportAddress(RawAddress * remote_bda,tBT_TRANSPORT transport)863 bool bluetooth::shim::BTM_ReadConnectedTransportAddress(
864     RawAddress* remote_bda, tBT_TRANSPORT transport) {
865   LOG_INFO("UNIMPLEMENTED %s", __func__);
866   CHECK(remote_bda != nullptr);
867   return false;
868 }
869 
BTM_BleReceiverTest(uint8_t rx_freq,tBTM_CMPL_CB * p_cmd_cmpl_cback)870 void bluetooth::shim::BTM_BleReceiverTest(uint8_t rx_freq,
871                                           tBTM_CMPL_CB* p_cmd_cmpl_cback) {
872   LOG_INFO("UNIMPLEMENTED %s", __func__);
873   CHECK(p_cmd_cmpl_cback != nullptr);
874 }
875 
BTM_BleTransmitterTest(uint8_t tx_freq,uint8_t test_data_len,uint8_t packet_payload,tBTM_CMPL_CB * p_cmd_cmpl_cback)876 void bluetooth::shim::BTM_BleTransmitterTest(uint8_t tx_freq,
877                                              uint8_t test_data_len,
878                                              uint8_t packet_payload,
879                                              tBTM_CMPL_CB* p_cmd_cmpl_cback) {
880   LOG_INFO("UNIMPLEMENTED %s", __func__);
881   CHECK(p_cmd_cmpl_cback != nullptr);
882 }
883 
BTM_BleTestEnd(tBTM_CMPL_CB * p_cmd_cmpl_cback)884 void bluetooth::shim::BTM_BleTestEnd(tBTM_CMPL_CB* p_cmd_cmpl_cback) {
885   LOG_INFO("UNIMPLEMENTED %s", __func__);
886   CHECK(p_cmd_cmpl_cback != nullptr);
887 }
888 
BTM_UseLeLink(const RawAddress & raw_address)889 bool bluetooth::shim::BTM_UseLeLink(const RawAddress& raw_address) {
890   return Stack::GetInstance()->GetBtm()->UseLeLink(raw_address);
891 }
892 
BTM_SetBleDataLength(const RawAddress & bd_addr,uint16_t tx_pdu_length)893 tBTM_STATUS bluetooth::shim::BTM_SetBleDataLength(const RawAddress& bd_addr,
894                                                   uint16_t tx_pdu_length) {
895   LOG_INFO("UNIMPLEMENTED %s", __func__);
896   return BTM_NO_RESOURCES;
897 }
898 
BTM_BleReadPhy(const RawAddress & bd_addr,base::Callback<void (uint8_t tx_phy,uint8_t rx_phy,uint8_t status)> cb)899 void bluetooth::shim::BTM_BleReadPhy(
900     const RawAddress& bd_addr,
901     base::Callback<void(uint8_t tx_phy, uint8_t rx_phy, uint8_t status)> cb) {
902   LOG_INFO("UNIMPLEMENTED %s", __func__);
903 }
904 
BTM_BleSetPhy(const RawAddress & bd_addr,uint8_t tx_phys,uint8_t rx_phys,uint16_t phy_options)905 void bluetooth::shim::BTM_BleSetPhy(const RawAddress& bd_addr, uint8_t tx_phys,
906                                     uint8_t rx_phys, uint16_t phy_options) {
907   LOG_INFO("UNIMPLEMENTED %s", __func__);
908 }
909 
BTM_BleDataSignature(const RawAddress & bd_addr,uint8_t * p_text,uint16_t len,BLE_SIGNATURE signature)910 bool bluetooth::shim::BTM_BleDataSignature(const RawAddress& bd_addr,
911                                            uint8_t* p_text, uint16_t len,
912                                            BLE_SIGNATURE signature) {
913   LOG_INFO("UNIMPLEMENTED %s", __func__);
914   CHECK(p_text != nullptr);
915   return false;
916 }
917 
BTM_BleVerifySignature(const RawAddress & bd_addr,uint8_t * p_orig,uint16_t len,uint32_t counter,uint8_t * p_comp)918 bool bluetooth::shim::BTM_BleVerifySignature(const RawAddress& bd_addr,
919                                              uint8_t* p_orig, uint16_t len,
920                                              uint32_t counter,
921                                              uint8_t* p_comp) {
922   LOG_INFO("UNIMPLEMENTED %s", __func__);
923   CHECK(p_orig != nullptr);
924   CHECK(p_comp != nullptr);
925   return false;
926 }
927 
BTM_GetLeSecurityState(const RawAddress & bd_addr,uint8_t * p_le_dev_sec_flags,uint8_t * p_le_key_size)928 bool bluetooth::shim::BTM_GetLeSecurityState(const RawAddress& bd_addr,
929                                              uint8_t* p_le_dev_sec_flags,
930                                              uint8_t* p_le_key_size) {
931   LOG_INFO("UNIMPLEMENTED %s", __func__);
932   CHECK(p_le_dev_sec_flags != nullptr);
933   CHECK(p_le_key_size != nullptr);
934   return false;
935 }
936 
BTM_BleSecurityProcedureIsRunning(const RawAddress & bd_addr)937 bool bluetooth::shim::BTM_BleSecurityProcedureIsRunning(
938     const RawAddress& bd_addr) {
939   LOG_INFO("UNIMPLEMENTED %s", __func__);
940   return false;
941 }
942 
BTM_BleGetSupportedKeySize(const RawAddress & bd_addr)943 uint8_t bluetooth::shim::BTM_BleGetSupportedKeySize(const RawAddress& bd_addr) {
944   LOG_INFO("UNIMPLEMENTED %s", __func__);
945   return 0;
946 }
947 
948 /**
949  * This function update(add,delete or clear) the adv local name filtering
950  * condition.
951  */
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)952 void bluetooth::shim::BTM_LE_PF_local_name(tBTM_BLE_SCAN_COND_OP action,
953                                            tBTM_BLE_PF_FILT_INDEX filt_index,
954                                            std::vector<uint8_t> name,
955                                            tBTM_BLE_PF_CFG_CBACK cb) {
956   LOG_INFO("UNIMPLEMENTED %s", __func__);
957 }
958 
BTM_LE_PF_srvc_data(tBTM_BLE_SCAN_COND_OP action,tBTM_BLE_PF_FILT_INDEX filt_index)959 void bluetooth::shim::BTM_LE_PF_srvc_data(tBTM_BLE_SCAN_COND_OP action,
960                                           tBTM_BLE_PF_FILT_INDEX filt_index) {
961   LOG_INFO("UNIMPLEMENTED %s", __func__);
962 }
963 
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)964 void bluetooth::shim::BTM_LE_PF_manu_data(
965     tBTM_BLE_SCAN_COND_OP action, tBTM_BLE_PF_FILT_INDEX filt_index,
966     uint16_t company_id, uint16_t company_id_mask, std::vector<uint8_t> data,
967     std::vector<uint8_t> data_mask, tBTM_BLE_PF_CFG_CBACK cb) {
968   LOG_INFO("UNIMPLEMENTED %s", __func__);
969 }
970 
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)971 void bluetooth::shim::BTM_LE_PF_srvc_data_pattern(
972     tBTM_BLE_SCAN_COND_OP action, tBTM_BLE_PF_FILT_INDEX filt_index,
973     std::vector<uint8_t> data, std::vector<uint8_t> data_mask,
974     tBTM_BLE_PF_CFG_CBACK cb) {
975   LOG_INFO("UNIMPLEMENTED %s", __func__);
976 }
977 
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)978 void bluetooth::shim::BTM_LE_PF_addr_filter(tBTM_BLE_SCAN_COND_OP action,
979                                             tBTM_BLE_PF_FILT_INDEX filt_index,
980                                             tBLE_BD_ADDR addr,
981                                             tBTM_BLE_PF_CFG_CBACK cb) {
982   LOG_INFO("UNIMPLEMENTED %s", __func__);
983 }
984 
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)985 void bluetooth::shim::BTM_LE_PF_uuid_filter(tBTM_BLE_SCAN_COND_OP action,
986                                             tBTM_BLE_PF_FILT_INDEX filt_index,
987                                             tBTM_BLE_PF_COND_TYPE filter_type,
988                                             const bluetooth::Uuid& uuid,
989                                             tBTM_BLE_PF_LOGIC_TYPE cond_logic,
990                                             const bluetooth::Uuid& uuid_mask,
991                                             tBTM_BLE_PF_CFG_CBACK cb) {
992   LOG_INFO("UNIMPLEMENTED %s", __func__);
993 }
994 
BTM_LE_PF_set(tBTM_BLE_PF_FILT_INDEX filt_index,std::vector<ApcfCommand> commands,tBTM_BLE_PF_CFG_CBACK cb)995 void bluetooth::shim::BTM_LE_PF_set(tBTM_BLE_PF_FILT_INDEX filt_index,
996                                     std::vector<ApcfCommand> commands,
997                                     tBTM_BLE_PF_CFG_CBACK cb) {
998   LOG_INFO("UNIMPLEMENTED %s", __func__);
999 }
1000 
BTM_LE_PF_clear(tBTM_BLE_PF_FILT_INDEX filt_index,tBTM_BLE_PF_CFG_CBACK cb)1001 void bluetooth::shim::BTM_LE_PF_clear(tBTM_BLE_PF_FILT_INDEX filt_index,
1002                                       tBTM_BLE_PF_CFG_CBACK cb) {
1003   LOG_INFO("UNIMPLEMENTED %s", __func__);
1004 }
1005 
BTM_BleAdvFilterParamSetup(int 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)1006 void bluetooth::shim::BTM_BleAdvFilterParamSetup(
1007     int action, tBTM_BLE_PF_FILT_INDEX filt_index,
1008     std::unique_ptr<btgatt_filt_param_setup_t> p_filt_params,
1009     tBTM_BLE_PF_PARAM_CB cb) {
1010   LOG_INFO("UNIMPLEMENTED %s", __func__);
1011 }
1012 
BTM_BleUpdateAdvFilterPolicy(tBTM_BLE_AFP adv_policy)1013 void bluetooth::shim::BTM_BleUpdateAdvFilterPolicy(tBTM_BLE_AFP adv_policy) {
1014   LOG_INFO("UNIMPLEMENTED %s", __func__);
1015 }
1016 
BTM_BleEnableDisableFilterFeature(uint8_t enable,tBTM_BLE_PF_STATUS_CBACK p_stat_cback)1017 void bluetooth::shim::BTM_BleEnableDisableFilterFeature(
1018     uint8_t enable, tBTM_BLE_PF_STATUS_CBACK p_stat_cback) {
1019   LOG_INFO("UNIMPLEMENTED %s", __func__);
1020 }
1021 
BTM_BleMaxMultiAdvInstanceCount()1022 uint8_t bluetooth::shim::BTM_BleMaxMultiAdvInstanceCount() {
1023   return Stack::GetInstance()->GetBtm()->GetNumberOfAdvertisingInstances();
1024 }
1025 
BTM_BleLocalPrivacyEnabled(void)1026 bool bluetooth::shim::BTM_BleLocalPrivacyEnabled(void) {
1027   return controller_get_interface()->supports_ble_privacy();
1028 }
1029 
BTM_SecBond(const RawAddress & bd_addr,tBLE_ADDR_TYPE addr_type,tBT_TRANSPORT transport,int device_type)1030 tBTM_STATUS bluetooth::shim::BTM_SecBond(const RawAddress& bd_addr,
1031                                          tBLE_ADDR_TYPE addr_type,
1032                                          tBT_TRANSPORT transport,
1033                                          int device_type) {
1034   return Stack::GetInstance()->GetBtm()->CreateBond(bd_addr, addr_type,
1035                                                     transport, device_type);
1036 }
1037 
BTM_SecRegister(const tBTM_APPL_INFO * p_cb_info)1038 bool bluetooth::shim::BTM_SecRegister(const tBTM_APPL_INFO* p_cb_info) {
1039   CHECK(p_cb_info != nullptr);
1040   LOG_DEBUG("%s Registering security application", __func__);
1041 
1042   if (p_cb_info->p_authorize_callback == nullptr) {
1043     LOG_INFO("UNIMPLEMENTED %s authorize_callback", __func__);
1044   }
1045 
1046   if (p_cb_info->p_pin_callback == nullptr) {
1047     LOG_INFO("UNIMPLEMENTED %s pin_callback", __func__);
1048   }
1049 
1050   if (p_cb_info->p_link_key_callback == nullptr) {
1051     LOG_INFO("UNIMPLEMENTED %s link_key_callback", __func__);
1052   }
1053 
1054   if (p_cb_info->p_auth_complete_callback == nullptr) {
1055     LOG_INFO("UNIMPLEMENTED %s auth_complete_callback", __func__);
1056   }
1057 
1058   if (p_cb_info->p_bond_cancel_cmpl_callback == nullptr) {
1059     LOG_INFO("UNIMPLEMENTED %s bond_cancel_complete_callback", __func__);
1060   }
1061 
1062   if (p_cb_info->p_le_callback == nullptr) {
1063     LOG_INFO("UNIMPLEMENTED %s le_callback", __func__);
1064   }
1065 
1066   if (p_cb_info->p_le_key_callback == nullptr) {
1067     LOG_INFO("UNIMPLEMENTED %s le_key_callback", __func__);
1068   }
1069 
1070   return true;
1071 }
1072 
BTM_SecBondCancel(const RawAddress & bd_addr)1073 tBTM_STATUS bluetooth::shim::BTM_SecBondCancel(const RawAddress& bd_addr) {
1074   if (Stack::GetInstance()->GetBtm()->CancelBond(bd_addr)) {
1075     return BTM_SUCCESS;
1076   } else {
1077     return BTM_UNKNOWN_ADDR;
1078   }
1079 }
1080 
BTM_SecDeleteDevice(const RawAddress & bd_addr)1081 bool bluetooth::shim::BTM_SecDeleteDevice(const RawAddress& bd_addr) {
1082   return Stack::GetInstance()->GetBtm()->RemoveBond(bd_addr);
1083 }
1084 
BTM_GetHCIConnHandle(const RawAddress & remote_bda,tBT_TRANSPORT transport)1085 uint16_t bluetooth::shim::BTM_GetHCIConnHandle(const RawAddress& remote_bda,
1086                                                tBT_TRANSPORT transport) {
1087   return Stack::GetInstance()->GetBtm()->GetAclHandle(remote_bda, transport);
1088 }
1089 
remote_name_request_complete_noop(void * p_name)1090 static void remote_name_request_complete_noop(void* p_name){
1091     // Should notify BTM_Sec, but we should use GD SMP.
1092 };
1093 
SendRemoteNameRequest(const RawAddress & raw_address)1094 void bluetooth::shim::SendRemoteNameRequest(const RawAddress& raw_address) {
1095   Stack::GetInstance()->GetBtm()->ReadClassicRemoteDeviceName(
1096       raw_address, remote_name_request_complete_noop);
1097 }
1098 
btm_sec_mx_access_request(const RawAddress & bd_addr,uint16_t psm,bool is_originator,uint32_t mx_proto_id,uint32_t mx_chan_id,tBTM_SEC_CBACK * p_callback,void * p_ref_data)1099 tBTM_STATUS bluetooth::shim::btm_sec_mx_access_request(
1100     const RawAddress& bd_addr, uint16_t psm, bool is_originator,
1101     uint32_t mx_proto_id, uint32_t mx_chan_id, tBTM_SEC_CBACK* p_callback,
1102     void* p_ref_data) {
1103   // Security has already been fulfilled by the l2cap connection, so reply back
1104   // that everything is totally fine and legit and definitely not two kids in a
1105   // trenchcoat
1106 
1107   if (p_callback) {
1108     (*p_callback)(&bd_addr, false, p_ref_data, BTM_SUCCESS);
1109   }
1110   return BTM_SUCCESS;
1111 }
1112 
BTM_SetEncryption(const RawAddress & bd_addr,tBT_TRANSPORT transport,tBTM_SEC_CBACK * p_callback,void * p_ref_data,tBTM_BLE_SEC_ACT sec_act)1113 tBTM_STATUS bluetooth::shim::BTM_SetEncryption(const RawAddress& bd_addr,
1114                                                tBT_TRANSPORT transport,
1115                                                tBTM_SEC_CBACK* p_callback,
1116                                                void* p_ref_data,
1117                                                tBTM_BLE_SEC_ACT sec_act) {
1118   // When we just bond a device, encryption is already done
1119   (*p_callback)(&bd_addr, transport, p_ref_data, BTM_SUCCESS);
1120 
1121   // TODO(hsz): Re-encrypt the link after first bonded
1122 
1123   return BTM_SUCCESS;
1124 }