1
0
mirror of https://github.com/mik3y/usb-serial-for-android synced 2025-06-07 16:06:10 +00:00

improve setParameter() error handling

harmonize exception messages, more UI friendly messages
distinguish IllegalArgumentException and UnsupportedOperationException
This commit is contained in:
kai-morich 2019-11-02 13:08:03 +01:00
parent 18b5b6e648
commit 5c6748e1b8
10 changed files with 92 additions and 110 deletions

View File

@ -553,33 +553,12 @@ public class DeviceTest implements SerialInputOutputManager.Listener {
// invalid values // invalid values
try { try {
usbParameters(-1, 8, 1, UsbSerialPort.PARITY_NONE); usbParameters(-1, 8, 1, UsbSerialPort.PARITY_NONE);
if (usbSerialDriver instanceof Ch34xSerialDriver) fail("invalid baud rate");
; // todo: add range check in driver
else if (usbSerialDriver instanceof FtdiSerialDriver)
; // todo: add range check in driver
else if (usbSerialDriver instanceof ProlificSerialDriver)
; // todo: add range check in driver
else if (usbSerialDriver instanceof Cp21xxSerialDriver)
; // todo: add range check in driver
else if (usbSerialDriver instanceof CdcAcmSerialDriver)
; // todo: add range check in driver
else
fail("invalid baudrate 0");
} catch (IOException ignored) { // cp2105 second port
} catch (IllegalArgumentException ignored) { } catch (IllegalArgumentException ignored) {
} }
try { try {
usbParameters(0, 8, 1, UsbSerialPort.PARITY_NONE); usbParameters(0, 8, 1, UsbSerialPort.PARITY_NONE);
if (usbSerialDriver instanceof ProlificSerialDriver) fail("invalid baud rate");
; // todo: add range check in driver
else if (usbSerialDriver instanceof Cp21xxSerialDriver)
; // todo: add range check in driver
else if (usbSerialDriver instanceof CdcAcmSerialDriver)
; // todo: add range check in driver
else
fail("invalid baudrate 0");
} catch (ArithmeticException ignored) { // ch340
} catch (IOException ignored) { // cp2105 second port
} catch (IllegalArgumentException ignored) { } catch (IllegalArgumentException ignored) {
} }
try { try {
@ -593,8 +572,9 @@ public class DeviceTest implements SerialInputOutputManager.Listener {
else if (usbSerialDriver instanceof CdcAcmSerialDriver) else if (usbSerialDriver instanceof CdcAcmSerialDriver)
; ;
else else
fail("invalid baudrate 0"); fail("invalid baudrate 1");
} catch (IOException ignored) { // ch340 } catch (UnsupportedOperationException ignored) { // ch340
} catch (IOException ignored) { // cp2105 second port
} catch (IllegalArgumentException ignored) { } catch (IllegalArgumentException ignored) {
} }
try { try {
@ -679,12 +659,7 @@ public class DeviceTest implements SerialInputOutputManager.Listener {
for(int i: new int[] {0, 4, 9}) { for(int i: new int[] {0, 4, 9}) {
try { try {
usbParameters(19200, i, 1, UsbSerialPort.PARITY_NONE); usbParameters(19200, i, 1, UsbSerialPort.PARITY_NONE);
if (usbSerialDriver instanceof ProlificSerialDriver) fail("invalid databits "+i);
; // todo: add range check in driver
else if (usbSerialDriver instanceof CdcAcmSerialDriver)
; // todo: add range check in driver
else
fail("invalid databits "+i);
} catch (IllegalArgumentException ignored) { } catch (IllegalArgumentException ignored) {
} }
} }
@ -721,7 +696,7 @@ public class DeviceTest implements SerialInputOutputManager.Listener {
usbWrite(new byte[]{(byte) 0xff}); usbWrite(new byte[]{(byte) 0xff});
data = telnetRead(2); data = telnetRead(2);
assertThat("19000/7N1", data, equalTo(new byte[]{(byte) 0x80, (byte) 0xff})); assertThat("19000/7N1", data, equalTo(new byte[]{(byte) 0x80, (byte) 0xff}));
} catch (IllegalArgumentException e) { } catch (UnsupportedOperationException e) {
if(!isCp21xxRestrictedPort) if(!isCp21xxRestrictedPort)
throw e; throw e;
} }
@ -732,7 +707,7 @@ public class DeviceTest implements SerialInputOutputManager.Listener {
usbWrite(new byte[]{(byte) 0xff}); usbWrite(new byte[]{(byte) 0xff});
data = telnetRead(2); data = telnetRead(2);
assertThat("19000/6N1", data, equalTo(new byte[]{(byte) 0xc0, (byte) 0xff})); assertThat("19000/6N1", data, equalTo(new byte[]{(byte) 0xc0, (byte) 0xff}));
} catch (IllegalArgumentException e) { } catch (UnsupportedOperationException e) {
if (!(isCp21xxRestrictedPort || usbSerialDriver instanceof FtdiSerialDriver)) if (!(isCp21xxRestrictedPort || usbSerialDriver instanceof FtdiSerialDriver))
throw e; throw e;
} }
@ -743,7 +718,7 @@ public class DeviceTest implements SerialInputOutputManager.Listener {
usbWrite(new byte[] {(byte)0xff}); usbWrite(new byte[] {(byte)0xff});
data = telnetRead(2); data = telnetRead(2);
assertThat("19000/5N1", data, equalTo(new byte[] {(byte)0xe0, (byte)0xff})); assertThat("19000/5N1", data, equalTo(new byte[] {(byte)0xe0, (byte)0xff}));
} catch (IllegalArgumentException e) { } catch (UnsupportedOperationException e) {
if (!(isCp21xxRestrictedPort || usbSerialDriver instanceof FtdiSerialDriver)) if (!(isCp21xxRestrictedPort || usbSerialDriver instanceof FtdiSerialDriver))
throw e; throw e;
} }
@ -774,11 +749,11 @@ public class DeviceTest implements SerialInputOutputManager.Listener {
try { try {
usbParameters(19200, 8, 1, UsbSerialPort.PARITY_MARK); usbParameters(19200, 8, 1, UsbSerialPort.PARITY_MARK);
fail("parity mark"); fail("parity mark");
} catch (IllegalArgumentException ignored) {} } catch (UnsupportedOperationException ignored) {}
try { try {
usbParameters(19200, 8, 1, UsbSerialPort.PARITY_SPACE); usbParameters(19200, 8, 1, UsbSerialPort.PARITY_SPACE);
fail("parity space"); fail("parity space");
} catch (IllegalArgumentException ignored) {} } catch (UnsupportedOperationException ignored) {}
return; return;
// test below not possible as it requires unsupported 7 dataBits // test below not possible as it requires unsupported 7 dataBits
} }
@ -894,7 +869,7 @@ public class DeviceTest implements SerialInputOutputManager.Listener {
usbWrite(new byte[]{(byte) 0x41, (byte) 0xf9}); usbWrite(new byte[]{(byte) 0x41, (byte) 0xf9});
data = telnetRead(2); data = telnetRead(2);
assertThat("19200/8N1", data, equalTo(new byte[]{1, 11})); assertThat("19200/8N1", data, equalTo(new byte[]{1, 11}));
} catch(IllegalArgumentException e) { } catch(UnsupportedOperationException e) {
if(!isCp21xxRestrictedPort) if(!isCp21xxRestrictedPort)
throw e; throw e;
} }
@ -902,7 +877,7 @@ public class DeviceTest implements SerialInputOutputManager.Listener {
usbParameters(19200, 8, UsbSerialPort.STOPBITS_1_5, UsbSerialPort.PARITY_NONE); usbParameters(19200, 8, UsbSerialPort.STOPBITS_1_5, UsbSerialPort.PARITY_NONE);
// todo: could create similar test for 1.5 stopbits, by reading at double speed // todo: could create similar test for 1.5 stopbits, by reading at double speed
// but only some devices support 1.5 stopbits and it is basically not used any more // but only some devices support 1.5 stopbits and it is basically not used any more
} catch(IllegalArgumentException ignored) { } catch(UnsupportedOperationException ignored) {
} }
} }
} }

View File

@ -134,14 +134,14 @@ public class CdcAcmSerialDriver implements UsbSerialDriver {
Log.d(TAG, "data iface=" + mDataInterface); Log.d(TAG, "data iface=" + mDataInterface);
if (!mConnection.claimInterface(mControlInterface, true)) { if (!mConnection.claimInterface(mControlInterface, true)) {
throw new IOException("Could not claim shared control/data interface."); throw new IOException("Could not claim shared control/data interface");
} }
int endCount = mControlInterface.getEndpointCount(); int endCount = mControlInterface.getEndpointCount();
if (endCount < 3) { if (endCount < 3) {
Log.d(TAG,"not enough endpoints - need 3. count=" + mControlInterface.getEndpointCount()); Log.d(TAG,"not enough endpoints - need 3. count=" + mControlInterface.getEndpointCount());
throw new IOException("Insufficient number of endpoints(" + mControlInterface.getEndpointCount() + ")"); throw new IOException("Insufficient number of endpoints (" + mControlInterface.getEndpointCount() + ")");
} }
// Analyse endpoints for their properties // Analyse endpoints for their properties
@ -198,26 +198,26 @@ public class CdcAcmSerialDriver implements UsbSerialDriver {
} }
if(mControlInterface == null) { if(mControlInterface == null) {
throw new IOException("no control interface."); throw new IOException("No control interface");
} }
Log.d(TAG, "Control iface=" + mControlInterface); Log.d(TAG, "Control iface=" + mControlInterface);
if (!mConnection.claimInterface(mControlInterface, true)) { if (!mConnection.claimInterface(mControlInterface, true)) {
throw new IOException("Could not claim control interface."); throw new IOException("Could not claim control interface");
} }
mControlEndpoint = mControlInterface.getEndpoint(0); mControlEndpoint = mControlInterface.getEndpoint(0);
if (mControlEndpoint.getDirection() != UsbConstants.USB_DIR_IN || mControlEndpoint.getType() != UsbConstants.USB_ENDPOINT_XFER_INT) { if (mControlEndpoint.getDirection() != UsbConstants.USB_DIR_IN || mControlEndpoint.getType() != UsbConstants.USB_ENDPOINT_XFER_INT) {
throw new IOException("invalid control endpoint"); throw new IOException("Invalid control endpoint");
} }
if(mDataInterface == null) { if(mDataInterface == null) {
throw new IOException("no data interface."); throw new IOException("No data interface");
} }
Log.d(TAG, "data iface=" + mDataInterface); Log.d(TAG, "data iface=" + mDataInterface);
if (!mConnection.claimInterface(mDataInterface, true)) { if (!mConnection.claimInterface(mDataInterface, true)) {
throw new IOException("Could not claim data interface."); throw new IOException("Could not claim data interface");
} }
mReadEndpoint = null; mReadEndpoint = null;
@ -230,7 +230,7 @@ public class CdcAcmSerialDriver implements UsbSerialDriver {
mWriteEndpoint = ep; mWriteEndpoint = ep;
} }
if (mReadEndpoint == null || mWriteEndpoint == null) { if (mReadEndpoint == null || mWriteEndpoint == null) {
throw new IOException("Could not get read&write endpoints."); throw new IOException("Could not get read&write endpoints");
} }
} }
@ -238,7 +238,7 @@ public class CdcAcmSerialDriver implements UsbSerialDriver {
int len = mConnection.controlTransfer( int len = mConnection.controlTransfer(
USB_RT_ACM, request, value, mControlIndex, buf, buf != null ? buf.length : 0, 5000); USB_RT_ACM, request, value, mControlIndex, buf, buf != null ? buf.length : 0, 5000);
if(len < 0) { if(len < 0) {
throw new IOException("controlTransfer failed."); throw new IOException("controlTransfer failed");
} }
return len; return len;
} }
@ -276,7 +276,7 @@ public class CdcAcmSerialDriver implements UsbSerialDriver {
request.initialize(mConnection, mReadEndpoint); request.initialize(mConnection, mReadEndpoint);
final ByteBuffer buf = ByteBuffer.wrap(dest); final ByteBuffer buf = ByteBuffer.wrap(dest);
if (!request.queue(buf, dest.length)) { if (!request.queue(buf, dest.length)) {
throw new IOException("Error queueing request."); throw new IOException("Error queueing request");
} }
mUsbRequest = request; mUsbRequest = request;
final UsbRequest response = mConnection.requestWait(); final UsbRequest response = mConnection.requestWait();
@ -340,12 +340,18 @@ public class CdcAcmSerialDriver implements UsbSerialDriver {
@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 {
if(baudRate <= 0) {
throw new IllegalArgumentException("Invalid baud rate: " + baudRate);
}
if(dataBits < DATABITS_5 || dataBits > DATABITS_8) {
throw new IllegalArgumentException("Invalid data bits: " + dataBits);
}
byte stopBitsByte; byte stopBitsByte;
switch (stopBits) { switch (stopBits) {
case STOPBITS_1: stopBitsByte = 0; break; case STOPBITS_1: stopBitsByte = 0; break;
case STOPBITS_1_5: stopBitsByte = 1; break; case STOPBITS_1_5: stopBitsByte = 1; break;
case STOPBITS_2: stopBitsByte = 2; break; case STOPBITS_2: stopBitsByte = 2; break;
default: throw new IllegalArgumentException("Bad value for stopBits: " + stopBits); default: throw new IllegalArgumentException("Invalid stop bits: " + stopBits);
} }
byte parityBitesByte; byte parityBitesByte;
@ -355,9 +361,8 @@ public class CdcAcmSerialDriver implements UsbSerialDriver {
case PARITY_EVEN: parityBitesByte = 2; break; case PARITY_EVEN: parityBitesByte = 2; break;
case PARITY_MARK: parityBitesByte = 3; break; case PARITY_MARK: parityBitesByte = 3; break;
case PARITY_SPACE: parityBitesByte = 4; break; case PARITY_SPACE: parityBitesByte = 4; break;
default: throw new IllegalArgumentException("Bad value for parity: " + parity); default: throw new IllegalArgumentException("Invalid parity: " + parity);
} }
byte[] msg = { byte[] msg = {
(byte) ( baudRate & 0xff), (byte) ( baudRate & 0xff),
(byte) ((baudRate >> 8 ) & 0xff), (byte) ((baudRate >> 8 ) & 0xff),

View File

@ -99,7 +99,7 @@ public class Ch34xSerialDriver implements UsbSerialDriver {
@Override @Override
public void open(UsbDeviceConnection connection) throws IOException { public void open(UsbDeviceConnection connection) throws IOException {
if (mConnection != null) { if (mConnection != null) {
throw new IOException("Already opened."); throw new IOException("Already open");
} }
mConnection = connection; mConnection = connection;
@ -108,7 +108,7 @@ public class Ch34xSerialDriver implements UsbSerialDriver {
for (int i = 0; i < mDevice.getInterfaceCount(); i++) { for (int i = 0; i < mDevice.getInterfaceCount(); i++) {
UsbInterface usbIface = mDevice.getInterface(i); UsbInterface usbIface = mDevice.getInterface(i);
if (!mConnection.claimInterface(usbIface, true)) { if (!mConnection.claimInterface(usbIface, true)) {
throw new IOException("Could not claim data interface."); throw new IOException("Could not claim data interface");
} }
} }
@ -166,7 +166,7 @@ public class Ch34xSerialDriver implements UsbSerialDriver {
request.initialize(mConnection, mReadEndpoint); request.initialize(mConnection, mReadEndpoint);
final ByteBuffer buf = ByteBuffer.wrap(dest); final ByteBuffer buf = ByteBuffer.wrap(dest);
if (!request.queue(buf, dest.length)) { if (!request.queue(buf, dest.length)) {
throw new IOException("Error queueing request."); throw new IOException("Error queueing request");
} }
mUsbRequest = request; mUsbRequest = request;
final UsbRequest response = mConnection.requestWait(); final UsbRequest response = mConnection.requestWait();
@ -275,7 +275,7 @@ public class Ch34xSerialDriver implements UsbSerialDriver {
checkState("init #1", 0x5f, 0, new int[]{-1 /* 0x27, 0x30 */, 0x00}); checkState("init #1", 0x5f, 0, new int[]{-1 /* 0x27, 0x30 */, 0x00});
if (controlOut(0xa1, 0, 0) < 0) { if (controlOut(0xa1, 0, 0) < 0) {
throw new IOException("init failed! #2"); throw new IOException("Init failed: #2");
} }
setBaudRate(DEFAULT_BAUD_RATE); setBaudRate(DEFAULT_BAUD_RATE);
@ -283,13 +283,13 @@ public class Ch34xSerialDriver implements UsbSerialDriver {
checkState("init #4", 0x95, 0x2518, new int[]{-1 /* 0x56, c3*/, 0x00}); checkState("init #4", 0x95, 0x2518, new int[]{-1 /* 0x56, c3*/, 0x00});
if (controlOut(0x9a, 0x2518, LCR_ENABLE_RX | LCR_ENABLE_TX | LCR_CS8) < 0) { if (controlOut(0x9a, 0x2518, LCR_ENABLE_RX | LCR_ENABLE_TX | LCR_CS8) < 0) {
throw new IOException("init failed! #5"); throw new IOException("Init failed: #5");
} }
checkState("init #6", 0x95, 0x0706, new int[]{0xff, 0xee}); checkState("init #6", 0x95, 0x0706, new int[]{0xff, 0xee});
if (controlOut(0xa1, 0x501f, 0xd90a) < 0) { if (controlOut(0xa1, 0x501f, 0xd90a) < 0) {
throw new IOException("init failed! #7"); throw new IOException("Init failed: #7");
} }
setBaudRate(DEFAULT_BAUD_RATE); setBaudRate(DEFAULT_BAUD_RATE);
@ -313,25 +313,27 @@ public class Ch34xSerialDriver implements UsbSerialDriver {
} }
if (factor > 0xfff0) { if (factor > 0xfff0) {
throw new IOException("Baudrate " + baudRate + " not supported"); throw new UnsupportedOperationException("Unsupported baud rate: " + baudRate);
} }
factor = 0x10000 - factor; factor = 0x10000 - factor;
int ret = controlOut(0x9a, 0x1312, (int) ((factor & 0xff00) | divisor)); int ret = controlOut(0x9a, 0x1312, (int) ((factor & 0xff00) | divisor));
if (ret < 0) { if (ret < 0) {
throw new IOException("Error setting baud rate. #1)"); throw new IOException("Error setting baud rate: #1)");
} }
ret = controlOut(0x9a, 0x0f2c, (int) (factor & 0xff)); ret = controlOut(0x9a, 0x0f2c, (int) (factor & 0xff));
if (ret < 0) { if (ret < 0) {
throw new IOException("Error setting baud rate. #2"); throw new IOException("Error setting baud rate: #2");
} }
} }
@Override @Override
public void setParameters(int baudRate, int dataBits, int stopBits, int parity) public void setParameters(int baudRate, int dataBits, int stopBits, int parity) throws IOException {
throws IOException { if(baudRate <= 0) {
throw new IllegalArgumentException("Invalid baud rate: " + baudRate);
}
setBaudRate(baudRate); setBaudRate(baudRate);
int lcr = LCR_ENABLE_RX | LCR_ENABLE_TX; int lcr = LCR_ENABLE_RX | LCR_ENABLE_TX;
@ -350,7 +352,7 @@ public class Ch34xSerialDriver implements UsbSerialDriver {
lcr |= LCR_CS8; lcr |= LCR_CS8;
break; break;
default: default:
throw new IllegalArgumentException("Unknown dataBits value: " + dataBits); throw new IllegalArgumentException("Invalid data bits: " + dataBits);
} }
switch (parity) { switch (parity) {
@ -369,19 +371,19 @@ public class Ch34xSerialDriver implements UsbSerialDriver {
lcr |= LCR_ENABLE_PAR | LCR_MARK_SPACE | LCR_PAR_EVEN; lcr |= LCR_ENABLE_PAR | LCR_MARK_SPACE | LCR_PAR_EVEN;
break; break;
default: default:
throw new IllegalArgumentException("Unknown parity value: " + parity); throw new IllegalArgumentException("Invalid parity: " + parity);
} }
switch (stopBits) { switch (stopBits) {
case STOPBITS_1: case STOPBITS_1:
break; break;
case STOPBITS_1_5: case STOPBITS_1_5:
throw new IllegalArgumentException("Unsupported stopBits value: 1.5"); throw new UnsupportedOperationException("Unsupported stop bits: 1.5");
case STOPBITS_2: case STOPBITS_2:
lcr |= LCR_STOP_BITS_2; lcr |= LCR_STOP_BITS_2;
break; break;
default: default:
throw new IllegalArgumentException("Unknown stopBits value: " + stopBits); throw new IllegalArgumentException("Invalid stop bits: " + stopBits);
} }
int ret = controlOut(0x9a, 0x2518, lcr); int ret = controlOut(0x9a, 0x2518, lcr);

View File

@ -132,8 +132,7 @@ abstract class CommonUsbSerialPort implements UsbSerialPort {
public abstract int write(final byte[] src, final int timeoutMillis) throws IOException; public abstract int write(final byte[] src, final int timeoutMillis) throws IOException;
@Override @Override
public abstract void setParameters( public abstract void setParameters(int baudRate, int dataBits, int stopBits, int parity) throws IOException;
int baudRate, int dataBits, int stopBits, int parity) throws IOException;
@Override @Override
public abstract boolean getCD() throws IOException; public abstract boolean getCD() throws IOException;

View File

@ -135,7 +135,7 @@ public class Cp21xxSerialDriver implements UsbSerialDriver {
@Override @Override
public void open(UsbDeviceConnection connection) throws IOException { public void open(UsbDeviceConnection connection) throws IOException {
if (mConnection != null) { if (mConnection != null) {
throw new IOException("Already opened."); throw new IOException("Already open");
} }
mConnection = connection; mConnection = connection;
@ -205,7 +205,7 @@ public class Cp21xxSerialDriver implements UsbSerialDriver {
request.initialize(mConnection, mReadEndpoint); request.initialize(mConnection, mReadEndpoint);
final ByteBuffer buf = ByteBuffer.wrap(dest); final ByteBuffer buf = ByteBuffer.wrap(dest);
if (!request.queue(buf, dest.length)) { if (!request.queue(buf, dest.length)) {
throw new IOException("Error queueing request."); throw new IOException("Error queueing request");
} }
mUsbRequest = request; mUsbRequest = request;
final UsbRequest response = mConnection.requestWait(); final UsbRequest response = mConnection.requestWait();
@ -276,37 +276,39 @@ public class Cp21xxSerialDriver implements UsbSerialDriver {
int ret = mConnection.controlTransfer(REQTYPE_HOST_TO_DEVICE, SILABSER_SET_BAUDRATE, int ret = mConnection.controlTransfer(REQTYPE_HOST_TO_DEVICE, SILABSER_SET_BAUDRATE,
0, mPortNumber, data, 4, USB_WRITE_TIMEOUT_MILLIS); 0, mPortNumber, data, 4, USB_WRITE_TIMEOUT_MILLIS);
if (ret < 0) { if (ret < 0) {
throw new IOException("Error setting baud rate."); throw new IOException("Error setting baud rate");
} }
} }
@Override @Override
public void setParameters(int baudRate, int dataBits, int stopBits, int parity) public void setParameters(int baudRate, int dataBits, int stopBits, int parity) throws IOException {
throws IOException { if(baudRate <= 0) {
throw new IllegalArgumentException("Invalid baud rate: " + baudRate);
}
setBaudRate(baudRate); setBaudRate(baudRate);
int configDataBits = 0; int configDataBits = 0;
switch (dataBits) { switch (dataBits) {
case DATABITS_5: case DATABITS_5:
if(mIsRestrictedPort) if(mIsRestrictedPort)
throw new IllegalArgumentException("Unsupported dataBits value: " + dataBits); throw new UnsupportedOperationException("Unsupported data bits: " + dataBits);
configDataBits |= 0x0500; configDataBits |= 0x0500;
break; break;
case DATABITS_6: case DATABITS_6:
if(mIsRestrictedPort) if(mIsRestrictedPort)
throw new IllegalArgumentException("Unsupported dataBits value: " + dataBits); throw new UnsupportedOperationException("Unsupported data bits: " + dataBits);
configDataBits |= 0x0600; configDataBits |= 0x0600;
break; break;
case DATABITS_7: case DATABITS_7:
if(mIsRestrictedPort) if(mIsRestrictedPort)
throw new IllegalArgumentException("Unsupported dataBits value: " + dataBits); throw new UnsupportedOperationException("Unsupported data bits: " + dataBits);
configDataBits |= 0x0700; configDataBits |= 0x0700;
break; break;
case DATABITS_8: case DATABITS_8:
configDataBits |= 0x0800; configDataBits |= 0x0800;
break; break;
default: default:
throw new IllegalArgumentException("Unknown dataBits value: " + dataBits); throw new IllegalArgumentException("Invalid data bits: " + dataBits);
} }
switch (parity) { switch (parity) {
@ -320,30 +322,30 @@ public class Cp21xxSerialDriver implements UsbSerialDriver {
break; break;
case PARITY_MARK: case PARITY_MARK:
if(mIsRestrictedPort) if(mIsRestrictedPort)
throw new IllegalArgumentException("Unsupported parity value: mark"); throw new UnsupportedOperationException("Unsupported parity: mark");
configDataBits |= 0x0030; configDataBits |= 0x0030;
break; break;
case PARITY_SPACE: case PARITY_SPACE:
if(mIsRestrictedPort) if(mIsRestrictedPort)
throw new IllegalArgumentException("Unsupported parity value: space"); throw new UnsupportedOperationException("Unsupported parity: space");
configDataBits |= 0x0040; configDataBits |= 0x0040;
break; break;
default: default:
throw new IllegalArgumentException("Unknown parity value: " + parity); throw new IllegalArgumentException("Invalid parity: " + parity);
} }
switch (stopBits) { switch (stopBits) {
case STOPBITS_1: case STOPBITS_1:
break; break;
case STOPBITS_1_5: case STOPBITS_1_5:
throw new IllegalArgumentException("Unsupported stopBits value: 1.5"); throw new UnsupportedOperationException("Unsupported stop bits: 1.5");
case STOPBITS_2: case STOPBITS_2:
if(mIsRestrictedPort) if(mIsRestrictedPort)
throw new IllegalArgumentException("Unsupported stopBits value: 2"); throw new UnsupportedOperationException("Unsupported stop bits: 2");
configDataBits |= 2; configDataBits |= 2;
break; break;
default: default:
throw new IllegalArgumentException("Unknown stopBits value: " + stopBits); throw new IllegalArgumentException("Invalid stop bits: " + stopBits);
} }
setConfigSingle(SILABSER_SET_LINE_CTL_REQUEST_CODE, configDataBits); setConfigSingle(SILABSER_SET_LINE_CTL_REQUEST_CODE, configDataBits);
} }

View File

@ -287,7 +287,7 @@ public class FtdiSerialDriver implements UsbSerialDriver {
try { try {
request.initialize(mConnection, endpoint); request.initialize(mConnection, endpoint);
if (!request.queue(buf, dest.length)) { if (!request.queue(buf, dest.length)) {
throw new IOException("Error queueing request."); throw new IOException("Error queueing request");
} }
final UsbRequest response = mConnection.requestWait(); final UsbRequest response = mConnection.requestWait();
@ -360,21 +360,23 @@ public class FtdiSerialDriver implements UsbSerialDriver {
} }
@Override @Override
public void setParameters(int baudRate, int dataBits, int stopBits, int parity) public void setParameters(int baudRate, int dataBits, int stopBits, int parity) throws IOException {
throws IOException { if(baudRate <= 0) {
throw new IllegalArgumentException("Invalid baud rate: " + baudRate);
}
setBaudRate(baudRate); setBaudRate(baudRate);
int config = 0; int config = 0;
switch (dataBits) { switch (dataBits) {
case DATABITS_5: case DATABITS_5:
case DATABITS_6: case DATABITS_6:
throw new IllegalArgumentException("Unsupported dataBits value: " + dataBits); throw new UnsupportedOperationException("Unsupported data bits: " + dataBits);
case DATABITS_7: case DATABITS_7:
case DATABITS_8: case DATABITS_8:
config |= dataBits; config |= dataBits;
break; break;
default: default:
throw new IllegalArgumentException("Unknown dataBits value: " + dataBits); throw new IllegalArgumentException("Invalid data bits: " + dataBits);
} }
switch (parity) { switch (parity) {
@ -394,7 +396,7 @@ public class FtdiSerialDriver implements UsbSerialDriver {
config |= (0x04 << 8); config |= (0x04 << 8);
break; break;
default: default:
throw new IllegalArgumentException("Unknown parity value: " + parity); throw new IllegalArgumentException("Invalid parity: " + parity);
} }
switch (stopBits) { switch (stopBits) {
@ -402,12 +404,12 @@ public class FtdiSerialDriver implements UsbSerialDriver {
config |= (0x00 << 11); config |= (0x00 << 11);
break; break;
case STOPBITS_1_5: case STOPBITS_1_5:
throw new IllegalArgumentException("Unsupported stopBits value: 1.5"); throw new UnsupportedOperationException("Unsupported stop bits: 1.5");
case STOPBITS_2: case STOPBITS_2:
config |= (0x02 << 11); config |= (0x02 << 11);
break; break;
default: default:
throw new IllegalArgumentException("Unknown stopBits value: " + stopBits); throw new IllegalArgumentException("Invalid stop bits: " + stopBits);
} }
int result = mConnection.controlTransfer(FTDI_DEVICE_OUT_REQTYPE, int result = mConnection.controlTransfer(FTDI_DEVICE_OUT_REQTYPE,

View File

@ -380,7 +380,7 @@ public class ProlificSerialDriver implements UsbSerialDriver {
request.initialize(mConnection, mReadEndpoint); request.initialize(mConnection, mReadEndpoint);
final ByteBuffer buf = ByteBuffer.wrap(dest); final ByteBuffer buf = ByteBuffer.wrap(dest);
if (!request.queue(buf, dest.length)) { if (!request.queue(buf, dest.length)) {
throw new IOException("Error queueing request."); throw new IOException("Error queueing request");
} }
final UsbRequest response = mConnection.requestWait(); final UsbRequest response = mConnection.requestWait();
@ -439,8 +439,7 @@ public class ProlificSerialDriver implements UsbSerialDriver {
} }
@Override @Override
public void setParameters(int baudRate, int dataBits, int stopBits, public void setParameters(int baudRate, int dataBits, int stopBits, int parity) throws IOException {
int parity) throws IOException {
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
@ -449,6 +448,9 @@ 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);
@ -458,44 +460,39 @@ public class ProlificSerialDriver implements UsbSerialDriver {
case STOPBITS_1: case STOPBITS_1:
lineRequestData[4] = 0; lineRequestData[4] = 0;
break; break;
case STOPBITS_1_5: case STOPBITS_1_5:
lineRequestData[4] = 1; lineRequestData[4] = 1;
break; break;
case STOPBITS_2: case STOPBITS_2:
lineRequestData[4] = 2; lineRequestData[4] = 2;
break; break;
default: default:
throw new IllegalArgumentException("Unknown stopBits value: " + stopBits); throw new IllegalArgumentException("Invalid stop bits: " + stopBits);
} }
switch (parity) { switch (parity) {
case PARITY_NONE: case PARITY_NONE:
lineRequestData[5] = 0; lineRequestData[5] = 0;
break; break;
case PARITY_ODD: case PARITY_ODD:
lineRequestData[5] = 1; lineRequestData[5] = 1;
break; break;
case PARITY_EVEN: case PARITY_EVEN:
lineRequestData[5] = 2; lineRequestData[5] = 2;
break; break;
case PARITY_MARK: case PARITY_MARK:
lineRequestData[5] = 3; lineRequestData[5] = 3;
break; break;
case PARITY_SPACE: case PARITY_SPACE:
lineRequestData[5] = 4; lineRequestData[5] = 4;
break; break;
default: default:
throw new IllegalArgumentException("Unknown parity value: " + parity); throw new IllegalArgumentException("Invalid parity: " + parity);
} }
if(dataBits < DATABITS_5 || dataBits > DATABITS_8) {
throw new IllegalArgumentException("Invalid data bits: " + dataBits);
}
lineRequestData[6] = (byte) dataBits; lineRequestData[6] = (byte) dataBits;
ctrlOut(SET_LINE_REQUEST, 0, 0, lineRequestData); ctrlOut(SET_LINE_REQUEST, 0, 0, lineRequestData);

View File

@ -76,7 +76,7 @@ public final class UsbId {
public static final int ARM_MBED = 0x0204; public static final int ARM_MBED = 0x0204;
private UsbId() { private UsbId() {
throw new IllegalAccessError("Non-instantiable class."); throw new IllegalAccessError("Non-instantiable class");
} }
} }

View File

@ -145,9 +145,9 @@ public interface UsbSerialPort {
* {@link #PARITY_EVEN}, {@link #PARITY_MARK}, or * {@link #PARITY_EVEN}, {@link #PARITY_MARK}, or
* {@link #PARITY_SPACE}. * {@link #PARITY_SPACE}.
* @throws IOException on error setting the port parameters * @throws IOException on error setting the port parameters
* @throws UnsupportedOperationException if not supported by a specific device
*/ */
public void setParameters( public void setParameters(int baudRate, int dataBits, int stopBits, int parity) throws IOException;
int baudRate, int dataBits, int stopBits, int parity) throws IOException;
/** /**
* Gets the CD (Carrier Detect) bit from the underlying UART. * Gets the CD (Carrier Detect) bit from the underlying UART.

View File

@ -125,12 +125,12 @@ public class SerialInputOutputManager implements Runnable {
public void run() { public void run() {
synchronized (this) { synchronized (this) {
if (getState() != State.STOPPED) { if (getState() != State.STOPPED) {
throw new IllegalStateException("Already running."); throw new IllegalStateException("Already running");
} }
mState = State.RUNNING; mState = State.RUNNING;
} }
Log.i(TAG, "Running .."); Log.i(TAG, "Running ...");
try { try {
while (true) { while (true) {
if (getState() != State.RUNNING) { if (getState() != State.RUNNING) {
@ -148,7 +148,7 @@ public class SerialInputOutputManager implements Runnable {
} finally { } finally {
synchronized (this) { synchronized (this) {
mState = State.STOPPED; mState = State.STOPPED;
Log.i(TAG, "Stopped."); Log.i(TAG, "Stopped");
} }
} }
} }