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