bluetooth.c revision 8fe58875ce67c6e1099e7ba2339dcd2b979491b0
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#include <stdio.h>
28#include <stdlib.h>
29#include <unistd.h>
30
31#include <hardware/bluetooth.h>
32#include <hardware/bt_hf.h>
33#include <hardware/bt_av.h>
34#include <hardware/bt_sock.h>
35#include <hardware/bt_hh.h>
36#include <hardware/bt_hl.h>
37#include <hardware/bt_pan.h>
38#include <hardware/bt_gatt.h>
39#include <hardware/bt_rc.h>
40
41#define LOG_NDDEBUG 0
42#define LOG_TAG "bluedroid"
43
44#include "btif_api.h"
45#include "bt_utils.h"
46
47/************************************************************************************
48**  Constants & Macros
49************************************************************************************/
50
51#define is_profile(profile, str) ((strlen(str) == strlen(profile)) && strncmp((const char *)profile, str, strlen(str)) == 0)
52
53/************************************************************************************
54**  Local type definitions
55************************************************************************************/
56
57/************************************************************************************
58**  Static variables
59************************************************************************************/
60
61bt_callbacks_t *bt_hal_cbacks = NULL;
62
63/************************************************************************************
64**  Static functions
65************************************************************************************/
66
67/************************************************************************************
68**  Externs
69************************************************************************************/
70
71/* list all extended interfaces here */
72
73/* handsfree profile */
74extern bthf_interface_t *btif_hf_get_interface();
75/* advanced audio profile */
76extern btav_interface_t *btif_av_get_interface();
77/*rfc l2cap*/
78extern btsock_interface_t *btif_sock_get_interface();
79/* hid host profile */
80extern bthh_interface_t *btif_hh_get_interface();
81/* health device profile */
82extern bthl_interface_t *btif_hl_get_interface();
83/*pan*/
84extern btpan_interface_t *btif_pan_get_interface();
85#if BLE_INCLUDED == TRUE
86/* gatt */
87extern btgatt_interface_t *btif_gatt_get_interface();
88#endif
89/* avrc */
90extern btrc_interface_t *btif_rc_get_interface();
91
92/************************************************************************************
93**  Functions
94************************************************************************************/
95
96static uint8_t interface_ready(void)
97{
98    /* add checks here that would prevent API calls other than init to be executed */
99    if (bt_hal_cbacks == NULL)
100        return FALSE;
101
102    return TRUE;
103}
104
105
106/*****************************************************************************
107**
108**   BLUETOOTH HAL INTERFACE FUNCTIONS
109**
110*****************************************************************************/
111
112static int init(bt_callbacks_t* callbacks )
113{
114    ALOGI("init");
115
116    /* sanity check */
117    if (interface_ready() == TRUE)
118        return BT_STATUS_DONE;
119
120    /* store reference to user callbacks */
121    bt_hal_cbacks = callbacks;
122
123    /* add checks for individual callbacks ? */
124
125    bt_utils_init();
126
127    /* init btif */
128    btif_init_bluetooth();
129
130    return BT_STATUS_SUCCESS;
131}
132
133static int enable( void )
134{
135    ALOGI("enable");
136
137    /* sanity check */
138    if (interface_ready() == FALSE)
139        return BT_STATUS_NOT_READY;
140
141    return btif_enable_bluetooth();
142}
143
144static int disable(void)
145{
146    /* sanity check */
147    if (interface_ready() == FALSE)
148        return BT_STATUS_NOT_READY;
149
150    return btif_disable_bluetooth();
151}
152
153static void cleanup( void )
154{
155    /* sanity check */
156    if (interface_ready() == FALSE)
157        return;
158
159    btif_shutdown_bluetooth();
160
161    /* hal callbacks reset upon shutdown complete callback */
162
163    return;
164}
165
166static int get_adapter_properties(void)
167{
168    /* sanity check */
169    if (interface_ready() == FALSE)
170        return BT_STATUS_NOT_READY;
171
172    return btif_get_adapter_properties();
173}
174
175static int get_adapter_property(bt_property_type_t type)
176{
177    /* sanity check */
178    if (interface_ready() == FALSE)
179        return BT_STATUS_NOT_READY;
180
181    return btif_get_adapter_property(type);
182}
183
184static int set_adapter_property(const bt_property_t *property)
185{
186    /* sanity check */
187    if (interface_ready() == FALSE)
188        return BT_STATUS_NOT_READY;
189
190    return btif_set_adapter_property(property);
191}
192
193int get_remote_device_properties(bt_bdaddr_t *remote_addr)
194{
195    /* sanity check */
196    if (interface_ready() == FALSE)
197        return BT_STATUS_NOT_READY;
198
199    return btif_get_remote_device_properties(remote_addr);
200}
201
202int get_remote_device_property(bt_bdaddr_t *remote_addr, bt_property_type_t type)
203{
204    /* sanity check */
205    if (interface_ready() == FALSE)
206        return BT_STATUS_NOT_READY;
207
208    return btif_get_remote_device_property(remote_addr, type);
209}
210
211int set_remote_device_property(bt_bdaddr_t *remote_addr, const bt_property_t *property)
212{
213    /* sanity check */
214    if (interface_ready() == FALSE)
215        return BT_STATUS_NOT_READY;
216
217    return btif_set_remote_device_property(remote_addr, property);
218}
219
220int get_remote_service_record(bt_bdaddr_t *remote_addr, bt_uuid_t *uuid)
221{
222    /* sanity check */
223    if (interface_ready() == FALSE)
224        return BT_STATUS_NOT_READY;
225
226    return btif_get_remote_service_record(remote_addr, uuid);
227}
228
229int get_remote_services(bt_bdaddr_t *remote_addr)
230{
231    /* sanity check */
232    if (interface_ready() == FALSE)
233        return BT_STATUS_NOT_READY;
234
235    return btif_dm_get_remote_services(remote_addr);
236}
237
238static int start_discovery(void)
239{
240    /* sanity check */
241    if (interface_ready() == FALSE)
242        return BT_STATUS_NOT_READY;
243
244    return btif_dm_start_discovery();
245}
246
247static int cancel_discovery(void)
248{
249    /* sanity check */
250    if (interface_ready() == FALSE)
251        return BT_STATUS_NOT_READY;
252
253    return btif_dm_cancel_discovery();
254}
255
256static int create_bond(const bt_bdaddr_t *bd_addr)
257{
258    /* sanity check */
259    if (interface_ready() == FALSE)
260        return BT_STATUS_NOT_READY;
261
262    return btif_dm_create_bond(bd_addr);
263}
264
265static int cancel_bond(const bt_bdaddr_t *bd_addr)
266{
267    /* sanity check */
268    if (interface_ready() == FALSE)
269        return BT_STATUS_NOT_READY;
270
271    return btif_dm_cancel_bond(bd_addr);
272}
273
274static int remove_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_remove_bond(bd_addr);
281}
282
283static int pin_reply(const bt_bdaddr_t *bd_addr, uint8_t accept,
284                 uint8_t pin_len, bt_pin_code_t *pin_code)
285{
286    /* sanity check */
287    if (interface_ready() == FALSE)
288        return BT_STATUS_NOT_READY;
289
290    return btif_dm_pin_reply(bd_addr, accept, pin_len, pin_code);
291}
292
293static int ssp_reply(const bt_bdaddr_t *bd_addr, bt_ssp_variant_t variant,
294                       uint8_t accept, uint32_t passkey)
295{
296    /* sanity check */
297    if (interface_ready() == FALSE)
298        return BT_STATUS_NOT_READY;
299
300    return btif_dm_ssp_reply(bd_addr, variant, accept, passkey);
301}
302
303static const void* get_profile_interface (const char *profile_id)
304{
305    ALOGI("get_profile_interface %s", profile_id);
306
307    /* sanity check */
308    if (interface_ready() == FALSE)
309        return NULL;
310
311    /* check for supported profile interfaces */
312    if (is_profile(profile_id, BT_PROFILE_HANDSFREE_ID))
313        return btif_hf_get_interface();
314
315    if (is_profile(profile_id, BT_PROFILE_SOCKETS_ID))
316        return btif_sock_get_interface();
317
318    if (is_profile(profile_id, BT_PROFILE_PAN_ID))
319        return btif_pan_get_interface();
320
321    if (is_profile(profile_id, BT_PROFILE_ADVANCED_AUDIO_ID))
322        return btif_av_get_interface();
323
324    if (is_profile(profile_id, BT_PROFILE_HIDHOST_ID))
325        return btif_hh_get_interface();
326
327    if (is_profile(profile_id, BT_PROFILE_HEALTH_ID))
328        return btif_hl_get_interface();
329
330#if ( BTA_GATT_INCLUDED == TRUE &&BLE_INCLUDED == TRUE)
331    if (is_profile(profile_id, BT_PROFILE_GATT_ID))
332        return btif_gatt_get_interface();
333#endif
334
335    if (is_profile(profile_id, BT_PROFILE_AV_RC_ID))
336        return btif_rc_get_interface();
337
338    return NULL;
339}
340
341int dut_mode_configure(uint8_t enable)
342{
343    ALOGI("dut_mode_configure");
344
345    /* sanity check */
346    if (interface_ready() == FALSE)
347        return BT_STATUS_NOT_READY;
348
349    return btif_dut_mode_configure(enable);
350}
351
352int dut_mode_send(uint16_t opcode, uint8_t* buf, uint8_t len)
353{
354    ALOGI("dut_mode_send");
355
356    /* sanity check */
357    if (interface_ready() == FALSE)
358        return BT_STATUS_NOT_READY;
359
360    return btif_dut_mode_send(opcode, buf, len);
361}
362
363#if BLE_INCLUDED == TRUE
364int le_test_mode(uint16_t opcode, uint8_t* buf, uint8_t len)
365{
366    ALOGI("le_test_mode");
367
368    /* sanity check */
369    if (interface_ready() == FALSE)
370        return BT_STATUS_NOT_READY;
371
372    return btif_le_test_mode(opcode, buf, len);
373}
374#endif
375
376int config_hci_snoop_log(uint8_t enable)
377{
378    ALOGI("config_hci_snoop_log");
379
380    /* sanity check */
381    if (interface_ready() == FALSE)
382        return BT_STATUS_NOT_READY;
383
384    return btif_config_hci_snoop_log(enable);
385}
386
387static const bt_interface_t bluetoothInterface = {
388    sizeof(bluetoothInterface),
389    init,
390    enable,
391    disable,
392    cleanup,
393    get_adapter_properties,
394    get_adapter_property,
395    set_adapter_property,
396    get_remote_device_properties,
397    get_remote_device_property,
398    set_remote_device_property,
399    get_remote_service_record,
400    get_remote_services,
401    start_discovery,
402    cancel_discovery,
403    create_bond,
404    remove_bond,
405    cancel_bond,
406    pin_reply,
407    ssp_reply,
408    get_profile_interface,
409    dut_mode_configure,
410    dut_mode_send,
411#if BLE_INCLUDED == TRUE
412    le_test_mode,
413#else
414    NULL,
415#endif
416    config_hci_snoop_log
417};
418
419const bt_interface_t* bluetooth__get_bluetooth_interface ()
420{
421    /* fixme -- add property to disable bt interface ? */
422
423    return &bluetoothInterface;
424}
425
426static int close_bluetooth_stack(struct hw_device_t* device)
427{
428    UNUSED(device);
429    cleanup();
430    return 0;
431}
432
433static int open_bluetooth_stack (const struct hw_module_t* module, char const* name,
434                                 struct hw_device_t** abstraction)
435{
436    UNUSED(name);
437
438    bluetooth_device_t *stack = malloc(sizeof(bluetooth_device_t) );
439    memset(stack, 0, sizeof(bluetooth_device_t) );
440    stack->common.tag = HARDWARE_DEVICE_TAG;
441    stack->common.version = 0;
442    stack->common.module = (struct hw_module_t*)module;
443    stack->common.close = close_bluetooth_stack;
444    stack->get_bluetooth_interface = bluetooth__get_bluetooth_interface;
445    *abstraction = (struct hw_device_t*)stack;
446    return 0;
447}
448
449
450static struct hw_module_methods_t bt_stack_module_methods = {
451    .open = open_bluetooth_stack,
452};
453
454struct hw_module_t HAL_MODULE_INFO_SYM = {
455    .tag = HARDWARE_MODULE_TAG,
456    .version_major = 1,
457    .version_minor = 0,
458    .id = BT_HARDWARE_MODULE_ID,
459    .name = "Bluetooth Stack",
460    .author = "The Android Open Source Project",
461    .methods = &bt_stack_module_methods
462};
463
464