- Changed the project name according to the new naming convention

- Changed package names according to the new naming convention


git-svn-id: https://svn.code.sf.net/p/libusbjava/code/trunk@277 94ad28fe-ef68-46b1-9651-e7ae4fcf1c4c
This commit is contained in:
uniederer
2012-03-16 12:17:47 +00:00
parent 8b297fd01e
commit 5ef50110e7
43 changed files with 362 additions and 362 deletions

View File

@@ -0,0 +1,816 @@
/*
* Java libusb wrapper
* Copyright (c) 2005-2006 Andreas Schl<68>pfer <spandi at users.sourceforge.net>
*
* http://libusbjava.sourceforge.net
* This library is covered by the LGPL, read LGPL.txt for details.
*/
package ch.ntb.inf.libusbJava;
import java.util.logging.Level;
import java.util.logging.Logger;
import ch.ntb.inf.libusbJava.logger.LogUtil;
/**
* This class represents an USB device.<br>
* To get an instance of an USB device use <code>USB.getDevice(...)</code>.
*
*/
public class Device {
private static final Logger logger = LogUtil.getLogger("ch.ntb.inf.libusbJava");
private int maxPacketSize;
/**
* Mandatory identification values for the device.
*/
private int idVendor, idProduct;
/**
* Optional identification value for the device (e.g. if there are multiple
* devices with the same vendor and product id).
*/
private String filename;
/**
* Optional identification value for the device (e.g. if there are multiple
* devices with the same vendor and product id).
*/
private String busName;
private int dev_configuration, dev_interface, dev_altinterface;
private long usbDevHandle;
private boolean resetOnFirstOpen, resetDone;
private int resetTimeout = 2000;
private Usb_Device dev;
protected Device(short idVendor, short idProduct) {
resetOnFirstOpen = false;
resetDone = false;
maxPacketSize = -1;
this.idVendor = idVendor;
this.idProduct = idProduct;
this.filename = null;
}
protected Device(short idVendor, short idProduct, String busName,
String filename) {
resetOnFirstOpen = false;
resetDone = false;
maxPacketSize = -1;
this.idVendor = idVendor;
this.idProduct = idProduct;
this.busName = busName;
this.filename = filename;
}
private void updateMaxPacketSize(Usb_Device device) throws USBException {
maxPacketSize = -1;
Usb_Config_Descriptor[] confDesc = device.getConfig();
for (int i = 0; i < confDesc.length; i++) {
Usb_Interface[] int_ = confDesc[i].getInterface();
for (int j = 0; j < int_.length; j++) {
Usb_Interface_Descriptor[] intDesc = int_[j].getAltsetting();
for (int k = 0; k < intDesc.length; k++) {
Usb_Endpoint_Descriptor[] epDesc = intDesc[k].getEndpoint();
for (int l = 0; l < epDesc.length; l++) {
maxPacketSize = Math.max(epDesc[l].getWMaxPacketSize(),
maxPacketSize);
}
}
}
}
if (maxPacketSize <= 0) {
throw new USBException(
"No USB endpoints found. Check the device configuration");
}
}
/**
* Initializes the device. The parameters <code>idVendor</code> and
* <code>idProduct</code> are mandatory. The parameter <code>filename</code>
* is optional.
*/
private Usb_Device initDevice(int idVendorParam, int idProductParam,
String busName, String filename) throws USBException {
Usb_Bus bus = USB.getBus();
Usb_Device device = null;
// search for device
while (bus != null) {
device = bus.getDevices();
while (device != null) {
Usb_Device_Descriptor devDesc = device.getDescriptor();
if (busName != null && filename != null) {
if (busName.compareTo(bus.getDirname()) == 0
&& filename.compareTo(device.getFilename()) == 0
&& devDesc.getIdVendor() == idVendor
&& devDesc.getIdProduct() == idProduct) {
logger.info("Device found. bus: " + bus.getDirname()
+ ", filename: " + device.getFilename());
updateMaxPacketSize(device);
return device;
}
} else if (filename != null) {
if (filename.compareTo(device.getFilename()) == 0
&& devDesc.getIdVendor() == idVendor
&& devDesc.getIdProduct() == idProduct) {
logger.info("Device found. bus: " + bus.getDirname()
+ ", filename: " + device.getFilename());
updateMaxPacketSize(device);
return device;
}
} else if (busName != null) {
if (busName.compareTo(bus.getDirname()) == 0
&& devDesc.getIdVendor() == idVendor
&& devDesc.getIdProduct() == idProduct) {
logger.info("Device found. bus: " + bus.getDirname()
+ ", filename: " + device.getFilename());
updateMaxPacketSize(device);
return device;
}
} else if (devDesc.getIdVendor() == idVendor
&& devDesc.getIdProduct() == idProduct) {
logger.info("Device found. bus: " + bus.getDirname()
+ ", filename: " + device.getFilename());
updateMaxPacketSize(device);
return device;
}
device = device.getNext();
}
bus = bus.getNext();
}
return null;
}
/**
* Updates the device and descriptor information from the bus.<br>
* The descriptors can be read with {@link #getDeviceDescriptor()} and
* {@link #getConfigDescriptors()}.
*
* @throws USBException
*/
public void updateDescriptors() throws USBException {
dev = initDevice(idVendor, idProduct, busName, filename);
}
/**
* Returns the device descriptor associated with this device.<br>
* The descriptor is updated by calling {@link #updateDescriptors()} or
* {@link #open(int, int, int)}.
*
* @return the device descriptor associated with this device or
* <code>null</code>
*/
public Usb_Device_Descriptor getDeviceDescriptor() {
if (dev == null) {
return null;
}
return dev.getDescriptor();
}
/**
* Returns the configuration descriptors associated with this device.<br>
* The descriptors are updated by calling {@link #updateDescriptors()} or
* {@link #open(int, int, int)}.
*
* @return the configuration descriptors associated with this device or
* <code>null</code>
*/
public Usb_Config_Descriptor[] getConfigDescriptors() {
if (dev == null) {
return null;
}
return dev.getConfig();
}
/**
* Opens the device and claims the specified configuration, interface and
* altinterface.<br>
* First the bus is enumerated. If the device is found its descriptors are
* read and the <code>maxPacketSize</code> value is updated. If no endpoints
* are found in the descriptors an exception is thrown.
*
* @param configuration
* the configuration, see
* {@link Usb_Config_Descriptor#getBConfigurationValue()}
* @param interface_
* the interface, see
* {@link Usb_Interface_Descriptor#getBInterfaceNumber()}
* @param altinterface
* the alternate interface, see
* {@link Usb_Interface_Descriptor#getBAlternateSetting()}. If no
* alternate interface must be set <i>-1</i> can be used.
* @throws USBException
*/
public void open(int configuration, int interface_, int altinterface)
throws USBException {
this.dev_configuration = configuration;
this.dev_interface = interface_;
this.dev_altinterface = altinterface;
if (usbDevHandle != 0) {
throw new USBException("device opened, close or reset first");
}
dev = initDevice(idVendor, idProduct, busName, filename);
if (dev != null) {
long res = LibusbJava.usb_open(dev);
if (res == 0) {
throw new USBException("LibusbJava.usb_open: "
+ LibusbJava.usb_strerror());
}
usbDevHandle = res;
}
if (dev == null || usbDevHandle == 0) {
throw new USBException("USB device with " + toString()
+ " not found on USB");
}
claim_interface(usbDevHandle, configuration, interface_, altinterface);
if (resetOnFirstOpen & !resetDone) {
logger.info("reset on first open");
resetDone = true;
reset();
try {
Thread.sleep(resetTimeout);
} catch (InterruptedException e) {
//
}
open(configuration, interface_, altinterface);
}
}
/**
* Release the claimed interface and close the opened device.<br>
*
* @throws USBException
*/
public void close() throws USBException {
if (usbDevHandle == 0) {
throw new USBException("invalid device handle");
}
release_interface(usbDevHandle, dev_interface);
if (LibusbJava.usb_close(usbDevHandle) < 0) {
usbDevHandle = 0;
throw new USBException("LibusbJava.usb_close: "
+ LibusbJava.usb_strerror());
}
usbDevHandle = 0;
maxPacketSize = -1;
logger.info("device closed");
}
/**
* Sends an USB reset to the device. The device handle will no longer be
* valid. To use the device again, {@link #open(int, int, int)} must be
* called.<br>
* Note that the device is re-attached to the USB which may cause the bus
* and filename to be changed. If the bus and filename parameters are used
* in {@link USB#getDevice(short, short, String, String)} unregister the
* device using {@link USB#unregisterDevice(Device)}, re-enumerate the bus
* and create a new device instance. If that is not done the device may not
* be found.
*
* @throws USBException
*/
public void reset() throws USBException {
if (usbDevHandle == 0) {
throw new USBException("invalid device handle");
}
release_interface(usbDevHandle, dev_interface);
if (LibusbJava.usb_reset(usbDevHandle) < 0) {
usbDevHandle = 0;
throw new USBException("LibusbJava.usb_reset: "
+ LibusbJava.usb_strerror());
}
usbDevHandle = 0;
logger.info("device reset");
}
/**
* Write data to the device using a bulk transfer.<br>
*
* @param out_ep_address
* endpoint address to write to
* @param data
* data to write to this endpoint
* @param size
* size of the data
* @param timeout
* amount of time in ms the device will try to send the data
* until a timeout exception is thrown
* @param reopenOnTimeout
* if set to true, the device will try to open the connection and
* send the data again before a timeout exception is thrown
* @return the actual number of bytes written
* @throws USBException
*/
public int writeBulk(int out_ep_address, byte[] data, int size,
int timeout, boolean reopenOnTimeout) throws USBException {
if (usbDevHandle == 0) {
throw new USBException("invalid device handle");
}
if (data == null) {
throw new USBException("data must not be null");
}
if (size <= 0 || size > data.length) {
throw new ArrayIndexOutOfBoundsException("invalid size: " + size);
}
int lenWritten = LibusbJava.usb_bulk_write(usbDevHandle,
out_ep_address, data, size, timeout);
if (lenWritten < 0) {
if (lenWritten == LibusbJava.ERROR_TIMEDOUT) {
// try to reopen the device and send the data again
if (reopenOnTimeout) {
logger.info("try to reopen");
reset();
open(dev_configuration, dev_interface, dev_altinterface);
return writeBulk(out_ep_address, data, size, timeout, false);
}
throw new USBTimeoutException("LibusbJava.usb_bulk_write: "
+ LibusbJava.usb_strerror());
}
throw new USBException("LibusbJava.usb_bulk_write: "
+ LibusbJava.usb_strerror());
}
logger.info("length written: " + lenWritten);
if (logger.isLoggable(Level.FINEST)) {
StringBuffer sb = new StringBuffer("bulkwrite, ep 0x"
+ Integer.toHexString(out_ep_address) + ": " + lenWritten
+ " Bytes sent: ");
for (int i = 0; i < lenWritten; i++) {
sb.append("0x" + String.format("%1$02X", data[i]) + " ");
}
logger.info(sb.toString());
}
return lenWritten;
}
/**
* Read data from the device using a bulk transfer.<br>
*
* @param in_ep_address
* endpoint address to read from
* @param data
* data buffer for the data to be read
* @param size
* the maximum requested data size
* @param timeout
* amount of time in ms the device will try to receive data until
* a timeout exception is thrown
* @param reopenOnTimeout
* if set to true, the device will try to open the connection and
* receive the data again before a timeout exception is thrown
* @return the actual number of bytes read
* @throws USBException
*/
public int readBulk(int in_ep_address, byte[] data, int size, int timeout,
boolean reopenOnTimeout) throws USBException {
if (usbDevHandle == 0) {
throw new USBException("invalid device handle");
}
if (data == null) {
throw new USBException("data must not be null");
}
if (size <= 0 || size > data.length) {
throw new ArrayIndexOutOfBoundsException("invalid size: " + size);
}
int lenRead = LibusbJava.usb_bulk_read(usbDevHandle, in_ep_address,
data, size, timeout);
if (lenRead < 0) {
if (lenRead == LibusbJava.ERROR_TIMEDOUT) {
// try to reopen the device and send the data again
if (reopenOnTimeout) {
logger.info("try to reopen");
reset();
open(dev_configuration, dev_interface, dev_altinterface);
return readBulk(in_ep_address, data, size, timeout, false);
}
throw new USBTimeoutException("LibusbJava.usb_bulk_read: "
+ LibusbJava.usb_strerror());
}
throw new USBException("LibusbJava.usb_bulk_read: "
+ LibusbJava.usb_strerror());
}
logger.info("length read: " + lenRead);
if (logger.isLoggable(Level.FINEST)) {
StringBuffer sb = new StringBuffer("bulkread, ep 0x"
+ Integer.toHexString(in_ep_address) + ": " + lenRead
+ " Bytes received: ");
for (int i = 0; i < lenRead; i++) {
sb.append("0x" + String.format("%1$02X", data[i]) + " ");
}
logger.info(sb.toString());
}
return lenRead;
}
/**
* Write data to the device using a interrupt transfer.<br>
*
* @param out_ep_address
* endpoint address to write to
* @param data
* data to write to this endpoint
* @param size
* size of the data
* @param timeout
* amount of time in ms the device will try to send the data
* until a timeout exception is thrown
* @param reopenOnTimeout
* if set to true, the device will try to open the connection and
* send the data again before a timeout exception is thrown
* @return the actual number of bytes written
* @throws USBException
*/
public int writeInterrupt(int out_ep_address, byte[] data, int size,
int timeout, boolean reopenOnTimeout) throws USBException {
if (usbDevHandle == 0) {
throw new USBException("invalid device handle");
}
if (data == null) {
throw new USBException("data must not be null");
}
if (size <= 0 || size > data.length) {
throw new ArrayIndexOutOfBoundsException("invalid size: " + size);
}
int lenWritten = LibusbJava.usb_interrupt_write(usbDevHandle,
out_ep_address, data, size, timeout);
if (lenWritten < 0) {
if (lenWritten == LibusbJava.ERROR_TIMEDOUT) {
// try to reopen the device and send the data again
if (reopenOnTimeout) {
logger.info("try to reopen");
reset();
open(dev_configuration, dev_interface, dev_altinterface);
return writeInterrupt(out_ep_address, data, size, timeout,
false);
}
throw new USBTimeoutException(
"LibusbJava.usb_interrupt_write: "
+ LibusbJava.usb_strerror());
}
throw new USBException("LibusbJava.usb_interrupt_write: "
+ LibusbJava.usb_strerror());
}
logger.info("length written: " + lenWritten);
if (logger.isLoggable(Level.FINEST)) {
StringBuffer sb = new StringBuffer("interruptwrite, ep 0x"
+ Integer.toHexString(out_ep_address) + ": " + lenWritten
+ " Bytes sent: ");
for (int i = 0; i < lenWritten; i++) {
sb.append("0x" + String.format("%1$02X", data[i]) + " ");
}
logger.info(sb.toString());
}
return lenWritten;
}
/**
* Read data from the device using a interrupt transfer.<br>
*
* @param in_ep_address
* endpoint address to read from
* @param data
* data buffer for the data to be read
* @param size
* the maximum requested data size
* @param timeout
* amount of time in ms the device will try to receive data until
* a timeout exception is thrown
* @param reopenOnTimeout
* if set to true, the device will try to open the connection and
* receive the data again before a timeout exception is thrown
* @return the actual number of bytes read
* @throws USBException
*/
public int readInterrupt(int in_ep_address, byte[] data, int size,
int timeout, boolean reopenOnTimeout) throws USBException {
if (usbDevHandle == 0) {
throw new USBException("invalid device handle");
}
if (data == null) {
throw new USBException("data must not be null");
}
if (size <= 0 || size > data.length) {
throw new ArrayIndexOutOfBoundsException("invalid size: " + size);
}
int lenRead = LibusbJava.usb_interrupt_read(usbDevHandle,
in_ep_address, data, size, timeout);
if (lenRead < 0) {
if (lenRead == LibusbJava.ERROR_TIMEDOUT) {
// try to reopen the device and send the data again
if (reopenOnTimeout) {
logger.info("try to reopen");
reset();
open(dev_configuration, dev_interface, dev_altinterface);
return readInterrupt(in_ep_address, data, size, timeout,
false);
}
throw new USBTimeoutException("LibusbJava.usb_interrupt_read: "
+ LibusbJava.usb_strerror());
}
throw new USBException("LibusbJava.usb_interrupt_read: "
+ LibusbJava.usb_strerror());
}
logger.info("length read: " + lenRead);
if (logger.isLoggable(Level.FINEST)) {
StringBuffer sb = new StringBuffer("interrupt, ep 0x"
+ Integer.toHexString(in_ep_address) + ": " + lenRead
+ " Bytes received: ");
for (int i = 0; i < lenRead; i++) {
sb.append("0x" + String.format("%1$02X", data[i]) + " ");
}
logger.info(sb.toString());
}
return lenRead;
}
/**
* Performs a control request to the default control pipe on a device.<br>
* The parameters mirror the types of the same name in the USB
* specification.
*
* @param requestType
* USB device request type (USB specification 9.3,
* bmRequestType). Use constants from {@link ch.ntb.inf.libusbJava.USB}
* (REQ_TYPE_xxx).
* @param request
* specific request (USB specification 9.4, bRequest). Use
* constants from {@link ch.ntb.inf.libusbJava.USB} (REQ_xxx).
* @param value
* field that varies according to request (USB specification 9.4,
* wValue)
* @param index
* field that varies according to request (USB specification 9.4,
* wIndex)
* @param data
* the send/receive buffer
* @param size
* the buffer size. 0 is a valid value, but there must still be a
* dummy data buffer provided.
* @param timeout
* amount of time in ms the device will try to send/receive data
* until a timeout exception is thrown
* @param reopenOnTimeout
* if set to true, the device will try to open the connection and
* send/receive the data again before a timeout exception is
* thrown
* @return the number of bytes written/read
* @throws USBException
*/
public int controlMsg(int requestType, int request, int value, int index,
byte[] data, int size, int timeout, boolean reopenOnTimeout)
throws USBException {
if (usbDevHandle == 0) {
throw new USBException("invalid device handle");
}
if (data == null) {
throw new USBException("data must not be null");
}
if (size < 0 || size > data.length) {
throw new ArrayIndexOutOfBoundsException("invalid size: " + size);
}
int len = LibusbJava.usb_control_msg(usbDevHandle, requestType,
request, value, index, data, size, timeout);
if (len < 0) {
if (len == LibusbJava.ERROR_TIMEDOUT) {
// try to reopen the device and send the data again
if (reopenOnTimeout) {
logger.info("try to reopen");
reset();
open(dev_configuration, dev_interface, dev_altinterface);
return controlMsg(requestType, request, value, index, data,
size, timeout, false);
}
throw new USBTimeoutException("LibusbJava.controlMsg: "
+ LibusbJava.usb_strerror());
}
throw new USBException("LibusbJava.controlMsg: "
+ LibusbJava.usb_strerror());
}
logger.info("length read/written: " + len);
if (logger.isLoggable(Level.FINEST)) {
StringBuffer sb = new StringBuffer("controlMsg: " + len
+ " Bytes received(written: ");
for (int i = 0; i < len; i++) {
sb.append("0x" + String.format("%1$02X", data[i]) + " ");
}
logger.info(sb.toString());
}
return len;
}
/**
* Claim an interface to send and receive USB data.<br>
*
* @param usb_dev_handle
* the handle of the device <b>(MUST BE VALID)</b>
* @param configuration
* the configuration to use
* @param interface_
* the interface to claim
* @param altinterface
* the alternate interface to use. If no alternate interface must
* be set <i>-1</i> can be used.
* @throws USBException
* throws an USBException if the action fails
*/
private void claim_interface(long usb_dev_handle, int configuration,
int interface_, int altinterface) throws USBException {
if (LibusbJava.usb_set_configuration(usb_dev_handle, configuration) < 0) {
usbDevHandle = 0;
throw new USBException("LibusbJava.usb_set_configuration: "
+ LibusbJava.usb_strerror());
}
if (LibusbJava.usb_claim_interface(usb_dev_handle, interface_) < 0) {
usbDevHandle = 0;
throw new USBException("LibusbJava.usb_claim_interface: "
+ LibusbJava.usb_strerror());
}
if (altinterface >= 0) {
if (LibusbJava.usb_set_altinterface(usb_dev_handle, altinterface) < 0) {
try {
release_interface(usb_dev_handle, interface_);
} catch (USBException e) {
// ignore
}
usbDevHandle = 0;
throw new USBException("LibusbJava.usb_set_altinterface: "
+ LibusbJava.usb_strerror());
}
}
logger.info("interface claimed");
}
/**
* Release a previously claimed interface.<br>
*
* @param dev_handle
* the handle of the device <b>(MUST BE VALID)</b>
* @param interface_
* the interface to claim
* @throws USBException
* throws an USBException if the action fails
*/
private void release_interface(long dev_handle, int interface_)
throws USBException {
if (LibusbJava.usb_release_interface(dev_handle, interface_) < 0) {
usbDevHandle = 0;
throw new USBException("LibusbJava.usb_release_interface: "
+ LibusbJava.usb_strerror());
}
logger.info("interface released");
}
/**
* Returns the product ID of the device.<br>
*
* @return the product ID of the device.
*/
public int getIdProduct() {
return idProduct;
}
/**
* Returns the vendor ID of the device.<br>
*
* @return the vendor ID of the device.
*/
public int getIdVendor() {
return idVendor;
}
/**
* Returns the alternative interface.<br>
* This value is only valid after opening the device.
*
* @return the alternative interface. This value is only valid after opening
* the device.
*/
public int getAltinterface() {
return dev_altinterface;
}
/**
* Returns the current configuration used.<br>
* This value is only valid after opening the device.
*
* @return the current configuration used. This value is only valid after
* opening the device.
*/
public int getConfiguration() {
return dev_configuration;
}
/**
* Returns the current interface.<br>
* This value is only valid after opening the device.
*
* @return the current interface. This value is only valid after opening the
* device.
*/
public int getInterface() {
return dev_interface;
}
/**
* Returns the maximum packet size in bytes which is allowed to be
* transmitted at once.<br>
* The value is determined by reading the endpoint descriptor(s) when
* opening the device. It is invalid before the device is opened! Note that
* if some endpoints use different packet sizes the maximum packet size is
* return. This value may be used to determine if a device is opened in
* fullspeed or highspeed mode.
*
* @return the maximum packet size
*/
public int getMaxPacketSize() {
return maxPacketSize;
}
/**
* Check if the device is open.<br>
* This checks only for a valid device handle. It doesn't check if the
* device is still attached or working.
*
* @return true if the device is open
*/
public boolean isOpen() {
return usbDevHandle != 0;
}
/**
* If enabled, the device is reset when first opened. <br>
* This will only happen once. When the application is started, the device
* state is unknown. If the device is not reset, read or write may result in
* a {@link USBTimeoutException}.<br>
* <br>
* This feature is disabled by default.
*
* @param enable
* true if the device should be reset when first opened
* @param timeout
* the timeout between the reset and the reopening
*/
public void setResetOnFirstOpen(boolean enable, int timeout) {
resetOnFirstOpen = enable;
resetTimeout = timeout;
}
/**
* Returns the optional filename which is set when there are multiple
* devices with the same vendor and product id. See
* {@link USB#getDevice(short, short, String, String)}. Use
* {@link Usb_Device#getFilename()} to read the filename of a device.
*
* @return the filename or null
*/
protected String getFilename() {
return filename;
}
/**
* Returns the optional bus name which is set when there are multiple
* devices with the same vendor and product id. See
* {@link USB#getDevice(short, short, String, String)}. Use
* {@link Usb_Bus#getDirname()} to read the name of a bus.
*
* @return the bus name or null
*/
protected String getBusName() {
return busName;
}
/**
* Returns the Usb_Device instance associated with this device. This value
* is only valid after opening the device.
*
* @return the Usb_Device instance associated with this device.
*/
public Usb_Device getDevice() {
return dev;
}
@Override
public String toString() {
return "idVendor: 0x" + Integer.toHexString(getIdVendor() & 0xffff)
+ ", idProduct: 0x"
+ Integer.toHexString(getIdProduct() & 0xffff) + ", busName: "
+ getBusName() + ", filename: " + getFilename();
}
}

View File

@@ -0,0 +1,378 @@
/*
* Java libusb wrapper
* Copyright (c) 2005-2006 Andreas Schl<68>pfer <spandi at users.sourceforge.net>
*
* http://libusbjava.sourceforge.net
* This library is covered by the LGPL, read LGPL.txt for details.
*/
package ch.ntb.inf.libusbJava;
/**
* This class represents the Java Native Interface to the shared library which
* is (with some exceptions) a one-to-one representation of the libusb API.<br>
* <br>
* <h1>Project Description</h1>
* Java libusb is a Java wrapper for the libusb and libusb-win32 USB library.
*
* <a href="http://libusb.sourceforge.net/">libusb</a> aim is to create a
* library for use by user level applications to access USB devices regardless
* of OS.<br>
* <a href="http://libusb-win32.sourceforge.net/">Libusb-win32</a> is a port of
* the USB library <a href="http://libusb.sourceforge.net/">libusb</a> to the
* Windows operating systems. The library allows user space applications to
* access any USB device on Windows in a generic way without writing any line of
* kernel driver code.<br>
* <br>
* The API description of this class has been copied from the <a
* href="http://libusb.sourceforge.net/documentation.html">libusb documentation</a>
* and adapted where neccessary.<br>
*
*/
public class LibusbJava {
/**
* System error codes.<br>
* This list is not complete! For more error codes see the file 'errorno.h'
* on your system.
*/
public static int ERROR_SUCCESS, ERROR_BAD_FILE_DESCRIPTOR,
ERROR_NO_SUCH_DEVICE_OR_ADDRESS, ERROR_BUSY,
ERROR_INVALID_PARAMETER, ERROR_TIMEDOUT, ERROR_IO_ERROR,
ERROR_NOT_ENOUGH_MEMORY;;
/**
* Sets the debugging level of libusb.<br>
*
* The range is from 0 to 255, where 0 disables debug output and 255 enables
* all output. On application start, debugging is disabled (0).
*
* @param level
* 0 to 255
*/
public static native void usb_set_debug(int level);
// Core
/**
* Just like the name implies, <code>usb_init</code> sets up some internal
* structures. <code>usb_init</code> must be called before any other
* libusb functions.
*/
public static native void usb_init();
/**
* <code>usb_find_busses</code> will find all of the busses on the system.
*
* @return the number of changes since previous call to this function (total
* of new busses and busses removed).
*/
public static native int usb_find_busses();
/**
* <code>usb_find_devices</code> will find all of the devices on each bus.
* This should be called after <code>usb_find_busses</code>.
*
* @return the number of changes since the previous call to this function
* (total of new device and devices removed).
*/
public static native int usb_find_devices();
/**
* <code>usb_get_busses</code> returns a tree of descriptor objects.<br>
* The tree represents the bus structure with devices, configurations,
* interfaces and endpoints. Note that this is only a copy. To refresh the
* information, <code>usb_get_busses()</code> must be called again.<br>
* The name of the objects contained in the tree is starting with
* <code>Usb_</code>.
*
* @return the structure of all busses and devices. <code>Note:</code> The
* java objects are copies of the C structs.
*/
public static native Usb_Bus usb_get_busses();
// Device Operations
/**
* <code>usb_open</code> is to be used to open up a device for use.
* <code>usb_open</code> must be called before attempting to perform any
* operations to the device.
*
* @param dev
* The device to open.
* @return a handle used in future communication with the device. 0 if an
* error has occurred.
*/
public static native long usb_open(Usb_Device dev);
/**
* <code>usb_close</code> closes a device opened with
* <code>usb_open</code>.
*
* @param dev_handle
* The handle to the device.
* @return 0 on success or < 0 on error.
*/
public static native int usb_close(long dev_handle);
/**
* Sets the active configuration of a device
*
* @param dev_handle
* The handle to the device.
* @param configuration
* The value as specified in the descriptor field
* bConfigurationValue.
* @return 0 on success or < 0 on error.
*/
public static native int usb_set_configuration(long dev_handle,
int configuration);
/**
* Sets the active alternate setting of the current interface
*
* @param dev_handle
* The handle to the device.
* @param alternate
* The value as specified in the descriptor field
* bAlternateSetting.
* @return 0 on success or < 0 on error.
*/
public static native int usb_set_altinterface(long dev_handle, int alternate);
/**
* Clears any halt status on an endpoint.
*
* @param dev_handle
* The handle to the device.
* @param ep
* The value specified in the descriptor field bEndpointAddress.
* @return 0 on success or < 0 on error.
*/
public static native int usb_clear_halt(long dev_handle, int ep);
/**
* Resets a device by sending a RESET down the port it is connected to.<br>
* <br>
* <b>Causes re-enumeration:</b> After calling <code>usb_reset</code>,
* the device will need to re-enumerate and thusly, requires you to find the
* new device and open a new handle. The handle used to call
* <code>usb_reset</code> will no longer work.
*
* @param dev_handle
* The handle to the device.
* @return 0 on success or < 0 on error.
*/
public static native int usb_reset(long dev_handle);
/**
* Claim an interface of a device.<br>
* <br>
* <b>Must be called!:</b> <code>usb_claim_interface</code> must be
* called before you perform any operations related to this interface (like
* <code>usb_set_altinterface, usb_bulk_write</code>, etc).
*
* @param dev_handle
* The handle to the device.
* @param interface_
* The value as specified in the descriptor field
* bInterfaceNumber.
* @return 0 on success or < 0 on error.
*/
public static native int usb_claim_interface(long dev_handle, int interface_);
/**
* Releases a previously claimed interface
*
* @param dev_handle
* The handle to the device.
* @param interface_
* The value as specified in the descriptor field
* bInterfaceNumber.
* @return 0 on success or < 0 on error.
*/
public static native int usb_release_interface(long dev_handle,
int interface_);
// Control Transfers
/**
* Performs a control request to the default control pipe on a device. The
* parameters mirror the types of the same name in the USB specification.
*
* @param dev_handle
* The handle to the device.
* @param requesttype
* @param request
* @param value
* @param index
* @param bytes
* @param size
* @param timeout
* @return the number of bytes written/read or < 0 on error.
*/
public static native int usb_control_msg(long dev_handle, int requesttype,
int request, int value, int index, byte[] bytes, int size,
int timeout);
/**
* Retrieves the string descriptor specified by index and langid from a
* device.
*
* @param dev_handle
* The handle to the device.
* @param index
* @param langid
* @return the descriptor String or null
*/
public static native String usb_get_string(long dev_handle, int index,
int langid);
/**
* <code>usb_get_string_simple</code> is a wrapper around
* <code>usb_get_string</code> that retrieves the string description
* specified by index in the first language for the descriptor.
*
* @param dev_handle
* The handle to the device.
* @param index
* @return the descriptor String or null
*/
public static native String usb_get_string_simple(long dev_handle, int index);
/**
* Retrieves a descriptor from the device identified by the type and index
* of the descriptor from the default control pipe.<br>
* <br>
* See {@link #usb_get_descriptor_by_endpoint(long, int, byte, byte, int)}
* for a function that allows the control endpoint to be specified.
*
* @param dev_handle
* The handle to the device.
* @param type
* @param index
* @param size
* number of charactes which will be retrieved (the length of the
* resulting String)
* @return the descriptor String or null
*/
public static native String usb_get_descriptor(long dev_handle, byte type,
byte index, int size);
/**
* Retrieves a descriptor from the device identified by the type and index
* of the descriptor from the control pipe identified by ep.
*
* @param dev_handle
* The handle to the device.
* @param ep
* @param type
* @param index
* @param size
* number of charactes which will be retrieved (the length of the
* resulting String)
* @return the descriptor String or null
*/
public static native String usb_get_descriptor_by_endpoint(long dev_handle,
int ep, byte type, byte index, int size);
// Bulk Transfers
/**
* Performs a bulk write request to the endpoint specified by ep.
*
* @param dev_handle
* The handle to the device.
* @param ep
* @param bytes
* @param size
* @param timeout
* @return the number of bytes written on success or < 0 on error.
*/
public static native int usb_bulk_write(long dev_handle, int ep,
byte[] bytes, int size, int timeout);
/**
* Performs a bulk read request to the endpoint specified by ep.
*
* @param dev_handle
* The handle to the device.
* @param ep
* @param bytes
* @param size
* @param timeout
* @return the number of bytes read on success or < 0 on error.
*/
public static native int usb_bulk_read(long dev_handle, int ep,
byte[] bytes, int size, int timeout);
// Interrupt Transfers
/**
* Performs an interrupt write request to the endpoint specified by ep.
*
* @param dev_handle
* The handle to the device.
* @param ep
* @param bytes
* @param size
* @param timeout
* @return the number of bytes written on success or < 0 on error.
*/
public static native int usb_interrupt_write(long dev_handle, int ep,
byte[] bytes, int size, int timeout);
/**
* Performs a interrupt read request to the endpoint specified by ep.
*
* @param dev_handle
* The handle to the device.
* @param ep
* @param bytes
* @param size
* @param timeout
* @return the number of bytes read on success or < 0 on error.
*/
public static native int usb_interrupt_read(long dev_handle, int ep,
byte[] bytes, int size, int timeout);
/**
* Returns the error string after an error occured.
*
* @return the last error sring.
*/
public static native String usb_strerror();
/** **************************************************************** */
/**
* Maps the Java error code to the system error code.<br>
* <br>
* Note that not all error codes are be mapped by this method. For more
* error codes see the file 'errno.h' on your system.<br>
* <br>
* 1: EBADF: Bad file descriptor.<br>
* 2: ENXIO: No such device or address.<br>
* 3: EBUSY: Device or resource busy.<br>
* 4: EINVAL: Invalid argument.<br>
* 5: ETIMEDOUT: Connection timed out.<br>
* 6: EIO: I/O error.<br>
* 7: ENOMEM: Not enough memory.<br>
*
*
* @return the system error code or 100000 if no mapping has been found.
*/
private static native int usb_error_no(int value);
static {
String os = System.getProperty("os.name");
if (os.contains("Windows")) {
System.loadLibrary("LibusbJava");
} else {
System.loadLibrary("usbJava");
}
// define the error codes
ERROR_SUCCESS = 0;
ERROR_BAD_FILE_DESCRIPTOR = -usb_error_no(1);
ERROR_NO_SUCH_DEVICE_OR_ADDRESS = -usb_error_no(2);
ERROR_BUSY = -usb_error_no(3);
ERROR_INVALID_PARAMETER = -usb_error_no(4);
ERROR_TIMEDOUT = -usb_error_no(5);
ERROR_IO_ERROR = -usb_error_no(6);
ERROR_NOT_ENOUGH_MEMORY = -usb_error_no(7);
}
}

View File

@@ -0,0 +1,318 @@
/*
* Java libusb wrapper
* Copyright (c) 2005-2006 Andreas Schl<68>pfer <spandi at users.sourceforge.net>
*
* http://libusbjava.sourceforge.net
* This library is covered by the LGPL, read LGPL.txt for details.
*/
package ch.ntb.inf.libusbJava;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.logging.Logger;
import ch.ntb.inf.libusbJava.logger.LogUtil;
/**
* This class manages all USB devices and defines some USB specific constants.<br>
*
*/
public class USB {
// Standard requests (USB spec 9.4)
/**
* This request returns status for the specified recipient (USB spec 9.4.5).
*
* @see ch.ntb.inf.libusbJava.Device#controlMsg(int, int, int, int, byte[], int, int,
* boolean)
*/
public static final int REQ_GET_STATUS = 0x00;
/**
* This request is used to clear or disable a specific feature (USB spec
* 9.4.1).
*
* @see ch.ntb.inf.libusbJava.Device#controlMsg(int, int, int, int, byte[], int, int,
* boolean)
*/
public static final int REQ_CLEAR_FEATURE = 0x01;
// 0x02 is reserved
/**
* This request is used to set or enable a specific feature (USB spec
* 9.4.9).
*
* @see ch.ntb.inf.libusbJava.Device#controlMsg(int, int, int, int, byte[], int, int,
* boolean)
*/
public static final int REQ_SET_FEATURE = 0x03;
// 0x04 is reserved
/**
* This request sets the device address for all future device accesses (USB
* spec 9.4.6).
*
* @see ch.ntb.inf.libusbJava.Device#controlMsg(int, int, int, int, byte[], int, int,
* boolean)
*/
public static final int REQ_SET_ADDRESS = 0x05;
/**
* This request returns the specified descriptor if the descriptor exists
* (USB spec 9.4.3).
*
* @see ch.ntb.inf.libusbJava.Device#controlMsg(int, int, int, int, byte[], int, int,
* boolean)
*/
public static final int REQ_GET_DESCRIPTOR = 0x06;
/**
* This request is optional and may be used to update existing descriptors
* or new descriptors may be added (USB spec 9.4.8).
*
* @see ch.ntb.inf.libusbJava.Device#controlMsg(int, int, int, int, byte[], int, int,
* boolean)
*/
public static final int REQ_SET_DESCRIPTOR = 0x07;
/**
* This request returns the current device configuration value (USB spec
* 9.4.2).
*
* @see ch.ntb.inf.libusbJava.Device#controlMsg(int, int, int, int, byte[], int, int,
* boolean)
*/
public static final int REQ_GET_CONFIGURATION = 0x08;
/**
* This request sets the device configuration (USB spec 9.4.7).
*
* @see ch.ntb.inf.libusbJava.Device#controlMsg(int, int, int, int, byte[], int, int,
* boolean)
*/
public static final int REQ_SET_CONFIGURATION = 0x09;
/**
* This request returns the selected alternate setting for the specified
* interface (USB spec 9.4.4).
*
* @see ch.ntb.inf.libusbJava.Device#controlMsg(int, int, int, int, byte[], int, int,
* boolean)
*/
public static final int REQ_GET_INTERFACE = 0x0A;
/**
* This request allows the host to select an alternate setting for the
* specified interface (USB spec 9.4.10).
*
* @see ch.ntb.inf.libusbJava.Device#controlMsg(int, int, int, int, byte[], int, int,
* boolean)
*/
public static final int REQ_SET_INTERFACE = 0x0B;
/**
* This request is used to set and then report an endpoint<6E>s synchronization
* frame (USB spec 9.4.11).
*
* @see ch.ntb.inf.libusbJava.Device#controlMsg(int, int, int, int, byte[], int, int,
* boolean)
*/
public static final int REQ_SYNCH_FRAME = 0x0C;
// data transfer direction (USB spec 9.3)
/**
* Identifies the direction of data transfer in the second phase of the
* control transfer.<br>
* The state of the Direction bit is ignored if the wLength field is zero,
* signifying there is no Data stage.<br>
* Specifies bit 7 of bmRequestType.
*
* @see ch.ntb.inf.libusbJava.Device#controlMsg(int, int, int, int, byte[], int, int,
* boolean)
*/
public static final int REQ_TYPE_DIR_HOST_TO_DEVICE = (0x00 << 7),
REQ_TYPE_DIR_DEVICE_TO_HOST = (0x01 << 7);
// request types (USB spec 9.3)
/**
* Specifies the type of the request.<br>
* Specifies bits 6..5 of bmRequestType.
*
* @see ch.ntb.inf.libusbJava.Device#controlMsg(int, int, int, int, byte[], int, int,
* boolean)
*/
public static final int REQ_TYPE_TYPE_STANDARD = (0x00 << 5),
REQ_TYPE_TYPE_CLASS = (0x01 << 5),
REQ_TYPE_TYPE_VENDOR = (0x02 << 5),
REQ_TYPE_TYPE_RESERVED = (0x03 << 5);
// request recipient (USB spec 9.3)
/**
* Specifies the intended recipient of the request.<br>
* Requests may be directed to the device, an interface on the device, or a
* specific endpoint on a device. When an interface or endpoint is
* specified, the wIndex field identifies the interface or endpoint.<br>
* Specifies bits 4..0 of bmRequestType.
*
* @see ch.ntb.inf.libusbJava.Device#controlMsg(int, int, int, int, byte[], int, int,
* boolean)
*/
public static final int REQ_TYPE_RECIP_DEVICE = 0x00,
REQ_TYPE_RECIP_INTERFACE = 0x01, REQ_TYPE_RECIP_ENDPOINT = 0x02,
REQ_TYPE_RECIP_OTHER = 0x03;
/**
* The maximum packet size of a bulk transfer when operating in highspeed
* (480 MB/s) mode.
*/
public static int HIGHSPEED_MAX_BULK_PACKET_SIZE = 512;
/**
* The maximum packet size of a bulk transfer when operating in fullspeed
* (12 MB/s) mode.
*/
public static int FULLSPEED_MAX_BULK_PACKET_SIZE = 64;
private static final Logger logger = LogUtil.getLogger("ch.ntb.inf.libusbJava");
private static LinkedList<Device> devices = new LinkedList<Device>();
private static boolean initUSBDone = false;
/**
* Create a new device an register it in a device list. If the device is
* already registered, a reference to it will be returned.<br>
* After resetting or re-attaching a device the busName and filename may
* change. You can unregister the current device instance (see
* {@link #unregisterDevice(Device)}) and get a new instance with the
* updated bus and filename.
*
* @param idVendor
* the vendor id of the USB device
* @param idProduct
* the product id of the USB device
* @param busName
* optional name of the bus which can be used to distinguish
* multiple devices with the same vendor and product id.<br>
* see {@link Usb_Bus#getDirname()}
* @param filename
* optional filename which can be used to distinguish multiple
* devices with the same vendor and product id.<br>
* see {@link Usb_Device#getFilename()}
* @return a newly created device or an already registered device
*/
public static Device getDevice(short idVendor, short idProduct,
String busName, String filename) {
// check if this device is already registered
Device dev = getRegisteredDevice(idVendor, idProduct, busName, filename);
if (dev != null) {
logger.info("return already registered device: " + dev);
return dev;
}
dev = new Device(idVendor, idProduct, busName, filename);
logger.info("create new device: " + dev);
devices.add(dev);
return dev;
}
/**
* See {@link #getDevice(short, short, String, String)}. The parameter
* <code>filename</code> and <code>busName</code>is set to null.
*
* @param idVendor
* @param idProduct
* @return a newly created device or an already registered device
*/
public static Device getDevice(short idVendor, short idProduct) {
return getDevice(idVendor, idProduct, null, null);
}
/**
* Unregister a registered device.
*
* @param dev
* the device to unregister
* @return true if the device has been removed, else false
*/
public static boolean unregisterDevice(Device dev) {
return devices.remove(dev);
}
/**
* Get an already registered device or null if the device does not exist.<br>
* To uniquely identify a device bus and filename should be set. If only one
* of those is set the first device matching the criteria is returned.
*
* @param idVendor
* the vendor id of the USB device
* @param idProduct
* the product id of the USB device
* @param busName
* the name of the bus which can be used to distinguish multiple
* devices with the same vendor and product id.<br>
* see {@link Usb_Bus#getDirname()}
* @param filename
* an optional filename which can be used to distinguish multiple
* devices with the same vendor and product id. see
* {@link Usb_Device#getFilename()}
*
* @return the device or null
*/
private static Device getRegisteredDevice(short idVendor, short idProduct,
String busName, String filename) {
for (Iterator<Device> iter = devices.iterator(); iter.hasNext();) {
Device dev = iter.next();
// bus and filename
if (busName != null && filename != null) {
if (busName.compareTo(dev.getBusName() == null ? "" : dev
.getBusName()) == 0
&& filename.compareTo(dev.getFilename() == null ? ""
: dev.getFilename()) == 0
&& dev.getIdVendor() == idVendor
&& dev.getIdProduct() == idProduct) {
return dev;
}
} else if (filename != null) {
if (filename.compareTo(dev.getFilename() == null ? "" : dev
.getFilename()) == 0
&& dev.getIdVendor() == idVendor
&& dev.getIdProduct() == idProduct) {
return dev;
}
} else if (busName != null) {
if (busName.compareTo(dev.getBusName() == null ? "" : dev
.getBusName()) == 0
&& dev.getIdVendor() == idVendor
&& dev.getIdProduct() == idProduct) {
return dev;
}
} else if (dev.getIdVendor() == idVendor
&& dev.getIdProduct() == idProduct) {
return dev;
}
}
return null;
}
/**
* Returns the root {@link Usb_Bus} element.
*
* @return the root {@link Usb_Bus} element
* @throws USBException
*/
public static Usb_Bus getBus() throws USBException {
if (!initUSBDone) {
init();
}
LibusbJava.usb_find_busses();
LibusbJava.usb_find_devices();
Usb_Bus bus = LibusbJava.usb_get_busses();
if (bus == null) {
throw new USBException("LibusbJava.usb_get_busses(): "
+ LibusbJava.usb_strerror());
}
return bus;
}
/**
* Explicitly calls {@link LibusbJava#usb_init()}. Note that you don't need
* to call this procedure as it is called implicitly when creating a new
* device with {@link USB#getDevice(short, short, String, String)}.
*/
public static void init() {
LibusbJava.usb_init();
initUSBDone = true;
}
}

View File

@@ -0,0 +1,23 @@
/*
* Java libusb wrapper
* Copyright (c) 2005-2006 Andreas Schl<68>pfer <spandi at users.sourceforge.net>
*
* http://libusbjava.sourceforge.net
* This library is covered by the LGPL, read LGPL.txt for details.
*/
package ch.ntb.inf.libusbJava;
import java.io.IOException;
public class USBException extends IOException {
public USBException(String string) {
super(string);
}
/**
*
*/
private static final long serialVersionUID = 1690857437804284710L;
}

View File

@@ -0,0 +1,21 @@
/*
* Java libusb wrapper
* Copyright (c) 2005-2006 Andreas Schl<68>pfer <spandi at users.sourceforge.net>
*
* http://libusbjava.sourceforge.net
* This library is covered by the LGPL, read LGPL.txt for details.
*/
package ch.ntb.inf.libusbJava;
public class USBTimeoutException extends USBException {
public USBTimeoutException(String string) {
super(string);
}
/**
*
*/
private static final long serialVersionUID = -1065328371159778249L;
}

View File

@@ -0,0 +1,86 @@
/*
* Java libusb wrapper
* Copyright (c) 2005-2006 Andreas Schl<68>pfer <spandi at users.sourceforge.net>
*
* http://libusbjava.sourceforge.net
* This library is covered by the LGPL, read LGPL.txt for details.
*/
package ch.ntb.inf.libusbJava;
/**
* Represents an USB bus.<br>
* This is the root class for the representation of the libusb USB structure.
* Zero or more devices may be connected to an USB bus.
*
*/
public class Usb_Bus {
private Usb_Bus next, prev;
private String dirname;
private Usb_Device devices;
private long location;
private Usb_Device root_dev;
/**
* Get the first device ojects of the devices linked list.<br>
*
* @return the first device ojects of the devices linked list or null
*/
public Usb_Device getDevices() {
return devices;
}
/**
* Returns the systems String representation of the bus.<br>
*
* @return the systems String representation of the bus
*/
public String getDirname() {
return dirname;
}
/**
* Returns the next bus object.<br>
*
* @return Returns the next bus object or null
*/
public Usb_Bus getNext() {
return next;
}
/**
* Returns the previous bus object.<br>
*
* @return Returns the previous bus object or null
*/
public Usb_Bus getPrev() {
return prev;
}
/**
* Get the root device of this bus.<br>
*
* @return the root device oject or null
*/
public Usb_Device getRootDev() {
return root_dev;
}
/**
* Returns the location in the USB bus linked list.<br>
*
* @return the location in the USB bus linked list
*/
public long getLocation() {
return location;
}
@Override
public String toString() {
return "Usb_Bus " + dirname;
}
}

View File

@@ -0,0 +1,139 @@
/*
* Java libusb wrapper
* Copyright (c) 2005-2006 Andreas Schl<68>pfer <spandi at users.sourceforge.net>
*
* http://libusbjava.sourceforge.net
* This library is covered by the LGPL, read LGPL.txt for details.
*/
package ch.ntb.inf.libusbJava;
/**
* Represents the descriptor of a USB configuration.<br>
* A USB device can have several different configuration.<br>
* <br>
* The length of the configuration descriptor is
* {@link ch.ntb.inf.libusbJava.Usb_Descriptor#USB_DT_CONFIG_SIZE} and the type is
* {@link ch.ntb.inf.libusbJava.Usb_Descriptor#USB_DT_CONFIG}.
*
*/
public class Usb_Config_Descriptor extends Usb_Descriptor {
/**
* Maximum number of configurations per device
*/
public static final int USB_MAXCONFIG = 8;
private short wTotalLength;
private byte bNumInterfaces;
private byte bConfigurationValue;
private byte iConfiguration;
private byte bmAttributes;
private byte MaxPower;
private Usb_Interface[] interface_;
private byte[] extra; /* Extra descriptors */
private int extralen;
/**
* Returns the value to use as an argument to select this configuration ({@link LibusbJava#usb_set_configuration(long, int)}).
*
* @return the value to use as an argument to select this configuration
*/
public byte getBConfigurationValue() {
return bConfigurationValue;
}
/**
* Returns the power parameters for this configuration.<br>
* <br>
* Bit 7: Reserved, set to 1 (USB 1.0 Bus Powered)<br>
* Bit 6: Self Powered<br>
* Bit 5: Remote Wakeup<br>
* Bit 4..0: Reserved, set to 0
*
* @return the power parameters for this configuration
*/
public byte getBmAttributes() {
return bmAttributes;
}
/**
* Returns the number of interfaces.<br>
*
* @return the number of interfaces
*/
public byte getBNumInterfaces() {
return bNumInterfaces;
}
/**
* Returns the data of extra descriptor(s) if available.<br>
*
* @return null or a byte array with the extra descriptor data
*/
public byte[] getExtra() {
return extra;
}
/**
* Returns the number of bytes of the extra descriptor.<br>
*
* @return the number of bytes of the extra descriptor
*/
public int getExtralen() {
return extralen;
}
/**
* Returns the index of the String descriptor describing this configuration.<br>
*
* @return the index of the String descriptor
*/
public byte getIConfiguration() {
return iConfiguration;
}
/**
* Returns the USB interface descriptors.<br>
*
* @return the USB interface descriptors
*/
public Usb_Interface[] getInterface() {
return interface_;
}
/**
* Returns the maximum power consumption in 2mA units.<br>
*
* @return the maximum power consumption in 2mA units
*/
public byte getMaxPower() {
return MaxPower;
}
/**
* Returns the total length in bytes of all descriptors.<br>
* When the configuration descriptor is read, it returns the entire
* configuration hierarchy which includes all related interface and endpoint
* descriptors. The <code>wTotalLength</code> field reflects the number of
* bytes in the hierarchy.
*
* @return the total length in bytes of all descriptors
*/
public short getWTotalLength() {
return wTotalLength;
}
@Override
public String toString() {
return "Usb_Config_Descriptor bNumInterfaces: 0x"
+ Integer.toHexString(bNumInterfaces);
}
}

View File

@@ -0,0 +1,59 @@
/*
* Java libusb wrapper
* Copyright (c) 2005-2006 Andreas Schl<68>pfer <spandi at users.sourceforge.net>
*
* http://libusbjava.sourceforge.net
* This library is covered by the LGPL, read LGPL.txt for details.
*/
package ch.ntb.inf.libusbJava;
/**
* Common USB descriptor values.<br>
*
*/
public class Usb_Descriptor {
/**
* Descriptor types ({@link #bDescriptorType}).
*/
public static final int USB_DT_DEVICE = 0x01, USB_DT_CONFIG = 0x02,
USB_DT_STRING = 0x03, USB_DT_INTERFACE = 0x04,
USB_DT_ENDPOINT = 0x05;
/**
* Descriptor types ({@link #bDescriptorType}).
*/
public static final int USB_DT_HID = 0x21, USB_DT_REPORT = 0x22,
USB_DT_PHYSICAL = 0x23, USB_DT_HUB = 0x29;
/**
* Descriptor sizes per descriptor type ({@link #bLength}).
*/
public static final int USB_DT_DEVICE_SIZE = 18, USB_DT_CONFIG_SIZE = 9,
USB_DT_INTERFACE_SIZE = 9, USB_DT_ENDPOINT_SIZE = 7,
USB_DT_ENDPOINT_AUDIO_SIZE = 9 /* Audio extension */,
USB_DT_HUB_NONVAR_SIZE = 7;
private byte bLength;
private byte bDescriptorType;
/**
* Get the type of this descriptor.<br>
*
* @return the type of this descriptor
*/
public byte getBDescriptorType() {
return bDescriptorType;
}
/**
* Get the size of this descriptor in bytes.<br>
*
* @return the size of this descriptor in bytes
*/
public byte getBLength() {
return bLength;
}
}

View File

@@ -0,0 +1,125 @@
/*
* Java libusb wrapper
* Copyright (c) 2005-2006 Andreas Schl<68>pfer <spandi at users.sourceforge.net>
*
* http://libusbjava.sourceforge.net
* This library is covered by the LGPL, read LGPL.txt for details.
*/
package ch.ntb.inf.libusbJava;
/**
* Represents an USB device.<br>
* An USB device has one device descriptor and it may have multiple
* configuration descriptors.
*
*/
public class Usb_Device {
private Usb_Device next, prev;
private String filename;
private Usb_Bus bus;
private Usb_Device_Descriptor descriptor;
private Usb_Config_Descriptor[] config;
private byte devnum;
private byte num_children;
private Usb_Device children;
/**
* The address of the device structure to be passed to usb_open. This value
* is used only internally so we don't use getter or setter methods.
*/
public long devStructAddr;
/**
* Returns the reference to the bus to which this device is connected.<br>
*
* @return the reference to the bus to which this device is connected
*/
public Usb_Bus getBus() {
return bus;
}
/**
* Returns a reference to the first child.<br>
*
* @return a reference to the first child
*/
public Usb_Device getChildren() {
return children;
}
/**
* Returns the USB config descriptors.<br>
*
* @return the USB config descriptors
*/
public Usb_Config_Descriptor[] getConfig() {
return config;
}
/**
* Returns the USB device descriptor.<br>
*
* @return the USB device descriptor
*/
public Usb_Device_Descriptor getDescriptor() {
return descriptor;
}
/**
* Returns the number assigned to this device.<br>
*
* @return the number assigned to this device
*/
public byte getDevnum() {
return devnum;
}
/**
* Returns the systems String representation.<br>
*
* @return the systems String representation
*/
public String getFilename() {
return filename;
}
/**
* Returns the pointer to the next device.<br>
*
* @return the pointer to the next device or null
*/
public Usb_Device getNext() {
return next;
}
/**
* Returns the number of children of this device.<br>
*
* @return the number of children of this device
*/
public byte getNumChildren() {
return num_children;
}
/**
* Returns the pointer to the previous device.<br>
*
* @return the pointer to the previous device or null
*/
public Usb_Device getPrev() {
return prev;
}
@Override
public String toString() {
return "Usb_Device " + filename;
}
}

View File

@@ -0,0 +1,190 @@
/*
* Java libusb wrapper
* Copyright (c) 2005-2006 Andreas Schl<68>pfer <spandi at users.sourceforge.net>
*
* http://libusbjava.sourceforge.net
* This library is covered by the LGPL, read LGPL.txt for details.
*/
package ch.ntb.inf.libusbJava;
/**
* Represents the descriptor of a USB device.<br>
* A USB device can only have one device descriptor. It specifies some basic,
* yet important information about the device.<br>
* <br>
* The length of the device descriptor is
* {@link ch.ntb.inf.libusbJava.Usb_Descriptor#USB_DT_DEVICE_SIZE} and the type is
* {@link ch.ntb.inf.libusbJava.Usb_Descriptor#USB_DT_DEVICE}.
*
*/
public class Usb_Device_Descriptor extends Usb_Descriptor {
/**
* Device and/or interface class codes.
*/
public static final int USB_CLASS_PER_INTERFACE = 0, USB_CLASS_AUDIO = 1,
USB_CLASS_COMM = 2, USB_CLASS_HID = 3, USB_CLASS_PRINTER = 7,
USB_CLASS_MASS_STORAGE = 8, USB_CLASS_HUB = 9, USB_CLASS_DATA = 10,
USB_CLASS_VENDOR_SPEC = 0xff;
private short bcdUSB;
private byte bDeviceClass;
private byte bDeviceSubClass;
private byte bDeviceProtocol;
private byte bMaxPacketSize0;
private short idVendor;
private short idProduct;
private short bcdDevice;
private byte iManufacturer;
private byte iProduct;
private byte iSerialNumber;
private byte bNumConfigurations;
/**
* Returns the device release number.<br>
* Assigned by the manufacturer of the device.
*
* @return the device release number
*/
public short getBcdDevice() {
return bcdDevice;
}
/**
* Returns the USB specification number to which the device complies to.<br>
* This field reports the highest version of USB the device supports. The
* value is in binary coded decimal with a format of 0xJJMN where JJ is the
* major version number, M is the minor version number and N is the sub
* minor version number.<br>
* Examples: USB 2.0 is reported as 0x0200, USB 1.1 as 0x0110 and USB 1.0 as
* 0x100
*
* @return the USB specification number to which the device complies to
*/
public short getBcdUSB() {
return bcdUSB;
}
/**
* Returns the class code (Assigned by <a
* href="http://www.usb.org">www.usb.org</a>)<br>
* If equal to zero, each interface specifies it's own class code. If equal
* to 0xFF, the class code is vendor specified. Otherwise the field is a
* valid class code.
*
* @return the class code
*/
public byte getBDeviceClass() {
return bDeviceClass;
}
/**
* Returns the protocol code (Assigned by <a
* href="http://www.usb.org">www.usb.org</a>)<br>
*
* @return the protocol code
*/
public byte getBDeviceProtocol() {
return bDeviceProtocol;
}
/**
* Returns the subclass code (Assigned by <a
* href="http://www.usb.org">www.usb.org</a>)<br>
*
* @return the subclass code
*/
public byte getBDeviceSubClass() {
return bDeviceSubClass;
}
/**
* Returns the maximum packet size for endpoint zero.<br>
* Valid sizes are 8, 16, 32, 64.
*
* @return the maximum packet size for endpoint zero
*/
public byte getBMaxPacketSize0() {
return bMaxPacketSize0;
}
/**
* Returns the number of possible configurations supported at its current
* speed.<br>
*
* @return the number of possible configurations supported at its current
* speed
*/
public byte getBNumConfigurations() {
return bNumConfigurations;
}
/**
* Returns the product ID (Assigned by <a
* href="http://www.usb.org">www.usb.org</a>)<br>
*
* @return the product ID
*/
public short getIdProduct() {
return idProduct;
}
/**
* Returns the Vendor ID (Assigned by <a
* href="http://www.usb.org">www.usb.org</a>)<br>
*
* @return the Vendor ID
*/
public short getIdVendor() {
return idVendor;
}
/**
* Returns the index of the manufacturer string descriptor.<br>
* If this value is 0, no string descriptor is used.
*
* @return the index of the manufacturer string descriptor
*/
public byte getIManufacturer() {
return iManufacturer;
}
/**
* Returns the index of the product string descriptor.<br>
* If this value is 0, no string descriptor is used.
*
* @return the index of the product string descriptor
*/
public byte getIProduct() {
return iProduct;
}
/**
* Returns the index of serial number string descriptor.<br>
* If this value is 0, no string descriptor is used.
*
* @return the index of serial number string descriptor
*/
public byte getISerialNumber() {
return iSerialNumber;
}
@Override
public String toString() {
StringBuffer sb = new StringBuffer();
sb.append("Usb_Device_Descriptor idVendor: 0x"
+ Integer.toHexString(idVendor & 0xFFFF) + ", idProduct: 0x"
+ Integer.toHexString(idProduct & 0xFFFF));
return sb.toString();
}
}

View File

@@ -0,0 +1,158 @@
/*
* Java libusb wrapper
* Copyright (c) 2005-2006 Andreas Schl<68>pfer <spandi at users.sourceforge.net>
*
* http://libusbjava.sourceforge.net
* This library is covered by the LGPL, read LGPL.txt for details.
*/
package ch.ntb.inf.libusbJava;
/**
* Represents the descriptor of an USB endpoint.<br>
* Endpoint descriptors are used to describe endpoints other than endpoint zero.
* Endpoint zero is always assumed to be a control endpoint and is configured
* before any descriptors are even requested. The host will use the information
* returned from these descriptors to determine the bandwidth requirements of
* the bus.<br>
* <br>
* The length of the configuration descriptor is
* {@link ch.ntb.inf.libusbJava.Usb_Descriptor#USB_DT_ENDPOINT_SIZE} and the type is
* {@link ch.ntb.inf.libusbJava.Usb_Descriptor#USB_DT_ENDPOINT}.
*
*/
public class Usb_Endpoint_Descriptor extends Usb_Descriptor {
/**
* Maximum number of endpoints
*/
public static final int USB_MAXENDPOINTS = 32;
/**
* Endpoint address mask (in bEndpointAddress).
*/
public static final int USB_ENDPOINT_ADDRESS_MASK = 0x0f,
USB_ENDPOINT_DIR_MASK = 0x80;
/**
* Endpoint type mask (in bmAttributes).
*/
public static final int USB_ENDPOINT_TYPE_MASK = 0x03;
/**
* Possible endpoint types (in bmAttributes).
*/
public static final int USB_ENDPOINT_TYPE_CONTROL = 0,
USB_ENDPOINT_TYPE_ISOCHRONOUS = 1, USB_ENDPOINT_TYPE_BULK = 2,
USB_ENDPOINT_TYPE_INTERRUPT = 3;
private byte bEndpointAddress;
private byte bmAttributes;
private short wMaxPacketSize;
private byte bInterval;
private byte bRefresh;
private byte bSynchAddress;
private byte[] extra; /* Extra descriptors */
private int extralen;
/**
* Returns the endpoint address.<br>
* <br>
* Bits 3..0: Endpoint number <br>
* Bits 6..4: Reserved. Set to zero <br>
* Bit 7: Direction (host to device). 0 = OUT (send data from host to
* device), 1 = IN (host receives data from device). Note: these values are
* ignored for control endpoints.<br>
*
* @return the endpoint address
*/
public byte getBEndpointAddress() {
return bEndpointAddress;
}
/**
* Returns the intervall for polling endpoint data transfers.<br>
* Value in frame counts. Ignored for Bulk & Control eEndpoints. Isochronous
* endpoints must equal 1 and field may range from 1 to 255 for interrupt
* endpoints.
*
* @return the intervall for polling endpoint data transfers
*/
public byte getBInterval() {
return bInterval;
}
/**
* Returns the attributes of this endpoint.<br>
*
* Bits 1..0: Transfer Type (see <i>USB_ENDPOINT_TYPE_XXX</i>).<br>
* Bits 7..2: Reserved.<br>
*
* <pre>
* If isochronous endpoint:
* Bits 3..2: Synchronisation type
* 00 = No synchronisation
* 01 = Asynchronous
* 10 = Adaptive
* 11 = Synchronous
* Bits 5..4: Usage Type
* 00 = Data endpoint
* 01 = Feedback endpoint
* 10 = Explicit feedback data endpoint
* 11 = Reserved
* </pre>
*
* @return the attributes of this endpoint
*/
public byte getBmAttributes() {
return bmAttributes;
}
public byte getBRefresh() {
return bRefresh;
}
public byte getBSynchAddress() {
return bSynchAddress;
}
/**
* Returns the data of extra descriptor(s) if available.<br>
*
* @return null or a byte array with the extra descriptor data
*/
public byte[] getExtra() {
return extra;
}
/**
* Returns the number of bytes of the extra descriptor.<br>
*
* @return the number of bytes of the extra descriptor
*/
public int getExtralen() {
return extralen;
}
/**
* Returns the maximum packet size of this endpoint is capable of sending or
* receiving.<br>
*
* @return the maximum packet size
*/
public short getWMaxPacketSize() {
return wMaxPacketSize;
}
@Override
public String toString() {
return "Usb_Endpoint_Descriptor bEndpointAddress: 0x"
+ Integer.toHexString(bEndpointAddress & 0xFF);
}
}

View File

@@ -0,0 +1,50 @@
/*
* Java libusb wrapper
* Copyright (c) 2005-2006 Andreas Schl<68>pfer <spandi at users.sourceforge.net>
*
* http://libusbjava.sourceforge.net
* This library is covered by the LGPL, read LGPL.txt for details.
*/
package ch.ntb.inf.libusbJava;
/**
* Represents an USB interface.<br>
* An interface is a group of alternate settings of a configuration.<br>
*
*/
public class Usb_Interface {
/**
* Maximal number of alternate settings
*/
public static final int USB_MAXALTSETTING = 128; /* Hard limit */
private Usb_Interface_Descriptor[] altsetting;
private int num_altsetting;
@Override
public String toString() {
return "Usb_Interface num_altsetting: 0x"
+ Integer.toHexString(num_altsetting);
}
/**
* Retuns an array of interface descriptors.<br>
*
* @return an array of interface descriptors
*/
public Usb_Interface_Descriptor[] getAltsetting() {
return altsetting;
}
/**
* Returns the number of alternate settings.<br>
*
* @return the number of alternate settings
*/
public int getNumAltsetting() {
return num_altsetting;
}
}

View File

@@ -0,0 +1,145 @@
/*
* Java libusb wrapper
* Copyright (c) 2005-2006 Andreas Schl<68>pfer <spandi at users.sourceforge.net>
*
* http://libusbjava.sourceforge.net
* This library is covered by the LGPL, read LGPL.txt for details.
*/
package ch.ntb.inf.libusbJava;
/**
* Represents the descriptor of a USB interface.<br>
* The interface descriptor could be seen as a header or grouping of the
* endpoints into a functional group performing a single feature of the device.<br>
* <br>
* 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}.
*
*/
public class Usb_Interface_Descriptor extends Usb_Descriptor {
/**
* Maximum number of interfaces
*/
public static final int USB_MAXINTERFACES = 32;
private byte bInterfaceNumber;
private byte bAlternateSetting;
private byte bNumEndpoints;
private byte bInterfaceClass;
private byte bInterfaceSubClass;
private byte bInterfaceProtocol;
private byte iInterface;
private Usb_Endpoint_Descriptor[] endpoint;
private byte[] extra; /* Extra descriptors */
private int extralen;
@Override
public String toString() {
return "Usb_Interface_Descriptor bNumEndpoints: 0x"
+ Integer.toHexString(bNumEndpoints);
}
/**
* Returns the value used to select the alternate setting ({@link LibusbJava#usb_set_altinterface(long, int)}).<br>
*
* @return the alternate setting
*/
public byte getBAlternateSetting() {
return bAlternateSetting;
}
/**
* Returns the class code (Assigned by <a
* href="http://www.usb.org">www.usb.org</a>).<br>
*
* @return the class code
*/
public byte getBInterfaceClass() {
return bInterfaceClass;
}
/**
* Returns the number (identifier) of this interface.<br>
*
* @return the number (identifier) of this interface
*/
public byte getBInterfaceNumber() {
return bInterfaceNumber;
}
/**
* Returns the protocol code (Assigned by <a
* href="http://www.usb.org">www.usb.org</a>).<br>
*
* @return the protocol code
*/
public byte getBInterfaceProtocol() {
return bInterfaceProtocol;
}
/**
* Returns the subclass code (Assigned by <a
* href="http://www.usb.org">www.usb.org</a>).<br>
*
* @return the subclass code
*/
public byte getBInterfaceSubClass() {
return bInterfaceSubClass;
}
/**
* Returns the number of endpoints used for this interface.<br>
*
* @return the number of endpoints used for this interface
*/
public byte getBNumEndpoints() {
return bNumEndpoints;
}
/**
* Returns an array of endpoint descriptors.<br>
*
* @return an array of endpoint descriptors
*/
public Usb_Endpoint_Descriptor[] getEndpoint() {
return endpoint;
}
/**
* Returns the data of extra descriptor(s) if available.<br>
*
* @return null or a byte array with the extra descriptor data
*/
public byte[] getExtra() {
return extra;
}
/**
* Returns the number of bytes of the extra descriptor.<br>
*
* @return the number of bytes of the extra descriptor
*/
public int getExtralen() {
return extralen;
}
/**
* Returns the index of the String descriptor describing this interface.<br>
*
* @return the index of the String descriptor
*/
public byte getIInterface() {
return iInterface;
}
}

View File

@@ -0,0 +1,60 @@
/*
* Java libusb wrapper
* Copyright (c) 2005-2006 Andreas Schl<68>pfer <spandi at users.sourceforge.net>
*
* http://libusbjava.sourceforge.net
* This library is covered by the LGPL, read LGPL.txt for details.
*/
package ch.ntb.inf.libusbJava;
import java.io.PrintStream;
public class Utils {
public static void logBus(Usb_Bus bus) {
logBus(bus, System.out);
}
public static void logBus(Usb_Bus bus, PrintStream out) {
Usb_Bus usb_Bus = bus;
while (usb_Bus != null) {
out.println(usb_Bus.toString());
Usb_Device dev = usb_Bus.getDevices();
while (dev != null) {
out.println("\t" + dev.toString());
// Usb_Device_Descriptor
Usb_Device_Descriptor defDesc = dev.getDescriptor();
out.println("\t\t" + defDesc.toString());
// Usb_Config_Descriptor
Usb_Config_Descriptor[] confDesc = dev.getConfig();
for (int i = 0; i < confDesc.length; i++) {
out.println("\t\t" + confDesc[i].toString());
Usb_Interface[] int_ = confDesc[i].getInterface();
if (int_ != null) {
for (int j = 0; j < int_.length; j++) {
out.println("\t\t\t" + int_[j].toString());
Usb_Interface_Descriptor[] intDesc = int_[j]
.getAltsetting();
if (intDesc != null) {
for (int k = 0; k < intDesc.length; k++) {
out.println("\t\t\t\t"
+ intDesc[k].toString());
Usb_Endpoint_Descriptor[] epDesc = intDesc[k]
.getEndpoint();
if (epDesc != null) {
for (int e = 0; e < epDesc.length; e++) {
out.println("\t\t\t\t\t"
+ epDesc[e].toString());
}
}
}
}
}
}
}
dev = dev.getNext();
}
usb_Bus = usb_Bus.getNext();
}
}
}

View File

@@ -0,0 +1,135 @@
/*
* Java libusb wrapper
* Copyright (c) 2005-2006 Andreas Schl<68>pfer <spandi at users.sourceforge.net>
*
* http://libusbjava.sourceforge.net
* This library is covered by the LGPL, read LGPL.txt for details.
*/
package ch.ntb.inf.libusbJava.logger;
import java.io.InputStream;
import java.util.Properties;
import java.util.logging.ConsoleHandler;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.Logger;
public class LogUtil {
// debug this class
private static final boolean debugLogUtil = false;
private static final String PLUGIN_ID = "ch.ntb.inf.libusbJava";
private static final String PROPERTIES_FILE = ".configure";
private static final String LOGGER_WARNING = "Warning in class "
+ LogUtil.class.getName()
+ ": could not load the logger properties file " + PROPERTIES_FILE;
private static boolean debugEnabled;
static {
createLoggersFromProperties();
}
private static void debugMsg(String method, String message) {
if (debugLogUtil) {
System.out.println(method + ": " + message);
}
}
public static void setLevel(Logger logger, Level loglevel) {
Handler[] h = logger.getHandlers();
for (int i = 0; i < h.length; i++) {
System.out.println("setLevel " + loglevel.toString());
h[i].setLevel(loglevel);
}
logger.setLevel(loglevel);
}
public static Logger getLogger(String name) {
debugMsg("getLogger", name);
LogManager manager = LogManager.getLogManager();
// check if logger is already registered
Logger logger = manager.getLogger(name);
if (logger == null) {
logger = Logger.getLogger(name);
setLevel(logger, Level.OFF);
manager.addLogger(logger);
debugMsg("getLogger", "creating new logger");
}
if (logger.getLevel() == null) {
debugMsg("getLogger", "level == null -> setLevel to OFF ");
setLevel(logger, Level.OFF);
}
debugMsg("getLogger", "logLevel " + logger.getLevel().getName());
return logger;
}
private static void initLevel(Logger logger, Level loglevel) {
Handler[] h = logger.getHandlers();
for (int i = 0; i < h.length; i++) {
logger.removeHandler(h[i]);
}
Handler console = new ConsoleHandler();
console.setLevel(loglevel);
logger.addHandler(console);
logger.setLevel(loglevel);
logger.setUseParentHandlers(false);
}
private static void createLoggersFromProperties() {
try {
debugMsg(LogUtil.class.getName(), "createLoggersFromProperties");
InputStream is = LogUtil.class.getClassLoader()
.getResourceAsStream(PROPERTIES_FILE);
if (is == null) {
System.err.println(LOGGER_WARNING);
} else {
Properties prop = new Properties();
prop.load(is);
debugMsg("createLoggersFromProperties",
"properties file loaded: " + PROPERTIES_FILE);
debugMsg("createLoggersFromProperties", "file content:\n"
+ prop.toString());
// get global debug enable flag
debugEnabled = Boolean.parseBoolean(prop.getProperty(PLUGIN_ID
+ "/debug"));
debugMsg("createLoggersFromProperties", "debuging enabled: "
+ debugEnabled);
// get and configure loggers
boolean moreLoggers = true;
int loggerCount = 0;
while (moreLoggers) {
String loggerProp = prop.getProperty(PLUGIN_ID
+ "/debug/logger" + loggerCount);
loggerCount++;
if (loggerProp != null) {
// parse string and get logger name and log level
int slashIndex = loggerProp.indexOf('/');
String loggerName = loggerProp.substring(0, slashIndex)
.trim();
String logLevel = loggerProp.substring(slashIndex + 1,
loggerProp.length());
// register logger
Level level;
if (debugEnabled) {
level = Level.parse(logLevel);
} else {
level = Level.OFF;
}
Logger logger = getLogger(loggerName);
initLevel(logger, level);
debugMsg("createLoggersFromProperties",
"create logger " + loggerName + " with level "
+ level.toString());
} else {
moreLoggers = false;
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

View File

@@ -0,0 +1,16 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<html>
<head></head>
<body bgcolor="white">
Logging related classes.
<h2>Related Resources</h2>
For more information about this project visit
<a
href="http://libusbjava.sourceforge.net">http://libusbjava.sourceforge.net</a>
.
</body>
</html>

View File

@@ -0,0 +1,26 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<html>
<head></head>
<body bgcolor="white">
Includes the main classes for the Java libusb wrapper.
<br>
<i>LibusbJava.java</i>
is the JNI class to the
<i>LibusbJava.dll</i>
. Every class starting with
<i>Usb_</i>
represents a descriptor of the bus structure which is retrieved by
calling
<i>LibusbJava.usb_get_busses()</i>
.
<h2>Related Resources</h2>
For more information about this project visit
<a
href="http://libusbjava.sourceforge.net">http://libusbjava.sourceforge.net</a>
.
</body>
</html>

View File

@@ -0,0 +1,211 @@
/*
* Java libusb wrapper
* Copyright (c) 2005-2006 Andreas Schl<68>pfer <spandi at users.sourceforge.net>
*
* http://libusbjava.sourceforge.net
* This library is covered by the LGPL, read LGPL.txt for details.
*/
package ch.ntb.inf.libusbJava.testApp;
public abstract class AbstractDeviceInfo {
private short idVendor;
private short idProduct;
private String filename = null;
private String busName = null;
private int timeout;
private int configuration;
private int interface_;
private int altinterface;
private int outEPBulk = -1;
private int inEPBulk = -1;
private int outEPInt = -1;
private int inEPInt = -1;
private int sleepTimeout;
private int maxDataSize;
private TransferMode mode;
private boolean compareData = true;
private String manufacturer = null;
private String product = null;
private String serialVersion = null;
public static enum TransferMode {
Bulk, Interrupt
}
public AbstractDeviceInfo() {
initValues();
}
abstract public void initValues();
public int getAltinterface() {
return altinterface;
}
public int getConfiguration() {
return configuration;
}
public short getIdProduct() {
return idProduct;
}
public short getIdVendor() {
return idVendor;
}
public int getInEPBulk() {
return inEPBulk;
}
public int getInEPInt() {
return inEPInt;
}
public int getInterface() {
return interface_;
}
public int getMaxDataSize() {
return maxDataSize;
}
public int getOutEPBulk() {
return outEPBulk;
}
public int getOutEPInt() {
return outEPInt;
}
public int getSleepTimeout() {
return sleepTimeout;
}
public int getTimeout() {
return timeout;
}
public void setAltinterface(int altinterface) {
this.altinterface = altinterface;
}
public void setConfiguration(int configuration) {
this.configuration = configuration;
}
public void setIdProduct(short idProduct) {
this.idProduct = idProduct;
}
public void setIdVendor(short idVendor) {
this.idVendor = idVendor;
}
public void setInEPBulk(int in_ep_bulk) {
this.inEPBulk = in_ep_bulk;
}
public void setInEPInt(int in_ep_int) {
this.inEPInt = in_ep_int;
}
public void setInterface(int interface_) {
this.interface_ = interface_;
}
public void setMaxDataSize(int maxDataSize) {
this.maxDataSize = maxDataSize;
}
public void setOutEPBulk(int out_ep_bulk) {
this.outEPBulk = out_ep_bulk;
}
public void setOutEPInt(int out_ep_int) {
this.outEPInt = out_ep_int;
}
public void setSleepTimeout(int sleepTimeout) {
this.sleepTimeout = sleepTimeout;
}
public void setTimeout(int timeout) {
this.timeout = timeout;
}
public TransferMode getMode() {
return mode;
}
public void setMode(TransferMode mode) {
this.mode = mode;
}
public boolean doCompareData() {
return compareData;
}
public void setDoCompareData(boolean compareData) {
this.compareData = compareData;
}
public String getManufacturer() {
return manufacturer;
}
public void setManufacturer(String manufacturer) {
this.manufacturer = manufacturer;
}
public String getProduct() {
return product;
}
public void setProduct(String product) {
this.product = product;
}
public String getSerialVersion() {
return serialVersion;
}
public void setSerialVersion(String serialVersion) {
this.serialVersion = serialVersion;
}
public String getFilename() {
return filename;
}
public void setFilename(String filename) {
this.filename = filename;
}
public String getBusName() {
return busName;
}
public void setBusName(String busName) {
this.busName = busName;
}
}

View File

@@ -0,0 +1,709 @@
/*
* Java libusb wrapper
* Copyright (c) 2005-2006 Andreas Schl<68>pfer <spandi at users.sourceforge.net>
*
* http://libusbjava.sourceforge.net
* This library is covered by the LGPL, read LGPL.txt for details.
*/
package ch.ntb.inf.libusbJava.testApp;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.event.ItemEvent;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.border.TitledBorder;
public class TestApp extends JFrame {
private static final long serialVersionUID = 994508729204158681L;
TestDevice dev;
private JPanel rootPanel = null;
private JPanel sendReceivePanel = null;
private JPanel settingsPanel = null;
private JButton openDeviceButton = null;
private JButton closeDevice = null;
private JButton resetButton = null;
private JPanel settingsPanelTop = null;
private JPanel settingsPanelBottom = null;
JTextField vendorIDText = null;
JTextField productIDText = null;
private JPanel vendorIDPanel = null;
private JPanel productIDPanel = null;
private JPanel configurationPanel = null;
JTextField configurationText = null;
private JPanel interfacePanel = null;
JTextField interfaceText = null;
private JPanel altInterfacePanel = null;
JTextField altInterfaceText = null;
private JPanel settingsPanelTop2Left = null;
private JPanel settingsPanelTop2Right = null;
private JPanel outEpPanel = null;
JTextField outEpText = null;
private JPanel inEpPanel = null;
JTextField inEpText = null;
private JPanel timeoutPanel = null;
private JTextField timeoutText = null;
private JPanel sendDataPanel = null;
private JPanel sendRecButtonsPanel = null;
private JButton sendButton = null;
private JButton recButton = null;
JTextField sendDataText = null;
JComboBox sendTypeComboBox = null;
private JComboBox recTypeComboBox = null;
private JPanel sendRecButtonsPanelTop = null;
private JPanel sendRecButtonsPanelBottom = null;
public TestApp(TestDevice devInfo) {
super();
this.dev = devInfo;
initialize();
}
private void initialize() {
this.setTitle("USB Test Application");
this.setContentPane(getRootPanel());
// read default values
this.vendorIDText.setText(toHexString(dev.getIdVendor() & 0xffff));
this.productIDText.setText(toHexString(dev.getIdProduct() & 0xffff));
this.configurationText.setText(new Integer(dev.getConfiguration())
.toString());
this.interfaceText.setText(new Integer(dev.getInterface()).toString());
this.altInterfaceText.setText(new Integer(dev.getAltinterface())
.toString());
this.timeoutText.setText(new Integer(dev.getTimeout()).toString());
this.sendDataText.setText(dev.getSendData());
setOutEpAddr();
setInEpAddr();
this.pack();
this.setVisible(true);
}
void setOutEpAddr() {
switch (dev.getOutMode()) {
case Bulk:
this.outEpText.setText(toHexString(dev.getOutEPBulk()));
break;
case Interrupt:
this.outEpText.setText(toHexString(dev.getOutEPInt()));
break;
default:
break;
}
}
void setInEpAddr() {
switch (dev.getInMode()) {
case Bulk:
this.inEpText.setText(toHexString(dev.getInEPBulk()));
break;
case Interrupt:
this.inEpText.setText(toHexString(dev.getInEPInt()));
break;
default:
break;
}
}
private JPanel getRootPanel() {
if (rootPanel == null) {
rootPanel = new JPanel();
rootPanel
.setLayout(new BoxLayout(getRootPanel(), BoxLayout.Y_AXIS));
rootPanel.add(getSettingsPanel(), null);
rootPanel.add(getSendReceivePanel(), null);
}
return rootPanel;
}
private JPanel getSendReceivePanel() {
if (sendReceivePanel == null) {
BorderLayout borderLayout2 = new BorderLayout();
borderLayout2.setHgap(5);
sendReceivePanel = new JPanel();
sendReceivePanel.setLayout(borderLayout2);
sendReceivePanel.setBorder(BorderFactory.createTitledBorder(null,
"Send and Receive Data",
TitledBorder.DEFAULT_JUSTIFICATION,
TitledBorder.DEFAULT_POSITION, new Font("Dialog",
Font.BOLD, 12), new Color(51, 51, 51)));
sendReceivePanel.add(getSendRecButtonsPanel(), BorderLayout.NORTH);
sendReceivePanel.add(getSendDataPanel(), BorderLayout.SOUTH);
}
return sendReceivePanel;
}
private JPanel getSettingsPanel() {
if (settingsPanel == null) {
settingsPanel = new JPanel();
settingsPanel.setLayout(new BoxLayout(getSettingsPanel(),
BoxLayout.Y_AXIS));
settingsPanel.setBorder(BorderFactory.createTitledBorder(null,
"Device Settings", TitledBorder.DEFAULT_JUSTIFICATION,
TitledBorder.DEFAULT_POSITION, new Font("Dialog",
Font.BOLD, 12), new Color(51, 51, 51)));
settingsPanel.add(getSettingsPanelTop(), null);
settingsPanel.add(getSettingsPanelBottom(), null);
}
return settingsPanel;
}
private JButton getOpenDeviceButton() {
if (openDeviceButton == null) {
openDeviceButton = new JButton();
openDeviceButton.setText("Open Device");
openDeviceButton
.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
// update values for the device
dev.setIdVendor((short) parseInt(vendorIDText
.getText().trim()));
dev.setIdProduct((short) parseInt(productIDText
.getText().trim()));
dev.setConfiguration(parseInt(configurationText
.getText().trim()));
dev.setInterface(parseInt(interfaceText.getText()
.trim()));
dev.setAltinterface(parseInt(altInterfaceText
.getText().trim()));
// opent the device
dev.openUsbDevice();
}
});
}
return openDeviceButton;
}
private JButton getCloseDevice() {
if (closeDevice == null) {
closeDevice = new JButton();
closeDevice.setText("Close Device");
closeDevice.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
dev.closeUsbDevice();
}
});
}
return closeDevice;
}
private JButton getResetButton() {
if (resetButton == null) {
resetButton = new JButton();
resetButton.setText("Reset Device");
resetButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
dev.resetUsbDevice();
}
});
}
return resetButton;
}
private JPanel getSettingsPanelTop() {
if (settingsPanelTop == null) {
FlowLayout flowLayout = new FlowLayout();
flowLayout.setVgap(1);
flowLayout.setAlignment(FlowLayout.LEFT);
settingsPanelTop = new JPanel();
settingsPanelTop.setLayout(flowLayout);
settingsPanelTop.add(getVendorIDPanel(), null);
settingsPanelTop.add(getProductIDPanel(), null);
settingsPanelTop.add(getConfigurationPanel(), null);
settingsPanelTop.add(getInterfacePanel(), null);
settingsPanelTop.add(getAltInterfacePanel(), null);
}
return settingsPanelTop;
}
private JPanel getSettingsPanelBottom() {
if (settingsPanelBottom == null) {
FlowLayout flowLayout1 = new FlowLayout();
flowLayout1.setVgap(1);
flowLayout1.setHgap(0);
flowLayout1.setAlignment(FlowLayout.LEFT);
GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
gridBagConstraints1.gridx = -1;
gridBagConstraints1.gridy = -1;
settingsPanelBottom = new JPanel();
settingsPanelBottom.setLayout(flowLayout1);
settingsPanelBottom.add(getSettingsPanelTop2Left(), null);
settingsPanelBottom.add(getSettingsPanelTop2Right(), null);
}
return settingsPanelBottom;
}
private JTextField getVendorIDText() {
if (vendorIDText == null) {
vendorIDText = new JTextField();
vendorIDText.setPreferredSize(new Dimension(100, 20));
}
return vendorIDText;
}
private JTextField getProductIDText() {
if (productIDText == null) {
productIDText = new JTextField();
productIDText.setPreferredSize(new Dimension(100, 20));
}
return productIDText;
}
private JPanel getVendorIDPanel() {
if (vendorIDPanel == null) {
GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
gridBagConstraints3.fill = GridBagConstraints.VERTICAL;
gridBagConstraints3.gridy = -1;
gridBagConstraints3.weightx = 1.0;
gridBagConstraints3.gridx = -1;
GridBagConstraints gridBagConstraints5 = new GridBagConstraints();
gridBagConstraints5.gridx = -1;
gridBagConstraints5.gridy = -1;
vendorIDPanel = new JPanel();
vendorIDPanel.setBorder(BorderFactory.createTitledBorder(null,
"VendorID", TitledBorder.DEFAULT_JUSTIFICATION,
TitledBorder.DEFAULT_POSITION, new Font("Dialog",
Font.BOLD, 12), new Color(51, 51, 51)));
vendorIDPanel.setLayout(new BoxLayout(getVendorIDPanel(),
BoxLayout.X_AXIS));
vendorIDPanel.add(getVendorIDText(), null);
}
return vendorIDPanel;
}
private JPanel getProductIDPanel() {
if (productIDPanel == null) {
GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
gridBagConstraints4.fill = GridBagConstraints.VERTICAL;
gridBagConstraints4.gridy = -1;
gridBagConstraints4.weightx = 1.0;
gridBagConstraints4.gridx = -1;
productIDPanel = new JPanel();
productIDPanel.setBorder(BorderFactory.createTitledBorder(null,
"ProductID", TitledBorder.DEFAULT_JUSTIFICATION,
TitledBorder.DEFAULT_POSITION, new Font("Dialog",
Font.BOLD, 12), new Color(51, 51, 51)));
productIDPanel.setLayout(new BoxLayout(getProductIDPanel(),
BoxLayout.X_AXIS));
productIDPanel.add(getProductIDText(), null);
}
return productIDPanel;
}
private JPanel getConfigurationPanel() {
if (configurationPanel == null) {
configurationPanel = new JPanel();
configurationPanel.setLayout(new BoxLayout(getConfigurationPanel(),
BoxLayout.X_AXIS));
configurationPanel.setBorder(BorderFactory.createTitledBorder(null,
"Configuration", TitledBorder.DEFAULT_JUSTIFICATION,
TitledBorder.DEFAULT_POSITION, new Font("Dialog",
Font.BOLD, 12), new Color(51, 51, 51)));
configurationPanel.add(getConfigurationText(), null);
}
return configurationPanel;
}
private JTextField getConfigurationText() {
if (configurationText == null) {
configurationText = new JTextField();
configurationText.setPreferredSize(new Dimension(100, 20));
}
return configurationText;
}
private JPanel getInterfacePanel() {
if (interfacePanel == null) {
interfacePanel = new JPanel();
interfacePanel.setLayout(new BoxLayout(getInterfacePanel(),
BoxLayout.X_AXIS));
interfacePanel.setBorder(BorderFactory.createTitledBorder(null,
"Interface", TitledBorder.DEFAULT_JUSTIFICATION,
TitledBorder.DEFAULT_POSITION, new Font("Dialog",
Font.BOLD, 12), new Color(51, 51, 51)));
interfacePanel.add(getInterfaceText(), null);
}
return interfacePanel;
}
private JTextField getInterfaceText() {
if (interfaceText == null) {
interfaceText = new JTextField();
interfaceText.setPreferredSize(new Dimension(100, 20));
}
return interfaceText;
}
private JPanel getAltInterfacePanel() {
if (altInterfacePanel == null) {
altInterfacePanel = new JPanel();
altInterfacePanel.setLayout(new BoxLayout(getAltInterfacePanel(),
BoxLayout.X_AXIS));
altInterfacePanel.setBorder(BorderFactory.createTitledBorder(null,
"Alternate Int", TitledBorder.DEFAULT_JUSTIFICATION,
TitledBorder.DEFAULT_POSITION, new Font("Dialog",
Font.BOLD, 12), new Color(51, 51, 51)));
altInterfacePanel.add(getAltInterfaceText(), null);
}
return altInterfacePanel;
}
private JTextField getAltInterfaceText() {
if (altInterfaceText == null) {
altInterfaceText = new JTextField();
altInterfaceText.setPreferredSize(new Dimension(100, 20));
}
return altInterfaceText;
}
private JPanel getSettingsPanelTop2Left() {
if (settingsPanelTop2Left == null) {
FlowLayout flowLayout2 = new FlowLayout();
flowLayout2.setVgap(2);
flowLayout2.setAlignment(FlowLayout.LEFT);
flowLayout2.setHgap(5);
settingsPanelTop2Left = new JPanel();
settingsPanelTop2Left.setLayout(flowLayout2);
settingsPanelTop2Left.add(getOutEpPanel(), null);
settingsPanelTop2Left.add(getInEpPanel(), null);
settingsPanelTop2Left.add(getTimeoutPanel(), null);
}
return settingsPanelTop2Left;
}
private JPanel getSettingsPanelTop2Right() {
if (settingsPanelTop2Right == null) {
FlowLayout flowLayout3 = new FlowLayout();
flowLayout3.setVgap(2);
settingsPanelTop2Right = new JPanel();
settingsPanelTop2Right.setLayout(flowLayout3);
settingsPanelTop2Right.add(getOpenDeviceButton(), null);
settingsPanelTop2Right.add(getCloseDevice(), null);
settingsPanelTop2Right.add(getResetButton(), null);
}
return settingsPanelTop2Right;
}
private JPanel getOutEpPanel() {
if (outEpPanel == null) {
outEpPanel = new JPanel();
outEpPanel.setLayout(new BoxLayout(getOutEpPanel(),
BoxLayout.X_AXIS));
outEpPanel.setBorder(BorderFactory.createTitledBorder(null,
"OUT EP", TitledBorder.DEFAULT_JUSTIFICATION,
TitledBorder.DEFAULT_POSITION, new Font("Dialog",
Font.BOLD, 12), new Color(51, 51, 51)));
outEpPanel.add(getOutEpText(), null);
}
return outEpPanel;
}
private JTextField getOutEpText() {
if (outEpText == null) {
outEpText = new JTextField();
outEpText.setPreferredSize(new Dimension(100, 20));
}
return outEpText;
}
private JPanel getInEpPanel() {
if (inEpPanel == null) {
inEpPanel = new JPanel();
inEpPanel
.setLayout(new BoxLayout(getInEpPanel(), BoxLayout.X_AXIS));
inEpPanel.setBorder(BorderFactory.createTitledBorder(null, "IN EP",
TitledBorder.DEFAULT_JUSTIFICATION,
TitledBorder.DEFAULT_POSITION, new Font("Dialog",
Font.BOLD, 12), new Color(51, 51, 51)));
inEpPanel.add(getInEpText(), null);
}
return inEpPanel;
}
private JTextField getInEpText() {
if (inEpText == null) {
inEpText = new JTextField();
inEpText.setPreferredSize(new Dimension(100, 20));
}
return inEpText;
}
private JPanel getTimeoutPanel() {
if (timeoutPanel == null) {
timeoutPanel = new JPanel();
timeoutPanel.setLayout(new BoxLayout(getTimeoutPanel(),
BoxLayout.X_AXIS));
timeoutPanel.setBorder(BorderFactory.createTitledBorder(null,
"Timeout", TitledBorder.DEFAULT_JUSTIFICATION,
TitledBorder.DEFAULT_POSITION, new Font("Dialog",
Font.BOLD, 12), new Color(51, 51, 51)));
timeoutPanel.add(getTimeoutText(), null);
}
return timeoutPanel;
}
private JTextField getTimeoutText() {
if (timeoutText == null) {
timeoutText = new JTextField();
timeoutText.setPreferredSize(new Dimension(100, 20));
}
return timeoutText;
}
private JPanel getSendDataPanel() {
if (sendDataPanel == null) {
FlowLayout flowLayout4 = new FlowLayout();
flowLayout4.setAlignment(FlowLayout.LEFT);
sendDataPanel = new JPanel();
sendDataPanel.setLayout(flowLayout4);
sendDataPanel.setBorder(BorderFactory.createTitledBorder(null,
"Data to send [hex]", TitledBorder.DEFAULT_JUSTIFICATION,
TitledBorder.DEFAULT_POSITION, new Font("Dialog",
Font.BOLD, 12), new Color(51, 51, 51)));
sendDataPanel.add(getSendRecDataText(), null);
}
return sendDataPanel;
}
private JPanel getSendRecButtonsPanel() {
if (sendRecButtonsPanel == null) {
FlowLayout flowLayout5 = new FlowLayout();
flowLayout5.setAlignment(FlowLayout.LEFT);
flowLayout5.setVgap(0);
sendRecButtonsPanel = new JPanel();
sendRecButtonsPanel.setLayout(flowLayout5);
sendRecButtonsPanel.add(getSendRecButtonsPanelTop(), null);
sendRecButtonsPanel.add(getSendRecButtonsPanelBottom(), null);
}
return sendRecButtonsPanel;
}
private JButton getSendButton() {
if (sendButton == null) {
sendButton = new JButton();
sendButton.setText("Send");
sendButton.setName("sendButton");
sendButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
int index = sendTypeComboBox.getSelectedIndex();
if (index == TestDevice.TransferMode.Bulk.ordinal()) {
dev.setOutEPBulk(parseInt(outEpText.getText().trim()));
dev.setMode(TestDevice.TransferMode.Bulk);
} else if (index == TestDevice.TransferMode.Interrupt
.ordinal()) {
dev.setOutEPInt(parseInt(outEpText.getText().trim()));
dev.setMode(TestDevice.TransferMode.Interrupt);
}
byte[] data = parseByteArray(sendDataText.getText().trim());
dev.write(data, data.length);
}
});
}
return sendButton;
}
private JButton getRecButton() {
if (recButton == null) {
recButton = new JButton();
recButton.setText("Receive");
recButton.setName("recButton");
recButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
int index = sendTypeComboBox.getSelectedIndex();
if (index == TestDevice.TransferMode.Bulk.ordinal()) {
dev.setInEPBulk(parseInt(inEpText.getText().trim()));
dev.setMode(TestDevice.TransferMode.Bulk);
} else if (index == TestDevice.TransferMode.Interrupt
.ordinal()) {
dev.setInEPInt(parseInt(inEpText.getText().trim()));
dev.setMode(TestDevice.TransferMode.Interrupt);
}
dev.read();
}
});
}
return recButton;
}
private JTextField getSendRecDataText() {
if (sendDataText == null) {
sendDataText = new JTextField();
sendDataText.setPreferredSize(new Dimension(650, 20));
}
return sendDataText;
}
int parseInt(String s) {
if (s == "")
return 0;
if (s.indexOf('x') > 0) {
// is hex number
if (s.length() <= 2) { // exception for "0x"
return 0;
}
return Integer.parseInt(
s.substring(s.indexOf('x') + 1, s.length()), 16);
}
// is decimal number
return Integer.parseInt(s);
}
byte[] parseByteArray(String s) {
final int HEX_WIDTH = 5;
StringBuffer sb = new StringBuffer();
int stringIndex = 0, spaceIndex = 0;
String ss;
while (stringIndex + 3 < s.length()) {
ss = s.substring(spaceIndex, spaceIndex + 4);
spaceIndex = s.indexOf(' ', stringIndex) + 1;
sb.append((char) parseInt(ss));
stringIndex += HEX_WIDTH;
}
return sb.toString().getBytes();
}
private static String toHexString(int value) {
return "0x" + Integer.toHexString(value);
}
/**
* This method initializes sendTypeComboBox
*
* @return javax.swing.JComboBox
*/
private JComboBox getSendTypeComboBox() {
if (sendTypeComboBox == null) {
sendTypeComboBox = new JComboBox(dev.getTransferTypes());
sendTypeComboBox.setSelectedIndex(dev.getOutMode().ordinal());
sendTypeComboBox.addItemListener(new java.awt.event.ItemListener() {
public void itemStateChanged(java.awt.event.ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
String mode = (String) e.getItem();
if (mode.equalsIgnoreCase("Bulk")) {
dev.setOutMode(TestDevice.TransferMode.Bulk);
setOutEpAddr();
} else if (mode.equalsIgnoreCase("Interrupt")) {
dev.setOutMode(TestDevice.TransferMode.Interrupt);
setOutEpAddr();
}
}
}
});
}
return sendTypeComboBox;
}
/**
* This method initializes recTypeComboBox
*
* @return javax.swing.JComboBox
*/
private JComboBox getRecTypeComboBox() {
if (recTypeComboBox == null) {
recTypeComboBox = new JComboBox(dev.getTransferTypes());
recTypeComboBox.setSelectedIndex(dev.getInMode().ordinal());
recTypeComboBox.addItemListener(new java.awt.event.ItemListener() {
public void itemStateChanged(java.awt.event.ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
String mode = (String) e.getItem();
if (mode.equalsIgnoreCase("Bulk")) {
dev.setInMode(TestDevice.TransferMode.Bulk);
setInEpAddr();
} else if (mode.equalsIgnoreCase("Interrupt")) {
dev.setInMode(TestDevice.TransferMode.Interrupt);
setInEpAddr();
}
}
}
});
// recTypeComboBox.addActionListener(new
// java.awt.event.ActionListener() {
// public void actionPerformed(java.awt.event.ActionEvent e) {
// JComboBox source = (JComboBox) e.getSource();
// String mode = "";
// } if (mode.equalsIgnoreCase("Bulk")) {
// dev.setInMode(TestDevice.TransferMode.Bulk);
// setInEpAddr();
// } else if (mode.equalsIgnoreCase("Interrupt")) {
// dev.setInMode(TestDevice.TransferMode.Interrupt);
// setInEpAddr();
// }
//
// });
}
return recTypeComboBox;
}
/**
* This method initializes sendRecButtonsPanelTop
*
* @return javax.swing.JPanel
*/
private JPanel getSendRecButtonsPanelTop() {
if (sendRecButtonsPanelTop == null) {
BorderLayout borderLayout1 = new BorderLayout();
borderLayout1.setHgap(5);
sendRecButtonsPanelTop = new JPanel();
sendRecButtonsPanelTop.setBorder(BorderFactory.createTitledBorder(
null, "OUT", TitledBorder.DEFAULT_JUSTIFICATION,
TitledBorder.DEFAULT_POSITION, new Font("Dialog",
Font.BOLD, 12), new Color(51, 51, 51)));
sendRecButtonsPanelTop.setLayout(borderLayout1);
sendRecButtonsPanelTop.add(getSendButton(), BorderLayout.EAST);
sendRecButtonsPanelTop
.add(getSendTypeComboBox(), BorderLayout.WEST);
}
return sendRecButtonsPanelTop;
}
/**
* This method initializes sendRecButtonsPanelBottom
*
* @return javax.swing.JPanel
*/
private JPanel getSendRecButtonsPanelBottom() {
if (sendRecButtonsPanelBottom == null) {
BorderLayout borderLayout = new BorderLayout();
borderLayout.setHgap(5);
sendRecButtonsPanelBottom = new JPanel();
sendRecButtonsPanelBottom.setBorder(BorderFactory
.createTitledBorder(null, "IN",
TitledBorder.DEFAULT_JUSTIFICATION,
TitledBorder.DEFAULT_POSITION, new Font("Dialog",
Font.BOLD, 12), new Color(51, 51, 51)));
sendRecButtonsPanelBottom.setLayout(borderLayout);
sendRecButtonsPanelBottom.add(getRecButton(), BorderLayout.EAST);
sendRecButtonsPanelBottom.add(getRecTypeComboBox(),
BorderLayout.WEST);
}
return sendRecButtonsPanelBottom;
}
public static void main(String[] args) {
// set LookAndFeel
try {
UIManager
.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch (Exception e) {
e.printStackTrace();
}
TestApp app = new TestApp(new TestDevice());
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

View File

@@ -0,0 +1,191 @@
/*
* Java libusb wrapper
* Copyright (c) 2005-2006 Andreas Schl<68>pfer <spandi at users.sourceforge.net>
*
* http://libusbjava.sourceforge.net
* This library is covered by the LGPL, read LGPL.txt for details.
*/
package ch.ntb.inf.libusbJava.testApp;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import ch.ntb.inf.libusbJava.Device;
import ch.ntb.inf.libusbJava.USB;
import ch.ntb.inf.libusbJava.USBException;
import ch.ntb.inf.libusbJava.logger.LogUtil;
public class TestDevice extends AbstractDeviceInfo {
private static final Logger logger = LogUtil.getLogger("ch.ntb.inf.libusbJava.test");
private String sendData = "0x5b 0x02 0x01 0x00 0x03 0x03 0xf0 0xf0 0x1f";
private Vector<String> transferTypes;
private static Device dev = null;
private TransferMode inMode;
private TransferMode outMode;
public TestDevice() {
logger.setLevel(Level.ALL);
// create a vector for transfer types
transferTypes = new Vector<String>();
transferTypes
.add(TransferMode.Bulk.ordinal(), TransferMode.Bulk.name());
transferTypes.add(TransferMode.Interrupt.ordinal(),
TransferMode.Interrupt.name());
inMode = TransferMode.Bulk;
outMode = TransferMode.Bulk;
}
@Override
public void initValues() {
setIdVendor((short) 0x8235);
setIdProduct((short) 0x0222);
setTimeout(2000);
setConfiguration(1);
setInterface(0);
setAltinterface(-1);
setOutEPBulk(0x01);
setInEPBulk(0x82);
setOutEPInt(0x03);
setInEPInt(0x84);
setSleepTimeout(2000);
setMaxDataSize(USB.FULLSPEED_MAX_BULK_PACKET_SIZE);
setMode(TransferMode.Bulk);
}
public void openUsbDevice() {
dev = USB.getDevice(getIdVendor(), getIdProduct());
try {
dev.open(getConfiguration(), getInterface(), getAltinterface());
logger.info("device opened, interface claimed");
} catch (USBException e) {
e.printStackTrace();
}
}
public void closeUsbDevice() {
try {
if (dev != null) {
dev.close();
logger.info("device closed");
} else {
logger.warning("no device to close -> open first");
}
} catch (USBException e) {
e.printStackTrace();
}
}
public void resetUsbDevice() {
try {
if (dev != null) {
dev.reset();
logger.info("device reset");
} else {
logger.warning("no device to reset -> open first");
}
} catch (USBException e) {
e.printStackTrace();
}
}
public void write(byte[] data, int length) {
int lenWritten = 0;
try {
if (dev != null) {
StringBuffer sb = new StringBuffer();
switch (getOutMode()) {
case Bulk:
lenWritten = dev.writeBulk(getOutEPBulk(), data, length,
getTimeout(), false);
sb.append("write_bulk, ep: 0x"
+ Integer.toHexString(getOutEPBulk()) + ", "
+ lenWritten + " Bytes sent: ");
break;
case Interrupt:
lenWritten = dev.writeInterrupt(getOutEPInt(), data,
length, getTimeout(), false);
sb.append("write_interrupt, ep: 0x"
+ Integer.toHexString(getOutEPInt()) + ", "
+ lenWritten + " Bytes sent: ");
break;
}
for (int i = 0; i < lenWritten; i++) {
sb.append("0x" + String.format("%1$02X", data[i]) + " ");
}
logger.info(sb.toString());
} else {
logger.warning("no device opened");
}
} catch (USBException e) {
e.printStackTrace();
}
}
public void read() {
if (dev != null) {
byte[] data = new byte[dev.getMaxPacketSize()];
int lenRead = 0;
try {
StringBuffer sb = new StringBuffer();
switch (getInMode()) {
case Bulk:
lenRead = dev.readBulk(getInEPBulk(), data, dev
.getMaxPacketSize(), getTimeout(), false);
sb.append("read_bulk, ep: 0x"
+ Integer.toHexString(getInEPBulk()) + ", "
+ lenRead + " Bytes received: Data: ");
break;
case Interrupt:
lenRead = dev.readInterrupt(getInEPInt(), data, dev
.getMaxPacketSize(), getTimeout(), false);
sb.append("read_interrupt, ep: 0x"
+ Integer.toHexString(getInEPInt()) + ", "
+ lenRead + " Bytes received: Data: ");
break;
}
for (int i = 0; i < lenRead; i++) {
sb.append("0x" + String.format("%1$02X", data[i]) + " ");
}
logger.info(sb.toString());
} catch (USBException e) {
e.printStackTrace();
}
} else {
logger.warning("no device opened");
}
}
public String getSendData() {
return sendData;
}
public void setSendData(String sendData) {
this.sendData = sendData;
}
public Vector<String> getTransferTypes() {
return transferTypes;
}
public TransferMode getOutMode() {
return outMode;
}
public void setOutMode(TransferMode outMode) {
this.outMode = outMode;
}
public TransferMode getInMode() {
return inMode;
}
public void setInMode(TransferMode inMode) {
this.inMode = inMode;
}
}

View File

@@ -0,0 +1,16 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<html>
<head></head>
<body bgcolor="white">
A test application with a GUI to write to and read from a device (based on Swing).
<h2>Related Resources</h2>
For more information about this project visit
<a
href="http://libusbjava.sourceforge.net">http://libusbjava.sourceforge.net</a>
.
</body>
</html>

View File

@@ -0,0 +1,495 @@
/*
* Java libusb wrapper
* Copyright (c) 2005-2006 Andreas Schl<68>pfer <spandi at users.sourceforge.net>
*
* http://libusbjava.sourceforge.net
* This library is covered by the LGPL, read LGPL.txt for details.
*/
package ch.ntb.inf.libusbJava.usbView;
import java.util.Vector;
import javax.swing.JTextArea;
import javax.swing.JTree;
import javax.swing.event.TreeModelEvent;
import javax.swing.event.TreeModelListener;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;
import ch.ntb.inf.libusbJava.LibusbJava;
import ch.ntb.inf.libusbJava.Usb_Bus;
import ch.ntb.inf.libusbJava.Usb_Config_Descriptor;
import ch.ntb.inf.libusbJava.Usb_Device;
import ch.ntb.inf.libusbJava.Usb_Device_Descriptor;
import ch.ntb.inf.libusbJava.Usb_Endpoint_Descriptor;
import ch.ntb.inf.libusbJava.Usb_Interface;
import ch.ntb.inf.libusbJava.Usb_Interface_Descriptor;
public class UsbTreeModel implements TreeModel, TreeSelectionListener {
private Usb_Bus rootBus;
private static final String USB_ROOT = "USB";
private JTextArea textArea;
private Vector<TreeModelListener> treeModelListeners = new Vector<TreeModelListener>();
/**
* Default constructor.<br>
*
* @param rootBus
* the root bus from which the data is read
* @param textArea
* the text area to which the data is written
*/
public UsbTreeModel(Usb_Bus rootBus, JTextArea textArea) {
this.rootBus = rootBus;
this.textArea = textArea;
}
/**
* Returns the root of the tree.
*/
public Object getRoot() {
return USB_ROOT;
}
/**
* Returns the child of parent at index index in the parent's child array.
*/
public Object getChild(Object parent, int index) {
if (parent instanceof String && ((String) parent).compareTo(USB_ROOT) == 0)
{
Usb_Bus curBus = rootBus;
for (int i = 0; curBus != null; curBus = curBus.getNext(), i++)
{
if (i == index)
return curBus;
}
}
else if (parent instanceof Usb_Bus) {
Usb_Device device = ((Usb_Bus) parent).getDevices();
int count = 0;
while (device != null) {
if (count == index)
return device;
count++;
device = device.getNext();
}
return null;
} else if (parent instanceof Usb_Device) {
Usb_Device dev = (Usb_Device) parent;
// return the Usb_Device_Descriptor at index 0
if (index == 0) {
return dev.getDescriptor();
}
Usb_Config_Descriptor[] confDescs = dev.getConfig();
if (index >= confDescs.length + 1)
return null;
return confDescs[index - 1];
} else if (parent instanceof Usb_Config_Descriptor) {
Usb_Interface[] intDescs = ((Usb_Config_Descriptor) parent)
.getInterface();
if (index >= intDescs.length)
return null;
return intDescs[index];
} else if (parent instanceof Usb_Interface) {
Usb_Interface_Descriptor[] altSettings = ((Usb_Interface) parent)
.getAltsetting();
if (index >= altSettings.length)
return null;
return altSettings[index];
} else if (parent instanceof Usb_Interface_Descriptor) {
Usb_Endpoint_Descriptor[] endpoints = ((Usb_Interface_Descriptor) parent)
.getEndpoint();
if (index >= endpoints.length)
return null;
return endpoints[index];
}
return null;
}
/**
* Returns the number of children of parent.
*/
public int getChildCount(Object parent)
{
if (parent instanceof String && ((String) parent).compareTo(USB_ROOT) == 0)
{
int count = 0;
Usb_Bus curBus = rootBus;
for (; curBus != null; curBus = curBus.getNext())
{
count++;
}
return count;
}
else if (parent instanceof Usb_Bus) {
Usb_Device device = ((Usb_Bus) parent).getDevices();
int count = 0;
while (device != null) {
count++;
device = device.getNext();
}
return count;
} else if (parent instanceof Usb_Device) {
// add the Usb_Device_Descriptor
return ((Usb_Device) parent).getConfig().length + 1;
} else if (parent instanceof Usb_Config_Descriptor) {
return ((Usb_Config_Descriptor) parent).getInterface().length;
} else if (parent instanceof Usb_Interface) {
return ((Usb_Interface) parent).getAltsetting().length;
} else if (parent instanceof Usb_Interface_Descriptor) {
return ((Usb_Interface_Descriptor) parent).getEndpoint().length;
}
return 0;
}
/**
* Returns true if node is a leaf.
*/
public boolean isLeaf(Object node) {
return false;
}
/**
* Messaged when the user has altered the value for the item identified by
* path to newValue. Not used by this model.
*/
public void valueForPathChanged(TreePath path, Object newValue) {
System.out.println("*** valueForPathChanged : " + path + " --> "
+ newValue);
}
/**
* Returns the index of child in parent.
*/
public int getIndexOfChild(Object parent, Object child) {
return 0;
}
public void addTreeModelListener(TreeModelListener l) {
treeModelListeners.addElement(l);
}
public void removeTreeModelListener(TreeModelListener l) {
treeModelListeners.removeElement(l);
}
/**
* The only event raised by this model is TreeStructureChanged with the root
* as path, i.e. the whole tree has changed.
*/
protected void fireTreeStructureChanged(Usb_Bus newRootBus) {
rootBus = newRootBus;
int len = treeModelListeners.size();
TreeModelEvent e = new TreeModelEvent(this, new Object[] { newRootBus });
for (int i = 0; i < len; i++) {
treeModelListeners.elementAt(i).treeStructureChanged(e);
}
}
public void valueChanged(TreeSelectionEvent e) {
JTree tree = (JTree) e.getSource();
Object component = tree.getLastSelectedPathComponent();
if (component instanceof Usb_Bus) {
Usb_Bus bus = (Usb_Bus) component;
StringBuffer sb = new StringBuffer("Usb_Bus\n");
sb.append("\tdirname: " + bus.getDirname() + "\n");
sb.append("\tlocation: 0x" + Long.toHexString(bus.getLocation())
+ "\n");
textArea.setText(sb.toString());
} else if (component instanceof Usb_Device) {
Usb_Device device = (Usb_Device) component;
StringBuffer sb = new StringBuffer("Usb_Device\n");
sb.append("\tfilename: " + device.getFilename() + "\n");
sb.append("\tdevnum: " + device.getDevnum() + "\n");
sb.append("\tnum_children: " + device.getNumChildren() + "\n");
textArea.setText(sb.toString());
} else if (component instanceof Usb_Device_Descriptor) {
Usb_Device_Descriptor devDesc = (Usb_Device_Descriptor) component;
StringBuffer sb = new StringBuffer("Usb_Device_Descriptor\n");
sb.append("\tblenght: 0x"
+ Integer.toHexString(devDesc.getBLength() & 0xFF) + "\n");
sb.append("\tbDescriptorType: 0x"
+ Integer.toHexString(devDesc.getBDescriptorType() & 0xFF)
+ "\n");
sb.append("\tbcdUSB: 0x"
+ Integer.toHexString(devDesc.getBcdUSB() & 0xFFFF) + "\n");
sb.append("\tbDeviceClass: 0x"
+ Integer.toHexString(devDesc.getBDeviceClass() & 0xFF)
+ "\n");
sb.append("\tbDeviceSubClass: 0x"
+ Integer.toHexString(devDesc.getBDeviceSubClass() & 0xFF)
+ "\n");
sb.append("\tbDeviceProtocol: 0x"
+ Integer.toHexString(devDesc.getBDeviceProtocol() & 0xFF)
+ "\n");
sb.append("\tbMaxPacketSize0: 0x"
+ Integer.toHexString(devDesc.getBMaxPacketSize0() & 0xFF)
+ " (" + devDesc.getBMaxPacketSize0() + ")\n");
sb.append("\tidVendor: 0x"
+ Integer.toHexString(devDesc.getIdVendor() & 0xFFFF)
+ "\n");
sb.append("\tidProduct: 0x"
+ Integer.toHexString(devDesc.getIdProduct() & 0xFFFF)
+ "\n");
sb
.append("\tbcdDevice: 0x"
+ Integer
.toHexString(devDesc.getBcdDevice() & 0xFF)
+ "\n");
sb.append("\tiManufacturer: 0x"
+ Integer.toHexString(devDesc.getIManufacturer() & 0xFF)
+ "\n");
sb.append("\tiProduct: 0x"
+ Integer.toHexString(devDesc.getIProduct()) + "\n");
sb.append("\tiSerialNumber: 0x"
+ Integer.toHexString(devDesc.getISerialNumber() & 0xFF)
+ "\n");
sb
.append("\tbNumConfigurations: 0x"
+ Integer.toHexString(devDesc
.getBNumConfigurations() & 0xFF) + "\n");
// get device handle to retrieve string descriptors
Usb_Bus bus = rootBus;
while (bus != null) {
Usb_Device dev = bus.getDevices();
while (dev != null) {
Usb_Device_Descriptor tmpDevDesc = dev.getDescriptor();
if ((dev.getDescriptor() != null)
&& ((dev.getDescriptor().getIManufacturer() > 0)
|| (dev.getDescriptor().getIProduct() > 0) || (dev
.getDescriptor().getISerialNumber() > 0))) {
if (tmpDevDesc.equals(devDesc)) {
long handle = LibusbJava.usb_open(dev);
sb.append("\nString descriptors\n");
if (handle <= 0) {
sb.append("\terror opening the device\n");
break;
}
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();
}
bus = bus.getNext();
}
textArea.setText(sb.toString());
} else if (component instanceof Usb_Config_Descriptor) {
Usb_Config_Descriptor confDesc = (Usb_Config_Descriptor) component;
StringBuffer sb = new StringBuffer("Usb_Config_Descriptor\n");
sb.append("\tblenght: 0x"
+ Integer.toHexString(confDesc.getBLength()) + "\n");
sb.append("\tbDescriptorType: 0x"
+ Integer.toHexString(confDesc.getBDescriptorType() & 0xFF)
+ "\n");
sb.append("\tbNumInterfaces: 0x"
+ Integer.toHexString(confDesc.getBNumInterfaces() & 0xFF)
+ "\n");
sb
.append("\tbConfigurationValue: 0x"
+ Integer.toHexString(confDesc
.getBConfigurationValue() & 0xFF) + "\n");
sb.append("\tiConfiguration: 0x"
+ Integer.toHexString(confDesc.getIConfiguration() & 0xFF)
+ "\n");
sb.append("\tbmAttributes: 0x"
+ Integer.toHexString(confDesc.getBmAttributes() & 0xFF)
+ "\n");
sb.append("\tMaxPower [mA]: 0x"
+ Integer.toHexString(confDesc.getMaxPower() & 0xFF) + " ("
+ confDesc.getMaxPower() + ")\n");
sb.append("\textralen: 0x"
+ Integer.toHexString(confDesc.getExtralen()) + "\n");
sb.append("\textra: " + extraDescriptorToString(confDesc.getExtra()) + "\n");
// get device handle to retrieve string descriptors
Usb_Bus bus = rootBus;
while (bus != null) {
Usb_Device dev = bus.getDevices();
while (dev != null) {
Usb_Config_Descriptor[] tmpConfDesc = dev.getConfig();
for (int i = 0; i < tmpConfDesc.length; i++) {
if ((tmpConfDesc.equals(confDesc))
&& (confDesc.getIConfiguration() > 0)) {
long handle = LibusbJava.usb_open(dev);
sb.append("\nString descriptors\n");
if (handle <= 0) {
sb.append("\terror opening the device\n");
break;
}
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();
}
bus = bus.getNext();
}
textArea.setText(sb.toString());
} else if (component instanceof Usb_Interface) {
Usb_Interface int_ = (Usb_Interface) component;
StringBuffer sb = new StringBuffer("Usb_Interface\n");
sb.append("\tnum_altsetting: 0x"
+ Integer.toHexString(int_.getNumAltsetting()) + "\n");
sb.append("\taltsetting: " + int_.getAltsetting() + "\n");
textArea.setText(sb.toString());
} else if (component instanceof Usb_Interface_Descriptor) {
Usb_Interface_Descriptor intDesc = (Usb_Interface_Descriptor) component;
StringBuffer sb = new StringBuffer("Usb_Interface_Descriptor\n");
sb.append("\tblenght: 0x"
+ Integer.toHexString(intDesc.getBLength() & 0xFF) + "\n");
sb.append("\tbDescriptorType: 0x"
+ Integer.toHexString(intDesc.getBDescriptorType() & 0xFF)
+ "\n");
sb.append("\tbInterfaceNumber: 0x"
+ Integer.toHexString(intDesc.getBInterfaceNumber() & 0xFF)
+ "\n");
sb.append("\tbAlternateSetting: 0x"
+ Integer
.toHexString(intDesc.getBAlternateSetting() & 0xFF)
+ "\n");
sb.append("\tbNumEndpoints: 0x"
+ Integer.toHexString(intDesc.getBNumEndpoints() & 0xFF)
+ "\n");
sb.append("\tbInterfaceClass: 0x"
+ Integer.toHexString(intDesc.getBInterfaceClass() & 0xFF)
+ "\n");
sb
.append("\tbInterfaceSubClass: 0x"
+ Integer.toHexString(intDesc
.getBInterfaceSubClass() & 0xFF) + "\n");
sb
.append("\tbInterfaceProtocol: 0x"
+ Integer.toHexString(intDesc
.getBInterfaceProtocol() & 0xFF) + "\n");
sb.append("\tiInterface: 0x"
+ Integer.toHexString(intDesc.getIInterface()) + "\n");
sb.append("\textralen: 0x"
+ Integer.toHexString(intDesc.getExtralen()) + "\n");
sb.append("\textra: " + extraDescriptorToString(intDesc.getExtra()) + "\n");
// get device handle to retrieve string descriptors
Usb_Bus bus = rootBus;
while (bus != null) {
Usb_Device dev = bus.getDevices();
while (dev != null) {
Usb_Config_Descriptor[] confDescs = dev.getConfig();
for (int i = 0; i < confDescs.length; i++) {
Usb_Interface[] ints = confDescs[i].getInterface();
for (int j = 0; j < ints.length; j++) {
Usb_Interface_Descriptor[] tmpIntDescs = ints[j]
.getAltsetting();
for (int k = 0; k < ints.length; k++) {
if (i < tmpIntDescs.length && tmpIntDescs[i].equals(intDesc)
&& (intDesc.getIInterface() > 0)) {
long handle = LibusbJava.usb_open(dev);
sb.append("\nString descriptors\n");
if (handle <= 0) {
sb
.append("\terror opening the device\n");
break;
}
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);
}
}
}
}
dev = dev.getNext();
}
bus = bus.getNext();
}
textArea.setText(sb.toString());
} else if (component instanceof Usb_Endpoint_Descriptor) {
Usb_Endpoint_Descriptor epDesc = (Usb_Endpoint_Descriptor) component;
StringBuffer sb = new StringBuffer("Usb_Endpoint_Descriptor\n");
sb.append("\tblenght: 0x"
+ Integer.toHexString(epDesc.getBLength() & 0xFF) + "\n");
sb.append("\tbDescriptorType: 0x"
+ Integer.toHexString(epDesc.getBDescriptorType() & 0xFF)
+ "\n");
sb.append("\tbEndpointAddress: 0x"
+ Integer.toHexString(epDesc.getBEndpointAddress() & 0xFF)
+ "\n");
sb.append("\tbmAttributes: 0x"
+ Integer.toHexString(epDesc.getBmAttributes() & 0xFF)
+ "\n");
sb.append("\twMaxPacketSize: 0x"
+ Integer.toHexString(epDesc.getWMaxPacketSize() & 0xFFFF)
+ " (" + epDesc.getWMaxPacketSize() + ")\n");
sb.append("\tbInterval: 0x"
+ Integer.toHexString(epDesc.getBInterval() & 0xFF) + "\n");
sb.append("\tbRefresh: 0x"
+ Integer.toHexString(epDesc.getBRefresh() & 0xFF) + "\n");
sb.append("\tbSynchAddress: 0x"
+ Integer.toHexString(epDesc.getBSynchAddress()) + "\n");
sb.append("\textralen: 0x"
+ Integer.toHexString(epDesc.getExtralen()) + "\n");
sb.append("\textra: " + extraDescriptorToString(epDesc.getExtra()) + "\n");
textArea.setText(sb.toString());
}
}
private String extraDescriptorToString(byte[] extra) {
if (extra != null) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < extra.length; i++) {
sb.append("0x");
sb.append(Integer.toHexString(extra[i] & 0xff));
sb.append(' ');
}
return sb.toString();
}
return null;
}
}

View File

@@ -0,0 +1,403 @@
/*
* Java libusb wrapper
* Copyright (c) 2005-2006 Andreas Schl<68>pfer <spandi at users.sourceforge.net>
*
* http://libusbjava.sourceforge.net
* This library is covered by the LGPL, read LGPL.txt for details.
*/
package ch.ntb.inf.libusbJava.usbView;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
import javax.swing.JTree;
import javax.swing.KeyStroke;
import javax.swing.tree.TreePath;
import ch.ntb.inf.libusbJava.LibusbJava;
import ch.ntb.inf.libusbJava.Usb_Bus;
import ch.ntb.inf.libusbJava.Usb_Config_Descriptor;
import ch.ntb.inf.libusbJava.Usb_Device;
import ch.ntb.inf.libusbJava.Usb_Device_Descriptor;
import ch.ntb.inf.libusbJava.Usb_Endpoint_Descriptor;
import ch.ntb.inf.libusbJava.Usb_Interface_Descriptor;
import ch.ntb.inf.libusbJava.testApp.TestApp;
import ch.ntb.inf.libusbJava.testApp.TestDevice;
import ch.ntb.inf.libusbJava.testApp.AbstractDeviceInfo.TransferMode;
public class UsbView extends JFrame {
private static final long serialVersionUID = 4693554326612734263L;
private static final int APP_WIDTH = 600, APP_HIGHT = 800;
private JPanel jContentPane = null;
private JMenuBar jJMenuBar = null;
private JMenu commandsMenu = null;
private JMenuItem exitMenuItem = null;
private JMenuItem updateMenuItem = null;
JTree usbTree = null;
private JSplitPane jSplitPane = null;
private JTextArea jPropertiesArea = null;
UsbTreeModel treeModel;
JPopupMenu testAppPopup;
protected JPopupMenu endpointPopup;
/**
* This is the default constructor
*/
public UsbView() {
super();
initialize();
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setJMenuBar(getJJMenuBar());
this.setSize(APP_WIDTH, APP_HIGHT);
this.setContentPane(getJContentPane());
this.setTitle("USB View");
}
/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(new BorderLayout());
jContentPane.add(getJSplitPane(), java.awt.BorderLayout.CENTER);
}
return jContentPane;
}
/**
* This method initializes jJMenuBar
*
* @return javax.swing.JMenuBar
*/
private JMenuBar getJJMenuBar() {
if (jJMenuBar == null) {
jJMenuBar = new JMenuBar();
jJMenuBar.add(getFileMenu());
}
return jJMenuBar;
}
/**
* This method initializes jMenu
*
* @return javax.swing.JMenu
*/
private JMenu getFileMenu() {
if (commandsMenu == null) {
commandsMenu = new JMenu();
commandsMenu.setText("Commands");
commandsMenu.add(getUpdateMenuItem());
commandsMenu.add(getExitMenuItem());
}
return commandsMenu;
}
/**
* This method initializes jMenuItem
*
* @return javax.swing.JMenuItem
*/
private JMenuItem getExitMenuItem() {
if (exitMenuItem == null) {
exitMenuItem = new JMenuItem();
exitMenuItem.setText("Exit");
exitMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
}
return exitMenuItem;
}
/**
* This method initializes jMenuItem
*
* @return javax.swing.JMenuItem
*/
private JMenuItem getUpdateMenuItem() {
if (updateMenuItem == null) {
updateMenuItem = new JMenuItem();
updateMenuItem.setText("Update");
updateMenuItem.setAccelerator(KeyStroke.getKeyStroke(
KeyEvent.VK_F5, 0, true));
updateMenuItem
.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
// open bus
LibusbJava.usb_init();
LibusbJava.usb_find_busses();
LibusbJava.usb_find_devices();
Usb_Bus bus = LibusbJava.usb_get_busses();
if (bus != null) {
treeModel.fireTreeStructureChanged(bus);
expandAll(usbTree);
}
}
});
}
return updateMenuItem;
}
/**
* This method initializes usbTree
*
* @return javax.swing.JTree
*/
private JTree getUsbTree() {
if (usbTree == null) {
// open bus
LibusbJava.usb_init();
LibusbJava.usb_find_busses();
LibusbJava.usb_find_devices();
Usb_Bus bus = LibusbJava.usb_get_busses();
treeModel = new UsbTreeModel(bus, jPropertiesArea);
usbTree = new JTree(treeModel);
expandAll(usbTree);
usbTree.addTreeSelectionListener(treeModel);
getJTestAppPopup();
usbTree.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
if (e.isPopupTrigger()) {
Object source = e.getSource();
if (source instanceof JTree) {
JTree tree = (JTree) source;
TreePath path = tree.getPathForLocation(e.getX(), e
.getY());
if (path != null
&& (path.getLastPathComponent() instanceof Usb_Interface_Descriptor)) {
usbTree.setSelectionPath(path);
testAppPopup.show(tree, e.getX(), e.getY());
}
}
}
}
@Override
public void mouseReleased(MouseEvent e) {
if (e.isPopupTrigger()) {
if (e.isPopupTrigger()) {
Object source = e.getSource();
if (source instanceof JTree) {
JTree tree = (JTree) source;
TreePath path = tree.getPathForLocation(e
.getX(), e.getY());
if (path != null
&& (path.getLastPathComponent() instanceof Usb_Interface_Descriptor)) {
usbTree.setSelectionPath(path);
testAppPopup.show(tree, e.getX(), e.getY());
}
}
}
}
}
});
}
return usbTree;
}
private void getJTestAppPopup() {
// Create the popup menu.
testAppPopup = new JPopupMenu();
endpointPopup = new JPopupMenu();
JMenuItem menuItem = new JMenuItem(
"Start a test application using this interface");
menuItem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
initAndStartTestApp();
}
private void initAndStartTestApp() {
JTree tree = (JTree) testAppPopup.getInvoker();
TreePath path = tree.getSelectionPath();
TreePath parent = path;
Usb_Endpoint_Descriptor[] endpoints = null;
int altinterface = -1;
int interface_ = -1;
int configuration = -1;
short vendorId = -1;
short productId = -1;
while (parent != null
&& !(parent.getLastPathComponent() instanceof Usb_Bus)) {
Object usbObj = parent.getLastPathComponent();
if (usbObj instanceof Usb_Interface_Descriptor) {
Usb_Interface_Descriptor usbIntDesc = (Usb_Interface_Descriptor) usbObj;
endpoints = usbIntDesc.getEndpoint();
interface_ = usbIntDesc.getBInterfaceNumber();
altinterface = usbIntDesc.getBAlternateSetting();
} else if (usbObj instanceof Usb_Config_Descriptor) {
configuration = ((Usb_Config_Descriptor) usbObj)
.getBConfigurationValue();
} else if (usbObj instanceof Usb_Device) {
Usb_Device_Descriptor devDesc = ((Usb_Device) usbObj)
.getDescriptor();
productId = devDesc.getIdProduct();
vendorId = devDesc.getIdVendor();
}
parent = parent.getParentPath();
}
if (parent != null) {
// present a dialog to select in/out endpoint
// TODO: present dialog to select in/out endpoint
Usb_Endpoint_Descriptor[] outEPs = null;
int nofOutEPs = 0;
Usb_Endpoint_Descriptor[] inEPs = null;
int nofInEPs = 0;
if (endpoints != null) {
outEPs = new Usb_Endpoint_Descriptor[endpoints.length];
inEPs = new Usb_Endpoint_Descriptor[endpoints.length];
for (int i = 0; i < endpoints.length; i++) {
int epAddr = endpoints[i].getBEndpointAddress() & 0xFF;
if ((epAddr & 0x80) > 0) {
// is IN endpoint
inEPs[nofInEPs++] = endpoints[i];
} else {
// is OUT endpoint
outEPs[nofOutEPs++] = endpoints[i];
}
}
}
// create a new TestDevice
TestDevice testDevice = new TestDevice();
testDevice.setIdProduct(productId);
testDevice.setIdVendor(vendorId);
testDevice.setAltinterface(altinterface);
testDevice.setConfiguration(configuration);
testDevice.setInterface(interface_);
if (inEPs != null) {
for (int i = 0; i < nofInEPs; i++) {
int type = inEPs[i].getBmAttributes() & 0x03;
switch (type) {
case Usb_Endpoint_Descriptor.USB_ENDPOINT_TYPE_BULK:
testDevice.setInEPBulk(inEPs[i]
.getBEndpointAddress() & 0xff);
testDevice.setInMode(TransferMode.Bulk);
break;
case Usb_Endpoint_Descriptor.USB_ENDPOINT_TYPE_INTERRUPT:
testDevice.setInEPInt(inEPs[i]
.getBEndpointAddress() & 0xff);
testDevice.setInMode(TransferMode.Interrupt);
break;
default:
break;
}
}
}
if (outEPs != null) {
for (int i = 0; i < nofOutEPs; i++) {
int type = outEPs[i].getBmAttributes() & 0x03;
switch (type) {
case Usb_Endpoint_Descriptor.USB_ENDPOINT_TYPE_BULK:
testDevice.setOutEPBulk(outEPs[i]
.getBEndpointAddress() & 0xff);
testDevice.setOutMode(TransferMode.Bulk);
break;
case Usb_Endpoint_Descriptor.USB_ENDPOINT_TYPE_INTERRUPT:
testDevice.setOutEPInt(outEPs[i]
.getBEndpointAddress() & 0xff);
testDevice.setOutMode(TransferMode.Interrupt);
break;
default:
break;
}
}
}
// open a new testApp
TestApp app = new TestApp(testDevice);
app.setVisible(true);
} else {
System.out.println("error, could not find device node");
// TODO: handle error
}
}
});
testAppPopup.add(menuItem);
}
/**
* This method initializes jSplitPane
*
* @return javax.swing.JSplitPane
*/
private JSplitPane getJSplitPane() {
if (jSplitPane == null) {
jSplitPane = new JSplitPane();
jSplitPane.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);
jSplitPane.setContinuousLayout(true);
jSplitPane.setDividerLocation(APP_HIGHT / 2);
jSplitPane
.setBottomComponent(createScrollPane(getJPropertiesArea()));
jSplitPane.setTopComponent(createScrollPane(getUsbTree()));
}
return jSplitPane;
}
/**
* This method initializes jPropertiesArea
*
* @return javax.swing.JTextArea
*/
private JTextArea getJPropertiesArea() {
if (jPropertiesArea == null) {
jPropertiesArea = new JTextArea();
}
return jPropertiesArea;
}
private JScrollPane createScrollPane(Component view) {
JScrollPane scrollPane = new JScrollPane(view);
return scrollPane;
}
/**
* Launches this application
*/
public static void main(String[] args) {
UsbView application = new UsbView();
application.setVisible(true);
}
void expandAll(JTree tree) {
for (int row = 0; row < tree.getRowCount(); row++) {
tree.expandRow(row);
}
}
}

View File

@@ -0,0 +1,17 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<html>
<head></head>
<body bgcolor="white">
Displays the bus and device information of the currently attached
devices in a tree (based on Swing).
<h2>Related Resources</h2>
For more information about this project visit
<a
href="http://libusbjava.sourceforge.net">http://libusbjava.sourceforge.net</a>
.
</body>
</html>