altosui: Open serial device at 'new' time. Prohibit duplicate opens.
authorKeith Packard <keithp@keithp.com>
Sun, 14 Nov 2010 00:07:04 +0000 (16:07 -0800)
committerKeith Packard <keithp@keithp.com>
Sun, 14 Nov 2010 00:07:04 +0000 (16:07 -0800)
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 <keithp@keithp.com>
ao-tools/altosui/AltosConfig.java
ao-tools/altosui/AltosDebug.java
ao-tools/altosui/AltosEepromDownload.java
ao-tools/altosui/AltosFlash.java
ao-tools/altosui/AltosFlashUI.java
ao-tools/altosui/AltosSerial.java
ao-tools/altosui/AltosTelemetryReader.java
ao-tools/altosui/AltosUI.java
ao-tools/altosui/Makefile.am

index 30f7d541bd60b44d612bf8574b03052167fb02e5..09e204a9de3e9f7e7077937548d19c886f2db0f7 100644 (file)
@@ -231,10 +231,9 @@ public class AltosConfig implements Runnable, ActionListener {
                product = new string_ref("unknown");
 
                device = AltosDeviceDialog.show(owner, AltosDevice.product_any);
                product = new string_ref("unknown");
 
                device = AltosDeviceDialog.show(owner, AltosDevice.product_any);
-               serial_line = new AltosSerial();
                if (device != null) {
                        try {
                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);
                                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);
                                                                            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(),
                        } catch (IOException ee) {
                                JOptionPane.showMessageDialog(owner,
                                                              device.getPath(),
index 9c10129ddbf2aeb4ece08dc9af3a5f47f4d375b7..9aa35d3f2f834813d7cfdd51a5dbf2a631b750ee 100644 (file)
@@ -19,9 +19,10 @@ package altosui;
 
 import java.lang.*;
 import java.io.*;
 
 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 {
 
 
 public class AltosDebug extends AltosSerial {
 
@@ -259,4 +260,8 @@ public class AltosDebug extends AltosSerial {
        public void reset() {
                printf ("R\n");
        }
        public void reset() {
                printf ("R\n");
        }
+
+       public AltosDebug (altos_device in_device) throws FileNotFoundException, AltosSerialInUseException {
+               super(in_device);
+       }
 }
\ No newline at end of file
 }
\ No newline at end of file
index bd9e4b4853920297569fdb7f6baa818f1b593d9d..8996b9243a5284cc8a5b2606a6d15abc648e653b 100644 (file)
@@ -244,12 +244,11 @@ public class AltosEepromDownload implements Runnable {
                frame = given_frame;
                device = AltosDeviceDialog.show(frame, AltosDevice.product_any);
 
                frame = given_frame;
                device = AltosDeviceDialog.show(frame, AltosDevice.product_any);
 
-               serial_line = new AltosSerial();
                remote = false;
 
                if (device != null) {
                        try {
                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);
                                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);
                                                                            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(),
                        } catch (IOException ee) {
                                JOptionPane.showMessageDialog(frame,
                                                              device.getPath(),
index 25b4a06efedadfb5c4a3bcb7926d28b8a606e243..fa2465d337748a0af7ee0fd79dcdd0c80d5e8552 100644 (file)
@@ -329,17 +329,14 @@ public class AltosFlash {
                return rom_config;
        }
 
                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);
                input = new FileInputStream(file);
                image = new AltosHexfile(input);
-               debug.open(debug_dongle);
                if (!debug.check_connection())
                        throw new IOException("Debug port not connected");
        }
                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
 }
\ No newline at end of file
index 70c8c5490106ad9b5a7a5732286fb7b39262ac55..b09cb59472e0d6c2608161bce26d2f838ae0fdbd 100644 (file)
@@ -65,10 +65,9 @@ public class AltosFlashUI
        }
 
        public void run() {
        }
 
        public void run() {
-               flash = new AltosFlash(file, debug_dongle);
-               flash.addActionListener(this);
                try {
                try {
-                       flash.open();
+                       flash = new AltosFlash(file, debug_dongle);
+                       flash.addActionListener(this);
                        AltosRomconfigUI romconfig_ui = new AltosRomconfigUI (frame);
 
                        romconfig_ui.set(flash.romconfig());
                        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);
                                                      "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(),
                } catch (IOException e) {
                        JOptionPane.showMessageDialog(frame,
                                                      e.getMessage(),
index 6787e0c82de78026566159eda85a1c51e513c770..99a92fdba5153c69aa8569de3a21012220172a43 100644 (file)
@@ -23,9 +23,8 @@ package altosui;
 
 import java.lang.*;
 import java.io.*;
 
 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.*;
 
 
 import libaltosJNI.*;
 
@@ -37,6 +36,9 @@ import libaltosJNI.*;
 
 public class AltosSerial implements Runnable {
 
 
 public class AltosSerial implements Runnable {
 
+       static List<String> devices_opened = Collections.synchronizedList(new LinkedList<String>());
+
+       altos_device device;
        SWIGTYPE_p_altos_file altos;
        LinkedList<LinkedBlockingQueue<AltosLine>> monitors;
        LinkedBlockingQueue<AltosLine> reply_queue;
        SWIGTYPE_p_altos_file altos;
        LinkedList<LinkedBlockingQueue<AltosLine>> monitors;
        LinkedBlockingQueue<AltosLine> reply_queue;
@@ -141,10 +143,6 @@ public class AltosSerial implements Runnable {
                        set_monitor(false);
        }
 
                        set_monitor(false);
        }
 
-       public boolean opened() {
-               return altos != null;
-       }
-
        public void close() {
                if (altos != null) {
                        libaltos.altos_close(altos);
        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;
                }
                        libaltos.altos_free(altos);
                        altos = null;
                }
+               synchronized (devices_opened) {
+                       devices_opened.remove(device.getPath());
+               }
        }
 
        public void putc(char c) {
        }
 
        public void putc(char c) {
@@ -178,7 +179,12 @@ public class AltosSerial implements Runnable {
                print(String.format(format, arguments));
        }
 
                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)
                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<LinkedBlockingQueue<AltosLine>> ();
                reply_queue = new LinkedBlockingQueue<AltosLine> ();
                line = "";
                monitor_mode = false;
                monitors = new LinkedList<LinkedBlockingQueue<AltosLine>> ();
                reply_queue = new LinkedBlockingQueue<AltosLine> ();
+               open();
        }
 }
        }
 }
index 0b5509ebedb203a5dcc5057972cb815ba3a97fe5..ff02c7225c00762b1516e420edb778d7317fe89b 100644 (file)
@@ -50,9 +50,10 @@ class AltosTelemetryReader extends AltosFlightReader {
                serial.set_callsign(callsign);
        }
 
                serial.set_callsign(callsign);
        }
 
-       public AltosTelemetryReader (AltosDevice in_device) throws FileNotFoundException, IOException {
+       public AltosTelemetryReader (AltosDevice in_device)
+               throws FileNotFoundException, AltosSerialInUseException, IOException {
                device = in_device;
                device = in_device;
-               serial = new AltosSerial();
+               serial = new AltosSerial(device);
                log = new AltosLog(serial);
                name = device.getPath();
 
                log = new AltosLog(serial);
                name = device.getPath();
 
index 9ab451defca7847e24354195809dad779f2b8231..0d8f0e8d30d48bb6ca89c09e47a555495bd37929 100644 (file)
@@ -56,6 +56,12 @@ public class AltosUI extends JFrame {
                                                                    device.getPath()),
                                                      "Cannot open target device",
                                                      JOptionPane.ERROR_MESSAGE);
                                                                    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(),
                } catch (IOException ee) {
                        JOptionPane.showMessageDialog(AltosUI.this,
                                                      device.getPath(),
index 267bae63b85ae8c7153b0e84fe84ba794800db42..f4c743dfa8158aca9e72b6f0bbbd077462cb7a5a 100644 (file)
@@ -58,6 +58,7 @@ altosui_JAVA = \
        AltosRomconfig.java \
        AltosRomconfigUI.java \
        AltosSerial.java \
        AltosRomconfig.java \
        AltosRomconfigUI.java \
        AltosSerial.java \
+       AltosSerialInUseException.java \
        AltosSerialMonitor.java \
        AltosState.java \
        AltosStatusTable.java \
        AltosSerialMonitor.java \
        AltosState.java \
        AltosStatusTable.java \