Merge remote-tracking branch 'mjb/master'
[fw/altos] / altosdroid / src / org / altusmetrum / AltosDroid / TelemetryReader.java
1 /*\r
2  * Copyright © 2011 Keith Packard <keithp@keithp.com>\r
3  * Copyright © 2012 Mike Beattie <mike@ethernal.org>\r
4  *\r
5  * This program is free software; you can redistribute it and/or modify\r
6  * it under the terms of the GNU General Public License as published by\r
7  * the Free Software Foundation; version 2 of the License.\r
8  *\r
9  * This program is distributed in the hope that it will be useful, but\r
10  * WITHOUT ANY WARRANTY; without even the implied warranty of\r
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\r
12  * General Public License for more details.\r
13  *\r
14  * You should have received a copy of the GNU General Public License along\r
15  * with this program; if not, write to the Free Software Foundation, Inc.,\r
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.\r
17  */\r
18 \r
19 \r
20 package org.altusmetrum.AltosDroid;\r
21 \r
22 import java.text.*;\r
23 import java.io.*;\r
24 import java.util.concurrent.*;\r
25 import android.util.Log;\r
26 import android.os.Handler;\r
27 \r
28 import org.altusmetrum.altoslib_1.*;\r
29 \r
30 \r
31 public class TelemetryReader extends Thread {\r
32 \r
33         private static final String TAG = "TelemetryReader";\r
34 \r
35         int         crc_errors;\r
36 \r
37         Handler     handler;\r
38 \r
39         AltosLink   link;\r
40         AltosRecord previous;\r
41 \r
42         LinkedBlockingQueue<AltosLine> telem;\r
43 \r
44         public AltosRecord read() throws ParseException, AltosCRCException, InterruptedException, IOException {\r
45                 AltosLine l = telem.take();\r
46                 if (l.line == null)\r
47                         throw new IOException("IO error");\r
48                 AltosRecord     next = AltosTelemetry.parse(l.line, previous);\r
49                 previous = next;\r
50                 return next;\r
51         }\r
52 \r
53         public void close() {\r
54                 previous = null;\r
55                 link.remove_monitor(telem);\r
56                 link = null;\r
57                 telem.clear();\r
58                 telem = null;\r
59         }\r
60 \r
61         public void run() {\r
62                 AltosState  state = null;\r
63 \r
64                 try {\r
65                         for (;;) {\r
66                                 try {\r
67                                         AltosRecord record = read();\r
68                                         if (record == null)\r
69                                                 break;\r
70                                         state = new AltosState(record, state);\r
71 \r
72                                         handler.obtainMessage(TelemetryService.MSG_TELEMETRY, state).sendToTarget();\r
73                                 } catch (ParseException pp) {\r
74                                         Log.e(TAG, String.format("Parse error: %d \"%s\"", pp.getErrorOffset(), pp.getMessage()));\r
75                                 } catch (AltosCRCException ce) {\r
76                                         ++crc_errors;\r
77                                 }\r
78                         }\r
79                 } catch (InterruptedException ee) {\r
80                 } catch (IOException ie) {\r
81                 } finally {\r
82                         close();\r
83                 }\r
84         }\r
85 \r
86         public TelemetryReader (AltosLink in_link, Handler in_handler) {\r
87                 link    = in_link;\r
88                 handler = in_handler;\r
89 \r
90                 previous = null;\r
91                 telem = new LinkedBlockingQueue<AltosLine>();\r
92                 link.add_monitor(telem);\r
93         }\r
94 }\r