• 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 <hardware/bluetooth.h>
31 #include <hardware/bluetooth_headset_interface.h>
32 #include <hardware/bt_av.h>
33 #include <hardware/bt_gatt.h>
34 #include <hardware/bt_hd.h>
35 #include <hardware/bt_hearing_aid.h>
36 #include <hardware/bt_hf_client.h>
37 #include <hardware/bt_hh.h>
38 #include <hardware/bt_le_audio.h>
39 #include <hardware/bt_pan.h>
40 #include <hardware/bt_rc.h>
41 #include <hardware/bt_sdp.h>
42 #include <hardware/bt_sock.h>
43 #include <hardware/bt_vc.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <unistd.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_activity_attribution.h"
55 #include "btif_api.h"
56 #include "btif_av.h"
57 #include "btif_bqr.h"
58 #include "btif_config.h"
59 #include "btif_debug.h"
60 #include "btif_debug_btsnoop.h"
61 #include "btif_debug_conn.h"
62 #include "btif_hf.h"
63 #include "btif_keystore.h"
64 #include "btif_metrics_logging.h"
65 #include "btif_storage.h"
66 #include "btsnoop.h"
67 #include "btsnoop_mem.h"
68 #include "common/address_obfuscator.h"
69 #include "common/metric_id_allocator.h"
70 #include "common/metrics.h"
71 #include "common/os_utils.h"
72 #include "device/include/interop.h"
73 #include "gd/common/init_flags.h"
74 #include "main/shim/dumpsys.h"
75 #include "main/shim/shim.h"
76 #include "osi/include/alarm.h"
77 #include "osi/include/allocation_tracker.h"
78 #include "osi/include/log.h"
79 #include "osi/include/osi.h"
80 #include "osi/include/wakelock.h"
81 #include "stack/gatt/connection_manager.h"
82 #include "stack/include/avdt_api.h"
83 #include "stack/include/btm_api.h"
84 #include "stack/include/btu.h"
85 #include "stack_manager.h"
86 
87 using bluetooth::hearing_aid::HearingAidInterface;
88 using bluetooth::le_audio::LeAudioClientInterface;
89 using bluetooth::vc::VolumeControlInterface;
90 
91 /*******************************************************************************
92  *  Static variables
93  ******************************************************************************/
94 
95 static bt_callbacks_t* bt_hal_cbacks = NULL;
96 bool restricted_mode = false;
97 bool common_criteria_mode = false;
98 const int CONFIG_COMPARE_ALL_PASS = 0b11;
99 int common_criteria_config_compare_result = CONFIG_COMPARE_ALL_PASS;
100 bool is_local_device_atv = false;
101 
102 /*******************************************************************************
103  *  Externs
104  ******************************************************************************/
105 
106 /* list all extended interfaces here */
107 
108 /* handsfree profile - client */
109 extern const bthf_client_interface_t* btif_hf_client_get_interface();
110 /* advanced audio profile */
111 extern const btav_source_interface_t* btif_av_get_src_interface();
112 extern const btav_sink_interface_t* btif_av_get_sink_interface();
113 /*rfc l2cap*/
114 extern const btsock_interface_t* btif_sock_get_interface();
115 /* hid host profile */
116 extern const bthh_interface_t* btif_hh_get_interface();
117 /* hid device profile */
118 extern const bthd_interface_t* btif_hd_get_interface();
119 /*pan*/
120 extern const btpan_interface_t* btif_pan_get_interface();
121 /* gatt */
122 extern const btgatt_interface_t* btif_gatt_get_interface();
123 /* avrc target */
124 extern const btrc_interface_t* btif_rc_get_interface();
125 /* avrc controller */
126 extern const btrc_ctrl_interface_t* btif_rc_ctrl_get_interface();
127 /*SDP search client*/
128 extern const btsdp_interface_t* btif_sdp_get_interface();
129 /*Hearing Aid client*/
130 extern HearingAidInterface* btif_hearing_aid_get_interface();
131 /* LeAudio testi client */
132 extern LeAudioClientInterface* btif_le_audio_get_interface();
133 /* Volume Control client */
134 extern VolumeControlInterface* btif_volume_control_get_interface();
135 
136 /*******************************************************************************
137  *  Functions
138  ******************************************************************************/
139 
interface_ready(void)140 static bool interface_ready(void) { return bt_hal_cbacks != NULL; }
set_hal_cbacks(bt_callbacks_t * callbacks)141 void set_hal_cbacks(bt_callbacks_t* callbacks) { bt_hal_cbacks = callbacks; }
142 
is_profile(const char * p1,const char * p2)143 static bool is_profile(const char* p1, const char* p2) {
144   CHECK(p1);
145   CHECK(p2);
146   return strlen(p1) == strlen(p2) && strncmp(p1, p2, strlen(p2)) == 0;
147 }
148 
149 /*****************************************************************************
150  *
151  *   BLUETOOTH HAL INTERFACE FUNCTIONS
152  *
153  ****************************************************************************/
154 
init(bt_callbacks_t * callbacks,bool start_restricted,bool is_common_criteria_mode,int config_compare_result,const char ** init_flags,bool is_atv)155 static int init(bt_callbacks_t* callbacks, bool start_restricted,
156                 bool is_common_criteria_mode, int config_compare_result,
157                 const char** init_flags, bool is_atv) {
158   LOG_INFO(
159       "%s: start restricted = %d ; common criteria mode = %d, config compare "
160       "result = %d",
161       __func__, start_restricted, is_common_criteria_mode,
162       config_compare_result);
163 
164   bluetooth::common::InitFlags::Load(init_flags);
165 
166   if (interface_ready()) return BT_STATUS_DONE;
167 
168 #ifdef BLUEDROID_DEBUG
169   allocation_tracker_init();
170 #endif
171 
172   set_hal_cbacks(callbacks);
173 
174   restricted_mode = start_restricted;
175   common_criteria_mode = is_common_criteria_mode;
176   common_criteria_config_compare_result = config_compare_result;
177   is_local_device_atv = is_atv;
178 
179   stack_manager_get_interface()->init_stack();
180   btif_debug_init();
181   return BT_STATUS_SUCCESS;
182 }
183 
enable()184 static int enable() {
185   if (!interface_ready()) return BT_STATUS_NOT_READY;
186 
187   stack_manager_get_interface()->start_up_stack_async();
188   return BT_STATUS_SUCCESS;
189 }
190 
disable(void)191 static int disable(void) {
192   if (!interface_ready()) return BT_STATUS_NOT_READY;
193 
194   stack_manager_get_interface()->shut_down_stack_async();
195   return BT_STATUS_SUCCESS;
196 }
197 
cleanup(void)198 static void cleanup(void) { stack_manager_get_interface()->clean_up_stack(); }
199 
is_restricted_mode()200 bool is_restricted_mode() { return restricted_mode; }
is_common_criteria_mode()201 bool is_common_criteria_mode() {
202   return is_bluetooth_uid() && common_criteria_mode;
203 }
204 // if common criteria mode disable, will always return
205 // CONFIG_COMPARE_ALL_PASS(0b11) indicate don't check config checksum.
get_common_criteria_config_compare_result()206 int get_common_criteria_config_compare_result() {
207   return is_common_criteria_mode() ? common_criteria_config_compare_result
208                                    : CONFIG_COMPARE_ALL_PASS;
209 }
210 
is_atv_device()211 bool is_atv_device() { return is_local_device_atv; }
212 
get_adapter_properties(void)213 static int get_adapter_properties(void) {
214   if (!btif_is_enabled()) return BT_STATUS_NOT_READY;
215 
216   do_in_main_thread(FROM_HERE, base::BindOnce(btif_get_adapter_properties));
217   return BT_STATUS_SUCCESS;
218 }
219 
get_adapter_property(bt_property_type_t type)220 static int get_adapter_property(bt_property_type_t type) {
221   /* Allow get_adapter_property only for BDADDR and BDNAME if BT is disabled */
222   if (!btif_is_enabled() && (type != BT_PROPERTY_BDADDR) &&
223       (type != BT_PROPERTY_BDNAME) && (type != BT_PROPERTY_CLASS_OF_DEVICE))
224     return BT_STATUS_NOT_READY;
225 
226   do_in_main_thread(FROM_HERE, base::BindOnce(btif_get_adapter_property, type));
227   return BT_STATUS_SUCCESS;
228 }
229 
set_adapter_property(const bt_property_t * property)230 static int set_adapter_property(const bt_property_t* property) {
231   if (!btif_is_enabled()) return BT_STATUS_NOT_READY;
232 
233   switch (property->type) {
234     case BT_PROPERTY_BDNAME:
235     case BT_PROPERTY_ADAPTER_SCAN_MODE:
236     case BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT:
237     case BT_PROPERTY_CLASS_OF_DEVICE:
238     case BT_PROPERTY_LOCAL_IO_CAPS:
239     case BT_PROPERTY_LOCAL_IO_CAPS_BLE:
240       break;
241     default:
242       return BT_STATUS_FAIL;
243   }
244 
245   do_in_main_thread(FROM_HERE, base::BindOnce(
246                                    [](bt_property_t* property) {
247                                      btif_set_adapter_property(property);
248                                      osi_free(property);
249                                    },
250                                    property_deep_copy(property)));
251   return BT_STATUS_SUCCESS;
252 }
253 
get_remote_device_properties(RawAddress * remote_addr)254 int get_remote_device_properties(RawAddress* remote_addr) {
255   if (!btif_is_enabled()) return BT_STATUS_NOT_READY;
256 
257   do_in_main_thread(FROM_HERE, base::BindOnce(btif_get_remote_device_properties,
258                                               *remote_addr));
259   return BT_STATUS_SUCCESS;
260 }
261 
get_remote_device_property(RawAddress * remote_addr,bt_property_type_t type)262 int get_remote_device_property(RawAddress* remote_addr,
263                                bt_property_type_t type) {
264   if (!btif_is_enabled()) return BT_STATUS_NOT_READY;
265 
266   do_in_main_thread(FROM_HERE, base::BindOnce(btif_get_remote_device_property,
267                                               *remote_addr, type));
268   return BT_STATUS_SUCCESS;
269 }
270 
set_remote_device_property(RawAddress * remote_addr,const bt_property_t * property)271 int set_remote_device_property(RawAddress* remote_addr,
272                                const bt_property_t* property) {
273   if (!btif_is_enabled()) return BT_STATUS_NOT_READY;
274 
275   do_in_main_thread(
276       FROM_HERE, base::BindOnce(
277                      [](RawAddress remote_addr, bt_property_t* property) {
278                        btif_set_remote_device_property(&remote_addr, property);
279                        osi_free(property);
280                      },
281                      *remote_addr, property_deep_copy(property)));
282   return BT_STATUS_SUCCESS;
283 }
284 
get_remote_services(RawAddress * remote_addr)285 int get_remote_services(RawAddress* remote_addr) {
286   if (!interface_ready()) return BT_STATUS_NOT_READY;
287 
288   do_in_main_thread(FROM_HERE,
289                     base::BindOnce(btif_dm_get_remote_services, *remote_addr,
290                                    BT_TRANSPORT_UNKNOWN));
291   return BT_STATUS_SUCCESS;
292 }
293 
start_discovery(void)294 static int start_discovery(void) {
295   if (!interface_ready()) return BT_STATUS_NOT_READY;
296 
297   do_in_main_thread(FROM_HERE, base::BindOnce(btif_dm_start_discovery));
298   return BT_STATUS_SUCCESS;
299 }
300 
cancel_discovery(void)301 static int cancel_discovery(void) {
302   if (!interface_ready()) return BT_STATUS_NOT_READY;
303 
304   do_in_main_thread(FROM_HERE, base::BindOnce(btif_dm_cancel_discovery));
305   return BT_STATUS_SUCCESS;
306 }
307 
create_bond(const RawAddress * bd_addr,int transport)308 static int create_bond(const RawAddress* bd_addr, int transport) {
309   if (!interface_ready()) return BT_STATUS_NOT_READY;
310   if (btif_dm_pairing_is_busy()) return BT_STATUS_BUSY;
311 
312   do_in_main_thread(FROM_HERE,
313                     base::BindOnce(btif_dm_create_bond, *bd_addr, transport));
314   return BT_STATUS_SUCCESS;
315 }
316 
create_bond_out_of_band(const RawAddress * bd_addr,int transport,const bt_oob_data_t * p192_data,const bt_oob_data_t * p256_data)317 static int create_bond_out_of_band(const RawAddress* bd_addr, int transport,
318                                    const bt_oob_data_t* p192_data,
319                                    const bt_oob_data_t* p256_data) {
320   if (!interface_ready()) return BT_STATUS_NOT_READY;
321   if (btif_dm_pairing_is_busy()) return BT_STATUS_BUSY;
322 
323   do_in_main_thread(FROM_HERE,
324                     base::BindOnce(btif_dm_create_bond_out_of_band, *bd_addr,
325                                    transport, *p192_data, *p256_data));
326   return BT_STATUS_SUCCESS;
327 }
328 
generate_local_oob_data(tBT_TRANSPORT transport)329 static int generate_local_oob_data(tBT_TRANSPORT transport) {
330   LOG_INFO("%s", __func__);
331   if (!interface_ready()) return BT_STATUS_NOT_READY;
332 
333   return do_in_main_thread(
334       FROM_HERE, base::BindOnce(btif_dm_generate_local_oob_data, transport));
335 }
336 
cancel_bond(const RawAddress * bd_addr)337 static int cancel_bond(const RawAddress* bd_addr) {
338   if (!interface_ready()) return BT_STATUS_NOT_READY;
339 
340   do_in_main_thread(FROM_HERE, base::BindOnce(btif_dm_cancel_bond, *bd_addr));
341   return BT_STATUS_SUCCESS;
342 }
343 
remove_bond(const RawAddress * bd_addr)344 static int remove_bond(const RawAddress* bd_addr) {
345   if (is_restricted_mode() && !btif_storage_is_restricted_device(bd_addr))
346     return BT_STATUS_SUCCESS;
347 
348   if (!interface_ready()) return BT_STATUS_NOT_READY;
349 
350   do_in_main_thread(FROM_HERE, base::BindOnce(btif_dm_remove_bond, *bd_addr));
351   return BT_STATUS_SUCCESS;
352 }
353 
get_connection_state(const RawAddress * bd_addr)354 static int get_connection_state(const RawAddress* bd_addr) {
355   if (!interface_ready()) return 0;
356 
357   return btif_dm_get_connection_state(bd_addr);
358 }
359 
pin_reply(const RawAddress * bd_addr,uint8_t accept,uint8_t pin_len,bt_pin_code_t * pin_code)360 static int pin_reply(const RawAddress* bd_addr, uint8_t accept, uint8_t pin_len,
361                      bt_pin_code_t* pin_code) {
362   if (!interface_ready()) return BT_STATUS_NOT_READY;
363   if (pin_code == nullptr || pin_len > PIN_CODE_LEN) return BT_STATUS_FAIL;
364 
365   do_in_main_thread(FROM_HERE, base::BindOnce(btif_dm_pin_reply, *bd_addr,
366                                               accept, pin_len, *pin_code));
367   return BT_STATUS_SUCCESS;
368 }
369 
ssp_reply(const RawAddress * bd_addr,bt_ssp_variant_t variant,uint8_t accept,uint32_t passkey)370 static int ssp_reply(const RawAddress* bd_addr, bt_ssp_variant_t variant,
371                      uint8_t accept, uint32_t passkey) {
372   if (!interface_ready()) return BT_STATUS_NOT_READY;
373   if (variant == BT_SSP_VARIANT_PASSKEY_ENTRY) return BT_STATUS_FAIL;
374 
375   do_in_main_thread(
376       FROM_HERE, base::BindOnce(btif_dm_ssp_reply, *bd_addr, variant, accept));
377   return BT_STATUS_SUCCESS;
378 }
379 
read_energy_info()380 static int read_energy_info() {
381   if (!interface_ready()) return BT_STATUS_NOT_READY;
382 
383   do_in_main_thread(FROM_HERE, base::BindOnce(btif_dm_read_energy_info));
384   return BT_STATUS_SUCCESS;
385 }
386 
dump(int fd,const char ** arguments)387 static void dump(int fd, const char** arguments) {
388   btif_debug_conn_dump(fd);
389   btif_debug_bond_event_dump(fd);
390   btif_debug_a2dp_dump(fd);
391   btif_debug_av_dump(fd);
392   bta_debug_av_dump(fd);
393   stack_debug_avdtp_api_dump(fd);
394   bluetooth::avrcp::AvrcpService::DebugDump(fd);
395   btif_debug_config_dump(fd);
396   BTA_HfClientDumpStatistics(fd);
397   wakelock_debug_dump(fd);
398   osi_allocator_debug_dump(fd);
399   alarm_debug_dump(fd);
400   HearingAid::DebugDump(fd);
401   connection_manager::dump(fd);
402   bluetooth::bqr::DebugDump(fd);
403   if (bluetooth::shim::is_any_gd_enabled()) {
404     bluetooth::shim::Dump(fd, arguments);
405   } else {
406 #if (BTSNOOP_MEM == TRUE)
407     btif_debug_btsnoop_dump(fd);
408 #endif
409   }
410 }
411 
dumpMetrics(std::string * output)412 static void dumpMetrics(std::string* output) {
413   bluetooth::common::BluetoothMetricsLogger::GetInstance()->WriteString(output);
414 }
415 
get_profile_interface(const char * profile_id)416 static const void* get_profile_interface(const char* profile_id) {
417   LOG_INFO("%s: id = %s", __func__, profile_id);
418 
419   /* sanity check */
420   if (!interface_ready()) return NULL;
421 
422   /* check for supported profile interfaces */
423   if (is_profile(profile_id, BT_PROFILE_HANDSFREE_ID))
424     return bluetooth::headset::GetInterface();
425 
426   if (is_profile(profile_id, BT_PROFILE_HANDSFREE_CLIENT_ID))
427     return btif_hf_client_get_interface();
428 
429   if (is_profile(profile_id, BT_PROFILE_SOCKETS_ID))
430     return btif_sock_get_interface();
431 
432   if (is_profile(profile_id, BT_PROFILE_PAN_ID))
433     return btif_pan_get_interface();
434 
435   if (is_profile(profile_id, BT_PROFILE_ADVANCED_AUDIO_ID))
436     return btif_av_get_src_interface();
437 
438   if (is_profile(profile_id, BT_PROFILE_ADVANCED_AUDIO_SINK_ID))
439     return btif_av_get_sink_interface();
440 
441   if (is_profile(profile_id, BT_PROFILE_HIDHOST_ID))
442     return btif_hh_get_interface();
443 
444   if (is_profile(profile_id, BT_PROFILE_HIDDEV_ID))
445     return btif_hd_get_interface();
446 
447   if (is_profile(profile_id, BT_PROFILE_SDP_CLIENT_ID))
448     return btif_sdp_get_interface();
449 
450   if (is_profile(profile_id, BT_PROFILE_GATT_ID))
451     return btif_gatt_get_interface();
452 
453   if (is_profile(profile_id, BT_PROFILE_AV_RC_ID))
454     return btif_rc_get_interface();
455 
456   if (is_profile(profile_id, BT_PROFILE_AV_RC_CTRL_ID))
457     return btif_rc_ctrl_get_interface();
458 
459   if (is_profile(profile_id, BT_PROFILE_HEARING_AID_ID))
460     return btif_hearing_aid_get_interface();
461 
462   if (is_profile(profile_id, BT_KEYSTORE_ID))
463     return bluetooth::bluetooth_keystore::getBluetoothKeystoreInterface();
464 
465   if (is_profile(profile_id, BT_ACTIVITY_ATTRIBUTION_ID)) {
466     return bluetooth::activity_attribution::get_activity_attribution_instance();
467   }
468 
469   if (is_profile(profile_id, BT_PROFILE_LE_AUDIO_ID))
470     return btif_le_audio_get_interface();
471 
472   if (is_profile(profile_id, BT_PROFILE_VC_ID))
473     return btif_volume_control_get_interface();
474 
475   return NULL;
476 }
477 
dut_mode_configure(uint8_t enable)478 int dut_mode_configure(uint8_t enable) {
479   if (!interface_ready()) return BT_STATUS_NOT_READY;
480   if (!stack_manager_get_interface()->get_stack_is_running())
481     return BT_STATUS_NOT_READY;
482 
483   do_in_main_thread(FROM_HERE, base::BindOnce(btif_dut_mode_configure, enable));
484   return BT_STATUS_SUCCESS;
485 }
486 
dut_mode_send(uint16_t opcode,uint8_t * buf,uint8_t len)487 int dut_mode_send(uint16_t opcode, uint8_t* buf, uint8_t len) {
488   if (!interface_ready()) return BT_STATUS_NOT_READY;
489   if (!btif_is_dut_mode()) return BT_STATUS_FAIL;
490 
491   uint8_t* copy = (uint8_t*)osi_calloc(len);
492   memcpy(copy, buf, len);
493 
494   do_in_main_thread(FROM_HERE,
495                     base::BindOnce(
496                         [](uint16_t opcode, uint8_t* buf, uint8_t len) {
497                           btif_dut_mode_send(opcode, buf, len);
498                           osi_free(buf);
499                         },
500                         opcode, copy, len));
501   return BT_STATUS_SUCCESS;
502 }
503 
le_test_mode(uint16_t opcode,uint8_t * buf,uint8_t len)504 int le_test_mode(uint16_t opcode, uint8_t* buf, uint8_t len) {
505   if (!interface_ready()) return BT_STATUS_NOT_READY;
506 
507   switch (opcode) {
508     case HCI_BLE_TRANSMITTER_TEST:
509       if (len != 3) return BT_STATUS_PARM_INVALID;
510       do_in_main_thread(FROM_HERE, base::BindOnce(btif_ble_transmitter_test,
511                                                   buf[0], buf[1], buf[2]));
512       break;
513     case HCI_BLE_RECEIVER_TEST:
514       if (len != 1) return BT_STATUS_PARM_INVALID;
515       do_in_main_thread(FROM_HERE,
516                         base::BindOnce(btif_ble_receiver_test, buf[0]));
517       break;
518     case HCI_BLE_TEST_END:
519       do_in_main_thread(FROM_HERE, base::BindOnce(btif_ble_test_end));
520       break;
521     default:
522       return BT_STATUS_UNSUPPORTED;
523   }
524   return BT_STATUS_SUCCESS;
525 }
526 
527 static bt_os_callouts_t* wakelock_os_callouts_saved = nullptr;
528 
acquire_wake_lock_cb(const char * lock_name)529 static int acquire_wake_lock_cb(const char* lock_name) {
530   return do_in_jni_thread(
531       FROM_HERE, base::Bind(base::IgnoreResult(
532                                 wakelock_os_callouts_saved->acquire_wake_lock),
533                             lock_name));
534 }
535 
release_wake_lock_cb(const char * lock_name)536 static int release_wake_lock_cb(const char* lock_name) {
537   return do_in_jni_thread(
538       FROM_HERE, base::Bind(base::IgnoreResult(
539                                 wakelock_os_callouts_saved->release_wake_lock),
540                             lock_name));
541 }
542 
543 static bt_os_callouts_t wakelock_os_callouts_jni = {
544     sizeof(wakelock_os_callouts_jni),
545     nullptr /* not used */,
546     acquire_wake_lock_cb,
547     release_wake_lock_cb,
548 };
549 
set_os_callouts(bt_os_callouts_t * callouts)550 static int set_os_callouts(bt_os_callouts_t* callouts) {
551   wakelock_os_callouts_saved = callouts;
552   wakelock_set_os_callouts(&wakelock_os_callouts_jni);
553   return BT_STATUS_SUCCESS;
554 }
555 
config_clear(void)556 static int config_clear(void) {
557   LOG_INFO("%s", __func__);
558   return btif_config_clear() ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
559 }
560 
get_avrcp_service(void)561 static bluetooth::avrcp::ServiceInterface* get_avrcp_service(void) {
562   return bluetooth::avrcp::AvrcpService::GetServiceInterface();
563 }
564 
obfuscate_address(const RawAddress & address)565 static std::string obfuscate_address(const RawAddress& address) {
566   return bluetooth::common::AddressObfuscator::GetInstance()->Obfuscate(
567       address);
568 }
569 
get_metric_id(const RawAddress & address)570 static int get_metric_id(const RawAddress& address) {
571   return allocate_metric_id_from_metric_id_allocator(address);
572 }
573 
set_dynamic_audio_buffer_size(int codec,int size)574 static int set_dynamic_audio_buffer_size(int codec, int size) {
575   return btif_set_dynamic_audio_buffer_size(codec, size);
576 }
577 
578 EXPORT_SYMBOL bt_interface_t bluetoothInterface = {
579     sizeof(bluetoothInterface),
580     init,
581     enable,
582     disable,
583     cleanup,
584     get_adapter_properties,
585     get_adapter_property,
586     set_adapter_property,
587     get_remote_device_properties,
588     get_remote_device_property,
589     set_remote_device_property,
590     nullptr,
591     get_remote_services,
592     start_discovery,
593     cancel_discovery,
594     create_bond,
595     create_bond_out_of_band,
596     remove_bond,
597     cancel_bond,
598     get_connection_state,
599     pin_reply,
600     ssp_reply,
601     get_profile_interface,
602     dut_mode_configure,
603     dut_mode_send,
604     le_test_mode,
605     set_os_callouts,
606     read_energy_info,
607     dump,
608     dumpMetrics,
609     config_clear,
610     interop_database_clear,
611     interop_database_add,
612     get_avrcp_service,
613     obfuscate_address,
614     get_metric_id,
615     set_dynamic_audio_buffer_size,
616     generate_local_oob_data};
617 
618 // callback reporting helpers
619 
property_deep_copy_array(int num_properties,bt_property_t * properties)620 bt_property_t* property_deep_copy_array(int num_properties,
621                                         bt_property_t* properties) {
622   bt_property_t* copy = nullptr;
623   if (num_properties > 0) {
624     size_t content_len = 0;
625     for (int i = 0; i < num_properties; i++) {
626       auto len = properties[i].len;
627       if (len > 0) {
628         content_len += len;
629       }
630     }
631 
632     copy = (bt_property_t*)osi_calloc((sizeof(bt_property_t) * num_properties) +
633                                       content_len);
634     uint8_t* content = (uint8_t*)(copy + num_properties);
635 
636     for (int i = 0; i < num_properties; i++) {
637       auto len = properties[i].len;
638       copy[i].type = properties[i].type;
639       copy[i].len = len;
640       if (len <= 0) {
641         continue;
642       }
643       copy[i].val = content;
644       memcpy(content, properties[i].val, len);
645       content += len;
646     }
647   }
648   return copy;
649 }
650 
invoke_adapter_state_changed_cb(bt_state_t state)651 void invoke_adapter_state_changed_cb(bt_state_t state) {
652   do_in_jni_thread(FROM_HERE, base::BindOnce(
653                                   [](bt_state_t state) {
654                                     HAL_CBACK(bt_hal_cbacks,
655                                               adapter_state_changed_cb, state);
656                                   },
657                                   state));
658 }
659 
invoke_adapter_properties_cb(bt_status_t status,int num_properties,bt_property_t * properties)660 void invoke_adapter_properties_cb(bt_status_t status, int num_properties,
661                                   bt_property_t* properties) {
662   do_in_jni_thread(FROM_HERE,
663                    base::BindOnce(
664                        [](bt_status_t status, int num_properties,
665                           bt_property_t* properties) {
666                          HAL_CBACK(bt_hal_cbacks, adapter_properties_cb, status,
667                                    num_properties, properties);
668                          if (properties) {
669                            osi_free(properties);
670                          }
671                        },
672                        status, num_properties,
673                        property_deep_copy_array(num_properties, properties)));
674 }
675 
invoke_remote_device_properties_cb(bt_status_t status,RawAddress bd_addr,int num_properties,bt_property_t * properties)676 void invoke_remote_device_properties_cb(bt_status_t status, RawAddress bd_addr,
677                                         int num_properties,
678                                         bt_property_t* properties) {
679   do_in_jni_thread(
680       FROM_HERE, base::BindOnce(
681                      [](bt_status_t status, RawAddress bd_addr,
682                         int num_properties, bt_property_t* properties) {
683                        HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
684                                  status, &bd_addr, num_properties, properties);
685                        if (properties) {
686                          osi_free(properties);
687                        }
688                      },
689                      status, bd_addr, num_properties,
690                      property_deep_copy_array(num_properties, properties)));
691 }
692 
invoke_device_found_cb(int num_properties,bt_property_t * properties)693 void invoke_device_found_cb(int num_properties, bt_property_t* properties) {
694   do_in_jni_thread(FROM_HERE,
695                    base::BindOnce(
696                        [](int num_properties, bt_property_t* properties) {
697                          HAL_CBACK(bt_hal_cbacks, device_found_cb,
698                                    num_properties, properties);
699                          if (properties) {
700                            osi_free(properties);
701                          }
702                        },
703                        num_properties,
704                        property_deep_copy_array(num_properties, properties)));
705 }
706 
invoke_discovery_state_changed_cb(bt_discovery_state_t state)707 void invoke_discovery_state_changed_cb(bt_discovery_state_t state) {
708   do_in_jni_thread(FROM_HERE, base::BindOnce(
709                                   [](bt_discovery_state_t state) {
710                                     HAL_CBACK(bt_hal_cbacks,
711                                               discovery_state_changed_cb,
712                                               state);
713                                   },
714                                   state));
715 }
716 
invoke_pin_request_cb(RawAddress bd_addr,bt_bdname_t bd_name,uint32_t cod,bool min_16_digit)717 void invoke_pin_request_cb(RawAddress bd_addr, bt_bdname_t bd_name,
718                            uint32_t cod, bool min_16_digit) {
719   do_in_jni_thread(FROM_HERE, base::BindOnce(
720                                   [](RawAddress bd_addr, bt_bdname_t bd_name,
721                                      uint32_t cod, bool min_16_digit) {
722                                     HAL_CBACK(bt_hal_cbacks, pin_request_cb,
723                                               &bd_addr, &bd_name, cod,
724                                               min_16_digit);
725                                   },
726                                   bd_addr, bd_name, cod, min_16_digit));
727 }
728 
invoke_ssp_request_cb(RawAddress bd_addr,bt_bdname_t bd_name,uint32_t cod,bt_ssp_variant_t pairing_variant,uint32_t pass_key)729 void invoke_ssp_request_cb(RawAddress bd_addr, bt_bdname_t bd_name,
730                            uint32_t cod, bt_ssp_variant_t pairing_variant,
731                            uint32_t pass_key) {
732   do_in_jni_thread(FROM_HERE,
733                    base::BindOnce(
734                        [](RawAddress bd_addr, bt_bdname_t bd_name, uint32_t cod,
735                           bt_ssp_variant_t pairing_variant, uint32_t pass_key) {
736                          HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr,
737                                    &bd_name, cod, pairing_variant, pass_key);
738                        },
739                        bd_addr, bd_name, cod, pairing_variant, pass_key));
740 }
741 
invoke_oob_data_request_cb(tBT_TRANSPORT t,bool valid,Octet16 c,Octet16 r,RawAddress raw_address,uint8_t address_type)742 void invoke_oob_data_request_cb(tBT_TRANSPORT t, bool valid, Octet16 c,
743                                 Octet16 r, RawAddress raw_address,
744                                 uint8_t address_type) {
745   LOG_INFO("%s", __func__);
746   bt_oob_data_t oob_data = {};
747   char* local_name;
748   BTM_ReadLocalDeviceName(&local_name);
749   for (int i = 0; i < BTM_MAX_LOC_BD_NAME_LEN; i++) {
750     oob_data.device_name[i] = local_name[i];
751   }
752 
753   // Set the local address
754   int j = 5;
755   for (int i = 0; i < 6; i++) {
756     oob_data.address[i] = raw_address.address[j];
757     j--;
758   }
759   oob_data.address[6] = address_type;
760 
761   // Each value (for C and R) is 16 octets in length
762   bool c_empty = true;
763   for (int i = 0; i < 16; i++) {
764     // C cannot be all 0s, if so then we want to fail
765     if (c[i] != 0) c_empty = false;
766     oob_data.c[i] = c[i];
767     // R is optional and may be empty
768     oob_data.r[i] = r[i];
769   }
770   oob_data.is_valid = valid && !c_empty;
771   // The oob_data_length is 2 octects in length.  The value includes the length
772   // of itself. 16 + 16 + 2 = 34 Data 0x0022 Little Endian order 0x2200
773   oob_data.oob_data_length[0] = 0;
774   oob_data.oob_data_length[1] = 34;
775   bt_status_t status = do_in_jni_thread(
776       FROM_HERE, base::BindOnce(
777                      [](tBT_TRANSPORT t, bt_oob_data_t oob_data) {
778                        HAL_CBACK(bt_hal_cbacks, generate_local_oob_data_cb, t,
779                                  oob_data);
780                      },
781                      t, oob_data));
782   if (status != BT_STATUS_SUCCESS) {
783     LOG_ERROR("%s: Failed to call callback!", __func__);
784   }
785 }
786 
invoke_bond_state_changed_cb(bt_status_t status,RawAddress bd_addr,bt_bond_state_t state)787 void invoke_bond_state_changed_cb(bt_status_t status, RawAddress bd_addr,
788                                   bt_bond_state_t state) {
789   do_in_jni_thread(
790       FROM_HERE,
791       base::BindOnce(
792           [](bt_status_t status, RawAddress bd_addr, bt_bond_state_t state) {
793             HAL_CBACK(bt_hal_cbacks, bond_state_changed_cb, status, &bd_addr,
794                       state);
795           },
796           status, bd_addr, state));
797 }
798 
invoke_acl_state_changed_cb(bt_status_t status,RawAddress bd_addr,bt_acl_state_t state,bt_hci_error_code_t hci_reason)799 void invoke_acl_state_changed_cb(bt_status_t status, RawAddress bd_addr,
800                                  bt_acl_state_t state, bt_hci_error_code_t hci_reason) {
801   do_in_jni_thread(
802       FROM_HERE,
803       base::BindOnce(
804           [](bt_status_t status, RawAddress bd_addr, bt_acl_state_t state,
805              bt_hci_error_code_t hci_reason) {
806             HAL_CBACK(bt_hal_cbacks, acl_state_changed_cb, status, &bd_addr,
807                       state, hci_reason);
808           },
809           status, bd_addr, state, hci_reason));
810 }
811 
invoke_thread_evt_cb(bt_cb_thread_evt event)812 void invoke_thread_evt_cb(bt_cb_thread_evt event) {
813   do_in_jni_thread(FROM_HERE, base::BindOnce(
814                                   [](bt_cb_thread_evt event) {
815                                     HAL_CBACK(bt_hal_cbacks, thread_evt_cb,
816                                               event);
817                                     if (event == DISASSOCIATE_JVM) {
818                                       bt_hal_cbacks = NULL;
819                                     }
820                                   },
821                                   event));
822 }
823 
invoke_le_test_mode_cb(bt_status_t status,uint16_t count)824 void invoke_le_test_mode_cb(bt_status_t status, uint16_t count) {
825   do_in_jni_thread(FROM_HERE, base::BindOnce(
826                                   [](bt_status_t status, uint16_t count) {
827                                     HAL_CBACK(bt_hal_cbacks, le_test_mode_cb,
828                                               status, count);
829                                   },
830                                   status, count));
831 }
832 
833 // takes ownership of |uid_data|
invoke_energy_info_cb(bt_activity_energy_info energy_info,bt_uid_traffic_t * uid_data)834 void invoke_energy_info_cb(bt_activity_energy_info energy_info,
835                            bt_uid_traffic_t* uid_data) {
836   do_in_jni_thread(
837       FROM_HERE,
838       base::BindOnce(
839           [](bt_activity_energy_info energy_info, bt_uid_traffic_t* uid_data) {
840             HAL_CBACK(bt_hal_cbacks, energy_info_cb, &energy_info, uid_data);
841             osi_free(uid_data);
842           },
843           energy_info, uid_data));
844 }
845 
invoke_link_quality_report_cb(uint64_t timestamp,int report_id,int rssi,int snr,int retransmission_count,int packets_not_receive_count,int negative_acknowledgement_count)846 void invoke_link_quality_report_cb(
847     uint64_t timestamp, int report_id, int rssi, int snr,
848     int retransmission_count, int packets_not_receive_count,
849     int negative_acknowledgement_count) {
850   do_in_jni_thread(
851       FROM_HERE,
852       base::BindOnce(
853           [](uint64_t timestamp, int report_id, int rssi, int snr,
854              int retransmission_count, int packets_not_receive_count,
855              int negative_acknowledgement_count) {
856             HAL_CBACK(bt_hal_cbacks, link_quality_report_cb,
857                       timestamp, report_id, rssi, snr, retransmission_count,
858                       packets_not_receive_count,
859                       negative_acknowledgement_count);
860           },
861           timestamp, report_id, rssi, snr, retransmission_count,
862           packets_not_receive_count, negative_acknowledgement_count));
863 }
864