mirror of
				https://github.com/mik3y/usb-serial-for-android
				synced 2025-10-30 18:07:21 +00:00 
			
		
		
		
	improve setParameter() error handling
harmonize exception messages, more UI friendly messages distinguish IllegalArgumentException and UnsupportedOperationException
This commit is contained in:
		
							parent
							
								
									18b5b6e648
								
							
						
					
					
						commit
						5c6748e1b8
					
				| @ -553,33 +553,12 @@ public class DeviceTest implements SerialInputOutputManager.Listener { | ||||
|         // invalid values | ||||
|         try { | ||||
|             usbParameters(-1, 8, 1, UsbSerialPort.PARITY_NONE); | ||||
|             if (usbSerialDriver instanceof Ch34xSerialDriver) | ||||
|                 ; // 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 | ||||
|             fail("invalid baud rate"); | ||||
|         } catch (IllegalArgumentException ignored) { | ||||
|         } | ||||
|         try { | ||||
|             usbParameters(0, 8, 1, UsbSerialPort.PARITY_NONE); | ||||
|             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 (ArithmeticException ignored) { // ch340 | ||||
|         } catch (IOException ignored) { // cp2105 second port | ||||
|             fail("invalid baud rate"); | ||||
|         } catch (IllegalArgumentException ignored) { | ||||
|         } | ||||
|         try { | ||||
| @ -593,8 +572,9 @@ public class DeviceTest implements SerialInputOutputManager.Listener { | ||||
|             else if (usbSerialDriver instanceof CdcAcmSerialDriver) | ||||
|                 ; | ||||
|             else | ||||
|                 fail("invalid baudrate 0"); | ||||
|         } catch (IOException ignored) { // ch340 | ||||
|                 fail("invalid baudrate 1"); | ||||
|         } catch (UnsupportedOperationException ignored) { // ch340 | ||||
|         } catch (IOException ignored) { // cp2105 second port | ||||
|         } catch (IllegalArgumentException ignored) { | ||||
|         } | ||||
|         try { | ||||
| @ -679,12 +659,7 @@ public class DeviceTest implements SerialInputOutputManager.Listener { | ||||
|         for(int i: new int[] {0, 4, 9}) { | ||||
|             try { | ||||
|                 usbParameters(19200, i, 1, UsbSerialPort.PARITY_NONE); | ||||
|                 if (usbSerialDriver instanceof ProlificSerialDriver) | ||||
|                     ; // todo: add range check in driver | ||||
|                 else if (usbSerialDriver instanceof CdcAcmSerialDriver) | ||||
|                     ; // todo: add range check in driver | ||||
|                 else | ||||
|                     fail("invalid databits "+i); | ||||
|                 fail("invalid databits "+i); | ||||
|             } catch (IllegalArgumentException ignored) { | ||||
|             } | ||||
|         } | ||||
| @ -721,7 +696,7 @@ public class DeviceTest implements SerialInputOutputManager.Listener { | ||||
|             usbWrite(new byte[]{(byte) 0xff}); | ||||
|             data = telnetRead(2); | ||||
|             assertThat("19000/7N1", data, equalTo(new byte[]{(byte) 0x80, (byte) 0xff})); | ||||
|         } catch (IllegalArgumentException e) { | ||||
|         } catch (UnsupportedOperationException e) { | ||||
|                 if(!isCp21xxRestrictedPort) | ||||
|                     throw e; | ||||
|         } | ||||
| @ -732,7 +707,7 @@ public class DeviceTest implements SerialInputOutputManager.Listener { | ||||
|             usbWrite(new byte[]{(byte) 0xff}); | ||||
|             data = telnetRead(2); | ||||
|             assertThat("19000/6N1", data, equalTo(new byte[]{(byte) 0xc0, (byte) 0xff})); | ||||
|         } catch (IllegalArgumentException e) { | ||||
|         } catch (UnsupportedOperationException e) { | ||||
|             if (!(isCp21xxRestrictedPort || usbSerialDriver instanceof FtdiSerialDriver)) | ||||
|                 throw e; | ||||
|         } | ||||
| @ -743,7 +718,7 @@ public class DeviceTest implements SerialInputOutputManager.Listener { | ||||
|             usbWrite(new byte[] {(byte)0xff}); | ||||
|             data = telnetRead(2); | ||||
|             assertThat("19000/5N1", data, equalTo(new byte[] {(byte)0xe0, (byte)0xff})); | ||||
|         } catch (IllegalArgumentException e) { | ||||
|         } catch (UnsupportedOperationException e) { | ||||
|             if (!(isCp21xxRestrictedPort || usbSerialDriver instanceof FtdiSerialDriver)) | ||||
|                 throw e; | ||||
|         } | ||||
| @ -774,11 +749,11 @@ public class DeviceTest implements SerialInputOutputManager.Listener { | ||||
|             try { | ||||
|                 usbParameters(19200, 8, 1, UsbSerialPort.PARITY_MARK); | ||||
|                 fail("parity mark"); | ||||
|             } catch (IllegalArgumentException ignored) {} | ||||
|             } catch (UnsupportedOperationException ignored) {} | ||||
|             try { | ||||
|                 usbParameters(19200, 8, 1, UsbSerialPort.PARITY_SPACE); | ||||
|                 fail("parity space"); | ||||
|             } catch (IllegalArgumentException ignored) {} | ||||
|             } catch (UnsupportedOperationException ignored) {} | ||||
|             return; | ||||
|             // 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}); | ||||
|                 data = telnetRead(2); | ||||
|                 assertThat("19200/8N1", data, equalTo(new byte[]{1, 11})); | ||||
|             } catch(IllegalArgumentException e) { | ||||
|             } catch(UnsupportedOperationException e) { | ||||
|                 if(!isCp21xxRestrictedPort) | ||||
|                     throw e; | ||||
|             } | ||||
| @ -902,7 +877,7 @@ public class DeviceTest implements SerialInputOutputManager.Listener { | ||||
|                 usbParameters(19200, 8, UsbSerialPort.STOPBITS_1_5, UsbSerialPort.PARITY_NONE); | ||||
|                 // 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 | ||||
|             } catch(IllegalArgumentException ignored) { | ||||
|             } catch(UnsupportedOperationException ignored) { | ||||
|             } | ||||
|         } | ||||
|     } | ||||
|  | ||||
| @ -134,14 +134,14 @@ public class CdcAcmSerialDriver implements UsbSerialDriver { | ||||
|             Log.d(TAG, "data iface=" + mDataInterface); | ||||
| 
 | ||||
|             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(); | ||||
| 
 | ||||
|             if (endCount < 3) { | ||||
|                 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 | ||||
| @ -198,26 +198,26 @@ public class CdcAcmSerialDriver implements UsbSerialDriver { | ||||
|             } | ||||
| 
 | ||||
|             if(mControlInterface == null) { | ||||
|                 throw new IOException("no control interface."); | ||||
|                 throw new IOException("No control interface"); | ||||
|             } | ||||
|             Log.d(TAG, "Control iface=" + mControlInterface); | ||||
| 
 | ||||
|             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); | ||||
|             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) { | ||||
|                 throw new IOException("no data interface."); | ||||
|                 throw new IOException("No data interface"); | ||||
|             } | ||||
|             Log.d(TAG, "data iface=" + mDataInterface); | ||||
| 
 | ||||
|             if (!mConnection.claimInterface(mDataInterface, true)) { | ||||
|                 throw new IOException("Could not claim data interface."); | ||||
|                 throw new IOException("Could not claim data interface"); | ||||
|             } | ||||
| 
 | ||||
|             mReadEndpoint = null; | ||||
| @ -230,7 +230,7 @@ public class CdcAcmSerialDriver implements UsbSerialDriver { | ||||
|                     mWriteEndpoint = ep; | ||||
|             } | ||||
|             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( | ||||
|                     USB_RT_ACM, request, value, mControlIndex, buf, buf != null ? buf.length : 0, 5000); | ||||
|             if(len < 0) { | ||||
|                 throw new IOException("controlTransfer failed."); | ||||
|                 throw new IOException("controlTransfer failed"); | ||||
|             } | ||||
|             return len; | ||||
|         } | ||||
| @ -276,7 +276,7 @@ public class CdcAcmSerialDriver implements UsbSerialDriver { | ||||
|                 request.initialize(mConnection, mReadEndpoint); | ||||
|                 final ByteBuffer buf = ByteBuffer.wrap(dest); | ||||
|                 if (!request.queue(buf, dest.length)) { | ||||
|                     throw new IOException("Error queueing request."); | ||||
|                     throw new IOException("Error queueing request"); | ||||
|                 } | ||||
|                 mUsbRequest = request; | ||||
|                 final UsbRequest response = mConnection.requestWait(); | ||||
| @ -340,12 +340,18 @@ public class CdcAcmSerialDriver implements UsbSerialDriver { | ||||
| 
 | ||||
|         @Override | ||||
|         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; | ||||
|             switch (stopBits) { | ||||
|                 case STOPBITS_1: stopBitsByte = 0; break; | ||||
|                 case STOPBITS_1_5: stopBitsByte = 1; 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; | ||||
| @ -355,9 +361,8 @@ public class CdcAcmSerialDriver implements UsbSerialDriver { | ||||
|                 case PARITY_EVEN: parityBitesByte = 2; break; | ||||
|                 case PARITY_MARK: parityBitesByte = 3; 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) ( baudRate & 0xff), | ||||
|                     (byte) ((baudRate >> 8 ) & 0xff), | ||||
|  | ||||
| @ -99,7 +99,7 @@ public class Ch34xSerialDriver implements UsbSerialDriver { | ||||
| 		@Override | ||||
| 		public void open(UsbDeviceConnection connection) throws IOException { | ||||
| 			if (mConnection != null) { | ||||
| 				throw new IOException("Already opened."); | ||||
| 				throw new IOException("Already open"); | ||||
| 			} | ||||
| 
 | ||||
| 			mConnection = connection; | ||||
| @ -108,7 +108,7 @@ public class Ch34xSerialDriver implements UsbSerialDriver { | ||||
| 				for (int i = 0; i < mDevice.getInterfaceCount(); i++) { | ||||
| 					UsbInterface usbIface = mDevice.getInterface(i); | ||||
| 					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); | ||||
| 				final ByteBuffer buf = ByteBuffer.wrap(dest); | ||||
| 				if (!request.queue(buf, dest.length)) { | ||||
| 					throw new IOException("Error queueing request."); | ||||
| 					throw new IOException("Error queueing request"); | ||||
| 				} | ||||
| 				mUsbRequest = request; | ||||
| 				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}); | ||||
| 
 | ||||
| 			if (controlOut(0xa1, 0, 0) < 0) { | ||||
| 				throw new IOException("init failed! #2"); | ||||
| 				throw new IOException("Init failed: #2"); | ||||
| 			} | ||||
| 
 | ||||
| 			setBaudRate(DEFAULT_BAUD_RATE); | ||||
| @ -283,13 +283,13 @@ public class Ch34xSerialDriver implements UsbSerialDriver { | ||||
| 			checkState("init #4", 0x95, 0x2518, new int[]{-1 /* 0x56, c3*/, 0x00}); | ||||
| 
 | ||||
| 			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}); | ||||
| 
 | ||||
| 			if (controlOut(0xa1, 0x501f, 0xd90a) < 0) { | ||||
| 				throw new IOException("init failed! #7"); | ||||
| 				throw new IOException("Init failed: #7"); | ||||
| 			} | ||||
| 
 | ||||
| 			setBaudRate(DEFAULT_BAUD_RATE); | ||||
| @ -313,25 +313,27 @@ public class Ch34xSerialDriver implements UsbSerialDriver { | ||||
| 					} | ||||
| 
 | ||||
| 			if (factor > 0xfff0) { | ||||
| 				throw new IOException("Baudrate " + baudRate + " not supported"); | ||||
| 				throw new UnsupportedOperationException("Unsupported baud rate: " + baudRate); | ||||
| 			} | ||||
| 
 | ||||
| 			factor = 0x10000 - factor; | ||||
| 
 | ||||
| 			int ret = controlOut(0x9a, 0x1312, (int) ((factor & 0xff00) | divisor)); | ||||
| 			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)); | ||||
| 			if (ret < 0) { | ||||
| 				throw new IOException("Error setting baud rate. #2"); | ||||
| 				throw new IOException("Error setting baud rate: #2"); | ||||
| 			} | ||||
| 		} | ||||
| 
 | ||||
| 		@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); | ||||
| 			} | ||||
| 			setBaudRate(baudRate); | ||||
| 
 | ||||
| 			int lcr = LCR_ENABLE_RX | LCR_ENABLE_TX; | ||||
| @ -350,7 +352,7 @@ public class Ch34xSerialDriver implements UsbSerialDriver { | ||||
| 					lcr |= LCR_CS8; | ||||
| 					break; | ||||
| 				default: | ||||
| 					throw new IllegalArgumentException("Unknown dataBits value: " + dataBits); | ||||
| 					throw new IllegalArgumentException("Invalid data bits: " + dataBits); | ||||
| 			} | ||||
| 
 | ||||
| 			switch (parity) { | ||||
| @ -369,19 +371,19 @@ public class Ch34xSerialDriver implements UsbSerialDriver { | ||||
| 					lcr |= LCR_ENABLE_PAR | LCR_MARK_SPACE | LCR_PAR_EVEN; | ||||
| 					break; | ||||
| 				default: | ||||
| 					throw new IllegalArgumentException("Unknown parity value: " + parity); | ||||
| 					throw new IllegalArgumentException("Invalid parity: " + parity); | ||||
| 			} | ||||
| 
 | ||||
| 			switch (stopBits) { | ||||
| 				case STOPBITS_1: | ||||
| 					break; | ||||
| 				case STOPBITS_1_5: | ||||
| 					throw new IllegalArgumentException("Unsupported stopBits value: 1.5"); | ||||
| 					throw new UnsupportedOperationException("Unsupported stop bits: 1.5"); | ||||
| 				case STOPBITS_2: | ||||
| 					lcr |= LCR_STOP_BITS_2; | ||||
| 					break; | ||||
| 				default: | ||||
| 					throw new IllegalArgumentException("Unknown stopBits value: " + stopBits); | ||||
| 					throw new IllegalArgumentException("Invalid stop bits: " + stopBits); | ||||
| 			} | ||||
| 
 | ||||
| 			int ret = controlOut(0x9a, 0x2518, lcr); | ||||
|  | ||||
| @ -132,8 +132,7 @@ abstract class CommonUsbSerialPort implements UsbSerialPort { | ||||
|     public abstract int write(final byte[] src, final int timeoutMillis) throws IOException; | ||||
| 
 | ||||
|     @Override | ||||
|     public abstract void setParameters( | ||||
|             int baudRate, int dataBits, int stopBits, int parity) throws IOException; | ||||
|     public abstract void setParameters(int baudRate, int dataBits, int stopBits, int parity) throws IOException; | ||||
| 
 | ||||
|     @Override | ||||
|     public abstract boolean getCD() throws IOException; | ||||
|  | ||||
| @ -135,7 +135,7 @@ public class Cp21xxSerialDriver implements UsbSerialDriver { | ||||
|         @Override | ||||
|         public void open(UsbDeviceConnection connection) throws IOException { | ||||
|             if (mConnection != null) { | ||||
|                 throw new IOException("Already opened."); | ||||
|                 throw new IOException("Already open"); | ||||
|             } | ||||
| 
 | ||||
|             mConnection = connection; | ||||
| @ -205,7 +205,7 @@ public class Cp21xxSerialDriver implements UsbSerialDriver { | ||||
|                 request.initialize(mConnection, mReadEndpoint); | ||||
|                 final ByteBuffer buf = ByteBuffer.wrap(dest); | ||||
|                 if (!request.queue(buf, dest.length)) { | ||||
|                     throw new IOException("Error queueing request."); | ||||
|                     throw new IOException("Error queueing request"); | ||||
|                 } | ||||
|                 mUsbRequest = request; | ||||
|                 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, | ||||
|                     0, mPortNumber, data, 4, USB_WRITE_TIMEOUT_MILLIS); | ||||
|             if (ret < 0) { | ||||
|                 throw new IOException("Error setting baud rate."); | ||||
|                 throw new IOException("Error setting baud rate"); | ||||
|             } | ||||
|         } | ||||
| 
 | ||||
|         @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); | ||||
|             } | ||||
|             setBaudRate(baudRate); | ||||
| 
 | ||||
|             int configDataBits = 0; | ||||
|             switch (dataBits) { | ||||
|                 case DATABITS_5: | ||||
|                     if(mIsRestrictedPort) | ||||
|                         throw new IllegalArgumentException("Unsupported dataBits value: " + dataBits); | ||||
|                         throw new UnsupportedOperationException("Unsupported data bits: " + dataBits); | ||||
|                     configDataBits |= 0x0500; | ||||
|                     break; | ||||
|                 case DATABITS_6: | ||||
|                     if(mIsRestrictedPort) | ||||
|                         throw new IllegalArgumentException("Unsupported dataBits value: " + dataBits); | ||||
|                         throw new UnsupportedOperationException("Unsupported data bits: " + dataBits); | ||||
|                     configDataBits |= 0x0600; | ||||
|                     break; | ||||
|                 case DATABITS_7: | ||||
|                     if(mIsRestrictedPort) | ||||
|                         throw new IllegalArgumentException("Unsupported dataBits value: " + dataBits); | ||||
|                         throw new UnsupportedOperationException("Unsupported data bits: " + dataBits); | ||||
|                     configDataBits |= 0x0700; | ||||
|                     break; | ||||
|                 case DATABITS_8: | ||||
|                     configDataBits |= 0x0800; | ||||
|                     break; | ||||
|                 default: | ||||
|                     throw new IllegalArgumentException("Unknown dataBits value: " + dataBits); | ||||
|                     throw new IllegalArgumentException("Invalid data bits: " + dataBits); | ||||
|             } | ||||
|              | ||||
|             switch (parity) { | ||||
| @ -320,30 +322,30 @@ public class Cp21xxSerialDriver implements UsbSerialDriver { | ||||
|                     break; | ||||
|                 case PARITY_MARK: | ||||
|                     if(mIsRestrictedPort) | ||||
|                         throw new IllegalArgumentException("Unsupported parity value: mark"); | ||||
|                         throw new UnsupportedOperationException("Unsupported parity: mark"); | ||||
|                     configDataBits |= 0x0030; | ||||
|                     break; | ||||
|                 case PARITY_SPACE: | ||||
|                     if(mIsRestrictedPort) | ||||
|                         throw new IllegalArgumentException("Unsupported parity value: space"); | ||||
|                         throw new UnsupportedOperationException("Unsupported parity: space"); | ||||
|                     configDataBits |= 0x0040; | ||||
|                     break; | ||||
|                 default: | ||||
|                     throw new IllegalArgumentException("Unknown parity value: " + parity); | ||||
|                     throw new IllegalArgumentException("Invalid parity: " + parity); | ||||
|             } | ||||
|              | ||||
|             switch (stopBits) { | ||||
|                 case STOPBITS_1: | ||||
|                     break; | ||||
|                 case STOPBITS_1_5: | ||||
|                     throw new IllegalArgumentException("Unsupported stopBits value: 1.5"); | ||||
|                     throw new UnsupportedOperationException("Unsupported stop bits: 1.5"); | ||||
|                 case STOPBITS_2: | ||||
|                     if(mIsRestrictedPort) | ||||
|                         throw new IllegalArgumentException("Unsupported stopBits value: 2"); | ||||
|                         throw new UnsupportedOperationException("Unsupported stop bits: 2"); | ||||
|                     configDataBits |= 2; | ||||
|                     break; | ||||
|                 default: | ||||
|                     throw new IllegalArgumentException("Unknown stopBits value: " + stopBits); | ||||
|                     throw new IllegalArgumentException("Invalid stop bits: " + stopBits); | ||||
|             } | ||||
|             setConfigSingle(SILABSER_SET_LINE_CTL_REQUEST_CODE, configDataBits); | ||||
|         } | ||||
|  | ||||
| @ -287,7 +287,7 @@ public class FtdiSerialDriver implements UsbSerialDriver { | ||||
|             try { | ||||
|                 request.initialize(mConnection, endpoint); | ||||
|                 if (!request.queue(buf, dest.length)) { | ||||
|                     throw new IOException("Error queueing request."); | ||||
|                     throw new IOException("Error queueing request"); | ||||
|                 } | ||||
| 
 | ||||
|                 final UsbRequest response = mConnection.requestWait(); | ||||
| @ -360,21 +360,23 @@ public class FtdiSerialDriver implements UsbSerialDriver { | ||||
|         } | ||||
| 
 | ||||
|         @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); | ||||
|             } | ||||
|             setBaudRate(baudRate); | ||||
| 
 | ||||
|             int config = 0; | ||||
|             switch (dataBits) { | ||||
|                 case DATABITS_5: | ||||
|                 case DATABITS_6: | ||||
|                     throw new IllegalArgumentException("Unsupported dataBits value: " + dataBits); | ||||
|                     throw new UnsupportedOperationException("Unsupported data bits: " + dataBits); | ||||
|                 case DATABITS_7: | ||||
|                 case DATABITS_8: | ||||
|                     config |= dataBits; | ||||
|                     break; | ||||
|                 default: | ||||
|                     throw new IllegalArgumentException("Unknown dataBits value: " + dataBits); | ||||
|                     throw new IllegalArgumentException("Invalid data bits: " + dataBits); | ||||
|             } | ||||
| 
 | ||||
|             switch (parity) { | ||||
| @ -394,7 +396,7 @@ public class FtdiSerialDriver implements UsbSerialDriver { | ||||
|                     config |= (0x04 << 8); | ||||
|                     break; | ||||
|                 default: | ||||
|                     throw new IllegalArgumentException("Unknown parity value: " + parity); | ||||
|                     throw new IllegalArgumentException("Invalid parity: " + parity); | ||||
|             } | ||||
| 
 | ||||
|             switch (stopBits) { | ||||
| @ -402,12 +404,12 @@ public class FtdiSerialDriver implements UsbSerialDriver { | ||||
|                     config |= (0x00 << 11); | ||||
|                     break; | ||||
|                 case STOPBITS_1_5: | ||||
|                     throw new IllegalArgumentException("Unsupported stopBits value: 1.5"); | ||||
|                     throw new UnsupportedOperationException("Unsupported stop bits: 1.5"); | ||||
|                 case STOPBITS_2: | ||||
|                     config |= (0x02 << 11); | ||||
|                     break; | ||||
|                 default: | ||||
|                     throw new IllegalArgumentException("Unknown stopBits value: " + stopBits); | ||||
|                     throw new IllegalArgumentException("Invalid stop bits: " + stopBits); | ||||
|             } | ||||
| 
 | ||||
|             int result = mConnection.controlTransfer(FTDI_DEVICE_OUT_REQTYPE, | ||||
|  | ||||
| @ -380,7 +380,7 @@ public class ProlificSerialDriver implements UsbSerialDriver { | ||||
|                 request.initialize(mConnection, mReadEndpoint); | ||||
|                 final ByteBuffer buf = ByteBuffer.wrap(dest); | ||||
|                 if (!request.queue(buf, dest.length)) { | ||||
|                     throw new IOException("Error queueing request."); | ||||
|                     throw new IOException("Error queueing request"); | ||||
|                 } | ||||
| 
 | ||||
|                 final UsbRequest response = mConnection.requestWait(); | ||||
| @ -439,8 +439,7 @@ public class ProlificSerialDriver implements UsbSerialDriver { | ||||
|         } | ||||
| 
 | ||||
|         @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 ((mBaudRate == baudRate) && (mDataBits == dataBits) | ||||
|                     && (mStopBits == stopBits) && (mParity == parity)) { | ||||
|                 // 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]; | ||||
| 
 | ||||
|             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); | ||||
| @ -458,44 +460,39 @@ public class ProlificSerialDriver implements UsbSerialDriver { | ||||
|             case STOPBITS_1: | ||||
|                 lineRequestData[4] = 0; | ||||
|                 break; | ||||
| 
 | ||||
|             case STOPBITS_1_5: | ||||
|                 lineRequestData[4] = 1; | ||||
|                 break; | ||||
| 
 | ||||
|             case STOPBITS_2: | ||||
|                 lineRequestData[4] = 2; | ||||
|                 break; | ||||
| 
 | ||||
|             default: | ||||
|                 throw new IllegalArgumentException("Unknown stopBits value: " + stopBits); | ||||
|                 throw new IllegalArgumentException("Invalid stop bits: " + stopBits); | ||||
|             } | ||||
| 
 | ||||
|             switch (parity) { | ||||
|             case PARITY_NONE: | ||||
|                 lineRequestData[5] = 0; | ||||
|                 break; | ||||
| 
 | ||||
|             case PARITY_ODD: | ||||
|                 lineRequestData[5] = 1; | ||||
|                 break; | ||||
|              | ||||
|             case PARITY_EVEN: | ||||
|                 lineRequestData[5] = 2; | ||||
|                 break; | ||||
| 
 | ||||
|             case PARITY_MARK: | ||||
|                 lineRequestData[5] = 3; | ||||
|                 break; | ||||
| 
 | ||||
|             case PARITY_SPACE: | ||||
|                 lineRequestData[5] = 4; | ||||
|                 break; | ||||
| 
 | ||||
|             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; | ||||
| 
 | ||||
|             ctrlOut(SET_LINE_REQUEST, 0, 0, lineRequestData); | ||||
|  | ||||
| @ -76,7 +76,7 @@ public final class UsbId { | ||||
|     public static final int ARM_MBED = 0x0204; | ||||
| 
 | ||||
|     private UsbId() { | ||||
|         throw new IllegalAccessError("Non-instantiable class."); | ||||
|         throw new IllegalAccessError("Non-instantiable class"); | ||||
|     } | ||||
| 
 | ||||
| } | ||||
|  | ||||
| @ -145,9 +145,9 @@ public interface UsbSerialPort { | ||||
|      *            {@link #PARITY_EVEN}, {@link #PARITY_MARK}, or | ||||
|      *            {@link #PARITY_SPACE}. | ||||
|      * @throws IOException on error setting the port parameters | ||||
|      * @throws UnsupportedOperationException if not supported by a specific device | ||||
|      */ | ||||
|     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; | ||||
| 
 | ||||
|     /** | ||||
|      * Gets the CD (Carrier Detect) bit from the underlying UART. | ||||
|  | ||||
| @ -125,12 +125,12 @@ public class SerialInputOutputManager implements Runnable { | ||||
|     public void run() { | ||||
|         synchronized (this) { | ||||
|             if (getState() != State.STOPPED) { | ||||
|                 throw new IllegalStateException("Already running."); | ||||
|                 throw new IllegalStateException("Already running"); | ||||
|             } | ||||
|             mState = State.RUNNING; | ||||
|         } | ||||
| 
 | ||||
|         Log.i(TAG, "Running .."); | ||||
|         Log.i(TAG, "Running ..."); | ||||
|         try { | ||||
|             while (true) { | ||||
|                 if (getState() != State.RUNNING) { | ||||
| @ -148,7 +148,7 @@ public class SerialInputOutputManager implements Runnable { | ||||
|         } finally { | ||||
|             synchronized (this) { | ||||
|                 mState = State.STOPPED; | ||||
|                 Log.i(TAG, "Stopped."); | ||||
|                 Log.i(TAG, "Stopped"); | ||||
|             } | ||||
|         } | ||||
|     } | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user