changeset 138:26d04027287d

8074953: UART implement flowcontrol functions Summary: Setter/getter for flowcontrol mode Reviewed-by: snazarki Contributed-by: alexey.karaksin@oracle.com
author snazarki
date Tue, 14 Apr 2015 17:30:21 +0400
parents 1f20e05d1a7a
children c20b78e3144c
files src/se/native/com/oracle/dio/uart/impl/jni_uart.cpp src/share/classes/com/oracle/dio/uart/impl/UARTImpl.java src/share/linux/native/com/oracle/dio/uart/uart.c src/share/native/com/oracle/dio/javacall_serial.h src/share/native/com/oracle/dio/javacall_uart.h
diffstat 5 files changed, 189 insertions(+), 22 deletions(-) [+]
line wrap: on
line diff
--- a/src/se/native/com/oracle/dio/uart/impl/jni_uart.cpp	Fri Apr 10 18:34:14 2015 +0300
+++ b/src/se/native/com/oracle/dio/uart/impl/jni_uart.cpp	Tue Apr 14 17:30:21 2015 +0400
@@ -79,7 +79,7 @@
     javacall_dio_result result;
     result = javacall_uart_open_start(devName, baudRate,
                                      (javacall_uart_stop_bits)stopBits,
-                                     flowControl,
+                                     (javacall_serial_flowcontrol)flowControl,
                                      (javacall_uart_bits_per_char)bitsPerChar,
                                      (javacall_uart_parity)parity,
                                      (exclusive != JNI_FALSE ? JAVACALL_TRUE : JAVACALL_FALSE),
@@ -517,6 +517,32 @@
 
 /*
  * Class:     com_oracle_dio_uart_impl_UARTImpl
+ * Method:    getFlowControlMode0
+ */
+JNIEXPORT jint JNICALL Java_com_oracle_dio_uart_impl_UARTImpl_getFlowControlMode0
+  (JNIEnv* env, jobject obj) {
+    device_reference device = getDeviceReferenceFromDeviceObject(env, obj);
+    javacall_serial_flowcontrol flow_control;
+    javacall_dio_result result = javacall_uart_get_flowcontrol(getDeviceHandle(device), &flow_control);
+
+    checkJavacallFailure(env, result);
+    return (jint)flow_control;
+}
+
+/*
+ * Class:     com_oracle_dio_uart_impl_UARTImpl
+ * Method:    setFlowControlMode0
+ */
+JNIEXPORT void JNICALL Java_com_oracle_dio_uart_impl_UARTImpl_setFlowControlMode0
+  (JNIEnv* env, jobject obj, jint flow_control) {
+    device_reference device = getDeviceReferenceFromDeviceObject(env, obj);
+    javacall_dio_result result = javacall_uart_set_flowcontrol(getDeviceHandle(device),
+                                        (javacall_serial_flowcontrol)flow_control);
+    checkJavacallFailure(env, result);
+}
+
+/*
+ * Class:     com_oracle_dio_uart_impl_UARTImpl
  * Method:    stopWriting0
  */
 JNIEXPORT void JNICALL Java_com_oracle_dio_uart_impl_UARTImpl_stopWriting0
--- a/src/share/classes/com/oracle/dio/uart/impl/UARTImpl.java	Fri Apr 10 18:34:14 2015 +0300
+++ b/src/share/classes/com/oracle/dio/uart/impl/UARTImpl.java	Tue Apr 14 17:30:21 2015 +0400
@@ -341,8 +341,8 @@
     }
 
     @Override
-    public int getFlowControlMode() throws IOException, UnavailableDeviceException, ClosedDeviceException {
-        throw new UnsupportedOperationException();
+    public synchronized int getFlowControlMode() throws IOException, UnavailableDeviceException, ClosedDeviceException {
+        return getFlowControlMode0();
     }
 
     /**
@@ -441,8 +441,27 @@
     }
 
     @Override
-    public void setFlowControlMode(int flowcontrol) throws IOException, UnavailableDeviceException, ClosedDeviceException {
-        throw new UnsupportedOperationException();
+    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();
+        }
+
+        setFlowControlMode0(flowcontrol);
     }
 
     /**
@@ -908,4 +927,16 @@
         "com.oracle.dio.impl.AbstractPeripheral.handle",
     })
     private native int getUartId0();
+
+    @Local(DontRemoveFields = {
+        "com.oracle.dio.impl.Handle.native_handle",
+        "com.oracle.dio.impl.AbstractPeripheral.handle",
+    })
+    private native int setFlowControlMode0(int flowcontrol);
+
+    @Local(DontRemoveFields = {
+        "com.oracle.dio.impl.Handle.native_handle",
+        "com.oracle.dio.impl.AbstractPeripheral.handle",
+    })
+    private native int getFlowControlMode0();
 }
--- a/src/share/linux/native/com/oracle/dio/uart/uart.c	Fri Apr 10 18:34:14 2015 +0300
+++ b/src/share/linux/native/com/oracle/dio/uart/uart.c	Tue Apr 14 17:30:21 2015 +0400
@@ -450,9 +450,75 @@
     return JAVACALL_DIO_OK;
 }
 
+
+/**
+ * Update the flow control mode of an open serial port
+ */
+javacall_dio_result /*OPTIONAL*/
+javacall_uart_set_flowcontrol(javacall_handle handle, javacall_serial_flowcontrol mode){
+    struct termios term;
+
+    if(getTermios(handle, &term) != JAVACALL_OK){
+        return JAVACALL_DIO_FAIL;
+    }
+
+    /*
+    IXON   Enable XON/XOFF flow control on output.
+    IXOFF  Enable XON/XOFF flow control on input.
+    (not in POSIX) Enable RTS/CTS (hardware) flow control.
+    */
+
+    /*
+    1 flag for hardware flow control. that means FLOWCONTROL_RTSCTS_IN FLOWCONTROL_RTSCTS_OUT
+    ought to be both set or unset, if not - exception.
+    */
+    if( ((mode & FLOWCONTROL_RTSCTS_IN) == 0)  !=  ((mode & FLOWCONTROL_RTSCTS_OUT) == 0) ){
+        return JAVACALL_DIO_UNSUPPORTED_OPERATION;
+    }
+
+    if(mode & FLOWCONTROL_NONE){
+        term.c_iflag &= ~(IXON | IXOFF);
+        term.c_cflag &= ~(CRTSCTS);
+    }else{
+        if(mode & FLOWCONTROL_RTSCTS_IN){
+            term.c_cflag |= CRTSCTS;
+        }
+        if(mode & FLOWCONTROL_RTSCTS_OUT){
+            term.c_cflag |= CRTSCTS;
+        }
+        if(mode & FLOWCONTROL_XONXOFF_IN){
+            term.c_cflag |= IXON;
+        }
+        if(mode & FLOWCONTROL_XONXOFF_OUT){
+            term.c_cflag |= IXOFF;
+        }
+    }
+
+    return javacall_result2dio_result(setTermios(handle, &term));
+}
+
+/**
+ * Retrive the flow control mode of the open serial port
+ */
+javacall_dio_result /*OPTIONAL*/
+javacall_uart_get_flowcontrol(javacall_handle handle, /*OUT*/ javacall_serial_flowcontrol *mode){
+    struct termios term;
+    if(getTermios(handle, &term) != JAVACALL_OK){
+        return JAVACALL_DIO_FAIL;
+    }
+
+    *mode = 0; //means FLOWCONTROL_NONE
+
+    if(term.c_cflag & CRTSCTS) *mode |= (FLOWCONTROL_RTSCTS_IN | FLOWCONTROL_RTSCTS_OUT);
+    if(term.c_cflag & IXON) *mode |= FLOWCONTROL_XONXOFF_IN;
+    if(term.c_cflag & IXOFF) *mode |= FLOWCONTROL_XONXOFF_OUT;
+
+    return JAVACALL_DIO_OK;
+}
+
 javacall_dio_result /*OPTIONAL*/
 javacall_uart_open_start(const char *devName, unsigned int baudRate, javacall_uart_stop_bits stopBits, \
-                   unsigned int flowControl, javacall_uart_bits_per_char bitsPerchar, \
+                   javacall_serial_flowcontrol flowControl, javacall_uart_bits_per_char bitsPerchar, \
                    javacall_uart_parity parity, const javacall_bool exclusive,
                    /*OUT*/javacall_handle *pHandle){
     int size;
@@ -496,6 +562,11 @@
         return JAVACALL_DIO_INVALID_CONFIG;
     }
 
+    if (JAVACALL_DIO_OK != javacall_uart_set_flowcontrol((javacall_handle)p, flowControl)){
+        javacall_uart_close_start(p, NULL);
+        return JAVACALL_DIO_INVALID_CONFIG;
+    }
+
     if(JAVACALL_DIO_OK != jc_serial_init_buffers_threads(p)){
         javacall_uart_close_start(p, NULL);
         return JAVACALL_DIO_FAIL;
--- a/src/share/native/com/oracle/dio/javacall_serial.h	Fri Apr 10 18:34:14 2015 +0300
+++ b/src/share/native/com/oracle/dio/javacall_serial.h	Tue Apr 14 17:30:21 2015 +0400
@@ -129,21 +129,35 @@
 #define JAVACALL_UNSPECIFIED_BAUD_RATE  -1
 
 /**
- * RTS/CTS (hardware) flow control on input.
+ * @enum javacall_serial_flowcontrol
+ * @brief UART flowcontrol modes
  */
-#define FLOWCONTROL_RTSCTS_IN 1
-/**
-  * RTS/CTS (hardware) flow control on output.
-  */
-#define FLOWCONTROL_RTSCTS_OUT 2
-/**
- * XON/XOFF (software) flow control on input.
- */
-#define FLOWCONTROL_XONXOFF_IN 4
-/**
-  * XON/XOFF (software) flow control on output.
-  */
-#define FLOWCONTROL_XONXOFF_OUT 8
+typedef enum {
+    /**
+     * Flow control off.
+     */
+    FLOWCONTROL_NONE = 0,
+    /**
+     * RTS/CTS (hardware) flow control on input.
+     * This bit flag can be bitwise-combined (OR) with other output flow control bit flags.
+     */
+    FLOWCONTROL_RTSCTS_IN = 1,
+    /**
+     * RTS/CTS (hardware) flow control on output.
+     * This bit flag can be bitwise-combined (OR) with other output flow control bit flags.
+     */
+    FLOWCONTROL_RTSCTS_OUT = 2,
+    /**
+     * XON/XOFF (software) flow control on input.
+     * This bit flag can be bitwise-combined (OR) with other input flow control bit flags.
+     */
+    FLOWCONTROL_XONXOFF_IN = 4,
+    /**
+     * XON/XOFF (software) flow control on output.
+     * This bit flag can be bitwise-combined (OR) with other input flow control bit flags.
+     */
+    FLOWCONTROL_XONXOFF_OUT = 8
+} javacall_serial_flowcontrol;
 
 /**
  * Initiates opening serial link according to the given parameters.
--- a/src/share/native/com/oracle/dio/javacall_uart.h	Fri Apr 10 18:34:14 2015 +0300
+++ b/src/share/native/com/oracle/dio/javacall_uart.h	Tue Apr 14 17:30:21 2015 +0400
@@ -38,7 +38,7 @@
 
 #include "javacall_defs.h"
 #include "javacall_dio.h"
-
+#include "javacall_serial.h"
 
 /**
  * @enum javacall_uart_parity
@@ -144,7 +144,7 @@
  */
 javacall_dio_result
 javacall_uart_open_start(const char *devName, unsigned int baudRate,
-                         javacall_uart_stop_bits stopBits, unsigned int flowControl,
+                         javacall_uart_stop_bits stopBits, javacall_serial_flowcontrol flowControl,
                          javacall_uart_bits_per_char bitsPerchar, javacall_uart_parity parity,
                          const javacall_bool exclusive, /*OUT*/javacall_handle *pHandle);
 
@@ -348,6 +348,31 @@
 javacall_uart_get_bits_per_char(javacall_handle handle, /*OUT*/ javacall_uart_bits_per_char *bitsPerChar);
 
 /**
+ * Update the flowcontrol mode of an open uart port
+ *
+ * @param hPort the port to configure
+ * @param mode new flowcontrol mode (bitwise) for the open connection
+ * @retval JAVACALL_DIO_OK    on success,
+ * @retval JAVACALL_DIO_UNSUPPORTED_OPERATION  if the mode
+ *         is not supported
+ * @retval JAVACALL_DIO_FAIL  on error
+ */
+javacall_dio_result /*OPTIONAL*/
+javacall_uart_set_flowcontrol(javacall_handle handle, javacall_serial_flowcontrol mode);
+
+/**
+ * Retrive the current flowcontrol of the open uart port
+ *
+ * @param hPort the port to configure
+ * @param mode pointer to where to return the flowcontrol mode (bitwise)
+ * @retval JAVACALL_DIO_OK on success,
+ * @retval JAVACALL_DIO_UNSUPPORTED_OPERATION  if the flowcontrol is not supported
+ *         JAVACALL_DIO_FAIL on error
+ */
+javacall_dio_result /*OPTIONAL*/
+javacall_uart_get_flowcontrol(javacall_handle handle, /*OUT*/ javacall_serial_flowcontrol *mode);
+
+/**
  * Stops write operations if any pending
  *
  * @param hPort the port to configure