OSDN Git Service

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