From: Keith Packard Date: Tue, 13 Jun 2017 17:46:03 +0000 (-0700) Subject: altoslib: Rename AltosEepromNew to AltosEeprom X-Git-Tag: 1.8~29 X-Git-Url: https://git.gag.com/?p=fw%2Faltos;a=commitdiff_plain;h=e4f1fa544f1cb21070c9d79c81ebae0fb71d77db altoslib: Rename AltosEepromNew to AltosEeprom Signed-off-by: Keith Packard --- diff --git a/altoslib/AltosEeprom.java b/altoslib/AltosEeprom.java new file mode 100644 index 00000000..2600e802 --- /dev/null +++ b/altoslib/AltosEeprom.java @@ -0,0 +1,291 @@ +/* + * Copyright © 2017 Keith Packard + * + * 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. + */ + +package org.altusmetrum.altoslib_11; + +import java.util.*; +import java.io.*; + +public class AltosEeprom { + + private AltosJson config; + ArrayList data; + private AltosConfigData config_data; + + /* + * Public accessor APIs + */ + public int data8(int offset) { + return ((int) data.get(offset)) & 0xff; + } + + public int data16(int offset) { + return data8(offset) | (data8(offset+1) << 8); + } + + public int data24(int offset) { + return (data8(offset) | + (data8(offset+1) << 8) | + (data8(offset+2) << 16)); + } + + public int data32(int offset) { + return (data8(offset) | + (data8(offset+1) << 8) | + (data8(offset+2) << 16) | + (data8(offset+3) << 24)); + } + + public int size() { + return data.size(); + } + + public AltosConfigData config_data() { + if (config_data == null) { + config_data = (AltosConfigData) config.make(AltosConfigData.class); + if (config_data == null) + config_data = new AltosConfigData(); + + if (config_data.log_format == AltosLib.AO_LOG_FORMAT_UNKNOWN) { + config_data.log_format = AltosLib.AO_LOG_FORMAT_FULL; + if (config_data.product != null) { + if (config_data.product.startsWith("TeleMetrum")) + config_data.log_format = AltosLib.AO_LOG_FORMAT_FULL; + else if (config_data.product.startsWith("TeleMini")) + config_data.log_format = AltosLib.AO_LOG_FORMAT_TINY; + } + } + } + return config_data; + } + + private void write_config(Writer w) throws IOException { + config.write(w, 0, true); + w.append('\n'); + } + + /* + * Private I/O APIs + */ + private void write_data(Writer w) throws IOException { + PrintWriter pw = new PrintWriter(w); + + for (int i = 0; i < data.size(); i++) { + if (i > 0) { + if ((i & 0x1f) == 0) + pw.printf("\n"); + else + pw.printf(" "); + } + pw.printf("%02x", data.get(i)); + } + w.append('\n'); + } + + private boolean read_config(InputStream stream) throws IOException { + config = AltosJson.fromInputStream(stream); + if (config == null) + return false; + return true; + } + + private String read_line(InputStream stream) throws IOException { + StringBuffer buffer = null; + int c; + + for (;;) { + c = stream.read(); + if (c == -1 && buffer == null) + return null; + if (buffer == null) + buffer = new StringBuffer(); + if (c == -1 || c == '\n') + return buffer.toString(); + buffer.append((char) c); + } + } + + private boolean read_data(InputStream stream) throws IOException { + String s; + + data = new ArrayList(); + while ((s = read_line(stream)) != null) { + + String[] tokens = s.split("\\s+"); + + for (int i = 0; i < tokens.length; i++) { + if (tokens[i].length() > 0) { + try { + data.add((byte) AltosLib.fromhex(tokens[i])); + } catch (NumberFormatException e) { + throw new IOException(e.toString()); + } + } + } + } + return true; + } + + private boolean read_old_config(InputStream stream) throws IOException { + AltosConfigData cfg = new AltosConfigData(); + for (;;) { + boolean done = false; + + /* The data starts with an upper case F character followed by a space */ + stream.mark(2); + int first = stream.read(); + if (first == 'F') { + int second = stream.read(); + if (second == ' ') + done = true; + } + stream.reset(); + if (done) + break; + + String line = read_line(stream); + if (line == null) + return false; + cfg.parse_line(line); + } + config = new AltosJson(cfg); + return true; + } + + private boolean read_old_data(InputStream stream) throws IOException { + String line; + + data = new ArrayList(); + while ((line = read_line(stream)) != null) { + String[] tokens = line.split("\\s+"); + + /* Make sure there's at least a type and time */ + if (tokens.length < 2) + break; + + /* packet type */ + if (tokens[0].length() != 1) + break; + int start = data.size(); + + if (config_data().log_format != AltosLib.AO_LOG_FORMAT_TINY) { + byte cmd = (byte) tokens[0].codePointAt(0); + data.add(cmd); + + int time = AltosLib.fromhex(tokens[1]); + + data.add((byte) 0); + data.add((byte) (time & 0xff)); + data.add((byte) (time >> 8)); + } + if (tokens.length == 4) { + /* Handle ancient log files */ + if (config_data().log_format == AltosLib.AO_LOG_FORMAT_TINY) { + /* + * Ancient TeleMini log files stored "extra" data to pretend + * that it was a TeleMetrum device. Throw that away and + * just save the actual log data. + */ + int a = AltosLib.fromhex(tokens[2]); + int b = AltosLib.fromhex(tokens[3]); + if (a != 0) + b = 0x8000 | a; + data.add((byte) (b & 0xff)); + data.add((byte) ((b >> 8))); + } else { + for (int i = 2; i < tokens.length; i++) { + int v = AltosLib.fromhex(tokens[i]); + data.add((byte) (v & 0xff)); + data.add((byte) ((v >> 8))); + } + /* Re-compute the checksum byte */ + data.set(start + 1, (byte) (256 - AltosConvert.checksum(data, start, data.size() - start))); + } + } else { + for (int i = 2; i < tokens.length; i++) + data.add((byte) AltosLib.fromhex(tokens[i])); + /* Re-compute the checksum byte */ + data.set(start + 1, (byte) (256 - AltosConvert.checksum(data, start, data.size() - start))); + } + } + return true; + } + + private void read(InputStream stream) throws IOException { + BufferedInputStream bis = new BufferedInputStream(stream); + + bis.mark(1); + int c = bis.read(); + bis.reset(); + + if (c == '{') { + if (!read_config(bis)) + throw new IOException("failed to read config"); + if (!read_data(bis)) + throw new IOException("failed to read data"); + } else { + if (!read_old_config(bis)) + throw new IOException("failed to read old config"); + if (!read_old_data(bis)) + throw new IOException("failed to read old data"); + } + } + + /* + * Public APIs for I/O + */ + public void write(Writer w) throws IOException { + write_config(w); + write_data(w); + } + + public String toString() { + try { + Writer w = new StringWriter(); + + write(w); + return w.toString(); + } catch (Exception e) { + return null; + } + } + + public void print() throws IOException { + System.out.printf("%s", toString()); + } + + /* + * Constructors + */ + public AltosEeprom(InputStream stream) throws IOException { + read(stream); + } + + public AltosEeprom(String s) throws IOException { + read(new AltosStringInputStream(s)); + } + + public AltosEeprom(AltosJson config, ArrayList data) { + this.config = config; + this.data = data; + } + + public AltosEeprom(AltosConfigData config_data, ArrayList data) { + this.config = new AltosJson(config_data); + this.data = data; + } + + public AltosEeprom() { + } +} diff --git a/altoslib/AltosEepromDownload.java b/altoslib/AltosEepromDownload.java index 8a2053ec..961b32cd 100644 --- a/altoslib/AltosEepromDownload.java +++ b/altoslib/AltosEepromDownload.java @@ -181,7 +181,7 @@ public class AltosEepromDownload implements Runnable { } /* Construct our internal representation of the eeprom data */ - AltosEepromNew eeprom = new AltosEepromNew(flights.config_data, data); + AltosEeprom eeprom = new AltosEeprom(flights.config_data, data); /* Now see if we can't actually parse the resulting * file to generate a better filename. Note that this diff --git a/altoslib/AltosEepromNew.java b/altoslib/AltosEepromNew.java deleted file mode 100644 index c373bff3..00000000 --- a/altoslib/AltosEepromNew.java +++ /dev/null @@ -1,291 +0,0 @@ -/* - * Copyright © 2017 Keith Packard - * - * 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. - */ - -package org.altusmetrum.altoslib_11; - -import java.util.*; -import java.io.*; - -public class AltosEepromNew { - - private AltosJson config; - ArrayList data; - private AltosConfigData config_data; - - /* - * Public accessor APIs - */ - public int data8(int offset) { - return ((int) data.get(offset)) & 0xff; - } - - public int data16(int offset) { - return data8(offset) | (data8(offset+1) << 8); - } - - public int data24(int offset) { - return (data8(offset) | - (data8(offset+1) << 8) | - (data8(offset+2) << 16)); - } - - public int data32(int offset) { - return (data8(offset) | - (data8(offset+1) << 8) | - (data8(offset+2) << 16) | - (data8(offset+3) << 24)); - } - - public int size() { - return data.size(); - } - - public AltosConfigData config_data() { - if (config_data == null) { - config_data = (AltosConfigData) config.make(AltosConfigData.class); - if (config_data == null) - config_data = new AltosConfigData(); - - if (config_data.log_format == AltosLib.AO_LOG_FORMAT_UNKNOWN) { - config_data.log_format = AltosLib.AO_LOG_FORMAT_FULL; - if (config_data.product != null) { - if (config_data.product.startsWith("TeleMetrum")) - config_data.log_format = AltosLib.AO_LOG_FORMAT_FULL; - else if (config_data.product.startsWith("TeleMini")) - config_data.log_format = AltosLib.AO_LOG_FORMAT_TINY; - } - } - } - return config_data; - } - - private void write_config(Writer w) throws IOException { - config.write(w, 0, true); - w.append('\n'); - } - - /* - * Private I/O APIs - */ - private void write_data(Writer w) throws IOException { - PrintWriter pw = new PrintWriter(w); - - for (int i = 0; i < data.size(); i++) { - if (i > 0) { - if ((i & 0x1f) == 0) - pw.printf("\n"); - else - pw.printf(" "); - } - pw.printf("%02x", data.get(i)); - } - w.append('\n'); - } - - private boolean read_config(InputStream stream) throws IOException { - config = AltosJson.fromInputStream(stream); - if (config == null) - return false; - return true; - } - - private String read_line(InputStream stream) throws IOException { - StringBuffer buffer = null; - int c; - - for (;;) { - c = stream.read(); - if (c == -1 && buffer == null) - return null; - if (buffer == null) - buffer = new StringBuffer(); - if (c == -1 || c == '\n') - return buffer.toString(); - buffer.append((char) c); - } - } - - private boolean read_data(InputStream stream) throws IOException { - String s; - - data = new ArrayList(); - while ((s = read_line(stream)) != null) { - - String[] tokens = s.split("\\s+"); - - for (int i = 0; i < tokens.length; i++) { - if (tokens[i].length() > 0) { - try { - data.add((byte) AltosLib.fromhex(tokens[i])); - } catch (NumberFormatException e) { - throw new IOException(e.toString()); - } - } - } - } - return true; - } - - private boolean read_old_config(InputStream stream) throws IOException { - AltosConfigData cfg = new AltosConfigData(); - for (;;) { - boolean done = false; - - /* The data starts with an upper case F character followed by a space */ - stream.mark(2); - int first = stream.read(); - if (first == 'F') { - int second = stream.read(); - if (second == ' ') - done = true; - } - stream.reset(); - if (done) - break; - - String line = read_line(stream); - if (line == null) - return false; - cfg.parse_line(line); - } - config = new AltosJson(cfg); - return true; - } - - private boolean read_old_data(InputStream stream) throws IOException { - String line; - - data = new ArrayList(); - while ((line = read_line(stream)) != null) { - String[] tokens = line.split("\\s+"); - - /* Make sure there's at least a type and time */ - if (tokens.length < 2) - break; - - /* packet type */ - if (tokens[0].length() != 1) - break; - int start = data.size(); - - if (config_data().log_format != AltosLib.AO_LOG_FORMAT_TINY) { - byte cmd = (byte) tokens[0].codePointAt(0); - data.add(cmd); - - int time = AltosLib.fromhex(tokens[1]); - - data.add((byte) 0); - data.add((byte) (time & 0xff)); - data.add((byte) (time >> 8)); - } - if (tokens.length == 4) { - /* Handle ancient log files */ - if (config_data().log_format == AltosLib.AO_LOG_FORMAT_TINY) { - /* - * Ancient TeleMini log files stored "extra" data to pretend - * that it was a TeleMetrum device. Throw that away and - * just save the actual log data. - */ - int a = AltosLib.fromhex(tokens[2]); - int b = AltosLib.fromhex(tokens[3]); - if (a != 0) - b = 0x8000 | a; - data.add((byte) (b & 0xff)); - data.add((byte) ((b >> 8))); - } else { - for (int i = 2; i < tokens.length; i++) { - int v = AltosLib.fromhex(tokens[i]); - data.add((byte) (v & 0xff)); - data.add((byte) ((v >> 8))); - } - /* Re-compute the checksum byte */ - data.set(start + 1, (byte) (256 - AltosConvert.checksum(data, start, data.size() - start))); - } - } else { - for (int i = 2; i < tokens.length; i++) - data.add((byte) AltosLib.fromhex(tokens[i])); - /* Re-compute the checksum byte */ - data.set(start + 1, (byte) (256 - AltosConvert.checksum(data, start, data.size() - start))); - } - } - return true; - } - - private void read(InputStream stream) throws IOException { - BufferedInputStream bis = new BufferedInputStream(stream); - - bis.mark(1); - int c = bis.read(); - bis.reset(); - - if (c == '{') { - if (!read_config(bis)) - throw new IOException("failed to read config"); - if (!read_data(bis)) - throw new IOException("failed to read data"); - } else { - if (!read_old_config(bis)) - throw new IOException("failed to read old config"); - if (!read_old_data(bis)) - throw new IOException("failed to read old data"); - } - } - - /* - * Public APIs for I/O - */ - public void write(Writer w) throws IOException { - write_config(w); - write_data(w); - } - - public String toString() { - try { - Writer w = new StringWriter(); - - write(w); - return w.toString(); - } catch (Exception e) { - return null; - } - } - - public void print() throws IOException { - System.out.printf("%s", toString()); - } - - /* - * Constructors - */ - public AltosEepromNew(InputStream stream) throws IOException { - read(stream); - } - - public AltosEepromNew(String s) throws IOException { - read(new AltosStringInputStream(s)); - } - - public AltosEepromNew(AltosJson config, ArrayList data) { - this.config = config; - this.data = data; - } - - public AltosEepromNew(AltosConfigData config_data, ArrayList data) { - this.config = new AltosJson(config_data); - this.data = data; - } - - public AltosEepromNew() { - } -} diff --git a/altoslib/AltosEepromRecord.java b/altoslib/AltosEepromRecord.java index 75fe5381..c479a621 100644 --- a/altoslib/AltosEepromRecord.java +++ b/altoslib/AltosEepromRecord.java @@ -16,7 +16,7 @@ package org.altusmetrum.altoslib_11; public abstract class AltosEepromRecord implements Comparable { - AltosEepromNew eeprom; + AltosEeprom eeprom; int wide_tick; @@ -115,7 +115,7 @@ public abstract class AltosEepromRecord implements Comparable public abstract AltosEepromRecord next(); - public AltosEepromRecord(AltosEepromNew eeprom, int start, int length) { + public AltosEepromRecord(AltosEeprom eeprom, int start, int length) { this.eeprom = eeprom; this.start = start; this.length = length; diff --git a/altoslib/AltosEepromRecordFireTwo.java b/altoslib/AltosEepromRecordFireTwo.java index 713d0bb7..48d480c5 100644 --- a/altoslib/AltosEepromRecordFireTwo.java +++ b/altoslib/AltosEepromRecordFireTwo.java @@ -92,11 +92,11 @@ public class AltosEepromRecordFireTwo extends AltosEepromRecord { return new AltosEepromRecordFireTwo(eeprom, s); } - public AltosEepromRecordFireTwo(AltosEepromNew eeprom, int start) { + public AltosEepromRecordFireTwo(AltosEeprom eeprom, int start) { super(eeprom, start, record_length); } - public AltosEepromRecordFireTwo(AltosEepromNew eeprom) { + public AltosEepromRecordFireTwo(AltosEeprom eeprom) { this(eeprom, 0); } } diff --git a/altoslib/AltosEepromRecordFull.java b/altoslib/AltosEepromRecordFull.java index c29da025..10ae0bef 100644 --- a/altoslib/AltosEepromRecordFull.java +++ b/altoslib/AltosEepromRecordFull.java @@ -104,11 +104,11 @@ public class AltosEepromRecordFull extends AltosEepromRecord { return new AltosEepromRecordFull(eeprom, s); } - public AltosEepromRecordFull(AltosEepromNew eeprom, int start) { + public AltosEepromRecordFull(AltosEeprom eeprom, int start) { super(eeprom, start, record_length); } - public AltosEepromRecordFull(AltosEepromNew eeprom) { + public AltosEepromRecordFull(AltosEeprom eeprom) { this(eeprom, 0); } } diff --git a/altoslib/AltosEepromRecordGps.java b/altoslib/AltosEepromRecordGps.java index a992abff..ee76d073 100644 --- a/altoslib/AltosEepromRecordGps.java +++ b/altoslib/AltosEepromRecordGps.java @@ -135,11 +135,11 @@ public class AltosEepromRecordGps extends AltosEepromRecord { return new AltosEepromRecordGps(eeprom, s); } - public AltosEepromRecordGps(AltosEepromNew eeprom, int start) { + public AltosEepromRecordGps(AltosEeprom eeprom, int start) { super(eeprom, start, record_length); } - public AltosEepromRecordGps(AltosEepromNew eeprom) { + public AltosEepromRecordGps(AltosEeprom eeprom) { this(eeprom, 0); } } diff --git a/altoslib/AltosEepromRecordMega.java b/altoslib/AltosEepromRecordMega.java index d0da1b7c..fdbbd6f7 100644 --- a/altoslib/AltosEepromRecordMega.java +++ b/altoslib/AltosEepromRecordMega.java @@ -249,12 +249,12 @@ public class AltosEepromRecordMega extends AltosEepromRecord { return new AltosEepromRecordMega(eeprom, s); } - public AltosEepromRecordMega(AltosEepromNew eeprom, int start) { + public AltosEepromRecordMega(AltosEeprom eeprom, int start) { super(eeprom, start, record_length); log_format = eeprom.config_data().log_format; } - public AltosEepromRecordMega(AltosEepromNew eeprom) { + public AltosEepromRecordMega(AltosEeprom eeprom) { this(eeprom, 0); } } diff --git a/altoslib/AltosEepromRecordMetrum.java b/altoslib/AltosEepromRecordMetrum.java index 248709b3..65d8f086 100644 --- a/altoslib/AltosEepromRecordMetrum.java +++ b/altoslib/AltosEepromRecordMetrum.java @@ -135,11 +135,11 @@ public class AltosEepromRecordMetrum extends AltosEepromRecord { return new AltosEepromRecordMetrum(eeprom, s); } - public AltosEepromRecordMetrum(AltosEepromNew eeprom, int start) { + public AltosEepromRecordMetrum(AltosEeprom eeprom, int start) { super(eeprom, start, record_length); } - public AltosEepromRecordMetrum(AltosEepromNew eeprom) { + public AltosEepromRecordMetrum(AltosEeprom eeprom) { this(eeprom, 0); } } diff --git a/altoslib/AltosEepromRecordMini.java b/altoslib/AltosEepromRecordMini.java index 22bd073a..787f4e0d 100644 --- a/altoslib/AltosEepromRecordMini.java +++ b/altoslib/AltosEepromRecordMini.java @@ -95,11 +95,11 @@ public class AltosEepromRecordMini extends AltosEepromRecord { return new AltosEepromRecordMini(eeprom, s); } - public AltosEepromRecordMini(AltosEepromNew eeprom, int start) { + public AltosEepromRecordMini(AltosEeprom eeprom, int start) { super(eeprom, start, record_length); } - public AltosEepromRecordMini(AltosEepromNew eeprom) { + public AltosEepromRecordMini(AltosEeprom eeprom) { this(eeprom, 0); } } diff --git a/altoslib/AltosEepromRecordSet.java b/altoslib/AltosEepromRecordSet.java index 1f10e677..df1aad9f 100644 --- a/altoslib/AltosEepromRecordSet.java +++ b/altoslib/AltosEepromRecordSet.java @@ -18,7 +18,7 @@ import java.io.*; import java.util.*; public class AltosEepromRecordSet implements AltosRecordSet { - AltosEepromNew eeprom; + AltosEeprom eeprom; TreeSet ordered; AltosCalData cal_data; @@ -50,7 +50,7 @@ public class AltosEepromRecordSet implements AltosRecordSet { listener.finish(); } - public AltosEepromRecordSet(AltosEepromNew eeprom) { + public AltosEepromRecordSet(AltosEeprom eeprom) { this.eeprom = eeprom; AltosConfigData config_data = eeprom.config_data(); @@ -115,6 +115,6 @@ public class AltosEepromRecordSet implements AltosRecordSet { } public AltosEepromRecordSet(InputStream input) throws IOException { - this(new AltosEepromNew(input)); + this(new AltosEeprom(input)); } } diff --git a/altoslib/AltosEepromRecordTiny.java b/altoslib/AltosEepromRecordTiny.java index 70960a2a..628e18c3 100644 --- a/altoslib/AltosEepromRecordTiny.java +++ b/altoslib/AltosEepromRecordTiny.java @@ -77,11 +77,11 @@ public class AltosEepromRecordTiny extends AltosEepromRecord implements AltosDat return new AltosEepromRecordTiny(eeprom, s); } - public AltosEepromRecordTiny(AltosEepromNew eeprom, int start) { + public AltosEepromRecordTiny(AltosEeprom eeprom, int start) { super(eeprom, start, record_length); } - public AltosEepromRecordTiny(AltosEepromNew eeprom) { + public AltosEepromRecordTiny(AltosEeprom eeprom) { this(eeprom, 0); } } diff --git a/altoslib/Makefile.am b/altoslib/Makefile.am index ce934b4f..11b5d562 100644 --- a/altoslib/Makefile.am +++ b/altoslib/Makefile.am @@ -35,7 +35,7 @@ altoslib_JAVA = \ AltosCRCException.java \ AltosCSV.java \ AltosDebug.java \ - AltosEepromNew.java \ + AltosEeprom.java \ AltosRecordSet.java \ AltosEepromRecord.java \ AltosEepromRecordFull.java \