altosui: Catch timeout errors when setting up TD telem monitoring
[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;
19
20 import java.lang.*;
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         AltosRecord     previous;
29         double          frequency;
30         int             telemetry;
31
32         LinkedBlockingQueue<AltosLine> telem;
33
34         public AltosRecord read() throws InterruptedException, ParseException, AltosCRCException, IOException {
35                 AltosLine l = telem.take();
36                 if (l.line == null)
37                         throw new IOException("IO error");
38                 AltosRecord     next = AltosTelemetry.parse(l.line, previous);
39                 previous = next;
40                 return next;
41         }
42
43         public void flush() {
44                 telem.clear();
45         }
46
47         public void close(boolean interrupted) {
48                 link.remove_monitor(telem);
49                 log.close();
50                 link.close();
51         }
52
53         public void set_frequency(double in_frequency) throws InterruptedException, TimeoutException {
54                 frequency = in_frequency;
55                 link.set_radio_frequency(frequency);
56         }
57
58         public boolean supports_telemetry(int telemetry) {
59
60                 try {
61                         /* Version 1.0 or later firmware supports all telemetry formats */
62                         if (link.config_data().compare_version("1.0") >= 0)
63                                 return true;
64
65                         /* Version 0.9 firmware only supports 0.9 telemetry */
66                         if (link.config_data().compare_version("0.9") >= 0) {
67                                 if (telemetry == AltosLib.ao_telemetry_0_9)
68                                         return true;
69                                 else
70                                         return false;
71                         }
72
73                         /* Version 0.8 firmware only supports 0.8 telemetry */
74                         if (telemetry == AltosLib.ao_telemetry_0_8)
75                                 return true;
76                         else
77                                 return false;
78                 } catch (InterruptedException ie) {
79                         return true;
80                 } catch (TimeoutException te) {
81                         return true;
82                 }
83         }
84
85         public void save_frequency() {
86                 AltosPreferences.set_frequency(link.serial, frequency);
87         }
88
89         public void set_telemetry(int in_telemetry) {
90                 telemetry = in_telemetry;
91                 link.set_telemetry(telemetry);
92         }
93
94         public void save_telemetry() {
95                 AltosPreferences.set_telemetry(link.serial, telemetry);
96         }
97
98         public void set_monitor(boolean monitor) {
99                 link.set_monitor(monitor);
100         }
101
102         public File backing_file() {
103                 return log.file();
104         }
105
106         public AltosTelemetryReader (AltosLink in_link)
107                 throws IOException, InterruptedException, TimeoutException {
108                 link = in_link;
109                 try {
110                         log = new AltosLog(link);
111                         name = link.name;
112                         previous = null;
113                         telem = new LinkedBlockingQueue<AltosLine>();
114                         frequency = AltosPreferences.frequency(link.serial);
115                         set_frequency(frequency);
116                         telemetry = AltosPreferences.telemetry(link.serial);
117                         set_telemetry(telemetry);
118                         link.add_monitor(telem);
119                 } catch (TimeoutException e) {
120                         close(true);
121                         throw(e);
122                 } catch (InterruptedException e) {
123                         close(true);
124                         throw(e);
125                 }
126         }
127 }