refactor duplicated code in close method

This commit is contained in:
kai-morich 2019-11-09 22:48:00 +01:00
parent 5767298636
commit 24187b3af6
6 changed files with 29 additions and 67 deletions

View File

@ -239,26 +239,11 @@ public class CdcAcmSerialDriver implements UsbSerialDriver {
}
@Override
public void close() throws IOException {
if (mConnection == null) {
throw new IOException("Already closed");
}
synchronized (this) {
if (mUsbRequest != null)
mUsbRequest.cancel();
}
mControlEndpoint = null;
mReadEndpoint = null;
mWriteEndpoint = null;
public void closeInt() {
try {
mConnection.releaseInterface(mControlInterface);
mConnection.releaseInterface(mDataInterface);
} catch(Exception ignored) {}
try {
mConnection.close();
} finally {
mConnection = null;
}
}
@Override

View File

@ -129,23 +129,11 @@ public class Ch34xSerialDriver implements UsbSerialDriver {
}
@Override
public void close() throws IOException {
if (mConnection == null) {
throw new IOException("Already closed");
}
synchronized (this) {
if (mUsbRequest != null)
mUsbRequest.cancel();
}
public void closeInt() {
try {
for (int i = 0; i < mDevice.getInterfaceCount(); i++)
mConnection.releaseInterface(mDevice.getInterface(i));
} catch(Exception ignored) {}
try {
mConnection.close();
} finally {
mConnection = null;
}
}
private int controlOut(int request, int value, int index) {

View File

@ -109,7 +109,26 @@ public abstract class CommonUsbSerialPort implements UsbSerialPort {
public abstract void open(UsbDeviceConnection connection) throws IOException;
@Override
public abstract void close() throws IOException;
public void close() throws IOException {
if (mConnection == null) {
throw new IOException("Already closed");
}
synchronized (this) {
if (mUsbRequest != null)
mUsbRequest.cancel();
}
try {
closeInt();
} catch(Exception ignored) {}
try {
mConnection.close();
} finally {
mConnection = null;
}
}
protected abstract void closeInt();
@Override
public int read(final byte[] dest, final int timeoutMillis) throws IOException {

View File

@ -166,26 +166,13 @@ public class Cp21xxSerialDriver implements UsbSerialDriver {
}
@Override
public void close() throws IOException {
if (mConnection == null) {
throw new IOException("Already closed");
}
synchronized (this) {
if(mUsbRequest != null) {
mUsbRequest.cancel();
}
}
public void closeInt() {
try {
setConfigSingle(SILABSER_IFC_ENABLE_REQUEST_CODE, UART_DISABLE);
} catch (Exception ignored) {}
try {
mConnection.releaseInterface(mDevice.getInterface(mPortNumber));
} catch(Exception ignored) {}
try {
mConnection.close();
} finally {
mConnection = null;
}
}
private void setBaudRate(int baudRate) throws IOException {

View File

@ -266,18 +266,10 @@ public class FtdiSerialDriver implements UsbSerialDriver {
}
@Override
public void close() throws IOException {
if (mConnection == null) {
throw new IOException("Already closed");
}
public void closeInt() {
try {
mConnection.releaseInterface(mDevice.getInterface(mPortNumber));
} catch(Exception ignored) {}
try {
mConnection.close();
} finally {
mConnection = null;
}
}
private int setBaudRate(int baudRate) throws IOException {

View File

@ -338,10 +338,7 @@ public class ProlificSerialDriver implements UsbSerialDriver {
}
@Override
public void close() throws IOException {
if (mConnection == null) {
throw new IOException("Already closed");
}
public void closeInt() {
try {
mStopReadStatusThread = true;
synchronized (mReadStatusThreadLock) {
@ -354,16 +351,10 @@ public class ProlificSerialDriver implements UsbSerialDriver {
}
}
resetDevice();
} finally {
try {
mConnection.releaseInterface(mDevice.getInterface(0));
} catch(Exception ignored) {}
try {
mConnection.close();
} finally {
mConnection = null;
}
}
} catch(Exception ignored) {}
try {
mConnection.releaseInterface(mDevice.getInterface(0));
} catch(Exception ignored) {}
}
@Override