altoslib: Support binary reading/writing in AltosLink
[fw/altos] / altosui / AltosSerial.java
index 8b692fa909cc36e3c06ed82be0ee8241e9df29f3..b85a7fa1d4bf3e19984290d9cab4a0180a40006f 100644 (file)
 
 package altosui;
 
-import java.lang.*;
 import java.io.*;
-import java.util.concurrent.*;
 import java.util.*;
-import java.text.*;
 import java.awt.*;
-import java.awt.event.*;
 import javax.swing.*;
-import javax.swing.filechooser.FileNameExtensionFilter;
-import javax.swing.table.*;
-import org.altusmetrum.AltosLib.*;
+import org.altusmetrum.altoslib_2.*;
+import org.altusmetrum.altosuilib_1.*;
 
 import libaltosJNI.*;
 
@@ -41,7 +36,7 @@ import libaltosJNI.*;
  * threads.
  */
 
-public class AltosSerial extends AltosLink implements Runnable {
+public class AltosSerial extends AltosLink  {
 
        static java.util.List<String> devices_opened = Collections.synchronizedList(new LinkedList<String>());
 
@@ -62,11 +57,8 @@ public class AltosSerial extends AltosLink implements Runnable {
        public void flush_output() {
                super.flush_output();
                if (altos != null) {
-                       if (libaltos.altos_flush(altos) != 0) {
-                               libaltos.altos_close(altos);
-                               altos = null;
-                               abort_reply();
-                       }
+                       if (libaltos.altos_flush(altos) != 0)
+                               close_serial();
                }
        }
 
@@ -77,7 +69,7 @@ public class AltosSerial extends AltosLink implements Runnable {
                Object[] options = { "Cancel" };
 
                JOptionPane     pane = new JOptionPane();
-               pane.setMessage(String.format("Connecting to %s, %7.3f MHz", device.toShortString(), frequency));
+               pane.setMessage(String.format("Connecting to %s, %7.3f MHz as %s", device.toShortString(), frequency, callsign));
                pane.setOptions(options);
                pane.setInitialValue(null);
 
@@ -127,6 +119,17 @@ public class AltosSerial extends AltosLink implements Runnable {
                SwingUtilities.invokeLater(r);
        }
 
+       private void close_serial() {
+               synchronized (devices_opened) {
+                       devices_opened.remove(device.getPath());
+               }
+               if (altos != null) {
+                       libaltos.altos_free(altos);
+                       altos = null;
+               }
+               abort_reply();
+       }
+
        public void close() {
                if (remote) {
                        try {
@@ -137,9 +140,8 @@ public class AltosSerial extends AltosLink implements Runnable {
                if (in_reply != 0)
                        System.out.printf("Uh-oh. Closing active serial device\n");
 
-               if (altos != null) {
-                       libaltos.altos_close(altos);
-               }
+               close_serial();
+
                if (input_thread != null) {
                        try {
                                input_thread.interrupt();
@@ -148,13 +150,6 @@ public class AltosSerial extends AltosLink implements Runnable {
                        }
                        input_thread = null;
                }
-               if (altos != null) {
-                       libaltos.altos_free(altos);
-                       altos = null;
-               }
-               synchronized (devices_opened) {
-                       devices_opened.remove(device.getPath());
-               }
                if (debug)
                        System.out.printf("Closing %s\n", device.getPath());
        }
@@ -162,10 +157,18 @@ public class AltosSerial extends AltosLink implements Runnable {
        private void putc(char c) {
                if (altos != null)
                        if (libaltos.altos_putchar(altos, c) != 0) {
-                               libaltos.altos_close(altos);
-                               altos = null;
-                               abort_reply();
+                               close_serial();
+                       }
+       }
+
+       public void putchar(byte c) {
+               if (altos != null) {
+                       if (debug)
+                               System.out.printf(" %02x", (int) c & 0xff);
+                       if (libaltos.altos_putchar(altos, (char) c) != 0) {
+                               close_serial();
                        }
+               }
        }
 
        public void print(String data) {