Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 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 | |
Mike Lockwood | c4308f0 | 2011-03-01 08:04:54 -0800 | [diff] [blame] | 17 | package android.hardware.usb; |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 18 | |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 19 | import android.os.Parcel; |
| 20 | import android.os.Parcelable; |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 21 | |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 22 | /** |
Mike Lockwood | 11dd5ae | 2011-04-01 14:00:08 -0400 | [diff] [blame] | 23 | * This class represents a USB device attached to the android device with the android device |
| 24 | * acting as the USB host. |
| 25 | * Each device contains one or more {@link UsbInterface}s, each of which contains a number of |
| 26 | * {@link UsbEndpoint}s (the channels via which data is transmitted over USB). |
| 27 | * |
| 28 | * <p> This class contains information (along with {@link UsbInterface} and {@link UsbEndpoint}) |
| 29 | * that describes the capabilities of the USB device. |
| 30 | * To communicate with the device, you open a {@link UsbDeviceConnection} for the device |
| 31 | * and use {@link UsbRequest} to send and receive data on an endpoint. |
| 32 | * {@link UsbDeviceConnection#controlTransfer} is used for control requests on endpoint zero. |
Joe Fernandez | 3aef8e1d | 2011-12-20 10:38:34 -0800 | [diff] [blame] | 33 | * |
| 34 | * <div class="special reference"> |
| 35 | * <h3>Developer Guides</h3> |
| 36 | * <p>For more information about communicating with USB hardware, read the |
| 37 | * <a href="{@docRoot}guide/topics/usb/index.html">USB</a> developer guide.</p> |
| 38 | * </div> |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 39 | */ |
Mike Lockwood | acc29cc | 2011-03-11 08:18:08 -0500 | [diff] [blame] | 40 | public class UsbDevice implements Parcelable { |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 41 | |
| 42 | private static final String TAG = "UsbDevice"; |
| 43 | |
Mike Lockwood | acc29cc | 2011-03-11 08:18:08 -0500 | [diff] [blame] | 44 | private final String mName; |
Robin Cutshaw | 575ca85 | 2012-05-01 19:45:25 -0400 | [diff] [blame] | 45 | private final String mManufacturerName; |
| 46 | private final String mProductName; |
| 47 | private final String mSerialNumber; |
Mike Lockwood | acc29cc | 2011-03-11 08:18:08 -0500 | [diff] [blame] | 48 | private final int mVendorId; |
| 49 | private final int mProductId; |
| 50 | private final int mClass; |
| 51 | private final int mSubclass; |
| 52 | private final int mProtocol; |
| 53 | private final Parcelable[] mInterfaces; |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 54 | |
| 55 | /** |
| 56 | * UsbDevice should only be instantiated by UsbService implementation |
| 57 | * @hide |
| 58 | */ |
| 59 | public UsbDevice(String name, int vendorId, int productId, |
Robin Cutshaw | 575ca85 | 2012-05-01 19:45:25 -0400 | [diff] [blame] | 60 | int Class, int subClass, int protocol, |
| 61 | String manufacturerName, String productName, String serialNumber, |
| 62 | Parcelable[] interfaces) { |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 63 | mName = name; |
| 64 | mVendorId = vendorId; |
| 65 | mProductId = productId; |
| 66 | mClass = Class; |
| 67 | mSubclass = subClass; |
| 68 | mProtocol = protocol; |
Robin Cutshaw | 575ca85 | 2012-05-01 19:45:25 -0400 | [diff] [blame] | 69 | mManufacturerName = manufacturerName; |
| 70 | mProductName = productName; |
| 71 | mSerialNumber = serialNumber; |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 72 | mInterfaces = interfaces; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Returns the name of the device. |
| 77 | * In the standard implementation, this is the path of the device file |
| 78 | * for the device in the usbfs file system. |
| 79 | * |
| 80 | * @return the device name |
| 81 | */ |
| 82 | public String getDeviceName() { |
| 83 | return mName; |
| 84 | } |
| 85 | |
| 86 | /** |
Robin Cutshaw | 575ca85 | 2012-05-01 19:45:25 -0400 | [diff] [blame] | 87 | * Returns the manufacturer name of the device. |
| 88 | * |
| 89 | * @return the manufacturer name |
| 90 | */ |
| 91 | public String getManufacturerName() { |
| 92 | return mManufacturerName; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Returns the product name of the device. |
| 97 | * |
| 98 | * @return the product name |
| 99 | */ |
| 100 | public String getProductName() { |
| 101 | return mProductName; |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Returns the serial number of the device. |
| 106 | * |
| 107 | * @return the serial number name |
| 108 | */ |
| 109 | public String getSerialNumber() { |
| 110 | return mSerialNumber; |
| 111 | } |
| 112 | |
| 113 | /** |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 114 | * Returns a unique integer ID for the device. |
| 115 | * This is a convenience for clients that want to use an integer to represent |
| 116 | * the device, rather than the device name. |
| 117 | * IDs are not persistent across USB disconnects. |
| 118 | * |
| 119 | * @return the device ID |
| 120 | */ |
| 121 | public int getDeviceId() { |
| 122 | return getDeviceId(mName); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Returns a vendor ID for the device. |
| 127 | * |
| 128 | * @return the device vendor ID |
| 129 | */ |
| 130 | public int getVendorId() { |
| 131 | return mVendorId; |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Returns a product ID for the device. |
| 136 | * |
| 137 | * @return the device product ID |
| 138 | */ |
| 139 | public int getProductId() { |
| 140 | return mProductId; |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Returns the devices's class field. |
Mike Lockwood | 11dd5ae | 2011-04-01 14:00:08 -0400 | [diff] [blame] | 145 | * Some useful constants for USB device classes can be found in {@link UsbConstants}. |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 146 | * |
| 147 | * @return the devices's class |
| 148 | */ |
| 149 | public int getDeviceClass() { |
| 150 | return mClass; |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Returns the device's subclass field. |
| 155 | * |
| 156 | * @return the device's subclass |
| 157 | */ |
| 158 | public int getDeviceSubclass() { |
| 159 | return mSubclass; |
| 160 | } |
| 161 | |
| 162 | /** |
Mike Lockwood | 11dd5ae | 2011-04-01 14:00:08 -0400 | [diff] [blame] | 163 | * Returns the device's protocol field. |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 164 | * |
| 165 | * @return the device's protocol |
| 166 | */ |
| 167 | public int getDeviceProtocol() { |
| 168 | return mProtocol; |
| 169 | } |
| 170 | |
| 171 | /** |
Mike Lockwood | 11dd5ae | 2011-04-01 14:00:08 -0400 | [diff] [blame] | 172 | * Returns the number of {@link UsbInterface}s this device contains. |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 173 | * |
| 174 | * @return the number of interfaces |
| 175 | */ |
| 176 | public int getInterfaceCount() { |
| 177 | return mInterfaces.length; |
| 178 | } |
| 179 | |
| 180 | /** |
Mike Lockwood | 11dd5ae | 2011-04-01 14:00:08 -0400 | [diff] [blame] | 181 | * Returns the {@link UsbInterface} at the given index. |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 182 | * |
| 183 | * @return the interface |
| 184 | */ |
| 185 | public UsbInterface getInterface(int index) { |
| 186 | return (UsbInterface)mInterfaces[index]; |
| 187 | } |
| 188 | |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 189 | @Override |
| 190 | public boolean equals(Object o) { |
| 191 | if (o instanceof UsbDevice) { |
| 192 | return ((UsbDevice)o).mName.equals(mName); |
| 193 | } else if (o instanceof String) { |
| 194 | return ((String)o).equals(mName); |
| 195 | } else { |
| 196 | return false; |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | @Override |
Mike Lockwood | c6f23e8 | 2011-03-09 12:05:20 -0500 | [diff] [blame] | 201 | public int hashCode() { |
| 202 | return mName.hashCode(); |
| 203 | } |
| 204 | |
| 205 | @Override |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 206 | public String toString() { |
| 207 | return "UsbDevice[mName=" + mName + ",mVendorId=" + mVendorId + |
| 208 | ",mProductId=" + mProductId + ",mClass=" + mClass + |
| 209 | ",mSubclass=" + mSubclass + ",mProtocol=" + mProtocol + |
Robin Cutshaw | 575ca85 | 2012-05-01 19:45:25 -0400 | [diff] [blame] | 210 | ",mManufacturerName=" + mManufacturerName + ",mProductName=" + mProductName + |
| 211 | ",mSerialNumber=" + mSerialNumber + ",mInterfaces=" + mInterfaces + "]"; |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | public static final Parcelable.Creator<UsbDevice> CREATOR = |
| 215 | new Parcelable.Creator<UsbDevice>() { |
| 216 | public UsbDevice createFromParcel(Parcel in) { |
| 217 | String name = in.readString(); |
| 218 | int vendorId = in.readInt(); |
| 219 | int productId = in.readInt(); |
| 220 | int clasz = in.readInt(); |
| 221 | int subClass = in.readInt(); |
| 222 | int protocol = in.readInt(); |
Robin Cutshaw | 575ca85 | 2012-05-01 19:45:25 -0400 | [diff] [blame] | 223 | String manufacturerName = in.readString(); |
| 224 | String productName = in.readString(); |
| 225 | String serialNumber = in.readString(); |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 226 | Parcelable[] interfaces = in.readParcelableArray(UsbInterface.class.getClassLoader()); |
Robin Cutshaw | 575ca85 | 2012-05-01 19:45:25 -0400 | [diff] [blame] | 227 | return new UsbDevice(name, vendorId, productId, clasz, subClass, protocol, |
| 228 | manufacturerName, productName, serialNumber, interfaces); |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | public UsbDevice[] newArray(int size) { |
| 232 | return new UsbDevice[size]; |
| 233 | } |
| 234 | }; |
| 235 | |
| 236 | public int describeContents() { |
| 237 | return 0; |
| 238 | } |
| 239 | |
| 240 | public void writeToParcel(Parcel parcel, int flags) { |
| 241 | parcel.writeString(mName); |
| 242 | parcel.writeInt(mVendorId); |
| 243 | parcel.writeInt(mProductId); |
| 244 | parcel.writeInt(mClass); |
| 245 | parcel.writeInt(mSubclass); |
| 246 | parcel.writeInt(mProtocol); |
Robin Cutshaw | 575ca85 | 2012-05-01 19:45:25 -0400 | [diff] [blame] | 247 | parcel.writeString(mManufacturerName); |
| 248 | parcel.writeString(mProductName); |
| 249 | parcel.writeString(mSerialNumber); |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 250 | parcel.writeParcelableArray(mInterfaces, 0); |
| 251 | } |
| 252 | |
| 253 | public static int getDeviceId(String name) { |
| 254 | return native_get_device_id(name); |
| 255 | } |
| 256 | |
| 257 | public static String getDeviceName(int id) { |
| 258 | return native_get_device_name(id); |
| 259 | } |
| 260 | |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 261 | private static native int native_get_device_id(String name); |
| 262 | private static native String native_get_device_name(int id); |
| 263 | } |