模拟器 USB 透传模式集成指南

本页介绍如何将两个外设(蓝牙和 Wi-Fi)连接到 AAOS 仿真器。在此过程中,驱动程序支持的三个特定领域是关键:

  • 访客内核
  • 访客安卓
  • Linux主机

首先,在 Guest 内核中编译并启用相关的 USB 驱动程序。接下来,Guest Android 必须选择正确的 HAL 和服务来启动驱动程序。最后,Linux 主机必须访问 USB 驱动程序,然后将其传输到QEMU

经过测试的加密狗

测试了以下加密狗:

其他加密狗可能也可以工作,但没有测试其他加密狗。

原生 USB 支持

QEMU 提供了将 USB 传递到仿真器的选项。 AAOS 系统映像已经可以处理连接的手机。有关详细信息,请参阅Android 开放附件 (AOA)

由于电话在建立 AOA 连接时更改了vendorIDproductID的值,因此当电话处于 AOA 模式时,新的 USB 接口(以及原始 USB 接口)会看到该设备。

要确定 AOA 模式之前和之后的vendorIDproductID的值,请使用lsusb

# Note Vendor ID and Product ID of your phone
$ lsusb
Bus 001 Device 079: ID 18d1:4ee1 Google Inc. Nexus/Pixel Device (MTP)
# Start up an emulator!
$ ./emulator @AVD_NAME -no-snapshot -qemu -device usb-ehci,id=ehci -device usb-host,bus=ehci.0,vendorid=0x18d1,productid=0x4ee1 -device usb-host,bus=ehci.0,vendorid=0x18d1,productid=0x2d00

或者,将物理端口传递给 QEMU:

# First plug something into the interested USB port and note the Bus and Device number.
$ lsusb
Bus 001 Device 012: ID 0bda:c820 Realtek Semiconductor Corp. 802.11ac NIC
# Now figure out where the Port number is.
$ lsusb -t
/:  Bus 01.Port 1: Dev 1, Class=root_hub, Driver=xhci_hcd/12p, 480M
    |__ Port 4: Dev 12, If 1, Class=Wireless, Driver=btusb, 480M
    |__ Port 4: Dev 12, If 2, Class=Vendor Specific Class, Driver=, 480M
    |__ Port 4: Dev 12, If 0, Class=Wireless, Driver=btusb, 480M
# Launch the emulator
 ./emulator @AVD_NAME -no-snapshot -qemu -device usb-ehci,id=ehci -device usb-host,bus=ehci.0,hostbus=1,hostport=4
# Now, whatever you plug into the emulator, USB passthrough will happen on the fly.

AAOS系统镜像识别手机,将其置于AOA模式,并在AOA模式运行时再次识别。

要支持 USB 直通,请确认您使用的是模拟器30.5.0

# Check for the emulator version
$ emulator --version

模拟器30.5.0包括libusb升级和临时解决方法,以解决华硕加密狗支持的速度兼容性问题:

蓝牙支持

为了支持蓝牙直通,Google 测试了 Auscomer 的ASUS USB-BT400 USB 适配器 USBBT400和 USB Wi-Fi 蓝牙适配器。

首先,您需要添加对加密狗的内核支持。要了解 Android 蓝牙堆栈,请参阅蓝牙。对于 HIDL,模拟器使用模拟实现。因此,请使用本机 Linux 实现来切换它。

访客内核

要支持 USB 蓝牙适配器:

  1. 将缺少的btusb.ko添加到您的内核中:

    --- a/goldfish_defconfig.fragment
    +++ b/goldfish_defconfig.fragment
    @@ -1,6 +1,7 @@
     # CONFIG_CRYPTO_DEV_VIRTIO is not set
     CONFIG_BLK_DEV_MD=m
    +CONFIG_BT_HCIBTUSB=m
     CONFIG_CPUFREQ_DUMMY=m
    

访客安卓

  1. vendor.mk文件中,包含Linux本机HIDL和几个权限:

    PRODUCT_PACKAGES += \
        android.hardware.bluetooth@1.1-service.btlinux
    PRODUCT_COPY_FILES += \ frameworks/native/data/etc/android.hardware.usb.host.xml:$(TARGET_COPY_OUT_VENDOR)/etc/permissions/android.hardware.usb.host.xml
    
  2. 创建一个单向路径属性来切换 HIDL,以便它使用 Linux 本机 HIDL 实现:

    selinux/common/domain.te
    
    get_prop(domain, qemu_prop)
    +get_prop(domain, vendor_build_prop)
    
    selinux/common/property_contexts
    
    qemu.cmdline            u:object_r:qemu_cmdline:s0
    +qemu.preferred.bt.service u:object_r:qemu_prop:s0
    
  3. 每当属性qemu.preferred.bt.service设置为passthrough时,您将切换 HIDL 实现:

    service btlinux-1.1 /vendor/bin/hw/android.hardware.bluetooth@1.1-service.btlinux
      class hal
      user bluetooth
      group bluetooth net_admin net_bt_admin
      capabilities NET_ADMIN NET_RAW SYS_NICE
      disabled
    
    on property:qemu.preferred.bt.service=passthrough
      stop vendor.bluetooth-1-1
      start btlinux-1.1
    
  4. 添加蓝牙配置文件以获得完整功能,例如在真正的 USB 设备上:

    hal/bluetooth/bdroid_buildcfg.h
    
    #ifndef _BDROID_BUILDCFG_H
    #define _BDROID_BUILDCFG_H
    #define BTM_DEF_LOCAL_NAME "gCar Emulator"
    #define BTA_AV_SINK_INCLUDED TRUE
    /* Handsfree device */
    #define BTA_DM_COD {0x26, 0x04, 0x08}
    #endif
    
  5. 修改BoardConfig.mk文件以确定配置文件的保存位置:

    BoardConfig.mk
    
    # Bluetooth
    BOARD_HAVE_BLUETOOTH := true
    BOARD_BLUETOOTH_BDROID_BUILDCFG_INCLUDE_DIR := vendor/auto/embedded/hal/bluetooth
    

Linux主机

在Linux主机上:

  1. 更新udev设置以允许用户进程(例如 QEMU)具有读/写权限:

    $ echo 'SUBSYSTEM=="usb", ATTRS{idVendor}=="0b05", ATTRS{idProduct}=="17cb", MODE="0666", GROUP="plugdev"' | sudo tee /etc/udev/rules.d/99-mynew.rules >/dev/null
    $ sudo udevadm control --reload
    $ sudo udevadm trigger
    
  2. 要运行模拟器,请设置以下命令行参数:

    # Start up an emulator!
    $ ./emulator @AVD_NAME -no-snapshot -prop qemu.preferred.bt.service=passthrough -qemu -device usb-ehci,id=ehci -device usb-host,bus=ehci.0,vendorid=0x0b05,productid=0x17cb
    # Start Bluetooth Passthrough
    

无线网络支持

为了验证双蓝牙和 Wi-Fi,Google 使用 USB Wi-Fi 蓝牙适配器进行了测试。

访客内核

这个特定的 USB 加密狗使用 RTL8821CU 芯片,但上游主线内核尚不支持。您可以在8821cu找到新开发的内核模块。

然后,在变更列表 1575108中,外部内核模块被集成到 goldfish 内核源中以对其进行编译。

最后,内核模块编译成功,但出现一些 CFI 崩溃。您需要手动修补代码才能解决此问题。有关详细信息,请参阅Android 内核中的控制流完整性

启用CONFIG_CFI_PERMISSIVE并首先调试堆栈的其余部分可能会有所帮助:

--- a/goldfish_defconfig.fragment
+++ b/goldfish_defconfig.fragment
@@ -1,6 +1,7 @@
 CONFIG_CFI_CLANG=m
+CONFIG_CFI_PERMISSIVE=m

无论如何,请转到更改列表 1575109查看 CFI 崩溃的正确修复方法。

访客安卓

要了解有关 Wi-Fi 堆栈的更多信息,请参阅Wi-Fi 概述。模拟器附带了使 Wi-Fi 工作的设置。

Linux主机

在Linux主机上,您必须更新udev设置以使用户进程(例如QEMU)具有读/写权限。

# /lib/udev/rules.d/40-usb_modeswitch.rules
$ ATTR{idVendor}=="0bda", ATTR{idProduct}=="1a2b", RUN+="usb_modeswitch '/%k'"
$ echo 'SUBSYSTEM=="usb", ATTRS{idVendor}=="0bda", ATTRS{idProduct}=="c820", MODE="0666", GROUP="plugdev"' | sudo tee /etc/udev/rules.d/99-mynew2.rules >/dev/null
$ sudo udevadm control --reload
$ sudo udevadm trigger

将加密狗传递给 QEMU:

# Start up an emulator!
$ ./emulator @AVD_NAME -no-snapshot -qemu -device usb-ehci,id=ehci -device usb-host,bus=ehci.0,vendorid=0x0bda,productid=0xc820

端口变更列表

移植以下变更列表: