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 {
27 public abstract void print(String data);
28 public abstract void close();
30 public static boolean debug = false;
31 public static void set_debug(boolean in_debug) { debug = in_debug; }
32 LinkedList<String> pending_output = new LinkedList<String>();
34 public LinkedList<LinkedBlockingQueue<AltosLine>> monitors = new LinkedList<LinkedBlockingQueue<AltosLine>> ();;
35 public LinkedBlockingQueue<AltosLine> reply_queue = new LinkedBlockingQueue<AltosLine>();
37 public void add_monitor(LinkedBlockingQueue<AltosLine> q) {
42 public void remove_monitor(LinkedBlockingQueue<AltosLine> q) {
44 if (monitors.isEmpty())
48 public void printf(String format, Object ... arguments) {
49 String line = String.format(format, arguments);
51 pending_output.add(line);
55 public String get_reply_no_dialog(int timeout) throws InterruptedException, TimeoutException {
57 AltosLine line = reply_queue.poll(timeout, TimeUnit.MILLISECONDS);
63 public String get_reply(int timeout) throws InterruptedException {
65 return get_reply_no_dialog(timeout);
66 } catch (TimeoutException te) {
71 public String get_reply() throws InterruptedException {
72 return get_reply(5000);
75 public void add_telem(AltosLine line) throws InterruptedException {
76 for (int e = 0; e < monitors.size(); e++) {
77 LinkedBlockingQueue<AltosLine> q = monitors.get(e);
82 public void add_reply(AltosLine line) throws InterruptedException {
83 reply_queue.put (line);
86 public void add_string(String line) throws InterruptedException {
87 if (line.startsWith("TELEM") || line.startsWith("VERSION") || line.startsWith("CRC")) {
88 add_telem(new AltosLine(line));
90 add_reply(new AltosLine(line));
94 public void add_bytes(byte[] bytes, int len) throws InterruptedException {
97 line = new String(bytes, 0, len, "UTF-8");
98 } catch (UnsupportedEncodingException ue) {
100 for (int i = 0; i < len; i++)
101 line = line + bytes[i];
104 System.out.printf("\t\t\t\t\t%s\n", line);
108 public void flush_output() {
109 for (String s : pending_output)
111 pending_output.clear();
114 public void flush_input(int timeout) throws InterruptedException {
119 Thread.sleep(timeout);
120 got_some = !reply_queue.isEmpty();
126 public void flush_input() throws InterruptedException {
132 * Various command-level operations on
135 public boolean monitor_mode = false;
136 public int telemetry = AltosLib.ao_telemetry_standard;
137 public double frequency;
138 AltosConfigData config_data;
140 private int telemetry_len() {
141 return AltosLib.telemetry_len(telemetry);
144 private void set_radio_freq(int frequency) {
146 printf("m 0\nc F %d\nm %x\n",
147 frequency, telemetry_len());
149 printf("c F %d\n", frequency);
153 public void set_radio_frequency(double frequency,
154 boolean has_frequency,
158 System.out.printf("set_radio_frequency %7.3f (freq %b) (set %b) %d\n", frequency, has_frequency, has_setting, cal);
162 set_radio_freq((int) Math.floor (frequency * 1000));
163 else if (has_setting)
164 set_radio_setting(AltosConvert.radio_frequency_to_setting(frequency, cal));
166 set_channel(AltosConvert.radio_frequency_to_channel(frequency));
169 public void set_radio_frequency(double in_frequency) throws InterruptedException, TimeoutException {
170 frequency = in_frequency;
172 set_radio_frequency(frequency,
173 config_data.radio_frequency != 0,
174 config_data.radio_setting != 0,
175 config_data.radio_calibration);
178 public void set_telemetry(int in_telemetry) {
179 telemetry = in_telemetry;
181 printf("m 0\nm %x\n", telemetry_len());
185 public void set_monitor(boolean monitor) {
186 monitor_mode = monitor;
188 printf("m %x\n", telemetry_len());
194 private void set_channel(int channel) {
196 printf("m 0\nc r %d\nm %x\n",
197 channel, telemetry_len());
199 printf("c r %d\n", channel);
203 private void set_radio_setting(int setting) {
205 printf("m 0\nc R %d\nm %x\n",
206 setting, telemetry_len());
208 printf("c R %d\n", setting);
212 public AltosConfigData config_data() throws InterruptedException, TimeoutException {
213 if (config_data == null)
214 config_data = new AltosConfigData(this);
218 public void set_callsign(String callsign) {
219 printf ("c c %s\n", callsign);
223 public boolean remote;
227 public void start_remote() throws TimeoutException, InterruptedException {
229 System.out.printf("start remote %7.3f\n", frequency);
230 if (frequency == 0.0)
231 frequency = AltosPreferences.frequency(serial);
232 set_radio_frequency(frequency);
233 set_callsign(AltosPreferences.callsign());
239 public void stop_remote() throws InterruptedException {
241 System.out.printf("stop remote\n");