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 <algorithm>
20 #include <chrono>
21 #include <cstddef>
22 #include <cstdint>
23 #include <cstring>
24 #include <mutex>
25
26 #include "bta/include/bta_api.h"
27 #include "main/shim/btm.h"
28 #include "main/shim/controller.h"
29 #include "main/shim/entry.h"
30 #include "main/shim/helpers.h"
31 #include "main/shim/shim.h"
32 #include "stack/btm/btm_dev.h"
33 #include "stack/btm/btm_int_types.h"
34 #include "types/bt_transport.h"
35 #include "types/raw_address.h"
36
37 #include "gd/hci/le_advertising_manager.h"
38 #include "gd/hci/le_scanning_manager.h"
39 #include "gd/neighbor/connectability.h"
40 #include "gd/neighbor/discoverability.h"
41 #include "gd/neighbor/inquiry.h"
42 #include "gd/neighbor/name.h"
43 #include "gd/neighbor/page.h"
44 #include "gd/security/security_module.h"
45
46 extern tBTM_CB btm_cb;
47
48 static constexpr size_t kRemoteDeviceNameLength = 248;
49
50 static constexpr bool kActiveScanning = true;
51 static constexpr bool kPassiveScanning = false;
52
53 using BtmRemoteDeviceName = tBTM_REMOTE_DEV_NAME;
54
55 extern void btm_process_cancel_complete(uint8_t status, uint8_t mode);
56 extern void btm_process_inq_complete(uint8_t status, uint8_t result_type);
57 extern void btm_ble_process_adv_addr(RawAddress& raw_address,
58 tBLE_ADDR_TYPE* address_type);
59 extern void btm_ble_process_adv_pkt_cont(
60 uint16_t event_type, uint8_t address_type, const RawAddress& raw_address,
61 uint8_t primary_phy, uint8_t secondary_phy, uint8_t advertising_sid,
62 int8_t tx_power, int8_t rssi, uint16_t periodic_adv_int, uint8_t data_len,
63 uint8_t* data);
64
65 extern void btm_api_process_inquiry_result(const RawAddress& raw_address,
66 uint8_t page_scan_rep_mode,
67 DEV_CLASS device_class,
68 uint16_t clock_offset);
69
70 extern void btm_api_process_inquiry_result_with_rssi(RawAddress raw_address,
71 uint8_t page_scan_rep_mode,
72 DEV_CLASS device_class,
73 uint16_t clock_offset,
74 int8_t rssi);
75
76 extern void btm_api_process_extended_inquiry_result(
77 RawAddress raw_address, uint8_t page_scan_rep_mode, DEV_CLASS device_class,
78 uint16_t clock_offset, int8_t rssi, const uint8_t* eir_data,
79 size_t eir_len);
80
81 namespace bluetooth {
82
83 namespace shim {
84
Start(RawAddress raw_address)85 bool Btm::ReadRemoteName::Start(RawAddress raw_address) {
86 std::unique_lock<std::mutex> lock(mutex_);
87 if (in_progress_) {
88 return false;
89 }
90 raw_address_ = raw_address;
91 in_progress_ = true;
92 return true;
93 }
94
Stop()95 void Btm::ReadRemoteName::Stop() {
96 std::unique_lock<std::mutex> lock(mutex_);
97 raw_address_ = RawAddress::kEmpty;
98 in_progress_ = false;
99 }
100
IsInProgress() const101 bool Btm::ReadRemoteName::IsInProgress() const { return in_progress_; }
AddressString() const102 std::string Btm::ReadRemoteName::AddressString() const {
103 return raw_address_.ToString();
104 }
105
OnScannerRegistered(const bluetooth::hci::Uuid app_uuid,bluetooth::hci::ScannerId scanner_id,ScanningStatus status)106 void Btm::ScanningCallbacks::OnScannerRegistered(
107 const bluetooth::hci::Uuid app_uuid, bluetooth::hci::ScannerId scanner_id,
108 ScanningStatus status){};
109
OnScanResult(uint16_t event_type,uint8_t address_type,bluetooth::hci::Address address,uint8_t primary_phy,uint8_t secondary_phy,uint8_t advertising_sid,int8_t tx_power,int8_t rssi,uint16_t periodic_advertising_interval,std::vector<uint8_t> advertising_data)110 void Btm::ScanningCallbacks::OnScanResult(
111 uint16_t event_type, uint8_t address_type, bluetooth::hci::Address address,
112 uint8_t primary_phy, uint8_t secondary_phy, uint8_t advertising_sid,
113 int8_t tx_power, int8_t rssi, uint16_t periodic_advertising_interval,
114 std::vector<uint8_t> advertising_data) {
115 tBLE_ADDR_TYPE ble_address_type = static_cast<tBLE_ADDR_TYPE>(address_type);
116 uint16_t extended_event_type = 0;
117
118 RawAddress raw_address;
119 RawAddress::FromString(address.ToString(), raw_address);
120
121 if (ble_address_type != BLE_ADDR_ANONYMOUS) {
122 btm_ble_process_adv_addr(raw_address, &ble_address_type);
123 }
124
125 btm_ble_process_adv_addr(raw_address, &ble_address_type);
126 btm_ble_process_adv_pkt_cont(extended_event_type, ble_address_type,
127 raw_address, primary_phy, secondary_phy,
128 advertising_sid, tx_power, rssi,
129 periodic_advertising_interval,
130 advertising_data.size(), &advertising_data[0]);
131 }
132
OnTrackAdvFoundLost(bluetooth::hci::AdvertisingFilterOnFoundOnLostInfo on_found_on_lost_info)133 void Btm::ScanningCallbacks::OnTrackAdvFoundLost(
134 bluetooth::hci::AdvertisingFilterOnFoundOnLostInfo on_found_on_lost_info){};
OnBatchScanReports(int client_if,int status,int report_format,int num_records,std::vector<uint8_t> data)135 void Btm::ScanningCallbacks::OnBatchScanReports(int client_if, int status,
136 int report_format,
137 int num_records,
138 std::vector<uint8_t> data){};
139
OnBatchScanThresholdCrossed(int client_if)140 void Btm::ScanningCallbacks::OnBatchScanThresholdCrossed(int client_if){};
OnTimeout()141 void Btm::ScanningCallbacks::OnTimeout(){};
OnFilterEnable(bluetooth::hci::Enable enable,uint8_t status)142 void Btm::ScanningCallbacks::OnFilterEnable(bluetooth::hci::Enable enable,
143 uint8_t status){};
OnFilterParamSetup(uint8_t available_spaces,bluetooth::hci::ApcfAction action,uint8_t status)144 void Btm::ScanningCallbacks::OnFilterParamSetup(
145 uint8_t available_spaces, bluetooth::hci::ApcfAction action,
146 uint8_t status){};
OnFilterConfigCallback(bluetooth::hci::ApcfFilterType filter_type,uint8_t available_spaces,bluetooth::hci::ApcfAction action,uint8_t status)147 void Btm::ScanningCallbacks::OnFilterConfigCallback(
148 bluetooth::hci::ApcfFilterType filter_type, uint8_t available_spaces,
149 bluetooth::hci::ApcfAction action, uint8_t status){};
150
Btm(os::Handler * handler,neighbor::InquiryModule * inquiry)151 Btm::Btm(os::Handler* handler, neighbor::InquiryModule* inquiry)
152 : scanning_timer_(handler), observing_timer_(handler) {
153 ASSERT(handler != nullptr);
154 ASSERT(inquiry != nullptr);
155 bluetooth::neighbor::InquiryCallbacks inquiry_callbacks = {
156 .result = std::bind(&Btm::OnInquiryResult, this, std::placeholders::_1),
157 .result_with_rssi =
158 std::bind(&Btm::OnInquiryResultWithRssi, this, std::placeholders::_1),
159 .extended_result =
160 std::bind(&Btm::OnExtendedInquiryResult, this, std::placeholders::_1),
161 .complete =
162 std::bind(&Btm::OnInquiryComplete, this, std::placeholders::_1)};
163 inquiry->RegisterCallbacks(std::move(inquiry_callbacks));
164 }
165
OnInquiryResult(bluetooth::hci::InquiryResultView view)166 void Btm::OnInquiryResult(bluetooth::hci::InquiryResultView view) {
167 for (auto& response : view.GetInquiryResults()) {
168 btm_api_process_inquiry_result(
169 ToRawAddress(response.bd_addr_),
170 static_cast<uint8_t>(response.page_scan_repetition_mode_),
171 response.class_of_device_.data(), response.clock_offset_);
172 }
173 }
174
OnInquiryResultWithRssi(bluetooth::hci::InquiryResultWithRssiView view)175 void Btm::OnInquiryResultWithRssi(
176 bluetooth::hci::InquiryResultWithRssiView view) {
177 for (auto& response : view.GetInquiryResults()) {
178 btm_api_process_inquiry_result_with_rssi(
179 ToRawAddress(response.address_),
180 static_cast<uint8_t>(response.page_scan_repetition_mode_),
181 response.class_of_device_.data(), response.clock_offset_,
182 response.rssi_);
183 }
184 }
185
OnExtendedInquiryResult(bluetooth::hci::ExtendedInquiryResultView view)186 void Btm::OnExtendedInquiryResult(
187 bluetooth::hci::ExtendedInquiryResultView view) {
188 constexpr size_t kMaxExtendedInquiryResponse = 240;
189 uint8_t gap_data_buffer[kMaxExtendedInquiryResponse];
190 uint8_t* data = nullptr;
191 size_t data_len = 0;
192
193 if (!view.GetExtendedInquiryResponse().empty()) {
194 bzero(gap_data_buffer, sizeof(gap_data_buffer));
195 uint8_t* p = gap_data_buffer;
196 for (auto gap_data : view.GetExtendedInquiryResponse()) {
197 *p++ = gap_data.data_.size() + sizeof(gap_data.data_type_);
198 *p++ = static_cast<uint8_t>(gap_data.data_type_);
199 p = (uint8_t*)memcpy(p, &gap_data.data_[0], gap_data.data_.size()) +
200 gap_data.data_.size();
201 }
202 data = gap_data_buffer;
203 data_len = p - data;
204 }
205
206 btm_api_process_extended_inquiry_result(
207 ToRawAddress(view.GetAddress()),
208 static_cast<uint8_t>(view.GetPageScanRepetitionMode()),
209 view.GetClassOfDevice().data(), view.GetClockOffset(), view.GetRssi(),
210 data, data_len);
211 }
212
OnInquiryComplete(bluetooth::hci::ErrorCode status)213 void Btm::OnInquiryComplete(bluetooth::hci::ErrorCode status) {
214 limited_inquiry_active_ = false;
215 general_inquiry_active_ = false;
216 legacy_inquiry_complete_callback_((static_cast<uint16_t>(status) == 0)
217 ? (BTM_SUCCESS)
218 : (BTM_ERR_PROCESSING),
219 active_inquiry_mode_);
220
221 active_inquiry_mode_ = kInquiryModeOff;
222 }
223
SetStandardInquiryResultMode()224 void Btm::SetStandardInquiryResultMode() {
225 GetInquiry()->SetStandardInquiryResultMode();
226 }
227
SetInquiryWithRssiResultMode()228 void Btm::SetInquiryWithRssiResultMode() {
229 GetInquiry()->SetInquiryWithRssiResultMode();
230 }
231
SetExtendedInquiryResultMode()232 void Btm::SetExtendedInquiryResultMode() {
233 GetInquiry()->SetExtendedInquiryResultMode();
234 }
235
SetInterlacedInquiryScan()236 void Btm::SetInterlacedInquiryScan() { GetInquiry()->SetInterlacedScan(); }
237
SetStandardInquiryScan()238 void Btm::SetStandardInquiryScan() { GetInquiry()->SetStandardScan(); }
239
IsInterlacedScanSupported() const240 bool Btm::IsInterlacedScanSupported() const {
241 return controller_get_interface()->supports_interlaced_inquiry_scan();
242 }
243
244 /**
245 * One shot inquiry
246 */
StartInquiry(uint8_t mode,uint8_t duration,uint8_t max_responses,LegacyInquiryCompleteCallback legacy_inquiry_complete_callback)247 bool Btm::StartInquiry(
248 uint8_t mode, uint8_t duration, uint8_t max_responses,
249 LegacyInquiryCompleteCallback legacy_inquiry_complete_callback) {
250 switch (mode) {
251 case kInquiryModeOff:
252 LOG_INFO("%s Stopping inquiry mode", __func__);
253 if (limited_inquiry_active_ || general_inquiry_active_) {
254 GetInquiry()->StopInquiry();
255 limited_inquiry_active_ = false;
256 general_inquiry_active_ = false;
257 }
258 active_inquiry_mode_ = kInquiryModeOff;
259 break;
260
261 case kLimitedInquiryMode:
262 case kGeneralInquiryMode: {
263 if (mode == kLimitedInquiryMode) {
264 LOG_INFO(
265
266 "%s Starting limited inquiry mode duration:%hhd max responses:%hhd",
267 __func__, duration, max_responses);
268 limited_inquiry_active_ = true;
269 GetInquiry()->StartLimitedInquiry(duration, max_responses);
270 active_inquiry_mode_ = kLimitedInquiryMode;
271 } else {
272 LOG_INFO(
273
274 "%s Starting general inquiry mode duration:%hhd max responses:%hhd",
275 __func__, duration, max_responses);
276 general_inquiry_active_ = true;
277 GetInquiry()->StartGeneralInquiry(duration, max_responses);
278 legacy_inquiry_complete_callback_ = legacy_inquiry_complete_callback;
279 }
280 } break;
281
282 default:
283 LOG_WARN("%s Unknown inquiry mode:%d", __func__, mode);
284 return false;
285 }
286 return true;
287 }
288
CancelInquiry()289 void Btm::CancelInquiry() {
290 LOG_INFO("%s", __func__);
291 if (limited_inquiry_active_ || general_inquiry_active_) {
292 GetInquiry()->StopInquiry();
293 limited_inquiry_active_ = false;
294 general_inquiry_active_ = false;
295 }
296 }
297
IsInquiryActive() const298 bool Btm::IsInquiryActive() const {
299 return IsGeneralInquiryActive() || IsLimitedInquiryActive();
300 }
301
IsGeneralInquiryActive() const302 bool Btm::IsGeneralInquiryActive() const { return general_inquiry_active_; }
303
IsLimitedInquiryActive() const304 bool Btm::IsLimitedInquiryActive() const { return limited_inquiry_active_; }
305
306 /**
307 * Periodic
308 */
StartPeriodicInquiry(uint8_t mode,uint8_t duration,uint8_t max_responses,uint16_t max_delay,uint16_t min_delay,tBTM_INQ_RESULTS_CB * p_results_cb)309 bool Btm::StartPeriodicInquiry(uint8_t mode, uint8_t duration,
310 uint8_t max_responses, uint16_t max_delay,
311 uint16_t min_delay,
312 tBTM_INQ_RESULTS_CB* p_results_cb) {
313 switch (mode) {
314 case kInquiryModeOff:
315 limited_periodic_inquiry_active_ = false;
316 general_periodic_inquiry_active_ = false;
317 GetInquiry()->StopPeriodicInquiry();
318 break;
319
320 case kLimitedInquiryMode:
321 case kGeneralInquiryMode: {
322 if (mode == kLimitedInquiryMode) {
323 LOG_INFO("%s Starting limited periodic inquiry mode", __func__);
324 limited_periodic_inquiry_active_ = true;
325 GetInquiry()->StartLimitedPeriodicInquiry(duration, max_responses,
326 max_delay, min_delay);
327 } else {
328 LOG_INFO("%s Starting general periodic inquiry mode", __func__);
329 general_periodic_inquiry_active_ = true;
330 GetInquiry()->StartGeneralPeriodicInquiry(duration, max_responses,
331 max_delay, min_delay);
332 }
333 } break;
334
335 default:
336 LOG_WARN("%s Unknown inquiry mode:%d", __func__, mode);
337 return false;
338 }
339 return true;
340 }
341
IsGeneralPeriodicInquiryActive() const342 bool Btm::IsGeneralPeriodicInquiryActive() const {
343 return general_periodic_inquiry_active_;
344 }
345
IsLimitedPeriodicInquiryActive() const346 bool Btm::IsLimitedPeriodicInquiryActive() const {
347 return limited_periodic_inquiry_active_;
348 }
349
350 /**
351 * Discoverability
352 */
353
354 bluetooth::neighbor::ScanParameters params_{
355 .interval = 0,
356 .window = 0,
357 };
358
SetClassicGeneralDiscoverability(uint16_t window,uint16_t interval)359 void Btm::SetClassicGeneralDiscoverability(uint16_t window, uint16_t interval) {
360 params_.window = window;
361 params_.interval = interval;
362
363 GetInquiry()->SetScanActivity(params_);
364 GetDiscoverability()->StartGeneralDiscoverability();
365 }
366
SetClassicLimitedDiscoverability(uint16_t window,uint16_t interval)367 void Btm::SetClassicLimitedDiscoverability(uint16_t window, uint16_t interval) {
368 params_.window = window;
369 params_.interval = interval;
370 GetInquiry()->SetScanActivity(params_);
371 GetDiscoverability()->StartLimitedDiscoverability();
372 }
373
SetClassicDiscoverabilityOff()374 void Btm::SetClassicDiscoverabilityOff() {
375 GetDiscoverability()->StopDiscoverability();
376 }
377
GetClassicDiscoverabilityState() const378 DiscoverabilityState Btm::GetClassicDiscoverabilityState() const {
379 DiscoverabilityState state{.mode = BTM_NON_DISCOVERABLE,
380 .interval = params_.interval,
381 .window = params_.window};
382
383 if (GetDiscoverability()->IsGeneralDiscoverabilityEnabled()) {
384 state.mode = BTM_GENERAL_DISCOVERABLE;
385 } else if (GetDiscoverability()->IsLimitedDiscoverabilityEnabled()) {
386 state.mode = BTM_LIMITED_DISCOVERABLE;
387 }
388 return state;
389 }
390
SetLeGeneralDiscoverability()391 void Btm::SetLeGeneralDiscoverability() {
392 LOG_WARN("UNIMPLEMENTED %s", __func__);
393 }
394
SetLeLimitedDiscoverability()395 void Btm::SetLeLimitedDiscoverability() {
396 LOG_WARN("UNIMPLEMENTED %s", __func__);
397 }
398
SetLeDiscoverabilityOff()399 void Btm::SetLeDiscoverabilityOff() { LOG_WARN("UNIMPLEMENTED %s", __func__); }
400
GetLeDiscoverabilityState() const401 DiscoverabilityState Btm::GetLeDiscoverabilityState() const {
402 DiscoverabilityState state{
403 .mode = kDiscoverableModeOff,
404 .interval = 0,
405 .window = 0,
406 };
407 LOG_WARN("UNIMPLEMENTED %s", __func__);
408 return state;
409 }
410
411 /**
412 * Connectability
413 */
SetClassicConnectibleOn()414 void Btm::SetClassicConnectibleOn() {
415 GetConnectability()->StartConnectability();
416 }
417
SetClassicConnectibleOff()418 void Btm::SetClassicConnectibleOff() {
419 GetConnectability()->StopConnectability();
420 }
421
GetClassicConnectabilityState() const422 ConnectabilityState Btm::GetClassicConnectabilityState() const {
423 ConnectabilityState state{.interval = params_.interval,
424 .window = params_.window};
425
426 if (GetConnectability()->IsConnectable()) {
427 state.mode = BTM_CONNECTABLE;
428 } else {
429 state.mode = BTM_NON_CONNECTABLE;
430 }
431 return state;
432 }
433
SetInterlacedPageScan()434 void Btm::SetInterlacedPageScan() { GetPage()->SetInterlacedScan(); }
435
SetStandardPageScan()436 void Btm::SetStandardPageScan() { GetPage()->SetStandardScan(); }
437
SetLeConnectibleOn()438 void Btm::SetLeConnectibleOn() { LOG_WARN("UNIMPLEMENTED %s", __func__); }
439
SetLeConnectibleOff()440 void Btm::SetLeConnectibleOff() { LOG_WARN("UNIMPLEMENTED %s", __func__); }
441
GetLeConnectabilityState() const442 ConnectabilityState Btm::GetLeConnectabilityState() const {
443 ConnectabilityState state{
444 .mode = kConnectibleModeOff,
445 .interval = 0,
446 .window = 0,
447 };
448 LOG_WARN("UNIMPLEMENTED %s", __func__);
449 return state;
450 }
451
UseLeLink(const RawAddress & raw_address) const452 bool Btm::UseLeLink(const RawAddress& raw_address) const {
453 if (GetAclManager()->HACK_GetHandle(ToGdAddress(raw_address)) != 0xFFFF) {
454 return false;
455 }
456 if (GetAclManager()->HACK_GetLeHandle(ToGdAddress(raw_address)) != 0xFFFF) {
457 return true;
458 }
459 // TODO(hsz): use correct transport by using storage records. For now assume
460 // LE for GATT and HID.
461 return true;
462 }
463
ReadClassicRemoteDeviceName(const RawAddress & raw_address,tBTM_CMPL_CB * callback)464 BtmStatus Btm::ReadClassicRemoteDeviceName(const RawAddress& raw_address,
465 tBTM_CMPL_CB* callback) {
466 if (!CheckClassicAclLink(raw_address)) {
467 return BTM_UNKNOWN_ADDR;
468 }
469
470 if (!classic_read_remote_name_.Start(raw_address)) {
471 LOG_INFO("%s Read remote name is currently busy address:%s", __func__,
472 raw_address.ToString().c_str());
473 return BTM_BUSY;
474 }
475
476 LOG_INFO("%s Start read name from address:%s", __func__,
477 raw_address.ToString().c_str());
478 GetName()->ReadRemoteNameRequest(
479 ToGdAddress(raw_address), hci::PageScanRepetitionMode::R1,
480 0 /* clock_offset */, hci::ClockOffsetValid::INVALID,
481
482 base::Bind(
483 [](tBTM_CMPL_CB* callback, ReadRemoteName* classic_read_remote_name,
484 hci::ErrorCode status, hci::Address address,
485 std::array<uint8_t, kRemoteDeviceNameLength> remote_name) {
486 RawAddress raw_address = ToRawAddress(address);
487
488 BtmRemoteDeviceName name{
489 .status = (static_cast<uint8_t>(status) == 0)
490 ? (BTM_SUCCESS)
491 : (BTM_BAD_VALUE_RET),
492 .bd_addr = raw_address,
493 .length = kRemoteDeviceNameLength,
494 };
495 std::copy(remote_name.begin(), remote_name.end(),
496 name.remote_bd_name);
497 LOG_INFO("%s Finish read name from address:%s name:%s", __func__,
498 address.ToString().c_str(), name.remote_bd_name);
499 callback(&name);
500 classic_read_remote_name->Stop();
501 },
502 callback, &classic_read_remote_name_),
503 GetGdShimHandler());
504 return BTM_CMD_STARTED;
505 }
506
ReadLeRemoteDeviceName(const RawAddress & raw_address,tBTM_CMPL_CB * callback)507 BtmStatus Btm::ReadLeRemoteDeviceName(const RawAddress& raw_address,
508 tBTM_CMPL_CB* callback) {
509 if (!CheckLeAclLink(raw_address)) {
510 return BTM_UNKNOWN_ADDR;
511 }
512
513 if (!le_read_remote_name_.Start(raw_address)) {
514 return BTM_BUSY;
515 }
516
517 LOG_INFO("UNIMPLEMENTED %s need access to GATT module", __func__);
518 return BTM_UNKNOWN_ADDR;
519 }
520
CancelAllReadRemoteDeviceName()521 BtmStatus Btm::CancelAllReadRemoteDeviceName() {
522 if (classic_read_remote_name_.IsInProgress() ||
523 le_read_remote_name_.IsInProgress()) {
524 if (classic_read_remote_name_.IsInProgress()) {
525 hci::Address address;
526 hci::Address::FromString(classic_read_remote_name_.AddressString(),
527 address);
528
529 GetName()->CancelRemoteNameRequest(
530 address,
531 common::BindOnce(
532 [](ReadRemoteName* classic_read_remote_name,
533 hci::ErrorCode status,
534 hci::Address address) { classic_read_remote_name->Stop(); },
535 &classic_read_remote_name_),
536 GetGdShimHandler());
537 }
538 if (le_read_remote_name_.IsInProgress()) {
539 LOG_INFO("UNIMPLEMENTED %s need access to GATT module", __func__);
540 }
541 return BTM_UNKNOWN_ADDR;
542 }
543 LOG_WARN("%s Cancelling classic remote device name without one in progress",
544 __func__);
545 return BTM_WRONG_MODE;
546 }
547
StartAdvertising()548 void Btm::StartAdvertising() {
549 if (advertiser_id_ == hci::LeAdvertisingManager::kInvalidId) {
550 LOG_WARN("%s Already advertising; please stop prior to starting again",
551 __func__);
552 return;
553 }
554
555 hci::ExtendedAdvertisingConfig config = {};
556 advertiser_id_ = GetAdvertising()->ExtendedCreateAdvertiser(
557 0x00, config,
558 common::Bind([](hci::Address, hci::AddressType) { /*OnScan*/ }),
559 common::Bind([](hci::ErrorCode, uint8_t, uint8_t) { /*OnTerminated*/ }),
560 0, 0, GetGdShimHandler());
561 if (advertiser_id_ == hci::LeAdvertisingManager::kInvalidId) {
562 LOG_WARN("%s Unable to start advertising", __func__);
563 return;
564 }
565 LOG_INFO("%s Started advertising", __func__);
566 }
567
StopAdvertising()568 void Btm::StopAdvertising() {
569 if (advertiser_id_ == hci::LeAdvertisingManager::kInvalidId) {
570 LOG_WARN("%s No active advertising", __func__);
571 return;
572 }
573 GetAdvertising()->RemoveAdvertiser(advertiser_id_);
574 advertiser_id_ = hci::LeAdvertisingManager::kInvalidId;
575 LOG_INFO("%s Stopped advertising", __func__);
576 }
577
StartConnectability()578 void Btm::StartConnectability() { StartAdvertising(); }
579
StopConnectability()580 void Btm::StopConnectability() { StopAdvertising(); }
581
StartActiveScanning()582 void Btm::StartActiveScanning() { StartScanning(kActiveScanning); }
583
StopActiveScanning()584 void Btm::StopActiveScanning() { GetScanning()->Scan(false); }
585
SetScanningTimer(uint64_t duration_ms,common::OnceCallback<void ()> callback)586 void Btm::SetScanningTimer(uint64_t duration_ms,
587 common::OnceCallback<void()> callback) {
588 scanning_timer_.Schedule(std::move(callback),
589 std::chrono::milliseconds(duration_ms));
590 }
591
CancelScanningTimer()592 void Btm::CancelScanningTimer() { scanning_timer_.Cancel(); }
593
StartObserving()594 void Btm::StartObserving() { StartScanning(kPassiveScanning); }
595
StopObserving()596 void Btm::StopObserving() { StopActiveScanning(); }
597
SetObservingTimer(uint64_t duration_ms,common::OnceCallback<void ()> callback)598 void Btm::SetObservingTimer(uint64_t duration_ms,
599 common::OnceCallback<void()> callback) {
600 observing_timer_.Schedule(std::move(callback),
601 std::chrono::milliseconds(duration_ms));
602 }
603
CancelObservingTimer()604 void Btm::CancelObservingTimer() { observing_timer_.Cancel(); }
605
StartScanning(bool use_active_scanning)606 void Btm::StartScanning(bool use_active_scanning) {
607 GetScanning()->RegisterScanningCallback(&scanning_callbacks_);
608 GetScanning()->Scan(true);
609 }
610
GetNumberOfAdvertisingInstances() const611 size_t Btm::GetNumberOfAdvertisingInstances() const {
612 return GetAdvertising()->GetNumberOfAdvertisingInstances();
613 }
614
CreateBond(const RawAddress & bd_addr,tBLE_ADDR_TYPE addr_type,tBT_TRANSPORT transport,int device_type)615 tBTM_STATUS Btm::CreateBond(const RawAddress& bd_addr, tBLE_ADDR_TYPE addr_type,
616 tBT_TRANSPORT transport, int device_type) {
617 if (transport == BT_TRANSPORT_UNKNOWN) {
618 if (device_type & BT_DEVICE_TYPE_BLE) {
619 transport = BT_TRANSPORT_LE;
620 } else if (device_type & BT_DEVICE_TYPE_BREDR) {
621 transport = BT_TRANSPORT_BR_EDR;
622 }
623 LOG_INFO("%s guessing transport as %02x ", __func__, transport);
624 }
625
626 auto security_manager = GetSecurityModule()->GetSecurityManager();
627 switch (transport) {
628 case BT_TRANSPORT_BR_EDR:
629 security_manager->CreateBond(ToAddressWithType(bd_addr, BLE_ADDR_PUBLIC));
630 break;
631 case BT_TRANSPORT_LE:
632 security_manager->CreateBondLe(ToAddressWithType(bd_addr, addr_type));
633 break;
634 default:
635 return BTM_ILLEGAL_VALUE;
636 }
637 return BTM_CMD_STARTED;
638 }
639
CancelBond(const RawAddress & bd_addr)640 bool Btm::CancelBond(const RawAddress& bd_addr) {
641 auto security_manager = GetSecurityModule()->GetSecurityManager();
642 security_manager->CancelBond(ToAddressWithType(bd_addr, BLE_ADDR_PUBLIC));
643 return true;
644 }
645
RemoveBond(const RawAddress & bd_addr)646 bool Btm::RemoveBond(const RawAddress& bd_addr) {
647 // TODO(cmanton) Check if acl is connected
648 auto security_manager = GetSecurityModule()->GetSecurityManager();
649 security_manager->RemoveBond(ToAddressWithType(bd_addr, BLE_ADDR_PUBLIC));
650 return true;
651 }
652
GetAclHandle(const RawAddress & remote_bda,tBT_TRANSPORT transport)653 uint16_t Btm::GetAclHandle(const RawAddress& remote_bda,
654 tBT_TRANSPORT transport) {
655 auto acl_manager = GetAclManager();
656 if (transport == BT_TRANSPORT_BR_EDR) {
657 return acl_manager->HACK_GetHandle(ToGdAddress(remote_bda));
658 } else {
659 return acl_manager->HACK_GetLeHandle(ToGdAddress(remote_bda));
660 }
661 }
662
GetAddressAndType(const RawAddress & bd_addr)663 hci::AddressWithType Btm::GetAddressAndType(const RawAddress& bd_addr) {
664 tBTM_SEC_DEV_REC* p_dev_rec = btm_find_dev(bd_addr);
665 if (p_dev_rec != NULL && p_dev_rec->device_type & BT_DEVICE_TYPE_BLE) {
666 if (!p_dev_rec->ble.identity_address_with_type.bda.IsEmpty()) {
667 return ToAddressWithType(p_dev_rec->ble.identity_address_with_type.bda,
668 p_dev_rec->ble.identity_address_with_type.type);
669 } else {
670 return ToAddressWithType(p_dev_rec->ble.pseudo_addr,
671 p_dev_rec->ble.ble_addr_type);
672 }
673 }
674 LOG(ERROR) << "Unknown bd_addr. Use public address";
675 return ToAddressWithType(bd_addr, BLE_ADDR_PUBLIC);
676 }
677
Register_HACK_SetScoDisconnectCallback(HACK_ScoDisconnectCallback callback)678 void Btm::Register_HACK_SetScoDisconnectCallback(
679 HACK_ScoDisconnectCallback callback) {
680 GetAclManager()->HACK_SetScoDisconnectCallback(callback);
681 }
682
683 } // namespace shim
684
685 } // namespace bluetooth
686