altosui: Add telemetry format menu and preferences
[fw/altos] / altosui / AltosSerial.java
1 /*
2  * Copyright © 2010 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 /*
19  * Deal with TeleDongle on a serial port
20  */
21
22 package altosui;
23
24 import java.lang.*;
25 import java.io.*;
26 import java.util.concurrent.*;
27 import java.util.*;
28
29 import libaltosJNI.*;
30
31 /*
32  * This class reads from the serial port and places each received
33  * line in a queue. Dealing with that queue is left up to other
34  * threads.
35  */
36
37 public class AltosSerial implements Runnable {
38
39         static List<String> devices_opened = Collections.synchronizedList(new LinkedList<String>());
40
41         AltosDevice device;
42         SWIGTYPE_p_altos_file altos;
43         LinkedList<LinkedBlockingQueue<AltosLine>> monitors;
44         LinkedBlockingQueue<AltosLine> reply_queue;
45         Thread input_thread;
46         String line;
47         byte[] line_bytes;
48         int line_count;
49         boolean monitor_mode;
50         int telemetry;
51         int channel;
52         static boolean debug;
53         boolean remote;
54         LinkedList<String> pending_output = new LinkedList<String>();
55
56         static void set_debug(boolean new_debug) {
57                 debug = new_debug;
58         }
59
60         public void run () {
61                 int c;
62
63                 try {
64                         for (;;) {
65                                 c = libaltos.altos_getchar(altos, 0);
66                                 if (Thread.interrupted())
67                                         break;
68                                 if (c == libaltosConstants.LIBALTOS_ERROR) {
69                                         for (int e = 0; e < monitors.size(); e++) {
70                                                 LinkedBlockingQueue<AltosLine> q = monitors.get(e);
71                                                 q.put(new AltosLine());
72                                         }
73                                         reply_queue.put (new AltosLine());
74                                         break;
75                                 }
76                                 if (c == libaltosConstants.LIBALTOS_TIMEOUT)
77                                         continue;
78                                 if (c == '\r')
79                                         continue;
80                                 synchronized(this) {
81                                         if (c == '\n') {
82                                                 if (line_count != 0) {
83                                                         try {
84                                                                 line = new String(line_bytes, 0, line_count, "UTF-8");
85                                                         } catch (UnsupportedEncodingException ue) {
86                                                                 line = "";
87                                                                 for (int i = 0; i < line_count; i++)
88                                                                         line = line + line_bytes[i];
89                                                         }
90                                                         if (debug)
91                                                                 System.out.printf("\t\t\t\t\t%s\n", line);
92                                                         if (line.startsWith("VERSION") || line.startsWith("CRC")) {
93                                                                 for (int e = 0; e < monitors.size(); e++) {
94                                                                         LinkedBlockingQueue<AltosLine> q = monitors.get(e);
95                                                                         q.put(new AltosLine (line));
96                                                                 }
97                                                         } else {
98                                                                 reply_queue.put(new AltosLine (line));
99                                                         }
100                                                         line_count = 0;
101                                                         line = "";
102                                                 }
103                                         } else {
104                                                 if (line_bytes == null) {
105                                                         line_bytes = new byte[256];
106                                                 } else if (line_count == line_bytes.length) {
107                                                         byte[] new_line_bytes = new byte[line_count * 2];
108                                                         System.arraycopy(line_bytes, 0, new_line_bytes, 0, line_count);
109                                                         line_bytes = new_line_bytes;
110                                                 }
111                                                 line_bytes[line_count] = (byte) c;
112                                                 line_count++;
113                                         }
114                                 }
115                         }
116                 } catch (InterruptedException e) {
117                 }
118         }
119
120         public void flush_output() {
121                 if (altos != null) {
122                         for (String s : pending_output)
123                                 System.out.print(s);
124                         pending_output.clear();
125                         libaltos.altos_flush(altos);
126                 }
127         }
128
129         public void flush_input() {
130                 flush_output();
131                 boolean got_some;
132
133                 int timeout = 100;
134                 if (remote)
135                         timeout = 300;
136                 do {
137                         try {
138                                 Thread.sleep(timeout);
139                         } catch (InterruptedException ie) {
140                         }
141                         got_some = !reply_queue.isEmpty();
142                         synchronized(this) {
143                                 if (!"VERSION".startsWith(line) &&
144                                     !line.startsWith("VERSION"))
145                                         line = "";
146                                 reply_queue.clear();
147                         }
148                 } while (got_some);
149         }
150
151         public String get_reply() throws InterruptedException {
152                 flush_output();
153                 AltosLine line = reply_queue.take();
154                 return line.line;
155         }
156
157         public String get_reply(int timeout) throws InterruptedException {
158                 flush_output();
159                 AltosLine line = reply_queue.poll(timeout, TimeUnit.MILLISECONDS);
160                 if (line == null)
161                         return null;
162                 return line.line;
163         }
164
165         public void add_monitor(LinkedBlockingQueue<AltosLine> q) {
166                 set_monitor(true);
167                 monitors.add(q);
168         }
169
170         public void remove_monitor(LinkedBlockingQueue<AltosLine> q) {
171                 monitors.remove(q);
172                 if (monitors.isEmpty())
173                         set_monitor(false);
174         }
175
176         public void close() {
177                 if (altos != null) {
178                         libaltos.altos_close(altos);
179                 }
180                 if (input_thread != null) {
181                         try {
182                                 input_thread.interrupt();
183                                 input_thread.join();
184                         } catch (InterruptedException e) {
185                         }
186                         input_thread = null;
187                 }
188                 if (altos != null) {
189                         libaltos.altos_free(altos);
190                         altos = null;
191                 }
192                 synchronized (devices_opened) {
193                         devices_opened.remove(device.getPath());
194                 }
195                 if (debug)
196                         System.out.printf("Closing %s\n", device.getPath());
197         }
198
199         private void putc(char c) {
200                 if (altos != null)
201                         libaltos.altos_putchar(altos, c);
202         }
203
204         public void print(String data) {
205                 if (debug)
206                         pending_output.add(data);
207                 for (int i = 0; i < data.length(); i++)
208                         putc(data.charAt(i));
209         }
210
211         public void printf(String format, Object ... arguments) {
212                 print(String.format(format, arguments));
213         }
214
215         private void open() throws FileNotFoundException, AltosSerialInUseException {
216                 synchronized (devices_opened) {
217                         if (devices_opened.contains(device.getPath()))
218                                 throw new AltosSerialInUseException(device);
219                         devices_opened.add(device.getPath());
220                 }
221                 altos = libaltos.altos_open(device);
222                 if (altos == null) {
223                         close();
224                         throw new FileNotFoundException(device.toShortString());
225                 }
226                 if (debug)
227                         System.out.printf("Open %s\n", device.getPath());
228                 input_thread = new Thread(this);
229                 input_thread.start();
230                 print("~\nE 0\n");
231                 set_monitor(false);
232                 flush_output();
233         }
234
235         public void set_radio() {
236                 telemetry = AltosPreferences.telemetry(device.getSerial());
237                 channel = AltosPreferences.channel(device.getSerial());
238                 set_channel(channel);
239                 set_callsign(AltosPreferences.callsign());
240         }
241
242         public void set_channel(int in_channel) {
243                 channel = in_channel;
244                 if (altos != null) {
245                         if (monitor_mode)
246                                 printf("m 0\nc r %d\nm %d\n", channel, telemetry);
247                         else
248                                 printf("c r %d\n", channel);
249                         flush_output();
250                 }
251         }
252
253         public void set_telemetry(int in_telemetry) {
254                 telemetry = in_telemetry;
255                 if (altos != null) {
256                         if (monitor_mode)
257                                 printf("m 0\nm %d\n", telemetry);
258                         flush_output();
259                 }
260         }
261
262         void set_monitor(boolean monitor) {
263                 monitor_mode = monitor;
264                 if (altos != null) {
265                         if (monitor)
266                                 printf("m %d\n", telemetry);
267                         else
268                                 printf("m 0\n");
269                         flush_output();
270                 }
271         }
272
273         public void set_callsign(String callsign) {
274                 if (altos != null) {
275                         printf ("c c %s\n", callsign);
276                         flush_output();
277                 }
278         }
279
280         public void start_remote() {
281                 if (debug)
282                         System.out.printf("start remote\n");
283                 set_radio();
284                 printf("p\nE 0\n");
285                 flush_input();
286                 remote = true;
287         }
288
289         public void stop_remote() {
290                 if (debug)
291                         System.out.printf("stop remote\n");
292                 flush_input();
293                 printf ("~");
294                 flush_output();
295                 remote = false;
296         }
297
298         public AltosSerial(AltosDevice in_device) throws FileNotFoundException, AltosSerialInUseException {
299                 device = in_device;
300                 line = "";
301                 monitor_mode = false;
302                 telemetry = Altos.ao_telemetry_full;
303                 monitors = new LinkedList<LinkedBlockingQueue<AltosLine>> ();
304                 reply_queue = new LinkedBlockingQueue<AltosLine> ();
305                 open();
306         }
307 }