altosui: Clean up serial close handling
[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.io.*;
25 import java.util.*;
26 import java.awt.*;
27 import javax.swing.*;
28 import org.altusmetrum.altoslib_2.*;
29 import org.altusmetrum.altosuilib_1.*;
30
31 import libaltosJNI.*;
32
33 /*
34  * This class reads from the serial port and places each received
35  * line in a queue. Dealing with that queue is left up to other
36  * threads.
37  */
38
39 public class AltosSerial extends AltosLink  {
40
41         static java.util.List<String> devices_opened = Collections.synchronizedList(new LinkedList<String>());
42
43         AltosDevice device;
44         SWIGTYPE_p_altos_file altos;
45         Thread input_thread;
46         String line;
47         byte[] line_bytes;
48         int line_count;
49         Frame frame;
50
51         public int getchar() {
52                 if (altos == null)
53                         return ERROR;
54                 return libaltos.altos_getchar(altos, 0);
55         }
56
57         public void flush_output() {
58                 super.flush_output();
59                 if (altos != null) {
60                         if (libaltos.altos_flush(altos) != 0)
61                                 close_serial();
62                 }
63         }
64
65         JDialog         timeout_dialog;
66
67         private void start_timeout_dialog_internal() {
68
69                 Object[] options = { "Cancel" };
70
71                 JOptionPane     pane = new JOptionPane();
72                 pane.setMessage(String.format("Connecting to %s, %7.3f MHz as %s", device.toShortString(), frequency, callsign));
73                 pane.setOptions(options);
74                 pane.setInitialValue(null);
75
76                 timeout_dialog = pane.createDialog(frame, "Connecting...");
77
78                 timeout_dialog.setVisible(true);
79
80                 Object o = pane.getValue();
81                 if (o == null)
82                         return;
83                 if (options[0].equals(o))
84                         reply_abort = true;
85                 timeout_dialog.dispose();
86                 timeout_dialog = null;
87         }
88
89         /*
90          * These are required by the AltosLink implementation
91          */
92
93         public boolean can_cancel_reply() {
94                 /*
95                  * Can cancel any replies not called from the dispatch thread
96                  */
97                 return !SwingUtilities.isEventDispatchThread();
98         }
99
100         public boolean show_reply_timeout() {
101                 if (!SwingUtilities.isEventDispatchThread() && frame != null) {
102                         Runnable r = new Runnable() {
103                                         public void run() {
104                                                 start_timeout_dialog_internal();
105                                         }
106                                 };
107                         SwingUtilities.invokeLater(r);
108                         return true;
109                 }
110                 return false;
111         }
112
113         public void hide_reply_timeout() {
114                 Runnable r = new Runnable() {
115                                 public void run() {
116                                         timeout_dialog.setVisible(false);
117                                 }
118                         };
119                 SwingUtilities.invokeLater(r);
120         }
121
122         private void close_serial() {
123                 synchronized (devices_opened) {
124                         devices_opened.remove(device.getPath());
125                 }
126                 if (altos != null) {
127                         libaltos.altos_free(altos);
128                         altos = null;
129                 }
130                 abort_reply();
131         }
132
133         public void close() {
134                 if (remote) {
135                         try {
136                                 stop_remote();
137                         } catch (InterruptedException ie) {
138                         }
139                 }
140                 if (in_reply != 0)
141                         System.out.printf("Uh-oh. Closing active serial device\n");
142
143                 close_serial();
144
145                 if (input_thread != null) {
146                         try {
147                                 input_thread.interrupt();
148                                 input_thread.join();
149                         } catch (InterruptedException e) {
150                         }
151                         input_thread = null;
152                 }
153                 if (debug)
154                         System.out.printf("Closing %s\n", device.getPath());
155         }
156
157         private void putc(char c) {
158                 if (altos != null)
159                         if (libaltos.altos_putchar(altos, c) != 0) {
160                                 close_serial();
161                         }
162         }
163
164         public void print(String data) {
165                 for (int i = 0; i < data.length(); i++)
166                         putc(data.charAt(i));
167         }
168
169         private void open() throws FileNotFoundException, AltosSerialInUseException {
170                 synchronized (devices_opened) {
171                         if (devices_opened.contains(device.getPath()))
172                                 throw new AltosSerialInUseException(device);
173                         devices_opened.add(device.getPath());
174                 }
175                 altos = device.open();
176                 if (altos == null) {
177                         final String    message = device.getErrorString();
178                         close();
179                         throw new FileNotFoundException(String.format("%s (%s)",
180                                                                       device.toShortString(), message));
181                 }
182                 if (debug)
183                         System.out.printf("Open %s\n", device.getPath());
184                 input_thread = new Thread(this);
185                 input_thread.start();
186                 print("~\nE 0\n");
187                 set_monitor(false);
188                 flush_output();
189         }
190
191         public void set_frame(Frame in_frame) {
192                 frame = in_frame;
193         }
194
195         public AltosSerial(AltosDevice in_device) throws FileNotFoundException, AltosSerialInUseException {
196                 device = in_device;
197                 frame = null;
198                 serial = device.getSerial();
199                 name = device.toShortString();
200                 open();
201         }
202 }