diff --git a/java/src/ch/ntb/usb/Device.java b/java/src/ch/ntb/usb/Device.java
index 0da29b2..208fc2c 100644
--- a/java/src/ch/ntb/usb/Device.java
+++ b/java/src/ch/ntb/usb/Device.java
@@ -26,9 +26,14 @@ public class Device {
private boolean resetOnFirstOpen, resetDone;
- private int resetTimeout = 1000;
+ private int resetTimeout = 2000;
+
+ private Usb_Device dev;
+
+ private boolean initUSBDone;
protected Device(short idVendor, short idProduct) {
+ initUSBDone = false;
resetOnFirstOpen = false;
resetDone = false;
maxPacketSize = -1;
@@ -36,6 +41,111 @@ public class Device {
this.idProduct = idProduct;
}
+ private void initUSB() {
+ LibusbWin.usb_init();
+ initUSBDone = true;
+ }
+
+ private Usb_Bus initBus() throws USBException {
+ LibusbWin.usb_find_busses();
+ LibusbWin.usb_find_devices();
+
+ Usb_Bus bus = LibusbWin.usb_get_busses();
+ if (bus == null) {
+ throw new USBException("LibusbWin.usb_get_busses(): "
+ + LibusbWin.usb_strerror());
+ }
+
+ return bus;
+ }
+
+ private void updateMaxPacketSize(Usb_Device device) throws USBException {
+ maxPacketSize = -1;
+ Usb_Config_Descriptor[] confDesc = device.config;
+ for (int i = 0; i < confDesc.length; i++) {
+ Usb_Interface[] int_ = confDesc[i].interface_;
+ for (int j = 0; j < int_.length; j++) {
+ Usb_Interface_Descriptor[] intDesc = int_[j].altsetting;
+ for (int k = 0; k < intDesc.length; k++) {
+ Usb_Endpoint_Descriptor[] epDesc = intDesc[k].endpoint;
+ for (int l = 0; l < epDesc.length; l++) {
+ maxPacketSize = Math.max(epDesc[l].wMaxPacketSize,
+ maxPacketSize);
+ }
+ }
+ }
+ }
+ if (maxPacketSize <= 0) {
+ throw new USBException(
+ "No USB endpoints found. Check the device configuration");
+ }
+ }
+
+ private Usb_Device initDevice() throws USBException {
+ Usb_Bus bus = initBus();
+
+ Usb_Device device = null;
+ // search for device
+ while (bus != null) {
+ device = bus.devices;
+ while (device != null) {
+ Usb_Device_Descriptor devDesc = device.descriptor;
+ if ((devDesc.idVendor == idVendor)
+ && (devDesc.idProduct == idProduct)) {
+ logger.info("Device found: " + device.filename);
+ updateMaxPacketSize(device);
+ return device;
+ }
+ device = device.next;
+ }
+ bus = bus.next;
+ }
+ return null;
+ }
+
+ /**
+ * Updates the device and descriptor information from the bus.
+ * The descriptors can be read with {@link #getDeviceDescriptor()} and
+ * {@link #getConfigDescriptors()}.
+ *
+ * @throws USBException
+ */
+ public void updateDescriptors() throws USBException {
+ if (!initUSBDone)
+ initUSB();
+ dev = initDevice();
+ }
+
+ /**
+ * Returns the device descriptor associated with this device.
+ * The descriptor is updated by calling {@link #updateDescriptors()} or
+ * {@link #open(int, int, int)}.
+ *
+ * @return the device descriptor associated with this device or
+ * null
+ */
+ public Usb_Device_Descriptor getDeviceDescriptor() {
+ if (dev == null) {
+ return null;
+ }
+ return dev.descriptor;
+ }
+
+ /**
+ * Returns the configuration descriptors associated with this device.
+ * The descriptors are updated by calling {@link #updateDescriptors()} or
+ * {@link #open(int, int, int)}.
+ *
+ * @return the configuration descriptors associated with this device or
+ * null
+ */
+ public Usb_Config_Descriptor[] getConfigDescriptors() {
+ if (dev == null) {
+ return null;
+ }
+ return dev.config;
+ }
+
/**
* Opens the device and claims the specified configuration, interface and
* altinterface.
@@ -58,65 +168,24 @@ public class Device {
this.dev_interface = interface_;
this.dev_altinterface = altinterface;
- Usb_Bus bus;
-
if (usbDevHandle > 0) {
throw new USBException("device opened, close or reset first");
}
- // open bus
- LibusbWin.usb_init();
- LibusbWin.usb_find_busses();
- LibusbWin.usb_find_devices();
+ initUSB();
- bus = LibusbWin.usb_get_busses();
- if (bus == null) {
- throw new USBException("LibusbWin.usb_get_busses(): "
- + LibusbWin.usb_strerror());
- }
+ dev = initDevice();
- maxPacketSize = -1;
-
- // search for device
- while (bus != null) {
- Usb_Device dev = bus.devices;
- while (dev != null) {
- Usb_Device_Descriptor devDesc = dev.descriptor;
- if ((devDesc.idVendor == idVendor)
- && (devDesc.idProduct == idProduct)) {
- logger.info("Open device: " + dev.filename);
- int res = LibusbWin.usb_open(dev);
- if (res <= 0) {
- throw new USBException("LibusbWin.usb_open: "
- + LibusbWin.usb_strerror());
- }
- usbDevHandle = res;
- // get endpoint wMaxPacketSize
- Usb_Config_Descriptor[] confDesc = dev.config;
- for (int i = 0; i < confDesc.length; i++) {
- Usb_Interface[] int_ = confDesc[i].interface_;
- for (int j = 0; j < int_.length; j++) {
- Usb_Interface_Descriptor[] intDesc = int_[j].altsetting;
- for (int k = 0; k < intDesc.length; k++) {
- Usb_Endpoint_Descriptor[] epDesc = intDesc[k].endpoint;
- for (int l = 0; l < epDesc.length; l++) {
- maxPacketSize = Math.max(
- epDesc[l].wMaxPacketSize,
- maxPacketSize);
- }
- }
- }
- }
- if (maxPacketSize <= 0) {
- throw new USBException(
- "No USB endpoints found. Check the device configuration");
- }
- }
- dev = dev.next;
+ if (dev != null) {
+ int res = LibusbWin.usb_open(dev);
+ if (res <= 0) {
+ throw new USBException("LibusbWin.usb_open: "
+ + LibusbWin.usb_strerror());
}
- bus = bus.next;
+ usbDevHandle = res;
}
- if (usbDevHandle <= 0) {
+
+ if (dev == null || usbDevHandle <= 0) {
throw new USBException("USB device with idVendor 0x"
+ Integer.toHexString(idVendor & 0xFFFF)
+ " and idProduct 0x"
@@ -492,8 +561,8 @@ public class Device {
}
/**
- * Returns the alternative interface. This value is only valid after opening
- * the device.
+ * Returns the alternative interface.
+ * This value is only valid after opening the device.
*
* @return the alternative interface. This value is only valid after opening
* the device.
@@ -503,8 +572,8 @@ public class Device {
}
/**
- * Returns the current configuration used. This value is only valid after
- * opening the device.
+ * Returns the current configuration used.
+ * This value is only valid after opening the device.
*
* @return the current configuration used. This value is only valid after
* opening the device.
@@ -514,8 +583,8 @@ public class Device {
}
/**
- * Returns the current interface. This value is only valid after opening the
- * device.
+ * Returns the current interface.
+ * This value is only valid after opening the device.
*
* @return the current interface. This value is only valid after opening the
* device.
@@ -524,17 +593,6 @@ public class Device {
return dev_interface;
}
- /**
- * Returns the current device handle. This value is only valid after opening
- * the device.
- *
- * @return the current device handle. This value is only valid after opening
- * the device.
- */
- public int getUsbDevHandle() {
- return usbDevHandle;
- }
-
/**
* Returns the maximum packet size in bytes which is allowed to be
* transmitted at once.
diff --git a/java/test/ch/ntb/usb/test/DeviceTest.java b/java/test/ch/ntb/usb/test/DeviceTest.java
index c202192..8db4ee3 100644
--- a/java/test/ch/ntb/usb/test/DeviceTest.java
+++ b/java/test/ch/ntb/usb/test/DeviceTest.java
@@ -1,6 +1,7 @@
package ch.ntb.usb.test;
-import static org.junit.Assert.fail;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
import java.io.FileInputStream;
import java.io.InputStream;
@@ -16,6 +17,8 @@ import ch.ntb.usb.Device;
import ch.ntb.usb.LibusbWin;
import ch.ntb.usb.USB;
import ch.ntb.usb.USBException;
+import ch.ntb.usb.Usb_Config_Descriptor;
+import ch.ntb.usb.Usb_Device_Descriptor;
import ch.ntb.usb.test.AbstractDeviceInfo.WriteMode;
public class DeviceTest {
@@ -32,6 +35,7 @@ public class DeviceTest {
private static Device dev;
+ @SuppressWarnings("unchecked")
@BeforeClass
public static void setUp() throws Exception {
// load the device info class with the key
@@ -56,6 +60,19 @@ public class DeviceTest {
dev = USB.getDevice(devinfo.getIdVendor(), devinfo.getIdProduct());
}
+ @SuppressWarnings("null")
+ @Test
+ public void getDescriptors() throws Exception {
+ dev.updateDescriptors();
+ Usb_Device_Descriptor devDescriptor = dev.getDeviceDescriptor();
+ assertTrue(devDescriptor != null);
+ assertEquals(devinfo.getIdProduct(), devDescriptor.idProduct);
+ assertEquals(devinfo.getIdVendor(), devDescriptor.idVendor);
+ Usb_Config_Descriptor confDescriptors[] = dev.getConfigDescriptors();
+ assertTrue(confDescriptors != null);
+ assertTrue(confDescriptors[0].interface_.length > 0);
+ }
+
@Test
public void initalReset() throws Exception {
doOpen();
@@ -233,8 +250,7 @@ public class DeviceTest {
private static void compare(byte[] d1, byte[] d2) {
int minLength = Math.min(d1.length, d2.length);
for (int i = 0; i < minLength; i++) {
- if (d1[i] != d2[i])
- fail("received data not equal to sent data");
+ assertEquals(d1[i], d2[i]);
}
}