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 *
30 * <div class="special reference">
31 * <h3>Developer Guides</h3>
32 * <p>For more information about communicating with USB hardware, read the
33 * <a href="{@docRoot}guide/topics/usb/index.html">USB</a> developer guide.</p>
34 * </div>
35 */
36public class UsbInterface implements Parcelable {
37
38    private final int mId;
39    private final int mClass;
40    private final int mSubclass;
41    private final int mProtocol;
42    private final Parcelable[] mEndpoints;
43
44    /**
45     * UsbInterface should only be instantiated by UsbService implementation
46     * @hide
47     */
48    public UsbInterface(int id, int Class, int subClass, int protocol,
49            Parcelable[] endpoints) {
50        mId = id;
51        mClass = Class;
52        mSubclass = subClass;
53        mProtocol = protocol;
54        mEndpoints = endpoints;
55    }
56
57    /**
58     * Returns the interface's ID field.
59     * This is an integer that uniquely identifies the interface on the device.
60     *
61     * @return the interface's ID
62     */
63    public int getId() {
64        return mId;
65    }
66
67    /**
68     * Returns the interface's class field.
69     * Some useful constants for USB classes can be found in {@link UsbConstants}
70     *
71     * @return the interface's class
72     */
73    public int getInterfaceClass() {
74        return mClass;
75    }
76
77    /**
78     * Returns the interface's subclass field.
79     *
80     * @return the interface's subclass
81     */
82    public int getInterfaceSubclass() {
83        return mSubclass;
84    }
85
86    /**
87     * Returns the interface's protocol field.
88     *
89     * @return the interface's protocol
90     */
91    public int getInterfaceProtocol() {
92        return mProtocol;
93    }
94
95    /**
96     * Returns the number of {@link android.hardware.usb.UsbEndpoint}s this interface contains.
97     *
98     * @return the number of endpoints
99     */
100    public int getEndpointCount() {
101        return mEndpoints.length;
102    }
103
104    /**
105     * Returns the {@link android.hardware.usb.UsbEndpoint} at the given index.
106     *
107     * @return the endpoint
108     */
109    public UsbEndpoint getEndpoint(int index) {
110        return (UsbEndpoint)mEndpoints[index];
111    }
112
113    @Override
114    public String toString() {
115        return "UsbInterface[mId=" + mId + ",mClass=" + mClass +
116                ",mSubclass=" + mSubclass + ",mProtocol=" + mProtocol +
117                ",mEndpoints=" + mEndpoints + "]";
118    }
119
120    public static final Parcelable.Creator<UsbInterface> CREATOR =
121        new Parcelable.Creator<UsbInterface>() {
122        public UsbInterface createFromParcel(Parcel in) {
123            int id = in.readInt();
124            int Class = in.readInt();
125            int subClass = in.readInt();
126            int protocol = in.readInt();
127            Parcelable[] endpoints = in.readParcelableArray(UsbEndpoint.class.getClassLoader());
128            return new UsbInterface(id, Class, subClass, protocol, endpoints);
129        }
130
131        public UsbInterface[] newArray(int size) {
132            return new UsbInterface[size];
133        }
134    };
135
136    public int describeContents() {
137        return 0;
138    }
139
140    public void writeToParcel(Parcel parcel, int flags) {
141        parcel.writeInt(mId);
142        parcel.writeInt(mClass);
143        parcel.writeInt(mSubclass);
144        parcel.writeInt(mProtocol);
145        parcel.writeParcelableArray(mEndpoints, 0);
146   }
147}
148