altosui: Move serial datastream parser to altoslib
[fw/altos] / altosui / AltosSerial.java
1 /*
2  * Copyright © 2010 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 /*
19  * Deal with TeleDongle on a serial port
20  */
21
22 package altosui;
23
24 import java.lang.*;
25 import java.io.*;
26 import java.util.concurrent.*;
27 import java.util.*;
28 import java.text.*;
29 import java.awt.*;
30 import java.awt.event.*;
31 import javax.swing.*;
32 import javax.swing.filechooser.FileNameExtensionFilter;
33 import javax.swing.table.*;
34 import org.altusmetrum.AltosLib.*;
35
36 import libaltosJNI.*;
37
38 /*
39  * This class reads from the serial port and places each received
40  * line in a queue. Dealing with that queue is left up to other
41  * threads.
42  */
43
44 public class AltosSerial extends AltosLink implements Runnable {
45
46         static java.util.List<String> devices_opened = Collections.synchronizedList(new LinkedList<String>());
47
48         AltosDevice device;
49         SWIGTYPE_p_altos_file altos;
50         Thread input_thread;
51         String line;
52         byte[] line_bytes;
53         int line_count;
54         Frame frame;
55
56         public int getchar() {
57                 return libaltos.altos_getchar(altos, 0);
58         }
59
60         public void flush_output() {
61                 super.flush_output();
62                 if (altos != null) {
63                         libaltos.altos_flush(altos);
64                 }
65         }
66
67         JDialog         timeout_dialog;
68
69         private void start_timeout_dialog_internal() {
70
71                 Object[] options = { "Cancel" };
72
73                 JOptionPane     pane = new JOptionPane();
74                 pane.setMessage(String.format("Connecting to %s, %7.3f MHz", device.toShortString(), frequency));
75                 pane.setOptions(options);
76                 pane.setInitialValue(null);
77
78                 timeout_dialog = pane.createDialog(frame, "Connecting...");
79
80                 timeout_dialog.setVisible(true);
81
82                 Object o = pane.getValue();
83                 if (o == null)
84                         return;
85                 if (options[0].equals(o))
86                         reply_abort = true;
87                 timeout_dialog.dispose();
88                 timeout_dialog = null;
89         }
90
91         /*
92          * These are required by the AltosLink implementation
93          */
94
95         public boolean can_cancel_reply() {
96                 /*
97                  * Can cancel any replies not called from the dispatch thread
98                  */
99                 return !SwingUtilities.isEventDispatchThread();
100         }
101
102         public boolean show_reply_timeout() {
103                 if (!SwingUtilities.isEventDispatchThread() && frame != null) {
104                         Runnable r = new Runnable() {
105                                         public void run() {
106                                                 start_timeout_dialog_internal();
107                                         }
108                                 };
109                         SwingUtilities.invokeLater(r);
110                         return true;
111                 }
112                 return false;
113         }
114
115         public void hide_reply_timeout() {
116                 Runnable r = new Runnable() {
117                                 public void run() {
118                                         timeout_dialog.setVisible(false);
119                                 }
120                         };
121                 SwingUtilities.invokeLater(r);
122         }
123
124         public void close() {
125                 if (remote) {
126                         try {
127                                 stop_remote();
128                         } catch (InterruptedException ie) {
129                         }
130                 }
131                 if (in_reply != 0)
132                         System.out.printf("Uh-oh. Closing active serial device\n");
133
134                 if (altos != null) {
135                         libaltos.altos_close(altos);
136                 }
137                 if (input_thread != null) {
138                         try {
139                                 input_thread.interrupt();
140                                 input_thread.join();
141                         } catch (InterruptedException e) {
142                         }
143                         input_thread = null;
144                 }
145                 if (altos != null) {
146                         libaltos.altos_free(altos);
147                         altos = null;
148                 }
149                 synchronized (devices_opened) {
150                         devices_opened.remove(device.getPath());
151                 }
152                 if (debug)
153                         System.out.printf("Closing %s\n", device.getPath());
154         }
155
156         private void putc(char c) {
157                 if (altos != null)
158                         libaltos.altos_putchar(altos, c);
159         }
160
161         public void print(String data) {
162                 for (int i = 0; i < data.length(); i++)
163                         putc(data.charAt(i));
164         }
165
166         private void open() throws FileNotFoundException, AltosSerialInUseException {
167                 synchronized (devices_opened) {
168                         if (devices_opened.contains(device.getPath()))
169                                 throw new AltosSerialInUseException(device);
170                         devices_opened.add(device.getPath());
171                 }
172                 altos = device.open();
173                 if (altos == null) {
174                         final String    message = device.getErrorString();
175                         close();
176                         throw new FileNotFoundException(String.format("%s (%s)",
177                                                                       device.toShortString(), message));
178                 }
179                 if (debug)
180                         System.out.printf("Open %s\n", device.getPath());
181                 input_thread = new Thread(this);
182                 input_thread.start();
183                 print("~\nE 0\n");
184                 set_monitor(false);
185                 flush_output();
186         }
187
188         public void set_frame(Frame in_frame) {
189                 frame = in_frame;
190         }
191
192         public AltosSerial(AltosDevice in_device) throws FileNotFoundException, AltosSerialInUseException {
193                 device = in_device;
194                 frame = null;
195                 serial = device.getSerial();
196                 name = device.toShortString();
197                 open();
198         }
199 }