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