5b47960f79a28f8d7e4bd92977147b6eaad30587
[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
51         public void run () {
52                 int c;
53
54                 try {
55                         for (;;) {
56                                 c = libaltos.altos_getchar(altos, 0);
57                                 if (Thread.interrupted())
58                                         break;
59                                 if (c == -1)
60                                         continue;
61                                 if (c == '\r')
62                                         continue;
63                                 synchronized(this) {
64                                         if (c == '\n') {
65                                                 if (line_count != 0) {
66                                                         try {
67                                                                 line = new String(line_bytes, 0, line_count, "UTF-8");
68                                                         } catch (UnsupportedEncodingException ue) {
69                                                                 line = "";
70                                                                 for (int i = 0; i < line_count; i++)
71                                                                         line = line + line_bytes[i];
72                                                         }
73                                                         if (line.startsWith("VERSION")) {
74                                                                 for (int e = 0; e < monitors.size(); e++) {
75                                                                         LinkedBlockingQueue<String> q = monitors.get(e);
76                                                                         q.put(line);
77                                                                 }
78                                                         } else {
79 //                                                              System.out.printf("GOT: %s\n", line);
80                                                                 reply_queue.put(line);
81                                                         }
82                                                         line_count = 0;
83                                                         line = "";
84                                                 }
85                                         } else {
86                                                 if (line_bytes == null) {
87                                                         line_bytes = new byte[256];
88                                                 } else if (line_count == line_bytes.length) {
89                                                         byte[] new_line_bytes = new byte[line_count * 2];
90                                                         System.arraycopy(line_bytes, 0, new_line_bytes, 0, line_count);
91                                                         line_bytes = new_line_bytes;
92                                                 }
93                                                 line_bytes[line_count] = (byte) c;
94                                                 line_count++;
95                                         }
96                                 }
97                         }
98                 } catch (InterruptedException e) {
99                 }
100         }
101
102         public void flush_reply() {
103                 libaltos.altos_flush(altos);
104                 try {
105                         Thread.sleep(100);
106                 } catch (InterruptedException ie) {
107                 }
108                 reply_queue.clear();
109         }
110
111         public String get_reply() throws InterruptedException {
112                 libaltos.altos_flush(altos);
113                 String line = reply_queue.take();
114                 return line;
115         }
116
117         public void add_monitor(LinkedBlockingQueue<String> q) {
118                 monitors.add(q);
119         }
120
121         public void remove_monitor(LinkedBlockingQueue<String> q) {
122                 monitors.remove(q);
123         }
124
125         public void flush () {
126                 synchronized(this) {
127                         if (!"VERSION".startsWith(line) && !line.startsWith("VERSION"))
128                                 line = "";
129                         reply_queue.clear();
130                 }
131         }
132
133         public boolean opened() {
134                 return altos != null;
135         }
136
137         public void close() {
138                 if (altos != null)
139                         libaltos.altos_close(altos);
140                 if (input_thread != null) {
141                         try {
142                                 input_thread.interrupt();
143                                 input_thread.join();
144                         } catch (InterruptedException e) {
145                         }
146                         input_thread = null;
147                 }
148                 if (altos != null) {
149                         libaltos.altos_free(altos);
150                         altos = null;
151                 }
152         }
153
154         public void putc(char c) {
155                 if (altos != null)
156                         libaltos.altos_putchar(altos, c);
157         }
158
159         public void print(String data) {
160 //h             System.out.printf("\"%s\" ", data);
161                 for (int i = 0; i < data.length(); i++)
162                         putc(data.charAt(i));
163         }
164
165         public void printf(String format, Object ... arguments) {
166                 print(String.format(format, arguments));
167         }
168
169         public void open(altos_device device) throws FileNotFoundException {
170                 close();
171                 altos = libaltos.altos_open(device);
172                 if (altos == null)
173                         throw new FileNotFoundException(device.getPath());
174                 input_thread = new Thread(this);
175                 input_thread.start();
176                 print("\nE 0\n");
177                 try {
178                         Thread.sleep(200);
179                 } catch (InterruptedException e) {
180                 }
181                 flush();
182         }
183
184         public void set_channel(int channel) {
185                 if (altos != null)
186                         printf("m 0\nc r %d\nm 1\n", channel);
187         }
188
189         public void set_callsign(String callsign) {
190                 if (altos != null)
191                         printf ("c c %s\n", callsign);
192         }
193
194         public AltosSerial() {
195                 altos = null;
196                 input_thread = null;
197                 line = "";
198                 monitors = new LinkedList<LinkedBlockingQueue<String>> ();
199                 reply_queue = new LinkedBlockingQueue<String> ();
200         }
201 }