new parameter added to be able to open a specific device based on the filename if there are multiple devices with the same vendor/product id
procedure added to get Usb_Bus object from USB class git-svn-id: https://svn.code.sf.net/p/libusbjava/code/trunk@268 94ad28fe-ef68-46b1-9651-e7ae4fcf1c4c
This commit is contained in:
@@ -23,8 +23,18 @@ public class Device {
|
||||
|
||||
private int maxPacketSize;
|
||||
|
||||
private int idVendor, idProduct, dev_configuration, dev_interface,
|
||||
dev_altinterface;
|
||||
/**
|
||||
* 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;
|
||||
|
||||
private int dev_configuration, dev_interface, dev_altinterface;
|
||||
|
||||
private long usbDevHandle;
|
||||
|
||||
@@ -34,32 +44,22 @@ public class Device {
|
||||
|
||||
private Usb_Device dev;
|
||||
|
||||
private boolean initUSBDone;
|
||||
|
||||
protected Device(short idVendor, short idProduct) {
|
||||
initUSBDone = false;
|
||||
resetOnFirstOpen = false;
|
||||
resetDone = false;
|
||||
maxPacketSize = -1;
|
||||
this.idVendor = idVendor;
|
||||
this.idProduct = idProduct;
|
||||
this.filename = null;
|
||||
}
|
||||
|
||||
private void initUSB() {
|
||||
LibusbJava.usb_init();
|
||||
initUSBDone = true;
|
||||
}
|
||||
|
||||
private Usb_Bus initBus() throws USBException {
|
||||
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;
|
||||
protected Device(short idVendor, short idProduct, String filename) {
|
||||
resetOnFirstOpen = false;
|
||||
resetDone = false;
|
||||
maxPacketSize = -1;
|
||||
this.idVendor = idVendor;
|
||||
this.idProduct = idProduct;
|
||||
this.filename = filename;
|
||||
}
|
||||
|
||||
private void updateMaxPacketSize(Usb_Device device) throws USBException {
|
||||
@@ -84,8 +84,14 @@ public class Device {
|
||||
}
|
||||
}
|
||||
|
||||
private Usb_Device initDevice() throws USBException {
|
||||
Usb_Bus bus = initBus();
|
||||
/**
|
||||
* 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 filename) throws USBException {
|
||||
Usb_Bus bus = USB.getBus();
|
||||
|
||||
Usb_Device device = null;
|
||||
// search for device
|
||||
@@ -93,8 +99,17 @@ public class Device {
|
||||
device = bus.getDevices();
|
||||
while (device != null) {
|
||||
Usb_Device_Descriptor devDesc = device.getDescriptor();
|
||||
if ((devDesc.getIdVendor() == idVendor)
|
||||
&& (devDesc.getIdProduct() == idProduct)) {
|
||||
if (filename != null
|
||||
&& filename.compareTo(device.getFilename()) == 0
|
||||
&& devDesc.getIdVendor() == idVendorParam
|
||||
&& devDesc.getIdProduct() == idProductParam) {
|
||||
// idVendor, idProduct and filename
|
||||
logger.info("Device found: " + device.getFilename());
|
||||
updateMaxPacketSize(device);
|
||||
return device;
|
||||
} else if (devDesc.getIdVendor() == idVendorParam
|
||||
&& devDesc.getIdProduct() == idProductParam) {
|
||||
// only idVendor and idProduct
|
||||
logger.info("Device found: " + device.getFilename());
|
||||
updateMaxPacketSize(device);
|
||||
return device;
|
||||
@@ -114,9 +129,7 @@ public class Device {
|
||||
* @throws USBException
|
||||
*/
|
||||
public void updateDescriptors() throws USBException {
|
||||
if (!initUSBDone)
|
||||
initUSB();
|
||||
dev = initDevice();
|
||||
dev = initDevice(idVendor, idProduct, filename);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -175,9 +188,7 @@ public class Device {
|
||||
throw new USBException("device opened, close or reset first");
|
||||
}
|
||||
|
||||
initUSB();
|
||||
|
||||
dev = initDevice();
|
||||
dev = initDevice(idVendor, idProduct, filename);
|
||||
|
||||
if (dev != null) {
|
||||
long res = LibusbJava.usb_open(dev);
|
||||
@@ -724,4 +735,26 @@ public class Device {
|
||||
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)}. Use
|
||||
* {@link Usb_Device#getFilename()} to read the filename of a device.
|
||||
*
|
||||
* @return the filename if set or null
|
||||
*/
|
||||
protected String getFilename() {
|
||||
return filename;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -167,6 +167,8 @@ public class USB {
|
||||
|
||||
private static LinkedList<Device> devices = new LinkedList<Device>();
|
||||
|
||||
private static boolean initUSBDone = false;
|
||||
|
||||
/**
|
||||
* Create a new device an register it in a device queue. If the device is
|
||||
* already registered, a reference to it will be returned.<br>
|
||||
@@ -175,22 +177,38 @@ public class USB {
|
||||
* the vendor id of the USB device
|
||||
* @param idProduct
|
||||
* the product id of the USB device
|
||||
* @param filename
|
||||
* an optional filename which can be used to distinguish multiple
|
||||
* devices with the same vendor and product id.
|
||||
* @return a newly created device or an already registered device
|
||||
*/
|
||||
public static Device getDevice(short idVendor, short idProduct) {
|
||||
public static Device getDevice(short idVendor, short idProduct,
|
||||
String filename) {
|
||||
|
||||
// check if this device is already registered
|
||||
Device dev = getRegisteredDevice(idVendor, idProduct);
|
||||
Device dev = getRegisteredDevice(idVendor, idProduct, filename);
|
||||
if (dev != null) {
|
||||
logger.info("return already registered device");
|
||||
return dev;
|
||||
}
|
||||
dev = new Device(idVendor, idProduct);
|
||||
dev = new Device(idVendor, idProduct, filename);
|
||||
logger.info("create new device");
|
||||
devices.add(dev);
|
||||
return dev;
|
||||
}
|
||||
|
||||
/**
|
||||
* See {@link #getDevice(short, short, String)}. The parameter
|
||||
* <code>filename</code> is set to null.
|
||||
*
|
||||
* @param idVendor
|
||||
* @param idProduct
|
||||
* @return
|
||||
*/
|
||||
public static Device getDevice(short idVendor, short idProduct) {
|
||||
return getDevice(idVendor, idProduct, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an already registered device or null if the device does not exist.<br>
|
||||
*
|
||||
@@ -198,16 +216,55 @@ public class USB {
|
||||
* the vendor id of the USB device
|
||||
* @param idProduct
|
||||
* the product id of the USB device
|
||||
* @param filename
|
||||
* an optional filename which can be used to distinguish multiple
|
||||
* devices with the same vendor and product id.
|
||||
* @return the device or null
|
||||
*/
|
||||
private static Device getRegisteredDevice(short idVendor, short idProduct) {
|
||||
private static Device getRegisteredDevice(short idVendor, short idProduct,
|
||||
String filename) {
|
||||
for (Iterator<Device> iter = devices.iterator(); iter.hasNext();) {
|
||||
Device dev = iter.next();
|
||||
if ((dev.getIdVendor() == idVendor)
|
||||
&& (dev.getIdProduct() == idProduct)) {
|
||||
if (filename != null && filename.compareTo(dev.getFilename()) == 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)}.
|
||||
*/
|
||||
public static void init() {
|
||||
LibusbJava.usb_init();
|
||||
initUSBDone = true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,35 +7,43 @@
|
||||
*/
|
||||
package ch.ntb.usb;
|
||||
|
||||
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) {
|
||||
System.out.println(usb_Bus.toString());
|
||||
out.println(usb_Bus.toString());
|
||||
Usb_Device dev = usb_Bus.getDevices();
|
||||
while (dev != null) {
|
||||
System.out.println("\t" + dev.toString());
|
||||
out.println("\t" + dev.toString());
|
||||
// Usb_Device_Descriptor
|
||||
Usb_Device_Descriptor defDesc = dev.getDescriptor();
|
||||
System.out.println("\t\t" + defDesc.toString());
|
||||
out.println("\t\t" + defDesc.toString());
|
||||
// Usb_Config_Descriptor
|
||||
Usb_Config_Descriptor[] confDesc = dev.getConfig();
|
||||
for (int i = 0; i < confDesc.length; i++) {
|
||||
System.out.println("\t\t" + confDesc[i].toString());
|
||||
out.println("\t\t" + confDesc[i].toString());
|
||||
Usb_Interface[] int_ = confDesc[i].getInterface();
|
||||
if (int_ != null) {
|
||||
for (int j = 0; j < int_.length; j++) {
|
||||
System.out.println("\t\t\t" + int_[j].toString());
|
||||
Usb_Interface_Descriptor[] intDesc = int_[j].getAltsetting();
|
||||
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++) {
|
||||
System.out.println("\t\t\t\t"
|
||||
out.println("\t\t\t\t"
|
||||
+ intDesc[k].toString());
|
||||
Usb_Endpoint_Descriptor[] epDesc = intDesc[k].getEndpoint();
|
||||
Usb_Endpoint_Descriptor[] epDesc = intDesc[k]
|
||||
.getEndpoint();
|
||||
if (epDesc != null) {
|
||||
for (int e = 0; e < epDesc.length; e++) {
|
||||
System.out.println("\t\t\t\t\t"
|
||||
out.println("\t\t\t\t\t"
|
||||
+ epDesc[e].toString());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user