altosui: discard invalid lines while reading Eeprom flight data
[fw/altos] / ao-tools / 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.LinkedBlockingQueue;
27 import java.util.LinkedList;
28 import java.util.Iterator;
29 import altosui.AltosSerialMonitor;
30 import libaltosJNI.libaltos;
31 import libaltosJNI.altos_device;
32 import libaltosJNI.SWIGTYPE_p_altos_file;
33 import libaltosJNI.SWIGTYPE_p_altos_list;
34
35 /*
36  * This class reads from the serial port and places each received
37  * line in a queue. Dealing with that queue is left up to other
38  * threads.
39  */
40
41 public class AltosSerial implements Runnable {
42
43         SWIGTYPE_p_altos_file altos;
44         LinkedList<LinkedBlockingQueue<String>> monitors;
45         LinkedBlockingQueue<String> reply_queue;
46         Thread input_thread;
47         String line;
48         byte[] line_bytes;
49         int line_count;
50         boolean monitor_mode;
51
52         public void run () {
53                 int c;
54
55                 try {
56                         for (;;) {
57                                 c = libaltos.altos_getchar(altos, 0);
58                                 if (Thread.interrupted())
59                                         break;
60                                 if (c == -1)
61                                         continue;
62                                 if (c == '\r')
63                                         continue;
64                                 synchronized(this) {
65                                         if (c == '\n') {
66                                                 if (line_count != 0) {
67                                                         try {
68                                                                 line = new String(line_bytes, 0, line_count, "UTF-8");
69                                                         } catch (UnsupportedEncodingException ue) {
70                                                                 line = "";
71                                                                 for (int i = 0; i < line_count; i++)
72                                                                         line = line + line_bytes[i];
73                                                         }
74                                                         if (line.startsWith("VERSION")) {
75                                                                 for (int e = 0; e < monitors.size(); e++) {
76                                                                         LinkedBlockingQueue<String> q = monitors.get(e);
77                                                                         q.put(line);
78                                                                 }
79                                                         } else {
80 //                                                              System.out.printf("GOT: %s\n", line);
81                                                                 reply_queue.put(line);
82                                                         }
83                                                         line_count = 0;
84                                                         line = "";
85                                                 }
86                                         } else {
87                                                 if (line_bytes == null) {
88                                                         line_bytes = new byte[256];
89                                                 } else if (line_count == line_bytes.length) {
90                                                         byte[] new_line_bytes = new byte[line_count * 2];
91                                                         System.arraycopy(line_bytes, 0, new_line_bytes, 0, line_count);
92                                                         line_bytes = new_line_bytes;
93                                                 }
94                                                 line_bytes[line_count] = (byte) c;
95                                                 line_count++;
96                                         }
97                                 }
98                         }
99                 } catch (InterruptedException e) {
100                 }
101         }
102
103         public void flush_output() {
104                 libaltos.altos_flush(altos);
105         }
106
107         public void flush_input() {
108                 flush_output();
109                 try {
110                         Thread.sleep(200);
111                 } catch (InterruptedException ie) {
112                 }
113                 synchronized(this) {
114                         if (!"VERSION".startsWith(line) &&
115                             !line.startsWith("VERSION"))
116                                 line = "";
117                         reply_queue.clear();
118                 }
119         }
120
121         public String get_reply() throws InterruptedException {
122                 flush_output();
123                 String line = reply_queue.take();
124                 return line;
125         }
126
127         public void add_monitor(LinkedBlockingQueue<String> q) {
128                 set_monitor(true);
129                 monitors.add(q);
130         }
131
132         public void remove_monitor(LinkedBlockingQueue<String> q) {
133                 monitors.remove(q);
134                 if (monitors.isEmpty())
135                         set_monitor(false);
136         }
137
138         public boolean opened() {
139                 return altos != null;
140         }
141
142         public void close() {
143                 if (altos != null) {
144                         libaltos.altos_close(altos);
145                 }
146                 if (input_thread != null) {
147                         try {
148                                 input_thread.interrupt();
149                                 input_thread.join();
150                         } catch (InterruptedException e) {
151                         }
152                         input_thread = null;
153                 }
154                 if (altos != null) {
155                         libaltos.altos_free(altos);
156                         altos = null;
157                 }
158         }
159
160         public void putc(char c) {
161                 if (altos != null)
162                         libaltos.altos_putchar(altos, c);
163         }
164
165         public void print(String data) {
166 //              System.out.printf("\"%s\" ", data);
167                 for (int i = 0; i < data.length(); i++)
168                         putc(data.charAt(i));
169         }
170
171         public void printf(String format, Object ... arguments) {
172                 print(String.format(format, arguments));
173         }
174
175         public void open(altos_device device) throws FileNotFoundException {
176                 close();
177                 altos = libaltos.altos_open(device);
178                 if (altos == null)
179                         throw new FileNotFoundException(device.getPath());
180                 input_thread = new Thread(this);
181                 input_thread.start();
182                 print("~\nE 0\n");
183                 set_monitor(monitor_mode);
184                 flush_input();
185         }
186
187         public void set_channel(int channel) {
188                 if (altos != null) {
189                         if (monitor_mode)
190                                 printf("m 0\nc r %d\nm 1\n", channel);
191                         else
192                                 printf("c r %d\n", channel);
193                 }
194         }
195
196         void set_monitor(boolean monitor) {
197                 monitor_mode = monitor;
198                 if (altos != null) {
199                         if (monitor)
200                                 printf("m 1\n");
201                         else
202                                 printf("m 0\n");
203                 }
204         }
205
206         public void set_callsign(String callsign) {
207                 if (altos != null)
208                         printf ("c c %s\n", callsign);
209         }
210
211         public AltosSerial() {
212                 altos = null;
213                 input_thread = null;
214                 line = "";
215                 monitor_mode = false;
216                 monitors = new LinkedList<LinkedBlockingQueue<String>> ();
217                 reply_queue = new LinkedBlockingQueue<String> ();
218         }
219 }