blob: 7ec4b5fa4eeee433889b07c9087ea5b3b5329d37 [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>
33#include <hardware/bt_gatt.h>
Hemant Gupta28425222014-04-18 12:34:55 +053034#include <hardware/bt_hd.h>
Jakub Pawlowski7d06e572018-02-28 22:44:42 -080035#include <hardware/bt_hearing_aid.h>
Hemant Guptad9e9cf92013-08-19 18:33:01 +053036#include <hardware/bt_hf_client.h>
The Android Open Source Project624a7872012-12-12 16:00:35 -080037#include <hardware/bt_hh.h>
Hemant Guptad0ecdbb2014-04-18 12:54:08 +053038#include <hardware/bt_mce.h>
Marie Janssene06e1512015-07-08 11:48:57 -070039#include <hardware/bt_pan.h>
Ravi Nagarajan78b35202013-03-06 05:29:48 -080040#include <hardware/bt_rc.h>
Kim Schulzb2337fe2015-03-25 10:39:40 +010041#include <hardware/bt_sdp.h>
Marie Janssene06e1512015-07-08 11:48:57 -070042#include <hardware/bt_sock.h>
Jack He028bf632020-08-04 01:28:36 -070043#include <stdio.h>
44#include <stdlib.h>
45#include <string.h>
46#include <unistd.h>
The Android Open Source Project624a7872012-12-12 16:00:35 -080047
Marie Janssene06e1512015-07-08 11:48:57 -070048#include "bt_utils.h"
Hansong Zhangc5df6b02018-05-09 14:39:56 -070049#include "bta/include/bta_hearing_aid_api.h"
Sanket Agarwald338a642016-11-30 19:43:47 -080050#include "bta/include/bta_hf_client_api.h"
Jakub Pawlowski4533a272018-05-25 09:31:26 -070051#include "btif/avrcp/avrcp_service.h"
Pavlin Radoslavovcd102012016-09-23 16:36:47 -070052#include "btif_a2dp.h"
The Android Open Source Project624a7872012-12-12 16:00:35 -080053#include "btif_api.h"
Pavlin Radoslavov5c6e64c2017-11-24 19:12:11 -080054#include "btif_av.h"
Ray4a73d962019-01-03 21:19:16 +080055#include "btif_bqr.h"
Myles Watson3f68a752016-10-19 09:50:22 -070056#include "btif_config.h"
Andre Eisenbach8f6c5be2014-12-05 09:40:20 -080057#include "btif_debug.h"
Jack Hee43b2372017-12-14 15:00:39 -080058#include "btif_debug_btsnoop.h"
59#include "btif_debug_conn.h"
60#include "btif_hf.h"
weichinweng6033dfb2020-04-21 09:42:27 +080061#include "btif_keystore.h"
Myles Watson3f68a752016-10-19 09:50:22 -070062#include "btif_storage.h"
Zach Johnson03234662014-09-22 22:11:55 -070063#include "btsnoop.h"
Andre Eisenbach8f6c5be2014-12-05 09:40:20 -080064#include "btsnoop_mem.h"
Jack He9183fd32018-11-16 02:59:43 -080065#include "common/address_obfuscator.h"
Chen Chen88ca6ae2020-03-05 15:02:46 -080066#include "common/metric_id_allocator.h"
Jack He42823982018-08-15 12:38:37 -070067#include "common/metrics.h"
Andre Eisenbach35c0c1d62016-01-14 02:00:24 -080068#include "device/include/interop.h"
Zach Johnson8ba246e2020-05-22 10:02:41 -070069#include "gd/common/init_flags.h"
Chris Mantoned916602020-01-13 21:11:48 -080070#include "main/shim/dumpsys.h"
Chris Mantonb0ecaa72019-09-11 19:53:32 -070071#include "main/shim/shim.h"
Pavlin Radoslavov30698fa2015-12-04 17:36:34 -080072#include "osi/include/alarm.h"
Myles Watson3f68a752016-10-19 09:50:22 -070073#include "osi/include/allocation_tracker.h"
Sharvil Nanavati83951772014-12-23 23:08:58 -080074#include "osi/include/log.h"
Marie Janssene06e1512015-07-08 11:48:57 -070075#include "osi/include/osi.h"
Pavlin Radoslavov5bb6cac2015-08-24 16:29:21 -070076#include "osi/include/wakelock.h"
Jakub Pawlowski6037e032018-11-13 15:41:42 +010077#include "stack/gatt/connection_manager.h"
Zach Johnsoned9491b2014-09-09 16:31:14 -070078#include "stack_manager.h"
The Android Open Source Project624a7872012-12-12 16:00:35 -080079
Jakub Pawlowski7d06e572018-02-28 22:44:42 -080080using bluetooth::hearing_aid::HearingAidInterface;
81
Myles Watsonfbfa1a52016-11-23 14:49:54 -080082/*******************************************************************************
Myles Watson3f68a752016-10-19 09:50:22 -070083 * Static variables
Myles Watsonfbfa1a52016-11-23 14:49:54 -080084 ******************************************************************************/
The Android Open Source Project624a7872012-12-12 16:00:35 -080085
Myles Watson3f68a752016-10-19 09:50:22 -070086bt_callbacks_t* bt_hal_cbacks = NULL;
Marie Janssen0fc1f132016-06-22 12:52:19 -070087bool restricted_mode = false;
weichinweng7107d8f2020-04-21 10:26:26 +080088bool niap_mode = false;
89const int CONFIG_COMPARE_ALL_PASS = 0b11;
90int niap_config_compare_result = CONFIG_COMPARE_ALL_PASS;
The Android Open Source Project624a7872012-12-12 16:00:35 -080091
Myles Watsonfbfa1a52016-11-23 14:49:54 -080092/*******************************************************************************
Myles Watson3f68a752016-10-19 09:50:22 -070093 * Externs
Myles Watsonfbfa1a52016-11-23 14:49:54 -080094 ******************************************************************************/
The Android Open Source Project624a7872012-12-12 16:00:35 -080095
96/* list all extended interfaces here */
97
Hemant Guptad9e9cf92013-08-19 18:33:01 +053098/* handsfree profile - client */
Jack He13d8b742017-12-04 14:30:42 -080099extern const bthf_client_interface_t* btif_hf_client_get_interface();
The Android Open Source Project624a7872012-12-12 16:00:35 -0800100/* advanced audio profile */
Jack He13d8b742017-12-04 14:30:42 -0800101extern const btav_source_interface_t* btif_av_get_src_interface();
102extern const btav_sink_interface_t* btif_av_get_sink_interface();
The Android Open Source Project624a7872012-12-12 16:00:35 -0800103/*rfc l2cap*/
Jack He13d8b742017-12-04 14:30:42 -0800104extern const btsock_interface_t* btif_sock_get_interface();
The Android Open Source Project624a7872012-12-12 16:00:35 -0800105/* hid host profile */
Jack He13d8b742017-12-04 14:30:42 -0800106extern const bthh_interface_t* btif_hh_get_interface();
Hemant Gupta28425222014-04-18 12:34:55 +0530107/* hid device profile */
Jack He13d8b742017-12-04 14:30:42 -0800108extern const bthd_interface_t* btif_hd_get_interface();
The Android Open Source Project624a7872012-12-12 16:00:35 -0800109/*pan*/
Jack He13d8b742017-12-04 14:30:42 -0800110extern const btpan_interface_t* btif_pan_get_interface();
Hemant Guptad0ecdbb2014-04-18 12:54:08 +0530111/*map client*/
Jack He13d8b742017-12-04 14:30:42 -0800112extern const btmce_interface_t* btif_mce_get_interface();
Ganesh Ganapathi Batta1def2552013-02-05 15:22:31 -0800113/* gatt */
Myles Watson3f68a752016-10-19 09:50:22 -0700114extern const btgatt_interface_t* btif_gatt_get_interface();
Mike Lockwoodc02ef772014-06-02 16:21:49 -0700115/* avrc target */
Jack He13d8b742017-12-04 14:30:42 -0800116extern const btrc_interface_t* btif_rc_get_interface();
Mike Lockwoodc02ef772014-06-02 16:21:49 -0700117/* avrc controller */
Jack He13d8b742017-12-04 14:30:42 -0800118extern const btrc_ctrl_interface_t* btif_rc_ctrl_get_interface();
Kim Schulzb2337fe2015-03-25 10:39:40 +0100119/*SDP search client*/
Jack He13d8b742017-12-04 14:30:42 -0800120extern const btsdp_interface_t* btif_sdp_get_interface();
Jakub Pawlowski7d06e572018-02-28 22:44:42 -0800121/*Hearing Aid client*/
122extern HearingAidInterface* btif_hearing_aid_get_interface();
The Android Open Source Project624a7872012-12-12 16:00:35 -0800123
Myles Watsonfbfa1a52016-11-23 14:49:54 -0800124/*******************************************************************************
Myles Watson3f68a752016-10-19 09:50:22 -0700125 * Functions
Myles Watsonfbfa1a52016-11-23 14:49:54 -0800126 ******************************************************************************/
The Android Open Source Project624a7872012-12-12 16:00:35 -0800127
Myles Watson3f68a752016-10-19 09:50:22 -0700128static bool interface_ready(void) { return bt_hal_cbacks != NULL; }
The Android Open Source Project624a7872012-12-12 16:00:35 -0800129
Myles Watson3f68a752016-10-19 09:50:22 -0700130static bool is_profile(const char* p1, const char* p2) {
Jack He358dd112016-12-13 01:59:12 -0800131 CHECK(p1);
132 CHECK(p2);
Andre Eisenbach80c23e52016-02-17 12:43:12 -0800133 return strlen(p1) == strlen(p2) && strncmp(p1, p2, strlen(p2)) == 0;
134}
135
The Android Open Source Project624a7872012-12-12 16:00:35 -0800136/*****************************************************************************
Myles Watson3f68a752016-10-19 09:50:22 -0700137 *
138 * BLUETOOTH HAL INTERFACE FUNCTIONS
139 *
140 ****************************************************************************/
The Android Open Source Project624a7872012-12-12 16:00:35 -0800141
Martin Brabhame02a8be2019-05-10 12:42:15 -0700142static int init(bt_callbacks_t* callbacks, bool start_restricted,
Zach Johnson8ba246e2020-05-22 10:02:41 -0700143 bool is_niap_mode, int config_compare_result,
144 const char** init_flags) {
weichinweng7107d8f2020-04-21 10:26:26 +0800145 LOG_INFO("%s: start restricted = %d ; niap = %d, config compare result = %d",
146 __func__, start_restricted, is_niap_mode, config_compare_result);
The Android Open Source Project624a7872012-12-12 16:00:35 -0800147
Zach Johnson8ba246e2020-05-22 10:02:41 -0700148 bluetooth::common::InitFlags::Load(init_flags);
Chris Mantonb0ecaa72019-09-11 19:53:32 -0700149
Myles Watson3f68a752016-10-19 09:50:22 -0700150 if (interface_ready()) return BT_STATUS_DONE;
The Android Open Source Project624a7872012-12-12 16:00:35 -0800151
Sharvil Nanavati19f4b182014-11-13 01:04:19 -0800152#ifdef BLUEDROID_DEBUG
153 allocation_tracker_init();
154#endif
155
Zach Johnsoned9491b2014-09-09 16:31:14 -0700156 bt_hal_cbacks = callbacks;
Martin Brabhame02a8be2019-05-10 12:42:15 -0700157 restricted_mode = start_restricted;
weichinweng7107d8f2020-04-21 10:26:26 +0800158 niap_mode = is_niap_mode;
159 niap_config_compare_result = config_compare_result;
160
Zach Johnsoned9491b2014-09-09 16:31:14 -0700161 stack_manager_get_interface()->init_stack();
Andre Eisenbach8f6c5be2014-12-05 09:40:20 -0800162 btif_debug_init();
Zach Johnsoned9491b2014-09-09 16:31:14 -0700163 return BT_STATUS_SUCCESS;
The Android Open Source Project624a7872012-12-12 16:00:35 -0800164}
165
Martin Brabhame02a8be2019-05-10 12:42:15 -0700166static int enable() {
Myles Watson3f68a752016-10-19 09:50:22 -0700167 if (!interface_ready()) return BT_STATUS_NOT_READY;
The Android Open Source Project624a7872012-12-12 16:00:35 -0800168
Zach Johnsoned9491b2014-09-09 16:31:14 -0700169 stack_manager_get_interface()->start_up_stack_async();
170 return BT_STATUS_SUCCESS;
The Android Open Source Project624a7872012-12-12 16:00:35 -0800171}
172
Zach Johnsoned9491b2014-09-09 16:31:14 -0700173static int disable(void) {
Myles Watson3f68a752016-10-19 09:50:22 -0700174 if (!interface_ready()) return BT_STATUS_NOT_READY;
The Android Open Source Project624a7872012-12-12 16:00:35 -0800175
Zach Johnsoned9491b2014-09-09 16:31:14 -0700176 stack_manager_get_interface()->shut_down_stack_async();
177 return BT_STATUS_SUCCESS;
The Android Open Source Project624a7872012-12-12 16:00:35 -0800178}
179
Myles Watson3f68a752016-10-19 09:50:22 -0700180static void cleanup(void) { stack_manager_get_interface()->clean_up_stack(); }
181
182bool is_restricted_mode() { return restricted_mode; }
weichinweng7107d8f2020-04-21 10:26:26 +0800183bool is_niap_mode() { return niap_mode; }
184// if niap mode disable, will always return CONFIG_COMPARE_ALL_PASS(0b11)
185// indicate don't check config checksum.
186int get_niap_config_compare_result() {
187 return niap_mode ? niap_config_compare_result : CONFIG_COMPARE_ALL_PASS;
188}
Myles Watson3f68a752016-10-19 09:50:22 -0700189
190static int get_adapter_properties(void) {
Zach Johnson3f7946d2020-08-14 10:11:14 -0700191 if (!btif_is_enabled()) return BT_STATUS_NOT_READY;
Myles Watson3f68a752016-10-19 09:50:22 -0700192
Zach Johnson3f7946d2020-08-14 10:11:14 -0700193 do_in_jni_thread(FROM_HERE, base::BindOnce(btif_get_adapter_properties));
194 return BT_STATUS_SUCCESS;
The Android Open Source Project624a7872012-12-12 16:00:35 -0800195}
196
Myles Watson3f68a752016-10-19 09:50:22 -0700197static int get_adapter_property(bt_property_type_t type) {
Zach Johnson1ee34862020-08-14 10:23:10 -0700198 /* Allow get_adapter_property only for BDADDR and BDNAME if BT is disabled */
199 if (!btif_is_enabled() && (type != BT_PROPERTY_BDADDR) &&
200 (type != BT_PROPERTY_BDNAME) && (type != BT_PROPERTY_CLASS_OF_DEVICE))
201 return BT_STATUS_NOT_READY;
Myles Watson3f68a752016-10-19 09:50:22 -0700202
Zach Johnson1ee34862020-08-14 10:23:10 -0700203 do_in_jni_thread(FROM_HERE, base::BindOnce(btif_get_adapter_property, type));
204 return BT_STATUS_SUCCESS;
Ajay Panickerc796ac82016-03-17 17:09:24 -0700205}
206
Myles Watson3f68a752016-10-19 09:50:22 -0700207static int set_adapter_property(const bt_property_t* property) {
Zach Johnsonf0f1c0c2020-08-14 11:24:35 -0700208 if (!btif_is_enabled()) return BT_STATUS_NOT_READY;
The Android Open Source Project624a7872012-12-12 16:00:35 -0800209
Zach Johnsonf0f1c0c2020-08-14 11:24:35 -0700210 switch (property->type) {
211 case BT_PROPERTY_BDNAME:
212 case BT_PROPERTY_ADAPTER_SCAN_MODE:
213 case BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT:
214 case BT_PROPERTY_CLASS_OF_DEVICE:
215 case BT_PROPERTY_LOCAL_IO_CAPS:
216 case BT_PROPERTY_LOCAL_IO_CAPS_BLE:
217 break;
218 default:
219 return BT_STATUS_FAIL;
220 }
221
222 do_in_jni_thread(FROM_HERE, base::BindOnce(
223 [](bt_property_t* property) {
224 btif_set_adapter_property(property);
225 osi_free(property);
226 },
227 property_deep_copy(property)));
228 return BT_STATUS_SUCCESS;
The Android Open Source Project624a7872012-12-12 16:00:35 -0800229}
230
Jakub Pawlowskid8e815c2017-06-24 17:30:18 -0700231int get_remote_device_properties(RawAddress* remote_addr) {
Zach Johnson28d5bde2020-08-14 12:07:19 -0700232 if (!btif_is_enabled()) return BT_STATUS_NOT_READY;
The Android Open Source Project624a7872012-12-12 16:00:35 -0800233
Zach Johnson28d5bde2020-08-14 12:07:19 -0700234 do_in_jni_thread(FROM_HERE, base::BindOnce(btif_get_remote_device_properties,
235 *remote_addr));
236 return BT_STATUS_SUCCESS;
The Android Open Source Project624a7872012-12-12 16:00:35 -0800237}
238
Jakub Pawlowskid8e815c2017-06-24 17:30:18 -0700239int get_remote_device_property(RawAddress* remote_addr,
Myles Watson3f68a752016-10-19 09:50:22 -0700240 bt_property_type_t type) {
Zach Johnsondbda78c2020-08-14 11:58:08 -0700241 if (!btif_is_enabled()) return BT_STATUS_NOT_READY;
The Android Open Source Project624a7872012-12-12 16:00:35 -0800242
Zach Johnsondbda78c2020-08-14 11:58:08 -0700243 do_in_jni_thread(FROM_HERE, base::BindOnce(btif_get_remote_device_property,
244 *remote_addr, type));
245 return BT_STATUS_SUCCESS;
The Android Open Source Project624a7872012-12-12 16:00:35 -0800246}
247
Jakub Pawlowskid8e815c2017-06-24 17:30:18 -0700248int set_remote_device_property(RawAddress* remote_addr,
Myles Watson3f68a752016-10-19 09:50:22 -0700249 const bt_property_t* property) {
Zach Johnson7cfca2e2020-08-14 12:17:08 -0700250 if (!btif_is_enabled()) return BT_STATUS_NOT_READY;
The Android Open Source Project624a7872012-12-12 16:00:35 -0800251
Zach Johnson7cfca2e2020-08-14 12:17:08 -0700252 do_in_jni_thread(
253 FROM_HERE, base::BindOnce(
254 [](RawAddress remote_addr, bt_property_t* property) {
255 btif_set_remote_device_property(&remote_addr, property);
256 osi_free(property);
257 },
258 *remote_addr, property_deep_copy(property)));
259 return BT_STATUS_SUCCESS;
The Android Open Source Project624a7872012-12-12 16:00:35 -0800260}
261
Jakub Pawlowski2238a882017-07-10 09:56:09 -0700262int get_remote_service_record(const RawAddress& remote_addr,
263 const bluetooth::Uuid& uuid) {
Zach Johnsonf1853832020-08-14 12:28:56 -0700264 if (!btif_is_enabled()) return BT_STATUS_NOT_READY;
The Android Open Source Project624a7872012-12-12 16:00:35 -0800265
Zach Johnsonb3d29782020-08-14 12:37:05 -0700266 do_in_jni_thread(FROM_HERE, base::BindOnce(btif_dm_get_remote_service_record,
Zach Johnsonf1853832020-08-14 12:28:56 -0700267 remote_addr, uuid));
268 return BT_STATUS_SUCCESS;
The Android Open Source Project624a7872012-12-12 16:00:35 -0800269}
270
Jakub Pawlowskid8e815c2017-06-24 17:30:18 -0700271int get_remote_services(RawAddress* remote_addr) {
Myles Watson6a7e1ac2017-10-06 16:39:05 -0700272 if (!interface_ready()) return BT_STATUS_NOT_READY;
The Android Open Source Project624a7872012-12-12 16:00:35 -0800273
Zach Johnson947f2652020-08-14 12:34:06 -0700274 do_in_jni_thread(FROM_HERE,
275 base::BindOnce(btif_dm_get_remote_services, *remote_addr));
276 return BT_STATUS_SUCCESS;
The Android Open Source Project624a7872012-12-12 16:00:35 -0800277}
278
Myles Watson3f68a752016-10-19 09:50:22 -0700279static int start_discovery(void) {
Myles Watson6a7e1ac2017-10-06 16:39:05 -0700280 if (!interface_ready()) return BT_STATUS_NOT_READY;
The Android Open Source Project624a7872012-12-12 16:00:35 -0800281
Zach Johnson1653a202020-08-14 12:44:42 -0700282 do_in_jni_thread(FROM_HERE, base::BindOnce(btif_dm_start_discovery));
283 return BT_STATUS_SUCCESS;
The Android Open Source Project624a7872012-12-12 16:00:35 -0800284}
285
Myles Watson3f68a752016-10-19 09:50:22 -0700286static int cancel_discovery(void) {
Myles Watson6a7e1ac2017-10-06 16:39:05 -0700287 if (!interface_ready()) return BT_STATUS_NOT_READY;
The Android Open Source Project624a7872012-12-12 16:00:35 -0800288
Zach Johnson5cd06df2020-08-14 12:47:29 -0700289 do_in_jni_thread(FROM_HERE, base::BindOnce(btif_dm_cancel_discovery));
290 return BT_STATUS_SUCCESS;
The Android Open Source Project624a7872012-12-12 16:00:35 -0800291}
292
Jakub Pawlowskid8e815c2017-06-24 17:30:18 -0700293static int create_bond(const RawAddress* bd_addr, int transport) {
Myles Watson6a7e1ac2017-10-06 16:39:05 -0700294 if (!interface_ready()) return BT_STATUS_NOT_READY;
Zach Johnson35f416e2020-08-14 12:52:58 -0700295 if (btif_dm_pairing_is_busy()) return BT_STATUS_BUSY;
The Android Open Source Project624a7872012-12-12 16:00:35 -0800296
Zach Johnson35f416e2020-08-14 12:52:58 -0700297 do_in_jni_thread(FROM_HERE,
298 base::BindOnce(btif_dm_create_bond, *bd_addr, transport));
299 return BT_STATUS_SUCCESS;
The Android Open Source Project624a7872012-12-12 16:00:35 -0800300}
301
Jakub Pawlowskid8e815c2017-06-24 17:30:18 -0700302static int create_bond_out_of_band(const RawAddress* bd_addr, int transport,
Myles Watson3f68a752016-10-19 09:50:22 -0700303 const bt_out_of_band_data_t* oob_data) {
Myles Watson6a7e1ac2017-10-06 16:39:05 -0700304 if (!interface_ready()) return BT_STATUS_NOT_READY;
Zach Johnson106ceb42020-08-14 13:00:54 -0700305 if (btif_dm_pairing_is_busy()) return BT_STATUS_BUSY;
The Android Open Source Project624a7872012-12-12 16:00:35 -0800306
Zach Johnson106ceb42020-08-14 13:00:54 -0700307 do_in_jni_thread(FROM_HERE, base::BindOnce(btif_dm_create_bond_out_of_band,
308 *bd_addr, transport, *oob_data));
309 return BT_STATUS_SUCCESS;
The Android Open Source Project624a7872012-12-12 16:00:35 -0800310}
311
Jakub Pawlowskid8e815c2017-06-24 17:30:18 -0700312static int cancel_bond(const RawAddress* bd_addr) {
Myles Watson6a7e1ac2017-10-06 16:39:05 -0700313 if (!interface_ready()) return BT_STATUS_NOT_READY;
The Android Open Source Project624a7872012-12-12 16:00:35 -0800314
Zach Johnson030162a2020-08-14 13:06:01 -0700315 do_in_jni_thread(FROM_HERE, base::BindOnce(btif_dm_cancel_bond, *bd_addr));
316 return BT_STATUS_SUCCESS;
The Android Open Source Project624a7872012-12-12 16:00:35 -0800317}
318
Jakub Pawlowskid8e815c2017-06-24 17:30:18 -0700319static int remove_bond(const RawAddress* bd_addr) {
Myles Watson3f68a752016-10-19 09:50:22 -0700320 if (is_restricted_mode() && !btif_storage_is_restricted_device(bd_addr))
Satya Calloji75fd8802014-07-03 17:18:02 -0700321 return BT_STATUS_SUCCESS;
Myles Watson3f68a752016-10-19 09:50:22 -0700322
Myles Watson6a7e1ac2017-10-06 16:39:05 -0700323 if (!interface_ready()) return BT_STATUS_NOT_READY;
Myles Watson3f68a752016-10-19 09:50:22 -0700324
Zach Johnson62b79ea2020-08-14 13:08:57 -0700325 do_in_jni_thread(FROM_HERE, base::BindOnce(btif_dm_remove_bond, *bd_addr));
326 return BT_STATUS_SUCCESS;
Satya Calloji75fd8802014-07-03 17:18:02 -0700327}
328
Jakub Pawlowskid8e815c2017-06-24 17:30:18 -0700329static int get_connection_state(const RawAddress* bd_addr) {
Myles Watson6a7e1ac2017-10-06 16:39:05 -0700330 if (!interface_ready()) return 0;
Myles Watson3f68a752016-10-19 09:50:22 -0700331
332 return btif_dm_get_connection_state(bd_addr);
333}
334
Jakub Pawlowskid8e815c2017-06-24 17:30:18 -0700335static int pin_reply(const RawAddress* bd_addr, uint8_t accept, uint8_t pin_len,
336 bt_pin_code_t* pin_code) {
Myles Watson6a7e1ac2017-10-06 16:39:05 -0700337 if (!interface_ready()) return BT_STATUS_NOT_READY;
Zach Johnsonda4e25f2020-08-14 13:39:45 -0700338 if (pin_code == nullptr || pin_len > PIN_CODE_LEN) return BT_STATUS_FAIL;
Myles Watson3f68a752016-10-19 09:50:22 -0700339
Zach Johnsonda4e25f2020-08-14 13:39:45 -0700340 do_in_jni_thread(FROM_HERE, base::BindOnce(btif_dm_pin_reply, *bd_addr,
341 accept, pin_len, *pin_code));
342 return BT_STATUS_SUCCESS;
Myles Watson3f68a752016-10-19 09:50:22 -0700343}
344
Jakub Pawlowskid8e815c2017-06-24 17:30:18 -0700345static int ssp_reply(const RawAddress* bd_addr, bt_ssp_variant_t variant,
Myles Watson3f68a752016-10-19 09:50:22 -0700346 uint8_t accept, uint32_t passkey) {
347 /* sanity check */
Myles Watson6a7e1ac2017-10-06 16:39:05 -0700348 if (!interface_ready()) return BT_STATUS_NOT_READY;
Myles Watson3f68a752016-10-19 09:50:22 -0700349
350 return btif_dm_ssp_reply(bd_addr, variant, accept, passkey);
351}
352
353static int read_energy_info() {
Myles Watson6a7e1ac2017-10-06 16:39:05 -0700354 if (!interface_ready()) return BT_STATUS_NOT_READY;
Myles Watson3f68a752016-10-19 09:50:22 -0700355 btif_dm_read_energy_info();
356 return BT_STATUS_SUCCESS;
357}
358
359static void dump(int fd, const char** arguments) {
Myles Watson3f68a752016-10-19 09:50:22 -0700360 btif_debug_conn_dump(fd);
361 btif_debug_bond_event_dump(fd);
362 btif_debug_a2dp_dump(fd);
Pavlin Radoslavov5c6e64c2017-11-24 19:12:11 -0800363 btif_debug_av_dump(fd);
364 bta_debug_av_dump(fd);
365 stack_debug_avdtp_api_dump(fd);
Ajay Panicker13d845b2018-05-02 16:23:13 -0700366 bluetooth::avrcp::AvrcpService::DebugDump(fd);
Myles Watson3f68a752016-10-19 09:50:22 -0700367 btif_debug_config_dump(fd);
Sanket Agarwald338a642016-11-30 19:43:47 -0800368 BTA_HfClientDumpStatistics(fd);
Myles Watson3f68a752016-10-19 09:50:22 -0700369 wakelock_debug_dump(fd);
Pavlin Radoslavov64c30932017-02-01 03:55:33 -0800370 osi_allocator_debug_dump(fd);
Myles Watson3f68a752016-10-19 09:50:22 -0700371 alarm_debug_dump(fd);
Hansong Zhangc5df6b02018-05-09 14:39:56 -0700372 HearingAid::DebugDump(fd);
Jakub Pawlowskie80bfd52018-12-28 19:03:09 +0100373 connection_manager::dump(fd);
Ray4a73d962019-01-03 21:19:16 +0800374 bluetooth::bqr::DebugDump(fd);
Jack He028bf632020-08-04 01:28:36 -0700375 if (bluetooth::shim::is_any_gd_enabled()) {
Chris Mantond127e852020-04-20 20:04:42 -0700376 bluetooth::shim::Dump(fd, arguments);
Chris Mantoned916602020-01-13 21:11:48 -0800377 } else {
Marie Janssen0fc1f132016-06-22 12:52:19 -0700378#if (BTSNOOP_MEM == TRUE)
Chris Mantoned916602020-01-13 21:11:48 -0800379 btif_debug_btsnoop_dump(fd);
Pavlin Radoslavov30698fa2015-12-04 17:36:34 -0800380#endif
Chris Mantoned916602020-01-13 21:11:48 -0800381 }
Andre Eisenbach8f6c5be2014-12-05 09:40:20 -0800382}
383
Jack Hed20346ab2018-01-31 16:51:26 -0800384static void dumpMetrics(std::string* output) {
Jack He42823982018-08-15 12:38:37 -0700385 bluetooth::common::BluetoothMetricsLogger::GetInstance()->WriteString(output);
Jack Hed20346ab2018-01-31 16:51:26 -0800386}
387
Myles Watson3f68a752016-10-19 09:50:22 -0700388static const void* get_profile_interface(const char* profile_id) {
Jakub Pawlowski13ee9b82020-03-05 21:16:10 +0100389 LOG_INFO("%s: id = %s", __func__, profile_id);
The Android Open Source Project624a7872012-12-12 16:00:35 -0800390
Myles Watson3f68a752016-10-19 09:50:22 -0700391 /* sanity check */
Myles Watson6a7e1ac2017-10-06 16:39:05 -0700392 if (!interface_ready()) return NULL;
The Android Open Source Project624a7872012-12-12 16:00:35 -0800393
Myles Watson3f68a752016-10-19 09:50:22 -0700394 /* check for supported profile interfaces */
395 if (is_profile(profile_id, BT_PROFILE_HANDSFREE_ID))
Jack Hee43b2372017-12-14 15:00:39 -0800396 return bluetooth::headset::GetInterface();
The Android Open Source Project624a7872012-12-12 16:00:35 -0800397
Myles Watson3f68a752016-10-19 09:50:22 -0700398 if (is_profile(profile_id, BT_PROFILE_HANDSFREE_CLIENT_ID))
399 return btif_hf_client_get_interface();
Hemant Guptad9e9cf92013-08-19 18:33:01 +0530400
Myles Watson3f68a752016-10-19 09:50:22 -0700401 if (is_profile(profile_id, BT_PROFILE_SOCKETS_ID))
402 return btif_sock_get_interface();
The Android Open Source Project624a7872012-12-12 16:00:35 -0800403
Myles Watson3f68a752016-10-19 09:50:22 -0700404 if (is_profile(profile_id, BT_PROFILE_PAN_ID))
405 return btif_pan_get_interface();
The Android Open Source Project624a7872012-12-12 16:00:35 -0800406
Myles Watson3f68a752016-10-19 09:50:22 -0700407 if (is_profile(profile_id, BT_PROFILE_ADVANCED_AUDIO_ID))
408 return btif_av_get_src_interface();
Mike Lockwoodc80e2d72014-05-23 12:42:24 -0700409
Myles Watson3f68a752016-10-19 09:50:22 -0700410 if (is_profile(profile_id, BT_PROFILE_ADVANCED_AUDIO_SINK_ID))
411 return btif_av_get_sink_interface();
The Android Open Source Project624a7872012-12-12 16:00:35 -0800412
Myles Watson3f68a752016-10-19 09:50:22 -0700413 if (is_profile(profile_id, BT_PROFILE_HIDHOST_ID))
414 return btif_hh_get_interface();
The Android Open Source Project624a7872012-12-12 16:00:35 -0800415
Hemant Gupta28425222014-04-18 12:34:55 +0530416 if (is_profile(profile_id, BT_PROFILE_HIDDEV_ID))
417 return btif_hd_get_interface();
418
Myles Watson3f68a752016-10-19 09:50:22 -0700419 if (is_profile(profile_id, BT_PROFILE_SDP_CLIENT_ID))
420 return btif_sdp_get_interface();
Hemant Guptad0ecdbb2014-04-18 12:54:08 +0530421
Myles Watson3f68a752016-10-19 09:50:22 -0700422 if (is_profile(profile_id, BT_PROFILE_GATT_ID))
423 return btif_gatt_get_interface();
Ravi Nagarajan78b35202013-03-06 05:29:48 -0800424
Myles Watson3f68a752016-10-19 09:50:22 -0700425 if (is_profile(profile_id, BT_PROFILE_AV_RC_ID))
426 return btif_rc_get_interface();
Ravi Nagarajan78b35202013-03-06 05:29:48 -0800427
Myles Watson3f68a752016-10-19 09:50:22 -0700428 if (is_profile(profile_id, BT_PROFILE_AV_RC_CTRL_ID))
429 return btif_rc_ctrl_get_interface();
Mike Lockwoodc02ef772014-06-02 16:21:49 -0700430
Jakub Pawlowski7d06e572018-02-28 22:44:42 -0800431 if (is_profile(profile_id, BT_PROFILE_HEARING_AID_ID))
432 return btif_hearing_aid_get_interface();
weichinweng6033dfb2020-04-21 09:42:27 +0800433
434 if (is_profile(profile_id, BT_KEYSTORE_ID))
435 return bluetooth::bluetooth_keystore::getBluetoothKeystoreInterface();
Myles Watson3f68a752016-10-19 09:50:22 -0700436 return NULL;
The Android Open Source Project624a7872012-12-12 16:00:35 -0800437}
438
Myles Watson3f68a752016-10-19 09:50:22 -0700439int dut_mode_configure(uint8_t enable) {
Jakub Pawlowski13ee9b82020-03-05 21:16:10 +0100440 LOG_INFO("%s", __func__);
The Android Open Source Project624a7872012-12-12 16:00:35 -0800441
Myles Watson3f68a752016-10-19 09:50:22 -0700442 /* sanity check */
Myles Watson6a7e1ac2017-10-06 16:39:05 -0700443 if (!interface_ready()) return BT_STATUS_NOT_READY;
The Android Open Source Project624a7872012-12-12 16:00:35 -0800444
Myles Watson3f68a752016-10-19 09:50:22 -0700445 return btif_dut_mode_configure(enable);
The Android Open Source Project624a7872012-12-12 16:00:35 -0800446}
447
Myles Watson3f68a752016-10-19 09:50:22 -0700448int dut_mode_send(uint16_t opcode, uint8_t* buf, uint8_t len) {
Jakub Pawlowski13ee9b82020-03-05 21:16:10 +0100449 LOG_INFO("%s", __func__);
The Android Open Source Project624a7872012-12-12 16:00:35 -0800450
Myles Watson3f68a752016-10-19 09:50:22 -0700451 /* sanity check */
Myles Watson6a7e1ac2017-10-06 16:39:05 -0700452 if (!interface_ready()) return BT_STATUS_NOT_READY;
The Android Open Source Project624a7872012-12-12 16:00:35 -0800453
Myles Watson3f68a752016-10-19 09:50:22 -0700454 return btif_dut_mode_send(opcode, buf, len);
The Android Open Source Project624a7872012-12-12 16:00:35 -0800455}
Matthew Xie41fb4362013-05-06 20:51:02 -0700456
Myles Watson3f68a752016-10-19 09:50:22 -0700457int le_test_mode(uint16_t opcode, uint8_t* buf, uint8_t len) {
Jakub Pawlowski13ee9b82020-03-05 21:16:10 +0100458 LOG_INFO("%s", __func__);
Ganesh Ganapathi Batta1def2552013-02-05 15:22:31 -0800459
Myles Watson3f68a752016-10-19 09:50:22 -0700460 /* sanity check */
Myles Watson6a7e1ac2017-10-06 16:39:05 -0700461 if (!interface_ready()) return BT_STATUS_NOT_READY;
Ganesh Ganapathi Batta1def2552013-02-05 15:22:31 -0800462
Myles Watson3f68a752016-10-19 09:50:22 -0700463 return btif_le_test_mode(opcode, buf, len);
Ganesh Ganapathi Batta1def2552013-02-05 15:22:31 -0800464}
Ganesh Ganapathi Batta1def2552013-02-05 15:22:31 -0800465
Pavlin Radoslavov34bd6332018-08-24 18:45:16 -0700466static bt_os_callouts_t* wakelock_os_callouts_saved = nullptr;
467
468static int acquire_wake_lock_cb(const char* lock_name) {
469 return do_in_jni_thread(
470 FROM_HERE, base::Bind(base::IgnoreResult(
471 wakelock_os_callouts_saved->acquire_wake_lock),
472 lock_name));
473}
474
475static int release_wake_lock_cb(const char* lock_name) {
476 return do_in_jni_thread(
477 FROM_HERE, base::Bind(base::IgnoreResult(
478 wakelock_os_callouts_saved->release_wake_lock),
479 lock_name));
480}
481
482static bt_os_callouts_t wakelock_os_callouts_jni = {
483 sizeof(wakelock_os_callouts_jni),
484 nullptr /* not used */,
485 acquire_wake_lock_cb,
486 release_wake_lock_cb,
487};
488
Myles Watson3f68a752016-10-19 09:50:22 -0700489static int set_os_callouts(bt_os_callouts_t* callouts) {
Pavlin Radoslavov34bd6332018-08-24 18:45:16 -0700490 wakelock_os_callouts_saved = callouts;
491 wakelock_set_os_callouts(&wakelock_os_callouts_jni);
Myles Watson3f68a752016-10-19 09:50:22 -0700492 return BT_STATUS_SUCCESS;
Sharvil Nanavati55e690a2014-05-28 17:09:46 -0700493}
494
Ajay Panickerec102082015-07-28 16:54:53 -0700495static int config_clear(void) {
Jakub Pawlowski13ee9b82020-03-05 21:16:10 +0100496 LOG_INFO("%s", __func__);
Myles Watson3f68a752016-10-19 09:50:22 -0700497 return btif_config_clear() ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
Ajay Panickerec102082015-07-28 16:54:53 -0700498}
499
Ajay Panicker9e1c9582018-03-14 23:45:54 -0700500static bluetooth::avrcp::ServiceInterface* get_avrcp_service(void) {
501 return bluetooth::avrcp::AvrcpService::GetServiceInterface();
502}
503
Jack He9183fd32018-11-16 02:59:43 -0800504static std::string obfuscate_address(const RawAddress& address) {
505 return bluetooth::common::AddressObfuscator::GetInstance()->Obfuscate(
506 address);
507}
508
Chen Chen88ca6ae2020-03-05 15:02:46 -0800509static int get_metric_id(const RawAddress& address) {
510 return bluetooth::common::MetricIdAllocator::GetInstance().AllocateId(
511 address);
512}
513
Jakub Pawlowski5f52ab02017-10-16 15:57:16 -0700514EXPORT_SYMBOL bt_interface_t bluetoothInterface = {
Ganesh Ganapathi Batta1def2552013-02-05 15:22:31 -0800515 sizeof(bluetoothInterface),
The Android Open Source Project624a7872012-12-12 16:00:35 -0800516 init,
517 enable,
518 disable,
519 cleanup,
520 get_adapter_properties,
521 get_adapter_property,
522 set_adapter_property,
523 get_remote_device_properties,
524 get_remote_device_property,
525 set_remote_device_property,
526 get_remote_service_record,
527 get_remote_services,
528 start_discovery,
529 cancel_discovery,
530 create_bond,
Jakub Pawlowskidf0ab812015-12-01 12:14:22 -0800531 create_bond_out_of_band,
The Android Open Source Project624a7872012-12-12 16:00:35 -0800532 remove_bond,
533 cancel_bond,
Andre Eisenbach4a6f8cf2014-06-18 12:20:37 -0700534 get_connection_state,
The Android Open Source Project624a7872012-12-12 16:00:35 -0800535 pin_reply,
536 ssp_reply,
537 get_profile_interface,
538 dut_mode_configure,
Ganesh Ganapathi Batta1def2552013-02-05 15:22:31 -0800539 dut_mode_send,
Zhihai Xu8572d792013-06-04 18:21:25 -0700540 le_test_mode,
Sharvil Nanavati55e690a2014-05-28 17:09:46 -0700541 set_os_callouts,
Satya Calloji75fd8802014-07-03 17:18:02 -0700542 read_energy_info,
Ajay Panickerec102082015-07-28 16:54:53 -0700543 dump,
Jack Hed20346ab2018-01-31 16:51:26 -0800544 dumpMetrics,
Andre Eisenbach35c0c1d62016-01-14 02:00:24 -0800545 config_clear,
546 interop_database_clear,
547 interop_database_add,
Ajay Panicker9e1c9582018-03-14 23:45:54 -0700548 get_avrcp_service,
Jack He9183fd32018-11-16 02:59:43 -0800549 obfuscate_address,
Chen Chen88ca6ae2020-03-05 15:02:46 -0800550 get_metric_id,
The Android Open Source Project624a7872012-12-12 16:00:35 -0800551};