blob: 9bd38f9c33e839327ef5f165b967e59b60886b6c [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
19import android.os.Bundle;
20import android.os.Parcel;
21import android.os.Parcelable;
Mike Lockwoode7d511e2010-12-30 13:39:37 -050022import android.util.Log;
23
24import java.io.FileDescriptor;
25
Mike Lockwoode7d511e2010-12-30 13:39:37 -050026/**
Mike Lockwood11dd5ae2011-04-01 14:00:08 -040027 * This class represents a USB device attached to the android device with the android device
28 * acting as the USB host.
29 * Each device contains one or more {@link UsbInterface}s, each of which contains a number of
30 * {@link UsbEndpoint}s (the channels via which data is transmitted over USB).
31 *
32 * <p> This class contains information (along with {@link UsbInterface} and {@link UsbEndpoint})
33 * that describes the capabilities of the USB device.
34 * To communicate with the device, you open a {@link UsbDeviceConnection} for the device
35 * and use {@link UsbRequest} to send and receive data on an endpoint.
36 * {@link UsbDeviceConnection#controlTransfer} is used for control requests on endpoint zero.
Joe Fernandez3aef8e1d2011-12-20 10:38:34 -080037 *
38 * <div class="special reference">
39 * <h3>Developer Guides</h3>
40 * <p>For more information about communicating with USB hardware, read the
41 * <a href="{@docRoot}guide/topics/usb/index.html">USB</a> developer guide.</p>
42 * </div>
Mike Lockwoode7d511e2010-12-30 13:39:37 -050043 */
Mike Lockwoodacc29cc2011-03-11 08:18:08 -050044public class UsbDevice implements Parcelable {
Mike Lockwoode7d511e2010-12-30 13:39:37 -050045
46 private static final String TAG = "UsbDevice";
47
Mike Lockwoodacc29cc2011-03-11 08:18:08 -050048 private final String mName;
49 private final int mVendorId;
50 private final int mProductId;
51 private final int mClass;
52 private final int mSubclass;
53 private final int mProtocol;
54 private final Parcelable[] mInterfaces;
Mike Lockwoode7d511e2010-12-30 13:39:37 -050055
56 /**
57 * UsbDevice should only be instantiated by UsbService implementation
58 * @hide
59 */
60 public UsbDevice(String name, int vendorId, int productId,
61 int Class, int subClass, int protocol, Parcelable[] interfaces) {
62 mName = name;
63 mVendorId = vendorId;
64 mProductId = productId;
65 mClass = Class;
66 mSubclass = subClass;
67 mProtocol = protocol;
68 mInterfaces = interfaces;
69 }
70
71 /**
72 * Returns the name of the device.
73 * In the standard implementation, this is the path of the device file
74 * for the device in the usbfs file system.
75 *
76 * @return the device name
77 */
78 public String getDeviceName() {
79 return mName;
80 }
81
82 /**
83 * Returns a unique integer ID for the device.
84 * This is a convenience for clients that want to use an integer to represent
85 * the device, rather than the device name.
86 * IDs are not persistent across USB disconnects.
87 *
88 * @return the device ID
89 */
90 public int getDeviceId() {
91 return getDeviceId(mName);
92 }
93
94 /**
95 * Returns a vendor ID for the device.
96 *
97 * @return the device vendor ID
98 */
99 public int getVendorId() {
100 return mVendorId;
101 }
102
103 /**
104 * Returns a product ID for the device.
105 *
106 * @return the device product ID
107 */
108 public int getProductId() {
109 return mProductId;
110 }
111
112 /**
113 * Returns the devices's class field.
Mike Lockwood11dd5ae2011-04-01 14:00:08 -0400114 * Some useful constants for USB device classes can be found in {@link UsbConstants}.
Mike Lockwoode7d511e2010-12-30 13:39:37 -0500115 *
116 * @return the devices's class
117 */
118 public int getDeviceClass() {
119 return mClass;
120 }
121
122 /**
123 * Returns the device's subclass field.
124 *
125 * @return the device's subclass
126 */
127 public int getDeviceSubclass() {
128 return mSubclass;
129 }
130
131 /**
Mike Lockwood11dd5ae2011-04-01 14:00:08 -0400132 * Returns the device's protocol field.
Mike Lockwoode7d511e2010-12-30 13:39:37 -0500133 *
134 * @return the device's protocol
135 */
136 public int getDeviceProtocol() {
137 return mProtocol;
138 }
139
140 /**
Mike Lockwood11dd5ae2011-04-01 14:00:08 -0400141 * Returns the number of {@link UsbInterface}s this device contains.
Mike Lockwoode7d511e2010-12-30 13:39:37 -0500142 *
143 * @return the number of interfaces
144 */
145 public int getInterfaceCount() {
146 return mInterfaces.length;
147 }
148
149 /**
Mike Lockwood11dd5ae2011-04-01 14:00:08 -0400150 * Returns the {@link UsbInterface} at the given index.
Mike Lockwoode7d511e2010-12-30 13:39:37 -0500151 *
152 * @return the interface
153 */
154 public UsbInterface getInterface(int index) {
155 return (UsbInterface)mInterfaces[index];
156 }
157
Mike Lockwoode7d511e2010-12-30 13:39:37 -0500158 @Override
159 public boolean equals(Object o) {
160 if (o instanceof UsbDevice) {
161 return ((UsbDevice)o).mName.equals(mName);
162 } else if (o instanceof String) {
163 return ((String)o).equals(mName);
164 } else {
165 return false;
166 }
167 }
168
169 @Override
Mike Lockwoodc6f23e82011-03-09 12:05:20 -0500170 public int hashCode() {
171 return mName.hashCode();
172 }
173
174 @Override
Mike Lockwoode7d511e2010-12-30 13:39:37 -0500175 public String toString() {
176 return "UsbDevice[mName=" + mName + ",mVendorId=" + mVendorId +
177 ",mProductId=" + mProductId + ",mClass=" + mClass +
178 ",mSubclass=" + mSubclass + ",mProtocol=" + mProtocol +
179 ",mInterfaces=" + mInterfaces + "]";
180 }
181
182 public static final Parcelable.Creator<UsbDevice> CREATOR =
183 new Parcelable.Creator<UsbDevice>() {
184 public UsbDevice createFromParcel(Parcel in) {
185 String name = in.readString();
186 int vendorId = in.readInt();
187 int productId = in.readInt();
188 int clasz = in.readInt();
189 int subClass = in.readInt();
190 int protocol = in.readInt();
191 Parcelable[] interfaces = in.readParcelableArray(UsbInterface.class.getClassLoader());
Mike Lockwoodacc29cc2011-03-11 08:18:08 -0500192 return new UsbDevice(name, vendorId, productId, clasz, subClass, protocol, interfaces);
Mike Lockwoode7d511e2010-12-30 13:39:37 -0500193 }
194
195 public UsbDevice[] newArray(int size) {
196 return new UsbDevice[size];
197 }
198 };
199
200 public int describeContents() {
201 return 0;
202 }
203
204 public void writeToParcel(Parcel parcel, int flags) {
205 parcel.writeString(mName);
206 parcel.writeInt(mVendorId);
207 parcel.writeInt(mProductId);
208 parcel.writeInt(mClass);
209 parcel.writeInt(mSubclass);
210 parcel.writeInt(mProtocol);
211 parcel.writeParcelableArray(mInterfaces, 0);
212 }
213
214 public static int getDeviceId(String name) {
215 return native_get_device_id(name);
216 }
217
218 public static String getDeviceName(int id) {
219 return native_get_device_name(id);
220 }
221
Mike Lockwoode7d511e2010-12-30 13:39:37 -0500222 private static native int native_get_device_id(String name);
223 private static native String native_get_device_name(int id);
224}