mirror of
https://github.com/mik3y/usb-serial-for-android.git
synced 2026-08-15 02:13:01 +00:00
improve close handling
Use releaseInterface to interrupt read() and terminate SerialInputOutputManager. Previously some drivers used usbRequest.cancel() but this does not interrupt read() on older Android. Added connection check to read(). Before Android 8.0 request.initialize() did not check usbConnection, which can lead to native crash if NULL
This commit is contained in:
+8
-11
@@ -50,7 +50,6 @@ public class CdcAcmSerialDriver implements UsbSerialDriver {
|
||||
|
||||
private final UsbDevice mDevice;
|
||||
private final UsbSerialPort mPort;
|
||||
private UsbRequest mUsbRequest;
|
||||
|
||||
public CdcAcmSerialDriver(UsbDevice device) {
|
||||
mDevice = device;
|
||||
@@ -254,28 +253,27 @@ public class CdcAcmSerialDriver implements UsbSerialDriver {
|
||||
if (mConnection == null) {
|
||||
throw new IOException("Already closed");
|
||||
}
|
||||
synchronized (this) {
|
||||
if (mUsbRequest != null)
|
||||
mUsbRequest.cancel();
|
||||
try {
|
||||
mConnection.releaseInterface(mControlInterface);
|
||||
mConnection.releaseInterface(mDataInterface);
|
||||
mConnection.close();
|
||||
} finally {
|
||||
mConnection = null;
|
||||
}
|
||||
mConnection.close();
|
||||
mConnection = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int read(byte[] dest, int timeoutMillis) throws IOException {
|
||||
final UsbRequest request = new UsbRequest();
|
||||
try {
|
||||
if(mConnection == null)
|
||||
throw new IOException("Connection closed");
|
||||
request.initialize(mConnection, mReadEndpoint);
|
||||
final ByteBuffer buf = ByteBuffer.wrap(dest);
|
||||
if (!request.queue(buf, dest.length)) {
|
||||
throw new IOException("Error queueing request.");
|
||||
}
|
||||
mUsbRequest = request;
|
||||
final UsbRequest response = mConnection.requestWait();
|
||||
synchronized (this) {
|
||||
mUsbRequest = null;
|
||||
}
|
||||
if (response == null) {
|
||||
throw new IOException("Null response");
|
||||
}
|
||||
@@ -288,7 +286,6 @@ public class CdcAcmSerialDriver implements UsbSerialDriver {
|
||||
return 0;
|
||||
}
|
||||
} finally {
|
||||
mUsbRequest = null;
|
||||
request.close();
|
||||
}
|
||||
}
|
||||
|
||||
+4
-10
@@ -85,7 +85,6 @@ public class Ch34xSerialDriver implements UsbSerialDriver {
|
||||
|
||||
private UsbEndpoint mReadEndpoint;
|
||||
private UsbEndpoint mWriteEndpoint;
|
||||
private UsbRequest mUsbRequest;
|
||||
|
||||
public Ch340SerialPort(UsbDevice device, int portNumber) {
|
||||
super(device, portNumber);
|
||||
@@ -144,11 +143,9 @@ public class Ch34xSerialDriver implements UsbSerialDriver {
|
||||
if (mConnection == null) {
|
||||
throw new IOException("Already closed");
|
||||
}
|
||||
synchronized (this) {
|
||||
if (mUsbRequest != null)
|
||||
mUsbRequest.cancel();
|
||||
}
|
||||
try {
|
||||
for (int i = 0; i < mDevice.getInterfaceCount(); i++)
|
||||
mConnection.releaseInterface(mDevice.getInterface(i));
|
||||
mConnection.close();
|
||||
} finally {
|
||||
mConnection = null;
|
||||
@@ -160,16 +157,14 @@ public class Ch34xSerialDriver implements UsbSerialDriver {
|
||||
public int read(byte[] dest, int timeoutMillis) throws IOException {
|
||||
final UsbRequest request = new UsbRequest();
|
||||
try {
|
||||
if(mConnection == null)
|
||||
throw new IOException("Connection closed");
|
||||
request.initialize(mConnection, mReadEndpoint);
|
||||
final ByteBuffer buf = ByteBuffer.wrap(dest);
|
||||
if (!request.queue(buf, dest.length)) {
|
||||
throw new IOException("Error queueing request.");
|
||||
}
|
||||
mUsbRequest = request;
|
||||
final UsbRequest response = mConnection.requestWait();
|
||||
synchronized (this) {
|
||||
mUsbRequest = null;
|
||||
}
|
||||
if (response == null) {
|
||||
throw new IOException("Null response");
|
||||
}
|
||||
@@ -182,7 +177,6 @@ public class Ch34xSerialDriver implements UsbSerialDriver {
|
||||
return 0;
|
||||
}
|
||||
} finally {
|
||||
mUsbRequest = null;
|
||||
request.close();
|
||||
}
|
||||
}
|
||||
|
||||
+3
-11
@@ -108,7 +108,6 @@ public class Cp21xxSerialDriver implements UsbSerialDriver {
|
||||
|
||||
private UsbEndpoint mReadEndpoint;
|
||||
private UsbEndpoint mWriteEndpoint;
|
||||
private UsbRequest mUsbRequest;
|
||||
|
||||
// second port of Cp2105 has limited baudRate, dataBits, stopBits, parity
|
||||
// unsupported baudrate returns error at controlTransfer(), other parameters are silently ignored
|
||||
@@ -177,16 +176,12 @@ public class Cp21xxSerialDriver implements UsbSerialDriver {
|
||||
if (mConnection == null) {
|
||||
throw new IOException("Already closed");
|
||||
}
|
||||
synchronized (this) {
|
||||
if(mUsbRequest != null) {
|
||||
mUsbRequest.cancel();
|
||||
}
|
||||
}
|
||||
try {
|
||||
setConfigSingle(SILABSER_IFC_ENABLE_REQUEST_CODE, UART_DISABLE);
|
||||
} catch (Exception ignored)
|
||||
{}
|
||||
try {
|
||||
mConnection.releaseInterface(mDevice.getInterface(mPortNumber));
|
||||
mConnection.close();
|
||||
} finally {
|
||||
mConnection = null;
|
||||
@@ -197,16 +192,14 @@ public class Cp21xxSerialDriver implements UsbSerialDriver {
|
||||
public int read(byte[] dest, int timeoutMillis) throws IOException {
|
||||
final UsbRequest request = new UsbRequest();
|
||||
try {
|
||||
if(mConnection == null)
|
||||
throw new IOException("Connection closed");
|
||||
request.initialize(mConnection, mReadEndpoint);
|
||||
final ByteBuffer buf = ByteBuffer.wrap(dest);
|
||||
if (!request.queue(buf, dest.length)) {
|
||||
throw new IOException("Error queueing request.");
|
||||
}
|
||||
mUsbRequest = request;
|
||||
final UsbRequest response = mConnection.requestWait();
|
||||
synchronized (this) {
|
||||
mUsbRequest = null;
|
||||
}
|
||||
if (response == null) {
|
||||
throw new IOException("Null response");
|
||||
}
|
||||
@@ -219,7 +212,6 @@ public class Cp21xxSerialDriver implements UsbSerialDriver {
|
||||
return 0;
|
||||
}
|
||||
} finally {
|
||||
mUsbRequest = null;
|
||||
request.close();
|
||||
}
|
||||
}
|
||||
|
||||
+3
@@ -268,6 +268,7 @@ public class FtdiSerialDriver implements UsbSerialDriver {
|
||||
throw new IOException("Already closed");
|
||||
}
|
||||
try {
|
||||
mConnection.releaseInterface(mDevice.getInterface(mPortNumber));
|
||||
mConnection.close();
|
||||
} finally {
|
||||
mConnection = null;
|
||||
@@ -280,6 +281,8 @@ public class FtdiSerialDriver implements UsbSerialDriver {
|
||||
final UsbRequest request = new UsbRequest();
|
||||
final ByteBuffer buf = ByteBuffer.wrap(dest);
|
||||
try {
|
||||
if(mConnection == null)
|
||||
throw new IOException("Connection closed");
|
||||
request.initialize(mConnection, endpoint);
|
||||
if (!request.queue(buf, dest.length)) {
|
||||
throw new IOException("Error queueing request.");
|
||||
|
||||
+3
@@ -362,6 +362,7 @@ public class ProlificSerialDriver implements UsbSerialDriver {
|
||||
} finally {
|
||||
try {
|
||||
mConnection.releaseInterface(mDevice.getInterface(0));
|
||||
mConnection.close();
|
||||
} finally {
|
||||
mConnection = null;
|
||||
}
|
||||
@@ -372,6 +373,8 @@ public class ProlificSerialDriver implements UsbSerialDriver {
|
||||
public int read(byte[] dest, int timeoutMillis) throws IOException {
|
||||
final UsbRequest request = new UsbRequest();
|
||||
try {
|
||||
if(mConnection == null)
|
||||
throw new IOException("Connection closed");
|
||||
request.initialize(mConnection, mReadEndpoint);
|
||||
final ByteBuffer buf = ByteBuffer.wrap(dest);
|
||||
if (!request.queue(buf, dest.length)) {
|
||||
|
||||
Reference in New Issue
Block a user