- TestApp ported from SWT to Swing

- Start TestApp from UsbView with selected interface
- some refactoring

git-svn-id: https://svn.code.sf.net/p/libusbjava/code/trunk@225 94ad28fe-ef68-46b1-9651-e7ae4fcf1c4c
This commit is contained in:
schlaepfer
2006-12-04 08:01:32 +00:00
parent 3f79739681
commit db8d66c992
11 changed files with 1223 additions and 676 deletions

View File

@@ -79,17 +79,20 @@ public class UsbTreeModel implements TreeModel, TreeSelectionListener {
return null;
return confDescs[index - 1];
} else if (parent instanceof Usb_Config_Descriptor) {
Usb_Interface[] intDescs = ((Usb_Config_Descriptor) parent).getInterface();
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();
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();
Usb_Endpoint_Descriptor[] endpoints = ((Usb_Interface_Descriptor) parent)
.getEndpoint();
if (index >= endpoints.length)
return null;
return endpoints[index];
@@ -173,7 +176,8 @@ public class UsbTreeModel implements TreeModel, TreeSelectionListener {
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");
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;
@@ -185,14 +189,16 @@ public class UsbTreeModel implements TreeModel, TreeSelectionListener {
} 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())
+ "\n");
sb.append("\tblenght: 0x"
+ Integer.toHexString(devDesc.getBLength() & 0xFF) + "\n");
sb.append("\tbDescriptorType: 0x"
+ Integer.toHexString(devDesc.getBDescriptorType()) + "\n");
+ 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");
+ Integer.toHexString(devDesc.getBDeviceClass() & 0xFF)
+ "\n");
sb.append("\tbDeviceSubClass: 0x"
+ Integer.toHexString(devDesc.getBDeviceSubClass() & 0xFF)
+ "\n");
@@ -203,19 +209,28 @@ public class UsbTreeModel implements TreeModel, TreeSelectionListener {
+ 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()) + "\n");
sb.append("\tiManufacturer: 0x"
+ Integer.toHexString(devDesc.getIManufacturer()) + "\n");
sb.append("\tiProduct: 0x" + Integer.toHexString(devDesc.getIProduct())
+ 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()) + "\n");
sb.append("\tbNumConfigurations: 0x"
+ Integer.toHexString(devDesc.getBNumConfigurations()) + "\n");
+ 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) {
@@ -224,7 +239,8 @@ public class UsbTreeModel implements TreeModel, TreeSelectionListener {
Usb_Device_Descriptor tmpDevDesc = dev.getDescriptor();
if ((dev.getDescriptor() != null)
&& ((dev.getDescriptor().getIManufacturer() > 0)
|| (dev.getDescriptor().getIProduct() > 0) || (dev.getDescriptor().getISerialNumber() > 0))) {
|| (dev.getDescriptor().getIProduct() > 0) || (dev
.getDescriptor().getISerialNumber() > 0))) {
if (tmpDevDesc.equals(devDesc)) {
int handle = LibusbJava.usb_open(dev);
sb.append("\nString descriptors\n");
@@ -234,8 +250,8 @@ public class UsbTreeModel implements TreeModel, TreeSelectionListener {
}
if (dev.getDescriptor().getIManufacturer() > 0) {
String manufacturer = LibusbJava
.usb_get_string_simple(handle,
devDesc.getIManufacturer());
.usb_get_string_simple(handle, devDesc
.getIManufacturer());
if (manufacturer == null)
manufacturer = "unable to fetch manufacturer string";
sb.append("\tiManufacturer: " + manufacturer
@@ -243,16 +259,16 @@ public class UsbTreeModel implements TreeModel, TreeSelectionListener {
}
if (dev.getDescriptor().getIProduct() > 0) {
String product = LibusbJava
.usb_get_string_simple(handle,
devDesc.getIProduct());
.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());
.usb_get_string_simple(handle, devDesc
.getISerialNumber());
if (serialNumber == null)
serialNumber = "unable to fetch serial number string";
sb.append("\tiSerialNumber: " + serialNumber
@@ -269,23 +285,29 @@ public class UsbTreeModel implements TreeModel, TreeSelectionListener {
} 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("\tblenght: 0x"
+ Integer.toHexString(confDesc.getBLength()) + "\n");
sb.append("\tbDescriptorType: 0x"
+ Integer.toHexString(confDesc.getBDescriptorType()) + "\n");
+ Integer.toHexString(confDesc.getBDescriptorType() & 0xFF)
+ "\n");
sb.append("\tbNumInterfaces: 0x"
+ Integer.toHexString(confDesc.getBNumInterfaces()) + "\n");
sb.append("\tbConfigurationValue: 0x"
+ Integer.toHexString(confDesc.getBConfigurationValue()) + "\n");
+ Integer.toHexString(confDesc.getBNumInterfaces() & 0xFF)
+ "\n");
sb
.append("\tbConfigurationValue: 0x"
+ Integer.toHexString(confDesc
.getBConfigurationValue() & 0xFF) + "\n");
sb.append("\tiConfiguration: 0x"
+ Integer.toHexString(confDesc.getIConfiguration()) + "\n");
+ Integer.toHexString(confDesc.getIConfiguration() & 0xFF)
+ "\n");
sb.append("\tbmAttributes: 0x"
+ Integer.toHexString(confDesc.getBmAttributes() & 0xFF) + "\n");
+ 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("\textralen: 0x"
+ Integer.toHexString(confDesc.getExtralen()) + "\n");
sb.append("\textra: " + confDesc.getExtra() + "\n");
// get device handle to retrieve string descriptors
Usb_Bus bus = rootBus;
@@ -303,8 +325,8 @@ public class UsbTreeModel implements TreeModel, TreeSelectionListener {
break;
}
String configuration = LibusbJava
.usb_get_string_simple(handle,
confDesc.getIConfiguration());
.usb_get_string_simple(handle, confDesc
.getIConfiguration());
if (configuration == null)
configuration = "unable to fetch configuration string";
sb.append("\tiConfiguration: " + configuration
@@ -329,29 +351,36 @@ public class UsbTreeModel implements TreeModel, TreeSelectionListener {
} 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())
+ "\n");
sb.append("\tblenght: 0x"
+ Integer.toHexString(intDesc.getBLength() & 0xFF) + "\n");
sb.append("\tbDescriptorType: 0x"
+ Integer.toHexString(intDesc.getBDescriptorType()) + "\n");
+ Integer.toHexString(intDesc.getBDescriptorType() & 0xFF)
+ "\n");
sb.append("\tbInterfaceNumber: 0x"
+ Integer.toHexString(intDesc.getBInterfaceNumber()) + "\n");
+ Integer.toHexString(intDesc.getBInterfaceNumber() & 0xFF)
+ "\n");
sb.append("\tbAlternateSetting: 0x"
+ Integer.toHexString(intDesc.getBAlternateSetting()) + "\n");
+ Integer
.toHexString(intDesc.getBAlternateSetting() & 0xFF)
+ "\n");
sb.append("\tbNumEndpoints: 0x"
+ Integer.toHexString(intDesc.getBNumEndpoints()) + "\n");
+ 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("\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("\textralen: 0x"
+ Integer.toHexString(intDesc.getExtralen()) + "\n");
sb.append("\textra: " + intDesc.getExtra() + "\n");
// get device handle to retrieve string descriptors
Usb_Bus bus = rootBus;
@@ -362,7 +391,8 @@ public class UsbTreeModel implements TreeModel, TreeSelectionListener {
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();
Usb_Interface_Descriptor[] tmpIntDescs = ints[j]
.getAltsetting();
for (int k = 0; k < ints.length; k++) {
if (tmpIntDescs[i].equals(intDesc)
&& (intDesc.getIInterface() > 0)) {
@@ -393,26 +423,28 @@ public class UsbTreeModel implements TreeModel, TreeSelectionListener {
} 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())
+ "\n");
sb.append("\tblenght: 0x"
+ Integer.toHexString(epDesc.getBLength() & 0xFF) + "\n");
sb.append("\tbDescriptorType: 0x"
+ Integer.toHexString(epDesc.getBDescriptorType()) + "\n");
+ 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");
+ 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())
+ "\n");
sb.append("\tbRefresh: 0x" + Integer.toHexString(epDesc.getBRefresh())
+ "\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("\textralen: 0x"
+ Integer.toHexString(epDesc.getExtralen()) + "\n");
sb.append("\textra: " + epDesc.getExtra() + "\n");
textArea.setText(sb.toString());
}

View File

@@ -11,20 +11,32 @@ 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.usb.LibusbJava;
import ch.ntb.usb.Usb_Bus;
import ch.ntb.usb.Usb_Config_Descriptor;
import ch.ntb.usb.Usb_Device;
import ch.ntb.usb.Usb_Device_Descriptor;
import ch.ntb.usb.Usb_Endpoint_Descriptor;
import ch.ntb.usb.Usb_Interface_Descriptor;
import ch.ntb.usb.test.AbstractDeviceInfo.TransferMode;
import ch.ntb.usb.testApp.TestApp;
import ch.ntb.usb.testApp.TestDevice;
public class UsbView extends JFrame {
@@ -44,6 +56,10 @@ public class UsbView extends JFrame {
UsbTreeModel treeModel;
JPopupMenu testAppPopup;
protected JPopupMenu endpointPopup;
/**
* This is the default constructor
*/
@@ -179,10 +195,168 @@ public class UsbView extends JFrame {
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
*