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