aboutsummaryrefslogtreecommitdiff
path: root/car-usb-handler/src/android/car/usb/handler/AoapInterface.java
blob: e4d843f7520b9e9bcdc8a816337d05e2c9096308 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/**
 * Copyright (C) 2016 The Android Open Source Project
 *
 * <p>Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a copy of the License at
 *
 * <p>http://www.apache.org/licenses/LICENSE-2.0
 *
 * <p>Unless required by applicable law or agreed to in writing, software distributed under the
 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied. See the License for the specific language governing permissions and
 * limitations under the License.
 */
package android.car.usb.handler;

import android.hardware.usb.UsbConstants;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbDeviceConnection;
import android.util.Log;
import java.io.IOException;

final class AoapInterface {
    /**
     * Use Google Vendor ID when in accessory mode
     */
    public static final int USB_ACCESSORY_VENDOR_ID = 0x18D1;

    /**
     * Product ID to use when in accessory mode
     */
    public static final int USB_ACCESSORY_PRODUCT_ID = 0x2D00;

    /**
     * Product ID to use when in accessory mode and adb is enabled
     */
    public static final int USB_ACCESSORY_ADB_PRODUCT_ID = 0x2D01;

    /**
     * Indexes for strings sent by the host via ACCESSORY_SEND_STRING
     */
    public static final int ACCESSORY_STRING_MANUFACTURER = 0;
    public static final int ACCESSORY_STRING_MODEL = 1;
    public static final int ACCESSORY_STRING_DESCRIPTION = 2;
    public static final int ACCESSORY_STRING_VERSION = 3;
    public static final int ACCESSORY_STRING_URI = 4;
    public static final int ACCESSORY_STRING_SERIAL = 5;

    /**
     * Control request for retrieving device's protocol version
     *
     *  requestType:    USB_DIR_IN | USB_TYPE_VENDOR
     *  request:        ACCESSORY_GET_PROTOCOL
     *  value:          0
     *  index:          0
     *  data            version number (16 bits little endian)
     *                     1 for original accessory support
     *                     2 adds HID and device to host audio support
     */
    public static final int ACCESSORY_GET_PROTOCOL = 51;

    /**
     * Control request for host to send a string to the device
     *
     *  requestType:    USB_DIR_OUT | USB_TYPE_VENDOR
     *  request:        ACCESSORY_SEND_STRING
     *  value:          0
     *  index:          string ID
     *  data            zero terminated UTF8 string
     *
     *  The device can later retrieve these strings via the
     *  ACCESSORY_GET_STRING_* ioctls
     */
    public static final int ACCESSORY_SEND_STRING = 52;

    /**
     * Control request for starting device in accessory mode.
     * The host sends this after setting all its strings to the device.
     *
     *  requestType:    USB_DIR_OUT | USB_TYPE_VENDOR
     *  request:        ACCESSORY_START
     *  value:          0
     *  index:          0
     *  data            none
     */
    public static final int ACCESSORY_START = 53;

    /**
     * Max payload size for AOAP. Limited by driver.
     */
    public static final int MAX_PAYLOAD_SIZE = 16384;

    /**
     * Accessory write timeout.
     */
    public static final int AOAP_TIMEOUT_MS = 2000;

    private static final String TAG = AoapInterface.class.getSimpleName();

    public static int getProtocol(UsbDeviceConnection conn) {
        byte[] buffer = new byte[2];
        int len = conn.controlTransfer(
                UsbConstants.USB_DIR_IN | UsbConstants.USB_TYPE_VENDOR,
                ACCESSORY_GET_PROTOCOL, 0, 0, buffer, 2, AOAP_TIMEOUT_MS);
        if (len != 2) {
            return -1;
        }
        return (buffer[1] << 8) | buffer[0];
    }

    public static boolean isSupported(UsbDeviceConnection conn) {
        return getProtocol(conn) >= 1;
    }

    public static void sendString(UsbDeviceConnection conn, int index, String string)
            throws IOException {
        byte[] buffer = (string + "\0").getBytes();
        int len = conn.controlTransfer(
                UsbConstants.USB_DIR_OUT | UsbConstants.USB_TYPE_VENDOR,
                ACCESSORY_SEND_STRING, 0, index,
                buffer, buffer.length, AOAP_TIMEOUT_MS);
        if (len != buffer.length) {
            throw new IOException("Failed to send string " + index + ": \"" + string + "\"");
        } else {
            Log.i(TAG, "Sent string " + index + ": \"" + string + "\"");
        }
    }

    public static void sendAoapStart(UsbDeviceConnection conn) throws IOException {
        int len = conn.controlTransfer(
                UsbConstants.USB_DIR_OUT | UsbConstants.USB_TYPE_VENDOR,
                ACCESSORY_START, 0, 0, null, 0, AOAP_TIMEOUT_MS);
        if (len < 0) {
            throw new IOException("Control transfer for accessory start failed: " + len);
        }
    }

    public static boolean isDeviceInAoapMode(UsbDevice device) {
        if (device == null) {
            return false;
        }
        final int vid = device.getVendorId();
        final int pid = device.getProductId();
        return vid == USB_ACCESSORY_VENDOR_ID
                && (pid == USB_ACCESSORY_PRODUCT_ID || pid == USB_ACCESSORY_ADB_PRODUCT_ID);
    }
}