1/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package com.android.server.usb.descriptors;
17
18import com.android.server.usb.descriptors.report.ReportCanvas;
19import com.android.server.usb.descriptors.report.UsbStrings;
20
21/**
22 * @hide
23 * A USB Device Descriptor.
24 * see usb11.pdf section 9.6.1
25 */
26public final class UsbDeviceDescriptor extends UsbDescriptor {
27    private static final String TAG = "UsbDeviceDescriptor";
28
29    public static final int USBSPEC_1_0 = 0x0100;
30    public static final int USBSPEC_1_1 = 0x0110;
31    public static final int USBSPEC_2_0 = 0x0200;
32
33    private int mSpec;          // 2:2 bcdUSB 2 BCD USB Specification Number - BCD
34    private byte mDevClass;     // 4:1 class code
35    private byte mDevSubClass;  // 5:1 subclass code
36    private byte mProtocol;     // 6:1 protocol
37    private byte mPacketSize;   // 7:1 Maximum Packet Size for Zero Endpoint.
38                                // Valid Sizes are 8, 16, 32, 64
39    private int mVendorID;      // 8:2 vendor ID
40    private int mProductID;     // 10:2 product ID
41    private int mDeviceRelease; // 12:2 Device Release number - BCD
42    private byte mMfgIndex;     // 14:1 Index of Manufacturer String Descriptor
43    private byte mProductIndex; // 15:1 Index of Product String Descriptor
44    private byte mSerialNum;    // 16:1 Index of Serial Number String Descriptor
45    private byte mNumConfigs;   // 17:1 Number of Possible Configurations
46
47    UsbDeviceDescriptor(int length, byte type) {
48        super(length, type);
49        mHierarchyLevel = 1;
50    }
51
52    public int getSpec() {
53        return mSpec;
54    }
55
56    public byte getDevClass() {
57        return mDevClass;
58    }
59
60    public byte getDevSubClass() {
61        return mDevSubClass;
62    }
63
64    public byte getProtocol() {
65        return mProtocol;
66    }
67
68    public byte getPacketSize() {
69        return mPacketSize;
70    }
71
72    public int getVendorID() {
73        return mVendorID;
74    }
75
76    public int getProductID() {
77        return mProductID;
78    }
79
80    public int getDeviceRelease() {
81        return mDeviceRelease;
82    }
83
84    public byte getMfgIndex() {
85        return mMfgIndex;
86    }
87
88    public byte getProductIndex() {
89        return mProductIndex;
90    }
91
92    public byte getSerialNum() {
93        return mSerialNum;
94    }
95
96    public byte getNumConfigs() {
97        return mNumConfigs;
98    }
99
100    @Override
101    public int parseRawDescriptors(ByteStream stream) {
102        mSpec = stream.unpackUsbShort();
103        mDevClass = stream.getByte();
104        mDevSubClass = stream.getByte();
105        mProtocol = stream.getByte();
106        mPacketSize = stream.getByte();
107        mVendorID = stream.unpackUsbShort();
108        mProductID = stream.unpackUsbShort();
109        mDeviceRelease = stream.unpackUsbShort();
110        mMfgIndex = stream.getByte();
111        mProductIndex = stream.getByte();
112        mSerialNum = stream.getByte();
113        mNumConfigs = stream.getByte();
114
115        return mLength;
116    }
117
118    @Override
119    public void report(ReportCanvas canvas) {
120        super.report(canvas);
121
122        canvas.openList();
123
124        int spec = getSpec();
125        canvas.writeListItem("Spec: " + ReportCanvas.getBCDString(spec));
126
127        byte devClass = getDevClass();
128        String classStr = UsbStrings.getClassName(devClass);
129        byte devSubClass = getDevSubClass();
130        String subClasStr = UsbStrings.getClassName(devSubClass);
131        canvas.writeListItem("Class " + devClass + ": " + classStr + " Subclass"
132                + devSubClass + ": " + subClasStr);
133        canvas.writeListItem("Vendor ID: " + ReportCanvas.getHexString(getVendorID())
134                + " Product ID: " + ReportCanvas.getHexString(getProductID())
135                + " Product Release: " + ReportCanvas.getBCDString(getDeviceRelease()));
136
137        byte mfgIndex = getMfgIndex();
138        String manufacturer =
139                UsbDescriptor.getUsbDescriptorString(canvas.getConnection(), mfgIndex);
140        byte productIndex = getProductIndex();
141        String product =
142                UsbDescriptor.getUsbDescriptorString(canvas.getConnection(), productIndex);
143
144        canvas.writeListItem("Manufacturer " + mfgIndex + ": " + manufacturer
145                + " Product " + productIndex + ": " + product);
146        canvas.closeList();
147    }
148}
149