bluetooth.cc revision d35a648d39710bbc5ac59f8add85166455af5af7
1/******************************************************************************
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
19/************************************************************************************
20 *
21 *  Filename:      bluetooth.c
22 *
23 *  Description:   Bluetooth HAL implementation
24 *
25 ***********************************************************************************/
26
27#define LOG_TAG "bt_btif"
28
29#include <assert.h>
30#include <stdio.h>
31#include <stdlib.h>
32#include <string.h>
33#include <unistd.h>
34
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>
47
48#include "bt_utils.h"
49#include "btif/include/btif_debug_btsnoop.h"
50#include "btif/include/btif_debug_conn.h"
51#include "btif_a2dp.h"
52#include "btif_api.h"
53#include "btif_config.h"
54#include "btif_debug.h"
55#include "btif_storage.h"
56#include "btsnoop.h"
57#include "btsnoop_mem.h"
58#include "device/include/interop.h"
59#include "osi/include/alarm.h"
60#include "osi/include/allocation_tracker.h"
61#include "osi/include/log.h"
62#include "osi/include/metrics.h"
63#include "osi/include/osi.h"
64#include "osi/include/wakelock.h"
65#include "stack_manager.h"
66
67/************************************************************************************
68 *  Static variables
69 ***********************************************************************************/
70
71bt_callbacks_t* bt_hal_cbacks = NULL;
72bool restricted_mode = false;
73
74/************************************************************************************
75 *  Externs
76 ***********************************************************************************/
77
78/* list all extended interfaces here */
79
80/* handsfree profile */
81extern bthf_interface_t* btif_hf_get_interface();
82/* handsfree profile - client */
83extern bthf_client_interface_t* btif_hf_client_get_interface();
84/* advanced audio profile */
85extern btav_interface_t* btif_av_get_src_interface();
86extern btav_interface_t* btif_av_get_sink_interface();
87/*rfc l2cap*/
88extern btsock_interface_t* btif_sock_get_interface();
89/* hid host profile */
90extern bthh_interface_t* btif_hh_get_interface();
91/* health device profile */
92extern bthl_interface_t* btif_hl_get_interface();
93/*pan*/
94extern btpan_interface_t* btif_pan_get_interface();
95/*map client*/
96extern btmce_interface_t* btif_mce_get_interface();
97#if (BLE_INCLUDED == TRUE)
98/* gatt */
99extern const btgatt_interface_t* btif_gatt_get_interface();
100#endif
101/* avrc target */
102extern btrc_interface_t* btif_rc_get_interface();
103/* avrc controller */
104extern btrc_interface_t* btif_rc_ctrl_get_interface();
105/*SDP search client*/
106extern btsdp_interface_t* btif_sdp_get_interface();
107
108/************************************************************************************
109 *  Functions
110 ***********************************************************************************/
111
112static bool interface_ready(void) { return bt_hal_cbacks != NULL; }
113
114static bool is_profile(const char* p1, const char* p2) {
115  assert(p1);
116  assert(p2);
117  return strlen(p1) == strlen(p2) && strncmp(p1, p2, strlen(p2)) == 0;
118}
119
120/*****************************************************************************
121 *
122 *   BLUETOOTH HAL INTERFACE FUNCTIONS
123 *
124 ****************************************************************************/
125
126static int init(bt_callbacks_t* callbacks) {
127  LOG_INFO(LOG_TAG, "%s", __func__);
128
129  if (interface_ready()) return BT_STATUS_DONE;
130
131#ifdef BLUEDROID_DEBUG
132  allocation_tracker_init();
133#endif
134
135  bt_hal_cbacks = callbacks;
136  stack_manager_get_interface()->init_stack();
137  btif_debug_init();
138  return BT_STATUS_SUCCESS;
139}
140
141static int enable(bool start_restricted) {
142  LOG_INFO(LOG_TAG, "%s: start restricted = %d", __func__, start_restricted);
143
144  restricted_mode = start_restricted;
145
146  if (!interface_ready()) return BT_STATUS_NOT_READY;
147
148  stack_manager_get_interface()->start_up_stack_async();
149  return BT_STATUS_SUCCESS;
150}
151
152static int disable(void) {
153  if (!interface_ready()) return BT_STATUS_NOT_READY;
154
155  stack_manager_get_interface()->shut_down_stack_async();
156  return BT_STATUS_SUCCESS;
157}
158
159static void cleanup(void) { stack_manager_get_interface()->clean_up_stack(); }
160
161bool is_restricted_mode() { return restricted_mode; }
162
163static int get_adapter_properties(void) {
164  /* sanity check */
165  if (interface_ready() == false) return BT_STATUS_NOT_READY;
166
167  return btif_get_adapter_properties();
168}
169
170static int get_adapter_property(bt_property_type_t type) {
171  /* sanity check */
172  if (interface_ready() == false) return BT_STATUS_NOT_READY;
173
174  return btif_get_adapter_property(type);
175}
176
177static int set_adapter_property(const bt_property_t* property) {
178  /* sanity check */
179  if (interface_ready() == false) return BT_STATUS_NOT_READY;
180
181  return btif_set_adapter_property(property);
182}
183
184int get_remote_device_properties(bt_bdaddr_t* remote_addr) {
185  /* sanity check */
186  if (interface_ready() == false) return BT_STATUS_NOT_READY;
187
188  return btif_get_remote_device_properties(remote_addr);
189}
190
191int get_remote_device_property(bt_bdaddr_t* remote_addr,
192                               bt_property_type_t type) {
193  /* sanity check */
194  if (interface_ready() == false) return BT_STATUS_NOT_READY;
195
196  return btif_get_remote_device_property(remote_addr, type);
197}
198
199int set_remote_device_property(bt_bdaddr_t* remote_addr,
200                               const bt_property_t* property) {
201  /* sanity check */
202  if (interface_ready() == false) return BT_STATUS_NOT_READY;
203
204  return btif_set_remote_device_property(remote_addr, property);
205}
206
207int get_remote_service_record(bt_bdaddr_t* remote_addr, bt_uuid_t* uuid) {
208  /* sanity check */
209  if (interface_ready() == false) return BT_STATUS_NOT_READY;
210
211  return btif_get_remote_service_record(remote_addr, uuid);
212}
213
214int get_remote_services(bt_bdaddr_t* remote_addr) {
215  /* sanity check */
216  if (interface_ready() == false) return BT_STATUS_NOT_READY;
217
218  return btif_dm_get_remote_services(remote_addr);
219}
220
221static int start_discovery(void) {
222  /* sanity check */
223  if (interface_ready() == false) return BT_STATUS_NOT_READY;
224
225  return btif_dm_start_discovery();
226}
227
228static int cancel_discovery(void) {
229  /* sanity check */
230  if (interface_ready() == false) return BT_STATUS_NOT_READY;
231
232  return btif_dm_cancel_discovery();
233}
234
235static int create_bond(const bt_bdaddr_t* bd_addr, int transport) {
236  /* sanity check */
237  if (interface_ready() == false) return BT_STATUS_NOT_READY;
238
239  return btif_dm_create_bond(bd_addr, transport);
240}
241
242static int create_bond_out_of_band(const bt_bdaddr_t* bd_addr, int transport,
243                                   const bt_out_of_band_data_t* oob_data) {
244  /* sanity check */
245  if (interface_ready() == false) return BT_STATUS_NOT_READY;
246
247  return btif_dm_create_bond_out_of_band(bd_addr, transport, oob_data);
248}
249
250static int cancel_bond(const bt_bdaddr_t* bd_addr) {
251  /* sanity check */
252  if (interface_ready() == false) return BT_STATUS_NOT_READY;
253
254  return btif_dm_cancel_bond(bd_addr);
255}
256
257static int remove_bond(const bt_bdaddr_t* bd_addr) {
258  if (is_restricted_mode() && !btif_storage_is_restricted_device(bd_addr))
259    return BT_STATUS_SUCCESS;
260
261  /* sanity check */
262  if (interface_ready() == false) return BT_STATUS_NOT_READY;
263
264  return btif_dm_remove_bond(bd_addr);
265}
266
267static int get_connection_state(const bt_bdaddr_t* bd_addr) {
268  /* sanity check */
269  if (interface_ready() == false) return 0;
270
271  return btif_dm_get_connection_state(bd_addr);
272}
273
274static int pin_reply(const bt_bdaddr_t* bd_addr, uint8_t accept,
275                     uint8_t pin_len, bt_pin_code_t* pin_code) {
276  /* sanity check */
277  if (interface_ready() == false) return BT_STATUS_NOT_READY;
278
279  return btif_dm_pin_reply(bd_addr, accept, pin_len, pin_code);
280}
281
282static int ssp_reply(const bt_bdaddr_t* bd_addr, bt_ssp_variant_t variant,
283                     uint8_t accept, uint32_t passkey) {
284  /* sanity check */
285  if (interface_ready() == false) return BT_STATUS_NOT_READY;
286
287  return btif_dm_ssp_reply(bd_addr, variant, accept, passkey);
288}
289
290static int read_energy_info() {
291  if (interface_ready() == false) return BT_STATUS_NOT_READY;
292  btif_dm_read_energy_info();
293  return BT_STATUS_SUCCESS;
294}
295
296static void dump(int fd, const char** arguments) {
297  if (arguments != NULL && arguments[0] != NULL) {
298    if (strncmp(arguments[0], "--proto-bin", 11) == 0) {
299      btif_update_a2dp_metrics();
300      metrics_write(fd, true);
301      return;
302    }
303  }
304  btif_debug_conn_dump(fd);
305  btif_debug_bond_event_dump(fd);
306  btif_debug_a2dp_dump(fd);
307  btif_debug_config_dump(fd);
308  wakelock_debug_dump(fd);
309  alarm_debug_dump(fd);
310#if (BTSNOOP_MEM == TRUE)
311  btif_debug_btsnoop_dump(fd);
312#endif
313
314  close(fd);
315}
316
317static const void* get_profile_interface(const char* profile_id) {
318  LOG_INFO(LOG_TAG, "get_profile_interface %s", profile_id);
319
320  /* sanity check */
321  if (interface_ready() == false) return NULL;
322
323  /* check for supported profile interfaces */
324  if (is_profile(profile_id, BT_PROFILE_HANDSFREE_ID))
325    return btif_hf_get_interface();
326
327  if (is_profile(profile_id, BT_PROFILE_HANDSFREE_CLIENT_ID))
328    return btif_hf_client_get_interface();
329
330  if (is_profile(profile_id, BT_PROFILE_SOCKETS_ID))
331    return btif_sock_get_interface();
332
333  if (is_profile(profile_id, BT_PROFILE_PAN_ID))
334    return btif_pan_get_interface();
335
336  if (is_profile(profile_id, BT_PROFILE_ADVANCED_AUDIO_ID))
337    return btif_av_get_src_interface();
338
339  if (is_profile(profile_id, BT_PROFILE_ADVANCED_AUDIO_SINK_ID))
340    return btif_av_get_sink_interface();
341
342  if (is_profile(profile_id, BT_PROFILE_HIDHOST_ID))
343    return btif_hh_get_interface();
344
345  if (is_profile(profile_id, BT_PROFILE_HEALTH_ID))
346    return btif_hl_get_interface();
347
348  if (is_profile(profile_id, BT_PROFILE_SDP_CLIENT_ID))
349    return btif_sdp_get_interface();
350
351#if (BTA_GATT_INCLUDED == TRUE && BLE_INCLUDED == TRUE)
352  if (is_profile(profile_id, BT_PROFILE_GATT_ID))
353    return btif_gatt_get_interface();
354#endif
355
356  if (is_profile(profile_id, BT_PROFILE_AV_RC_ID))
357    return btif_rc_get_interface();
358
359  if (is_profile(profile_id, BT_PROFILE_AV_RC_CTRL_ID))
360    return btif_rc_ctrl_get_interface();
361
362  return NULL;
363}
364
365int dut_mode_configure(uint8_t enable) {
366  LOG_INFO(LOG_TAG, "dut_mode_configure");
367
368  /* sanity check */
369  if (interface_ready() == false) return BT_STATUS_NOT_READY;
370
371  return btif_dut_mode_configure(enable);
372}
373
374int dut_mode_send(uint16_t opcode, uint8_t* buf, uint8_t len) {
375  LOG_INFO(LOG_TAG, "dut_mode_send");
376
377  /* sanity check */
378  if (interface_ready() == false) return BT_STATUS_NOT_READY;
379
380  return btif_dut_mode_send(opcode, buf, len);
381}
382
383#if (BLE_INCLUDED == TRUE)
384int le_test_mode(uint16_t opcode, uint8_t* buf, uint8_t len) {
385  LOG_INFO(LOG_TAG, "le_test_mode");
386
387  /* sanity check */
388  if (interface_ready() == false) return BT_STATUS_NOT_READY;
389
390  return btif_le_test_mode(opcode, buf, len);
391}
392#endif
393
394int config_hci_snoop_log(uint8_t enable) {
395  LOG_INFO(LOG_TAG, "config_hci_snoop_log");
396
397  if (!interface_ready()) return BT_STATUS_NOT_READY;
398
399  btsnoop_get_interface()->set_api_wants_to_log(enable);
400  return BT_STATUS_SUCCESS;
401}
402
403static int set_os_callouts(bt_os_callouts_t* callouts) {
404  wakelock_set_os_callouts(callouts);
405  return BT_STATUS_SUCCESS;
406}
407
408static int config_clear(void) {
409  LOG_INFO(LOG_TAG, "%s", __func__);
410  return btif_config_clear() ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
411}
412
413static const bt_interface_t bluetoothInterface = {
414    sizeof(bluetoothInterface),
415    init,
416    enable,
417    disable,
418    cleanup,
419    get_adapter_properties,
420    get_adapter_property,
421    set_adapter_property,
422    get_remote_device_properties,
423    get_remote_device_property,
424    set_remote_device_property,
425    get_remote_service_record,
426    get_remote_services,
427    start_discovery,
428    cancel_discovery,
429    create_bond,
430    create_bond_out_of_band,
431    remove_bond,
432    cancel_bond,
433    get_connection_state,
434    pin_reply,
435    ssp_reply,
436    get_profile_interface,
437    dut_mode_configure,
438    dut_mode_send,
439#if (BLE_INCLUDED == TRUE)
440    le_test_mode,
441#else
442    NULL,
443#endif
444    config_hci_snoop_log,
445    set_os_callouts,
446    read_energy_info,
447    dump,
448    config_clear,
449    interop_database_clear,
450    interop_database_add,
451};
452
453const bt_interface_t* bluetooth__get_bluetooth_interface() {
454  /* fixme -- add property to disable bt interface ? */
455
456  return &bluetoothInterface;
457}
458
459static int close_bluetooth_stack(UNUSED_ATTR struct hw_device_t *device) {
460  cleanup();
461  return 0;
462}
463
464static int open_bluetooth_stack(const struct hw_module_t* module,
465                                UNUSED_ATTR char const* name,
466                                struct hw_device_t** abstraction) {
467  static bluetooth_device_t device;
468  device.common.tag = HARDWARE_DEVICE_TAG;
469  device.common.version = 0;
470  device.common.close = close_bluetooth_stack;
471  device.get_bluetooth_interface = bluetooth__get_bluetooth_interface;
472  device.common.module = (struct hw_module_t*)module;
473  *abstraction = (struct hw_device_t*)&device;
474  return 0;
475}
476
477static struct hw_module_methods_t bt_stack_module_methods = {
478    .open = open_bluetooth_stack,
479};
480
481EXPORT_SYMBOL struct hw_module_t HAL_MODULE_INFO_SYM = {
482    .tag = HARDWARE_MODULE_TAG,
483    .version_major = 1,
484    .version_minor = 0,
485    .id = BT_HARDWARE_MODULE_ID,
486    .name = "Bluetooth Stack",
487    .author = "The Android Open Source Project",
488    .methods = &bt_stack_module_methods};
489