bluetooth.cc revision 9af0078f41f489059982614e8558536e100f9a1a
1/******************************************************************************
2 *
3 *  Copyright 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/bluetooth_headset_interface.h>
37#include <hardware/bt_av.h>
38#include <hardware/bt_gatt.h>
39#include <hardware/bt_hd.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_a2dp.h"
52#include "btif_api.h"
53#include "btif_av.h"
54#include "btif_config.h"
55#include "btif_debug.h"
56#include "btif_debug_btsnoop.h"
57#include "btif_debug_conn.h"
58#include "btif_hf.h"
59#include "btif_storage.h"
60#include "btsnoop.h"
61#include "btsnoop_mem.h"
62#include "device/include/interop.h"
63#include "osi/include/alarm.h"
64#include "osi/include/allocation_tracker.h"
65#include "osi/include/log.h"
66#include "osi/include/metrics.h"
67#include "osi/include/osi.h"
68#include "osi/include/wakelock.h"
69#include "stack_manager.h"
70
71/* Test interface includes */
72#include "mca_api.h"
73
74/*******************************************************************************
75 *  Static variables
76 ******************************************************************************/
77
78bt_callbacks_t* bt_hal_cbacks = NULL;
79bool restricted_mode = false;
80
81/*******************************************************************************
82 *  Externs
83 ******************************************************************************/
84
85/* list all extended interfaces here */
86
87/* handsfree profile - client */
88extern const bthf_client_interface_t* btif_hf_client_get_interface();
89/* advanced audio profile */
90extern const btav_source_interface_t* btif_av_get_src_interface();
91extern const btav_sink_interface_t* btif_av_get_sink_interface();
92/*rfc l2cap*/
93extern const btsock_interface_t* btif_sock_get_interface();
94/* hid host profile */
95extern const bthh_interface_t* btif_hh_get_interface();
96/* hid device profile */
97extern const bthd_interface_t* btif_hd_get_interface();
98/* health device profile */
99extern const bthl_interface_t* btif_hl_get_interface();
100/*pan*/
101extern const btpan_interface_t* btif_pan_get_interface();
102/*map client*/
103extern const btmce_interface_t* btif_mce_get_interface();
104/* gatt */
105extern const btgatt_interface_t* btif_gatt_get_interface();
106/* avrc target */
107extern const btrc_interface_t* btif_rc_get_interface();
108/* avrc controller */
109extern const btrc_ctrl_interface_t* btif_rc_ctrl_get_interface();
110/*SDP search client*/
111extern const btsdp_interface_t* btif_sdp_get_interface();
112
113/* List all test interface here */
114extern const 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()) 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()) 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()) return BT_STATUS_NOT_READY;
188
189  return btif_set_adapter_property(property);
190}
191
192int get_remote_device_properties(RawAddress* remote_addr) {
193  /* sanity check */
194  if (!interface_ready()) return BT_STATUS_NOT_READY;
195
196  return btif_get_remote_device_properties(remote_addr);
197}
198
199int get_remote_device_property(RawAddress* remote_addr,
200                               bt_property_type_t type) {
201  /* sanity check */
202  if (!interface_ready()) return BT_STATUS_NOT_READY;
203
204  return btif_get_remote_device_property(remote_addr, type);
205}
206
207int set_remote_device_property(RawAddress* remote_addr,
208                               const bt_property_t* property) {
209  /* sanity check */
210  if (!interface_ready()) return BT_STATUS_NOT_READY;
211
212  return btif_set_remote_device_property(remote_addr, property);
213}
214
215int get_remote_service_record(const RawAddress& remote_addr,
216                              const bluetooth::Uuid& uuid) {
217  /* sanity check */
218  if (!interface_ready()) return BT_STATUS_NOT_READY;
219
220  return btif_get_remote_service_record(remote_addr, uuid);
221}
222
223int get_remote_services(RawAddress* remote_addr) {
224  /* sanity check */
225  if (!interface_ready()) return BT_STATUS_NOT_READY;
226
227  return btif_dm_get_remote_services(*remote_addr);
228}
229
230static int start_discovery(void) {
231  /* sanity check */
232  if (!interface_ready()) return BT_STATUS_NOT_READY;
233
234  return btif_dm_start_discovery();
235}
236
237static int cancel_discovery(void) {
238  /* sanity check */
239  if (!interface_ready()) return BT_STATUS_NOT_READY;
240
241  return btif_dm_cancel_discovery();
242}
243
244static int create_bond(const RawAddress* bd_addr, int transport) {
245  /* sanity check */
246  if (!interface_ready()) return BT_STATUS_NOT_READY;
247
248  return btif_dm_create_bond(bd_addr, transport);
249}
250
251static int create_bond_out_of_band(const RawAddress* bd_addr, int transport,
252                                   const bt_out_of_band_data_t* oob_data) {
253  /* sanity check */
254  if (!interface_ready()) return BT_STATUS_NOT_READY;
255
256  return btif_dm_create_bond_out_of_band(bd_addr, transport, oob_data);
257}
258
259static int cancel_bond(const RawAddress* bd_addr) {
260  /* sanity check */
261  if (!interface_ready()) return BT_STATUS_NOT_READY;
262
263  return btif_dm_cancel_bond(bd_addr);
264}
265
266static int remove_bond(const RawAddress* bd_addr) {
267  if (is_restricted_mode() && !btif_storage_is_restricted_device(bd_addr))
268    return BT_STATUS_SUCCESS;
269
270  /* sanity check */
271  if (!interface_ready()) return BT_STATUS_NOT_READY;
272
273  return btif_dm_remove_bond(bd_addr);
274}
275
276static int get_connection_state(const RawAddress* bd_addr) {
277  /* sanity check */
278  if (!interface_ready()) return 0;
279
280  return btif_dm_get_connection_state(bd_addr);
281}
282
283static int pin_reply(const RawAddress* bd_addr, uint8_t accept, uint8_t pin_len,
284                     bt_pin_code_t* pin_code) {
285  /* sanity check */
286  if (!interface_ready()) return BT_STATUS_NOT_READY;
287
288  return btif_dm_pin_reply(bd_addr, accept, pin_len, pin_code);
289}
290
291static int ssp_reply(const RawAddress* bd_addr, bt_ssp_variant_t variant,
292                     uint8_t accept, uint32_t passkey) {
293  /* sanity check */
294  if (!interface_ready()) return BT_STATUS_NOT_READY;
295
296  return btif_dm_ssp_reply(bd_addr, variant, accept, passkey);
297}
298
299static int read_energy_info() {
300  if (!interface_ready()) return BT_STATUS_NOT_READY;
301  btif_dm_read_energy_info();
302  return BT_STATUS_SUCCESS;
303}
304
305static void dump(int fd, const char** arguments) {
306  btif_debug_conn_dump(fd);
307  btif_debug_bond_event_dump(fd);
308  btif_debug_a2dp_dump(fd);
309  btif_debug_av_dump(fd);
310  bta_debug_av_dump(fd);
311  stack_debug_avdtp_api_dump(fd);
312  btif_debug_config_dump(fd);
313  BTA_HfClientDumpStatistics(fd);
314  wakelock_debug_dump(fd);
315  osi_allocator_debug_dump(fd);
316  alarm_debug_dump(fd);
317#if (BTSNOOP_MEM == TRUE)
318  btif_debug_btsnoop_dump(fd);
319#endif
320
321  close(fd);
322}
323
324static void dumpMetrics(std::string* output) {
325  system_bt_osi::BluetoothMetricsLogger::GetInstance()->WriteString(output,
326                                                                    true);
327}
328
329static const void* get_profile_interface(const char* profile_id) {
330  LOG_INFO(LOG_TAG, "%s: id = %s", __func__, profile_id);
331
332  /* sanity check */
333  if (!interface_ready()) return NULL;
334
335  /* check for supported profile interfaces */
336  if (is_profile(profile_id, BT_PROFILE_HANDSFREE_ID))
337    return bluetooth::headset::GetInterface();
338
339  if (is_profile(profile_id, BT_PROFILE_HANDSFREE_CLIENT_ID))
340    return btif_hf_client_get_interface();
341
342  if (is_profile(profile_id, BT_PROFILE_SOCKETS_ID))
343    return btif_sock_get_interface();
344
345  if (is_profile(profile_id, BT_PROFILE_PAN_ID))
346    return btif_pan_get_interface();
347
348  if (is_profile(profile_id, BT_PROFILE_ADVANCED_AUDIO_ID))
349    return btif_av_get_src_interface();
350
351  if (is_profile(profile_id, BT_PROFILE_ADVANCED_AUDIO_SINK_ID))
352    return btif_av_get_sink_interface();
353
354  if (is_profile(profile_id, BT_PROFILE_HIDHOST_ID))
355    return btif_hh_get_interface();
356
357  if (is_profile(profile_id, BT_PROFILE_HIDDEV_ID))
358    return btif_hd_get_interface();
359
360  if (is_profile(profile_id, BT_PROFILE_HEALTH_ID))
361    return btif_hl_get_interface();
362
363  if (is_profile(profile_id, BT_PROFILE_SDP_CLIENT_ID))
364    return btif_sdp_get_interface();
365
366  if (is_profile(profile_id, BT_PROFILE_GATT_ID))
367    return btif_gatt_get_interface();
368
369  if (is_profile(profile_id, BT_PROFILE_AV_RC_ID))
370    return btif_rc_get_interface();
371
372  if (is_profile(profile_id, BT_PROFILE_AV_RC_CTRL_ID))
373    return btif_rc_ctrl_get_interface();
374
375  if (is_profile(profile_id, BT_TEST_INTERFACE_MCAP_ID))
376    return stack_mcap_get_interface();
377
378  return NULL;
379}
380
381int dut_mode_configure(uint8_t enable) {
382  LOG_INFO(LOG_TAG, "%s", __func__);
383
384  /* sanity check */
385  if (!interface_ready()) return BT_STATUS_NOT_READY;
386
387  return btif_dut_mode_configure(enable);
388}
389
390int dut_mode_send(uint16_t opcode, uint8_t* buf, uint8_t len) {
391  LOG_INFO(LOG_TAG, "%s", __func__);
392
393  /* sanity check */
394  if (!interface_ready()) return BT_STATUS_NOT_READY;
395
396  return btif_dut_mode_send(opcode, buf, len);
397}
398
399int le_test_mode(uint16_t opcode, uint8_t* buf, uint8_t len) {
400  LOG_INFO(LOG_TAG, "%s", __func__);
401
402  /* sanity check */
403  if (!interface_ready()) return BT_STATUS_NOT_READY;
404
405  return btif_le_test_mode(opcode, buf, len);
406}
407
408static int set_os_callouts(bt_os_callouts_t* callouts) {
409  wakelock_set_os_callouts(callouts);
410  return BT_STATUS_SUCCESS;
411}
412
413static int config_clear(void) {
414  LOG_INFO(LOG_TAG, "%s", __func__);
415  return btif_config_clear() ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
416}
417
418EXPORT_SYMBOL bt_interface_t bluetoothInterface = {
419    sizeof(bluetoothInterface),
420    init,
421    enable,
422    disable,
423    cleanup,
424    get_adapter_properties,
425    get_adapter_property,
426    set_adapter_property,
427    get_remote_device_properties,
428    get_remote_device_property,
429    set_remote_device_property,
430    get_remote_service_record,
431    get_remote_services,
432    start_discovery,
433    cancel_discovery,
434    create_bond,
435    create_bond_out_of_band,
436    remove_bond,
437    cancel_bond,
438    get_connection_state,
439    pin_reply,
440    ssp_reply,
441    get_profile_interface,
442    dut_mode_configure,
443    dut_mode_send,
444    le_test_mode,
445    set_os_callouts,
446    read_energy_info,
447    dump,
448    dumpMetrics,
449    config_clear,
450    interop_database_clear,
451    interop_database_add,
452};
453