- structural changes (Device as instance)

git-svn-id: https://svn.code.sf.net/p/libusbjava/code/trunk@100 94ad28fe-ef68-46b1-9651-e7ae4fcf1c4c
This commit is contained in:
schlaepfer
2006-02-17 17:04:04 +00:00
parent 718f9fb4c5
commit 27bfb3683f
8 changed files with 234 additions and 104 deletions

View File

@@ -5,23 +5,46 @@ import java.util.logging.Level;
import ch.ntb.usb.logger.LogUtil;
import ch.ntb.usb.logger.UsbLogger;
/**
* This class represents an USB device.<br>
* To get an instance of an USB device use <code>USB.getDevice(...)</code>.
*
* @author schlaepfer
*
*/
public class Device {
private static UsbLogger logger = LogUtil.ch_ntb_usb;
private static final int TIMEOUT_ERROR_CODE = -116;
public int MAX_DATA_SIZE = USB.MAX_DATA_SIZE;
private int maxPacketSize;
private int idVendor, idProduct, configuration, interface_, altinterface;
private int usb_dev_handle;
Device(short idVendor, short idProduct) {
protected Device(short idVendor, short idProduct) {
maxPacketSize = -1;
this.idVendor = idVendor;
this.idProduct = idProduct;
}
/**
* 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 desired configuration
* @param interface_
* the desired interface
* @param altinterface
* the desired alternative interface
* @throws USBException
*/
public void open(int configuration, int interface_, int altinterface)
throws USBException {
this.configuration = configuration;
@@ -44,6 +67,9 @@ public class Device {
throw new USBException("LibusbWin.usb_get_busses(): "
+ LibusbWin.usb_strerror());
}
maxPacketSize = -1;
// search for device
while (bus != null) {
Usb_Device dev = bus.devices;
@@ -59,6 +85,25 @@ public class Device {
+ LibusbWin.usb_strerror());
} else {
usb_dev_handle = res;
// get endpoint wMaxPacketSize
Usb_Config_Descriptor[] confDesc = dev.config;
for (int i = 0; i < confDesc.length; i++) {
Usb_Interface[] int_ = confDesc[i].interface_;
for (int j = 0; j < int_.length; j++) {
Usb_Interface_Descriptor[] intDesc = int_[j].altsetting;
for (int k = 0; k < intDesc.length; k++) {
Usb_Endpoint_Descriptor[] epDesc = intDesc[k].endpoint;
for (int l = 0; l < epDesc.length; l++) {
Math.max(epDesc[l].wMaxPacketSize,
maxPacketSize);
}
}
}
}
if (maxPacketSize <= 0) {
throw new USBException(
"No USB endpoints found. Check the device configuration");
}
}
}
dev = dev.next;
@@ -71,21 +116,21 @@ public class Device {
+ " and idProduct 0x"
+ Integer.toHexString(idProduct & 0xFFFF) + " not found");
}
USB.claim_interface(usb_dev_handle, configuration, interface_,
altinterface);
claim_interface(usb_dev_handle, configuration, interface_, altinterface);
}
public void close() throws USBException {
if (usb_dev_handle <= 0) {
throw new USBException("invalid device handle");
}
USB.release_interface(usb_dev_handle, interface_);
release_interface(usb_dev_handle, interface_);
if (LibusbWin.usb_close(usb_dev_handle) < 0) {
usb_dev_handle = 0;
throw new USBException("LibusbWin.usb_close: "
+ LibusbWin.usb_strerror());
}
usb_dev_handle = 0;
maxPacketSize = -1;
logger.info("device closed");
}
@@ -107,8 +152,23 @@ public class Device {
logger.info("device reset");
}
public int bulkwrite(int out_ep_address, byte[] data, int length, int timeout)
throws USBException {
/**
* 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 length
* length of the data
* @param timeout
* amount of time in ms the device will try to send the data
* until a timeout exception is thrown
* @return the actual number of bytes written
* @throws USBException
*/
public int bulkwrite(int out_ep_address, byte[] data, int length,
int timeout) throws USBException {
if (usb_dev_handle <= 0) {
throw new USBException("invalid device handle");
}
@@ -118,8 +178,8 @@ public class Device {
if (length <= 0) {
throw new USBException("size must be > 0");
}
int lenWritten = LibusbWin.usb_bulk_write(usb_dev_handle, out_ep_address,
data, length, timeout);
int lenWritten = LibusbWin.usb_bulk_write(usb_dev_handle,
out_ep_address, data, length, timeout);
if (lenWritten < 0) {
if (lenWritten == TIMEOUT_ERROR_CODE) {
throw new USBTimeoutException("LibusbWin.usb_bulk_write: "
@@ -141,6 +201,19 @@ public class Device {
return lenWritten;
}
/**
* @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
* @return the actual number of bytes read
* @throws USBException
*/
public int bulkread(int in_ep_address, byte[] data, int size, int timeout)
throws USBException {
if (usb_dev_handle <= 0) {
@@ -152,8 +225,8 @@ public class Device {
if (size <= 0) {
throw new USBException("size must be > 0");
}
int lenRead = LibusbWin.usb_bulk_read(usb_dev_handle, in_ep_address, data,
size, timeout);
int lenRead = LibusbWin.usb_bulk_read(usb_dev_handle, in_ep_address,
data, size, timeout);
if (lenRead < 0) {
if (lenRead == TIMEOUT_ERROR_CODE) {
throw new USBTimeoutException("LibusbWin.usb_bulk_read: "
@@ -175,28 +248,112 @@ public class Device {
return lenRead;
}
/**
* Claim an interface to send and receive USB data.
*
* @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 alternative interface to use
* @throws USBException
* throws an USBException if the action fails
*/
private void claim_interface(int usb_dev_handle, int configuration,
int interface_, int altinterface) throws USBException {
if (LibusbWin.usb_set_configuration(usb_dev_handle, configuration) < 0) {
throw new USBException("LibusbWin.usb_set_configuration: "
+ LibusbWin.usb_strerror());
}
if (LibusbWin.usb_claim_interface(usb_dev_handle, interface_) < 0) {
throw new USBException("LibusbWin.usb_claim_interface: "
+ LibusbWin.usb_strerror());
}
if (LibusbWin.usb_set_altinterface(usb_dev_handle, altinterface) < 0) {
throw new USBException("LibusbWin.usb_set_altinterface: "
+ LibusbWin.usb_strerror());
}
logger.info("interface claimed");
}
/**
* Release a previously claimed interface.
*
* @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(int dev_handle, int interface_)
throws USBException {
if (LibusbWin.usb_release_interface(dev_handle, interface_) < 0) {
throw new USBException("LibusbWin.usb_release_interface: "
+ LibusbWin.usb_strerror());
}
logger.info("interface released");
}
/**
* @return the product ID of the device.
*/
public int getIdProduct() {
return idProduct;
}
/**
* @return the vendor ID of the device.
*/
public int getIdVendor() {
return idVendor;
}
/**
* @return the alternative interface. This value is only valid after opening
* the device.
*/
public int getAltinterface() {
return altinterface;
}
/**
* @return the current configuration used. This value is only valid after
* opening the device.
*/
public int getConfiguration() {
return configuration;
}
/**
* @return the current interface. This value is only valid after opening the
* device.
*/
public int getInterface() {
return interface_;
}
/**
* @return the current device handle. This value is only valid after opening
* the device.
*/
public int getUsb_dev_handle() {
return usb_dev_handle;
}
/**
* 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!
*
* @return the maximum packet size
*/
public int getMaxPacketSize() {
return maxPacketSize;
}
}

View File

@@ -1,8 +1,6 @@
package ch.ntb.usb;
public class LibusbWin {
private static final String sysDir = "C:/Windows";
// Core
/**
@@ -15,8 +13,8 @@ public class LibusbWin {
/**
* <code>usb_find_busses</code> will find all of the busses on the system.
*
* @return Returns the number of changes since previous call to this
* function (total of new busses and busses removed).
* @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();
@@ -24,8 +22,8 @@ public class LibusbWin {
* <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 Returns the number of changes since the previous call to this
* function (total of new device and devices removed).
* @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();
@@ -35,7 +33,7 @@ public class LibusbWin {
* support C calling convention and can use shared libraries, but don't
* support C global variables (like Delphi).
*
* @return The structure of all busses and devices. <b>Note:</b> The java
* @return the structure of all busses and devices. <b>Note:</b> The java
* objects are copies of the C structs.
*/
public static native Usb_Bus usb_get_busses();
@@ -48,7 +46,7 @@ public class LibusbWin {
*
* @param dev
* The device to open.
* @return Returns a handle used in future communication with the device.
* @return a handle used in future communication with the device.
*/
public static native int usb_open(Usb_Device dev);
@@ -108,7 +106,7 @@ public class LibusbWin {
*
* @param dev_handle
* The handle to the device.
* @return Returns 0 on success or < 0 on error.
* @return 0 on success or < 0 on error.
*/
public static native int usb_reset(int dev_handle);
@@ -155,7 +153,7 @@ public class LibusbWin {
* @param bytes
* @param size
* @param timeout
* @return number of bytes written/read or < 0 on error.
* @return the number of bytes written/read or < 0 on error.
*/
public static native int usb_control_msg(int dev_handle, int requesttype,
int request, int value, int index, byte[] bytes, int size,
@@ -171,7 +169,7 @@ public class LibusbWin {
* @param langid
* @param buf
* @param buflen
* @return number of bytes returned in buf or < 0 on error.
* @return the number of bytes returned in buf or < 0 on error.
*/
public static native int usb_get_string(int dev_handle, int index,
int langid, String buf, int buflen);
@@ -186,7 +184,7 @@ public class LibusbWin {
* @param index
* @param buf
* @param buflen
* @return number of bytes returned in buf or < 0 on error.
* @return the number of bytes returned in buf or < 0 on error.
*/
public static native int usb_get_string_simple(int dev_handle, int index,
String buf, int buflen);
@@ -204,7 +202,7 @@ public class LibusbWin {
* @param index
* @param buf
* @param size
* @return number of bytes read for the descriptor or < 0 on error.
* @return the number of bytes read for the descriptor or < 0 on error.
*/
public static native int usb_get_descriptor(int dev_handle, byte type,
byte index, String buf, int size);
@@ -220,7 +218,7 @@ public class LibusbWin {
* @param index
* @param buf
* @param size
* @return number of bytes read for the descriptor or < 0 on error.
* @return the number of bytes read for the descriptor or < 0 on error.
*/
public static native int usb_get_descriptor_by_endpoint(int dev_handle,
int ep, byte type, byte index, String buf, int size);
@@ -235,7 +233,7 @@ public class LibusbWin {
* @param bytes
* @param size
* @param timeout
* @return number of bytes written on success or < 0 on error.
* @return the number of bytes written on success or < 0 on error.
*/
public static native int usb_bulk_write(int dev_handle, int ep,
byte[] bytes, int size, int timeout);
@@ -249,7 +247,7 @@ public class LibusbWin {
* @param bytes
* @param size
* @param timeout
* @return number of bytes read on success or < 0 on error.
* @return the number of bytes read on success or < 0 on error.
*/
public static native int usb_bulk_read(int dev_handle, int ep,
byte[] bytes, int size, int timeout);
@@ -264,7 +262,7 @@ public class LibusbWin {
* @param bytes
* @param size
* @param timeout
* @return number of bytes written on success or < 0 on error.
* @return the number of bytes written on success or < 0 on error.
*/
public static native int usb_interrupt_write(int dev_handle, int ep,
byte[] bytes, int size, int timeout);
@@ -278,7 +276,7 @@ public class LibusbWin {
* @param bytes
* @param size
* @param timeout
* @return number of bytes read on success or < 0 on error.
* @return the number of bytes read on success or < 0 on error.
*/
public static native int usb_interrupt_read(int dev_handle, int ep,
byte[] bytes, int size, int timeout);
@@ -286,13 +284,13 @@ public class LibusbWin {
/**
* Returns the error string after an error occured.
*
* @return error sring.
* @return the last error sring.
*/
public static native String usb_strerror();
/*******************************************************************/
/** **************************************************************** */
static {
System.load(sysDir + "/system32/LibusbWin.dll");
System.loadLibrary("LibusbWin.dll");
}
}

View File

@@ -6,13 +6,25 @@ import java.util.LinkedList;
import ch.ntb.usb.logger.LogUtil;
import ch.ntb.usb.logger.UsbLogger;
/**
* This class manages all USB devices and defines some USB specific constants.
*
* @author schlaepfer
*
*/
public class USB {
/**
* The maximal data size in bytes which is allowed to be transmitted at
* once.
* The maximum packet size of a bulk transfer when operation in highspeed
* (480 MB/s) mode.
*/
public static final int MAX_DATA_SIZE = 512;
public static int HIGHSPEED_MAX_BULK_PACKET_SIZE = 512;
/**
* The maximum packet size of a bulk transfer when operation in fullspeed
* (12 MB/s) mode.
*/
public static int FULLSPEED_MAX_BULK_PACKET_SIZE = 64;
private static UsbLogger logger = LogUtil.ch_ntb_usb;
@@ -33,9 +45,11 @@ public class USB {
// check if this device is already registered
Device dev = getRegisteredDevice(idVendor, idProduct);
if (dev != null) {
logger.info("return already registered device");
return dev;
}
dev = new Device(idVendor, idProduct);
logger.info("create new device");
devices.add(dev);
return dev;
}
@@ -59,54 +73,4 @@ public class USB {
}
return null;
}
/**
* Claim an interface to send and receive USB data.
*
* @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 alternative interface to use
* @throws USBException
* throws an USBException if the action fails
*/
static void claim_interface(int usb_dev_handle, int configuration,
int interface_, int altinterface) throws USBException {
if (LibusbWin.usb_set_configuration(usb_dev_handle, configuration) < 0) {
throw new USBException("LibusbWin.usb_set_configuration: "
+ LibusbWin.usb_strerror());
}
if (LibusbWin.usb_claim_interface(usb_dev_handle, interface_) < 0) {
throw new USBException("LibusbWin.usb_claim_interface: "
+ LibusbWin.usb_strerror());
}
if (LibusbWin.usb_set_altinterface(usb_dev_handle, altinterface) < 0) {
throw new USBException("LibusbWin.usb_set_altinterface: "
+ LibusbWin.usb_strerror());
}
logger.info("interface claimed");
}
/**
* Release a previously claimed interface.
*
* @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
*/
static void release_interface(int dev_handle, int interface_)
throws USBException {
if (LibusbWin.usb_release_interface(dev_handle, interface_) < 0) {
throw new USBException("LibusbWin.usb_release_interface: "
+ LibusbWin.usb_strerror());
}
logger.info("interface released");
}
}

View File

@@ -17,9 +17,10 @@ public class Usb_Config_Descriptor {
public byte MaxPower;
public Usb_Interface []interface_;
public Usb_Interface[] interface_;
// TODO: Extra descriptors are not interpreted because of their unknown structure
// TODO: Extra descriptors are not interpreted because of their unknown
// structure
public Usb_Config_Descriptor extra; /* Extra descriptors */
public int extralen;
@@ -32,8 +33,9 @@ public class Usb_Config_Descriptor {
sb.append("\tbNumInterfaces: " + bNumInterfaces + "\n");
sb.append("\tbConfigurationValue: " + bConfigurationValue + "\n");
sb.append("\tiConfiguration: " + iConfiguration + "\n");
sb.append("\tbmAttributes: " + bmAttributes + "\n");
sb.append("\tMaxPower: " + MaxPower + "\n");
sb.append("\tbmAttributes: 0x"
+ Integer.toHexString(bmAttributes & 0xFF) + "\n");
sb.append("\tMaxPower [mA]: " + MaxPower + "\n");
return sb.toString();
}

View File

@@ -28,19 +28,21 @@ public class Usb_Device_Descriptor {
public byte iSerialNumber;
public byte bNumConfigurations;
public String toString() {
StringBuffer sb = new StringBuffer();
sb.append("** Usb_Device_Descriptor **\n");
sb.append("\tblenght: " + bLength + "\n");
sb.append("\tbDescriptorType: " + bDescriptorType + "\n");
sb.append("\tbcdUSB: " + bcdUSB + "\n");
sb.append("\tbcdUSB: 0x" + Integer.toHexString(bcdUSB) + "\n");
sb.append("\tbDeviceClass: " + bDeviceClass + "\n");
sb.append("\tbDeviceSubClass: " + bDeviceSubClass + "\n");
sb.append("\tbDeviceProtocol: " + bDeviceProtocol + "\n");
sb.append("\tbMaxPacketSize0: " + bMaxPacketSize0 + "\n");
sb.append("\tidVendor: " + idVendor + "\n");
sb.append("\tidProduct: " + idProduct + "\n");
sb.append("\tidVendor: 0x" + Integer.toHexString(idVendor & 0xFFFF)
+ "\n");
sb.append("\tidProduct: 0x" + Integer.toHexString(idProduct & 0xFFFF)
+ "\n");
sb.append("\tbcdDevice: " + bcdDevice + "\n");
sb.append("\tiManufacturer: " + iManufacturer + "\n");
sb.append("\tiProduct: " + iProduct + "\n");

View File

@@ -17,18 +17,21 @@ public class Usb_Endpoint_Descriptor {
public byte bSynchAddress;
// TODO: Extra descriptors are not interpreted because of their unknown structure
// TODO: Extra descriptors are not interpreted because of their unknown
// structure
public Usb_Endpoint_Descriptor extra; /* Extra descriptors */
public int extralen;
public String toString() {
StringBuffer sb = new StringBuffer();
sb.append("**Usb_Endpoint_Descriptor**\n");
sb.append("\tblenght: " + bLength + "\n");
sb.append("\tbDescriptorType: " + bDescriptorType + "\n");
sb.append("\tbEndpointAddress: " + bEndpointAddress + "\n");
sb.append("\tbmAttributes: " + bmAttributes + "\n");
sb.append("\tbEndpointAddress: 0x"
+ Integer.toHexString(bEndpointAddress & 0xFF) + "\n");
sb.append("\tbmAttributes: 0x"
+ Integer.toHexString(bmAttributes & 0xFF) + "\n");
sb.append("\twMaxPacketSize: " + wMaxPacketSize + "\n");
sb.append("\tbInterval: " + bInterval + "\n");
sb.append("\tbRefresh: " + bRefresh + "\n");

View File

@@ -21,7 +21,8 @@ public class Usb_Interface_Descriptor {
public Usb_Endpoint_Descriptor[] endpoint;
// TODO: Extra descriptors are not interpreted because of their unknown structure
// TODO: Extra descriptors are not interpreted because of their unknown
// structure
public Usb_Interface_Descriptor extra; /* Extra descriptors */
public int extralen;
@@ -34,9 +35,12 @@ public class Usb_Interface_Descriptor {
sb.append("\tbInterfaceNumber: " + bInterfaceNumber + "\n");
sb.append("\tbAlternateSetting: " + bAlternateSetting + "\n");
sb.append("\tbNumEndpoints: " + bNumEndpoints + "\n");
sb.append("\tbInterfaceClass: " + bInterfaceClass + "\n");
sb.append("\tbInterfaceSubClass: " + bInterfaceSubClass + "\n");
sb.append("\tbInterfaceProtocol: " + bInterfaceProtocol + "\n");
sb.append("\tbInterfaceClass: 0x"
+ Integer.toHexString(bInterfaceClass & 0xFF) + "\n");
sb.append("\tbInterfaceSubClass: 0x"
+ Integer.toHexString(bInterfaceSubClass & 0xFF) + "\n");
sb.append("\tbInterfaceProtocol: 0x"
+ Integer.toHexString(bInterfaceProtocol & 0xFF) + "\n");
sb.append("\tiInterface: " + iInterface + "\n");
return sb.toString();
}

View File

@@ -82,11 +82,11 @@ public class TestImplementation {
}
static void read() {
byte[] data = new byte[USB.MAX_DATA_SIZE];
byte[] data = new byte[dev.getMaxPacketSize()];
int lenRead = 0;
try {
lenRead = dev.bulkread(IN_ENDPOINT, data,
USB.MAX_DATA_SIZE, TIMEOUT);
lenRead = dev.bulkread(IN_ENDPOINT, data, dev.getMaxPacketSize(),
TIMEOUT);
StringBuffer sb = new StringBuffer("read_bulkdata: " + lenRead
+ " Bytes received: Data: ");
for (int i = 0; i < lenRead; i++) {