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 ***********************************************************************************/
32 #include <hardware/bluetooth.h>
33 #include <hardware/bt_hf.h>
34 #include <hardware/bt_hf_client.h>
35 #include <hardware/bt_av.h>
36 #include <hardware/bt_sock.h>
37 #include <hardware/bt_hh.h>
38 #include <hardware/bt_hl.h>
39 #include <hardware/bt_pan.h>
40 #include <hardware/bt_mce.h>
41 #include <hardware/bt_gatt.h>
42 #include <hardware/bt_rc.h>
43 #include <hardware/bt_sdp.h>
46 #define LOG_TAG "bt_bluedroid"
49 #include "btif_debug.h"
51 #include "btsnoop_mem.h"
53 #include "osi/include/osi.h"
54 #include "osi/include/allocation_tracker.h"
55 #include "osi/include/log.h"
56 #include "stack_manager.h"
57 #include "btif_config.h"
59 /************************************************************************************
61 ************************************************************************************/
63 #define is_profile(profile, str) ((strlen(str) == strlen(profile)) && strncmp((const char *)profile, str, strlen(str)) == 0)
65 /************************************************************************************
67 ************************************************************************************/
69 bt_callbacks_t *bt_hal_cbacks = NULL;
71 /** Operating System specific callouts for resource management */
72 bt_os_callouts_t *bt_os_callouts = NULL;
74 /************************************************************************************
76 ************************************************************************************/
78 /* list all extended interfaces here */
80 /* handsfree profile */
81 extern bthf_interface_t *btif_hf_get_interface();
82 /* handsfree profile - client */
83 extern bthf_client_interface_t *btif_hf_client_get_interface();
84 /* advanced audio profile */
85 extern btav_interface_t *btif_av_get_src_interface();
86 extern btav_interface_t *btif_av_get_sink_interface();
88 extern btsock_interface_t *btif_sock_get_interface();
89 /* hid host profile */
90 extern bthh_interface_t *btif_hh_get_interface();
91 /* health device profile */
92 extern bthl_interface_t *btif_hl_get_interface();
94 extern btpan_interface_t *btif_pan_get_interface();
96 extern btmce_interface_t *btif_mce_get_interface();
97 #if BLE_INCLUDED == TRUE
99 extern btgatt_interface_t *btif_gatt_get_interface();
102 extern btrc_interface_t *btif_rc_get_interface();
103 /* avrc controller */
104 extern btrc_interface_t *btif_rc_ctrl_get_interface();
105 /*SDP search client*/
106 extern btsdp_interface_t *btif_sdp_get_interface();
108 /************************************************************************************
110 ************************************************************************************/
112 static bool interface_ready(void) {
113 return bt_hal_cbacks != NULL;
116 /*****************************************************************************
118 ** BLUETOOTH HAL INTERFACE FUNCTIONS
120 *****************************************************************************/
122 static int init(bt_callbacks_t *callbacks) {
123 LOG_INFO(LOG_TAG, "%s", __func__);
125 if (interface_ready())
126 return BT_STATUS_DONE;
128 #ifdef BLUEDROID_DEBUG
129 allocation_tracker_init();
132 bt_hal_cbacks = callbacks;
133 stack_manager_get_interface()->init_stack();
135 return BT_STATUS_SUCCESS;
138 static int enable(void) {
139 LOG_INFO(LOG_TAG, "%s", __func__);
141 if (!interface_ready())
142 return BT_STATUS_NOT_READY;
144 stack_manager_get_interface()->start_up_stack_async();
145 return BT_STATUS_SUCCESS;
148 static int disable(void) {
149 if (!interface_ready())
150 return BT_STATUS_NOT_READY;
152 stack_manager_get_interface()->shut_down_stack_async();
153 return BT_STATUS_SUCCESS;
156 static void cleanup(void) {
157 stack_manager_get_interface()->clean_up_stack_async();
160 static int get_adapter_properties(void)
163 if (interface_ready() == FALSE)
164 return BT_STATUS_NOT_READY;
166 return btif_get_adapter_properties();
169 static int get_adapter_property(bt_property_type_t type)
172 if (interface_ready() == FALSE)
173 return BT_STATUS_NOT_READY;
175 return btif_get_adapter_property(type);
178 static int set_adapter_property(const bt_property_t *property)
181 if (interface_ready() == FALSE)
182 return BT_STATUS_NOT_READY;
184 return btif_set_adapter_property(property);
187 int get_remote_device_properties(bt_bdaddr_t *remote_addr)
190 if (interface_ready() == FALSE)
191 return BT_STATUS_NOT_READY;
193 return btif_get_remote_device_properties(remote_addr);
196 int get_remote_device_property(bt_bdaddr_t *remote_addr, bt_property_type_t type)
199 if (interface_ready() == FALSE)
200 return BT_STATUS_NOT_READY;
202 return btif_get_remote_device_property(remote_addr, type);
205 int set_remote_device_property(bt_bdaddr_t *remote_addr, const bt_property_t *property)
208 if (interface_ready() == FALSE)
209 return BT_STATUS_NOT_READY;
211 return btif_set_remote_device_property(remote_addr, property);
214 int get_remote_service_record(bt_bdaddr_t *remote_addr, bt_uuid_t *uuid)
217 if (interface_ready() == FALSE)
218 return BT_STATUS_NOT_READY;
220 return btif_get_remote_service_record(remote_addr, uuid);
223 int get_remote_services(bt_bdaddr_t *remote_addr)
226 if (interface_ready() == FALSE)
227 return BT_STATUS_NOT_READY;
229 return btif_dm_get_remote_services(remote_addr);
232 static int start_discovery(void)
235 if (interface_ready() == FALSE)
236 return BT_STATUS_NOT_READY;
238 return btif_dm_start_discovery();
241 static int cancel_discovery(void)
244 if (interface_ready() == FALSE)
245 return BT_STATUS_NOT_READY;
247 return btif_dm_cancel_discovery();
250 static int create_bond(const bt_bdaddr_t *bd_addr, int transport)
253 if (interface_ready() == FALSE)
254 return BT_STATUS_NOT_READY;
256 return btif_dm_create_bond(bd_addr, transport);
259 static int cancel_bond(const bt_bdaddr_t *bd_addr)
262 if (interface_ready() == FALSE)
263 return BT_STATUS_NOT_READY;
265 return btif_dm_cancel_bond(bd_addr);
268 static int remove_bond(const bt_bdaddr_t *bd_addr)
271 if (interface_ready() == FALSE)
272 return BT_STATUS_NOT_READY;
274 return btif_dm_remove_bond(bd_addr);
277 static int get_connection_state(const bt_bdaddr_t *bd_addr)
280 if (interface_ready() == FALSE)
283 return btif_dm_get_connection_state(bd_addr);
286 static int pin_reply(const bt_bdaddr_t *bd_addr, uint8_t accept,
287 uint8_t pin_len, bt_pin_code_t *pin_code)
290 if (interface_ready() == FALSE)
291 return BT_STATUS_NOT_READY;
293 return btif_dm_pin_reply(bd_addr, accept, pin_len, pin_code);
296 static int ssp_reply(const bt_bdaddr_t *bd_addr, bt_ssp_variant_t variant,
297 uint8_t accept, uint32_t passkey)
300 if (interface_ready() == FALSE)
301 return BT_STATUS_NOT_READY;
303 return btif_dm_ssp_reply(bd_addr, variant, accept, passkey);
306 static int read_energy_info()
308 if (interface_ready() == FALSE)
309 return BT_STATUS_NOT_READY;
310 btif_dm_read_energy_info();
311 return BT_STATUS_SUCCESS;
314 static void dump(int fd)
319 static const void* get_profile_interface (const char *profile_id)
321 LOG_INFO(LOG_TAG, "get_profile_interface %s", profile_id);
324 if (interface_ready() == FALSE)
327 /* check for supported profile interfaces */
328 if (is_profile(profile_id, BT_PROFILE_HANDSFREE_ID))
329 return btif_hf_get_interface();
331 if (is_profile(profile_id, BT_PROFILE_HANDSFREE_CLIENT_ID))
332 return btif_hf_client_get_interface();
334 if (is_profile(profile_id, BT_PROFILE_SOCKETS_ID))
335 return btif_sock_get_interface();
337 if (is_profile(profile_id, BT_PROFILE_PAN_ID))
338 return btif_pan_get_interface();
340 if (is_profile(profile_id, BT_PROFILE_ADVANCED_AUDIO_ID))
341 return btif_av_get_src_interface();
343 if (is_profile(profile_id, BT_PROFILE_ADVANCED_AUDIO_SINK_ID))
344 return btif_av_get_sink_interface();
346 if (is_profile(profile_id, BT_PROFILE_HIDHOST_ID))
347 return btif_hh_get_interface();
349 if (is_profile(profile_id, BT_PROFILE_HEALTH_ID))
350 return btif_hl_get_interface();
352 if (is_profile(profile_id, BT_PROFILE_SDP_CLIENT_ID))
353 return btif_sdp_get_interface();
355 #if ( BTA_GATT_INCLUDED == TRUE && BLE_INCLUDED == TRUE)
356 if (is_profile(profile_id, BT_PROFILE_GATT_ID))
357 return btif_gatt_get_interface();
360 if (is_profile(profile_id, BT_PROFILE_AV_RC_ID))
361 return btif_rc_get_interface();
363 if (is_profile(profile_id, BT_PROFILE_AV_RC_CTRL_ID))
364 return btif_rc_ctrl_get_interface();
369 int dut_mode_configure(uint8_t enable)
371 LOG_INFO(LOG_TAG, "dut_mode_configure");
374 if (interface_ready() == FALSE)
375 return BT_STATUS_NOT_READY;
377 return btif_dut_mode_configure(enable);
380 int dut_mode_send(uint16_t opcode, uint8_t* buf, uint8_t len)
382 LOG_INFO(LOG_TAG, "dut_mode_send");
385 if (interface_ready() == FALSE)
386 return BT_STATUS_NOT_READY;
388 return btif_dut_mode_send(opcode, buf, len);
391 #if BLE_INCLUDED == TRUE
392 int le_test_mode(uint16_t opcode, uint8_t* buf, uint8_t len)
394 LOG_INFO(LOG_TAG, "le_test_mode");
397 if (interface_ready() == FALSE)
398 return BT_STATUS_NOT_READY;
400 return btif_le_test_mode(opcode, buf, len);
404 int config_hci_snoop_log(uint8_t enable)
406 LOG_INFO(LOG_TAG, "config_hci_snoop_log");
408 if (!interface_ready())
409 return BT_STATUS_NOT_READY;
411 btsnoop_get_interface()->set_api_wants_to_log(enable);
412 return BT_STATUS_SUCCESS;
415 static int set_os_callouts(bt_os_callouts_t *callouts) {
416 bt_os_callouts = callouts;
417 return BT_STATUS_SUCCESS;
420 static int config_clear(void) {
421 LOG_INFO("%s", __func__);
422 return btif_config_clear();
425 static const bt_interface_t bluetoothInterface = {
426 sizeof(bluetoothInterface),
431 get_adapter_properties,
432 get_adapter_property,
433 set_adapter_property,
434 get_remote_device_properties,
435 get_remote_device_property,
436 set_remote_device_property,
437 get_remote_service_record,
444 get_connection_state,
447 get_profile_interface,
450 #if BLE_INCLUDED == TRUE
455 config_hci_snoop_log,
462 const bt_interface_t* bluetooth__get_bluetooth_interface ()
464 /* fixme -- add property to disable bt interface ? */
466 return &bluetoothInterface;
469 static int close_bluetooth_stack(struct hw_device_t* device)
476 static int open_bluetooth_stack(const struct hw_module_t *module, UNUSED_ATTR char const *name, struct hw_device_t **abstraction) {
477 static bluetooth_device_t device = {
479 .tag = HARDWARE_DEVICE_TAG,
481 .close = close_bluetooth_stack,
483 .get_bluetooth_interface = bluetooth__get_bluetooth_interface
486 device.common.module = (struct hw_module_t *)module;
487 *abstraction = (struct hw_device_t *)&device;
492 static struct hw_module_methods_t bt_stack_module_methods = {
493 .open = open_bluetooth_stack,
496 EXPORT_SYMBOL struct hw_module_t HAL_MODULE_INFO_SYM = {
497 .tag = HARDWARE_MODULE_TAG,
500 .id = BT_HARDWARE_MODULE_ID,
501 .name = "Bluetooth Stack",
502 .author = "The Android Open Source Project",
503 .methods = &bt_stack_module_methods