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 private boolean read_data(Reader r) throws IOException {
107 BufferedReader br = new BufferedReader(r);
110 data = new ArrayList<Byte>();
111 while ((s = br.readLine()) != null) {
113 String[] tokens = s.split("\\s+");
115 for (int i = 0; i < tokens.length; i++) {
116 if (tokens[i].length() > 0) {
118 data.add((byte) AltosLib.fromhex(tokens[i]));
119 } catch (NumberFormatException e) {
120 throw new IOException(e.toString());
128 private boolean read_old_config(BufferedReader r) throws IOException {
129 AltosConfigData cfg = new AltosConfigData();
131 boolean done = false;
133 /* The data starts with an upper case F character followed by a space */
135 int first = r.read();
137 int second = r.read();
145 String line = r.readLine();
148 cfg.parse_line(line);
150 config = new AltosJson(cfg);
154 private boolean read_old_data(BufferedReader r) throws IOException {
157 data = new ArrayList<Byte>();
158 while ((line = r.readLine()) != null) {
159 String[] tokens = line.split("\\s+");
161 /* Make sure there's at least a type and time */
162 if (tokens.length < 2)
166 if (tokens[0].length() != 1)
168 int start = data.size();
170 if (config_data().log_format != AltosLib.AO_LOG_FORMAT_TINY) {
171 data.add((byte) tokens[0].codePointAt(0));
173 int time = AltosLib.fromhex(tokens[1]);
176 data.add((byte) (time & 0xff));
177 data.add((byte) (time >> 8));
179 if (tokens.length == 4) {
180 /* Handle ancient log files */
181 if (config_data().log_format == AltosLib.AO_LOG_FORMAT_TINY) {
183 * Ancient TeleMini log files stored "extra" data to pretend
184 * that it was a TeleMetrum device. Throw that away and
185 * just save the actual log data.
187 int a = AltosLib.fromhex(tokens[2]);
188 int b = AltosLib.fromhex(tokens[3]);
191 data.add((byte) (b & 0xff));
192 data.add((byte) ((b >> 8)));
194 for (int i = 2; i < tokens.length; i++) {
195 int v = AltosLib.fromhex(tokens[i]);
196 data.add((byte) (v & 0xff));
197 data.add((byte) ((v >> 8)));
199 /* Re-compute the checksum byte */
200 data.set(start + 1, (byte) (256 - AltosConvert.checksum(data, start, data.size() - start)));
203 for (int i = 2; i < tokens.length; i++)
204 data.add((byte) AltosLib.fromhex(tokens[i]));
205 /* Re-compute the checksum byte */
206 data.set(start + 1, (byte) (256 - AltosConvert.checksum(data, start, data.size() - start)));
212 private void read(Reader r) throws IOException {
213 BufferedReader br = new BufferedReader(r);
220 if (!read_config(br))
221 throw new IOException("failed to read config");
223 throw new IOException("failed to read data");
225 if (!read_old_config(br))
226 throw new IOException("failed to read old config");
227 if (!read_old_data(br))
228 throw new IOException("failed to read old data");
233 * Public APIs for I/O
235 public void write(Writer w) throws IOException {
240 public String toString() {
242 Writer w = new StringWriter();
246 } catch (Exception e) {
251 public void print() throws IOException {
252 System.out.printf("%s", toString());
258 public AltosEepromNew(Reader r) throws IOException {
262 public AltosEepromNew(String s) throws IOException {
263 read(new StringReader(s));
266 public AltosEepromNew(AltosJson config, ArrayList<Byte> data) {
267 this.config = config;
271 public AltosEepromNew(AltosConfigData config_data, ArrayList<Byte> data) {
272 this.config = new AltosJson(config_data);
276 public AltosEepromNew() {