1
0
mirror of https://github.com/mik3y/usb-serial-for-android.git synced 2026-08-15 02:13:01 +00:00

read w/o timeout now throws exception on connection lost or buffer to small

SerialInputOutputManager already returned connection lost exception, as the next read failed
This commit is contained in:
kai-morich
2021-03-22 08:57:16 +01:00
parent 2d4d2f78a5
commit f4166f34a0
10 changed files with 138 additions and 45 deletions
@@ -155,7 +155,7 @@ public abstract class CommonUsbSerialPort implements UsbSerialPort {
byte[] buf = new byte[2];
int len = mConnection.controlTransfer(0x80 /*DEVICE*/, 0 /*GET_STATUS*/, 0, 0, buf, buf.length, 200);
if(len < 0)
throw new IOException("USB get_status request failed");
throw new IOException("Connection lost, USB get_status request failed");
}
@Override
@@ -183,7 +183,7 @@ public abstract class CommonUsbSerialPort implements UsbSerialPort {
long endTime = testConnection ? MonotonicClock.millis() + timeout : 0;
int readMax = Math.min(dest.length, MAX_READ_SIZE);
nread = mConnection.bulkTransfer(mReadEndpoint, dest, readMax, timeout);
// Android error propagation is improvable, nread == -1 can be: timeout, connection lost, buffer undersized, ...
// Android error propagation is improvable, nread == -1 can be: timeout, connection lost, buffer to small
if(nread == -1 && testConnection && MonotonicClock.millis() < endTime)
testConnection();
@@ -197,11 +197,15 @@ public abstract class CommonUsbSerialPort implements UsbSerialPort {
throw new IOException("Waiting for USB request failed");
}
nread = buf.position();
if(nread == 0) {
if(dest.length % mReadEndpoint.getMaxPacketSize() != 0) {
throw new IOException("Connection lost or buffer to small");
} else {
throw new IOException("Connection lost");
}
}
}
if (nread > 0)
return nread;
else
return 0;
return Math.max(nread, 0);
}
@Override
@@ -162,7 +162,7 @@ public class FtdiSerialDriver implements UsbSerialDriver {
return readFilter(dest, nread);
}
private int readFilter(byte[] buffer, int totalBytesRead) throws IOException {
protected int readFilter(byte[] buffer, int totalBytesRead) throws IOException {
final int maxPacketSize = mReadEndpoint.getMaxPacketSize();
int destPos = 0;
for(int srcPos = 0; srcPos < totalBytesRead; srcPos += maxPacketSize) {
@@ -217,7 +217,7 @@ public class ProlificSerialDriver implements UsbSerialDriver {
IOException readStatusException = mReadStatusException;
if (mReadStatusException != null) {
mReadStatusException = null;
throw readStatusException;
throw new IOException(readStatusException);
}
return mStatus;