From b794092c81049d2be6996f8468e075785e2d433b Mon Sep 17 00:00:00 2001 From: Kai Morich Date: Sun, 2 Jun 2024 09:51:19 +0200 Subject: [PATCH] improved error handling for read() with concurrent close() (#569) reworked previous solution from change 8b9ad7ef / v3.7.1 because closeInt() was not working any more --- .../usbserial/driver/CommonUsbSerialPort.java | 24 ++++++++----------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/usbSerialForAndroid/src/main/java/com/hoho/android/usbserial/driver/CommonUsbSerialPort.java b/usbSerialForAndroid/src/main/java/com/hoho/android/usbserial/driver/CommonUsbSerialPort.java index aa3d2209..7762e5ac 100644 --- a/usbSerialForAndroid/src/main/java/com/hoho/android/usbserial/driver/CommonUsbSerialPort.java +++ b/usbSerialForAndroid/src/main/java/com/hoho/android/usbserial/driver/CommonUsbSerialPort.java @@ -34,7 +34,7 @@ public abstract class CommonUsbSerialPort implements UsbSerialPort { protected final int mPortNumber; // non-null when open() - protected UsbDeviceConnection mConnection = null; + protected UsbDeviceConnection mConnection; protected UsbEndpoint mReadEndpoint; protected UsbEndpoint mWriteEndpoint; protected UsbRequest mUsbRequest; @@ -139,18 +139,18 @@ public void close() throws IOException { if (mConnection == null) { throw new IOException("Already closed"); } - UsbDeviceConnection connection = mConnection; - mConnection = null; + UsbRequest usbRequest = mUsbRequest; + mUsbRequest = null; try { - mUsbRequest.cancel(); + usbRequest.cancel(); } catch(Exception ignored) {} - mUsbRequest = null; try { closeInt(); } catch(Exception ignored) {} try { - connection.close(); + mConnection.close(); } catch(Exception ignored) {} + mConnection = null; } protected abstract void closeInt(); @@ -163,7 +163,7 @@ protected void testConnection(boolean full) throws IOException { } protected void testConnection(boolean full, String msg) throws IOException { - if(mConnection == null) { + if(mUsbRequest == null) { throw new IOException("Connection closed"); } if(!full) { @@ -187,9 +187,7 @@ public int read(final byte[] dest, final int timeout) throws IOException { public int read(final byte[] dest, final int length, final int timeout) throws IOException {return read(dest, length, timeout, true);} protected int read(final byte[] dest, int length, final int timeout, boolean testConnection) throws IOException { - if(mConnection == null) { - throw new IOException("Connection closed"); - } + testConnection(false); if(length <= 0) { throw new IllegalArgumentException("Read length too small"); } @@ -240,9 +238,7 @@ public void write(final byte[] src, int length, final int timeout) throws IOExce long startTime = MonotonicClock.millis(); length = Math.min(length, src.length); - if(mConnection == null) { - throw new IOException("Connection closed"); - } + testConnection(false); while (offset < length) { int requestTimeout; final int requestLength; @@ -295,7 +291,7 @@ public void write(final byte[] src, int length, final int timeout) throws IOExce @Override public boolean isOpen() { - return mConnection != null; + return mUsbRequest != null; } @Override