您现在的位置是:首页 >技术杂谈 >RK3588 Android12调试USB 蓝牙模块网站首页技术杂谈

RK3588 Android12调试USB 蓝牙模块

GitFranc 2023-05-18 16:00:02
简介RK3588 Android12调试USB 蓝牙模块

一、蓝牙的架构

  Android4.2 之后采用 bluedroid 作为协议;整体由 bluetooth.apk,bluedroid ,libbt-vendor 三个 部 分 组 成 。
  • bluetooth.apk :作 为 蓝 牙 服 务 进 程 , 通 过 libblueooth_jni 层 获 取bluetooth*.default.so 提 供 的bt_interface_t 控 制 接 口 并 状 态 与 数 据 回 调 注 册 到bluetooth*.default.so;完成对蓝牙模块的控制与数据接收;
  • libbluetooth:主要是bluedroid的蓝牙协议栈的具体实现,向下调用libbt-hci的接口加载**libbt-vendor;**向上供libblueooth_jni的回调;
  • **libbt-vendor *完成对蓝牙模块硬件初始化与控制(物理通讯接口初始化,固件下载,供电控制)RK平台通过判断wifi+Bt模块的type,在libbuetooth_jni层选择加载不同适配厂商协议栈;并在libbt-hci中根据模块类型加载不同厂家模块的libbt-vendor.so,完成动态适配模块。

Android蓝牙框架图.png
RK 平台通过判断模块的 type,在 libbuetooth_jni 层选择加载不同适配厂商协议栈;并在 libbt-hci 中根据模块类型加载不同厂家模块的 libbt-vendor*.so,完成动态适配模块。

二、USB 蓝牙模块的调试

这里选项的是RTL8723DU的一款USB蓝牙适配器,下面将从kernel开始一步步看下适配过程;

2.1 kernel部分

首先,将Realtek提供的蓝牙驱动文件rtk_btusb.h和rtk_btusb.c拷贝到kernel的drivers/bluetooth/目录下:

kernel-5.10/drivers/bluetooth/rtk_btusb.c
kernel-5.10/drivers/bluetooth/rtk_btusb.h

修改kernel的drivers/bluetooth/目录下的文件“Kconfig” 和“Makefile”。

config BT_RTKBTUSB
    tristate "RTK HCI USB driver"
    depends on USB
    help
       RTK Bluetooth HCI USB driver
obj-$(CONFIG_BT_RTKBTUSB)	+= rtk_btusb.o

平台对应的defconfig文件添加Realtek BT Chip支持的宏定义,这里是dten_ribeye_me_pro.config文件

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 增加设备节点以及权限

device/rockchip/common目录:添加一些节点的权限和一些宏控开关
在device/rockchip/common/init.connectivity.rc文件(一般rk平台代码已经添加):

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

在device/rockchip/common/wifi_bt_common.mk文件(一般rk平台代码已经添加),这个先关闭BCM的芯片,防止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 libbt-vendor.so的配置

在hardware/realtek目录:HAL层添加蓝牙协议栈的代码和对应的libbt-vendor.so的源代码,libbt-vendor完成对蓝牙模块硬件初始化与控制(物理通讯接口初始化,固件下载,供电控制)

@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$ 

最终会通过mk文件hardware/realtek/rtkbt/rtkbt.mk拷贝到机器的以下目录,打开蓝牙的时候会去加载fw:

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))

最终拷贝到设备上为:/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 根据type选择具体的vendor库

部分HAL层对应的libbt-vendor.so的源代码浅析:
首先是bluedroid 蓝牙协议栈libbt-hci【原生蓝牙协议栈】通过system/bt/hci/src/hci_layer_android.cc的hci_initialize函数,调用interfaces/bluetooth/1.0/default/vendor_interface.cc中的initialize函数,进而调用open函数打开对应so库。

//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 协议栈的选择

system/bt目录下,蓝牙协议栈代码和加载libbt-vendor.so库的代码,通常情况下建议使用原生的蓝牙协议栈【在system/bt目录】,如果有厂商的协议栈对原生协议栈做过较大的修正而必须使用厂商自己的协议栈的情况下在 jni 层完成对不同厂商协议栈【一般在hardware目录下】的加载

//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.1 Kernel相关日志

//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 应用侧和framework相关日志

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 协议栈libbluetooth/libbt_hci日志

//协议栈初始化以及消息线程的处理
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/android.hardware.bluetooth@1.0-impl( 9496): BluetoothHci::initialize()

3.4 HDIL接口相关日志

02-24 14:09:01.136 I/android.hardware.bluetooth@1.0-impl( 9496): BluetoothHci::initialize()
//根据type来决定加载不同的so库
02-24 14:09:01.137 E/android.hardware.bluetooth@1.0-service( 9496): check_wifi_chip_type_string : APXXX: Permission denied
//打开libbt-vendor.so来配置端口,上下电,firmware的加载等
02-24 14:09:01.137 D/android.hardware.bluetooth@1.0-impl( 9496): Open: libbt-vendor.so
02-24 14:09:01.138 E/android.hardware.bluetooth@1.0-service( 9496): vendor storage read error
02-24 14:09:01.138 D/android.hardware.bluetooth@1.0-service( 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/android.hardware.bluetooth@1.0-impl( 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/android.hardware.bluetooth@1.0-impl( 9496): OnFirmwareConfigured result: 0
02-24 14:09:03.170 I/android.hardware.bluetooth@1.0-impl( 9496): Firmware configured in 0.831s
02-24 14:09:03.170 I/android.hardware.bluetooth@1.0-impl( 9496): OnFirmwareConfigured: lpm_timeout_ms 0
02-24 14:09:03.170 D/android.hardware.bluetooth@1.0-impl( 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.1 芯片type获取异常

目前通过check_wifi_chip_type_string这个接口获取提示有权限问题,因此调试RTL8723DU时候,是将BCM的宏是关掉了的。后面如果check_wifi_chip_type_string这个接口ok,BCM宏默认是可以打开做兼容处理。下面需要加日志具体分析check_wifi_chip_type_string接口返回异常的问题。

4.2 USB蓝牙适配器SCO模式不工作问题

耳机正常配对连接过A2DP模式可以正常工作,因为不需要注册声卡,走的是蓝牙本身协议;而SCO走的是pcm流需要通过ALSA走声卡的流程。目前测试看声卡用的是原生的card 2,device 0是针对BCM芯片拐角的配置,因此USB蓝牙适配器的声卡需要自己注册,而且需要将USB的数据转为PCM处理。

4.3 开会强制使用A2DP

为了开会使用蓝牙耳机,先强制在开会时候使用A2DP模式,Mic使用本机local的mic。而开会使用SCO模式主要调用以下几个接口:

//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)

强制使能A2DP,则将上述接口屏蔽即可。这里简单说下新增接口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();
 }
风语者!平时喜欢研究各种技术,目前在从事后端开发工作,热爱生活、热爱工作。