2 * Copyright © 2011 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.
18 package org.altusmetrum.AltosLib;
22 import java.util.concurrent.*;
26 public abstract class AltosLink {
28 public final static int ERROR = -1;
29 public final static int TIMEOUT = -2;
31 public abstract int getchar();
32 public abstract void print(String data);
33 public abstract void close();
35 public static boolean debug = false;
36 public static void set_debug(boolean in_debug) { debug = in_debug; }
37 LinkedList<String> pending_output = new LinkedList<String>();
39 public LinkedList<LinkedBlockingQueue<AltosLine>> monitors = new LinkedList<LinkedBlockingQueue<AltosLine>> ();;
40 public LinkedBlockingQueue<AltosLine> reply_queue = new LinkedBlockingQueue<AltosLine>();
42 public void add_monitor(LinkedBlockingQueue<AltosLine> q) {
47 public void remove_monitor(LinkedBlockingQueue<AltosLine> q) {
49 if (monitors.isEmpty())
53 public void printf(String format, Object ... arguments) {
54 String line = String.format(format, arguments);
56 pending_output.add(line);
60 public String get_reply_no_dialog(int timeout) throws InterruptedException, TimeoutException {
62 AltosLine line = reply_queue.poll(timeout, TimeUnit.MILLISECONDS);
68 public String get_reply() throws InterruptedException {
69 return get_reply(5000);
73 public abstract boolean can_cancel_reply();
74 public abstract boolean show_reply_timeout();
75 public abstract void hide_reply_timeout();
77 public boolean reply_abort;
80 boolean reply_timeout_shown = false;
82 private boolean check_reply_timeout() {
83 if (!reply_timeout_shown)
84 reply_timeout_shown = show_reply_timeout();
88 private void cleanup_reply_timeout() {
89 if (reply_timeout_shown) {
90 reply_timeout_shown = false;
98 byte[] line_bytes = null;
104 if (Thread.interrupted())
107 add_telem (new AltosLine());
108 add_reply (new AltosLine());
117 if (line_count != 0) {
118 add_bytes(line_bytes, line_count);
122 if (line_bytes == null) {
123 line_bytes = new byte[256];
124 } else if (line_count == line_bytes.length) {
125 byte[] new_line_bytes = new byte[line_count * 2];
126 System.arraycopy(line_bytes, 0, new_line_bytes, 0, line_count);
127 line_bytes = new_line_bytes;
129 line_bytes[line_count] = (byte) c;
134 } catch (InterruptedException e) {
138 public String get_reply(int timeout) throws InterruptedException {
139 boolean can_cancel = can_cancel_reply();
142 if (!can_cancel && remote)
143 System.out.printf("Uh-oh, reading remote serial device from swing thread\n");
145 if (remote && can_cancel)
153 reply_timeout_shown = false;
155 AltosLine line = reply_queue.poll(timeout, TimeUnit.MILLISECONDS);
157 cleanup_reply_timeout();
161 if (!remote || !can_cancel || check_reply_timeout()) {
172 public void add_telem(AltosLine line) throws InterruptedException {
173 for (int e = 0; e < monitors.size(); e++) {
174 LinkedBlockingQueue<AltosLine> q = monitors.get(e);
179 public void add_reply(AltosLine line) throws InterruptedException {
180 reply_queue.put (line);
183 public void add_string(String line) throws InterruptedException {
184 if (line.startsWith("TELEM") || line.startsWith("VERSION") || line.startsWith("CRC")) {
185 add_telem(new AltosLine(line));
187 add_reply(new AltosLine(line));
191 public void add_bytes(byte[] bytes, int len) throws InterruptedException {
194 line = new String(bytes, 0, len, "UTF-8");
195 } catch (UnsupportedEncodingException ue) {
197 for (int i = 0; i < len; i++)
198 line = line + bytes[i];
201 System.out.printf("\t\t\t\t\t%s\n", line);
205 public void flush_output() {
206 for (String s : pending_output)
208 pending_output.clear();
211 public void flush_input(int timeout) throws InterruptedException {
216 Thread.sleep(timeout);
217 got_some = !reply_queue.isEmpty();
223 public void flush_input() throws InterruptedException {
232 * Various command-level operations on
235 public boolean monitor_mode = false;
236 public int telemetry = AltosLib.ao_telemetry_standard;
237 public double frequency;
238 AltosConfigData config_data;
240 private int telemetry_len() {
241 return AltosLib.telemetry_len(telemetry);
244 private void set_radio_freq(int frequency) {
246 printf("m 0\nc F %d\nm %x\n",
247 frequency, telemetry_len());
249 printf("c F %d\n", frequency);
253 public void set_radio_frequency(double frequency,
254 boolean has_frequency,
258 System.out.printf("set_radio_frequency %7.3f (freq %b) (set %b) %d\n", frequency, has_frequency, has_setting, cal);
262 set_radio_freq((int) Math.floor (frequency * 1000));
263 else if (has_setting)
264 set_radio_setting(AltosConvert.radio_frequency_to_setting(frequency, cal));
266 set_channel(AltosConvert.radio_frequency_to_channel(frequency));
269 public void set_radio_frequency(double in_frequency) throws InterruptedException, TimeoutException {
270 frequency = in_frequency;
272 set_radio_frequency(frequency,
273 config_data.radio_frequency != 0,
274 config_data.radio_setting != 0,
275 config_data.radio_calibration);
278 public void set_telemetry(int in_telemetry) {
279 telemetry = in_telemetry;
281 printf("m 0\nm %x\n", telemetry_len());
285 public void set_monitor(boolean monitor) {
286 monitor_mode = monitor;
288 printf("m %x\n", telemetry_len());
294 private void set_channel(int channel) {
296 printf("m 0\nc r %d\nm %x\n",
297 channel, telemetry_len());
299 printf("c r %d\n", channel);
303 private void set_radio_setting(int setting) {
305 printf("m 0\nc R %d\nm %x\n",
306 setting, telemetry_len());
308 printf("c R %d\n", setting);
312 public AltosConfigData config_data() throws InterruptedException, TimeoutException {
313 if (config_data == null)
314 config_data = new AltosConfigData(this);
318 public void set_callsign(String callsign) {
319 printf ("c c %s\n", callsign);
323 public boolean remote;
327 public void start_remote() throws TimeoutException, InterruptedException {
329 System.out.printf("start remote %7.3f\n", frequency);
330 if (frequency == 0.0)
331 frequency = AltosPreferences.frequency(serial);
332 set_radio_frequency(frequency);
333 set_callsign(AltosPreferences.callsign());
339 public void stop_remote() throws InterruptedException {
341 System.out.printf("stop remote\n");