From 59798c6fd11502a9c8b66090c23ba50eb250692e Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Fri, 3 Sep 2010 12:43:45 -0700 Subject: [PATCH] altosui: Catch I/O errors on telemetry device, report to user This catches the USB device being unplugged and makes sure the user sees an error dialog in this case. Signed-off-by: Keith Packard --- ao-tools/altosui/AltosLine.java | 30 +++++++++++++++++++++++++++ ao-tools/altosui/AltosLog.java | 15 ++++++++------ ao-tools/altosui/AltosSerial.java | 34 ++++++++++++++++++++----------- ao-tools/altosui/AltosUI.java | 27 ++++++++++++++++-------- ao-tools/altosui/Makefile | 1 + ao-tools/libaltos/libaltos.c | 15 +++++++++++++- 6 files changed, 95 insertions(+), 27 deletions(-) create mode 100644 ao-tools/altosui/AltosLine.java diff --git a/ao-tools/altosui/AltosLine.java b/ao-tools/altosui/AltosLine.java new file mode 100644 index 00000000..86e9d4c6 --- /dev/null +++ b/ao-tools/altosui/AltosLine.java @@ -0,0 +1,30 @@ +/* + * Copyright © 2010 Keith Packard + * + * 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 diff --git a/ao-tools/altosui/AltosLog.java b/ao-tools/altosui/AltosLog.java index 2c241771..f876beba 100644 --- a/ao-tools/altosui/AltosLog.java +++ b/ao-tools/altosui/AltosLog.java @@ -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 input_queue; + LinkedBlockingQueue input_queue; LinkedBlockingQueue 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 (); - input_queue = new LinkedBlockingQueue (); + input_queue = new LinkedBlockingQueue (); s.add_monitor(input_queue); serial = -1; flight = -1; diff --git a/ao-tools/altosui/AltosSerial.java b/ao-tools/altosui/AltosSerial.java index bc35d6b8..a1fc4371 100644 --- a/ao-tools/altosui/AltosSerial.java +++ b/ao-tools/altosui/AltosSerial.java @@ -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> monitors; - LinkedBlockingQueue reply_queue; + LinkedList> monitors; + LinkedBlockingQueue 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 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 q = monitors.get(e); - q.put(line); + LinkedBlockingQueue 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 q) { + public void add_monitor(LinkedBlockingQueue q) { set_monitor(true); monitors.add(q); } - public void remove_monitor(LinkedBlockingQueue q) { + public void remove_monitor(LinkedBlockingQueue 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> (); - reply_queue = new LinkedBlockingQueue (); + monitors = new LinkedList> (); + reply_queue = new LinkedBlockingQueue (); } } diff --git a/ao-tools/altosui/AltosUI.java b/ao-tools/altosui/AltosUI.java index e63a004c..7e3fb7f9 100644 --- a/ao-tools/altosui/AltosUI.java +++ b/ao-tools/altosui/AltosUI.java @@ -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 telem; + LinkedBlockingQueue 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(); + telem = new LinkedBlockingQueue(); 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); diff --git a/ao-tools/altosui/Makefile b/ao-tools/altosui/Makefile index 8509391e..cc9a440d 100644 --- a/ao-tools/altosui/Makefile +++ b/ao-tools/altosui/Makefile @@ -23,6 +23,7 @@ CLASSFILES=\ AltosGPS.class \ AltosGreatCircle.class \ AltosHexfile.class \ + AltosLine.class \ AltosLog.class \ AltosLogfileChooser.class \ AltosParse.class \ diff --git a/ao-tools/libaltos/libaltos.c b/ao-tools/libaltos/libaltos.c index 059d2ae9..465f0ac8 100644 --- a/ao-tools/libaltos/libaltos.c +++ b/ao-tools/libaltos/libaltos.c @@ -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; } -- 2.30.2