summaryrefslogtreecommitdiff
path: root/altoslib/AltosEepromDownload.java
blob: 594f053f5cc012e3f71289f2855fb9fb28891617 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
 * Copyright © 2010 Keith Packard <keithp@keithp.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
 */

package org.altusmetrum.altoslib_8;

import java.io.*;
import java.util.*;
import java.text.*;
import java.util.concurrent.*;

public class AltosEepromDownload implements Runnable {

	AltosLink		link;
	boolean			remote;
	Thread			eeprom_thread;
	AltosEepromMonitor	monitor;

	boolean			want_file;
	FileWriter		eeprom_file;
	LinkedList<String>	eeprom_pending;

	AltosEepromList		flights;
	boolean			success;
	String			parse_errors;
	AltosState		state;

	private void FlushPending() throws IOException {
		for (String s : flights.config_data) {
			eeprom_file.write(s);
			eeprom_file.write('\n');
		}

		for (String s : eeprom_pending)
			eeprom_file.write(s);
	}

	private void CheckFile(boolean force) throws IOException {
		if (eeprom_file != null)
			return;
		if (force || (state.flight != 0 && want_file)) {
			AltosFile		eeprom_name;
			AltosGPS		gps = state.gps;

			if (gps != null &&
			    gps.year != AltosLib.MISSING &&
			    gps.month != AltosLib.MISSING &&
			    gps.day != AltosLib.MISSING)
			{
				eeprom_name = new AltosFile(gps.year, gps.month, gps.day,
							    state.serial, state.flight, "eeprom");
			} else
				eeprom_name = new AltosFile(state.serial, state.flight, "eeprom");

			eeprom_file = new FileWriter(eeprom_name);
			if (eeprom_file != null) {
				monitor.set_filename(eeprom_name.getName());
				FlushPending();
				eeprom_pending = null;
			}
		}
	}

	boolean			done;
	int			prev_state;
	int			state_block;

	void LogEeprom(AltosEeprom r) throws IOException {
		if (r.cmd != AltosLib.AO_LOG_INVALID) {
			String line = r.string();
			if (eeprom_file != null)
				eeprom_file.write(line);
			else
				eeprom_pending.add(line);
		}
	}

	void LogError(String error) {
		if (parse_errors != null)
			parse_errors.concat(error.concat("\n"));
		else
			parse_errors = error;
	}

	void CaptureEeprom(AltosEepromChunk eechunk, int log_format) throws IOException, ParseException {
		boolean any_valid = false;
		boolean got_flight = false;

		int record_length = 8;

		state.set_serial(flights.config_data.serial);
		monitor.set_serial(flights.config_data.serial);

		for (int i = 0; i < AltosEepromChunk.chunk_size && !done; i += record_length) {
			AltosEeprom r = null;

			try {
				r = eechunk.eeprom(i, log_format, state);
			} catch (ParseException pe) {
				LogError(pe.getMessage());
				r = null;
			}

			if (r == null)
				continue;

			record_length = r.record_length();

			r.update_state(state);

			if (!got_flight && state.flight != AltosLib.MISSING)
				monitor.set_flight(state.flight);

			/* Monitor state transitions to update display */
			if (state.state() != AltosLib.ao_flight_invalid &&
			    state.state() <= AltosLib.ao_flight_landed)
			{
				if (state.state() > AltosLib.ao_flight_pad)
					want_file = true;
				if (state.state() == AltosLib.ao_flight_landed)
					done = true;
			}

			if (state.gps != null)
				want_file = true;

			if (r.valid) {
				any_valid = true;
				LogEeprom(r);
			}
		}
		if (!any_valid)
			done = true;

		CheckFile(false);
	}

	void CaptureLog(AltosEepromLog log) throws IOException, InterruptedException, TimeoutException, ParseException {
		int			block, state_block = 0;
		int			log_format = flights.config_data.log_format;

		state = new AltosState();

		done = false;

		if (flights.config_data.serial < 0)
			throw new IOException("no serial number found");

		/* Reset per-capture variables */
		want_file = false;
		eeprom_pending = new LinkedList<String>();

		/* Set serial number in the monitor dialog window */
		/* Now scan the eeprom, reading blocks of data and converting to .eeprom file form */

		state_block = log.start_block;
		prev_state = AltosLib.ao_flight_startup;
		for (block = log.start_block; !done && block < log.end_block; block++) {
			AltosEepromChunk	eechunk = new AltosEepromChunk(link, block, block == log.start_block);

			/*
			 * Guess what kind of data is there if the device
			 * didn't tell us
			 */

			if (log_format == AltosLib.AO_LOG_FORMAT_UNKNOWN) {
				if (block == log.start_block) {
					if (eechunk.data(0) == AltosLib.AO_LOG_FLIGHT)
						log_format = AltosLib.AO_LOG_FORMAT_FULL;
					else
						log_format = AltosLib.AO_LOG_FORMAT_TINY;
				}
			}

			CaptureEeprom (eechunk, log_format);

			if (state.state() != prev_state && state.state() != AltosLib.ao_flight_invalid) {
				state_block = block;
				prev_state = state.state();
			}

			monitor.set_value(state.state_name(),
					  state.state(),
					  block - state_block,
					  block - log.start_block);
		}
		CheckFile(true);
	}

	public void run () {
		try {
			boolean	failed = false;
			if (remote)
				link.start_remote();

			for (AltosEepromLog log : flights) {
				parse_errors = null;
				if (log.selected) {
					monitor.reset();
					eeprom_file = null;
					try {
						CaptureLog(log);
					} catch (ParseException e) {
						LogError(e.getMessage());
					}
					if (eeprom_file != null) {
						eeprom_file.flush();
						eeprom_file.close();
					}
				}
				if (parse_errors != null) {
					failed = true;
					monitor.show_message(String.format("Flight %d download error. Valid log data saved\n%s",
									   log.flight,
									   parse_errors),
							     link.name,
							     AltosEepromMonitor.WARNING_MESSAGE);
				}
			}
			success = !failed;
		} catch (IOException ee) {
			monitor.show_message(ee.getLocalizedMessage(),
					     link.name,
					     AltosEepromMonitor.ERROR_MESSAGE);
		} catch (InterruptedException ie) {
			monitor.show_message(String.format("Connection to \"%s\" interrupted",
							   link.name),
					     "Connection Interrupted",
					     AltosEepromMonitor.ERROR_MESSAGE);
		} catch (TimeoutException te) {
			monitor.show_message(String.format("Connection to \"%s\" failed",
							   link.name),
					     "Connection Failed",
					     AltosEepromMonitor.ERROR_MESSAGE);
		} finally {
			if (remote) {
				try {
					link.stop_remote();
				} catch (InterruptedException ie) {
				}
			}
			link.flush_output();
		}
		monitor.done(success);
	}

	public void start() {
		eeprom_thread = new Thread(this);
		monitor.set_thread(eeprom_thread);
		eeprom_thread.start();
	}

	public AltosEepromDownload(AltosEepromMonitor given_monitor,
				   AltosLink given_link,
				   boolean given_remote,
				   AltosEepromList given_flights) {

		monitor = given_monitor;
		link = given_link;
		remote = given_remote;
		flights = given_flights;
		success = false;

		if (flights.config_data.log_has_state())
			monitor.set_states(AltosLib.ao_flight_boost, AltosLib.ao_flight_landed);
		else
			monitor.set_states(AltosLib.ao_flight_invalid, AltosLib.ao_flight_invalid);

		monitor.start();
	}
}