- Switched UsbTreeModel to LibusbJava1

- Corrected code formatting
- Introduced explicit setup and teardown functions

git-svn-id: https://svn.code.sf.net/p/libusbjava/code/trunk@308 94ad28fe-ef68-46b1-9651-e7ae4fcf1c4c
This commit is contained in:
uniederer
2013-02-03 17:04:22 +00:00
parent 3c1a359c28
commit 620fb612b7
2 changed files with 186 additions and 142 deletions

View File

@@ -74,7 +74,8 @@ public class LibusbJava1 {
* <br> * <br>
* *
* @return a context to operate on * @return a context to operate on
* @throws LibusbError if an error occured * @throws LibusbError
* if an error occured
*/ */
public static native long libusb_init() throws LibusbError; public static native long libusb_init() throws LibusbError;
@@ -191,15 +192,15 @@ public class LibusbJava1 {
* @param dev * @param dev
* the device to open * the device to open
* *
* @return a device handler * @return a device handle >= 0
* *
* @throws LibusbError * @throws LibusbError
* in case of an error<br> * in case of an error<br>
* Possible causes are:<br> * Possible causes are:<br>
* - ERROR_NO_MEM on memory allocation failure - ERROR_ACCESS if * - ERROR_NO_MEM on memory allocation failure<br>
* the user has insufficient permissions - ERROR_NO_DEVICE if * - ERROR_ACCESS if the user has insufficient permissions<br>
* the device has been disconnected - another ERROR code on * - ERROR_NO_DEVICE if the device has been disconnected<br>
* other failure * - another ERROR code on other failure
* *
* @see #libusb_get_device(long) * @see #libusb_get_device(long)
*/ */
@@ -1383,33 +1384,44 @@ public class LibusbJava1 {
} else { } else {
System.loadLibrary("usbJava-1.0"); System.loadLibrary("usbJava-1.0");
} }
/*
* After loading the library, we register a "shutdown hook" to be called
* for cleaning up stuff in the library when exiting.
*/
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
try {
LibusbJava1.teardown();
} catch (Exception e) {
e.printStackTrace();
}
}
});
/*
* After setting up the cleanup callback, it's now safe to call the code
* that initializes stuff in the native library
*/
int setup_result = setup();
if (setup_result < 0) {
throw new UnsatisfiedLinkError("Could not setup the library. ("
+ setup_result + ")");
}
} }
/** /**
* This method is only used for testing the DLL-code that throws exceptions * This method is used to setup stuff in the native library. After calling
* in the java environment. * this function, {@link #teardown()} has to be called if the native library
* is not used anymore.
* *
* @param code * @return - 0 if successful - <0 if an error occured
* Code of the error to be simulated and hence the code of the
* exception that shall be thrown.
*
* @throws LibusbError
* @deprecated This function is only for testing purpose and should not be
* called in production code
*/ */
@Deprecated private static native int setup();
public static native void libusb_exceptionTest(int code) throws LibusbError;
/** /**
* This method is only used for testing the DLL helpercode. It creates a * This method cleans up stuff initialized using {@link #setup()}.
* byte Array of the given size from the given string.
*
* @param str String to be copied into the array
* @param size Size of the array to be created
*
* @deprecated This function is only for testing purpose and should not be
* called in production code
*/ */
@Deprecated private static native void teardown();
public static native byte[] to_byteArrayTest(String str, int size);
} }

View File

@@ -18,7 +18,7 @@ import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.TreeModel; import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath; import javax.swing.tree.TreePath;
import ch.ntb.inf.libusbJava.LibusbJava; import ch.ntb.inf.libusbJava.LibusbJava1;
import ch.ntb.inf.libusbJava.Usb_Bus; import ch.ntb.inf.libusbJava.Usb_Bus;
import ch.ntb.inf.libusbJava.Usb_Config_Descriptor; import ch.ntb.inf.libusbJava.Usb_Config_Descriptor;
import ch.ntb.inf.libusbJava.Usb_Device; import ch.ntb.inf.libusbJava.Usb_Device;
@@ -26,6 +26,7 @@ import ch.ntb.inf.libusbJava.Usb_Device_Descriptor;
import ch.ntb.inf.libusbJava.Usb_Endpoint_Descriptor; import ch.ntb.inf.libusbJava.Usb_Endpoint_Descriptor;
import ch.ntb.inf.libusbJava.Usb_Interface; import ch.ntb.inf.libusbJava.Usb_Interface;
import ch.ntb.inf.libusbJava.Usb_Interface_Descriptor; import ch.ntb.inf.libusbJava.Usb_Interface_Descriptor;
import ch.ntb.inf.libusbJava.exceptions.LibusbError;
public class UsbTreeModel implements TreeModel, TreeSelectionListener { public class UsbTreeModel implements TreeModel, TreeSelectionListener {
@@ -62,12 +63,11 @@ public class UsbTreeModel implements TreeModel, TreeSelectionListener {
*/ */
public Object getChild(Object parent, int index) { public Object getChild(Object parent, int index) {
if (parent instanceof String && ((String) parent).compareTo(USB_ROOT) == 0) if (parent instanceof String
{ && ((String) parent).compareTo(USB_ROOT) == 0) {
Usb_Bus curBus = rootBus; Usb_Bus curBus = rootBus;
for (int i = 0; curBus != null; curBus = curBus.getNext(), i++) for (int i = 0; curBus != null; curBus = curBus.getNext(), i++) {
{
if (i == index) if (i == index)
return curBus; return curBus;
} }
@@ -118,23 +118,20 @@ public class UsbTreeModel implements TreeModel, TreeSelectionListener {
/** /**
* Returns the number of children of parent. * Returns the number of children of parent.
*/ */
public int getChildCount(Object parent) public int getChildCount(Object parent) {
{ if (parent instanceof String
if (parent instanceof String && ((String) parent).compareTo(USB_ROOT) == 0) && ((String) parent).compareTo(USB_ROOT) == 0) {
{
int count = 0; int count = 0;
Usb_Bus curBus = rootBus; Usb_Bus curBus = rootBus;
for (; curBus != null; curBus = curBus.getNext()) for (; curBus != null; curBus = curBus.getNext()) {
{
count++; count++;
} }
return count; return count;
} } else if (parent instanceof Usb_Bus) {
else if (parent instanceof Usb_Bus) {
Usb_Device device = ((Usb_Bus) parent).getDevices(); Usb_Device device = ((Usb_Bus) parent).getDevices();
int count = 0; int count = 0;
while (device != null) { while (device != null) {
@@ -244,11 +241,8 @@ public class UsbTreeModel implements TreeModel, TreeSelectionListener {
sb.append("\tidProduct: 0x" sb.append("\tidProduct: 0x"
+ Integer.toHexString(devDesc.getIdProduct() & 0xFFFF) + Integer.toHexString(devDesc.getIdProduct() & 0xFFFF)
+ "\n"); + "\n");
sb sb.append("\tbcdDevice: 0x"
.append("\tbcdDevice: 0x" + Integer.toHexString(devDesc.getBcdDevice() & 0xFF) + "\n");
+ Integer
.toHexString(devDesc.getBcdDevice() & 0xFF)
+ "\n");
sb.append("\tiManufacturer: 0x" sb.append("\tiManufacturer: 0x"
+ Integer.toHexString(devDesc.getIManufacturer() & 0xFF) + Integer.toHexString(devDesc.getIManufacturer() & 0xFF)
+ "\n"); + "\n");
@@ -257,10 +251,9 @@ public class UsbTreeModel implements TreeModel, TreeSelectionListener {
sb.append("\tiSerialNumber: 0x" sb.append("\tiSerialNumber: 0x"
+ Integer.toHexString(devDesc.getISerialNumber() & 0xFF) + Integer.toHexString(devDesc.getISerialNumber() & 0xFF)
+ "\n"); + "\n");
sb sb.append("\tbNumConfigurations: 0x"
.append("\tbNumConfigurations: 0x" + Integer.toHexString(devDesc.getBNumConfigurations() & 0xFF)
+ Integer.toHexString(devDesc + "\n");
.getBNumConfigurations() & 0xFF) + "\n");
// get device handle to retrieve string descriptors // get device handle to retrieve string descriptors
Usb_Bus bus = rootBus; Usb_Bus bus = rootBus;
while (bus != null) { while (bus != null) {
@@ -272,37 +265,54 @@ public class UsbTreeModel implements TreeModel, TreeSelectionListener {
|| (dev.getDescriptor().getIProduct() > 0) || (dev || (dev.getDescriptor().getIProduct() > 0) || (dev
.getDescriptor().getISerialNumber() > 0))) { .getDescriptor().getISerialNumber() > 0))) {
if (tmpDevDesc.equals(devDesc)) { if (tmpDevDesc.equals(devDesc)) {
long handle = LibusbJava.usb_open(dev); try {
sb.append("\nString descriptors\n"); sb.append("\nString descriptors\n");
if (handle == 0) { long handle = LibusbJava1.libusb_open(dev);
sb.append("\terror opening the device\n");
break; try {
if (dev.getDescriptor().getIManufacturer() > 0) {
String manufacturer = LibusbJava1
.libusb_get_string_descriptor_ascii(
handle,
devDesc.getIManufacturer(),
255);
if (manufacturer == null)
manufacturer = "unable to fetch manufacturer string";
sb.append("\tiManufacturer: "
+ manufacturer + "\n");
}
if (dev.getDescriptor().getIProduct() > 0) {
String product = LibusbJava1
.libusb_get_string_descriptor_ascii(
handle,
devDesc.getIProduct(),
255);
if (product == null)
product = "unable to fetch product string";
sb.append("\tiProduct: " + product
+ "\n");
}
if (dev.getDescriptor().getISerialNumber() > 0) {
String serialNumber = LibusbJava1
.libusb_get_string_descriptor_ascii(
handle,
devDesc.getISerialNumber(),
255);
if (serialNumber == null)
serialNumber = "unable to fetch serial number string";
sb.append("\tiSerialNumber: "
+ serialNumber + "\n");
}
} catch (LibusbError ex) {
sb.append("\terror getting descriptors: "
+ ex.getErrorString() + "\n");
} finally {
LibusbJava1.libusb_close(handle);
}
} catch (LibusbError ex) {
sb.append("\terror opening the device"
+ ex.getErrorString() + "\n");
} }
if (dev.getDescriptor().getIManufacturer() > 0) {
String manufacturer = LibusbJava.usb_get_string_simple(handle, devDesc.getIManufacturer());
if (manufacturer == null)
manufacturer = "unable to fetch manufacturer string";
sb.append("\tiManufacturer: " + manufacturer
+ "\n");
}
if (dev.getDescriptor().getIProduct() > 0) {
String product = LibusbJava
.usb_get_string_simple(handle, devDesc
.getIProduct());
if (product == null)
product = "unable to fetch product string";
sb.append("\tiProduct: " + product + "\n");
}
if (dev.getDescriptor().getISerialNumber() > 0) {
String serialNumber = LibusbJava
.usb_get_string_simple(handle, devDesc
.getISerialNumber());
if (serialNumber == null)
serialNumber = "unable to fetch serial number string";
sb.append("\tiSerialNumber: " + serialNumber
+ "\n");
}
LibusbJava.usb_close(handle);
} }
} }
dev = dev.getNext(); dev = dev.getNext();
@@ -321,10 +331,9 @@ public class UsbTreeModel implements TreeModel, TreeSelectionListener {
sb.append("\tbNumInterfaces: 0x" sb.append("\tbNumInterfaces: 0x"
+ Integer.toHexString(confDesc.getBNumInterfaces() & 0xFF) + Integer.toHexString(confDesc.getBNumInterfaces() & 0xFF)
+ "\n"); + "\n");
sb sb.append("\tbConfigurationValue: 0x"
.append("\tbConfigurationValue: 0x" + Integer.toHexString(confDesc.getBConfigurationValue() & 0xFF)
+ Integer.toHexString(confDesc + "\n");
.getBConfigurationValue() & 0xFF) + "\n");
sb.append("\tiConfiguration: 0x" sb.append("\tiConfiguration: 0x"
+ Integer.toHexString(confDesc.getIConfiguration() & 0xFF) + Integer.toHexString(confDesc.getIConfiguration() & 0xFF)
+ "\n"); + "\n");
@@ -336,7 +345,8 @@ public class UsbTreeModel implements TreeModel, TreeSelectionListener {
+ confDesc.getMaxPower() + ")\n"); + confDesc.getMaxPower() + ")\n");
sb.append("\textralen: 0x" sb.append("\textralen: 0x"
+ Integer.toHexString(confDesc.getExtralen()) + "\n"); + Integer.toHexString(confDesc.getExtralen()) + "\n");
sb.append("\textra: " + extraDescriptorToString(confDesc.getExtra()) + "\n"); sb.append("\textra: "
+ extraDescriptorToString(confDesc.getExtra()) + "\n");
// get device handle to retrieve string descriptors // get device handle to retrieve string descriptors
Usb_Bus bus = rootBus; Usb_Bus bus = rootBus;
while (bus != null) { while (bus != null) {
@@ -346,22 +356,31 @@ public class UsbTreeModel implements TreeModel, TreeSelectionListener {
for (int i = 0; i < tmpConfDesc.length; i++) { for (int i = 0; i < tmpConfDesc.length; i++) {
if ((tmpConfDesc.equals(confDesc)) if ((tmpConfDesc.equals(confDesc))
&& (confDesc.getIConfiguration() > 0)) { && (confDesc.getIConfiguration() > 0)) {
long handle = LibusbJava.usb_open(dev); try {
sb.append("\nString descriptors\n"); sb.append("\nString descriptors\n");
if (handle == 0) { long handle = LibusbJava1.libusb_open(dev);
sb.append("\terror opening the device\n");
break; try {
String configuration = LibusbJava1
.libusb_get_string_descriptor_ascii(
handle,
confDesc.getIConfiguration(),
255);
if (configuration == null)
configuration = "unable to fetch configuration string";
sb.append("\tiConfiguration: "
+ configuration + "\n");
} catch (LibusbError e1) {
sb.append("\terror getting config descriptor: "
+ e1.getErrorString() + "\n");
} finally {
LibusbJava1.libusb_close(handle);
}
} catch (LibusbError e1) {
sb.append("\terror opening the device: "
+ e1.getErrorString() + "\n");
} finally {
} }
String configuration = LibusbJava
.usb_get_string_simple(handle, confDesc
.getIConfiguration());
if (configuration == null)
configuration = "unable to fetch configuration string";
sb.append("\tiConfiguration: " + configuration
+ "\n");
LibusbJava.usb_close(handle);
} }
} }
dev = dev.getNext(); dev = dev.getNext();
@@ -388,8 +407,7 @@ public class UsbTreeModel implements TreeModel, TreeSelectionListener {
+ Integer.toHexString(intDesc.getBInterfaceNumber() & 0xFF) + Integer.toHexString(intDesc.getBInterfaceNumber() & 0xFF)
+ "\n"); + "\n");
sb.append("\tbAlternateSetting: 0x" sb.append("\tbAlternateSetting: 0x"
+ Integer + Integer.toHexString(intDesc.getBAlternateSetting() & 0xFF)
.toHexString(intDesc.getBAlternateSetting() & 0xFF)
+ "\n"); + "\n");
sb.append("\tbNumEndpoints: 0x" sb.append("\tbNumEndpoints: 0x"
+ Integer.toHexString(intDesc.getBNumEndpoints() & 0xFF) + Integer.toHexString(intDesc.getBNumEndpoints() & 0xFF)
@@ -397,52 +415,65 @@ public class UsbTreeModel implements TreeModel, TreeSelectionListener {
sb.append("\tbInterfaceClass: 0x" sb.append("\tbInterfaceClass: 0x"
+ Integer.toHexString(intDesc.getBInterfaceClass() & 0xFF) + Integer.toHexString(intDesc.getBInterfaceClass() & 0xFF)
+ "\n"); + "\n");
sb sb.append("\tbInterfaceSubClass: 0x"
.append("\tbInterfaceSubClass: 0x" + Integer.toHexString(intDesc.getBInterfaceSubClass() & 0xFF)
+ Integer.toHexString(intDesc + "\n");
.getBInterfaceSubClass() & 0xFF) + "\n"); sb.append("\tbInterfaceProtocol: 0x"
sb + Integer.toHexString(intDesc.getBInterfaceProtocol() & 0xFF)
.append("\tbInterfaceProtocol: 0x" + "\n");
+ Integer.toHexString(intDesc
.getBInterfaceProtocol() & 0xFF) + "\n");
sb.append("\tiInterface: 0x" sb.append("\tiInterface: 0x"
+ Integer.toHexString(intDesc.getIInterface()) + "\n"); + Integer.toHexString(intDesc.getIInterface()) + "\n");
sb.append("\textralen: 0x" sb.append("\textralen: 0x"
+ Integer.toHexString(intDesc.getExtralen()) + "\n"); + Integer.toHexString(intDesc.getExtralen()) + "\n");
sb.append("\textra: " + extraDescriptorToString(intDesc.getExtra()) + "\n"); sb.append("\textra: " + extraDescriptorToString(intDesc.getExtra())
+ "\n");
// get device handle to retrieve string descriptors // get device handle to retrieve string descriptors
Usb_Bus bus = rootBus; Usb_Bus bus = rootBus;
while (bus != null) { while (bus != null) {
Usb_Device dev = bus.getDevices(); Usb_Device dev = bus.getDevices();
while (dev != null) { while (dev != null) {
Usb_Config_Descriptor[] confDescs = dev.getConfig(); try {
for (int i = 0; i < confDescs.length; i++) { long handle = LibusbJava1.libusb_open(dev);
Usb_Interface[] ints = confDescs[i].getInterface();
for (int j = 0; j < ints.length; j++) { Usb_Config_Descriptor[] confDescs = dev.getConfig();
Usb_Interface_Descriptor[] tmpIntDescs = ints[j] for (int i = 0; i < confDescs.length; i++) {
.getAltsetting(); Usb_Interface[] ints = confDescs[i].getInterface();
for (int k = 0; k < ints.length; k++) {
if (i < tmpIntDescs.length && tmpIntDescs[i].equals(intDesc) for (int j = 0; j < ints.length; j++) {
&& (intDesc.getIInterface() > 0)) { Usb_Interface_Descriptor[] tmpIntDescs = ints[j]
long handle = LibusbJava.usb_open(dev); .getAltsetting();
sb.append("\nString descriptors\n");
if (handle == 0) { for (int k = 0; k < ints.length; k++) {
sb if (i < tmpIntDescs.length
.append("\terror opening the device\n"); && tmpIntDescs[i].equals(intDesc)
break; && (intDesc.getIInterface() > 0)) {
sb.append("\nString descriptors\n");
try {
String interface_ = LibusbJava1
.libusb_get_string_descriptor_ascii(
handle,
intDesc.getIInterface(),
255);
if (interface_ == null)
interface_ = "unable to fetch interface string";
sb.append("\tiInterface: "
+ interface_ + "\n");
} catch (LibusbError e1) {
sb.append("\terror while reading descriptors: "
+ e1.getErrorString()
+ "\n");
}
} }
String interface_ = LibusbJava
.usb_get_string_simple(handle,
intDesc.getIInterface());
if (interface_ == null)
interface_ = "unable to fetch interface string";
sb.append("\tiInterface: " + interface_
+ "\n");
LibusbJava.usb_close(handle);
} }
} }
} }
LibusbJava1.libusb_close(handle);
} catch (LibusbError e1) {
sb.append("\terror opening the device: "
+ e1.getErrorString() + "\n");
} }
dev = dev.getNext(); dev = dev.getNext();
} }
bus = bus.getNext(); bus = bus.getNext();
@@ -473,7 +504,8 @@ public class UsbTreeModel implements TreeModel, TreeSelectionListener {
+ Integer.toHexString(epDesc.getBSynchAddress()) + "\n"); + Integer.toHexString(epDesc.getBSynchAddress()) + "\n");
sb.append("\textralen: 0x" sb.append("\textralen: 0x"
+ Integer.toHexString(epDesc.getExtralen()) + "\n"); + Integer.toHexString(epDesc.getExtralen()) + "\n");
sb.append("\textra: " + extraDescriptorToString(epDesc.getExtra()) + "\n"); sb.append("\textra: " + extraDescriptorToString(epDesc.getExtra())
+ "\n");
textArea.setText(sb.toString()); textArea.setText(sb.toString());
} }
} }