kopia lustrzana https://github.com/mik3y/usb-serial-for-android
consolidate get[Supported]ControlLines
rodzic
de6d5aa384
commit
b06118b156
|
@ -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
|
||||
]
|
||||
}
|
||||
|
|
|
@ -22,10 +22,12 @@ import android.util.Log;
|
|||
|
||||
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;
|
||||
|
@ -71,6 +73,7 @@ import static org.junit.Assert.assertNotEquals;
|
|||
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;
|
||||
|
||||
|
@ -1616,15 +1619,10 @@ public class DeviceTest {
|
|||
|
||||
@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
|
||||
|
@ -1645,9 +1643,9 @@ public class DeviceTest {
|
|||
}
|
||||
}
|
||||
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");
|
||||
|
@ -1661,9 +1659,9 @@ public class DeviceTest {
|
|||
}
|
||||
// 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)
|
||||
|
@ -1679,9 +1677,9 @@ public class DeviceTest {
|
|||
}
|
||||
}
|
||||
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)
|
||||
|
@ -1697,9 +1695,9 @@ public class DeviceTest {
|
|||
}
|
||||
}
|
||||
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");
|
||||
|
@ -1711,6 +1709,36 @@ public class DeviceTest {
|
|||
} 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);
|
||||
|
@ -1743,7 +1771,11 @@ public class DeviceTest {
|
|||
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);
|
||||
|
@ -1760,6 +1792,16 @@ public class DeviceTest {
|
|||
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();
|
||||
|
|
|
@ -56,6 +56,7 @@ public class UsbWrapper implements SerialInputOutputManager.Listener {
|
|||
|
||||
// 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;
|
||||
|
@ -99,9 +100,10 @@ public class UsbWrapper implements SerialInputOutputManager.Listener {
|
|||
|
||||
// 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
|
||||
|
@ -110,16 +112,21 @@ public class UsbWrapper implements SerialInputOutputManager.Listener {
|
|||
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) {
|
||||
|
|
|
@ -80,11 +80,6 @@ public class ChromeCcdSerialDriver implements UsbSerialDriver{
|
|||
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);
|
||||
|
|
|
@ -305,7 +305,7 @@ public abstract class CommonUsbSerialPort implements UsbSerialPort {
|
|||
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;
|
||||
|
|
|
@ -89,14 +89,9 @@ public class GsmModemSerialDriver implements UsbSerialDriver{
|
|||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Ładowanie…
Reference in New Issue