changeset 173:dfb0a1f399c7

8081509: [UART] Implement setters parameter verifcation Summary: UARTConfig.Builder is reused as data checker Reviewed-by: alkonsta
author snazarki
date Fri, 29 May 2015 18:01:24 +0300
parents 416d7f008dd6
children feda4d56c9a5
files src/share/classes/com/oracle/dio/uart/impl/UARTImpl.java src/share/classes/jdk/dio/uart/UARTConfig.java src/share/native/com/oracle/dio/javacall_uart.h
diffstat 3 files changed, 43 insertions(+), 32 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/com/oracle/dio/uart/impl/UARTImpl.java	Thu May 28 12:52:58 2015 +0300
+++ b/src/share/classes/com/oracle/dio/uart/impl/UARTImpl.java	Fri May 29 18:01:24 2015 +0300
@@ -44,20 +44,21 @@
 
 import jdk.dio.*;
 import jdk.dio.uart.*;
+import jdk.dio.uart.UARTConfig.Builder;
 
 import romizer.*;
 
 class UARTImpl extends PowerManagedBase<UART> implements UART {
     private boolean isWriting;
 
-    private Object synchReadLock = new Object();
-    private Object synchWriteLock = new Object();
+    private final Object synchReadLock = new Object();
+    private final Object synchWriteLock = new Object();
 
-    private ByteBuffer writeBuffers[] = new ByteBuffer[2];
-    private int writeBuffersPositions[] = new int[2];
+    private final ByteBuffer writeBuffers[] = new ByteBuffer[2];
+    private final int writeBuffersPositions[] = new int[2];
 
-    private ByteBuffer readBuffers[] = new ByteBuffer[2];
-    private int readBuffersPositions[] = new int[2];
+    private final ByteBuffer readBuffers[] = new ByteBuffer[2];
+    private final int readBuffersPositions[] = new int[2];
 
     private int readBufferIdx = 0;
     private int writeBufferIdx = 0;
@@ -376,7 +377,7 @@
     public synchronized void setBaudRate(int baudRate) throws IOException, UnavailableDeviceException, ClosedDeviceException{
         checkPowerState();
         if(baudRate <= 0){
-            throw new java.lang.UnsupportedOperationException();
+            throw new IllegalArgumentException();
         }
         setBaudRate0( baudRate);
     }
@@ -445,25 +446,8 @@
 
     @Override
     public synchronized void setFlowControlMode(int flowcontrol) throws IOException, UnavailableDeviceException, ClosedDeviceException {
-
         checkPowerState();
-
-        //check if flowcontrol is bitwise of supported values
-        if(0!=(flowcontrol & (~(UARTConfig.FLOWCONTROL_NONE | UARTConfig.FLOWCONTROL_RTSCTS_IN |
-                           UARTConfig.FLOWCONTROL_RTSCTS_OUT | UARTConfig.FLOWCONTROL_XONXOFF_IN |
-                           UARTConfig.FLOWCONTROL_XONXOFF_OUT)))){
-            throw new IllegalArgumentException();
-        }
-
-        //check if 1 flaf for input is set
-        if(0!=(flowcontrol&UARTConfig.FLOWCONTROL_RTSCTS_IN) && 0!=(flowcontrol&UARTConfig.FLOWCONTROL_XONXOFF_IN)){
-            throw new IllegalArgumentException();
-        }
-        //check if 1 flaf for output is set
-        if(0!=(flowcontrol&UARTConfig.FLOWCONTROL_RTSCTS_OUT) && 0!=(flowcontrol&UARTConfig.FLOWCONTROL_XONXOFF_OUT)){
-            throw new IllegalArgumentException();
-        }
-
+        new UARTConfig.Builder().setFlowControlMode(flowcontrol);
         setFlowControlMode0(flowcontrol);
     }
 
@@ -473,6 +457,7 @@
     @Override
     public synchronized void setParity(int parity) throws IOException, UnavailableDeviceException, ClosedDeviceException{
         checkPowerState();
+        new UARTConfig.Builder().setParity(parity);
         setParity0(parity);
     }
 
@@ -483,6 +468,7 @@
     @Override
     public synchronized void setStopBits(int stopBits) throws IOException, UnavailableDeviceException, ClosedDeviceException{
         checkPowerState();
+        new UARTConfig.Builder().setStopBits(stopBits);
         setStopBits0(stopBits);
     }
 
@@ -774,9 +760,7 @@
     public synchronized void setReceiveTimeout(int timeout) throws IOException, UnavailableDeviceException, ClosedDeviceException {
         checkPowerState();
         if(timeout < 0 ){
-            throw new UnsupportedOperationException(
-                    ExceptionMessage.format(ExceptionMessage.UART_NEGATIVE_TIMEOUT)
-            );
+            throw new IllegalArgumentException(ExceptionMessage.format(ExceptionMessage.UART_NEGATIVE_TIMEOUT));
         }
         inputTimeout = timeout;
     }
--- a/src/share/classes/jdk/dio/uart/UARTConfig.java	Thu May 28 12:52:58 2015 +0300
+++ b/src/share/classes/jdk/dio/uart/UARTConfig.java	Fri May 29 18:01:24 2015 +0300
@@ -195,7 +195,7 @@
          */
         public UARTConfig build() {
             instance.checkParameters();
-            return instance;
+            return (UARTConfig)Platform.clone(instance);
         }
 
         /**
@@ -207,6 +207,7 @@
          */
         public Builder setControllerName(String controllerName) {
             instance.controllerName = controllerName;
+            instance.checkParameters();
             return this;
         }
 
@@ -222,6 +223,7 @@
          */
         public Builder setChannelNumber(int channelNumber) {
             instance.channelNumber = channelNumber;
+            instance.checkParameters();
             return this;
         }
 
@@ -237,6 +239,7 @@
          */
         public Builder setControllerNumber(int controllerNumber) {
             instance.controllerNumber = controllerNumber;
+            instance.checkParameters();
             return this;
         }
 
@@ -250,6 +253,7 @@
          */
         public Builder setBaudRate(int baudRate) {
             instance.baudRate = baudRate;
+            instance.checkParameters();
             return this;
         }
 
@@ -265,6 +269,7 @@
          */
         public Builder setDataBits(int dataBits) {
             instance.dataBits = dataBits;
+            instance.checkParameters();
             return this;
         }
 
@@ -282,6 +287,7 @@
          */
         public Builder setFlowControlMode(int flowcontrol) {
             instance.flowcontrol = flowcontrol;
+            instance.checkParameters();
             return this;
         }
 
@@ -298,6 +304,7 @@
          */
         public Builder setInputBufferSize(int inputBufferSize) {
             instance.inputBufferSize = inputBufferSize;
+            instance.checkParameters();
             return this;
         }
 
@@ -314,6 +321,7 @@
          */
         public Builder setOutputBufferSize(int outputBufferSize) {
             instance.outputBufferSize = outputBufferSize;
+            instance.checkParameters();
             return this;
         }
 
@@ -328,6 +336,7 @@
          */
         public Builder setParity(int parity) {
             instance.parity = parity;
+            instance.checkParameters();
             return this;
         }
 
@@ -343,6 +352,7 @@
          */
         public Builder setStopBits(int stopBits) {
             instance.stopBits = stopBits;
+            instance.checkParameters();
             return this;
         }
     }
@@ -558,15 +568,20 @@
     }
 
     private void checkParameters(){
-
         if ((null == controllerName && controllerNumber < DEFAULT) ||
             channelNumber < DEFAULT ||
             baudRate <= 0 ||
             (dataBits < DATABITS_5 || dataBits > DATABITS_9) ||
             (parity < PARITY_NONE || parity > PARITY_SPACE) ||
             (stopBits < STOPBITS_1 || stopBits > STOPBITS_2) ||
-            (flowcontrol!=FLOWCONTROL_NONE && flowcontrol!=FLOWCONTROL_RTSCTS_IN &&
-             flowcontrol!=FLOWCONTROL_RTSCTS_OUT && flowcontrol!=FLOWCONTROL_XONXOFF_IN && flowcontrol!=FLOWCONTROL_XONXOFF_OUT) ||
+            //check if flowcontrol is bitwise of supported values
+            (0 != (flowcontrol & ~(UARTConfig.FLOWCONTROL_NONE | UARTConfig.FLOWCONTROL_RTSCTS_IN |
+                               UARTConfig.FLOWCONTROL_RTSCTS_OUT | UARTConfig.FLOWCONTROL_XONXOFF_IN |
+                               UARTConfig.FLOWCONTROL_XONXOFF_OUT))) ||
+            //check if 1 flaf for input is set
+            (0!=(flowcontrol&UARTConfig.FLOWCONTROL_RTSCTS_IN) && 0!=(flowcontrol&UARTConfig.FLOWCONTROL_XONXOFF_IN)) ||
+            //check if 1 flaf for output is set
+            (0!=(flowcontrol&UARTConfig.FLOWCONTROL_RTSCTS_OUT) && 0!=(flowcontrol&UARTConfig.FLOWCONTROL_XONXOFF_OUT)) ||
             inputBufferSize  < DEFAULT ||
             outputBufferSize < DEFAULT ) {
            throw new IllegalArgumentException();
--- a/src/share/native/com/oracle/dio/javacall_uart.h	Thu May 28 12:52:58 2015 +0300
+++ b/src/share/native/com/oracle/dio/javacall_uart.h	Fri May 29 18:01:24 2015 +0300
@@ -278,6 +278,9 @@
 /**
  * Update the stopBits of an open uart port
  *
+ * @note It is guaranteed that <code>stopBits</code> value is
+ *       valid in terms of specification requirements.
+ *
  * @param hPort the port to configure
  * @param stopBits new stopBits for the open connection
  * @retval JAVACALL_DIO_OK    on success,
@@ -302,6 +305,9 @@
 /**
  * Update the parity of an open uart port
  *
+ * @note It is guaranteed that <code>parity</code> value is
+ *       valid in terms of specification requirements.
+ *
  * @param hPort the port to configure
  * @param parity new parity for the open connection
  * @retval JAVACALL_DIO_OK on success,
@@ -326,6 +332,9 @@
 /**
  * Update the bitsPerChar of an open uart port
  *
+ * @note It is guaranteed that <code>bitsPerChar</code> value is
+ *       valid in terms of specification requirements.
+ *
  * @param hPort the port to configure
  * @param bitsPerChar new bits per char for the open connection
  * @retval JAVACALL_DIO_OK on success,
@@ -350,6 +359,9 @@
 /**
  * Update the flowcontrol mode of an open uart port
  *
+ * @note It is guaranteed that <code>mode</code> value is valid
+ *       in terms of specification requirements.
+ *
  * @param hPort the port to configure
  * @param mode new flowcontrol mode (bitwise) for the open connection
  * @retval JAVACALL_DIO_OK    on success,