Android Usb 主机:如何在 URB 功能设置为 URB_FUNCTION_CLASS_ENDPOINT 的情况下发送控制传输?

问题描述

我正在尝试使用 Android 设备作为 USB 主机 (USB OTG) 对设备和 PC 之间的 USB 通信进行逆向工程。我正在使用 Android SDK (https://developer.android.com/guide/topics/connectivity/usb/host) 提供的 Usb Host 类

我使用wireshark记录了设备和PC之间的通信如下:

USB URB
[Source: host]
[Destination: 1.3.0]
USBPcap pseudoheader length: 28
IRP ID: 0xffffa6827a5a4010
IRP USBD_STATUS: USBD_STATUS_SUCCESS (0x00000000)
URB Function: URB_FUNCTION_CLASS_INTERFACE (0x001b)
IRP @R_467_4045@ion: 0x00,Direction: FDO -> PDO
URB bus id: 1
Device address: 3
Endpoint: 0x00,Direction: OUT
URB transfer type: URB_CONTROL (0x02)
Packet Data Length: 8
Control transfer stage: Setup (0)
[bInterfaceClass: UnkNown (0xffff)]

URB setup
bmRequestType: 0x21
bRequest: 1
wValue: 0x0102
wIndex: 1280 (0x0500)
wLength: 2

我可以使用以下代码发送相同的消息,并且工作正常:

fun connectDevice(context: Context,device: UsbDevice) {
        val usbManager = context.getSystemService(Context.USB_SERVICE) as UsbManager
        usbInterface = device.getInterface(0).also { intf ->
            connection = usbManager.openDevice(device)
            try{
                val claimed = connection?.claimInterface(intf,true) ?: false
                if (connection != null && claimed) {
                    LogUtil.d("USB CONNECTED")
                } else {
                    LogUtil.d("USB NOT CONNECTED")
                    connection?.close()
                    connection = null
                }

            }catch (e: Exception){
                LogUtil.d("USB EXCEPTION ${e.message}")
                e.printstacktrace()
            }
        }
    }
        
private fun setVolume(input: Int,output: Int,value: ByteArray): String {
        return connection?.controlTransfer(
            0x21,//request type
            1,//
            output.shl(8).or(input),//output//input 0xffff,0x (01-09) (01-09,0a-0f,10)
            0x0500,//wIndex
            value,//
            2,//length
            USB_TIMEOUT// timeout
        ).toString()
    }

现在我正在尝试以相同的方式发送第二种类型的控制传输消息:

USB URB

[Source: host]
[Destination: 1.2.0]
USBPcap pseudoheader length: 28
IRP ID: 0xffffa6827b3f0010
IRP USBD_STATUS: USBD_STATUS_SUCCESS (0x00000000)
URB Function: URB_FUNCTION_CLASS_ENDPOINT (0x001c)
IRP @R_467_4045@ion: 0x00,Direction: FDO -> PDO
URB bus id: 1
Device address: 2
Endpoint: 0x00,Direction: OUT
URB transfer type: URB_CONTROL (0x02)
Packet Data Length: 8
Control transfer stage: Setup (0)
[bInterfaceClass: UnkNown (0xffff)]

URB setup
bmRequestType: 0x22
bRequest: 1
wValue: 0x0100
wIndex: 1 (0x0001)
wLength: 3

使用以下方法

fun setInternalSampleRate(value: ByteArray): String{
        return connection?.controlTransfer(
            0x22,//
            0x0100,1,//
            3,//length
            USB_TIMEOUT// timeout
        ).toString()
    }

但是它返回一个错误。我在wireshark中看到的两个调用间的唯一区别(接受我相应更改的URB设置)是URB函数参数的值:

URB_FUNCTION_CLASS_INTERFACE (0x001b) 对比 URB_FUNCTION_CLASS_ENDPOINT (0x001c)

有没有办法使用 Android SDK 发送带有 URB_FUNCTION_CLASS_ENDPOINT 参数的 USB 控制传输?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)