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_13;
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) {
49                 super.set_gps(gps);
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
75         public AltosEepromNameData(AltosCalData cal_data) {
76                 super(cal_data);
77         }
78 }
79
80 public class AltosEepromDownload implements Runnable {
81
82         AltosLink               link;
83         boolean                 remote;
84         Thread                  eeprom_thread;
85         AltosEepromMonitor      monitor;
86
87         AltosEepromList         flights;
88         String                  parse_errors;
89
90         private boolean has_gps_date(AltosState state) {
91                 if (state == null)
92                         return false;
93
94                 AltosGPS gps = state.gps;
95
96                 return gps != null &&
97                         gps.year != AltosLib.MISSING &&
98                         gps.month != AltosLib.MISSING &&
99                         gps.day != AltosLib.MISSING;
100         }
101
102         private AltosFile MakeFile(int serial, int flight, AltosEepromNameData name_data) throws IOException {
103                 AltosFile               eeprom_name;
104
105                 for (;;) {
106                         if (name_data.gps != null) {
107                                 AltosGPS                gps = name_data.gps;
108                                 eeprom_name = new AltosFile(gps.year, gps.month, gps.day,
109                                                             serial, flight, "eeprom");
110                         } else
111                                 eeprom_name = new AltosFile(serial, flight, "eeprom");
112                         if (!name_data.avoid_duplicate_files)
113                                 break;
114                         if (!eeprom_name.exists())
115                                 break;
116                         flight++;
117                 }
118
119                 return eeprom_name;
120         }
121
122         boolean                 done;
123         int                     prev_state;
124         int                     state_block;
125
126         void LogError(String error) {
127                 if (parse_errors != null)
128                         parse_errors.concat(error.concat("\n"));
129                 else
130                         parse_errors = error;
131         }
132
133         class BlockCache extends Hashtable<Integer,AltosEepromChunk> {
134                 AltosEepromLog  log;
135
136                 AltosEepromChunk get(int start, boolean add) throws TimeoutException, InterruptedException {
137                         if (contains(start))
138                                 return super.get(start);
139                         AltosEepromChunk eechunk = new AltosEepromChunk(link, start, start == log.start_block);
140                         if (add)
141                                 put(start, eechunk);
142                         return eechunk;
143                 }
144
145                 public BlockCache(AltosEepromLog log) {
146                         this.log = log;
147                 }
148         }
149
150         int FindLastLog(AltosEepromLog log, BlockCache cache) throws TimeoutException, InterruptedException {
151                 int     low = log.start_block;
152                 int     high = log.end_block - 1;
153
154                 while (low <= high) {
155                         int mid = (high + low) / 2;
156
157                         if (!cache.get(mid, true).erased())
158                                 low = mid + 1;
159                         else
160                                 high = mid - 1;
161                 }
162                 return low;
163         }
164
165         void CaptureLog(AltosEepromLog log) throws IOException, InterruptedException, TimeoutException, ParseException {
166                 int                     block, state_block = 0;
167                 int                     log_format = flights.config_data.log_format;
168                 BlockCache              cache = new BlockCache(log);
169
170                 done = false;
171
172                 if (flights.config_data.serial < 0)
173                         throw new IOException("no serial number found");
174
175                 /* Set serial number in the monitor dialog window */
176                 monitor.set_serial(log.serial);
177                 monitor.set_flight(log.flight);
178
179                 int     start_block = log.start_block;
180                 int     end_block = FindLastLog(log, cache);
181
182                 monitor.set_max(end_block - start_block - 1);
183
184                 ArrayList<Byte> data = new ArrayList<Byte>();
185
186                 /* Now scan the eeprom, reading blocks of data to create a byte array of data */
187
188                 for (block = start_block; block < end_block; block++) {
189                         monitor.set_block(block - start_block);
190
191                         AltosEepromChunk        eechunk = cache.get(block, false);
192
193                         for (int i = 0; i < eechunk.data.length; i++)
194                                 data.add((byte) eechunk.data[i]);
195                 }
196
197                 /* Construct our internal representation of the eeprom data */
198                 AltosEeprom     eeprom = new AltosEeprom(flights.config_data, data);
199
200                 /* Now see if we can't actually parse the resulting
201                  * file to generate a better filename. Note that this
202                  * doesn't need to work; we'll still save the data using
203                  * a less accurate name.
204                  */
205                 AltosEepromRecordSet            set = new AltosEepromRecordSet(eeprom);
206                 AltosEepromNameData name_data = new AltosEepromNameData(set.cal_data());
207
208                 for (AltosEepromRecord record : set.ordered) {
209                         record.provide_data(name_data, set.cal_data());
210                         if (name_data.done())
211                                 break;
212                 }
213
214                 AltosFile f = MakeFile(flights.config_data.serial, log.flight, name_data);
215
216                 log.set_file(f);
217
218                 boolean do_write = true;
219
220                 if (f.exists())
221                         do_write = monitor.check_overwrite(f);
222
223                 if (do_write) {
224                         FileWriter w = new FileWriter(f);
225
226                         eeprom.write(w);
227                         w.close();
228                 }
229
230                 if (eeprom.errors != 0)
231                         throw new ParseException(String.format("%d CRC Errors", eeprom.errors), 0);
232         }
233
234         static String label(int flight) {
235                 if (flight < 0)
236                         return "Corrupt";
237                 else
238                         return "Flight";
239         }
240
241         static int flight(int flight) {
242                 if (flight < 0)
243                         return -flight;
244                 return flight;
245         }
246
247         public void run () {
248                 boolean success = false;
249
250                 try {
251                         if (remote)
252                                 link.start_remote();
253
254                         for (AltosEepromLog log : flights) {
255                                 parse_errors = null;
256                                 if (log.download_selected) {
257                                         monitor.reset();
258                                         try {
259                                                 CaptureLog(log);
260                                         } catch (ParseException e) {
261                                                 LogError(e.getMessage());
262                                         }
263                                 }
264                                 success = true;
265                                 if (parse_errors != null) {
266                                         monitor.show_message(String.format("%s %d download error. Valid log data saved\n%s",
267                                                                            label(log.flight),
268                                                                            flight(log.flight),
269                                                                            parse_errors),
270                                                              link.name,
271                                                              AltosEepromMonitor.WARNING_MESSAGE);
272                                 }
273                         }
274                 } catch (IOException ee) {
275                         monitor.show_message(ee.getLocalizedMessage(),
276                                              link.name,
277                                              AltosEepromMonitor.ERROR_MESSAGE);
278                 } catch (InterruptedException ie) {
279                         monitor.show_message(String.format("Connection to \"%s\" interrupted",
280                                                            link.name),
281                                              "Connection Interrupted",
282                                              AltosEepromMonitor.ERROR_MESSAGE);
283                 } catch (TimeoutException te) {
284                         monitor.show_message(String.format("Connection to \"%s\" failed",
285                                                            link.name),
286                                              "Connection Failed",
287                                              AltosEepromMonitor.ERROR_MESSAGE);
288                 } finally {
289                         if (remote) {
290                                 try {
291                                         link.stop_remote();
292                                 } catch (InterruptedException ie) {
293                                 }
294                         }
295                         link.flush_output();
296                 }
297                 monitor.done(success);
298         }
299
300         public void start() {
301                 eeprom_thread = new Thread(this);
302                 monitor.set_thread(eeprom_thread);
303                 eeprom_thread.start();
304         }
305
306         public AltosEepromDownload(AltosEepromMonitor given_monitor,
307                                    AltosLink given_link,
308                                    boolean given_remote,
309                                    AltosEepromList given_flights) {
310
311                 monitor = given_monitor;
312                 link = given_link;
313                 remote = given_remote;
314                 flights = given_flights;
315
316                 monitor.start();
317         }
318 }