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