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_1;
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();
30 public abstract void print(String data);
31 public abstract void close();
33 public static boolean debug = false;
34 public static void set_debug(boolean in_debug) { debug = in_debug; }
35 LinkedList<String> pending_output = new LinkedList<String>();
37 public LinkedList<LinkedBlockingQueue<AltosLine>> monitors = new LinkedList<LinkedBlockingQueue<AltosLine>> ();;
38 public LinkedBlockingQueue<AltosLine> reply_queue = new LinkedBlockingQueue<AltosLine>();
40 public void add_monitor(LinkedBlockingQueue<AltosLine> q) {
45 public void remove_monitor(LinkedBlockingQueue<AltosLine> q) {
47 if (monitors.isEmpty())
51 public void printf(String format, Object ... arguments) {
52 String line = String.format(format, arguments);
54 pending_output.add(line);
58 public String get_reply_no_dialog(int timeout) throws InterruptedException, TimeoutException {
60 AltosLine line = reply_queue.poll(timeout, TimeUnit.MILLISECONDS);
66 public String get_reply() throws InterruptedException {
67 return get_reply(5000);
71 public abstract boolean can_cancel_reply();
72 public abstract boolean show_reply_timeout();
73 public abstract void hide_reply_timeout();
75 public boolean reply_abort;
78 boolean reply_timeout_shown = false;
80 private boolean check_reply_timeout() {
81 if (!reply_timeout_shown)
82 reply_timeout_shown = show_reply_timeout();
86 private void cleanup_reply_timeout() {
87 if (reply_timeout_shown) {
88 reply_timeout_shown = false;
96 byte[] line_bytes = null;
102 if (Thread.interrupted()) {
104 System.out.printf("INTERRUPTED\n");
109 System.out.printf("ERROR\n");
110 add_telem (new AltosLine());
111 add_reply (new AltosLine());
116 System.out.printf("TIMEOUT\n");
123 if (line_count != 0) {
124 add_bytes(line_bytes, line_count);
128 if (line_bytes == null) {
129 line_bytes = new byte[256];
130 } else if (line_count == line_bytes.length) {
131 byte[] new_line_bytes = new byte[line_count * 2];
132 System.arraycopy(line_bytes, 0, new_line_bytes, 0, line_count);
133 line_bytes = new_line_bytes;
135 line_bytes[line_count] = (byte) c;
140 } catch (InterruptedException e) {
144 public String get_reply(int timeout) throws InterruptedException {
145 boolean can_cancel = can_cancel_reply();
148 if (!can_cancel && remote)
149 System.out.printf("Uh-oh, reading remote serial device from swing thread\n");
151 if (remote && can_cancel)
159 reply_timeout_shown = false;
161 AltosLine line = reply_queue.poll(timeout, TimeUnit.MILLISECONDS);
163 cleanup_reply_timeout();
167 if (!remote || !can_cancel || check_reply_timeout()) {
178 public void add_telem(AltosLine line) throws InterruptedException {
179 for (int e = 0; e < monitors.size(); e++) {
180 LinkedBlockingQueue<AltosLine> q = monitors.get(e);
185 public void add_reply(AltosLine line) throws InterruptedException {
186 reply_queue.put (line);
189 public void abort_reply() {
191 add_telem (new AltosLine());
192 add_reply (new AltosLine());
193 } catch (InterruptedException e) {
197 public void add_string(String line) throws InterruptedException {
198 if (line.startsWith("TELEM") || line.startsWith("VERSION") || line.startsWith("CRC")) {
199 add_telem(new AltosLine(line));
201 add_reply(new AltosLine(line));
205 public void add_bytes(byte[] bytes, int len) throws InterruptedException {
208 line = new String(bytes, 0, len, "UTF-8");
209 } catch (UnsupportedEncodingException ue) {
211 for (int i = 0; i < len; i++)
212 line = line + bytes[i];
215 System.out.printf("\t\t\t\t\t%s\n", line);
219 public void flush_output() {
220 for (String s : pending_output)
222 pending_output.clear();
225 public void flush_input(int timeout) throws InterruptedException {
230 Thread.sleep(timeout);
231 got_some = !reply_queue.isEmpty();
237 public void flush_input() throws InterruptedException {
246 * Various command-level operations on
249 public boolean monitor_mode = false;
250 public int telemetry = AltosLib.ao_telemetry_standard;
251 public double frequency;
252 public String callsign;
253 AltosConfigData config_data;
255 private int telemetry_len() {
256 return AltosLib.telemetry_len(telemetry);
259 private void set_radio_freq(int frequency) {
261 printf("m 0\nc F %d\nm %x\n",
262 frequency, telemetry_len());
264 printf("c F %d\n", frequency);
268 public void set_radio_frequency(double frequency,
269 boolean has_frequency,
273 System.out.printf("set_radio_frequency %7.3f (freq %b) (set %b) %d\n", frequency, has_frequency, has_setting, cal);
277 set_radio_freq((int) Math.floor (frequency * 1000));
278 else if (has_setting)
279 set_radio_setting(AltosConvert.radio_frequency_to_setting(frequency, cal));
281 set_channel(AltosConvert.radio_frequency_to_channel(frequency));
284 public void set_radio_frequency(double in_frequency) throws InterruptedException, TimeoutException {
285 frequency = in_frequency;
287 set_radio_frequency(frequency,
288 config_data.radio_frequency > 0,
289 config_data.radio_setting > 0,
290 config_data.radio_calibration);
293 public void set_telemetry(int in_telemetry) {
294 telemetry = in_telemetry;
296 printf("m 0\nm %x\n", telemetry_len());
300 public void set_monitor(boolean monitor) {
301 monitor_mode = monitor;
303 printf("m %x\n", telemetry_len());
309 private void set_channel(int channel) {
311 printf("m 0\nc r %d\nm %x\n",
312 channel, telemetry_len());
314 printf("c r %d\n", channel);
318 private void set_radio_setting(int setting) {
320 printf("m 0\nc R %d\nm %x\n",
321 setting, telemetry_len());
323 printf("c R %d\n", setting);
327 public AltosConfigData config_data() throws InterruptedException, TimeoutException {
328 if (config_data == null)
329 config_data = new AltosConfigData(this);
333 public void set_callsign(String callsign) {
334 this.callsign = callsign;
335 printf ("c c %s\n", callsign);
339 public boolean remote;
343 public void start_remote() throws TimeoutException, InterruptedException {
344 if (frequency == 0.0)
345 frequency = AltosPreferences.frequency(serial);
347 System.out.printf("start remote %7.3f\n", frequency);
348 set_radio_frequency(frequency);
349 set_callsign(AltosPreferences.callsign());
355 public void stop_remote() throws InterruptedException {
357 System.out.printf("stop remote\n");
367 public int rssi() throws TimeoutException, InterruptedException {
371 String line = get_reply_no_dialog(5000);
373 throw new TimeoutException();
374 String[] items = line.split("\\s+");
375 if (items.length < 2)
377 if (!items[0].equals("RSSI:"))
379 int rssi = Integer.parseInt(items[1]);
383 public String[] adc() throws TimeoutException, InterruptedException {
386 String line = get_reply_no_dialog(5000);
388 throw new TimeoutException();
390 if (!line.startsWith("tick:"))
392 String[] items = line.split("\\s+");
397 public boolean has_monitor_battery() {
398 return config_data.has_monitor_battery();
401 public double monitor_battery() {
402 int monitor_batt = AltosRecord.MISSING;
404 if (config_data.has_monitor_battery()) {
406 String[] items = adc();
407 for (int i = 0; i < items.length;) {
408 if (items[i].equals("batt")) {
409 monitor_batt = Integer.parseInt(items[i+1]);
415 } catch (InterruptedException ie) {
416 } catch (TimeoutException te) {
419 if (monitor_batt == AltosRecord.MISSING)
420 return AltosRecord.MISSING;
421 return AltosConvert.cc_battery_to_voltage(monitor_batt);