OSDN Git Service

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