carplay逆向第一步:USB口OTG主从切换
USB的“主设备”变更为“从设备”,这个功能实现起来很简单。实现carplay功能的同学,原车中控需要先检测是否是苹果设备,应比较复杂了,涉及到驱动一系列的改动,具体如下:
1)USB描述符修改。
该功能最为复杂,涉及到USB的 设备描述符(Device Descriptor),配置描述符(Configuration Descriptor),接口描述符(Interface Descriptor)。
A)设备描述符(Device Descriptor)1个:
Get Device Descriptor
bLength: 18
bDescriptorType: 0x1
bcdUSB: 0x0200
bDeviceClass: 0
bDeviceSubClass: 0
bDeviceProtocol: 0
bMaxPacketSize0: 64
bytes idVendor: 0x05AC
idProduct: 0x12A8
bcdDevice: 0x1001
iManufacturer: 1
iProduct: 2
iSerialNumber: 3
bNumConfigurations: 4
B)配置描述符(Configuration Descriptor)4个:
Get Configuration Descriptor
bLength: 9
bDescriptorType: 0x2
wTotalLength: 39 bytes
bNumInterfaces: 1
bConfigurationValue: 1
iConfiguration: 5
bmAttributes: 0xC0
bMaxPower: 250 (500 mA)
Get Configuration Descriptor
bLength: 9
bDescriptorType: 0x2
wTotalLength: 149 bytes
bNumInterfaces: 3
bConfigurationValue: 2
iConfiguration: 6
bmAttributes: 0xC0
bMaxPower: 250 (500 mA)
Get Configuration Descriptor
bLength: 9
bDescriptorType: 0x2
wTotalLength: 62 bytes
bNumInterfaces: 2
bConfigurationValue: 3
iConfiguration: 7
bmAttributes: 0xC0
bMaxPower: 250 (500 mA)
Configuration Descriptor
bLength: 9
bDescriptorType: 0x2
wTotalLength: 117 bytes
bNumInterfaces: 3
bConfigurationValue: 4
iConfiguration: 8
bmAttributes: 0xC0
bMaxPower: 250 (500 mA)
C)接口描述符(Interface Descriptor)9个
因为个数太多,我这里就不一一列举了,各位同学可以将苹果手机通过USB直接插入ubuntu电脑,使用lsusb 命令,即可看到。
2)该设备是否支持carplay功能。
原车中控确定USB连接的设备是苹果设备以后,将会通过USB control通道发送请求,询问是否支持carplay功能,请求的内容如下:
0xC0, 0x53, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00
苹果设备需要回复,表示支持carplay功能。
3)请求主从切换。
原车中控确定USB连接的设备是苹果设备,而且支持carplay功能,将会请求主从切换,请求内容如下:
0x40, 0x51, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00
苹果设备收到该信息以后,将会进行主从切换。
以上内容,需要在驱动层进行修改。修改完成以后,可以和苹果设备一样插入电脑,使用命令lsusb进行查看,如果确认和苹果手机描述符相同,我们就走完了万里长征第一步了。