Switch from GPLv2 to GPLv2+
[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; either version 2 of the License, or
7  * (at your option) any later version.
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 package org.altusmetrum.altoslib_11;
20
21 import java.text.*;
22 import java.io.*;
23 import java.util.concurrent.*;
24
25 public class AltosTelemetryReader extends AltosFlightReader {
26         AltosLink       link;
27         AltosLog        log;
28         double          frequency;
29         int             telemetry;
30         int             telemetry_rate;
31         AltosState      state = null;
32
33         LinkedBlockingQueue<AltosLine> telem;
34
35         public AltosState read() throws InterruptedException, ParseException, AltosCRCException, IOException {
36                 AltosLine l;
37                 do {
38                         l = telem.take();
39                         if (l.line == null)
40                                 throw new IOException("IO error");
41                 } while (!link.get_monitor());
42                 AltosTelemetry  telem = AltosTelemetry.parse(l.line);
43                 if (state == null)
44                         state = new AltosState();
45                 else
46                         state = state.clone();
47                 telem.update_state(state);
48                 return state;
49         }
50
51         public void flush() {
52                 telem.clear();
53         }
54
55         public void reset() {
56                 flush();
57                 state = null;
58         }
59
60         public void close(boolean interrupted) {
61
62                 link.remove_monitor(telem);
63                 log.close();
64                 try {
65                         link.close();
66                 } catch (InterruptedException ie) {
67                 }
68         }
69
70         public void set_frequency(double in_frequency) throws InterruptedException, TimeoutException {
71                 frequency = in_frequency;
72                 link.set_radio_frequency(frequency);
73         }
74
75         public boolean supports_telemetry(int telemetry) {
76
77                 try {
78                         /* Version 1.0 or later firmware supports all telemetry formats */
79                         if (link.config_data().compare_version("1.0") >= 0)
80                                 return true;
81
82                         /* Version 0.9 firmware only supports 0.9 telemetry */
83                         if (link.config_data().compare_version("0.9") >= 0) {
84                                 if (telemetry == AltosLib.ao_telemetry_0_9)
85                                         return true;
86                                 else
87                                         return false;
88                         }
89
90                         /* Version 0.8 firmware only supports 0.8 telemetry */
91                         if (telemetry == AltosLib.ao_telemetry_0_8)
92                                 return true;
93                         else
94                                 return false;
95                 } catch (InterruptedException ie) {
96                         return false;
97                 } catch (TimeoutException te) {
98                         return true;
99                 }
100         }
101
102         public boolean supports_telemetry_rate(int telemetry_rate) {
103                 try {
104                         /* Version 1.4.1.1 supports all rates, older versions don't */
105                         if (link.config_data().compare_version("1.4.1.1") >= 0)
106                                 return true;
107
108                         if (telemetry_rate == AltosLib.ao_telemetry_rate_38400)
109                                 return true;
110                         else
111                                 return false;
112                 } catch (InterruptedException ie) {
113                         return false;
114                 } catch (TimeoutException te) {
115                         return true;
116                 }
117         }
118
119         public void save_frequency() {
120                 AltosPreferences.set_frequency(link.serial, frequency);
121         }
122
123         public void set_telemetry(int in_telemetry) {
124                 telemetry = in_telemetry;
125                 link.set_telemetry(telemetry);
126         }
127
128         public void save_telemetry() {
129                 AltosPreferences.set_telemetry(link.serial, telemetry);
130         }
131
132         public void set_telemetry_rate(int in_telemetry_rate) {
133                 telemetry_rate = in_telemetry_rate;
134                 link.set_telemetry_rate(telemetry_rate);
135         }
136
137         public void save_telemetry_rate() {
138                 AltosPreferences.set_telemetry_rate(link.serial, telemetry_rate);
139         }
140
141         public void set_monitor(boolean monitor) {
142                 link.set_monitor(monitor);
143         }
144
145         public File backing_file() {
146                 return log.file();
147         }
148
149         public boolean has_monitor_battery() {
150                 return link.has_monitor_battery();
151         }
152
153         public double monitor_battery() throws InterruptedException {
154                 return link.monitor_battery();
155         }
156
157         public AltosTelemetryReader (AltosLink in_link)
158                 throws IOException, InterruptedException, TimeoutException {
159                 link = in_link;
160                 boolean success = false;
161                 try {
162                         log = new AltosLog(link);
163                         name = link.name;
164                         telem = new LinkedBlockingQueue<AltosLine>();
165                         frequency = AltosPreferences.frequency(link.serial);
166                         set_frequency(frequency);
167                         telemetry = AltosPreferences.telemetry(link.serial);
168                         set_telemetry(telemetry);
169                         telemetry_rate = AltosPreferences.telemetry_rate(link.serial);
170                         set_telemetry_rate(telemetry_rate);
171                         link.add_monitor(telem);
172                         success = true;
173                 } finally {
174                         if (!success)
175                                 close(true);
176                 }
177         }
178 }