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