Skip to content

Commit

Permalink
consolidate get[Supported]ControlLines
Browse files Browse the repository at this point in the history
  • Loading branch information
kai-morich committed Oct 2, 2023
1 parent de6d5aa commit b06118b
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 36 deletions.
2 changes: 1 addition & 1 deletion usbSerialForAndroid/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ android {

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
testInstrumentationRunnerArguments = [ // Raspi Windows LinuxVM ...
'rfc2217_server_host': '192.168.0.143',
'rfc2217_server_host': '192.168.0.145',
'rfc2217_server_nonstandard_baudrates': 'true', // true false false
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@

import com.hoho.android.usbserial.driver.CdcAcmSerialDriver;
import com.hoho.android.usbserial.driver.Ch34xSerialDriver;
import com.hoho.android.usbserial.driver.ChromeCcdSerialDriver;
import com.hoho.android.usbserial.driver.CommonUsbSerialPort;
import com.hoho.android.usbserial.driver.CommonUsbSerialPortWrapper;
import com.hoho.android.usbserial.driver.Cp21xxSerialDriver;
import com.hoho.android.usbserial.driver.FtdiSerialDriver;
import com.hoho.android.usbserial.driver.GsmModemSerialDriver;
import com.hoho.android.usbserial.driver.ProbeTable;
import com.hoho.android.usbserial.driver.ProlificSerialDriver;
import com.hoho.android.usbserial.driver.ProlificSerialPortWrapper;
Expand Down Expand Up @@ -71,6 +73,7 @@
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

Expand Down Expand Up @@ -1616,15 +1619,10 @@ public void readTimeout() throws Exception {

@Test
public void wrongDriver() throws Exception {

UsbDeviceConnection wrongDeviceConnection;
UsbSerialDriver wrongSerialDriver;
UsbSerialPort wrongSerialPort;

if(!(usb.serialDriver instanceof CdcAcmSerialDriver)) {
wrongDeviceConnection = usbManager.openDevice(usb.serialDriver.getDevice());
wrongSerialDriver = new CdcAcmSerialDriver(usb.serialDriver.getDevice());
wrongSerialPort = wrongSerialDriver.getPorts().get(0);
UsbDeviceConnection wrongDeviceConnection = usbManager.openDevice(usb.serialDriver.getDevice());
UsbSerialDriver wrongSerialDriver = new CdcAcmSerialDriver(usb.serialDriver.getDevice());
UsbSerialPort wrongSerialPort = wrongSerialDriver.getPorts().get(0);
try {
wrongSerialPort.open(wrongDeviceConnection);
wrongSerialPort.setParameters(115200, UsbSerialPort.DATABITS_8, UsbSerialPort.STOPBITS_1, UsbSerialPort.PARITY_NONE); // ch340 fails here
Expand All @@ -1645,9 +1643,9 @@ public void wrongDriver() throws Exception {
}
}
if(!(usb.serialDriver instanceof Ch34xSerialDriver)) {
wrongDeviceConnection = usbManager.openDevice(usb.serialDriver.getDevice());
wrongSerialDriver = new Ch34xSerialDriver(usb.serialDriver.getDevice());
wrongSerialPort = wrongSerialDriver.getPorts().get(0);
UsbDeviceConnection wrongDeviceConnection = usbManager.openDevice(usb.serialDriver.getDevice());
UsbSerialDriver wrongSerialDriver = new Ch34xSerialDriver(usb.serialDriver.getDevice());
UsbSerialPort wrongSerialPort = wrongSerialDriver.getPorts().get(0);
try {
wrongSerialPort.open(wrongDeviceConnection);
fail("error expected");
Expand All @@ -1661,9 +1659,9 @@ public void wrongDriver() throws Exception {
}
// FTDI only recovers from Cp21xx control commands with power toggle, so skip this combination!
if(!(usb.serialDriver instanceof Cp21xxSerialDriver | usb.serialDriver instanceof FtdiSerialDriver)) {
wrongDeviceConnection = usbManager.openDevice(usb.serialDriver.getDevice());
wrongSerialDriver = new Cp21xxSerialDriver(usb.serialDriver.getDevice());
wrongSerialPort = wrongSerialDriver.getPorts().get(0);
UsbDeviceConnection wrongDeviceConnection = usbManager.openDevice(usb.serialDriver.getDevice());
UsbSerialDriver wrongSerialDriver = new Cp21xxSerialDriver(usb.serialDriver.getDevice());
UsbSerialPort wrongSerialPort = wrongSerialDriver.getPorts().get(0);
try {
wrongSerialPort.open(wrongDeviceConnection);
//if(usb.usbSerialDriver instanceof FtdiSerialDriver)
Expand All @@ -1679,9 +1677,9 @@ public void wrongDriver() throws Exception {
}
}
if(!(usb.serialDriver instanceof FtdiSerialDriver)) {
wrongDeviceConnection = usbManager.openDevice(usb.serialDriver.getDevice());
wrongSerialDriver = new FtdiSerialDriver(usb.serialDriver.getDevice());
wrongSerialPort = wrongSerialDriver.getPorts().get(0);
UsbDeviceConnection wrongDeviceConnection = usbManager.openDevice(usb.serialDriver.getDevice());
UsbSerialDriver wrongSerialDriver = new FtdiSerialDriver(usb.serialDriver.getDevice());
UsbSerialPort wrongSerialPort = wrongSerialDriver.getPorts().get(0);
try {
wrongSerialPort.open(wrongDeviceConnection);
if(usb.serialDriver instanceof Cp21xxSerialDriver)
Expand All @@ -1697,9 +1695,9 @@ public void wrongDriver() throws Exception {
}
}
if(!(usb.serialDriver instanceof ProlificSerialDriver)) {
wrongDeviceConnection = usbManager.openDevice(usb.serialDriver.getDevice());
wrongSerialDriver = new ProlificSerialDriver(usb.serialDriver.getDevice());
wrongSerialPort = wrongSerialDriver.getPorts().get(0);
UsbDeviceConnection wrongDeviceConnection = usbManager.openDevice(usb.serialDriver.getDevice());
UsbSerialDriver wrongSerialDriver = new ProlificSerialDriver(usb.serialDriver.getDevice());
UsbSerialPort wrongSerialPort = wrongSerialDriver.getPorts().get(0);
try {
wrongSerialPort.open(wrongDeviceConnection);
fail("error expected");
Expand All @@ -1711,6 +1709,36 @@ public void wrongDriver() throws Exception {
} catch (IOException ignored) {
}
}
if(!(usb.serialDriver instanceof GsmModemSerialDriver)) {
UsbDeviceConnection wrongDeviceConnection = usbManager.openDevice(usb.serialDriver.getDevice());
UsbSerialDriver wrongSerialDriver = new GsmModemSerialDriver(usb.serialDriver.getDevice());
UsbSerialPort wrongSerialPort = wrongSerialDriver.getPorts().get(0);
try {
wrongSerialPort.open(wrongDeviceConnection);
} catch (IOException ignored) {
}
assertThrows(UnsupportedOperationException.class, () -> wrongSerialPort.setParameters(9200, 8, 1, 0));
assertEquals(EnumSet.noneOf(ControlLine.class), wrongSerialPort.getSupportedControlLines());
try {
wrongSerialPort.close();
} catch (IOException ignored) {
}
}
if(!(usb.serialDriver instanceof ChromeCcdSerialDriver)) {
UsbDeviceConnection wrongDeviceConnection = usbManager.openDevice(usb.serialDriver.getDevice());
UsbSerialDriver wrongSerialDriver = new ChromeCcdSerialDriver(usb.serialDriver.getDevice());
UsbSerialPort wrongSerialPort = wrongSerialDriver.getPorts().get(0);
try {
wrongSerialPort.open(wrongDeviceConnection);
} catch (IOException ignored) {
}
assertThrows(UnsupportedOperationException.class, () -> wrongSerialPort.setParameters(9200, 8, 1, 0));
assertEquals(EnumSet.noneOf(ControlLine.class), wrongSerialPort.getSupportedControlLines());
try {
wrongSerialPort.close();
} catch (IOException ignored) {
}
}
// test that device recovers from wrong commands
usb.open();
telnet.setParameters(19200, 8, 1, UsbSerialPort.PARITY_NONE);
Expand Down Expand Up @@ -1743,7 +1771,11 @@ public void controlLines() throws Exception {
Boolean inputLineFalse = usb.inputLinesSupported ? Boolean.FALSE : null;
Boolean inputLineTrue = usb.inputLinesConnected ? Boolean.TRUE : inputLineFalse;

EnumSet<ControlLine> supportedControlLines = EnumSet.of(ControlLine.RTS, ControlLine.DTR);
EnumSet<ControlLine> supportedControlLines = EnumSet.noneOf(ControlLine.class);
if(usb.outputLinesSupported) {
supportedControlLines.add(ControlLine.RTS);
supportedControlLines.add(ControlLine.DTR);
}
if(usb.inputLinesSupported) {
supportedControlLines.add(ControlLine.CTS);
supportedControlLines.add(ControlLine.DSR);
Expand All @@ -1760,6 +1792,16 @@ public void controlLines() throws Exception {
Thread.sleep(sleep);

assertEquals(supportedControlLines, usb.serialPort.getSupportedControlLines());
if(supportedControlLines == EnumSet.noneOf(ControlLine.class)) {
assertThrows(UnsupportedOperationException.class, () -> usb.serialPort.getControlLines());
assertThrows(UnsupportedOperationException.class, () -> usb.serialPort.getRTS());
assertThrows(UnsupportedOperationException.class, () -> usb.serialPort.getCTS());
assertThrows(UnsupportedOperationException.class, () -> usb.serialPort.getDTR());
assertThrows(UnsupportedOperationException.class, () -> usb.serialPort.getDSR());
assertThrows(UnsupportedOperationException.class, () -> usb.serialPort.getCD());
assertThrows(UnsupportedOperationException.class, () -> usb.serialPort.getRI());
return;
}

// control lines reset on initial open
data = "none".getBytes();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public enum OpenCloseFlags { NO_IOMANAGER_THREAD, NO_IOMANAGER_START, NO_CONTROL

// device properties
public boolean isCp21xxRestrictedPort; // second port of Cp2105 has limited dataBits, stopBits, parity
public boolean outputLinesSupported;
public boolean inputLinesSupported;
public boolean inputLinesConnected;
public boolean inputLinesOnlyRtsCts;
Expand Down Expand Up @@ -99,9 +100,10 @@ public void onReceive(Context context, Intent intent) {

// extract some device properties:
isCp21xxRestrictedPort = serialDriver instanceof Cp21xxSerialDriver && serialDriver.getPorts().size()==2 && serialPort.getPortNumber() == 1;
// output lines are supported by all drivers
// input lines are supported by all drivers except CDC
// output lines are supported by all common drivers
// input lines are supported by all common drivers except CDC
if (serialDriver instanceof FtdiSerialDriver) {
outputLinesSupported = true;
inputLinesSupported = true;
if(serialDriver.getDevice().getProductId() == UsbId.FTDI_FT2232H)
inputLinesConnected = true; // I only have 74LS138 connected at FT2232, not at FT232
Expand All @@ -110,16 +112,21 @@ public void onReceive(Context context, Intent intent) {
inputLinesOnlyRtsCts = true; // I only test with FT230X that has only these 2 control lines. DTR is silently ignored
}
} else if (serialDriver instanceof Cp21xxSerialDriver) {
outputLinesSupported = true;
inputLinesSupported = true;
if(serialDriver.getPorts().size() == 1)
inputLinesConnected = true; // I only have 74LS138 connected at CP2102, not at CP2105
} else if (serialDriver instanceof ProlificSerialDriver) {
outputLinesSupported = true;
inputLinesSupported = true;
inputLinesConnected = true;
} else if (serialDriver instanceof Ch34xSerialDriver) {
outputLinesSupported = true;
inputLinesSupported = true;
if(serialDriver.getDevice().getProductId() == UsbId.QINHENG_CH340)
inputLinesConnected = true; // I only have 74LS138 connected at CH340, not connected at CH341A
} else if (serialDriver instanceof CdcAcmSerialDriver) {
outputLinesSupported = true;
}

if (serialDriver instanceof Cp21xxSerialDriver) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,6 @@ public void setParameters(int baudRate, int dataBits, int stopBits, int parity)
throw new UnsupportedOperationException();
}

@Override
public EnumSet<ControlLine> getControlLines() throws IOException {
return EnumSet.noneOf(ControlLine.class);
}

@Override
public EnumSet<ControlLine> getSupportedControlLines() throws IOException {
return EnumSet.noneOf(ControlLine.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ public boolean isOpen() {
public void setRTS(boolean value) throws IOException { throw new UnsupportedOperationException(); }

@Override
public abstract EnumSet<ControlLine> getControlLines() throws IOException;
public EnumSet<ControlLine> getControlLines() throws IOException { throw new UnsupportedOperationException(); }

@Override
public abstract EnumSet<ControlLine> getSupportedControlLines() throws IOException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,9 @@ public void setParameters(int baudRate, int dataBits, int stopBits, int parity)
throw new UnsupportedOperationException();
}

@Override
public EnumSet<ControlLine> getControlLines() throws IOException {
throw new UnsupportedOperationException();
}

@Override
public EnumSet<ControlLine> getSupportedControlLines() throws IOException {
throw new UnsupportedOperationException();
return EnumSet.noneOf(ControlLine.class);
}
}

Expand Down

0 comments on commit b06118b

Please sign in to comment.