bec518516f9113ac7b2c5749ba39fd872b97c947
[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.util.Log;
26 import android.os.Handler;
27
28 import org.altusmetrum.altoslib_5.*;
29
30
31 public class TelemetryReader extends Thread {
32
33         private static final String TAG = "TelemetryReader";
34
35         int         crc_errors;
36
37         Handler     handler;
38
39         AltosLink   link;
40         AltosState  state = null;
41
42         AltosFlightReader       stacked;
43
44         LinkedBlockingQueue<AltosLine> telemQueue;
45
46         public AltosState read() throws ParseException, AltosCRCException, InterruptedException, IOException {
47                 if (stacked != null) {
48                         state = stacked.read();
49                         if (state != null)
50                                 return state;
51                         stacked = null;
52                 }
53                 AltosLine l = telemQueue.take();
54                 if (l.line == null)
55                         throw new IOException("IO error");
56                 AltosTelemetry telem = AltosTelemetryLegacy.parse(l.line);
57                 if (state == null)
58                         state = new AltosState();
59                 else
60                         state = state.clone();
61                 telem.update_state(state);
62                 return state;
63         }
64
65         public void close() {
66                 state = null;
67                 if (stacked != null) {
68                         stacked.close(false);
69                         stacked = null;
70                 }
71                 link.remove_monitor(telemQueue);
72                 link = null;
73                 telemQueue.clear();
74                 telemQueue = null;
75         }
76
77         public void run() {
78                 AltosState  state = null;
79
80                 try {
81                         while (telemQueue != null) {
82                                 try {
83                                         state = read();
84                                         handler.obtainMessage(TelemetryService.MSG_TELEMETRY, state).sendToTarget();
85                                 } catch (ParseException pp) {
86                                         Log.e(TAG, String.format("Parse error: %d \"%s\"", pp.getErrorOffset(), pp.getMessage()));
87                                 } catch (AltosCRCException ce) {
88                                         ++crc_errors;
89                                         handler.obtainMessage(TelemetryService.MSG_CRC_ERROR, new Integer(crc_errors)).sendToTarget();
90                                 }
91                         }
92                 } catch (InterruptedException ee) {
93                 } catch (IOException ie) {
94                 } finally {
95                         close();
96                 }
97         }
98
99         public TelemetryReader (AltosLink in_link, Handler in_handler, AltosFlightReader in_stacked) {
100                 link    = in_link;
101                 handler = in_handler;
102                 stacked = in_stacked;
103
104                 state = null;
105                 telemQueue = new LinkedBlockingQueue<AltosLine>();
106                 link.add_monitor(telemQueue);
107                 try {
108                         link.set_radio_frequency(AltosPreferences.frequency(link.serial));
109                         link.set_telemetry(AltosLib.ao_telemetry_standard);
110                         link.set_telemetry_rate(AltosPreferences.telemetry_rate(link.serial));
111                 } catch (InterruptedException ee) {
112                         close();
113                 } catch (TimeoutException te) {
114                         close();
115                 }
116         }
117
118         private static AltosFlightReader existing_data(AltosLink link) {
119                 if (link == null)
120                         return null;
121
122                 File    file = AltosPreferences.logfile(link.serial);
123                 if (file != null) {
124                         AltosStateIterable      iterable = AltosStateIterable.iterable(file);
125                         if (iterable != null)
126                                 return new AltosReplayReader(iterable.iterator(), file, false);
127                 }
128                 return null;
129         }
130
131         public TelemetryReader(AltosLink link, Handler handler) {
132                 this(link, handler, existing_data(link));
133         }
134 }