blob: b0ba9c193ecff22eaa0df28dfb82d9690a5164c3 [file] [log] [blame]
Mike Lockwoode7d511e2010-12-30 13:39:37 -05001/*
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 Lockwoodc4308f02011-03-01 08:04:54 -080017package android.hardware.usb;
Mike Lockwoode7d511e2010-12-30 13:39:37 -050018
Mike Lockwoode7d511e2010-12-30 13:39:37 -050019import android.os.Parcel;
20import android.os.Parcelable;
Mike Lockwoode7d511e2010-12-30 13:39:37 -050021
Mike Lockwoode7d511e2010-12-30 13:39:37 -050022/**
Mike Lockwood11dd5ae2011-04-01 14:00:08 -040023 * 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 Fernandez3aef8e1d2011-12-20 10:38:34 -080033 *
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 Lockwoode7d511e2010-12-30 13:39:37 -050039 */
Mike Lockwoodacc29cc2011-03-11 08:18:08 -050040public class UsbDevice implements Parcelable {
Mike Lockwoode7d511e2010-12-30 13:39:37 -050041
42 private static final String TAG = "UsbDevice";
43
Mike Lockwoodacc29cc2011-03-11 08:18:08 -050044 private final String mName;
Robin Cutshaw575ca852012-05-01 19:45:25 -040045 private final String mManufacturerName;
46 private final String mProductName;
47 private final String mSerialNumber;
Mike Lockwoodacc29cc2011-03-11 08:18:08 -050048 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 Lockwoode7d511e2010-12-30 13:39:37 -050054
55 /**
56 * UsbDevice should only be instantiated by UsbService implementation
57 * @hide
58 */
59 public UsbDevice(String name, int vendorId, int productId,
Robin Cutshaw575ca852012-05-01 19:45:25 -040060 int Class, int subClass, int protocol,
61 String manufacturerName, String productName, String serialNumber,
62 Parcelable[] interfaces) {
Mike Lockwoode7d511e2010-12-30 13:39:37 -050063 mName = name;
64 mVendorId = vendorId;
65 mProductId = productId;
66 mClass = Class;
67 mSubclass = subClass;
68 mProtocol = protocol;
Robin Cutshaw575ca852012-05-01 19:45:25 -040069 mManufacturerName = manufacturerName;
70 mProductName = productName;
71 mSerialNumber = serialNumber;
Mike Lockwoode7d511e2010-12-30 13:39:37 -050072 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 Cutshaw575ca852012-05-01 19:45:25 -040087 * 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 Lockwoode7d511e2010-12-30 13:39:37 -0500114 * 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 Lockwood11dd5ae2011-04-01 14:00:08 -0400145 * Some useful constants for USB device classes can be found in {@link UsbConstants}.
Mike Lockwoode7d511e2010-12-30 13:39:37 -0500146 *
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 Lockwood11dd5ae2011-04-01 14:00:08 -0400163 * Returns the device's protocol field.
Mike Lockwoode7d511e2010-12-30 13:39:37 -0500164 *
165 * @return the device's protocol
166 */
167 public int getDeviceProtocol() {
168 return mProtocol;
169 }
170
171 /**
Mike Lockwood11dd5ae2011-04-01 14:00:08 -0400172 * Returns the number of {@link UsbInterface}s this device contains.
Mike Lockwoode7d511e2010-12-30 13:39:37 -0500173 *
174 * @return the number of interfaces
175 */
176 public int getInterfaceCount() {
177 return mInterfaces.length;
178 }
179
180 /**
Mike Lockwood11dd5ae2011-04-01 14:00:08 -0400181 * Returns the {@link UsbInterface} at the given index.
Mike Lockwoode7d511e2010-12-30 13:39:37 -0500182 *
183 * @return the interface
184 */
185 public UsbInterface getInterface(int index) {
186 return (UsbInterface)mInterfaces[index];
187 }
188
Mike Lockwoode7d511e2010-12-30 13:39:37 -0500189 @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 Lockwoodc6f23e82011-03-09 12:05:20 -0500201 public int hashCode() {
202 return mName.hashCode();
203 }
204
205 @Override
Mike Lockwoode7d511e2010-12-30 13:39:37 -0500206 public String toString() {
207 return "UsbDevice[mName=" + mName + ",mVendorId=" + mVendorId +
208 ",mProductId=" + mProductId + ",mClass=" + mClass +
209 ",mSubclass=" + mSubclass + ",mProtocol=" + mProtocol +
Robin Cutshaw575ca852012-05-01 19:45:25 -0400210 ",mManufacturerName=" + mManufacturerName + ",mProductName=" + mProductName +
211 ",mSerialNumber=" + mSerialNumber + ",mInterfaces=" + mInterfaces + "]";
Mike Lockwoode7d511e2010-12-30 13:39:37 -0500212 }
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 Cutshaw575ca852012-05-01 19:45:25 -0400223 String manufacturerName = in.readString();
224 String productName = in.readString();
225 String serialNumber = in.readString();
Mike Lockwoode7d511e2010-12-30 13:39:37 -0500226 Parcelable[] interfaces = in.readParcelableArray(UsbInterface.class.getClassLoader());
Robin Cutshaw575ca852012-05-01 19:45:25 -0400227 return new UsbDevice(name, vendorId, productId, clasz, subClass, protocol,
228 manufacturerName, productName, serialNumber, interfaces);
Mike Lockwoode7d511e2010-12-30 13:39:37 -0500229 }
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 Cutshaw575ca852012-05-01 19:45:25 -0400247 parcel.writeString(mManufacturerName);
248 parcel.writeString(mProductName);
249 parcel.writeString(mSerialNumber);
Mike Lockwoode7d511e2010-12-30 13:39:37 -0500250 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 Lockwoode7d511e2010-12-30 13:39:37 -0500261 private static native int native_get_device_id(String name);
262 private static native String native_get_device_name(int id);
263}