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