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"
35 #include <hardware/bluetooth.h>
36 #include <hardware/bt_av.h>
37 #include <hardware/bt_gatt.h>
38 #include <hardware/bt_hf.h>
39 #include <hardware/bt_hf_client.h>
40 #include <hardware/bt_hh.h>
41 #include <hardware/bt_hl.h>
42 #include <hardware/bt_mce.h>
43 #include <hardware/bt_pan.h>
44 #include <hardware/bt_rc.h>
45 #include <hardware/bt_sdp.h>
46 #include <hardware/bt_sock.h>
50 #include "btif_debug.h"
52 #include "btsnoop_mem.h"
53 #include "osi/include/allocation_tracker.h"
54 #include "osi/include/log.h"
55 #include "osi/include/osi.h"
56 #include "osi/include/wakelock.h"
57 #include "stack_manager.h"
58 #include "btif_config.h"
60 /************************************************************************************
62 ************************************************************************************/
64 #define is_profile(profile, str) ((strlen(str) == strlen(profile)) && strncmp((const char *)profile, str, strlen(str)) == 0)
66 /************************************************************************************
68 ************************************************************************************/
70 bt_callbacks_t *bt_hal_cbacks = NULL;
72 /************************************************************************************
74 ************************************************************************************/
76 /* list all extended interfaces here */
78 /* handsfree profile */
79 extern bthf_interface_t *btif_hf_get_interface();
80 /* handsfree profile - client */
81 extern bthf_client_interface_t *btif_hf_client_get_interface();
82 /* advanced audio profile */
83 extern btav_interface_t *btif_av_get_src_interface();
84 extern btav_interface_t *btif_av_get_sink_interface();
86 extern btsock_interface_t *btif_sock_get_interface();
87 /* hid host profile */
88 extern bthh_interface_t *btif_hh_get_interface();
89 /* health device profile */
90 extern bthl_interface_t *btif_hl_get_interface();
92 extern btpan_interface_t *btif_pan_get_interface();
94 extern btmce_interface_t *btif_mce_get_interface();
95 #if BLE_INCLUDED == TRUE
97 extern btgatt_interface_t *btif_gatt_get_interface();
100 extern btrc_interface_t *btif_rc_get_interface();
101 /* avrc controller */
102 extern btrc_interface_t *btif_rc_ctrl_get_interface();
103 /*SDP search client*/
104 extern btsdp_interface_t *btif_sdp_get_interface();
106 /************************************************************************************
108 ************************************************************************************/
110 static bool interface_ready(void) {
111 return bt_hal_cbacks != NULL;
114 /*****************************************************************************
116 ** BLUETOOTH HAL INTERFACE FUNCTIONS
118 *****************************************************************************/
120 static int init(bt_callbacks_t *callbacks) {
121 LOG_INFO(LOG_TAG, "%s", __func__);
123 if (interface_ready())
124 return BT_STATUS_DONE;
126 #ifdef BLUEDROID_DEBUG
127 allocation_tracker_init();
130 bt_hal_cbacks = callbacks;
131 stack_manager_get_interface()->init_stack();
133 return BT_STATUS_SUCCESS;
136 static int enable(void) {
137 LOG_INFO(LOG_TAG, "%s", __func__);
139 if (!interface_ready())
140 return BT_STATUS_NOT_READY;
142 stack_manager_get_interface()->start_up_stack_async();
143 return BT_STATUS_SUCCESS;
146 static int disable(void) {
147 if (!interface_ready())
148 return BT_STATUS_NOT_READY;
150 stack_manager_get_interface()->shut_down_stack_async();
151 return BT_STATUS_SUCCESS;
154 static void cleanup(void) {
155 stack_manager_get_interface()->clean_up_stack();
158 static int get_adapter_properties(void)
161 if (interface_ready() == FALSE)
162 return BT_STATUS_NOT_READY;
164 return btif_get_adapter_properties();
167 static int get_adapter_property(bt_property_type_t type)
170 if (interface_ready() == FALSE)
171 return BT_STATUS_NOT_READY;
173 return btif_get_adapter_property(type);
176 static int set_adapter_property(const bt_property_t *property)
179 if (interface_ready() == FALSE)
180 return BT_STATUS_NOT_READY;
182 return btif_set_adapter_property(property);
185 int get_remote_device_properties(bt_bdaddr_t *remote_addr)
188 if (interface_ready() == FALSE)
189 return BT_STATUS_NOT_READY;
191 return btif_get_remote_device_properties(remote_addr);
194 int get_remote_device_property(bt_bdaddr_t *remote_addr, bt_property_type_t type)
197 if (interface_ready() == FALSE)
198 return BT_STATUS_NOT_READY;
200 return btif_get_remote_device_property(remote_addr, type);
203 int set_remote_device_property(bt_bdaddr_t *remote_addr, const bt_property_t *property)
206 if (interface_ready() == FALSE)
207 return BT_STATUS_NOT_READY;
209 return btif_set_remote_device_property(remote_addr, property);
212 int get_remote_service_record(bt_bdaddr_t *remote_addr, bt_uuid_t *uuid)
215 if (interface_ready() == FALSE)
216 return BT_STATUS_NOT_READY;
218 return btif_get_remote_service_record(remote_addr, uuid);
221 int get_remote_services(bt_bdaddr_t *remote_addr)
224 if (interface_ready() == FALSE)
225 return BT_STATUS_NOT_READY;
227 return btif_dm_get_remote_services(remote_addr);
230 static int start_discovery(void)
233 if (interface_ready() == FALSE)
234 return BT_STATUS_NOT_READY;
236 return btif_dm_start_discovery();
239 static int cancel_discovery(void)
242 if (interface_ready() == FALSE)
243 return BT_STATUS_NOT_READY;
245 return btif_dm_cancel_discovery();
248 static int create_bond(const bt_bdaddr_t *bd_addr, int transport)
251 if (interface_ready() == FALSE)
252 return BT_STATUS_NOT_READY;
254 return btif_dm_create_bond(bd_addr, transport);
257 static int create_bond_out_of_band(const bt_bdaddr_t *bd_addr, int transport,
258 const bt_out_of_band_data_t *oob_data)
261 if (interface_ready() == FALSE)
262 return BT_STATUS_NOT_READY;
264 return btif_dm_create_bond_out_of_band(bd_addr, transport, oob_data);
267 static int cancel_bond(const bt_bdaddr_t *bd_addr)
270 if (interface_ready() == FALSE)
271 return BT_STATUS_NOT_READY;
273 return btif_dm_cancel_bond(bd_addr);
276 static int remove_bond(const bt_bdaddr_t *bd_addr)
279 if (interface_ready() == FALSE)
280 return BT_STATUS_NOT_READY;
282 return btif_dm_remove_bond(bd_addr);
285 static int get_connection_state(const bt_bdaddr_t *bd_addr)
288 if (interface_ready() == FALSE)
291 return btif_dm_get_connection_state(bd_addr);
294 static int pin_reply(const bt_bdaddr_t *bd_addr, uint8_t accept,
295 uint8_t pin_len, bt_pin_code_t *pin_code)
298 if (interface_ready() == FALSE)
299 return BT_STATUS_NOT_READY;
301 return btif_dm_pin_reply(bd_addr, accept, pin_len, pin_code);
304 static int ssp_reply(const bt_bdaddr_t *bd_addr, bt_ssp_variant_t variant,
305 uint8_t accept, uint32_t passkey)
308 if (interface_ready() == FALSE)
309 return BT_STATUS_NOT_READY;
311 return btif_dm_ssp_reply(bd_addr, variant, accept, passkey);
314 static int read_energy_info()
316 if (interface_ready() == FALSE)
317 return BT_STATUS_NOT_READY;
318 btif_dm_read_energy_info();
319 return BT_STATUS_SUCCESS;
322 static void dump(int fd)
327 static const void* get_profile_interface (const char *profile_id)
329 LOG_INFO(LOG_TAG, "get_profile_interface %s", profile_id);
332 if (interface_ready() == FALSE)
335 /* check for supported profile interfaces */
336 if (is_profile(profile_id, BT_PROFILE_HANDSFREE_ID))
337 return btif_hf_get_interface();
339 if (is_profile(profile_id, BT_PROFILE_HANDSFREE_CLIENT_ID))
340 return btif_hf_client_get_interface();
342 if (is_profile(profile_id, BT_PROFILE_SOCKETS_ID))
343 return btif_sock_get_interface();
345 if (is_profile(profile_id, BT_PROFILE_PAN_ID))
346 return btif_pan_get_interface();
348 if (is_profile(profile_id, BT_PROFILE_ADVANCED_AUDIO_ID))
349 return btif_av_get_src_interface();
351 if (is_profile(profile_id, BT_PROFILE_ADVANCED_AUDIO_SINK_ID))
352 return btif_av_get_sink_interface();
354 if (is_profile(profile_id, BT_PROFILE_HIDHOST_ID))
355 return btif_hh_get_interface();
357 if (is_profile(profile_id, BT_PROFILE_HEALTH_ID))
358 return btif_hl_get_interface();
360 if (is_profile(profile_id, BT_PROFILE_SDP_CLIENT_ID))
361 return btif_sdp_get_interface();
363 #if ( BTA_GATT_INCLUDED == TRUE && BLE_INCLUDED == TRUE)
364 if (is_profile(profile_id, BT_PROFILE_GATT_ID))
365 return btif_gatt_get_interface();
368 if (is_profile(profile_id, BT_PROFILE_AV_RC_ID))
369 return btif_rc_get_interface();
371 if (is_profile(profile_id, BT_PROFILE_AV_RC_CTRL_ID))
372 return btif_rc_ctrl_get_interface();
377 int dut_mode_configure(uint8_t enable)
379 LOG_INFO(LOG_TAG, "dut_mode_configure");
382 if (interface_ready() == FALSE)
383 return BT_STATUS_NOT_READY;
385 return btif_dut_mode_configure(enable);
388 int dut_mode_send(uint16_t opcode, uint8_t* buf, uint8_t len)
390 LOG_INFO(LOG_TAG, "dut_mode_send");
393 if (interface_ready() == FALSE)
394 return BT_STATUS_NOT_READY;
396 return btif_dut_mode_send(opcode, buf, len);
399 #if BLE_INCLUDED == TRUE
400 int le_test_mode(uint16_t opcode, uint8_t* buf, uint8_t len)
402 LOG_INFO(LOG_TAG, "le_test_mode");
405 if (interface_ready() == FALSE)
406 return BT_STATUS_NOT_READY;
408 return btif_le_test_mode(opcode, buf, len);
412 int config_hci_snoop_log(uint8_t enable)
414 LOG_INFO(LOG_TAG, "config_hci_snoop_log");
416 if (!interface_ready())
417 return BT_STATUS_NOT_READY;
419 btsnoop_get_interface()->set_api_wants_to_log(enable);
420 return BT_STATUS_SUCCESS;
423 static int set_os_callouts(bt_os_callouts_t *callouts) {
424 wakelock_set_os_callouts(callouts);
425 return BT_STATUS_SUCCESS;
428 static int config_clear(void) {
429 LOG_INFO("%s", __func__);
430 return btif_config_clear();
433 static const bt_interface_t bluetoothInterface = {
434 sizeof(bluetoothInterface),
439 get_adapter_properties,
440 get_adapter_property,
441 set_adapter_property,
442 get_remote_device_properties,
443 get_remote_device_property,
444 set_remote_device_property,
445 get_remote_service_record,
450 create_bond_out_of_band,
453 get_connection_state,
456 get_profile_interface,
459 #if BLE_INCLUDED == TRUE
464 config_hci_snoop_log,
471 const bt_interface_t* bluetooth__get_bluetooth_interface ()
473 /* fixme -- add property to disable bt interface ? */
475 return &bluetoothInterface;
478 static int close_bluetooth_stack(struct hw_device_t* device)
485 static int open_bluetooth_stack(const struct hw_module_t *module, UNUSED_ATTR char const *name, struct hw_device_t **abstraction) {
486 static bluetooth_device_t device = {
488 .tag = HARDWARE_DEVICE_TAG,
490 .close = close_bluetooth_stack,
492 .get_bluetooth_interface = bluetooth__get_bluetooth_interface
495 device.common.module = (struct hw_module_t *)module;
496 *abstraction = (struct hw_device_t *)&device;
500 static struct hw_module_methods_t bt_stack_module_methods = {
501 .open = open_bluetooth_stack,
504 EXPORT_SYMBOL struct hw_module_t HAL_MODULE_INFO_SYM = {
505 .tag = HARDWARE_MODULE_TAG,
508 .id = BT_HARDWARE_MODULE_ID,
509 .name = "Bluetooth Stack",
510 .author = "The Android Open Source Project",
511 .methods = &bt_stack_module_methods