1
0
mirror of https://github.com/mik3y/usb-serial-for-android synced 2025-06-08 08:26:18 +00:00

PL2303 throw error on unsupported baud rates

instead of silently falling back to 9600 baud
This commit is contained in:
kai-morich 2020-09-26 09:36:04 +02:00
parent d63a24762d
commit 1adf2a9b98
4 changed files with 185 additions and 104 deletions

View File

@ -220,16 +220,19 @@ public class DeviceTest {
} }
private void doReadWrite(String reason) throws Exception { private void doReadWrite(String reason) throws Exception {
doReadWrite(reason, -1);
}
private void doReadWrite(String reason, int readWait) throws Exception {
byte[] buf1 = new byte[]{ 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16}; byte[] buf1 = new byte[]{ 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16};
byte[] buf2 = new byte[]{ 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26}; byte[] buf2 = new byte[]{ 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26};
byte[] data; byte[] data;
telnet.write(buf1); telnet.write(buf1);
data = usb.read(buf1.length); data = usb.read(buf1.length, -1, readWait);
assertThat(reason, data, equalTo(buf1)); // includes array content in output assertThat(reason, data, equalTo(buf1)); // includes array content in output
//assertArrayEquals("net2usb".getBytes(), data); // only includes array length in output //assertArrayEquals("net2usb".getBytes(), data); // only includes array length in output
usb.write(buf2); usb.write(buf2);
data = telnet.read(buf2.length); data = telnet.read(buf2.length, readWait);
assertThat(reason, data, equalTo(buf2)); assertThat(reason, data, equalTo(buf2));
} }
@ -289,6 +292,105 @@ public class DeviceTest {
usb.ioManager = null; usb.ioManager = null;
} }
@Test
public void prolificBaudRate() throws Exception {
if(!(usb.serialDriver instanceof ProlificSerialDriver))
return;
int[] baudRates = {
75, 150, 300, 600, 1200, 1800, 2400, 3600, 4800, 7200, 9600, 14400, 19200,
28800, 38400, 57600, 115200, 128000, 134400, 161280, 201600, 230400, 268800,
403200, 460800, 614400, 806400, 921600, 1228800, 2457600, 3000000, /*6000000*/
};
for(int baudRate : baudRates) {
usb.open();
int readWait = 500;
if(baudRate < 300) readWait = 1000;
if(baudRate < 150) readWait = 2000;
telnet.setParameters(baudRate, 8, 1, UsbSerialPort.PARITY_NONE);
usb.setParameters(baudRate, 8, 1, UsbSerialPort.PARITY_NONE);
doReadWrite(String.valueOf(baudRate), readWait);
try {
usb.setParameters(baudRate + 1, 8, 1, UsbSerialPort.PARITY_NONE);
fail("unsupported baud rate error expected "+baudRate);
} catch(UnsupportedOperationException ignored) {}
usb.setParameters(baudRate + (1<<29), 8, 1, UsbSerialPort.PARITY_NONE);
doReadWrite(String.valueOf(baudRate) + " + 1<<29", readWait);
// silent fallback to 9600 for unsupported baud rates
telnet.setParameters(9600, 8, 1, UsbSerialPort.PARITY_NONE);
usb.setParameters(baudRate + 1 + (1<<29), 8, 1, UsbSerialPort.PARITY_NONE);
doReadWrite(String.valueOf(baudRate + 1) + " + 1<<29", readWait);
usb.close();
}
// some PL2303... data sheets mention additional baud rates, others don't
// they do not work with my devices and linux driver also excludes them
usb.open();
telnet.setParameters(9600, 8, 1, UsbSerialPort.PARITY_NONE);
baudRates = new int[]{110, 56000, 256000};
for(int baudRate : baudRates) {
int readWait = 500;
if(baudRate < 300) readWait = 1000;
if(baudRate < 150) readWait = 2000;
try {
usb.setParameters(baudRate, 8, 1, UsbSerialPort.PARITY_NONE);
fail("unsupported baud rate error expected "+baudRate);
} catch(UnsupportedOperationException ignored) {}
// silent fallback to 9600 for unsupported baud rates
usb.setParameters(baudRate + (1<<29), 8, 1, UsbSerialPort.PARITY_NONE);
doReadWrite(String.valueOf(baudRate ) + " + 1<<29", readWait);
}
usb.close();
}
@Test
public void ftdiBaudRate() throws Exception {
if (!(usb.serialDriver instanceof FtdiSerialDriver))
return;
usb.open();
try {
usb.setParameters(183, 8, 1, UsbSerialPort.PARITY_NONE);
fail("baud rate to low expected");
} catch (IOException ignored) {
}
usb.setParameters(184, 8, 1, UsbSerialPort.PARITY_NONE);
usb.setParameters( 960000, 8, 1, UsbSerialPort.PARITY_NONE);
usb.setParameters(1000000, 8, 1, UsbSerialPort.PARITY_NONE);
usb.setParameters(1043478, 8, 1, UsbSerialPort.PARITY_NONE);
usb.setParameters(1090909, 8, 1, UsbSerialPort.PARITY_NONE);
usb.setParameters(1142857, 8, 1, UsbSerialPort.PARITY_NONE);
usb.setParameters(1200000, 8, 1, UsbSerialPort.PARITY_NONE);
usb.setParameters(1263157, 8, 1, UsbSerialPort.PARITY_NONE);
usb.setParameters(1333333, 8, 1, UsbSerialPort.PARITY_NONE);
usb.setParameters(1411764, 8, 1, UsbSerialPort.PARITY_NONE);
usb.setParameters(1500000, 8, 1, UsbSerialPort.PARITY_NONE);
try {
usb.setParameters((int)(2000000/1.04), 8, 1, UsbSerialPort.PARITY_NONE);
fail("baud rate error expected");
} catch (IOException ignored) {
}
usb.setParameters((int)(2000000/1.03), 8, 1, UsbSerialPort.PARITY_NONE);
usb.setParameters(2000000, 8, 1, UsbSerialPort.PARITY_NONE);
usb.setParameters((int)(2000000*1.03), 8, 1, UsbSerialPort.PARITY_NONE);
try {
usb.setParameters((int)(2000000*1.04), 8, 1, UsbSerialPort.PARITY_NONE);
fail("baud rate error expected");
} catch (IOException ignored) {
}
usb.setParameters(2000000, 8, 1, UsbSerialPort.PARITY_NONE);
usb.setParameters(3000000, 8, 1, UsbSerialPort.PARITY_NONE);
try {
usb.setParameters(4000000, 8, 1, UsbSerialPort.PARITY_NONE);
fail("baud rate to high expected");
} catch (IOException ignored) {
}
}
@Test @Test
public void baudRate() throws Exception { public void baudRate() throws Exception {
usb.open(); usb.open();
@ -329,7 +431,7 @@ public class DeviceTest {
} catch (IllegalArgumentException ignored) { } catch (IllegalArgumentException ignored) {
} }
try { try {
usb.setParameters(2<<31, 8, 1, UsbSerialPort.PARITY_NONE); usb.setParameters(1<<31, 8, 1, UsbSerialPort.PARITY_NONE);
if (usb.serialDriver instanceof ProlificSerialDriver) if (usb.serialDriver instanceof ProlificSerialDriver)
; ;
else if (usb.serialDriver instanceof Cp21xxSerialDriver) else if (usb.serialDriver instanceof Cp21xxSerialDriver)
@ -358,72 +460,36 @@ public class DeviceTest {
doReadWrite(baudRate+"/8N1"); doReadWrite(baudRate+"/8N1");
} }
if(rfc2217_server_nonstandard_baudrates && !isCp21xxRestrictedPort) { if(rfc2217_server_nonstandard_baudrates && !isCp21xxRestrictedPort) {
// usbParameters does not fail on devices that do not support nonstandard baud rates
usb.setParameters(42000, 8, 1, UsbSerialPort.PARITY_NONE);
telnet.setParameters(42000, 8, 1, UsbSerialPort.PARITY_NONE);
byte[] buf1 = "abc".getBytes();
byte[] buf2 = "ABC".getBytes();
byte[] data1, data2;
usb.write(buf1);
data1 = telnet.read();
telnet.write(buf2);
data2 = usb.read();
if (usb.serialDriver instanceof ProlificSerialDriver) { if (usb.serialDriver instanceof ProlificSerialDriver) {
// not supported try {
assertNotEquals(data1, buf2); usb.setParameters(42000, 8, 1, UsbSerialPort.PARITY_NONE);
assertNotEquals(data2, buf2); fail("unsupported baud rate error expected");
} else if (usb.serialDriver instanceof Cp21xxSerialDriver) { } catch (UnsupportedOperationException ignored) {}
if (usb.serialDriver.getPorts().size() > 1) { } else {
// supported on cp2105 first port usb.setParameters(42000, 8, 1, UsbSerialPort.PARITY_NONE);
telnet.setParameters(42000, 8, 1, UsbSerialPort.PARITY_NONE);
byte[] buf1 = "abc".getBytes();
byte[] buf2 = "ABC".getBytes();
byte[] data1, data2;
usb.write(buf1);
data1 = telnet.read();
telnet.write(buf2);
data2 = usb.read();
if (usb.serialDriver instanceof Cp21xxSerialDriver) {
if (usb.serialDriver.getPorts().size() > 1) {
// supported on cp2105 first port
assertThat("42000/8N1", data1, equalTo(buf1));
assertThat("42000/8N1", data2, equalTo(buf2));
} else {
// not supported on cp2102
assertNotEquals(data1, buf1);
assertNotEquals(data2, buf2);
}
} else {
assertThat("42000/8N1", data1, equalTo(buf1)); assertThat("42000/8N1", data1, equalTo(buf1));
assertThat("42000/8N1", data2, equalTo(buf2)); assertThat("42000/8N1", data2, equalTo(buf2));
} else {
// not supported on cp2102
assertNotEquals(data1, buf1);
assertNotEquals(data2, buf2);
} }
} else {
assertThat("42000/8N1", data1, equalTo(buf1));
assertThat("42000/8N1", data2, equalTo(buf2));
}
}
if (usb.serialDriver instanceof FtdiSerialDriver) {
try {
usb.setParameters(183, 8, 1, UsbSerialPort.PARITY_NONE);
fail("baud rate to low expected");
} catch (IOException ignored) {
}
usb.setParameters(184, 8, 1, UsbSerialPort.PARITY_NONE);
usb.setParameters( 960000, 8, 1, UsbSerialPort.PARITY_NONE);
usb.setParameters(1000000, 8, 1, UsbSerialPort.PARITY_NONE);
usb.setParameters(1043478, 8, 1, UsbSerialPort.PARITY_NONE);
usb.setParameters(1090909, 8, 1, UsbSerialPort.PARITY_NONE);
usb.setParameters(1142857, 8, 1, UsbSerialPort.PARITY_NONE);
usb.setParameters(1200000, 8, 1, UsbSerialPort.PARITY_NONE);
usb.setParameters(1263157, 8, 1, UsbSerialPort.PARITY_NONE);
usb.setParameters(1333333, 8, 1, UsbSerialPort.PARITY_NONE);
usb.setParameters(1411764, 8, 1, UsbSerialPort.PARITY_NONE);
usb.setParameters(1500000, 8, 1, UsbSerialPort.PARITY_NONE);
try {
usb.setParameters((int)(2000000/1.04), 8, 1, UsbSerialPort.PARITY_NONE);
fail("baud rate error expected");
} catch (IOException ignored) {
}
usb.setParameters((int)(2000000/1.03), 8, 1, UsbSerialPort.PARITY_NONE);
usb.setParameters(2000000, 8, 1, UsbSerialPort.PARITY_NONE);
usb.setParameters((int)(2000000*1.03), 8, 1, UsbSerialPort.PARITY_NONE);
try {
usb.setParameters((int)(2000000*1.04), 8, 1, UsbSerialPort.PARITY_NONE);
fail("baud rate error expected");
} catch (IOException ignored) {
}
usb.setParameters(2000000, 8, 1, UsbSerialPort.PARITY_NONE);
usb.setParameters(3000000, 8, 1, UsbSerialPort.PARITY_NONE);
try {
usb.setParameters(4000000, 8, 1, UsbSerialPort.PARITY_NONE);
fail("baud rate to high expected");
} catch (IOException ignored) {
} }
} }
{ // non matching baud rate { // non matching baud rate

View File

@ -92,11 +92,15 @@ public class TelnetWrapper {
// wait full time // wait full time
public byte[] read() throws Exception { public byte[] read() throws Exception {
return read(-1); return read(-1, -1);
} }
public byte[] read(int expectedLength) throws Exception { public byte[] read(int expectedLength) throws Exception {
long end = System.currentTimeMillis() + TELNET_READ_WAIT; return read(expectedLength, -1);
}
public byte[] read(int expectedLength, int readWait) throws Exception {
if(readWait == -1)
readWait = TELNET_READ_WAIT;
long end = System.currentTimeMillis() + readWait;
ByteBuffer buf = ByteBuffer.allocate(65536); ByteBuffer buf = ByteBuffer.allocate(65536);
while(System.currentTimeMillis() < end) { while(System.currentTimeMillis() < end) {
if(readStream.available() > 0) { if(readStream.available() > 0) {

View File

@ -179,11 +179,19 @@ public class UsbWrapper implements SerialInputOutputManager.Listener {
} }
// wait full time // wait full time
public byte[] read() throws Exception { return read(-1, -1); } public byte[] read() throws Exception {
public byte[] read(int expectedLength) throws Exception { return read(expectedLength, -1); } return read(-1, -1, -1);
}
public byte[] read(int expectedLength) throws Exception {
return read(expectedLength, -1, -1);
}
public byte[] read(int expectedLength, int readBufferSize) throws Exception { public byte[] read(int expectedLength, int readBufferSize) throws Exception {
long end = System.currentTimeMillis() + USB_READ_WAIT; return read(expectedLength, readBufferSize, -1);
}
public byte[] read(int expectedLength, int readBufferSize, int readWait) throws Exception {
if(readWait == -1)
readWait = USB_READ_WAIT;
long end = System.currentTimeMillis() + readWait;
ByteBuffer buf = ByteBuffer.allocate(16*1024); ByteBuffer buf = ByteBuffer.allocate(16*1024);
if(ioManager != null) { if(ioManager != null) {
while (System.currentTimeMillis() < end) { while (System.currentTimeMillis() < end) {

View File

@ -16,8 +16,9 @@ import android.hardware.usb.UsbEndpoint;
import android.hardware.usb.UsbInterface; import android.hardware.usb.UsbInterface;
import android.util.Log; import android.util.Log;
import com.hoho.android.usbserial.BuildConfig;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Collections; import java.util.Collections;
import java.util.EnumSet; import java.util.EnumSet;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
@ -28,6 +29,13 @@ public class ProlificSerialDriver implements UsbSerialDriver {
private final String TAG = ProlificSerialDriver.class.getSimpleName(); private final String TAG = ProlificSerialDriver.class.getSimpleName();
private final static int[] standardBaudRates = {
75, 150, 300, 600, 1200, 1800, 2400, 3600, 4800, 7200, 9600, 14400, 19200,
28800, 38400, 57600, 115200, 128000, 134400, 161280, 201600, 230400, 268800,
403200, 460800, 614400, 806400, 921600, 1228800, 2457600, 3000000, 6000000
};
private final UsbDevice mDevice; private final UsbDevice mDevice;
private final UsbSerialPort mPort; private final UsbSerialPort mPort;
@ -56,14 +64,9 @@ public class ProlificSerialDriver implements UsbSerialDriver {
private static final int PROLIFIC_VENDOR_READ_REQUEST = 0x01; private static final int PROLIFIC_VENDOR_READ_REQUEST = 0x01;
private static final int PROLIFIC_VENDOR_WRITE_REQUEST = 0x01; private static final int PROLIFIC_VENDOR_WRITE_REQUEST = 0x01;
private static final int PROLIFIC_VENDOR_OUT_REQTYPE = UsbConstants.USB_DIR_OUT private static final int PROLIFIC_VENDOR_OUT_REQTYPE = UsbConstants.USB_DIR_OUT | UsbConstants.USB_TYPE_VENDOR;
| UsbConstants.USB_TYPE_VENDOR; private static final int PROLIFIC_VENDOR_IN_REQTYPE = UsbConstants.USB_DIR_IN | UsbConstants.USB_TYPE_VENDOR;
private static final int PROLIFIC_CTRL_OUT_REQTYPE = UsbConstants.USB_DIR_OUT | UsbConstants.USB_TYPE_CLASS | USB_RECIP_INTERFACE;
private static final int PROLIFIC_VENDOR_IN_REQTYPE = UsbConstants.USB_DIR_IN
| UsbConstants.USB_TYPE_VENDOR;
private static final int PROLIFIC_CTRL_OUT_REQTYPE = UsbConstants.USB_DIR_OUT
| UsbConstants.USB_TYPE_CLASS | USB_RECIP_INTERFACE;
private static final int WRITE_ENDPOINT = 0x02; private static final int WRITE_ENDPOINT = 0x02;
private static final int READ_ENDPOINT = 0x83; private static final int READ_ENDPOINT = 0x83;
@ -91,11 +94,8 @@ public class ProlificSerialDriver implements UsbSerialDriver {
private static final int DEVICE_TYPE_1 = 2; private static final int DEVICE_TYPE_1 = 2;
private int mDeviceType = DEVICE_TYPE_HX; private int mDeviceType = DEVICE_TYPE_HX;
private UsbEndpoint mInterruptEndpoint; private UsbEndpoint mInterruptEndpoint;
private int mControlLinesValue = 0; private int mControlLinesValue = 0;
private int mBaudRate = -1, mDataBits = -1, mStopBits = -1, mParity = -1; private int mBaudRate = -1, mDataBits = -1, mStopBits = -1, mParity = -1;
private int mStatus = 0; private int mStatus = 0;
@ -274,11 +274,11 @@ public class ProlificSerialDriver implements UsbSerialDriver {
if (mDevice.getDeviceClass() == 0x02) { if (mDevice.getDeviceClass() == 0x02) {
mDeviceType = DEVICE_TYPE_0; mDeviceType = DEVICE_TYPE_0;
} else { } else {
try { byte[] rawDescriptors = connection.getRawDescriptors();
Method getRawDescriptorsMethod if(rawDescriptors == null || rawDescriptors.length <8) {
= mConnection.getClass().getMethod("getRawDescriptors"); Log.w(TAG, "Could not get device descriptors, Assuming that it is a HX device");
byte[] rawDescriptors mDeviceType = DEVICE_TYPE_HX;
= (byte[]) getRawDescriptorsMethod.invoke(mConnection); } else {
byte maxPacketSize0 = rawDescriptors[7]; byte maxPacketSize0 = rawDescriptors[7];
if (maxPacketSize0 == 64) { if (maxPacketSize0 == 64) {
mDeviceType = DEVICE_TYPE_HX; mDeviceType = DEVICE_TYPE_HX;
@ -286,18 +286,9 @@ public class ProlificSerialDriver implements UsbSerialDriver {
|| (mDevice.getDeviceClass() == 0xff)) { || (mDevice.getDeviceClass() == 0xff)) {
mDeviceType = DEVICE_TYPE_1; mDeviceType = DEVICE_TYPE_1;
} else { } else {
Log.w(TAG, "Could not detect PL2303 subtype, " Log.w(TAG, "Could not detect PL2303 subtype, Assuming that it is a HX device");
+ "Assuming that it is a HX device"); mDeviceType = DEVICE_TYPE_HX;
mDeviceType = DEVICE_TYPE_HX;
} }
} catch (NoSuchMethodException e) {
Log.w(TAG, "Method UsbDeviceConnection.getRawDescriptors, "
+ "required for PL2303 subtype detection, not "
+ "available! Assuming that it is a HX device");
mDeviceType = DEVICE_TYPE_HX;
} catch (Exception e) {
Log.e(TAG, "An unexpected exception occured while trying "
+ "to detect PL2303 subtype", e);
} }
} }
setControlLines(mControlLinesValue); setControlLines(mControlLinesValue);
@ -325,8 +316,24 @@ public class ProlificSerialDriver implements UsbSerialDriver {
} catch(Exception ignored) {} } catch(Exception ignored) {}
} }
private int filterBaudRate(int baudRate) {
if(BuildConfig.DEBUG && (baudRate & (3<<29)) == (1<<29)) {
return baudRate & ~(1<<29); // for testing purposes accept without further checks
}
if (baudRate <= 0) {
throw new IllegalArgumentException("Invalid baud rate: " + baudRate);
}
for(int br : standardBaudRates) {
if (br == baudRate) {
return baudRate;
}
}
throw new UnsupportedOperationException("Unsupported baud rate: " + baudRate);
}
@Override @Override
public void setParameters(int baudRate, int dataBits, int stopBits, int parity) throws IOException { public void setParameters(int baudRate, int dataBits, int stopBits, int parity) throws IOException {
baudRate = filterBaudRate(baudRate);
if ((mBaudRate == baudRate) && (mDataBits == dataBits) if ((mBaudRate == baudRate) && (mDataBits == dataBits)
&& (mStopBits == stopBits) && (mParity == parity)) { && (mStopBits == stopBits) && (mParity == parity)) {
// Make sure no action is performed if there is nothing to change // Make sure no action is performed if there is nothing to change
@ -334,10 +341,6 @@ public class ProlificSerialDriver implements UsbSerialDriver {
} }
byte[] lineRequestData = new byte[7]; byte[] lineRequestData = new byte[7];
if(baudRate <= 0) {
throw new IllegalArgumentException("Invalid baud rate: " + baudRate);
}
lineRequestData[0] = (byte) (baudRate & 0xff); lineRequestData[0] = (byte) (baudRate & 0xff);
lineRequestData[1] = (byte) ((baudRate >> 8) & 0xff); lineRequestData[1] = (byte) ((baudRate >> 8) & 0xff);
lineRequestData[2] = (byte) ((baudRate >> 16) & 0xff); lineRequestData[2] = (byte) ((baudRate >> 16) & 0xff);