20526a2c584b95d4780a54cfae4a5ef73fa2718f
[fw/altos] / altoslib / AltosTelemetryReader.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 package org.altusmetrum.altoslib_5;
19
20 import java.text.*;
21 import java.io.*;
22 import java.util.concurrent.*;
23
24 public class AltosTelemetryReader extends AltosFlightReader {
25         AltosLink       link;
26         AltosLog        log;
27         double          frequency;
28         int             telemetry;
29         int             telemetry_rate;
30         AltosState      state = null;
31         AltosFlightReader       stacked;
32
33         LinkedBlockingQueue<AltosLine> telem;
34
35         public AltosState read() throws InterruptedException, ParseException, AltosCRCException, IOException {
36                 if (stacked != null) {
37                         state = stacked.read();
38                         if (state != null)
39                                 return state;
40                         stacked = null;
41                 }
42                 AltosLine l = telem.take();
43                 if (l.line == null)
44                         throw new IOException("IO error");
45                 AltosTelemetry  telem = AltosTelemetry.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 flush() {
55                 telem.clear();
56         }
57
58         public void reset() {
59                 flush();
60         }
61
62         public void close(boolean interrupted) {
63
64                 if (stacked != null) {
65                         stacked.close(interrupted);
66                         stacked = null;
67                 }
68
69                 link.remove_monitor(telem);
70                 log.close();
71                 try {
72                         link.close();
73                 } catch (InterruptedException ie) {
74                 }
75         }
76
77         public void set_frequency(double in_frequency) throws InterruptedException, TimeoutException {
78                 frequency = in_frequency;
79                 link.set_radio_frequency(frequency);
80         }
81
82         public boolean supports_telemetry(int telemetry) {
83
84                 try {
85                         /* Version 1.0 or later firmware supports all telemetry formats */
86                         if (link.config_data().compare_version("1.0") >= 0)
87                                 return true;
88
89                         /* Version 0.9 firmware only supports 0.9 telemetry */
90                         if (link.config_data().compare_version("0.9") >= 0) {
91                                 if (telemetry == AltosLib.ao_telemetry_0_9)
92                                         return true;
93                                 else
94                                         return false;
95                         }
96
97                         /* Version 0.8 firmware only supports 0.8 telemetry */
98                         if (telemetry == AltosLib.ao_telemetry_0_8)
99                                 return true;
100                         else
101                                 return false;
102                 } catch (InterruptedException ie) {
103                         return false;
104                 } catch (TimeoutException te) {
105                         return true;
106                 }
107         }
108
109         public boolean supports_telemetry_rate(int telemetry_rate) {
110                 try {
111                         /* Version 1.4.1.1 supports all rates, older versions don't */
112                         if (link.config_data().compare_version("1.4.1.1") >= 0)
113                                 return true;
114
115                         if (telemetry_rate == AltosLib.ao_telemetry_rate_38400)
116                                 return true;
117                         else
118                                 return false;
119                 } catch (InterruptedException ie) {
120                         return false;
121                 } catch (TimeoutException te) {
122                         return true;
123                 }
124         }
125
126         public void save_frequency() {
127                 AltosPreferences.set_frequency(link.serial, frequency);
128         }
129
130         public void set_telemetry(int in_telemetry) {
131                 telemetry = in_telemetry;
132                 link.set_telemetry(telemetry);
133         }
134
135         public void save_telemetry() {
136                 AltosPreferences.set_telemetry(link.serial, telemetry);
137         }
138
139         public void set_telemetry_rate(int in_telemetry_rate) {
140                 telemetry_rate = in_telemetry_rate;
141                 link.set_telemetry_rate(telemetry_rate);
142         }
143
144         public void save_telemetry_rate() {
145                 AltosPreferences.set_telemetry_rate(link.serial, telemetry_rate);
146         }
147
148         public void set_monitor(boolean monitor) {
149                 link.set_monitor(monitor);
150         }
151
152         public File backing_file() {
153                 return log.file();
154         }
155
156         public boolean has_monitor_battery() {
157                 return link.has_monitor_battery();
158         }
159
160         public double monitor_battery() throws InterruptedException {
161                 return link.monitor_battery();
162         }
163
164         public AltosTelemetryReader (AltosLink in_link, AltosFlightReader in_stacked)
165                 throws IOException, InterruptedException, TimeoutException {
166                 link = in_link;
167                 stacked = in_stacked;
168                 boolean success = false;
169                 try {
170                         log = new AltosLog(link);
171                         name = link.name;
172                         telem = new LinkedBlockingQueue<AltosLine>();
173                         frequency = AltosPreferences.frequency(link.serial);
174                         set_frequency(frequency);
175                         telemetry = AltosPreferences.telemetry(link.serial);
176                         set_telemetry(telemetry);
177                         telemetry_rate = AltosPreferences.telemetry_rate(link.serial);
178                         set_telemetry_rate(telemetry_rate);
179                         link.add_monitor(telem);
180                         success = true;
181                 } finally {
182                         if (!success)
183                                 close(true);
184                 }
185         }
186
187         private static AltosFlightReader existing_data(AltosLink link) {
188                 if (link == null)
189                         return null;
190
191                 File    file = AltosPreferences.logfile(link.serial);
192                 if (file != null) {
193                         AltosStateIterable      iterable = AltosStateIterable.iterable(file);
194                         if (iterable != null)
195                                 return new AltosReplayReader(iterable.iterator(), file, false);
196                 }
197                 return null;
198         }
199
200         public AltosTelemetryReader(AltosLink link)
201                 throws IOException, InterruptedException, TimeoutException {
202                 this(link, null);
203         }
204 }