Line 42... |
Line 42... |
public class Ztex1 {
|
public class Ztex1 {
|
private final int maxDevNum = 1023;
|
private final int maxDevNum = 1023;
|
private long handle;
|
private long handle;
|
private ZtexDevice1 dev = null;
|
private ZtexDevice1 dev = null;
|
private boolean oldDevices[] = new boolean[maxDevNum+1];
|
private boolean oldDevices[] = new boolean[maxDevNum+1];
|
|
private int oldDevNum = -1;
|
private String usbBusName = null;
|
private String usbBusName = null;
|
private boolean[] interfaceClaimed = new boolean[256];
|
private boolean[] interfaceClaimed = new boolean[256];
|
|
private boolean configurationSet = false;
|
/** * Setting to true enables certain workarounds, e.g. to deal with bad driver/OS implementations. */
|
/** * Setting to true enables certain workarounds, e.g. to deal with bad driver/OS implementations. */
|
public boolean certainWorkarounds = false;
|
public boolean certainWorkarounds = false;
|
/** * The timeout for control messages in ms. */
|
/** * The timeout for control messages in ms. */
|
public int controlMsgTimeout = 1000; // in ms
|
public int controlMsgTimeout = 1000; // in ms
|
private long lastVendorCommandT = 0;
|
private long lastVendorCommandT = 0;
|
Line 318... |
Line 320... |
* @throws UsbException if an error occurs while attempting to set the configuration.
|
* @throws UsbException if an error occurs while attempting to set the configuration.
|
*/
|
*/
|
public void setConfiguration ( int config) throws UsbException{
|
public void setConfiguration ( int config) throws UsbException{
|
if ( LibusbJava.usb_set_configuration(handle(), config) < 0 )
|
if ( LibusbJava.usb_set_configuration(handle(), config) < 0 )
|
throw new UsbException("Setting configuration to " + config + " failed: " + LibusbJava.usb_strerror());
|
throw new UsbException("Setting configuration to " + config + " failed: " + LibusbJava.usb_strerror());
|
|
configurationSet = true;
|
}
|
}
|
|
|
|
|
// ******* trySetConfiguration ****************************************************
|
// ******* trySetConfiguration ****************************************************
|
/**
|
/**
|
Line 330... |
Line 333... |
* @param config The configuration number (usually 1)
|
* @param config The configuration number (usually 1)
|
*/
|
*/
|
public void trySetConfiguration ( int config) {
|
public void trySetConfiguration ( int config) {
|
if ( LibusbJava.usb_set_configuration(handle(), config) < 0 )
|
if ( LibusbJava.usb_set_configuration(handle(), config) < 0 )
|
System.err.println("Setting configuration to " + config + " failed: " + LibusbJava.usb_strerror());
|
System.err.println("Setting configuration to " + config + " failed: " + LibusbJava.usb_strerror());
|
|
configurationSet = true;
|
}
|
}
|
|
|
|
|
// ******* getInterfaceClaimed *************************************************
|
// ******* getInterfaceClaimed *************************************************
|
/**
|
/**
|
Line 351... |
Line 355... |
* Claims an interface.
|
* Claims an interface.
|
* @param iface The interface number (usually 0)
|
* @param iface The interface number (usually 0)
|
* @throws UsbException if an error occurs while attempting to claim the interface.
|
* @throws UsbException if an error occurs while attempting to claim the interface.
|
*/
|
*/
|
public void claimInterface ( int iface) throws UsbException{
|
public void claimInterface ( int iface) throws UsbException{
|
|
if ( ! configurationSet )
|
|
trySetConfiguration(1);
|
if ( ( iface<0 || iface>=256 || (! interfaceClaimed[iface]) ) && ( LibusbJava.usb_claim_interface(handle(), iface) < 0 ) )
|
if ( ( iface<0 || iface>=256 || (! interfaceClaimed[iface]) ) && ( LibusbJava.usb_claim_interface(handle(), iface) < 0 ) )
|
throw new UsbException("Claiming interface " + iface + " failed: " + LibusbJava.usb_strerror());
|
throw new UsbException("Claiming interface " + iface + " failed: " + LibusbJava.usb_strerror());
|
if ( iface>=0 && iface < 256 )
|
if ( iface>=0 && iface < 256 )
|
interfaceClaimed[iface]=true;
|
interfaceClaimed[iface]=true;
|
}
|
}
|
Line 374... |
Line 380... |
}
|
}
|
|
|
|
|
// ******* findOldDevices ******************************************************
|
// ******* findOldDevices ******************************************************
|
private synchronized void findOldDevices () throws DeviceLostException {
|
private synchronized void findOldDevices () throws DeviceLostException {
|
Usb_Bus bus = dev.dev().getBus();
|
usbBusName = dev.dev().getBus().getDirname();
|
usbBusName = bus.getDirname();
|
|
|
Usb_Bus bus = LibusbJava.usb_get_busses();
|
|
while ( bus != null && ! bus.getDirname().equals(usbBusName) )
|
|
bus = bus.getNext();
|
|
if ( bus == null )
|
|
throw new DeviceLostException( "findOldDevice: Bus dissapeared" );
|
|
|
for ( int i=0; i<=maxDevNum; i++ )
|
for ( int i=0; i<=maxDevNum; i++ )
|
oldDevices[i] = false;
|
oldDevices[i] = false;
|
|
|
Usb_Device d = bus.getDevices();
|
Usb_Device d = bus.getDevices();
|
Line 389... |
Line 400... |
throw new DeviceLostException( "Device number too large: " + b + " > " + maxDevNum );
|
throw new DeviceLostException( "Device number too large: " + b + " > " + maxDevNum );
|
if ( b > 0 )
|
if ( b > 0 )
|
oldDevices[b] = true;
|
oldDevices[b] = true;
|
d = d.getNext();
|
d = d.getNext();
|
}
|
}
|
oldDevices[dev.dev().getDevnum()] = false;
|
oldDevNum = dev.dev().getDevnum();
|
}
|
}
|
|
|
// ******* findNewDevice *******************************************************
|
// ******* findNewDevice *******************************************************
|
private synchronized Usb_Device findNewDevice ( String errMsg ) throws DeviceLostException {
|
private synchronized Usb_Device findNewDevice ( String errMsg ) throws DeviceLostException {
|
Usb_Device newDev = null;
|
Usb_Device newDev = null;
|
Line 401... |
Line 412... |
LibusbJava.usb_find_devices();
|
LibusbJava.usb_find_devices();
|
|
|
Usb_Bus bus = LibusbJava.usb_get_busses();
|
Usb_Bus bus = LibusbJava.usb_get_busses();
|
while ( bus != null && ! bus.getDirname().equals(usbBusName) )
|
while ( bus != null && ! bus.getDirname().equals(usbBusName) )
|
bus = bus.getNext();
|
bus = bus.getNext();
|
|
if ( bus == null )
|
|
throw new DeviceLostException( "findNewDevice: Bus dissapeared" );
|
|
|
Usb_Device d = bus != null ? bus.getDevices() : null;
|
Usb_Device d = bus != null ? bus.getDevices() : null;
|
while ( d != null ) {
|
while ( d != null ) {
|
byte b = d.getDevnum();
|
byte b = d.getDevnum();
|
if ( b > maxDevNum )
|
if ( b > maxDevNum )
|
Line 422... |
Line 435... |
|
|
// ******* initNewDevice *******************************************************
|
// ******* initNewDevice *******************************************************
|
private void initNewDevice ( String errBase, boolean scanUnconfigured ) throws DeviceLostException, UsbException, InvalidFirmwareException {
|
private void initNewDevice ( String errBase, boolean scanUnconfigured ) throws DeviceLostException, UsbException, InvalidFirmwareException {
|
// scan the bus for up to 60 s for a new device. Boot sequence may take a while.
|
// scan the bus for up to 60 s for a new device. Boot sequence may take a while.
|
Usb_Device newDev = null;
|
Usb_Device newDev = null;
|
for ( int i=0; i<300 && newDev==null; i++ ) {
|
int i;
|
|
for ( i=0; i<300 && newDev==null; i++ ) {
|
try {
|
try {
|
Thread.sleep( 200 );
|
Thread.sleep( 200 );
|
}
|
}
|
catch ( InterruptedException e ) {
|
catch ( InterruptedException e ) {
|
}
|
}
|
|
if ( i > 10 && oldDevNum >= 0 && oldDevNum < maxDevNum )
|
|
oldDevices[oldDevNum ] = false;
|
newDev = findNewDevice( errBase + ": " );
|
newDev = findNewDevice( errBase + ": " );
|
}
|
}
|
|
oldDevNum = -1;
|
if ( newDev == null )
|
if ( newDev == null )
|
throw new DeviceLostException( errBase + ": No new device found" );
|
throw new DeviceLostException( errBase + ": No new device found" );
|
|
|
// init new device
|
// init new device
|
Usb_Device_Descriptor dd = newDev.getDescriptor();
|
Usb_Device_Descriptor dd = newDev.getDescriptor();
|