OSDN Git Service

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