74912ed4bac7218b8d9a2462360e4e8c1acc32dc
[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_11;
20
21 import java.io.*;
22 import java.util.*;
23 import java.text.*;
24 import java.util.concurrent.*;
25
26 public class AltosEepromDownload implements Runnable {
27
28         AltosLink               link;
29         boolean                 remote;
30         Thread                  eeprom_thread;
31         AltosEepromMonitor      monitor;
32
33         AltosEepromList         flights;
34         String                  parse_errors;
35
36         private boolean has_gps_date(AltosState state) {
37                 if (state == null)
38                         return false;
39
40                 AltosGPS gps = state.gps;
41
42                 return gps != null &&
43                         gps.year != AltosLib.MISSING &&
44                         gps.month != AltosLib.MISSING &&
45                         gps.day != AltosLib.MISSING;
46         }
47
48         private AltosFile MakeFile(int serial, int flight, AltosState state) throws IOException {
49                 AltosFile               eeprom_name;
50
51                 if (has_gps_date(state)) {
52                         AltosGPS                gps = state.gps;
53                         eeprom_name = new AltosFile(gps.year, gps.month, gps.day,
54                                                     serial, flight, "eeprom");
55                 } else
56                         eeprom_name = new AltosFile(serial, flight, "eeprom");
57
58                 return eeprom_name;
59         }
60
61         boolean                 done;
62         int                     prev_state;
63         int                     state_block;
64
65         void LogError(String error) {
66                 if (parse_errors != null)
67                         parse_errors.concat(error.concat("\n"));
68                 else
69                         parse_errors = error;
70         }
71
72         class BlockCache extends Hashtable<Integer,AltosEepromChunk> {
73                 AltosEepromLog  log;
74
75                 AltosEepromChunk get(int start, boolean add) throws TimeoutException, InterruptedException {
76                         if (contains(start))
77                                 return super.get(start);
78                         AltosEepromChunk eechunk = new AltosEepromChunk(link, start, start == log.start_block);
79                         if (add)
80                                 put(start, eechunk);
81                         return eechunk;
82                 }
83
84                 public BlockCache(AltosEepromLog log) {
85                         this.log = log;
86                 }
87         }
88
89         int FindLastLog(AltosEepromLog log, BlockCache cache) throws TimeoutException, InterruptedException {
90                 int     low = log.start_block;
91                 int     high = log.end_block - 1;
92
93                 while (low <= high) {
94                         int mid = (high + low) / 2;
95
96                         if (!cache.get(mid, true).erased())
97                                 low = mid + 1;
98                         else
99                                 high = mid - 1;
100                 }
101                 return low;
102         }
103
104         void CaptureLog(AltosEepromLog log) throws IOException, InterruptedException, TimeoutException, ParseException {
105                 int                     block, state_block = 0;
106                 int                     log_format = flights.config_data.log_format;
107                 BlockCache              cache = new BlockCache(log);
108
109                 done = false;
110
111                 if (flights.config_data.serial < 0)
112                         throw new IOException("no serial number found");
113
114                 /* Set serial number in the monitor dialog window */
115                 monitor.set_serial(log.serial);
116                 monitor.set_flight(log.flight);
117
118                 int     start_block = log.start_block;
119                 int     end_block = FindLastLog(log, cache);
120
121                 monitor.set_max(end_block - start_block - 1);
122
123                 ArrayList<Byte> data = new ArrayList<Byte>();
124
125                 /* Now scan the eeprom, reading blocks of data to create a byte array of data */
126
127                 for (block = start_block; block < end_block; block++) {
128                         monitor.set_block(block - start_block);
129
130                         AltosEepromChunk        eechunk = cache.get(block, false);
131
132                         for (int i = 0; i < eechunk.data.length; i++)
133                                 data.add((byte) eechunk.data[i]);
134                 }
135
136                 /* Construct our internal representation of the eeprom data */
137                 AltosEepromNew  eeprom = new AltosEepromNew(flights.config_data, data);
138
139                 /* Now see if we can't actually parse the resulting
140                  * file to generate a better filename. Note that this
141                  * doesn't need to work; we'll still save the data using
142                  * a less accurate name.
143                  */
144                 AltosEepromRecordSet    set = new AltosEepromRecordSet(eeprom);
145
146                 AltosState state = new AltosState();
147
148                 for (AltosState s : set) {
149                         state = s;
150                         if (state.gps != null)
151                                 break;
152                 }
153
154                 AltosFile f = MakeFile(flights.config_data.serial, log.flight, state);
155
156                 monitor.set_filename(f.toString());
157
158                 FileWriter w = new FileWriter(f);
159
160                 eeprom.write(w);
161                 w.close();
162         }
163
164         public void run () {
165                 boolean success = false;
166
167                 try {
168                         boolean failed = false;
169                         if (remote)
170                                 link.start_remote();
171
172                         for (AltosEepromLog log : flights) {
173                                 parse_errors = null;
174                                 if (log.selected) {
175                                         monitor.reset();
176                                         try {
177                                                 CaptureLog(log);
178                                         } catch (ParseException e) {
179                                                 LogError(e.getMessage());
180                                         }
181                                 }
182                                 if (parse_errors != null) {
183                                         failed = true;
184                                         monitor.show_message(String.format("Flight %d download error. Valid log data saved\n%s",
185                                                                            log.flight,
186                                                                            parse_errors),
187                                                              link.name,
188                                                              AltosEepromMonitor.WARNING_MESSAGE);
189                                 }
190                         }
191                         success = !failed;
192                 } catch (IOException ee) {
193                         monitor.show_message(ee.getLocalizedMessage(),
194                                              link.name,
195                                              AltosEepromMonitor.ERROR_MESSAGE);
196                 } catch (InterruptedException ie) {
197                         monitor.show_message(String.format("Connection to \"%s\" interrupted",
198                                                            link.name),
199                                              "Connection Interrupted",
200                                              AltosEepromMonitor.ERROR_MESSAGE);
201                 } catch (TimeoutException te) {
202                         monitor.show_message(String.format("Connection to \"%s\" failed",
203                                                            link.name),
204                                              "Connection Failed",
205                                              AltosEepromMonitor.ERROR_MESSAGE);
206                 } finally {
207                         if (remote) {
208                                 try {
209                                         link.stop_remote();
210                                 } catch (InterruptedException ie) {
211                                 }
212                         }
213                         link.flush_output();
214                 }
215                 monitor.done(success);
216         }
217
218         public void start() {
219                 eeprom_thread = new Thread(this);
220                 monitor.set_thread(eeprom_thread);
221                 eeprom_thread.start();
222         }
223
224         public AltosEepromDownload(AltosEepromMonitor given_monitor,
225                                    AltosLink given_link,
226                                    boolean given_remote,
227                                    AltosEepromList given_flights) {
228
229                 monitor = given_monitor;
230                 link = given_link;
231                 remote = given_remote;
232                 flights = given_flights;
233
234                 monitor.start();
235         }
236 }