From dcfa56498d1b65a213b8aba9cbd6c4806532383c Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Sat, 13 Nov 2010 16:07:04 -0800 Subject: [PATCH] altosui: Open serial device at 'new' time. Prohibit duplicate opens. With the per-serial UI, there's never a reason to create a serial device without opening it right away. This eliminates the bug caused by not opening the serial device for telemetry reception. Serial devices can now be opened only once; this eliminates errors when trying to reflash or configure devices while receiving telemetry. Signed-off-by: Keith Packard --- ao-tools/altosui/AltosConfig.java | 9 +++++-- ao-tools/altosui/AltosDebug.java | 11 ++++++--- ao-tools/altosui/AltosEepromDownload.java | 9 +++++-- ao-tools/altosui/AltosFlash.java | 13 ++++------ ao-tools/altosui/AltosFlashUI.java | 11 ++++++--- ao-tools/altosui/AltosSerial.java | 28 +++++++++++++--------- ao-tools/altosui/AltosTelemetryReader.java | 5 ++-- ao-tools/altosui/AltosUI.java | 6 +++++ ao-tools/altosui/Makefile.am | 1 + 9 files changed, 62 insertions(+), 31 deletions(-) diff --git a/ao-tools/altosui/AltosConfig.java b/ao-tools/altosui/AltosConfig.java index 30f7d541..09e204a9 100644 --- a/ao-tools/altosui/AltosConfig.java +++ b/ao-tools/altosui/AltosConfig.java @@ -231,10 +231,9 @@ public class AltosConfig implements Runnable, ActionListener { product = new string_ref("unknown"); device = AltosDeviceDialog.show(owner, AltosDevice.product_any); - serial_line = new AltosSerial(); if (device != null) { try { - serial_line.open(device); + serial_line = new AltosSerial(device); if (!device.matchProduct(AltosDevice.product_telemetrum)) remote = true; config_thread = new Thread(this); @@ -245,6 +244,12 @@ public class AltosConfig implements Runnable, ActionListener { device.getPath()), "Cannot open target device", JOptionPane.ERROR_MESSAGE); + } catch (AltosSerialInUseException si) { + JOptionPane.showMessageDialog(owner, + String.format("Device \"%s\" already in use", + device.getPath()), + "Device in use", + JOptionPane.ERROR_MESSAGE); } catch (IOException ee) { JOptionPane.showMessageDialog(owner, device.getPath(), diff --git a/ao-tools/altosui/AltosDebug.java b/ao-tools/altosui/AltosDebug.java index 9c10129d..9aa35d3f 100644 --- a/ao-tools/altosui/AltosDebug.java +++ b/ao-tools/altosui/AltosDebug.java @@ -19,9 +19,10 @@ package altosui; import java.lang.*; import java.io.*; -import java.util.concurrent.LinkedBlockingQueue; -import java.util.LinkedList; -import java.util.Iterator; +import java.util.concurrent.*; +import java.util.*; + +import libaltosJNI.*; public class AltosDebug extends AltosSerial { @@ -259,4 +260,8 @@ public class AltosDebug extends AltosSerial { public void reset() { printf ("R\n"); } + + public AltosDebug (altos_device in_device) throws FileNotFoundException, AltosSerialInUseException { + super(in_device); + } } \ No newline at end of file diff --git a/ao-tools/altosui/AltosEepromDownload.java b/ao-tools/altosui/AltosEepromDownload.java index bd9e4b48..8996b924 100644 --- a/ao-tools/altosui/AltosEepromDownload.java +++ b/ao-tools/altosui/AltosEepromDownload.java @@ -244,12 +244,11 @@ public class AltosEepromDownload implements Runnable { frame = given_frame; device = AltosDeviceDialog.show(frame, AltosDevice.product_any); - serial_line = new AltosSerial(); remote = false; if (device != null) { try { - serial_line.open(device); + serial_line = new AltosSerial(device); if (!device.matchProduct(AltosDevice.product_telemetrum)) remote = true; eeprom_thread = new Thread(this); @@ -260,6 +259,12 @@ public class AltosEepromDownload implements Runnable { device.getPath()), "Cannot open target device", JOptionPane.ERROR_MESSAGE); + } catch (AltosSerialInUseException si) { + JOptionPane.showMessageDialog(frame, + String.format("Device \"%s\" already in use", + device.getPath()), + "Device in use", + JOptionPane.ERROR_MESSAGE); } catch (IOException ee) { JOptionPane.showMessageDialog(frame, device.getPath(), diff --git a/ao-tools/altosui/AltosFlash.java b/ao-tools/altosui/AltosFlash.java index 25b4a06e..fa2465d3 100644 --- a/ao-tools/altosui/AltosFlash.java +++ b/ao-tools/altosui/AltosFlash.java @@ -329,17 +329,14 @@ public class AltosFlash { return rom_config; } - public void open() throws IOException, FileNotFoundException, InterruptedException { + public AltosFlash(File in_file, AltosDevice in_debug_dongle) + throws IOException, FileNotFoundException, AltosSerialInUseException, InterruptedException { + file = in_file; + debug_dongle = in_debug_dongle; + debug = new AltosDebug(in_debug_dongle); input = new FileInputStream(file); image = new AltosHexfile(input); - debug.open(debug_dongle); if (!debug.check_connection()) throw new IOException("Debug port not connected"); } - - public AltosFlash(File in_file, AltosDevice in_debug_dongle) { - file = in_file; - debug_dongle = in_debug_dongle; - debug = new AltosDebug(); - } } \ No newline at end of file diff --git a/ao-tools/altosui/AltosFlashUI.java b/ao-tools/altosui/AltosFlashUI.java index 70c8c549..b09cb594 100644 --- a/ao-tools/altosui/AltosFlashUI.java +++ b/ao-tools/altosui/AltosFlashUI.java @@ -65,10 +65,9 @@ public class AltosFlashUI } public void run() { - flash = new AltosFlash(file, debug_dongle); - flash.addActionListener(this); try { - flash.open(); + flash = new AltosFlash(file, debug_dongle); + flash.addActionListener(this); AltosRomconfigUI romconfig_ui = new AltosRomconfigUI (frame); romconfig_ui.set(flash.romconfig()); @@ -88,6 +87,12 @@ public class AltosFlashUI "Cannot open image", file.toString(), JOptionPane.ERROR_MESSAGE); + } catch (AltosSerialInUseException si) { + JOptionPane.showMessageDialog(frame, + String.format("Device \"%s\" already in use", + debug_dongle.getPath()), + "Device in use", + JOptionPane.ERROR_MESSAGE); } catch (IOException e) { JOptionPane.showMessageDialog(frame, e.getMessage(), diff --git a/ao-tools/altosui/AltosSerial.java b/ao-tools/altosui/AltosSerial.java index 6787e0c8..99a92fdb 100644 --- a/ao-tools/altosui/AltosSerial.java +++ b/ao-tools/altosui/AltosSerial.java @@ -23,9 +23,8 @@ package altosui; import java.lang.*; import java.io.*; -import java.util.concurrent.LinkedBlockingQueue; -import java.util.LinkedList; -import java.util.Iterator; +import java.util.concurrent.*; +import java.util.*; import libaltosJNI.*; @@ -37,6 +36,9 @@ import libaltosJNI.*; public class AltosSerial implements Runnable { + static List devices_opened = Collections.synchronizedList(new LinkedList()); + + altos_device device; SWIGTYPE_p_altos_file altos; LinkedList> monitors; LinkedBlockingQueue reply_queue; @@ -141,10 +143,6 @@ public class AltosSerial implements Runnable { set_monitor(false); } - public boolean opened() { - return altos != null; - } - public void close() { if (altos != null) { libaltos.altos_close(altos); @@ -161,6 +159,9 @@ public class AltosSerial implements Runnable { libaltos.altos_free(altos); altos = null; } + synchronized (devices_opened) { + devices_opened.remove(device.getPath()); + } } public void putc(char c) { @@ -178,7 +179,12 @@ public class AltosSerial implements Runnable { print(String.format(format, arguments)); } - public void open(altos_device device) throws FileNotFoundException { + private void open() throws FileNotFoundException, AltosSerialInUseException { + synchronized (devices_opened) { + if (devices_opened.contains(device.getPath())) + throw new AltosSerialInUseException(device); + devices_opened.add(device.getPath()); + } close(); altos = libaltos.altos_open(device); if (altos == null) @@ -220,12 +226,12 @@ public class AltosSerial implements Runnable { } } - public AltosSerial() { - altos = null; - input_thread = null; + public AltosSerial(altos_device in_device) throws FileNotFoundException, AltosSerialInUseException { + device = in_device; line = ""; monitor_mode = false; monitors = new LinkedList> (); reply_queue = new LinkedBlockingQueue (); + open(); } } diff --git a/ao-tools/altosui/AltosTelemetryReader.java b/ao-tools/altosui/AltosTelemetryReader.java index 0b5509eb..ff02c722 100644 --- a/ao-tools/altosui/AltosTelemetryReader.java +++ b/ao-tools/altosui/AltosTelemetryReader.java @@ -50,9 +50,10 @@ class AltosTelemetryReader extends AltosFlightReader { serial.set_callsign(callsign); } - public AltosTelemetryReader (AltosDevice in_device) throws FileNotFoundException, IOException { + public AltosTelemetryReader (AltosDevice in_device) + throws FileNotFoundException, AltosSerialInUseException, IOException { device = in_device; - serial = new AltosSerial(); + serial = new AltosSerial(device); log = new AltosLog(serial); name = device.getPath(); diff --git a/ao-tools/altosui/AltosUI.java b/ao-tools/altosui/AltosUI.java index 9ab451de..0d8f0e8d 100644 --- a/ao-tools/altosui/AltosUI.java +++ b/ao-tools/altosui/AltosUI.java @@ -56,6 +56,12 @@ public class AltosUI extends JFrame { device.getPath()), "Cannot open target device", JOptionPane.ERROR_MESSAGE); + } catch (AltosSerialInUseException si) { + JOptionPane.showMessageDialog(AltosUI.this, + String.format("Device \"%s\" already in use", + device.getPath()), + "Device in use", + JOptionPane.ERROR_MESSAGE); } catch (IOException ee) { JOptionPane.showMessageDialog(AltosUI.this, device.getPath(), diff --git a/ao-tools/altosui/Makefile.am b/ao-tools/altosui/Makefile.am index 267bae63..f4c743df 100644 --- a/ao-tools/altosui/Makefile.am +++ b/ao-tools/altosui/Makefile.am @@ -58,6 +58,7 @@ altosui_JAVA = \ AltosRomconfig.java \ AltosRomconfigUI.java \ AltosSerial.java \ + AltosSerialInUseException.java \ AltosSerialMonitor.java \ AltosState.java \ AltosStatusTable.java \ -- 2.30.2