cdd1decdc68b9189a77812b6676c5dcee204d76d
[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_2.*;\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         AltosState  state = null;\r
41 \r
42         LinkedBlockingQueue<AltosLine> telemQueue;\r
43 \r
44         public AltosState read() throws ParseException, AltosCRCException, InterruptedException, IOException {\r
45                 AltosLine l = telemQueue.take();\r
46                 if (l.line == null)\r
47                         throw new IOException("IO error");\r
48                 AltosTelemetry telem = AltosTelemetryLegacy.parse(l.line);\r
49                 if (state == null)\r
50                         state = new AltosState();\r
51                 else\r
52                         state = state.clone();\r
53                 telem.update_state(state);\r
54                 return state;\r
55         }\r
56 \r
57         public void close() {\r
58                 state = null;\r
59                 link.remove_monitor(telemQueue);\r
60                 link = null;\r
61                 telemQueue.clear();\r
62                 telemQueue = null;\r
63         }\r
64 \r
65         public void run() {\r
66                 AltosState  state = null;\r
67 \r
68                 try {\r
69                         for (;;) {\r
70                                 try {\r
71                                         state = read();\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                                         handler.obtainMessage(TelemetryService.MSG_CRC_ERROR, new Integer(crc_errors)).sendToTarget();\r
78                                 }\r
79                         }\r
80                 } catch (InterruptedException ee) {\r
81                 } catch (IOException ie) {\r
82                 } finally {\r
83                         close();\r
84                 }\r
85         }\r
86 \r
87         public TelemetryReader (AltosLink in_link, Handler in_handler) {\r
88                 link    = in_link;\r
89                 handler = in_handler;\r
90 \r
91                 state = null;\r
92                 telemQueue = new LinkedBlockingQueue<AltosLine>();\r
93                 link.add_monitor(telemQueue);\r
94         }\r
95 }\r