altosdroid: initial attempt at a UI.
[fw/altos] / altoslib / AltosLink.java
1 /*
2  * Copyright © 2011 Keith Packard <keithp@keithp.com>
3  *
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.
7  *
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.
12  *
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.
16  */
17
18 package org.altusmetrum.AltosLib;
19
20 import java.lang.*;
21 import java.io.*;
22 import java.util.concurrent.*;
23 import java.util.*;
24 import java.text.*;
25
26 public abstract class AltosLink {
27
28         public final static int ERROR = -1;
29         public final static int TIMEOUT = -2;
30
31         public abstract int getchar();
32         public abstract void print(String data);
33         public abstract void close();
34
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>();
38
39         public LinkedList<LinkedBlockingQueue<AltosLine>> monitors = new LinkedList<LinkedBlockingQueue<AltosLine>> ();;
40         public LinkedBlockingQueue<AltosLine> reply_queue = new LinkedBlockingQueue<AltosLine>();
41
42         public void add_monitor(LinkedBlockingQueue<AltosLine> q) {
43                 set_monitor(true);
44                 monitors.add(q);
45         }
46
47         public void remove_monitor(LinkedBlockingQueue<AltosLine> q) {
48                 monitors.remove(q);
49                 if (monitors.isEmpty())
50                         set_monitor(false);
51         }
52
53         public void printf(String format, Object ... arguments) {
54                 String  line = String.format(format, arguments);
55                 if (debug)
56                         pending_output.add(line);
57                 print(line);
58         }
59
60         public String get_reply_no_dialog(int timeout) throws InterruptedException, TimeoutException {
61                 flush_output();
62                 AltosLine line = reply_queue.poll(timeout, TimeUnit.MILLISECONDS);
63                 if (line != null)
64                         return line.line;
65                 return null;
66         }
67
68         public String get_reply() throws InterruptedException {
69                 return get_reply(5000);
70         }
71
72                 
73         public abstract boolean can_cancel_reply();
74         public abstract boolean show_reply_timeout();
75         public abstract void hide_reply_timeout();
76
77         public boolean  reply_abort;
78         public int      in_reply;
79
80         boolean         reply_timeout_shown = false;
81
82         private boolean check_reply_timeout() {
83                 if (!reply_timeout_shown)
84                         reply_timeout_shown = show_reply_timeout();
85                 return reply_abort;
86         }
87
88         private void cleanup_reply_timeout() {
89                 if (reply_timeout_shown) {
90                         reply_timeout_shown = false;
91                         hide_reply_timeout();
92                 }
93         }
94
95
96         public void run () {
97                 int c;
98                 byte[] line_bytes = null;
99                 int line_count = 0;
100
101                 try {
102                         for (;;) {
103                                 c = getchar();
104                                 if (Thread.interrupted())
105                                         break;
106                                 if (c == ERROR) {
107                                         add_telem (new AltosLine());
108                                         add_reply (new AltosLine());
109                                         break;
110                                 }
111                                 if (c == TIMEOUT)
112                                         continue;
113                                 if (c == '\r')
114                                         continue;
115                                 synchronized(this) {
116                                         if (c == '\n') {
117                                                 if (line_count != 0) {
118                                                         add_bytes(line_bytes, line_count);
119                                                         line_count = 0;
120                                                 }
121                                         } else {
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;
128                                                 }
129                                                 line_bytes[line_count] = (byte) c;
130                                                 line_count++;
131                                         }
132                                 }
133                         }
134                 } catch (InterruptedException e) {
135                 }
136         }
137
138         public String get_reply(int timeout) throws InterruptedException {
139                 boolean can_cancel = can_cancel_reply();
140                 String  reply = null;
141
142                 if (!can_cancel && remote)
143                         System.out.printf("Uh-oh, reading remote serial device from swing thread\n");
144
145                 if (remote && can_cancel)
146                         timeout = 500;
147                 try {
148                         ++in_reply;
149
150                         flush_output();
151
152                         reply_abort = false;
153                         reply_timeout_shown = false;
154                         for (;;) {
155                                 AltosLine line = reply_queue.poll(timeout, TimeUnit.MILLISECONDS);
156                                 if (line != null) {
157                                         cleanup_reply_timeout();
158                                         reply = line.line;
159                                         break;
160                                 }
161                                 if (!remote || !can_cancel || check_reply_timeout()) {
162                                         reply = null;
163                                         break;
164                                 }
165                         }
166                 } finally {
167                         --in_reply;
168                 }
169                 return reply;
170         }
171
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);
175                         q.put(line);
176                 }
177         }
178
179         public void add_reply(AltosLine line) throws InterruptedException {
180                 reply_queue.put (line);
181         }
182
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));
186                 } else {
187                         add_reply(new AltosLine(line));
188                 }
189         }
190
191         public void add_bytes(byte[] bytes, int len) throws InterruptedException {
192                 String  line;
193                 try {
194                         line = new String(bytes, 0, len, "UTF-8");
195                 } catch (UnsupportedEncodingException ue) {
196                         line = "";
197                         for (int i = 0; i < len; i++)
198                                 line = line + bytes[i];
199                 }
200                 if (debug)
201                         System.out.printf("\t\t\t\t\t%s\n", line);
202                 add_string(line);
203         }
204
205         public void flush_output() {
206                 for (String s : pending_output)
207                         System.out.print(s);
208                 pending_output.clear();
209         }
210
211         public void flush_input(int timeout) throws InterruptedException {
212                 flush_output();
213                 boolean got_some;
214
215                 do {
216                         Thread.sleep(timeout);
217                         got_some = !reply_queue.isEmpty();
218                         reply_queue.clear();
219                 } while (got_some);
220         }
221
222
223         public void flush_input() throws InterruptedException {
224                 if (remote)
225                         flush_input(500);
226                 else
227                         flush_input(100);
228         }
229
230
231         /*
232          * Various command-level operations on
233          * the link
234          */
235         public boolean monitor_mode = false;
236         public int telemetry = AltosLib.ao_telemetry_standard;
237         public double frequency;
238         AltosConfigData config_data;
239
240         private int telemetry_len() {
241                 return AltosLib.telemetry_len(telemetry);
242         }
243
244         private void set_radio_freq(int frequency) {
245                 if (monitor_mode)
246                         printf("m 0\nc F %d\nm %x\n",
247                                frequency, telemetry_len());
248                 else
249                         printf("c F %d\n", frequency);
250                 flush_output();
251         }
252
253         public void set_radio_frequency(double frequency,
254                                         boolean has_frequency,
255                                         boolean has_setting,
256                                         int cal) {
257                 if (debug)
258                         System.out.printf("set_radio_frequency %7.3f (freq %b) (set %b) %d\n", frequency, has_frequency, has_setting, cal);
259                 if (frequency == 0)
260                         return;
261                 if (has_frequency)
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));
265                 else
266                         set_channel(AltosConvert.radio_frequency_to_channel(frequency));
267         }
268
269         public void set_radio_frequency(double in_frequency) throws InterruptedException, TimeoutException {
270                 frequency = in_frequency;
271                 config_data();
272                 set_radio_frequency(frequency,
273                                     config_data.radio_frequency != 0,
274                                     config_data.radio_setting != 0,
275                                     config_data.radio_calibration);
276         }
277
278         public void set_telemetry(int in_telemetry) {
279                 telemetry = in_telemetry;
280                 if (monitor_mode)
281                         printf("m 0\nm %x\n", telemetry_len());
282                 flush_output();
283         }
284
285         public void set_monitor(boolean monitor) {
286                 monitor_mode = monitor;
287                 if (monitor)
288                         printf("m %x\n", telemetry_len());
289                 else
290                         printf("m 0\n");
291                 flush_output();
292         }
293
294         private void set_channel(int channel) {
295                 if (monitor_mode)
296                         printf("m 0\nc r %d\nm %x\n",
297                                channel, telemetry_len());
298                 else
299                         printf("c r %d\n", channel);
300                 flush_output();
301         }
302
303         private void set_radio_setting(int setting) {
304                 if (monitor_mode)
305                         printf("m 0\nc R %d\nm %x\n",
306                                setting, telemetry_len());
307                 else
308                         printf("c R %d\n", setting);
309                 flush_output();
310         }
311
312         public AltosConfigData config_data() throws InterruptedException, TimeoutException {
313                 if (config_data == null)
314                         config_data = new AltosConfigData(this);
315                 return config_data;
316         }
317
318         public void set_callsign(String callsign) {
319                 printf ("c c %s\n", callsign);
320                 flush_output();
321         }
322
323         public boolean remote;
324         public int serial;
325         public String name;
326
327         public void start_remote() throws TimeoutException, InterruptedException {
328                 if (debug)
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());
334                 printf("p\nE 0\n");
335                 flush_input();
336                 remote = true;
337         }
338
339         public void stop_remote() throws InterruptedException {
340                 if (debug)
341                         System.out.printf("stop remote\n");
342                 try {
343                         flush_input();
344                 } finally {
345                         printf ("~\n");
346                         flush_output();
347                 }
348                 remote = false;
349         }
350
351         public AltosLink() {
352         }
353 }