bluetooth.cc revision 5b790feeeb211c42bf78ca3ae9c26aa30e516765
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/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()) 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  if (arguments != NULL && arguments[0] != NULL) {
307    if (strncmp(arguments[0], "--proto-bin", 11) == 0) {
308      system_bt_osi::BluetoothMetricsLogger::GetInstance()->WriteBase64(fd,
309                                                                        true);
310      return;
311    }
312  }
313  btif_debug_conn_dump(fd);
314  btif_debug_bond_event_dump(fd);
315  btif_debug_a2dp_dump(fd);
316  btif_debug_config_dump(fd);
317  BTA_HfClientDumpStatistics(fd);
318  wakelock_debug_dump(fd);
319  osi_allocator_debug_dump(fd);
320  alarm_debug_dump(fd);
321#if (BTSNOOP_MEM == TRUE)
322  btif_debug_btsnoop_dump(fd);
323#endif
324
325  close(fd);
326}
327
328static const void* get_profile_interface(const char* profile_id) {
329  LOG_INFO(LOG_TAG, "%s: id = %s", __func__, profile_id);
330
331  /* sanity check */
332  if (!interface_ready()) return NULL;
333
334  /* check for supported profile interfaces */
335  if (is_profile(profile_id, BT_PROFILE_HANDSFREE_ID))
336    return btif_hf_get_interface();
337
338  if (is_profile(profile_id, BT_PROFILE_HANDSFREE_CLIENT_ID))
339    return btif_hf_client_get_interface();
340
341  if (is_profile(profile_id, BT_PROFILE_SOCKETS_ID))
342    return btif_sock_get_interface();
343
344  if (is_profile(profile_id, BT_PROFILE_PAN_ID))
345    return btif_pan_get_interface();
346
347  if (is_profile(profile_id, BT_PROFILE_ADVANCED_AUDIO_ID))
348    return btif_av_get_src_interface();
349
350  if (is_profile(profile_id, BT_PROFILE_ADVANCED_AUDIO_SINK_ID))
351    return btif_av_get_sink_interface();
352
353  if (is_profile(profile_id, BT_PROFILE_HIDHOST_ID))
354    return btif_hh_get_interface();
355
356  if (is_profile(profile_id, BT_PROFILE_HIDDEV_ID))
357    return btif_hd_get_interface();
358
359  if (is_profile(profile_id, BT_PROFILE_HEALTH_ID))
360    return btif_hl_get_interface();
361
362  if (is_profile(profile_id, BT_PROFILE_SDP_CLIENT_ID))
363    return btif_sdp_get_interface();
364
365  if (is_profile(profile_id, BT_PROFILE_GATT_ID))
366    return btif_gatt_get_interface();
367
368  if (is_profile(profile_id, BT_PROFILE_AV_RC_ID))
369    return btif_rc_get_interface();
370
371  if (is_profile(profile_id, BT_PROFILE_AV_RC_CTRL_ID))
372    return btif_rc_ctrl_get_interface();
373
374  if (is_profile(profile_id, BT_TEST_INTERFACE_MCAP_ID))
375    return stack_mcap_get_interface();
376
377  return NULL;
378}
379
380int dut_mode_configure(uint8_t enable) {
381  LOG_INFO(LOG_TAG, "%s", __func__);
382
383  /* sanity check */
384  if (!interface_ready()) return BT_STATUS_NOT_READY;
385
386  return btif_dut_mode_configure(enable);
387}
388
389int dut_mode_send(uint16_t opcode, uint8_t* buf, uint8_t len) {
390  LOG_INFO(LOG_TAG, "%s", __func__);
391
392  /* sanity check */
393  if (!interface_ready()) return BT_STATUS_NOT_READY;
394
395  return btif_dut_mode_send(opcode, buf, len);
396}
397
398int le_test_mode(uint16_t opcode, uint8_t* buf, uint8_t len) {
399  LOG_INFO(LOG_TAG, "%s", __func__);
400
401  /* sanity check */
402  if (!interface_ready()) return BT_STATUS_NOT_READY;
403
404  return btif_le_test_mode(opcode, buf, len);
405}
406
407static int set_os_callouts(bt_os_callouts_t* callouts) {
408  wakelock_set_os_callouts(callouts);
409  return BT_STATUS_SUCCESS;
410}
411
412static int config_clear(void) {
413  LOG_INFO(LOG_TAG, "%s", __func__);
414  return btif_config_clear() ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
415}
416
417EXPORT_SYMBOL bt_interface_t bluetoothInterface = {
418    sizeof(bluetoothInterface),
419    init,
420    enable,
421    disable,
422    cleanup,
423    get_adapter_properties,
424    get_adapter_property,
425    set_adapter_property,
426    get_remote_device_properties,
427    get_remote_device_property,
428    set_remote_device_property,
429    get_remote_service_record,
430    get_remote_services,
431    start_discovery,
432    cancel_discovery,
433    create_bond,
434    create_bond_out_of_band,
435    remove_bond,
436    cancel_bond,
437    get_connection_state,
438    pin_reply,
439    ssp_reply,
440    get_profile_interface,
441    dut_mode_configure,
442    dut_mode_send,
443    le_test_mode,
444    set_os_callouts,
445    read_energy_info,
446    dump,
447    config_clear,
448    interop_database_clear,
449    interop_database_add,
450};
451