blob: 31595c3544b66f8b801d2fd629f7c112cb114de2 [file] [log] [blame]
The Android Open Source Project624a7872012-12-12 16:00:35 -08001/******************************************************************************
2 *
3 * Copyright (C) 2009-2012 Broadcom Corporation
4 *
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
Andre Eisenbach80c23e52016-02-17 12:43:12 -080029#include <assert.h>
The Android Open Source Project624a7872012-12-12 16:00:35 -080030#include <stdio.h>
31#include <stdlib.h>
Scott James Remnant34121f52015-04-02 15:22:14 -070032#include <string.h>
The Android Open Source Project624a7872012-12-12 16:00:35 -080033#include <unistd.h>
34
35#include <hardware/bluetooth.h>
Marie Janssene06e1512015-07-08 11:48:57 -070036#include <hardware/bt_av.h>
37#include <hardware/bt_gatt.h>
The Android Open Source Project624a7872012-12-12 16:00:35 -080038#include <hardware/bt_hf.h>
Hemant Guptad9e9cf92013-08-19 18:33:01 +053039#include <hardware/bt_hf_client.h>
The Android Open Source Project624a7872012-12-12 16:00:35 -080040#include <hardware/bt_hh.h>
41#include <hardware/bt_hl.h>
Hemant Guptad0ecdbb2014-04-18 12:54:08 +053042#include <hardware/bt_mce.h>
Marie Janssene06e1512015-07-08 11:48:57 -070043#include <hardware/bt_pan.h>
Ravi Nagarajan78b35202013-03-06 05:29:48 -080044#include <hardware/bt_rc.h>
Kim Schulzb2337fe2015-03-25 10:39:40 +010045#include <hardware/bt_sdp.h>
Marie Janssene06e1512015-07-08 11:48:57 -070046#include <hardware/bt_sock.h>
The Android Open Source Project624a7872012-12-12 16:00:35 -080047
Marie Janssene06e1512015-07-08 11:48:57 -070048#include "bt_utils.h"
Myles Watson3f68a752016-10-19 09:50:22 -070049#include "btif/include/btif_debug_btsnoop.h"
50#include "btif/include/btif_debug_conn.h"
Pavlin Radoslavovcd102012016-09-23 16:36:47 -070051#include "btif_a2dp.h"
The Android Open Source Project624a7872012-12-12 16:00:35 -080052#include "btif_api.h"
Myles Watson3f68a752016-10-19 09:50:22 -070053#include "btif_config.h"
Andre Eisenbach8f6c5be2014-12-05 09:40:20 -080054#include "btif_debug.h"
Myles Watson3f68a752016-10-19 09:50:22 -070055#include "btif_storage.h"
Zach Johnson03234662014-09-22 22:11:55 -070056#include "btsnoop.h"
Andre Eisenbach8f6c5be2014-12-05 09:40:20 -080057#include "btsnoop_mem.h"
Andre Eisenbach35c0c1d62016-01-14 02:00:24 -080058#include "device/include/interop.h"
Pavlin Radoslavov30698fa2015-12-04 17:36:34 -080059#include "osi/include/alarm.h"
Myles Watson3f68a752016-10-19 09:50:22 -070060#include "osi/include/allocation_tracker.h"
Sharvil Nanavati83951772014-12-23 23:08:58 -080061#include "osi/include/log.h"
Marie Janssena84a4ee2016-01-15 16:14:14 -080062#include "osi/include/metrics.h"
Marie Janssene06e1512015-07-08 11:48:57 -070063#include "osi/include/osi.h"
Pavlin Radoslavov5bb6cac2015-08-24 16:29:21 -070064#include "osi/include/wakelock.h"
Zach Johnsoned9491b2014-09-09 16:31:14 -070065#include "stack_manager.h"
The Android Open Source Project624a7872012-12-12 16:00:35 -080066
Myles Watsonfbfa1a52016-11-23 14:49:54 -080067/*******************************************************************************
Myles Watson3f68a752016-10-19 09:50:22 -070068 * Static variables
Myles Watsonfbfa1a52016-11-23 14:49:54 -080069 ******************************************************************************/
The Android Open Source Project624a7872012-12-12 16:00:35 -080070
Myles Watson3f68a752016-10-19 09:50:22 -070071bt_callbacks_t* bt_hal_cbacks = NULL;
Marie Janssen0fc1f132016-06-22 12:52:19 -070072bool restricted_mode = false;
The Android Open Source Project624a7872012-12-12 16:00:35 -080073
Myles Watsonfbfa1a52016-11-23 14:49:54 -080074/*******************************************************************************
Myles Watson3f68a752016-10-19 09:50:22 -070075 * Externs
Myles Watsonfbfa1a52016-11-23 14:49:54 -080076 ******************************************************************************/
The Android Open Source Project624a7872012-12-12 16:00:35 -080077
78/* list all extended interfaces here */
79
80/* handsfree profile */
Myles Watson3f68a752016-10-19 09:50:22 -070081extern bthf_interface_t* btif_hf_get_interface();
Hemant Guptad9e9cf92013-08-19 18:33:01 +053082/* handsfree profile - client */
Myles Watson3f68a752016-10-19 09:50:22 -070083extern bthf_client_interface_t* btif_hf_client_get_interface();
The Android Open Source Project624a7872012-12-12 16:00:35 -080084/* advanced audio profile */
Myles Watson3f68a752016-10-19 09:50:22 -070085extern btav_interface_t* btif_av_get_src_interface();
86extern btav_interface_t* btif_av_get_sink_interface();
The Android Open Source Project624a7872012-12-12 16:00:35 -080087/*rfc l2cap*/
Myles Watson3f68a752016-10-19 09:50:22 -070088extern btsock_interface_t* btif_sock_get_interface();
The Android Open Source Project624a7872012-12-12 16:00:35 -080089/* hid host profile */
Myles Watson3f68a752016-10-19 09:50:22 -070090extern bthh_interface_t* btif_hh_get_interface();
The Android Open Source Project624a7872012-12-12 16:00:35 -080091/* health device profile */
Myles Watson3f68a752016-10-19 09:50:22 -070092extern bthl_interface_t* btif_hl_get_interface();
The Android Open Source Project624a7872012-12-12 16:00:35 -080093/*pan*/
Myles Watson3f68a752016-10-19 09:50:22 -070094extern btpan_interface_t* btif_pan_get_interface();
Hemant Guptad0ecdbb2014-04-18 12:54:08 +053095/*map client*/
Myles Watson3f68a752016-10-19 09:50:22 -070096extern btmce_interface_t* btif_mce_get_interface();
Ganesh Ganapathi Batta1def2552013-02-05 15:22:31 -080097/* gatt */
Myles Watson3f68a752016-10-19 09:50:22 -070098extern const btgatt_interface_t* btif_gatt_get_interface();
Mike Lockwoodc02ef772014-06-02 16:21:49 -070099/* avrc target */
Myles Watson3f68a752016-10-19 09:50:22 -0700100extern btrc_interface_t* btif_rc_get_interface();
Mike Lockwoodc02ef772014-06-02 16:21:49 -0700101/* avrc controller */
Myles Watson3f68a752016-10-19 09:50:22 -0700102extern btrc_interface_t* btif_rc_ctrl_get_interface();
Kim Schulzb2337fe2015-03-25 10:39:40 +0100103/*SDP search client*/
Myles Watson3f68a752016-10-19 09:50:22 -0700104extern btsdp_interface_t* btif_sdp_get_interface();
The Android Open Source Project624a7872012-12-12 16:00:35 -0800105
Myles Watsonfbfa1a52016-11-23 14:49:54 -0800106/*******************************************************************************
Myles Watson3f68a752016-10-19 09:50:22 -0700107 * Functions
Myles Watsonfbfa1a52016-11-23 14:49:54 -0800108 ******************************************************************************/
The Android Open Source Project624a7872012-12-12 16:00:35 -0800109
Myles Watson3f68a752016-10-19 09:50:22 -0700110static bool interface_ready(void) { return bt_hal_cbacks != NULL; }
The Android Open Source Project624a7872012-12-12 16:00:35 -0800111
Myles Watson3f68a752016-10-19 09:50:22 -0700112static bool is_profile(const char* p1, const char* p2) {
Andre Eisenbach80c23e52016-02-17 12:43:12 -0800113 assert(p1);
114 assert(p2);
115 return strlen(p1) == strlen(p2) && strncmp(p1, p2, strlen(p2)) == 0;
116}
117
The Android Open Source Project624a7872012-12-12 16:00:35 -0800118/*****************************************************************************
Myles Watson3f68a752016-10-19 09:50:22 -0700119 *
120 * BLUETOOTH HAL INTERFACE FUNCTIONS
121 *
122 ****************************************************************************/
The Android Open Source Project624a7872012-12-12 16:00:35 -0800123
Myles Watson3f68a752016-10-19 09:50:22 -0700124static int init(bt_callbacks_t* callbacks) {
Marie Janssenfc8006e2015-06-26 14:53:46 -0700125 LOG_INFO(LOG_TAG, "%s", __func__);
The Android Open Source Project624a7872012-12-12 16:00:35 -0800126
Myles Watson3f68a752016-10-19 09:50:22 -0700127 if (interface_ready()) return BT_STATUS_DONE;
The Android Open Source Project624a7872012-12-12 16:00:35 -0800128
Sharvil Nanavati19f4b182014-11-13 01:04:19 -0800129#ifdef BLUEDROID_DEBUG
130 allocation_tracker_init();
131#endif
132
Zach Johnsoned9491b2014-09-09 16:31:14 -0700133 bt_hal_cbacks = callbacks;
134 stack_manager_get_interface()->init_stack();
Andre Eisenbach8f6c5be2014-12-05 09:40:20 -0800135 btif_debug_init();
Zach Johnsoned9491b2014-09-09 16:31:14 -0700136 return BT_STATUS_SUCCESS;
The Android Open Source Project624a7872012-12-12 16:00:35 -0800137}
138
Ajay Panickerc796ac82016-03-17 17:09:24 -0700139static int enable(bool start_restricted) {
140 LOG_INFO(LOG_TAG, "%s: start restricted = %d", __func__, start_restricted);
141
142 restricted_mode = start_restricted;
The Android Open Source Project624a7872012-12-12 16:00:35 -0800143
Myles Watson3f68a752016-10-19 09:50:22 -0700144 if (!interface_ready()) return BT_STATUS_NOT_READY;
The Android Open Source Project624a7872012-12-12 16:00:35 -0800145
Zach Johnsoned9491b2014-09-09 16:31:14 -0700146 stack_manager_get_interface()->start_up_stack_async();
147 return BT_STATUS_SUCCESS;
The Android Open Source Project624a7872012-12-12 16:00:35 -0800148}
149
Zach Johnsoned9491b2014-09-09 16:31:14 -0700150static int disable(void) {
Myles Watson3f68a752016-10-19 09:50:22 -0700151 if (!interface_ready()) return BT_STATUS_NOT_READY;
The Android Open Source Project624a7872012-12-12 16:00:35 -0800152
Zach Johnsoned9491b2014-09-09 16:31:14 -0700153 stack_manager_get_interface()->shut_down_stack_async();
154 return BT_STATUS_SUCCESS;
The Android Open Source Project624a7872012-12-12 16:00:35 -0800155}
156
Myles Watson3f68a752016-10-19 09:50:22 -0700157static void cleanup(void) { stack_manager_get_interface()->clean_up_stack(); }
158
159bool is_restricted_mode() { return restricted_mode; }
160
161static int get_adapter_properties(void) {
162 /* sanity check */
163 if (interface_ready() == false) return BT_STATUS_NOT_READY;
164
165 return btif_get_adapter_properties();
The Android Open Source Project624a7872012-12-12 16:00:35 -0800166}
167
Myles Watson3f68a752016-10-19 09:50:22 -0700168static int get_adapter_property(bt_property_type_t type) {
169 /* sanity check */
170 if (interface_ready() == false) return BT_STATUS_NOT_READY;
171
172 return btif_get_adapter_property(type);
Ajay Panickerc796ac82016-03-17 17:09:24 -0700173}
174
Myles Watson3f68a752016-10-19 09:50:22 -0700175static int set_adapter_property(const bt_property_t* property) {
176 /* sanity check */
177 if (interface_ready() == false) return BT_STATUS_NOT_READY;
The Android Open Source Project624a7872012-12-12 16:00:35 -0800178
Myles Watson3f68a752016-10-19 09:50:22 -0700179 return btif_set_adapter_property(property);
The Android Open Source Project624a7872012-12-12 16:00:35 -0800180}
181
Myles Watson3f68a752016-10-19 09:50:22 -0700182int get_remote_device_properties(bt_bdaddr_t* remote_addr) {
183 /* sanity check */
184 if (interface_ready() == false) return BT_STATUS_NOT_READY;
The Android Open Source Project624a7872012-12-12 16:00:35 -0800185
Myles Watson3f68a752016-10-19 09:50:22 -0700186 return btif_get_remote_device_properties(remote_addr);
The Android Open Source Project624a7872012-12-12 16:00:35 -0800187}
188
Myles Watson3f68a752016-10-19 09:50:22 -0700189int get_remote_device_property(bt_bdaddr_t* remote_addr,
190 bt_property_type_t type) {
191 /* sanity check */
192 if (interface_ready() == false) return BT_STATUS_NOT_READY;
The Android Open Source Project624a7872012-12-12 16:00:35 -0800193
Myles Watson3f68a752016-10-19 09:50:22 -0700194 return btif_get_remote_device_property(remote_addr, type);
The Android Open Source Project624a7872012-12-12 16:00:35 -0800195}
196
Myles Watson3f68a752016-10-19 09:50:22 -0700197int set_remote_device_property(bt_bdaddr_t* remote_addr,
198 const bt_property_t* property) {
199 /* sanity check */
200 if (interface_ready() == false) return BT_STATUS_NOT_READY;
The Android Open Source Project624a7872012-12-12 16:00:35 -0800201
Myles Watson3f68a752016-10-19 09:50:22 -0700202 return btif_set_remote_device_property(remote_addr, property);
The Android Open Source Project624a7872012-12-12 16:00:35 -0800203}
204
Myles Watson3f68a752016-10-19 09:50:22 -0700205int get_remote_service_record(bt_bdaddr_t* remote_addr, bt_uuid_t* uuid) {
206 /* sanity check */
207 if (interface_ready() == false) return BT_STATUS_NOT_READY;
The Android Open Source Project624a7872012-12-12 16:00:35 -0800208
Myles Watson3f68a752016-10-19 09:50:22 -0700209 return btif_get_remote_service_record(remote_addr, uuid);
The Android Open Source Project624a7872012-12-12 16:00:35 -0800210}
211
Myles Watson3f68a752016-10-19 09:50:22 -0700212int get_remote_services(bt_bdaddr_t* remote_addr) {
213 /* sanity check */
214 if (interface_ready() == false) return BT_STATUS_NOT_READY;
The Android Open Source Project624a7872012-12-12 16:00:35 -0800215
Myles Watson3f68a752016-10-19 09:50:22 -0700216 return btif_dm_get_remote_services(remote_addr);
The Android Open Source Project624a7872012-12-12 16:00:35 -0800217}
218
Myles Watson3f68a752016-10-19 09:50:22 -0700219static int start_discovery(void) {
220 /* sanity check */
221 if (interface_ready() == false) return BT_STATUS_NOT_READY;
The Android Open Source Project624a7872012-12-12 16:00:35 -0800222
Myles Watson3f68a752016-10-19 09:50:22 -0700223 return btif_dm_start_discovery();
The Android Open Source Project624a7872012-12-12 16:00:35 -0800224}
225
Myles Watson3f68a752016-10-19 09:50:22 -0700226static int cancel_discovery(void) {
227 /* sanity check */
228 if (interface_ready() == false) return BT_STATUS_NOT_READY;
The Android Open Source Project624a7872012-12-12 16:00:35 -0800229
Myles Watson3f68a752016-10-19 09:50:22 -0700230 return btif_dm_cancel_discovery();
The Android Open Source Project624a7872012-12-12 16:00:35 -0800231}
232
Myles Watson3f68a752016-10-19 09:50:22 -0700233static int create_bond(const bt_bdaddr_t* bd_addr, int transport) {
234 /* sanity check */
235 if (interface_ready() == false) return BT_STATUS_NOT_READY;
The Android Open Source Project624a7872012-12-12 16:00:35 -0800236
Myles Watson3f68a752016-10-19 09:50:22 -0700237 return btif_dm_create_bond(bd_addr, transport);
The Android Open Source Project624a7872012-12-12 16:00:35 -0800238}
239
Myles Watson3f68a752016-10-19 09:50:22 -0700240static int create_bond_out_of_band(const bt_bdaddr_t* bd_addr, int transport,
241 const bt_out_of_band_data_t* oob_data) {
242 /* sanity check */
243 if (interface_ready() == false) return BT_STATUS_NOT_READY;
The Android Open Source Project624a7872012-12-12 16:00:35 -0800244
Myles Watson3f68a752016-10-19 09:50:22 -0700245 return btif_dm_create_bond_out_of_band(bd_addr, transport, oob_data);
The Android Open Source Project624a7872012-12-12 16:00:35 -0800246}
247
Myles Watson3f68a752016-10-19 09:50:22 -0700248static int cancel_bond(const bt_bdaddr_t* bd_addr) {
249 /* sanity check */
250 if (interface_ready() == false) return BT_STATUS_NOT_READY;
The Android Open Source Project624a7872012-12-12 16:00:35 -0800251
Myles Watson3f68a752016-10-19 09:50:22 -0700252 return btif_dm_cancel_bond(bd_addr);
The Android Open Source Project624a7872012-12-12 16:00:35 -0800253}
254
Myles Watson3f68a752016-10-19 09:50:22 -0700255static int remove_bond(const bt_bdaddr_t* bd_addr) {
256 if (is_restricted_mode() && !btif_storage_is_restricted_device(bd_addr))
Satya Calloji75fd8802014-07-03 17:18:02 -0700257 return BT_STATUS_SUCCESS;
Myles Watson3f68a752016-10-19 09:50:22 -0700258
259 /* sanity check */
260 if (interface_ready() == false) return BT_STATUS_NOT_READY;
261
262 return btif_dm_remove_bond(bd_addr);
Satya Calloji75fd8802014-07-03 17:18:02 -0700263}
264
Myles Watson3f68a752016-10-19 09:50:22 -0700265static int get_connection_state(const bt_bdaddr_t* bd_addr) {
266 /* sanity check */
267 if (interface_ready() == false) return 0;
268
269 return btif_dm_get_connection_state(bd_addr);
270}
271
272static int pin_reply(const bt_bdaddr_t* bd_addr, uint8_t accept,
273 uint8_t pin_len, bt_pin_code_t* pin_code) {
274 /* sanity check */
275 if (interface_ready() == false) return BT_STATUS_NOT_READY;
276
277 return btif_dm_pin_reply(bd_addr, accept, pin_len, pin_code);
278}
279
280static int ssp_reply(const bt_bdaddr_t* bd_addr, bt_ssp_variant_t variant,
281 uint8_t accept, uint32_t passkey) {
282 /* sanity check */
283 if (interface_ready() == false) return BT_STATUS_NOT_READY;
284
285 return btif_dm_ssp_reply(bd_addr, variant, accept, passkey);
286}
287
288static int read_energy_info() {
289 if (interface_ready() == false) return BT_STATUS_NOT_READY;
290 btif_dm_read_energy_info();
291 return BT_STATUS_SUCCESS;
292}
293
294static void dump(int fd, const char** arguments) {
295 if (arguments != NULL && arguments[0] != NULL) {
Myles Watson3f68a752016-10-19 09:50:22 -0700296 if (strncmp(arguments[0], "--proto-bin", 11) == 0) {
297 btif_update_a2dp_metrics();
298 metrics_write(fd, true);
299 return;
300 }
301 }
302 btif_debug_conn_dump(fd);
303 btif_debug_bond_event_dump(fd);
304 btif_debug_a2dp_dump(fd);
305 btif_debug_config_dump(fd);
306 wakelock_debug_dump(fd);
307 alarm_debug_dump(fd);
Marie Janssen0fc1f132016-06-22 12:52:19 -0700308#if (BTSNOOP_MEM == TRUE)
Myles Watson3f68a752016-10-19 09:50:22 -0700309 btif_debug_btsnoop_dump(fd);
Pavlin Radoslavov30698fa2015-12-04 17:36:34 -0800310#endif
311
Myles Watson3f68a752016-10-19 09:50:22 -0700312 close(fd);
Andre Eisenbach8f6c5be2014-12-05 09:40:20 -0800313}
314
Myles Watson3f68a752016-10-19 09:50:22 -0700315static const void* get_profile_interface(const char* profile_id) {
316 LOG_INFO(LOG_TAG, "get_profile_interface %s", profile_id);
The Android Open Source Project624a7872012-12-12 16:00:35 -0800317
Myles Watson3f68a752016-10-19 09:50:22 -0700318 /* sanity check */
319 if (interface_ready() == false) return NULL;
The Android Open Source Project624a7872012-12-12 16:00:35 -0800320
Myles Watson3f68a752016-10-19 09:50:22 -0700321 /* check for supported profile interfaces */
322 if (is_profile(profile_id, BT_PROFILE_HANDSFREE_ID))
323 return btif_hf_get_interface();
The Android Open Source Project624a7872012-12-12 16:00:35 -0800324
Myles Watson3f68a752016-10-19 09:50:22 -0700325 if (is_profile(profile_id, BT_PROFILE_HANDSFREE_CLIENT_ID))
326 return btif_hf_client_get_interface();
Hemant Guptad9e9cf92013-08-19 18:33:01 +0530327
Myles Watson3f68a752016-10-19 09:50:22 -0700328 if (is_profile(profile_id, BT_PROFILE_SOCKETS_ID))
329 return btif_sock_get_interface();
The Android Open Source Project624a7872012-12-12 16:00:35 -0800330
Myles Watson3f68a752016-10-19 09:50:22 -0700331 if (is_profile(profile_id, BT_PROFILE_PAN_ID))
332 return btif_pan_get_interface();
The Android Open Source Project624a7872012-12-12 16:00:35 -0800333
Myles Watson3f68a752016-10-19 09:50:22 -0700334 if (is_profile(profile_id, BT_PROFILE_ADVANCED_AUDIO_ID))
335 return btif_av_get_src_interface();
Mike Lockwoodc80e2d72014-05-23 12:42:24 -0700336
Myles Watson3f68a752016-10-19 09:50:22 -0700337 if (is_profile(profile_id, BT_PROFILE_ADVANCED_AUDIO_SINK_ID))
338 return btif_av_get_sink_interface();
The Android Open Source Project624a7872012-12-12 16:00:35 -0800339
Myles Watson3f68a752016-10-19 09:50:22 -0700340 if (is_profile(profile_id, BT_PROFILE_HIDHOST_ID))
341 return btif_hh_get_interface();
The Android Open Source Project624a7872012-12-12 16:00:35 -0800342
Myles Watson3f68a752016-10-19 09:50:22 -0700343 if (is_profile(profile_id, BT_PROFILE_HEALTH_ID))
344 return btif_hl_get_interface();
Ravi Nagarajan78b35202013-03-06 05:29:48 -0800345
Myles Watson3f68a752016-10-19 09:50:22 -0700346 if (is_profile(profile_id, BT_PROFILE_SDP_CLIENT_ID))
347 return btif_sdp_get_interface();
Hemant Guptad0ecdbb2014-04-18 12:54:08 +0530348
Myles Watson3f68a752016-10-19 09:50:22 -0700349 if (is_profile(profile_id, BT_PROFILE_GATT_ID))
350 return btif_gatt_get_interface();
Ravi Nagarajan78b35202013-03-06 05:29:48 -0800351
Myles Watson3f68a752016-10-19 09:50:22 -0700352 if (is_profile(profile_id, BT_PROFILE_AV_RC_ID))
353 return btif_rc_get_interface();
Ravi Nagarajan78b35202013-03-06 05:29:48 -0800354
Myles Watson3f68a752016-10-19 09:50:22 -0700355 if (is_profile(profile_id, BT_PROFILE_AV_RC_CTRL_ID))
356 return btif_rc_ctrl_get_interface();
Mike Lockwoodc02ef772014-06-02 16:21:49 -0700357
Myles Watson3f68a752016-10-19 09:50:22 -0700358 return NULL;
The Android Open Source Project624a7872012-12-12 16:00:35 -0800359}
360
Myles Watson3f68a752016-10-19 09:50:22 -0700361int dut_mode_configure(uint8_t enable) {
362 LOG_INFO(LOG_TAG, "dut_mode_configure");
The Android Open Source Project624a7872012-12-12 16:00:35 -0800363
Myles Watson3f68a752016-10-19 09:50:22 -0700364 /* sanity check */
365 if (interface_ready() == false) return BT_STATUS_NOT_READY;
The Android Open Source Project624a7872012-12-12 16:00:35 -0800366
Myles Watson3f68a752016-10-19 09:50:22 -0700367 return btif_dut_mode_configure(enable);
The Android Open Source Project624a7872012-12-12 16:00:35 -0800368}
369
Myles Watson3f68a752016-10-19 09:50:22 -0700370int dut_mode_send(uint16_t opcode, uint8_t* buf, uint8_t len) {
371 LOG_INFO(LOG_TAG, "dut_mode_send");
The Android Open Source Project624a7872012-12-12 16:00:35 -0800372
Myles Watson3f68a752016-10-19 09:50:22 -0700373 /* sanity check */
374 if (interface_ready() == false) return BT_STATUS_NOT_READY;
The Android Open Source Project624a7872012-12-12 16:00:35 -0800375
Myles Watson3f68a752016-10-19 09:50:22 -0700376 return btif_dut_mode_send(opcode, buf, len);
The Android Open Source Project624a7872012-12-12 16:00:35 -0800377}
Matthew Xie41fb4362013-05-06 20:51:02 -0700378
Myles Watson3f68a752016-10-19 09:50:22 -0700379int le_test_mode(uint16_t opcode, uint8_t* buf, uint8_t len) {
380 LOG_INFO(LOG_TAG, "le_test_mode");
Ganesh Ganapathi Batta1def2552013-02-05 15:22:31 -0800381
Myles Watson3f68a752016-10-19 09:50:22 -0700382 /* sanity check */
383 if (interface_ready() == false) return BT_STATUS_NOT_READY;
Ganesh Ganapathi Batta1def2552013-02-05 15:22:31 -0800384
Myles Watson3f68a752016-10-19 09:50:22 -0700385 return btif_le_test_mode(opcode, buf, len);
Ganesh Ganapathi Batta1def2552013-02-05 15:22:31 -0800386}
Ganesh Ganapathi Batta1def2552013-02-05 15:22:31 -0800387
Myles Watson3f68a752016-10-19 09:50:22 -0700388int config_hci_snoop_log(uint8_t enable) {
389 LOG_INFO(LOG_TAG, "config_hci_snoop_log");
Zhihai Xu8572d792013-06-04 18:21:25 -0700390
Myles Watson3f68a752016-10-19 09:50:22 -0700391 if (!interface_ready()) return BT_STATUS_NOT_READY;
Zhihai Xu8572d792013-06-04 18:21:25 -0700392
Myles Watson3f68a752016-10-19 09:50:22 -0700393 btsnoop_get_interface()->set_api_wants_to_log(enable);
394 return BT_STATUS_SUCCESS;
Zhihai Xu8572d792013-06-04 18:21:25 -0700395}
396
Myles Watson3f68a752016-10-19 09:50:22 -0700397static int set_os_callouts(bt_os_callouts_t* callouts) {
398 wakelock_set_os_callouts(callouts);
399 return BT_STATUS_SUCCESS;
Sharvil Nanavati55e690a2014-05-28 17:09:46 -0700400}
401
Ajay Panickerec102082015-07-28 16:54:53 -0700402static int config_clear(void) {
Myles Watson3f68a752016-10-19 09:50:22 -0700403 LOG_INFO(LOG_TAG, "%s", __func__);
404 return btif_config_clear() ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
Ajay Panickerec102082015-07-28 16:54:53 -0700405}
406
The Android Open Source Project624a7872012-12-12 16:00:35 -0800407static const bt_interface_t bluetoothInterface = {
Ganesh Ganapathi Batta1def2552013-02-05 15:22:31 -0800408 sizeof(bluetoothInterface),
The Android Open Source Project624a7872012-12-12 16:00:35 -0800409 init,
410 enable,
411 disable,
412 cleanup,
413 get_adapter_properties,
414 get_adapter_property,
415 set_adapter_property,
416 get_remote_device_properties,
417 get_remote_device_property,
418 set_remote_device_property,
419 get_remote_service_record,
420 get_remote_services,
421 start_discovery,
422 cancel_discovery,
423 create_bond,
Jakub Pawlowskidf0ab812015-12-01 12:14:22 -0800424 create_bond_out_of_band,
The Android Open Source Project624a7872012-12-12 16:00:35 -0800425 remove_bond,
426 cancel_bond,
Andre Eisenbach4a6f8cf2014-06-18 12:20:37 -0700427 get_connection_state,
The Android Open Source Project624a7872012-12-12 16:00:35 -0800428 pin_reply,
429 ssp_reply,
430 get_profile_interface,
431 dut_mode_configure,
Ganesh Ganapathi Batta1def2552013-02-05 15:22:31 -0800432 dut_mode_send,
Zhihai Xu8572d792013-06-04 18:21:25 -0700433 le_test_mode,
Sharvil Nanavati55e690a2014-05-28 17:09:46 -0700434 config_hci_snoop_log,
435 set_os_callouts,
Satya Calloji75fd8802014-07-03 17:18:02 -0700436 read_energy_info,
Ajay Panickerec102082015-07-28 16:54:53 -0700437 dump,
Andre Eisenbach35c0c1d62016-01-14 02:00:24 -0800438 config_clear,
439 interop_database_clear,
440 interop_database_add,
The Android Open Source Project624a7872012-12-12 16:00:35 -0800441};
442
Myles Watson3f68a752016-10-19 09:50:22 -0700443const bt_interface_t* bluetooth__get_bluetooth_interface() {
444 /* fixme -- add property to disable bt interface ? */
The Android Open Source Project624a7872012-12-12 16:00:35 -0800445
Myles Watson3f68a752016-10-19 09:50:22 -0700446 return &bluetoothInterface;
The Android Open Source Project624a7872012-12-12 16:00:35 -0800447}
448
Myles Watson824963f2016-11-15 16:33:22 -0800449static int close_bluetooth_stack(UNUSED_ATTR struct hw_device_t* device) {
Myles Watson3f68a752016-10-19 09:50:22 -0700450 cleanup();
451 return 0;
The Android Open Source Project624a7872012-12-12 16:00:35 -0800452}
453
Myles Watson3f68a752016-10-19 09:50:22 -0700454static int open_bluetooth_stack(const struct hw_module_t* module,
455 UNUSED_ATTR char const* name,
456 struct hw_device_t** abstraction) {
Jakub Pawlowski4a813832016-04-21 13:16:45 -0700457 static bluetooth_device_t device;
458 device.common.tag = HARDWARE_DEVICE_TAG;
459 device.common.version = 0;
460 device.common.close = close_bluetooth_stack;
461 device.get_bluetooth_interface = bluetooth__get_bluetooth_interface;
Myles Watson3f68a752016-10-19 09:50:22 -0700462 device.common.module = (struct hw_module_t*)module;
463 *abstraction = (struct hw_device_t*)&device;
Sharvil Nanavati7e1f98a2014-08-29 15:39:56 -0700464 return 0;
The Android Open Source Project624a7872012-12-12 16:00:35 -0800465}
466
The Android Open Source Project624a7872012-12-12 16:00:35 -0800467static struct hw_module_methods_t bt_stack_module_methods = {
468 .open = open_bluetooth_stack,
469};
470
Ian Coolidge231c4ac2015-04-21 16:25:08 -0700471EXPORT_SYMBOL struct hw_module_t HAL_MODULE_INFO_SYM = {
The Android Open Source Project624a7872012-12-12 16:00:35 -0800472 .tag = HARDWARE_MODULE_TAG,
473 .version_major = 1,
474 .version_minor = 0,
475 .id = BT_HARDWARE_MODULE_ID,
476 .name = "Bluetooth Stack",
477 .author = "The Android Open Source Project",
Myles Watson3f68a752016-10-19 09:50:22 -0700478 .methods = &bt_stack_module_methods};