- descriptors extended from Usb_Descriptor

- toString()
- javadoc added
- interface changes (Strings, dll)

git-svn-id: https://svn.code.sf.net/p/libusbjava/code/trunk@147 94ad28fe-ef68-46b1-9651-e7ae4fcf1c4c
This commit is contained in:
schlaepfer
2006-05-10 13:15:33 +00:00
parent 5d69fa84bf
commit 2ae9ca02d7
10 changed files with 391 additions and 167 deletions

View File

@@ -1,7 +1,8 @@
package ch.ntb.usb;
/**
* This class represents the Java Native Interface to the LibUsbWin.dll.<br>
* This class represents the Java Native Interface to the LibUsbWin.dll which is
* (with some exceptions) a one-to-one representation of the libusb API.<br>
* <br>
* <h1>Project Description</h1>
* Java LibUsb-Win32 is a Java wrapper for the LibUsb-Win32 USB library. <a
@@ -9,9 +10,13 @@ package ch.ntb.usb;
* 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.
* 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>.<br>
*
* @author schlaepfer
* @version DLL: 00.02.00
*
*/
public class LibusbWin {
@@ -184,12 +189,10 @@ public class LibusbWin {
* The handle to the device.
* @param index
* @param langid
* @param buf
* @param buflen
* @return the number of bytes returned in buf or < 0 on error.
* @return the descriptor String or null
*/
public static native int usb_get_string(int dev_handle, int index,
int langid, String buf, int buflen);
public static native String usb_get_string(int dev_handle, int index,
int langid);
/**
* <code>usb_get_string_simple</code> is a wrapper around
@@ -199,30 +202,28 @@ public class LibusbWin {
* @param dev_handle
* The handle to the device.
* @param index
* @param buf
* @param buflen
* @return the number of bytes returned in buf or < 0 on error.
* @return the descriptor String or null
*/
public static native int usb_get_string_simple(int dev_handle, int index,
String buf, int buflen);
public static native String usb_get_string_simple(int 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 <code>usb_get_descriptor_by_endpoint</code> for a function that
* allows the control endpoint to be specified.
* See {@link #usb_get_descriptor_by_endpoint(int, 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 buf
* @param size
* @return the number of bytes read for the descriptor or < 0 on error.
* number of charactes which will be retrieved (the length of the
* resulting String)
* @return the descriptor String or null
*/
public static native int usb_get_descriptor(int dev_handle, byte type,
byte index, String buf, int size);
public static native String usb_get_descriptor(int dev_handle, byte type,
byte index, int size);
/**
* Retrieves a descriptor from the device identified by the type and index
@@ -233,12 +234,13 @@ public class LibusbWin {
* @param ep
* @param type
* @param index
* @param buf
* @param size
* @return the number of bytes read for the descriptor or < 0 on error.
* number of charactes which will be retrieved (the length of the
* resulting String)
* @return the descriptor String or null
*/
public static native int usb_get_descriptor_by_endpoint(int dev_handle,
int ep, byte type, byte index, String buf, int size);
public static native String usb_get_descriptor_by_endpoint(int dev_handle,
int ep, byte type, byte index, int size);
// Bulk Transfers
/**

View File

@@ -7,7 +7,7 @@ import ch.ntb.usb.logger.LogUtil;
import ch.ntb.usb.logger.UsbLogger;
/**
* This class manages all USB devices and defines some USB specific constants.
* This class manages all USB devices and defines some USB specific constants.<br>
*
* @author schlaepfer
*

View File

@@ -1,23 +1,38 @@
package ch.ntb.usb;
/**
* 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.
*
* @author schlaepfer
*
*/
public class Usb_Bus {
/**
* The next and previous bus object
*/
public Usb_Bus next, prev;
/**
* Systems String representation of the bus
*/
public String dirname;
/**
* Device objects attached to this bus
*/
public Usb_Device devices;
/**
* Location in the USB bus linked list
*/
public long location;
public Usb_Device root_dev;
public String toString() {
StringBuffer sb = new StringBuffer();
sb.append("** Usb_Bus **\n");
sb.append("\tdirname: " + dirname + "\n");
sb.append("\tlocation: " + location + "\n");
return sb.toString();
}
public String toString() {
return "Usb_Bus " + dirname;
}
}

View File

@@ -1,44 +1,78 @@
package ch.ntb.usb;
public class Usb_Config_Descriptor {
/**
* 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.usb.Usb_Descriptor#USB_DT_CONFIG_SIZE} and the type is
* {@link ch.ntb.usb.Usb_Descriptor#USB_DT_CONFIG}.
*
* @author schlaepfer
*
*/
public class Usb_Config_Descriptor extends Usb_Descriptor {
/**
* Maximum number of configuration per device
*/
public static final int USB_MAXCONFIG = 8;
public byte bLength;
public byte bDescriptorType;
/**
* Total length in bytes of data returned.<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.
*/
public short wTotalLength;
/**
* Number of interfaces
*/
public byte bNumInterfaces;
/**
* Value to use as an argument to select this configuration ({@link LibusbWin#usb_set_configuration(int, int)}).
*/
public byte bConfigurationValue;
/**
* Index of String descriptor describing this configuration
*/
public byte iConfiguration;
/**
* Specifies 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
*/
public byte bmAttributes;
/**
* Maximum power consumption in 2mA units
*/
public byte MaxPower;
/**
* USB interface descriptors
*/
public Usb_Interface[] interface_;
// TODO: Extra descriptors are not interpreted because of their unknown
// structure
/**
* Extra descriptors are currently not interpreted because of their unknown
* structure.
*/
public Usb_Config_Descriptor extra; /* Extra descriptors */
// TODO
public int extralen;
public String toString() {
StringBuffer sb = new StringBuffer();
sb.append("** Usb_Config_Descriptor **\n");
sb.append("\tblenght: " + bLength + "\n");
sb.append("\tbDescriptorType: " + bDescriptorType + "\n");
sb.append("\tbNumInterfaces: " + bNumInterfaces + "\n");
sb.append("\tbConfigurationValue: " + bConfigurationValue + "\n");
sb.append("\tiConfiguration: " + iConfiguration + "\n");
sb.append("\tbmAttributes: 0x"
+ Integer.toHexString(bmAttributes & 0xFF) + "\n");
sb.append("\tMaxPower [mA]: " + MaxPower + "\n");
return sb.toString();
return "Usb_Config_Descriptor bNumInterfaces: 0x"
+ Integer.toHexString(bNumInterfaces);
}
}

View File

@@ -0,0 +1,42 @@
package ch.ntb.usb;
/**
* Common USB descriptor values.<br>
*
* @author schlaepfer
*
*/
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;
/**
* Size of descriptor in bytes.
*/
public byte bLength;
/**
* Type of descriptor.
*/
public byte bDescriptorType;
}

View File

@@ -1,29 +1,57 @@
package ch.ntb.usb;
/**
* Represents an USB device.<br>
* An USB device has one device descriptor and it may have multiple
* configuration descriptors.
*
* @author schlaepfer
*
*/
public class Usb_Device {
/**
* Pointers to the next and previous device
*/
public Usb_Device next, prev;
/**
* Systems String representation
*/
public String filename;
/**
* Reference to the bus to which this device is connected
*/
public Usb_Bus bus;
/**
* USB device descriptor
*/
public Usb_Device_Descriptor descriptor;
/**
* USB config descriptors
*/
public Usb_Config_Descriptor[] config;
/**
* Number assigned to this device
*/
public byte devnum;
/**
* Number of children of this device
*/
public byte num_children;
/**
* Reference to the first child
*/
public Usb_Device children;
public String toString() {
StringBuffer sb = new StringBuffer();
sb.append("** Usb_Device **\n");
sb.append("\tfilename: " + filename + "\n");
sb.append("\tdevnum: " + devnum + "\n");
sb.append("\tnum_children: " + num_children + "\n");
return sb.toString();
return "Usb_Device " + filename;
}
}

View File

@@ -1,91 +1,105 @@
package ch.ntb.usb;
public class Usb_Device_Descriptor {
/*
* Device and/or Interface Class codes
/**
* 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.usb.Usb_Descriptor#USB_DT_DEVICE_SIZE} and the type is
* {@link ch.ntb.usb.Usb_Descriptor#USB_DT_DEVICE}.
*
* @author schlaepfer
*
*/
public class Usb_Device_Descriptor extends Usb_Descriptor {
/**
* Device and/or interface class codes.
*/
public static final int USB_CLASS_PER_INTERFACE = 0;
public static final int USB_CLASS_AUDIO = 1;
public static final int USB_CLASS_COMM = 2;
public static final int USB_CLASS_HID = 3;
public static final int USB_CLASS_PRINTER = 7;
public static final int USB_CLASS_MASS_STORAGE = 8;
public static final int USB_CLASS_HUB = 9;
public static final int USB_CLASS_DATA = 10;
public static final int USB_CLASS_VENDOR_SPEC = 0xff;
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;
/*
* Descriptor types
/**
* 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
*/
public static final int USB_DT_DEVICE = 0x01;
public static final int USB_DT_CONFIG = 0x02;
public static final int USB_DT_STRING = 0x03;
public static final int USB_DT_INTERFACE = 0x04;
public static final int USB_DT_ENDPOINT = 0x05;
public static final int USB_DT_HID = 0x21;
public static final int USB_DT_REPORT = 0x22;
public static final int USB_DT_PHYSICAL = 0x23;
public static final int USB_DT_HUB = 0x29;
/*
* Descriptor sizes per descriptor type
*/
public static final int USB_DT_DEVICE_SIZE = 18;
public static final int USB_DT_CONFIG_SIZE = 9;
public static final int USB_DT_INTERFACE_SIZE = 9;
public static final int USB_DT_ENDPOINT_SIZE = 7;
public static final int USB_DT_ENDPOINT_AUDIO_SIZE = 9; /* Audio extension */
public static final int USB_DT_HUB_NONVAR_SIZE = 7;
public byte bLength;
public byte bDescriptorType;
public short bcdUSB;
/**
* 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.
*/
public byte bDeviceClass;
/**
* Subclass code (Assigned by <a href="http://www.usb.org">www.usb.org</a>)<br>
*/
public byte bDeviceSubClass;
/**
* Protocol code (Assigned by <a href="http://www.usb.org">www.usb.org</a>)<br>
*/
public byte bDeviceProtocol;
/**
* Maximum packet size for endpoint zero. <br>
* Valid sizes are 8, 16, 32, 64.
*/
public byte bMaxPacketSize0;
/**
* Vendor ID (Assigned by <a href="http://www.usb.org">www.usb.org</a>)<br>
*/
public short idVendor;
/**
* Product ID (Assigned by <a href="http://www.usb.org">www.usb.org</a>)<br>
*/
public short idProduct;
/**
* Device release number<br>
* Assigned by the manufacturer of the device.
*/
public short bcdDevice;
/**
* Index of manufacturer string descriptor<br>
* If this value is 0, no string descriptor is used.
*/
public byte iManufacturer;
/**
* Index of product string descriptor<br>
* If this value is 0, no string descriptor is used.
*/
public byte iProduct;
/**
* Index of serial number string descriptor<br>
* If this value is 0, no string descriptor is used.
*/
public byte iSerialNumber;
/**
* Number of possible configurations supported at its current speed
*/
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: 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: 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");
sb.append("\tiSerialNumber: " + iSerialNumber + "\n");
sb.append("\tbNumConfigurations: " + bNumConfigurations + "\n");
sb.append("Usb_Device_Descriptor idVendor: 0x"
+ Integer.toHexString(idVendor & 0xFFFF) + ", idProduct: 0x"
+ Integer.toHexString(idProduct & 0xFFFF));
return sb.toString();
}
};

View File

@@ -1,54 +1,102 @@
package ch.ntb.usb;
public class Usb_Endpoint_Descriptor {
/**
* Represents the descriptor of a 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.usb.Usb_Descriptor#USB_DT_ENDPOINT_SIZE} and the type is
* {@link ch.ntb.usb.Usb_Descriptor#USB_DT_ENDPOINT}.
*
* @author schlaepfer
*
*/
public class Usb_Endpoint_Descriptor extends Usb_Descriptor {
/**
* Maximum number of endpoints
*/
public static final int USB_MAXENDPOINTS = 32;
/* in bEndpointAddress */
public static final int USB_ENDPOINT_ADDRESS_MASK = 0x0f;
public static final int USB_ENDPOINT_DIR_MASK = 0x80;
/**
* Endpoint address mask (in bEndpointAddress).
*/
public static final int USB_ENDPOINT_ADDRESS_MASK = 0x0f,
USB_ENDPOINT_DIR_MASK = 0x80;
/* in bmAttributes */
/**
* Endpoint type mask (in bmAttributes).
*/
public static final int USB_ENDPOINT_TYPE_MASK = 0x03;
public static final int USB_ENDPOINT_TYPE_CONTROL = 0;
public static final int USB_ENDPOINT_TYPE_ISOCHRONOUS = 1;
public static final int USB_ENDPOINT_TYPE_BULK = 2;
public static final int USB_ENDPOINT_TYPE_INTERRUPT = 3;
public byte bLength;
public byte bDescriptorType;
/**
* 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;
/**
* Endpoint Address<br>
* <br>
* Bits 3..0: Endpoint number <br>
* Bits 6..4: Reserved. Set to zero <br>
* Bit 7: Direction. 0 = Out, 1 = In (ignored for control endpoints)<br>
*/
public byte bEndpointAddress;
/**
* 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>
*/
public byte bmAttributes;
/**
* Maximum packet size this endpoint is capable of sending or receiving
*/
public short wMaxPacketSize;
/**
* 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.
*/
public byte bInterval;
public byte bRefresh;
public byte bSynchAddress;
// TODO: Extra descriptors are not interpreted because of their unknown
// structure
/**
* Extra descriptors are currently not interpreted because of their unknown
* structure.
*/
public Usb_Endpoint_Descriptor extra; /* Extra descriptors */
// TODO
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: 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");
sb.append("\tbSynchAddress: " + bSynchAddress + "\n");
return sb.toString();
return "Usb_Endpoint_Descriptor bEndpointAddress: 0x"
+ Integer.toHexString(bEndpointAddress);
}
}

View File

@@ -1,17 +1,32 @@
package ch.ntb.usb;
/**
* Represents an USB interface.<br>
* An interface is a group of alternate settings of a configuration.<br>
*
* @author schlaepfer
*
*/
public class Usb_Interface {
/**
* Maximal number of alternate settings
*/
public static final int USB_MAXALTSETTING = 128; /* Hard limit */
/**
* Interface descriptors
*/
public Usb_Interface_Descriptor[] altsetting;
/**
* Number of alternate settings
*/
public int num_altsetting;
public String toString() {
StringBuffer sb = new StringBuffer();
sb.append("** Usb_Interface **\n");
sb.append("\tnum_altsetting: " + num_altsetting + "\n");
return sb.toString();
return "Usb_Interface num_altsetting: 0x"
+ Integer.toHexString(num_altsetting);
}
}

View File

@@ -1,49 +1,75 @@
package ch.ntb.usb;
public class Usb_Interface_Descriptor {
/**
* 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.usb.Usb_Descriptor#USB_DT_INTERFACE_SIZE} and the type is
* {@link ch.ntb.usb.Usb_Descriptor#USB_DT_INTERFACE}.
*
* @author schlaepfer
*
*/
public class Usb_Interface_Descriptor extends Usb_Descriptor {
/**
* Maximal number of interfaces
*/
public static final int USB_MAXINTERFACES = 32;
public byte bLength;
public byte bDescriptorType;
/**
* Number of interface
*/
public byte bInterfaceNumber;
/**
* Value used to select alternate setting ({@link LibusbWin#usb_set_altinterface(int, int)}).
*/
public byte bAlternateSetting;
/**
* Number of Endpoints used for this interface
*/
public byte bNumEndpoints;
/**
* Class code (Assigned by <a href="http://www.usb.org">www.usb.org</a>)<br>
*/
public byte bInterfaceClass;
/**
* Subclass code (Assigned by <a href="http://www.usb.org">www.usb.org</a>)<br>
*/
public byte bInterfaceSubClass;
/**
* Protocol code (Assigned by <a href="http://www.usb.org">www.usb.org</a>)<br>
*/
public byte bInterfaceProtocol;
/**
* Index of String descriptor describing this interface
*/
public byte iInterface;
/**
* Endpoint descriptors
*/
public Usb_Endpoint_Descriptor[] endpoint;
// TODO: Extra descriptors are not interpreted because of their unknown
// structure
/**
* Extra descriptors are currently not interpreted because of their unknown
* structure.
*/
public Usb_Interface_Descriptor extra; /* Extra descriptors */
// TODO
public int extralen;
public String toString() {
StringBuffer sb = new StringBuffer();
sb.append("** Usb_Interface_Descriptor **\n");
sb.append("\tblenght: " + bLength + "\n");
sb.append("\tbDescriptorType: " + bDescriptorType + "\n");
sb.append("\tbInterfaceNumber: " + bInterfaceNumber + "\n");
sb.append("\tbAlternateSetting: " + bAlternateSetting + "\n");
sb.append("\tbNumEndpoints: " + bNumEndpoints + "\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();
return "Usb_Interface_Descriptor bNumEndpoints: 0x"
+ Integer.toHexString(bNumEndpoints);
}
}