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