2 * Copyright © 2010 Keith Packard <keithp@keithp.com>
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.
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.
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.
19 * Deal with TeleDongle on a serial port
26 import java.util.concurrent.*;
30 import java.awt.event.*;
32 import javax.swing.filechooser.FileNameExtensionFilter;
33 import javax.swing.table.*;
38 * This class reads from the serial port and places each received
39 * line in a queue. Dealing with that queue is left up to other
43 public class AltosSerial implements Runnable {
45 static java.util.List<String> devices_opened = Collections.synchronizedList(new LinkedList<String>());
48 SWIGTYPE_p_altos_file altos;
49 LinkedList<LinkedBlockingQueue<AltosLine>> monitors;
50 LinkedBlockingQueue<AltosLine> reply_queue;
60 LinkedList<String> pending_output = new LinkedList<String>();
63 static void set_debug(boolean new_debug) {
72 c = libaltos.altos_getchar(altos, 0);
73 if (Thread.interrupted())
75 if (c == libaltosConstants.LIBALTOS_ERROR) {
76 for (int e = 0; e < monitors.size(); e++) {
77 LinkedBlockingQueue<AltosLine> q = monitors.get(e);
78 q.put(new AltosLine());
80 reply_queue.put (new AltosLine());
83 if (c == libaltosConstants.LIBALTOS_TIMEOUT)
89 if (line_count != 0) {
91 line = new String(line_bytes, 0, line_count, "UTF-8");
92 } catch (UnsupportedEncodingException ue) {
94 for (int i = 0; i < line_count; i++)
95 line = line + line_bytes[i];
98 System.out.printf("\t\t\t\t\t%s\n", line);
99 if (line.startsWith("TELEM") || line.startsWith("VERSION") || line.startsWith("CRC")) {
100 for (int e = 0; e < monitors.size(); e++) {
101 LinkedBlockingQueue<AltosLine> q = monitors.get(e);
102 q.put(new AltosLine (line));
105 reply_queue.put(new AltosLine (line));
111 if (line_bytes == null) {
112 line_bytes = new byte[256];
113 } else if (line_count == line_bytes.length) {
114 byte[] new_line_bytes = new byte[line_count * 2];
115 System.arraycopy(line_bytes, 0, new_line_bytes, 0, line_count);
116 line_bytes = new_line_bytes;
118 line_bytes[line_count] = (byte) c;
123 } catch (InterruptedException e) {
127 public void flush_output() {
129 for (String s : pending_output)
131 pending_output.clear();
132 libaltos.altos_flush(altos);
137 JDialog timeout_dialog;
138 boolean timeout_started = false;
140 private void stop_timeout_dialog() {
141 if (timeout_started) {
142 timeout_started = false;
143 Runnable r = new Runnable() {
145 timeout_dialog.setVisible(false);
148 SwingUtilities.invokeLater(r);
152 private void start_timeout_dialog_internal() {
154 Object[] options = { "Cancel" };
156 JOptionPane pane = new JOptionPane();
157 pane.setMessage(String.format("Connecting to %s", device.getPath()));
158 pane.setOptions(options);
159 pane.setInitialValue(null);
161 timeout_dialog = pane.createDialog(frame, "Connecting...");
163 timeout_dialog.setVisible(true);
165 Object o = pane.getValue();
168 if (options[0].equals(o))
170 timeout_dialog.dispose();
171 timeout_dialog = null;
174 private boolean check_timeout() {
175 if (!timeout_started && frame != null) {
176 if (!SwingUtilities.isEventDispatchThread()) {
177 timeout_started = true;
178 Runnable r = new Runnable() {
180 start_timeout_dialog_internal();
183 SwingUtilities.invokeLater(r);
189 public void flush_input() {
198 Thread.sleep(timeout);
199 } catch (InterruptedException ie) {
201 got_some = !reply_queue.isEmpty();
203 if (!"VERSION".startsWith(line) &&
204 !line.startsWith("VERSION"))
211 public String get_reply() throws InterruptedException {
212 if (SwingUtilities.isEventDispatchThread())
213 System.out.printf("Uh-oh, reading serial device from swing thread\n");
215 AltosLine line = reply_queue.take();
221 public String get_reply(int timeout) throws InterruptedException {
222 boolean can_cancel = true;
225 if (SwingUtilities.isEventDispatchThread()) {
227 System.out.printf("Uh-oh, reading serial device from swing thread\n");
230 if (remote && can_cancel) {
234 timeout_started = false;
236 AltosLine line = reply_queue.poll(timeout, TimeUnit.MILLISECONDS);
238 stop_timeout_dialog();
242 System.out.printf("no line remote %b can_cancel %b\n", remote, can_cancel);
243 if (!remote || !can_cancel || check_timeout()) {
250 public String get_reply_no_dialog(int timeout) throws InterruptedException, TimeoutException {
252 AltosLine line = reply_queue.poll(timeout, TimeUnit.MILLISECONDS);
258 public void add_monitor(LinkedBlockingQueue<AltosLine> q) {
263 public void remove_monitor(LinkedBlockingQueue<AltosLine> q) {
265 if (monitors.isEmpty())
269 public void close() {
271 System.out.printf("Uh-oh. Closing active serial device\n");
274 libaltos.altos_close(altos);
276 if (input_thread != null) {
278 input_thread.interrupt();
280 } catch (InterruptedException e) {
285 libaltos.altos_free(altos);
288 synchronized (devices_opened) {
289 devices_opened.remove(device.getPath());
292 System.out.printf("Closing %s\n", device.getPath());
295 private void putc(char c) {
297 libaltos.altos_putchar(altos, c);
300 public void print(String data) {
302 pending_output.add(data);
303 for (int i = 0; i < data.length(); i++)
304 putc(data.charAt(i));
307 public void printf(String format, Object ... arguments) {
308 print(String.format(format, arguments));
311 private void open() throws FileNotFoundException, AltosSerialInUseException {
312 synchronized (devices_opened) {
313 if (devices_opened.contains(device.getPath()))
314 throw new AltosSerialInUseException(device);
315 devices_opened.add(device.getPath());
317 altos = device.open();
320 throw new FileNotFoundException(device.toShortString());
323 System.out.printf("Open %s\n", device.getPath());
324 input_thread = new Thread(this);
325 input_thread.start();
331 public void set_radio() {
332 telemetry = AltosPreferences.telemetry(device.getSerial());
333 channel = AltosPreferences.channel(device.getSerial());
334 set_channel(channel);
335 set_callsign(AltosPreferences.callsign());
338 private int telemetry_len() {
339 return Altos.telemetry_len(telemetry);
342 public void set_channel(int in_channel) {
343 channel = in_channel;
346 printf("m 0\nc r %d\nm %x\n",
347 channel, telemetry_len());
349 printf("c r %d\n", channel);
354 public void set_telemetry(int in_telemetry) {
355 telemetry = in_telemetry;
358 printf("m 0\nm %x\n", telemetry_len());
363 void set_monitor(boolean monitor) {
364 monitor_mode = monitor;
367 printf("m %x\n", telemetry_len());
374 public void set_callsign(String callsign) {
376 printf ("c c %s\n", callsign);
381 public void start_remote() {
383 System.out.printf("start remote\n");
390 public void stop_remote() {
392 System.out.printf("stop remote\n");
402 public void set_frame(Frame in_frame) {
406 public AltosSerial(AltosDevice in_device) throws FileNotFoundException, AltosSerialInUseException {
409 monitor_mode = false;
411 telemetry = Altos.ao_telemetry_standard;
412 monitors = new LinkedList<LinkedBlockingQueue<AltosLine>> ();
413 reply_queue = new LinkedBlockingQueue<AltosLine> ();