changeset 60:5d629ccf074a

8066263: Split UARTimpl and ModemUARTImpl implementation in DIO realization Summary: Making ModemUART implementation optional Reviewed-by: snazarki Contributed-by: alexey.mironov@oracle.com
author snazarki
date Thu, 04 Dec 2014 14:49:18 +0400
parents a5e2cee209be
children c69cb4805050
files src/se/native/com/oracle/dio/uart/impl/jni_modem.cpp src/se/native/com/oracle/dio/uart/impl/jni_uart.cpp src/share/classes/com/oracle/dio/uart/impl/ModemUARTFactory.java src/share/classes/com/oracle/dio/uart/impl/ModemUARTImpl.java src/share/classes/com/oracle/dio/uart/impl/UARTImpl.java
diffstat 5 files changed, 240 insertions(+), 137 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/se/native/com/oracle/dio/uart/impl/jni_modem.cpp	Thu Dec 04 14:49:18 2014 +0400
@@ -0,0 +1,60 @@
+/*
+ * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+#include <dio_common.h>
+#include <dio_exceptions.h>
+#include <dio_nio.h>
+#include <javacall_serial.h>
+#include <javacall_uart.h>
+#include <javacall_memory.h>
+#include <dio_event_queue.h>
+
+/*
+ * Class:     com_oracle_dio_uart_impl_ModemUARTImpl
+ * Method:    getDCESignalState0
+ */
+JNIEXPORT jboolean JNICALL Java_com_oracle_dio_uart_impl_ModemUARTImpl_getDCESignalState0
+  (JNIEnv* env, jobject obj, jint signal) {
+    device_reference device = getDeviceReferenceFromDeviceObject(env, obj);
+    javacall_bool state = JAVACALL_FALSE;
+    javacall_result result;
+    result = javacall_serial_get_dce_signal(getDeviceHandle(device),
+                                            (javacall_serial_signal_type)signal,
+                                            &state);
+    return (state != JAVACALL_FALSE ? JNI_TRUE : JNI_FALSE);
+}
+
+/*
+ * Class:     com_oracle_dio_uart_impl_ModemUARTImpl
+ * Method:    setDTESignalState0
+ */
+JNIEXPORT void JNICALL Java_com_oracle_dio_uart_impl_ModemUARTImpl_setDTESignalState0
+  (JNIEnv* env, jobject obj, jint signal, jboolean state) {
+    device_reference device = getDeviceReferenceFromDeviceObject(env, obj);
+    javacall_result result;
+    result = javacall_serial_set_dte_signal(getDeviceHandle(device),
+                                            (javacall_serial_signal_type)signal,
+                                            (state != JNI_FALSE ? JAVACALL_TRUE : JAVACALL_FALSE));
+}
--- a/src/se/native/com/oracle/dio/uart/impl/jni_uart.cpp	Wed Dec 03 22:10:07 2014 +0400
+++ b/src/se/native/com/oracle/dio/uart/impl/jni_uart.cpp	Thu Dec 04 14:49:18 2014 +0400
@@ -128,34 +128,6 @@
 
 /*
  * Class:     com_oracle_dio_uart_impl_UARTImpl
- * Method:    setDTESignalState0
- */
-JNIEXPORT void JNICALL Java_com_oracle_dio_uart_impl_UARTImpl_setDTESignalState0
-  (JNIEnv* env, jobject obj, jint signal, jboolean state) {
-    device_reference device = getDeviceReferenceFromDeviceObject(env, obj);
-    javacall_result result;
-    result = javacall_serial_set_dte_signal(getDeviceHandle(device),
-                                            (javacall_serial_signal_type)signal,
-                                            (state != JNI_FALSE ? JAVACALL_TRUE : JAVACALL_FALSE));
-}
-
-/*
- * Class:     com_oracle_dio_uart_impl_UARTImpl
- * Method:    getDCESignalState0
- */
-JNIEXPORT jboolean JNICALL Java_com_oracle_dio_uart_impl_UARTImpl_getDCESignalState0
-  (JNIEnv* env, jobject obj, jint signal) {
-    device_reference device = getDeviceReferenceFromDeviceObject(env, obj);
-    javacall_bool state = JAVACALL_FALSE;
-    javacall_result result;
-    result = javacall_serial_get_dce_signal(getDeviceHandle(device),
-                                            (javacall_serial_signal_type)signal,
-                                            &state);
-    return (state != JAVACALL_FALSE ? JNI_TRUE : JNI_FALSE);
-}
-
-/*
- * Class:     com_oracle_dio_uart_impl_UARTImpl
  * Method:    removeEventListener0
  */
 JNIEXPORT void JNICALL Java_com_oracle_dio_uart_impl_UARTImpl_removeEventListener0
--- a/src/share/classes/com/oracle/dio/uart/impl/ModemUARTFactory.java	Wed Dec 03 22:10:07 2014 +0400
+++ b/src/share/classes/com/oracle/dio/uart/impl/ModemUARTFactory.java	Thu Dec 04 14:49:18 2014 +0400
@@ -51,6 +51,6 @@
         */
         DeviceDescriptor<UART> dscr1 = new PeripheralDescriptorImpl(dscr.getID(),
             dscr.getName(), dscr.getConfiguration(), ModemUART.class, null);
-        return new UARTImpl(dscr1, mode);
+        return new ModemUARTImpl(dscr1, mode);
     }
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/share/classes/com/oracle/dio/uart/impl/ModemUARTImpl.java	Thu Dec 04 14:49:18 2014 +0400
@@ -0,0 +1,151 @@
+/*
+ * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+package com.oracle.dio.uart.impl;
+import java.io.IOException;
+
+import com.oracle.dio.utils.ExceptionMessage;
+
+import jdk.dio.ClosedDeviceException;
+import jdk.dio.DeviceDescriptor;
+import jdk.dio.DeviceNotFoundException;
+import jdk.dio.InvalidDeviceConfigException;
+import jdk.dio.UnavailableDeviceException;
+import jdk.dio.UnsupportedAccessModeException;
+import jdk.dio.modem.ModemSignalEvent;
+import jdk.dio.modem.ModemSignalListener;
+import jdk.dio.modem.ModemSignalsControl;
+import jdk.dio.uart.ModemUART;
+import jdk.dio.uart.UART;
+
+
+class ModemUARTImpl extends UARTImpl implements ModemUART, ModemSignalDispatcher.SerialSignalListener {
+
+    private ModemSignalListener modemListener;
+
+    ModemUARTImpl(DeviceDescriptor<UART> dscr, int mode)
+                                throws DeviceNotFoundException, InvalidDeviceConfigException, UnsupportedAccessModeException{
+        super(dscr, mode);
+    }
+
+
+    /**
+     * Sets or clears the designated signal.
+     */
+    public synchronized void setSignalState(int signalID, boolean state) throws IOException,
+                                UnavailableDeviceException, ClosedDeviceException{
+
+        if (signalID != ModemSignalsControl.DTR_SIGNAL && signalID != ModemSignalsControl.RTS_SIGNAL) {
+
+            throw new IllegalArgumentException(
+                    ExceptionMessage.format(ExceptionMessage.UART_UNKNOWN_SIGNAL_ID)
+            );
+        }
+
+        checkPowerState();
+        setDTESignalState0( signalID, state);
+    }
+    /**
+     * Gets the state of the designated signal.
+     *
+     */
+    public synchronized boolean getSignalState(int signalID) throws IOException,
+                                UnavailableDeviceException, ClosedDeviceException{
+
+        if (signalID != ModemSignalsControl.CTS_SIGNAL && signalID != ModemSignalsControl.DCD_SIGNAL &&
+            signalID != ModemSignalsControl.DSR_SIGNAL && signalID != ModemSignalsControl.RI_SIGNAL) {
+
+            throw new IllegalArgumentException(
+                ExceptionMessage.format(ExceptionMessage.UART_UNKNOWN_SIGNAL_ID)
+            );
+        }
+        checkPowerState();
+        return getDCESignalState0( signalID);
+    };
+
+    /**
+     * Registers a {@link ModemSignalListener} instance which will get asynchronously notified when one of the
+     * designated signals changes. Notification will automatically begin after registration completes.
+     */
+    public synchronized void setSignalChangeListener(ModemSignalListener<ModemUART> listener, int signals)
+            throws IOException, UnavailableDeviceException, ClosedDeviceException{
+
+        // valid IDs of signals to monitor: DCD_SIGNAL, DSR_SIGNAL, RI_SIGNAL or CTS_SIGNAL.
+        int tmpS = ~(ModemSignalsControl.CTS_SIGNAL | ModemSignalsControl.DCD_SIGNAL |
+                ModemSignalsControl.DSR_SIGNAL | ModemSignalsControl.RI_SIGNAL);
+
+        if ( ( signals & tmpS ) != 0 ){
+
+            throw new IllegalArgumentException(
+                ExceptionMessage.format(ExceptionMessage.UART_SIGNALS_NOT_BITWISE_COMBINATION)
+            );
+        }
+
+        checkPowerState();
+
+        if ( (listener != null) && (modemListener != null) ){
+            throw new IllegalStateException(
+                ExceptionMessage.format(ExceptionMessage.UART_LISTENER_ALREADY_REGISTERED)
+            );
+        }
+
+        if (listener != null) {
+            if (modemListener == null) {
+                ModemSignalDispatcher.getInstance().addListener(getHandle().getNativeHandle(), this);
+            }
+            modemListener = listener;
+        } else {
+            ModemSignalDispatcher.getInstance().removeListener(getHandle().getNativeHandle(), this);
+            modemListener = null;
+        }
+    }
+
+    public void signalChanged(int signalLine, boolean state) {
+        ModemSignalListener l = modemListener;
+        if (null != l) {
+            ModemSignalEvent sce = new ModemSignalEvent(this, signalLine, state);
+            try{
+                l.signalStateChanged(sce);
+            }catch(Exception e){
+                //do nothing
+            }
+        }
+    }
+
+    @Override
+    public synchronized void close() throws IOException{
+        if (isOpen()) {
+            if (modemListener != null){
+                ModemSignalDispatcher.getInstance().removeListener(getHandle().getNativeHandle(), this);
+                modemListener = null;
+            }
+            super.close();
+        }
+    }
+
+
+    private native void setDTESignalState0( int signalID, boolean state);
+    private native boolean getDCESignalState0( int signalID);
+};
--- a/src/share/classes/com/oracle/dio/uart/impl/UARTImpl.java	Wed Dec 03 22:10:07 2014 +0400
+++ b/src/share/classes/com/oracle/dio/uart/impl/UARTImpl.java	Thu Dec 04 14:49:18 2014 +0400
@@ -46,7 +46,6 @@
 import java.nio.ByteBuffer;
 
 import jdk.dio.uart.*;
-import jdk.dio.modem.*;
 
 import com.oracle.dio.power.impl.PowerManagedBase;
 import com.oracle.dio.utils.Constants;
@@ -64,8 +63,7 @@
 import java.security.AccessController;
 import jdk.dio.DevicePermission;
 
-class UARTImpl extends PowerManagedBase<UART> implements ModemUART,
-        ModemSignalDispatcher.SerialSignalListener {
+class UARTImpl extends PowerManagedBase<UART> implements UART {
     private boolean isWriting;
 
     private Object synchReadLock = new Object();
@@ -88,7 +86,6 @@
     private int inputTimeout;
     private Timer receiveTimer;
 
-    private ModemSignalListener modemListener;
     private InputRoundListener<UART, ByteBuffer> iRL;
 
     UARTImpl(DeviceDescriptor<UART> dscr, int mode)
@@ -104,6 +101,7 @@
 
         UARTConfig cfg = dscr.getConfiguration();
 
+        //deviceName = uart device prefix + device number
         deviceName = getSecurityName();
 
         if (deviceName == null){
@@ -209,9 +207,6 @@
             }
             // first port in list is DEFAULT port
             String ports = Configuration.getProperty("microedition.commports");
-            if (ports == null) {
-                ports = Configuration.getProperty("jdk.dio.uart.ports");
-            }
             if (null != ports) {
                 StringTokenizer t = new StringTokenizer(ports, ",");
                 while(devNum-- > 0 && t.hasMoreTokens()) {
@@ -262,20 +257,21 @@
                     return;
                 }
                 /*
-                 *  read0 is designed to copy available data from the javacall buffer to java buffer,
-                 *  because of that no slice() call is necessary, the following is necessary:
-                 *
-                 *  int bytesReaden = read0(buffer.slice());
-                 */
+                    read0 is designed to copy available data from the javacall buffer to java buffer,
+                    because of that no slice() call is necessary, the following is redundand:
+
+                    int bytesReaden = read0(buffer.slice());
+
+                */
                 int bytesRead = read0(buffer);
-                try {
+                try{
                     buffer.position(buffer.position() + bytesRead);
-                } catch (IllegalArgumentException e) {
+                }catch(IllegalArgumentException e){
                     //buffer.position() + bytesRead < 0 (not expected) or buffer.position() + bytesRead > limit
                     buffer.position(buffer.limit());
                 }
 
-                if(!buffer.hasRemaining() || ( receiveTriggerLevel !=0 && buffer.position() > receiveTriggerLevel) || (-1 == bytesProcessed)) {
+                if(!buffer.hasRemaining() || ( receiveTriggerLevel !=0 && (buffer.position() - readBuffersPositions[readBufferIdx]) >= receiveTriggerLevel) || (-1 == bytesProcessed)){
                     RoundCompletionEvent<UART,ByteBuffer> rcEvent =
                         new RoundCompletionEvent(this, buffer, buffer.position() - readBuffersPositions[readBufferIdx]);
 
@@ -331,9 +327,9 @@
                         writeBufferIdx = writeBufferIdx == 0 ? 1 : 0;
                         buffer = writeBuffers[writeBufferIdx];
                         //keep writing from the second buffer before user notice
-                        if (isWriting) {
-                            if (buffer.hasRemaining()) {
-                                /* since write0 needs to be called by the following manner:
+                        if(isWriting){
+                            if (buffer.hasRemaining()){
+                                 /* since write0 needs to be called by the following manner:
                                  * int bytesWritten = write0(buffer.slice());
                                  * buffer.position(buffer.position() + bytesWritten);
                                  *
@@ -539,7 +535,7 @@
      */
     @Override
     public synchronized void stopWriting() throws IOException, UnavailableDeviceException, ClosedDeviceException{
-        checkPowerState();
+        checkOpen();
         if (isWriting){
             unsubscribe(UARTEvent.OUTPUT_BUFFER_EMPTY);
 
@@ -558,6 +554,7 @@
     public synchronized void startReading(ByteBuffer src, InputRoundListener<UART, ByteBuffer> listener) throws IOException, UnavailableDeviceException, ClosedDeviceException{
         checkPowerState();
         checkRead();
+
         if(src == null || listener == null){
             throw new NullPointerException(
                 ExceptionMessage.format(ExceptionMessage.UART_NULL_SRC_OR_LISTENER)
@@ -582,6 +579,7 @@
     @Override
     public synchronized void startReading(ByteBuffer src1, ByteBuffer src2, InputRoundListener<UART, ByteBuffer> listener) throws IOException,
             UnavailableDeviceException, ClosedDeviceException{
+
         if(src1 == null || src2 == null || listener == null){
             throw new NullPointerException(
                 ExceptionMessage.format(ExceptionMessage.UART_NULL_SRC1_OR_SRC2_OR_LISTENER)
@@ -597,7 +595,7 @@
       */
     @Override
     public synchronized void stopReading() throws IOException, UnavailableDeviceException, ClosedDeviceException{
-        checkPowerState();
+        checkOpen();
         if (inRoundListener != null){
             unsubscribe(UARTEvent.INPUT_DATA_AVAILABLE);
             inRoundListener = null;
@@ -664,6 +662,7 @@
                 }
             }
             stopReading();
+
             ret = dst.position() - ret;
         }
         return ret==0?-1:ret ;
@@ -697,7 +696,7 @@
             ret = write0(src.slice());
             try{
                 src.position(src.position() + ret);
-            } catch (IllegalArgumentException e) {
+            }catch(IllegalArgumentException e){
                 //IAE happens if src.position() + ret < 0 (not expected) or src.position() + ret > limit
                 src.position(src.limit());
             }
@@ -710,10 +709,6 @@
     @Override
     public synchronized void close() throws IOException{
         if (isOpen()) {
-            if (modemListener != null){
-                ModemSignalDispatcher.getInstance().removeListener(getHandle().getNativeHandle(), this);
-                modemListener = null;
-            }
             synchronized(synchReadLock){
                 synchReadLock.notifyAll();
             }
@@ -757,86 +752,6 @@
         }
     }
 
-//ModemUART stuff
-    /**
-     * Sets or clears the designated signal.
-     */
-    public synchronized void setSignalState(int signalID, boolean state) throws IOException,
-                                UnavailableDeviceException, ClosedDeviceException{
-        checkPowerState();
-        setDTESignalState0( signalID, state);
-    }
-    /**
-     * Gets the state of the designated signal.
-     *
-     */
-    public synchronized boolean getSignalState(int signalID) throws IOException,
-                                UnavailableDeviceException, ClosedDeviceException{
-
-        if (signalID != ModemSignalsControl.CTS_SIGNAL && signalID != ModemSignalsControl.DCD_SIGNAL &&
-            signalID != ModemSignalsControl.DSR_SIGNAL && signalID != ModemSignalsControl.DTR_SIGNAL &&
-            signalID != ModemSignalsControl.RI_SIGNAL  && signalID != ModemSignalsControl.RTS_SIGNAL){
-
-            throw new IllegalArgumentException(
-                ExceptionMessage.format(ExceptionMessage.UART_UNKNOWN_SIGNAL_ID)
-            );
-        }
-        checkPowerState();
-        return getDCESignalState0( signalID);
-    };
-
-    /**
-     * Registers a {@link ModemSignalListener} instance which will get asynchronously notified when one of the
-     * designated signals changes. Notification will automatically begin after registration completes.
-     */
-    public synchronized void setSignalChangeListener(ModemSignalListener<ModemUART> listener, int signals)
-            throws IOException, UnavailableDeviceException, ClosedDeviceException{
-        checkPowerState();
-
-        int tmpS = ~(ModemSignalsControl.CTS_SIGNAL|ModemSignalsControl.DCD_SIGNAL|ModemSignalsControl.DSR_SIGNAL
-                  |ModemSignalsControl.DTR_SIGNAL|ModemSignalsControl.RI_SIGNAL|ModemSignalsControl.RTS_SIGNAL);
-
-        if ( ( signals & tmpS ) > 0 ){
-
-            throw new IllegalArgumentException(
-                ExceptionMessage.format(ExceptionMessage.UART_SIGNALS_NOT_BITWISE_COMBINATION)
-            );
-        }
-
-        if ( (listener != null) && (modemListener != null) ){
-            throw new IllegalStateException(
-                ExceptionMessage.format(ExceptionMessage.UART_LISTENER_ALREADY_REGISTERED)
-            );
-        }
-
-        if (listener != null) {
-            if (modemListener == null) {
-                ModemSignalDispatcher.getInstance().addListener(getHandle().getNativeHandle(), this);
-            }
-            modemListener = listener;
-        } else {
-            ModemSignalDispatcher.getInstance().removeListener(getHandle().getNativeHandle(), this);
-            modemListener = null;
-        }
-    }
-
-    public void signalChanged(int signalLine, boolean state) {
-        ModemSignalListener l = modemListener;
-        if (null != l) {
-            ModemSignalEvent sce = new ModemSignalEvent(this, signalLine, state);
-            try{
-                l.signalStateChanged(sce);
-            }catch(Exception e){
-                //do nothing
-            }
-        }
-    }
-//end ModemUART stuff
-
-//ModemUART native
-    private native void setDTESignalState0( int signalID, boolean state);
-    private native boolean getDCESignalState0( int signalID);
-//end ModemUART native
 
     protected synchronized int getGrpID() {
         return getUartId0();
@@ -856,8 +771,13 @@
     private native void setEventListener0(int eventId);
 
     private native void openUARTByConfig0(byte[] devName, int baudrate, int stopBits, int flowControl, int bitsPerChar, int parity, boolean exclusive);
-
+    /*
+     * write0 does not shift the buffer's position, it only returns bytes wrote, in case of asynch operation it must return 0.
+     */
     private native int write0(ByteBuffer src);
+    /*
+     * read0 does not shift the buffer's position, it only returns bytes read.
+     */
     private native int read0(ByteBuffer src);
 
     private native int getBaudRate0();
@@ -867,10 +787,10 @@
     private native void setDataBits0(int dataBits);
 
     private native int getParity0();
-    private native void setParity0(int parity);
+    private native int setParity0(int parity);
 
     private native int getStopBits0();
-    private native void setStopBits0(int stopBits);
+    private native int setStopBits0(int stopBits);
 
     private native void stopWriting0();
     private native void stopReading0();