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