• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <string.h>
30 #include <unistd.h>
31 
32 #include <hardware/bluetooth.h>
33 #include <hardware/bt_hf.h>
34 #include <hardware/bt_hf_client.h>
35 #include <hardware/bt_av.h>
36 #include <hardware/bt_sock.h>
37 #include <hardware/bt_hh.h>
38 #include <hardware/bt_hl.h>
39 #include <hardware/bt_pan.h>
40 #include <hardware/bt_mce.h>
41 #include <hardware/bt_gatt.h>
42 #include <hardware/bt_rc.h>
43 #include <hardware/bt_sdp.h>
44 
45 #define LOG_NDDEBUG 0
46 #define LOG_TAG "bt_bluedroid"
47 
48 #include "btif_api.h"
49 #include "btif_debug.h"
50 #include "btsnoop.h"
51 #include "btsnoop_mem.h"
52 #include "bt_utils.h"
53 #include "device/include/interop.h"
54 #include "osi/include/allocation_tracker.h"
55 #include "osi/include/log.h"
56 #include "osi/include/osi.h"
57 #include "stack_manager.h"
58 #include "btif_config.h"
59 #include "btif_storage.h"
60 
61 /************************************************************************************
62 **  Constants & Macros
63 ************************************************************************************/
64 
65 #define is_profile(profile, str) ((strlen(str) == strlen(profile)) && strncmp((const char *)profile, str, strlen(str)) == 0)
66 
67 /************************************************************************************
68 **  Static variables
69 ************************************************************************************/
70 
71 bt_callbacks_t *bt_hal_cbacks = NULL;
72 bool restricted_mode = FALSE;
73 
74 /** Operating System specific callouts for resource management */
75 bt_os_callouts_t *bt_os_callouts = NULL;
76 
77 /************************************************************************************
78 **  Externs
79 ************************************************************************************/
80 
81 /* list all extended interfaces here */
82 
83 /* handsfree profile */
84 extern bthf_interface_t *btif_hf_get_interface();
85 /* handsfree profile - client */
86 extern bthf_client_interface_t *btif_hf_client_get_interface();
87 /* advanced audio profile */
88 extern btav_interface_t *btif_av_get_src_interface();
89 extern btav_interface_t *btif_av_get_sink_interface();
90 /*rfc l2cap*/
91 extern btsock_interface_t *btif_sock_get_interface();
92 /* hid host profile */
93 extern bthh_interface_t *btif_hh_get_interface();
94 /* health device profile */
95 extern bthl_interface_t *btif_hl_get_interface();
96 /*pan*/
97 extern btpan_interface_t *btif_pan_get_interface();
98 /*map client*/
99 extern btmce_interface_t *btif_mce_get_interface();
100 #if BLE_INCLUDED == TRUE
101 /* gatt */
102 extern btgatt_interface_t *btif_gatt_get_interface();
103 #endif
104 /* avrc target */
105 extern btrc_interface_t *btif_rc_get_interface();
106 /* avrc controller */
107 extern btrc_interface_t *btif_rc_ctrl_get_interface();
108 /*SDP search client*/
109 extern btsdp_interface_t *btif_sdp_get_interface();
110 
111 /************************************************************************************
112 **  Functions
113 ************************************************************************************/
114 
interface_ready(void)115 static bool interface_ready(void) {
116   return bt_hal_cbacks != NULL;
117 }
118 
119 /*****************************************************************************
120 **
121 **   BLUETOOTH HAL INTERFACE FUNCTIONS
122 **
123 *****************************************************************************/
124 
init(bt_callbacks_t * callbacks)125 static int init(bt_callbacks_t *callbacks) {
126   LOG_INFO("%s", __func__);
127 
128   if (interface_ready())
129     return BT_STATUS_DONE;
130 
131 #ifdef BLUEDROID_DEBUG
132   allocation_tracker_init();
133 #endif
134 
135   bt_hal_cbacks = callbacks;
136   stack_manager_get_interface()->init_stack();
137   btif_debug_init();
138   return BT_STATUS_SUCCESS;
139 }
140 
enable(bool start_restricted)141 static int enable(bool start_restricted) {
142   LOG_INFO(LOG_TAG, "%s: start restricted = %d", __func__, start_restricted);
143 
144   restricted_mode = start_restricted;
145 
146   if (!interface_ready())
147     return BT_STATUS_NOT_READY;
148 
149   stack_manager_get_interface()->start_up_stack_async();
150   return BT_STATUS_SUCCESS;
151 }
152 
disable(void)153 static int disable(void) {
154   if (!interface_ready())
155     return BT_STATUS_NOT_READY;
156 
157   stack_manager_get_interface()->shut_down_stack_async();
158   return BT_STATUS_SUCCESS;
159 }
160 
cleanup(void)161 static void cleanup(void) {
162   stack_manager_get_interface()->clean_up_stack_async();
163 }
164 
is_restricted_mode()165 bool is_restricted_mode() {
166   return restricted_mode;
167 }
168 
get_adapter_properties(void)169 static 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 
get_adapter_property(bt_property_type_t type)178 static 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 
set_adapter_property(const bt_property_t * property)187 static 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 
get_remote_device_properties(bt_bdaddr_t * remote_addr)196 int 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 
get_remote_device_property(bt_bdaddr_t * remote_addr,bt_property_type_t type)205 int 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 
set_remote_device_property(bt_bdaddr_t * remote_addr,const bt_property_t * property)214 int 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 
get_remote_service_record(bt_bdaddr_t * remote_addr,bt_uuid_t * uuid)223 int 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 
get_remote_services(bt_bdaddr_t * remote_addr)232 int 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 
start_discovery(void)241 static 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 
cancel_discovery(void)250 static 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 
create_bond(const bt_bdaddr_t * bd_addr,int transport)259 static int create_bond(const bt_bdaddr_t *bd_addr, int transport)
260 {
261     /* sanity check */
262     if (interface_ready() == FALSE)
263         return BT_STATUS_NOT_READY;
264 
265     return btif_dm_create_bond(bd_addr, transport);
266 }
267 
cancel_bond(const bt_bdaddr_t * bd_addr)268 static 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 
remove_bond(const bt_bdaddr_t * bd_addr)277 static int remove_bond(const bt_bdaddr_t *bd_addr)
278 {
279     if (is_restricted_mode() && !btif_storage_is_restricted_device(bd_addr))
280         return BT_STATUS_SUCCESS;
281 
282     /* sanity check */
283     if (interface_ready() == FALSE)
284         return BT_STATUS_NOT_READY;
285 
286     return btif_dm_remove_bond(bd_addr);
287 }
288 
get_connection_state(const bt_bdaddr_t * bd_addr)289 static int get_connection_state(const bt_bdaddr_t *bd_addr)
290 {
291     /* sanity check */
292     if (interface_ready() == FALSE)
293         return 0;
294 
295     return btif_dm_get_connection_state(bd_addr);
296 }
297 
pin_reply(const bt_bdaddr_t * bd_addr,uint8_t accept,uint8_t pin_len,bt_pin_code_t * pin_code)298 static int pin_reply(const bt_bdaddr_t *bd_addr, uint8_t accept,
299                  uint8_t pin_len, bt_pin_code_t *pin_code)
300 {
301     /* sanity check */
302     if (interface_ready() == FALSE)
303         return BT_STATUS_NOT_READY;
304 
305     return btif_dm_pin_reply(bd_addr, accept, pin_len, pin_code);
306 }
307 
ssp_reply(const bt_bdaddr_t * bd_addr,bt_ssp_variant_t variant,uint8_t accept,uint32_t passkey)308 static int ssp_reply(const bt_bdaddr_t *bd_addr, bt_ssp_variant_t variant,
309                        uint8_t accept, uint32_t passkey)
310 {
311     /* sanity check */
312     if (interface_ready() == FALSE)
313         return BT_STATUS_NOT_READY;
314 
315     return btif_dm_ssp_reply(bd_addr, variant, accept, passkey);
316 }
317 
read_energy_info()318 static int read_energy_info()
319 {
320     if (interface_ready() == FALSE)
321         return BT_STATUS_NOT_READY;
322     btif_dm_read_energy_info();
323     return BT_STATUS_SUCCESS;
324 }
325 
dump(int fd)326 static void dump(int fd)
327 {
328     btif_debug_dump(fd);
329 }
330 
get_profile_interface(const char * profile_id)331 static const void* get_profile_interface (const char *profile_id)
332 {
333     LOG_INFO("get_profile_interface %s", profile_id);
334 
335     /* sanity check */
336     if (interface_ready() == FALSE)
337         return NULL;
338 
339     /* check for supported profile interfaces */
340     if (is_profile(profile_id, BT_PROFILE_HANDSFREE_ID))
341         return btif_hf_get_interface();
342 
343     if (is_profile(profile_id, BT_PROFILE_HANDSFREE_CLIENT_ID))
344         return btif_hf_client_get_interface();
345 
346     if (is_profile(profile_id, BT_PROFILE_SOCKETS_ID))
347         return btif_sock_get_interface();
348 
349     if (is_profile(profile_id, BT_PROFILE_PAN_ID))
350         return btif_pan_get_interface();
351 
352     if (is_profile(profile_id, BT_PROFILE_ADVANCED_AUDIO_ID))
353         return btif_av_get_src_interface();
354 
355     if (is_profile(profile_id, BT_PROFILE_ADVANCED_AUDIO_SINK_ID))
356         return btif_av_get_sink_interface();
357 
358     if (is_profile(profile_id, BT_PROFILE_HIDHOST_ID))
359         return btif_hh_get_interface();
360 
361     if (is_profile(profile_id, BT_PROFILE_HEALTH_ID))
362         return btif_hl_get_interface();
363 
364     if (is_profile(profile_id, BT_PROFILE_SDP_CLIENT_ID))
365         return btif_sdp_get_interface();
366 
367 #if ( BTA_GATT_INCLUDED == TRUE && BLE_INCLUDED == TRUE)
368     if (is_profile(profile_id, BT_PROFILE_GATT_ID))
369         return btif_gatt_get_interface();
370 #endif
371 
372     if (is_profile(profile_id, BT_PROFILE_AV_RC_ID))
373         return btif_rc_get_interface();
374 
375     if (is_profile(profile_id, BT_PROFILE_AV_RC_CTRL_ID))
376         return btif_rc_ctrl_get_interface();
377 
378     return NULL;
379 }
380 
dut_mode_configure(uint8_t enable)381 int dut_mode_configure(uint8_t enable)
382 {
383     LOG_INFO("dut_mode_configure");
384 
385     /* sanity check */
386     if (interface_ready() == FALSE)
387         return BT_STATUS_NOT_READY;
388 
389     return btif_dut_mode_configure(enable);
390 }
391 
dut_mode_send(uint16_t opcode,uint8_t * buf,uint8_t len)392 int dut_mode_send(uint16_t opcode, uint8_t* buf, uint8_t len)
393 {
394     LOG_INFO("dut_mode_send");
395 
396     /* sanity check */
397     if (interface_ready() == FALSE)
398         return BT_STATUS_NOT_READY;
399 
400     return btif_dut_mode_send(opcode, buf, len);
401 }
402 
403 #if BLE_INCLUDED == TRUE
le_test_mode(uint16_t opcode,uint8_t * buf,uint8_t len)404 int le_test_mode(uint16_t opcode, uint8_t* buf, uint8_t len)
405 {
406     LOG_INFO("le_test_mode");
407 
408     /* sanity check */
409     if (interface_ready() == FALSE)
410         return BT_STATUS_NOT_READY;
411 
412     return btif_le_test_mode(opcode, buf, len);
413 }
414 #endif
415 
config_hci_snoop_log(uint8_t enable)416 int config_hci_snoop_log(uint8_t enable)
417 {
418     LOG_INFO("config_hci_snoop_log");
419 
420     if (!interface_ready())
421         return BT_STATUS_NOT_READY;
422 
423     btsnoop_get_interface()->set_api_wants_to_log(enable);
424     return BT_STATUS_SUCCESS;
425 }
426 
set_os_callouts(bt_os_callouts_t * callouts)427 static int set_os_callouts(bt_os_callouts_t *callouts) {
428     bt_os_callouts = callouts;
429     return BT_STATUS_SUCCESS;
430 }
431 
config_clear(void)432 static int config_clear(void) {
433     LOG_INFO("%s", __func__);
434     return btif_config_clear();
435 }
436 
437 static const bt_interface_t bluetoothInterface = {
438     sizeof(bluetoothInterface),
439     init,
440     enable,
441     disable,
442     cleanup,
443     get_adapter_properties,
444     get_adapter_property,
445     set_adapter_property,
446     get_remote_device_properties,
447     get_remote_device_property,
448     set_remote_device_property,
449     get_remote_service_record,
450     get_remote_services,
451     start_discovery,
452     cancel_discovery,
453     create_bond,
454     remove_bond,
455     cancel_bond,
456     get_connection_state,
457     pin_reply,
458     ssp_reply,
459     get_profile_interface,
460     dut_mode_configure,
461     dut_mode_send,
462 #if BLE_INCLUDED == TRUE
463     le_test_mode,
464 #else
465     NULL,
466 #endif
467     config_hci_snoop_log,
468     set_os_callouts,
469     read_energy_info,
470     dump,
471     config_clear,
472     interop_database_clear,
473     interop_database_add,
474 };
475 
bluetooth__get_bluetooth_interface()476 const bt_interface_t* bluetooth__get_bluetooth_interface ()
477 {
478     /* fixme -- add property to disable bt interface ? */
479 
480     return &bluetoothInterface;
481 }
482 
close_bluetooth_stack(struct hw_device_t * device)483 static int close_bluetooth_stack(struct hw_device_t* device)
484 {
485     UNUSED(device);
486     cleanup();
487     return 0;
488 }
489 
open_bluetooth_stack(const struct hw_module_t * module,UNUSED_ATTR char const * name,struct hw_device_t ** abstraction)490 static int open_bluetooth_stack(const struct hw_module_t *module, UNUSED_ATTR char const *name, struct hw_device_t **abstraction) {
491   static bluetooth_device_t device = {
492     .common = {
493       .tag = HARDWARE_DEVICE_TAG,
494       .version = 0,
495       .close = close_bluetooth_stack,
496     },
497     .get_bluetooth_interface = bluetooth__get_bluetooth_interface
498   };
499 
500   device.common.module = (struct hw_module_t *)module;
501   *abstraction = (struct hw_device_t *)&device;
502   return 0;
503 }
504 
505 
506 static struct hw_module_methods_t bt_stack_module_methods = {
507     .open = open_bluetooth_stack,
508 };
509 
510 struct hw_module_t HAL_MODULE_INFO_SYM = {
511     .tag = HARDWARE_MODULE_TAG,
512     .version_major = 1,
513     .version_minor = 0,
514     .id = BT_HARDWARE_MODULE_ID,
515     .name = "Bluetooth Stack",
516     .author = "The Android Open Source Project",
517     .methods = &bt_stack_module_methods
518 };
519