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 import org.altusmetrum.altosuilib.*;
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                                 libaltos.altos_close(altos);
62                                 altos = null;
63                                 abort_reply();
64                         }
65                 }
66         }
67
68         JDialog         timeout_dialog;
69
70         private void start_timeout_dialog_internal() {
71
72                 Object[] options = { "Cancel" };
73
74                 JOptionPane     pane = new JOptionPane();
75                 pane.setMessage(String.format("Connecting to %s, %7.3f MHz", device.toShortString(), frequency));
76                 pane.setOptions(options);
77                 pane.setInitialValue(null);
78
79                 timeout_dialog = pane.createDialog(frame, "Connecting...");
80
81                 timeout_dialog.setVisible(true);
82
83                 Object o = pane.getValue();
84                 if (o == null)
85                         return;
86                 if (options[0].equals(o))
87                         reply_abort = true;
88                 timeout_dialog.dispose();
89                 timeout_dialog = null;
90         }
91
92         /*
93          * These are required by the AltosLink implementation
94          */
95
96         public boolean can_cancel_reply() {
97                 /*
98                  * Can cancel any replies not called from the dispatch thread
99                  */
100                 return !SwingUtilities.isEventDispatchThread();
101         }
102
103         public boolean show_reply_timeout() {
104                 if (!SwingUtilities.isEventDispatchThread() && frame != null) {
105                         Runnable r = new Runnable() {
106                                         public void run() {
107                                                 start_timeout_dialog_internal();
108                                         }
109                                 };
110                         SwingUtilities.invokeLater(r);
111                         return true;
112                 }
113                 return false;
114         }
115
116         public void hide_reply_timeout() {
117                 Runnable r = new Runnable() {
118                                 public void run() {
119                                         timeout_dialog.setVisible(false);
120                                 }
121                         };
122                 SwingUtilities.invokeLater(r);
123         }
124
125         public void close() {
126                 if (remote) {
127                         try {
128                                 stop_remote();
129                         } catch (InterruptedException ie) {
130                         }
131                 }
132                 if (in_reply != 0)
133                         System.out.printf("Uh-oh. Closing active serial device\n");
134
135                 if (altos != null) {
136                         libaltos.altos_close(altos);
137                 }
138                 if (input_thread != null) {
139                         try {
140                                 input_thread.interrupt();
141                                 input_thread.join();
142                         } catch (InterruptedException e) {
143                         }
144                         input_thread = null;
145                 }
146                 if (altos != null) {
147                         libaltos.altos_free(altos);
148                         altos = null;
149                 }
150                 synchronized (devices_opened) {
151                         devices_opened.remove(device.getPath());
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                                 libaltos.altos_close(altos);
161                                 altos = null;
162                                 abort_reply();
163                         }
164         }
165
166         public void print(String data) {
167                 for (int i = 0; i < data.length(); i++)
168                         putc(data.charAt(i));
169         }
170
171         private void open() throws FileNotFoundException, AltosSerialInUseException {
172                 synchronized (devices_opened) {
173                         if (devices_opened.contains(device.getPath()))
174                                 throw new AltosSerialInUseException(device);
175                         devices_opened.add(device.getPath());
176                 }
177                 altos = device.open();
178                 if (altos == null) {
179                         final String    message = device.getErrorString();
180                         close();
181                         throw new FileNotFoundException(String.format("%s (%s)",
182                                                                       device.toShortString(), message));
183                 }
184                 if (debug)
185                         System.out.printf("Open %s\n", device.getPath());
186                 input_thread = new Thread(this);
187                 input_thread.start();
188                 print("~\nE 0\n");
189                 set_monitor(false);
190                 flush_output();
191         }
192
193         public void set_frame(Frame in_frame) {
194                 frame = in_frame;
195         }
196
197         public AltosSerial(AltosDevice in_device) throws FileNotFoundException, AltosSerialInUseException {
198                 device = in_device;
199                 frame = null;
200                 serial = device.getSerial();
201                 name = device.toShortString();
202                 open();
203         }
204 }