| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
package com.android.hardware.usb.externalmanagementtest; |
| 17 |
|
| 18 |
import android.hardware.usb.UsbConstants; |
| 19 |
import android.hardware.usb.UsbDevice; |
| 20 |
import android.hardware.usb.UsbDeviceConnection; |
| 21 |
import android.util.Log; |
| 22 |
|
| 23 |
public class AoapInterface { |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
public static final int USB_ACCESSORY_VENDOR_ID = 0x18D1; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
public static final int USB_ACCESSORY_PRODUCT_ID = 0x2D00; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
public static final int USB_ACCESSORY_ADB_PRODUCT_ID = 0x2D01; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
public static final int ACCESSORY_STRING_MANUFACTURER = 0; |
| 43 |
public static final int ACCESSORY_STRING_MODEL = 1; |
| 44 |
public static final int ACCESSORY_STRING_DESCRIPTION = 2; |
| 45 |
public static final int ACCESSORY_STRING_VERSION = 3; |
| 46 |
public static final int ACCESSORY_STRING_URI = 4; |
| 47 |
public static final int ACCESSORY_STRING_SERIAL = 5; |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
public static final int ACCESSORY_GET_PROTOCOL = 51; |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
public static final int ACCESSORY_SEND_STRING = 52; |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
public static final int ACCESSORY_START = 53; |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
public static final int MAX_PAYLOAD_SIZE = 16384; |
| 92 |
|
| 93 |
private static final String TAG = AoapInterface.class.getSimpleName(); |
| 94 |
|
| 95 |
public static int getProtocol(UsbDeviceConnection conn) { |
| 96 |
byte buffer[] = new byte[2]; |
| 97 |
int len = conn.controlTransfer( |
| 98 |
UsbConstants.USB_DIR_IN | UsbConstants.USB_TYPE_VENDOR, |
| 99 |
AoapInterface.ACCESSORY_GET_PROTOCOL, 0, 0, buffer, 2, 10000); |
| 100 |
if (len != 2) { |
| 101 |
return -1; |
| 102 |
} |
| 103 |
return (buffer[1] << 8) | buffer[0]; |
| 104 |
} |
| 105 |
|
| 106 |
public static void sendString(UsbDeviceConnection conn, int index, String string) { |
| 107 |
byte[] buffer = (string + "\0").getBytes(); |
| 108 |
int len = conn.controlTransfer( |
| 109 |
UsbConstants.USB_DIR_OUT | UsbConstants.USB_TYPE_VENDOR, |
| 110 |
AoapInterface.ACCESSORY_SEND_STRING, 0, index, |
| 111 |
buffer, buffer.length, 10000); |
| 112 |
if (len != buffer.length) { |
| 113 |
throw new RuntimeException("Failed to send string " + index + ": \"" + string + "\""); |
| 114 |
} else { |
| 115 |
Log.i(TAG, "Sent string " + index + ": \"" + string + "\""); |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
public static void sendAoapStart(UsbDeviceConnection conn) { |
| 120 |
int len = conn.controlTransfer( |
| 121 |
UsbConstants.USB_DIR_OUT | UsbConstants.USB_TYPE_VENDOR, |
| 122 |
AoapInterface.ACCESSORY_START, 0, 0, null, 0, 10000); |
| 123 |
if (len < 0) { |
| 124 |
throw new RuntimeException("control transfer for accessory start failed:" + len); |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
public static boolean isDeviceInAoapMode(UsbDevice device) { |
| 129 |
final int vid = device.getVendorId(); |
| 130 |
final int pid = device.getProductId(); |
| 131 |
return vid == USB_ACCESSORY_VENDOR_ID |
| 132 |
&& (pid == USB_ACCESSORY_PRODUCT_ID |
| 133 |
|| pid == USB_ACCESSORY_ADB_PRODUCT_ID); |
| 134 |
} |
| 135 |
} |