From f0e731ab72d189c9a30983aab95d8b154804bb65 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Tue, 7 May 2013 00:29:22 +0200 Subject: [PATCH] android/GlueIOIOPort: fix spurious errors after IOIO baud rate change Generic solution for the workaround in commit fb7f0ec3. Fixes TRAC tickets #2733, #2754. --- NEWS.txt | 1 + android/src/AbstractAndroidPort.java | 2 +- android/src/GlueIOIOPort.java | 37 ++++++++++++++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/NEWS.txt b/NEWS.txt index 6193cd095..122abb321 100644 --- a/NEWS.txt +++ b/NEWS.txt @@ -10,6 +10,7 @@ Version 6.6.1 - not yet released * devices - indicate duplicate devices in list - allow using more than one TCP/UDP device + - fix spurious errors after IOIO baud rate change (#2733, #2754) * configuration - fix regression with polar configuration (#2803) diff --git a/android/src/AbstractAndroidPort.java b/android/src/AbstractAndroidPort.java index a8cd2adda..44370ba10 100644 --- a/android/src/AbstractAndroidPort.java +++ b/android/src/AbstractAndroidPort.java @@ -94,7 +94,7 @@ abstract class AbstractAndroidPort implements AndroidPort { return o != null && o.drain(); } - @Override public final int write(byte[] data, int length) { + @Override public int write(byte[] data, int length) { OutputThread o = output; return o != null ? o.write(data, length) diff --git a/android/src/GlueIOIOPort.java b/android/src/GlueIOIOPort.java index 64eb9f04d..35c981ca7 100644 --- a/android/src/GlueIOIOPort.java +++ b/android/src/GlueIOIOPort.java @@ -40,6 +40,13 @@ final class GlueIOIOPort extends IOIOPort implements IOIOConnectionListener { private IOIOConnectionHolder holder; private boolean connected; + + /** + * Set to true when the connection is being cycled, e.g. during a + * baud rate change. It is used by waitCycled(). + */ + private boolean cycling; + private final int inPin; private final int outPin; private int baudrate = 0; @@ -70,6 +77,24 @@ final class GlueIOIOPort extends IOIOPort implements IOIOConnectionListener { _holder.addListener(this); } + /** + * Wait for a certain amoutn of time until the port has been + * reconnected after a baud rate change. Call this to avoid + * spurious I/O errors while the baud rate is being changed. + */ + private synchronized void waitCycled() { + if (!cycling) + return; + + /* wait only once */ + cycling = false; + + try { + wait(200); + } catch (InterruptedException e) { + } + } + @Override public void onIOIOConnect(IOIO ioio) throws ConnectionLostException, InterruptedException { connected = true; @@ -84,6 +109,11 @@ final class GlueIOIOPort extends IOIOPort implements IOIOConnectionListener { } set(uart); + + synchronized(this) { + cycling = false; + notifyAll(); + } } @Override public void onIOIODisconnect() { @@ -123,7 +153,14 @@ final class GlueIOIOPort extends IOIOPort implements IOIOConnectionListener { return false; baudrate = _baudrate; + if (connected) + cycling = true; holder.cycleListener(this); return true; } + + @Override public int write(byte[] data, int length) { + waitCycled(); + return super.write(data, length); + } } -- 2.11.4.GIT