libaltos: remove usb id filtering from darwin code
[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 ie) {
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         public void putchar(byte c) {
164                 if (altos != null) {
165                         if (debug)
166                                 System.out.printf(" %02x", (int) c & 0xff);
167                         if (libaltos.altos_putchar(altos, (char) c) != 0)
168                                 close_serial();
169                 }
170         }
171
172         public void print(String data) {
173                 for (int i = 0; i < data.length(); i++)
174                         putc(data.charAt(i));
175         }
176
177         private void open() throws FileNotFoundException, AltosSerialInUseException {
178                 synchronized (devices_opened) {
179                         if (devices_opened.contains(device.getPath()))
180                                 throw new AltosSerialInUseException(device);
181                         devices_opened.add(device.getPath());
182                 }
183                 altos = device.open();
184                 if (altos == null) {
185                         final String    message = device.getErrorString();
186                         close();
187                         throw new FileNotFoundException(String.format("%s (%s)",
188                                                                       device.toShortString(), message));
189                 }
190                 if (debug)
191                         System.out.printf("Open %s\n", device.getPath());
192                 input_thread = new Thread(this);
193                 input_thread.start();
194                 print("~\nE 0\n");
195                 set_monitor(false);
196                 flush_output();
197         }
198
199         public void set_frame(Frame in_frame) {
200                 frame = in_frame;
201         }
202
203         public AltosSerial(AltosDevice in_device) throws FileNotFoundException, AltosSerialInUseException {
204                 device = in_device;
205                 frame = null;
206                 serial = device.getSerial();
207                 name = device.toShortString();
208                 open();
209         }
210 }