UsbInterface.java revision 11dd5ae97b1cd5889bb66862fd12718da62a9c75
1/*
2 * Copyright (C) 2010 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 */
16
17package android.hardware.usb;
18
19import android.os.Bundle;
20import android.os.Parcel;
21import android.os.Parcelable;
22
23/**
24 * A class representing an interface on a {@link UsbDevice}.
25 * USB devices can have one or more interfaces, each one providing a different
26 * piece of functionality, separate from the other interfaces.
27 * An interface will have one or more {@link UsbEndpoint}s, which are the
28 * channels by which the host transfers data with the device.
29 */
30public class UsbInterface implements Parcelable {
31
32    private final int mId;
33    private final int mClass;
34    private final int mSubclass;
35    private final int mProtocol;
36    private final Parcelable[] mEndpoints;
37
38    /**
39     * UsbInterface should only be instantiated by UsbService implementation
40     * @hide
41     */
42    public UsbInterface(int id, int Class, int subClass, int protocol,
43            Parcelable[] endpoints) {
44        mId = id;
45        mClass = Class;
46        mSubclass = subClass;
47        mProtocol = protocol;
48        mEndpoints = endpoints;
49    }
50
51    /**
52     * Returns the interface's ID field.
53     * This is an integer that uniquely identifies the interface on the device.
54     *
55     * @return the interface's ID
56     */
57    public int getId() {
58        return mId;
59    }
60
61    /**
62     * Returns the interface's class field.
63     * Some useful constants for USB classes can be found in {@link UsbConstants}
64     *
65     * @return the interface's class
66     */
67    public int getInterfaceClass() {
68        return mClass;
69    }
70
71    /**
72     * Returns the interface's subclass field.
73     *
74     * @return the interface's subclass
75     */
76    public int getInterfaceSubclass() {
77        return mSubclass;
78    }
79
80    /**
81     * Returns the interface's protocol field.
82     *
83     * @return the interface's protocol
84     */
85    public int getInterfaceProtocol() {
86        return mProtocol;
87    }
88
89    /**
90     * Returns the number of {@link android.hardware.usb.UsbEndpoint}s this interface contains.
91     *
92     * @return the number of endpoints
93     */
94    public int getEndpointCount() {
95        return mEndpoints.length;
96    }
97
98    /**
99     * Returns the {@link android.hardware.usb.UsbEndpoint} at the given index.
100     *
101     * @return the endpoint
102     */
103    public UsbEndpoint getEndpoint(int index) {
104        return (UsbEndpoint)mEndpoints[index];
105    }
106
107    @Override
108    public String toString() {
109        return "UsbInterface[mId=" + mId + ",mClass=" + mClass +
110                ",mSubclass=" + mSubclass + ",mProtocol=" + mProtocol +
111                ",mEndpoints=" + mEndpoints + "]";
112    }
113
114    public static final Parcelable.Creator<UsbInterface> CREATOR =
115        new Parcelable.Creator<UsbInterface>() {
116        public UsbInterface createFromParcel(Parcel in) {
117            int id = in.readInt();
118            int Class = in.readInt();
119            int subClass = in.readInt();
120            int protocol = in.readInt();
121            Parcelable[] endpoints = in.readParcelableArray(UsbEndpoint.class.getClassLoader());
122            return new UsbInterface(id, Class, subClass, protocol, endpoints);
123        }
124
125        public UsbInterface[] newArray(int size) {
126            return new UsbInterface[size];
127        }
128    };
129
130    public int describeContents() {
131        return 0;
132    }
133
134    public void writeToParcel(Parcel parcel, int flags) {
135        parcel.writeInt(mId);
136        parcel.writeInt(mClass);
137        parcel.writeInt(mSubclass);
138        parcel.writeInt(mProtocol);
139        parcel.writeParcelableArray(mEndpoints, 0);
140   }
141}
142