altosui: Catch I/O errors on telemetry device, report to user
authorKeith Packard <keithp@keithp.com>
Fri, 3 Sep 2010 19:43:45 +0000 (12:43 -0700)
committerKeith Packard <keithp@keithp.com>
Fri, 3 Sep 2010 19:43:45 +0000 (12:43 -0700)
This catches the USB device being unplugged and makes sure the
user sees an error dialog in this case.

Signed-off-by: Keith Packard <keithp@keithp.com>
ao-tools/altosui/AltosLine.java [new file with mode: 0644]
ao-tools/altosui/AltosLog.java
ao-tools/altosui/AltosSerial.java
ao-tools/altosui/AltosUI.java
ao-tools/altosui/Makefile
ao-tools/libaltos/libaltos.c

diff --git a/ao-tools/altosui/AltosLine.java b/ao-tools/altosui/AltosLine.java
new file mode 100644 (file)
index 0000000..86e9d4c
--- /dev/null
@@ -0,0 +1,30 @@
+/*
+ * Copyright © 2010 Keith Packard <keithp@keithp.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program 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 for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ */
+
+package altosui;
+
+public class AltosLine {
+       public String   line;
+
+       public AltosLine() {
+               line = null;
+       }
+
+       public AltosLine(String s) {
+               line = s;
+       }
+}
\ No newline at end of file
index 2c24177..f876beb 100644 (file)
@@ -24,6 +24,7 @@ import java.text.ParseException;
 import java.util.concurrent.LinkedBlockingQueue;
 import altosui.AltosSerial;
 import altosui.AltosFile;
+import altosui.AltosLine;
 
 /*
  * This creates a thread to capture telemetry data and write it to
@@ -31,7 +32,7 @@ import altosui.AltosFile;
  */
 class AltosLog implements Runnable {
 
-       LinkedBlockingQueue<String>     input_queue;
+       LinkedBlockingQueue<AltosLine>  input_queue;
        LinkedBlockingQueue<String>     pending_queue;
        int                             serial;
        int                             flight;
@@ -64,9 +65,11 @@ class AltosLog implements Runnable {
        public void run () {
                try {
                        for (;;) {
-                               String  line = input_queue.take();
+                               AltosLine       line = input_queue.take();
+                               if (line.line == null)
+                                       continue;
                                try {
-                                       AltosTelemetry  telem = new AltosTelemetry(line);
+                                       AltosTelemetry  telem = new AltosTelemetry(line.line);
                                        if (telem.serial != serial || telem.flight != flight || log_file == null) {
                                                close();
                                                serial = telem.serial;
@@ -77,11 +80,11 @@ class AltosLog implements Runnable {
                                } catch (AltosCRCException ce) {
                                }
                                if (log_file != null) {
-                                       log_file.write(line);
+                                       log_file.write(line.line);
                                        log_file.write('\n');
                                        log_file.flush();
                                } else
-                                       pending_queue.put(line);
+                                       pending_queue.put(line.line);
                        }
                } catch (InterruptedException ie) {
                } catch (IOException ie) {
@@ -94,7 +97,7 @@ class AltosLog implements Runnable {
 
        public AltosLog (AltosSerial s) {
                pending_queue = new LinkedBlockingQueue<String> ();
-               input_queue = new LinkedBlockingQueue<String> ();
+               input_queue = new LinkedBlockingQueue<AltosLine> ();
                s.add_monitor(input_queue);
                serial = -1;
                flight = -1;
index bc35d6b..a1fc437 100644 (file)
@@ -27,10 +27,12 @@ import java.util.concurrent.LinkedBlockingQueue;
 import java.util.LinkedList;
 import java.util.Iterator;
 import altosui.AltosSerialMonitor;
+import altosui.AltosLine;
 import libaltosJNI.libaltos;
 import libaltosJNI.altos_device;
 import libaltosJNI.SWIGTYPE_p_altos_file;
 import libaltosJNI.SWIGTYPE_p_altos_list;
+import libaltosJNI.libaltosConstants;
 
 /*
  * This class reads from the serial port and places each received
@@ -41,8 +43,8 @@ import libaltosJNI.SWIGTYPE_p_altos_list;
 public class AltosSerial implements Runnable {
 
        SWIGTYPE_p_altos_file altos;
-       LinkedList<LinkedBlockingQueue<String>> monitors;
-       LinkedBlockingQueue<String> reply_queue;
+       LinkedList<LinkedBlockingQueue<AltosLine>> monitors;
+       LinkedBlockingQueue<AltosLine> reply_queue;
        Thread input_thread;
        String line;
        byte[] line_bytes;
@@ -57,7 +59,15 @@ public class AltosSerial implements Runnable {
                                c = libaltos.altos_getchar(altos, 0);
                                if (Thread.interrupted())
                                        break;
-                               if (c == -1)
+                               if (c == libaltosConstants.LIBALTOS_ERROR) {
+                                       for (int e = 0; e < monitors.size(); e++) {
+                                               LinkedBlockingQueue<AltosLine> q = monitors.get(e);
+                                               q.put(new AltosLine());
+                                       }
+                                       reply_queue.put (new AltosLine());
+                                       break;
+                               }
+                               if (c == libaltosConstants.LIBALTOS_TIMEOUT)
                                        continue;
                                if (c == '\r')
                                        continue;
@@ -73,12 +83,12 @@ public class AltosSerial implements Runnable {
                                                        }
                                                        if (line.startsWith("VERSION") || line.startsWith("CRC")) {
                                                                for (int e = 0; e < monitors.size(); e++) {
-                                                                       LinkedBlockingQueue<String> q = monitors.get(e);
-                                                                       q.put(line);
+                                                                       LinkedBlockingQueue<AltosLine> q = monitors.get(e);
+                                                                       q.put(new AltosLine (line));
                                                                }
                                                        } else {
 //                                                             System.out.printf("GOT: %s\n", line);
-                                                               reply_queue.put(line);
+                                                               reply_queue.put(new AltosLine (line));
                                                        }
                                                        line_count = 0;
                                                        line = "";
@@ -121,16 +131,16 @@ public class AltosSerial implements Runnable {
 
        public String get_reply() throws InterruptedException {
                flush_output();
-               String line = reply_queue.take();
-               return line;
+               AltosLine line = reply_queue.take();
+               return line.line;
        }
 
-       public void add_monitor(LinkedBlockingQueue<String> q) {
+       public void add_monitor(LinkedBlockingQueue<AltosLine> q) {
                set_monitor(true);
                monitors.add(q);
        }
 
-       public void remove_monitor(LinkedBlockingQueue<String> q) {
+       public void remove_monitor(LinkedBlockingQueue<AltosLine> q) {
                monitors.remove(q);
                if (monitors.isEmpty())
                        set_monitor(false);
@@ -218,7 +228,7 @@ public class AltosSerial implements Runnable {
                input_thread = null;
                line = "";
                monitor_mode = false;
-               monitors = new LinkedList<LinkedBlockingQueue<String>> ();
-               reply_queue = new LinkedBlockingQueue<String> ();
+               monitors = new LinkedList<LinkedBlockingQueue<AltosLine>> ();
+               reply_queue = new LinkedBlockingQueue<AltosLine> ();
        }
 }
index e63a004..7e3fb7f 100644 (file)
@@ -44,6 +44,7 @@ import altosui.AltosChannelMenu;
 import altosui.AltosFlashUI;
 import altosui.AltosLogfileChooser;
 import altosui.AltosCSVUI;
+import altosui.AltosLine;
 
 import libaltosJNI.*;
 
@@ -169,6 +170,8 @@ public class AltosUI extends JFrame {
        }
 
        public void show(AltosState state, int crc_errors) {
+               if (state == null)
+                       return;
                flightStatusModel.set(state);
 
                info_reset();
@@ -367,7 +370,7 @@ public class AltosUI extends JFrame {
 
                void init() { }
 
-               AltosRecord read() throws InterruptedException, ParseException, AltosCRCException { return null; }
+               AltosRecord read() throws InterruptedException, ParseException, AltosCRCException, IOException { return null; }
 
                void close() { }
 
@@ -403,6 +406,11 @@ public class AltosUI extends JFrame {
                                        }
                                }
                        } catch (InterruptedException ee) {
+                       } catch (IOException ie) {
+                               JOptionPane.showMessageDialog(AltosUI.this,
+                                                             String.format("Error reading from \"%s\"", name),
+                                                             "Telemetry Read Error",
+                                                             JOptionPane.ERROR_MESSAGE);
                        } finally {
                                close();
                                idle_thread.interrupt();
@@ -417,10 +425,13 @@ public class AltosUI extends JFrame {
 
        class DeviceThread extends DisplayThread {
                AltosSerial     serial;
-               LinkedBlockingQueue<String> telem;
+               LinkedBlockingQueue<AltosLine> telem;
 
-               AltosRecord read() throws InterruptedException, ParseException, AltosCRCException {
-                       return new AltosTelemetry(telem.take());
+               AltosRecord read() throws InterruptedException, ParseException, AltosCRCException, IOException {
+                       AltosLine l = telem.take();
+                       if (l.line == null)
+                               throw new IOException("IO error");
+                       return new AltosTelemetry(l.line);
                }
 
                void close() {
@@ -428,11 +439,11 @@ public class AltosUI extends JFrame {
                        serial.remove_monitor(telem);
                }
 
-               public DeviceThread(AltosSerial s) {
+               public DeviceThread(AltosSerial s, String in_name) {
                        serial = s;
-                       telem = new LinkedBlockingQueue<String>();
+                       telem = new LinkedBlockingQueue<AltosLine>();
                        serial.add_monitor(telem);
-                       name = "telemetry";
+                       name = in_name;
                }
        }
 
@@ -443,7 +454,7 @@ public class AltosUI extends JFrame {
                        try {
                                stop_display();
                                serial_line.open(device);
-                               DeviceThread thread = new DeviceThread(serial_line);
+                               DeviceThread thread = new DeviceThread(serial_line, device.getPath());
                                serial_line.set_channel(AltosPreferences.channel());
                                serial_line.set_callsign(AltosPreferences.callsign());
                                run_display(thread);
index 8509391..cc9a440 100644 (file)
@@ -23,6 +23,7 @@ CLASSFILES=\
        AltosGPS.class \
        AltosGreatCircle.class \
        AltosHexfile.class \
+       AltosLine.class \
        AltosLog.class \
        AltosLogfileChooser.class \
        AltosParse.class \
index 059d2ae..465f0ac 100644 (file)
@@ -576,6 +576,11 @@ altos_free(struct altos_file *file)
 PUBLIC int
 altos_flush(struct altos_file *file)
 {
+       if (file->out_used && 0) {
+               printf ("flush \"");
+               fwrite(file->out_data, 1, file->out_used, stdout);
+               printf ("\"\n");
+       }
        while (file->out_used) {
                int     ret;
 
@@ -634,7 +639,7 @@ altos_fill(struct altos_file *file, int timeout)
                        return LIBALTOS_ERROR;
 #ifdef USE_POLL
                fd[0].fd = file->fd;
-               fd[0].events = POLLIN;
+               fd[0].events = POLLIN|POLLERR|POLLHUP|POLLNVAL;
                fd[1].fd = file->pipe[0];
                fd[1].events = POLLIN;
                ret = poll(fd, 2, timeout);
@@ -644,6 +649,9 @@ altos_fill(struct altos_file *file, int timeout)
                }
                if (ret == 0)
                        return LIBALTOS_TIMEOUT;
+
+               if (fd[0].revents & (POLLHUP|POLLERR|POLLNVAL))
+                       return LIBALTOS_ERROR;
                if (fd[0].revents & POLLIN)
 #endif
                {
@@ -660,6 +668,11 @@ altos_fill(struct altos_file *file, int timeout)
 #endif
                }
        }
+       if (file->in_used && 0) {
+               printf ("fill \"");
+               fwrite(file->in_data, 1, file->in_used, stdout);
+               printf ("\"\n");
+       }
        return 0;
 }