1
0
mirror of https://github.com/mik3y/usb-serial-for-android synced 2025-06-07 16:06:10 +00:00

throw UnsupportedOperationException instead of returning false

This commit is contained in:
kai-morich 2020-07-31 21:02:59 +02:00
parent 954295456c
commit a664082f23
10 changed files with 109 additions and 93 deletions

View File

@ -15,6 +15,10 @@ android {
'rfc2217_server_nonstandard_baudrates': 'true', // true false false
]
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {

View File

@ -32,6 +32,7 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import static org.junit.Assume.assumeTrue;
@RunWith(AndroidJUnit4.class)
public class CrossoverTest {
@ -51,6 +52,9 @@ public class CrossoverTest {
@Before
public void setUp() throws Exception {
assumeTrue("ignore test for device specific coverage report",
InstrumentationRegistry.getArguments().getString("test_device_driver") == null);
context = InstrumentationRegistry.getContext();
usbManager = (UsbManager) context.getSystemService(Context.USB_SERVICE);
List<UsbSerialDriver> availableDrivers = UsbSerialProber.getDefaultProber().findAllDrivers(usbManager);

View File

@ -960,7 +960,13 @@ public class DeviceTest {
usb.write(buf);
Thread.sleep(50); // ~ 12 bytes
boolean purged = usb.serialPort.purgeHwBuffers(true, false);
boolean purged;
try {
usb.serialPort.purgeHwBuffers(true, false);
purged = true;
} catch (UnsupportedOperationException ex) {
purged = false;
}
usb.write("bcd".getBytes());
Thread.sleep(50);
while(data.length()==0 || data.charAt(data.length()-1)!='d')
@ -984,7 +990,8 @@ public class DeviceTest {
telnet.setParameters(19200, 8, 1, UsbSerialPort.PARITY_NONE);
telnet.write("x".getBytes());
Thread.sleep(10); // ~ 20 bytes
purged = usb.serialPort.purgeHwBuffers(false, true);
if(purged)
usb.serialPort.purgeHwBuffers(false, true);
Log.d(TAG, "purged = " + purged);
telnet.write("y".getBytes());
Thread.sleep(10); // ~ 20 bytes
@ -1299,6 +1306,9 @@ public class DeviceTest {
inputLinesSupported = true;
inputLinesConnected = true;
}
Boolean inputLineFalse = inputLinesSupported ? Boolean.FALSE : null;
Boolean inputLineTrue = inputLinesConnected ? Boolean.TRUE : inputLineFalse;
EnumSet<UsbSerialPort.ControlLine> supportedControlLines = EnumSet.of(UsbSerialPort.ControlLine.RTS, UsbSerialPort.ControlLine.DTR);
if(inputLinesSupported) {
supportedControlLines.add(UsbSerialPort.ControlLine.CTS);
@ -1331,12 +1341,12 @@ public class DeviceTest {
? EnumSet.of(UsbSerialPort.ControlLine.RI)
: EnumSet.noneOf(UsbSerialPort.ControlLine.class),
usb.serialPort.getControlLines());
assertFalse(usb.serialPort.getRTS());
assertFalse(usb.serialPort.getCTS());
assertFalse(usb.serialPort.getDTR());
assertFalse(usb.serialPort.getDSR());
assertFalse(usb.serialPort.getCD());
assertEquals(usb.serialPort.getRI(), inputLinesConnected);
assertThat(usb.getControlLine(usb.serialPort::getRTS), equalTo(Boolean.FALSE));
assertThat(usb.getControlLine(usb.serialPort::getCTS), equalTo(inputLineFalse));
assertThat(usb.getControlLine(usb.serialPort::getDTR), equalTo(Boolean.FALSE));
assertThat(usb.getControlLine(usb.serialPort::getDSR), equalTo(inputLineFalse));
assertThat(usb.getControlLine(usb.serialPort::getCD), equalTo(inputLineFalse));
assertThat(usb.getControlLine(usb.serialPort::getRI), equalTo(inputLineTrue));
telnet.write(data);
if(usb.serialDriver instanceof CdcAcmSerialDriver)
// arduino: control line feedback as serial_state notification is not implemented.
@ -1354,12 +1364,12 @@ public class DeviceTest {
? EnumSet.of(UsbSerialPort.ControlLine.RTS, UsbSerialPort.ControlLine.CTS)
: EnumSet.of(UsbSerialPort.ControlLine.RTS),
usb.serialPort.getControlLines());
assertTrue(usb.serialPort.getRTS());
assertEquals(usb.serialPort.getCTS(), inputLinesConnected);
assertFalse(usb.serialPort.getDTR());
assertFalse(usb.serialPort.getDSR());
assertFalse(usb.serialPort.getCD());
assertFalse(usb.serialPort.getRI());
assertThat(usb.getControlLine(usb.serialPort::getRTS), equalTo(Boolean.TRUE));
assertThat(usb.getControlLine(usb.serialPort::getCTS), equalTo(inputLineTrue));
assertThat(usb.getControlLine(usb.serialPort::getDTR), equalTo(Boolean.FALSE));
assertThat(usb.getControlLine(usb.serialPort::getDSR), equalTo(inputLineFalse));
assertThat(usb.getControlLine(usb.serialPort::getCD), equalTo(inputLineFalse));
assertThat(usb.getControlLine(usb.serialPort::getRI), equalTo(inputLineFalse));
telnet.write(data);
assertThat(Arrays.toString(data), usb.read(4), equalTo(data));
usb.write(data);
@ -1372,12 +1382,12 @@ public class DeviceTest {
? EnumSet.of(UsbSerialPort.ControlLine.RTS, UsbSerialPort.ControlLine.DTR, UsbSerialPort.ControlLine.CD)
: EnumSet.of(UsbSerialPort.ControlLine.RTS, UsbSerialPort.ControlLine.DTR),
usb.serialPort.getControlLines());
assertTrue(usb.serialPort.getRTS());
assertFalse(usb.serialPort.getCTS());
assertTrue(usb.serialPort.getDTR());
assertFalse(usb.serialPort.getDSR());
assertEquals(usb.serialPort.getCD(), inputLinesConnected);
assertFalse(usb.serialPort.getRI());
assertThat(usb.getControlLine(usb.serialPort::getRTS), equalTo(Boolean.TRUE));
assertThat(usb.getControlLine(usb.serialPort::getCTS), equalTo(inputLineFalse));
assertThat(usb.getControlLine(usb.serialPort::getDTR), equalTo(Boolean.TRUE));
assertThat(usb.getControlLine(usb.serialPort::getDSR), equalTo(inputLineFalse));
assertThat(usb.getControlLine(usb.serialPort::getCD), equalTo(inputLineTrue));
assertThat(usb.getControlLine(usb.serialPort::getRI), equalTo(inputLineFalse));
telnet.write(data);
assertThat(Arrays.toString(data), usb.read(4), equalTo(data));
usb.write(data);
@ -1390,12 +1400,12 @@ public class DeviceTest {
? EnumSet.of(UsbSerialPort.ControlLine.DTR, UsbSerialPort.ControlLine.DSR)
: EnumSet.of(UsbSerialPort.ControlLine.DTR),
usb.serialPort.getControlLines());
assertFalse(usb.serialPort.getRTS());
assertFalse(usb.serialPort.getCTS());
assertTrue(usb.serialPort.getDTR());
assertEquals(usb.serialPort.getDSR(), inputLinesConnected);
assertFalse(usb.serialPort.getCD());
assertFalse(usb.serialPort.getRI());
assertThat(usb.getControlLine(usb.serialPort::getRTS), equalTo(Boolean.FALSE));
assertThat(usb.getControlLine(usb.serialPort::getCTS), equalTo(inputLineFalse));
assertThat(usb.getControlLine(usb.serialPort::getDTR), equalTo(Boolean.TRUE));
assertThat(usb.getControlLine(usb.serialPort::getDSR), equalTo(inputLineTrue));
assertThat(usb.getControlLine(usb.serialPort::getCD), equalTo(inputLineFalse));
assertThat(usb.getControlLine(usb.serialPort::getRI), equalTo(inputLineFalse));
telnet.write(data);
assertThat(Arrays.toString(data), usb.read(4), equalTo(data));
usb.write(data);
@ -1412,12 +1422,12 @@ public class DeviceTest {
if(outputRetained) retainedControlLines.add(UsbSerialPort.ControlLine.DTR);
if(inputRetained) retainedControlLines.add(UsbSerialPort.ControlLine.DSR);
assertEquals(retainedControlLines, usb.serialPort.getControlLines());
assertFalse(usb.serialPort.getRTS());
assertFalse(usb.serialPort.getCTS());
assertEquals(usb.serialPort.getDTR(), outputRetained);
assertEquals(usb.serialPort.getDSR(), inputRetained);
assertFalse(usb.serialPort.getCD());
assertFalse(usb.serialPort.getRI());
assertThat(usb.getControlLine(usb.serialPort::getRTS), equalTo(Boolean.FALSE));
assertThat(usb.getControlLine(usb.serialPort::getCTS), equalTo(inputLineFalse));
assertThat(usb.getControlLine(usb.serialPort::getDTR), equalTo(outputRetained));
assertThat(usb.getControlLine(usb.serialPort::getDSR), equalTo(inputRetained ? inputLineTrue : inputLineFalse));
assertThat(usb.getControlLine(usb.serialPort::getCD), equalTo(inputLineFalse));
assertThat(usb.getControlLine(usb.serialPort::getRI), equalTo(inputLineFalse));
usb.close(EnumSet.of(UsbWrapper.OpenCloseFlags.NO_CONTROL_LINE_INIT));
usb.open(EnumSet.of(UsbWrapper.OpenCloseFlags.NO_CONTROL_LINE_INIT, UsbWrapper.OpenCloseFlags.NO_IOMANAGER_THREAD));
@ -1436,28 +1446,35 @@ public class DeviceTest {
// get... error
try {
usb.serialPort.getRI();
if (!inputLinesSupported)
;
else if (usb.serialDriver instanceof ProlificSerialDriver)
if (usb.serialDriver instanceof ProlificSerialDriver)
; // todo: currently not possible to detect, as bulkTransfer in background thread does not distinguish timeout and error
else
fail("error expected");
} catch (IOException ignored) {
} catch (UnsupportedOperationException ignored) {
}
}
@Test
public void deviceConnection() throws Exception {
byte buf[] = new byte[256];
byte[] buf = new byte[256];
usb.open(EnumSet.of(UsbWrapper.OpenCloseFlags.NO_IOMANAGER_THREAD));
usb.setParameters(115200, 8, 1, UsbSerialPort.PARITY_NONE);
usb.write("x".getBytes());
usb.serialPort.read(buf, 1000);
usb.serialPort.setRTS(true);
usb.serialPort.getRI();
boolean purged = usb.serialPort.purgeHwBuffers(true, true);
try {
usb.serialPort.getRI();
} catch (UnsupportedOperationException ignored) {
}
boolean purged;
try {
usb.serialPort.purgeHwBuffers(true, true);
purged = true;
} catch (UnsupportedOperationException ex) {
purged = false;
}
usb.deviceConnection.close();
try {
usb.setParameters(115200, 8, 1, UsbSerialPort.PARITY_NONE);
@ -1481,13 +1498,12 @@ public class DeviceTest {
fail("setRts error expected");
} catch (IOException ignored) {
}
if(usb.serialPort.getSupportedControlLines().contains(UsbSerialPort.ControlLine.RI) ) {
try {
usb.serialPort.getRI();
if(!(usb.serialDriver instanceof ProlificSerialDriver))
fail("getRI error expected");
} catch (IOException ignored) {
}
try {
usb.serialPort.getRI();
if(!(usb.serialDriver instanceof ProlificSerialDriver))
fail("getRI error expected");
} catch (IOException ignored) {
} catch (UnsupportedOperationException ignored) {
}
if(purged) {
try {

View File

@ -20,6 +20,7 @@ import java.nio.ByteBuffer;
import java.util.Deque;
import java.util.EnumSet;
import java.util.LinkedList;
import java.util.concurrent.Callable;
import java.util.concurrent.Executors;
import static org.junit.Assert.assertEquals;
@ -223,6 +224,15 @@ public class UsbWrapper implements SerialInputOutputManager.Listener {
Thread.sleep(1);
}
/* return TRUE/FALSE/null instead of true/false/<throw UnsupportedOperationException> */
public Boolean getControlLine(Callable<?> callable) throws Exception {
try {
return (Boolean)callable.call();
} catch (UnsupportedOperationException t) {
return null;
}
}
@Override
public void onNewData(byte[] data) {
long now = System.currentTimeMillis();

View File

@ -239,21 +239,6 @@ public class CdcAcmSerialDriver implements UsbSerialDriver {
sendAcmControlMessage(SET_LINE_CODING, 0, msg);
}
@Override
public boolean getCD() throws IOException {
return false; // TODO
}
@Override
public boolean getCTS() throws IOException {
return false; // TODO
}
@Override
public boolean getDSR() throws IOException {
return false; // TODO
}
@Override
public boolean getDTR() throws IOException {
return mDtr;
@ -265,11 +250,6 @@ public class CdcAcmSerialDriver implements UsbSerialDriver {
setDtrRts();
}
@Override
public boolean getRI() throws IOException {
return false; // TODO
}
@Override
public boolean getRTS() throws IOException {
return mRts;

View File

@ -212,28 +212,28 @@ public abstract class CommonUsbSerialPort implements UsbSerialPort {
public abstract void setParameters(int baudRate, int dataBits, int stopBits, int parity) throws IOException;
@Override
public abstract boolean getCD() throws IOException;
public boolean getCD() throws IOException { throw new UnsupportedOperationException(); }
@Override
public abstract boolean getCTS() throws IOException;
public boolean getCTS() throws IOException { throw new UnsupportedOperationException(); }
@Override
public abstract boolean getDSR() throws IOException;
public boolean getDSR() throws IOException { throw new UnsupportedOperationException(); }
@Override
public abstract boolean getDTR() throws IOException;
public boolean getDTR() throws IOException { throw new UnsupportedOperationException(); }
@Override
public abstract void setDTR(boolean value) throws IOException;
public void setDTR(boolean value) throws IOException { throw new UnsupportedOperationException(); }
@Override
public abstract boolean getRI() throws IOException;
public boolean getRI() throws IOException { throw new UnsupportedOperationException(); }
@Override
public abstract boolean getRTS() throws IOException;
public boolean getRTS() throws IOException { throw new UnsupportedOperationException(); }
@Override
public abstract void setRTS(boolean value) throws IOException;
public void setRTS(boolean value) throws IOException { throw new UnsupportedOperationException(); }
@Override
public abstract EnumSet<ControlLine> getControlLines() throws IOException;
@ -242,8 +242,8 @@ public abstract class CommonUsbSerialPort implements UsbSerialPort {
public abstract EnumSet<ControlLine> getSupportedControlLines() throws IOException;
@Override
public boolean purgeHwBuffers(boolean purgeWriteBuffers, boolean purgeReadBuffers) throws IOException {
return false;
public void purgeHwBuffers(boolean purgeWriteBuffers, boolean purgeReadBuffers) throws IOException {
throw new UnsupportedOperationException();
}
}

View File

@ -305,15 +305,13 @@ public class Cp21xxSerialDriver implements UsbSerialDriver {
@Override
// note: only working on some devices, on other devices ignored w/o error
public boolean purgeHwBuffers(boolean purgeWriteBuffers, boolean purgeReadBuffers) throws IOException {
public void purgeHwBuffers(boolean purgeWriteBuffers, boolean purgeReadBuffers) throws IOException {
int value = (purgeReadBuffers ? FLUSH_READ_CODE : 0)
| (purgeWriteBuffers ? FLUSH_WRITE_CODE : 0);
if (value != 0) {
setConfigSingle(SILABSER_FLUSH_REQUEST_CODE, value);
}
return true;
}
}

View File

@ -337,7 +337,7 @@ public class FtdiSerialDriver implements UsbSerialDriver {
}
@Override
public boolean purgeHwBuffers(boolean purgeWriteBuffers, boolean purgeReadBuffers) throws IOException {
public void purgeHwBuffers(boolean purgeWriteBuffers, boolean purgeReadBuffers) throws IOException {
if (purgeWriteBuffers) {
int result = mConnection.controlTransfer(REQTYPE_HOST_TO_DEVICE, RESET_REQUEST,
RESET_PURGE_RX, mPortNumber+1, null, 0, USB_WRITE_TIMEOUT_MILLIS);
@ -353,7 +353,6 @@ public class FtdiSerialDriver implements UsbSerialDriver {
throw new IOException("purge read buffer failed: result=" + result);
}
}
return true;
}
public void setLatencyTimer(int latencyTime) throws IOException {

View File

@ -464,7 +464,7 @@ public class ProlificSerialDriver implements UsbSerialDriver {
}
@Override
public boolean purgeHwBuffers(boolean purgeWriteBuffers, boolean purgeReadBuffers) throws IOException {
public void purgeHwBuffers(boolean purgeWriteBuffers, boolean purgeReadBuffers) throws IOException {
if (purgeWriteBuffers) {
vendorOut(FLUSH_RX_REQUEST, 0, null);
}
@ -472,8 +472,6 @@ public class ProlificSerialDriver implements UsbSerialDriver {
if (purgeReadBuffers) {
vendorOut(FLUSH_TX_REQUEST, 0, null);
}
return true;
}
}

View File

@ -152,32 +152,36 @@ public interface UsbSerialPort extends Closeable {
/**
* Gets the CD (Carrier Detect) bit from the underlying UART.
*
* @return the current state, or {@code false} if not supported.
* @return the current state
* @throws IOException if an error occurred during reading
* @throws UnsupportedOperationException if not supported
*/
public boolean getCD() throws IOException;
/**
* Gets the CTS (Clear To Send) bit from the underlying UART.
*
* @return the current state, or {@code false} if not supported.
* @return the current state
* @throws IOException if an error occurred during reading
* @throws UnsupportedOperationException if not supported
*/
public boolean getCTS() throws IOException;
/**
* Gets the DSR (Data Set Ready) bit from the underlying UART.
*
* @return the current state, or {@code false} if not supported.
* @return the current state
* @throws IOException if an error occurred during reading
* @throws UnsupportedOperationException if not supported
*/
public boolean getDSR() throws IOException;
/**
* Gets the DTR (Data Terminal Ready) bit from the underlying UART.
*
* @return the current state, or {@code false} if not supported.
* @return the current state
* @throws IOException if an error occurred during reading
* @throws UnsupportedOperationException if not supported
*/
public boolean getDTR() throws IOException;
@ -186,22 +190,25 @@ public interface UsbSerialPort extends Closeable {
*
* @param value the value to set
* @throws IOException if an error occurred during writing
* @throws UnsupportedOperationException if not supported
*/
public void setDTR(boolean value) throws IOException;
/**
* Gets the RI (Ring Indicator) bit from the underlying UART.
*
* @return the current state, or {@code false} if not supported.
* @return the current state
* @throws IOException if an error occurred during reading
* @throws UnsupportedOperationException if not supported
*/
public boolean getRI() throws IOException;
/**
* Gets the RTS (Request To Send) bit from the underlying UART.
*
* @return the current state, or {@code false} if not supported.
* @return the current state
* @throws IOException if an error occurred during reading
* @throws UnsupportedOperationException if not supported
*/
public boolean getRTS() throws IOException;
@ -210,6 +217,7 @@ public interface UsbSerialPort extends Closeable {
*
* @param value the value to set
* @throws IOException if an error occurred during writing
* @throws UnsupportedOperationException if not supported
*/
public void setRTS(boolean value) throws IOException;
@ -235,11 +243,10 @@ public interface UsbSerialPort extends Closeable {
*
* @param purgeWriteBuffers {@code true} to discard non-transmitted output data
* @param purgeReadBuffers {@code true} to discard non-read input data
* @return {@code true} if the operation was successful, or
* {@code false} if the operation is not supported by the driver or device
* @throws IOException if an error occurred during flush
* @throws UnsupportedOperationException if not supported
*/
public boolean purgeHwBuffers(boolean purgeWriteBuffers, boolean purgeReadBuffers) throws IOException;
public void purgeHwBuffers(boolean purgeWriteBuffers, boolean purgeReadBuffers) throws IOException;
/**
* Returns the current state of the connection.