org.lsst.ccs.drivers.usb
Class UsbComm

java.lang.Object
  extended by org.lsst.ccs.drivers.usb.UsbComm

public class UsbComm
extends java.lang.Object

Communicates with a USB device

Author:
Owen Saxton

Field Summary
static int CFG_ATTR_REMOTE_WAKE
          Configuration attribute - remote wakeup
static int CFG_ATTR_SELF_POWER
          Configuration attribute - self-powered
static int DEV_CLASS_CDC_CTRL
          Device class - CDC control
static int DEV_CLASS_DIAGNOSTIC
          Device class - diagnostic
static int DEV_CLASS_HUB
          Device class - hub
static int DEV_CLASS_INTERFACE
          Device class - see interface
static int DEV_CLASS_MISC
          Device class - miscellaneous
static int DEV_CLASS_VENDOR_SPEC
          Device class - vendor specific
static int DEV_SPEED_FULL
          Device speed - full
static int DEV_SPEED_LOW
          Device speed - low
static int DEV_SPEED_UNDEFINED
          Device speed - undefined
static int DEV_SPEED_UNKNOWN
          Device speed - unknown
static int EP_ATTR_BULK
          Endpoint attribute - bulk data
static int EP_ATTR_CONTROL
          Endpoint attribute - control
static int EP_ATTR_INTERRUPT
          Endpoint attribute - interrupt
static int EP_ATTR_ISOCHRONOUS
          Endpoint attribute - isochronous
static int IFC_CLASS_APP_SPEC
          Interface class - application specific
static int IFC_CLASS_AUDIO
          Interface class - audio
static int IFC_CLASS_CDC_CTRL
          Interface class - CDC control
static int IFC_CLASS_CDC_DATA
          Interface class - CDC data
static int IFC_CLASS_CONTENT_SEC
          Interface class security x
static int IFC_CLASS_DIAGNOSTIC
          Interface class - diagnostic
static int IFC_CLASS_HEALTHCARE
          Interface class - personal healthcare
static int IFC_CLASS_HID
          Interface class - human interface
static int IFC_CLASS_IMAGE
          Interface class - still image
static int IFC_CLASS_MASS_STORE
          Interface class - mass storage
static int IFC_CLASS_MISC
          Interface class - miscellaneous
static int IFC_CLASS_PHYSICAL
          Interface class - physical
static int IFC_CLASS_PRINTER
          Interface class - printer
static int IFC_CLASS_SMART_CARD
          Interface class - smart card
static int IFC_CLASS_VENDOR_SPEC
          Interface class - vendor specific
static int IFC_CLASS_VIDEO
          Interface class - video
static int IFC_CLASS_WIRELESS
          Interface class - wireless
 
Constructor Summary
UsbComm()
          Constructor
 
Method Summary
 void close()
          Closes the connection to the device
 UsbDevice findDevice(int vid, int pid, java.lang.String serial)
          Finds a USB device and sets it
static java.util.List findDevices(int vid, int pid, java.lang.String serial)
          Finds USB devices
 int flush()
          Flushes any pending read data
 UsbConfiguration getConfiguration()
          Gets the current USB configuration
 UsbDevice getDevice()
          Gets the USB device
 UsbEndpoint getEndpoint(int epnum)
          Gets a USB endpoint for the current open device
static UsbEndpoint getEndpoint(UsbInterface iface, int epnum)
          Gets a USB endpoint for the specified interface
 UsbInterface getInterface()
          Gets the USB interface being used
 UsbInterface getInterface(int iface)
          Gets the specified USB interface of the current device
static UsbInterface getInterface(UsbDevice device, int iface)
          Gets the specified USB interface of the specified device
 int getSpeed()
          Gets the speed of the current device
static int getSpeed(UsbDevice devc)
          Gets the speed of a device
 void open(int iface, int ends, boolean force)
          Opens a connection to a device
 int read(byte[] data, int timeout)
          Reads data from the device
 int read(byte[] data, int offs, int leng, int timeout)
          Reads data from the device
 void setDevice(UsbDevice device)
          Sets the USB device
 int write(byte[] data)
          Writes data to the device
 int write(byte[] data, int offs, int leng)
          Writes data to the device
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEV_SPEED_UNDEFINED

public static final int DEV_SPEED_UNDEFINED
Device speed - undefined

See Also:
Constant Field Values

DEV_SPEED_UNKNOWN

public static final int DEV_SPEED_UNKNOWN
Device speed - unknown

See Also:
Constant Field Values

DEV_SPEED_LOW

public static final int DEV_SPEED_LOW
Device speed - low

See Also:
Constant Field Values

DEV_SPEED_FULL

public static final int DEV_SPEED_FULL
Device speed - full

See Also:
Constant Field Values

DEV_CLASS_INTERFACE

public static final int DEV_CLASS_INTERFACE
Device class - see interface

See Also:
Constant Field Values

DEV_CLASS_CDC_CTRL

public static final int DEV_CLASS_CDC_CTRL
Device class - CDC control

See Also:
Constant Field Values

DEV_CLASS_HUB

public static final int DEV_CLASS_HUB
Device class - hub

See Also:
Constant Field Values

DEV_CLASS_DIAGNOSTIC

public static final int DEV_CLASS_DIAGNOSTIC
Device class - diagnostic

See Also:
Constant Field Values

DEV_CLASS_MISC

public static final int DEV_CLASS_MISC
Device class - miscellaneous

See Also:
Constant Field Values

DEV_CLASS_VENDOR_SPEC

public static final int DEV_CLASS_VENDOR_SPEC
Device class - vendor specific

See Also:
Constant Field Values

CFG_ATTR_SELF_POWER

public static final int CFG_ATTR_SELF_POWER
Configuration attribute - self-powered

See Also:
Constant Field Values

CFG_ATTR_REMOTE_WAKE

public static final int CFG_ATTR_REMOTE_WAKE
Configuration attribute - remote wakeup

See Also:
Constant Field Values

IFC_CLASS_AUDIO

public static final int IFC_CLASS_AUDIO
Interface class - audio

See Also:
Constant Field Values

IFC_CLASS_CDC_CTRL

public static final int IFC_CLASS_CDC_CTRL
Interface class - CDC control

See Also:
Constant Field Values

IFC_CLASS_HID

public static final int IFC_CLASS_HID
Interface class - human interface

See Also:
Constant Field Values

IFC_CLASS_PHYSICAL

public static final int IFC_CLASS_PHYSICAL
Interface class - physical

See Also:
Constant Field Values

IFC_CLASS_IMAGE

public static final int IFC_CLASS_IMAGE
Interface class - still image

See Also:
Constant Field Values

IFC_CLASS_PRINTER

public static final int IFC_CLASS_PRINTER
Interface class - printer

See Also:
Constant Field Values

IFC_CLASS_MASS_STORE

public static final int IFC_CLASS_MASS_STORE
Interface class - mass storage

See Also:
Constant Field Values

IFC_CLASS_CDC_DATA

public static final int IFC_CLASS_CDC_DATA
Interface class - CDC data

See Also:
Constant Field Values

IFC_CLASS_SMART_CARD

public static final int IFC_CLASS_SMART_CARD
Interface class - smart card

See Also:
Constant Field Values

IFC_CLASS_CONTENT_SEC

public static final int IFC_CLASS_CONTENT_SEC
Interface class security x

See Also:
Constant Field Values

IFC_CLASS_VIDEO

public static final int IFC_CLASS_VIDEO
Interface class - video

See Also:
Constant Field Values

IFC_CLASS_HEALTHCARE

public static final int IFC_CLASS_HEALTHCARE
Interface class - personal healthcare

See Also:
Constant Field Values

IFC_CLASS_DIAGNOSTIC

public static final int IFC_CLASS_DIAGNOSTIC
Interface class - diagnostic

See Also:
Constant Field Values

IFC_CLASS_WIRELESS

public static final int IFC_CLASS_WIRELESS
Interface class - wireless

See Also:
Constant Field Values

IFC_CLASS_MISC

public static final int IFC_CLASS_MISC
Interface class - miscellaneous

See Also:
Constant Field Values

IFC_CLASS_APP_SPEC

public static final int IFC_CLASS_APP_SPEC
Interface class - application specific

See Also:
Constant Field Values

IFC_CLASS_VENDOR_SPEC

public static final int IFC_CLASS_VENDOR_SPEC
Interface class - vendor specific

See Also:
Constant Field Values

EP_ATTR_CONTROL

public static final int EP_ATTR_CONTROL
Endpoint attribute - control

See Also:
Constant Field Values

EP_ATTR_ISOCHRONOUS

public static final int EP_ATTR_ISOCHRONOUS
Endpoint attribute - isochronous

See Also:
Constant Field Values

EP_ATTR_BULK

public static final int EP_ATTR_BULK
Endpoint attribute - bulk data

See Also:
Constant Field Values

EP_ATTR_INTERRUPT

public static final int EP_ATTR_INTERRUPT
Endpoint attribute - interrupt

See Also:
Constant Field Values
Constructor Detail

UsbComm

public UsbComm()
        throws UsbException
Constructor

Throws:
UsbException
Method Detail

findDevices

public static java.util.List findDevices(int vid,
                                         int pid,
                                         java.lang.String serial)
                                  throws UsbException
Finds USB devices

The tree of USB devices is searched, looking for devices whose vendor and product IDs match the ones specified. If a non-null serial number is provided, this must be matched as well; otherwise the first ID match is used.

Parameters:
vid - The vendor ID, or -1 if any vendor ID matches.
pid - The product ID, or -1 if any product ID matches.
serial - The serial number, or null if the serial number is not to be matched.
Returns:
The list of matching UsbDevices, empty if no matches found.
Throws:
UsbException

findDevice

public UsbDevice findDevice(int vid,
                            int pid,
                            java.lang.String serial)
                     throws UsbException
Finds a USB device and sets it

The tree of USB devices is searched, looking for devices whose vendor and product IDs match the ones specified. If a non-null serial number is provided, this must be matched as well; otherwise the first ID match is used. The current device is set to the resultant device, possibly null.

Parameters:
vid - The vendor ID
pid - The product ID
serial - The serial number, or null if the serial number is not to be matched.
Returns:
The matching UsbDevice or null if no match found.
Throws:
UsbException - if a device is already open.

getDevice

public UsbDevice getDevice()
Gets the USB device

Returns:
The USB device object

setDevice

public void setDevice(UsbDevice device)
               throws UsbException
Sets the USB device

Parameters:
device - The UsbDevice object to set. Can be null.
Throws:
UsbException - if a device is already open.

getConfiguration

public UsbConfiguration getConfiguration()
Gets the current USB configuration

Returns:
The USB configuration object

getInterface

public UsbInterface getInterface()
Gets the USB interface being used

Returns:
The USB interface object

getInterface

public UsbInterface getInterface(int iface)
Gets the specified USB interface of the current device

Parameters:
iface - The number, starting from 0, of the interface .
Returns:
The USB interface object, or null

getInterface

public static UsbInterface getInterface(UsbDevice device,
                                        int iface)
Gets the specified USB interface of the specified device

Parameters:
device - The USB device
iface - The number, starting from 0, of the interface .
Returns:
The USB interface object, or null

getEndpoint

public UsbEndpoint getEndpoint(int epnum)
Gets a USB endpoint for the current open device

Parameters:
epnum - The endpoint to get: 0 = in, 1 = out.
Returns:
The USB endpoint object, or null

getEndpoint

public static UsbEndpoint getEndpoint(UsbInterface iface,
                                      int epnum)
Gets a USB endpoint for the specified interface

Parameters:
iface - The USB interface to use
epnum - The endpoint to get: 0 = in, anything else = out.
Returns:
The USB endpoint object

getSpeed

public int getSpeed()
Gets the speed of the current device

Returns:
The speed of the device, encoded locally.

getSpeed

public static int getSpeed(UsbDevice devc)
Gets the speed of a device

Returns:
The speed of the device, encoded locally.

open

public void open(int iface,
                 int ends,
                 boolean force)
          throws UsbException
Opens a connection to a device

The device is assumed to be configured, and the specified interface is used. The interface must have an endpoint of each requested type.

Parameters:
iface - The number of the interface to use, starting at 0.
ends - The endpoints to be opened: 0 = in and out, 1 = in, 2 = out, otherwise in and out.
force - Set to true if claiming the device interface is to be forced, allowing the use of devices claimed by another driver or user.
Throws:
UsbException - if the device has not been set or the requested interface or endpoints do not exist.
UsbClaimException - if the interface cannot be claimed.

close

public void close()
           throws UsbException
Closes the connection to the device

Throws:
UsbException - if the device is not open.

write

public int write(byte[] data)
          throws UsbException
Writes data to the device

Parameters:
data - Byte array of data to be written.
Returns:
The number of bytes of data actually written.
Throws:
UsbException - if the out endpoint is not open.

write

public int write(byte[] data,
                 int offs,
                 int leng)
          throws UsbException
Writes data to the device

Parameters:
data - Byte array containing the data to be written.
offs - The offset of the first byte to be written.
leng - The number of bytes to be written.
Returns:
The number of bytes of data actually written.
Throws:
UsbException - if the out endpoint is not open.

read

public int read(byte[] data,
                int timeout)
         throws UsbException,
                UsbTimeoutException
Reads data from the device

Parameters:
data - Byte array to receive the read data.
timeout - The maximum time, in milliseconds, to wait for data.
Returns:
The number of bytes read.
Throws:
UsbException - if the in endpoint is not open.
UsbTimeoutException - if the read times out.

read

public int read(byte[] data,
                int offs,
                int leng,
                int timeout)
         throws UsbException,
                UsbTimeoutException
Reads data from the device

Parameters:
data - Byte array to receive the read data.
offs - The offset in data where the read data is to start.
leng - The number of bytes of data to read.
timeout - The maximum time, in milliseconds, to wait for data.
Returns:
The number of bytes read.
Throws:
UsbException - if the in endpoint is not open.
UsbTimeoutException - if the read times out.

flush

public int flush()
          throws UsbException
Flushes any pending read data

Returns:
The number of bytes flushed.
Throws:
UsbException


Copyright © 2012 LSST. All Rights Reserved.