mirror of
https://github.com/mik3y/usb-serial-for-android
synced 2025-06-07 07:56:20 +00:00
write(): throw SerialTimeoutException if connection still valid
This commit is contained in:
parent
2fbceb6cc7
commit
cab862599d
6
.idea/.gitignore
generated
vendored
6
.idea/.gitignore
generated
vendored
@ -2,5 +2,7 @@ caches
|
|||||||
codeStyles
|
codeStyles
|
||||||
libraries
|
libraries
|
||||||
workspace.xml
|
workspace.xml
|
||||||
androidTestResultsUserPreferences.xml
|
androidTestResultsUserPreferences.xml
|
||||||
deploymentTargetDropDown.xml
|
appInsightsSettings.xml
|
||||||
|
deploymentTargetDropDown.xml
|
||||||
|
deploymentTargetSelector.xml
|
@ -50,7 +50,7 @@ project.afterEvaluate {
|
|||||||
// values used for local maven repo, jitpack uses github release:
|
// values used for local maven repo, jitpack uses github release:
|
||||||
groupId 'com.github.mik3y'
|
groupId 'com.github.mik3y'
|
||||||
artifactId 'usb-serial-for-android'
|
artifactId 'usb-serial-for-android'
|
||||||
version '3.5.2beta'
|
version '3.7.3beta'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -159,6 +159,10 @@ public abstract class CommonUsbSerialPort implements UsbSerialPort {
|
|||||||
* use simple USB request supported by all devices to test if connection is still valid
|
* use simple USB request supported by all devices to test if connection is still valid
|
||||||
*/
|
*/
|
||||||
protected void testConnection(boolean full) throws IOException {
|
protected void testConnection(boolean full) throws IOException {
|
||||||
|
testConnection(full, "USB get_status request failed");
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void testConnection(boolean full, String msg) throws IOException {
|
||||||
if(mConnection == null) {
|
if(mConnection == null) {
|
||||||
throw new IOException("Connection closed");
|
throw new IOException("Connection closed");
|
||||||
}
|
}
|
||||||
@ -168,7 +172,7 @@ public abstract class CommonUsbSerialPort implements UsbSerialPort {
|
|||||||
byte[] buf = new byte[2];
|
byte[] buf = new byte[2];
|
||||||
int len = mConnection.controlTransfer(0x80 /*DEVICE*/, 0 /*GET_STATUS*/, 0, 0, buf, buf.length, 200);
|
int len = mConnection.controlTransfer(0x80 /*DEVICE*/, 0 /*GET_STATUS*/, 0, 0, buf, buf.length, 200);
|
||||||
if(len < 0)
|
if(len < 0)
|
||||||
throw new IOException("USB get_status request failed");
|
throw new IOException(msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -233,7 +237,7 @@ public abstract class CommonUsbSerialPort implements UsbSerialPort {
|
|||||||
@Override
|
@Override
|
||||||
public void write(final byte[] src, int length, final int timeout) throws IOException {
|
public void write(final byte[] src, int length, final int timeout) throws IOException {
|
||||||
int offset = 0;
|
int offset = 0;
|
||||||
final long endTime = (timeout == 0) ? 0 : (MonotonicClock.millis() + timeout);
|
long startTime = MonotonicClock.millis();
|
||||||
length = Math.min(length, src.length);
|
length = Math.min(length, src.length);
|
||||||
|
|
||||||
if(mConnection == null) {
|
if(mConnection == null) {
|
||||||
@ -261,7 +265,7 @@ public abstract class CommonUsbSerialPort implements UsbSerialPort {
|
|||||||
if (timeout == 0 || offset == 0) {
|
if (timeout == 0 || offset == 0) {
|
||||||
requestTimeout = timeout;
|
requestTimeout = timeout;
|
||||||
} else {
|
} else {
|
||||||
requestTimeout = (int)(endTime - MonotonicClock.millis());
|
requestTimeout = (int)(startTime + timeout - MonotonicClock.millis());
|
||||||
if(requestTimeout == 0)
|
if(requestTimeout == 0)
|
||||||
requestTimeout = -1;
|
requestTimeout = -1;
|
||||||
}
|
}
|
||||||
@ -271,16 +275,18 @@ public abstract class CommonUsbSerialPort implements UsbSerialPort {
|
|||||||
actualLength = mConnection.bulkTransfer(mWriteEndpoint, writeBuffer, requestLength, requestTimeout);
|
actualLength = mConnection.bulkTransfer(mWriteEndpoint, writeBuffer, requestLength, requestTimeout);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
long elapsed = MonotonicClock.millis() - startTime;
|
||||||
if (DEBUG) {
|
if (DEBUG) {
|
||||||
Log.d(TAG, "Wrote " + actualLength + "/" + requestLength + " offset " + offset + "/" + length + " timeout " + requestTimeout);
|
Log.d(TAG, "Wrote " + actualLength + "/" + requestLength + " offset " + offset + "/" + length + " time " + elapsed + "/" + requestTimeout);
|
||||||
}
|
}
|
||||||
if (actualLength <= 0) {
|
if (actualLength <= 0) {
|
||||||
if (timeout != 0 && MonotonicClock.millis() >= endTime) {
|
String msg = "Error writing " + requestLength + " bytes at offset " + offset + " of total " + src.length + " after " + elapsed + "msec, rc=" + actualLength;
|
||||||
SerialTimeoutException ex = new SerialTimeoutException("Error writing " + requestLength + " bytes at offset " + offset + " of total " + src.length + ", rc=" + actualLength);
|
if (timeout != 0) {
|
||||||
ex.bytesTransferred = offset;
|
testConnection(elapsed < timeout, msg);
|
||||||
throw ex;
|
throw new SerialTimeoutException(msg, offset);
|
||||||
} else {
|
} else {
|
||||||
throw new IOException("Error writing " + requestLength + " bytes at offset " + offset + " of total " + length);
|
throw new IOException(msg);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
offset += actualLength;
|
offset += actualLength;
|
||||||
|
@ -9,7 +9,8 @@ import java.io.InterruptedIOException;
|
|||||||
* {@see InterruptedIOException#bytesTransferred} may contain bytes transferred
|
* {@see InterruptedIOException#bytesTransferred} may contain bytes transferred
|
||||||
*/
|
*/
|
||||||
public class SerialTimeoutException extends InterruptedIOException {
|
public class SerialTimeoutException extends InterruptedIOException {
|
||||||
public SerialTimeoutException(String s) {
|
public SerialTimeoutException(String s, int bytesTransferred) {
|
||||||
super(s);
|
super(s);
|
||||||
|
this.bytesTransferred = bytesTransferred;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user