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 if (!remote || !can_cancel || check_timeout()) {
249 public void add_monitor(LinkedBlockingQueue<AltosLine> q) {
254 public void remove_monitor(LinkedBlockingQueue<AltosLine> q) {
256 if (monitors.isEmpty())
260 public void close() {
262 System.out.printf("Uh-oh. Closing active serial device\n");
265 libaltos.altos_close(altos);
267 if (input_thread != null) {
269 input_thread.interrupt();
271 } catch (InterruptedException e) {
276 libaltos.altos_free(altos);
279 synchronized (devices_opened) {
280 devices_opened.remove(device.getPath());
283 System.out.printf("Closing %s\n", device.getPath());
286 private void putc(char c) {
288 libaltos.altos_putchar(altos, c);
291 public void print(String data) {
293 pending_output.add(data);
294 for (int i = 0; i < data.length(); i++)
295 putc(data.charAt(i));
298 public void printf(String format, Object ... arguments) {
299 print(String.format(format, arguments));
302 private void open() throws FileNotFoundException, AltosSerialInUseException {
303 synchronized (devices_opened) {
304 if (devices_opened.contains(device.getPath()))
305 throw new AltosSerialInUseException(device);
306 devices_opened.add(device.getPath());
308 altos = device.open();
311 throw new FileNotFoundException(device.toShortString());
314 System.out.printf("Open %s\n", device.getPath());
315 input_thread = new Thread(this);
316 input_thread.start();
322 public void set_radio() {
323 telemetry = AltosPreferences.telemetry(device.getSerial());
324 channel = AltosPreferences.channel(device.getSerial());
325 set_channel(channel);
326 set_callsign(AltosPreferences.callsign());
329 private int telemetry_len() {
330 return Altos.telemetry_len(telemetry);
333 public void set_channel(int in_channel) {
334 channel = in_channel;
337 printf("m 0\nc r %d\nm %x\n",
338 channel, telemetry_len());
340 printf("c r %d\n", channel);
345 public void set_telemetry(int in_telemetry) {
346 telemetry = in_telemetry;
349 printf("m 0\nm %x\n", telemetry_len());
354 void set_monitor(boolean monitor) {
355 monitor_mode = monitor;
358 printf("m %x\n", telemetry_len());
365 public void set_callsign(String callsign) {
367 printf ("c c %s\n", callsign);
372 public void start_remote() {
374 System.out.printf("start remote\n");
381 public void stop_remote() {
383 System.out.printf("stop remote\n");
393 public void set_frame(Frame in_frame) {
397 public AltosSerial(AltosDevice in_device) throws FileNotFoundException, AltosSerialInUseException {
400 monitor_mode = false;
402 telemetry = Altos.ao_telemetry_standard;
403 monitors = new LinkedList<LinkedBlockingQueue<AltosLine>> ();
404 reply_queue = new LinkedBlockingQueue<AltosLine> ();