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 common super-class for all USB Interface Descritor subtypes.
24 * see usb11.pdf section 9.6.3
25 */
26public class UsbInterfaceDescriptor extends UsbDescriptor {
27    private static final String TAG = "UsbInterfaceDescriptor";
28
29    protected byte mInterfaceNumber;  // 2:1 Number of Interface
30    protected byte mAlternateSetting; // 3:1 Value used to select alternative setting
31    protected byte mNumEndpoints;     // 4:1 Number of Endpoints used for this interface
32    protected byte mUsbClass;         // 5:1 Class Code
33    protected byte mUsbSubclass;      // 6:1 Subclass Code
34    protected byte mProtocol;         // 7:1 Protocol Code
35    protected byte mDescrIndex;       // 8:1 Index of String Descriptor Describing this interface
36
37    UsbInterfaceDescriptor(int length, byte type) {
38        super(length, type);
39        mHierarchyLevel = 3;
40    }
41
42    @Override
43    public int parseRawDescriptors(ByteStream stream) {
44        mInterfaceNumber = stream.getByte();
45        mAlternateSetting = stream.getByte();
46        mNumEndpoints = stream.getByte();
47        mUsbClass = stream.getByte();
48        mUsbSubclass = stream.getByte();
49        mProtocol = stream.getByte();
50        mDescrIndex = stream.getByte();
51
52        return mLength;
53    }
54
55    public byte getInterfaceNumber() {
56        return mInterfaceNumber;
57    }
58
59    public byte getAlternateSetting() {
60        return mAlternateSetting;
61    }
62
63    public byte getNumEndpoints() {
64        return mNumEndpoints;
65    }
66
67    public byte getUsbClass() {
68        return mUsbClass;
69    }
70
71    public byte getUsbSubclass() {
72        return mUsbSubclass;
73    }
74
75    public byte getProtocol() {
76        return mProtocol;
77    }
78
79    public byte getDescrIndex() {
80        return mDescrIndex;
81    }
82
83    @Override
84    public void report(ReportCanvas canvas) {
85        super.report(canvas);
86
87        byte usbClass = getUsbClass();
88        byte usbSubclass = getUsbSubclass();
89        byte protocol = getProtocol();
90        String className = UsbStrings.getClassName(usbClass);
91        String subclassName = "";
92        if (usbClass == UsbDescriptor.CLASSID_AUDIO) {
93            subclassName = UsbStrings.getAudioSubclassName(usbSubclass);
94        }
95
96        canvas.openList();
97        canvas.writeListItem("Interface #" + getInterfaceNumber());
98        canvas.writeListItem("Class: " + ReportCanvas.getHexString(usbClass) + ": " + className);
99        canvas.writeListItem("Subclass: "
100                + ReportCanvas.getHexString(usbSubclass) + ": " + subclassName);
101        canvas.writeListItem("Protocol: " + protocol + ": " + ReportCanvas.getHexString(protocol));
102        canvas.writeListItem("Endpoints: " + getNumEndpoints());
103        canvas.closeList();
104    }
105}
106