- LibusbWin renamed to LibusbJava

git-svn-id: https://svn.code.sf.net/p/libusbjava/code/trunk@208 94ad28fe-ef68-46b1-9651-e7ae4fcf1c4c
This commit is contained in:
schlaepfer
2006-11-24 13:05:08 +00:00
parent 48d62f7232
commit 17dcf1b09d
8 changed files with 70 additions and 66 deletions

View File

@@ -48,18 +48,18 @@ public class Device {
} }
private void initUSB() { private void initUSB() {
LibusbWin.usb_init(); LibusbJava.usb_init();
initUSBDone = true; initUSBDone = true;
} }
private Usb_Bus initBus() throws USBException { private Usb_Bus initBus() throws USBException {
LibusbWin.usb_find_busses(); LibusbJava.usb_find_busses();
LibusbWin.usb_find_devices(); LibusbJava.usb_find_devices();
Usb_Bus bus = LibusbWin.usb_get_busses(); Usb_Bus bus = LibusbJava.usb_get_busses();
if (bus == null) { if (bus == null) {
throw new USBException("LibusbWin.usb_get_busses(): " throw new USBException("LibusbWin.usb_get_busses(): "
+ LibusbWin.usb_strerror()); + LibusbJava.usb_strerror());
} }
return bus; return bus;
@@ -183,10 +183,10 @@ public class Device {
dev = initDevice(); dev = initDevice();
if (dev != null) { if (dev != null) {
int res = LibusbWin.usb_open(dev); int res = LibusbJava.usb_open(dev);
if (res <= 0) { if (res <= 0) {
throw new USBException("LibusbWin.usb_open: " throw new USBException("LibusbWin.usb_open: "
+ LibusbWin.usb_strerror()); + LibusbJava.usb_strerror());
} }
usbDevHandle = res; usbDevHandle = res;
} }
@@ -221,10 +221,10 @@ public class Device {
throw new USBException("invalid device handle"); throw new USBException("invalid device handle");
} }
release_interface(usbDevHandle, dev_interface); release_interface(usbDevHandle, dev_interface);
if (LibusbWin.usb_close(usbDevHandle) < 0) { if (LibusbJava.usb_close(usbDevHandle) < 0) {
usbDevHandle = 0; usbDevHandle = 0;
throw new USBException("LibusbWin.usb_close: " throw new USBException("LibusbWin.usb_close: "
+ LibusbWin.usb_strerror()); + LibusbJava.usb_strerror());
} }
usbDevHandle = 0; usbDevHandle = 0;
maxPacketSize = -1; maxPacketSize = -1;
@@ -242,10 +242,10 @@ public class Device {
if (usbDevHandle <= 0) { if (usbDevHandle <= 0) {
throw new USBException("invalid device handle"); throw new USBException("invalid device handle");
} }
if (LibusbWin.usb_reset(usbDevHandle) < 0) { if (LibusbJava.usb_reset(usbDevHandle) < 0) {
usbDevHandle = 0; usbDevHandle = 0;
throw new USBException("LibusbWin.usb_reset: " throw new USBException("LibusbWin.usb_reset: "
+ LibusbWin.usb_strerror()); + LibusbJava.usb_strerror());
} }
usbDevHandle = 0; usbDevHandle = 0;
logger.info("device reset"); logger.info("device reset");
@@ -280,7 +280,7 @@ public class Device {
if (length <= 0) { if (length <= 0) {
throw new USBException("size must be > 0"); throw new USBException("size must be > 0");
} }
int lenWritten = LibusbWin.usb_bulk_write(usbDevHandle, out_ep_address, int lenWritten = LibusbJava.usb_bulk_write(usbDevHandle, out_ep_address,
data, length, timeout); data, length, timeout);
if (lenWritten < 0) { if (lenWritten < 0) {
if (lenWritten == TIMEOUT_ERROR_CODE) { if (lenWritten == TIMEOUT_ERROR_CODE) {
@@ -293,10 +293,10 @@ public class Device {
false); false);
} }
throw new USBTimeoutException("LibusbWin.usb_bulk_write: " throw new USBTimeoutException("LibusbWin.usb_bulk_write: "
+ LibusbWin.usb_strerror()); + LibusbJava.usb_strerror());
} }
throw new USBException("LibusbWin.usb_bulk_write: " throw new USBException("LibusbWin.usb_bulk_write: "
+ LibusbWin.usb_strerror()); + LibusbJava.usb_strerror());
} }
logger.info("length written: " + lenWritten); logger.info("length written: " + lenWritten);
@@ -341,7 +341,7 @@ public class Device {
if (size <= 0) { if (size <= 0) {
throw new USBException("size must be > 0"); throw new USBException("size must be > 0");
} }
int lenRead = LibusbWin.usb_bulk_read(usbDevHandle, in_ep_address, int lenRead = LibusbJava.usb_bulk_read(usbDevHandle, in_ep_address,
data, size, timeout); data, size, timeout);
if (lenRead < 0) { if (lenRead < 0) {
if (lenRead == TIMEOUT_ERROR_CODE) { if (lenRead == TIMEOUT_ERROR_CODE) {
@@ -353,10 +353,10 @@ public class Device {
return readBulk(in_ep_address, data, size, timeout, false); return readBulk(in_ep_address, data, size, timeout, false);
} }
throw new USBTimeoutException("LibusbWin.usb_bulk_read: " throw new USBTimeoutException("LibusbWin.usb_bulk_read: "
+ LibusbWin.usb_strerror()); + LibusbJava.usb_strerror());
} }
throw new USBException("LibusbWin.usb_bulk_read: " throw new USBException("LibusbWin.usb_bulk_read: "
+ LibusbWin.usb_strerror()); + LibusbJava.usb_strerror());
} }
logger.info("length read: " + lenRead); logger.info("length read: " + lenRead);
@@ -401,7 +401,7 @@ public class Device {
if (length <= 0) { if (length <= 0) {
throw new USBException("size must be > 0"); throw new USBException("size must be > 0");
} }
int lenWritten = LibusbWin.usb_interrupt_write(usbDevHandle, int lenWritten = LibusbJava.usb_interrupt_write(usbDevHandle,
out_ep_address, data, length, timeout); out_ep_address, data, length, timeout);
if (lenWritten < 0) { if (lenWritten < 0) {
if (lenWritten == TIMEOUT_ERROR_CODE) { if (lenWritten == TIMEOUT_ERROR_CODE) {
@@ -414,10 +414,10 @@ public class Device {
timeout, false); timeout, false);
} }
throw new USBTimeoutException("LibusbWin.usb_bulk_write: " throw new USBTimeoutException("LibusbWin.usb_bulk_write: "
+ LibusbWin.usb_strerror()); + LibusbJava.usb_strerror());
} }
throw new USBException("LibusbWin.usb_bulk_write: " throw new USBException("LibusbWin.usb_bulk_write: "
+ LibusbWin.usb_strerror()); + LibusbJava.usb_strerror());
} }
logger.info("length written: " + lenWritten); logger.info("length written: " + lenWritten);
@@ -462,7 +462,7 @@ public class Device {
if (size <= 0) { if (size <= 0) {
throw new USBException("size must be > 0"); throw new USBException("size must be > 0");
} }
int lenRead = LibusbWin.usb_interrupt_read(usbDevHandle, in_ep_address, int lenRead = LibusbJava.usb_interrupt_read(usbDevHandle, in_ep_address,
data, size, timeout); data, size, timeout);
if (lenRead < 0) { if (lenRead < 0) {
if (lenRead == TIMEOUT_ERROR_CODE) { if (lenRead == TIMEOUT_ERROR_CODE) {
@@ -475,10 +475,10 @@ public class Device {
false); false);
} }
throw new USBTimeoutException("LibusbWin.usb_bulk_read: " throw new USBTimeoutException("LibusbWin.usb_bulk_read: "
+ LibusbWin.usb_strerror()); + LibusbJava.usb_strerror());
} }
throw new USBException("LibusbWin.usb_bulk_read: " throw new USBException("LibusbWin.usb_bulk_read: "
+ LibusbWin.usb_strerror()); + LibusbJava.usb_strerror());
} }
logger.info("length read: " + lenRead); logger.info("length read: " + lenRead);
@@ -511,18 +511,18 @@ public class Device {
*/ */
private void claim_interface(int usb_dev_handle, int configuration, private void claim_interface(int usb_dev_handle, int configuration,
int interface_, int altinterface) throws USBException { int interface_, int altinterface) throws USBException {
if (LibusbWin.usb_set_configuration(usb_dev_handle, configuration) < 0) { if (LibusbJava.usb_set_configuration(usb_dev_handle, configuration) < 0) {
throw new USBException("LibusbWin.usb_set_configuration: " throw new USBException("LibusbWin.usb_set_configuration: "
+ LibusbWin.usb_strerror()); + LibusbJava.usb_strerror());
} }
if (LibusbWin.usb_claim_interface(usb_dev_handle, interface_) < 0) { if (LibusbJava.usb_claim_interface(usb_dev_handle, interface_) < 0) {
throw new USBException("LibusbWin.usb_claim_interface: " throw new USBException("LibusbWin.usb_claim_interface: "
+ LibusbWin.usb_strerror()); + LibusbJava.usb_strerror());
} }
if (altinterface >= 0) { if (altinterface >= 0) {
if (LibusbWin.usb_set_altinterface(usb_dev_handle, altinterface) < 0) { if (LibusbJava.usb_set_altinterface(usb_dev_handle, altinterface) < 0) {
throw new USBException("LibusbWin.usb_set_altinterface: " throw new USBException("LibusbWin.usb_set_altinterface: "
+ LibusbWin.usb_strerror()); + LibusbJava.usb_strerror());
} }
} }
logger.info("interface claimed"); logger.info("interface claimed");
@@ -540,10 +540,10 @@ public class Device {
*/ */
private void release_interface(int dev_handle, int interface_) private void release_interface(int dev_handle, int interface_)
throws USBException { throws USBException {
if (LibusbWin.usb_release_interface(dev_handle, interface_) < 0) { if (LibusbJava.usb_release_interface(dev_handle, interface_) < 0) {
usbDevHandle = 0; usbDevHandle = 0;
throw new USBException("LibusbWin.usb_release_interface: " throw new USBException("LibusbWin.usb_release_interface: "
+ LibusbWin.usb_strerror()); + LibusbJava.usb_strerror());
} }
logger.info("interface released"); logger.info("interface released");
} }

View File

@@ -23,10 +23,9 @@ package ch.ntb.usb;
* and adapted where neccessary.<br> * and adapted where neccessary.<br>
* *
* @author schlaepfer * @author schlaepfer
* @version DLL: 00.02.00
* *
*/ */
public class LibusbWin { public class LibusbJava {
/** /**
* Sets the debugging level of libusb.<br> * Sets the debugging level of libusb.<br>
@@ -327,6 +326,11 @@ public class LibusbWin {
/** **************************************************************** */ /** **************************************************************** */
static { static {
System.load(System.getenv("SystemRoot") + "/system32/LibusbWin.dll"); String os = System.getProperty("os.name");
if (os.contains("Windows")) {
System.loadLibrary("LibusbJava");
} else {
System.loadLibrary("usbJava");
}
} }
} }

View File

@@ -39,7 +39,7 @@ public class Usb_Config_Descriptor extends Usb_Descriptor {
public byte bNumInterfaces; public byte bNumInterfaces;
/** /**
* Value to use as an argument to select this configuration ({@link LibusbWin#usb_set_configuration(int, int)}). * Value to use as an argument to select this configuration ({@link LibusbJava#usb_set_configuration(int, int)}).
*/ */
public byte bConfigurationValue; public byte bConfigurationValue;

View File

@@ -31,7 +31,7 @@ public class Usb_Interface_Descriptor extends Usb_Descriptor {
public byte bInterfaceNumber; public byte bInterfaceNumber;
/** /**
* Value used to select alternate setting ({@link LibusbWin#usb_set_altinterface(int, int)}). * Value used to select alternate setting ({@link LibusbJava#usb_set_altinterface(int, int)}).
*/ */
public byte bAlternateSetting; public byte bAlternateSetting;

View File

@@ -8,7 +8,7 @@ package ch.ntb.usb;
public class Utils { public class Utils {
public static void logUsb(Usb_Bus bus) { public static void logBus(Usb_Bus bus) {
Usb_Bus usb_Bus = bus; Usb_Bus usb_Bus = bus;
while (usb_Bus != null) { while (usb_Bus != null) {
System.out.println(usb_Bus.toString()); System.out.println(usb_Bus.toString());
@@ -17,24 +17,24 @@ public class Utils {
System.out.println("\t" + dev.toString()); System.out.println("\t" + dev.toString());
// Usb_Device_Descriptor // Usb_Device_Descriptor
Usb_Device_Descriptor defDesc = dev.descriptor; Usb_Device_Descriptor defDesc = dev.descriptor;
System.out.println("\t" + defDesc.toString()); System.out.println("\t\t" + defDesc.toString());
// Usb_Config_Descriptor // Usb_Config_Descriptor
Usb_Config_Descriptor[] confDesc = dev.config; Usb_Config_Descriptor[] confDesc = dev.config;
for (int i = 0; i < confDesc.length; i++) { for (int i = 0; i < confDesc.length; i++) {
System.out.println("\t" + confDesc[i].toString()); System.out.println("\t\t" + confDesc[i].toString());
Usb_Interface[] int_ = confDesc[i].interface_; Usb_Interface[] int_ = confDesc[i].interface_;
if (int_ != null) { if (int_ != null) {
for (int j = 0; j < int_.length; j++) { for (int j = 0; j < int_.length; j++) {
System.out.println("\t" + int_[j].toString()); System.out.println("\t\t\t" + int_[j].toString());
Usb_Interface_Descriptor[] intDesc = int_[j].altsetting; Usb_Interface_Descriptor[] intDesc = int_[j].altsetting;
if (intDesc != null) { if (intDesc != null) {
for (int k = 0; k < intDesc.length; k++) { for (int k = 0; k < intDesc.length; k++) {
System.out.println("\t" System.out.println("\t\t\t\t"
+ intDesc[k].toString()); + intDesc[k].toString());
Usb_Endpoint_Descriptor[] epDesc = intDesc[k].endpoint; Usb_Endpoint_Descriptor[] epDesc = intDesc[k].endpoint;
if (epDesc != null) { if (epDesc != null) {
for (int e = 0; e < epDesc.length; e++) { for (int e = 0; e < epDesc.length; e++) {
System.out.println("\t" System.out.println("\t\t\t\t\t"
+ epDesc[e].toString()); + epDesc[e].toString());
} }
} }

View File

@@ -17,7 +17,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.usb.LibusbWin; import ch.ntb.usb.LibusbJava;
import ch.ntb.usb.Usb_Bus; import ch.ntb.usb.Usb_Bus;
import ch.ntb.usb.Usb_Config_Descriptor; import ch.ntb.usb.Usb_Config_Descriptor;
import ch.ntb.usb.Usb_Device; import ch.ntb.usb.Usb_Device;
@@ -226,14 +226,14 @@ public class UsbTreeModel implements TreeModel, TreeSelectionListener {
&& ((dev.descriptor.iManufacturer > 0) && ((dev.descriptor.iManufacturer > 0)
|| (dev.descriptor.iProduct > 0) || (dev.descriptor.iSerialNumber > 0))) { || (dev.descriptor.iProduct > 0) || (dev.descriptor.iSerialNumber > 0))) {
if (tmpDevDesc.equals(devDesc)) { if (tmpDevDesc.equals(devDesc)) {
int handle = LibusbWin.usb_open(dev); int handle = LibusbJava.usb_open(dev);
sb.append("\nString descriptors\n"); sb.append("\nString descriptors\n");
if (handle <= 0) { if (handle <= 0) {
sb.append("\terror opening the device\n"); sb.append("\terror opening the device\n");
break; break;
} }
if (dev.descriptor.iManufacturer > 0) { if (dev.descriptor.iManufacturer > 0) {
String manufacturer = LibusbWin String manufacturer = LibusbJava
.usb_get_string_simple(handle, .usb_get_string_simple(handle,
devDesc.iManufacturer); devDesc.iManufacturer);
if (manufacturer == null) if (manufacturer == null)
@@ -242,7 +242,7 @@ public class UsbTreeModel implements TreeModel, TreeSelectionListener {
+ "\n"); + "\n");
} }
if (dev.descriptor.iProduct > 0) { if (dev.descriptor.iProduct > 0) {
String product = LibusbWin String product = LibusbJava
.usb_get_string_simple(handle, .usb_get_string_simple(handle,
devDesc.iProduct); devDesc.iProduct);
if (product == null) if (product == null)
@@ -250,7 +250,7 @@ public class UsbTreeModel implements TreeModel, TreeSelectionListener {
sb.append("\tiProduct: " + product + "\n"); sb.append("\tiProduct: " + product + "\n");
} }
if (dev.descriptor.iSerialNumber > 0) { if (dev.descriptor.iSerialNumber > 0) {
String serialNumber = LibusbWin String serialNumber = LibusbJava
.usb_get_string_simple(handle, .usb_get_string_simple(handle,
devDesc.iSerialNumber); devDesc.iSerialNumber);
if (serialNumber == null) if (serialNumber == null)
@@ -258,7 +258,7 @@ public class UsbTreeModel implements TreeModel, TreeSelectionListener {
sb.append("\tiSerialNumber: " + serialNumber sb.append("\tiSerialNumber: " + serialNumber
+ "\n"); + "\n");
} }
LibusbWin.usb_close(handle); LibusbJava.usb_close(handle);
} }
} }
dev = dev.next; dev = dev.next;
@@ -296,13 +296,13 @@ 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.iConfiguration > 0)) { && (confDesc.iConfiguration > 0)) {
int handle = LibusbWin.usb_open(dev); int handle = LibusbJava.usb_open(dev);
sb.append("\nString descriptors\n"); sb.append("\nString descriptors\n");
if (handle <= 0) { if (handle <= 0) {
sb.append("\terror opening the device\n"); sb.append("\terror opening the device\n");
break; break;
} }
String configuration = LibusbWin String configuration = LibusbJava
.usb_get_string_simple(handle, .usb_get_string_simple(handle,
confDesc.iConfiguration); confDesc.iConfiguration);
if (configuration == null) if (configuration == null)
@@ -310,7 +310,7 @@ public class UsbTreeModel implements TreeModel, TreeSelectionListener {
sb.append("\tiConfiguration: " + configuration sb.append("\tiConfiguration: " + configuration
+ "\n"); + "\n");
LibusbWin.usb_close(handle); LibusbJava.usb_close(handle);
} }
} }
@@ -366,21 +366,21 @@ public class UsbTreeModel implements TreeModel, TreeSelectionListener {
for (int k = 0; k < ints.length; k++) { for (int k = 0; k < ints.length; k++) {
if (tmpIntDescs[i].equals(intDesc) if (tmpIntDescs[i].equals(intDesc)
&& (intDesc.iInterface > 0)) { && (intDesc.iInterface > 0)) {
int handle = LibusbWin.usb_open(dev); int handle = LibusbJava.usb_open(dev);
sb.append("\nString descriptors\n"); sb.append("\nString descriptors\n");
if (handle <= 0) { if (handle <= 0) {
sb sb
.append("\terror opening the device\n"); .append("\terror opening the device\n");
break; break;
} }
String interface_ = LibusbWin String interface_ = LibusbJava
.usb_get_string_simple(handle, .usb_get_string_simple(handle,
intDesc.iInterface); intDesc.iInterface);
if (interface_ == null) if (interface_ == null)
interface_ = "unable to fetch interface string"; interface_ = "unable to fetch interface string";
sb.append("\tiInterface: " + interface_ sb.append("\tiInterface: " + interface_
+ "\n"); + "\n");
LibusbWin.usb_close(handle); LibusbJava.usb_close(handle);
} }
} }
} }

View File

@@ -21,7 +21,7 @@ import javax.swing.JTextArea;
import javax.swing.JTree; import javax.swing.JTree;
import javax.swing.KeyStroke; import javax.swing.KeyStroke;
import ch.ntb.usb.LibusbWin; import ch.ntb.usb.LibusbJava;
import ch.ntb.usb.Usb_Bus; import ch.ntb.usb.Usb_Bus;
public class UsbView extends JFrame { public class UsbView extends JFrame {
@@ -144,11 +144,11 @@ public class UsbView extends JFrame {
.addActionListener(new java.awt.event.ActionListener() { .addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) { public void actionPerformed(java.awt.event.ActionEvent e) {
// open bus // open bus
LibusbWin.usb_init(); LibusbJava.usb_init();
LibusbWin.usb_find_busses(); LibusbJava.usb_find_busses();
LibusbWin.usb_find_devices(); LibusbJava.usb_find_devices();
Usb_Bus bus = LibusbWin.usb_get_busses(); Usb_Bus bus = LibusbJava.usb_get_busses();
if (bus != null) { if (bus != null) {
treeModel.fireTreeStructureChanged(bus); treeModel.fireTreeStructureChanged(bus);
} }
@@ -166,11 +166,11 @@ public class UsbView extends JFrame {
private JTree getUsbTree() { private JTree getUsbTree() {
if (usbTree == null) { if (usbTree == null) {
// open bus // open bus
LibusbWin.usb_init(); LibusbJava.usb_init();
LibusbWin.usb_find_busses(); LibusbJava.usb_find_busses();
LibusbWin.usb_find_devices(); LibusbJava.usb_find_devices();
Usb_Bus bus = LibusbWin.usb_get_busses(); Usb_Bus bus = LibusbJava.usb_get_busses();
treeModel = new UsbTreeModel(bus, jPropertiesArea); treeModel = new UsbTreeModel(bus, jPropertiesArea);
usbTree = new JTree(treeModel); usbTree = new JTree(treeModel);

View File

@@ -20,7 +20,7 @@ import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
import ch.ntb.usb.Device; import ch.ntb.usb.Device;
import ch.ntb.usb.LibusbWin; import ch.ntb.usb.LibusbJava;
import ch.ntb.usb.USB; import ch.ntb.usb.USB;
import ch.ntb.usb.USBException; import ch.ntb.usb.USBException;
import ch.ntb.usb.Usb_Config_Descriptor; import ch.ntb.usb.Usb_Config_Descriptor;
@@ -52,7 +52,7 @@ public class DeviceTest {
devInfoProp.load(propInputStream); devInfoProp.load(propInputStream);
String devInfoClazzName = devInfoProp.getProperty(deviceInfoKey); String devInfoClazzName = devInfoProp.getProperty(deviceInfoKey);
if (devInfoClazzName == null) { if (devInfoClazzName == null) {
throw new IllegalArgumentException("property " + deviceInfoKey throw new Exception("property " + deviceInfoKey
+ " not found in file " + testdevicePropertiesFile); + " not found in file " + testdevicePropertiesFile);
} }
Class devInfoClazz = Class.forName(devInfoClazzName); Class devInfoClazz = Class.forName(devInfoClazzName);
@@ -62,7 +62,7 @@ public class DeviceTest {
testData = new byte[devinfo.getMaxDataSize()]; testData = new byte[devinfo.getMaxDataSize()];
readData = new byte[testData.length]; readData = new byte[testData.length];
// initialise the device // initialise the device
LibusbWin.usb_set_debug(255); LibusbJava.usb_set_debug(255);
dev = USB.getDevice(devinfo.getIdVendor(), devinfo.getIdProduct()); dev = USB.getDevice(devinfo.getIdVendor(), devinfo.getIdProduct());
} }