blob: 58d6da0c1d92b256f3f8777f58b4e449f5b9bb2d [file] [log] [blame]
The Android Open Source Project624a7872012-12-12 16:00:35 -08001/******************************************************************************
2 *
Jakub Pawlowski3b10fdd2017-09-18 09:00:20 -07003 * Copyright 2009-2012 Broadcom Corporation
The Android Open Source Project624a7872012-12-12 16:00:35 -08004 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 ******************************************************************************/
18
Myles Watsonfbfa1a52016-11-23 14:49:54 -080019/*******************************************************************************
The Android Open Source Project624a7872012-12-12 16:00:35 -080020 *
21 * Filename: bluetooth.c
22 *
23 * Description: Bluetooth HAL implementation
24 *
Myles Watsonfbfa1a52016-11-23 14:49:54 -080025 ******************************************************************************/
The Android Open Source Project624a7872012-12-12 16:00:35 -080026
Andre Eisenbach896aa432015-10-29 11:18:20 -070027#define LOG_TAG "bt_btif"
Marie Janssene06e1512015-07-08 11:48:57 -070028
Jack He358dd112016-12-13 01:59:12 -080029#include <base/logging.h>
The Android Open Source Project624a7872012-12-12 16:00:35 -080030#include <hardware/bluetooth.h>
Jack Hee43b2372017-12-14 15:00:39 -080031#include <hardware/bluetooth_headset_interface.h>
Marie Janssene06e1512015-07-08 11:48:57 -070032#include <hardware/bt_av.h>
Łukasz Rymanowski06a5be22020-12-15 09:14:51 +000033#include <hardware/bt_csis.h>
Marie Janssene06e1512015-07-08 11:48:57 -070034#include <hardware/bt_gatt.h>
Jakub Tyszkowskib87fc992021-04-08 15:08:35 +000035#include <hardware/bt_has.h>
Hemant Gupta28425222014-04-18 12:34:55 +053036#include <hardware/bt_hd.h>
Jakub Pawlowski7d06e572018-02-28 22:44:42 -080037#include <hardware/bt_hearing_aid.h>
Hemant Guptad9e9cf92013-08-19 18:33:01 +053038#include <hardware/bt_hf_client.h>
The Android Open Source Project624a7872012-12-12 16:00:35 -080039#include <hardware/bt_hh.h>
Grzegorz Kołodziejczyk177328a2020-11-30 13:51:05 +010040#include <hardware/bt_le_audio.h>
Marie Janssene06e1512015-07-08 11:48:57 -070041#include <hardware/bt_pan.h>
Ravi Nagarajan78b35202013-03-06 05:29:48 -080042#include <hardware/bt_rc.h>
Kim Schulzb2337fe2015-03-25 10:39:40 +010043#include <hardware/bt_sdp.h>
Marie Janssene06e1512015-07-08 11:48:57 -070044#include <hardware/bt_sock.h>
Łukasz Rymanowski35b08362021-01-29 09:05:10 +000045#include <hardware/bt_vc.h>
Jack He028bf632020-08-04 01:28:36 -070046#include <stdio.h>
47#include <stdlib.h>
48#include <string.h>
49#include <unistd.h>
The Android Open Source Project624a7872012-12-12 16:00:35 -080050
Chen Chen2fb6ce32022-01-31 15:50:01 -080051#include "audio_hal_interface/a2dp_encoding.h"
Marie Janssene06e1512015-07-08 11:48:57 -070052#include "bt_utils.h"
Łukasz Rymanowskid853b3a2021-10-07 16:58:07 +000053#include "bta/include/bta_csis_api.h"
Jakub Tyszkowskib87fc992021-04-08 15:08:35 +000054#include "bta/include/bta_has_api.h"
Hansong Zhangc5df6b02018-05-09 14:39:56 -070055#include "bta/include/bta_hearing_aid_api.h"
Sanket Agarwald338a642016-11-30 19:43:47 -080056#include "bta/include/bta_hf_client_api.h"
Grzegorz Kołodziejczyk926fd432021-10-06 11:54:53 +000057#include "bta/include/bta_le_audio_api.h"
Jakub Tyszkowski86048262021-12-13 12:32:59 +010058#include "bta/include/bta_le_audio_broadcaster_api.h"
Łukasz Rymanowski1cf59992022-03-07 09:19:18 +000059#include "bta/include/bta_vc_api.h"
Jakub Pawlowski4533a272018-05-25 09:31:26 -070060#include "btif/avrcp/avrcp_service.h"
Chris Manton813de982021-06-08 22:54:06 -070061#include "btif/include/stack_manager.h"
Pavlin Radoslavovcd102012016-09-23 16:36:47 -070062#include "btif_a2dp.h"
Michael Sun7cc8f122020-11-23 18:54:29 +000063#include "btif_activity_attribution.h"
The Android Open Source Project624a7872012-12-12 16:00:35 -080064#include "btif_api.h"
Pavlin Radoslavov5c6e64c2017-11-24 19:12:11 -080065#include "btif_av.h"
Ray4a73d962019-01-03 21:19:16 +080066#include "btif_bqr.h"
Myles Watson3f68a752016-10-19 09:50:22 -070067#include "btif_config.h"
Jack Hee43b2372017-12-14 15:00:39 -080068#include "btif_debug_conn.h"
69#include "btif_hf.h"
weichinweng6033dfb2020-04-21 09:42:27 +080070#include "btif_keystore.h"
Chen Chen534bf242021-03-10 15:41:42 -080071#include "btif_metrics_logging.h"
Myles Watson3f68a752016-10-19 09:50:22 -070072#include "btif_storage.h"
Jack He9183fd32018-11-16 02:59:43 -080073#include "common/address_obfuscator.h"
Chen Chen88ca6ae2020-03-05 15:02:46 -080074#include "common/metric_id_allocator.h"
Jack He42823982018-08-15 12:38:37 -070075#include "common/metrics.h"
weichinweng5afd2892021-03-05 14:12:09 +080076#include "common/os_utils.h"
Andre Eisenbach35c0c1d62016-01-14 02:00:24 -080077#include "device/include/interop.h"
Zach Johnson8ba246e2020-05-22 10:02:41 -070078#include "gd/common/init_flags.h"
weichinweng08b6b582021-09-23 11:41:52 +080079#include "gd/os/parameter_provider.h"
Chris Mantoned916602020-01-13 21:11:48 -080080#include "main/shim/dumpsys.h"
Chris Mantonb0ecaa72019-09-11 19:53:32 -070081#include "main/shim/shim.h"
Pavlin Radoslavov30698fa2015-12-04 17:36:34 -080082#include "osi/include/alarm.h"
Myles Watson3f68a752016-10-19 09:50:22 -070083#include "osi/include/allocation_tracker.h"
Chris Manton1fd02612021-08-28 16:03:54 -070084#include "osi/include/allocator.h"
Sharvil Nanavati83951772014-12-23 23:08:58 -080085#include "osi/include/log.h"
Marie Janssene06e1512015-07-08 11:48:57 -070086#include "osi/include/osi.h"
Pavlin Radoslavov5bb6cac2015-08-24 16:29:21 -070087#include "osi/include/wakelock.h"
Jakub Pawlowski6037e032018-11-13 15:41:42 +010088#include "stack/gatt/connection_manager.h"
Chris Mantona87686c2021-01-30 15:59:29 -080089#include "stack/include/avdt_api.h"
Martin Brabhamafea75b2021-06-30 12:08:02 -070090#include "stack/include/btm_api.h"
Zach Johnsonaf85a8b2020-08-14 17:33:35 -070091#include "stack/include/btu.h"
Chris Manton2a9d9f22021-09-10 23:05:47 -070092#include "types/raw_address.h"
The Android Open Source Project624a7872012-12-12 16:00:35 -080093
Łukasz Rymanowski06a5be22020-12-15 09:14:51 +000094using bluetooth::csis::CsisClientInterface;
Jakub Tyszkowskib87fc992021-04-08 15:08:35 +000095using bluetooth::has::HasClientInterface;
Jakub Pawlowski7d06e572018-02-28 22:44:42 -080096using bluetooth::hearing_aid::HearingAidInterface;
Abhishek Pandit-Subedi83a3fa82022-01-28 17:08:55 -080097#ifndef TARGET_FLOSS
Jakub Tyszkowski86048262021-12-13 12:32:59 +010098using bluetooth::le_audio::LeAudioBroadcasterInterface;
Grzegorz Kołodziejczyk177328a2020-11-30 13:51:05 +010099using bluetooth::le_audio::LeAudioClientInterface;
Abhishek Pandit-Subedi83a3fa82022-01-28 17:08:55 -0800100#endif
Łukasz Rymanowski35b08362021-01-29 09:05:10 +0000101using bluetooth::vc::VolumeControlInterface;
Jakub Pawlowski7d06e572018-02-28 22:44:42 -0800102
Myles Watsonfbfa1a52016-11-23 14:49:54 -0800103/*******************************************************************************
Myles Watson3f68a752016-10-19 09:50:22 -0700104 * Static variables
Myles Watsonfbfa1a52016-11-23 14:49:54 -0800105 ******************************************************************************/
The Android Open Source Project624a7872012-12-12 16:00:35 -0800106
Zach Johnsond6e10362020-08-14 16:58:12 -0700107static bt_callbacks_t* bt_hal_cbacks = NULL;
Marie Janssen0fc1f132016-06-22 12:52:19 -0700108bool restricted_mode = false;
weichinweng8cd854f2021-03-04 09:11:30 +0800109bool common_criteria_mode = false;
weichinweng7107d8f2020-04-21 10:26:26 +0800110const int CONFIG_COMPARE_ALL_PASS = 0b11;
weichinweng8cd854f2021-03-04 09:11:30 +0800111int common_criteria_config_compare_result = CONFIG_COMPARE_ALL_PASS;
Rahul Sabnis0c983e22020-07-31 19:44:27 -0700112bool is_local_device_atv = false;
The Android Open Source Project624a7872012-12-12 16:00:35 -0800113
Myles Watsonfbfa1a52016-11-23 14:49:54 -0800114/*******************************************************************************
Myles Watson3f68a752016-10-19 09:50:22 -0700115 * Externs
Myles Watsonfbfa1a52016-11-23 14:49:54 -0800116 ******************************************************************************/
The Android Open Source Project624a7872012-12-12 16:00:35 -0800117
118/* list all extended interfaces here */
119
Hemant Guptad9e9cf92013-08-19 18:33:01 +0530120/* handsfree profile - client */
Jack He13d8b742017-12-04 14:30:42 -0800121extern const bthf_client_interface_t* btif_hf_client_get_interface();
The Android Open Source Project624a7872012-12-12 16:00:35 -0800122/* advanced audio profile */
Jack He13d8b742017-12-04 14:30:42 -0800123extern const btav_source_interface_t* btif_av_get_src_interface();
124extern const btav_sink_interface_t* btif_av_get_sink_interface();
The Android Open Source Project624a7872012-12-12 16:00:35 -0800125/*rfc l2cap*/
Jack He13d8b742017-12-04 14:30:42 -0800126extern const btsock_interface_t* btif_sock_get_interface();
The Android Open Source Project624a7872012-12-12 16:00:35 -0800127/* hid host profile */
Jack He13d8b742017-12-04 14:30:42 -0800128extern const bthh_interface_t* btif_hh_get_interface();
Hemant Gupta28425222014-04-18 12:34:55 +0530129/* hid device profile */
Jack He13d8b742017-12-04 14:30:42 -0800130extern const bthd_interface_t* btif_hd_get_interface();
The Android Open Source Project624a7872012-12-12 16:00:35 -0800131/*pan*/
Jack He13d8b742017-12-04 14:30:42 -0800132extern const btpan_interface_t* btif_pan_get_interface();
Ganesh Ganapathi Batta1def2552013-02-05 15:22:31 -0800133/* gatt */
Myles Watson3f68a752016-10-19 09:50:22 -0700134extern const btgatt_interface_t* btif_gatt_get_interface();
Mike Lockwoodc02ef772014-06-02 16:21:49 -0700135/* avrc target */
Jack He13d8b742017-12-04 14:30:42 -0800136extern const btrc_interface_t* btif_rc_get_interface();
Mike Lockwoodc02ef772014-06-02 16:21:49 -0700137/* avrc controller */
Jack He13d8b742017-12-04 14:30:42 -0800138extern const btrc_ctrl_interface_t* btif_rc_ctrl_get_interface();
Kim Schulzb2337fe2015-03-25 10:39:40 +0100139/*SDP search client*/
Jack He13d8b742017-12-04 14:30:42 -0800140extern const btsdp_interface_t* btif_sdp_get_interface();
Jakub Pawlowski7d06e572018-02-28 22:44:42 -0800141/*Hearing Aid client*/
142extern HearingAidInterface* btif_hearing_aid_get_interface();
Abhishek Pandit-Subedi83a3fa82022-01-28 17:08:55 -0800143#ifndef TARGET_FLOSS
Jakub Tyszkowskib87fc992021-04-08 15:08:35 +0000144/* Hearing Access client */
145extern HasClientInterface* btif_has_client_get_interface();
Grzegorz Kołodziejczyk177328a2020-11-30 13:51:05 +0100146/* LeAudio testi client */
147extern LeAudioClientInterface* btif_le_audio_get_interface();
Jakub Tyszkowski86048262021-12-13 12:32:59 +0100148/* LeAudio Broadcaster */
149extern LeAudioBroadcasterInterface* btif_le_audio_broadcaster_get_interface();
Abhishek Pandit-Subedi83a3fa82022-01-28 17:08:55 -0800150#endif
Łukasz Rymanowski06a5be22020-12-15 09:14:51 +0000151/* Coordinated Set Service Client */
152extern CsisClientInterface* btif_csis_client_get_interface();
Łukasz Rymanowski35b08362021-01-29 09:05:10 +0000153/* Volume Control client */
154extern VolumeControlInterface* btif_volume_control_get_interface();
The Android Open Source Project624a7872012-12-12 16:00:35 -0800155
Myles Watsonfbfa1a52016-11-23 14:49:54 -0800156/*******************************************************************************
Myles Watson3f68a752016-10-19 09:50:22 -0700157 * Functions
Myles Watsonfbfa1a52016-11-23 14:49:54 -0800158 ******************************************************************************/
The Android Open Source Project624a7872012-12-12 16:00:35 -0800159
Myles Watson3f68a752016-10-19 09:50:22 -0700160static bool interface_ready(void) { return bt_hal_cbacks != NULL; }
Chris Mantonf93fece2021-04-08 16:16:53 -0700161void set_hal_cbacks(bt_callbacks_t* callbacks) { bt_hal_cbacks = callbacks; }
The Android Open Source Project624a7872012-12-12 16:00:35 -0800162
Myles Watson3f68a752016-10-19 09:50:22 -0700163static bool is_profile(const char* p1, const char* p2) {
Jack He358dd112016-12-13 01:59:12 -0800164 CHECK(p1);
165 CHECK(p2);
Andre Eisenbach80c23e52016-02-17 12:43:12 -0800166 return strlen(p1) == strlen(p2) && strncmp(p1, p2, strlen(p2)) == 0;
167}
168
The Android Open Source Project624a7872012-12-12 16:00:35 -0800169/*****************************************************************************
Myles Watson3f68a752016-10-19 09:50:22 -0700170 *
171 * BLUETOOTH HAL INTERFACE FUNCTIONS
172 *
173 ****************************************************************************/
The Android Open Source Project624a7872012-12-12 16:00:35 -0800174
William Escandeb94e4d72022-05-27 12:03:36 -0700175const std::vector<std::string> get_allowed_bt_package_name(void);
176void handle_migration(const std::string& dst,
177 const std::vector<std::string>& allowed_bt_package_name);
178
Martin Brabhame02a8be2019-05-10 12:42:15 -0700179static int init(bt_callbacks_t* callbacks, bool start_restricted,
weichinweng8cd854f2021-03-04 09:11:30 +0800180 bool is_common_criteria_mode, int config_compare_result,
William Escandeb94e4d72022-05-27 12:03:36 -0700181 const char** init_flags, bool is_atv,
182 const char* user_data_directory) {
weichinweng8cd854f2021-03-04 09:11:30 +0800183 LOG_INFO(
184 "%s: start restricted = %d ; common criteria mode = %d, config compare "
185 "result = %d",
186 __func__, start_restricted, is_common_criteria_mode,
187 config_compare_result);
The Android Open Source Project624a7872012-12-12 16:00:35 -0800188
William Escandeb94e4d72022-05-27 12:03:36 -0700189 if (user_data_directory != nullptr) {
190 handle_migration(std::string(user_data_directory),
191 get_allowed_bt_package_name());
192 }
193
Zach Johnson8ba246e2020-05-22 10:02:41 -0700194 bluetooth::common::InitFlags::Load(init_flags);
Chris Mantonb0ecaa72019-09-11 19:53:32 -0700195
Myles Watson3f68a752016-10-19 09:50:22 -0700196 if (interface_ready()) return BT_STATUS_DONE;
The Android Open Source Project624a7872012-12-12 16:00:35 -0800197
Sharvil Nanavati19f4b182014-11-13 01:04:19 -0800198#ifdef BLUEDROID_DEBUG
199 allocation_tracker_init();
200#endif
201
Chris Mantonf93fece2021-04-08 16:16:53 -0700202 set_hal_cbacks(callbacks);
203
Martin Brabhame02a8be2019-05-10 12:42:15 -0700204 restricted_mode = start_restricted;
weichinweng08b6b582021-09-23 11:41:52 +0800205
Chris Manton11ca0bb2021-10-01 13:54:42 -0700206 bluetooth::os::ParameterProvider::SetBtKeystoreInterface(
207 bluetooth::bluetooth_keystore::getBluetoothKeystoreInterface());
208 bluetooth::os::ParameterProvider::SetCommonCriteriaMode(
209 is_common_criteria_mode);
210 if (is_bluetooth_uid() && is_common_criteria_mode) {
211 bluetooth::os::ParameterProvider::SetCommonCriteriaConfigCompareResult(
212 config_compare_result);
weichinweng08b6b582021-09-23 11:41:52 +0800213 } else {
Chris Manton11ca0bb2021-10-01 13:54:42 -0700214 bluetooth::os::ParameterProvider::SetCommonCriteriaConfigCompareResult(
215 CONFIG_COMPARE_ALL_PASS);
weichinweng08b6b582021-09-23 11:41:52 +0800216 }
217
Rahul Sabnis0c983e22020-07-31 19:44:27 -0700218 is_local_device_atv = is_atv;
weichinweng7107d8f2020-04-21 10:26:26 +0800219
Zach Johnsoned9491b2014-09-09 16:31:14 -0700220 stack_manager_get_interface()->init_stack();
221 return BT_STATUS_SUCCESS;
The Android Open Source Project624a7872012-12-12 16:00:35 -0800222}
223
Martin Brabhame02a8be2019-05-10 12:42:15 -0700224static int enable() {
Myles Watson3f68a752016-10-19 09:50:22 -0700225 if (!interface_ready()) return BT_STATUS_NOT_READY;
The Android Open Source Project624a7872012-12-12 16:00:35 -0800226
Zach Johnsoned9491b2014-09-09 16:31:14 -0700227 stack_manager_get_interface()->start_up_stack_async();
228 return BT_STATUS_SUCCESS;
The Android Open Source Project624a7872012-12-12 16:00:35 -0800229}
230
Zach Johnsoned9491b2014-09-09 16:31:14 -0700231static int disable(void) {
Myles Watson3f68a752016-10-19 09:50:22 -0700232 if (!interface_ready()) return BT_STATUS_NOT_READY;
The Android Open Source Project624a7872012-12-12 16:00:35 -0800233
Zach Johnsoned9491b2014-09-09 16:31:14 -0700234 stack_manager_get_interface()->shut_down_stack_async();
235 return BT_STATUS_SUCCESS;
The Android Open Source Project624a7872012-12-12 16:00:35 -0800236}
237
Myles Watson3f68a752016-10-19 09:50:22 -0700238static void cleanup(void) { stack_manager_get_interface()->clean_up_stack(); }
239
240bool is_restricted_mode() { return restricted_mode; }
weichinweng5afd2892021-03-05 14:12:09 +0800241bool is_common_criteria_mode() {
242 return is_bluetooth_uid() && common_criteria_mode;
243}
weichinweng8cd854f2021-03-04 09:11:30 +0800244// if common criteria mode disable, will always return
245// CONFIG_COMPARE_ALL_PASS(0b11) indicate don't check config checksum.
246int get_common_criteria_config_compare_result() {
weichinweng5afd2892021-03-05 14:12:09 +0800247 return is_common_criteria_mode() ? common_criteria_config_compare_result
248 : CONFIG_COMPARE_ALL_PASS;
weichinweng7107d8f2020-04-21 10:26:26 +0800249}
Myles Watson3f68a752016-10-19 09:50:22 -0700250
Rahul Sabnis0c983e22020-07-31 19:44:27 -0700251bool is_atv_device() { return is_local_device_atv; }
252
Myles Watson3f68a752016-10-19 09:50:22 -0700253static int get_adapter_properties(void) {
Zach Johnson3f7946d2020-08-14 10:11:14 -0700254 if (!btif_is_enabled()) return BT_STATUS_NOT_READY;
Myles Watson3f68a752016-10-19 09:50:22 -0700255
Zach Johnsonaf85a8b2020-08-14 17:33:35 -0700256 do_in_main_thread(FROM_HERE, base::BindOnce(btif_get_adapter_properties));
Zach Johnson3f7946d2020-08-14 10:11:14 -0700257 return BT_STATUS_SUCCESS;
The Android Open Source Project624a7872012-12-12 16:00:35 -0800258}
259
Myles Watson3f68a752016-10-19 09:50:22 -0700260static int get_adapter_property(bt_property_type_t type) {
Zach Johnson1ee34862020-08-14 10:23:10 -0700261 /* Allow get_adapter_property only for BDADDR and BDNAME if BT is disabled */
262 if (!btif_is_enabled() && (type != BT_PROPERTY_BDADDR) &&
263 (type != BT_PROPERTY_BDNAME) && (type != BT_PROPERTY_CLASS_OF_DEVICE))
264 return BT_STATUS_NOT_READY;
Myles Watson3f68a752016-10-19 09:50:22 -0700265
Zach Johnsonaf85a8b2020-08-14 17:33:35 -0700266 do_in_main_thread(FROM_HERE, base::BindOnce(btif_get_adapter_property, type));
Zach Johnson1ee34862020-08-14 10:23:10 -0700267 return BT_STATUS_SUCCESS;
Ajay Panickerc796ac82016-03-17 17:09:24 -0700268}
269
Myles Watson3f68a752016-10-19 09:50:22 -0700270static int set_adapter_property(const bt_property_t* property) {
Zach Johnsonf0f1c0c2020-08-14 11:24:35 -0700271 if (!btif_is_enabled()) return BT_STATUS_NOT_READY;
The Android Open Source Project624a7872012-12-12 16:00:35 -0800272
Zach Johnsonf0f1c0c2020-08-14 11:24:35 -0700273 switch (property->type) {
274 case BT_PROPERTY_BDNAME:
275 case BT_PROPERTY_ADAPTER_SCAN_MODE:
Katherine Laib68a4b52022-02-14 18:41:22 +0000276 case BT_PROPERTY_ADAPTER_DISCOVERABLE_TIMEOUT:
Zach Johnsonf0f1c0c2020-08-14 11:24:35 -0700277 case BT_PROPERTY_CLASS_OF_DEVICE:
278 case BT_PROPERTY_LOCAL_IO_CAPS:
279 case BT_PROPERTY_LOCAL_IO_CAPS_BLE:
280 break;
281 default:
282 return BT_STATUS_FAIL;
283 }
284
Zach Johnsonaf85a8b2020-08-14 17:33:35 -0700285 do_in_main_thread(FROM_HERE, base::BindOnce(
286 [](bt_property_t* property) {
287 btif_set_adapter_property(property);
288 osi_free(property);
289 },
290 property_deep_copy(property)));
Zach Johnsonf0f1c0c2020-08-14 11:24:35 -0700291 return BT_STATUS_SUCCESS;
The Android Open Source Project624a7872012-12-12 16:00:35 -0800292}
293
Jakub Pawlowskid8e815c2017-06-24 17:30:18 -0700294int get_remote_device_properties(RawAddress* remote_addr) {
Zach Johnson28d5bde2020-08-14 12:07:19 -0700295 if (!btif_is_enabled()) return BT_STATUS_NOT_READY;
The Android Open Source Project624a7872012-12-12 16:00:35 -0800296
Zach Johnsonaf85a8b2020-08-14 17:33:35 -0700297 do_in_main_thread(FROM_HERE, base::BindOnce(btif_get_remote_device_properties,
298 *remote_addr));
Zach Johnson28d5bde2020-08-14 12:07:19 -0700299 return BT_STATUS_SUCCESS;
The Android Open Source Project624a7872012-12-12 16:00:35 -0800300}
301
Jakub Pawlowskid8e815c2017-06-24 17:30:18 -0700302int get_remote_device_property(RawAddress* remote_addr,
Myles Watson3f68a752016-10-19 09:50:22 -0700303 bt_property_type_t type) {
Zach Johnsondbda78c2020-08-14 11:58:08 -0700304 if (!btif_is_enabled()) return BT_STATUS_NOT_READY;
The Android Open Source Project624a7872012-12-12 16:00:35 -0800305
Zach Johnsonaf85a8b2020-08-14 17:33:35 -0700306 do_in_main_thread(FROM_HERE, base::BindOnce(btif_get_remote_device_property,
307 *remote_addr, type));
Zach Johnsondbda78c2020-08-14 11:58:08 -0700308 return BT_STATUS_SUCCESS;
The Android Open Source Project624a7872012-12-12 16:00:35 -0800309}
310
Jakub Pawlowskid8e815c2017-06-24 17:30:18 -0700311int set_remote_device_property(RawAddress* remote_addr,
Myles Watson3f68a752016-10-19 09:50:22 -0700312 const bt_property_t* property) {
Zach Johnson7cfca2e2020-08-14 12:17:08 -0700313 if (!btif_is_enabled()) return BT_STATUS_NOT_READY;
The Android Open Source Project624a7872012-12-12 16:00:35 -0800314
Zach Johnsonaf85a8b2020-08-14 17:33:35 -0700315 do_in_main_thread(
Zach Johnson7cfca2e2020-08-14 12:17:08 -0700316 FROM_HERE, base::BindOnce(
317 [](RawAddress remote_addr, bt_property_t* property) {
318 btif_set_remote_device_property(&remote_addr, property);
319 osi_free(property);
320 },
321 *remote_addr, property_deep_copy(property)));
322 return BT_STATUS_SUCCESS;
The Android Open Source Project624a7872012-12-12 16:00:35 -0800323}
324
Alice Kuoaf6ec992021-08-13 00:30:48 +0800325int get_remote_services(RawAddress* remote_addr, int transport) {
Myles Watson6a7e1ac2017-10-06 16:39:05 -0700326 if (!interface_ready()) return BT_STATUS_NOT_READY;
The Android Open Source Project624a7872012-12-12 16:00:35 -0800327
Chris Manton29e70292021-06-22 10:16:00 -0700328 do_in_main_thread(FROM_HERE, base::BindOnce(btif_dm_get_remote_services,
Alice Kuoaf6ec992021-08-13 00:30:48 +0800329 *remote_addr, transport));
Zach Johnson947f2652020-08-14 12:34:06 -0700330 return BT_STATUS_SUCCESS;
The Android Open Source Project624a7872012-12-12 16:00:35 -0800331}
332
Myles Watson3f68a752016-10-19 09:50:22 -0700333static int start_discovery(void) {
Myles Watson6a7e1ac2017-10-06 16:39:05 -0700334 if (!interface_ready()) return BT_STATUS_NOT_READY;
The Android Open Source Project624a7872012-12-12 16:00:35 -0800335
Zach Johnsonaf85a8b2020-08-14 17:33:35 -0700336 do_in_main_thread(FROM_HERE, base::BindOnce(btif_dm_start_discovery));
Zach Johnson1653a202020-08-14 12:44:42 -0700337 return BT_STATUS_SUCCESS;
The Android Open Source Project624a7872012-12-12 16:00:35 -0800338}
339
Myles Watson3f68a752016-10-19 09:50:22 -0700340static int cancel_discovery(void) {
Myles Watson6a7e1ac2017-10-06 16:39:05 -0700341 if (!interface_ready()) return BT_STATUS_NOT_READY;
The Android Open Source Project624a7872012-12-12 16:00:35 -0800342
Zach Johnsonaf85a8b2020-08-14 17:33:35 -0700343 do_in_main_thread(FROM_HERE, base::BindOnce(btif_dm_cancel_discovery));
Zach Johnson5cd06df2020-08-14 12:47:29 -0700344 return BT_STATUS_SUCCESS;
The Android Open Source Project624a7872012-12-12 16:00:35 -0800345}
346
Jakub Pawlowskid8e815c2017-06-24 17:30:18 -0700347static int create_bond(const RawAddress* bd_addr, int transport) {
Myles Watson6a7e1ac2017-10-06 16:39:05 -0700348 if (!interface_ready()) return BT_STATUS_NOT_READY;
Zach Johnson35f416e2020-08-14 12:52:58 -0700349 if (btif_dm_pairing_is_busy()) return BT_STATUS_BUSY;
The Android Open Source Project624a7872012-12-12 16:00:35 -0800350
Zach Johnsonaf85a8b2020-08-14 17:33:35 -0700351 do_in_main_thread(FROM_HERE,
352 base::BindOnce(btif_dm_create_bond, *bd_addr, transport));
Zach Johnson35f416e2020-08-14 12:52:58 -0700353 return BT_STATUS_SUCCESS;
The Android Open Source Project624a7872012-12-12 16:00:35 -0800354}
355
Jakub Pawlowskid8e815c2017-06-24 17:30:18 -0700356static int create_bond_out_of_band(const RawAddress* bd_addr, int transport,
Martin Brabhamc5c24132021-01-21 10:37:43 -0800357 const bt_oob_data_t* p192_data,
358 const bt_oob_data_t* p256_data) {
Myles Watson6a7e1ac2017-10-06 16:39:05 -0700359 if (!interface_ready()) return BT_STATUS_NOT_READY;
Zach Johnson106ceb42020-08-14 13:00:54 -0700360 if (btif_dm_pairing_is_busy()) return BT_STATUS_BUSY;
The Android Open Source Project624a7872012-12-12 16:00:35 -0800361
Martin Brabhamc5c24132021-01-21 10:37:43 -0800362 do_in_main_thread(FROM_HERE,
363 base::BindOnce(btif_dm_create_bond_out_of_band, *bd_addr,
364 transport, *p192_data, *p256_data));
Zach Johnson106ceb42020-08-14 13:00:54 -0700365 return BT_STATUS_SUCCESS;
The Android Open Source Project624a7872012-12-12 16:00:35 -0800366}
367
Martin Brabhambbe7f092021-03-31 09:41:34 -0700368static int generate_local_oob_data(tBT_TRANSPORT transport) {
369 LOG_INFO("%s", __func__);
370 if (!interface_ready()) return BT_STATUS_NOT_READY;
371
372 return do_in_main_thread(
373 FROM_HERE, base::BindOnce(btif_dm_generate_local_oob_data, transport));
374}
375
Jakub Pawlowskid8e815c2017-06-24 17:30:18 -0700376static int cancel_bond(const RawAddress* bd_addr) {
Myles Watson6a7e1ac2017-10-06 16:39:05 -0700377 if (!interface_ready()) return BT_STATUS_NOT_READY;
The Android Open Source Project624a7872012-12-12 16:00:35 -0800378
Zach Johnsonaf85a8b2020-08-14 17:33:35 -0700379 do_in_main_thread(FROM_HERE, base::BindOnce(btif_dm_cancel_bond, *bd_addr));
Zach Johnson030162a2020-08-14 13:06:01 -0700380 return BT_STATUS_SUCCESS;
The Android Open Source Project624a7872012-12-12 16:00:35 -0800381}
382
Jakub Pawlowskid8e815c2017-06-24 17:30:18 -0700383static int remove_bond(const RawAddress* bd_addr) {
Myles Watson3f68a752016-10-19 09:50:22 -0700384 if (is_restricted_mode() && !btif_storage_is_restricted_device(bd_addr))
Satya Calloji75fd8802014-07-03 17:18:02 -0700385 return BT_STATUS_SUCCESS;
Myles Watson3f68a752016-10-19 09:50:22 -0700386
Myles Watson6a7e1ac2017-10-06 16:39:05 -0700387 if (!interface_ready()) return BT_STATUS_NOT_READY;
Myles Watson3f68a752016-10-19 09:50:22 -0700388
Zach Johnsonaf85a8b2020-08-14 17:33:35 -0700389 do_in_main_thread(FROM_HERE, base::BindOnce(btif_dm_remove_bond, *bd_addr));
Zach Johnson62b79ea2020-08-14 13:08:57 -0700390 return BT_STATUS_SUCCESS;
Satya Calloji75fd8802014-07-03 17:18:02 -0700391}
392
Jakub Pawlowskid8e815c2017-06-24 17:30:18 -0700393static int get_connection_state(const RawAddress* bd_addr) {
Myles Watson6a7e1ac2017-10-06 16:39:05 -0700394 if (!interface_ready()) return 0;
Myles Watson3f68a752016-10-19 09:50:22 -0700395
396 return btif_dm_get_connection_state(bd_addr);
397}
398
Jakub Pawlowskid8e815c2017-06-24 17:30:18 -0700399static int pin_reply(const RawAddress* bd_addr, uint8_t accept, uint8_t pin_len,
400 bt_pin_code_t* pin_code) {
Brian Delwiche2c09d202022-08-10 06:47:52 +0000401 bt_pin_code_t tmp_pin_code;
Myles Watson6a7e1ac2017-10-06 16:39:05 -0700402 if (!interface_ready()) return BT_STATUS_NOT_READY;
Zach Johnsonda4e25f2020-08-14 13:39:45 -0700403 if (pin_code == nullptr || pin_len > PIN_CODE_LEN) return BT_STATUS_FAIL;
Myles Watson3f68a752016-10-19 09:50:22 -0700404
Brian Delwiche2c09d202022-08-10 06:47:52 +0000405 memcpy(&tmp_pin_code, pin_code, pin_len);
406
Zach Johnsonaf85a8b2020-08-14 17:33:35 -0700407 do_in_main_thread(FROM_HERE, base::BindOnce(btif_dm_pin_reply, *bd_addr,
Brian Delwiche2c09d202022-08-10 06:47:52 +0000408 accept, pin_len, tmp_pin_code));
Zach Johnsonda4e25f2020-08-14 13:39:45 -0700409 return BT_STATUS_SUCCESS;
Myles Watson3f68a752016-10-19 09:50:22 -0700410}
411
Jakub Pawlowskid8e815c2017-06-24 17:30:18 -0700412static int ssp_reply(const RawAddress* bd_addr, bt_ssp_variant_t variant,
Myles Watson3f68a752016-10-19 09:50:22 -0700413 uint8_t accept, uint32_t passkey) {
Myles Watson6a7e1ac2017-10-06 16:39:05 -0700414 if (!interface_ready()) return BT_STATUS_NOT_READY;
Zach Johnson418fb352020-08-14 14:02:15 -0700415 if (variant == BT_SSP_VARIANT_PASSKEY_ENTRY) return BT_STATUS_FAIL;
Myles Watson3f68a752016-10-19 09:50:22 -0700416
Zach Johnsonaf85a8b2020-08-14 17:33:35 -0700417 do_in_main_thread(
Zach Johnson418fb352020-08-14 14:02:15 -0700418 FROM_HERE, base::BindOnce(btif_dm_ssp_reply, *bd_addr, variant, accept));
419 return BT_STATUS_SUCCESS;
Myles Watson3f68a752016-10-19 09:50:22 -0700420}
421
422static int read_energy_info() {
Myles Watson6a7e1ac2017-10-06 16:39:05 -0700423 if (!interface_ready()) return BT_STATUS_NOT_READY;
Zach Johnsond590bb12020-08-14 14:03:44 -0700424
Zach Johnsonaf85a8b2020-08-14 17:33:35 -0700425 do_in_main_thread(FROM_HERE, base::BindOnce(btif_dm_read_energy_info));
Myles Watson3f68a752016-10-19 09:50:22 -0700426 return BT_STATUS_SUCCESS;
427}
428
Martin Brabham08eeffa2022-01-26 18:50:36 +0000429static int clear_event_filter() {
430 LOG_VERBOSE("%s", __func__);
431 if (!interface_ready()) return BT_STATUS_NOT_READY;
432
433 do_in_main_thread(FROM_HERE, base::BindOnce(btif_dm_clear_event_filter));
434 return BT_STATUS_SUCCESS;
435}
436
Myles Watson3f68a752016-10-19 09:50:22 -0700437static void dump(int fd, const char** arguments) {
Myles Watson3f68a752016-10-19 09:50:22 -0700438 btif_debug_conn_dump(fd);
439 btif_debug_bond_event_dump(fd);
Josh Wu6ea85d42022-03-23 01:54:04 -0700440 btif_debug_linkkey_type_dump(fd);
Myles Watson3f68a752016-10-19 09:50:22 -0700441 btif_debug_a2dp_dump(fd);
Pavlin Radoslavov5c6e64c2017-11-24 19:12:11 -0800442 btif_debug_av_dump(fd);
443 bta_debug_av_dump(fd);
444 stack_debug_avdtp_api_dump(fd);
Ajay Panicker13d845b2018-05-02 16:23:13 -0700445 bluetooth::avrcp::AvrcpService::DebugDump(fd);
Myles Watson3f68a752016-10-19 09:50:22 -0700446 btif_debug_config_dump(fd);
Sanket Agarwald338a642016-11-30 19:43:47 -0800447 BTA_HfClientDumpStatistics(fd);
Myles Watson3f68a752016-10-19 09:50:22 -0700448 wakelock_debug_dump(fd);
Pavlin Radoslavov64c30932017-02-01 03:55:33 -0800449 osi_allocator_debug_dump(fd);
Myles Watson3f68a752016-10-19 09:50:22 -0700450 alarm_debug_dump(fd);
Łukasz Rymanowskid853b3a2021-10-07 16:58:07 +0000451 bluetooth::csis::CsisClient::DebugDump(fd);
Abhishek Pandit-Subedi99311372022-02-03 16:25:54 -0800452#ifndef TARGET_FLOSS
Jakub Tyszkowskib87fc992021-04-08 15:08:35 +0000453 le_audio::has::HasClient::DebugDump(fd);
Abhishek Pandit-Subedi99311372022-02-03 16:25:54 -0800454#endif
Hansong Zhangc5df6b02018-05-09 14:39:56 -0700455 HearingAid::DebugDump(fd);
Abhishek Pandit-Subedi83a3fa82022-01-28 17:08:55 -0800456#ifndef TARGET_FLOSS
Grzegorz Kołodziejczyk926fd432021-10-06 11:54:53 +0000457 LeAudioClient::DebugDump(fd);
Jakub Tyszkowski86048262021-12-13 12:32:59 +0100458 LeAudioBroadcaster::DebugDump(fd);
Łukasz Rymanowski1cf59992022-03-07 09:19:18 +0000459 VolumeControl::DebugDump(fd);
Abhishek Pandit-Subedi83a3fa82022-01-28 17:08:55 -0800460#endif
Jakub Pawlowskie80bfd52018-12-28 19:03:09 +0100461 connection_manager::dump(fd);
Ray4a73d962019-01-03 21:19:16 +0800462 bluetooth::bqr::DebugDump(fd);
Chris Mantonb939d842021-10-01 13:50:06 -0700463 bluetooth::shim::Dump(fd, arguments);
Andre Eisenbach8f6c5be2014-12-05 09:40:20 -0800464}
465
Jack Hed20346ab2018-01-31 16:51:26 -0800466static void dumpMetrics(std::string* output) {
Jack He42823982018-08-15 12:38:37 -0700467 bluetooth::common::BluetoothMetricsLogger::GetInstance()->WriteString(output);
Jack Hed20346ab2018-01-31 16:51:26 -0800468}
469
Myles Watson3f68a752016-10-19 09:50:22 -0700470static const void* get_profile_interface(const char* profile_id) {
Jakub Pawlowski13ee9b82020-03-05 21:16:10 +0100471 LOG_INFO("%s: id = %s", __func__, profile_id);
The Android Open Source Project624a7872012-12-12 16:00:35 -0800472
Myles Watson3f68a752016-10-19 09:50:22 -0700473 /* sanity check */
Myles Watson6a7e1ac2017-10-06 16:39:05 -0700474 if (!interface_ready()) return NULL;
The Android Open Source Project624a7872012-12-12 16:00:35 -0800475
Myles Watson3f68a752016-10-19 09:50:22 -0700476 /* check for supported profile interfaces */
477 if (is_profile(profile_id, BT_PROFILE_HANDSFREE_ID))
Jack Hee43b2372017-12-14 15:00:39 -0800478 return bluetooth::headset::GetInterface();
The Android Open Source Project624a7872012-12-12 16:00:35 -0800479
Myles Watson3f68a752016-10-19 09:50:22 -0700480 if (is_profile(profile_id, BT_PROFILE_HANDSFREE_CLIENT_ID))
481 return btif_hf_client_get_interface();
Hemant Guptad9e9cf92013-08-19 18:33:01 +0530482
Myles Watson3f68a752016-10-19 09:50:22 -0700483 if (is_profile(profile_id, BT_PROFILE_SOCKETS_ID))
484 return btif_sock_get_interface();
The Android Open Source Project624a7872012-12-12 16:00:35 -0800485
Myles Watson3f68a752016-10-19 09:50:22 -0700486 if (is_profile(profile_id, BT_PROFILE_PAN_ID))
487 return btif_pan_get_interface();
The Android Open Source Project624a7872012-12-12 16:00:35 -0800488
Myles Watson3f68a752016-10-19 09:50:22 -0700489 if (is_profile(profile_id, BT_PROFILE_ADVANCED_AUDIO_ID))
490 return btif_av_get_src_interface();
Mike Lockwoodc80e2d72014-05-23 12:42:24 -0700491
Myles Watson3f68a752016-10-19 09:50:22 -0700492 if (is_profile(profile_id, BT_PROFILE_ADVANCED_AUDIO_SINK_ID))
493 return btif_av_get_sink_interface();
The Android Open Source Project624a7872012-12-12 16:00:35 -0800494
Myles Watson3f68a752016-10-19 09:50:22 -0700495 if (is_profile(profile_id, BT_PROFILE_HIDHOST_ID))
496 return btif_hh_get_interface();
The Android Open Source Project624a7872012-12-12 16:00:35 -0800497
Hemant Gupta28425222014-04-18 12:34:55 +0530498 if (is_profile(profile_id, BT_PROFILE_HIDDEV_ID))
499 return btif_hd_get_interface();
500
Myles Watson3f68a752016-10-19 09:50:22 -0700501 if (is_profile(profile_id, BT_PROFILE_SDP_CLIENT_ID))
502 return btif_sdp_get_interface();
Hemant Guptad0ecdbb2014-04-18 12:54:08 +0530503
Myles Watson3f68a752016-10-19 09:50:22 -0700504 if (is_profile(profile_id, BT_PROFILE_GATT_ID))
505 return btif_gatt_get_interface();
Ravi Nagarajan78b35202013-03-06 05:29:48 -0800506
Myles Watson3f68a752016-10-19 09:50:22 -0700507 if (is_profile(profile_id, BT_PROFILE_AV_RC_ID))
508 return btif_rc_get_interface();
Ravi Nagarajan78b35202013-03-06 05:29:48 -0800509
Myles Watson3f68a752016-10-19 09:50:22 -0700510 if (is_profile(profile_id, BT_PROFILE_AV_RC_CTRL_ID))
511 return btif_rc_ctrl_get_interface();
Mike Lockwoodc02ef772014-06-02 16:21:49 -0700512
Jakub Pawlowski7d06e572018-02-28 22:44:42 -0800513 if (is_profile(profile_id, BT_PROFILE_HEARING_AID_ID))
514 return btif_hearing_aid_get_interface();
weichinweng6033dfb2020-04-21 09:42:27 +0800515
Abhishek Pandit-Subedi99311372022-02-03 16:25:54 -0800516#ifndef TARGET_FLOSS
Jakub Tyszkowskib87fc992021-04-08 15:08:35 +0000517 if (is_profile(profile_id, BT_PROFILE_HAP_CLIENT_ID))
518 return btif_has_client_get_interface();
Abhishek Pandit-Subedi99311372022-02-03 16:25:54 -0800519#endif
Jakub Tyszkowskib87fc992021-04-08 15:08:35 +0000520
weichinweng6033dfb2020-04-21 09:42:27 +0800521 if (is_profile(profile_id, BT_KEYSTORE_ID))
522 return bluetooth::bluetooth_keystore::getBluetoothKeystoreInterface();
Michael Sun80dff5c2020-11-02 07:17:47 +0000523
524 if (is_profile(profile_id, BT_ACTIVITY_ATTRIBUTION_ID)) {
Michael Sun7cc8f122020-11-23 18:54:29 +0000525 return bluetooth::activity_attribution::get_activity_attribution_instance();
Michael Sun80dff5c2020-11-02 07:17:47 +0000526 }
527
Abhishek Pandit-Subedi83a3fa82022-01-28 17:08:55 -0800528#ifndef TARGET_FLOSS
Grzegorz Kołodziejczyk177328a2020-11-30 13:51:05 +0100529 if (is_profile(profile_id, BT_PROFILE_LE_AUDIO_ID))
530 return btif_le_audio_get_interface();
531
Jakub Tyszkowski86048262021-12-13 12:32:59 +0100532 if (is_profile(profile_id, BT_PROFILE_LE_AUDIO_BROADCASTER_ID))
533 return btif_le_audio_broadcaster_get_interface();
Abhishek Pandit-Subedi83a3fa82022-01-28 17:08:55 -0800534#endif
Jakub Tyszkowski86048262021-12-13 12:32:59 +0100535
Łukasz Rymanowski35b08362021-01-29 09:05:10 +0000536 if (is_profile(profile_id, BT_PROFILE_VC_ID))
537 return btif_volume_control_get_interface();
538
Łukasz Rymanowski06a5be22020-12-15 09:14:51 +0000539 if (is_profile(profile_id, BT_PROFILE_CSIS_CLIENT_ID))
540 return btif_csis_client_get_interface();
541
Myles Watson3f68a752016-10-19 09:50:22 -0700542 return NULL;
The Android Open Source Project624a7872012-12-12 16:00:35 -0800543}
544
Myles Watson3f68a752016-10-19 09:50:22 -0700545int dut_mode_configure(uint8_t enable) {
Myles Watson6a7e1ac2017-10-06 16:39:05 -0700546 if (!interface_ready()) return BT_STATUS_NOT_READY;
Zach Johnson9a325722020-08-14 14:08:26 -0700547 if (!stack_manager_get_interface()->get_stack_is_running())
548 return BT_STATUS_NOT_READY;
The Android Open Source Project624a7872012-12-12 16:00:35 -0800549
Zach Johnsonaf85a8b2020-08-14 17:33:35 -0700550 do_in_main_thread(FROM_HERE, base::BindOnce(btif_dut_mode_configure, enable));
Zach Johnson9a325722020-08-14 14:08:26 -0700551 return BT_STATUS_SUCCESS;
The Android Open Source Project624a7872012-12-12 16:00:35 -0800552}
553
Myles Watson3f68a752016-10-19 09:50:22 -0700554int dut_mode_send(uint16_t opcode, uint8_t* buf, uint8_t len) {
Myles Watson6a7e1ac2017-10-06 16:39:05 -0700555 if (!interface_ready()) return BT_STATUS_NOT_READY;
Zach Johnsonc401e252020-08-14 14:16:35 -0700556 if (!btif_is_dut_mode()) return BT_STATUS_FAIL;
The Android Open Source Project624a7872012-12-12 16:00:35 -0800557
Zach Johnsonc401e252020-08-14 14:16:35 -0700558 uint8_t* copy = (uint8_t*)osi_calloc(len);
559 memcpy(copy, buf, len);
560
Zach Johnsonaf85a8b2020-08-14 17:33:35 -0700561 do_in_main_thread(FROM_HERE,
562 base::BindOnce(
563 [](uint16_t opcode, uint8_t* buf, uint8_t len) {
564 btif_dut_mode_send(opcode, buf, len);
565 osi_free(buf);
566 },
567 opcode, copy, len));
Zach Johnsonc401e252020-08-14 14:16:35 -0700568 return BT_STATUS_SUCCESS;
The Android Open Source Project624a7872012-12-12 16:00:35 -0800569}
Matthew Xie41fb4362013-05-06 20:51:02 -0700570
Myles Watson3f68a752016-10-19 09:50:22 -0700571int le_test_mode(uint16_t opcode, uint8_t* buf, uint8_t len) {
Myles Watson6a7e1ac2017-10-06 16:39:05 -0700572 if (!interface_ready()) return BT_STATUS_NOT_READY;
Ganesh Ganapathi Batta1def2552013-02-05 15:22:31 -0800573
Zach Johnsonb2bc7102020-08-14 14:25:49 -0700574 switch (opcode) {
575 case HCI_BLE_TRANSMITTER_TEST:
576 if (len != 3) return BT_STATUS_PARM_INVALID;
Zach Johnsonaf85a8b2020-08-14 17:33:35 -0700577 do_in_main_thread(FROM_HERE, base::BindOnce(btif_ble_transmitter_test,
578 buf[0], buf[1], buf[2]));
Zach Johnsonb2bc7102020-08-14 14:25:49 -0700579 break;
580 case HCI_BLE_RECEIVER_TEST:
581 if (len != 1) return BT_STATUS_PARM_INVALID;
Zach Johnsonaf85a8b2020-08-14 17:33:35 -0700582 do_in_main_thread(FROM_HERE,
583 base::BindOnce(btif_ble_receiver_test, buf[0]));
Zach Johnsonb2bc7102020-08-14 14:25:49 -0700584 break;
585 case HCI_BLE_TEST_END:
Zach Johnsonaf85a8b2020-08-14 17:33:35 -0700586 do_in_main_thread(FROM_HERE, base::BindOnce(btif_ble_test_end));
Zach Johnsonb2bc7102020-08-14 14:25:49 -0700587 break;
588 default:
589 return BT_STATUS_UNSUPPORTED;
590 }
591 return BT_STATUS_SUCCESS;
Ganesh Ganapathi Batta1def2552013-02-05 15:22:31 -0800592}
Ganesh Ganapathi Batta1def2552013-02-05 15:22:31 -0800593
Pavlin Radoslavov34bd6332018-08-24 18:45:16 -0700594static bt_os_callouts_t* wakelock_os_callouts_saved = nullptr;
595
596static int acquire_wake_lock_cb(const char* lock_name) {
Jack He85ec1132021-05-20 17:59:45 -0700597 return do_in_jni_thread(
Pavlin Radoslavov34bd6332018-08-24 18:45:16 -0700598 FROM_HERE, base::Bind(base::IgnoreResult(
599 wakelock_os_callouts_saved->acquire_wake_lock),
600 lock_name));
601}
602
603static int release_wake_lock_cb(const char* lock_name) {
Jack He85ec1132021-05-20 17:59:45 -0700604 return do_in_jni_thread(
Pavlin Radoslavov34bd6332018-08-24 18:45:16 -0700605 FROM_HERE, base::Bind(base::IgnoreResult(
606 wakelock_os_callouts_saved->release_wake_lock),
607 lock_name));
608}
609
610static bt_os_callouts_t wakelock_os_callouts_jni = {
611 sizeof(wakelock_os_callouts_jni),
612 nullptr /* not used */,
613 acquire_wake_lock_cb,
614 release_wake_lock_cb,
615};
616
Myles Watson3f68a752016-10-19 09:50:22 -0700617static int set_os_callouts(bt_os_callouts_t* callouts) {
Pavlin Radoslavov34bd6332018-08-24 18:45:16 -0700618 wakelock_os_callouts_saved = callouts;
619 wakelock_set_os_callouts(&wakelock_os_callouts_jni);
Myles Watson3f68a752016-10-19 09:50:22 -0700620 return BT_STATUS_SUCCESS;
Sharvil Nanavati55e690a2014-05-28 17:09:46 -0700621}
622
Ajay Panickerec102082015-07-28 16:54:53 -0700623static int config_clear(void) {
Jakub Pawlowski13ee9b82020-03-05 21:16:10 +0100624 LOG_INFO("%s", __func__);
Myles Watson3f68a752016-10-19 09:50:22 -0700625 return btif_config_clear() ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
Ajay Panickerec102082015-07-28 16:54:53 -0700626}
627
Ajay Panicker9e1c9582018-03-14 23:45:54 -0700628static bluetooth::avrcp::ServiceInterface* get_avrcp_service(void) {
629 return bluetooth::avrcp::AvrcpService::GetServiceInterface();
630}
631
Jack He9183fd32018-11-16 02:59:43 -0800632static std::string obfuscate_address(const RawAddress& address) {
633 return bluetooth::common::AddressObfuscator::GetInstance()->Obfuscate(
634 address);
635}
636
Chen Chen88ca6ae2020-03-05 15:02:46 -0800637static int get_metric_id(const RawAddress& address) {
Chen Chen534bf242021-03-10 15:41:42 -0800638 return allocate_metric_id_from_metric_id_allocator(address);
Chen Chen88ca6ae2020-03-05 15:02:46 -0800639}
640
More Kuo82c341f2020-08-13 14:30:33 +0800641static int set_dynamic_audio_buffer_size(int codec, int size) {
642 return btif_set_dynamic_audio_buffer_size(codec, size);
643}
644
Chen Chen31348f22022-01-13 17:57:01 -0800645static bool allow_low_latency_audio(bool allowed, const RawAddress& address) {
646 LOG_INFO("%s %s", __func__, allowed ? "true" : "false");
Chen Chenc989bf82022-02-14 18:26:50 -0800647 bluetooth::audio::a2dp::set_audio_low_latency_mode_allowed(allowed);
648 return true;
Chen Chen31348f22022-01-13 17:57:01 -0800649}
650
Jakub Pawlowski5f52ab02017-10-16 15:57:16 -0700651EXPORT_SYMBOL bt_interface_t bluetoothInterface = {
Ganesh Ganapathi Batta1def2552013-02-05 15:22:31 -0800652 sizeof(bluetoothInterface),
The Android Open Source Project624a7872012-12-12 16:00:35 -0800653 init,
654 enable,
655 disable,
656 cleanup,
657 get_adapter_properties,
658 get_adapter_property,
659 set_adapter_property,
660 get_remote_device_properties,
661 get_remote_device_property,
662 set_remote_device_property,
Zach Johnsona3229932020-08-17 12:55:28 -0700663 nullptr,
The Android Open Source Project624a7872012-12-12 16:00:35 -0800664 get_remote_services,
665 start_discovery,
666 cancel_discovery,
667 create_bond,
Jakub Pawlowskidf0ab812015-12-01 12:14:22 -0800668 create_bond_out_of_band,
The Android Open Source Project624a7872012-12-12 16:00:35 -0800669 remove_bond,
670 cancel_bond,
Andre Eisenbach4a6f8cf2014-06-18 12:20:37 -0700671 get_connection_state,
The Android Open Source Project624a7872012-12-12 16:00:35 -0800672 pin_reply,
673 ssp_reply,
674 get_profile_interface,
675 dut_mode_configure,
Ganesh Ganapathi Batta1def2552013-02-05 15:22:31 -0800676 dut_mode_send,
Zhihai Xu8572d792013-06-04 18:21:25 -0700677 le_test_mode,
Sharvil Nanavati55e690a2014-05-28 17:09:46 -0700678 set_os_callouts,
Satya Calloji75fd8802014-07-03 17:18:02 -0700679 read_energy_info,
Ajay Panickerec102082015-07-28 16:54:53 -0700680 dump,
Jack Hed20346ab2018-01-31 16:51:26 -0800681 dumpMetrics,
Andre Eisenbach35c0c1d62016-01-14 02:00:24 -0800682 config_clear,
683 interop_database_clear,
684 interop_database_add,
Ajay Panicker9e1c9582018-03-14 23:45:54 -0700685 get_avrcp_service,
Jack He9183fd32018-11-16 02:59:43 -0800686 obfuscate_address,
Chen Chen88ca6ae2020-03-05 15:02:46 -0800687 get_metric_id,
More Kuo82c341f2020-08-13 14:30:33 +0800688 set_dynamic_audio_buffer_size,
Chen Chen31348f22022-01-13 17:57:01 -0800689 generate_local_oob_data,
Martin Brabham08eeffa2022-01-26 18:50:36 +0000690 allow_low_latency_audio,
691 clear_event_filter};
Zach Johnsonb3b0a032020-08-14 15:42:22 -0700692
693// callback reporting helpers
694
695bt_property_t* property_deep_copy_array(int num_properties,
696 bt_property_t* properties) {
697 bt_property_t* copy = nullptr;
698 if (num_properties > 0) {
699 size_t content_len = 0;
700 for (int i = 0; i < num_properties; i++) {
701 auto len = properties[i].len;
702 if (len > 0) {
703 content_len += len;
704 }
705 }
706
707 copy = (bt_property_t*)osi_calloc((sizeof(bt_property_t) * num_properties) +
708 content_len);
Chris Manton43fd4932021-07-11 10:51:26 -0700709 ASSERT(copy != nullptr);
Zach Johnsonb3b0a032020-08-14 15:42:22 -0700710 uint8_t* content = (uint8_t*)(copy + num_properties);
711
712 for (int i = 0; i < num_properties; i++) {
713 auto len = properties[i].len;
714 copy[i].type = properties[i].type;
715 copy[i].len = len;
716 if (len <= 0) {
717 continue;
718 }
719 copy[i].val = content;
720 memcpy(content, properties[i].val, len);
721 content += len;
722 }
723 }
724 return copy;
725}
726
Zach Johnson96f0c592020-08-14 15:52:36 -0700727void invoke_adapter_state_changed_cb(bt_state_t state) {
728 do_in_jni_thread(FROM_HERE, base::BindOnce(
729 [](bt_state_t state) {
730 HAL_CBACK(bt_hal_cbacks,
731 adapter_state_changed_cb, state);
732 },
733 state));
734}
735
Zach Johnsonb3b0a032020-08-14 15:42:22 -0700736void invoke_adapter_properties_cb(bt_status_t status, int num_properties,
737 bt_property_t* properties) {
738 do_in_jni_thread(FROM_HERE,
739 base::BindOnce(
740 [](bt_status_t status, int num_properties,
741 bt_property_t* properties) {
742 HAL_CBACK(bt_hal_cbacks, adapter_properties_cb, status,
743 num_properties, properties);
744 if (properties) {
745 osi_free(properties);
746 }
747 },
748 status, num_properties,
749 property_deep_copy_array(num_properties, properties)));
750}
Zach Johnsonac73b312020-08-14 16:02:11 -0700751
752void invoke_remote_device_properties_cb(bt_status_t status, RawAddress bd_addr,
753 int num_properties,
754 bt_property_t* properties) {
755 do_in_jni_thread(
756 FROM_HERE, base::BindOnce(
757 [](bt_status_t status, RawAddress bd_addr,
758 int num_properties, bt_property_t* properties) {
759 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
760 status, &bd_addr, num_properties, properties);
761 if (properties) {
762 osi_free(properties);
763 }
764 },
765 status, bd_addr, num_properties,
766 property_deep_copy_array(num_properties, properties)));
767}
Zach Johnson66bbfee2020-08-14 16:06:59 -0700768
769void invoke_device_found_cb(int num_properties, bt_property_t* properties) {
770 do_in_jni_thread(FROM_HERE,
771 base::BindOnce(
772 [](int num_properties, bt_property_t* properties) {
773 HAL_CBACK(bt_hal_cbacks, device_found_cb,
774 num_properties, properties);
775 if (properties) {
776 osi_free(properties);
777 }
778 },
779 num_properties,
780 property_deep_copy_array(num_properties, properties)));
781}
Zach Johnson4ae115e2020-08-14 16:11:09 -0700782
783void invoke_discovery_state_changed_cb(bt_discovery_state_t state) {
784 do_in_jni_thread(FROM_HERE, base::BindOnce(
785 [](bt_discovery_state_t state) {
786 HAL_CBACK(bt_hal_cbacks,
787 discovery_state_changed_cb,
788 state);
789 },
790 state));
791}
Zach Johnson28f22c42020-08-14 16:15:26 -0700792
793void invoke_pin_request_cb(RawAddress bd_addr, bt_bdname_t bd_name,
794 uint32_t cod, bool min_16_digit) {
795 do_in_jni_thread(FROM_HERE, base::BindOnce(
796 [](RawAddress bd_addr, bt_bdname_t bd_name,
797 uint32_t cod, bool min_16_digit) {
798 HAL_CBACK(bt_hal_cbacks, pin_request_cb,
799 &bd_addr, &bd_name, cod,
800 min_16_digit);
801 },
802 bd_addr, bd_name, cod, min_16_digit));
803}
Zach Johnson66ba3062020-08-14 16:23:45 -0700804
805void invoke_ssp_request_cb(RawAddress bd_addr, bt_bdname_t bd_name,
806 uint32_t cod, bt_ssp_variant_t pairing_variant,
807 uint32_t pass_key) {
808 do_in_jni_thread(FROM_HERE,
809 base::BindOnce(
810 [](RawAddress bd_addr, bt_bdname_t bd_name, uint32_t cod,
811 bt_ssp_variant_t pairing_variant, uint32_t pass_key) {
812 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr,
813 &bd_name, cod, pairing_variant, pass_key);
814 },
815 bd_addr, bd_name, cod, pairing_variant, pass_key));
816}
Zach Johnson31cea6c2020-08-14 16:35:12 -0700817
Martin Brabhambbe7f092021-03-31 09:41:34 -0700818void invoke_oob_data_request_cb(tBT_TRANSPORT t, bool valid, Octet16 c,
Martin Brabhamdb035532021-05-20 14:30:09 -0700819 Octet16 r, RawAddress raw_address,
820 uint8_t address_type) {
Martin Brabhambbe7f092021-03-31 09:41:34 -0700821 LOG_INFO("%s", __func__);
Martin Brabham0c64cea2021-04-26 21:35:22 -0700822 bt_oob_data_t oob_data = {};
Chris Manton9ce97112022-01-15 14:47:12 -0800823 const char* local_name;
Martin Brabhamafea75b2021-06-30 12:08:02 -0700824 BTM_ReadLocalDeviceName(&local_name);
825 for (int i = 0; i < BTM_MAX_LOC_BD_NAME_LEN; i++) {
826 oob_data.device_name[i] = local_name[i];
827 }
Martin Brabham0c64cea2021-04-26 21:35:22 -0700828
829 // Set the local address
830 int j = 5;
831 for (int i = 0; i < 6; i++) {
832 oob_data.address[i] = raw_address.address[j];
833 j--;
834 }
Martin Brabhamdb035532021-05-20 14:30:09 -0700835 oob_data.address[6] = address_type;
Martin Brabham0c64cea2021-04-26 21:35:22 -0700836
Martin Brabhambbe7f092021-03-31 09:41:34 -0700837 // Each value (for C and R) is 16 octets in length
838 bool c_empty = true;
839 for (int i = 0; i < 16; i++) {
840 // C cannot be all 0s, if so then we want to fail
841 if (c[i] != 0) c_empty = false;
842 oob_data.c[i] = c[i];
843 // R is optional and may be empty
844 oob_data.r[i] = r[i];
845 }
846 oob_data.is_valid = valid && !c_empty;
847 // The oob_data_length is 2 octects in length. The value includes the length
848 // of itself. 16 + 16 + 2 = 34 Data 0x0022 Little Endian order 0x2200
849 oob_data.oob_data_length[0] = 0;
850 oob_data.oob_data_length[1] = 34;
851 bt_status_t status = do_in_jni_thread(
852 FROM_HERE, base::BindOnce(
853 [](tBT_TRANSPORT t, bt_oob_data_t oob_data) {
854 HAL_CBACK(bt_hal_cbacks, generate_local_oob_data_cb, t,
855 oob_data);
856 },
857 t, oob_data));
858 if (status != BT_STATUS_SUCCESS) {
859 LOG_ERROR("%s: Failed to call callback!", __func__);
860 }
861}
862
Zach Johnson31cea6c2020-08-14 16:35:12 -0700863void invoke_bond_state_changed_cb(bt_status_t status, RawAddress bd_addr,
wescandeb819e4e2021-07-27 11:15:49 +0200864 bt_bond_state_t state, int fail_reason) {
865 do_in_jni_thread(FROM_HERE, base::BindOnce(
866 [](bt_status_t status, RawAddress bd_addr,
867 bt_bond_state_t state, int fail_reason) {
868 HAL_CBACK(bt_hal_cbacks,
869 bond_state_changed_cb, status,
870 &bd_addr, state, fail_reason);
871 },
872 status, bd_addr, state, fail_reason));
Zach Johnson31cea6c2020-08-14 16:35:12 -0700873}
Zach Johnson9de212d2020-08-14 16:38:27 -0700874
Chienyuan21c28c42021-11-09 15:10:56 +0800875void invoke_address_consolidate_cb(RawAddress main_bd_addr,
876 RawAddress secondary_bd_addr) {
877 do_in_jni_thread(
878 FROM_HERE, base::BindOnce(
879 [](RawAddress main_bd_addr, RawAddress secondary_bd_addr) {
880 HAL_CBACK(bt_hal_cbacks, address_consolidate_cb,
881 &main_bd_addr, &secondary_bd_addr);
882 },
883 main_bd_addr, secondary_bd_addr));
884}
885
Rahul Sabnis73914232022-07-01 14:03:28 -0700886void invoke_le_address_associate_cb(RawAddress main_bd_addr,
887 RawAddress secondary_bd_addr) {
888 do_in_jni_thread(
889 FROM_HERE, base::BindOnce(
890 [](RawAddress main_bd_addr, RawAddress secondary_bd_addr) {
891 HAL_CBACK(bt_hal_cbacks, le_address_associate_cb,
892 &main_bd_addr, &secondary_bd_addr);
893 },
894 main_bd_addr, secondary_bd_addr));
895}
Zach Johnson9de212d2020-08-14 16:38:27 -0700896void invoke_acl_state_changed_cb(bt_status_t status, RawAddress bd_addr,
wescandeb819e4e2021-07-27 11:15:49 +0200897 bt_acl_state_t state, int transport_link_type,
898 bt_hci_error_code_t hci_reason) {
Zach Johnson9de212d2020-08-14 16:38:27 -0700899 do_in_jni_thread(
900 FROM_HERE,
901 base::BindOnce(
Andrew Cheng261f3602021-01-14 17:28:13 -0800902 [](bt_status_t status, RawAddress bd_addr, bt_acl_state_t state,
wescandeb819e4e2021-07-27 11:15:49 +0200903 int transport_link_type, bt_hci_error_code_t hci_reason) {
Zach Johnson9de212d2020-08-14 16:38:27 -0700904 HAL_CBACK(bt_hal_cbacks, acl_state_changed_cb, status, &bd_addr,
wescandeb819e4e2021-07-27 11:15:49 +0200905 state, transport_link_type, hci_reason);
Zach Johnson9de212d2020-08-14 16:38:27 -0700906 },
wescandeb819e4e2021-07-27 11:15:49 +0200907 status, bd_addr, state, transport_link_type, hci_reason));
Zach Johnson9de212d2020-08-14 16:38:27 -0700908}
Zach Johnson21c73782020-08-14 16:43:57 -0700909
910void invoke_thread_evt_cb(bt_cb_thread_evt event) {
911 do_in_jni_thread(FROM_HERE, base::BindOnce(
912 [](bt_cb_thread_evt event) {
913 HAL_CBACK(bt_hal_cbacks, thread_evt_cb,
914 event);
915 if (event == DISASSOCIATE_JVM) {
916 bt_hal_cbacks = NULL;
917 }
918 },
919 event));
920}
Zach Johnson9a822972020-08-14 16:48:46 -0700921
922void invoke_le_test_mode_cb(bt_status_t status, uint16_t count) {
923 do_in_jni_thread(FROM_HERE, base::BindOnce(
924 [](bt_status_t status, uint16_t count) {
925 HAL_CBACK(bt_hal_cbacks, le_test_mode_cb,
926 status, count);
927 },
928 status, count));
929}
Zach Johnsona2594732020-08-14 16:56:42 -0700930
931// takes ownership of |uid_data|
932void invoke_energy_info_cb(bt_activity_energy_info energy_info,
933 bt_uid_traffic_t* uid_data) {
934 do_in_jni_thread(
935 FROM_HERE,
936 base::BindOnce(
937 [](bt_activity_energy_info energy_info, bt_uid_traffic_t* uid_data) {
938 HAL_CBACK(bt_hal_cbacks, energy_info_cb, &energy_info, uid_data);
939 osi_free(uid_data);
940 },
941 energy_info, uid_data));
942}
Chen Chend35a71e2021-02-04 17:38:31 -0800943
944void invoke_link_quality_report_cb(
945 uint64_t timestamp, int report_id, int rssi, int snr,
946 int retransmission_count, int packets_not_receive_count,
947 int negative_acknowledgement_count) {
948 do_in_jni_thread(
949 FROM_HERE,
950 base::BindOnce(
951 [](uint64_t timestamp, int report_id, int rssi, int snr,
952 int retransmission_count, int packets_not_receive_count,
953 int negative_acknowledgement_count) {
954 HAL_CBACK(bt_hal_cbacks, link_quality_report_cb,
955 timestamp, report_id, rssi, snr, retransmission_count,
956 packets_not_receive_count,
957 negative_acknowledgement_count);
958 },
959 timestamp, report_id, rssi, snr, retransmission_count,
960 packets_not_receive_count, negative_acknowledgement_count));
961}
Chen Chena81d2c62022-01-19 12:10:20 -0800962
Chen Chenf41721c2022-02-11 12:13:48 -0800963void invoke_switch_buffer_size_cb(bool is_low_latency_buffer_size) {
Chen Chena81d2c62022-01-19 12:10:20 -0800964 do_in_jni_thread(
965 FROM_HERE,
966 base::BindOnce(
Chen Chenf41721c2022-02-11 12:13:48 -0800967 [](bool is_low_latency_buffer_size) {
968 HAL_CBACK(bt_hal_cbacks, switch_buffer_size_cb,
Chen Chena81d2c62022-01-19 12:10:20 -0800969 is_low_latency_buffer_size);
970 },
Chen Chenf41721c2022-02-11 12:13:48 -0800971 is_low_latency_buffer_size));
Chen Chena81d2c62022-01-19 12:10:20 -0800972}
Chen Chen63cee1a2022-02-15 18:39:16 -0800973
974void invoke_switch_codec_cb(bool is_low_latency_buffer_size) {
975 do_in_jni_thread(FROM_HERE, base::BindOnce(
976 [](bool is_low_latency_buffer_size) {
977 HAL_CBACK(bt_hal_cbacks, switch_codec_cb,
978 is_low_latency_buffer_size);
979 },
980 is_low_latency_buffer_size));
981}