Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
o Moving out some common CLI code to UsbCliUtil. o Creating a start of an UsbEcho utility to stream standard in to and endpoint and stream another endpoint back out to standard output. o The hex formatter now shows the ASCII values read. Adding a test for the hex formatter too.
- Loading branch information
Showing
17 changed files
with
388 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
package no.hackaton.usb.ftdi; | ||
|
||
import org.junit.*; | ||
import static org.junit.Assert.*; | ||
|
||
public class Ftdi232Test { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
usbtools/src/main/java/no/hackaton/usb/tools/UsbCliUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package no.hackaton.usb.tools; | ||
|
||
import static java.lang.Integer.*; | ||
import static java.lang.Short.*; | ||
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 = parseShort(id.substring(0, 4), 16); | ||
idProduct = parseShort(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 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(); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.