mirror of
https://github.com/mik3y/usb-serial-for-android
synced 2025-06-07 16:06:10 +00:00
PL2303 throw error on unsupported baud rates
instead of silently falling back to 9600 baud
This commit is contained in:
parent
d63a24762d
commit
1adf2a9b98
@ -220,16 +220,19 @@ public class DeviceTest {
|
||||
}
|
||||
|
||||
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[] buf2 = new byte[]{ 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26};
|
||||
byte[] data;
|
||||
|
||||
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
|
||||
//assertArrayEquals("net2usb".getBytes(), data); // only includes array length in output
|
||||
usb.write(buf2);
|
||||
data = telnet.read(buf2.length);
|
||||
data = telnet.read(buf2.length, readWait);
|
||||
assertThat(reason, data, equalTo(buf2));
|
||||
}
|
||||
|
||||
@ -289,6 +292,105 @@ public class DeviceTest {
|
||||
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
|
||||
public void baudRate() throws Exception {
|
||||
usb.open();
|
||||
@ -329,7 +431,7 @@ public class DeviceTest {
|
||||
} catch (IllegalArgumentException ignored) {
|
||||
}
|
||||
try {
|
||||
usb.setParameters(2<<31, 8, 1, UsbSerialPort.PARITY_NONE);
|
||||
usb.setParameters(1<<31, 8, 1, UsbSerialPort.PARITY_NONE);
|
||||
if (usb.serialDriver instanceof ProlificSerialDriver)
|
||||
;
|
||||
else if (usb.serialDriver instanceof Cp21xxSerialDriver)
|
||||
@ -358,72 +460,36 @@ public class DeviceTest {
|
||||
doReadWrite(baudRate+"/8N1");
|
||||
}
|
||||
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) {
|
||||
// not supported
|
||||
assertNotEquals(data1, buf2);
|
||||
assertNotEquals(data2, buf2);
|
||||
} else if (usb.serialDriver instanceof Cp21xxSerialDriver) {
|
||||
if (usb.serialDriver.getPorts().size() > 1) {
|
||||
// supported on cp2105 first port
|
||||
try {
|
||||
usb.setParameters(42000, 8, 1, UsbSerialPort.PARITY_NONE);
|
||||
fail("unsupported baud rate error expected");
|
||||
} catch (UnsupportedOperationException ignored) {}
|
||||
} else {
|
||||
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", 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
|
||||
|
@ -92,11 +92,15 @@ public class TelnetWrapper {
|
||||
|
||||
// wait full time
|
||||
public byte[] read() throws Exception {
|
||||
return read(-1);
|
||||
return read(-1, -1);
|
||||
}
|
||||
|
||||
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);
|
||||
while(System.currentTimeMillis() < end) {
|
||||
if(readStream.available() > 0) {
|
||||
|
@ -179,11 +179,19 @@ public class UsbWrapper implements SerialInputOutputManager.Listener {
|
||||
}
|
||||
|
||||
// wait full time
|
||||
public byte[] read() throws Exception { return read(-1, -1); }
|
||||
public byte[] read(int expectedLength) throws Exception { return read(expectedLength, -1); }
|
||||
|
||||
public byte[] read() throws Exception {
|
||||
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 {
|
||||
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);
|
||||
if(ioManager != null) {
|
||||
while (System.currentTimeMillis() < end) {
|
||||
|
@ -16,8 +16,9 @@ import android.hardware.usb.UsbEndpoint;
|
||||
import android.hardware.usb.UsbInterface;
|
||||
import android.util.Log;
|
||||
|
||||
import com.hoho.android.usbserial.BuildConfig;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Collections;
|
||||
import java.util.EnumSet;
|
||||
import java.util.LinkedHashMap;
|
||||
@ -28,6 +29,13 @@ public class ProlificSerialDriver implements UsbSerialDriver {
|
||||
|
||||
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 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_WRITE_REQUEST = 0x01;
|
||||
|
||||
private static final int PROLIFIC_VENDOR_OUT_REQTYPE = UsbConstants.USB_DIR_OUT
|
||||
| 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_OUT_REQTYPE = UsbConstants.USB_DIR_OUT | 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 WRITE_ENDPOINT = 0x02;
|
||||
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 int mDeviceType = DEVICE_TYPE_HX;
|
||||
|
||||
private UsbEndpoint mInterruptEndpoint;
|
||||
|
||||
private int mControlLinesValue = 0;
|
||||
|
||||
private int mBaudRate = -1, mDataBits = -1, mStopBits = -1, mParity = -1;
|
||||
|
||||
private int mStatus = 0;
|
||||
@ -274,11 +274,11 @@ public class ProlificSerialDriver implements UsbSerialDriver {
|
||||
if (mDevice.getDeviceClass() == 0x02) {
|
||||
mDeviceType = DEVICE_TYPE_0;
|
||||
} else {
|
||||
try {
|
||||
Method getRawDescriptorsMethod
|
||||
= mConnection.getClass().getMethod("getRawDescriptors");
|
||||
byte[] rawDescriptors
|
||||
= (byte[]) getRawDescriptorsMethod.invoke(mConnection);
|
||||
byte[] rawDescriptors = connection.getRawDescriptors();
|
||||
if(rawDescriptors == null || rawDescriptors.length <8) {
|
||||
Log.w(TAG, "Could not get device descriptors, Assuming that it is a HX device");
|
||||
mDeviceType = DEVICE_TYPE_HX;
|
||||
} else {
|
||||
byte maxPacketSize0 = rawDescriptors[7];
|
||||
if (maxPacketSize0 == 64) {
|
||||
mDeviceType = DEVICE_TYPE_HX;
|
||||
@ -286,18 +286,9 @@ public class ProlificSerialDriver implements UsbSerialDriver {
|
||||
|| (mDevice.getDeviceClass() == 0xff)) {
|
||||
mDeviceType = DEVICE_TYPE_1;
|
||||
} else {
|
||||
Log.w(TAG, "Could not detect PL2303 subtype, "
|
||||
+ "Assuming that it is a HX device");
|
||||
mDeviceType = DEVICE_TYPE_HX;
|
||||
Log.w(TAG, "Could not detect PL2303 subtype, Assuming that it is a HX device");
|
||||
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);
|
||||
@ -325,8 +316,24 @@ public class ProlificSerialDriver implements UsbSerialDriver {
|
||||
} 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
|
||||
public void setParameters(int baudRate, int dataBits, int stopBits, int parity) throws IOException {
|
||||
baudRate = filterBaudRate(baudRate);
|
||||
if ((mBaudRate == baudRate) && (mDataBits == dataBits)
|
||||
&& (mStopBits == stopBits) && (mParity == parity)) {
|
||||
// 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];
|
||||
|
||||
if(baudRate <= 0) {
|
||||
throw new IllegalArgumentException("Invalid baud rate: " + baudRate);
|
||||
}
|
||||
lineRequestData[0] = (byte) (baudRate & 0xff);
|
||||
lineRequestData[1] = (byte) ((baudRate >> 8) & 0xff);
|
||||
lineRequestData[2] = (byte) ((baudRate >> 16) & 0xff);
|
||||
|
Loading…
x
Reference in New Issue
Block a user