1 /******************************************************************************
3 * Copyright (C) 2009-2012 Broadcom Corporation
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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 ******************************************************************************/
19 /************************************************************************************
21 * Filename: bluetooth.c
23 * Description: Bluetooth HAL implementation
25 ***********************************************************************************/
28 #define LOG_TAG "bt_btif"
36 #include <hardware/bluetooth.h>
37 #include <hardware/bt_av.h>
38 #include <hardware/bt_gatt.h>
39 #include <hardware/bt_hf.h>
40 #include <hardware/bt_hf_client.h>
41 #include <hardware/bt_hh.h>
42 #include <hardware/bt_hl.h>
43 #include <hardware/bt_mce.h>
44 #include <hardware/bt_pan.h>
45 #include <hardware/bt_rc.h>
46 #include <hardware/bt_sdp.h>
47 #include <hardware/bt_sock.h>
51 #include "btif_debug.h"
53 #include "btsnoop_mem.h"
54 #include "device/include/interop.h"
55 #include "osi/include/allocation_tracker.h"
56 #include "osi/include/alarm.h"
57 #include "osi/include/log.h"
58 #include "osi/include/metrics.h"
59 #include "osi/include/osi.h"
60 #include "osi/include/wakelock.h"
61 #include "stack_manager.h"
62 #include "btif_config.h"
63 #include "btif/include/btif_debug_btsnoop.h"
64 #include "btif/include/btif_debug_conn.h"
65 #include "btif/include/btif_media.h"
67 /************************************************************************************
69 ************************************************************************************/
71 bt_callbacks_t *bt_hal_cbacks = NULL;
73 /************************************************************************************
75 ************************************************************************************/
77 /* list all extended interfaces here */
79 /* handsfree profile */
80 extern bthf_interface_t *btif_hf_get_interface();
81 /* handsfree profile - client */
82 extern bthf_client_interface_t *btif_hf_client_get_interface();
83 /* advanced audio profile */
84 extern btav_interface_t *btif_av_get_src_interface();
85 extern btav_interface_t *btif_av_get_sink_interface();
87 extern btsock_interface_t *btif_sock_get_interface();
88 /* hid host profile */
89 extern bthh_interface_t *btif_hh_get_interface();
90 /* health device profile */
91 extern bthl_interface_t *btif_hl_get_interface();
93 extern btpan_interface_t *btif_pan_get_interface();
95 extern btmce_interface_t *btif_mce_get_interface();
96 #if BLE_INCLUDED == TRUE
98 extern btgatt_interface_t *btif_gatt_get_interface();
101 extern btrc_interface_t *btif_rc_get_interface();
102 /* avrc controller */
103 extern btrc_interface_t *btif_rc_ctrl_get_interface();
104 /*SDP search client*/
105 extern btsdp_interface_t *btif_sdp_get_interface();
107 /************************************************************************************
109 ************************************************************************************/
111 static bool interface_ready(void) {
112 return bt_hal_cbacks != NULL;
115 static bool is_profile(const char *p1, const char *p2) {
118 return strlen(p1) == strlen(p2) && strncmp(p1, p2, strlen(p2)) == 0;
121 /*****************************************************************************
123 ** BLUETOOTH HAL INTERFACE FUNCTIONS
125 *****************************************************************************/
127 static int init(bt_callbacks_t *callbacks) {
128 LOG_INFO(LOG_TAG, "%s", __func__);
130 if (interface_ready())
131 return BT_STATUS_DONE;
133 #ifdef BLUEDROID_DEBUG
134 allocation_tracker_init();
137 bt_hal_cbacks = callbacks;
138 stack_manager_get_interface()->init_stack();
140 return BT_STATUS_SUCCESS;
143 static int enable(void) {
144 LOG_INFO(LOG_TAG, "%s", __func__);
146 if (!interface_ready())
147 return BT_STATUS_NOT_READY;
149 stack_manager_get_interface()->start_up_stack_async();
150 return BT_STATUS_SUCCESS;
153 static int disable(void) {
154 if (!interface_ready())
155 return BT_STATUS_NOT_READY;
157 stack_manager_get_interface()->shut_down_stack_async();
158 return BT_STATUS_SUCCESS;
161 static void cleanup(void) {
162 stack_manager_get_interface()->clean_up_stack();
165 static int get_adapter_properties(void)
168 if (interface_ready() == FALSE)
169 return BT_STATUS_NOT_READY;
171 return btif_get_adapter_properties();
174 static int get_adapter_property(bt_property_type_t type)
177 if (interface_ready() == FALSE)
178 return BT_STATUS_NOT_READY;
180 return btif_get_adapter_property(type);
183 static int set_adapter_property(const bt_property_t *property)
186 if (interface_ready() == FALSE)
187 return BT_STATUS_NOT_READY;
189 return btif_set_adapter_property(property);
192 int get_remote_device_properties(bt_bdaddr_t *remote_addr)
195 if (interface_ready() == FALSE)
196 return BT_STATUS_NOT_READY;
198 return btif_get_remote_device_properties(remote_addr);
201 int get_remote_device_property(bt_bdaddr_t *remote_addr, bt_property_type_t type)
204 if (interface_ready() == FALSE)
205 return BT_STATUS_NOT_READY;
207 return btif_get_remote_device_property(remote_addr, type);
210 int set_remote_device_property(bt_bdaddr_t *remote_addr, const bt_property_t *property)
213 if (interface_ready() == FALSE)
214 return BT_STATUS_NOT_READY;
216 return btif_set_remote_device_property(remote_addr, property);
219 int get_remote_service_record(bt_bdaddr_t *remote_addr, bt_uuid_t *uuid)
222 if (interface_ready() == FALSE)
223 return BT_STATUS_NOT_READY;
225 return btif_get_remote_service_record(remote_addr, uuid);
228 int get_remote_services(bt_bdaddr_t *remote_addr)
231 if (interface_ready() == FALSE)
232 return BT_STATUS_NOT_READY;
234 return btif_dm_get_remote_services(remote_addr);
237 static int start_discovery(void)
240 if (interface_ready() == FALSE)
241 return BT_STATUS_NOT_READY;
243 return btif_dm_start_discovery();
246 static int cancel_discovery(void)
249 if (interface_ready() == FALSE)
250 return BT_STATUS_NOT_READY;
252 return btif_dm_cancel_discovery();
255 static int create_bond(const bt_bdaddr_t *bd_addr, int transport)
258 if (interface_ready() == FALSE)
259 return BT_STATUS_NOT_READY;
261 return btif_dm_create_bond(bd_addr, transport);
264 static int create_bond_out_of_band(const bt_bdaddr_t *bd_addr, int transport,
265 const bt_out_of_band_data_t *oob_data)
268 if (interface_ready() == FALSE)
269 return BT_STATUS_NOT_READY;
271 return btif_dm_create_bond_out_of_band(bd_addr, transport, oob_data);
274 static int cancel_bond(const bt_bdaddr_t *bd_addr)
277 if (interface_ready() == FALSE)
278 return BT_STATUS_NOT_READY;
280 return btif_dm_cancel_bond(bd_addr);
283 static int remove_bond(const bt_bdaddr_t *bd_addr)
286 if (interface_ready() == FALSE)
287 return BT_STATUS_NOT_READY;
289 return btif_dm_remove_bond(bd_addr);
292 static int get_connection_state(const bt_bdaddr_t *bd_addr)
295 if (interface_ready() == FALSE)
298 return btif_dm_get_connection_state(bd_addr);
301 static int pin_reply(const bt_bdaddr_t *bd_addr, uint8_t accept,
302 uint8_t pin_len, bt_pin_code_t *pin_code)
305 if (interface_ready() == FALSE)
306 return BT_STATUS_NOT_READY;
308 return btif_dm_pin_reply(bd_addr, accept, pin_len, pin_code);
311 static int ssp_reply(const bt_bdaddr_t *bd_addr, bt_ssp_variant_t variant,
312 uint8_t accept, uint32_t passkey)
315 if (interface_ready() == FALSE)
316 return BT_STATUS_NOT_READY;
318 return btif_dm_ssp_reply(bd_addr, variant, accept, passkey);
321 static int read_energy_info()
323 if (interface_ready() == FALSE)
324 return BT_STATUS_NOT_READY;
325 btif_dm_read_energy_info();
326 return BT_STATUS_SUCCESS;
329 static void dump(int fd, const char **arguments)
331 if (arguments != NULL && arguments[0] != NULL) {
332 if (strncmp(arguments[0], "--proto-text", 12) == 0) {
333 btif_update_a2dp_metrics();
334 metrics_print(fd, true);
337 if (strncmp(arguments[0], "--proto-bin", 11) == 0) {
338 btif_update_a2dp_metrics();
339 metrics_write(fd, true);
343 btif_debug_conn_dump(fd);
344 btif_debug_bond_event_dump(fd);
345 btif_debug_a2dp_dump(fd);
346 wakelock_debug_dump(fd);
347 alarm_debug_dump(fd);
348 #if defined(BTSNOOP_MEM) && (BTSNOOP_MEM == TRUE)
349 btif_debug_btsnoop_dump(fd);
355 static const void* get_profile_interface (const char *profile_id)
357 LOG_INFO(LOG_TAG, "get_profile_interface %s", profile_id);
360 if (interface_ready() == FALSE)
363 /* check for supported profile interfaces */
364 if (is_profile(profile_id, BT_PROFILE_HANDSFREE_ID))
365 return btif_hf_get_interface();
367 if (is_profile(profile_id, BT_PROFILE_HANDSFREE_CLIENT_ID))
368 return btif_hf_client_get_interface();
370 if (is_profile(profile_id, BT_PROFILE_SOCKETS_ID))
371 return btif_sock_get_interface();
373 if (is_profile(profile_id, BT_PROFILE_PAN_ID))
374 return btif_pan_get_interface();
376 if (is_profile(profile_id, BT_PROFILE_ADVANCED_AUDIO_ID))
377 return btif_av_get_src_interface();
379 if (is_profile(profile_id, BT_PROFILE_ADVANCED_AUDIO_SINK_ID))
380 return btif_av_get_sink_interface();
382 if (is_profile(profile_id, BT_PROFILE_HIDHOST_ID))
383 return btif_hh_get_interface();
385 if (is_profile(profile_id, BT_PROFILE_HEALTH_ID))
386 return btif_hl_get_interface();
388 if (is_profile(profile_id, BT_PROFILE_SDP_CLIENT_ID))
389 return btif_sdp_get_interface();
391 #if ( BTA_GATT_INCLUDED == TRUE && BLE_INCLUDED == TRUE)
392 if (is_profile(profile_id, BT_PROFILE_GATT_ID))
393 return btif_gatt_get_interface();
396 if (is_profile(profile_id, BT_PROFILE_AV_RC_ID))
397 return btif_rc_get_interface();
399 if (is_profile(profile_id, BT_PROFILE_AV_RC_CTRL_ID))
400 return btif_rc_ctrl_get_interface();
405 int dut_mode_configure(uint8_t enable)
407 LOG_INFO(LOG_TAG, "dut_mode_configure");
410 if (interface_ready() == FALSE)
411 return BT_STATUS_NOT_READY;
413 return btif_dut_mode_configure(enable);
416 int dut_mode_send(uint16_t opcode, uint8_t* buf, uint8_t len)
418 LOG_INFO(LOG_TAG, "dut_mode_send");
421 if (interface_ready() == FALSE)
422 return BT_STATUS_NOT_READY;
424 return btif_dut_mode_send(opcode, buf, len);
427 #if BLE_INCLUDED == TRUE
428 int le_test_mode(uint16_t opcode, uint8_t* buf, uint8_t len)
430 LOG_INFO(LOG_TAG, "le_test_mode");
433 if (interface_ready() == FALSE)
434 return BT_STATUS_NOT_READY;
436 return btif_le_test_mode(opcode, buf, len);
440 int config_hci_snoop_log(uint8_t enable)
442 LOG_INFO(LOG_TAG, "config_hci_snoop_log");
444 if (!interface_ready())
445 return BT_STATUS_NOT_READY;
447 btsnoop_get_interface()->set_api_wants_to_log(enable);
448 return BT_STATUS_SUCCESS;
451 static int set_os_callouts(bt_os_callouts_t *callouts) {
452 wakelock_set_os_callouts(callouts);
453 return BT_STATUS_SUCCESS;
456 static int config_clear(void) {
457 LOG_INFO(LOG_TAG, "%s", __func__);
458 return btif_config_clear() ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
461 static const bt_interface_t bluetoothInterface = {
462 sizeof(bluetoothInterface),
467 get_adapter_properties,
468 get_adapter_property,
469 set_adapter_property,
470 get_remote_device_properties,
471 get_remote_device_property,
472 set_remote_device_property,
473 get_remote_service_record,
478 create_bond_out_of_band,
481 get_connection_state,
484 get_profile_interface,
487 #if BLE_INCLUDED == TRUE
492 config_hci_snoop_log,
497 interop_database_clear,
498 interop_database_add,
501 const bt_interface_t* bluetooth__get_bluetooth_interface ()
503 /* fixme -- add property to disable bt interface ? */
505 return &bluetoothInterface;
508 static int close_bluetooth_stack(struct hw_device_t* device)
515 static int open_bluetooth_stack(const struct hw_module_t *module, UNUSED_ATTR char const *name, struct hw_device_t **abstraction) {
516 static bluetooth_device_t device = {
518 .tag = HARDWARE_DEVICE_TAG,
520 .close = close_bluetooth_stack,
522 .get_bluetooth_interface = bluetooth__get_bluetooth_interface
525 device.common.module = (struct hw_module_t *)module;
526 *abstraction = (struct hw_device_t *)&device;
530 static struct hw_module_methods_t bt_stack_module_methods = {
531 .open = open_bluetooth_stack,
534 EXPORT_SYMBOL struct hw_module_t HAL_MODULE_INFO_SYM = {
535 .tag = HARDWARE_MODULE_TAG,
538 .id = BT_HARDWARE_MODULE_ID,
539 .name = "Bluetooth Stack",
540 .author = "The Android Open Source Project",
541 .methods = &bt_stack_module_methods