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