altoslib: Use symbols in AltosRomconfig instead of fixed offsets
[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; 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_2;
19
20 import java.io.*;
21 import java.util.*;
22 import java.text.*;
23 import java.util.concurrent.*;
24
25 public class AltosEepromDownload implements Runnable {
26
27         AltosLink               link;
28         boolean                 remote;
29         Thread                  eeprom_thread;
30         AltosEepromMonitor      monitor;
31
32         boolean                 want_file;
33         FileWriter              eeprom_file;
34         LinkedList<String>      eeprom_pending;
35
36         AltosEepromList         flights;
37         boolean                 success;
38         ParseException          parse_exception;
39         AltosState              state;
40
41         private void FlushPending() throws IOException {
42                 for (String s : flights.config_data) {
43                         eeprom_file.write(s);
44                         eeprom_file.write('\n');
45                 }
46
47                 for (String s : eeprom_pending)
48                         eeprom_file.write(s);
49         }
50
51         private void CheckFile(boolean force) throws IOException {
52                 if (eeprom_file != null)
53                         return;
54                 if (force || (state.flight != 0 && want_file)) {
55                         AltosFile               eeprom_name;
56                         AltosGPS                gps = state.gps;
57
58                         if (gps != null &&
59                             gps.year != AltosLib.MISSING &&
60                             gps.month != AltosLib.MISSING &&
61                             gps.day != AltosLib.MISSING)
62                         {
63                                 eeprom_name = new AltosFile(gps.year, gps.month, gps.day,
64                                                             state.serial, state.flight, "eeprom");
65                         } else
66                                 eeprom_name = new AltosFile(state.serial, state.flight, "eeprom");
67
68                         eeprom_file = new FileWriter(eeprom_name);
69                         if (eeprom_file != null) {
70                                 monitor.set_filename(eeprom_name.getName());
71                                 FlushPending();
72                                 eeprom_pending = null;
73                         }
74                 }
75         }
76
77         boolean                 done;
78         boolean                 start;
79
80         void LogEeprom(AltosEeprom r) throws IOException {
81                 if (r.cmd != AltosLib.AO_LOG_INVALID) {
82                         String line = r.string();
83                         if (eeprom_file != null)
84                                 eeprom_file.write(line);
85                         else
86                                 eeprom_pending.add(line);
87                 }
88         }
89
90         void CaptureEeprom(AltosEepromChunk eechunk, int log_format) throws IOException, ParseException {
91                 boolean any_valid = false;
92                 boolean got_flight = false;
93
94                 int record_length = 8;
95
96                 state.set_serial(flights.config_data.serial);
97                 monitor.set_serial(flights.config_data.serial);
98
99                 for (int i = 0; i < AltosEepromChunk.chunk_size && !done; i += record_length) {
100                         AltosEeprom r = eechunk.eeprom(i, log_format, state);
101
102                         if (r == null)
103                                 continue;
104
105                         record_length = r.record_length();
106
107                         r.update_state(state);
108
109                         if (!got_flight && state.flight != AltosLib.MISSING)
110                                 monitor.set_flight(state.flight);
111
112                         /* Monitor state transitions to update display */
113                         if (state.state != AltosLib.ao_flight_invalid &&
114                             state.state <= AltosLib.ao_flight_landed)
115                         {
116                                 if (state.state > AltosLib.ao_flight_pad)
117                                         want_file = true;
118                                 if (state.state == AltosLib.ao_flight_landed)
119                                         done = true;
120                         }
121
122                         if (state.gps != null)
123                                 want_file = true;
124
125                         if (r.valid) {
126                                 any_valid = true;
127                                 LogEeprom(r);
128                         }
129                 }
130                 if (!any_valid)
131                         done = true;
132
133                 CheckFile(false);
134         }
135         
136         void CaptureLog(AltosEepromLog log) throws IOException, InterruptedException, TimeoutException, ParseException {
137                 int                     block, state_block = 0;
138                 int                     log_format = flights.config_data.log_format;
139
140                 state = new AltosState();
141
142                 done = false;
143                 start = true;
144
145                 if (flights.config_data.serial < 0)
146                         throw new IOException("no serial number found");
147
148                 /* Reset per-capture variables */
149                 want_file = false;
150                 eeprom_file = null;
151                 eeprom_pending = new LinkedList<String>();
152
153                 /* Set serial number in the monitor dialog window */
154                 /* Now scan the eeprom, reading blocks of data and converting to .eeprom file form */
155
156                 state_block = log.start_block;
157                 for (block = log.start_block; !done && block < log.end_block; block++) {
158                         monitor.set_value(state.state_name(),
159                                           state.state,
160                                           block - state_block,
161                                           block - log.start_block);
162
163                         AltosEepromChunk        eechunk = new AltosEepromChunk(link, block, block == log.start_block);
164
165                         /*
166                          * Guess what kind of data is there if the device
167                          * didn't tell us
168                          */
169
170                         if (log_format == AltosLib.AO_LOG_FORMAT_UNKNOWN) {
171                                 if (block == log.start_block) {
172                                         if (eechunk.data(0) == AltosLib.AO_LOG_FLIGHT)
173                                                 log_format = AltosLib.AO_LOG_FORMAT_FULL;
174                                         else
175                                                 log_format = AltosLib.AO_LOG_FORMAT_TINY;
176                                 }
177                         }
178
179                         CaptureEeprom (eechunk, log_format);
180                 }
181                 CheckFile(true);
182                 if (eeprom_file != null) {
183                         eeprom_file.flush();
184                         eeprom_file.close();
185                 }
186         }
187
188         public void run () {
189                 try {
190                         boolean failed = false;
191                         if (remote)
192                                 link.start_remote();
193
194                         for (AltosEepromLog log : flights) {
195                                 parse_exception = null;
196                                 if (log.selected) {
197                                         monitor.reset();
198                                         try {
199                                                 CaptureLog(log);
200                                         } catch (ParseException e) {
201                                                 parse_exception = e;
202                                         }
203                                 }
204                                 if (parse_exception != null) {
205                                         failed = true;
206                                         monitor.show_message(String.format("Flight %d download error\n%s\nValid log data saved",
207                                                                            log.flight,
208                                                                            parse_exception.getMessage()),
209                                                              link.name,
210                                                              AltosEepromMonitor.WARNING_MESSAGE);
211                                 }
212                         }
213                         success = !failed;
214                 } catch (IOException ee) {
215                         monitor.show_message(ee.getLocalizedMessage(),
216                                              link.name,
217                                              AltosEepromMonitor.ERROR_MESSAGE);
218                 } catch (InterruptedException ie) {
219                         monitor.show_message(String.format("Connection to \"%s\" interrupted",
220                                                            link.name),
221                                              "Connection Interrupted",
222                                              AltosEepromMonitor.ERROR_MESSAGE);
223                 } catch (TimeoutException te) {
224                         monitor.show_message(String.format("Connection to \"%s\" failed",
225                                                            link.name),
226                                              "Connection Failed",
227                                              AltosEepromMonitor.ERROR_MESSAGE);
228                 } finally {
229                         if (remote) {
230                                 try {
231                                         link.stop_remote();
232                                 } catch (InterruptedException ie) {
233                                 }
234                         }
235                         link.flush_output();
236                 }
237                 monitor.done(success);
238         }
239
240         public void start() {
241                 eeprom_thread = new Thread(this);
242                 eeprom_thread.start();
243         }
244
245         public AltosEepromDownload(AltosEepromMonitor given_monitor,
246                                    AltosLink given_link,
247                                    boolean given_remote,
248                                    AltosEepromList given_flights) {
249
250                 monitor = given_monitor;
251                 link = given_link;
252                 remote = given_remote;
253                 flights = given_flights;
254                 success = false;
255
256                 monitor.set_states(AltosLib.ao_flight_boost, AltosLib.ao_flight_landed);
257
258                 monitor.set_thread(eeprom_thread);
259                 monitor.start();
260         }
261 }