1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
|
/*
* Copyright (C) 2010 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.hardware.usb;
import android.os.Parcel;
import android.os.Parcelable;
/**
* This class represents a USB device attached to the android device with the android device
* acting as the USB host.
* Each device contains one or more {@link UsbInterface}s, each of which contains a number of
* {@link UsbEndpoint}s (the channels via which data is transmitted over USB).
*
* <p> This class contains information (along with {@link UsbInterface} and {@link UsbEndpoint})
* that describes the capabilities of the USB device.
* To communicate with the device, you open a {@link UsbDeviceConnection} for the device
* and use {@link UsbRequest} to send and receive data on an endpoint.
* {@link UsbDeviceConnection#controlTransfer} is used for control requests on endpoint zero.
*
* <div class="special reference">
* <h3>Developer Guides</h3>
* <p>For more information about communicating with USB hardware, read the
* <a href="{@docRoot}guide/topics/usb/index.html">USB</a> developer guide.</p>
* </div>
*/
public class UsbDevice implements Parcelable {
private static final String TAG = "UsbDevice";
private static final boolean DEBUG = false;
private final String mName;
private final String mManufacturerName;
private final String mProductName;
private final String mVersion;
private final String mSerialNumber;
private final int mVendorId;
private final int mProductId;
private final int mClass;
private final int mSubclass;
private final int mProtocol;
private Parcelable[] mConfigurations;
// list of all interfaces on the device
private UsbInterface[] mInterfaces;
/**
* UsbDevice should only be instantiated by UsbService implementation
* @hide
*/
public UsbDevice(String name, int vendorId, int productId,
int Class, int subClass, int protocol,
String manufacturerName, String productName, String version, String serialNumber) {
mName = name;
mVendorId = vendorId;
mProductId = productId;
mClass = Class;
mSubclass = subClass;
mProtocol = protocol;
mManufacturerName = manufacturerName;
mProductName = productName;
mVersion = version;
mSerialNumber = serialNumber;
}
/**
* Returns the name of the device.
* In the standard implementation, this is the path of the device file
* for the device in the usbfs file system.
*
* @return the device name
*/
public String getDeviceName() {
return mName;
}
/**
* Returns the manufacturer name of the device.
*
* @return the manufacturer name
*/
public String getManufacturerName() {
return mManufacturerName;
}
/**
* Returns the product name of the device.
*
* @return the product name
*/
public String getProductName() {
return mProductName;
}
/**
* Returns the version number of the device.
*
* @return the device version
*/
public String getVersion() {
return mVersion;
}
/**
* Returns the serial number of the device.
*
* @return the serial number name
*/
public String getSerialNumber() {
return mSerialNumber;
}
/**
* Returns a unique integer ID for the device.
* This is a convenience for clients that want to use an integer to represent
* the device, rather than the device name.
* IDs are not persistent across USB disconnects.
*
* @return the device ID
*/
public int getDeviceId() {
return getDeviceId(mName);
}
/**
* Returns a vendor ID for the device.
*
* @return the device vendor ID
*/
public int getVendorId() {
return mVendorId;
}
/**
* Returns a product ID for the device.
*
* @return the device product ID
*/
public int getProductId() {
return mProductId;
}
/**
* Returns the devices's class field.
* Some useful constants for USB device classes can be found in {@link UsbConstants}.
*
* @return the devices's class
*/
public int getDeviceClass() {
return mClass;
}
/**
* Returns the device's subclass field.
*
* @return the device's subclass
*/
public int getDeviceSubclass() {
return mSubclass;
}
/**
* Returns the device's protocol field.
*
* @return the device's protocol
*/
public int getDeviceProtocol() {
return mProtocol;
}
/**
* Returns the number of {@link UsbConfiguration}s this device contains.
*
* @return the number of configurations
*/
public int getConfigurationCount() {
return mConfigurations.length;
}
/**
* Returns the {@link UsbConfiguration} at the given index.
*
* @return the configuration
*/
public UsbConfiguration getConfiguration(int index) {
return (UsbConfiguration)mConfigurations[index];
}
private UsbInterface[] getInterfaceList() {
if (mInterfaces == null) {
int configurationCount = mConfigurations.length;
int interfaceCount = 0;
for (int i = 0; i < configurationCount; i++) {
UsbConfiguration configuration = (UsbConfiguration)mConfigurations[i];
interfaceCount += configuration.getInterfaceCount();
}
mInterfaces = new UsbInterface[interfaceCount];
int offset = 0;
for (int i = 0; i < configurationCount; i++) {
UsbConfiguration configuration = (UsbConfiguration)mConfigurations[i];
interfaceCount = configuration.getInterfaceCount();
for (int j = 0; j < interfaceCount; j++) {
mInterfaces[offset++] = configuration.getInterface(j);
}
}
}
return mInterfaces;
}
/**
* Returns the number of {@link UsbInterface}s this device contains.
* For devices with multiple configurations, you will probably want to use
* {@link UsbConfiguration#getInterfaceCount} instead.
*
* @return the number of interfaces
*/
public int getInterfaceCount() {
return getInterfaceList().length;
}
/**
* Returns the {@link UsbInterface} at the given index.
* For devices with multiple configurations, you will probably want to use
* {@link UsbConfiguration#getInterface} instead.
*
* @return the interface
*/
public UsbInterface getInterface(int index) {
return getInterfaceList()[index];
}
/**
* Only used by UsbService implementation
* @hide
*/
public void setConfigurations(Parcelable[] configuration) {
mConfigurations = configuration;
}
@Override
public boolean equals(Object o) {
if (o instanceof UsbDevice) {
return ((UsbDevice)o).mName.equals(mName);
} else if (o instanceof String) {
return ((String)o).equals(mName);
} else {
return false;
}
}
@Override
public int hashCode() {
return mName.hashCode();
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder("UsbDevice[mName=" + mName +
",mVendorId=" + mVendorId + ",mProductId=" + mProductId +
",mClass=" + mClass + ",mSubclass=" + mSubclass + ",mProtocol=" + mProtocol +
",mManufacturerName=" + mManufacturerName + ",mProductName=" + mProductName +
",mVersion=" + mVersion + ",mSerialNumber=" + mSerialNumber + ",mConfigurations=[");
for (int i = 0; i < mConfigurations.length; i++) {
builder.append("\n");
builder.append(mConfigurations[i].toString());
}
builder.append("]");
return builder.toString();
}
public static final Parcelable.Creator<UsbDevice> CREATOR =
new Parcelable.Creator<UsbDevice>() {
public UsbDevice createFromParcel(Parcel in) {
String name = in.readString();
int vendorId = in.readInt();
int productId = in.readInt();
int clasz = in.readInt();
int subClass = in.readInt();
int protocol = in.readInt();
String manufacturerName = in.readString();
String productName = in.readString();
String version = in.readString();
String serialNumber = in.readString();
Parcelable[] configurations = in.readParcelableArray(UsbInterface.class.getClassLoader());
UsbDevice device = new UsbDevice(name, vendorId, productId, clasz, subClass, protocol,
manufacturerName, productName, version, serialNumber);
device.setConfigurations(configurations);
return device;
}
public UsbDevice[] newArray(int size) {
return new UsbDevice[size];
}
};
public int describeContents() {
return 0;
}
public void writeToParcel(Parcel parcel, int flags) {
parcel.writeString(mName);
parcel.writeInt(mVendorId);
parcel.writeInt(mProductId);
parcel.writeInt(mClass);
parcel.writeInt(mSubclass);
parcel.writeInt(mProtocol);
parcel.writeString(mManufacturerName);
parcel.writeString(mProductName);
parcel.writeString(mVersion);
parcel.writeString(mSerialNumber);
parcel.writeParcelableArray(mConfigurations, 0);
}
public static int getDeviceId(String name) {
return native_get_device_id(name);
}
public static String getDeviceName(int id) {
return native_get_device_name(id);
}
private static native int native_get_device_id(String name);
private static native String native_get_device_name(int id);
}
|