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_keystore.h"
63 #include "btif_storage.h"
64 #include "btsnoop.h"
65 #include "btsnoop_mem.h"
66 #include "common/address_obfuscator.h"
67 #include "common/metric_id_allocator.h"
68 #include "common/metrics.h"
69 #include "device/include/interop.h"
70 #include "gd/common/init_flags.h"
71 #include "main/shim/dumpsys.h"
72 #include "main/shim/shim.h"
73 #include "osi/include/alarm.h"
74 #include "osi/include/allocation_tracker.h"
75 #include "osi/include/log.h"
76 #include "osi/include/osi.h"
77 #include "osi/include/wakelock.h"
78 #include "stack/gatt/connection_manager.h"
79 #include "stack_manager.h"
80
81 using bluetooth::hearing_aid::HearingAidInterface;
82
83 /*******************************************************************************
84 * Static variables
85 ******************************************************************************/
86
87 bt_callbacks_t* bt_hal_cbacks = NULL;
88 bool restricted_mode = false;
89 bool niap_mode = false;
90 const int CONFIG_COMPARE_ALL_PASS = 0b11;
91 int niap_config_compare_result = CONFIG_COMPARE_ALL_PASS;
92
93 /*******************************************************************************
94 * Externs
95 ******************************************************************************/
96
97 /* list all extended interfaces here */
98
99 /* handsfree profile - client */
100 extern const bthf_client_interface_t* btif_hf_client_get_interface();
101 /* advanced audio profile */
102 extern const btav_source_interface_t* btif_av_get_src_interface();
103 extern const btav_sink_interface_t* btif_av_get_sink_interface();
104 /*rfc l2cap*/
105 extern const btsock_interface_t* btif_sock_get_interface();
106 /* hid host profile */
107 extern const bthh_interface_t* btif_hh_get_interface();
108 /* hid device profile */
109 extern const bthd_interface_t* btif_hd_get_interface();
110 /*pan*/
111 extern const btpan_interface_t* btif_pan_get_interface();
112 /*map client*/
113 extern const btmce_interface_t* btif_mce_get_interface();
114 /* gatt */
115 extern const btgatt_interface_t* btif_gatt_get_interface();
116 /* avrc target */
117 extern const btrc_interface_t* btif_rc_get_interface();
118 /* avrc controller */
119 extern const btrc_ctrl_interface_t* btif_rc_ctrl_get_interface();
120 /*SDP search client*/
121 extern const btsdp_interface_t* btif_sdp_get_interface();
122 /*Hearing Aid client*/
123 extern HearingAidInterface* btif_hearing_aid_get_interface();
124
125 /*******************************************************************************
126 * Functions
127 ******************************************************************************/
128
interface_ready(void)129 static bool interface_ready(void) { return bt_hal_cbacks != NULL; }
130
is_profile(const char * p1,const char * p2)131 static bool is_profile(const char* p1, const char* p2) {
132 CHECK(p1);
133 CHECK(p2);
134 return strlen(p1) == strlen(p2) && strncmp(p1, p2, strlen(p2)) == 0;
135 }
136
137 /*****************************************************************************
138 *
139 * BLUETOOTH HAL INTERFACE FUNCTIONS
140 *
141 ****************************************************************************/
142
init(bt_callbacks_t * callbacks,bool start_restricted,bool is_niap_mode,int config_compare_result,const char ** init_flags)143 static int init(bt_callbacks_t* callbacks, bool start_restricted,
144 bool is_niap_mode, int config_compare_result,
145 const char** init_flags) {
146 LOG_INFO("%s: start restricted = %d ; niap = %d, config compare result = %d",
147 __func__, start_restricted, is_niap_mode, config_compare_result);
148
149 bluetooth::common::InitFlags::Load(init_flags);
150
151 if (interface_ready()) return BT_STATUS_DONE;
152
153 #ifdef BLUEDROID_DEBUG
154 allocation_tracker_init();
155 #endif
156
157 bt_hal_cbacks = callbacks;
158 restricted_mode = start_restricted;
159 niap_mode = is_niap_mode;
160 niap_config_compare_result = config_compare_result;
161
162 stack_manager_get_interface()->init_stack();
163 btif_debug_init();
164 return BT_STATUS_SUCCESS;
165 }
166
enable()167 static int enable() {
168 if (!interface_ready()) return BT_STATUS_NOT_READY;
169
170 stack_manager_get_interface()->start_up_stack_async();
171 return BT_STATUS_SUCCESS;
172 }
173
disable(void)174 static int disable(void) {
175 if (!interface_ready()) return BT_STATUS_NOT_READY;
176
177 stack_manager_get_interface()->shut_down_stack_async();
178 return BT_STATUS_SUCCESS;
179 }
180
cleanup(void)181 static void cleanup(void) { stack_manager_get_interface()->clean_up_stack(); }
182
is_restricted_mode()183 bool is_restricted_mode() { return restricted_mode; }
is_niap_mode()184 bool is_niap_mode() { return niap_mode; }
185 // if niap mode disable, will always return CONFIG_COMPARE_ALL_PASS(0b11)
186 // indicate don't check config checksum.
get_niap_config_compare_result()187 int get_niap_config_compare_result() {
188 return niap_mode ? niap_config_compare_result : CONFIG_COMPARE_ALL_PASS;
189 }
190
get_adapter_properties(void)191 static int get_adapter_properties(void) {
192 /* sanity check */
193 if (!interface_ready()) return BT_STATUS_NOT_READY;
194
195 return btif_get_adapter_properties();
196 }
197
get_adapter_property(bt_property_type_t type)198 static int get_adapter_property(bt_property_type_t type) {
199 /* sanity check */
200 if (!interface_ready()) return BT_STATUS_NOT_READY;
201
202 return btif_get_adapter_property(type);
203 }
204
set_adapter_property(const bt_property_t * property)205 static int set_adapter_property(const bt_property_t* property) {
206 /* sanity check */
207 if (!interface_ready()) return BT_STATUS_NOT_READY;
208
209 return btif_set_adapter_property(property);
210 }
211
get_remote_device_properties(RawAddress * remote_addr)212 int get_remote_device_properties(RawAddress* remote_addr) {
213 /* sanity check */
214 if (!interface_ready()) return BT_STATUS_NOT_READY;
215
216 return btif_get_remote_device_properties(remote_addr);
217 }
218
get_remote_device_property(RawAddress * remote_addr,bt_property_type_t type)219 int get_remote_device_property(RawAddress* remote_addr,
220 bt_property_type_t type) {
221 /* sanity check */
222 if (!interface_ready()) return BT_STATUS_NOT_READY;
223
224 return btif_get_remote_device_property(remote_addr, type);
225 }
226
set_remote_device_property(RawAddress * remote_addr,const bt_property_t * property)227 int set_remote_device_property(RawAddress* remote_addr,
228 const bt_property_t* property) {
229 /* sanity check */
230 if (!interface_ready()) return BT_STATUS_NOT_READY;
231
232 return btif_set_remote_device_property(remote_addr, property);
233 }
234
get_remote_service_record(const RawAddress & remote_addr,const bluetooth::Uuid & uuid)235 int get_remote_service_record(const RawAddress& remote_addr,
236 const bluetooth::Uuid& uuid) {
237 /* sanity check */
238 if (!interface_ready()) return BT_STATUS_NOT_READY;
239
240 return btif_get_remote_service_record(remote_addr, uuid);
241 }
242
get_remote_services(RawAddress * remote_addr)243 int get_remote_services(RawAddress* remote_addr) {
244 /* sanity check */
245 if (!interface_ready()) return BT_STATUS_NOT_READY;
246
247 return btif_dm_get_remote_services(*remote_addr);
248 }
249
start_discovery(void)250 static int start_discovery(void) {
251 /* sanity check */
252 if (!interface_ready()) return BT_STATUS_NOT_READY;
253
254 return btif_dm_start_discovery();
255 }
256
cancel_discovery(void)257 static int cancel_discovery(void) {
258 /* sanity check */
259 if (!interface_ready()) return BT_STATUS_NOT_READY;
260
261 return btif_dm_cancel_discovery();
262 }
263
create_bond(const RawAddress * bd_addr,int transport)264 static int create_bond(const RawAddress* bd_addr, int transport) {
265 /* sanity check */
266 if (!interface_ready()) return BT_STATUS_NOT_READY;
267
268 return btif_dm_create_bond(bd_addr, transport);
269 }
270
create_bond_out_of_band(const RawAddress * bd_addr,int transport,const bt_out_of_band_data_t * oob_data)271 static int create_bond_out_of_band(const RawAddress* bd_addr, int transport,
272 const bt_out_of_band_data_t* oob_data) {
273 /* sanity check */
274 if (!interface_ready()) return BT_STATUS_NOT_READY;
275
276 return btif_dm_create_bond_out_of_band(bd_addr, transport, oob_data);
277 }
278
cancel_bond(const RawAddress * bd_addr)279 static int cancel_bond(const RawAddress* bd_addr) {
280 /* sanity check */
281 if (!interface_ready()) return BT_STATUS_NOT_READY;
282
283 return btif_dm_cancel_bond(bd_addr);
284 }
285
remove_bond(const RawAddress * bd_addr)286 static int remove_bond(const RawAddress* bd_addr) {
287 if (is_restricted_mode() && !btif_storage_is_restricted_device(bd_addr))
288 return BT_STATUS_SUCCESS;
289
290 /* sanity check */
291 if (!interface_ready()) return BT_STATUS_NOT_READY;
292
293 return btif_dm_remove_bond(bd_addr);
294 }
295
get_connection_state(const RawAddress * bd_addr)296 static int get_connection_state(const RawAddress* bd_addr) {
297 /* sanity check */
298 if (!interface_ready()) return 0;
299
300 return btif_dm_get_connection_state(bd_addr);
301 }
302
pin_reply(const RawAddress * bd_addr,uint8_t accept,uint8_t pin_len,bt_pin_code_t * pin_code)303 static int pin_reply(const RawAddress* bd_addr, uint8_t accept, uint8_t pin_len,
304 bt_pin_code_t* pin_code) {
305 /* sanity check */
306 if (!interface_ready()) return BT_STATUS_NOT_READY;
307
308 return btif_dm_pin_reply(bd_addr, accept, pin_len, pin_code);
309 }
310
ssp_reply(const RawAddress * bd_addr,bt_ssp_variant_t variant,uint8_t accept,uint32_t passkey)311 static int ssp_reply(const RawAddress* bd_addr, bt_ssp_variant_t variant,
312 uint8_t accept, uint32_t passkey) {
313 /* sanity check */
314 if (!interface_ready()) return BT_STATUS_NOT_READY;
315
316 return btif_dm_ssp_reply(bd_addr, variant, accept, passkey);
317 }
318
read_energy_info()319 static int read_energy_info() {
320 if (!interface_ready()) return BT_STATUS_NOT_READY;
321 btif_dm_read_energy_info();
322 return BT_STATUS_SUCCESS;
323 }
324
dump(int fd,const char ** arguments)325 static void dump(int fd, const char** arguments) {
326 btif_debug_conn_dump(fd);
327 btif_debug_bond_event_dump(fd);
328 btif_debug_a2dp_dump(fd);
329 btif_debug_av_dump(fd);
330 bta_debug_av_dump(fd);
331 stack_debug_avdtp_api_dump(fd);
332 bluetooth::avrcp::AvrcpService::DebugDump(fd);
333 btif_debug_config_dump(fd);
334 BTA_HfClientDumpStatistics(fd);
335 wakelock_debug_dump(fd);
336 osi_allocator_debug_dump(fd);
337 alarm_debug_dump(fd);
338 HearingAid::DebugDump(fd);
339 connection_manager::dump(fd);
340 bluetooth::bqr::DebugDump(fd);
341 if (bluetooth::shim::is_gd_shim_enabled()) {
342 bluetooth::shim::Dump(fd, arguments);
343 } else {
344 #if (BTSNOOP_MEM == TRUE)
345 btif_debug_btsnoop_dump(fd);
346 #endif
347 }
348 }
349
dumpMetrics(std::string * output)350 static void dumpMetrics(std::string* output) {
351 bluetooth::common::BluetoothMetricsLogger::GetInstance()->WriteString(output);
352 }
353
get_profile_interface(const char * profile_id)354 static const void* get_profile_interface(const char* profile_id) {
355 LOG_INFO("%s: id = %s", __func__, profile_id);
356
357 /* sanity check */
358 if (!interface_ready()) return NULL;
359
360 /* check for supported profile interfaces */
361 if (is_profile(profile_id, BT_PROFILE_HANDSFREE_ID))
362 return bluetooth::headset::GetInterface();
363
364 if (is_profile(profile_id, BT_PROFILE_HANDSFREE_CLIENT_ID))
365 return btif_hf_client_get_interface();
366
367 if (is_profile(profile_id, BT_PROFILE_SOCKETS_ID))
368 return btif_sock_get_interface();
369
370 if (is_profile(profile_id, BT_PROFILE_PAN_ID))
371 return btif_pan_get_interface();
372
373 if (is_profile(profile_id, BT_PROFILE_ADVANCED_AUDIO_ID))
374 return btif_av_get_src_interface();
375
376 if (is_profile(profile_id, BT_PROFILE_ADVANCED_AUDIO_SINK_ID))
377 return btif_av_get_sink_interface();
378
379 if (is_profile(profile_id, BT_PROFILE_HIDHOST_ID))
380 return btif_hh_get_interface();
381
382 if (is_profile(profile_id, BT_PROFILE_HIDDEV_ID))
383 return btif_hd_get_interface();
384
385 if (is_profile(profile_id, BT_PROFILE_SDP_CLIENT_ID))
386 return btif_sdp_get_interface();
387
388 if (is_profile(profile_id, BT_PROFILE_GATT_ID))
389 return btif_gatt_get_interface();
390
391 if (is_profile(profile_id, BT_PROFILE_AV_RC_ID))
392 return btif_rc_get_interface();
393
394 if (is_profile(profile_id, BT_PROFILE_AV_RC_CTRL_ID))
395 return btif_rc_ctrl_get_interface();
396
397 if (is_profile(profile_id, BT_PROFILE_HEARING_AID_ID))
398 return btif_hearing_aid_get_interface();
399
400 if (is_profile(profile_id, BT_KEYSTORE_ID))
401 return bluetooth::bluetooth_keystore::getBluetoothKeystoreInterface();
402 return NULL;
403 }
404
dut_mode_configure(uint8_t enable)405 int dut_mode_configure(uint8_t enable) {
406 LOG_INFO("%s", __func__);
407
408 /* sanity check */
409 if (!interface_ready()) return BT_STATUS_NOT_READY;
410
411 return btif_dut_mode_configure(enable);
412 }
413
dut_mode_send(uint16_t opcode,uint8_t * buf,uint8_t len)414 int dut_mode_send(uint16_t opcode, uint8_t* buf, uint8_t len) {
415 LOG_INFO("%s", __func__);
416
417 /* sanity check */
418 if (!interface_ready()) return BT_STATUS_NOT_READY;
419
420 return btif_dut_mode_send(opcode, buf, len);
421 }
422
le_test_mode(uint16_t opcode,uint8_t * buf,uint8_t len)423 int le_test_mode(uint16_t opcode, uint8_t* buf, uint8_t len) {
424 LOG_INFO("%s", __func__);
425
426 /* sanity check */
427 if (!interface_ready()) return BT_STATUS_NOT_READY;
428
429 return btif_le_test_mode(opcode, buf, len);
430 }
431
432 static bt_os_callouts_t* wakelock_os_callouts_saved = nullptr;
433
acquire_wake_lock_cb(const char * lock_name)434 static int acquire_wake_lock_cb(const char* lock_name) {
435 return do_in_jni_thread(
436 FROM_HERE, base::Bind(base::IgnoreResult(
437 wakelock_os_callouts_saved->acquire_wake_lock),
438 lock_name));
439 }
440
release_wake_lock_cb(const char * lock_name)441 static int release_wake_lock_cb(const char* lock_name) {
442 return do_in_jni_thread(
443 FROM_HERE, base::Bind(base::IgnoreResult(
444 wakelock_os_callouts_saved->release_wake_lock),
445 lock_name));
446 }
447
448 static bt_os_callouts_t wakelock_os_callouts_jni = {
449 sizeof(wakelock_os_callouts_jni),
450 nullptr /* not used */,
451 acquire_wake_lock_cb,
452 release_wake_lock_cb,
453 };
454
set_os_callouts(bt_os_callouts_t * callouts)455 static int set_os_callouts(bt_os_callouts_t* callouts) {
456 wakelock_os_callouts_saved = callouts;
457 wakelock_set_os_callouts(&wakelock_os_callouts_jni);
458 return BT_STATUS_SUCCESS;
459 }
460
config_clear(void)461 static int config_clear(void) {
462 LOG_INFO("%s", __func__);
463 return btif_config_clear() ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
464 }
465
get_avrcp_service(void)466 static bluetooth::avrcp::ServiceInterface* get_avrcp_service(void) {
467 return bluetooth::avrcp::AvrcpService::GetServiceInterface();
468 }
469
obfuscate_address(const RawAddress & address)470 static std::string obfuscate_address(const RawAddress& address) {
471 return bluetooth::common::AddressObfuscator::GetInstance()->Obfuscate(
472 address);
473 }
474
get_metric_id(const RawAddress & address)475 static int get_metric_id(const RawAddress& address) {
476 return bluetooth::common::MetricIdAllocator::GetInstance().AllocateId(
477 address);
478 }
479
480 EXPORT_SYMBOL bt_interface_t bluetoothInterface = {
481 sizeof(bluetoothInterface),
482 init,
483 enable,
484 disable,
485 cleanup,
486 get_adapter_properties,
487 get_adapter_property,
488 set_adapter_property,
489 get_remote_device_properties,
490 get_remote_device_property,
491 set_remote_device_property,
492 get_remote_service_record,
493 get_remote_services,
494 start_discovery,
495 cancel_discovery,
496 create_bond,
497 create_bond_out_of_band,
498 remove_bond,
499 cancel_bond,
500 get_connection_state,
501 pin_reply,
502 ssp_reply,
503 get_profile_interface,
504 dut_mode_configure,
505 dut_mode_send,
506 le_test_mode,
507 set_os_callouts,
508 read_energy_info,
509 dump,
510 dumpMetrics,
511 config_clear,
512 interop_database_clear,
513 interop_database_add,
514 get_avrcp_service,
515 obfuscate_address,
516 get_metric_id,
517 };
518