changeset 90:ecdc22ce1f87

8071673: [MEEP] Change javacall_serial_read(write)_* for MEEP compatibility Summary: Added blocking flag and few changes for opportunity to use it as blocking function Reviewed-by: snazarki Contributed-by: alexander.valitov@oracle.com
author amironov
date Wed, 28 Jan 2015 13:32:58 +0300
parents 1a63316a2a5d
children 0500a43b8c22
files src/share/linux/native/com/oracle/dio/uart/serial.c src/share/linux/native/com/oracle/dio/uart/serial.h src/share/native/com/oracle/dio/javacall_serial.h
diffstat 3 files changed, 27 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/linux/native/com/oracle/dio/uart/serial.c	Wed Jan 28 13:51:47 2015 +0400
+++ b/src/share/linux/native/com/oracle/dio/uart/serial.c	Wed Jan 28 13:32:58 2015 +0300
@@ -217,8 +217,10 @@
 }
 
 static void _new_data_avail_cb(javacall_handle handle, int param, javacall_result res) {
-    //never happens in this implementation
-    //javanotify_serial_event(JAVACALL_EVENT_SERIAL_RECEIVE, handle, res);
+    if (((serial_handle) handle)->isBlockedOnRead) {
+        ((serial_handle) handle)->isBlockedOnRead = JAVACALL_FALSE;
+        javanotify_serial_event(JAVACALL_EVENT_SERIAL_RECEIVE, handle, res);
+    }
 }
 
 /**
@@ -238,7 +240,7 @@
         return JAVACALL_OUT_OF_MEMORY;
     }
 
-    memset(p, sizeof(SERIAL_DESC), 0);
+    memset(p, 0, sizeof(SERIAL_DESC));
 
     // open port
     if ( JAVACALL_OK != (res = jc_serial_open(devName, p)) ) {
@@ -411,7 +413,11 @@
        //empty buffer
        *bytesRead = 0;
        *bytesAvailable = 0;
-    }else{
+       p->isBlockedOnRead = JAVACALL_TRUE;
+       // Check if data was received between call to 'circular_buffer_get_array' and
+       // 'isBlockedOnRead' was set.
+       javautil_circular_buffer_get_array(p->inBuffer, buffer, (javacall_int32*)bytesRead);
+    } else {
         //check if more bytes available
         javautil_circular_buffer_get_count(p->inBuffer, (javacall_int32*)bytesAvailable);
     }
@@ -434,12 +440,13 @@
 
     int bytesAvailable;
     javacall_result result;
+    (void)pContext; /* unused */
 
     pthread_mutex_lock( &((serial_handle)handle)->lock);
     result = jc_serial_read_common((serial_handle)handle, buffer, size, bytesRead, &bytesAvailable);
     pthread_mutex_unlock( &((serial_handle)handle)->lock);
 
-    return result;
+    return *bytesRead == 0 ? JAVACALL_WOULD_BLOCK : JAVACALL_OK;
 }
 
 
@@ -449,10 +456,9 @@
 javacall_result /*OPTIONAL*/javacall_serial_read_finish(javacall_handle handle,
         unsigned char* buffer, int size, int *bytesRead,
         void *context) {
-    /*
-        cannot be called in this implementation, because of _start returns OK or FAIL
-    */
-    return JAVACALL_FAIL;
+    (void)context; /* unused */
+
+    return javacall_serial_read_start(handle, buffer, size, bytesRead, NULL);
 }
 
 javacall_result jc_serial_write_common(serial_handle handle,
--- a/src/share/linux/native/com/oracle/dio/uart/serial.h	Wed Jan 28 13:51:47 2015 +0400
+++ b/src/share/linux/native/com/oracle/dio/uart/serial.h	Wed Jan 28 13:32:58 2015 +0300
@@ -72,6 +72,7 @@
     pthread_mutex_t          write_lock;
     pthread_cond_t           out_buffer_ready;
 
+    javacall_bool            isBlockedOnRead;
     javacall_handle          out_buffer;
     int                      out_buffer_size;
     int                      out_total_written;
--- a/src/share/native/com/oracle/dio/javacall_serial.h	Wed Jan 28 13:51:47 2015 +0400
+++ b/src/share/native/com/oracle/dio/javacall_serial.h	Wed Jan 28 13:32:58 2015 +0300
@@ -121,6 +121,8 @@
 #define JAVACALL_SERIAL_BITS_PER_CHAR_8 0xC0
 /** 9-bit chars */
 #define JAVACALL_SERIAL_BITS_PER_CHAR_9 0x100
+/** Blocking hint: prefer throughput over latency */
+#define JAVACALL_SERIAL_BLOCKING        0x200
 /** @} */
 
 /** Unspecified baud rate */
@@ -245,15 +247,15 @@
 javacall_result /*OPTIONAL*/ javacall_serial_close_finish(javacall_handle hPort, void *context);
 
 /**
- * Initiates reading a specified number of bytes from serial link,
-
+ * Initiates reading of up to 'len' bytes from serial link.
+ *
  * @param hPort the port to read the data from
  * @param buffer to which data is read
  * @param size number of bytes to be read. Actual number of bytes
  *              read may be less, if less data is available
  * @param bytesRead actual number the were read from the port.
  * @param pContext filled by ptr to data for reinvocations
- * @retval JAVACALL_OK          success
+ * @retval JAVACALL_OK          success, at least one byte was read
  * @retval JAVACALL_FAIL        if there was an error
  * @retval JAVACALL_WOULD_BLOCK  if the caller must call the finish function again to complete the operation
  */
@@ -277,7 +279,7 @@
   int size ,/*OUT*/int *bytesRead,/*OUT*/int *bytesAvailable);
 
 /**
- * Finishes reading a specified number of bytes from serial link,
+ * Finishes reading of up to 'len' bytes from serial link.
  *
  * @param hPort the port to read the data from
  * @param buffer to which data is read
@@ -286,7 +288,7 @@
  * @param bytesRead actual number the were read from the port.
  * @param size number of bytes to be write.
  * @param context ptr to data saved before sleeping
- * @retval JAVACALL_OK          success
+ * @retval JAVACALL_OK          success, at least one byte was read
  * @retval JAVACALL_FAIL        if there was an error
  * @retval JAVACALL_WOULD_BLOCK  if the caller must call the finish function again to complete the operation
  */
@@ -294,14 +296,14 @@
   int size, int *bytesRead, void *context);
 
 /**
- * Initiates writing a specified number of bytes to serial link,
+ * Initiates writing of up to 'len' bytes to serial link.
  *
  * @param hPort the port to write the data to
  * @param buffer buffer to write data from
  * @param size number of bytes to be written.
  * @param bytesWritten the number of bytes actually written.
  * @param pContext filled by ptr to data for reinvocations
- * @retval JAVACALL_OK          success
+ * @retval JAVACALL_OK          success, at least one byte was written
  * @retval JAVACALL_FAIL        if there was an error
  * @retval JAVACALL_WOULD_BLOCK  if the caller must call the finish function again to complete the operation
  */
@@ -309,14 +311,14 @@
   int size, int *bytesWritten, void **pContext);
 
 /**
- * Finishes writing a specified number of bytes to serial link,
+ * Finishes writing of up to 'len' bytes to serial link.
  *
  * @param hPort the port to write the data to
  * @param buffer buffer to write data from
  * @param size number of bytes to be write.
  * @param bytesWritten the number of bytes actually written.
  * @param context ptr to data saved before sleeping
- * @retval JAVACALL_OK          success
+ * @retval JAVACALL_OK          success, at least one byte was written
  * @retval JAVACALL_FAIL        if there was an error
  * @retval JAVACALL_WOULD_BLOCK  if the caller must call the finish function again to complete the operation
  */