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 
17 package android.hardware.usb;
18 
19 import android.os.Parcel;
20 import android.os.Parcelable;
21 
22 /**
23  * A class representing an interface on a {@link UsbDevice}.
24  * USB devices can have one or more interfaces, each one providing a different
25  * piece of functionality, separate from the other interfaces.
26  * An interface will have one or more {@link UsbEndpoint}s, which are the
27  * channels by which the host transfers data with the device.
28  *
29  * <div class="special reference">
30  * <h3>Developer Guides</h3>
31  * <p>For more information about communicating with USB hardware, read the
32  * <a href="{@docRoot}guide/topics/usb/index.html">USB</a> developer guide.</p>
33  * </div>
34  */
35 public class UsbInterface implements Parcelable {
36 
37     private final int mId;
38     private final int mAlternateSetting;
39     private final String mName;
40     private final int mClass;
41     private final int mSubclass;
42     private final int mProtocol;
43     private Parcelable[] mEndpoints;
44 
45     /**
46      * UsbInterface should only be instantiated by UsbService implementation
47      * @hide
48      */
UsbInterface(int id, int alternateSetting, String name, int Class, int subClass, int protocol)49     public UsbInterface(int id, int alternateSetting, String name,
50             int Class, int subClass, int protocol) {
51         mId = id;
52         mAlternateSetting = alternateSetting;
53         mName = name;
54         mClass = Class;
55         mSubclass = subClass;
56         mProtocol = protocol;
57     }
58 
59     /**
60      * Returns the interface's bInterfaceNumber field.
61      * This is an integer that along with the alternate setting uniquely identifies
62      * the interface on the device.
63      *
64      * @return the interface's ID
65      */
getId()66     public int getId() {
67         return mId;
68     }
69 
70     /**
71      * Returns the interface's bAlternateSetting field.
72      * This is an integer that along with the ID uniquely identifies
73      * the interface on the device.
74      * {@link UsbDeviceConnection#setInterface} can be used to switch between
75      * two interfaces with the same ID but different alternate setting.
76      *
77      * @return the interface's alternate setting
78      */
getAlternateSetting()79     public int getAlternateSetting() {
80         return mAlternateSetting;
81     }
82 
83     /**
84      * Returns the interface's name.
85      *
86      * @return the interface's name
87      */
getName()88     public String getName() {
89         return mName;
90     }
91 
92     /**
93      * Returns the interface's class field.
94      * Some useful constants for USB classes can be found in {@link UsbConstants}
95      *
96      * @return the interface's class
97      */
getInterfaceClass()98     public int getInterfaceClass() {
99         return mClass;
100     }
101 
102     /**
103      * Returns the interface's subclass field.
104      *
105      * @return the interface's subclass
106      */
getInterfaceSubclass()107     public int getInterfaceSubclass() {
108         return mSubclass;
109     }
110 
111     /**
112      * Returns the interface's protocol field.
113      *
114      * @return the interface's protocol
115      */
getInterfaceProtocol()116     public int getInterfaceProtocol() {
117         return mProtocol;
118     }
119 
120     /**
121      * Returns the number of {@link android.hardware.usb.UsbEndpoint}s this interface contains.
122      *
123      * @return the number of endpoints
124      */
getEndpointCount()125     public int getEndpointCount() {
126         return mEndpoints.length;
127     }
128 
129     /**
130      * Returns the {@link android.hardware.usb.UsbEndpoint} at the given index.
131      *
132      * @return the endpoint
133      */
getEndpoint(int index)134     public UsbEndpoint getEndpoint(int index) {
135         return (UsbEndpoint)mEndpoints[index];
136     }
137 
138     /**
139      * Only used by UsbService implementation
140      * @hide
141      */
setEndpoints(Parcelable[] endpoints)142     public void setEndpoints(Parcelable[] endpoints) {
143         mEndpoints = endpoints;
144     }
145 
146     @Override
toString()147     public String toString() {
148         StringBuilder builder = new StringBuilder("UsbInterface[mId=" + mId +
149                 ",mAlternateSetting=" + mAlternateSetting +
150                 ",mName=" + mName + ",mClass=" + mClass +
151                 ",mSubclass=" + mSubclass + ",mProtocol=" + mProtocol +
152                 ",mEndpoints=[");
153         for (int i = 0; i < mEndpoints.length; i++) {
154             builder.append("\n");
155             builder.append(mEndpoints[i].toString());
156         }
157         builder.append("]");
158         return builder.toString();
159     }
160 
161     public static final Parcelable.Creator<UsbInterface> CREATOR =
162         new Parcelable.Creator<UsbInterface>() {
163         public UsbInterface createFromParcel(Parcel in) {
164             int id = in.readInt();
165             int alternateSetting = in.readInt();
166             String name = in.readString();
167             int Class = in.readInt();
168             int subClass = in.readInt();
169             int protocol = in.readInt();
170             Parcelable[] endpoints = in.readParcelableArray(UsbEndpoint.class.getClassLoader());
171             UsbInterface intf = new UsbInterface(id, alternateSetting, name, Class, subClass, protocol);
172             intf.setEndpoints(endpoints);
173             return intf;
174         }
175 
176         public UsbInterface[] newArray(int size) {
177             return new UsbInterface[size];
178         }
179     };
180 
describeContents()181     public int describeContents() {
182         return 0;
183     }
184 
writeToParcel(Parcel parcel, int flags)185     public void writeToParcel(Parcel parcel, int flags) {
186         parcel.writeInt(mId);
187         parcel.writeInt(mAlternateSetting);
188         parcel.writeString(mName);
189         parcel.writeInt(mClass);
190         parcel.writeInt(mSubclass);
191         parcel.writeInt(mProtocol);
192         parcel.writeParcelableArray(mEndpoints, 0);
193    }
194 }
195