Improved error handling.

Changed visibility of all classes except UsbServicesImpl to package private.
This commit is contained in:
Klaus Reimer 2011-02-05 15:46:51 +01:00 committed by k
parent 7b4194c285
commit 9864bb3235
21 changed files with 94 additions and 73 deletions

View File

@ -1,20 +1,7 @@
JSR-80 implementation checklist:
JSR-80:
* Do extensive tests with USB pipes. interrupt data transfer seems to work
(MouseDriver demo is working) but never used bulk transfer and it's unclear
if the queue implementation is working correctly under stress.
[ ] UsbConfiguration
[x] UsbConfigurationDescriptor
[x] UsbDescriptor
[ ] UsbDevice
[x] UsbDeviceDescriptor
[x] UsbDeviceListener
[ ] UsbEndpoint
[x] UsbEndpointDescriptor
[ ] UsbHub
[ ] UsbInterface
[x] UsbInterfaceDescriptor
[ ] UsbPipe
[X] UsbPipeListener
[ ] UsbPort
[ ] UsbServices
[X] UsbServicesListener
[x] UsbStringDescritor

View File

@ -16,7 +16,6 @@ import static de.ailis.usb4java.USB.usb_get_string;
import static de.ailis.usb4java.USB.usb_open;
import static de.ailis.usb4java.USB.usb_release_interface;
import static de.ailis.usb4java.USB.usb_set_configuration;
import static de.ailis.usb4java.USB.usb_strerror;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
@ -132,8 +131,8 @@ abstract class AbstractDevice implements UsbDevice
this.handle = usb_open(this.device);
if (this.handle == null)
{
throw new UsbException("Can't open device "
+ this.device + ": " + usb_strerror());
throw new LibUsbException("Can't open device "
+ this.device);
}
}
finally
@ -159,11 +158,10 @@ abstract class AbstractDevice implements UsbDevice
USBLock.acquire();
try
{
if (usb_close(this.handle) < 0)
{
throw new UsbException("Can't close device "
+ this.device + ": " + usb_strerror());
}
final int result = usb_close(this.handle);
if (result < 0)
throw new LibUsbException("Can't close device "
+ this.device, result);
}
finally
{
@ -361,7 +359,9 @@ abstract class AbstractDevice implements UsbDevice
try
{
final int result = usb_set_configuration(open(), number & 0xff);
if (result < 0) throw new UsbException(usb_strerror());
if (result < 0)
throw new LibUsbException("Unable to set configuration",
result);
this.activeConfigurationNumber = number;
}
finally
@ -403,7 +403,9 @@ abstract class AbstractDevice implements UsbDevice
}
final int result = usb_claim_interface(open(), number & 0xff);
if (result < 0) throw new UsbException(usb_strerror());
if (result < 0)
throw new LibUsbException("Unable to claim interface",
result);
this.claimedInterfaceNumber = number;
}
finally
@ -436,7 +438,8 @@ abstract class AbstractDevice implements UsbDevice
try
{
final int result = usb_release_interface(open(), number & 0xff);
if (result < 0) throw new UsbException(usb_strerror());
if (result < 0)
throw new LibUsbException("Unable to release interface", result);
this.claimedInterfaceNumber = null;
}
finally
@ -510,9 +513,8 @@ abstract class AbstractDevice implements UsbDevice
final ByteBuffer buffer = ByteBuffer.allocateDirect(256);
final int len = usb_get_string(handle, index, langid, buffer);
if (len < 0)
throw new UsbException("Unable to get string descriptor "
+ index
+ " from device " + this.device + ": " + len);
throw new LibUsbException("Unable to get string descriptor "
+ index + " from device " + this.device, len);
return new UsbStringDescriptorImpl(
new USB_String_Descriptor(buffer));
}
@ -553,12 +555,11 @@ abstract class AbstractDevice implements UsbDevice
final int len = usb_get_descriptor(handle, USB_DT_STRING, 0,
buffer);
if (len < 0)
throw new UsbException(
"Unable to get string descriptor languages: "
+ usb_strerror());
throw new LibUsbException(
"Unable to get string descriptor languages", len);
if (len < 2)
throw new UsbException("Illegal descriptor length: "
+ usb_strerror());
throw new UsbException("Received illegal descriptor length: "
+ len);
final short[] languages = new short[(len - 2) / 2];
if (languages.length == 0) return languages;
buffer.position(2);
@ -580,15 +581,6 @@ abstract class AbstractDevice implements UsbDevice
@Override
public final void syncSubmit(final UsbControlIrp irp) throws UsbException
{
if ((irp.bRequest() == UsbConst.REQUEST_SET_CONFIGURATION)
&& (irp.bmRequestType() == 0))
{
final int result2 = usb_set_configuration(open(), irp.wValue());
if (result2 < 0) throw new UsbException(usb_strerror());
return;
}
USBLock.acquire();
try
{
@ -600,7 +592,9 @@ abstract class AbstractDevice implements UsbDevice
final int len = usb_control_msg(handle, irp.bmRequestType(),
irp.bRequest(),
irp.wValue(), irp.wIndex(), buffer, 250);
if (len < 0) throw new UsbException(usb_strerror());
if (len < 0)
throw new LibUsbException("Unable to submit control message",
len);
buffer.rewind();
buffer.get(irp.getData(), 0, len);
irp.setActualLength(len);

View File

@ -19,7 +19,7 @@ import java.util.List;
* The event listener type.
*/
public abstract class EventListenerList<T extends EventListener>
abstract class EventListenerList<T extends EventListener>
{
/** The list with registered listeners. */
protected final List<T> listeners = Collections

View File

@ -16,7 +16,7 @@ import javax.usb.UsbException;
* @author Klaus Reimer (k@ailis.de)
*/
public class LibUsbException extends UsbException
class LibUsbException extends UsbException
{
/** Serial version UID. */
private static final long serialVersionUID = 1L;
@ -25,6 +25,37 @@ public class LibUsbException extends UsbException
private final int errorCode;
/**
* Constructor.
*
* @param message
* The error message.
* @param errorCode
* The error code.
*/
public LibUsbException(final String message, final int errorCode)
{
super(String.format("USB error %d: %s: %s", -errorCode, message,
usb_strerror()));
this.errorCode = errorCode;
}
/**
* Constructor.
*
* @param message
* The error message.
*/
public LibUsbException(final String message)
{
super(String.format("USB error: %s: %s", message, usb_strerror()));
this.errorCode = 0;
}
/**
* Constructor.
*
@ -34,7 +65,7 @@ public class LibUsbException extends UsbException
public LibUsbException(final int errorCode)
{
super(String.format("USB error %d: %s", errorCode, usb_strerror()));
super(String.format("USB error %d: %s", -errorCode, usb_strerror()));
this.errorCode = errorCode;
}

View File

@ -193,12 +193,15 @@ final class PipeQueueProcessor extends Thread
final int ep = descriptor.bEndpointAddress();
final ByteBuffer buffer = ByteBuffer.allocateDirect(size);
final int result = usb_bulk_read(getDevice().open(), ep, buffer, 5000);
if (result < 0) throw new LibUsbException(result);
if (result < 0)
throw new LibUsbException("Unable to read from interrupt endpoint",
result);
buffer.rewind();
buffer.get(data, 0, result);
return result;
}
/**
* Writes the specified bytes to a bulk endpoint.
*
@ -223,7 +226,9 @@ final class PipeQueueProcessor extends Thread
buffer.put(data, written, Math.min(total - written, size));
buffer.rewind();
final int result = usb_bulk_write(handle, ep, buffer, 5000);
if (result < 0) throw new LibUsbException(result);
if (result < 0)
throw new LibUsbException(
"Unable to write to interrupt endpoint", result);
written += result;
buffer.rewind();
}
@ -250,7 +255,9 @@ final class PipeQueueProcessor extends Thread
final ByteBuffer buffer = ByteBuffer.allocateDirect(size);
final int result =
usb_interrupt_read(getDevice().open(), ep, buffer, 5000);
if (result < 0) throw new LibUsbException(result);
if (result < 0)
throw new LibUsbException("Unable to read from interrupt endpoint",
result);
buffer.rewind();
buffer.get(data, 0, result);
return result;
@ -281,7 +288,9 @@ final class PipeQueueProcessor extends Thread
buffer.put(data, written, Math.min(total - written, size));
buffer.rewind();
final int result = usb_interrupt_write(handle, ep, buffer, 5000);
if (result < 0) throw new LibUsbException(result);
if (result < 0)
throw new LibUsbException(
"Unable to write to interrupt endpoint", result);
written += result;
buffer.rewind();
}

View File

@ -16,7 +16,7 @@ import de.ailis.usb4java.USB_Config_Descriptor;
* @author Klaus Reimer (k@ailis.de)
*/
public final class UsbConfigurationDescriptorImpl extends
final class UsbConfigurationDescriptorImpl extends
UsbDescriptorImpl<USB_Config_Descriptor> implements
UsbConfigurationDescriptor
{

View File

@ -6,7 +6,6 @@
package de.ailis.usb4java.jsr80;
import static de.ailis.usb4java.USB.usb_set_altinterface;
import static de.ailis.usb4java.USB.usb_strerror;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
@ -33,7 +32,7 @@ import de.ailis.usb4java.USB_Interface_Descriptor;
* @author Klaus Reimer (k@ailis.de)
*/
public final class UsbConfigurationImpl implements UsbConfiguration
final class UsbConfigurationImpl implements UsbConfiguration
{
/** The JSR 80 USB configuration descriptor. */
private final UsbConfigurationDescriptor descriptor;
@ -187,7 +186,9 @@ public final class UsbConfigurationImpl implements UsbConfiguration
{
final int result = usb_set_altinterface(this.device.open(),
iface.getUsbInterfaceDescriptor().bAlternateSetting());
if (result < 0) throw new UsbException(usb_strerror());
if (result < 0)
throw new LibUsbException(
"Unable to set alternate interface", result);
this.activeSettings.put(number & 0xff, iface);
}
finally

View File

@ -18,7 +18,7 @@ import de.ailis.usb4java.USB_Descriptor_Header;
* The descriptor type.
*/
public abstract class UsbDescriptorImpl<T extends USB_Descriptor_Header>
abstract class UsbDescriptorImpl<T extends USB_Descriptor_Header>
implements UsbDescriptor
{
/** The low level USB descriptor header. */

View File

@ -16,7 +16,7 @@ import de.ailis.usb4java.USB_Device_Descriptor;
* @author Klaus Reimer (k@ailis.de)
*/
public final class UsbDeviceDescriptorImpl extends
final class UsbDeviceDescriptorImpl extends
UsbDescriptorImpl<USB_Device_Descriptor> implements UsbDeviceDescriptor
{
/**

View File

@ -14,7 +14,7 @@ import de.ailis.usb4java.USB_Device;
* @author Klaus Reimer (k@ailis.de)
*/
public final class UsbDeviceImpl extends AbstractDevice
final class UsbDeviceImpl extends AbstractDevice
{
/**
* Constructor.

View File

@ -17,7 +17,7 @@ import javax.usb.event.UsbDeviceListener;
* @author Klaus Reimer (k@ailis.de)
*/
public final class UsbDeviceListenerList extends
final class UsbDeviceListenerList extends
EventListenerList<UsbDeviceListener> implements UsbDeviceListener
{
/**

View File

@ -16,7 +16,7 @@ import de.ailis.usb4java.USB_Endpoint_Descriptor;
* @author Klaus Reimer (k@ailis.de)
*/
public final class UsbEndpointDescriptorImpl extends
final class UsbEndpointDescriptorImpl extends
UsbDescriptorImpl<USB_Endpoint_Descriptor> implements
UsbEndpointDescriptor
{

View File

@ -18,7 +18,7 @@ import javax.usb.UsbPipe;
* @author Klaus Reimer (k@ailis.de)
*/
public final class UsbEndpointImpl implements UsbEndpoint
final class UsbEndpointImpl implements UsbEndpoint
{
/** The USB interface this endpoint belongs to. */
private final UsbInterfaceImpl iface;

View File

@ -20,8 +20,7 @@ import de.ailis.usb4java.USB_Device;
* @author Klaus Reimer (k@ailis.de)
*/
public final class UsbHubImpl extends AbstractDevice implements UsbHub,
UsbPorts
final class UsbHubImpl extends AbstractDevice implements UsbHub, UsbPorts
{
/** The hub ports. */
private final UsbPortsImpl ports = new UsbPortsImpl(this);

View File

@ -16,7 +16,7 @@ import de.ailis.usb4java.USB_Interface_Descriptor;
* @author Klaus Reimer (k@ailis.de)
*/
public final class UsbInterfaceDescriptorImpl extends
final class UsbInterfaceDescriptorImpl extends
UsbDescriptorImpl<USB_Interface_Descriptor> implements
UsbInterfaceDescriptor
{

View File

@ -33,7 +33,7 @@ import de.ailis.usb4java.USB_Interface_Descriptor;
* @author Klaus Reimer (k@ailis.de)
*/
public final class UsbInterfaceImpl implements UsbInterface
final class UsbInterfaceImpl implements UsbInterface
{
/** The USB configuration. */
private final UsbConfigurationImpl configuration;

View File

@ -28,7 +28,7 @@ import javax.usb.util.DefaultUsbIrp;
* @author Klaus Reimer (k@ailis.de)
*/
public final class UsbPipeImpl implements UsbPipe
final class UsbPipeImpl implements UsbPipe
{
/** The endpoint this pipe belongs to. */
private final UsbEndpointImpl endpoint;

View File

@ -16,7 +16,7 @@ import javax.usb.event.UsbPipeListener;
* @author Klaus Reimer (k@ailis.de)
*/
public final class UsbPipeListenerList extends
final class UsbPipeListenerList extends
EventListenerList<UsbPipeListener> implements UsbPipeListener
{
/**

View File

@ -16,7 +16,7 @@ import javax.usb.UsbPort;
* @author Klaus Reimer (k@ailis.de)
*/
public final class UsbPortImpl implements UsbPort
final class UsbPortImpl implements UsbPort
{
/** The USB hub this port belongs to. */
private final UsbHub hub;

View File

@ -15,7 +15,7 @@ import javax.usb.event.UsbServicesListener;
* @author Klaus Reimer (k@ailis.de)
*/
public final class UsbServicesListenerList extends
final class UsbServicesListenerList extends
EventListenerList<UsbServicesListener> implements UsbServicesListener
{
/**

View File

@ -16,7 +16,7 @@ import de.ailis.usb4java.USB_String_Descriptor;
* @author Klaus Reimer (k@ailis.de)
*/
public final class UsbStringDescriptorImpl extends
final class UsbStringDescriptorImpl extends
UsbDescriptorImpl<USB_String_Descriptor> implements UsbStringDescriptor
{
/**