altosui: Use long input flush timeout when remote.
[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         static boolean debug;
51         boolean remote;
52         LinkedList<String> pending_output = new LinkedList<String>();
53
54         static void set_debug(boolean new_debug) {
55                 debug = new_debug;
56         }
57
58         public void run () {
59                 int c;
60
61                 try {
62                         for (;;) {
63                                 c = libaltos.altos_getchar(altos, 0);
64                                 if (Thread.interrupted())
65                                         break;
66                                 if (c == libaltosConstants.LIBALTOS_ERROR) {
67                                         for (int e = 0; e < monitors.size(); e++) {
68                                                 LinkedBlockingQueue<AltosLine> q = monitors.get(e);
69                                                 q.put(new AltosLine());
70                                         }
71                                         reply_queue.put (new AltosLine());
72                                         break;
73                                 }
74                                 if (c == libaltosConstants.LIBALTOS_TIMEOUT)
75                                         continue;
76                                 if (c == '\r')
77                                         continue;
78                                 synchronized(this) {
79                                         if (c == '\n') {
80                                                 if (line_count != 0) {
81                                                         try {
82                                                                 line = new String(line_bytes, 0, line_count, "UTF-8");
83                                                         } catch (UnsupportedEncodingException ue) {
84                                                                 line = "";
85                                                                 for (int i = 0; i < line_count; i++)
86                                                                         line = line + line_bytes[i];
87                                                         }
88                                                         if (debug)
89                                                                 System.out.printf("\t\t\t\t\t%s\n", line);
90                                                         if (line.startsWith("VERSION") || line.startsWith("CRC")) {
91                                                                 for (int e = 0; e < monitors.size(); e++) {
92                                                                         LinkedBlockingQueue<AltosLine> q = monitors.get(e);
93                                                                         q.put(new AltosLine (line));
94                                                                 }
95                                                         } else {
96                                                                 reply_queue.put(new AltosLine (line));
97                                                         }
98                                                         line_count = 0;
99                                                         line = "";
100                                                 }
101                                         } else {
102                                                 if (line_bytes == null) {
103                                                         line_bytes = new byte[256];
104                                                 } else if (line_count == line_bytes.length) {
105                                                         byte[] new_line_bytes = new byte[line_count * 2];
106                                                         System.arraycopy(line_bytes, 0, new_line_bytes, 0, line_count);
107                                                         line_bytes = new_line_bytes;
108                                                 }
109                                                 line_bytes[line_count] = (byte) c;
110                                                 line_count++;
111                                         }
112                                 }
113                         }
114                 } catch (InterruptedException e) {
115                 }
116         }
117
118         public void flush_output() {
119                 if (altos != null) {
120                         for (String s : pending_output)
121                                 System.out.print(s);
122                         pending_output.clear();
123                         libaltos.altos_flush(altos);
124                 }
125         }
126
127         public void flush_input() {
128                 flush_output();
129                 boolean got_some;
130
131                 int timeout = 100;
132                 if (remote)
133                         timeout = 300;
134                 do {
135                         try {
136                                 Thread.sleep(timeout);
137                         } catch (InterruptedException ie) {
138                         }
139                         got_some = !reply_queue.isEmpty();
140                         synchronized(this) {
141                                 if (!"VERSION".startsWith(line) &&
142                                     !line.startsWith("VERSION"))
143                                         line = "";
144                                 reply_queue.clear();
145                         }
146                 } while (got_some);
147         }
148
149         public String get_reply() throws InterruptedException {
150                 flush_output();
151                 AltosLine line = reply_queue.take();
152                 return line.line;
153         }
154
155         public String get_reply(int timeout) throws InterruptedException {
156                 flush_output();
157                 AltosLine line = reply_queue.poll(timeout, TimeUnit.MILLISECONDS);
158                 if (line == null)
159                         return null;
160                 return line.line;
161         }
162
163         public void add_monitor(LinkedBlockingQueue<AltosLine> q) {
164                 set_monitor(true);
165                 monitors.add(q);
166         }
167
168         public void remove_monitor(LinkedBlockingQueue<AltosLine> q) {
169                 monitors.remove(q);
170                 if (monitors.isEmpty())
171                         set_monitor(false);
172         }
173
174         public void close() {
175                 if (altos != null) {
176                         libaltos.altos_close(altos);
177                 }
178                 if (input_thread != null) {
179                         try {
180                                 input_thread.interrupt();
181                                 input_thread.join();
182                         } catch (InterruptedException e) {
183                         }
184                         input_thread = null;
185                 }
186                 if (altos != null) {
187                         libaltos.altos_free(altos);
188                         altos = null;
189                 }
190                 synchronized (devices_opened) {
191                         devices_opened.remove(device.getPath());
192                 }
193                 if (debug)
194                         System.out.printf("Closing %s\n", device.getPath());
195         }
196
197         private void putc(char c) {
198                 if (altos != null)
199                         libaltos.altos_putchar(altos, c);
200         }
201
202         public void print(String data) {
203                 if (debug)
204                         pending_output.add(data);
205                 for (int i = 0; i < data.length(); i++)
206                         putc(data.charAt(i));
207         }
208
209         public void printf(String format, Object ... arguments) {
210                 print(String.format(format, arguments));
211         }
212
213         private void open() throws FileNotFoundException, AltosSerialInUseException {
214                 synchronized (devices_opened) {
215                         if (devices_opened.contains(device.getPath()))
216                                 throw new AltosSerialInUseException(device);
217                         devices_opened.add(device.getPath());
218                 }
219                 altos = libaltos.altos_open(device);
220                 if (altos == null) {
221                         close();
222                         throw new FileNotFoundException(device.toShortString());
223                 }
224                 if (debug)
225                         System.out.printf("Open %s\n", device.getPath());
226                 input_thread = new Thread(this);
227                 input_thread.start();
228                 print("~\nE 0\n");
229                 set_monitor(false);
230                 flush_output();
231         }
232
233         public void set_radio() {
234                 set_channel(AltosPreferences.channel(device.getSerial()));
235                 set_callsign(AltosPreferences.callsign());
236         }
237
238         public void set_channel(int channel) {
239                 if (altos != null) {
240                         if (monitor_mode)
241                                 printf("m 0\nc r %d\nm 1\n", channel);
242                         else
243                                 printf("c r %d\n", channel);
244                         flush_output();
245                 }
246         }
247
248         void set_monitor(boolean monitor) {
249                 monitor_mode = monitor;
250                 if (altos != null) {
251                         if (monitor)
252                                 printf("m 1\n");
253                         else
254                                 printf("m 0\n");
255                         flush_output();
256                 }
257         }
258
259         public void set_callsign(String callsign) {
260                 if (altos != null) {
261                         printf ("c c %s\n", callsign);
262                         flush_output();
263                 }
264         }
265
266         public void start_remote() {
267                 if (debug)
268                         System.out.printf("start remote\n");
269                 set_radio();
270                 printf("p\nE 0\n");
271                 flush_input();
272                 remote = true;
273         }
274
275         public void stop_remote() {
276                 if (debug)
277                         System.out.printf("stop remote\n");
278                 flush_input();
279                 printf ("~");
280                 flush_output();
281                 remote = false;
282         }
283
284         public AltosSerial(AltosDevice in_device) throws FileNotFoundException, AltosSerialInUseException {
285                 device = in_device;
286                 line = "";
287                 monitor_mode = false;
288                 monitors = new LinkedList<LinkedBlockingQueue<AltosLine>> ();
289                 reply_queue = new LinkedBlockingQueue<AltosLine> ();
290                 open();
291         }
292 }