altosui: Abstract remote connection timeout stuff
[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 void run () {
57                 int c;
58                 byte[] line_bytes = null;
59                 int line_count = 0;
60
61                 try {
62                         for (;;) {
63                                 c = libaltos.altos_getchar(altos, 0);
64                                 if (Thread.interrupted())
65                                         break;
66                                 if (c == libaltosConstants.LIBALTOS_ERROR) {
67                                         add_telem (new AltosLine());
68                                         add_reply (new AltosLine());
69                                         break;
70                                 }
71                                 if (c == libaltosConstants.LIBALTOS_TIMEOUT)
72                                         continue;
73                                 if (c == '\r')
74                                         continue;
75                                 synchronized(this) {
76                                         if (c == '\n') {
77                                                 if (line_count != 0) {
78                                                         add_bytes(line_bytes, line_count);
79                                                         line_count = 0;
80                                                 }
81                                         } else {
82                                                 if (line_bytes == null) {
83                                                         line_bytes = new byte[256];
84                                                 } else if (line_count == line_bytes.length) {
85                                                         byte[] new_line_bytes = new byte[line_count * 2];
86                                                         System.arraycopy(line_bytes, 0, new_line_bytes, 0, line_count);
87                                                         line_bytes = new_line_bytes;
88                                                 }
89                                                 line_bytes[line_count] = (byte) c;
90                                                 line_count++;
91                                         }
92                                 }
93                         }
94                 } catch (InterruptedException e) {
95                 }
96         }
97
98         public void flush_output() {
99                 super.flush_output();
100                 if (altos != null) {
101                         libaltos.altos_flush(altos);
102                 }
103         }
104
105         JDialog         timeout_dialog;
106
107         private void start_timeout_dialog_internal() {
108
109                 Object[] options = { "Cancel" };
110
111                 JOptionPane     pane = new JOptionPane();
112                 pane.setMessage(String.format("Connecting to %s, %7.3f MHz", device.toShortString(), frequency));
113                 pane.setOptions(options);
114                 pane.setInitialValue(null);
115
116                 timeout_dialog = pane.createDialog(frame, "Connecting...");
117
118                 timeout_dialog.setVisible(true);
119
120                 Object o = pane.getValue();
121                 if (o == null)
122                         return;
123                 if (options[0].equals(o))
124                         reply_abort = true;
125                 timeout_dialog.dispose();
126                 timeout_dialog = null;
127         }
128
129         /*
130          * These are required by the AltosLink implementation
131          */
132
133         public boolean can_cancel_reply() {
134                 /*
135                  * Can cancel any replies not called from the dispatch thread
136                  */
137                 return !SwingUtilities.isEventDispatchThread();
138         }
139
140         public boolean show_reply_timeout() {
141                 if (!SwingUtilities.isEventDispatchThread() && frame != null) {
142                         Runnable r = new Runnable() {
143                                         public void run() {
144                                                 start_timeout_dialog_internal();
145                                         }
146                                 };
147                         SwingUtilities.invokeLater(r);
148                         return true;
149                 }
150                 return false;
151         }
152
153         public void hide_reply_timeout() {
154                 Runnable r = new Runnable() {
155                                 public void run() {
156                                         timeout_dialog.setVisible(false);
157                                 }
158                         };
159                 SwingUtilities.invokeLater(r);
160         }
161
162         public void close() {
163                 if (remote) {
164                         try {
165                                 stop_remote();
166                         } catch (InterruptedException ie) {
167                         }
168                 }
169                 if (in_reply != 0)
170                         System.out.printf("Uh-oh. Closing active serial device\n");
171
172                 if (altos != null) {
173                         libaltos.altos_close(altos);
174                 }
175                 if (input_thread != null) {
176                         try {
177                                 input_thread.interrupt();
178                                 input_thread.join();
179                         } catch (InterruptedException e) {
180                         }
181                         input_thread = null;
182                 }
183                 if (altos != null) {
184                         libaltos.altos_free(altos);
185                         altos = null;
186                 }
187                 synchronized (devices_opened) {
188                         devices_opened.remove(device.getPath());
189                 }
190                 if (debug)
191                         System.out.printf("Closing %s\n", device.getPath());
192         }
193
194         private void putc(char c) {
195                 if (altos != null)
196                         libaltos.altos_putchar(altos, c);
197         }
198
199         public void print(String data) {
200                 for (int i = 0; i < data.length(); i++)
201                         putc(data.charAt(i));
202         }
203
204         private void open() throws FileNotFoundException, AltosSerialInUseException {
205                 synchronized (devices_opened) {
206                         if (devices_opened.contains(device.getPath()))
207                                 throw new AltosSerialInUseException(device);
208                         devices_opened.add(device.getPath());
209                 }
210                 altos = device.open();
211                 if (altos == null) {
212                         final String    message = device.getErrorString();
213                         close();
214                         throw new FileNotFoundException(String.format("%s (%s)",
215                                                                       device.toShortString(), message));
216                 }
217                 if (debug)
218                         System.out.printf("Open %s\n", device.getPath());
219                 input_thread = new Thread(this);
220                 input_thread.start();
221                 print("~\nE 0\n");
222                 set_monitor(false);
223                 flush_output();
224         }
225
226         public void set_frame(Frame in_frame) {
227                 frame = in_frame;
228         }
229
230         public AltosSerial(AltosDevice in_device) throws FileNotFoundException, AltosSerialInUseException {
231                 device = in_device;
232                 frame = null;
233                 serial = device.getSerial();
234                 name = device.toShortString();
235                 open();
236         }
237 }