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 <android_bluetooth_sysprop.h>
32 #include <base/at_exit.h>
33 #include <base/functional/bind.h>
34 #include <base/threading/platform_thread.h>
35 #include <bluetooth/log.h>
36 #include <com_android_bluetooth_flags.h>
37 #include <signal.h>
38 #include <sys/types.h>
39
40 #include <cstdint>
41
42 #include "btif/include/btif_api.h"
43 #include "btif/include/btif_common.h"
44 #include "btif/include/btif_config.h"
45 #include "btif/include/btif_dm.h"
46 #include "btif/include/btif_jni_task.h"
47 #include "btif/include/btif_profile_queue.h"
48 #include "btif/include/btif_sock.h"
49 #include "btif/include/btif_storage.h"
50 #include "btif/include/core_callbacks.h"
51 #include "btif/include/stack_manager_t.h"
52 #include "common/message_loop_thread.h"
53 #include "device/include/device_iot_config.h"
54 #include "hci/controller_interface.h"
55 #include "internal_include/bt_target.h"
56 #include "lpp/lpp_offload_interface.h"
57 #include "main/shim/entry.h"
58 #include "main/shim/helpers.h"
59 #include "osi/include/allocator.h"
60 #include "osi/include/future.h"
61 #include "osi/include/properties.h"
62 #include "stack/include/a2dp_api.h"
63 #include "stack/include/btm_ble_api.h"
64 #include "stack/include/btm_client_interface.h"
65 #include "storage/config_keys.h"
66 #include "types/bluetooth/uuid.h"
67 #include "types/raw_address.h"
68
69 using base::PlatformThread;
70 using bluetooth::Uuid;
71 using bluetooth::common::MessageLoopThread;
72 using namespace bluetooth;
73
74 /*******************************************************************************
75 * Constants & Macros
76 ******************************************************************************/
77
78 #ifndef BTE_DID_CONF_FILE
79 // TODO(armansito): Find a better way than searching by a hardcoded path.
80 #if defined(TARGET_FLOSS)
81 #define BTE_DID_CONF_FILE "/var/lib/bluetooth/bt_did.conf"
82 #elif defined(__ANDROID__)
83 #define BTE_DID_CONF_FILE "/apex/com.android.btservices/etc/bluetooth/bt_did.conf"
84 #else // !defined(__ANDROID__)
85 #define BTE_DID_CONF_FILE "bt_did.conf"
86 #endif // defined(__ANDROID__)
87 #endif // BTE_DID_CONF_FILE
88
89 #define CODEC_TYPE_NUMBER 32
90 #define DEFAULT_BUFFER_TIME (MAX_PCM_FRAME_NUM_PER_TICK * 2)
91 #define MAXIMUM_BUFFER_TIME (MAX_PCM_FRAME_NUM_PER_TICK * 2)
92 #define MINIMUM_BUFFER_TIME MAX_PCM_FRAME_NUM_PER_TICK
93
94 /*******************************************************************************
95 * Static variables
96 ******************************************************************************/
97
98 static tBTA_SERVICE_MASK btif_enabled_services = 0;
99
100 /*
101 * This variable should be set to 1, if the Bluedroid+BTIF libraries are to
102 * function in DUT mode.
103 *
104 * To set this, the btif_init_bluetooth needs to be called with argument as 1
105 */
106 static uint8_t btif_dut_mode = 0;
107
108 static base::AtExitManager* exit_manager;
109 static uid_set_t* uid_set;
110
111 /*******************************************************************************
112 *
113 * Function btif_is_dut_mode
114 *
115 * Description checks if BTIF is currently in DUT mode
116 *
117 * Returns true if test mode, otherwise false
118 *
119 ******************************************************************************/
120
btif_is_dut_mode()121 bool btif_is_dut_mode() { return btif_dut_mode == 1; }
122
123 /*******************************************************************************
124 *
125 * Function btif_is_enabled
126 *
127 * Description checks if main adapter is fully enabled
128 *
129 * Returns 1 if fully enabled, otherwize 0
130 *
131 ******************************************************************************/
132
btif_is_enabled(void)133 int btif_is_enabled(void) {
134 return (!btif_is_dut_mode()) && (stack_manager_get_interface()->get_stack_is_running());
135 }
136
btif_init_ok()137 void btif_init_ok() { btif_dm_load_ble_local_keys(); }
138
139 /*******************************************************************************
140 *
141 * Function btif_init_bluetooth
142 *
143 * Description Creates BTIF task and prepares BT scheduler for startup
144 *
145 * Returns bt_status_t
146 *
147 ******************************************************************************/
btif_init_bluetooth()148 bt_status_t btif_init_bluetooth() {
149 log::info("entered");
150 exit_manager = new base::AtExitManager();
151 jni_thread_startup();
152 GetInterfaceToProfiles()->events->invoke_thread_evt_cb(ASSOCIATE_JVM);
153 log::info("finished");
154 return BT_STATUS_SUCCESS;
155 }
156
157 /*******************************************************************************
158 *
159 * Function btif_enable_bluetooth_evt
160 *
161 * Description Event indicating bluetooth enable is completed
162 * Notifies HAL user with updated adapter state
163 *
164 * Returns void
165 *
166 ******************************************************************************/
167
btif_enable_bluetooth_evt()168 void btif_enable_bluetooth_evt() {
169 /* Fetch the local BD ADDR */
170 RawAddress local_bd_addr =
171 bluetooth::ToRawAddress(bluetooth::shim::GetController()->GetMacAddress());
172
173 std::string bdstr = local_bd_addr.ToString();
174
175 // save bd addr to iot conf file
176 device_iot_config_set_str(IOT_CONF_KEY_SECTION_ADAPTER, IOT_CONF_KEY_ADDRESS, bdstr);
177
178 char val[PROPERTY_VALUE_MAX] = "";
179 int val_size = PROPERTY_VALUE_MAX;
180 if (!btif_config_get_str(BTIF_STORAGE_SECTION_ADAPTER, BTIF_STORAGE_KEY_ADDRESS, val,
181 &val_size) ||
182 strcmp(bdstr.c_str(), val) != 0) {
183 // We failed to get an address or the one in the config file does not match
184 // the address given by the controller interface. Update the config cache
185 log::info("Storing '{}' into the config file", local_bd_addr);
186 btif_config_set_str(BTIF_STORAGE_SECTION_ADAPTER, BTIF_STORAGE_KEY_ADDRESS, bdstr.c_str());
187
188 // fire HAL callback for property change
189 bt_property_t prop;
190 prop.type = BT_PROPERTY_BDADDR;
191 prop.val = (void*)&local_bd_addr;
192 prop.len = sizeof(RawAddress);
193 GetInterfaceToProfiles()->events->invoke_adapter_properties_cb(BT_STATUS_SUCCESS, 1, &prop);
194 }
195
196 /* callback to HAL */
197 uid_set = uid_set_create();
198
199 btif_dm_init(uid_set);
200
201 /* init rfcomm & l2cap api */
202 btif_sock_init(uid_set);
203
204 GetInterfaceToProfiles()->onBluetoothEnabled();
205
206 tSDP_DI_RECORD record = {
207 .vendor = uint16_t(android::sysprop::bluetooth::DeviceIDProperties::vendor_id().value_or(
208 LMP_COMPID_GOOGLE)),
209 .vendor_id_source = uint16_t(
210 android::sysprop::bluetooth::DeviceIDProperties::vendor_id_source().value_or(
211 DI_VENDOR_ID_SOURCE_BTSIG)),
212 .product = uint16_t(
213 android::sysprop::bluetooth::DeviceIDProperties::product_id().value_or(0)),
214 .primary_record = true,
215 };
216
217 uint32_t record_handle;
218 tBTA_STATUS status = BTA_DmSetLocalDiRecord(&record, &record_handle);
219 if (status != BTA_SUCCESS) {
220 log::error("unable to set device ID record error {}.", bta_status_text(status));
221 }
222
223 btif_dm_load_local_oob();
224
225 future_ready(stack_manager_get_hack_future(), FUTURE_SUCCESS);
226 log::info("Bluetooth enable event completed");
227 }
228
229 /*******************************************************************************
230 *
231 * Function btif_cleanup_bluetooth
232 *
233 * Description Cleanup BTIF state.
234 *
235 * Returns void
236 *
237 ******************************************************************************/
238
btif_cleanup_bluetooth()239 bt_status_t btif_cleanup_bluetooth() {
240 log::info("entered");
241 btif_dm_cleanup();
242 GetInterfaceToProfiles()->events->invoke_thread_evt_cb(DISASSOCIATE_JVM);
243 btif_queue_release();
244 jni_thread_shutdown();
245 delete exit_manager;
246 exit_manager = nullptr;
247 btif_dut_mode = 0;
248 log::info("finished");
249 return BT_STATUS_SUCCESS;
250 }
251
252 /*******************************************************************************
253 *
254 * Function btif_dut_mode_configure
255 *
256 * Description Configure Test Mode - 'enable' to 1 puts the device in test
257 * mode and 0 exits test mode
258 *
259 ******************************************************************************/
btif_dut_mode_configure(uint8_t enable)260 void btif_dut_mode_configure(uint8_t enable) {
261 log::verbose("");
262
263 btif_dut_mode = enable;
264 if (enable == 1) {
265 BTA_EnableTestMode();
266 } else {
267 // Can't do in process reset anyways - just quit
268 kill(getpid(), SIGKILL);
269 }
270 }
271
272 /*******************************************************************************
273 *
274 * Function btif_dut_mode_send
275 *
276 * Description Sends a HCI Vendor specific command to the controller
277 *
278 ******************************************************************************/
btif_dut_mode_send(uint16_t opcode,uint8_t * buf,uint8_t len)279 void btif_dut_mode_send(uint16_t opcode, uint8_t* buf, uint8_t len) {
280 log::verbose("");
281 /* For now nothing to be done. */
282 get_btm_client_interface().vendor.BTM_VendorSpecificCommand(opcode, len, buf,
283 [](tBTM_VSC_CMPL*) {});
284 }
285
286 /*****************************************************************************
287 *
288 * btif api adapter property functions
289 *
290 ****************************************************************************/
291
btif_in_get_adapter_properties(void)292 static bt_status_t btif_in_get_adapter_properties(void) {
293 const static uint32_t NUM_ADAPTER_PROPERTIES = 5;
294 bt_property_t properties[NUM_ADAPTER_PROPERTIES];
295 uint32_t num_props = 0;
296
297 RawAddress addr;
298 bt_bdname_t name;
299 bt_scan_mode_t mode;
300 uint32_t disc_timeout;
301 RawAddress bonded_devices[BTM_SEC_MAX_DEVICE_RECORDS];
302 Uuid local_uuids[BT_MAX_NUM_UUIDS];
303 bt_status_t status;
304
305 /* RawAddress */
306 BTIF_STORAGE_FILL_PROPERTY(&properties[num_props], BT_PROPERTY_BDADDR, sizeof(addr), &addr);
307 status = btif_storage_get_adapter_property(&properties[num_props]);
308 // Add BT_PROPERTY_BDADDR property into list only when successful.
309 // Otherwise, skip this property entry.
310 if (status == BT_STATUS_SUCCESS) {
311 num_props++;
312 }
313
314 /* BD_NAME */
315 BTIF_STORAGE_FILL_PROPERTY(&properties[num_props], BT_PROPERTY_BDNAME, sizeof(name), &name);
316 btif_storage_get_adapter_property(&properties[num_props]);
317 num_props++;
318
319 /* DISC_TIMEOUT */
320 BTIF_STORAGE_FILL_PROPERTY(&properties[num_props], BT_PROPERTY_ADAPTER_DISCOVERABLE_TIMEOUT,
321 sizeof(disc_timeout), &disc_timeout);
322 btif_storage_get_adapter_property(&properties[num_props]);
323 num_props++;
324
325 /* BONDED_DEVICES */
326 BTIF_STORAGE_FILL_PROPERTY(&properties[num_props], BT_PROPERTY_ADAPTER_BONDED_DEVICES,
327 sizeof(bonded_devices), bonded_devices);
328 btif_storage_get_adapter_property(&properties[num_props]);
329 num_props++;
330
331 /* LOCAL UUIDs */
332 BTIF_STORAGE_FILL_PROPERTY(&properties[num_props], BT_PROPERTY_UUIDS, sizeof(local_uuids),
333 local_uuids);
334 btif_storage_get_adapter_property(&properties[num_props]);
335 num_props++;
336
337 GetInterfaceToProfiles()->events->invoke_adapter_properties_cb(BT_STATUS_SUCCESS, num_props,
338 properties);
339 return BT_STATUS_SUCCESS;
340 }
341
btif_in_get_remote_device_properties(RawAddress * bd_addr)342 static bt_status_t btif_in_get_remote_device_properties(RawAddress* bd_addr) {
343 bt_property_t remote_properties[8];
344 uint32_t num_props = 0;
345
346 bt_bdname_t name, alias;
347 uint32_t cod, devtype;
348 Uuid remote_uuids[BT_MAX_NUM_UUIDS];
349
350 memset(remote_properties, 0, sizeof(remote_properties));
351 BTIF_STORAGE_FILL_PROPERTY(&remote_properties[num_props], BT_PROPERTY_BDNAME, sizeof(name),
352 &name);
353 btif_storage_get_remote_device_property(bd_addr, &remote_properties[num_props]);
354 num_props++;
355
356 BTIF_STORAGE_FILL_PROPERTY(&remote_properties[num_props], BT_PROPERTY_REMOTE_FRIENDLY_NAME,
357 sizeof(alias), &alias);
358 btif_storage_get_remote_device_property(bd_addr, &remote_properties[num_props]);
359 num_props++;
360
361 BTIF_STORAGE_FILL_PROPERTY(&remote_properties[num_props], BT_PROPERTY_CLASS_OF_DEVICE,
362 sizeof(cod), &cod);
363 btif_storage_get_remote_device_property(bd_addr, &remote_properties[num_props]);
364 num_props++;
365
366 BTIF_STORAGE_FILL_PROPERTY(&remote_properties[num_props], BT_PROPERTY_TYPE_OF_DEVICE,
367 sizeof(devtype), &devtype);
368 btif_storage_get_remote_device_property(bd_addr, &remote_properties[num_props]);
369 num_props++;
370
371 BTIF_STORAGE_FILL_PROPERTY(&remote_properties[num_props], BT_PROPERTY_UUIDS, sizeof(remote_uuids),
372 remote_uuids);
373 btif_storage_get_remote_device_property(bd_addr, &remote_properties[num_props]);
374 num_props++;
375
376 GetInterfaceToProfiles()->events->invoke_remote_device_properties_cb(
377 BT_STATUS_SUCCESS, *bd_addr, num_props, remote_properties);
378
379 return BT_STATUS_SUCCESS;
380 }
381
btif_core_storage_adapter_write(bt_property_t * prop)382 static void btif_core_storage_adapter_write(bt_property_t* prop) {
383 log::verbose("type: {}, len {}, {}", prop->type, prop->len, std::format_ptr(prop->val));
384 bt_status_t status = btif_storage_set_adapter_property(prop);
385 GetInterfaceToProfiles()->events->invoke_adapter_properties_cb(status, 1, prop);
386 }
387
btif_adapter_properties_evt(bt_status_t status,uint32_t num_props,bt_property_t * p_props)388 void btif_adapter_properties_evt(bt_status_t status, uint32_t num_props, bt_property_t* p_props) {
389 GetInterfaceToProfiles()->events->invoke_adapter_properties_cb(status, num_props, p_props);
390 }
btif_remote_properties_evt(bt_status_t status,RawAddress * remote_addr,uint32_t num_props,bt_property_t * p_props)391 void btif_remote_properties_evt(bt_status_t status, RawAddress* remote_addr, uint32_t num_props,
392 bt_property_t* p_props) {
393 GetInterfaceToProfiles()->events->invoke_remote_device_properties_cb(status, *remote_addr,
394 num_props, p_props);
395 }
396
397 /*******************************************************************************
398 *
399 * Function btif_get_adapter_properties
400 *
401 * Description Fetch all available properties (local & remote)
402 *
403 ******************************************************************************/
404
btif_get_adapter_properties(void)405 void btif_get_adapter_properties(void) {
406 log::verbose("");
407
408 btif_in_get_adapter_properties();
409 }
410
411 /*******************************************************************************
412 *
413 * Function btif_get_adapter_property
414 *
415 * Description Fetches property value from local cache
416 *
417 ******************************************************************************/
418
btif_get_adapter_property(bt_property_type_t type)419 void btif_get_adapter_property(bt_property_type_t type) {
420 log::verbose("{}", type);
421
422 bt_status_t status = BT_STATUS_SUCCESS;
423 char buf[512];
424 bt_property_t prop;
425 prop.type = type;
426 prop.val = (void*)buf;
427 prop.len = sizeof(buf);
428 if (prop.type == BT_PROPERTY_LOCAL_LE_FEATURES) {
429 tBTM_BLE_VSC_CB cmn_vsc_cb;
430 bt_local_le_features_t local_le_features;
431
432 /* LE features are not stored in storage. Should be retrieved from stack
433 */
434 BTM_BleGetVendorCapabilities(&cmn_vsc_cb);
435 local_le_features.local_privacy_enabled = BTM_BleLocalPrivacyEnabled();
436
437 prop.len = sizeof(bt_local_le_features_t);
438 if (cmn_vsc_cb.filter_support == 1) {
439 local_le_features.max_adv_filter_supported = cmn_vsc_cb.max_filter;
440 } else {
441 local_le_features.max_adv_filter_supported = 0;
442 }
443 local_le_features.max_adv_instance = cmn_vsc_cb.adv_inst_max;
444 local_le_features.max_irk_list_size = cmn_vsc_cb.max_irk_list_sz;
445 local_le_features.rpa_offload_supported = cmn_vsc_cb.rpa_offloading;
446 local_le_features.scan_result_storage_size = cmn_vsc_cb.tot_scan_results_strg;
447 local_le_features.activity_energy_info_supported = cmn_vsc_cb.energy_support;
448 local_le_features.version_supported = cmn_vsc_cb.version_supported;
449 local_le_features.total_trackable_advertisers = cmn_vsc_cb.total_trackable_advertisers;
450
451 local_le_features.extended_scan_support = cmn_vsc_cb.extended_scan_support > 0;
452 local_le_features.debug_logging_supported = cmn_vsc_cb.debug_logging_supported > 0;
453 auto controller = bluetooth::shim::GetController();
454
455 if (controller->SupportsBleExtendedAdvertising()) {
456 local_le_features.max_adv_instance = controller->GetLeNumberOfSupportedAdverisingSets();
457 }
458 local_le_features.le_2m_phy_supported = controller->SupportsBle2mPhy();
459 local_le_features.le_coded_phy_supported = controller->SupportsBleCodedPhy();
460 local_le_features.le_extended_advertising_supported =
461 controller->SupportsBleExtendedAdvertising();
462 local_le_features.le_periodic_advertising_supported =
463 controller->SupportsBlePeriodicAdvertising();
464 local_le_features.le_maximum_advertising_data_length =
465 controller->GetLeMaximumAdvertisingDataLength();
466
467 local_le_features.dynamic_audio_buffer_supported = cmn_vsc_cb.dynamic_audio_buffer_support;
468
469 local_le_features.le_periodic_advertising_sync_transfer_sender_supported =
470 controller->SupportsBlePeriodicAdvertisingSyncTransferSender();
471 local_le_features.le_connected_isochronous_stream_central_supported =
472 controller->SupportsBleConnectedIsochronousStreamCentral();
473 local_le_features.le_isochronous_broadcast_supported =
474 controller->SupportsBleIsochronousBroadcaster();
475 local_le_features.le_periodic_advertising_sync_transfer_recipient_supported =
476 controller->SupportsBlePeriodicAdvertisingSyncTransferRecipient();
477 local_le_features.adv_filter_extended_features_mask =
478 cmn_vsc_cb.adv_filter_extended_features_mask;
479 local_le_features.le_channel_sounding_supported = controller->SupportsBleChannelSounding();
480
481 memcpy(prop.val, &local_le_features, prop.len);
482 } else if (prop.type == BT_PROPERTY_DYNAMIC_AUDIO_BUFFER) {
483 tBTM_BLE_VSC_CB cmn_vsc_cb;
484 bt_dynamic_audio_buffer_item_t dynamic_audio_buffer_item;
485
486 BTM_BleGetVendorCapabilities(&cmn_vsc_cb);
487
488 prop.len = sizeof(bt_dynamic_audio_buffer_item_t);
489 if (GetInterfaceToProfiles()->config->isA2DPOffloadEnabled() == false) {
490 log::verbose("Get buffer millis for A2DP software encoding");
491 for (int i = 0; i < CODEC_TYPE_NUMBER; i++) {
492 dynamic_audio_buffer_item.dab_item[i] = {.default_buffer_time = DEFAULT_BUFFER_TIME,
493 .maximum_buffer_time = MAXIMUM_BUFFER_TIME,
494 .minimum_buffer_time = MINIMUM_BUFFER_TIME};
495 }
496 memcpy(prop.val, &dynamic_audio_buffer_item, prop.len);
497 } else {
498 if (cmn_vsc_cb.dynamic_audio_buffer_support != 0) {
499 log::verbose("Get buffer millis for A2DP Offload");
500 tBTM_BT_DYNAMIC_AUDIO_BUFFER_CB bt_dynamic_audio_buffer_cb[CODEC_TYPE_NUMBER];
501 BTM_BleGetDynamicAudioBuffer(bt_dynamic_audio_buffer_cb);
502
503 for (int i = 0; i < CODEC_TYPE_NUMBER; i++) {
504 dynamic_audio_buffer_item.dab_item[i] = {
505 .default_buffer_time = bt_dynamic_audio_buffer_cb[i].default_buffer_time,
506 .maximum_buffer_time = bt_dynamic_audio_buffer_cb[i].maximum_buffer_time,
507 .minimum_buffer_time = bt_dynamic_audio_buffer_cb[i].minimum_buffer_time};
508 }
509 memcpy(prop.val, &dynamic_audio_buffer_item, prop.len);
510 } else {
511 log::verbose("Don't support Dynamic Audio Buffer");
512 }
513 }
514 } else if (prop.type == BT_PROPERTY_LPP_OFFLOAD_FEATURES) {
515 bt_lpp_offload_features_t lpp_offload_features;
516 hal::SocketCapabilities socket_offload_capabilities =
517 bluetooth::shim::GetLppOffloadManager()->GetSocketCapabilities();
518 lpp_offload_features.number_of_supported_offloaded_le_coc_sockets =
519 socket_offload_capabilities.le_coc_capabilities.number_of_supported_sockets;
520 prop.len = sizeof(bt_lpp_offload_features_t);
521 memcpy(prop.val, &lpp_offload_features, prop.len);
522 } else {
523 status = btif_storage_get_adapter_property(&prop);
524 }
525 GetInterfaceToProfiles()->events->invoke_adapter_properties_cb(status, 1, &prop);
526 }
527
property_deep_copy(const bt_property_t * prop)528 bt_property_t* property_deep_copy(const bt_property_t* prop) {
529 bt_property_t* copy = (bt_property_t*)osi_calloc(sizeof(bt_property_t) + prop->len);
530 copy->type = prop->type;
531 copy->len = prop->len;
532 copy->val = (uint8_t*)(copy + 1);
533 memcpy(copy->val, prop->val, prop->len);
534 return copy;
535 }
536
537 /*******************************************************************************
538 *
539 * Function btif_set_scan_mode
540 *
541 * Description Updates core stack scan mode
542 *
543 ******************************************************************************/
544
btif_set_scan_mode(bt_scan_mode_t mode)545 void btif_set_scan_mode(bt_scan_mode_t mode) {
546 log::info("set scan mode : {:x}", mode);
547
548 BTA_DmSetVisibility(mode);
549 }
550
551 /*******************************************************************************
552 *
553 * Function btif_set_adapter_property
554 *
555 * Description Updates core stack with property value and stores it in
556 * local cache
557 *
558 ******************************************************************************/
559
btif_set_adapter_property(bt_property_t * property)560 void btif_set_adapter_property(bt_property_t* property) {
561 log::verbose("btif_set_adapter_property type: {}, len {}, {}", property->type, property->len,
562 std::format_ptr(property->val));
563
564 switch (property->type) {
565 case BT_PROPERTY_BDNAME: {
566 char bd_name[BD_NAME_LEN + 1];
567 uint16_t name_len = property->len > BD_NAME_LEN ? BD_NAME_LEN : property->len;
568 memcpy(bd_name, property->val, name_len);
569 bd_name[name_len] = '\0';
570
571 log::verbose("set property name : {}", (char*)bd_name);
572
573 BTA_DmSetDeviceName((const char*)bd_name);
574
575 btif_core_storage_adapter_write(property);
576 } break;
577
578 case BT_PROPERTY_ADAPTER_DISCOVERABLE_TIMEOUT: {
579 /* Nothing to do beside store the value in NV. Java
580 will change the SCAN_MODE property after setting timeout,
581 if required */
582 btif_core_storage_adapter_write(property);
583 } break;
584 default:
585 break;
586 }
587 }
588
589 /*******************************************************************************
590 *
591 * Function btif_get_remote_device_property
592 *
593 * Description Fetches the remote device property from the NVRAM
594 *
595 ******************************************************************************/
btif_get_remote_device_property(RawAddress remote_addr,bt_property_type_t type)596 void btif_get_remote_device_property(RawAddress remote_addr, bt_property_type_t type) {
597 char buf[1024];
598 bt_property_t prop;
599 prop.type = type;
600 prop.val = (void*)buf;
601 prop.len = sizeof(buf);
602
603 bt_status_t status = btif_storage_get_remote_device_property(&remote_addr, &prop);
604 GetInterfaceToProfiles()->events->invoke_remote_device_properties_cb(status, remote_addr, 1,
605 &prop);
606 }
607
608 /*******************************************************************************
609 *
610 * Function btif_get_remote_device_properties
611 *
612 * Description Fetches all the remote device properties from NVRAM
613 *
614 ******************************************************************************/
btif_get_remote_device_properties(RawAddress remote_addr)615 void btif_get_remote_device_properties(RawAddress remote_addr) {
616 btif_in_get_remote_device_properties(&remote_addr);
617 }
618
619 /*******************************************************************************
620 *
621 * Function btif_set_remote_device_property
622 *
623 * Description Writes the remote device property to NVRAM.
624 * Currently, BT_PROPERTY_REMOTE_FRIENDLY_NAME is the only
625 * remote device property that can be set
626 *
627 ******************************************************************************/
btif_set_remote_device_property(RawAddress * remote_addr,bt_property_t * property)628 void btif_set_remote_device_property(RawAddress* remote_addr, bt_property_t* property) {
629 btif_storage_set_remote_device_property(remote_addr, property);
630 }
631
632 /*******************************************************************************
633 *
634 * Function btif_get_enabled_services_mask
635 *
636 * Description Fetches currently enabled services
637 *
638 * Returns tBTA_SERVICE_MASK
639 *
640 ******************************************************************************/
641
btif_get_enabled_services_mask(void)642 tBTA_SERVICE_MASK btif_get_enabled_services_mask(void) { return btif_enabled_services; }
643
644 /*******************************************************************************
645 *
646 * Function btif_enable_service
647 *
648 * Description Enables the service 'service_ID' to the service_mask.
649 * Upon BT enable, BTIF core shall invoke the BTA APIs to
650 * enable the profiles
651 *
652 ******************************************************************************/
btif_enable_service(tBTA_SERVICE_ID service_id)653 void btif_enable_service(tBTA_SERVICE_ID service_id) {
654 btif_enabled_services |= (1 << service_id);
655
656 log::verbose("current services:0x{:x}", btif_enabled_services);
657
658 if (btif_is_enabled()) {
659 btif_dm_enable_service(service_id, true);
660 }
661 }
662 /*******************************************************************************
663 *
664 * Function btif_disable_service
665 *
666 * Description Disables the service 'service_ID' to the service_mask.
667 * Upon BT disable, BTIF core shall invoke the BTA APIs to
668 * disable the profiles
669 *
670 ******************************************************************************/
btif_disable_service(tBTA_SERVICE_ID service_id)671 void btif_disable_service(tBTA_SERVICE_ID service_id) {
672 btif_enabled_services &= (tBTA_SERVICE_MASK)(~(1 << service_id));
673
674 log::verbose("Current Services:0x{:x}", btif_enabled_services);
675
676 if (btif_is_enabled()) {
677 btif_dm_enable_service(service_id, false);
678 }
679 }
680
btif_set_dynamic_audio_buffer_size(int,int size)681 bt_status_t btif_set_dynamic_audio_buffer_size(int /* codec */, int size) {
682 log::verbose("");
683
684 tBTM_BLE_VSC_CB cmn_vsc_cb;
685 BTM_BleGetVendorCapabilities(&cmn_vsc_cb);
686
687 if (!GetInterfaceToProfiles()->config->isA2DPOffloadEnabled()) {
688 log::verbose("Set buffer size ({}) for A2DP software encoding", size);
689 GetInterfaceToProfiles()->profileSpecific_HACK->btif_av_set_dynamic_audio_buffer_size(
690 uint8_t(size));
691 } else {
692 if (cmn_vsc_cb.dynamic_audio_buffer_support != 0) {
693 log::verbose("Set buffer size ({}) for A2DP offload", size);
694 uint16_t firmware_tx_buffer_length_byte;
695 firmware_tx_buffer_length_byte = static_cast<uint16_t>(size);
696 log::info("firmware_tx_buffer_length_byte: {}", firmware_tx_buffer_length_byte);
697 bluetooth::shim::GetController()->SetDabAudioBufferTime(firmware_tx_buffer_length_byte);
698 }
699 }
700
701 return BT_STATUS_SUCCESS;
702 }
703