A chunk of json for the config values followed by hex numbers for the data.
Signed-off-by: Keith Packard <keithp@keithp.com>
return true;
}
- public AltosEeprom eeprom(int offset, int log_format, AltosState state) throws ParseException {
- AltosEeprom eeprom = null;
- switch (log_format) {
- case AltosLib.AO_LOG_FORMAT_FULL:
- eeprom = new AltosEepromTM(this, offset);
- break;
- case AltosLib.AO_LOG_FORMAT_TINY:
- eeprom = new AltosEepromTMini(this, offset, state);
- break;
- case AltosLib.AO_LOG_FORMAT_TELEMETRY:
- case AltosLib.AO_LOG_FORMAT_TELESCIENCE:
- break;
- case AltosLib.AO_LOG_FORMAT_TELEMEGA:
- case AltosLib.AO_LOG_FORMAT_TELEMEGA_OLD:
- eeprom = new AltosEepromMega(this, offset, log_format);
- break;
- case AltosLib.AO_LOG_FORMAT_TELEMETRUM:
- eeprom = new AltosEepromMetrum2(this, offset);
- break;
- case AltosLib.AO_LOG_FORMAT_TELEMINI2:
- case AltosLib.AO_LOG_FORMAT_TELEMINI3:
- case AltosLib.AO_LOG_FORMAT_EASYMINI:
- eeprom = new AltosEepromMini(this, offset);
- break;
- case AltosLib.AO_LOG_FORMAT_TELEGPS:
- eeprom = new AltosEepromGPS(this, offset);
- break;
- case AltosLib.AO_LOG_FORMAT_TELEFIRETWO:
- eeprom = new AltosEepromFireTwo(this, offset);
- break;
- default:
- throw new ParseException("unknown eeprom format " + log_format, 0);
- }
- return eeprom;
+ public boolean erased() {
+ return erased(0, chunk_size);
}
public AltosEepromChunk(AltosLink link, int block, boolean flush)
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');
- }
+ private boolean has_gps_date(AltosState state) {
+ if (state == null)
+ return false;
+
+ AltosGPS gps = state.gps;
- for (String s : eeprom_pending)
- eeprom_file.write(s);
+ return gps != null &&
+ gps.year != AltosLib.MISSING &&
+ gps.month != AltosLib.MISSING &&
+ gps.day != AltosLib.MISSING;
}
- private void CheckFile(boolean force) throws IOException {
- if (eeprom_file != null)
- return;
- if (force || (state.flight != 0 && want_file)) {
- AltosFile eeprom_name;
+ 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");
- 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;
- }
- }
+ return eeprom_name;
}
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"));
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);
+ class BlockCache extends Hashtable<Integer,AltosEepromChunk> {
+ AltosEepromLog log;
- 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);
+ 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;
+ }
- if (!got_flight && state.flight != AltosLib.MISSING)
- monitor.set_flight(state.flight);
+ public BlockCache(AltosEepromLog log) {
+ this.log = log;
+ }
+ }
- /* 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;
- }
+ int FindLastLog(AltosEepromLog log, BlockCache cache) throws TimeoutException, InterruptedException {
+ int low = log.start_block;
+ int high = log.end_block - 1;
- if (state.gps != null)
- want_file = true;
+ while (low <= high) {
+ int mid = (high + low) / 2;
- if (r.valid) {
- any_valid = true;
- LogEeprom(r);
- }
+ if (!cache.get(mid, true).erased())
+ low = mid + 1;
+ else
+ high = mid - 1;
}
- if (!any_valid)
- done = true;
-
- CheckFile(false);
+ return low;
}
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();
+ BlockCache cache = new BlockCache(log);
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;
- }
- }
+ monitor.set_serial(log.serial);
+ monitor.set_flight(log.flight);
- CaptureEeprom (eechunk, log_format);
+ int start_block = log.start_block;
+ int end_block = FindLastLog(log, cache);
- if (state.state() != prev_state && state.state() != AltosLib.ao_flight_invalid) {
- state_block = block;
- prev_state = state.state();
- }
+ 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]);
+ }
- monitor.set_value(state.state_name(),
- state.state(),
- block - state_block,
- block - log.start_block);
+ /* 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;
}
- CheckFile(true);
+
+ 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)
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;
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();
}
public interface AltosEepromMonitor {
- public void set_states(int min_state, int max_state);
+ public void set_block(int in_block);
- public void set_value(String in_state_name, int in_state, int in_state_block, int in_block);
+ public void set_max(int in_max);
public void set_serial(int in_serial);
return true;
}
- static private byte[] byte_list_to_array(List<Byte> bytes) {
- byte[] data = new byte[bytes.size()];
- int i = 0;
-
- for (Byte b : bytes) {
- data[i++] = b;
- }
- return data;
- }
-
private boolean read_data(Reader r) throws IOException {
BufferedReader br = new BufferedReader(r);
String s;
data = new ArrayList<Byte>();
while ((s = br.readLine()) != null) {
+
String[] tokens = s.split("\\s+");
for (int i = 0; i < tokens.length; i++) {
- try {
- data.add((byte) AltosLib.fromhex(tokens[i]));
- } catch (NumberFormatException e) {
- throw new IOException(e.toString());
+ if (tokens[i].length() > 0) {
+ try {
+ data.add((byte) AltosLib.fromhex(tokens[i]));
+ } catch (NumberFormatException e) {
+ throw new IOException(e.toString());
+ }
}
}
}
read(new StringReader(s));
}
+ public AltosEepromNew(AltosJson config, ArrayList<Byte> data) {
+ this.config = config;
+ this.data = data;
+ }
+
+ public AltosEepromNew(AltosConfigData config_data, ArrayList<Byte> data) {
+ this.config = new AltosJson(config_data);
+ this.data = data;
+ }
+
public AltosEepromNew() {
}
}
return data8(i) | (data8(i+1) << 8) | (data8(i+2) << 16) | (data8(i+3) << 24);
}
+ public boolean valid(int s) {
+ return AltosConvert.checksum(eeprom.data, s, length) == 0;
+ }
+
public boolean valid() {
- return AltosConvert.checksum(eeprom.data, start, length) == 0;
+ return valid(start);
}
private int cmdi() {
state.set_tick(tick());
}
+ public int next_start() {
+ int s = start + length;
+
+ while (s + length < eeprom.data.size()) {
+ if (valid(s))
+ return s;
+ s += length;
+ }
+ return -1;
+ }
+
public boolean hasNext() {
- return start + length * 2 < eeprom.data.size();
+ return next_start() >= 0;
}
public abstract AltosEepromRecord next();
this.eeprom = eeprom;
this.start = start;
this.length = length;
+
+ while (start + length < eeprom.data.size() && !valid())
+ start += length;
}
}
}
public AltosEepromRecord next() {
- if (start + length + length < eeprom.data.size())
- return new AltosEepromRecordFireTwo(eeprom, start + length);
- return null;
+ int s = next_start();
+ if (s < 0)
+ return null;
+ return new AltosEepromRecordFireTwo(eeprom, s);
}
public AltosEepromRecordFireTwo(AltosEepromNew eeprom, int start) {
}
public AltosEepromRecord next() {
- if (start + length + length < eeprom.data.size())
- return new AltosEepromRecordFull(eeprom, start + length);
- return null;
+ int s = next_start();
+ if (s < 0)
+ return null;
+ return new AltosEepromRecordFull(eeprom, s);
}
public AltosEepromRecordFull(AltosEepromNew eeprom, int start) {
}
public AltosEepromRecord next() {
- if (start + length + length < eeprom.data.size())
- return new AltosEepromRecordGps(eeprom, start + length);
- return null;
+ int s = next_start();
+ if (s < 0)
+ return null;
+ return new AltosEepromRecordGps(eeprom, s);
}
public AltosEepromRecordGps(AltosEepromNew eeprom, int start) {
}
public AltosEepromRecord next() {
- if (start + length + length < eeprom.data.size())
- return new AltosEepromRecordMega(eeprom, start + length);
- return null;
+ int s = next_start();
+ if (s < 0)
+ return null;
+ return new AltosEepromRecordMega(eeprom, s);
}
public AltosEepromRecordMega(AltosEepromNew eeprom, int start) {
}
public AltosEepromRecord next() {
- if (start + length + length < eeprom.data.size())
- return new AltosEepromRecordMetrum(eeprom, start + length);
- return null;
+ int s = next_start();
+ if (s < 0)
+ return null;
+ return new AltosEepromRecordMetrum(eeprom, s);
}
public AltosEepromRecordMetrum(AltosEepromNew eeprom, int start) {
}
public AltosEepromRecord next() {
- if (start + length + length < eeprom.data.size())
- return new AltosEepromRecordMini(eeprom, start + length);
- return null;
+ int s = next_start();
+ if (s < 0)
+ return null;
+ return new AltosEepromRecordMini(eeprom, s);
}
public AltosEepromRecordMini(AltosEepromNew eeprom, int start) {
return eeprom.data16(start);
}
+ public boolean valid(int s) {
+ return eeprom.data16(s) != 0xffff;
+ }
+
public int cmd() {
if (start == 0)
return AltosLib.AO_LOG_FLIGHT;
}
public AltosEepromRecord next() {
- if (start + record_length * 2 < eeprom.data.size())
- return new AltosEepromRecordTiny(eeprom, start + record_length);
- return null;
+ int s = next_start();
+ if (s < 0)
+ return null;
+ return new AltosEepromRecordTiny(eeprom, s);
}
public AltosEepromRecordTiny(AltosEepromNew eeprom, int start) {
static final int _colon = 9;
static final int _end = 10;
static final int _error = 11;
+ static final int _none = 12;
static String token_name(int token) {
switch (token) {
* Lexer for json
*/
class JsonLexer extends JsonUtil {
- Reader f;
- int line;
- int ungot = -2;
- StringBuffer pending_token;
- JsonToken token;
+ Reader f;
+ int line;
+ int ungot = -2;
+ StringBuffer pending_token;
+ private JsonToken token;
static class keyword {
String word;
}
void next() {
- token = lex();
+ token = null;
+ }
+
+ JsonToken token() {
+ if (token == null)
+ token = lex();
+ return token;
}
JsonToken expect(int e) {
- JsonToken t = token;
+ JsonToken t = token();
if (t.token != e)
throw new IllegalArgumentException(String.format("got \"%s\" while expecting \"%s\"",
token.token_name(),
lexer.next();
for (;;) {
/* Allow for empty hashes */
- if (lexer.token.token == JsonToken._cc) {
+ if (lexer.token().token == JsonToken._cc) {
lexer.next();
return hash;
}
AltosJson value = value();
hash.put(key, value);
- switch (lexer.token.token) {
+ switch (lexer.token().token) {
case JsonToken._comma:
lexer.next();
break;
lexer.next();
return hash;
default:
- parse_error("got %s expect \",\" or \"}\"", lexer.token.token_name());
+ parse_error("got %s expect \",\" or \"}\"", lexer.token().token_name());
return null;
}
}
lexer.next();
for (int i = 0;; i++) {
/* Allow for empty arrays */
- if (lexer.token.token == JsonToken._cs) {
+ if (lexer.token().token == JsonToken._cs) {
lexer.next();
return array;
}
AltosJson value = value();
array.put(i, value);
- switch (lexer.token.token) {
+ switch (lexer.token().token) {
case JsonToken._comma:
lexer.next();
break;
lexer.next();
return array;
default:
- parse_error("got %s expect \",\" or \"]\"", lexer.token.token_name());
+ parse_error("got %s expect \",\" or \"]\"", lexer.token().token_name());
return null;
}
}
* identify the next object in the input
*/
AltosJson value() {
- switch (lexer.token.token) {
+ switch (lexer.token().token) {
case JsonToken._oc:
return new AltosJson(hash());
case JsonToken._os:
return new AltosJson(array());
case JsonToken._double:
- double dval = lexer.token.dval;
+ double dval = lexer.token().dval;
lexer.next();
return new AltosJson(dval);
case JsonToken._long:
- long lval = lexer.token.lval;
+ long lval = lexer.token().lval;
lexer.next();
return new AltosJson(lval);
case JsonToken._string:
- String sval = lexer.token.sval;
+ String sval = lexer.token().sval;
lexer.next();
return new AltosJson(sval);
case JsonToken._boolean:
- boolean bval = lexer.token.bval;
+ boolean bval = lexer.token().bval;
lexer.next();
return new AltosJson(bval);
default:
- parse_error("Unexpected token \"%s\"", lexer.token.token_name());
+ parse_error("Unexpected token \"%s\"", lexer.token().token_name());
}
return null;
}
JLabel file_value;
JButton cancel;
JProgressBar pbar;
- int min_state, max_state;
ActionListener listener;
static final int progress_max = 10000;
listener = l;
}
- public void set_states(int min_state, int max_state) {
- this.min_state = min_state;
- this.max_state = max_state;
- }
-
public void set_thread(Thread in_eeprom_thread) {
final Thread eeprom_thread = in_eeprom_thread;
cancel.addActionListener(new ActionListener() {
setVisible(true);
}
- private void set_value_internal(String state_name, int state, int state_block, int block) {
+ int max_block = 1;
+
+ public void set_block_internal(int block) {
double pos;
String s;
- if (min_state == AltosLib.ao_flight_invalid) {
- int lblock = block;
- if (lblock > 1000)
- lblock = 1000;
- pos = lblock / 1000.0;
- s = String.format("block %d", block);
- } else {
- if (state == AltosLib.ao_flight_invalid)
- state = 0;
- if (state_block > 100)
- state_block = 100;
- if (state < min_state) state = min_state;
- if (state > max_state) state = max_state;
-
- if (state == max_state)
- state_block = 0;
-
- state -= min_state;
-
- int nstate = max_state - min_state;
+ pos = (double) block / (double) max_block;
- double spos = (double) state / (double) nstate;
- double bpos = state_block / 100.0;
-
- pos = spos + bpos / nstate;
-
- s = String.format("block %d state %s", block, state_name);
- }
+ s = String.format("block %d of %d", block, max_block);
pbar.setString(s);
pbar.setValue((int) (pos * progress_max));
}
- public void set_value(String in_state_name, int in_state, int in_state_block, int in_block) {
- final String state_name = in_state_name;
- final int state = in_state;
- final int state_block = in_state_block;
+ public void set_max(int max_block) {
+ this.max_block = max_block;
+ }
+
+ public void set_block(int in_block) {
final int block = in_block;
Runnable r = new Runnable() {
public void run() {
try {
- set_value_internal(state_name, state, state_block, block);
+ set_block_internal(block);
} catch (Exception ex) {
}
}
}
private void reset_internal() {
- set_value_internal("startup",min_state,0, 0);
+ set_max(1);
+ set_block_internal(0);
set_flight_internal(0);
set_filename_internal("");
}