diff --git a/mcdp/src/ch/ntb/usb/Device.java b/mcdp/src/ch/ntb/usb/Device.java new file mode 100644 index 0000000..2d40a02 --- /dev/null +++ b/mcdp/src/ch/ntb/usb/Device.java @@ -0,0 +1,202 @@ +package ch.ntb.usb; + +import java.util.logging.Level; + +import ch.ntb.usb.logger.LogUtil; +import ch.ntb.usb.logger.UsbLogger; + +public class Device { + + private static UsbLogger logger = LogUtil.ch_ntb_usb; + + private static final int TIMEOUT_ERROR_CODE = -116; + + public int MAX_DATA_SIZE = USB.MAX_DATA_SIZE; + + private int idVendor, idProduct, configuration, interface_, altinterface; + + private int usb_dev_handle; + + Device(short idVendor, short idProduct) { + this.idVendor = idVendor; + this.idProduct = idProduct; + } + + public void open(int configuration, int interface_, int altinterface) + throws USBException { + this.configuration = configuration; + this.interface_ = interface_; + this.altinterface = altinterface; + + Usb_Bus bus; + + if (usb_dev_handle > 0) { + throw new USBException("device opened, close or reset first"); + } + + // open bus + LibusbWin.usb_init(); + LibusbWin.usb_find_busses(); + LibusbWin.usb_find_devices(); + + bus = LibusbWin.usb_get_busses(); + if (bus == null) { + throw new USBException("LibusbWin.usb_get_busses(): " + + LibusbWin.usb_strerror()); + } + // search for device + while (bus != null) { + Usb_Device dev = bus.devices; + while (dev != null) { + // Usb_Device_Descriptor + Usb_Device_Descriptor defDesc = dev.descriptor; + if ((defDesc.idVendor == idVendor) + && (defDesc.idProduct == idProduct)) { + logger.info("Open device: " + dev.filename); + int res = LibusbWin.usb_open(dev); + if (res <= 0) { + throw new USBException("LibusbWin.usb_open: " + + LibusbWin.usb_strerror()); + } else { + usb_dev_handle = res; + } + } + dev = dev.next; + } + bus = bus.next; + } + if (usb_dev_handle <= 0) { + throw new USBException("USB device with idVendor 0x" + + Integer.toHexString(idVendor & 0xFFFF) + + " and idProduct 0x" + + Integer.toHexString(idProduct & 0xFFFF) + " not found"); + } + USB.claim_interface(usb_dev_handle, configuration, interface_, + altinterface); + } + + public void close() throws USBException { + if (usb_dev_handle <= 0) { + throw new USBException("invalid device handle"); + } + USB.release_interface(usb_dev_handle, interface_); + if (LibusbWin.usb_close(usb_dev_handle) < 0) { + usb_dev_handle = 0; + throw new USBException("LibusbWin.usb_close: " + + LibusbWin.usb_strerror()); + } + usb_dev_handle = 0; + logger.info("device closed"); + } + + /** + * Sends an USB reset to the device. The device handle will no longer be + * valid. To use the device again, open must be called. + * + * @throws USBException + */ + public void reset() throws USBException { + if (usb_dev_handle <= 0) { + throw new USBException("invalid device handle"); + } + if (LibusbWin.usb_reset(usb_dev_handle) < 0) { + throw new USBException("LibusbWin.usb_reset: " + + LibusbWin.usb_strerror()); + } + usb_dev_handle = 0; + logger.info("device reset"); + } + + public int bulkwrite(int out_ep_address, byte[] data, int length, int timeout) + throws USBException { + if (usb_dev_handle <= 0) { + throw new USBException("invalid device handle"); + } + if (data == null) { + throw new USBException("data must not be null"); + } + if (length <= 0) { + throw new USBException("size must be > 0"); + } + int lenWritten = LibusbWin.usb_bulk_write(usb_dev_handle, out_ep_address, + data, length, timeout); + if (lenWritten < 0) { + if (lenWritten == TIMEOUT_ERROR_CODE) { + throw new USBTimeoutException("LibusbWin.usb_bulk_write: " + + LibusbWin.usb_strerror()); + } + throw new USBException("LibusbWin.usb_bulk_write: " + + LibusbWin.usb_strerror()); + } + + if (logger.getLevel().intValue() <= Level.INFO.intValue()) { + StringBuffer sb = new StringBuffer("bulkwrite, ep 0x" + + Integer.toHexString(out_ep_address) + ": " + lenWritten + + " Bytes sent: "); + for (int i = 0; i < lenWritten; i++) { + sb.append("0x" + String.format("%1$02X", data[i]) + " "); + } + logger.info(sb.toString()); + } + return lenWritten; + } + + public int bulkread(int in_ep_address, byte[] data, int size, int timeout) + throws USBException { + if (usb_dev_handle <= 0) { + throw new USBException("invalid device handle"); + } + if (data == null) { + throw new USBException("data must not be null"); + } + if (size <= 0) { + throw new USBException("size must be > 0"); + } + int lenRead = LibusbWin.usb_bulk_read(usb_dev_handle, in_ep_address, data, + size, timeout); + if (lenRead < 0) { + if (lenRead == TIMEOUT_ERROR_CODE) { + throw new USBTimeoutException("LibusbWin.usb_bulk_read: " + + LibusbWin.usb_strerror()); + } + throw new USBException("LibusbWin.usb_bulk_read: " + + LibusbWin.usb_strerror()); + } + + if (logger.getLevel().intValue() <= Level.INFO.intValue()) { + StringBuffer sb = new StringBuffer("bulkread, ep 0x" + + Integer.toHexString(in_ep_address) + ": " + lenRead + + " Bytes received: "); + for (int i = 0; i < lenRead; i++) { + sb.append("0x" + String.format("%1$02X", data[i]) + " "); + } + logger.info(sb.toString()); + } + return lenRead; + } + + public int getIdProduct() { + return idProduct; + } + + public int getIdVendor() { + return idVendor; + } + + public int getAltinterface() { + return altinterface; + } + + public int getConfiguration() { + return configuration; + } + + public int getInterface() { + return interface_; + } + + public int getUsb_dev_handle() { + return usb_dev_handle; + } + +} diff --git a/mcdp/src/ch/ntb/usb/LibusbWin.java b/mcdp/src/ch/ntb/usb/LibusbWin.java index 8b20ab2..af174f7 100644 --- a/mcdp/src/ch/ntb/usb/LibusbWin.java +++ b/mcdp/src/ch/ntb/usb/LibusbWin.java @@ -1,6 +1,8 @@ package ch.ntb.usb; public class LibusbWin { + + private static final String sysDir = "C:/Windows"; // Core /** @@ -291,6 +293,6 @@ public class LibusbWin { /*******************************************************************/ static { - System.loadLibrary("LibusbWin"); + System.load(sysDir + "/system32/LibusbWin.dll"); } } \ No newline at end of file diff --git a/mcdp/src/ch/ntb/usb/USB.java b/mcdp/src/ch/ntb/usb/USB.java index 2f72b4f..700d104 100644 --- a/mcdp/src/ch/ntb/usb/USB.java +++ b/mcdp/src/ch/ntb/usb/USB.java @@ -1,342 +1,112 @@ package ch.ntb.usb; -import ch.ntb.usb.LibusbWin; -import ch.ntb.usb.Usb_Bus; -import ch.ntb.usb.Usb_Device; -import ch.ntb.usb.Usb_Device_Descriptor; +import java.util.Iterator; +import java.util.LinkedList; + +import ch.ntb.usb.logger.LogUtil; +import ch.ntb.usb.logger.UsbLogger; public class USB { + /** + * The maximal data size in bytes which is allowed to be transmitted at + * once. + */ public static final int MAX_DATA_SIZE = 512; - private static final int TIMEOUT_ERROR_CODE = -116; + private static UsbLogger logger = LogUtil.ch_ntb_usb; - private static final boolean DEBUG_ON = true; + private static LinkedList devices = new LinkedList(); - private static Usb_Bus bus; + /** + * Create a new device an register it in a device queue or get an already + * created device. + * + * @param idVendor + * the vendor id of the USB device + * @param idProduct + * the product id of the USB device + * @return a newly created device or an already registered device + */ + public static Device getDevice(short idVendor, short idProduct) { - private static int usb_dev_handle = 0; - - private static short IdVendor = -1; - - private static short IdProduct = -1; - - private static int Configuration = -1; - - private static int Interface = -1; - - private static int Altinterface = -1; - - private static int OUT_Endpoint_1 = -1; - - private static int IN_Endpoint_1 = -1; - - private static int OUT_Endpoint_2 = -1; - - private static int IN_Endpoint_2 = -1; - - public static void reset() { - bus = null; - usb_dev_handle = 0; - - if (DEBUG_ON) { - System.out.println("reset done"); + // check if this device is already registered + Device dev = getRegisteredDevice(idVendor, idProduct); + if (dev != null) { + return dev; } + dev = new Device(idVendor, idProduct); + devices.add(dev); + return dev; } - public static void openUsbDevice() throws USBException { - - int res; - - // open bus - if (bus == null) { - LibusbWin.usb_init(); - LibusbWin.usb_find_busses(); - LibusbWin.usb_find_devices(); - - bus = LibusbWin.usb_get_busses(); - if (bus == null) { - throw new USBException("LibusbWin.usb_get_busses(): " - + LibusbWin.usb_strerror()); + /** + * Get an already registered device or null if the device does not exist. + * + * @param idVendor + * the vendor id of the USB device + * @param idProduct + * the product id of the USB device + * @return the device or null + */ + private static Device getRegisteredDevice(short idVendor, short idProduct) { + for (Iterator iter = devices.iterator(); iter.hasNext();) { + Device dev = iter.next(); + if ((dev.getIdVendor() == idVendor) + && (dev.getIdProduct() == idProduct)) { + return dev; } } - // search for device - if (usb_dev_handle <= 0) { - - while (bus != null) { - Usb_Device dev = bus.devices; - while (dev != null) { - // Usb_Device_Descriptor - Usb_Device_Descriptor defDesc = dev.descriptor; - if ((defDesc.idVendor == IdVendor) - && (defDesc.idProduct == IdProduct)) { - if (DEBUG_ON) { - System.out.println("Open device: " + dev.filename); - } - res = LibusbWin.usb_open(dev); - if (res <= 0) { - throw new USBException("LibusbWin.usb_open: " - + LibusbWin.usb_strerror()); - } else { - usb_dev_handle = res; - } - } - dev = dev.next; - } - bus = bus.next; - } - if (usb_dev_handle <= 0) { - throw new USBException("UsbDevice with idVendor 0x" - + Integer.toHexString(IdVendor & 0xFFFF) - + " and idProduct 0x" - + Integer.toHexString(IdProduct & 0xFFFF) - + " not found"); - } - } - claim_interface(usb_dev_handle, Configuration, Interface, Altinterface); - - if (DEBUG_ON) { - System.out.println("device opened, interface claimed"); - } + return null; } - public static void closeUsbDevice() throws USBException { - release_interface(usb_dev_handle, Interface); - if (LibusbWin.usb_close(usb_dev_handle) < 0) { - throw new USBException("LibusbWin.usb_close: " - + LibusbWin.usb_strerror()); - } - - if (DEBUG_ON) { - System.out.println("device closed"); - } - } - - public static void resetUsbDevice() throws USBException { - if (LibusbWin.usb_reset(usb_dev_handle) < 0) { - throw new USBException("LibusbWin.usb_reset: " - + LibusbWin.usb_strerror()); - } - usb_dev_handle = 0; - - if (DEBUG_ON) { - System.out.println("resetUsbDevie done"); - } - } - - public static void write_EP1(byte[] data, int length, int timeout) - throws USBException { - if (data == null) { - throw new USBException("data must not be null"); - } - if (length <= 0) { - throw new USBException("size must be > 0"); - } - int lenWritten = LibusbWin.usb_bulk_write(usb_dev_handle, - OUT_Endpoint_1, data, length, timeout); - if (lenWritten < 0) { - if (lenWritten == TIMEOUT_ERROR_CODE) { - throw new USBTimeoutException("LibusbWin.usb_bulk_write EP1: " - + LibusbWin.usb_strerror()); - } - throw new USBException("LibusbWin.usb_bulk_write EP1: " - + LibusbWin.usb_strerror()); - } - - if (DEBUG_ON) { - System.out.print("write_bulkdata EP1: " + lenWritten - + " Bytes sent: "); - for (int i = 0; i < lenWritten; i++) { - System.out.print("0x" + String.format("%1$02X", data[i]) + " "); - } - System.out.println(); - } - } - - public static int read_EP1(byte[] data, int size, int timeout) - throws USBException { - if (data == null) { - throw new USBException("data must not be null"); - } - if (size <= 0) { - throw new USBException("size must be > 0"); - } - int lenRead = LibusbWin.usb_bulk_read(usb_dev_handle, IN_Endpoint_1, - data, size, timeout); - if (lenRead < 0) { - if (lenRead == TIMEOUT_ERROR_CODE) { - throw new USBTimeoutException("LibusbWin.usb_bulk_read EP1: " - + LibusbWin.usb_strerror()); - } - throw new USBException("LibusbWin.usb_bulk_read EP1: " - + LibusbWin.usb_strerror()); - } - - if (DEBUG_ON) { - System.out.print("read_bulkdata EP1: " + lenRead - + " Bytes received: "); - System.out.print("Data: "); - for (int i = 0; i < lenRead; i++) { - System.out.print("0x" + String.format("%1$02X", data[i]) + " "); - } - System.out.println(); - } - return lenRead; - } - - public static void write_EP2(byte[] data, int length, int timeout) - throws USBException { - if (data == null) { - throw new USBException("data must not be null"); - } - if (length <= 0) { - throw new USBException("size must be > 0"); - } - int lenWritten = LibusbWin.usb_bulk_write(usb_dev_handle, - OUT_Endpoint_2, data, length, timeout); - if (lenWritten < 0) { - if (lenWritten == TIMEOUT_ERROR_CODE) { - throw new USBTimeoutException("LibusbWin.usb_bulk_write EP2: " - + LibusbWin.usb_strerror()); - } - throw new USBException("LibusbWin.usb_bulk_write EP2: " - + LibusbWin.usb_strerror()); - } - - if (DEBUG_ON) { - System.out.print("write_bulkdata EP2: " + lenWritten - + " Bytes sent: "); - for (int i = 0; i < lenWritten; i++) { - System.out.print("0x" + String.format("%1$02X", data[i]) + " "); - } - System.out.println(); - } - } - - public static int read_EP2(byte[] data, int size, int timeout) - throws USBException { - if (data == null) { - throw new USBException("data must not be null"); - } - if (size <= 0) { - throw new USBException("size must be > 0"); - } - int lenRead = LibusbWin.usb_bulk_read(usb_dev_handle, IN_Endpoint_2, - data, size, timeout); - if (lenRead < 0) { - if (lenRead == TIMEOUT_ERROR_CODE) { - throw new USBTimeoutException("LibusbWin.usb_bulk_read EP2: " - + LibusbWin.usb_strerror()); - } - throw new USBException("LibusbWin.usb_bulk_read EP2: " - + LibusbWin.usb_strerror()); - } - - if (DEBUG_ON) { - System.out.print("read_bulkdata EP2: " + lenRead - + " Bytes received: "); - System.out.print("Data: "); - for (int i = 0; i < lenRead; i++) { - System.out.print("0x" + String.format("%1$02X", data[i]) + " "); - } - System.out.println(); - } - return lenRead; - } - - private static void claim_interface(int dev_handle, int configuration, + /** + * Claim an interface to send and receive USB data. + * + * @param usb_dev_handle + * the handle of the device (MUST BE VALID) + * @param configuration + * the configuration to use + * @param interface_ + * the interface to claim + * @param altinterface + * the alternative interface to use + * @throws USBException + * throws an USBException if the action fails + */ + static void claim_interface(int usb_dev_handle, int configuration, int interface_, int altinterface) throws USBException { if (LibusbWin.usb_set_configuration(usb_dev_handle, configuration) < 0) { throw new USBException("LibusbWin.usb_set_configuration: " + LibusbWin.usb_strerror()); } - if (LibusbWin.usb_claim_interface(dev_handle, interface_) < 0) { + if (LibusbWin.usb_claim_interface(usb_dev_handle, interface_) < 0) { throw new USBException("LibusbWin.usb_claim_interface: " + LibusbWin.usb_strerror()); } - if (LibusbWin.usb_set_altinterface(dev_handle, altinterface) < 0) { + if (LibusbWin.usb_set_altinterface(usb_dev_handle, altinterface) < 0) { throw new USBException("LibusbWin.usb_set_altinterface: " + LibusbWin.usb_strerror()); } + logger.info("interface claimed"); } - private static void release_interface(int dev_handle, int interface_) + /** + * Release a previously claimed interface. + * + * @param dev_handle + * the handle of the device (MUST BE VALID) + * @param interface_ + * the interface to claim + * @throws USBException + * throws an USBException if the action fails + */ + static void release_interface(int dev_handle, int interface_) throws USBException { if (LibusbWin.usb_release_interface(dev_handle, interface_) < 0) { throw new USBException("LibusbWin.usb_release_interface: " + LibusbWin.usb_strerror()); } - } - - public static short getIdProduct() { - return IdProduct; - } - - public static void setIdProduct(short idProduct) { - IdProduct = idProduct; - } - - public static short getIdVendor() { - return IdVendor; - } - - public static void setIdVendor(short idVendor) { - IdVendor = idVendor; - } - - public static int getConfiguration() { - return Configuration; - } - - public static void setConfiguration(int configuration) { - Configuration = configuration; - } - - public static int getInterface() { - return Interface; - } - - public static void setInterface(int interface_) { - Interface = interface_; - } - - public static int getIN_Endpoint_1() { - return IN_Endpoint_1; - } - - public static void setIN_Endpoint_1(int in_endpoint_1) { - IN_Endpoint_1 = in_endpoint_1; - } - - public static int getOUT_Endpoint_1() { - return OUT_Endpoint_1; - } - - public static void setOUT_Endpoint_1(int out_endpoint_1) { - OUT_Endpoint_1 = out_endpoint_1; - } - - public static int getIN_Endpoint_2() { - return IN_Endpoint_2; - } - - public static void setIN_Endpoint_2(int in_endpoint_2) { - IN_Endpoint_2 = in_endpoint_2; - } - - public static int getOUT_Endpoint_2() { - return OUT_Endpoint_2; - } - - public static void setOUT_Endpoint_2(int out_endpoint_2) { - OUT_Endpoint_2 = out_endpoint_2; - } - - public static int getAltinterface() { - return Altinterface; - } - - public static void setAltinterface(int altinterface) { - Altinterface = altinterface; + logger.info("interface released"); } } diff --git a/mcdp/src/ch/ntb/usb/USB_old.java b/mcdp/src/ch/ntb/usb/USB_old.java new file mode 100644 index 0000000..cdf7530 --- /dev/null +++ b/mcdp/src/ch/ntb/usb/USB_old.java @@ -0,0 +1,346 @@ +package ch.ntb.usb; + +import ch.ntb.usb.LibusbWin; +import ch.ntb.usb.Usb_Bus; +import ch.ntb.usb.Usb_Device; +import ch.ntb.usb.Usb_Device_Descriptor; + +public class USB_old { + + public static final int MAX_DATA_SIZE = 512; + + private static final int TIMEOUT_ERROR_CODE = -116; + + private static final boolean DEBUG_ON = false; + + private static Usb_Bus bus; + + private static int usb_dev_handle = 0; + + private static short IdVendor = -1; + + private static short IdProduct = -1; + + private static int Configuration = -1; + + private static int Interface = -1; + + private static int Altinterface = -1; + + private static int OUT_Endpoint_1 = -1; + + private static int IN_Endpoint_1 = -1; + + private static int OUT_Endpoint_2 = -1; + + private static int IN_Endpoint_2 = -1; + + public static void openUsbDevice() throws USBException { + + int res; + + // open bus + LibusbWin.usb_init(); + LibusbWin.usb_find_busses(); + LibusbWin.usb_find_devices(); + + bus = LibusbWin.usb_get_busses(); + if (bus == null) { + throw new USBException("LibusbWin.usb_get_busses(): " + + LibusbWin.usb_strerror()); + } + + while (bus != null) { + Usb_Device dev = bus.devices; + while (dev != null) { + // Usb_Device_Descriptor + Usb_Device_Descriptor defDesc = dev.descriptor; + if ((defDesc.idVendor == IdVendor) + && (defDesc.idProduct == IdProduct)) { + if (DEBUG_ON) { + System.out.println("Open device: " + dev.filename); + } + res = LibusbWin.usb_open(dev); + if (res <= 0) { + throw new USBException("LibusbWin.usb_open: " + + LibusbWin.usb_strerror()); + } else { + usb_dev_handle = res; + } + } + dev = dev.next; + } + bus = bus.next; + } + if (usb_dev_handle <= 0) { + throw new USBException("UsbDevice with idVendor 0x" + + Integer.toHexString(IdVendor & 0xFFFF) + + " and idProduct 0x" + + Integer.toHexString(IdProduct & 0xFFFF) + " not found"); + } + claim_interface(usb_dev_handle, Configuration, Interface, Altinterface); + + if (DEBUG_ON) { + System.out.println("device opened, interface claimed"); + } + } + + public static void closeUsbDevice() throws USBException { + if (usb_dev_handle <= 0) { + throw new USBException("Device not open"); + } + System.out.println(usb_dev_handle); + release_interface(usb_dev_handle, Interface); + if (LibusbWin.usb_close(usb_dev_handle) < 0) { + throw new USBException("LibusbWin.usb_close: " + + LibusbWin.usb_strerror()); + } + + if (DEBUG_ON) { + System.out.println("device closed"); + } + } + + public static void resetUsbDevice() throws USBException { + if (usb_dev_handle <= 0) { + throw new USBException("Device not open"); + } + if (LibusbWin.usb_reset(usb_dev_handle) < 0) { + throw new USBException("LibusbWin.usb_reset: " + + LibusbWin.usb_strerror()); + } + usb_dev_handle = 0; + + if (DEBUG_ON) { + System.out.println("resetUsbDevie done"); + } + } + + public static void write_EP1(byte[] data, int length, int timeout) + throws USBException { + if (usb_dev_handle <= 0) { + throw new USBException("Device not open"); + } + if (data == null) { + throw new USBException("data must not be null"); + } + if (length <= 0) { + throw new USBException("size must be > 0"); + } + int lenWritten = LibusbWin.usb_bulk_write(usb_dev_handle, + OUT_Endpoint_1, data, length, timeout); + if (lenWritten < 0) { + if (lenWritten == TIMEOUT_ERROR_CODE) { + throw new USBTimeoutException("LibusbWin.usb_bulk_write EP1: " + + LibusbWin.usb_strerror()); + } + throw new USBException("LibusbWin.usb_bulk_write EP1: " + + LibusbWin.usb_strerror()); + } + + if (DEBUG_ON) { + System.out.print("write_bulkdata EP1: " + lenWritten + + " Bytes sent: "); + for (int i = 0; i < lenWritten; i++) { + System.out.print("0x" + String.format("%1$02X", data[i]) + " "); + } + System.out.println(); + } + } + + public static int read_EP1(byte[] data, int size, int timeout) + throws USBException { + if (usb_dev_handle <= 0) { + throw new USBException("Device not open"); + } + if (data == null) { + throw new USBException("data must not be null"); + } + if (size <= 0) { + throw new USBException("size must be > 0"); + } + int lenRead = LibusbWin.usb_bulk_read(usb_dev_handle, IN_Endpoint_1, + data, size, timeout); + if (lenRead < 0) { + if (lenRead == TIMEOUT_ERROR_CODE) { + throw new USBTimeoutException("LibusbWin.usb_bulk_read EP1: " + + LibusbWin.usb_strerror()); + } + throw new USBException("LibusbWin.usb_bulk_read EP1: " + + LibusbWin.usb_strerror()); + } + + if (DEBUG_ON) { + System.out.print("read_bulkdata EP1: " + lenRead + + " Bytes received: "); + System.out.print("Data: "); + for (int i = 0; i < lenRead; i++) { + System.out.print("0x" + String.format("%1$02X", data[i]) + " "); + } + System.out.println(); + } + return lenRead; + } + + public static void write_EP2(byte[] data, int length, int timeout) + throws USBException { + if (usb_dev_handle <= 0) { + throw new USBException("Device not open"); + } + if (data == null) { + throw new USBException("data must not be null"); + } + if (length <= 0) { + throw new USBException("size must be > 0"); + } + int lenWritten = LibusbWin.usb_bulk_write(usb_dev_handle, + OUT_Endpoint_2, data, length, timeout); + if (lenWritten < 0) { + if (lenWritten == TIMEOUT_ERROR_CODE) { + throw new USBTimeoutException("LibusbWin.usb_bulk_write EP2: " + + LibusbWin.usb_strerror()); + } + throw new USBException("LibusbWin.usb_bulk_write EP2: " + + LibusbWin.usb_strerror()); + } + + if (DEBUG_ON) { + System.out.print("write_bulkdata EP2: " + lenWritten + + " Bytes sent: "); + for (int i = 0; i < lenWritten; i++) { + System.out.print("0x" + String.format("%1$02X", data[i]) + " "); + } + System.out.println(); + } + } + + public static int read_EP2(byte[] data, int size, int timeout) + throws USBException { + if (usb_dev_handle <= 0) { + throw new USBException("Device not open"); + } + if (data == null) { + throw new USBException("data must not be null"); + } + if (size <= 0) { + throw new USBException("size must be > 0"); + } + int lenRead = LibusbWin.usb_bulk_read(usb_dev_handle, IN_Endpoint_2, + data, size, timeout); + if (lenRead < 0) { + if (lenRead == TIMEOUT_ERROR_CODE) { + throw new USBTimeoutException("LibusbWin.usb_bulk_read EP2: " + + LibusbWin.usb_strerror()); + } + throw new USBException("LibusbWin.usb_bulk_read EP2: " + + LibusbWin.usb_strerror()); + } + + if (DEBUG_ON) { + System.out.print("read_bulkdata EP2: " + lenRead + + " Bytes received: "); + System.out.print("Data: "); + for (int i = 0; i < lenRead; i++) { + System.out.print("0x" + String.format("%1$02X", data[i]) + " "); + } + System.out.println(); + } + return lenRead; + } + + private static void claim_interface(int dev_handle, int configuration, + int interface_, int altinterface) throws USBException { + if (LibusbWin.usb_set_configuration(usb_dev_handle, configuration) < 0) { + throw new USBException("LibusbWin.usb_set_configuration: " + + LibusbWin.usb_strerror()); + } + if (LibusbWin.usb_claim_interface(dev_handle, interface_) < 0) { + throw new USBException("LibusbWin.usb_claim_interface: " + + LibusbWin.usb_strerror()); + } + if (LibusbWin.usb_set_altinterface(dev_handle, altinterface) < 0) { + throw new USBException("LibusbWin.usb_set_altinterface: " + + LibusbWin.usb_strerror()); + } + } + + private static void release_interface(int dev_handle, int interface_) + throws USBException { + if (LibusbWin.usb_release_interface(dev_handle, interface_) < 0) { + throw new USBException("LibusbWin.usb_release_interface: " + + LibusbWin.usb_strerror()); + } + } + + public static short getIdProduct() { + return IdProduct; + } + + public static void setIdProduct(short idProduct) { + IdProduct = idProduct; + } + + public static short getIdVendor() { + return IdVendor; + } + + public static void setIdVendor(short idVendor) { + IdVendor = idVendor; + } + + public static int getConfiguration() { + return Configuration; + } + + public static void setConfiguration(int configuration) { + Configuration = configuration; + } + + public static int getInterface() { + return Interface; + } + + public static void setInterface(int interface_) { + Interface = interface_; + } + + public static int getIN_Endpoint_1() { + return IN_Endpoint_1; + } + + public static void setIN_Endpoint_1(int in_endpoint_1) { + IN_Endpoint_1 = in_endpoint_1; + } + + public static int getOUT_Endpoint_1() { + return OUT_Endpoint_1; + } + + public static void setOUT_Endpoint_1(int out_endpoint_1) { + OUT_Endpoint_1 = out_endpoint_1; + } + + public static int getIN_Endpoint_2() { + return IN_Endpoint_2; + } + + public static void setIN_Endpoint_2(int in_endpoint_2) { + IN_Endpoint_2 = in_endpoint_2; + } + + public static int getOUT_Endpoint_2() { + return OUT_Endpoint_2; + } + + public static void setOUT_Endpoint_2(int out_endpoint_2) { + OUT_Endpoint_2 = out_endpoint_2; + } + + public static int getAltinterface() { + return Altinterface; + } + + public static void setAltinterface(int altinterface) { + Altinterface = altinterface; + } +} diff --git a/mcdp/src/ch/ntb/usb/Utils.java b/mcdp/src/ch/ntb/usb/Utils.java index b2d6edb..a590b7b 100644 --- a/mcdp/src/ch/ntb/usb/Utils.java +++ b/mcdp/src/ch/ntb/usb/Utils.java @@ -41,26 +41,4 @@ public class Utils { bus = bus.next; } } - - public static int openUsb_Device(Usb_Bus bus, short idVendor, short idProduct) { - int handle = -1; - - while (bus != null) { - Usb_Device dev = bus.devices; - while (dev != null) { - // Usb_Device_Descriptor - Usb_Device_Descriptor defDesc = dev.descriptor; - if ((defDesc.idVendor == idVendor) - && (defDesc.idProduct == idProduct)) { - System.out.println("Open device: " + dev.filename); - return LibusbWin.usb_open(dev); - } - dev = dev.next; - } - bus = bus.next; - } - - return handle; - } - } diff --git a/mcdp/src/ch/ntb/usb/logger/LogLevel.java b/mcdp/src/ch/ntb/usb/logger/LogLevel.java new file mode 100644 index 0000000..0c4c0e4 --- /dev/null +++ b/mcdp/src/ch/ntb/usb/logger/LogLevel.java @@ -0,0 +1,13 @@ +package ch.ntb.usb.logger; + +import java.util.logging.Level; + +public class LogLevel extends Level { + + public static final Level DEBUG = new LogLevel("DEBUG", 750); + + protected LogLevel(String name, int value) { + super(name, value); + } + +} diff --git a/mcdp/src/ch/ntb/usb/logger/LogUtil.java b/mcdp/src/ch/ntb/usb/logger/LogUtil.java new file mode 100644 index 0000000..36d0e57 --- /dev/null +++ b/mcdp/src/ch/ntb/usb/logger/LogUtil.java @@ -0,0 +1,12 @@ +package ch.ntb.usb.logger; + +public class LogUtil { + + public static UsbLogger ch_ntb_usb; + + static { + // set all loglevels here + ch_ntb_usb = UsbLogger.getLogger("ch.ntb.usb"); + ch_ntb_usb.setLevel(LogLevel.OFF); + } +} diff --git a/mcdp/src/ch/ntb/usb/logger/UsbLogger.java b/mcdp/src/ch/ntb/usb/logger/UsbLogger.java new file mode 100644 index 0000000..a255841 --- /dev/null +++ b/mcdp/src/ch/ntb/usb/logger/UsbLogger.java @@ -0,0 +1,27 @@ +package ch.ntb.usb.logger; + +import java.util.logging.LogManager; +import java.util.logging.Logger; + +public class UsbLogger extends Logger { + + protected UsbLogger(String name, String resourceBundleName) { + super(name, resourceBundleName); + } + + public void debug(String msg) { + log(LogLevel.DEBUG, msg); + } + + public static synchronized UsbLogger getLogger(String name) { + LogManager manager = LogManager.getLogManager(); + UsbLogger result = (UsbLogger) manager.getLogger(name); + if (result == null) { + result = new UsbLogger(name, null); + manager.addLogger(result); + result = (UsbLogger) manager.getLogger(name); + } + result.setLevel(null); + return result; + } +} diff --git a/mcdp/src/ch/ntb/usb/test/TestAppUsb.java b/mcdp/src/ch/ntb/usb/test/TestAppUsb_old.java similarity index 57% rename from mcdp/src/ch/ntb/usb/test/TestAppUsb.java rename to mcdp/src/ch/ntb/usb/test/TestAppUsb_old.java index 0823911..ad889f1 100644 --- a/mcdp/src/ch/ntb/usb/test/TestAppUsb.java +++ b/mcdp/src/ch/ntb/usb/test/TestAppUsb_old.java @@ -1,11 +1,11 @@ package ch.ntb.usb.test; import ch.ntb.usb.LibusbWin; +import ch.ntb.usb.USBException; +import ch.ntb.usb.USB_old; import ch.ntb.usb.Usb_Bus; -import ch.ntb.usb.Usb_Device; -import ch.ntb.usb.Usb_Device_Descriptor; -public class TestAppUsb { +public class TestAppUsb_old { static Usb_Bus bus; @@ -31,97 +31,31 @@ public class TestAppUsb { static boolean dev_opened = false; - static void reset() { - bus = null; - usb_dev_handle = 0; - dev_opened = false; - System.out.println("reset done"); - } - static void openUsbDevice() { - - int res; - - // open bus - if (bus == null) { - LibusbWin.usb_init(); - LibusbWin.usb_find_busses(); - LibusbWin.usb_find_devices(); - - bus = LibusbWin.usb_get_busses(); - if (bus == null) { - System.err.println("Error on LibusbWin.usb_get_busses(): " - + LibusbWin.usb_strerror()); - return; - } + try { + USB_old.openUsbDevice(); + } catch (USBException e) { + e.printStackTrace(); } - // search for device - dev_opened = false; - if (usb_dev_handle <= 0) { - - while (bus != null) { - Usb_Device dev = bus.devices; - while (dev != null) { - // Usb_Device_Descriptor - Usb_Device_Descriptor defDesc = dev.descriptor; - if ((defDesc.idVendor == IdVendor) - && (defDesc.idProduct == IdProduct)) { - System.out.println("Open device: " + dev.filename); - res = LibusbWin.usb_open(dev); - if (res <= 0) { - System.err.println("Error on LibusbWin.usb_open: " - + LibusbWin.usb_strerror()); - return; - } else { - usb_dev_handle = res; - } - } - dev = dev.next; - } - bus = bus.next; - } - if (usb_dev_handle <= 0) { - System.out.println("UsbDevice with idVendor 0x" - + Integer.toHexString(IdVendor) + " and idProduct 0x" - + Integer.toHexString(IdProduct) + " not found"); - return; - } - } - if (!claim_interface(usb_dev_handle, CONFIGURATION, INTERFACE, - ALTINTERFACE)) { - System.err.println("Error on claim_interface"); - return; - } - dev_opened = true; System.out.println("device opened, interface claimed"); } static void closeUsbDevice() { - if (!release_interface(usb_dev_handle, INTERFACE)) { - System.err.println("Error on release_interface"); + try { + USB_old.closeUsbDevice(); + } catch (USBException e) { + e.printStackTrace(); } - int res = LibusbWin.usb_close(usb_dev_handle); - if (res < 0) { - System.err.println("Error on LibusbWin.usb_close: " - + LibusbWin.usb_strerror()); - } - dev_opened = false; - bus = null; - usb_dev_handle = -1; System.out.println("device closed"); } static void write(byte[] data, int length) { - if (!dev_opened) { - System.out.println("Open Device first"); - return; - } - int res = LibusbWin.usb_bulk_write(usb_dev_handle, OUT_ENDPOINT, data, - length, TIMEOUT); - if (res < 0) { - System.err.println("Error on LibusbWin.usb_bulk_write: " - + LibusbWin.usb_strerror()); + try { + USB_old.write_EP1(data, length, TIMEOUT); + } catch (USBException e) { + e.printStackTrace(); } + int res = 0; System.out.print("write_bulkdata: " + res + " Bytes sent: "); for (int i = 0; i < res; i++) { System.out.print("0x" + String.format("%1$02X", data[i]) + " "); diff --git a/mcdp/src/ch/ntb/usb/test/TestApp.java b/mcdp/src/ch/ntb/usb/test/TestApp_old.java similarity index 89% rename from mcdp/src/ch/ntb/usb/test/TestApp.java rename to mcdp/src/ch/ntb/usb/test/TestApp_old.java index 424fe52..3c03c72 100644 --- a/mcdp/src/ch/ntb/usb/test/TestApp.java +++ b/mcdp/src/ch/ntb/usb/test/TestApp_old.java @@ -11,7 +11,7 @@ import org.eclipse.swt.widgets.Group; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Text; -public class TestApp { +public class TestApp_old { private static final int HEX_WIDTH = 5; private Shell rootShell = null; // @jve:decl-index=0:visual-constraint="10,10" @@ -98,10 +98,10 @@ public class TestApp { vendorID = new Text(vendorIDGroup, SWT.BORDER | SWT.RIGHT); vendorID.setBounds(new org.eclipse.swt.graphics.Rectangle(7,23,76,19)); vendorID.setText("0x8235"); - TestAppUsb.IdVendor = (short) parseInt(vendorID.getText()); + TestAppUsb_old.IdVendor = (short) parseInt(vendorID.getText()); vendorID.addModifyListener(new org.eclipse.swt.events.ModifyListener() { public void modifyText(org.eclipse.swt.events.ModifyEvent e) { - TestAppUsb.IdVendor = (short) parseInt(vendorID.getText()); + TestAppUsb_old.IdVendor = (short) parseInt(vendorID.getText()); } }); } @@ -116,10 +116,10 @@ public class TestApp { productID = new Text(productIDGroup, SWT.BORDER | SWT.RIGHT); productID.setBounds(new org.eclipse.swt.graphics.Rectangle(4,24,76,19)); productID.setText("0x0100"); - TestAppUsb.IdProduct = (short) parseInt(productID.getText()); + TestAppUsb_old.IdProduct = (short) parseInt(productID.getText()); productID.addModifyListener(new org.eclipse.swt.events.ModifyListener() { public void modifyText(org.eclipse.swt.events.ModifyEvent e) { - TestAppUsb.IdProduct = (short) parseInt(productID.getText()); + TestAppUsb_old.IdProduct = (short) parseInt(productID.getText()); } }); } @@ -136,7 +136,7 @@ public class TestApp { configuration.setText("1"); configuration.addModifyListener(new org.eclipse.swt.events.ModifyListener() { public void modifyText(org.eclipse.swt.events.ModifyEvent e) { - TestAppUsb.CONFIGURATION = parseInt(configuration.getText()); + TestAppUsb_old.CONFIGURATION = parseInt(configuration.getText()); } }); } @@ -153,7 +153,7 @@ public class TestApp { interface_.setText("0"); interface_.addModifyListener(new org.eclipse.swt.events.ModifyListener() { public void modifyText(org.eclipse.swt.events.ModifyEvent e) { - TestAppUsb.INTERFACE = parseInt(interface_.getText()); + TestAppUsb_old.INTERFACE = parseInt(interface_.getText()); } }); } @@ -170,7 +170,7 @@ public class TestApp { altInt.setText("0"); altInt.addModifyListener(new org.eclipse.swt.events.ModifyListener() { public void modifyText(org.eclipse.swt.events.ModifyEvent e) { - TestAppUsb.ALTINTERFACE = parseInt(altInt.getText()); + TestAppUsb_old.ALTINTERFACE = parseInt(altInt.getText()); } }); } @@ -228,7 +228,7 @@ public class TestApp { outEP.setText("0x02"); outEP.addModifyListener(new org.eclipse.swt.events.ModifyListener() { public void modifyText(org.eclipse.swt.events.ModifyEvent e) { - TestAppUsb.OUT_ENDPOINT = parseInt(outEP.getText()); + TestAppUsb_old.OUT_ENDPOINT = parseInt(outEP.getText()); } }); } @@ -245,7 +245,7 @@ public class TestApp { inEP.setText("0x86"); inEP.addModifyListener(new org.eclipse.swt.events.ModifyListener() { public void modifyText(org.eclipse.swt.events.ModifyEvent e) { - TestAppUsb.IN_ENDPOINT = parseInt(inEP.getText()); + TestAppUsb_old.IN_ENDPOINT = parseInt(inEP.getText()); } }); } @@ -262,7 +262,7 @@ public class TestApp { timeout.setText("2000"); timeout.addModifyListener(new org.eclipse.swt.events.ModifyListener() { public void modifyText(org.eclipse.swt.events.ModifyEvent e) { - TestAppUsb.TIMEOUT = parseInt(timeout.getText()); + TestAppUsb_old.TIMEOUT = parseInt(timeout.getText()); } }); } @@ -297,7 +297,7 @@ public class TestApp { sendButton.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() { public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) { byte[] b = parseByteArray(dataField.getText()); - TestAppUsb.write(b, b.length); + TestAppUsb_old.write(b, b.length); } }); recButton = new Button(dataButtonComp, SWT.NONE); @@ -305,7 +305,7 @@ public class TestApp { recButton.setText("Receive"); recButton.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() { public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) { - TestAppUsb.read(); + TestAppUsb_old.read(); } }); } @@ -339,7 +339,7 @@ public class TestApp { devOpenButton .addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() { public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) { - TestAppUsb.openUsbDevice(); + TestAppUsb_old.openUsbDevice(); } }); devCloseButton = new Button(devButtonComp, SWT.NONE); @@ -348,13 +348,13 @@ public class TestApp { resetButton.setText("Reset"); resetButton.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() { public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) { - TestAppUsb.reset(); + System.out.println("not implemented"); } }); devCloseButton .addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() { public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) { - TestAppUsb.closeUsbDevice(); + TestAppUsb_old.closeUsbDevice(); } }); } @@ -380,7 +380,7 @@ public class TestApp { } public static void main(String[] args) { - TestApp app = new TestApp(); + TestApp_old app = new TestApp_old(); app.createSShell(); app.rootShell.open(); diff --git a/mcdp/src/ch/ntb/usb/test/TestGUI.java b/mcdp/src/ch/ntb/usb/test/TestGUI.java new file mode 100644 index 0000000..fedb14a --- /dev/null +++ b/mcdp/src/ch/ntb/usb/test/TestGUI.java @@ -0,0 +1,395 @@ +package ch.ntb.usb.test; + +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; + private Text vendorID = null; + private Group productIDGroup = null; + private Text productID = null; + private Group configGroup = null; + private Text configuration = null; + private Group interfaceGroup = null; + private Text interface_ = null; + private Group altIntGroup = null; + private Text altInt = null; + private Group deviceGroup = null; + private Group endpointGroup = null; + private Group deviceGroup2 = null; + private Group outEPGroup = null; + private Text outEP = null; + private Group inEPGroup = null; + private Text inEP = null; + private Group timeoutGroup = null; + private 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; + private Text dataField = null; + private Button resetButton = null; + /** + * This method initializes sShell + */ + private 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); + } else { + // is decimal number + return Integer.parseInt(s); + } + } + + private 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("0x8235"); + 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("0x0100"); + 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("1"); + 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("0"); + 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("0"); + 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("0x02"); + 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("0x86"); + 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("2000"); + 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() { + 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() { + 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() { + 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() { + public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) { + TestImplementation.resetUsbDevice(); + } + }); + devCloseButton + .addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() { + 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); + // 33 5B 02 01 00 05 01 03 07 0F 7F 1F + dataField.setText("0x5B 0x02 0x01 0x00 0x05 0x01 0x03 0x07 0x0F 0x7F 0x1F"); + 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(); + } + } + } +} diff --git a/mcdp/src/ch/ntb/usb/test/TestImplementation.java b/mcdp/src/ch/ntb/usb/test/TestImplementation.java new file mode 100644 index 0000000..f4e4400 --- /dev/null +++ b/mcdp/src/ch/ntb/usb/test/TestImplementation.java @@ -0,0 +1,100 @@ +package ch.ntb.usb.test; + +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.Usb_Bus; + +public class TestImplementation { + + private static Logger logger = Logger.getLogger("ch.ntb.usb.test"); + + static Usb_Bus bus; + + static int usb_dev_handle = 0; + + static short IdVendor = 0x04B4; + + static short IdProduct = (short) 0x1004; + + static int TIMEOUT = 1000; + + static int CONFIGURATION = 1; + + static int INTERFACE = 0; + + static int ALTINTERFACE = 0; + + static int OUT_ENDPOINT = 0x02; + + static int IN_ENDPOINT = 0x86; + + 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.bulkwrite(OUT_ENDPOINT, data, length, TIMEOUT); + 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[USB.MAX_DATA_SIZE]; + int lenRead = 0; + try { + lenRead = dev.bulkread(IN_ENDPOINT, data, + USB.MAX_DATA_SIZE, TIMEOUT); + 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(); + } + } +} diff --git a/mcdp/src/ch/ntb/usb/test/TestUsb.java b/mcdp/src/ch/ntb/usb/test/TestUsb_old.java similarity index 68% rename from mcdp/src/ch/ntb/usb/test/TestUsb.java rename to mcdp/src/ch/ntb/usb/test/TestUsb_old.java index c7e8888..47c496f 100644 --- a/mcdp/src/ch/ntb/usb/test/TestUsb.java +++ b/mcdp/src/ch/ntb/usb/test/TestUsb_old.java @@ -4,7 +4,7 @@ import java.io.IOException; import ch.ntb.usb.*; -public class TestUsb { +public class TestUsb_old { static Usb_Bus bus; @@ -26,39 +26,57 @@ public class TestUsb { static final int MAX_BYTEARRAY_SIZE = 512; - static final int PACKET_HEADER_1 = 0x33; // first byte of header - static final int PACKET_HEADER_2 = 0x5B; // second byte of header - static final int PACKET_END = 0x1F; // last byte of packet - static final int PACKET_DATA_OFFSET = 6; // offset to the first byte of data - static final int PACKET_MIN_LENGTH = 7; // minimal Length of a packet (no payload) + static final int PACKET_HEADER_1 = 0x33; // first byte of header + + static final int PACKET_HEADER_2 = 0x5B; // second byte of header + + static final int PACKET_END = 0x1F; // last byte of packet + + static final int PACKET_DATA_OFFSET = 6; // offset to the first byte of + + // data + + static final int PACKET_MIN_LENGTH = 7; // minimal Length of a packet (no + + // payload) + + // Main Types + static final int MTYPE_ERROR = 0x01; // Errors before dispatching data - -// Main Types - static final int MTYPE_ERROR = 0x01; // Errors before dispatching data static final int MTYPE_BDI = 0x02; + static final int MTYPE_UART_1 = 0x03; -// Sub Types -// ERRORS - static final int STYPE_ERROR_HEADER = 0x01; // Header of packet wrong - static final int STYPE_ERROR_PACKET_END = 0x02; // Packet end wrong + // Sub Types + // ERRORS + static final int STYPE_ERROR_HEADER = 0x01; // Header of packet wrong -// BDI - static final int STYPE_BDI_35IN = 0x01; // 35 Bit Packet to BDI - static final int STYPE_BDI_35OUT = 0x02; // 35 Bit Packet from BDI - static final int STYPE_BDI_10IN = 0x03; // 10 Bit Packet to BDI - static final int STYPE_BDI_10OUT = 0x04; // 10 Bit Packet from BDI - static final int STYPE_BDI_FD_DATA = 0x05; // Fast Download Data - static final int STYPE_BDI_ERROR_FD_LENGTH = 0x06; // Error if length in FD packet too small + static final int STYPE_ERROR_PACKET_END = 0x02; // Packet end wrong -// UART 1 - static final int STYPE_UART_1_IN = 0x11; // Data to UART 1 - static final int STYPE_UART_1_OUT = 0x22; // Data from UART 1 + // BDI + static final int STYPE_BDI_35IN = 0x01; // 35 Bit Packet to BDI + + static final int STYPE_BDI_35OUT = 0x02; // 35 Bit Packet from BDI + + static final int STYPE_BDI_10IN = 0x03; // 10 Bit Packet to BDI + + static final int STYPE_BDI_10OUT = 0x04; // 10 Bit Packet from BDI + + static final int STYPE_BDI_FD_DATA = 0x05; // Fast Download Data + + static final int STYPE_BDI_ERROR_FD_LENGTH = 0x06; // Error if length in FD + + // packet too small + + // UART 1 + static final int STYPE_UART_1_IN = 0x11; // Data to UART 1 + + static final int STYPE_UART_1_OUT = 0x22; // Data from UART 1 private static void do_read(Usb_Bus bus, int dev_handle) { byte[] data = new byte[MAX_BYTEARRAY_SIZE]; - int res = read_bulkdata(dev_handle, CONFIGURATION, INTERFACE, ALTINTERFACE, - IN_ENDPOINT, data, MAX_BYTEARRAY_SIZE, TIMEOUT); + int res = read_bulkdata(dev_handle, CONFIGURATION, INTERFACE, + ALTINTERFACE, IN_ENDPOINT, data, MAX_BYTEARRAY_SIZE, TIMEOUT); if (res <= 0) { System.err.println("Error on read_bulkdata " + LibusbWin.usb_strerror()); @@ -106,7 +124,7 @@ public class TestUsb { } private static void openDevice(Usb_Bus bus) { - int handle = Utils.openUsb_Device(bus, EZ_USB_DevKit_idVendor, + int handle = openUsb_Device(bus, EZ_USB_DevKit_idVendor, EZ_USB_DevKit_idProduct); if (handle > 0) { System.out.println("Usb_device_handle: " + handle); @@ -170,23 +188,23 @@ public class TestUsb { private static void do_write(Usb_Bus bus, int dev_handle) { // byte[] data = new String("Data to send...").getBytes(); byte[] data = new byte[512]; - data[0] = PACKET_HEADER_1; // header - data[1] = (byte) PACKET_HEADER_2; // header - data[2] = MTYPE_BDI; // header - data[3] = STYPE_BDI_35IN; // header - data[4] = 0x00; // length of payload + data[0] = PACKET_HEADER_1; // header + data[1] = (byte) PACKET_HEADER_2; // header + data[2] = MTYPE_BDI; // header + data[3] = STYPE_BDI_35IN; // header + data[4] = 0x00; // length of payload data[5] = 0x05; - data[6] = 0x01; // payload + data[6] = 0x01; // payload data[7] = 0x03; data[8] = 0x07; data[9] = 0x0F; data[10] = 0x7F; - data[11] = (byte) PACKET_END; // packet end + data[11] = (byte) PACKET_END; // packet end int length = 12; - int res = write_bulkdata(dev_handle, CONFIGURATION, INTERFACE, ALTINTERFACE, - OUT_ENDPOINT, data, length, TIMEOUT); - if ( res <= 0) { + int res = write_bulkdata(dev_handle, CONFIGURATION, INTERFACE, + ALTINTERFACE, OUT_ENDPOINT, data, length, TIMEOUT); + if (res <= 0) { System.err.println("Error on write_bulkdata"); return; } @@ -209,7 +227,7 @@ public class TestUsb { } private static void do_write_read(Usb_Bus bus) { - int dev_handle = Utils.openUsb_Device(bus, EZ_USB_DevKit_idVendor, + int dev_handle = openUsb_Device(bus, EZ_USB_DevKit_idVendor, EZ_USB_DevKit_idProduct); if (dev_handle <= 0) { System.err.println("Error on openUsb_Device: " + dev_handle); @@ -217,7 +235,7 @@ public class TestUsb { } boolean run = true; char c = 'a'; - while(run){ + while (run) { try { c = (char) System.in.read(); } catch (IOException e) { @@ -241,6 +259,28 @@ public class TestUsb { LibusbWin.usb_close(dev_handle); } + public static int openUsb_Device(Usb_Bus bus, short idVendor, + short idProduct) { + int handle = -1; + + while (bus != null) { + Usb_Device dev = bus.devices; + while (dev != null) { + // Usb_Device_Descriptor + Usb_Device_Descriptor defDesc = dev.descriptor; + if ((defDesc.idVendor == idVendor) + && (defDesc.idProduct == idProduct)) { + System.out.println("Open device: " + dev.filename); + return LibusbWin.usb_open(dev); + } + dev = dev.next; + } + bus = bus.next; + } + + return handle; + } + public static void main(String[] args) { LibusbWin.usb_init(); LibusbWin.usb_find_busses(); @@ -259,4 +299,5 @@ public class TestUsb { System.out.println("LibusbWin done"); } + }