1. Bluetooth architecture
Android4.2 之后采用 bluedroid 作为协议;整体由 bluetooth.apk,bluedroid ,libbt-vendor 三个 部 分 组 成 。
- bluetooth.apk : As a Bluetooth service process, obtain the bt_interface_t control interface provided by bluetooth*.default.so through the libblueooth_jni layer and register the status and data callback to bluetooth*.default.so; complete the control and data reception of the Bluetooth module;
- libbluetooth : Mainly the specific implementation of bluedroid's Bluetooth protocol stack. It calls the libbt-hci interface downward to load **libbt-vendor; ** upwards provides the callback of libblueooth_jni;
- **libbt-vendor * Completes the hardware initialization and control of the Bluetooth module (physical communication interface initialization, firmware download, power supply control). The RK platform determines the type of the wifi+Bt module and selects and loads different adapter manufacturer protocol stacks at the libbuetooth_jni layer; and In libbt-hci, load libbt-vendor .so of modules from different manufacturers according to the module type to complete the dynamic adaptation module.
The RK platform determines the type of the module and selects and loads protocol stacks of different adapter manufacturers in the libbuetooth_jni layer; and loads libbt-vendor*.so of modules from different manufacturers in libbt-hci according to the module type to complete the dynamic adaptation module.
2. Debugging of USB Bluetooth module
The option here is a USB Bluetooth adapter of RTL8723DU. The following will look at the adaptation process step by step starting from the kernel;
2.1 kernel part
First, copy the Bluetooth driver files rtk_btusb.h and rtk_btusb.c provided by Realtek to the kernel's drivers/bluetooth/ directory:
kernel-5.10/drivers/bluetooth/rtk_btusb.c
kernel-5.10/drivers/bluetooth/rtk_btusb.h
Modify the files "Kconfig" and "Makefile" in the kernel's drivers/bluetooth/ directory.
config BT_RTKBTUSB
tristate "RTK HCI USB driver"
depends on USB
help
RTK Bluetooth HCI USB driver
obj-$(CONFIG_BT_RTKBTUSB) += rtk_btusb.o
Add the macro definitions supported by Realtek BT Chip to the defconfig file corresponding to the platform. Here is the dten_ribeye_me_pro.config file.
diff --git a/kernel-5.10/arch/arm64/configs/dten_ribeye_me_pro.config b/kernel-5.10/arch/arm64/configs/dten_ribeye_me_pro.config
index 54246f39..6894e782 100755
--- a/kernel-5.10/arch/arm64/configs/dten_ribeye_me_pro.config
+++ b/kernel-5.10/arch/arm64/configs/dten_ribeye_me_pro.config
@@ -34,3 +34,5 @@ CONFIG_RTC_DRV_AT8563=y
#DTEN Codec AD82088
CONFIG_SND_SOC_AD82088=y
CONFIG_BACKLIGHT_PWM=n
+# Add for usb bluetooth adapter
+CONFIG_BT_RTKBTUSB=y
2.2 Add device nodes and permissions
device/rockchip/common directory: Add some node permissions and some macro switches
in the device/rockchip/common/init.connectivity.rc file (general rk platform code has been added):
cfliu@sw-eng:RKOverlay$ gd device/rockchip/common/init.connectivity.rc
diff --git a/device/rockchip/common/init.connectivity.rc b/device/rockchip/common/init.connectivity.rc
index 82603ec0..db2e7176 100755
--- a/device/rockchip/common/init.connectivity.rc
+++ b/device/rockchip/common/init.connectivity.rc
@@ -8,6 +8,8 @@ on zygote-start
chown bluetooth net_bt /proc/bluetooth/sleep/lpm
chown bluetooth net_bt /proc/bluetooth/sleep/btwrite
+ # USB device
+ insmod /system/lib/modules/rtk_btusb.ko
chmod 0660 /dev/rtk_btusb
chown bluetooth net_bt /dev/rtk_btusb
chmod 0660 /dev/rtkbt_dev
In the device/rockchip/common/wifi_bt_common.mk file (the general rk platform code has been added), this first turns off the BCM chip to prevent the impact of BCM.
diff --git a/device/rockchip/common/wifi_bt_common.mk b/device/rockchip/common/wifi_bt_common.mk
index 23149e85..af3c7017 100644
--- a/device/rockchip/common/wifi_bt_common.mk
+++ b/device/rockchip/common/wifi_bt_common.mk
@@ -53,7 +53,7 @@ WIFI_DRIVER_FW_PATH_STA := "/vendor/etc/firmware/fw_bcm4329.bin"
WIFI_DRIVER_FW_PATH_P2P := "/vendor/etc/firmware/fw_bcm4329_p2p.bin"
WIFI_DRIVER_FW_PATH_AP := "/vendor/etc/firmware/fw_bcm4329_apsta.bin"
BOARD_HAVE_BLUETOOTH := true
-BOARD_HAVE_BLUETOOTH_BCM := true
+BOARD_HAVE_BLUETOOTH_BCM := false
BOARD_HAVE_BLUETOOTH_RTK := true
BOARD_BLUETOOTH_BDROID_BUILDCFG_INCLUDE_DIR ?= device/rockchip/$(TARGET_BOARD_PLATFORM)/bluetooth
endif
2.3 Configuration of libbt-vendor.so
In the hardware/realtek directory: HAL layer, add the code of the Bluetooth protocol stack and the corresponding source code of libbt-vendor.so. libbt-vendor completes the hardware initialization and control of the Bluetooth module (physical communication interface initialization, firmware download, power supply control)
@sw-eng:RK3588S$ cd hardware/realtek/rtkbt/vendor/firmware
@sw-eng:firmware$
@sw-eng:firmware$ ls
BT_Firmware.mk rtl8723a_fw rtl8723c_fw rtl8723f_config rtl8761at_config rtl8761cs_fw rtl8822bs_config
fw_info.txt rtl8723as_config rtl8723cs_cg_config rtl8723f_fw rtl8761at_fw rtl8821a_config rtl8822bs_fw
readme.txt rtl8723as_fw rtl8723cs_cg_fw rtl8723fs_config rtl8761au8192ee_fw rtl8821a_fw rtl8822c_config
rtl8703as_config rtl8723b_config rtl8723cs_vf_config rtl8723fs_fw rtl8761au8812ae_fw rtl8821as_config rtl8822c_fw
rtl8703as_fw rtl8723b_config_2Ant_S0 rtl8723cs_vf_fw rtl8725a_config rtl8761au_fw rtl8821as_fw rtl8822cs_config
rtl8703bs_config rtl8723b_fw rtl8723cs_xx_config rtl8725a_fw rtl8761aw8192eu_config rtl8821c_config rtl8822cs_fw
rtl8703bs_fw rtl8723bs_config rtl8723cs_xx_fw rtl8725as_config rtl8761aw8192eu_fw rtl8821c_fw rtl8852as_config
rtl8703cs_config rtl8723bs_fw rtl8723d_config rtl8725as_fw rtl8761b_config rtl8821cs_config rtl8852as_fw
rtl8703cs_fw rtl8723bs_VQ0_config rtl8723d_fw rtl8761a_config rtl8761b_fw rtl8821cs_fw rtl8852au_config
rtl8723a_config rtl8723bs_VQ0_fw rtl8723ds_config rtl8761at8192ee_fw rtl8761bt_config rtl8822b_config rtl8852au_fw
rtl8723a_config_addr rtl8723bu_config rtl8723ds_fw rtl8761at8812ae_fw rtl8761bt_fw rtl8822b_fw
cfliu@sw-eng:firmware$
Eventually, the mk file hardware/realtek/rtkbt/rtkbt.mk will be copied to the following directory of the machine, and fw will be loaded when Bluetooth is turned on:
ifeq ($(strip $(TARGET_BOARD_PLATFORM_PRODUCT)), tablet)
BT_FIRMWARE_FILES := $(shell ls $(CUR_PATH)/vendor/firmware)
PRODUCT_COPY_FILES += \
$(foreach file, $(BT_FIRMWARE_FILES), $(CUR_PATH)/vendor/firmware/$(file):$(TARGET_COPY_OUT_VENDOR)/etc/firmware/$(file))
The final copy to the device is:/vendor/etc/firmware
pro:/vendor/etc/firmware # ls
4343A0.hcd bcm4329.hcd fw_awnb108_ap.bin fw_bcm43438a0.bin fw_cyw43438.bin nvram_ap6212a.txt rtl8703bs_config rtl8723ds_config rtl8821a_fw
AP6275P_NVRAM_V1.1_20200702.txt bcm43341b0.hcd fw_bcm40181a2.bin fw_bcm43438a0_apsta.bin fw_cyw43455.bin nvram_ap6214.txt rtl8703bs_fw rtl8723ds_fw rtl8821as_config
BCM20710A1.hcd bcm4339a0.hcd fw_bcm40181a2_apsta.bin fw_bcm43438a0_p2p.bin fw_info.txt nvram_ap6214a.txt rtl8703cs_config rtl8723f_config rtl8821as_fw
BCM4330B1.hcd bcm43438a0.hcd fw_bcm40183b2.bin fw_bcm43438a1.bin mali_csffw.bin nvram_ap6216.txt rtl8703cs_fw rtl8723f_fw rtl8821c_config
BCM4343A0.hcd bcm43438a1.hcd fw_bcm40183b2_ag.bin fw_bcm43438a1_apsta.bin nh660.hcd nvram_ap6233f.txt rtl8723a_config rtl8723fs_config rtl8821c_fw
BCM4343A1.hcd bcm4354a1.hcd fw_bcm40183b2_ag_apsta.bin fw_bcm43438a1_p2p.bin nvram.txt nvram_ap6236.txt rtl8723a_config_addr rtl8723fs_fw rtl8821cs_config
BCM4343B0.hcd clm_bcm43752a2_ag.blob fw_bcm40183b2_ag_p2p.bin fw_bcm43455c0_ag.bin nvram_4330.txt nvram_ap6255.txt rtl8723a_fw rtl8725a_config rtl8821cs_fw
BCM4345C0.hcd clm_bcm43752a2_pcie_ag.blob fw_bcm40183b2_apsta.bin fw_bcm43455c0_ag_apsta.bin nvram_4330_oob.txt nvram_ap6256.txt rtl8723as_config rtl8725a_fw rtl8822b_config
BCM4345C5.hcd clm_bcm4375b4_pcie_ag.blob fw_bcm40183b2_p2p.bin fw_bcm43455c0_ag_p2p.bin nvram_AP6181.txt nvram_ap6275hh3.txt rtl8723as_fw rtl8725as_config rtl8822b_fw
BCM4354A2.hcd config.txt fw_bcm43241b4_ag.bin fw_bcm43456c5_ag.bin nvram_AP6210.txt nvram_ap6275s.txt rtl8723b_config rtl8725as_fw rtl8822bs_config
BCM4356A2.hcd esp32 fw_bcm43241b4_ag_apsta.bin fw_bcm43456c5_ag_apsta.bin nvram_AP6210_24M.txt nvram_ap62x2.txt rtl8723b_config_2Ant_S0 rtl8761a_config rtl8822bs_fw
BCM4359C0.hcd fw_RK901.bin fw_bcm43241b4_ag_p2p.bin fw_bcm4354a1_ag.bin nvram_AP6234.txt nvram_ap6354.txt rtl8723b_fw rtl8761at8192ee_fw rtl8822c_config
BCM4362A2.hcd fw_RK901a0.bin fw_bcm4330.bin fw_bcm4354a1_ag_apsta.bin nvram_AP6275P.txt nvram_ap6356.txt rtl8723bs_VQ0_config rtl8761at8812ae_fw rtl8822c_fw
BCM4375B1.hcd fw_RK901a0_apsta.bin fw_bcm4330_apsta.bin fw_bcm4354a1_ag_p2p.bin nvram_AP6330.txt nvram_ap6356s.txt rtl8723bs_VQ0_fw rtl8761at_config rtl8822cs_config
BT_Firmware.mk fw_RK901a2.bin fw_bcm43341b0_ag.bin fw_bcm4356a2_ag.bin nvram_AP6335.txt nvram_ap6398s.txt rtl8723bs_config rtl8761at_fw rtl8822cs_fw
RT2870AP.dat fw_RK901a2_apsta.bin fw_bcm43341b0_ag_apsta.bin fw_bcm4356a2_ag_apsta.bin nvram_AP6441.txt nvram_ap6398sa.txt rtl8723bs_fw rtl8761au8192ee_fw rtl8852as_config
RT2870APCard.dat fw_RK901a2_p2p.bin fw_bcm43341b0_ag_p2p.bin fw_bcm4356a2_ag_p2p.bin nvram_AP6476.txt nvram_ap6452.txt rtl8723bu_config rtl8761au8812ae_fw rtl8852as_fw
RT2870STA.dat fw_RK903.bin fw_bcm4334b1_ag.bin fw_bcm4359c0_ag.bin nvram_AP6493.txt nvram_awnb108.txt rtl8723c_fw rtl8761au_fw rtl8852au_config
RT2870STACard.dat fw_RK903_ag.bin fw_bcm4334b1_ag_apsta.bin fw_bcm4359c0_ag_apsta.bin nvram_B23.txt nvram_azw256.txt rtl8723cs_cg_config rtl8761aw8192eu_config rtl8852au_fw
awnb108.hcd fw_RK903_ag_apsta.bin fw_bcm4334b1_ag_p2p.bin fw_bcm4359c0_ag_p2p.bin nvram_GB86302I.txt nvram_azw372.txt rtl8723cs_cg_fw rtl8761aw8192eu_fw ssv6051-sw.bin
bcm20710a1.hcd fw_RK903_ag_p2p.bin fw_bcm4339a0_ag.bin fw_bcm43752a2_ag.bin nvram_RK901.txt otp.bin.z77 rtl8723cs_vf_config rtl8761b_config ssv6051-wifi.cfg
bcm20710a1_24M.hcd fw_RK903_p2p.bin fw_bcm4339a0_ag_apsta.bin fw_bcm43752a2_ag_apsta.bin nvram_RK903.cal readme.txt rtl8723cs_vf_fw rtl8761b_fw wifi_efuse_8189e.map
bcm20710a1_26M.hcd fw_RK903b2.bin fw_bcm4339a0_ag_p2p.bin fw_bcm43752a2_pcie_ag.bin nvram_RK903.txt rk903.hcd rtl8723cs_xx_config rtl8761bt_config wifi_efuse_8723bs-vq0.map
bcm2076b1.hcd fw_RK903b2_apsta.bin fw_bcm43436b0.bin fw_bcm43752a2_pcie_ag_apsta.bin nvram_RK903_26M.cal rk903_26M.hcd rtl8723cs_xx_fw rtl8761bt_fw wifi_efuse_8723cs.map
bcm40183b2.hcd fw_RK903b2_p2p.bin fw_bcm43436b0_apsta.bin fw_bcm4375b4_pcie_ag.bin nvram_WL211.txt rtl8703as_config rtl8723d_config rtl8761cs_fw wifi_efuse_8723ds.map
bcm43241b4.hcd fw_awnb108.bin fw_bcm43436b0_p2p.bin fw_bcm4375b4_pcie_ag_apsta.bin nvram_ap6212.txt rtl8703as_fw rtl8723d_fw rtl8821a_config wifi_efuse_8821cs.map
2.4 Select a specific vendor library based on type
A brief analysis of the source code of libbt-vendor.so corresponding to some HAL layers:
First, the bluedroid Bluetooth protocol stack libbt-hci [native Bluetooth protocol stack] calls interfaces/ through the hci_initialize function of system/bt/hci/src/hci_layer_android.cc The initialize function in bluetooth/1.0/default/vendor_interface.cc then calls the open function to open the corresponding so library.
//interfaces/bluetooth/1.0/default/vendor_interface.cc
static const char* VENDOR_LIBRARY_NAME = "libbt-vendor.so";
static const char* VENDOR_REALTEK_LIBRARY_NAME = "libbt-vendor-realtek.so";
static char wifi_type[64] = {
0};
extern "C" int check_wifi_chip_type_string(char *type);
bool VendorInterface::Open(InitializeCompleteCallback initialize_complete_cb,
PacketReadCallback event_cb,
PacketReadCallback acl_cb,
PacketReadCallback sco_cb,
PacketReadCallback iso_cb) {
initialize_complete_cb_ = initialize_complete_cb;
char vendor_lib_name[32];
// Initialize vendor interface
if (wifi_type[0] == 0)
//获取当前wifi芯片的type加载不同的so库,目前这个接口有权限问题,待进一步分析
check_wifi_chip_type_string(wifi_type);
if ((0 == strncmp(wifi_type, "RTL", 3)) ||
(0 == strncmp(wifi_type, "SSV", 3))) {
// for ssv6051 wifi + rtl8761 bt
//此时加载的是libbt-vendor-realtek.so
strcpy(vendor_lib_name, VENDOR_REALTEK_LIBRARY_NAME);
} else {
//此时加载的是libbt-vendor.so
strcpy(vendor_lib_name, VENDOR_LIBRARY_NAME);
}
ALOGD("%s: %s", __func__, vendor_lib_name);
lib_handle_ = dlopen(vendor_lib_name, RTLD_NOW);
if (!lib_handle_) {
ALOGE("%s unable to open %s (%s)", __func__, vendor_lib_name,
dlerror());
return false;
}
lib_interface_ = reinterpret_cast<bt_vendor_interface_t*>(
dlsym(lib_handle_, VENDOR_LIBRARY_SYMBOL_NAME));
if (!lib_interface_) {
ALOGE("%s unable to find symbol %s in %s (%s)", __func__,
VENDOR_LIBRARY_SYMBOL_NAME, vendor_lib_name, dlerror());
return false;
}
// Get the local BD address
uint8_t local_bda[BluetoothAddress::kBytes];
//获取本地的蓝牙地址
if (!BluetoothAddress::get_local_address(local_bda)) {
LOG_ALWAYS_FATAL("%s: No Bluetooth Address!", __func__);
}
//接口的初始化
int status = lib_interface_->init(&lib_callbacks, (unsigned char*)local_bda);
if (status) {
ALOGE("%s unable to initialize vendor library: %d", __func__, status);
return false;
}
ALOGD("%s vendor library loaded", __func__);
// Power on the controller
int power_state = BT_VND_PWR_ON;
lib_interface_->op(BT_VND_OP_POWER_CTRL, &power_state);
// Get the UART socket(s)
int fd_list[CH_MAX] = {
0};
int fd_count = lib_interface_->op(BT_VND_OP_USERIAL_OPEN, &fd_list);
if (fd_count < 1 || fd_count > CH_MAX - 1) {
ALOGE("%s: fd_count %d is invalid!", __func__, fd_count);
return false;
}
for (int i = 0; i < fd_count; i++) {
if (fd_list[i] == INVALID_FD) {
ALOGE("%s: fd %d is invalid!", __func__, fd_list[i]);
return false;
}
}
event_cb_ = event_cb;
PacketReadCallback intercept_events = [this](const hidl_vec<uint8_t>& event) {
HandleIncomingEvent(event);
};
if (fd_count == 1) {
hci::H4Protocol* h4_hci =
new hci::H4Protocol(fd_list[0], intercept_events, acl_cb, sco_cb, iso_cb);
fd_watcher_.WatchFdForNonBlockingReads(
fd_list[0], [h4_hci](int fd) {
h4_hci->OnDataReady(fd); });
hci_ = h4_hci;
} else {
hci::MctProtocol* mct_hci =
new hci::MctProtocol(fd_list, intercept_events, acl_cb);
fd_watcher_.WatchFdForNonBlockingReads(
fd_list[CH_EVT], [mct_hci](int fd) {
mct_hci->OnEventDataReady(fd); });
fd_watcher_.WatchFdForNonBlockingReads(
fd_list[CH_ACL_IN], [mct_hci](int fd) {
mct_hci->OnAclDataReady(fd); });
hci_ = mct_hci;
}
// Initially, the power management is off.
lpm_wake_deasserted = true;
// Start configuring the firmware
//配置和加载Firmware
firmware_startup_timer_ = new FirmwareStartupTimer();
lib_interface_->op(BT_VND_OP_FW_CFG, nullptr);
return true;
}
2.5 Selection of protocol stack
In the system/bt directory, the Bluetooth protocol stack code and the code for loading the libbt-vendor.so library are usually recommended to use the native Bluetooth protocol stack [in the system/bt directory]. If there is a manufacturer's protocol stack that does the native protocol stack If it is necessary to use the manufacturer's own protocol stack due to major modifications, load the protocol stacks of different manufacturers [usually in the hardware directory] at the jni layer.
//packages/apps/Bluetooth/jni/com_android_bluetooth_btservice_AdapterService.cpp
static void classInitNative(JNIEnv* env, jclass clazz) {
......
char type[64];
check_wifi_chip_type_string(type);
if (!strncmp(type, "RTL", 3)) {
//加载rtl厂商对应的蓝牙协议栈的库:bluetooth_rtk.default.so
ALOGD("%s, load %s.default.so", __func__, BT_STACK_RTK_MODULE_ID);
err = hw_get_module(BT_STACK_RTK_MODULE_ID, (hw_module_t const**)&module);
} else {
//加载原生的蓝牙协议栈的库:bluetooth.default.so
ALOGD("%s, load %s.default.so", __func__, id);
err = hw_get_module(id, (hw_module_t const**)&module);
}
}
//处理一些回调
const bt_vendor_callbacks_t lib_callbacks = {
sizeof(lib_callbacks), firmware_config_cb, sco_config_cb,
low_power_mode_cb, sco_audiostate_cb, buffer_alloc_cb,
buffer_free_cb, transmit_cb, epilog_cb,
a2dp_offload_cb};
} // namespace
//BT_STACK_RTK_MODULE_ID在./hardware/libhardware/include/hardware/bluetooth.h定义
3. Related logs
3.1 Kernel related logs
//USB识别设备类型pid,vid以及序列号等
02-24 14:08:54.866 I/KERNEL: usb 4-1.1( 7855): New USB device found, idVendor=0bda, idProduct=d723, bcdDevice= 2.00
02-24 14:08:54.866 I/KERNEL: usb 4-1.1( 7855): New USB device strings: Mfr=1, Product=2, SerialNumber=3
02-24 14:08:54.866 I/KERNEL: usb 4-1.1( 7855): Product: 802.11n WLAN Adapter
02-24 14:08:54.866 I/KERNEL: usb 4-1.1( 7855): Manufacturer: Realtek
02-24 14:08:54.866 I/KERNEL: usb 4-1.1( 7855): SerialNumber: 00e04c000001
//Realtk驱动加载已经装载firmware信息
02-24 14:08:54.869 I/KERNEL: rtk_btusb( 7855): btusb_probe: usb_interface 00000000a7ea3647, bInterfaceNumber 0, idVendor 0x0bda, idProduct 0x0000
02-24 14:08:54.869 I/KERNEL: rtk_btusb( 7855): get_fw_table_entry: Product id = 0xd723, fw table entry size 71
02-24 14:08:54.870 I/KERNEL: rtk_btusb( 7855): firmware_info_init: Auto suspend is disabled
02-24 14:08:54.870 I/KERNEL: rtk_btusb( 7855): btusb_probe: Check bt reset flag 0
02-24 14:08:54.870 I/KERNEL: rtk_btusb( 7855): load_firmware_info: Default lmp version = 0x8723, fw file name[rtl8723d_fw]
02-24 14:08:54.870 I/KERNEL: rtk_btusb( 7855): load_firmware_info: epatch_buf = vmalloc(fw->size, GFP_KERNEL)
02-24 14:08:55.152 I/KERNEL: rtk_btusb( 7855): get_eversion: status 0, eversion 2
02-24 14:08:55.152 I/KERNEL: rtk_btusb( 7855): load_firmware_info: Default lmp_version 0x8723, project_id[9] 0x8723 -> match
3.2 Application side and framework related logs
02-24 14:09:01.039 D/BluetoothManagerService( 797): enable(com.android.dten.settings): mBluetooth =null mBinding = false mState = OFF
02-24 14:09:01.039 D/BluetoothManagerService( 797): enable returning
02-24 14:09:01.039 D/BluetoothManagerService( 797): binding Bluetooth service
02-24 14:09:01.040 D/BluetoothA2dp( 1770): Binding service...
02-24 14:09:01.041 D/BluetoothHidHost( 1770): Binding service...
//加载协议栈libbluetooth.so库
02-24 14:09:01.091 I/BluetoothServiceJni( 9618): hal_util_load_bt_library: loaded Bluetooth library successfully
02-24 14:09:01.117 D/BluetoothManagerService( 797): MESSAGE_BLUETOOTH_STATE_CHANGE: OFF > BLE_TURNING_ON
02-24 14:09:01.117 D/BluetoothManagerService( 797): Sending BLE State Change: OFF > BLE_TURNING_ON
3.3 Protocol stack libbluetooth/libbt_hci log
//协议栈初始化以及消息线程的处理
02-24 14:09:01.104 I/bt_btif ( 9618): system/bt/btif/src/bluetooth.cc:162 init: init: start restricted = 0 ; common criteria mode = 0, config compare result = 0
02-24 14:09:01.105 I/droid.bluetoot( 9618): [0224/140901.105332:INFO:message_loop_thread.cc(224)] Run: message loop starting for thread bt_stack_manager_thread
02-24 14:09:01.105 I/bt_stack_manager( 9618): system/bt/btif/src/stack_manager.cc:210 event_init_stack: is initializing the stack
02-24 14:09:01.106 I/bt_btif_core( 9618): system/bt/btif/src/btif_core.cc:235 btif_init_bluetooth: btif_init_bluetooth entered
02-24 14:09:01.106 I/bt_stack_manager( 9618): system/bt/btif/src/stack_manager.cc:233 event_init_stack: finished
//hci协议层初始化,并且调用hardware下HDIL接口
02-24 14:09:01.135 I/bt_hci ( 9618): system/bt/hci/src/hci_layer.cc:200 hci_module_start_up: hci_module_start_up
02-24 14:09:01.135 I/bt_hci ( 9618): system/bt/hci/src/hci_layer_android.cc:159 hci_initialize: hci_initialize
02-24 14:09:01.136 I/bt_hci ( 9618): system/bt/hci/src/hci_layer_android.cc:178 hci_initialize: hci_initialize: IBluetoothHci::getService() returned 0xb400006ff7a7d390 (remote)
02-24 14:09:01.136 I/[email protected]( 9496): BluetoothHci::initialize()
3.4 HDIL interface related logs
02-24 14:09:01.136 I/[email protected]( 9496): BluetoothHci::initialize()
//根据type来决定加载不同的so库
02-24 14:09:01.137 E/[email protected]( 9496): check_wifi_chip_type_string : APXXX: Permission denied
//打开libbt-vendor.so来配置端口,上下电,firmware的加载等
02-24 14:09:01.137 D/[email protected]( 9496): Open: libbt-vendor.so
02-24 14:09:01.138 E/[email protected]( 9496): vendor storage read error
02-24 14:09:01.138 D/[email protected]( 9496): get_local_address: Trying /data/misc/bluetooth/bdaddr
// libbt_verdor库中处理,获取版本信息,日志开关等等
02-24 14:09:01.138 I/libbt_vendor( 9496): RTKBT_RELEASE_NAME: 20201130_BT_ANDROID_11.0
02-24 14:09:01.138 I/libbt_vendor( 9496): init
02-24 14:09:01.138 I/rtk_parse( 9496): RTKBT_RELEASE_NAME: 20200318_BT_ANDROID_10.0
02-24 14:09:01.138 E/libbt_vendor( 9496): rtk_btsnoop_dump = 0, rtk_btsnoop_save_log = 0
02-24 14:09:01.138 D/[email protected]( 9496): Open vendor library loaded
//设置电源管理情况,先下电在上电
02-24 14:09:01.338 D/libbt_vendor( 9496): set power off and delay 200ms
02-24 14:09:02.339 D/libbt_vendor( 9496): set power on and delay 1000ms
02-24 14:09:02.339 D/libbt_vendor( 9496): USB op for 3
02-24 14:09:02.339 I/bt_userial_vendor( 9496): device fd = 8 open
02-24 14:09:02.339 D/bt_service( 9496): OsAllocateTimer bt_service sigev.sigev_notify_thread_id = syscall(__NR_gettid)!
02-24 14:09:02.339 D/bt_service( 9496): RTK_btservice_init init done!
//驱动打开USB设备
02-24 14:09:03.814 I/KERNEL: DTEN( 182): HW_INFO:time:2023-2-24 6:9:3.
02-24 14:09:04.111 I/KERNEL: rtk_btusb( 9498): btchr_open: BT usb char device is opening
02-24 14:09:04.112 I/KERNEL: rtk_btusb( 9498): btchr_ioctl with Cmd:-2147203660
//libbt_verdor对设备的firmware的配置,以及信息的获取
02-24 14:09:02.601 D/bt_hwcfg_usb( 9496): RTKBT_RELEASE_NAME: 20201130_BT_ANDROID_11.0
02-24 14:09:02.601 D/bt_hwcfg_usb( 9496):
02-24 14:09:02.601 D/bt_hwcfg_usb( 9496): Realtek libbt-vendor_usb Version 5.1.1
02-24 14:09:02.601 D/bt_hwcfg_usb( 9496): hw_usb_config_start, transtype = 0x20, pid = 0xd723, vid = 0x0bda
02-24 14:09:04.373 I/KERNEL: rtk_btusb( 9498): btusb_open: Start, PM usage count 0
02-24 14:09:04.373 I/KERNEL: rtk_btusb( 9498): btusb_open: End, PM usage count 0
02-24 14:09:02.610 D/bt_hwcfg_usb( 9496): hw_cfg_cb.state = 9
02-24 14:09:02.911 D/bt_hwcfg_usb( 9496): hw_cfg_cb.state = 2
02-24 14:09:02.911 I/bt_hwcfg_usb( 9496): BT config file: /vendor/etc/firmware/rtl8723d_config
02-24 14:09:02.911 D/bt_hwcfg_usb( 9496): bt_addr = 0
02-24 14:09:02.911 I/bt_hwcfg_usb( 9496): config offset(0xe4),length(0x1)
//装载固件成功
02-24 14:09:02.911 I/bt_hwcfg( 9496): BT fw file: /vendor//etc/firmware/rtl8723d_fw
02-24 14:09:02.911 I/bt_hwcfg( 9496): Load FW OK
//解析firmware以及rtl8723d_config的信息
02-24 14:09:02.911 D/bt_hwcfg_usb( 9496): lmp_subversion = 0x8723 hw_cfg_cb.hci_version = 0x8 hw_cfg_cb.hci_revision = 0xd, hw_cfg_cb.lmp_sub_current = 0x4b4a
02-24 14:09:02.911 D/bt_hwcfg_usb( 9496): hw_usb_config_cback: Cold BT controller startup
02-24 14:09:02.912 D/bt_hwcfg_usb( 9496): hw_cfg_cb.state = 3
02-24 14:09:02.912 D/bt_hwcfg_usb( 9496): hw_usb_config_cback chip_id of the IC:3
02-24 14:09:02.912 E/bt_hwcfg_usb( 9496): update altsettings
02-24 14:09:02.912 I/bt_hwcfg_usb( 9496): ORG Config len=0000000a:
02-24 14:09:02.912 I/bt_hwcfg_usb( 9496): 00000000: 55 ab 23 87 04 00 e4 00 01 08 00 00 00 00 00 00
02-24 14:09:02.912 I/bt_hwcfg_usb( 9496): Couldn't open extra config /vendor/etc/bluetooth/rtk_btconfig.txt, err:No such file or directory
02-24 14:09:02.912 I/bt_hwcfg_usb( 9496): rtk_update_altsettings: 1 AltSettings
02-24 14:09:02.912 I/bt_hwcfg_usb( 9496): MAC: 22:22:2b:2c:02:00
02-24 14:09:02.912 I/bt_hwcfg_usb( 9496): rtk_update_altsettings: add 0044[06]
02-24 14:09:02.912 I/bt_hwcfg_usb( 9496): NEW Config len=00000013:
02-24 14:09:02.912 I/bt_hwcfg_usb( 9496): 00000000: 55 ab 23 87 0d 00 e4 00 01 08 44 00 06 00 02 2c
02-24 14:09:02.912 I/bt_hwcfg_usb( 9496): 00000010: 2b 22 22 00 00 00 00 00 00 00 00 00 00 00 00 00
02-24 14:09:02.912 I/bt_hwcfg( 9496): bt_hw_parse_project_id: opcode 0, len 1, data 9
02-24 14:09:02.912 I/bt_hwcfg( 9496): rtk_get_patch_entry: fw_ver 0x829a4b4a, patch_num 3
02-24 14:09:02.912 I/bt_hwcfg( 9496): rtk_get_patch_entry: chip_id 3, patch_len 0x7ea0, patch_offset 0x4940
02-24 14:09:02.912 I/bt_hwcfg_usb( 9496): total_len = 0x7eb3
02-24 14:09:02.912 I/bt_hwcfg_usb( 9496): BTCOEX:20200103-3535 svn_version:24232 lmp_subversion:0x8723 hci_version:0x8 hci_revision:0xd chip_type:31 Cut:3 libbt-vendor_uart version:5.1.1, patch->fw_version = 829a4b4a
02-24 14:09:02.912 I/bt_hwcfg_usb( 9496): Fw: exists, config file: exists
02-24 14:09:02.912 D/bt_hwcfg_usb( 9496): Check total_len(0x00007eb3) max_patch_size(0x0000a000)
//总共129条
02-24 14:09:02.912 D/bt_hwcfg_usb( 9496): patch fragment count 129, tail len 179
02-24 14:09:02.913 D/bt_hwcfg_usb( 9496): bt vendor lib: HW_CFG_DL_FW_PATCH status:0, opcode:0xfc20
02-24 14:09:02.913 D/bt_hwcfg_usb( 9496): bt vendor lib: HW_CFG_DL_FW_PATCH status:0, iIndexRx:0
....
02-24 14:09:03.170 D/bt_hwcfg_usb( 9496): bt vendor lib: HW_CFG_DL_FW_PATCH status:0, opcode:0xfc20
02-24 14:09:03.170 D/bt_hwcfg_usb( 9496): bt vendor lib: HW_CFG_DL_FW_PATCH status:0, iIndexRx:129
02-24 14:09:03.170 D/bt_hwcfg_usb( 9496): vendor lib fwcfg completed
//HDIL的相关回调
02-24 14:09:03.170 D/[email protected]( 9496): OnFirmwareConfigured result: 0
02-24 14:09:03.170 I/[email protected]( 9496): Firmware configured in 0.831s
02-24 14:09:03.170 I/[email protected]( 9496): OnFirmwareConfigured: lpm_timeout_ms 0
02-24 14:09:03.170 D/[email protected]( 9496): OnFirmwareConfigured Calling StartLowPowerWatchdog()
//Kernel io接口OK
02-24 14:09:04.943 I/KERNEL: rtk_btusb( 9722): btchr_ioctl with Cmd:1074021811
02-24 14:09:04.943 I/KERNEL: rtk_btusb( 9722): btchr_ioctl lmp_sub = 0x4b4a, patch_entry->lmp_sub = 0x4b4a
02-24 14:09:03.202 E/bt_userial_vendor( 9496): set event mask, it should bt stack init, set coex bt on
//rtk心跳相关配置和初始化
02-24 14:09:03.202 D/rtk_heartbeat( 9496): Heartbeat_init start
02-24 14:09:03.202 E/rtk_heartbeat( 9496): load_rtkbt_heartbeat_conf unable to open file '/vendor/etc/bluetooth/rtkbt_heartbeat.conf': No such file or directory
02-24 14:09:03.202 D/rtk_heartbeat( 9496): Heartbeat_init res = 0
02-24 14:09:03.202 D/rtk_heartbeat( 9496): Heartbeat_init end
4. Remaining issues
4.1 Abnormality in chip type acquisition
Currently, the check_wifi_chip_type_string interface prompts that there is a permission issue, so when debugging RTL8723DU, the BCM macro is turned off. If the check_wifi_chip_type_string interface is ok later, the BCM macro can be turned on by default for compatibility processing. Next, you need to add logs to specifically analyze the problem of abnormal return from the check_wifi_chip_type_string interface.
4.2 USB Bluetooth adapter SCO mode not working problem
The headset can work normally when paired and connected through A2DP mode, because there is no need to register the sound card and it uses the Bluetooth protocol itself; while SCO uses the pcm stream and needs to go through the sound card process through ALSA. The current test shows that the sound card uses the native card 2, and device 0 is configured for the corner of the BCM chip. Therefore, the sound card of the USB Bluetooth adapter needs to be registered by itself, and the USB data needs to be converted to PCM for processing.
4.3 Mandatory use of A2DP during meetings
In order to use a Bluetooth headset during a meeting, you must first force the A2DP mode to be used during the meeting, and the Mic will use the local mic of the machine. When using SCO mode for meetings, the following interfaces are mainly called:
//audioManager.java
//开启sco模式
void startBluetoothSco()
void startBluetoothScoVirtualCall()
//停止sco模式
void stopBluetoothSco()
//是否使用本机speaker
void setSpeakerphoneOn(boolean on)
//通知hal层进行模式和设备选择
void setMode(@AudioMode int mode)
//设置communication的设备,替代传统的方式startBluetoothSco/stopBluetoothSco/setSpeakerphoneOn
boolean setCommunicationDevice(@NonNull AudioDeviceInfo device)
To force A2DP to be enabled, just shield the above interfaces. Here is a brief introduction to the usage of the new interface setCommunicationDevice
//The example below shows how to enable and disable speakerphone mode.
// Get an AudioManager instance
AudioManager audioManager = Context.getSystemService(AudioManager.class);
AudioDeviceInfo speakerDevice = null;
List<AudioDeviceInfo> devices = audioManager.getAvailableCommunicationDevices();
for (AudioDeviceInfo device : devices) {
if (device.getType() == AudioDeviceInfo.TYPE_BUILTIN_SPEAKER) {
speakerDevice = device;
break;
}
}
if (speakerDevice != null) {
// Turn speakerphone ON.
boolean result = audioManager.setCommunicationDevice(speakerDevice);
if (!result) {
// Handle error.
}
// Turn speakerphone OFF.
audioManager.clearCommunicationDevice();
}