Skip to content

Commit

Permalink
o Enabling the FTDI build again.
Browse files Browse the repository at this point in the history
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
trygvis committed Sep 21, 2010
1 parent ba60662 commit e09dbd5
Show file tree
Hide file tree
Showing 17 changed files with 388 additions and 70 deletions.
6 changes: 6 additions & 0 deletions README.markdown
Expand Up @@ -83,6 +83,12 @@ The Maven setup will automatically build 64-bit versions of libusb *and*
javalibusb1 if you are using a 64-bit JVM. Run your Maven with -d64 to run a
64-bit JVM.

### Running main() Methods From Your IDE

At least IntelliJ IDEA does not realize that the usbtools module depend on the
javalibusb1 because the javalibusb1 is not recognized ha a "Java" module when
it has packaging=nar in its POM.

Notes on Keeping Syncronized with Upstream
------------------------------------------

Expand Down
6 changes: 4 additions & 2 deletions ftdi/pom.xml
Expand Up @@ -10,15 +10,17 @@
</parent>
<artifactId>ftdi</artifactId>
<name>Javalibusb1 :: FTDI Driver</name>
<packaging>nar</packaging>
<dependencies>
<dependency>
<groupId>no.hackaton.usb</groupId>
<artifactId>javax.usb</artifactId>
<artifactId>javalibusb1</artifactId>
<version>${project.version}</version>
<type>nar</type>
</dependency>
<dependency>
<groupId>no.hackaton.usb</groupId>
<artifactId>javalibusb1</artifactId>
<artifactId>javax.usb-extra</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
Expand Down
2 changes: 2 additions & 0 deletions ftdi/src/main/java/no/hackaton/usb/ftdi/Ftdi232.java
Expand Up @@ -3,6 +3,8 @@
import static javax.usb.UsbConst.*;
import static no.hackaton.usb.ftdi.Ftdi232Util.*;

import javax.usb.*;
import javax.usb.util.*;
import java.io.*;

public class Ftdi232 implements Closeable {
Expand Down
2 changes: 2 additions & 0 deletions ftdi/src/main/java/no/hackaton/usb/ftdi/Ftdi232Util.java
@@ -1,5 +1,7 @@
package no.hackaton.usb.ftdi;

import javax.usb.*;

public class Ftdi232Util {
public static int calculateBaudRate(int requestedBaudRate) {
int base = 3000000 / requestedBaudRate;
Expand Down
1 change: 1 addition & 0 deletions ftdi/src/test/java/no/hackaton/usb/ftdi/Ftdi232Test.java
@@ -1,5 +1,6 @@
package no.hackaton.usb.ftdi;

import org.junit.*;
import static org.junit.Assert.*;

public class Ftdi232Test {
Expand Down
2 changes: 2 additions & 0 deletions ftdi/src/test/java/no/hackaton/usb/ftdi/ReadSerial.java
@@ -1,5 +1,7 @@
package no.hackaton.usb.ftdi;

import javax.usb.*;

public class ReadSerial {
public static void main(String[] args) throws Exception {
UsbServices usbServices = UsbHostManager.getUsbServices();
Expand Down
2 changes: 2 additions & 0 deletions ftdi/src/test/java/no/hackaton/usb/ftdi/ToggleDtr.java
Expand Up @@ -2,6 +2,8 @@

import static java.lang.Thread.sleep;

import javax.usb.*;

public class ToggleDtr {
public static void main(String[] args) throws Exception {
UsbServices usbServices = UsbHostManager.getUsbServices();
Expand Down
3 changes: 2 additions & 1 deletion javalibusb1/src/main/java/javalibusb1/Libusb1UsbIrp.java
Expand Up @@ -93,7 +93,8 @@ public void setData(byte[] data) {
irp.setData(data);
}

public void setData(byte[] data, int offset, int length) {irp.setData(data, offset, length);
public void setData(byte[] data, int offset, int length) {
irp.setData(data, offset, length);
}

public void setLength(int length) {
Expand Down
2 changes: 0 additions & 2 deletions pom.xml
Expand Up @@ -182,9 +182,7 @@
</profile>
</profiles>
<modules>
<!--
<module>ftdi</module>
-->
<module>javax.usb</module>
<module>javax.usb-extra</module>
<module>javalibusb1</module>
Expand Down
8 changes: 8 additions & 0 deletions usbtools/pom.xml
Expand Up @@ -101,6 +101,14 @@
<name>fx2programmer</name>
<mainClass>no.hackaton.usb.tools.Fx2Programmer</mainClass>
</program>
<program>
<name>usbecho</name>
<mainClass>no.hackaton.usb.tools.UsbEcho</mainClass>
</program>
<program>
<name>ztex-memtest</name>
<mainClass>no.hackaton.usb.tools.ztex.MemTest</mainClass>
</program>
</programs>
</configuration>
</plugin>
Expand Down
49 changes: 12 additions & 37 deletions usbtools/src/main/java/no/hackaton/usb/tools/Fx2Programmer.java
@@ -1,13 +1,13 @@
package no.hackaton.usb.tools;

import static java.lang.Thread.sleep;
import static java.lang.Thread.*;
import static java.util.Arrays.*;
import static javax.usb.extra.ExtraUsbUtil.*;
import static javax.usb.util.UsbUtil.*;
import static no.hackaton.usb.tools.Fx2Device.*;
import no.hackaton.usb.tools.IntelHex.*;
import static no.hackaton.usb.tools.IntelHex.*;
import static no.hackaton.usb.tools.IntelHex.RecordType.*;
import static no.hackaton.usb.tools.IntelHex.openIntelHexFile;
import static no.hackaton.usb.tools.UsbCliUtil.*;

import javax.usb.*;
import java.io.*;
Expand All @@ -22,31 +22,25 @@ public static void main(String[] a) throws Exception {
short idVendor = FX2_ID_VENDOR;
short idProduct = FX2_ID_PRODUCT;

List<String> args = new ArrayList<String>();
args.addAll(asList(a));

List<String> args = new ArrayList<String>(asList(a));
Iterator<String> it = args.iterator();
while (it.hasNext()) {
String arg = it.next();
if (arg.equals("--list")) {
doList("0", hub, idVendor, idProduct);
listDevices(hub, idVendor, idProduct);
break;
}
else {
// TODO: Parse out --id=VVVV.PPPP[.N] and --device=[device path]
commandPhase(args, hub, idVendor, idProduct);
} else {
UsbDevice usbDevice = findUsbDevice(hub, idVendor, idProduct);
if (usbDevice == null) {
return;
}
commandPhase(usbDevice, args);
break;
}
}
}

private static void commandPhase(List<String> args, UsbHub hub, short idVendor, short idProduct) throws Exception {
UsbDevice usbDevice = findUsbDevice(hub, idVendor, idProduct);

if(usbDevice == null) {
System.err.println("Could not find fx2 device.");
return;
}
private static void commandPhase(UsbDevice usbDevice, List<String> args) throws Exception {

Fx2Device fx2 = new Fx2Device(usbDevice);

Expand Down Expand Up @@ -102,23 +96,4 @@ private static void doProgramDevice(File file, Fx2Device device) throws IOExcept
private static void doDelay(long delay) throws InterruptedException {
sleep(delay);
}

private static void doList(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()) {
doList(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();
}
}
}
}
64 changes: 49 additions & 15 deletions usbtools/src/main/java/no/hackaton/usb/tools/HexFormatter.java
Expand Up @@ -5,32 +5,66 @@
import java.io.*;

public class HexFormatter {
private static final int lineLength = 16;
private static final int chunkSize = 8;

public static void writeBytes(PrintStream print, byte[] bytes) {
writeBytes(print, bytes, 0, bytes.length);
}

public static void writeBytes(PrintStream print, byte[] bytes, int offset, int length) {
for (int i = offset; i < length; i++) {
byte b = bytes[i];

String s = Integer.toHexString(unsignedShort(b));
if(s.length() == 1) {
print.print('0');
}
print.print(s);
print.print(' ');
int size = length - offset;
int lines = size / lineLength;

if (i == 0) {
continue;
}
int i = offset;
int line;
for (line = 0; line < lines; line++) {
printLine(print, lineLength, bytes, i);
i += lineLength;
print.println();
}

// The last line
int left = size % lineLength;
printLine(print, left, bytes, i);
if (left > 0) {
print.println();
}
}

private static void printLine(PrintStream print, int count, byte[] bytes, int i) {
if (count == 0) {
return;
}

StringBuilder buffer = new StringBuilder(count);

for (int col = 0; col < count; col++) {
byte b = bytes[i++];
if (col == chunkSize) {
print.print(" ");
buffer.append(' ');
} else if (col != 0) {
print.print(' ');
}
print.print(toHexString(b));

if (b >= 0x20 && b < 127) {
buffer.append(Character.valueOf((char) b));
} else {
buffer.append('.');
}
}

if (i % 16 == 0) {
print.println();
} else if (i % 8 == 0) {
for(int col = count; col < lineLength; col++) {
if (col == chunkSize) {
print.print(" ");
}
print.print(" ");
}

print.println();
print.print(' ');
print.print(buffer);
}
}
90 changes: 90 additions & 0 deletions usbtools/src/main/java/no/hackaton/usb/tools/UsbCliUtil.java
@@ -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();
}
}
}
}

0 comments on commit e09dbd5

Please sign in to comment.