• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /******************************************************************************
2  *
3  *  Copyright 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 #define LOG_TAG "bt_btif"
28 
29 #include <base/logging.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <unistd.h>
34 
35 #include <hardware/bluetooth.h>
36 #include <hardware/bluetooth_headset_interface.h>
37 #include <hardware/bt_av.h>
38 #include <hardware/bt_gatt.h>
39 #include <hardware/bt_hd.h>
40 #include <hardware/bt_hearing_aid.h>
41 #include <hardware/bt_hf_client.h>
42 #include <hardware/bt_hh.h>
43 #include <hardware/bt_mce.h>
44 #include <hardware/bt_pan.h>
45 #include <hardware/bt_rc.h>
46 #include <hardware/bt_sdp.h>
47 #include <hardware/bt_sock.h>
48 
49 #include "bt_utils.h"
50 #include "bta/include/bta_hearing_aid_api.h"
51 #include "bta/include/bta_hf_client_api.h"
52 #include "btif/avrcp/avrcp_service.h"
53 #include "btif_a2dp.h"
54 #include "btif_api.h"
55 #include "btif_av.h"
56 #include "btif_bqr.h"
57 #include "btif_config.h"
58 #include "btif_debug.h"
59 #include "btif_debug_btsnoop.h"
60 #include "btif_debug_conn.h"
61 #include "btif_hf.h"
62 #include "btif_storage.h"
63 #include "btsnoop.h"
64 #include "btsnoop_mem.h"
65 #include "common/address_obfuscator.h"
66 #include "common/metrics.h"
67 #include "device/include/interop.h"
68 #include "osi/include/alarm.h"
69 #include "osi/include/allocation_tracker.h"
70 #include "osi/include/log.h"
71 #include "osi/include/osi.h"
72 #include "osi/include/wakelock.h"
73 #include "stack/gatt/connection_manager.h"
74 #include "stack_manager.h"
75 
76 using bluetooth::hearing_aid::HearingAidInterface;
77 
78 /*******************************************************************************
79  *  Static variables
80  ******************************************************************************/
81 
82 bt_callbacks_t* bt_hal_cbacks = NULL;
83 bool restricted_mode = false;
84 bool single_user_mode = false;
85 
86 /*******************************************************************************
87  *  Externs
88  ******************************************************************************/
89 
90 /* list all extended interfaces here */
91 
92 /* handsfree profile - client */
93 extern const bthf_client_interface_t* btif_hf_client_get_interface();
94 /* advanced audio profile */
95 extern const btav_source_interface_t* btif_av_get_src_interface();
96 extern const btav_sink_interface_t* btif_av_get_sink_interface();
97 /*rfc l2cap*/
98 extern const btsock_interface_t* btif_sock_get_interface();
99 /* hid host profile */
100 extern const bthh_interface_t* btif_hh_get_interface();
101 /* hid device profile */
102 extern const bthd_interface_t* btif_hd_get_interface();
103 /*pan*/
104 extern const btpan_interface_t* btif_pan_get_interface();
105 /*map client*/
106 extern const btmce_interface_t* btif_mce_get_interface();
107 /* gatt */
108 extern const btgatt_interface_t* btif_gatt_get_interface();
109 /* avrc target */
110 extern const btrc_interface_t* btif_rc_get_interface();
111 /* avrc controller */
112 extern const btrc_ctrl_interface_t* btif_rc_ctrl_get_interface();
113 /*SDP search client*/
114 extern const btsdp_interface_t* btif_sdp_get_interface();
115 /*Hearing Aid client*/
116 extern HearingAidInterface* btif_hearing_aid_get_interface();
117 
118 /*******************************************************************************
119  *  Functions
120  ******************************************************************************/
121 
interface_ready(void)122 static bool interface_ready(void) { return bt_hal_cbacks != NULL; }
123 
is_profile(const char * p1,const char * p2)124 static bool is_profile(const char* p1, const char* p2) {
125   CHECK(p1);
126   CHECK(p2);
127   return strlen(p1) == strlen(p2) && strncmp(p1, p2, strlen(p2)) == 0;
128 }
129 
130 /*****************************************************************************
131  *
132  *   BLUETOOTH HAL INTERFACE FUNCTIONS
133  *
134  ****************************************************************************/
135 
init(bt_callbacks_t * callbacks,bool start_restricted,bool is_single_user_mode)136 static int init(bt_callbacks_t* callbacks, bool start_restricted,
137                 bool is_single_user_mode) {
138   LOG_INFO(LOG_TAG, "%s: start restricted = %d ; single user = %d", __func__,
139            start_restricted, is_single_user_mode);
140 
141   if (interface_ready()) return BT_STATUS_DONE;
142 
143 #ifdef BLUEDROID_DEBUG
144   allocation_tracker_init();
145 #endif
146 
147   bt_hal_cbacks = callbacks;
148   restricted_mode = start_restricted;
149   single_user_mode = is_single_user_mode;
150   stack_manager_get_interface()->init_stack();
151   btif_debug_init();
152   return BT_STATUS_SUCCESS;
153 }
154 
enable()155 static int enable() {
156   if (!interface_ready()) return BT_STATUS_NOT_READY;
157 
158   stack_manager_get_interface()->start_up_stack_async();
159   return BT_STATUS_SUCCESS;
160 }
161 
disable(void)162 static int disable(void) {
163   if (!interface_ready()) return BT_STATUS_NOT_READY;
164 
165   stack_manager_get_interface()->shut_down_stack_async();
166   return BT_STATUS_SUCCESS;
167 }
168 
cleanup(void)169 static void cleanup(void) { stack_manager_get_interface()->clean_up_stack(); }
170 
is_restricted_mode()171 bool is_restricted_mode() { return restricted_mode; }
is_single_user_mode()172 bool is_single_user_mode() { return single_user_mode; }
173 
get_adapter_properties(void)174 static int get_adapter_properties(void) {
175   /* sanity check */
176   if (!interface_ready()) return BT_STATUS_NOT_READY;
177 
178   return btif_get_adapter_properties();
179 }
180 
get_adapter_property(bt_property_type_t type)181 static int get_adapter_property(bt_property_type_t type) {
182   /* sanity check */
183   if (!interface_ready()) return BT_STATUS_NOT_READY;
184 
185   return btif_get_adapter_property(type);
186 }
187 
set_adapter_property(const bt_property_t * property)188 static int set_adapter_property(const bt_property_t* property) {
189   /* sanity check */
190   if (!interface_ready()) return BT_STATUS_NOT_READY;
191 
192   return btif_set_adapter_property(property);
193 }
194 
get_remote_device_properties(RawAddress * remote_addr)195 int get_remote_device_properties(RawAddress* remote_addr) {
196   /* sanity check */
197   if (!interface_ready()) return BT_STATUS_NOT_READY;
198 
199   return btif_get_remote_device_properties(remote_addr);
200 }
201 
get_remote_device_property(RawAddress * remote_addr,bt_property_type_t type)202 int get_remote_device_property(RawAddress* remote_addr,
203                                bt_property_type_t type) {
204   /* sanity check */
205   if (!interface_ready()) return BT_STATUS_NOT_READY;
206 
207   return btif_get_remote_device_property(remote_addr, type);
208 }
209 
set_remote_device_property(RawAddress * remote_addr,const bt_property_t * property)210 int set_remote_device_property(RawAddress* remote_addr,
211                                const bt_property_t* property) {
212   /* sanity check */
213   if (!interface_ready()) return BT_STATUS_NOT_READY;
214 
215   return btif_set_remote_device_property(remote_addr, property);
216 }
217 
get_remote_service_record(const RawAddress & remote_addr,const bluetooth::Uuid & uuid)218 int get_remote_service_record(const RawAddress& remote_addr,
219                               const bluetooth::Uuid& uuid) {
220   /* sanity check */
221   if (!interface_ready()) return BT_STATUS_NOT_READY;
222 
223   return btif_get_remote_service_record(remote_addr, uuid);
224 }
225 
get_remote_services(RawAddress * remote_addr)226 int get_remote_services(RawAddress* remote_addr) {
227   /* sanity check */
228   if (!interface_ready()) return BT_STATUS_NOT_READY;
229 
230   return btif_dm_get_remote_services(*remote_addr);
231 }
232 
start_discovery(void)233 static int start_discovery(void) {
234   /* sanity check */
235   if (!interface_ready()) return BT_STATUS_NOT_READY;
236 
237   return btif_dm_start_discovery();
238 }
239 
cancel_discovery(void)240 static int cancel_discovery(void) {
241   /* sanity check */
242   if (!interface_ready()) return BT_STATUS_NOT_READY;
243 
244   return btif_dm_cancel_discovery();
245 }
246 
create_bond(const RawAddress * bd_addr,int transport)247 static int create_bond(const RawAddress* bd_addr, int transport) {
248   /* sanity check */
249   if (!interface_ready()) return BT_STATUS_NOT_READY;
250 
251   return btif_dm_create_bond(bd_addr, transport);
252 }
253 
create_bond_out_of_band(const RawAddress * bd_addr,int transport,const bt_out_of_band_data_t * oob_data)254 static int create_bond_out_of_band(const RawAddress* bd_addr, int transport,
255                                    const bt_out_of_band_data_t* oob_data) {
256   /* sanity check */
257   if (!interface_ready()) return BT_STATUS_NOT_READY;
258 
259   return btif_dm_create_bond_out_of_band(bd_addr, transport, oob_data);
260 }
261 
cancel_bond(const RawAddress * bd_addr)262 static int cancel_bond(const RawAddress* bd_addr) {
263   /* sanity check */
264   if (!interface_ready()) return BT_STATUS_NOT_READY;
265 
266   return btif_dm_cancel_bond(bd_addr);
267 }
268 
remove_bond(const RawAddress * bd_addr)269 static int remove_bond(const RawAddress* bd_addr) {
270   if (is_restricted_mode() && !btif_storage_is_restricted_device(bd_addr))
271     return BT_STATUS_SUCCESS;
272 
273   /* sanity check */
274   if (!interface_ready()) return BT_STATUS_NOT_READY;
275 
276   return btif_dm_remove_bond(bd_addr);
277 }
278 
get_connection_state(const RawAddress * bd_addr)279 static int get_connection_state(const RawAddress* bd_addr) {
280   /* sanity check */
281   if (!interface_ready()) return 0;
282 
283   return btif_dm_get_connection_state(bd_addr);
284 }
285 
pin_reply(const RawAddress * bd_addr,uint8_t accept,uint8_t pin_len,bt_pin_code_t * pin_code)286 static int pin_reply(const RawAddress* bd_addr, uint8_t accept, uint8_t pin_len,
287                      bt_pin_code_t* pin_code) {
288   /* sanity check */
289   if (!interface_ready()) return BT_STATUS_NOT_READY;
290 
291   return btif_dm_pin_reply(bd_addr, accept, pin_len, pin_code);
292 }
293 
ssp_reply(const RawAddress * bd_addr,bt_ssp_variant_t variant,uint8_t accept,uint32_t passkey)294 static int ssp_reply(const RawAddress* bd_addr, bt_ssp_variant_t variant,
295                      uint8_t accept, uint32_t passkey) {
296   /* sanity check */
297   if (!interface_ready()) return BT_STATUS_NOT_READY;
298 
299   return btif_dm_ssp_reply(bd_addr, variant, accept, passkey);
300 }
301 
read_energy_info()302 static int read_energy_info() {
303   if (!interface_ready()) return BT_STATUS_NOT_READY;
304   btif_dm_read_energy_info();
305   return BT_STATUS_SUCCESS;
306 }
307 
dump(int fd,const char ** arguments)308 static void dump(int fd, const char** arguments) {
309   btif_debug_conn_dump(fd);
310   btif_debug_bond_event_dump(fd);
311   btif_debug_a2dp_dump(fd);
312   btif_debug_av_dump(fd);
313   bta_debug_av_dump(fd);
314   stack_debug_avdtp_api_dump(fd);
315   bluetooth::avrcp::AvrcpService::DebugDump(fd);
316   btif_debug_config_dump(fd);
317   BTA_HfClientDumpStatistics(fd);
318   wakelock_debug_dump(fd);
319   osi_allocator_debug_dump(fd);
320   alarm_debug_dump(fd);
321   HearingAid::DebugDump(fd);
322   connection_manager::dump(fd);
323   bluetooth::bqr::DebugDump(fd);
324 #if (BTSNOOP_MEM == TRUE)
325   btif_debug_btsnoop_dump(fd);
326 #endif
327 }
328 
dumpMetrics(std::string * output)329 static void dumpMetrics(std::string* output) {
330   bluetooth::common::BluetoothMetricsLogger::GetInstance()->WriteString(output);
331 }
332 
get_profile_interface(const char * profile_id)333 static const void* get_profile_interface(const char* profile_id) {
334   LOG_INFO(LOG_TAG, "%s: id = %s", __func__, profile_id);
335 
336   /* sanity check */
337   if (!interface_ready()) return NULL;
338 
339   /* check for supported profile interfaces */
340   if (is_profile(profile_id, BT_PROFILE_HANDSFREE_ID))
341     return bluetooth::headset::GetInterface();
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_HIDDEV_ID))
362     return btif_hd_get_interface();
363 
364   if (is_profile(profile_id, BT_PROFILE_SDP_CLIENT_ID))
365     return btif_sdp_get_interface();
366 
367   if (is_profile(profile_id, BT_PROFILE_GATT_ID))
368     return btif_gatt_get_interface();
369 
370   if (is_profile(profile_id, BT_PROFILE_AV_RC_ID))
371     return btif_rc_get_interface();
372 
373   if (is_profile(profile_id, BT_PROFILE_AV_RC_CTRL_ID))
374     return btif_rc_ctrl_get_interface();
375 
376   if (is_profile(profile_id, BT_PROFILE_HEARING_AID_ID))
377     return btif_hearing_aid_get_interface();
378   return NULL;
379 }
380 
dut_mode_configure(uint8_t enable)381 int dut_mode_configure(uint8_t enable) {
382   LOG_INFO(LOG_TAG, "%s", __func__);
383 
384   /* sanity check */
385   if (!interface_ready()) return BT_STATUS_NOT_READY;
386 
387   return btif_dut_mode_configure(enable);
388 }
389 
dut_mode_send(uint16_t opcode,uint8_t * buf,uint8_t len)390 int dut_mode_send(uint16_t opcode, uint8_t* buf, uint8_t len) {
391   LOG_INFO(LOG_TAG, "%s", __func__);
392 
393   /* sanity check */
394   if (!interface_ready()) return BT_STATUS_NOT_READY;
395 
396   return btif_dut_mode_send(opcode, buf, len);
397 }
398 
le_test_mode(uint16_t opcode,uint8_t * buf,uint8_t len)399 int le_test_mode(uint16_t opcode, uint8_t* buf, uint8_t len) {
400   LOG_INFO(LOG_TAG, "%s", __func__);
401 
402   /* sanity check */
403   if (!interface_ready()) return BT_STATUS_NOT_READY;
404 
405   return btif_le_test_mode(opcode, buf, len);
406 }
407 
408 static bt_os_callouts_t* wakelock_os_callouts_saved = nullptr;
409 
acquire_wake_lock_cb(const char * lock_name)410 static int acquire_wake_lock_cb(const char* lock_name) {
411   return do_in_jni_thread(
412       FROM_HERE, base::Bind(base::IgnoreResult(
413                                 wakelock_os_callouts_saved->acquire_wake_lock),
414                             lock_name));
415 }
416 
release_wake_lock_cb(const char * lock_name)417 static int release_wake_lock_cb(const char* lock_name) {
418   return do_in_jni_thread(
419       FROM_HERE, base::Bind(base::IgnoreResult(
420                                 wakelock_os_callouts_saved->release_wake_lock),
421                             lock_name));
422 }
423 
424 static bt_os_callouts_t wakelock_os_callouts_jni = {
425     sizeof(wakelock_os_callouts_jni),
426     nullptr /* not used */,
427     acquire_wake_lock_cb,
428     release_wake_lock_cb,
429 };
430 
set_os_callouts(bt_os_callouts_t * callouts)431 static int set_os_callouts(bt_os_callouts_t* callouts) {
432   wakelock_os_callouts_saved = callouts;
433   wakelock_set_os_callouts(&wakelock_os_callouts_jni);
434   return BT_STATUS_SUCCESS;
435 }
436 
config_clear(void)437 static int config_clear(void) {
438   LOG_INFO(LOG_TAG, "%s", __func__);
439   return btif_config_clear() ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
440 }
441 
get_avrcp_service(void)442 static bluetooth::avrcp::ServiceInterface* get_avrcp_service(void) {
443   return bluetooth::avrcp::AvrcpService::GetServiceInterface();
444 }
445 
obfuscate_address(const RawAddress & address)446 static std::string obfuscate_address(const RawAddress& address) {
447   return bluetooth::common::AddressObfuscator::GetInstance()->Obfuscate(
448       address);
449 }
450 
451 EXPORT_SYMBOL bt_interface_t bluetoothInterface = {
452     sizeof(bluetoothInterface),
453     init,
454     enable,
455     disable,
456     cleanup,
457     get_adapter_properties,
458     get_adapter_property,
459     set_adapter_property,
460     get_remote_device_properties,
461     get_remote_device_property,
462     set_remote_device_property,
463     get_remote_service_record,
464     get_remote_services,
465     start_discovery,
466     cancel_discovery,
467     create_bond,
468     create_bond_out_of_band,
469     remove_bond,
470     cancel_bond,
471     get_connection_state,
472     pin_reply,
473     ssp_reply,
474     get_profile_interface,
475     dut_mode_configure,
476     dut_mode_send,
477     le_test_mode,
478     set_os_callouts,
479     read_energy_info,
480     dump,
481     dumpMetrics,
482     config_clear,
483     interop_database_clear,
484     interop_database_add,
485     get_avrcp_service,
486     obfuscate_address,
487 };
488