Skip to content

Commit 6cbd50c

Browse files
committed
Indefinitely block on interrupt/bulk reads but allow aborting after
each USB timeout. Timeout is now 5 seconds instead of 2.5
1 parent 065d7cc commit 6cbd50c

File tree

3 files changed

+22
-7
lines changed

3 files changed

+22
-7
lines changed

src/main/java/org/usb4java/javax/AbstractIrpQueue.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ abstract class AbstractIrpQueue<T extends UsbIrp>
3232

3333
/** The queue processor thread. */
3434
private volatile Thread processor;
35+
36+
/** If queue is currently aborting. */
37+
protected volatile boolean aborting;
3538

3639
/** The USB device. */
3740
private final AbstractDevice device;
@@ -152,6 +155,7 @@ final void process()
152155
*/
153156
public final void abort()
154157
{
158+
this.aborting = true;
155159
this.irps.clear();
156160
while (isBusy())
157161
{
@@ -167,6 +171,7 @@ public final void abort()
167171
Thread.currentThread().interrupt();
168172
}
169173
}
174+
this.aborting = false;
170175
}
171176

172177
/**

src/main/java/org/usb4java/javax/Config.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ final class Config
1818
private static final String KEY_BASE = "org.usb4java.javax.";
1919

2020
/** The default USB communication timeout in milliseconds. */
21-
private static final int DEFAULT_TIMEOUT = 2500;
21+
private static final int DEFAULT_TIMEOUT = 5000;
2222

2323
/** The default scan interval in milliseconds. */
2424
private static final int DEFAULT_SCAN_INTERVAL = 500;

src/main/java/org/usb4java/javax/IrpQueue.java

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -188,13 +188,20 @@ private int transfer(final DeviceHandle handle,
188188
final UsbEndpointDescriptor descriptor, final int type,
189189
final ByteBuffer buffer) throws UsbException
190190
{
191+
final byte address = descriptor.bEndpointAddress();
192+
final int timeout = getConfig().getTimeout();
193+
final boolean in = this.pipe.getUsbEndpoint().getDirection() ==
194+
UsbConst.ENDPOINT_DIRECTION_IN;
191195
final IntBuffer transferred = IntBuffer.allocate(1);
192196
int result;
193197
if (type == UsbConst.ENDPOINT_TYPE_BULK)
194198
{
195-
result = LibUsb.bulkTransfer(handle,
196-
descriptor.bEndpointAddress(), buffer, transferred,
197-
getConfig().getTimeout());
199+
do
200+
{
201+
result = LibUsb.bulkTransfer(handle, address, buffer,
202+
transferred, timeout);
203+
}
204+
while (in && result == LibUsb.ERROR_TIMEOUT && !this.aborting);
198205
if (result < 0)
199206
{
200207
throw new LibUsbException(
@@ -203,9 +210,12 @@ private int transfer(final DeviceHandle handle,
203210
}
204211
else if (type == UsbConst.ENDPOINT_TYPE_INTERRUPT)
205212
{
206-
result = LibUsb.interruptTransfer(handle,
207-
descriptor.bEndpointAddress(), buffer, transferred,
208-
getConfig().getTimeout());
213+
do
214+
{
215+
result = LibUsb.interruptTransfer(handle, address, buffer,
216+
transferred, timeout);
217+
}
218+
while (in && result == LibUsb.ERROR_TIMEOUT && !this.aborting);
209219
if (result < 0)
210220
{
211221
throw new LibUsbException(

0 commit comments

Comments
 (0)