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