altos/test: Adjust CRC error rate after FEC fix
[fw/altos] / altoslib / AltosEepromDownload.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_14;
20
21 import java.io.*;
22 import java.util.*;
23 import java.text.*;
24 import java.util.concurrent.*;
25
26 class AltosEepromNameData extends AltosDataListener {
27         AltosGPS        gps = null;
28
29         boolean avoid_duplicate_files = false;
30
31         public void set_rssi(int rssi, int status) { }
32         public void set_received_time(long received_time) { }
33
34         public void set_acceleration(double accel) { }
35         public void set_pressure(double pa) { }
36         public void set_thrust(double N) { }
37
38         public void set_temperature(double deg_c) { }
39         public void set_battery_voltage(double volts) { }
40
41         public void set_apogee_voltage(double volts) { }
42         public void set_main_voltage(double volts) { }
43
44         public void set_avoid_duplicate_files() {
45                 avoid_duplicate_files = true;
46         }
47
48         public void set_gps(AltosGPS gps, boolean set_location, boolean set_sats) {
49                 super.set_gps(gps, set_location, set_sats);
50                 if (gps != null &&
51                     gps.year != AltosLib.MISSING &&
52                     gps.month != AltosLib.MISSING &&
53                     gps.day != AltosLib.MISSING) {
54                         this.gps = gps;
55                 }
56         }
57
58         public boolean done() {
59                 if (gps == null)
60                         return false;
61                 return true;
62         }
63
64         public void set_gyro(double roll, double pitch, double yaw) { }
65         public void set_accel_ground(double along, double across, double through) { }
66         public void set_accel(double along, double across, double through) { }
67         public void set_mag(double along, double across, double through) { }
68         public void set_pyro_voltage(double volts) { }
69         public void set_igniter_voltage(double[] voltage) { }
70         public void set_pyro_fired(int pyro_mask) { }
71         public void set_companion(AltosCompanion companion) { }
72         public void set_kalman(double height, double speed, double acceleration) { }
73         public void set_orient(double new_orient) { }
74         public void set_motor_pressure(double motor_pressure) { }
75
76         public AltosEepromNameData(AltosCalData cal_data) {
77                 super(cal_data);
78         }
79 }
80
81 public class AltosEepromDownload implements Runnable {
82
83         AltosLink               link;
84         boolean                 remote;
85         Thread                  eeprom_thread;
86         AltosEepromMonitor      monitor;
87
88         AltosEepromList         flights;
89         String                  parse_errors;
90
91         private boolean has_gps_date(AltosState state) {
92                 if (state == null)
93                         return false;
94
95                 AltosGPS gps = state.gps;
96
97                 return gps != null &&
98                         gps.year != AltosLib.MISSING &&
99                         gps.month != AltosLib.MISSING &&
100                         gps.day != AltosLib.MISSING;
101         }
102
103         private AltosFile MakeFile(int serial, int flight, AltosEepromNameData name_data) throws IOException {
104                 AltosFile               eeprom_name;
105
106                 for (;;) {
107                         if (name_data.gps != null) {
108                                 AltosGPS                gps = name_data.gps;
109                                 eeprom_name = new AltosFile(gps.year, gps.month, gps.day,
110                                                             serial, flight, "eeprom");
111                         } else
112                                 eeprom_name = new AltosFile(serial, flight, "eeprom");
113                         if (!name_data.avoid_duplicate_files)
114                                 break;
115                         if (!eeprom_name.exists())
116                                 break;
117                         flight++;
118                 }
119
120                 return eeprom_name;
121         }
122
123         boolean                 done;
124         int                     prev_state;
125         int                     state_block;
126
127         void LogError(String error) {
128                 if (parse_errors != null)
129                         parse_errors.concat(error.concat("\n"));
130                 else
131                         parse_errors = error;
132         }
133
134         class BlockCache extends Hashtable<Integer,AltosEepromChunk> {
135                 AltosEepromLog  log;
136
137                 AltosEepromChunk get(int start, boolean add) throws TimeoutException, InterruptedException {
138                         if (contains(start))
139                                 return super.get(start);
140                         AltosEepromChunk eechunk = new AltosEepromChunk(link, start, start == log.start_block);
141                         if (add)
142                                 put(start, eechunk);
143                         return eechunk;
144                 }
145
146                 public BlockCache(AltosEepromLog log) {
147                         this.log = log;
148                 }
149         }
150
151         int FindLastLog(AltosEepromLog log, BlockCache cache) throws TimeoutException, InterruptedException {
152                 int     low = log.start_block;
153                 int     high = log.end_block - 1;
154
155                 while (low <= high) {
156                         int mid = (high + low) / 2;
157
158                         if (!cache.get(mid, true).erased())
159                                 low = mid + 1;
160                         else
161                                 high = mid - 1;
162                 }
163                 return low;
164         }
165
166         void CaptureLog(AltosEepromLog log) throws IOException, InterruptedException, TimeoutException, ParseException {
167                 int                     block, state_block = 0;
168                 int                     log_format = flights.config_data.log_format;
169                 BlockCache              cache = new BlockCache(log);
170
171                 done = false;
172
173                 if (flights.config_data.serial < 0)
174                         throw new IOException("no serial number found");
175
176                 /* Set serial number in the monitor dialog window */
177                 monitor.set_serial(log.serial);
178                 monitor.set_flight(log.flight);
179
180                 int     start_block = log.start_block;
181                 int     end_block = FindLastLog(log, cache);
182
183                 monitor.set_max(end_block - start_block - 1);
184
185                 ArrayList<Byte> data = new ArrayList<Byte>();
186
187                 /* Now scan the eeprom, reading blocks of data to create a byte array of data */
188
189                 for (block = start_block; block < end_block; block++) {
190                         monitor.set_block(block - start_block);
191
192                         AltosEepromChunk        eechunk = cache.get(block, false);
193
194                         for (int i = 0; i < eechunk.data.length; i++)
195                                 data.add((byte) eechunk.data[i]);
196                 }
197
198                 /* Construct our internal representation of the eeprom data */
199                 AltosEeprom     eeprom = new AltosEeprom(flights.config_data, data);
200
201                 /* Now see if we can't actually parse the resulting
202                  * file to generate a better filename. Note that this
203                  * doesn't need to work; we'll still save the data using
204                  * a less accurate name.
205                  */
206                 AltosEepromRecordSet            set = new AltosEepromRecordSet(eeprom);
207                 AltosEepromNameData name_data = new AltosEepromNameData(set.cal_data());
208
209                 for (AltosEepromRecord record : set.ordered) {
210                         record.provide_data(name_data, set.cal_data());
211                         if (name_data.done())
212                                 break;
213                 }
214
215                 AltosFile f = MakeFile(flights.config_data.serial, log.flight, name_data);
216
217                 log.set_file(f);
218
219                 boolean do_write = true;
220
221                 if (f.exists())
222                         do_write = monitor.check_overwrite(f);
223
224                 if (do_write) {
225                         FileWriter w = new FileWriter(f);
226
227                         eeprom.write(w);
228                         w.close();
229                 }
230
231                 if (eeprom.errors != 0)
232                         throw new ParseException(String.format("%d CRC Errors", eeprom.errors), 0);
233         }
234
235         static String label(int flight) {
236                 if (flight < 0)
237                         return "Corrupt";
238                 else
239                         return "Flight";
240         }
241
242         static int flight(int flight) {
243                 if (flight < 0)
244                         return -flight;
245                 return flight;
246         }
247
248         public void run () {
249                 boolean success = false;
250
251                 try {
252                         if (remote)
253                                 link.start_remote();
254
255                         for (AltosEepromLog log : flights) {
256                                 parse_errors = null;
257                                 if (log.download_selected) {
258                                         monitor.reset();
259                                         try {
260                                                 CaptureLog(log);
261                                         } catch (ParseException e) {
262                                                 LogError(e.getMessage());
263                                         }
264                                 }
265                                 success = true;
266                                 if (parse_errors != null) {
267                                         monitor.show_message(String.format("%s %d download error. Valid log data saved\n%s",
268                                                                            label(log.flight),
269                                                                            flight(log.flight),
270                                                                            parse_errors),
271                                                              link.name,
272                                                              AltosEepromMonitor.WARNING_MESSAGE);
273                                 }
274                         }
275                 } catch (IOException ee) {
276                         monitor.show_message(ee.getLocalizedMessage(),
277                                              link.name,
278                                              AltosEepromMonitor.ERROR_MESSAGE);
279                 } catch (InterruptedException ie) {
280                         monitor.show_message(String.format("Connection to \"%s\" interrupted",
281                                                            link.name),
282                                              "Connection Interrupted",
283                                              AltosEepromMonitor.ERROR_MESSAGE);
284                 } catch (TimeoutException te) {
285                         monitor.show_message(String.format("Connection to \"%s\" failed",
286                                                            link.name),
287                                              "Connection Failed",
288                                              AltosEepromMonitor.ERROR_MESSAGE);
289                 } finally {
290                         if (remote) {
291                                 try {
292                                         link.stop_remote();
293                                 } catch (InterruptedException ie) {
294                                 }
295                         }
296                         link.flush_output();
297                 }
298                 monitor.done(success);
299         }
300
301         public void start() {
302                 eeprom_thread = new Thread(this);
303                 monitor.set_thread(eeprom_thread);
304                 eeprom_thread.start();
305         }
306
307         public AltosEepromDownload(AltosEepromMonitor given_monitor,
308                                    AltosLink given_link,
309                                    boolean given_remote,
310                                    AltosEepromList given_flights) {
311
312                 monitor = given_monitor;
313                 link = given_link;
314                 remote = given_remote;
315                 flights = given_flights;
316
317                 monitor.start();
318         }
319 }