From 92a7bc71788b991c5782d4079303c4f5103218ba Mon Sep 17 00:00:00 2001 From: uniederer Date: Sun, 3 Feb 2013 17:08:07 +0000 Subject: [PATCH] - Removed library's JNI test - Introduced code to explicitly unref devices - Corrected code formatting git-svn-id: https://svn.code.sf.net/p/libusbjava/code/trunk@309 94ad28fe-ef68-46b1-9651-e7ae4fcf1c4c --- .../src/ch/ntb/inf/libusbJava/LibusbJava.java | 3 +- .../src/ch/ntb/inf/libusbJava/Usb_Device.java | 30 +++++++++ .../libusbJava/Usb_Interface_Descriptor.java | 7 +- .../ch/ntb/inf/libusbJava/demo/LogBus.java | 6 +- .../inf/libusbJava/test/LibusbJava1Test.java | 64 ------------------- 5 files changed, 40 insertions(+), 70 deletions(-) diff --git a/java/src/ch/ntb/inf/libusbJava/LibusbJava.java b/java/src/ch/ntb/inf/libusbJava/LibusbJava.java index ea8ed17..c15887f 100644 --- a/java/src/ch/ntb/inf/libusbJava/LibusbJava.java +++ b/java/src/ch/ntb/inf/libusbJava/LibusbJava.java @@ -299,7 +299,7 @@ public class LibusbJava { handle = LibusbJava1.libusb_open(dev); } catch (LibusbError e) { - System.err.println("LibusbJava-1.0 init failed with errorcode: " + System.err.println("LibusbJava-1.0 failed with errorcode: " + e.getMessage()); e.printStackTrace(); handle = 0; @@ -581,6 +581,7 @@ public class LibusbJava { try { result = LibusbJava1.libusb_get_descriptor(dev_handle, type, index, size); } catch (LibusbError e) { + e.printStackTrace(); } return result; diff --git a/java/src/ch/ntb/inf/libusbJava/Usb_Device.java b/java/src/ch/ntb/inf/libusbJava/Usb_Device.java index 4612176..77cc598 100644 --- a/java/src/ch/ntb/inf/libusbJava/Usb_Device.java +++ b/java/src/ch/ntb/inf/libusbJava/Usb_Device.java @@ -107,6 +107,36 @@ public class Usb_Device { next = dev; } + public static void freeDeviceList(Usb_Device device) + { + Usb_Device curr = device.getPrev(); + + /* Detach the left part of the list */ + device.setPrev(null); + + /* First walk to the left of the list and free all + devices on our way */ + while (curr != null) + { + freeDevice(curr); + curr = curr.getPrev(); + } + + curr = device; + /* Then walk to the right of the list and free all */ + while (curr != null) + { + freeDevice(curr); + curr = curr.getNext(); + } + } + + public static void freeDevice(Usb_Device device) + { + freeDeviceList(device.getChildren()); + LibusbJava1.libusb_unref_device(device); + } + /** * Returns the number of children of this device.
* diff --git a/java/src/ch/ntb/inf/libusbJava/Usb_Interface_Descriptor.java b/java/src/ch/ntb/inf/libusbJava/Usb_Interface_Descriptor.java index 519343a..a152f29 100644 --- a/java/src/ch/ntb/inf/libusbJava/Usb_Interface_Descriptor.java +++ b/java/src/ch/ntb/inf/libusbJava/Usb_Interface_Descriptor.java @@ -13,8 +13,8 @@ package ch.ntb.inf.libusbJava; * endpoints into a functional group performing a single feature of the device.
*
* The length of the interface descriptor is - * {@link ch.ntb.inf.libusbJava.Usb_Descriptor#USB_DT_INTERFACE_SIZE} and the type is - * {@link ch.ntb.inf.libusbJava.Usb_Descriptor#USB_DT_INTERFACE}. + * {@link ch.ntb.inf.libusbJava.Usb_Descriptor#USB_DT_INTERFACE_SIZE} and the + * type is {@link ch.ntb.inf.libusbJava.Usb_Descriptor#USB_DT_INTERFACE}. * */ public class Usb_Interface_Descriptor extends Usb_Descriptor { @@ -51,7 +51,8 @@ public class Usb_Interface_Descriptor extends Usb_Descriptor { } /** - * Returns the value used to select the alternate setting ({@link LibusbJava#usb_set_altinterface(long, int)}).
+ * Returns the value used to select the alternate setting ( + * {@link LibusbJava#usb_set_altinterface(long, int)}).
* * @return the alternate setting */ diff --git a/java/src/ch/ntb/inf/libusbJava/demo/LogBus.java b/java/src/ch/ntb/inf/libusbJava/demo/LogBus.java index 9e36d91..dd402a1 100644 --- a/java/src/ch/ntb/inf/libusbJava/demo/LogBus.java +++ b/java/src/ch/ntb/inf/libusbJava/demo/LogBus.java @@ -15,12 +15,14 @@ import ch.ntb.inf.libusbJava.Utils; * Initalises Libusb and prints the bus(ses) with attached devices to the * standard out.
* - * + * @deprecated This class will be removed with the legacy {@link LibusbJava} + * class */ public class LogBus { private static void logBus() { - // if you don't use the ch.ntb.inf.libusbJava.Device class you must initialise + // if you don't use the ch.ntb.inf.libusbJava.Device class you must + // initialise // Libusb before use LibusbJava.usb_init(); LibusbJava.usb_find_busses(); diff --git a/java/test/ch/ntb/inf/libusbJava/test/LibusbJava1Test.java b/java/test/ch/ntb/inf/libusbJava/test/LibusbJava1Test.java index 8d20de4..008649f 100644 --- a/java/test/ch/ntb/inf/libusbJava/test/LibusbJava1Test.java +++ b/java/test/ch/ntb/inf/libusbJava/test/LibusbJava1Test.java @@ -1,9 +1,7 @@ package ch.ntb.inf.libusbJava.test; -import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import org.junit.Test; @@ -21,68 +19,6 @@ public class LibusbJava1Test { public void testLibusb_set_debug() { } - /** - * This test is used to check if the library constructs and throws the - * correct exceptions if requested to do so. - */ - @SuppressWarnings("deprecation") - @Test - public void testLibusb_exception() { - /* - * We go through every currently possible error just to check a several - * different error codes. - */ - for (int i = -1; i > LibusbError.ERROR_OTHER; i--) { - boolean exception_occured = false; - - try { - LibusbJava1.libusb_exceptionTest(i); - } catch (LibusbError e) { - assertEquals("Exception code correct", e.getErrorCode(), i); - exception_occured = true; - } - - assertTrue("Exception occured", exception_occured); - } - } - - /** - * Tests a helper function in the DLL that creates a byte array object from - * a piece of memory given. - */ - @SuppressWarnings("deprecation") - @Test - public void testLibusb_to_byteArrayWithContent() { - final String str = "SimpleTest"; - final int testLen = 5; - final byte[] reference = str.substring(0, testLen).getBytes(); - - byte[] result = LibusbJava1.to_byteArrayTest(str, testLen); - - assertNotNull("Got a byte array", result); - assertEquals("Byte array has correct length", testLen, result.length); - - for (int i = 0;i < result.length;i++) - { - assertEquals("Array content is correct", reference[i], result[i]); - } - } - - /** - * Tests a helper function in the DLL that creates a byte array object from - * a piece of memory given. - */ - @SuppressWarnings("deprecation") - @Test - public void testLibusb_to_byteArrayLength0() { - final String str = "SimpleTest"; - - byte[] result = LibusbJava1.to_byteArrayTest(str, 0); - - assertNotNull("Got a byte array", result); - assertEquals("Byte array has correct length", 0, result.length); - } - @Test public void testLibusb_init() { try {