changeset 171:36fd4f8f325a

8080995: DIO UART read(ByteBuffer) method may result in wrong trigger level checking Summary: Redesigned a way to check trigger condition Reviewed-by: alkonsta
author snazarki
date Mon, 25 May 2015 18:45:07 +0300
parents 5cd6e5117297
children 416d7f008dd6
files src/se/classes/com/oracle/dio/uart/impl/UARTEventHandler.java src/share/classes/com/oracle/dio/impl/AbstractPeripheral.java src/share/classes/com/oracle/dio/uart/impl/UARTImpl.java
diffstat 3 files changed, 17 insertions(+), 30 deletions(-) [+]
line wrap: on
line diff
--- a/src/se/classes/com/oracle/dio/uart/impl/UARTEventHandler.java	Mon May 25 13:50:00 2015 +0300
+++ b/src/se/classes/com/oracle/dio/uart/impl/UARTEventHandler.java	Mon May 25 18:45:07 2015 +0300
@@ -121,18 +121,6 @@
         }
     }
 
-    public void sendTimeoutEvent(int handle) {
-        int type = jdk.dio.uart.UARTEvent.INPUT_DATA_AVAILABLE;
-        int bytes = -1;
-        byte[] payload = new byte[] {
-                (byte)(handle >> 24), (byte)(handle >> 16), (byte)(handle >> 8), (byte)(handle),
-                (byte)(type >> 24), (byte)(type >> 16), (byte)(type >> 8), (byte)(type),
-                (byte)(bytes >> 24), (byte)(bytes >> 16), (byte)(bytes >> 8), (byte)(bytes),
-            };
-        UARTEvent event = new UARTEvent(payload);
-        queue.postEvent(event);
-    }
-
     /**
      * This method is called by EventQueue.dispatch(). Each call is made on a
      * separate thread.
--- a/src/share/classes/com/oracle/dio/impl/AbstractPeripheral.java	Mon May 25 13:50:00 2015 +0300
+++ b/src/share/classes/com/oracle/dio/impl/AbstractPeripheral.java	Mon May 25 18:45:07 2015 +0300
@@ -152,7 +152,7 @@
         called after native call on Buffer.clice() returns,
         and necessary shift original buffers position
     */
-    protected void shiftBufferPosition(Buffer buff, int newExpectedPosition) throws IOException {
+    protected void shiftBufferPosition(Buffer buff, int newExpectedPosition)  {
         synchronized (buff){
             if (newExpectedPosition > buff.position()){
                 //try/catch because of possible not synchronized change of buff in another thread
--- a/src/share/classes/com/oracle/dio/uart/impl/UARTImpl.java	Mon May 25 13:50:00 2015 +0300
+++ b/src/share/classes/com/oracle/dio/uart/impl/UARTImpl.java	Mon May 25 18:45:07 2015 +0300
@@ -113,10 +113,19 @@
     }
 
     private class InternalRoundListener implements  InputRoundListener<UART, ByteBuffer> {
+
+            private final int endPos;
+
+            private InternalRoundListener(int endPos) {
+                this.endPos = endPos;
+            }
+
             @Override
             public void inputRoundCompleted(RoundCompletionEvent<UART, ByteBuffer> event) {
-                synchronized(synchReadLock){
-                    synchReadLock.notifyAll();
+                if (event.getBuffer().position() >= endPos) {
+                    synchronized (synchReadLock) {
+                        synchReadLock.notifyAll();
+                    }
                 }
             }
 
@@ -128,10 +137,6 @@
             }
     }
 
-    private InputRoundListener getLocalInputRoundListener(){
-        return new InternalRoundListener();
-    }
-
 
     private boolean isAlphaNumerical(char ch) {
         if ((('a' <= ch && ch <= 'z') ||
@@ -220,17 +225,11 @@
                 */
                 int tmpPos = buffer.position();
                 int bytesRead = read0(buffer);
-                try{
-                    shiftBufferPosition(buffer, tmpPos + bytesRead);
-                }catch(IOException e){
 
-                }
+                shiftBufferPosition(buffer, tmpPos + bytesRead);
 
                 if(!buffer.hasRemaining()
-                   || (0 != receiveTriggerLevel &&
-                       inRoundListener instanceof InternalRoundListener &&
-                       (long)(buffer.position() - readBuffersPositions[readBufferIdx]) >= receiveTriggerLevel)
-                   || (-1 == bytesProcessed)){
+                   || inRoundListener instanceof InternalRoundListener){
                     RoundCompletionEvent<UART,ByteBuffer> rcEvent =
                         new RoundCompletionEvent(this, buffer, buffer.position() - readBuffersPositions[readBufferIdx]);
 
@@ -275,7 +274,7 @@
                     }
                     return;
                 }
-                buffer.position(buffer.position() + bytesProcessed);
+                shiftBufferPosition(buffer, buffer.position() + bytesProcessed);
 
                 if (!buffer.hasRemaining()) {
                     RoundCompletionEvent<UART,ByteBuffer> rcEvent = new RoundCompletionEvent(this, buffer, buffer.position() - writeBuffersPositions[writeBufferIdx]);
@@ -688,14 +687,14 @@
                 /*read all data available*/
                 int readRes = read0(dst);
                 shiftBufferPosition(dst, ret + readRes);
-                if (dst.hasRemaining()) {
+                if ((0 == receiveTriggerLevel || readRes < receiveTriggerLevel) && dst.hasRemaining()) {
                     if (!UARTEventHandler.getInstance().isDispatchThread()) {
                         /*
                          * the user calls read() from the event callback, or inputTimeout is 0
                          * exit immediatelly,
                          * else read with timeout
                          */
-                        startReading(dst, getLocalInputRoundListener());
+                        startReading(dst, new InternalRoundListener((0 != receiveTriggerLevel) ? receiveTriggerLevel + ret : dst.limit()));
                             try{
                                 if(inputTimeout == Integer.MAX_VALUE){
                                     //timeout disabled, wait forever or till the buffer is fullfilled