/
UsbCliUtil.java
90 lines (73 loc) · 3.08 KB
/
UsbCliUtil.java
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
package io.trygvis.usb.tools;
import static java.lang.Integer.*;
import static javax.usb.extra.ExtraUsbUtil.*;
import static javax.usb.util.UsbUtil.toHexString;
import javax.usb.*;
import java.util.*;
/**
* Utilities for writing command line USB tools.
*/
public class UsbCliUtil {
/**
* Tries to find a device with either the id given or the vendor/product id given.
* <p/>
* This method will use stdout/stderr to print error messages so no messages should be
*
* @return Returns null if no device could be found.
*/
public static UsbDevice findDevice(UsbHub hub, Short idVendor, Short idProduct, String id) {
// TODO: Parse out --id=VVVV.PPPP[.N] and --device=[device path]
if (id != null) {
int i = id.indexOf(':');
if (i == 4) {
// this is an id on the form VVVV.PPPP
idVendor = (short) parseInt(id.substring(0, 4), 16);
idProduct = (short) parseInt(id.substring(5, 9), 16);
id = null;
}
}
if (id != null) {
// As this library is implemented with libusb which returns
// everything in a single, flat list for now just do this simple search.
int index;
try {
index = parseInt(id);
} catch (NumberFormatException e) {
System.err.println("Invalid 'id' parameter, has to be an integer.");
return null;
}
List<UsbDevice> devices = hub.getAttachedUsbDevices();
if (index >= devices.size() || index < 0) {
System.err.println("'id' parameter is out of range.");
return null;
}
return devices.get(index);
} else if (idProduct != null && idVendor != null) {
UsbDevice usbDevice = findUsbDevice(hub, idVendor, idProduct);
if (usbDevice == null) {
System.err.println("Could not find device with id " + deviceIdToString(idVendor, idProduct) + ".");
}
return usbDevice;
}
return null;
}
public static void listDevices(UsbHub hub, short idVendor, short idProduct) {
listDevices("0", hub, idVendor, idProduct);
}
private static void listDevices(String prefix, UsbHub hub, short idVendor, short idProduct) {
List<UsbDevice> list = hub.getAttachedUsbDevices();
for (int i = 0; i < list.size(); i++) {
UsbDevice device = list.get(i);
if (device.isUsbHub()) {
listDevices(prefix + "." + i, (UsbHub) device, idVendor, idProduct);
} else {
UsbDeviceDescriptor deviceDescriptor = device.getUsbDeviceDescriptor();
System.err.print(prefix + "." + i + " " + toHexString(deviceDescriptor.idVendor()) + ":" + toHexString(deviceDescriptor.idProduct()));
if (isUsbDevice(deviceDescriptor, idVendor, idProduct)) {
System.err.print(" (Unconfigured FX2)");
}
System.err.println();
}
}
}
}