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_5;
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 synchronized (pending_output) {
60 pending_output.add(line);
65 } catch (InterruptedException ie) {
70 public String get_reply_no_dialog(int timeout) throws InterruptedException, TimeoutException {
72 AltosLine line = reply_queue.poll(timeout, TimeUnit.MILLISECONDS);
78 public String get_reply() throws InterruptedException {
79 return get_reply(5000);
83 public abstract boolean can_cancel_reply();
84 public abstract boolean show_reply_timeout();
85 public abstract void hide_reply_timeout();
87 public boolean reply_abort;
90 boolean reply_timeout_shown = false;
92 private boolean check_reply_timeout() {
93 if (!reply_timeout_shown)
94 reply_timeout_shown = show_reply_timeout();
98 private void cleanup_reply_timeout() {
99 if (reply_timeout_shown) {
100 reply_timeout_shown = false;
101 hide_reply_timeout();
105 private int len_read = 0;
109 byte[] line_bytes = null;
115 if (Thread.interrupted()) {
120 System.out.printf("ERROR\n");
122 add_telem (new AltosLine());
123 add_reply (new AltosLine());
128 System.out.printf("TIMEOUT\n");
131 if (c == '\r' && len_read == 0)
134 if (c == '\n' && len_read == 0) {
135 if (line_count != 0) {
136 add_bytes(line_bytes, line_count);
140 if (line_bytes == null) {
141 line_bytes = new byte[256];
142 } else if (line_count == line_bytes.length) {
143 byte[] new_line_bytes = new byte[line_count * 2];
144 System.arraycopy(line_bytes, 0, new_line_bytes, 0, line_count);
145 line_bytes = new_line_bytes;
147 line_bytes[line_count] = (byte) c;
149 if (len_read !=0 && line_count == len_read) {
150 add_binary(line_bytes, line_count);
157 } catch (InterruptedException e) {
162 public String get_reply(int timeout) throws InterruptedException {
163 boolean can_cancel = can_cancel_reply();
166 if (!can_cancel && remote)
167 System.out.printf("Uh-oh, reading remote serial device from swing thread\n");
169 if (remote && can_cancel) {
171 switch (telemetry_rate) {
172 case AltosLib.ao_telemetry_rate_38400:
176 case AltosLib.ao_telemetry_rate_9600:
179 case AltosLib.ao_telemetry_rate_2400:
190 reply_timeout_shown = false;
192 AltosLine line = reply_queue.poll(timeout, TimeUnit.MILLISECONDS);
194 cleanup_reply_timeout();
198 if (!remote || !can_cancel || check_reply_timeout()) {
209 public byte[] get_binary_reply(int timeout, int len) throws InterruptedException {
210 boolean can_cancel = can_cancel_reply();
222 reply_timeout_shown = false;
224 bytes = binary_queue.poll(timeout, TimeUnit.MILLISECONDS);
226 cleanup_reply_timeout();
229 if (!remote || !can_cancel || check_reply_timeout()) {
241 public void add_telem(AltosLine line) throws InterruptedException {
242 for (int e = 0; e < monitors.size(); e++) {
243 LinkedBlockingQueue<AltosLine> q = monitors.get(e);
248 public void add_reply(AltosLine line) throws InterruptedException {
249 reply_queue.put (line);
252 public void abort_reply() {
254 add_telem (new AltosLine());
255 add_reply (new AltosLine());
256 } catch (InterruptedException ie) {
260 public void add_string(String line) throws InterruptedException {
261 if (line.startsWith("TELEM") || line.startsWith("VERSION") || line.startsWith("CRC")) {
262 add_telem(new AltosLine(line));
264 add_reply(new AltosLine(line));
268 public void add_bytes(byte[] bytes, int len) throws InterruptedException {
270 line = new String(bytes, 0, len, AltosLib.unicode_set);
272 System.out.printf("\t\t\t\t\t%s\n", line);
276 public void add_binary(byte[] bytes, int len) throws InterruptedException {
277 byte[] dup = new byte[len];
280 System.out.printf ("\t\t\t\t\t%d:", len);
281 for(int i = 0; i < len; i++) {
284 System.out.printf(" %02x", dup[i]);
287 System.out.printf("\n");
289 binary_queue.put(dup);
292 public synchronized void flush_output() {
293 if (pending_output == null)
295 synchronized (pending_output) {
296 for (String s : pending_output)
298 pending_output.clear();
302 public void flush_input(int timeout) throws InterruptedException {
307 Thread.sleep(timeout);
308 got_some = !reply_queue.isEmpty();
314 public void flush_input() throws InterruptedException {
323 * Various command-level operations on
326 public boolean monitor_mode = false;
327 public int telemetry = AltosLib.ao_telemetry_standard;
328 public int telemetry_rate = -1;
329 public double frequency;
330 public String callsign;
331 AltosConfigData config_data;
333 private Object config_data_lock = new Object();
335 private int telemetry_len() {
336 return AltosLib.telemetry_len(telemetry);
339 private void set_radio_freq(int frequency) {
341 printf("m 0\nc F %d\nm %x\n",
342 frequency, telemetry_len());
344 printf("c F %d\n", frequency);
348 public void set_radio_frequency(double frequency,
349 boolean has_frequency,
353 System.out.printf("set_radio_frequency %7.3f (freq %b) (set %b) %d\n", frequency, has_frequency, has_setting, cal);
357 set_radio_freq((int) Math.floor (frequency * 1000));
358 else if (has_setting)
359 set_radio_setting(AltosConvert.radio_frequency_to_setting(frequency, cal));
361 set_channel(AltosConvert.radio_frequency_to_channel(frequency));
364 public void set_radio_frequency(double in_frequency) throws InterruptedException, TimeoutException {
365 frequency = in_frequency;
367 set_radio_frequency(frequency,
368 config_data.radio_frequency > 0,
369 config_data.radio_setting > 0,
370 config_data.radio_calibration);
373 public void set_telemetry(int in_telemetry) {
374 telemetry = in_telemetry;
376 printf("m 0\nm %x\n", telemetry_len());
380 public void set_telemetry_rate(int in_telemetry_rate) {
381 telemetry_rate = in_telemetry_rate;
383 printf("m 0\nc T %d\nm %x\n", telemetry_rate, telemetry_len());
385 printf("c T %d\n", telemetry_rate);
389 public void set_monitor(boolean monitor) {
390 monitor_mode = monitor;
392 printf("m %x\n", telemetry_len());
398 private void set_channel(int channel) {
400 printf("m 0\nc r %d\nm %x\n",
401 channel, telemetry_len());
403 printf("c r %d\n", channel);
407 private void set_radio_setting(int setting) {
409 printf("m 0\nc R %d\nm %x\n",
410 setting, telemetry_len());
412 printf("c R %d\n", setting);
416 public AltosConfigData config_data() throws InterruptedException, TimeoutException {
417 synchronized(config_data_lock) {
418 if (config_data == null) {
420 config_data = new AltosConfigData(this);
428 public void set_callsign(String callsign) {
429 this.callsign = callsign;
430 if (callsign != null) {
431 printf ("c c %s\n", callsign);
436 public boolean is_loader() throws InterruptedException {
440 String line = get_reply();
444 if (line.startsWith("software-version"))
446 if (line.startsWith("altos-loader"))
452 public void to_loader() throws InterruptedException {
459 public boolean remote;
463 public void start_remote() throws TimeoutException, InterruptedException {
464 if (frequency == 0.0)
465 frequency = AltosPreferences.frequency(serial);
467 System.out.printf("start remote %7.3f\n", frequency);
468 set_radio_frequency(frequency);
469 if (telemetry_rate < 0)
470 telemetry_rate = AltosPreferences.telemetry_rate(serial);
471 set_telemetry_rate(telemetry_rate);
472 if (callsign == null || callsign.equals(""))
473 callsign = AltosPreferences.callsign();
474 set_callsign(callsign);
480 public void stop_remote() throws InterruptedException {
482 System.out.printf("stop remote\n");
492 public int rssi() throws TimeoutException, InterruptedException {
496 String line = get_reply_no_dialog(5000);
498 throw new TimeoutException();
499 String[] items = line.split("\\s+");
500 if (items.length < 2)
502 if (!items[0].equals("RSSI:"))
504 int rssi = Integer.parseInt(items[1]);
508 public String[] adc() throws TimeoutException, InterruptedException {
511 String line = get_reply_no_dialog(5000);
513 throw new TimeoutException();
515 if (!line.startsWith("tick:"))
517 String[] items = line.split("\\s+");
522 public boolean has_monitor_battery() {
523 return config_data.has_monitor_battery();
526 public double monitor_battery() throws InterruptedException {
527 int monitor_batt = AltosLib.MISSING;
529 if (config_data.has_monitor_battery()) {
531 String[] items = adc();
532 for (int i = 0; i < items.length;) {
533 if (items[i].equals("batt")) {
534 monitor_batt = Integer.parseInt(items[i+1]);
540 } catch (TimeoutException te) {
543 if (monitor_batt == AltosLib.MISSING)
544 return AltosLib.MISSING;
545 return AltosConvert.cc_battery_to_voltage(monitor_batt);