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