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