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_4;
21 import java.util.concurrent.*;
24 public abstract class AltosLink implements Runnable {
26 public final static int ERROR = -1;
27 public final static int TIMEOUT = -2;
29 public abstract int getchar() throws InterruptedException;
30 public abstract void print(String data) throws InterruptedException;
31 public abstract void putchar(byte c);
32 public abstract void close() throws InterruptedException;
34 public static boolean debug = false;
35 public static void set_debug(boolean in_debug) { debug = in_debug; }
37 public boolean has_error;
39 LinkedList<String> pending_output = new LinkedList<String>();
41 public LinkedList<LinkedBlockingQueue<AltosLine>> monitors = new LinkedList<LinkedBlockingQueue<AltosLine>> ();;
42 public LinkedBlockingQueue<AltosLine> reply_queue = new LinkedBlockingQueue<AltosLine>();
43 public LinkedBlockingQueue<byte[]> binary_queue = new LinkedBlockingQueue<byte[]>();
45 public synchronized void add_monitor(LinkedBlockingQueue<AltosLine> q) {
50 public synchronized void remove_monitor(LinkedBlockingQueue<AltosLine> q) {
52 if (monitors.isEmpty())
56 public void printf(String format, Object ... arguments) {
57 String line = String.format(format, arguments);
59 pending_output.add(line);
62 } catch (InterruptedException ie) {
67 public String get_reply_no_dialog(int timeout) throws InterruptedException, TimeoutException {
69 AltosLine line = reply_queue.poll(timeout, TimeUnit.MILLISECONDS);
75 public String get_reply() throws InterruptedException {
76 return get_reply(5000);
80 public abstract boolean can_cancel_reply();
81 public abstract boolean show_reply_timeout();
82 public abstract void hide_reply_timeout();
84 public boolean reply_abort;
87 boolean reply_timeout_shown = false;
89 private boolean check_reply_timeout() {
90 if (!reply_timeout_shown)
91 reply_timeout_shown = show_reply_timeout();
95 private void cleanup_reply_timeout() {
96 if (reply_timeout_shown) {
97 reply_timeout_shown = false;
102 private int len_read = 0;
106 byte[] line_bytes = null;
112 if (Thread.interrupted()) {
117 System.out.printf("ERROR\n");
119 add_telem (new AltosLine());
120 add_reply (new AltosLine());
125 System.out.printf("TIMEOUT\n");
128 if (c == '\r' && len_read == 0)
131 if (c == '\n' && len_read == 0) {
132 if (line_count != 0) {
133 add_bytes(line_bytes, line_count);
137 if (line_bytes == null) {
138 line_bytes = new byte[256];
139 } else if (line_count == line_bytes.length) {
140 byte[] new_line_bytes = new byte[line_count * 2];
141 System.arraycopy(line_bytes, 0, new_line_bytes, 0, line_count);
142 line_bytes = new_line_bytes;
144 line_bytes[line_count] = (byte) c;
146 if (len_read !=0 && line_count == len_read) {
147 add_binary(line_bytes, line_count);
154 } catch (InterruptedException e) {
159 public String get_reply(int timeout) throws InterruptedException {
160 boolean can_cancel = can_cancel_reply();
163 if (!can_cancel && remote)
164 System.out.printf("Uh-oh, reading remote serial device from swing thread\n");
166 if (remote && can_cancel)
174 reply_timeout_shown = false;
176 AltosLine line = reply_queue.poll(timeout, TimeUnit.MILLISECONDS);
178 cleanup_reply_timeout();
182 if (!remote || !can_cancel || check_reply_timeout()) {
193 public byte[] get_binary_reply(int timeout, int len) throws InterruptedException {
194 boolean can_cancel = can_cancel_reply();
206 reply_timeout_shown = false;
208 bytes = binary_queue.poll(timeout, TimeUnit.MILLISECONDS);
210 cleanup_reply_timeout();
213 if (!remote || !can_cancel || check_reply_timeout()) {
225 public void add_telem(AltosLine line) throws InterruptedException {
226 for (int e = 0; e < monitors.size(); e++) {
227 LinkedBlockingQueue<AltosLine> q = monitors.get(e);
232 public void add_reply(AltosLine line) throws InterruptedException {
233 reply_queue.put (line);
236 public void abort_reply() {
238 add_telem (new AltosLine());
239 add_reply (new AltosLine());
240 } catch (InterruptedException ie) {
244 public void add_string(String line) throws InterruptedException {
245 if (line.startsWith("TELEM") || line.startsWith("VERSION") || line.startsWith("CRC")) {
246 add_telem(new AltosLine(line));
248 add_reply(new AltosLine(line));
252 public void add_bytes(byte[] bytes, int len) throws InterruptedException {
254 line = new String(bytes, 0, len, AltosLib.unicode_set);
256 System.out.printf("\t\t\t\t\t%s\n", line);
260 public void add_binary(byte[] bytes, int len) throws InterruptedException {
261 byte[] dup = new byte[len];
264 System.out.printf ("\t\t\t\t\t%d:", len);
265 for(int i = 0; i < len; i++) {
268 System.out.printf(" %02x", dup[i]);
271 System.out.printf("\n");
273 binary_queue.put(dup);
276 public void flush_output() {
277 for (String s : pending_output)
279 pending_output.clear();
282 public void flush_input(int timeout) throws InterruptedException {
287 Thread.sleep(timeout);
288 got_some = !reply_queue.isEmpty();
294 public void flush_input() throws InterruptedException {
303 * Various command-level operations on
306 public boolean monitor_mode = false;
307 public int telemetry = AltosLib.ao_telemetry_standard;
308 public double frequency;
309 public String callsign;
310 AltosConfigData config_data;
312 private Object config_data_lock = new Object();
314 private int telemetry_len() {
315 return AltosLib.telemetry_len(telemetry);
318 private void set_radio_freq(int frequency) {
320 printf("m 0\nc F %d\nm %x\n",
321 frequency, telemetry_len());
323 printf("c F %d\n", frequency);
327 public void set_radio_frequency(double frequency,
328 boolean has_frequency,
332 System.out.printf("set_radio_frequency %7.3f (freq %b) (set %b) %d\n", frequency, has_frequency, has_setting, cal);
336 set_radio_freq((int) Math.floor (frequency * 1000));
337 else if (has_setting)
338 set_radio_setting(AltosConvert.radio_frequency_to_setting(frequency, cal));
340 set_channel(AltosConvert.radio_frequency_to_channel(frequency));
343 public void set_radio_frequency(double in_frequency) throws InterruptedException, TimeoutException {
344 frequency = in_frequency;
346 set_radio_frequency(frequency,
347 config_data.radio_frequency > 0,
348 config_data.radio_setting > 0,
349 config_data.radio_calibration);
352 public void set_telemetry(int in_telemetry) {
353 telemetry = in_telemetry;
355 printf("m 0\nm %x\n", telemetry_len());
359 public void set_monitor(boolean monitor) {
360 monitor_mode = monitor;
362 printf("m %x\n", telemetry_len());
368 private void set_channel(int channel) {
370 printf("m 0\nc r %d\nm %x\n",
371 channel, telemetry_len());
373 printf("c r %d\n", channel);
377 private void set_radio_setting(int setting) {
379 printf("m 0\nc R %d\nm %x\n",
380 setting, telemetry_len());
382 printf("c R %d\n", setting);
386 public AltosConfigData config_data() throws InterruptedException, TimeoutException {
387 synchronized(config_data_lock) {
388 if (config_data == null)
389 config_data = new AltosConfigData(this);
394 public void set_callsign(String callsign) {
395 this.callsign = callsign;
396 printf ("c c %s\n", callsign);
400 public boolean is_loader() throws InterruptedException {
404 String line = get_reply();
408 if (line.startsWith("software-version"))
410 if (line.startsWith("altos-loader"))
416 public void to_loader() throws InterruptedException {
423 public boolean remote;
427 public void start_remote() throws TimeoutException, InterruptedException {
428 if (frequency == 0.0)
429 frequency = AltosPreferences.frequency(serial);
431 System.out.printf("start remote %7.3f\n", frequency);
432 set_radio_frequency(frequency);
433 set_callsign(AltosPreferences.callsign());
439 public void stop_remote() throws InterruptedException {
441 System.out.printf("stop remote\n");
451 public int rssi() throws TimeoutException, InterruptedException {
455 String line = get_reply_no_dialog(5000);
457 throw new TimeoutException();
458 String[] items = line.split("\\s+");
459 if (items.length < 2)
461 if (!items[0].equals("RSSI:"))
463 int rssi = Integer.parseInt(items[1]);
467 public String[] adc() throws TimeoutException, InterruptedException {
470 String line = get_reply_no_dialog(5000);
472 throw new TimeoutException();
474 if (!line.startsWith("tick:"))
476 String[] items = line.split("\\s+");
481 public boolean has_monitor_battery() {
482 return config_data.has_monitor_battery();
485 public double monitor_battery() throws InterruptedException {
486 int monitor_batt = AltosLib.MISSING;
488 if (config_data.has_monitor_battery()) {
490 String[] items = adc();
491 for (int i = 0; i < items.length;) {
492 if (items[i].equals("batt")) {
493 monitor_batt = Integer.parseInt(items[i+1]);
499 } catch (TimeoutException te) {
502 if (monitor_batt == AltosLib.MISSING)
503 return AltosLib.MISSING;
504 return AltosConvert.cc_battery_to_voltage(monitor_batt);