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 AltosEepromNew {
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.MISSING) {
61 if (config_data.product != null) {
62 if (config_data.product.startsWith("TeleMetrum"))
63 config_data.log_format = AltosLib.AO_LOG_FORMAT_FULL;
64 else if (config_data.product.startsWith("TeleMini"))
65 config_data.log_format = AltosLib.AO_LOG_FORMAT_TINY;
72 public void reset_config_data() {
76 private void write_config(Writer w) throws IOException {
77 config.write(w, 0, true);
84 private void write_data(Writer w) throws IOException {
85 PrintWriter pw = new PrintWriter(w);
87 for (int i = 0; i < data.size(); i++) {
94 pw.printf("%02x", data.get(i));
99 private boolean read_config(Reader r) throws IOException {
100 config = AltosJson.fromReader(r);
106 static private byte[] byte_list_to_array(List<Byte> bytes) {
107 byte[] data = new byte[bytes.size()];
110 for (Byte b : bytes) {
116 private boolean read_data(Reader r) throws IOException {
117 BufferedReader br = new BufferedReader(r);
120 data = new ArrayList<Byte>();
121 while ((s = br.readLine()) != null) {
122 String[] tokens = s.split("\\s+");
124 for (int i = 0; i < tokens.length; i++) {
126 data.add((byte) AltosLib.fromhex(tokens[i]));
127 } catch (NumberFormatException e) {
128 throw new IOException(e.toString());
135 private boolean read_old_config(BufferedReader r) throws IOException {
136 AltosConfigData cfg = new AltosConfigData();
138 boolean done = false;
140 /* The data starts with an upper case F character followed by a space */
142 int first = r.read();
144 int second = r.read();
152 String line = r.readLine();
155 cfg.parse_line(line);
157 config = new AltosJson(cfg);
161 private boolean read_old_data(BufferedReader r) throws IOException {
164 data = new ArrayList<Byte>();
165 while ((line = r.readLine()) != null) {
166 String[] tokens = line.split("\\s+");
168 /* Make sure there's at least a type and time */
169 if (tokens.length < 2)
173 if (tokens[0].length() != 1)
175 int start = data.size();
177 if (config_data().log_format != AltosLib.AO_LOG_FORMAT_TINY) {
178 data.add((byte) tokens[0].codePointAt(0));
180 int time = AltosLib.fromhex(tokens[1]);
183 data.add((byte) (time & 0xff));
184 data.add((byte) (time >> 8));
186 if (tokens.length == 4) {
187 /* Handle ancient log files */
188 if (config_data().log_format == AltosLib.AO_LOG_FORMAT_TINY) {
190 * Ancient TeleMini log files stored "extra" data to pretend
191 * that it was a TeleMetrum device. Throw that away and
192 * just save the actual log data.
194 int a = AltosLib.fromhex(tokens[2]);
195 int b = AltosLib.fromhex(tokens[3]);
198 data.add((byte) (b & 0xff));
199 data.add((byte) ((b >> 8)));
201 for (int i = 2; i < tokens.length; i++) {
202 int v = AltosLib.fromhex(tokens[i]);
203 data.add((byte) (v & 0xff));
204 data.add((byte) ((v >> 8)));
206 /* Re-compute the checksum byte */
207 data.set(start + 1, (byte) (256 - AltosConvert.checksum(data, start, data.size() - start)));
210 for (int i = 2; i < tokens.length; i++)
211 data.add((byte) AltosLib.fromhex(tokens[i]));
212 /* Re-compute the checksum byte */
213 data.set(start + 1, (byte) (256 - AltosConvert.checksum(data, start, data.size() - start)));
219 private void read(Reader r) throws IOException {
220 BufferedReader br = new BufferedReader(r);
227 if (!read_config(br))
228 throw new IOException("failed to read config");
230 throw new IOException("failed to read data");
232 if (!read_old_config(br))
233 throw new IOException("failed to read old config");
234 if (!read_old_data(br))
235 throw new IOException("failed to read old data");
240 * Public APIs for I/O
242 public void write(Writer w) throws IOException {
247 public String toString() {
249 Writer w = new StringWriter();
253 } catch (Exception e) {
258 public void print() throws IOException {
259 System.out.printf("%s", toString());
265 public AltosEepromNew(Reader r) throws IOException {
269 public AltosEepromNew(String s) throws IOException {
270 read(new StringReader(s));
273 public AltosEepromNew() {