- 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

@@ -0,0 +1,702 @@
package ch.ntb.usb.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.setSize(750, 300);
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,174 @@
/*
* Java libusb wrapper
* Copyright (c) 2005-2006 Andreas Schl<68>pfer <libusb@drip.ch>
*
* This library is covered by the LGPL, read LGPL.txt for details.
*/
package ch.ntb.usb.testApp;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import ch.ntb.usb.Device;
import ch.ntb.usb.USB;
import ch.ntb.usb.USBException;
import ch.ntb.usb.logger.LogUtil;
import ch.ntb.usb.test.AT90USB1287;
public class TestDevice extends AT90USB1287 {
private static final Logger logger = LogUtil.getLogger("ch.ntb.usb.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;
}
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

@@ -1,429 +0,0 @@
/*
* Java libusb wrapper
* Copyright (c) 2005-2006 Andreas Schl<68>pfer <libusb@drip.ch>
*
* This library is covered by the LGPL, read LGPL.txt for details.
*/
package ch.ntb.usb.testApp;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.RowData;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class TestGUI {
private static final int HEX_WIDTH = 5;
private Shell rootShell = null; // @jve:decl-index=0:visual-constraint="10,10"
private Group vendorIDGroup = null;
Text vendorID = null;
private Group productIDGroup = null;
Text productID = null;
private Group configGroup = null;
Text configuration = null;
private Group interfaceGroup = null;
Text interface_ = null;
private Group altIntGroup = null;
Text altInt = null;
private Group deviceGroup = null;
private Group endpointGroup = null;
private Group deviceGroup2 = null;
private Group outEPGroup = null;
Text outEP = null;
private Group inEPGroup = null;
Text inEP = null;
private Group timeoutGroup = null;
Text timeout = null;
private Group dataGroup = null;
private Composite dataButtonComp = null;
private Button sendButton = null;
private Button recButton = null;
private Composite devComp = null;
private Composite devButtonComp = null;
private Button devOpenButton = null;
private Button devCloseButton = null;
private Group dataFieldGoup = null;
Text dataField = null;
private Button resetButton = null;
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) {
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 void createSShell() {
RowLayout rowLayout = new RowLayout();
rowLayout.type = org.eclipse.swt.SWT.VERTICAL;
rowLayout.justify = true;
rowLayout.fill = true;
rootShell = new Shell();
rootShell.setText("Usb TestApplication");
rootShell.setLayout(rowLayout);
createDeviceGroup();
createDataGroup();
rootShell.setSize(new org.eclipse.swt.graphics.Point(466, 315));
}
/**
* This method initializes vendorIDGroup
*
*/
private void createVendorIDGroup() {
vendorIDGroup = new Group(deviceGroup2, SWT.NONE);
vendorIDGroup.setText("VendorID");
vendorID = new Text(vendorIDGroup, SWT.BORDER | SWT.RIGHT);
vendorID
.setBounds(new org.eclipse.swt.graphics.Rectangle(7, 23, 76, 19));
vendorID.setText("0x"
+ Integer.toHexString(TestImplementation.IdVendor & 0xffff));
TestImplementation.IdVendor = (short) parseInt(vendorID.getText());
vendorID.addModifyListener(new org.eclipse.swt.events.ModifyListener() {
public void modifyText(org.eclipse.swt.events.ModifyEvent e) {
TestImplementation.IdVendor = (short) parseInt(vendorID
.getText());
}
});
}
/**
* This method initializes productIDGroup
*
*/
private void createProductIDGroup() {
productIDGroup = new Group(deviceGroup2, SWT.NONE);
productIDGroup.setText("ProductID");
productID = new Text(productIDGroup, SWT.BORDER | SWT.RIGHT);
productID.setBounds(new org.eclipse.swt.graphics.Rectangle(4, 24, 76,
19));
productID.setText("0x"
+ Integer.toHexString(TestImplementation.IdProduct & 0xffff));
TestImplementation.IdProduct = (short) parseInt(productID.getText());
productID
.addModifyListener(new org.eclipse.swt.events.ModifyListener() {
public void modifyText(org.eclipse.swt.events.ModifyEvent e) {
TestImplementation.IdProduct = (short) parseInt(productID
.getText());
}
});
}
/**
* This method initializes group
*
*/
private void createGroup() {
configGroup = new Group(deviceGroup2, SWT.NONE);
configGroup.setText("Configuration");
configuration = new Text(configGroup, SWT.BORDER | SWT.RIGHT);
configuration.setBounds(new org.eclipse.swt.graphics.Rectangle(4, 24,
75, 19));
configuration.setText(Integer
.toString(TestImplementation.CONFIGURATION));
configuration
.addModifyListener(new org.eclipse.swt.events.ModifyListener() {
public void modifyText(org.eclipse.swt.events.ModifyEvent e) {
TestImplementation.CONFIGURATION = parseInt(configuration
.getText());
}
});
}
/**
* This method initializes group
*
*/
private void createGroup2() {
interfaceGroup = new Group(deviceGroup2, SWT.NONE);
interfaceGroup.setText("Interface");
interface_ = new Text(interfaceGroup, SWT.BORDER | SWT.RIGHT);
interface_.setBounds(new org.eclipse.swt.graphics.Rectangle(4, 24, 57,
19));
interface_.setText(Integer.toString(TestImplementation.INTERFACE));
interface_
.addModifyListener(new org.eclipse.swt.events.ModifyListener() {
public void modifyText(org.eclipse.swt.events.ModifyEvent e) {
TestImplementation.INTERFACE = parseInt(interface_
.getText());
}
});
}
/**
* This method initializes group
*
*/
private void createGroup3() {
altIntGroup = new Group(deviceGroup2, SWT.NONE);
altIntGroup.setText("Alternative Int");
altInt = new Text(altIntGroup, SWT.BORDER | SWT.RIGHT);
altInt.setBounds(new Rectangle(4, 24, 76, 19));
altInt.setText(Integer.toString(TestImplementation.ALTINTERFACE));
altInt.addModifyListener(new org.eclipse.swt.events.ModifyListener() {
public void modifyText(org.eclipse.swt.events.ModifyEvent e) {
TestImplementation.ALTINTERFACE = parseInt(altInt.getText());
}
});
}
/**
* This method initializes deviceGroup
*
*/
private void createDeviceGroup() {
RowLayout rowLayout1 = new RowLayout();
rowLayout1.type = org.eclipse.swt.SWT.VERTICAL;
rowLayout1.fill = true;
deviceGroup = new Group(rootShell, SWT.NONE);
deviceGroup.setText("Device Settings");
createDeviceGroup2();
deviceGroup.setLayout(rowLayout1);
createDevComp();
}
/**
* This method initializes endpointGroup
*
*/
private void createEndpointGroup() {
endpointGroup = new Group(devComp, SWT.NONE);
endpointGroup.setLayout(new RowLayout());
createGroup4();
createGroup5();
createGroup6();
}
/**
* This method initializes deviceGroup2
*
*/
private void createDeviceGroup2() {
deviceGroup2 = new Group(deviceGroup, SWT.NONE);
deviceGroup2.setLayout(new RowLayout());
createVendorIDGroup();
createProductIDGroup();
createGroup();
createGroup2();
createGroup3();
}
/**
* This method initializes group
*
*/
private void createGroup4() {
outEPGroup = new Group(endpointGroup, SWT.NONE);
outEPGroup.setText("OUT EP");
outEP = new Text(outEPGroup, SWT.BORDER | SWT.RIGHT);
outEP.setBounds(new org.eclipse.swt.graphics.Rectangle(4, 24, 46, 19));
outEP.setText("0x"
+ Integer.toHexString(TestImplementation.OUT_ENDPOINT));
outEP.addModifyListener(new org.eclipse.swt.events.ModifyListener() {
public void modifyText(org.eclipse.swt.events.ModifyEvent e) {
TestImplementation.OUT_ENDPOINT = parseInt(outEP.getText());
}
});
}
/**
* This method initializes group
*
*/
private void createGroup5() {
inEPGroup = new Group(endpointGroup, SWT.NONE);
inEPGroup.setText("IN EP");
inEP = new Text(inEPGroup, SWT.BORDER | SWT.RIGHT);
inEP.setBounds(new org.eclipse.swt.graphics.Rectangle(4, 24, 46, 19));
inEP
.setText("0x"
+ Integer.toHexString(TestImplementation.IN_ENDPOINT));
inEP.addModifyListener(new org.eclipse.swt.events.ModifyListener() {
public void modifyText(org.eclipse.swt.events.ModifyEvent e) {
TestImplementation.IN_ENDPOINT = parseInt(inEP.getText());
}
});
}
/**
* This method initializes group
*
*/
private void createGroup6() {
timeoutGroup = new Group(endpointGroup, SWT.NONE);
timeoutGroup.setText("Timeout");
timeout = new Text(timeoutGroup, SWT.BORDER | SWT.RIGHT);
timeout.setBounds(new Rectangle(4, 24, 46, 19));
timeout.setText(Integer.toString(TestImplementation.TIMEOUT));
timeout.addModifyListener(new org.eclipse.swt.events.ModifyListener() {
public void modifyText(org.eclipse.swt.events.ModifyEvent e) {
TestImplementation.TIMEOUT = parseInt(timeout.getText());
}
});
}
/**
* This method initializes dataGroup
*
*/
private void createDataGroup() {
RowLayout rowLayout5 = new RowLayout();
rowLayout5.type = org.eclipse.swt.SWT.HORIZONTAL;
rowLayout5.spacing = 10;
dataGroup = new Group(rootShell, SWT.NONE);
dataGroup.setText("Send and Receive Data");
dataGroup.setLayout(rowLayout5);
createDataFieldGoup();
createButtonComp();
}
/**
* This method initializes buttonComp
*
*/
private void createButtonComp() {
RowLayout rowLayout3 = new RowLayout();
rowLayout3.type = org.eclipse.swt.SWT.VERTICAL;
rowLayout3.justify = true;
rowLayout3.fill = true;
dataButtonComp = new Composite(dataGroup, SWT.NONE);
sendButton = new Button(dataButtonComp, SWT.NONE);
sendButton.setText("Send");
sendButton
.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
@Override
public void widgetSelected(
org.eclipse.swt.events.SelectionEvent e) {
byte[] b = parseByteArray(dataField.getText());
TestImplementation.write(b, b.length);
}
});
recButton = new Button(dataButtonComp, SWT.NONE);
dataButtonComp.setLayout(rowLayout3);
recButton.setText("Receive");
recButton
.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
@Override
public void widgetSelected(
org.eclipse.swt.events.SelectionEvent e) {
TestImplementation.read();
}
});
}
/**
* This method initializes devComp
*
*/
private void createDevComp() {
RowLayout rowLayout4 = new RowLayout();
rowLayout4.fill = true;
rowLayout4.spacing = 50;
devComp = new Composite(deviceGroup, SWT.NONE);
createEndpointGroup();
devComp.setLayout(rowLayout4);
createDevButtonComp();
}
/**
* This method initializes devButtonComp
*
*/
private void createDevButtonComp() {
RowLayout rowLayout2 = new RowLayout();
rowLayout2.marginHeight = 25;
rowLayout2.spacing = 5;
devButtonComp = new Composite(devComp, SWT.NONE);
devButtonComp.setLayout(rowLayout2);
devOpenButton = new Button(devButtonComp, SWT.NONE);
devOpenButton.setText("Open Device");
devOpenButton
.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
@Override
public void widgetSelected(
org.eclipse.swt.events.SelectionEvent e) {
TestImplementation.openUsbDevice();
}
});
devCloseButton = new Button(devButtonComp, SWT.NONE);
devCloseButton.setText("Close Device");
resetButton = new Button(devButtonComp, SWT.NONE);
resetButton.setText("Reset");
resetButton
.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
@Override
public void widgetSelected(
org.eclipse.swt.events.SelectionEvent e) {
TestImplementation.resetUsbDevice();
}
});
devCloseButton
.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
@Override
public void widgetSelected(
org.eclipse.swt.events.SelectionEvent e) {
TestImplementation.closeUsbDevice();
}
});
}
/**
* This method initializes dataFieldGoup
*
*/
private void createDataFieldGoup() {
RowData rowData = new org.eclipse.swt.layout.RowData();
rowData.width = 340;
RowLayout rowLayout6 = new RowLayout();
rowLayout6.fill = true;
rowLayout6.marginHeight = 5;
rowLayout6.justify = true;
dataFieldGoup = new Group(dataGroup, SWT.NONE);
dataFieldGoup.setText("Data to send [hex]");
dataFieldGoup.setLayout(rowLayout6);
dataField = new Text(dataFieldGoup, SWT.BORDER);
dataField.setText(TestImplementation.sendData);
dataField.setLayoutData(rowData);
}
public static void main(String[] args) {
TestGUI app = new TestGUI();
app.createSShell();
app.rootShell.open();
Display display = app.rootShell.getDisplay();
while (!app.rootShell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
}

View File

@@ -1,105 +0,0 @@
/*
* Java libusb wrapper
* Copyright (c) 2005-2006 Andreas Schl<68>pfer <libusb@drip.ch>
*
* This library is covered by the LGPL, read LGPL.txt for details.
*/
package ch.ntb.usb.testApp;
import java.util.logging.Level;
import java.util.logging.Logger;
import ch.ntb.usb.Device;
import ch.ntb.usb.USB;
import ch.ntb.usb.USBException;
import ch.ntb.usb.logger.LogUtil;
public class TestImplementation {
private static final Logger logger = LogUtil.getLogger("ch.ntb.usb.test");
public static String sendData = "0x5b 0x02 0x01 0x00 0x03 0x03 0xf0 0xf0 0x1f";
public static short IdVendor = (short) 0x8235;
public static short IdProduct = 0x0200;
public static int TIMEOUT = 2000;
public static int CONFIGURATION = 1;
public static int INTERFACE = 0;
public static int ALTINTERFACE = 0;
public static int OUT_ENDPOINT = 0x01;
public static int IN_ENDPOINT = 0x82;
private static Device dev = null;
static {
logger.setLevel(Level.ALL);
}
static void openUsbDevice() {
dev = USB.getDevice(IdVendor, IdProduct);
try {
dev.open(CONFIGURATION, INTERFACE, ALTINTERFACE);
logger.info("device opened, interface claimed");
} catch (USBException e) {
e.printStackTrace();
}
}
static void closeUsbDevice() {
try {
dev.close();
logger.info("device closed");
} catch (USBException e) {
e.printStackTrace();
}
}
static void resetUsbDevice() {
try {
dev.reset();
logger.info("device reset");
} catch (USBException e) {
e.printStackTrace();
}
}
static void write(byte[] data, int length) {
int lenWritten = 0;
try {
lenWritten = dev.writeBulk(OUT_ENDPOINT, data, length, TIMEOUT,
false);
StringBuffer sb = new StringBuffer("write_bulkdata: " + lenWritten
+ " Bytes sent: ");
for (int i = 0; i < lenWritten; i++) {
sb.append("0x" + String.format("%1$02X", data[i]) + " ");
}
logger.info(sb.toString());
} catch (USBException e) {
e.printStackTrace();
}
}
static void read() {
byte[] data = new byte[dev.getMaxPacketSize()];
int lenRead = 0;
try {
lenRead = dev.readBulk(IN_ENDPOINT, data, dev.getMaxPacketSize(),
TIMEOUT, false);
StringBuffer sb = new StringBuffer("read_bulkdata: " + lenRead
+ " Bytes received: Data: ");
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();
}
}
}

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
*