1 /******************************************************************************
2 *
3 * Copyright (C) 2016 The Linux Foundation
4 * Copyright 2009-2012 Broadcom Corporation
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at:
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 ******************************************************************************/
19
20 /*******************************************************************************
21 *
22 * Filename: bluetooth.c
23 *
24 * Description: Bluetooth HAL implementation
25 *
26 ******************************************************************************/
27
28 #define LOG_TAG "bt_btif"
29
30 #include <bluetooth/log.h>
31 #include <com_android_bluetooth_flags.h>
32 #include <hardware/bluetooth.h>
33 #include <hardware/bluetooth_headset_interface.h>
34 #include <hardware/bt_av.h>
35 #include <hardware/bt_csis.h>
36 #include <hardware/bt_gatt.h>
37 #include <hardware/bt_has.h>
38 #include <hardware/bt_hd.h>
39 #include <hardware/bt_hearing_aid.h>
40 #include <hardware/bt_hf_client.h>
41 #include <hardware/bt_hh.h>
42 #include <hardware/bt_le_audio.h>
43 #include <hardware/bt_pan.h>
44 #include <hardware/bt_rc.h>
45 #include <hardware/bt_sdp.h>
46 #include <hardware/bt_sock.h>
47 #include <hardware/bt_vc.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <unistd.h>
51
52 #include "audio_hal_interface/a2dp_encoding.h"
53 #include "bta/hh/bta_hh_int.h" // for HID HACK profile methods
54 #include "bta/include/bta_api.h"
55 #include "bta/include/bta_ar_api.h"
56 #include "bta/include/bta_csis_api.h"
57 #include "bta/include/bta_has_api.h"
58 #include "bta/include/bta_hearing_aid_api.h"
59 #include "bta/include/bta_hf_client_api.h"
60 #include "bta/include/bta_le_audio_api.h"
61 #include "bta/include/bta_le_audio_broadcaster_api.h"
62 #include "bta/include/bta_vc_api.h"
63 #include "btif/avrcp/avrcp_service.h"
64 #include "btif/include/btif_sock.h"
65 #include "btif/include/btif_sock_logging.h"
66 #include "btif/include/core_callbacks.h"
67 #include "btif/include/stack_manager_t.h"
68 #include "btif_a2dp.h"
69 #include "btif_api.h"
70 #include "btif_av.h"
71 #include "btif_bqr.h"
72 #include "btif_config.h"
73 #include "btif_debug_conn.h"
74 #include "btif_dm.h"
75 #include "btif_hd.h"
76 #include "btif_hf.h"
77 #include "btif_hh.h"
78 #include "btif_keystore.h"
79 #include "btif_metrics_logging.h"
80 #include "btif_pan.h"
81 #include "btif_profile_storage.h"
82 #include "btif_rc.h"
83 #include "btif_sock.h"
84 #include "btif_sock_logging.h"
85 #include "btif_storage.h"
86 #include "common/address_obfuscator.h"
87 #include "common/init_flags.h"
88 #include "common/metrics.h"
89 #include "common/os_utils.h"
90 #include "device/include/device_iot_config.h"
91 #include "device/include/esco_parameters.h"
92 #include "device/include/interop.h"
93 #include "device/include/interop_config.h"
94 #include "internal_include/bt_target.h"
95 #include "main/shim/dumpsys.h"
96 #include "os/parameter_provider.h"
97 #include "osi/include/alarm.h"
98 #include "osi/include/allocator.h"
99 #include "osi/include/stack_power_telemetry.h"
100 #include "osi/include/wakelock.h"
101 #include "stack/btm/btm_sco_hfp_hal.h"
102 #include "stack/gatt/connection_manager.h"
103 #include "stack/include/a2dp_api.h"
104 #include "stack/include/avdt_api.h"
105 #include "stack/include/btm_api.h"
106 #include "stack/include/btm_client_interface.h"
107 #include "stack/include/hfp_lc3_decoder.h"
108 #include "stack/include/hfp_lc3_encoder.h"
109 #include "stack/include/hfp_msbc_decoder.h"
110 #include "stack/include/hfp_msbc_encoder.h"
111 #include "stack/include/hidh_api.h"
112 #include "stack/include/main_thread.h"
113 #include "stack/include/pan_api.h"
114 #include "storage/config_keys.h"
115 #include "types/raw_address.h"
116
117 using bluetooth::csis::CsisClientInterface;
118 using bluetooth::has::HasClientInterface;
119 using bluetooth::hearing_aid::HearingAidInterface;
120 using bluetooth::le_audio::LeAudioBroadcasterInterface;
121 using bluetooth::le_audio::LeAudioClientInterface;
122 using bluetooth::vc::VolumeControlInterface;
123 using namespace bluetooth;
124
125 /*******************************************************************************
126 * Static variables
127 ******************************************************************************/
128
129 static bt_callbacks_t* bt_hal_cbacks = NULL;
130 bool restricted_mode = false;
131 bool common_criteria_mode = false;
132 const int CONFIG_COMPARE_ALL_PASS = 0b11;
133 int common_criteria_config_compare_result = CONFIG_COMPARE_ALL_PASS;
134 bool is_local_device_atv = false;
135
136 /*******************************************************************************
137 * Externs
138 ******************************************************************************/
139
140 /* list all extended interfaces here */
141
142 /* handsfree profile - client */
143 extern const bthf_client_interface_t* btif_hf_client_get_interface();
144 /* advanced audio profile */
145 extern const btav_source_interface_t* btif_av_get_src_interface();
146 extern const btav_sink_interface_t* btif_av_get_sink_interface();
147 /*rfc l2cap*/
148 extern const btsock_interface_t* btif_sock_get_interface();
149 /* hid host profile */
150 extern const bthh_interface_t* btif_hh_get_interface();
151 /* hid device profile */
152 extern const bthd_interface_t* btif_hd_get_interface();
153 /*pan*/
154 extern const btpan_interface_t* btif_pan_get_interface();
155 /* gatt */
156 extern const btgatt_interface_t* btif_gatt_get_interface();
157 /* avrc target */
158 extern const btrc_interface_t* btif_rc_get_interface();
159 /* avrc controller */
160 extern const btrc_ctrl_interface_t* btif_rc_ctrl_get_interface();
161 /*SDP search client*/
162 extern const btsdp_interface_t* btif_sdp_get_interface();
163 /*Hearing Aid client*/
164 extern HearingAidInterface* btif_hearing_aid_get_interface();
165 /* Hearing Access client */
166 extern HasClientInterface* btif_has_client_get_interface();
167 /* LeAudio testi client */
168 extern LeAudioClientInterface* btif_le_audio_get_interface();
169 /* LeAudio Broadcaster */
170 extern LeAudioBroadcasterInterface* btif_le_audio_broadcaster_get_interface();
171 /* Coordinated Set Service Client */
172 extern CsisClientInterface* btif_csis_client_get_interface();
173 /* Volume Control client */
174 extern VolumeControlInterface* btif_volume_control_get_interface();
175
176 bt_status_t btif_av_sink_execute_service(bool b_enable);
177 bt_status_t btif_hh_execute_service(bool b_enable);
178 bt_status_t btif_hf_client_execute_service(bool b_enable);
179 bt_status_t btif_sdp_execute_service(bool b_enable);
180 bt_status_t btif_hd_execute_service(bool b_enable);
181
182 extern void gatt_tcb_dump(int fd);
183 extern void bta_gatt_client_dump(int fd);
184
185 /*******************************************************************************
186 * Callbacks from bluetooth::core (see go/invisalign-bt)
187 ******************************************************************************/
188
189 struct ConfigInterfaceImpl : bluetooth::core::ConfigInterface {
ConfigInterfaceImplConfigInterfaceImpl190 ConfigInterfaceImpl() : bluetooth::core::ConfigInterface(){};
191
isRestrictedModeConfigInterfaceImpl192 bool isRestrictedMode() override { return is_restricted_mode(); }
193
isA2DPOffloadEnabledConfigInterfaceImpl194 bool isA2DPOffloadEnabled() override {
195 char value_sup[PROPERTY_VALUE_MAX] = {'\0'};
196 char value_dis[PROPERTY_VALUE_MAX] = {'\0'};
197
198 osi_property_get("ro.bluetooth.a2dp_offload.supported", value_sup, "false");
199 osi_property_get("persist.bluetooth.a2dp_offload.disabled", value_dis,
200 "false");
201 auto a2dp_offload_enabled =
202 (strcmp(value_sup, "true") == 0) && (strcmp(value_dis, "false") == 0);
203 log::verbose("a2dp_offload.enable = {}", a2dp_offload_enabled);
204
205 return a2dp_offload_enabled;
206 }
207
isAndroidTVDeviceConfigInterfaceImpl208 bool isAndroidTVDevice() override { return is_atv_device(); }
209 };
210
211 // TODO(aryarahul): remove unnecessary indirection through hfp_msbc_*.cc
212 struct MSBCCodec : bluetooth::core::CodecInterface {
MSBCCodecMSBCCodec213 MSBCCodec() : bluetooth::core::CodecInterface(){};
214
initializeMSBCCodec215 void initialize() override {
216 hfp_msbc_decoder_init();
217 hfp_msbc_encoder_init();
218 }
219
cleanupMSBCCodec220 void cleanup() override {
221 hfp_msbc_decoder_cleanup();
222 hfp_msbc_encoder_cleanup();
223 }
224
encodePacketMSBCCodec225 uint32_t encodePacket(int16_t* input, uint8_t* output) {
226 return hfp_msbc_encode_frames(input, output);
227 }
228
decodePacketMSBCCodec229 bool decodePacket(const uint8_t* i_buf, int16_t* o_buf, size_t out_len) {
230 return hfp_msbc_decoder_decode_packet(i_buf, o_buf, out_len);
231 }
232 };
233
234 struct LC3Codec : bluetooth::core::CodecInterface {
LC3CodecLC3Codec235 LC3Codec() : bluetooth::core::CodecInterface(){};
236
initializeLC3Codec237 void initialize() override {
238 hfp_lc3_decoder_init();
239 hfp_lc3_encoder_init();
240 }
241
cleanupLC3Codec242 void cleanup() override {
243 hfp_lc3_encoder_cleanup();
244 hfp_lc3_decoder_cleanup();
245 }
246
encodePacketLC3Codec247 uint32_t encodePacket(int16_t* input, uint8_t* output) {
248 return hfp_lc3_encode_frames(input, output);
249 }
250
decodePacketLC3Codec251 bool decodePacket(const uint8_t* i_buf, int16_t* o_buf, size_t out_len) {
252 return hfp_lc3_decoder_decode_packet(i_buf, o_buf, out_len);
253 }
254 };
255
256 struct CoreInterfaceImpl : bluetooth::core::CoreInterface {
257 using bluetooth::core::CoreInterface::CoreInterface;
258
onBluetoothEnabledCoreInterfaceImpl259 void onBluetoothEnabled() override {
260 /* init pan */
261 btif_pan_init();
262 }
263
toggleProfileCoreInterfaceImpl264 bt_status_t toggleProfile(tBTA_SERVICE_ID service_id, bool enable) override {
265 /* Check the service_ID and invoke the profile's BT state changed API */
266 switch (service_id) {
267 case BTA_HFP_SERVICE_ID:
268 case BTA_HSP_SERVICE_ID: {
269 bluetooth::headset::ExecuteService(enable);
270 } break;
271 case BTA_A2DP_SOURCE_SERVICE_ID: {
272 btif_av_source_execute_service(enable);
273 } break;
274 case BTA_A2DP_SINK_SERVICE_ID: {
275 btif_av_sink_execute_service(enable);
276 } break;
277 case BTA_HID_SERVICE_ID: {
278 btif_hh_execute_service(enable);
279 } break;
280 case BTA_HFP_HS_SERVICE_ID: {
281 btif_hf_client_execute_service(enable);
282 } break;
283 case BTA_HIDD_SERVICE_ID: {
284 btif_hd_execute_service(enable);
285 } break;
286 case BTA_PBAP_SERVICE_ID:
287 case BTA_PCE_SERVICE_ID:
288 case BTA_MAP_SERVICE_ID:
289 case BTA_MN_SERVICE_ID: {
290 /**
291 * Do nothing; these services were started elsewhere. However, we need
292 * to flow through this codepath in order to properly report back the
293 * local UUIDs back to adapter properties in Java. To achieve this, we
294 * need to catch these service IDs in order for {@link
295 * btif_in_execute_service_request} to return {@code BT_STATUS_SUCCESS},
296 * so that in {@link btif_dm_enable_service} the check passes and the
297 * UUIDs are allowed to be passed up into the Java layer.
298 */
299 } break;
300 default:
301 log::error("Unknown service {} being {}", service_id,
302 (enable) ? "enabled" : "disabled");
303 return BT_STATUS_FAIL;
304 }
305 return BT_STATUS_SUCCESS;
306 }
307
removeDeviceFromProfilesCoreInterfaceImpl308 void removeDeviceFromProfiles(const RawAddress& bd_addr) override {
309 /*special handling for HID devices */
310 #if (defined(BTA_HH_INCLUDED) && (BTA_HH_INCLUDED == TRUE))
311 tAclLinkSpec link_spec;
312 link_spec.addrt.bda = bd_addr;
313 link_spec.addrt.type = BLE_ADDR_PUBLIC;
314 link_spec.transport = BT_TRANSPORT_AUTO;
315
316 btif_hh_remove_device(link_spec);
317 #endif
318 #if (defined(BTA_HD_INCLUDED) && (BTA_HD_INCLUDED == TRUE))
319 btif_hd_remove_device(bd_addr);
320 #endif
321 btif_hearing_aid_get_interface()->RemoveDevice(bd_addr);
322
323 if (bluetooth::csis::CsisClient::IsCsisClientRunning())
324 btif_csis_client_get_interface()->RemoveDevice(bd_addr);
325
326 if (LeAudioClient::IsLeAudioClientRunning())
327 btif_le_audio_get_interface()->RemoveDevice(bd_addr);
328
329 if (VolumeControl::IsVolumeControlRunning()) {
330 btif_volume_control_get_interface()->RemoveDevice(bd_addr);
331 }
332 }
333
onLinkDownCoreInterfaceImpl334 void onLinkDown(const RawAddress& bd_addr) override {
335 if (com::android::bluetooth::flags::a2dp_concurrent_source_sink()) {
336 btif_av_acl_disconnected(bd_addr, A2dpType::kSource);
337 btif_av_acl_disconnected(bd_addr, A2dpType::kSink);
338 } else {
339 btif_av_acl_disconnected(bd_addr, A2dpType::kUnknown);
340 }
341 }
342 };
343
CreateInterfaceToProfiles()344 static bluetooth::core::CoreInterface* CreateInterfaceToProfiles() {
345 static bluetooth::core::EventCallbacks eventCallbacks{
346 .invoke_adapter_state_changed_cb = invoke_adapter_state_changed_cb,
347 .invoke_adapter_properties_cb = invoke_adapter_properties_cb,
348 .invoke_remote_device_properties_cb = invoke_remote_device_properties_cb,
349 .invoke_device_found_cb = invoke_device_found_cb,
350 .invoke_discovery_state_changed_cb = invoke_discovery_state_changed_cb,
351 .invoke_pin_request_cb = invoke_pin_request_cb,
352 .invoke_ssp_request_cb = invoke_ssp_request_cb,
353 .invoke_oob_data_request_cb = invoke_oob_data_request_cb,
354 .invoke_bond_state_changed_cb = invoke_bond_state_changed_cb,
355 .invoke_address_consolidate_cb = invoke_address_consolidate_cb,
356 .invoke_le_address_associate_cb = invoke_le_address_associate_cb,
357 .invoke_acl_state_changed_cb = invoke_acl_state_changed_cb,
358 .invoke_thread_evt_cb = invoke_thread_evt_cb,
359 .invoke_le_test_mode_cb = invoke_le_test_mode_cb,
360 .invoke_energy_info_cb = invoke_energy_info_cb,
361 .invoke_link_quality_report_cb = invoke_link_quality_report_cb,
362 .invoke_key_missing_cb = invoke_key_missing_cb,
363 };
364 static bluetooth::core::HACK_ProfileInterface profileInterface{
365 // HID
366 .btif_hh_connect = btif_hh_connect,
367 .btif_hh_virtual_unplug = btif_hh_virtual_unplug,
368 .bta_hh_read_ssr_param = bta_hh_read_ssr_param,
369
370 // AVDTP
371 .btif_av_set_dynamic_audio_buffer_size =
372 btif_av_set_dynamic_audio_buffer_size,
373
374 // ASHA
375 .GetHearingAidDeviceCount = HearingAid::GetDeviceCount,
376
377 // LE Audio
378 .IsLeAudioClientRunning = LeAudioClient::IsLeAudioClientRunning,
379
380 // AVRCP
381 .AVRC_GetProfileVersion = AVRC_GetProfileVersion,
382 };
383
384 static auto configInterface = ConfigInterfaceImpl();
385 static auto msbcCodecInterface = MSBCCodec();
386 static auto lc3CodecInterface = LC3Codec();
387
388 static auto interfaceForCore =
389 CoreInterfaceImpl(&eventCallbacks, &configInterface, &msbcCodecInterface,
390 &lc3CodecInterface, &profileInterface);
391 return &interfaceForCore;
392 }
393
394 /*******************************************************************************
395 * Functions
396 ******************************************************************************/
397
interface_ready(void)398 static bool interface_ready(void) { return bt_hal_cbacks != NULL; }
set_hal_cbacks(bt_callbacks_t * callbacks)399 static void set_hal_cbacks(bt_callbacks_t* callbacks) {
400 bt_hal_cbacks = callbacks;
401 }
402
is_profile(const char * p1,const char * p2)403 static bool is_profile(const char* p1, const char* p2) {
404 log::assert_that(p1 != nullptr, "assert failed: p1 != nullptr");
405 log::assert_that(p2 != nullptr, "assert failed: p2 != nullptr");
406 return strlen(p1) == strlen(p2) && strncmp(p1, p2, strlen(p2)) == 0;
407 }
408
409 /*****************************************************************************
410 *
411 * BLUETOOTH HAL INTERFACE FUNCTIONS
412 *
413 ****************************************************************************/
414
init(bt_callbacks_t * callbacks,bool start_restricted,bool is_common_criteria_mode,int config_compare_result,const char ** init_flags,bool is_atv,const char * user_data_directory)415 static int init(bt_callbacks_t* callbacks, bool start_restricted,
416 bool is_common_criteria_mode, int config_compare_result,
417 const char** init_flags, bool is_atv,
418 const char* user_data_directory) {
419 (void)user_data_directory;
420 log::info(
421 "start restricted = {} ; common criteria mode = {}, config compare "
422 "result = {}",
423 start_restricted, is_common_criteria_mode, config_compare_result);
424
425 bluetooth::common::InitFlags::Load(init_flags);
426
427 if (interface_ready()) return BT_STATUS_DONE;
428
429 set_hal_cbacks(callbacks);
430
431 restricted_mode = start_restricted;
432
433 bluetooth::os::ParameterProvider::SetBtKeystoreInterface(
434 bluetooth::bluetooth_keystore::getBluetoothKeystoreInterface());
435 bluetooth::os::ParameterProvider::SetCommonCriteriaMode(
436 is_common_criteria_mode);
437 if (is_bluetooth_uid() && is_common_criteria_mode) {
438 bluetooth::os::ParameterProvider::SetCommonCriteriaConfigCompareResult(
439 config_compare_result);
440 } else {
441 bluetooth::os::ParameterProvider::SetCommonCriteriaConfigCompareResult(
442 CONFIG_COMPARE_ALL_PASS);
443 }
444
445 is_local_device_atv = is_atv;
446
447 stack_manager_get_interface()->init_stack(CreateInterfaceToProfiles());
448 return BT_STATUS_SUCCESS;
449 }
450
start_profiles()451 static void start_profiles() {
452 #if (BNEP_INCLUDED == TRUE)
453 BNEP_Init();
454 #if (PAN_INCLUDED == TRUE)
455 PAN_Init();
456 #endif /* PAN */
457 #endif /* BNEP Included */
458 A2DP_Init();
459 AVRC_Init();
460 #if (HID_HOST_INCLUDED == TRUE)
461 HID_HostInit();
462 #endif
463 bta_ar_init();
464 }
465
stop_profiles()466 static void stop_profiles() {
467 btif_sock_cleanup();
468 btif_pan_cleanup();
469 }
470
enable()471 static int enable() {
472 if (!interface_ready()) return BT_STATUS_NOT_READY;
473
474 stack_manager_get_interface()->start_up_stack_async(
475 CreateInterfaceToProfiles(), &start_profiles, &stop_profiles);
476 return BT_STATUS_SUCCESS;
477 }
478
disable(void)479 static int disable(void) {
480 if (!interface_ready()) return BT_STATUS_NOT_READY;
481
482 stack_manager_get_interface()->shut_down_stack_async(&stop_profiles);
483 return BT_STATUS_SUCCESS;
484 }
485
cleanup(void)486 static void cleanup(void) {
487 stack_manager_get_interface()->clean_up_stack(&stop_profiles);
488 }
489
is_restricted_mode()490 bool is_restricted_mode() { return restricted_mode; }
491
get_wbs_supported()492 static bool get_wbs_supported() {
493 return hfp_hal_interface::get_wbs_supported();
494 }
495
get_swb_supported()496 static bool get_swb_supported() {
497 return hfp_hal_interface::get_swb_supported();
498 }
499
is_coding_format_supported(esco_coding_format_t coding_format)500 static bool is_coding_format_supported(esco_coding_format_t coding_format) {
501 return hfp_hal_interface::is_coding_format_supported(coding_format);
502 }
503
is_common_criteria_mode()504 bool is_common_criteria_mode() {
505 return is_bluetooth_uid() && common_criteria_mode;
506 }
507 // if common criteria mode disable, will always return
508 // CONFIG_COMPARE_ALL_PASS(0b11) indicate don't check config checksum.
get_common_criteria_config_compare_result()509 int get_common_criteria_config_compare_result() {
510 return is_common_criteria_mode() ? common_criteria_config_compare_result
511 : CONFIG_COMPARE_ALL_PASS;
512 }
513
is_atv_device()514 bool is_atv_device() { return is_local_device_atv; }
515
get_adapter_properties(void)516 static int get_adapter_properties(void) {
517 if (!btif_is_enabled()) return BT_STATUS_NOT_READY;
518
519 do_in_main_thread(FROM_HERE, base::BindOnce(btif_get_adapter_properties));
520 return BT_STATUS_SUCCESS;
521 }
522
get_adapter_property(bt_property_type_t type)523 static int get_adapter_property(bt_property_type_t type) {
524 /* Allow get_adapter_property only for BDADDR and BDNAME if BT is disabled */
525 if (!btif_is_enabled() && (type != BT_PROPERTY_BDADDR) &&
526 (type != BT_PROPERTY_BDNAME))
527 return BT_STATUS_NOT_READY;
528
529 do_in_main_thread(FROM_HERE, base::BindOnce(btif_get_adapter_property, type));
530 return BT_STATUS_SUCCESS;
531 }
532
set_adapter_property(const bt_property_t * property)533 static int set_adapter_property(const bt_property_t* property) {
534 if (!btif_is_enabled()) return BT_STATUS_NOT_READY;
535
536 switch (property->type) {
537 case BT_PROPERTY_BDNAME:
538 case BT_PROPERTY_ADAPTER_SCAN_MODE:
539 case BT_PROPERTY_ADAPTER_DISCOVERABLE_TIMEOUT:
540 case BT_PROPERTY_CLASS_OF_DEVICE:
541 break;
542 default:
543 return BT_STATUS_UNHANDLED;
544 }
545
546 do_in_main_thread(FROM_HERE, base::BindOnce(
547 [](bt_property_t* property) {
548 btif_set_adapter_property(property);
549 osi_free(property);
550 },
551 property_deep_copy(property)));
552 return BT_STATUS_SUCCESS;
553 }
554
get_remote_device_properties(RawAddress * remote_addr)555 int get_remote_device_properties(RawAddress* remote_addr) {
556 if (!btif_is_enabled()) return BT_STATUS_NOT_READY;
557
558 do_in_main_thread(FROM_HERE, base::BindOnce(btif_get_remote_device_properties,
559 *remote_addr));
560 return BT_STATUS_SUCCESS;
561 }
562
get_remote_device_property(RawAddress * remote_addr,bt_property_type_t type)563 int get_remote_device_property(RawAddress* remote_addr,
564 bt_property_type_t type) {
565 if (!btif_is_enabled()) return BT_STATUS_NOT_READY;
566
567 do_in_main_thread(FROM_HERE, base::BindOnce(btif_get_remote_device_property,
568 *remote_addr, type));
569 return BT_STATUS_SUCCESS;
570 }
571
set_remote_device_property(RawAddress * remote_addr,const bt_property_t * property)572 int set_remote_device_property(RawAddress* remote_addr,
573 const bt_property_t* property) {
574 if (!btif_is_enabled()) return BT_STATUS_NOT_READY;
575
576 do_in_main_thread(
577 FROM_HERE, base::BindOnce(
578 [](RawAddress remote_addr, bt_property_t* property) {
579 btif_set_remote_device_property(&remote_addr, property);
580 osi_free(property);
581 },
582 *remote_addr, property_deep_copy(property)));
583 return BT_STATUS_SUCCESS;
584 }
585
get_remote_services(RawAddress * remote_addr,int transport)586 int get_remote_services(RawAddress* remote_addr, int transport) {
587 if (!interface_ready()) return BT_STATUS_NOT_READY;
588
589 do_in_main_thread(FROM_HERE, base::BindOnce(btif_dm_get_remote_services,
590 *remote_addr, transport));
591 return BT_STATUS_SUCCESS;
592 }
593
start_discovery(void)594 static int start_discovery(void) {
595 if (!interface_ready()) return BT_STATUS_NOT_READY;
596
597 do_in_main_thread(FROM_HERE, base::BindOnce(btif_dm_start_discovery));
598 return BT_STATUS_SUCCESS;
599 }
600
cancel_discovery(void)601 static int cancel_discovery(void) {
602 if (!interface_ready()) return BT_STATUS_NOT_READY;
603
604 do_in_main_thread(FROM_HERE, base::BindOnce(btif_dm_cancel_discovery));
605 return BT_STATUS_SUCCESS;
606 }
607
create_bond(const RawAddress * bd_addr,int transport)608 static int create_bond(const RawAddress* bd_addr, int transport) {
609 if (!interface_ready()) return BT_STATUS_NOT_READY;
610 if (btif_dm_pairing_is_busy()) return BT_STATUS_BUSY;
611
612 do_in_main_thread(FROM_HERE,
613 base::BindOnce(btif_dm_create_bond, *bd_addr, transport));
614 return BT_STATUS_SUCCESS;
615 }
616
create_bond_le(const RawAddress * bd_addr,uint8_t addr_type)617 static int create_bond_le(const RawAddress* bd_addr, uint8_t addr_type) {
618 if (!interface_ready()) return BT_STATUS_NOT_READY;
619 if (btif_dm_pairing_is_busy()) return BT_STATUS_BUSY;
620
621 do_in_main_thread(
622 FROM_HERE, base::BindOnce(btif_dm_create_bond_le, *bd_addr, addr_type));
623 return BT_STATUS_SUCCESS;
624 }
625
create_bond_out_of_band(const RawAddress * bd_addr,int transport,const bt_oob_data_t * p192_data,const bt_oob_data_t * p256_data)626 static int create_bond_out_of_band(const RawAddress* bd_addr, int transport,
627 const bt_oob_data_t* p192_data,
628 const bt_oob_data_t* p256_data) {
629 if (!interface_ready()) return BT_STATUS_NOT_READY;
630 if (btif_dm_pairing_is_busy()) return BT_STATUS_BUSY;
631
632 do_in_main_thread(FROM_HERE,
633 base::BindOnce(btif_dm_create_bond_out_of_band, *bd_addr,
634 transport, *p192_data, *p256_data));
635 return BT_STATUS_SUCCESS;
636 }
637
generate_local_oob_data(tBT_TRANSPORT transport)638 static int generate_local_oob_data(tBT_TRANSPORT transport) {
639 log::info("");
640 if (!interface_ready()) return BT_STATUS_NOT_READY;
641
642 return do_in_main_thread(
643 FROM_HERE, base::BindOnce(btif_dm_generate_local_oob_data, transport));
644 }
645
cancel_bond(const RawAddress * bd_addr)646 static int cancel_bond(const RawAddress* bd_addr) {
647 if (!interface_ready()) return BT_STATUS_NOT_READY;
648
649 do_in_main_thread(FROM_HERE, base::BindOnce(btif_dm_cancel_bond, *bd_addr));
650 return BT_STATUS_SUCCESS;
651 }
652
remove_bond(const RawAddress * bd_addr)653 static int remove_bond(const RawAddress* bd_addr) {
654 if (is_restricted_mode() && !btif_storage_is_restricted_device(bd_addr)) {
655 log::info("{} cannot be removed in restricted mode", *bd_addr);
656 return BT_STATUS_SUCCESS;
657 }
658
659 if (!interface_ready()) return BT_STATUS_NOT_READY;
660
661 do_in_main_thread(FROM_HERE, base::BindOnce(btif_dm_remove_bond, *bd_addr));
662 return BT_STATUS_SUCCESS;
663 }
664
pairing_is_busy()665 static bool pairing_is_busy() {
666 if (btif_dm_pairing_is_busy()) return true;
667
668 return false;
669 }
670
get_connection_state(const RawAddress * bd_addr)671 static int get_connection_state(const RawAddress* bd_addr) {
672 if (!interface_ready()) return 0;
673
674 if (bd_addr == nullptr) return 0;
675
676 return btif_dm_get_connection_state(*bd_addr);
677 }
678
pin_reply(const RawAddress * bd_addr,uint8_t accept,uint8_t pin_len,bt_pin_code_t * pin_code)679 static int pin_reply(const RawAddress* bd_addr, uint8_t accept, uint8_t pin_len,
680 bt_pin_code_t* pin_code) {
681 bt_pin_code_t tmp_pin_code;
682 if (!interface_ready()) return BT_STATUS_NOT_READY;
683 if (pin_code == nullptr || pin_len > PIN_CODE_LEN) {
684 return BT_STATUS_PARM_INVALID;
685 }
686
687 memcpy(&tmp_pin_code, pin_code, pin_len);
688
689 do_in_main_thread(FROM_HERE, base::BindOnce(btif_dm_pin_reply, *bd_addr,
690 accept, pin_len, tmp_pin_code));
691 return BT_STATUS_SUCCESS;
692 }
693
ssp_reply(const RawAddress * bd_addr,bt_ssp_variant_t variant,uint8_t accept,uint32_t)694 static int ssp_reply(const RawAddress* bd_addr, bt_ssp_variant_t variant,
695 uint8_t accept, uint32_t /* passkey */) {
696 if (!interface_ready()) return BT_STATUS_NOT_READY;
697 if (variant == BT_SSP_VARIANT_PASSKEY_ENTRY) return BT_STATUS_PARM_INVALID;
698
699 do_in_main_thread(
700 FROM_HERE, base::BindOnce(btif_dm_ssp_reply, *bd_addr, variant, accept));
701 return BT_STATUS_SUCCESS;
702 }
703
read_energy_info()704 static int read_energy_info() {
705 if (!interface_ready()) return BT_STATUS_NOT_READY;
706
707 do_in_main_thread(FROM_HERE, base::BindOnce(btif_dm_read_energy_info));
708 return BT_STATUS_SUCCESS;
709 }
710
clear_event_filter()711 static int clear_event_filter() {
712 log::verbose("");
713 if (!interface_ready()) return BT_STATUS_NOT_READY;
714
715 do_in_main_thread(FROM_HERE, base::BindOnce(btif_dm_clear_event_filter));
716 return BT_STATUS_SUCCESS;
717 }
718
clear_event_mask()719 static int clear_event_mask() {
720 log::verbose("");
721 if (!interface_ready()) return BT_STATUS_NOT_READY;
722
723 do_in_main_thread(FROM_HERE, base::BindOnce(btif_dm_clear_event_mask));
724 return BT_STATUS_SUCCESS;
725 }
726
clear_filter_accept_list()727 static int clear_filter_accept_list() {
728 log::verbose("");
729 if (!interface_ready()) return BT_STATUS_NOT_READY;
730
731 do_in_main_thread(FROM_HERE,
732 base::BindOnce(btif_dm_clear_filter_accept_list));
733 return BT_STATUS_SUCCESS;
734 }
735
disconnect_all_acls()736 static int disconnect_all_acls() {
737 log::verbose("");
738 if (!interface_ready()) return BT_STATUS_NOT_READY;
739
740 do_in_main_thread(FROM_HERE, base::BindOnce(btif_dm_disconnect_all_acls));
741 return BT_STATUS_SUCCESS;
742 }
743
le_rand_btif_cb(uint64_t random_number)744 static void le_rand_btif_cb(uint64_t random_number) {
745 log::verbose("");
746 do_in_jni_thread(base::BindOnce(
747 [](uint64_t random) { HAL_CBACK(bt_hal_cbacks, le_rand_cb, random); },
748 random_number));
749 }
750
le_rand()751 static int le_rand() {
752 log::verbose("");
753 if (!interface_ready()) return BT_STATUS_NOT_READY;
754
755 do_in_main_thread(
756 FROM_HERE, base::BindOnce(btif_dm_le_rand,
757 get_main_thread()->BindOnce(&le_rand_btif_cb)));
758 return BT_STATUS_SUCCESS;
759 }
760
set_event_filter_inquiry_result_all_devices()761 static int set_event_filter_inquiry_result_all_devices() {
762 if (!interface_ready()) return BT_STATUS_NOT_READY;
763 do_in_main_thread(
764 FROM_HERE,
765 base::BindOnce(btif_dm_set_event_filter_inquiry_result_all_devices));
766 return BT_STATUS_SUCCESS;
767 }
768
set_default_event_mask_except(uint64_t mask,uint64_t le_mask)769 static int set_default_event_mask_except(uint64_t mask, uint64_t le_mask) {
770 if (!interface_ready()) return BT_STATUS_NOT_READY;
771 do_in_main_thread(
772 FROM_HERE,
773 base::BindOnce(btif_dm_set_default_event_mask_except, mask, le_mask));
774 return BT_STATUS_SUCCESS;
775 }
776
restore_filter_accept_list()777 static int restore_filter_accept_list() {
778 if (!interface_ready()) return BT_STATUS_NOT_READY;
779 // TODO(b/260922031) - When restoring the filter accept list after a system
780 // suspend, we need to re-arm the LE connections that had `is_direct=False`.
781 // This should be the list of bonded devices and potentially any GATT
782 // connections that have `is_direct=False`. Currently, we only restore LE hid
783 // devices.
784 auto le_hid_addrs = btif_storage_get_le_hid_devices();
785 do_in_main_thread(FROM_HERE,
786 base::BindOnce(btif_dm_restore_filter_accept_list,
787 std::move(le_hid_addrs)));
788 return BT_STATUS_SUCCESS;
789 }
790
allow_wake_by_hid()791 static int allow_wake_by_hid() {
792 if (!interface_ready()) return BT_STATUS_NOT_READY;
793 auto le_hid_addrs = btif_storage_get_le_hid_devices();
794 auto classic_hid_addrs = btif_storage_get_wake_capable_classic_hid_devices();
795 do_in_main_thread(FROM_HERE, base::BindOnce(btif_dm_allow_wake_by_hid,
796 std::move(classic_hid_addrs),
797 std::move(le_hid_addrs)));
798 return BT_STATUS_SUCCESS;
799 }
800
set_event_filter_connection_setup_all_devices()801 static int set_event_filter_connection_setup_all_devices() {
802 if (!interface_ready()) return BT_STATUS_NOT_READY;
803 do_in_main_thread(
804 FROM_HERE,
805 base::BindOnce(btif_dm_set_event_filter_connection_setup_all_devices));
806 return BT_STATUS_SUCCESS;
807 }
808
dump(int fd,const char ** arguments)809 static void dump(int fd, const char** arguments) {
810 log::debug("Started bluetooth dumpsys");
811 btif_debug_conn_dump(fd);
812 btif_debug_bond_event_dump(fd);
813 btif_debug_linkkey_type_dump(fd);
814 btif_debug_rc_dump(fd);
815 btif_debug_a2dp_dump(fd);
816 btif_debug_av_dump(fd);
817 bta_debug_av_dump(fd);
818 stack_debug_avdtp_api_dump(fd);
819 btif_sock_dump(fd);
820 bluetooth::avrcp::AvrcpService::DebugDump(fd);
821 gatt_tcb_dump(fd);
822 bta_gatt_client_dump(fd);
823 device_debug_iot_config_dump(fd);
824 BTA_HfClientDumpStatistics(fd);
825 wakelock_debug_dump(fd);
826 alarm_debug_dump(fd);
827 bluetooth::csis::CsisClient::DebugDump(fd);
828 ::bluetooth::le_audio::has::HasClient::DebugDump(fd);
829 HearingAid::DebugDump(fd);
830 LeAudioClient::DebugDump(fd);
831 LeAudioBroadcaster::DebugDump(fd);
832 VolumeControl::DebugDump(fd);
833 connection_manager::dump(fd);
834 bluetooth::bqr::DebugDump(fd);
835 PAN_Dumpsys(fd);
836 DumpsysHid(fd);
837 DumpsysBtaDm(fd);
838 bluetooth::shim::Dump(fd, arguments);
839 power_telemetry::GetInstance().Dumpsys(fd);
840 log::debug("Finished bluetooth dumpsys");
841 }
842
dumpMetrics(std::string * output)843 static void dumpMetrics(std::string* output) {
844 bluetooth::common::BluetoothMetricsLogger::GetInstance()->WriteString(output);
845 }
846
get_remote_pbap_pce_version(const RawAddress * bd_addr)847 static int get_remote_pbap_pce_version(const RawAddress* bd_addr) {
848 // Read and restore the PCE version from local storage
849 uint16_t pce_version = 0;
850 size_t version_value_size = sizeof(pce_version);
851 if (!btif_config_get_bin(bd_addr->ToString(),
852 BTIF_STORAGE_KEY_PBAP_PCE_VERSION,
853 (uint8_t*)&pce_version, &version_value_size)) {
854 log::warn("Failed to read cached peer PCE version for {}", *bd_addr);
855 }
856 return pce_version;
857 }
858
pbap_pse_dynamic_version_upgrade_is_enabled()859 static bool pbap_pse_dynamic_version_upgrade_is_enabled() {
860 if (bluetooth::common::init_flags::
861 pbap_pse_dynamic_version_upgrade_is_enabled()) {
862 return true;
863 }
864 log::warn("PBAP PSE dynamic version upgrade is not enabled");
865 return false;
866 }
867
get_profile_interface(const char * profile_id)868 static const void* get_profile_interface(const char* profile_id) {
869 log::info("id = {}", profile_id);
870
871 /* sanity check */
872 if (!interface_ready()) return NULL;
873
874 /* check for supported profile interfaces */
875 if (is_profile(profile_id, BT_PROFILE_HANDSFREE_ID))
876 return bluetooth::headset::GetInterface();
877
878 if (is_profile(profile_id, BT_PROFILE_HANDSFREE_CLIENT_ID))
879 return btif_hf_client_get_interface();
880
881 if (is_profile(profile_id, BT_PROFILE_SOCKETS_ID))
882 return btif_sock_get_interface();
883
884 if (is_profile(profile_id, BT_PROFILE_PAN_ID))
885 return btif_pan_get_interface();
886
887 if (is_profile(profile_id, BT_PROFILE_ADVANCED_AUDIO_ID))
888 return btif_av_get_src_interface();
889
890 if (is_profile(profile_id, BT_PROFILE_ADVANCED_AUDIO_SINK_ID))
891 return btif_av_get_sink_interface();
892
893 if (is_profile(profile_id, BT_PROFILE_HIDHOST_ID))
894 return btif_hh_get_interface();
895
896 if (is_profile(profile_id, BT_PROFILE_HIDDEV_ID))
897 return btif_hd_get_interface();
898
899 if (is_profile(profile_id, BT_PROFILE_SDP_CLIENT_ID))
900 return btif_sdp_get_interface();
901
902 if (is_profile(profile_id, BT_PROFILE_GATT_ID))
903 return btif_gatt_get_interface();
904
905 if (is_profile(profile_id, BT_PROFILE_AV_RC_ID))
906 return btif_rc_get_interface();
907
908 if (is_profile(profile_id, BT_PROFILE_AV_RC_CTRL_ID))
909 return btif_rc_ctrl_get_interface();
910
911 if (is_profile(profile_id, BT_PROFILE_HEARING_AID_ID))
912 return btif_hearing_aid_get_interface();
913
914 if (is_profile(profile_id, BT_PROFILE_HAP_CLIENT_ID))
915 return btif_has_client_get_interface();
916
917 if (is_profile(profile_id, BT_KEYSTORE_ID))
918 return bluetooth::bluetooth_keystore::getBluetoothKeystoreInterface();
919
920 if (is_profile(profile_id, BT_PROFILE_LE_AUDIO_ID))
921 return btif_le_audio_get_interface();
922
923 if (is_profile(profile_id, BT_PROFILE_LE_AUDIO_BROADCASTER_ID))
924 return btif_le_audio_broadcaster_get_interface();
925
926 if (is_profile(profile_id, BT_PROFILE_VC_ID))
927 return btif_volume_control_get_interface();
928
929 if (is_profile(profile_id, BT_PROFILE_CSIS_CLIENT_ID))
930 return btif_csis_client_get_interface();
931
932 if (is_profile(profile_id, BT_BQR_ID))
933 return bluetooth::bqr::getBluetoothQualityReportInterface();
934
935 return NULL;
936 }
937
dut_mode_configure(uint8_t enable)938 int dut_mode_configure(uint8_t enable) {
939 if (!interface_ready()) return BT_STATUS_NOT_READY;
940 if (!stack_manager_get_interface()->get_stack_is_running())
941 return BT_STATUS_NOT_READY;
942
943 do_in_main_thread(FROM_HERE, base::BindOnce(btif_dut_mode_configure, enable));
944 return BT_STATUS_SUCCESS;
945 }
946
dut_mode_send(uint16_t opcode,uint8_t * buf,uint8_t len)947 int dut_mode_send(uint16_t opcode, uint8_t* buf, uint8_t len) {
948 if (!interface_ready()) return BT_STATUS_NOT_READY;
949 if (!btif_is_dut_mode()) return BT_STATUS_UNEXPECTED_STATE;
950
951 uint8_t* copy = (uint8_t*)osi_calloc(len);
952 memcpy(copy, buf, len);
953
954 do_in_main_thread(FROM_HERE,
955 base::BindOnce(
956 [](uint16_t opcode, uint8_t* buf, uint8_t len) {
957 btif_dut_mode_send(opcode, buf, len);
958 osi_free(buf);
959 },
960 opcode, copy, len));
961 return BT_STATUS_SUCCESS;
962 }
963
le_test_mode(uint16_t opcode,uint8_t * buf,uint8_t len)964 int le_test_mode(uint16_t opcode, uint8_t* buf, uint8_t len) {
965 if (!interface_ready()) return BT_STATUS_NOT_READY;
966
967 switch (opcode) {
968 case HCI_BLE_TRANSMITTER_TEST:
969 if (len != 3) return BT_STATUS_PARM_INVALID;
970 do_in_main_thread(FROM_HERE, base::BindOnce(btif_ble_transmitter_test,
971 buf[0], buf[1], buf[2]));
972 break;
973 case HCI_BLE_RECEIVER_TEST:
974 if (len != 1) return BT_STATUS_PARM_INVALID;
975 do_in_main_thread(FROM_HERE,
976 base::BindOnce(btif_ble_receiver_test, buf[0]));
977 break;
978 case HCI_BLE_TEST_END:
979 do_in_main_thread(FROM_HERE, base::BindOnce(btif_ble_test_end));
980 break;
981 default:
982 return BT_STATUS_UNSUPPORTED;
983 }
984 return BT_STATUS_SUCCESS;
985 }
986
987 static bt_os_callouts_t* wakelock_os_callouts_saved = nullptr;
988
acquire_wake_lock_cb(const char * lock_name)989 static int acquire_wake_lock_cb(const char* lock_name) {
990 return do_in_jni_thread(base::BindOnce(
991 base::IgnoreResult(wakelock_os_callouts_saved->acquire_wake_lock),
992 lock_name));
993 }
994
release_wake_lock_cb(const char * lock_name)995 static int release_wake_lock_cb(const char* lock_name) {
996 return do_in_jni_thread(base::BindOnce(
997 base::IgnoreResult(wakelock_os_callouts_saved->release_wake_lock),
998 lock_name));
999 }
1000
1001 static bt_os_callouts_t wakelock_os_callouts_jni = {
1002 sizeof(wakelock_os_callouts_jni),
1003 acquire_wake_lock_cb,
1004 release_wake_lock_cb,
1005 };
1006
set_os_callouts(bt_os_callouts_t * callouts)1007 static int set_os_callouts(bt_os_callouts_t* callouts) {
1008 wakelock_os_callouts_saved = callouts;
1009 wakelock_set_os_callouts(&wakelock_os_callouts_jni);
1010 return BT_STATUS_SUCCESS;
1011 }
1012
config_clear(void)1013 static int config_clear(void) {
1014 log::info("");
1015 int ret = BT_STATUS_SUCCESS;
1016 if (!btif_config_clear()) {
1017 log::error("Failed to clear btif config");
1018 ret = BT_STATUS_FAIL;
1019 }
1020
1021 if (!device_iot_config_clear()) {
1022 log::error("Failed to clear device iot config");
1023 ret = BT_STATUS_FAIL;
1024 }
1025
1026 return ret;
1027 }
1028
get_avrcp_service(void)1029 static bluetooth::avrcp::ServiceInterface* get_avrcp_service(void) {
1030 return bluetooth::avrcp::AvrcpService::GetServiceInterface();
1031 }
1032
obfuscate_address(const RawAddress & address)1033 static std::string obfuscate_address(const RawAddress& address) {
1034 return bluetooth::common::AddressObfuscator::GetInstance()->Obfuscate(
1035 address);
1036 }
1037
get_metric_id(const RawAddress & address)1038 static int get_metric_id(const RawAddress& address) {
1039 return allocate_metric_id_from_metric_id_allocator(address);
1040 }
1041
set_dynamic_audio_buffer_size(int codec,int size)1042 static int set_dynamic_audio_buffer_size(int codec, int size) {
1043 return btif_set_dynamic_audio_buffer_size(codec, size);
1044 }
1045
allow_low_latency_audio(bool allowed,const RawAddress &)1046 static bool allow_low_latency_audio(bool allowed,
1047 const RawAddress& /* address */) {
1048 log::info("{}", allowed);
1049 if (com::android::bluetooth::flags::a2dp_async_allow_low_latency()) {
1050 do_in_main_thread(
1051 FROM_HERE,
1052 base::BindOnce(
1053 bluetooth::audio::a2dp::set_audio_low_latency_mode_allowed,
1054 allowed));
1055 } else {
1056 bluetooth::audio::a2dp::set_audio_low_latency_mode_allowed(allowed);
1057 }
1058 return true;
1059 }
1060
metadata_changed(const RawAddress & remote_bd_addr,int key,std::vector<uint8_t> value)1061 static void metadata_changed(const RawAddress& remote_bd_addr, int key,
1062 std::vector<uint8_t> value) {
1063 if (!interface_ready()) {
1064 log::error("Interface not ready!");
1065 return;
1066 }
1067
1068 do_in_main_thread(
1069 FROM_HERE, base::BindOnce(btif_dm_metadata_changed, remote_bd_addr, key,
1070 std::move(value)));
1071 }
1072
interop_match_addr(const char * feature_name,const RawAddress * addr)1073 static bool interop_match_addr(const char* feature_name,
1074 const RawAddress* addr) {
1075 if (feature_name == NULL || addr == NULL) {
1076 return false;
1077 }
1078
1079 int feature = interop_feature_name_to_feature_id(feature_name);
1080 if (feature == -1) {
1081 log::error("feature doesn't exist: {}", feature_name);
1082 return false;
1083 }
1084
1085 return interop_match_addr((interop_feature_t)feature, addr);
1086 }
1087
interop_match_name(const char * feature_name,const char * name)1088 static bool interop_match_name(const char* feature_name, const char* name) {
1089 if (feature_name == NULL || name == NULL) {
1090 return false;
1091 }
1092
1093 int feature = interop_feature_name_to_feature_id(feature_name);
1094 if (feature == -1) {
1095 log::error("feature doesn't exist: {}", feature_name);
1096 return false;
1097 }
1098
1099 return interop_match_name((interop_feature_t)feature, name);
1100 }
1101
interop_match_addr_or_name(const char * feature_name,const RawAddress * addr)1102 static bool interop_match_addr_or_name(const char* feature_name,
1103 const RawAddress* addr) {
1104 if (feature_name == NULL || addr == NULL) {
1105 return false;
1106 }
1107
1108 int feature = interop_feature_name_to_feature_id(feature_name);
1109 if (feature == -1) {
1110 log::error("feature doesn't exist: {}", feature_name);
1111 return false;
1112 }
1113
1114 return interop_match_addr_or_name((interop_feature_t)feature, addr,
1115 &btif_storage_get_remote_device_property);
1116 }
1117
interop_database_add_remove_addr(bool do_add,const char * feature_name,const RawAddress * addr,int length)1118 static void interop_database_add_remove_addr(bool do_add,
1119 const char* feature_name,
1120 const RawAddress* addr,
1121 int length) {
1122 if (feature_name == NULL || addr == NULL) {
1123 return;
1124 }
1125
1126 int feature = interop_feature_name_to_feature_id(feature_name);
1127 if (feature == -1) {
1128 log::error("feature doesn't exist: {}", feature_name);
1129 return;
1130 }
1131
1132 if (do_add) {
1133 interop_database_add_addr((interop_feature_t)feature, addr, (size_t)length);
1134 } else {
1135 interop_database_remove_addr((interop_feature_t)feature, addr);
1136 }
1137 }
1138
interop_database_add_remove_name(bool do_add,const char * feature_name,const char * name)1139 static void interop_database_add_remove_name(bool do_add,
1140 const char* feature_name,
1141 const char* name) {
1142 if (feature_name == NULL || name == NULL) {
1143 return;
1144 }
1145
1146 int feature = interop_feature_name_to_feature_id(feature_name);
1147 if (feature == -1) {
1148 log::error("feature doesn't exist: {}", feature_name);
1149 return;
1150 }
1151
1152 if (do_add) {
1153 interop_database_add_name((interop_feature_t)feature, name);
1154 } else {
1155 interop_database_remove_name((interop_feature_t)feature, name);
1156 }
1157 }
1158
1159 EXPORT_SYMBOL bt_interface_t bluetoothInterface = {
1160 sizeof(bluetoothInterface),
1161 .init = init,
1162 .enable = enable,
1163 .disable = disable,
1164 .cleanup = cleanup,
1165 .get_adapter_properties = get_adapter_properties,
1166 .get_adapter_property = get_adapter_property,
1167 .set_adapter_property = set_adapter_property,
1168 .get_remote_device_properties = get_remote_device_properties,
1169 .get_remote_device_property = get_remote_device_property,
1170 .set_remote_device_property = set_remote_device_property,
1171 .get_remote_service_record = nullptr,
1172 .get_remote_services = get_remote_services,
1173 .start_discovery = start_discovery,
1174 .cancel_discovery = cancel_discovery,
1175 .create_bond = create_bond,
1176 .create_bond_le = create_bond_le,
1177 .create_bond_out_of_band = create_bond_out_of_band,
1178 .remove_bond = remove_bond,
1179 .cancel_bond = cancel_bond,
1180 .pairing_is_busy = pairing_is_busy,
1181 .get_connection_state = get_connection_state,
1182 .pin_reply = pin_reply,
1183 .ssp_reply = ssp_reply,
1184 .get_profile_interface = get_profile_interface,
1185 .dut_mode_configure = dut_mode_configure,
1186 .dut_mode_send = dut_mode_send,
1187 .le_test_mode = le_test_mode,
1188 .set_os_callouts = set_os_callouts,
1189 .read_energy_info = read_energy_info,
1190 .dump = dump,
1191 .dumpMetrics = dumpMetrics,
1192 .config_clear = config_clear,
1193 .interop_database_clear = interop_database_clear,
1194 .interop_database_add = interop_database_add,
1195 .get_avrcp_service = get_avrcp_service,
1196 .obfuscate_address = obfuscate_address,
1197 .get_metric_id = get_metric_id,
1198 .set_dynamic_audio_buffer_size = set_dynamic_audio_buffer_size,
1199 .generate_local_oob_data = generate_local_oob_data,
1200 .allow_low_latency_audio = allow_low_latency_audio,
1201 .clear_event_filter = clear_event_filter,
1202 .clear_event_mask = clear_event_mask,
1203 .clear_filter_accept_list = clear_filter_accept_list,
1204 .disconnect_all_acls = disconnect_all_acls,
1205 .le_rand = le_rand,
1206 .set_event_filter_inquiry_result_all_devices =
1207 set_event_filter_inquiry_result_all_devices,
1208 .set_default_event_mask_except = set_default_event_mask_except,
1209 .restore_filter_accept_list = restore_filter_accept_list,
1210 .allow_wake_by_hid = allow_wake_by_hid,
1211 .set_event_filter_connection_setup_all_devices =
1212 set_event_filter_connection_setup_all_devices,
1213 .get_wbs_supported = get_wbs_supported,
1214 .get_swb_supported = get_swb_supported,
1215 .is_coding_format_supported = is_coding_format_supported,
1216 .metadata_changed = metadata_changed,
1217 .interop_match_addr = interop_match_addr,
1218 .interop_match_name = interop_match_name,
1219 .interop_match_addr_or_name = interop_match_addr_or_name,
1220 .interop_database_add_remove_addr = interop_database_add_remove_addr,
1221 .interop_database_add_remove_name = interop_database_add_remove_name,
1222 .get_remote_pbap_pce_version = get_remote_pbap_pce_version,
1223 .pbap_pse_dynamic_version_upgrade_is_enabled =
1224 pbap_pse_dynamic_version_upgrade_is_enabled,
1225 };
1226
1227 // callback reporting helpers
1228
property_deep_copy_array(int num_properties,bt_property_t * properties)1229 bt_property_t* property_deep_copy_array(int num_properties,
1230 bt_property_t* properties) {
1231 bt_property_t* copy = nullptr;
1232 if (num_properties > 0) {
1233 size_t content_len = 0;
1234 for (int i = 0; i < num_properties; i++) {
1235 auto len = properties[i].len;
1236 if (len > 0) {
1237 content_len += len;
1238 }
1239 }
1240
1241 copy = (bt_property_t*)osi_calloc((sizeof(bt_property_t) * num_properties) +
1242 content_len);
1243 log::assert_that(copy != nullptr, "assert failed: copy != nullptr");
1244 uint8_t* content = (uint8_t*)(copy + num_properties);
1245
1246 for (int i = 0; i < num_properties; i++) {
1247 auto len = properties[i].len;
1248 copy[i].type = properties[i].type;
1249 copy[i].len = len;
1250 if (len <= 0) {
1251 continue;
1252 }
1253 copy[i].val = content;
1254 memcpy(content, properties[i].val, len);
1255 content += len;
1256 }
1257 }
1258 return copy;
1259 }
1260
invoke_adapter_state_changed_cb(bt_state_t state)1261 void invoke_adapter_state_changed_cb(bt_state_t state) {
1262 do_in_jni_thread(base::BindOnce(
1263 [](bt_state_t state) {
1264 HAL_CBACK(bt_hal_cbacks, adapter_state_changed_cb, state);
1265 },
1266 state));
1267 }
1268
invoke_adapter_properties_cb(bt_status_t status,int num_properties,bt_property_t * properties)1269 void invoke_adapter_properties_cb(bt_status_t status, int num_properties,
1270 bt_property_t* properties) {
1271 do_in_jni_thread(base::BindOnce(
1272 [](bt_status_t status, int num_properties, bt_property_t* properties) {
1273 HAL_CBACK(bt_hal_cbacks, adapter_properties_cb, status, num_properties,
1274 properties);
1275 if (properties) {
1276 osi_free(properties);
1277 }
1278 },
1279 status, num_properties,
1280 property_deep_copy_array(num_properties, properties)));
1281 }
1282
invoke_remote_device_properties_cb(bt_status_t status,RawAddress bd_addr,int num_properties,bt_property_t * properties)1283 void invoke_remote_device_properties_cb(bt_status_t status, RawAddress bd_addr,
1284 int num_properties,
1285 bt_property_t* properties) {
1286 do_in_jni_thread(base::BindOnce(
1287 [](bt_status_t status, RawAddress bd_addr, int num_properties,
1288 bt_property_t* properties) {
1289 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb, status, &bd_addr,
1290 num_properties, properties);
1291 if (properties) {
1292 osi_free(properties);
1293 }
1294 },
1295 status, bd_addr, num_properties,
1296 property_deep_copy_array(num_properties, properties)));
1297 }
1298
invoke_device_found_cb(int num_properties,bt_property_t * properties)1299 void invoke_device_found_cb(int num_properties, bt_property_t* properties) {
1300 do_in_jni_thread(base::BindOnce(
1301 [](int num_properties, bt_property_t* properties) {
1302 HAL_CBACK(bt_hal_cbacks, device_found_cb, num_properties, properties);
1303 if (properties) {
1304 osi_free(properties);
1305 }
1306 },
1307 num_properties, property_deep_copy_array(num_properties, properties)));
1308 }
1309
invoke_discovery_state_changed_cb(bt_discovery_state_t state)1310 void invoke_discovery_state_changed_cb(bt_discovery_state_t state) {
1311 do_in_jni_thread(base::BindOnce(
1312 [](bt_discovery_state_t state) {
1313 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb, state);
1314 },
1315 state));
1316 }
1317
invoke_pin_request_cb(RawAddress bd_addr,bt_bdname_t bd_name,uint32_t cod,bool min_16_digit)1318 void invoke_pin_request_cb(RawAddress bd_addr, bt_bdname_t bd_name,
1319 uint32_t cod, bool min_16_digit) {
1320 do_in_jni_thread(base::BindOnce(
1321 [](RawAddress bd_addr, bt_bdname_t bd_name, uint32_t cod,
1322 bool min_16_digit) {
1323 HAL_CBACK(bt_hal_cbacks, pin_request_cb, &bd_addr, &bd_name, cod,
1324 min_16_digit);
1325 },
1326 bd_addr, bd_name, cod, min_16_digit));
1327 }
1328
invoke_ssp_request_cb(RawAddress bd_addr,bt_ssp_variant_t pairing_variant,uint32_t pass_key)1329 void invoke_ssp_request_cb(RawAddress bd_addr, bt_ssp_variant_t pairing_variant,
1330 uint32_t pass_key) {
1331 do_in_jni_thread(base::BindOnce(
1332 [](RawAddress bd_addr, bt_ssp_variant_t pairing_variant,
1333 uint32_t pass_key) {
1334 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, pairing_variant,
1335 pass_key);
1336 },
1337 bd_addr, pairing_variant, pass_key));
1338 }
1339
invoke_oob_data_request_cb(tBT_TRANSPORT t,bool valid,Octet16 c,Octet16 r,RawAddress raw_address,uint8_t address_type)1340 void invoke_oob_data_request_cb(tBT_TRANSPORT t, bool valid, Octet16 c,
1341 Octet16 r, RawAddress raw_address,
1342 uint8_t address_type) {
1343 log::info("");
1344 bt_oob_data_t oob_data = {};
1345 const char* local_name;
1346 if (get_btm_client_interface().local.BTM_ReadLocalDeviceName(&local_name) !=
1347 BTM_SUCCESS) {
1348 log::warn("Unable to read local device name");
1349 }
1350 for (int i = 0; i < BD_NAME_LEN; i++) {
1351 oob_data.device_name[i] = local_name[i];
1352 }
1353
1354 // Set the local address
1355 int j = 5;
1356 for (int i = 0; i < 6; i++) {
1357 oob_data.address[i] = raw_address.address[j];
1358 j--;
1359 }
1360 oob_data.address[6] = address_type;
1361
1362 // Each value (for C and R) is 16 octets in length
1363 bool c_empty = true;
1364 for (int i = 0; i < 16; i++) {
1365 // C cannot be all 0s, if so then we want to fail
1366 if (c[i] != 0) c_empty = false;
1367 oob_data.c[i] = c[i];
1368 // R is optional and may be empty
1369 oob_data.r[i] = r[i];
1370 }
1371 oob_data.is_valid = valid && !c_empty;
1372 // The oob_data_length is 2 octects in length. The value includes the length
1373 // of itself. 16 + 16 + 2 = 34 Data 0x0022 Little Endian order 0x2200
1374 oob_data.oob_data_length[0] = 0;
1375 oob_data.oob_data_length[1] = 34;
1376 bt_status_t status = do_in_jni_thread(base::BindOnce(
1377 [](tBT_TRANSPORT t, bt_oob_data_t oob_data) {
1378 HAL_CBACK(bt_hal_cbacks, generate_local_oob_data_cb, t, oob_data);
1379 },
1380 t, oob_data));
1381 if (status != BT_STATUS_SUCCESS) {
1382 log::error("Failed to call callback!");
1383 }
1384 }
1385
invoke_bond_state_changed_cb(bt_status_t status,RawAddress bd_addr,bt_bond_state_t state,int fail_reason)1386 void invoke_bond_state_changed_cb(bt_status_t status, RawAddress bd_addr,
1387 bt_bond_state_t state, int fail_reason) {
1388 do_in_jni_thread(base::BindOnce(
1389 [](bt_status_t status, RawAddress bd_addr, bt_bond_state_t state,
1390 int fail_reason) {
1391 HAL_CBACK(bt_hal_cbacks, bond_state_changed_cb, status, &bd_addr, state,
1392 fail_reason);
1393 },
1394 status, bd_addr, state, fail_reason));
1395 }
1396
invoke_address_consolidate_cb(RawAddress main_bd_addr,RawAddress secondary_bd_addr)1397 void invoke_address_consolidate_cb(RawAddress main_bd_addr,
1398 RawAddress secondary_bd_addr) {
1399 do_in_jni_thread(base::BindOnce(
1400 [](RawAddress main_bd_addr, RawAddress secondary_bd_addr) {
1401 HAL_CBACK(bt_hal_cbacks, address_consolidate_cb, &main_bd_addr,
1402 &secondary_bd_addr);
1403 },
1404 main_bd_addr, secondary_bd_addr));
1405 }
1406
invoke_le_address_associate_cb(RawAddress main_bd_addr,RawAddress secondary_bd_addr)1407 void invoke_le_address_associate_cb(RawAddress main_bd_addr,
1408 RawAddress secondary_bd_addr) {
1409 do_in_jni_thread(base::BindOnce(
1410 [](RawAddress main_bd_addr, RawAddress secondary_bd_addr) {
1411 HAL_CBACK(bt_hal_cbacks, le_address_associate_cb, &main_bd_addr,
1412 &secondary_bd_addr);
1413 },
1414 main_bd_addr, secondary_bd_addr));
1415 }
invoke_acl_state_changed_cb(bt_status_t status,RawAddress bd_addr,bt_acl_state_t state,int transport_link_type,bt_hci_error_code_t hci_reason,bt_conn_direction_t direction,uint16_t acl_handle)1416 void invoke_acl_state_changed_cb(bt_status_t status, RawAddress bd_addr,
1417 bt_acl_state_t state, int transport_link_type,
1418 bt_hci_error_code_t hci_reason,
1419 bt_conn_direction_t direction,
1420 uint16_t acl_handle) {
1421 do_in_jni_thread(base::BindOnce(
1422 [](bt_status_t status, RawAddress bd_addr, bt_acl_state_t state,
1423 int transport_link_type, bt_hci_error_code_t hci_reason,
1424 bt_conn_direction_t direction, uint16_t acl_handle) {
1425 HAL_CBACK(bt_hal_cbacks, acl_state_changed_cb, status, &bd_addr, state,
1426 transport_link_type, hci_reason, direction, acl_handle);
1427 },
1428 status, bd_addr, state, transport_link_type, hci_reason, direction,
1429 acl_handle));
1430 }
1431
invoke_thread_evt_cb(bt_cb_thread_evt event)1432 void invoke_thread_evt_cb(bt_cb_thread_evt event) {
1433 do_in_jni_thread(base::BindOnce(
1434 [](bt_cb_thread_evt event) {
1435 HAL_CBACK(bt_hal_cbacks, thread_evt_cb, event);
1436 if (event == DISASSOCIATE_JVM) {
1437 bt_hal_cbacks = NULL;
1438 }
1439 },
1440 event));
1441 }
1442
invoke_le_test_mode_cb(bt_status_t status,uint16_t count)1443 void invoke_le_test_mode_cb(bt_status_t status, uint16_t count) {
1444 do_in_jni_thread(base::BindOnce(
1445 [](bt_status_t status, uint16_t count) {
1446 HAL_CBACK(bt_hal_cbacks, le_test_mode_cb, status, count);
1447 },
1448 status, count));
1449 }
1450
1451 // takes ownership of |uid_data|
invoke_energy_info_cb(bt_activity_energy_info energy_info,bt_uid_traffic_t * uid_data)1452 void invoke_energy_info_cb(bt_activity_energy_info energy_info,
1453 bt_uid_traffic_t* uid_data) {
1454 do_in_jni_thread(base::BindOnce(
1455 [](bt_activity_energy_info energy_info, bt_uid_traffic_t* uid_data) {
1456 HAL_CBACK(bt_hal_cbacks, energy_info_cb, &energy_info, uid_data);
1457 osi_free(uid_data);
1458 },
1459 energy_info, uid_data));
1460 }
1461
invoke_link_quality_report_cb(uint64_t timestamp,int report_id,int rssi,int snr,int retransmission_count,int packets_not_receive_count,int negative_acknowledgement_count)1462 void invoke_link_quality_report_cb(uint64_t timestamp, int report_id, int rssi,
1463 int snr, int retransmission_count,
1464 int packets_not_receive_count,
1465 int negative_acknowledgement_count) {
1466 do_in_jni_thread(
1467 base::BindOnce(
1468 [](uint64_t timestamp, int report_id, int rssi, int snr,
1469 int retransmission_count, int packets_not_receive_count,
1470 int negative_acknowledgement_count) {
1471 HAL_CBACK(bt_hal_cbacks, link_quality_report_cb, timestamp,
1472 report_id, rssi, snr, retransmission_count,
1473 packets_not_receive_count,
1474 negative_acknowledgement_count);
1475 },
1476 timestamp, report_id, rssi, snr, retransmission_count,
1477 packets_not_receive_count, negative_acknowledgement_count));
1478 }
1479
invoke_switch_buffer_size_cb(bool is_low_latency_buffer_size)1480 void invoke_switch_buffer_size_cb(bool is_low_latency_buffer_size) {
1481 do_in_jni_thread(base::BindOnce(
1482 [](bool is_low_latency_buffer_size) {
1483 HAL_CBACK(bt_hal_cbacks, switch_buffer_size_cb,
1484 is_low_latency_buffer_size);
1485 },
1486 is_low_latency_buffer_size));
1487 }
1488
invoke_switch_codec_cb(bool is_low_latency_buffer_size)1489 void invoke_switch_codec_cb(bool is_low_latency_buffer_size) {
1490 do_in_jni_thread(base::BindOnce(
1491 [](bool is_low_latency_buffer_size) {
1492 HAL_CBACK(bt_hal_cbacks, switch_codec_cb, is_low_latency_buffer_size);
1493 },
1494 is_low_latency_buffer_size));
1495 }
1496
invoke_key_missing_cb(RawAddress bd_addr)1497 void invoke_key_missing_cb(RawAddress bd_addr) {
1498 do_in_jni_thread(base::BindOnce(
1499 [](RawAddress bd_addr) {
1500 HAL_CBACK(bt_hal_cbacks, key_missing_cb, bd_addr);
1501 },
1502 bd_addr));
1503 }
1504
1505 namespace bluetooth::testing {
set_hal_cbacks(bt_callbacks_t * callbacks)1506 void set_hal_cbacks(bt_callbacks_t* callbacks) { ::set_hal_cbacks(callbacks); }
1507
1508 } // namespace bluetooth::testing
1509