仿真器 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

連接埠變更列表

移植以下變更清單: