2 * Copyright © 2017 Keith Packard <keithp@keithp.com>
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, either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
15 package org.altusmetrum.altoslib_11;
20 public class AltosEeprom {
22 private AltosJson config;
24 private AltosConfigData config_data;
27 * Public accessor APIs
29 public int data8(int offset) {
30 return ((int) data.get(offset)) & 0xff;
33 public int data16(int offset) {
34 return data8(offset) | (data8(offset+1) << 8);
37 public int data24(int offset) {
38 return (data8(offset) |
39 (data8(offset+1) << 8) |
40 (data8(offset+2) << 16));
43 public int data32(int offset) {
44 return (data8(offset) |
45 (data8(offset+1) << 8) |
46 (data8(offset+2) << 16) |
47 (data8(offset+3) << 24));
54 public AltosConfigData config_data() {
55 if (config_data == null) {
56 config_data = (AltosConfigData) config.make(AltosConfigData.class);
57 if (config_data == null)
58 config_data = new AltosConfigData();
60 if (config_data.log_format == AltosLib.AO_LOG_FORMAT_UNKNOWN) {
61 config_data.log_format = AltosLib.AO_LOG_FORMAT_FULL;
62 if (config_data.product != null) {
63 if (config_data.product.startsWith("TeleMetrum"))
64 config_data.log_format = AltosLib.AO_LOG_FORMAT_FULL;
65 else if (config_data.product.startsWith("TeleMini"))
66 config_data.log_format = AltosLib.AO_LOG_FORMAT_TINY;
73 private void write_config(Writer w) throws IOException {
74 config.write(w, 0, true);
81 private void write_data(Writer w) throws IOException {
82 PrintWriter pw = new PrintWriter(w);
84 for (int i = 0; i < data.size(); i++) {
91 pw.printf("%02x", data.get(i));
96 private boolean read_config(InputStream stream) throws IOException {
97 config = AltosJson.fromInputStream(stream);
103 private String read_line(InputStream stream) throws IOException {
104 StringBuffer buffer = null;
109 if (c == -1 && buffer == null)
112 buffer = new StringBuffer();
113 if (c == -1 || c == '\n')
114 return buffer.toString();
115 buffer.append((char) c);
119 private boolean read_data(InputStream stream) throws IOException {
122 data = new ArrayList<Byte>();
123 while ((s = read_line(stream)) != null) {
125 String[] tokens = s.split("\\s+");
127 for (int i = 0; i < tokens.length; i++) {
128 if (tokens[i].length() > 0) {
130 data.add((byte) AltosLib.fromhex(tokens[i]));
131 } catch (NumberFormatException e) {
132 throw new IOException(e.toString());
140 private boolean read_old_config(InputStream stream) throws IOException {
141 AltosConfigData cfg = new AltosConfigData();
143 boolean done = false;
145 /* The data starts with an upper case F character followed by a space */
147 int first = stream.read();
149 int second = stream.read();
157 String line = read_line(stream);
160 cfg.parse_line(line);
162 config = new AltosJson(cfg);
166 private boolean read_old_data(InputStream stream) throws IOException {
169 data = new ArrayList<Byte>();
170 while ((line = read_line(stream)) != null) {
171 String[] tokens = line.split("\\s+");
173 /* Make sure there's at least a type and time */
174 if (tokens.length < 2)
178 if (tokens[0].length() != 1)
180 int start = data.size();
182 if (config_data().log_format != AltosLib.AO_LOG_FORMAT_TINY) {
183 byte cmd = (byte) tokens[0].codePointAt(0);
186 int time = AltosLib.fromhex(tokens[1]);
189 data.add((byte) (time & 0xff));
190 data.add((byte) (time >> 8));
192 if (tokens.length == 4) {
193 /* Handle ancient log files */
194 if (config_data().log_format == AltosLib.AO_LOG_FORMAT_TINY) {
196 * Ancient TeleMini log files stored "extra" data to pretend
197 * that it was a TeleMetrum device. Throw that away and
198 * just save the actual log data.
200 int a = AltosLib.fromhex(tokens[2]);
201 int b = AltosLib.fromhex(tokens[3]);
204 data.add((byte) (b & 0xff));
205 data.add((byte) ((b >> 8)));
207 for (int i = 2; i < tokens.length; i++) {
208 int v = AltosLib.fromhex(tokens[i]);
209 data.add((byte) (v & 0xff));
210 data.add((byte) ((v >> 8)));
212 /* Re-compute the checksum byte */
213 data.set(start + 1, (byte) (256 - AltosConvert.checksum(data, start, data.size() - start)));
216 for (int i = 2; i < tokens.length; i++)
217 data.add((byte) AltosLib.fromhex(tokens[i]));
218 /* Re-compute the checksum byte */
219 data.set(start + 1, (byte) (256 - AltosConvert.checksum(data, start, data.size() - start)));
225 private void read(InputStream stream) throws IOException {
226 BufferedInputStream bis = new BufferedInputStream(stream);
233 if (!read_config(bis))
234 throw new IOException("failed to read config");
236 throw new IOException("failed to read data");
238 if (!read_old_config(bis))
239 throw new IOException("failed to read old config");
240 if (!read_old_data(bis))
241 throw new IOException("failed to read old data");
246 * Public APIs for I/O
248 public void write(Writer w) throws IOException {
253 public String toString() {
255 Writer w = new StringWriter();
259 } catch (Exception e) {
264 public void print() throws IOException {
265 System.out.printf("%s", toString());
271 public AltosEeprom(InputStream stream) throws IOException {
275 public AltosEeprom(String s) throws IOException {
276 read(new AltosStringInputStream(s));
279 public AltosEeprom(AltosJson config, ArrayList<Byte> data) {
280 this.config = config;
284 public AltosEeprom(AltosConfigData config_data, ArrayList<Byte> data) {
285 this.config = new AltosJson(config_data);
289 public AltosEeprom() {