summaryrefslogtreecommitdiff
path: root/altoslib/AltosEepromDownload.java
blob: 74912ed4bac7218b8d9a2462360e4e8c1acc32dc (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
/*
 * 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; either version 2 of the License, or
 * (at your option) any later version.
 *
 * 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_11;

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;

	AltosEepromList		flights;
	String			parse_errors;

	private boolean has_gps_date(AltosState state) {
		if (state == null)
			return false;

		AltosGPS gps = state.gps;

		return gps != null &&
			gps.year != AltosLib.MISSING &&
			gps.month != AltosLib.MISSING &&
			gps.day != AltosLib.MISSING;
	}

	private AltosFile MakeFile(int serial, int flight, AltosState state) throws IOException {
		AltosFile		eeprom_name;

		if (has_gps_date(state)) {
			AltosGPS		gps = state.gps;
			eeprom_name = new AltosFile(gps.year, gps.month, gps.day,
						    serial, flight, "eeprom");
		} else
			eeprom_name = new AltosFile(serial, flight, "eeprom");

		return eeprom_name;
	}

	boolean			done;
	int			prev_state;
	int			state_block;

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

	class BlockCache extends Hashtable<Integer,AltosEepromChunk> {
		AltosEepromLog	log;

		AltosEepromChunk get(int start, boolean add) throws TimeoutException, InterruptedException {
			if (contains(start))
				return super.get(start);
			AltosEepromChunk eechunk = new AltosEepromChunk(link, start, start == log.start_block);
			if (add)
				put(start, eechunk);
			return eechunk;
		}

		public BlockCache(AltosEepromLog log) {
			this.log = log;
		}
	}

	int FindLastLog(AltosEepromLog log, BlockCache cache) throws TimeoutException, InterruptedException {
		int	low = log.start_block;
		int	high = log.end_block - 1;

		while (low <= high) {
			int mid = (high + low) / 2;

			if (!cache.get(mid, true).erased())
				low = mid + 1;
			else
				high = mid - 1;
		}
		return low;
	}

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

		done = false;

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

		/* Set serial number in the monitor dialog window */
		monitor.set_serial(log.serial);
		monitor.set_flight(log.flight);

		int	start_block = log.start_block;
		int	end_block = FindLastLog(log, cache);

		monitor.set_max(end_block - start_block - 1);

		ArrayList<Byte> data = new ArrayList<Byte>();

		/* Now scan the eeprom, reading blocks of data to create a byte array of data */

		for (block = start_block; block < end_block; block++) {
			monitor.set_block(block - start_block);

			AltosEepromChunk	eechunk = cache.get(block, false);

			for (int i = 0; i < eechunk.data.length; i++)
				data.add((byte) eechunk.data[i]);
		}

		/* Construct our internal representation of the eeprom data */
		AltosEepromNew	eeprom = new AltosEepromNew(flights.config_data, data);

		/* Now see if we can't actually parse the resulting
		 * file to generate a better filename. Note that this
		 * doesn't need to work; we'll still save the data using
		 * a less accurate name.
		 */
		AltosEepromRecordSet	set = new AltosEepromRecordSet(eeprom);

		AltosState state = new AltosState();

		for (AltosState s : set) {
			state = s;
			if (state.gps != null)
				break;
		}

		AltosFile f = MakeFile(flights.config_data.serial, log.flight, state);

		monitor.set_filename(f.toString());

		FileWriter w = new FileWriter(f);

		eeprom.write(w);
		w.close();
	}

	public void run () {
		boolean success = false;

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

			for (AltosEepromLog log : flights) {
				parse_errors = null;
				if (log.selected) {
					monitor.reset();
					try {
						CaptureLog(log);
					} catch (ParseException e) {
						LogError(e.getMessage());
					}
				}
				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;

		monitor.start();
	}
}