Merge branch 'master' of ssh://git.gag.com/scm/git/fw/altos
[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.*;
29
30 import libaltosJNI.*;
31
32 /*
33  * This class reads from the serial port and places each received
34  * line in a queue. Dealing with that queue is left up to other
35  * threads.
36  */
37
38 public class AltosSerial extends AltosLink  {
39
40         static java.util.List<String> devices_opened = Collections.synchronizedList(new LinkedList<String>());
41
42         AltosDevice device;
43         SWIGTYPE_p_altos_file altos;
44         Thread input_thread;
45         String line;
46         byte[] line_bytes;
47         int line_count;
48         Frame frame;
49
50         public int getchar() {
51                 if (altos == null)
52                         return ERROR;
53                 return libaltos.altos_getchar(altos, 0);
54         }
55
56         public void flush_output() {
57                 super.flush_output();
58                 if (altos != null) {
59                         if (libaltos.altos_flush(altos) != 0) {
60                                 libaltos.altos_close(altos);
61                                 altos = null;
62                                 abort_reply();
63                         }
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                         if (libaltos.altos_putchar(altos, c) != 0) {
159                                 libaltos.altos_close(altos);
160                                 altos = null;
161                                 abort_reply();
162                         }
163         }
164
165         public void print(String data) {
166                 for (int i = 0; i < data.length(); i++)
167                         putc(data.charAt(i));
168         }
169
170         private void open() throws FileNotFoundException, AltosSerialInUseException {
171                 synchronized (devices_opened) {
172                         if (devices_opened.contains(device.getPath()))
173                                 throw new AltosSerialInUseException(device);
174                         devices_opened.add(device.getPath());
175                 }
176                 altos = device.open();
177                 if (altos == null) {
178                         final String    message = device.getErrorString();
179                         close();
180                         throw new FileNotFoundException(String.format("%s (%s)",
181                                                                       device.toShortString(), message));
182                 }
183                 if (debug)
184                         System.out.printf("Open %s\n", device.getPath());
185                 input_thread = new Thread(this);
186                 input_thread.start();
187                 print("~\nE 0\n");
188                 set_monitor(false);
189                 flush_output();
190         }
191
192         public void set_frame(Frame in_frame) {
193                 frame = in_frame;
194         }
195
196         public AltosSerial(AltosDevice in_device) throws FileNotFoundException, AltosSerialInUseException {
197                 device = in_device;
198                 frame = null;
199                 serial = device.getSerial();
200                 name = device.toShortString();
201                 open();
202         }
203 }