1 /******************************************************************************
2 *
3 * Copyright 2014 The Android Open Source Project
4 * Copyright 2009-2012 Broadcom Corporation
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at:
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 ******************************************************************************/
19
20 /*******************************************************************************
21 *
22 * Filename: btif_core.c
23 *
24 * Description: Contains core functionality related to interfacing between
25 * Bluetooth HAL and BTE core stack.
26 *
27 ******************************************************************************/
28
29 #define LOG_TAG "bt_btif_core"
30
31 #include <signal.h>
32 #include <sys/types.h>
33
34 #include <base/at_exit.h>
35 #include <base/bind.h>
36 #include <base/threading/platform_thread.h>
37 #include <cstdint>
38
39 #include "bt_target.h" // Must be first to define build configuration
40
41 #include "btif/include/btif_av.h"
42 #include "btif/include/btif_common.h"
43 #include "btif/include/btif_config.h"
44 #include "btif/include/btif_dm.h"
45 #include "btif/include/btif_pan.h"
46 #include "btif/include/btif_profile_queue.h"
47 #include "btif/include/btif_sock.h"
48 #include "btif/include/btif_storage.h"
49 #include "btif/include/stack_manager.h"
50 #include "common/message_loop_thread.h"
51 #include "device/include/controller.h"
52 #include "osi/include/future.h"
53 #include "osi/include/log.h"
54 #include "osi/include/properties.h"
55 #include "stack/include/a2dp_api.h"
56 #include "stack/include/btm_api.h"
57 #include "stack/include/btm_ble_api.h"
58 #include "types/bluetooth/uuid.h"
59
60 using base::PlatformThread;
61 using bluetooth::Uuid;
62 using bluetooth::common::MessageLoopThread;
63
64 static void bt_jni_msg_ready(void* context);
65
66 /*******************************************************************************
67 * Constants & Macros
68 ******************************************************************************/
69
70 #ifndef BTE_DID_CONF_FILE
71 // TODO(armansito): Find a better way than searching by a hardcoded path.
72 #if defined(OS_GENERIC) && !defined(TARGET_FLOSS)
73 #define BTE_DID_CONF_FILE "bt_did.conf"
74 #else // !defined(OS_GENERIC)
75 #define BTE_DID_CONF_FILE "/etc/bluetooth/bt_did.conf"
76 #endif // defined(OS_GENERIC)
77 #endif // BTE_DID_CONF_FILE
78
79 #define CODEC_TYPE_NUMBER 32
80 #define DEFAULT_BUFFER_TIME (MAX_PCM_FRAME_NUM_PER_TICK * 2)
81 #define MAXIMUM_BUFFER_TIME (MAX_PCM_FRAME_NUM_PER_TICK * 2)
82 #define MINIMUM_BUFFER_TIME MAX_PCM_FRAME_NUM_PER_TICK
83
84 /*******************************************************************************
85 * Static variables
86 ******************************************************************************/
87
88 static tBTA_SERVICE_MASK btif_enabled_services = 0;
89
90 /*
91 * This variable should be set to 1, if the Bluedroid+BTIF libraries are to
92 * function in DUT mode.
93 *
94 * To set this, the btif_init_bluetooth needs to be called with argument as 1
95 */
96 static uint8_t btif_dut_mode = 0;
97
98 static MessageLoopThread jni_thread("bt_jni_thread");
99 static base::AtExitManager* exit_manager;
100 static uid_set_t* uid_set;
101
102 /*******************************************************************************
103 * Externs
104 ******************************************************************************/
105 void btif_dm_enable_service(tBTA_SERVICE_ID service_id, bool enable);
106 #ifdef BTIF_DM_OOB_TEST
107 void btif_dm_load_local_oob(void);
108 #endif
109
110 /*******************************************************************************
111 *
112 * Function btif_transfer_context
113 *
114 * Description This function switches context to btif task
115 *
116 * p_cback : callback used to process message in btif context
117 * event : event id of message
118 * p_params : parameter area passed to callback (copied)
119 * param_len : length of parameter area
120 * p_copy_cback : If set this function will be invoked for deep
121 * copy
122 *
123 * Returns void
124 *
125 ******************************************************************************/
126
btif_transfer_context(tBTIF_CBACK * p_cback,uint16_t event,char * p_params,int param_len,tBTIF_COPY_CBACK * p_copy_cback)127 bt_status_t btif_transfer_context(tBTIF_CBACK* p_cback, uint16_t event,
128 char* p_params, int param_len,
129 tBTIF_COPY_CBACK* p_copy_cback) {
130 tBTIF_CONTEXT_SWITCH_CBACK* p_msg = (tBTIF_CONTEXT_SWITCH_CBACK*)osi_malloc(
131 sizeof(tBTIF_CONTEXT_SWITCH_CBACK) + param_len);
132
133 BTIF_TRACE_VERBOSE("btif_transfer_context event %d, len %d", event,
134 param_len);
135
136 /* allocate and send message that will be executed in btif context */
137 p_msg->hdr.event = BT_EVT_CONTEXT_SWITCH_EVT; /* internal event */
138 p_msg->p_cb = p_cback;
139
140 p_msg->event = event; /* callback event */
141
142 /* check if caller has provided a copy callback to do the deep copy */
143 if (p_copy_cback) {
144 p_copy_cback(event, p_msg->p_param, p_params);
145 } else if (p_params) {
146 memcpy(p_msg->p_param, p_params, param_len); /* callback parameter data */
147 }
148
149 do_in_jni_thread(base::Bind(&bt_jni_msg_ready, p_msg));
150 return BT_STATUS_SUCCESS;
151 }
152
153 /**
154 * This function posts a task into the btif message loop, that executes it in
155 * the JNI message loop.
156 **/
do_in_jni_thread(const base::Location & from_here,base::OnceClosure task)157 bt_status_t do_in_jni_thread(const base::Location& from_here,
158 base::OnceClosure task) {
159 if (!jni_thread.DoInThread(from_here, std::move(task))) {
160 LOG(ERROR) << __func__ << ": Post task to task runner failed!";
161 return BT_STATUS_FAIL;
162 }
163 return BT_STATUS_SUCCESS;
164 }
165
do_in_jni_thread(base::OnceClosure task)166 bt_status_t do_in_jni_thread(base::OnceClosure task) {
167 return do_in_jni_thread(FROM_HERE, std::move(task));
168 }
169
is_on_jni_thread()170 bool is_on_jni_thread() {
171 return jni_thread.GetThreadId() == PlatformThread::CurrentId();
172 }
173
get_jni_message_loop()174 btbase::AbstractMessageLoop* get_jni_message_loop() {
175 return jni_thread.message_loop();
176 }
177
178 /*******************************************************************************
179 *
180 * Function btif_is_dut_mode
181 *
182 * Description checks if BTIF is currently in DUT mode
183 *
184 * Returns true if test mode, otherwise false
185 *
186 ******************************************************************************/
187
btif_is_dut_mode()188 bool btif_is_dut_mode() { return btif_dut_mode == 1; }
189
190 /*******************************************************************************
191 *
192 * Function btif_is_enabled
193 *
194 * Description checks if main adapter is fully enabled
195 *
196 * Returns 1 if fully enabled, otherwize 0
197 *
198 ******************************************************************************/
199
btif_is_enabled(void)200 int btif_is_enabled(void) {
201 return ((!btif_is_dut_mode()) &&
202 (stack_manager_get_interface()->get_stack_is_running()));
203 }
204
btif_init_ok()205 void btif_init_ok() {
206 btif_dm_load_ble_local_keys();
207 }
208
209 /*******************************************************************************
210 *
211 * Function btif_task
212 *
213 * Description BTIF task handler managing all messages being passed
214 * Bluetooth HAL and BTA.
215 *
216 * Returns void
217 *
218 ******************************************************************************/
bt_jni_msg_ready(void * context)219 static void bt_jni_msg_ready(void* context) {
220 tBTIF_CONTEXT_SWITCH_CBACK* p = (tBTIF_CONTEXT_SWITCH_CBACK*)context;
221 if (p->p_cb) p->p_cb(p->event, p->p_param);
222 osi_free(p);
223 }
224
225 /*******************************************************************************
226 *
227 * Function btif_init_bluetooth
228 *
229 * Description Creates BTIF task and prepares BT scheduler for startup
230 *
231 * Returns bt_status_t
232 *
233 ******************************************************************************/
btif_init_bluetooth()234 bt_status_t btif_init_bluetooth() {
235 LOG_INFO("%s entered", __func__);
236 exit_manager = new base::AtExitManager();
237 jni_thread.StartUp();
238 invoke_thread_evt_cb(ASSOCIATE_JVM);
239 LOG_INFO("%s finished", __func__);
240 return BT_STATUS_SUCCESS;
241 }
242
btif_is_a2dp_offload_enabled()243 static bool btif_is_a2dp_offload_enabled() {
244 char value_sup[PROPERTY_VALUE_MAX] = {'\0'};
245 char value_dis[PROPERTY_VALUE_MAX] = {'\0'};
246 bool a2dp_offload_enabled_;
247
248 osi_property_get("ro.bluetooth.a2dp_offload.supported", value_sup, "false");
249 osi_property_get("persist.bluetooth.a2dp_offload.disabled", value_dis,
250 "false");
251 a2dp_offload_enabled_ =
252 (strcmp(value_sup, "true") == 0) && (strcmp(value_dis, "false") == 0);
253 BTIF_TRACE_DEBUG("a2dp_offload.enable = %d", a2dp_offload_enabled_);
254
255 return a2dp_offload_enabled_;
256 }
257
258 /*******************************************************************************
259 *
260 * Function btif_enable_bluetooth_evt
261 *
262 * Description Event indicating bluetooth enable is completed
263 * Notifies HAL user with updated adapter state
264 *
265 * Returns void
266 *
267 ******************************************************************************/
268
btif_enable_bluetooth_evt()269 void btif_enable_bluetooth_evt() {
270 /* Fetch the local BD ADDR */
271 RawAddress local_bd_addr = *controller_get_interface()->get_address();
272
273 std::string bdstr = local_bd_addr.ToString();
274
275 char val[PROPERTY_VALUE_MAX] = "";
276 int val_size = PROPERTY_VALUE_MAX;
277 if (!btif_config_get_str("Adapter", "Address", val, &val_size) ||
278 strcmp(bdstr.c_str(), val) != 0) {
279 // We failed to get an address or the one in the config file does not match
280 // the address given by the controller interface. Update the config cache
281 LOG_INFO("%s: Storing '%s' into the config file", __func__, bdstr.c_str());
282 btif_config_set_str("Adapter", "Address", bdstr.c_str());
283 btif_config_save();
284
285 // fire HAL callback for property change
286 bt_property_t prop;
287 prop.type = BT_PROPERTY_BDADDR;
288 prop.val = (void*)&local_bd_addr;
289 prop.len = sizeof(RawAddress);
290 invoke_adapter_properties_cb(BT_STATUS_SUCCESS, 1, &prop);
291 }
292
293 /* callback to HAL */
294 uid_set = uid_set_create();
295
296 btif_dm_init(uid_set);
297
298 /* init rfcomm & l2cap api */
299 btif_sock_init(uid_set);
300
301 /* init pan */
302 btif_pan_init();
303
304 /* load did configuration */
305 bte_load_did_conf(BTE_DID_CONF_FILE);
306
307 #ifdef BTIF_DM_OOB_TEST
308 btif_dm_load_local_oob();
309 #endif
310
311 future_ready(stack_manager_get_hack_future(), FUTURE_SUCCESS);
312 LOG_INFO("Bluetooth enable event completed");
313 }
314
315 /*******************************************************************************
316 *
317 * Function btif_cleanup_bluetooth
318 *
319 * Description Cleanup BTIF state.
320 *
321 * Returns void
322 *
323 ******************************************************************************/
324
btif_cleanup_bluetooth()325 bt_status_t btif_cleanup_bluetooth() {
326 LOG_INFO("%s entered", __func__);
327 btif_dm_cleanup();
328 invoke_thread_evt_cb(DISASSOCIATE_JVM);
329 btif_queue_release();
330 jni_thread.ShutDown();
331 delete exit_manager;
332 exit_manager = nullptr;
333 btif_dut_mode = 0;
334 LOG_INFO("%s finished", __func__);
335 return BT_STATUS_SUCCESS;
336 }
337
338 /*******************************************************************************
339 *
340 * Function btif_dut_mode_cback
341 *
342 * Description Callback invoked on completion of vendor specific test mode
343 * command
344 *
345 * Returns None
346 *
347 ******************************************************************************/
btif_dut_mode_cback(UNUSED_ATTR tBTM_VSC_CMPL * p)348 static void btif_dut_mode_cback(UNUSED_ATTR tBTM_VSC_CMPL* p) {
349 /* For now nothing to be done. */
350 }
351
352 /*******************************************************************************
353 *
354 * Function btif_dut_mode_configure
355 *
356 * Description Configure Test Mode - 'enable' to 1 puts the device in test
357 * mode and 0 exits test mode
358 *
359 ******************************************************************************/
btif_dut_mode_configure(uint8_t enable)360 void btif_dut_mode_configure(uint8_t enable) {
361 BTIF_TRACE_DEBUG("%s", __func__);
362
363 btif_dut_mode = enable;
364 if (enable == 1) {
365 BTA_EnableTestMode();
366 } else {
367 // Can't do in process reset anyways - just quit
368 kill(getpid(), SIGKILL);
369 }
370 }
371
372 /*******************************************************************************
373 *
374 * Function btif_dut_mode_send
375 *
376 * Description Sends a HCI Vendor specific command to the controller
377 *
378 ******************************************************************************/
btif_dut_mode_send(uint16_t opcode,uint8_t * buf,uint8_t len)379 void btif_dut_mode_send(uint16_t opcode, uint8_t* buf, uint8_t len) {
380 BTIF_TRACE_DEBUG("%s", __func__);
381 BTM_VendorSpecificCommand(opcode, len, buf, btif_dut_mode_cback);
382 }
383
384 /*****************************************************************************
385 *
386 * btif api adapter property functions
387 *
388 ****************************************************************************/
389
btif_in_get_adapter_properties(void)390 static bt_status_t btif_in_get_adapter_properties(void) {
391 const static uint32_t NUM_ADAPTER_PROPERTIES = 8;
392 bt_property_t properties[NUM_ADAPTER_PROPERTIES];
393 uint32_t num_props = 0;
394
395 RawAddress addr;
396 bt_bdname_t name;
397 bt_scan_mode_t mode;
398 uint32_t disc_timeout;
399 RawAddress bonded_devices[BTM_SEC_MAX_DEVICE_RECORDS];
400 Uuid local_uuids[BT_MAX_NUM_UUIDS];
401 bt_status_t status;
402 bt_io_cap_t local_bt_io_cap;
403 bt_io_cap_t local_bt_io_cap_ble;
404
405 /* RawAddress */
406 BTIF_STORAGE_FILL_PROPERTY(&properties[num_props], BT_PROPERTY_BDADDR,
407 sizeof(addr), &addr);
408 status = btif_storage_get_adapter_property(&properties[num_props]);
409 // Add BT_PROPERTY_BDADDR property into list only when successful.
410 // Otherwise, skip this property entry.
411 if (status == BT_STATUS_SUCCESS) {
412 num_props++;
413 }
414
415 /* BD_NAME */
416 BTIF_STORAGE_FILL_PROPERTY(&properties[num_props], BT_PROPERTY_BDNAME,
417 sizeof(name), &name);
418 btif_storage_get_adapter_property(&properties[num_props]);
419 num_props++;
420
421 /* SCAN_MODE */
422 BTIF_STORAGE_FILL_PROPERTY(&properties[num_props],
423 BT_PROPERTY_ADAPTER_SCAN_MODE, sizeof(mode),
424 &mode);
425 btif_storage_get_adapter_property(&properties[num_props]);
426 num_props++;
427
428 /* DISC_TIMEOUT */
429 BTIF_STORAGE_FILL_PROPERTY(&properties[num_props],
430 BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT,
431 sizeof(disc_timeout), &disc_timeout);
432 btif_storage_get_adapter_property(&properties[num_props]);
433 num_props++;
434
435 /* BONDED_DEVICES */
436 BTIF_STORAGE_FILL_PROPERTY(&properties[num_props],
437 BT_PROPERTY_ADAPTER_BONDED_DEVICES,
438 sizeof(bonded_devices), bonded_devices);
439 btif_storage_get_adapter_property(&properties[num_props]);
440 num_props++;
441
442 /* LOCAL UUIDs */
443 BTIF_STORAGE_FILL_PROPERTY(&properties[num_props], BT_PROPERTY_UUIDS,
444 sizeof(local_uuids), local_uuids);
445 btif_storage_get_adapter_property(&properties[num_props]);
446 num_props++;
447
448 /* LOCAL IO Capabilities */
449 BTIF_STORAGE_FILL_PROPERTY(&properties[num_props], BT_PROPERTY_LOCAL_IO_CAPS,
450 sizeof(bt_io_cap_t), &local_bt_io_cap);
451 btif_storage_get_adapter_property(&properties[num_props]);
452 num_props++;
453
454 BTIF_STORAGE_FILL_PROPERTY(&properties[num_props],
455 BT_PROPERTY_LOCAL_IO_CAPS_BLE, sizeof(bt_io_cap_t),
456 &local_bt_io_cap_ble);
457 btif_storage_get_adapter_property(&properties[num_props]);
458 num_props++;
459
460 invoke_adapter_properties_cb(BT_STATUS_SUCCESS, num_props, properties);
461 return BT_STATUS_SUCCESS;
462 }
463
btif_in_get_remote_device_properties(RawAddress * bd_addr)464 static bt_status_t btif_in_get_remote_device_properties(RawAddress* bd_addr) {
465 bt_property_t remote_properties[8];
466 uint32_t num_props = 0;
467
468 bt_bdname_t name, alias;
469 uint32_t cod, devtype;
470 Uuid remote_uuids[BT_MAX_NUM_UUIDS];
471
472 memset(remote_properties, 0, sizeof(remote_properties));
473 BTIF_STORAGE_FILL_PROPERTY(&remote_properties[num_props], BT_PROPERTY_BDNAME,
474 sizeof(name), &name);
475 btif_storage_get_remote_device_property(bd_addr,
476 &remote_properties[num_props]);
477 num_props++;
478
479 BTIF_STORAGE_FILL_PROPERTY(&remote_properties[num_props],
480 BT_PROPERTY_REMOTE_FRIENDLY_NAME, sizeof(alias),
481 &alias);
482 btif_storage_get_remote_device_property(bd_addr,
483 &remote_properties[num_props]);
484 num_props++;
485
486 BTIF_STORAGE_FILL_PROPERTY(&remote_properties[num_props],
487 BT_PROPERTY_CLASS_OF_DEVICE, sizeof(cod), &cod);
488 btif_storage_get_remote_device_property(bd_addr,
489 &remote_properties[num_props]);
490 num_props++;
491
492 BTIF_STORAGE_FILL_PROPERTY(&remote_properties[num_props],
493 BT_PROPERTY_TYPE_OF_DEVICE, sizeof(devtype),
494 &devtype);
495 btif_storage_get_remote_device_property(bd_addr,
496 &remote_properties[num_props]);
497 num_props++;
498
499 BTIF_STORAGE_FILL_PROPERTY(&remote_properties[num_props], BT_PROPERTY_UUIDS,
500 sizeof(remote_uuids), remote_uuids);
501 btif_storage_get_remote_device_property(bd_addr,
502 &remote_properties[num_props]);
503 num_props++;
504
505 invoke_remote_device_properties_cb(BT_STATUS_SUCCESS, *bd_addr, num_props,
506 remote_properties);
507
508 return BT_STATUS_SUCCESS;
509 }
510
btif_core_storage_adapter_notify_empty_success()511 static void btif_core_storage_adapter_notify_empty_success() {
512 invoke_adapter_properties_cb(BT_STATUS_SUCCESS, 0, NULL);
513 }
514
btif_core_storage_adapter_write(bt_property_t * prop)515 static void btif_core_storage_adapter_write(bt_property_t* prop) {
516 BTIF_TRACE_EVENT("type: %d, len %d, 0x%x", prop->type, prop->len, prop->val);
517 bt_status_t status = btif_storage_set_adapter_property(prop);
518 invoke_adapter_properties_cb(status, 1, prop);
519 }
520
btif_adapter_properties_evt(bt_status_t status,uint32_t num_props,bt_property_t * p_props)521 void btif_adapter_properties_evt(bt_status_t status, uint32_t num_props,
522 bt_property_t* p_props) {
523 invoke_adapter_properties_cb(status, num_props, p_props);
524 }
btif_remote_properties_evt(bt_status_t status,RawAddress * remote_addr,uint32_t num_props,bt_property_t * p_props)525 void btif_remote_properties_evt(bt_status_t status, RawAddress* remote_addr,
526 uint32_t num_props, bt_property_t* p_props) {
527 invoke_remote_device_properties_cb(status, *remote_addr, num_props, p_props);
528 }
529
530 /*******************************************************************************
531 *
532 * Function btif_get_adapter_properties
533 *
534 * Description Fetch all available properties (local & remote)
535 *
536 ******************************************************************************/
537
btif_get_adapter_properties(void)538 void btif_get_adapter_properties(void) {
539 BTIF_TRACE_EVENT("%s", __func__);
540
541 btif_in_get_adapter_properties();
542 }
543
544 /*******************************************************************************
545 *
546 * Function btif_get_adapter_property
547 *
548 * Description Fetches property value from local cache
549 *
550 ******************************************************************************/
551
btif_get_adapter_property(bt_property_type_t type)552 void btif_get_adapter_property(bt_property_type_t type) {
553 BTIF_TRACE_EVENT("%s %d", __func__, type);
554
555 bt_status_t status = BT_STATUS_SUCCESS;
556 char buf[512];
557 bt_property_t prop;
558 prop.type = type;
559 prop.val = (void*)buf;
560 prop.len = sizeof(buf);
561 if (prop.type == BT_PROPERTY_LOCAL_LE_FEATURES) {
562 tBTM_BLE_VSC_CB cmn_vsc_cb;
563 bt_local_le_features_t local_le_features;
564
565 /* LE features are not stored in storage. Should be retrived from stack
566 */
567 BTM_BleGetVendorCapabilities(&cmn_vsc_cb);
568 local_le_features.local_privacy_enabled = BTM_BleLocalPrivacyEnabled();
569
570 prop.len = sizeof(bt_local_le_features_t);
571 if (cmn_vsc_cb.filter_support == 1)
572 local_le_features.max_adv_filter_supported = cmn_vsc_cb.max_filter;
573 else
574 local_le_features.max_adv_filter_supported = 0;
575 local_le_features.max_adv_instance = cmn_vsc_cb.adv_inst_max;
576 local_le_features.max_irk_list_size = cmn_vsc_cb.max_irk_list_sz;
577 local_le_features.rpa_offload_supported = cmn_vsc_cb.rpa_offloading;
578 local_le_features.scan_result_storage_size =
579 cmn_vsc_cb.tot_scan_results_strg;
580 local_le_features.activity_energy_info_supported =
581 cmn_vsc_cb.energy_support;
582 local_le_features.version_supported = cmn_vsc_cb.version_supported;
583 local_le_features.total_trackable_advertisers =
584 cmn_vsc_cb.total_trackable_advertisers;
585
586 local_le_features.extended_scan_support =
587 cmn_vsc_cb.extended_scan_support > 0;
588 local_le_features.debug_logging_supported =
589 cmn_vsc_cb.debug_logging_supported > 0;
590 memcpy(prop.val, &local_le_features, prop.len);
591 } else if (prop.type == BT_PROPERTY_DYNAMIC_AUDIO_BUFFER) {
592 tBTM_BLE_VSC_CB cmn_vsc_cb;
593 bt_dynamic_audio_buffer_item_t dynamic_audio_buffer_item;
594
595 BTM_BleGetVendorCapabilities(&cmn_vsc_cb);
596
597 prop.len = sizeof(bt_dynamic_audio_buffer_item_t);
598 if (btif_is_a2dp_offload_enabled() == false) {
599 BTIF_TRACE_DEBUG("%s Get buffer millis for A2DP software encoding",
600 __func__);
601 for (int i = 0; i < CODEC_TYPE_NUMBER; i++) {
602 dynamic_audio_buffer_item.dab_item[i] = {
603 .default_buffer_time = DEFAULT_BUFFER_TIME,
604 .maximum_buffer_time = MAXIMUM_BUFFER_TIME,
605 .minimum_buffer_time = MINIMUM_BUFFER_TIME};
606 }
607 memcpy(prop.val, &dynamic_audio_buffer_item, prop.len);
608 } else {
609 if (cmn_vsc_cb.dynamic_audio_buffer_support != 0) {
610 BTIF_TRACE_DEBUG("%s Get buffer millis for A2DP Offload", __func__);
611 tBTM_BT_DYNAMIC_AUDIO_BUFFER_CB
612 bt_dynamic_audio_buffer_cb[CODEC_TYPE_NUMBER];
613 BTM_BleGetDynamicAudioBuffer(bt_dynamic_audio_buffer_cb);
614
615 for (int i = 0; i < CODEC_TYPE_NUMBER; i++) {
616 dynamic_audio_buffer_item.dab_item[i] = {
617 .default_buffer_time =
618 bt_dynamic_audio_buffer_cb[i].default_buffer_time,
619 .maximum_buffer_time =
620 bt_dynamic_audio_buffer_cb[i].maximum_buffer_time,
621 .minimum_buffer_time =
622 bt_dynamic_audio_buffer_cb[i].minimum_buffer_time};
623 }
624 memcpy(prop.val, &dynamic_audio_buffer_item, prop.len);
625 } else {
626 BTIF_TRACE_DEBUG("%s Don't support Dynamic Audio Buffer", __func__);
627 }
628 }
629 } else {
630 status = btif_storage_get_adapter_property(&prop);
631 }
632 invoke_adapter_properties_cb(status, 1, &prop);
633 }
634
property_deep_copy(const bt_property_t * prop)635 bt_property_t* property_deep_copy(const bt_property_t* prop) {
636 bt_property_t* copy =
637 (bt_property_t*)osi_calloc(sizeof(bt_property_t) + prop->len);
638 copy->type = prop->type;
639 copy->len = prop->len;
640 copy->val = (uint8_t*)(copy + 1);
641 memcpy(copy->val, prop->val, prop->len);
642 return copy;
643 }
644
645 /*******************************************************************************
646 *
647 * Function btif_set_adapter_property
648 *
649 * Description Updates core stack with property value and stores it in
650 * local cache
651 *
652 * Returns bt_status_t
653 *
654 ******************************************************************************/
655
btif_set_adapter_property(bt_property_t * property)656 void btif_set_adapter_property(bt_property_t* property) {
657 BTIF_TRACE_EVENT("btif_set_adapter_property type: %d, len %d, 0x%x",
658 property->type, property->len, property->val);
659
660 switch (property->type) {
661 case BT_PROPERTY_BDNAME: {
662 char bd_name[BTM_MAX_LOC_BD_NAME_LEN + 1];
663 uint16_t name_len = property->len > BTM_MAX_LOC_BD_NAME_LEN
664 ? BTM_MAX_LOC_BD_NAME_LEN
665 : property->len;
666 memcpy(bd_name, property->val, name_len);
667 bd_name[name_len] = '\0';
668
669 BTIF_TRACE_EVENT("set property name : %s", (char*)bd_name);
670
671 BTA_DmSetDeviceName((char*)bd_name);
672
673 btif_core_storage_adapter_write(property);
674 } break;
675
676 case BT_PROPERTY_ADAPTER_SCAN_MODE: {
677 bt_scan_mode_t mode = *(bt_scan_mode_t*)property->val;
678 BTIF_TRACE_EVENT("set property scan mode : %x", mode);
679
680 if (BTA_DmSetVisibility(mode)) {
681 btif_core_storage_adapter_write(property);
682 }
683 } break;
684 case BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT: {
685 /* Nothing to do beside store the value in NV. Java
686 will change the SCAN_MODE property after setting timeout,
687 if required */
688 btif_core_storage_adapter_write(property);
689 } break;
690 case BT_PROPERTY_CLASS_OF_DEVICE: {
691 DEV_CLASS dev_class;
692 memcpy(dev_class, property->val, DEV_CLASS_LEN);
693
694 BTIF_TRACE_EVENT("set property dev_class : 0x%02x%02x%02x", dev_class[0],
695 dev_class[1], dev_class[2]);
696
697 BTM_SetDeviceClass(dev_class);
698 btif_core_storage_adapter_notify_empty_success();
699 } break;
700 case BT_PROPERTY_LOCAL_IO_CAPS:
701 case BT_PROPERTY_LOCAL_IO_CAPS_BLE: {
702 // Changing IO Capability of stack at run-time is not currently supported.
703 // This call changes the stored value which will affect the stack next
704 // time it starts up.
705 btif_core_storage_adapter_write(property);
706 } break;
707 default:
708 break;
709 }
710 }
711
712 /*******************************************************************************
713 *
714 * Function btif_get_remote_device_property
715 *
716 * Description Fetches the remote device property from the NVRAM
717 *
718 ******************************************************************************/
btif_get_remote_device_property(RawAddress remote_addr,bt_property_type_t type)719 void btif_get_remote_device_property(RawAddress remote_addr,
720 bt_property_type_t type) {
721 char buf[1024];
722 bt_property_t prop;
723 prop.type = type;
724 prop.val = (void*)buf;
725 prop.len = sizeof(buf);
726
727 bt_status_t status =
728 btif_storage_get_remote_device_property(&remote_addr, &prop);
729 invoke_remote_device_properties_cb(status, remote_addr, 1, &prop);
730 }
731
732 /*******************************************************************************
733 *
734 * Function btif_get_remote_device_properties
735 *
736 * Description Fetches all the remote device properties from NVRAM
737 *
738 ******************************************************************************/
btif_get_remote_device_properties(RawAddress remote_addr)739 void btif_get_remote_device_properties(RawAddress remote_addr) {
740 btif_in_get_remote_device_properties(&remote_addr);
741 }
742
743 /*******************************************************************************
744 *
745 * Function btif_set_remote_device_property
746 *
747 * Description Writes the remote device property to NVRAM.
748 * Currently, BT_PROPERTY_REMOTE_FRIENDLY_NAME is the only
749 * remote device property that can be set
750 *
751 ******************************************************************************/
btif_set_remote_device_property(RawAddress * remote_addr,bt_property_t * property)752 void btif_set_remote_device_property(RawAddress* remote_addr,
753 bt_property_t* property) {
754 btif_storage_set_remote_device_property(remote_addr, property);
755 }
756
757 /*******************************************************************************
758 *
759 * Function btif_get_enabled_services_mask
760 *
761 * Description Fetches currently enabled services
762 *
763 * Returns tBTA_SERVICE_MASK
764 *
765 ******************************************************************************/
766
btif_get_enabled_services_mask(void)767 tBTA_SERVICE_MASK btif_get_enabled_services_mask(void) {
768 return btif_enabled_services;
769 }
770
771 /*******************************************************************************
772 *
773 * Function btif_enable_service
774 *
775 * Description Enables the service 'service_ID' to the service_mask.
776 * Upon BT enable, BTIF core shall invoke the BTA APIs to
777 * enable the profiles
778 *
779 ******************************************************************************/
btif_enable_service(tBTA_SERVICE_ID service_id)780 void btif_enable_service(tBTA_SERVICE_ID service_id) {
781 btif_enabled_services |= (1 << service_id);
782
783 BTIF_TRACE_DEBUG("%s: current services:0x%x", __func__,
784 btif_enabled_services);
785
786 if (btif_is_enabled()) {
787 btif_dm_enable_service(service_id, true);
788 }
789 }
790 /*******************************************************************************
791 *
792 * Function btif_disable_service
793 *
794 * Description Disables the service 'service_ID' to the service_mask.
795 * Upon BT disable, BTIF core shall invoke the BTA APIs to
796 * disable the profiles
797 *
798 ******************************************************************************/
btif_disable_service(tBTA_SERVICE_ID service_id)799 void btif_disable_service(tBTA_SERVICE_ID service_id) {
800 btif_enabled_services &= (tBTA_SERVICE_MASK)(~(1 << service_id));
801
802 BTIF_TRACE_DEBUG("%s: Current Services:0x%x", __func__,
803 btif_enabled_services);
804
805 if (btif_is_enabled()) {
806 btif_dm_enable_service(service_id, false);
807 }
808 }
809
DynamicAudiobufferSizeCompleteCallback(tBTM_VSC_CMPL * p_vsc_cmpl_params)810 void DynamicAudiobufferSizeCompleteCallback(tBTM_VSC_CMPL* p_vsc_cmpl_params) {
811 LOG(INFO) << __func__;
812
813 if (p_vsc_cmpl_params->param_len < 1) {
814 LOG(ERROR) << __func__
815 << ": The length of returned parameters is less than 1";
816 return;
817 }
818 uint8_t* p_event_param_buf = p_vsc_cmpl_params->p_param_buf;
819 uint8_t status = 0xff;
820 uint8_t opcode = 0xff;
821 uint16_t respond_buffer_time = 0xffff;
822
823 // [Return Parameter] | [Size] | [Purpose]
824 // Status | 1 octet | Command complete status
825 // Dynamic_Audio_Buffer_opcode| 1 octet | 0x02 - Set buffer time
826 // Audio_Codec_Buffer_Time | 2 octet | Current buffer time
827 STREAM_TO_UINT8(status, p_event_param_buf);
828 if (status != HCI_SUCCESS) {
829 LOG(ERROR) << __func__
830 << ": Fail to configure DFTB. status: " << loghex(status);
831 return;
832 }
833
834 if (p_vsc_cmpl_params->param_len != 4) {
835 LOG(FATAL) << __func__
836 << ": The length of returned parameters is not equal to 4: "
837 << std::to_string(p_vsc_cmpl_params->param_len);
838 return;
839 }
840
841 STREAM_TO_UINT8(opcode, p_event_param_buf);
842 LOG(INFO) << __func__ << ": opcode = " << loghex(opcode);
843
844 if (opcode == 0x02) {
845 STREAM_TO_UINT16(respond_buffer_time, p_event_param_buf);
846 LOG(INFO) << __func__
847 << ": Succeed to configure Media Tx Buffer, used_buffer_time = "
848 << loghex(respond_buffer_time);
849 }
850 }
851
btif_set_dynamic_audio_buffer_size(int codec,int size)852 bt_status_t btif_set_dynamic_audio_buffer_size(int codec, int size) {
853 BTIF_TRACE_DEBUG("%s", __func__);
854
855 tBTM_BLE_VSC_CB cmn_vsc_cb;
856 BTM_BleGetVendorCapabilities(&cmn_vsc_cb);
857
858 if (!btif_av_is_a2dp_offload_enabled()) {
859 BTIF_TRACE_DEBUG("%s Set buffer size (%d) for A2DP software encoding",
860 __func__, size);
861 btif_av_set_dynamic_audio_buffer_size((uint8_t(size)));
862 } else {
863 if (cmn_vsc_cb.dynamic_audio_buffer_support != 0) {
864 BTIF_TRACE_DEBUG("%s Set buffer size (%d) for A2DP offload", __func__,
865 size);
866 uint16_t firmware_tx_buffer_length_byte;
867 uint8_t param[3] = {0};
868 uint8_t* p_param = param;
869
870 firmware_tx_buffer_length_byte = static_cast<uint16_t>(size);
871 LOG(INFO) << __func__ << "firmware_tx_buffer_length_byte: "
872 << firmware_tx_buffer_length_byte;
873
874 UINT8_TO_STREAM(p_param, HCI_CONTROLLER_DAB_SET_BUFFER_TIME);
875 UINT16_TO_STREAM(p_param, firmware_tx_buffer_length_byte);
876 BTM_VendorSpecificCommand(HCI_CONTROLLER_DAB, p_param - param, param,
877 DynamicAudiobufferSizeCompleteCallback);
878 }
879 }
880
881 return BT_STATUS_SUCCESS;
882 }
883