mirror of
https://github.com/subsurface/subsurface.git
synced 2024-11-30 22:20:21 +00:00
usb-serial-for-android: Implement timeout-handling
Since the Android USB stack and subsequently the usb-serial-for-android driver have problems with read-timeouts, the read-timeout is now implemented in AndroidSerial.java. Also, DC_STATUS_TIMEOUT is returned if there are less bytes returned than expected. Different chipsets seem to behave differently with usb-serial-for-android. On CP210x the read blocks until there is some data here, but on FTDI the chip seems to return whatever is currently in the buffer (so 0 bytes if the buffer is empty). This different behaviour should be mitigated by the changes by this commit. Signed-off-by: Christof Arnosti <charno@charno.ch>
This commit is contained in:
parent
fe932059fe
commit
b15b9c6cd0
2 changed files with 13 additions and 5 deletions
|
@ -19,6 +19,7 @@ import java.util.Queue;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.lang.Math;
|
import java.lang.Math;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
public class AndroidSerial {
|
public class AndroidSerial {
|
||||||
|
|
||||||
|
@ -221,17 +222,20 @@ public class AndroidSerial {
|
||||||
Log.d(TAG, "read length: " + data.length);
|
Log.d(TAG, "read length: " + data.length);
|
||||||
|
|
||||||
int toReadFromHwLength = data.length - readBuffer.size();
|
int toReadFromHwLength = data.length - readBuffer.size();
|
||||||
|
|
||||||
int arraylength = (toReadFromHwLength % 64) != 0 ? toReadFromHwLength + (64 - (toReadFromHwLength % 64)): toReadFromHwLength; // use blocks of 64 for reading
|
int arraylength = (toReadFromHwLength % 64) != 0 ? toReadFromHwLength + (64 - (toReadFromHwLength % 64)): toReadFromHwLength; // use blocks of 64 for reading
|
||||||
|
|
||||||
// When we don't have enough in the buffer, try to read from HW
|
long startTime = (new Date()).getTime();
|
||||||
if (toReadFromHwLength > 0) {
|
|
||||||
|
// while we don't have enough in the buffer, try to read from HW until there is enough or timeout is reached.
|
||||||
|
while (toReadFromHwLength > 0 && (startTime + timeout > (new Date()).getTime() || timeout == 0)) {
|
||||||
// Read and append to buffer
|
// Read and append to buffer
|
||||||
byte[] readFromHwData = new byte[arraylength];
|
byte[] readFromHwData = new byte[arraylength];
|
||||||
int actuallyReadFromHwLength = usbSerialPort.read(readFromHwData, 0); // With this it works... But the timeout is ignored! Fix this!
|
int actuallyReadFromHwLength = usbSerialPort.read(readFromHwData, 0); // This behaves differently on different chipsets. CP210x blocks, FTDI seems to return instantly.
|
||||||
for (int i = 0; i < actuallyReadFromHwLength; i++ ) {
|
for (int i = 0; i < actuallyReadFromHwLength; i++ ) {
|
||||||
readBuffer.add(readFromHwData[i]);
|
readBuffer.add(readFromHwData[i]);
|
||||||
}
|
}
|
||||||
|
toReadFromHwLength = data.length - readBuffer.size();
|
||||||
|
arraylength = (toReadFromHwLength % 64) != 0 ? toReadFromHwLength + (64 - (toReadFromHwLength % 64)): toReadFromHwLength; // use blocks of 64 for reading
|
||||||
}
|
}
|
||||||
|
|
||||||
//Log.d(TAG, "read buffer: " + printQueue(readBuffer));
|
//Log.d(TAG, "read buffer: " + printQueue(readBuffer));
|
||||||
|
|
|
@ -141,7 +141,11 @@ static dc_status_t serial_usb_android_read(void *io, void *data, size_t size, si
|
||||||
env->GetByteArrayRegion(array, 0, retval, (jbyte *) data);
|
env->GetByteArrayRegion(array, 0, retval, (jbyte *) data);
|
||||||
env->DeleteLocalRef(array);
|
env->DeleteLocalRef(array);
|
||||||
TRACE (device->context, "%s: actual read size: %i", __FUNCTION__, retval);
|
TRACE (device->context, "%s: actual read size: %i", __FUNCTION__, retval);
|
||||||
return DC_STATUS_SUCCESS;
|
|
||||||
|
if (retval < size)
|
||||||
|
return DC_STATUS_TIMEOUT;
|
||||||
|
else
|
||||||
|
return DC_STATUS_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue