mirror of
https://github.com/mik3y/usb-serial-for-android.git
synced 2026-08-20 21:02:57 +00:00
PL2303 throw error on unsupported baud rates
instead of silently falling back to 9600 baud
This commit is contained in:
+35
-32
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user