blob: a00c0c662fddc2c0449d5ca103a86bf2b721b552 [file] [log] [blame]
The Android Open Source Project5738f832012-12-12 16:00:35 -08001/******************************************************************************
2 *
Jakub Pawlowski5b790fe2017-09-18 09:00:20 -07003 * Copyright 2009-2012 Broadcom Corporation
The Android Open Source Project5738f832012-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 Watsonee96a3c2016-11-23 14:49:54 -080019/*******************************************************************************
The Android Open Source Project5738f832012-12-12 16:00:35 -080020 *
21 * Filename: bluetooth.c
22 *
23 * Description: Bluetooth HAL implementation
24 *
Myles Watsonee96a3c2016-11-23 14:49:54 -080025 ******************************************************************************/
The Android Open Source Project5738f832012-12-12 16:00:35 -080026
Andre Eisenbachb7a077b2015-10-29 11:18:20 -070027#define LOG_TAG "bt_btif"
Marie Janssen49a86702015-07-08 11:48:57 -070028
Jack Hef2af1c42016-12-13 01:59:12 -080029#include <base/logging.h>
The Android Open Source Project5738f832012-12-12 16:00:35 -080030#include <stdio.h>
31#include <stdlib.h>
Scott James Remnant933926c2015-04-02 15:22:14 -070032#include <string.h>
The Android Open Source Project5738f832012-12-12 16:00:35 -080033#include <unistd.h>
34
35#include <hardware/bluetooth.h>
Jack He12a576d2017-12-14 15:00:39 -080036#include <hardware/bluetooth_headset_interface.h>
Marie Janssen49a86702015-07-08 11:48:57 -070037#include <hardware/bt_av.h>
38#include <hardware/bt_gatt.h>
Hemant Gupta8843cc82014-04-18 12:34:55 +053039#include <hardware/bt_hd.h>
Jakub Pawlowski89b0ccb2018-02-28 22:44:42 -080040#include <hardware/bt_hearing_aid.h>
Hemant Gupta41d4a262013-08-19 18:33:01 +053041#include <hardware/bt_hf_client.h>
The Android Open Source Project5738f832012-12-12 16:00:35 -080042#include <hardware/bt_hh.h>
Hemant Gupta2dc99992014-04-18 12:54:08 +053043#include <hardware/bt_mce.h>
Marie Janssen49a86702015-07-08 11:48:57 -070044#include <hardware/bt_pan.h>
Ravi Nagarajan9adddf42013-03-06 05:29:48 -080045#include <hardware/bt_rc.h>
Kim Schulz8372aa52015-03-25 10:39:40 +010046#include <hardware/bt_sdp.h>
Marie Janssen49a86702015-07-08 11:48:57 -070047#include <hardware/bt_sock.h>
The Android Open Source Project5738f832012-12-12 16:00:35 -080048
Marie Janssen49a86702015-07-08 11:48:57 -070049#include "bt_utils.h"
Hansong Zhangb596bb12018-05-09 14:39:56 -070050#include "bta/include/bta_hearing_aid_api.h"
Sanket Agarwal5eccd902016-11-30 19:43:47 -080051#include "bta/include/bta_hf_client_api.h"
Jakub Pawlowski9e814612018-05-25 09:31:26 -070052#include "btif/avrcp/avrcp_service.h"
Pavlin Radoslavov08406e92016-09-23 16:36:47 -070053#include "btif_a2dp.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080054#include "btif_api.h"
Pavlin Radoslavovd7522292017-11-24 19:12:11 -080055#include "btif_av.h"
Ray6e632472019-01-03 21:19:16 +080056#include "btif_bqr.h"
Myles Watson6bd442f2016-10-19 09:50:22 -070057#include "btif_config.h"
Andre Eisenbach89f5e412014-12-05 09:40:20 -080058#include "btif_debug.h"
Jack He12a576d2017-12-14 15:00:39 -080059#include "btif_debug_btsnoop.h"
60#include "btif_debug_conn.h"
61#include "btif_hf.h"
weichinwengb30bb982020-04-21 09:42:27 +080062#include "btif_keystore.h"
Myles Watson6bd442f2016-10-19 09:50:22 -070063#include "btif_storage.h"
Zach Johnson9891f322014-09-22 22:11:55 -070064#include "btsnoop.h"
Andre Eisenbach89f5e412014-12-05 09:40:20 -080065#include "btsnoop_mem.h"
Jack He78b69d92018-11-16 02:59:43 -080066#include "common/address_obfuscator.h"
Chen Chen4f6a3b22020-03-05 15:02:46 -080067#include "common/metric_id_allocator.h"
Jack He959bc332018-08-15 12:38:37 -070068#include "common/metrics.h"
Andre Eisenbache42d1d72016-01-14 02:00:24 -080069#include "device/include/interop.h"
Chris Manton0cb19c82020-01-13 21:11:48 -080070#include "main/shim/dumpsys.h"
Chris Mantona2a9faa2019-09-11 19:53:32 -070071#include "main/shim/shim.h"
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -080072#include "osi/include/alarm.h"
Myles Watson6bd442f2016-10-19 09:50:22 -070073#include "osi/include/allocation_tracker.h"
Sharvil Nanavati44802762014-12-23 23:08:58 -080074#include "osi/include/log.h"
Marie Janssen49a86702015-07-08 11:48:57 -070075#include "osi/include/osi.h"
Pavlin Radoslavovd2e25082015-08-24 16:29:21 -070076#include "osi/include/wakelock.h"
Jakub Pawlowski8ae68be2018-11-13 15:41:42 +010077#include "stack/gatt/connection_manager.h"
Zach Johnson8df0d802014-09-09 16:31:14 -070078#include "stack_manager.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080079
Jakub Pawlowski89b0ccb2018-02-28 22:44:42 -080080using bluetooth::hearing_aid::HearingAidInterface;
81
Myles Watsonee96a3c2016-11-23 14:49:54 -080082/*******************************************************************************
Myles Watson6bd442f2016-10-19 09:50:22 -070083 * Static variables
Myles Watsonee96a3c2016-11-23 14:49:54 -080084 ******************************************************************************/
The Android Open Source Project5738f832012-12-12 16:00:35 -080085
Myles Watson6bd442f2016-10-19 09:50:22 -070086bt_callbacks_t* bt_hal_cbacks = NULL;
Marie Janssenb7f64bc2016-06-22 12:52:19 -070087bool restricted_mode = false;
weichinweng14b98e02020-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;
Rahul Sabnise198eb92020-07-31 19:44:27 -070091bool is_local_device_atv = false;
The Android Open Source Project5738f832012-12-12 16:00:35 -080092
Myles Watsonee96a3c2016-11-23 14:49:54 -080093/*******************************************************************************
Myles Watson6bd442f2016-10-19 09:50:22 -070094 * Externs
Myles Watsonee96a3c2016-11-23 14:49:54 -080095 ******************************************************************************/
The Android Open Source Project5738f832012-12-12 16:00:35 -080096
97/* list all extended interfaces here */
98
Hemant Gupta41d4a262013-08-19 18:33:01 +053099/* handsfree profile - client */
Jack Heff997912017-12-04 14:30:42 -0800100extern const bthf_client_interface_t* btif_hf_client_get_interface();
The Android Open Source Project5738f832012-12-12 16:00:35 -0800101/* advanced audio profile */
Jack Heff997912017-12-04 14:30:42 -0800102extern const btav_source_interface_t* btif_av_get_src_interface();
103extern const btav_sink_interface_t* btif_av_get_sink_interface();
The Android Open Source Project5738f832012-12-12 16:00:35 -0800104/*rfc l2cap*/
Jack Heff997912017-12-04 14:30:42 -0800105extern const btsock_interface_t* btif_sock_get_interface();
The Android Open Source Project5738f832012-12-12 16:00:35 -0800106/* hid host profile */
Jack Heff997912017-12-04 14:30:42 -0800107extern const bthh_interface_t* btif_hh_get_interface();
Hemant Gupta8843cc82014-04-18 12:34:55 +0530108/* hid device profile */
Jack Heff997912017-12-04 14:30:42 -0800109extern const bthd_interface_t* btif_hd_get_interface();
The Android Open Source Project5738f832012-12-12 16:00:35 -0800110/*pan*/
Jack Heff997912017-12-04 14:30:42 -0800111extern const btpan_interface_t* btif_pan_get_interface();
Hemant Gupta2dc99992014-04-18 12:54:08 +0530112/*map client*/
Jack Heff997912017-12-04 14:30:42 -0800113extern const btmce_interface_t* btif_mce_get_interface();
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800114/* gatt */
Myles Watson6bd442f2016-10-19 09:50:22 -0700115extern const btgatt_interface_t* btif_gatt_get_interface();
Mike Lockwood4ad470e2014-06-02 16:21:49 -0700116/* avrc target */
Jack Heff997912017-12-04 14:30:42 -0800117extern const btrc_interface_t* btif_rc_get_interface();
Mike Lockwood4ad470e2014-06-02 16:21:49 -0700118/* avrc controller */
Jack Heff997912017-12-04 14:30:42 -0800119extern const btrc_ctrl_interface_t* btif_rc_ctrl_get_interface();
Kim Schulz8372aa52015-03-25 10:39:40 +0100120/*SDP search client*/
Jack Heff997912017-12-04 14:30:42 -0800121extern const btsdp_interface_t* btif_sdp_get_interface();
Jakub Pawlowski89b0ccb2018-02-28 22:44:42 -0800122/*Hearing Aid client*/
123extern HearingAidInterface* btif_hearing_aid_get_interface();
The Android Open Source Project5738f832012-12-12 16:00:35 -0800124
Myles Watsonee96a3c2016-11-23 14:49:54 -0800125/*******************************************************************************
Myles Watson6bd442f2016-10-19 09:50:22 -0700126 * Functions
Myles Watsonee96a3c2016-11-23 14:49:54 -0800127 ******************************************************************************/
The Android Open Source Project5738f832012-12-12 16:00:35 -0800128
Myles Watson6bd442f2016-10-19 09:50:22 -0700129static bool interface_ready(void) { return bt_hal_cbacks != NULL; }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800130
Myles Watson6bd442f2016-10-19 09:50:22 -0700131static bool is_profile(const char* p1, const char* p2) {
Jack Hef2af1c42016-12-13 01:59:12 -0800132 CHECK(p1);
133 CHECK(p2);
Andre Eisenbachdf5c69c2016-02-17 12:43:12 -0800134 return strlen(p1) == strlen(p2) && strncmp(p1, p2, strlen(p2)) == 0;
135}
136
The Android Open Source Project5738f832012-12-12 16:00:35 -0800137/*****************************************************************************
Myles Watson6bd442f2016-10-19 09:50:22 -0700138 *
139 * BLUETOOTH HAL INTERFACE FUNCTIONS
140 *
141 ****************************************************************************/
The Android Open Source Project5738f832012-12-12 16:00:35 -0800142
Martin Brabham1a88ace2019-05-10 12:42:15 -0700143static int init(bt_callbacks_t* callbacks, bool start_restricted,
Rahul Sabnise198eb92020-07-31 19:44:27 -0700144 bool is_niap_mode, int config_compare_result, bool is_atv) {
weichinweng14b98e02020-04-21 10:26:26 +0800145 LOG_INFO(LOG_TAG,
146 "%s: start restricted = %d ; niap = %d, config compare result = %d",
147 __func__, start_restricted, is_niap_mode, config_compare_result);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800148
Chris Mantona2a9faa2019-09-11 19:53:32 -0700149 if (bluetooth::shim::is_gd_shim_enabled()) {
150 LOG_INFO(LOG_TAG, "%s Enable Gd bluetooth functionality", __func__);
151 } else {
152 LOG_INFO(LOG_TAG, "%s Preserving legacy bluetooth functionality", __func__);
153 }
154
Myles Watson6bd442f2016-10-19 09:50:22 -0700155 if (interface_ready()) return BT_STATUS_DONE;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800156
Sharvil Nanavatic0745da2014-11-13 01:04:19 -0800157#ifdef BLUEDROID_DEBUG
158 allocation_tracker_init();
159#endif
160
Zach Johnson8df0d802014-09-09 16:31:14 -0700161 bt_hal_cbacks = callbacks;
Martin Brabham1a88ace2019-05-10 12:42:15 -0700162 restricted_mode = start_restricted;
weichinweng14b98e02020-04-21 10:26:26 +0800163 niap_mode = is_niap_mode;
164 niap_config_compare_result = config_compare_result;
Rahul Sabnise198eb92020-07-31 19:44:27 -0700165 is_local_device_atv = is_atv;
weichinweng14b98e02020-04-21 10:26:26 +0800166
Zach Johnson8df0d802014-09-09 16:31:14 -0700167 stack_manager_get_interface()->init_stack();
Andre Eisenbach89f5e412014-12-05 09:40:20 -0800168 btif_debug_init();
Zach Johnson8df0d802014-09-09 16:31:14 -0700169 return BT_STATUS_SUCCESS;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800170}
171
Martin Brabham1a88ace2019-05-10 12:42:15 -0700172static int enable() {
Myles Watson6bd442f2016-10-19 09:50:22 -0700173 if (!interface_ready()) return BT_STATUS_NOT_READY;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800174
Zach Johnson8df0d802014-09-09 16:31:14 -0700175 stack_manager_get_interface()->start_up_stack_async();
176 return BT_STATUS_SUCCESS;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800177}
178
Zach Johnson8df0d802014-09-09 16:31:14 -0700179static int disable(void) {
Myles Watson6bd442f2016-10-19 09:50:22 -0700180 if (!interface_ready()) return BT_STATUS_NOT_READY;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800181
Zach Johnson8df0d802014-09-09 16:31:14 -0700182 stack_manager_get_interface()->shut_down_stack_async();
183 return BT_STATUS_SUCCESS;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800184}
185
Myles Watson6bd442f2016-10-19 09:50:22 -0700186static void cleanup(void) { stack_manager_get_interface()->clean_up_stack(); }
187
188bool is_restricted_mode() { return restricted_mode; }
weichinweng14b98e02020-04-21 10:26:26 +0800189bool is_niap_mode() { return niap_mode; }
190// if niap mode disable, will always return CONFIG_COMPARE_ALL_PASS(0b11)
191// indicate don't check config checksum.
192int get_niap_config_compare_result() {
193 return niap_mode ? niap_config_compare_result : CONFIG_COMPARE_ALL_PASS;
194}
Myles Watson6bd442f2016-10-19 09:50:22 -0700195
Rahul Sabnise198eb92020-07-31 19:44:27 -0700196bool is_atv_device() { return is_local_device_atv; }
197
Myles Watson6bd442f2016-10-19 09:50:22 -0700198static int get_adapter_properties(void) {
199 /* sanity check */
Myles Watsonb749ebd2017-10-06 16:39:05 -0700200 if (!interface_ready()) return BT_STATUS_NOT_READY;
Myles Watson6bd442f2016-10-19 09:50:22 -0700201
202 return btif_get_adapter_properties();
The Android Open Source Project5738f832012-12-12 16:00:35 -0800203}
204
Myles Watson6bd442f2016-10-19 09:50:22 -0700205static int get_adapter_property(bt_property_type_t type) {
206 /* sanity check */
Myles Watsonb749ebd2017-10-06 16:39:05 -0700207 if (!interface_ready()) return BT_STATUS_NOT_READY;
Myles Watson6bd442f2016-10-19 09:50:22 -0700208
209 return btif_get_adapter_property(type);
Ajay Panicker7b266be2016-03-17 17:09:24 -0700210}
211
Myles Watson6bd442f2016-10-19 09:50:22 -0700212static int set_adapter_property(const bt_property_t* property) {
213 /* sanity check */
Myles Watsonb749ebd2017-10-06 16:39:05 -0700214 if (!interface_ready()) return BT_STATUS_NOT_READY;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800215
Myles Watson6bd442f2016-10-19 09:50:22 -0700216 return btif_set_adapter_property(property);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800217}
218
Jakub Pawlowskia484a882017-06-24 17:30:18 -0700219int get_remote_device_properties(RawAddress* remote_addr) {
Myles Watson6bd442f2016-10-19 09:50:22 -0700220 /* sanity check */
Myles Watsonb749ebd2017-10-06 16:39:05 -0700221 if (!interface_ready()) return BT_STATUS_NOT_READY;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800222
Myles Watson6bd442f2016-10-19 09:50:22 -0700223 return btif_get_remote_device_properties(remote_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800224}
225
Jakub Pawlowskia484a882017-06-24 17:30:18 -0700226int get_remote_device_property(RawAddress* remote_addr,
Myles Watson6bd442f2016-10-19 09:50:22 -0700227 bt_property_type_t type) {
228 /* sanity check */
Myles Watsonb749ebd2017-10-06 16:39:05 -0700229 if (!interface_ready()) return BT_STATUS_NOT_READY;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800230
Myles Watson6bd442f2016-10-19 09:50:22 -0700231 return btif_get_remote_device_property(remote_addr, type);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800232}
233
Jakub Pawlowskia484a882017-06-24 17:30:18 -0700234int set_remote_device_property(RawAddress* remote_addr,
Myles Watson6bd442f2016-10-19 09:50:22 -0700235 const bt_property_t* property) {
236 /* sanity check */
Myles Watsonb749ebd2017-10-06 16:39:05 -0700237 if (!interface_ready()) return BT_STATUS_NOT_READY;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800238
Myles Watson6bd442f2016-10-19 09:50:22 -0700239 return btif_set_remote_device_property(remote_addr, property);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800240}
241
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700242int get_remote_service_record(const RawAddress& remote_addr,
243 const bluetooth::Uuid& uuid) {
Myles Watson6bd442f2016-10-19 09:50:22 -0700244 /* sanity check */
Myles Watson5d5fcf22017-10-06 16:51:21 -0700245 if (!interface_ready()) return BT_STATUS_NOT_READY;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800246
Myles Watson6bd442f2016-10-19 09:50:22 -0700247 return btif_get_remote_service_record(remote_addr, uuid);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800248}
249
Jakub Pawlowskia484a882017-06-24 17:30:18 -0700250int get_remote_services(RawAddress* remote_addr) {
Myles Watson6bd442f2016-10-19 09:50:22 -0700251 /* sanity check */
Myles Watsonb749ebd2017-10-06 16:39:05 -0700252 if (!interface_ready()) return BT_STATUS_NOT_READY;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800253
Jakub Pawlowskic2276b02017-06-09 16:00:25 -0700254 return btif_dm_get_remote_services(*remote_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800255}
256
Myles Watson6bd442f2016-10-19 09:50:22 -0700257static int start_discovery(void) {
258 /* sanity check */
Myles Watsonb749ebd2017-10-06 16:39:05 -0700259 if (!interface_ready()) return BT_STATUS_NOT_READY;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800260
Myles Watson6bd442f2016-10-19 09:50:22 -0700261 return btif_dm_start_discovery();
The Android Open Source Project5738f832012-12-12 16:00:35 -0800262}
263
Myles Watson6bd442f2016-10-19 09:50:22 -0700264static int cancel_discovery(void) {
265 /* sanity check */
Myles Watsonb749ebd2017-10-06 16:39:05 -0700266 if (!interface_ready()) return BT_STATUS_NOT_READY;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800267
Myles Watson6bd442f2016-10-19 09:50:22 -0700268 return btif_dm_cancel_discovery();
The Android Open Source Project5738f832012-12-12 16:00:35 -0800269}
270
Jakub Pawlowskia484a882017-06-24 17:30:18 -0700271static int create_bond(const RawAddress* bd_addr, int transport) {
Myles Watson6bd442f2016-10-19 09:50:22 -0700272 /* sanity check */
Myles Watsonb749ebd2017-10-06 16:39:05 -0700273 if (!interface_ready()) return BT_STATUS_NOT_READY;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800274
Myles Watson6bd442f2016-10-19 09:50:22 -0700275 return btif_dm_create_bond(bd_addr, transport);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800276}
277
Jakub Pawlowskia484a882017-06-24 17:30:18 -0700278static int create_bond_out_of_band(const RawAddress* bd_addr, int transport,
Myles Watson6bd442f2016-10-19 09:50:22 -0700279 const bt_out_of_band_data_t* oob_data) {
280 /* sanity check */
Myles Watsonb749ebd2017-10-06 16:39:05 -0700281 if (!interface_ready()) return BT_STATUS_NOT_READY;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800282
Myles Watson6bd442f2016-10-19 09:50:22 -0700283 return btif_dm_create_bond_out_of_band(bd_addr, transport, oob_data);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800284}
285
Jakub Pawlowskia484a882017-06-24 17:30:18 -0700286static int cancel_bond(const RawAddress* bd_addr) {
Myles Watson6bd442f2016-10-19 09:50:22 -0700287 /* sanity check */
Myles Watsonb749ebd2017-10-06 16:39:05 -0700288 if (!interface_ready()) return BT_STATUS_NOT_READY;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800289
Myles Watson6bd442f2016-10-19 09:50:22 -0700290 return btif_dm_cancel_bond(bd_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800291}
292
Jakub Pawlowskia484a882017-06-24 17:30:18 -0700293static int remove_bond(const RawAddress* bd_addr) {
Myles Watson6bd442f2016-10-19 09:50:22 -0700294 if (is_restricted_mode() && !btif_storage_is_restricted_device(bd_addr))
Satya Callojie5ba8842014-07-03 17:18:02 -0700295 return BT_STATUS_SUCCESS;
Myles Watson6bd442f2016-10-19 09:50:22 -0700296
297 /* sanity check */
Myles Watsonb749ebd2017-10-06 16:39:05 -0700298 if (!interface_ready()) return BT_STATUS_NOT_READY;
Myles Watson6bd442f2016-10-19 09:50:22 -0700299
300 return btif_dm_remove_bond(bd_addr);
Satya Callojie5ba8842014-07-03 17:18:02 -0700301}
302
Jakub Pawlowskia484a882017-06-24 17:30:18 -0700303static int get_connection_state(const RawAddress* bd_addr) {
Myles Watson6bd442f2016-10-19 09:50:22 -0700304 /* sanity check */
Myles Watsonb749ebd2017-10-06 16:39:05 -0700305 if (!interface_ready()) return 0;
Myles Watson6bd442f2016-10-19 09:50:22 -0700306
307 return btif_dm_get_connection_state(bd_addr);
308}
309
Jakub Pawlowskia484a882017-06-24 17:30:18 -0700310static int pin_reply(const RawAddress* bd_addr, uint8_t accept, uint8_t pin_len,
311 bt_pin_code_t* pin_code) {
Myles Watson6bd442f2016-10-19 09:50:22 -0700312 /* sanity check */
Myles Watsonb749ebd2017-10-06 16:39:05 -0700313 if (!interface_ready()) return BT_STATUS_NOT_READY;
Myles Watson6bd442f2016-10-19 09:50:22 -0700314
315 return btif_dm_pin_reply(bd_addr, accept, pin_len, pin_code);
316}
317
Jakub Pawlowskia484a882017-06-24 17:30:18 -0700318static int ssp_reply(const RawAddress* bd_addr, bt_ssp_variant_t variant,
Myles Watson6bd442f2016-10-19 09:50:22 -0700319 uint8_t accept, uint32_t passkey) {
320 /* sanity check */
Myles Watsonb749ebd2017-10-06 16:39:05 -0700321 if (!interface_ready()) return BT_STATUS_NOT_READY;
Myles Watson6bd442f2016-10-19 09:50:22 -0700322
323 return btif_dm_ssp_reply(bd_addr, variant, accept, passkey);
324}
325
326static int read_energy_info() {
Myles Watsonb749ebd2017-10-06 16:39:05 -0700327 if (!interface_ready()) return BT_STATUS_NOT_READY;
Myles Watson6bd442f2016-10-19 09:50:22 -0700328 btif_dm_read_energy_info();
329 return BT_STATUS_SUCCESS;
330}
331
332static void dump(int fd, const char** arguments) {
Myles Watson6bd442f2016-10-19 09:50:22 -0700333 btif_debug_conn_dump(fd);
334 btif_debug_bond_event_dump(fd);
335 btif_debug_a2dp_dump(fd);
Pavlin Radoslavovd7522292017-11-24 19:12:11 -0800336 btif_debug_av_dump(fd);
337 bta_debug_av_dump(fd);
338 stack_debug_avdtp_api_dump(fd);
Ajay Panicker774eb4c2018-05-02 16:23:13 -0700339 bluetooth::avrcp::AvrcpService::DebugDump(fd);
Myles Watson6bd442f2016-10-19 09:50:22 -0700340 btif_debug_config_dump(fd);
Sanket Agarwal5eccd902016-11-30 19:43:47 -0800341 BTA_HfClientDumpStatistics(fd);
Myles Watson6bd442f2016-10-19 09:50:22 -0700342 wakelock_debug_dump(fd);
Pavlin Radoslavov0b4f3f72017-02-01 03:55:33 -0800343 osi_allocator_debug_dump(fd);
Myles Watson6bd442f2016-10-19 09:50:22 -0700344 alarm_debug_dump(fd);
Hansong Zhangb596bb12018-05-09 14:39:56 -0700345 HearingAid::DebugDump(fd);
Jakub Pawlowskif20d94a2018-12-28 19:03:09 +0100346 connection_manager::dump(fd);
Ray6e632472019-01-03 21:19:16 +0800347 bluetooth::bqr::DebugDump(fd);
Chris Manton0cb19c82020-01-13 21:11:48 -0800348 if (bluetooth::shim::is_gd_shim_enabled()) {
349 bluetooth::shim::Dump(fd);
350 } else {
Marie Janssenb7f64bc2016-06-22 12:52:19 -0700351#if (BTSNOOP_MEM == TRUE)
Chris Manton0cb19c82020-01-13 21:11:48 -0800352 btif_debug_btsnoop_dump(fd);
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800353#endif
Chris Manton0cb19c82020-01-13 21:11:48 -0800354 }
Andre Eisenbach89f5e412014-12-05 09:40:20 -0800355}
356
Jack He9af00782018-01-31 16:51:26 -0800357static void dumpMetrics(std::string* output) {
Jack He959bc332018-08-15 12:38:37 -0700358 bluetooth::common::BluetoothMetricsLogger::GetInstance()->WriteString(output);
Jack He9af00782018-01-31 16:51:26 -0800359}
360
Myles Watson6bd442f2016-10-19 09:50:22 -0700361static const void* get_profile_interface(const char* profile_id) {
Hemant Gupta8843cc82014-04-18 12:34:55 +0530362 LOG_INFO(LOG_TAG, "%s: id = %s", __func__, profile_id);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800363
Myles Watson6bd442f2016-10-19 09:50:22 -0700364 /* sanity check */
Myles Watsonb749ebd2017-10-06 16:39:05 -0700365 if (!interface_ready()) return NULL;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800366
Myles Watson6bd442f2016-10-19 09:50:22 -0700367 /* check for supported profile interfaces */
368 if (is_profile(profile_id, BT_PROFILE_HANDSFREE_ID))
Jack He12a576d2017-12-14 15:00:39 -0800369 return bluetooth::headset::GetInterface();
The Android Open Source Project5738f832012-12-12 16:00:35 -0800370
Myles Watson6bd442f2016-10-19 09:50:22 -0700371 if (is_profile(profile_id, BT_PROFILE_HANDSFREE_CLIENT_ID))
372 return btif_hf_client_get_interface();
Hemant Gupta41d4a262013-08-19 18:33:01 +0530373
Myles Watson6bd442f2016-10-19 09:50:22 -0700374 if (is_profile(profile_id, BT_PROFILE_SOCKETS_ID))
375 return btif_sock_get_interface();
The Android Open Source Project5738f832012-12-12 16:00:35 -0800376
Myles Watson6bd442f2016-10-19 09:50:22 -0700377 if (is_profile(profile_id, BT_PROFILE_PAN_ID))
378 return btif_pan_get_interface();
The Android Open Source Project5738f832012-12-12 16:00:35 -0800379
Myles Watson6bd442f2016-10-19 09:50:22 -0700380 if (is_profile(profile_id, BT_PROFILE_ADVANCED_AUDIO_ID))
381 return btif_av_get_src_interface();
Mike Lockwood3e8a242f2014-05-23 12:42:24 -0700382
Myles Watson6bd442f2016-10-19 09:50:22 -0700383 if (is_profile(profile_id, BT_PROFILE_ADVANCED_AUDIO_SINK_ID))
384 return btif_av_get_sink_interface();
The Android Open Source Project5738f832012-12-12 16:00:35 -0800385
Myles Watson6bd442f2016-10-19 09:50:22 -0700386 if (is_profile(profile_id, BT_PROFILE_HIDHOST_ID))
387 return btif_hh_get_interface();
The Android Open Source Project5738f832012-12-12 16:00:35 -0800388
Hemant Gupta8843cc82014-04-18 12:34:55 +0530389 if (is_profile(profile_id, BT_PROFILE_HIDDEV_ID))
390 return btif_hd_get_interface();
391
Myles Watson6bd442f2016-10-19 09:50:22 -0700392 if (is_profile(profile_id, BT_PROFILE_SDP_CLIENT_ID))
393 return btif_sdp_get_interface();
Hemant Gupta2dc99992014-04-18 12:54:08 +0530394
Myles Watson6bd442f2016-10-19 09:50:22 -0700395 if (is_profile(profile_id, BT_PROFILE_GATT_ID))
396 return btif_gatt_get_interface();
Ravi Nagarajan9adddf42013-03-06 05:29:48 -0800397
Myles Watson6bd442f2016-10-19 09:50:22 -0700398 if (is_profile(profile_id, BT_PROFILE_AV_RC_ID))
399 return btif_rc_get_interface();
Ravi Nagarajan9adddf42013-03-06 05:29:48 -0800400
Myles Watson6bd442f2016-10-19 09:50:22 -0700401 if (is_profile(profile_id, BT_PROFILE_AV_RC_CTRL_ID))
402 return btif_rc_ctrl_get_interface();
Mike Lockwood4ad470e2014-06-02 16:21:49 -0700403
Jakub Pawlowski89b0ccb2018-02-28 22:44:42 -0800404 if (is_profile(profile_id, BT_PROFILE_HEARING_AID_ID))
405 return btif_hearing_aid_get_interface();
weichinwengb30bb982020-04-21 09:42:27 +0800406
407 if (is_profile(profile_id, BT_KEYSTORE_ID))
408 return bluetooth::bluetooth_keystore::getBluetoothKeystoreInterface();
Myles Watson6bd442f2016-10-19 09:50:22 -0700409 return NULL;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800410}
411
Myles Watson6bd442f2016-10-19 09:50:22 -0700412int dut_mode_configure(uint8_t enable) {
Hemant Gupta8843cc82014-04-18 12:34:55 +0530413 LOG_INFO(LOG_TAG, "%s", __func__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800414
Myles Watson6bd442f2016-10-19 09:50:22 -0700415 /* sanity check */
Myles Watsonb749ebd2017-10-06 16:39:05 -0700416 if (!interface_ready()) return BT_STATUS_NOT_READY;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800417
Myles Watson6bd442f2016-10-19 09:50:22 -0700418 return btif_dut_mode_configure(enable);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800419}
420
Myles Watson6bd442f2016-10-19 09:50:22 -0700421int dut_mode_send(uint16_t opcode, uint8_t* buf, uint8_t len) {
Hemant Gupta8843cc82014-04-18 12:34:55 +0530422 LOG_INFO(LOG_TAG, "%s", __func__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800423
Myles Watson6bd442f2016-10-19 09:50:22 -0700424 /* sanity check */
Myles Watsonb749ebd2017-10-06 16:39:05 -0700425 if (!interface_ready()) return BT_STATUS_NOT_READY;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800426
Myles Watson6bd442f2016-10-19 09:50:22 -0700427 return btif_dut_mode_send(opcode, buf, len);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800428}
Matthew Xiefc4b2f12013-05-06 20:51:02 -0700429
Myles Watson6bd442f2016-10-19 09:50:22 -0700430int le_test_mode(uint16_t opcode, uint8_t* buf, uint8_t len) {
Hemant Gupta8843cc82014-04-18 12:34:55 +0530431 LOG_INFO(LOG_TAG, "%s", __func__);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800432
Myles Watson6bd442f2016-10-19 09:50:22 -0700433 /* sanity check */
Myles Watsonb749ebd2017-10-06 16:39:05 -0700434 if (!interface_ready()) return BT_STATUS_NOT_READY;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800435
Myles Watson6bd442f2016-10-19 09:50:22 -0700436 return btif_le_test_mode(opcode, buf, len);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800437}
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800438
Pavlin Radoslavovc788ad72018-08-24 18:45:16 -0700439static bt_os_callouts_t* wakelock_os_callouts_saved = nullptr;
440
441static int acquire_wake_lock_cb(const char* lock_name) {
442 return do_in_jni_thread(
443 FROM_HERE, base::Bind(base::IgnoreResult(
444 wakelock_os_callouts_saved->acquire_wake_lock),
445 lock_name));
446}
447
448static int release_wake_lock_cb(const char* lock_name) {
449 return do_in_jni_thread(
450 FROM_HERE, base::Bind(base::IgnoreResult(
451 wakelock_os_callouts_saved->release_wake_lock),
452 lock_name));
453}
454
455static bt_os_callouts_t wakelock_os_callouts_jni = {
456 sizeof(wakelock_os_callouts_jni),
457 nullptr /* not used */,
458 acquire_wake_lock_cb,
459 release_wake_lock_cb,
460};
461
Myles Watson6bd442f2016-10-19 09:50:22 -0700462static int set_os_callouts(bt_os_callouts_t* callouts) {
Pavlin Radoslavovc788ad72018-08-24 18:45:16 -0700463 wakelock_os_callouts_saved = callouts;
464 wakelock_set_os_callouts(&wakelock_os_callouts_jni);
Myles Watson6bd442f2016-10-19 09:50:22 -0700465 return BT_STATUS_SUCCESS;
Sharvil Nanavati10aecea2014-05-28 17:09:46 -0700466}
467
Ajay Panickeraa3be1a2015-07-28 16:54:53 -0700468static int config_clear(void) {
Myles Watson6bd442f2016-10-19 09:50:22 -0700469 LOG_INFO(LOG_TAG, "%s", __func__);
470 return btif_config_clear() ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
Ajay Panickeraa3be1a2015-07-28 16:54:53 -0700471}
472
Ajay Panicker66401792018-03-14 23:45:54 -0700473static bluetooth::avrcp::ServiceInterface* get_avrcp_service(void) {
474 return bluetooth::avrcp::AvrcpService::GetServiceInterface();
475}
476
Jack He78b69d92018-11-16 02:59:43 -0800477static std::string obfuscate_address(const RawAddress& address) {
478 return bluetooth::common::AddressObfuscator::GetInstance()->Obfuscate(
479 address);
480}
481
Chen Chen4f6a3b22020-03-05 15:02:46 -0800482static int get_metric_id(const RawAddress& address) {
483 return bluetooth::common::MetricIdAllocator::GetInstance().AllocateId(
484 address);
485}
486
Jakub Pawlowski2d262d52017-10-16 15:57:16 -0700487EXPORT_SYMBOL bt_interface_t bluetoothInterface = {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800488 sizeof(bluetoothInterface),
The Android Open Source Project5738f832012-12-12 16:00:35 -0800489 init,
490 enable,
491 disable,
492 cleanup,
493 get_adapter_properties,
494 get_adapter_property,
495 set_adapter_property,
496 get_remote_device_properties,
497 get_remote_device_property,
498 set_remote_device_property,
499 get_remote_service_record,
500 get_remote_services,
501 start_discovery,
502 cancel_discovery,
503 create_bond,
Jakub Pawlowski1a5bb5f2015-12-01 12:14:22 -0800504 create_bond_out_of_band,
The Android Open Source Project5738f832012-12-12 16:00:35 -0800505 remove_bond,
506 cancel_bond,
Andre Eisenbach5c0b0522014-06-18 12:20:37 -0700507 get_connection_state,
The Android Open Source Project5738f832012-12-12 16:00:35 -0800508 pin_reply,
509 ssp_reply,
510 get_profile_interface,
511 dut_mode_configure,
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800512 dut_mode_send,
Zhihai Xubad70b12013-06-04 18:21:25 -0700513 le_test_mode,
Sharvil Nanavati10aecea2014-05-28 17:09:46 -0700514 set_os_callouts,
Satya Callojie5ba8842014-07-03 17:18:02 -0700515 read_energy_info,
Ajay Panickeraa3be1a2015-07-28 16:54:53 -0700516 dump,
Jack He9af00782018-01-31 16:51:26 -0800517 dumpMetrics,
Andre Eisenbache42d1d72016-01-14 02:00:24 -0800518 config_clear,
519 interop_database_clear,
520 interop_database_add,
Ajay Panicker66401792018-03-14 23:45:54 -0700521 get_avrcp_service,
Jack He78b69d92018-11-16 02:59:43 -0800522 obfuscate_address,
Chen Chen4f6a3b22020-03-05 15:02:46 -0800523 get_metric_id,
The Android Open Source Project5738f832012-12-12 16:00:35 -0800524};