1
0
mirror of https://github.com/mik3y/usb-serial-for-android synced 2025-06-08 00:16:13 +00:00

CdcAcm driver: cancel read() on close()

This commit is contained in:
Kai Morich 2019-10-26 20:37:06 +02:00
parent fac8c9f340
commit 800381e370

View File

@ -50,6 +50,7 @@ public class CdcAcmSerialDriver implements UsbSerialDriver {
private final UsbDevice mDevice; private final UsbDevice mDevice;
private final UsbSerialPort mPort; private final UsbSerialPort mPort;
private UsbRequest mUsbRequest;
public CdcAcmSerialDriver(UsbDevice device) { public CdcAcmSerialDriver(UsbDevice device) {
mDevice = device; mDevice = device;
@ -253,6 +254,10 @@ public class CdcAcmSerialDriver implements UsbSerialDriver {
if (mConnection == null) { if (mConnection == null) {
throw new IOException("Already closed"); throw new IOException("Already closed");
} }
synchronized (this) {
if (mUsbRequest != null)
mUsbRequest.cancel();
}
mConnection.close(); mConnection.close();
mConnection = null; mConnection = null;
} }
@ -266,8 +271,11 @@ public class CdcAcmSerialDriver implements UsbSerialDriver {
if (!request.queue(buf, dest.length)) { if (!request.queue(buf, dest.length)) {
throw new IOException("Error queueing request."); throw new IOException("Error queueing request.");
} }
mUsbRequest = request;
final UsbRequest response = mConnection.requestWait(); final UsbRequest response = mConnection.requestWait();
synchronized (this) {
mUsbRequest = null;
}
if (response == null) { if (response == null) {
throw new IOException("Null response"); throw new IOException("Null response");
} }
@ -280,6 +288,7 @@ public class CdcAcmSerialDriver implements UsbSerialDriver {
return 0; return 0;
} }
} finally { } finally {
mUsbRequest = null;
request.close(); request.close();
} }
} }