-
Notifications
You must be signed in to change notification settings - Fork 109
Expand file tree
/
Copy pathDescriptorUtils.java
More file actions
394 lines (371 loc) · 12.8 KB
/
DescriptorUtils.java
File metadata and controls
394 lines (371 loc) · 12.8 KB
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/*
* Copyright (C) 2013 Klaus Reimer <[email protected]>
* See LICENSE.md for licensing information.
*/
package org.usb4java;
import java.nio.ByteBuffer;
import java.util.HashMap;
import java.util.Map;
/**
* Utility methods used for descriptor dumps.
*
* @author Klaus Reimer ([email protected])
*/
public final class DescriptorUtils
{
/** Mapping from USB class id to USB class name. */
private static final Map<Byte, String> CLASS_NAMES =
new HashMap<Byte, String>();
static
{
CLASS_NAMES.put(LibUsb.CLASS_PER_INTERFACE, "Per Interface");
CLASS_NAMES.put(LibUsb.CLASS_AUDIO, "Audio");
CLASS_NAMES.put(LibUsb.CLASS_COMM, "Communications");
CLASS_NAMES.put(LibUsb.CLASS_HID, "HID");
CLASS_NAMES.put(LibUsb.CLASS_PHYSICAL, "Physical");
CLASS_NAMES.put(LibUsb.CLASS_IMAGE, "Imaging");
CLASS_NAMES.put(LibUsb.CLASS_PRINTER, "Printer");
CLASS_NAMES.put(LibUsb.CLASS_MASS_STORAGE, "Mass Storage");
CLASS_NAMES.put(LibUsb.CLASS_HUB, "Hub");
CLASS_NAMES.put(LibUsb.CLASS_DATA, "Data");
CLASS_NAMES.put(LibUsb.CLASS_SMART_CARD, "Smart Card");
CLASS_NAMES.put(LibUsb.CLASS_CONTENT_SECURITY, "Content Security");
CLASS_NAMES.put(LibUsb.CLASS_VIDEO, "Video");
CLASS_NAMES.put(LibUsb.CLASS_PERSONAL_HEALTHCARE, "Personal Healthcare");
CLASS_NAMES.put(LibUsb.CLASS_DIAGNOSTIC_DEVICE, "Diagnostic Device");
CLASS_NAMES.put(LibUsb.CLASS_WIRELESS, "Wireless");
CLASS_NAMES.put(LibUsb.CLASS_APPLICATION, "Application");
CLASS_NAMES.put(LibUsb.CLASS_VENDOR_SPEC, "Vendor-specific");
}
/**
* Private constructor to prevent instantiation.
*/
private DescriptorUtils()
{
// Empty
}
/**
* Returns the name of the specified USB class. "unknown" is returned for a
* class which is unknown to libusb.
*
* @param usbClass
* The numeric USB class.
* @return The USB class name.
*/
public static String getUSBClassName(final byte usbClass)
{
final String name = CLASS_NAMES.get(usbClass);
if (name == null)
{
return "Unknown";
}
return name;
}
/**
* Decodes a binary-coded decimal into a string and returns it.
*
* @param bcd
* The binary-coded decimal to decode.
* @return The decoded binary-coded decimal.
*/
public static String decodeBCD(final short bcd)
{
return String.format("%x.%02x", (bcd & 0xFF00) >> 8, bcd & 0x00FF);
}
/**
* Dumps the specified byte buffer into a hex string and returns it.
*
* @param bytes
* The bytes to dump.
* @return The hex dump.
*/
public static String dump(final ByteBuffer bytes)
{
bytes.rewind();
final int columns = 16;
final StringBuilder builder = new StringBuilder();
int i = 0;
while (bytes.hasRemaining())
{
if ((i % columns) != 0)
{
builder.append(' ');
}
else if (i >= columns)
{
builder.append(String.format("%n"));
}
builder.append(String.format("%02x", bytes.get()));
i++;
}
return builder.toString();
}
/**
* Dumps the specified USB device descriptor into a string and returns it.
*
* @param descriptor
* The USB device descriptor to dump.
* @return The descriptor dump.
*/
public static String dump(final DeviceDescriptor descriptor)
{
return dump(descriptor, null, null, null);
}
/**
* Dumps the specified USB device descriptor into a string and returns it.
*
* @param descriptor
* The USB device descriptor to dump.
* @param manufacturer
* The manufacturer string or null if unknown.
* @param product
* The product string or null if unknown.
* @param serial
* The serial number string or null if unknown.
* @return The descriptor dump.
*/
public static String dump(final DeviceDescriptor descriptor,
final String manufacturer, final String product, final String serial)
{
return String.format(
"Device Descriptor:%n" +
" bLength %18d%n" +
" bDescriptorType %10d%n" +
" bcdUSB %19s%n" +
" bDeviceClass %13d %s%n" +
" bDeviceSubClass %10d%n" +
" bDeviceProtocol %10d%n" +
" bMaxPacketSize0 %10d%n" +
" idVendor %17s%n" +
" idProduct %16s%n" +
" bcdDevice %16s%n" +
" iManufacturer %12d%s%n" +
" iProduct %17d%s%n" +
" iSerial %18d%s%n" +
" bNumConfigurations %7d%n",
descriptor.bLength(),
descriptor.bDescriptorType(),
decodeBCD(descriptor.bcdUSB()),
descriptor.bDeviceClass() & 0xff,
getUSBClassName(descriptor.bDeviceClass()),
descriptor.bDeviceSubClass() & 0xff,
descriptor.bDeviceProtocol() & 0xff,
descriptor.bMaxPacketSize0() & 0xff,
String.format("0x%04x", descriptor.idVendor() & 0xffff),
String.format("0x%04x", descriptor.idProduct() & 0xffff),
decodeBCD(descriptor.bcdDevice()),
descriptor.iManufacturer() & 0xff,
(manufacturer == null) ? "" : (" " + manufacturer),
descriptor.iProduct() & 0xff,
(product == null) ? "" : (" " + product),
descriptor.iSerialNumber() & 0xff,
(serial == null) ? "" : (" " + serial),
descriptor.bNumConfigurations() & 0xff);
}
/**
* Dumps the specified USB configuration descriptor into a string and
* returns it.
*
* @param descriptor
* The USB configuration descriptor to dump.
* @return The descriptor dump.
*/
public static String dump(final ConfigDescriptor descriptor)
{
return String.format(
"Configuration Descriptor:%n" +
" bLength %18d%n" +
" bDescriptorType %10d%n" +
" wTotalLength %13d%n" +
" bNumInterfaces %11d%n" +
" bConfigurationValue %6d%n" +
" iConfiguration %11d%n" +
" bmAttributes %13s%n" +
" %s%n" +
"%s" +
" bMaxPower %16smA%n",
descriptor.bLength(),
descriptor.bDescriptorType(),
descriptor.wTotalLength() & 0xffff,
descriptor.bNumInterfaces() & 0xff,
descriptor.bConfigurationValue() & 0xff,
descriptor.iConfiguration() & 0xff,
String.format("0x%02x", descriptor.bmAttributes() & 0xff),
((descriptor.bmAttributes() & 64) == 0) ? "(Bus Powered)"
: "Self Powered",
((descriptor.bmAttributes() & 32) == 0) ? ""
: String.format(" Remote Wakeup%n"),
(descriptor.bMaxPower() & 0xff) * 2);
}
/**
* Dumps the specified USB interface descriptor into a string and returns
* it.
*
* @param descriptor
* The USB interface descriptor to dump.
* @return The descriptor dump.
*/
public static String dump(final InterfaceDescriptor descriptor)
{
return String.format(
"Interface Descriptor:%n" +
" bLength %18d%n" +
" bDescriptorType %10d%n" +
" bInterfaceNumber %9d%n" +
" bAlternateSetting %8d%n" +
" bNumEndpoints %12d%n" +
" bInterfaceClass %10d %s%n" +
" bInterfaceSubClass %7d%n" +
" bInterfaceProtocol %7d%n" +
" iInterface %15d%n",
descriptor.bLength(),
descriptor.bDescriptorType(),
descriptor.bInterfaceNumber() & 0xff,
descriptor.bAlternateSetting() & 0xff,
descriptor.bNumEndpoints() & 0xff,
descriptor.bInterfaceClass() & 0xff,
getUSBClassName(descriptor.bInterfaceClass()),
descriptor.bInterfaceSubClass() & 0xff,
descriptor.bInterfaceProtocol() & 0xff,
descriptor.iInterface() & 0xff);
}
/**
* Dumps the specified USB endpoint descriptor into a string and returns it.
*
* @param descriptor
* The USB endpoint descriptor to dump.
* @return The descriptor dump.
*/
public static String dump(final EndpointDescriptor descriptor)
{
return String.format(
"Endpoint Descriptor:%n" +
" bLength %18d%n" +
" bDescriptorType %10d%n" +
" bEndpointAddress %9s EP %d %s%n" +
" bmAttributes %13d%n" +
" Transfer Type %s%n" +
" Synch Type %s%n" +
" Usage Type %s%n" +
" wMaxPacketSize %11d%n" +
" bInterval %16d%n",
descriptor.bLength(),
descriptor.bDescriptorType(),
String.format("0x%02x", descriptor.bEndpointAddress() & 0xff),
descriptor.bEndpointAddress() & 0x0f,
getDirectionName(descriptor.bEndpointAddress()),
descriptor.bmAttributes() & 0xff,
getTransferTypeName(descriptor.bmAttributes()),
getSynchTypeName(descriptor.bmAttributes()),
getUsageTypeName(descriptor.bmAttributes()),
descriptor.wMaxPacketSize() & 0xffff,
descriptor.bInterval() & 0xff);
}
/**
* Returns the name for the transfer type in the specified endpoint
* attributes.
*
* @param bmAttributes
* The endpoint attributes value.
* @return The transfer type name.
*/
public static String getTransferTypeName(final byte bmAttributes)
{
switch (bmAttributes & LibUsb.TRANSFER_TYPE_MASK)
{
case LibUsb.TRANSFER_TYPE_CONTROL:
return "Control";
case LibUsb.TRANSFER_TYPE_ISOCHRONOUS:
return "Isochronous";
case LibUsb.TRANSFER_TYPE_BULK:
return "Bulk";
case LibUsb.TRANSFER_TYPE_INTERRUPT:
return "Interrupt";
default:
return "Unknown";
}
}
/**
* Returns the name for the synchronization type in the specified endpoint
* attributes.
*
* @param bmAttributes
* The endpoint attributes value.
* @return The synch type name.
*/
public static String getSynchTypeName(final byte bmAttributes)
{
switch ((bmAttributes & LibUsb.ISO_SYNC_TYPE_MASK) >> 2)
{
case LibUsb.ISO_SYNC_TYPE_NONE:
return "None";
case LibUsb.ISO_SYNC_TYPE_ASYNC:
return "Asynchronous";
case LibUsb.ISO_SYNC_TYPE_ADAPTIVE:
return "Adaptive";
case LibUsb.ISO_SYNC_TYPE_SYNC:
return "Synchronous";
default:
return "Unknown";
}
}
/**
* Returns the name for the usage type in the specified endpoint attributes.
*
* @param bmAttributes
* The endpoint attributes value.
* @return The usage type name.
*/
public static String getUsageTypeName(final byte bmAttributes)
{
switch ((bmAttributes & LibUsb.ISO_USAGE_TYPE_MASK) >> 4)
{
case LibUsb.ISO_USAGE_TYPE_DATA:
return "Data";
case LibUsb.ISO_USAGE_TYPE_FEEDBACK:
return "Feedback";
case LibUsb.ISO_USAGE_TYPE_IMPLICIT:
return "Implicit Feedback Data";
case 3:
// b11 is considered "Reserved" according to USB 3.0 spec.
return "Reserved";
default:
return "Unknown";
}
}
/**
* Returns the name for the specified speed number.
*
* @param speed
* The speed number.
* @return The speed name.
*/
public static String getSpeedName(final int speed)
{
switch (speed)
{
case LibUsb.SPEED_SUPER:
return "Super";
case LibUsb.SPEED_FULL:
return "Full";
case LibUsb.SPEED_HIGH:
return "High";
case LibUsb.SPEED_LOW:
return "Low";
default:
return "Unknown";
}
}
/**
* Returns the name of the USB traffic direction for the specified
* endpoint address.
*
* @param bEndpointAddress
* The endpoint address.
* @return The direction name.
*/
public static String getDirectionName(final byte bEndpointAddress)
{
return ((bEndpointAddress & LibUsb.ENDPOINT_IN) == 0) ? "OUT" : "IN";
}
}