bluetooth.c revision 1025687c6679608fe9df918f37011414ffc72b62
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_hf_client.h>
34#include <hardware/bt_av.h>
35#include <hardware/bt_sock.h>
36#include <hardware/bt_hh.h>
37#include <hardware/bt_hl.h>
38#include <hardware/bt_pan.h>
39#include <hardware/bt_gatt.h>
40#include <hardware/bt_rc.h>
41
42#define LOG_NDDEBUG 0
43#define LOG_TAG "bluedroid"
44
45#include "btif_api.h"
46#include "bt_utils.h"
47
48/************************************************************************************
49**  Constants & Macros
50************************************************************************************/
51
52#define is_profile(profile, str) ((strlen(str) == strlen(profile)) && strncmp((const char *)profile, str, strlen(str)) == 0)
53
54/************************************************************************************
55**  Local type definitions
56************************************************************************************/
57
58/************************************************************************************
59**  Static variables
60************************************************************************************/
61
62bt_callbacks_t *bt_hal_cbacks = NULL;
63
64/************************************************************************************
65**  Static functions
66************************************************************************************/
67
68/************************************************************************************
69**  Externs
70************************************************************************************/
71
72/* list all extended interfaces here */
73
74/* handsfree profile */
75extern bthf_interface_t *btif_hf_get_interface();
76/* handsfree profile - client */
77extern bthf_client_interface_t *btif_hf_client_get_interface();
78/* advanced audio profile */
79extern btav_interface_t *btif_av_get_interface();
80/*rfc l2cap*/
81extern btsock_interface_t *btif_sock_get_interface();
82/* hid host profile */
83extern bthh_interface_t *btif_hh_get_interface();
84/* health device profile */
85extern bthl_interface_t *btif_hl_get_interface();
86/*pan*/
87extern btpan_interface_t *btif_pan_get_interface();
88#if BLE_INCLUDED == TRUE
89/* gatt */
90extern btgatt_interface_t *btif_gatt_get_interface();
91#endif
92/* avrc */
93extern btrc_interface_t *btif_rc_get_interface();
94
95/************************************************************************************
96**  Functions
97************************************************************************************/
98
99static uint8_t interface_ready(void)
100{
101    /* add checks here that would prevent API calls other than init to be executed */
102    if (bt_hal_cbacks == NULL)
103        return FALSE;
104
105    return TRUE;
106}
107
108
109/*****************************************************************************
110**
111**   BLUETOOTH HAL INTERFACE FUNCTIONS
112**
113*****************************************************************************/
114
115static int init(bt_callbacks_t* callbacks )
116{
117    ALOGI("init");
118
119    /* sanity check */
120    if (interface_ready() == TRUE)
121        return BT_STATUS_DONE;
122
123    /* store reference to user callbacks */
124    bt_hal_cbacks = callbacks;
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_HANDSFREE_CLIENT_ID))
319        return btif_hf_client_get_interface();
320
321    if (is_profile(profile_id, BT_PROFILE_SOCKETS_ID))
322        return btif_sock_get_interface();
323
324    if (is_profile(profile_id, BT_PROFILE_PAN_ID))
325        return btif_pan_get_interface();
326
327    if (is_profile(profile_id, BT_PROFILE_ADVANCED_AUDIO_ID))
328        return btif_av_get_interface();
329
330    if (is_profile(profile_id, BT_PROFILE_HIDHOST_ID))
331        return btif_hh_get_interface();
332
333    if (is_profile(profile_id, BT_PROFILE_HEALTH_ID))
334        return btif_hl_get_interface();
335
336#if ( BTA_GATT_INCLUDED == TRUE &&BLE_INCLUDED == TRUE)
337    if (is_profile(profile_id, BT_PROFILE_GATT_ID))
338        return btif_gatt_get_interface();
339#endif
340
341    if (is_profile(profile_id, BT_PROFILE_AV_RC_ID))
342        return btif_rc_get_interface();
343
344    return NULL;
345}
346
347int dut_mode_configure(uint8_t enable)
348{
349    ALOGI("dut_mode_configure");
350
351    /* sanity check */
352    if (interface_ready() == FALSE)
353        return BT_STATUS_NOT_READY;
354
355    return btif_dut_mode_configure(enable);
356}
357
358int dut_mode_send(uint16_t opcode, uint8_t* buf, uint8_t len)
359{
360    ALOGI("dut_mode_send");
361
362    /* sanity check */
363    if (interface_ready() == FALSE)
364        return BT_STATUS_NOT_READY;
365
366    return btif_dut_mode_send(opcode, buf, len);
367}
368
369#if BLE_INCLUDED == TRUE
370int le_test_mode(uint16_t opcode, uint8_t* buf, uint8_t len)
371{
372    ALOGI("le_test_mode");
373
374    /* sanity check */
375    if (interface_ready() == FALSE)
376        return BT_STATUS_NOT_READY;
377
378    return btif_le_test_mode(opcode, buf, len);
379}
380#endif
381
382int config_hci_snoop_log(uint8_t enable)
383{
384    ALOGI("config_hci_snoop_log");
385
386    /* sanity check */
387    if (interface_ready() == FALSE)
388        return BT_STATUS_NOT_READY;
389
390    return btif_config_hci_snoop_log(enable);
391}
392
393static const bt_interface_t bluetoothInterface = {
394    sizeof(bluetoothInterface),
395    init,
396    enable,
397    disable,
398    cleanup,
399    get_adapter_properties,
400    get_adapter_property,
401    set_adapter_property,
402    get_remote_device_properties,
403    get_remote_device_property,
404    set_remote_device_property,
405    get_remote_service_record,
406    get_remote_services,
407    start_discovery,
408    cancel_discovery,
409    create_bond,
410    remove_bond,
411    cancel_bond,
412    pin_reply,
413    ssp_reply,
414    get_profile_interface,
415    dut_mode_configure,
416    dut_mode_send,
417#if BLE_INCLUDED == TRUE
418    le_test_mode,
419#else
420    NULL,
421#endif
422    config_hci_snoop_log
423};
424
425const bt_interface_t* bluetooth__get_bluetooth_interface ()
426{
427    /* fixme -- add property to disable bt interface ? */
428
429    return &bluetoothInterface;
430}
431
432static int close_bluetooth_stack(struct hw_device_t* device)
433{
434    UNUSED(device);
435    cleanup();
436    return 0;
437}
438
439static int open_bluetooth_stack (const struct hw_module_t* module, char const* name,
440                                 struct hw_device_t** abstraction)
441{
442    UNUSED(name);
443
444    bluetooth_device_t *stack = malloc(sizeof(bluetooth_device_t) );
445    memset(stack, 0, sizeof(bluetooth_device_t) );
446    stack->common.tag = HARDWARE_DEVICE_TAG;
447    stack->common.version = 0;
448    stack->common.module = (struct hw_module_t*)module;
449    stack->common.close = close_bluetooth_stack;
450    stack->get_bluetooth_interface = bluetooth__get_bluetooth_interface;
451    *abstraction = (struct hw_device_t*)stack;
452    return 0;
453}
454
455
456static struct hw_module_methods_t bt_stack_module_methods = {
457    .open = open_bluetooth_stack,
458};
459
460struct hw_module_t HAL_MODULE_INFO_SYM = {
461    .tag = HARDWARE_MODULE_TAG,
462    .version_major = 1,
463    .version_minor = 0,
464    .id = BT_HARDWARE_MODULE_ID,
465    .name = "Bluetooth Stack",
466    .author = "The Android Open Source Project",
467    .methods = &bt_stack_module_methods
468};
469
470