altoslib: Save eeprom data in new .eeprom format
[fw/altos] / altoslib / AltosEepromNew.java
1 /*
2  * Copyright © 2017 Keith Packard <keithp@keithp.com>
3  *
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.
8  *
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.
13  */
14
15 package org.altusmetrum.altoslib_11;
16
17 import java.util.*;
18 import java.io.*;
19
20 public class AltosEepromNew {
21
22         private AltosJson       config;
23         ArrayList<Byte>         data;
24         private AltosConfigData config_data;
25
26         /*
27          * Public accessor APIs
28          */
29         public int data8(int offset) {
30                 return ((int) data.get(offset)) & 0xff;
31         }
32
33         public int data16(int offset) {
34                 return data8(offset) | (data8(offset+1) << 8);
35         }
36
37         public int data24(int offset) {
38                 return (data8(offset) |
39                         (data8(offset+1) << 8) |
40                         (data8(offset+2) << 16));
41         }
42
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));
48         }
49
50         public int size() {
51                 return data.size();
52         }
53
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();
59
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;
66                                 }
67                         }
68                 }
69                 return config_data;
70         }
71
72         public void reset_config_data() {
73                 config_data = null;
74         }
75
76         private void write_config(Writer w) throws IOException {
77                 config.write(w, 0, true);
78                 w.append('\n');
79         }
80
81         /*
82          * Private I/O APIs
83          */
84         private void write_data(Writer w) throws IOException {
85                 PrintWriter pw = new PrintWriter(w);
86
87                 for (int i = 0; i < data.size(); i++) {
88                         if (i > 0) {
89                                 if ((i & 0x1f) == 0)
90                                         pw.printf("\n");
91                                 else
92                                         pw.printf(" ");
93                         }
94                         pw.printf("%02x", data.get(i));
95                 }
96                 w.append('\n');
97         }
98
99         private boolean read_config(Reader r) throws IOException {
100                 config = AltosJson.fromReader(r);
101                 if (config == null)
102                         return false;
103                 return true;
104         }
105
106         private boolean read_data(Reader r) throws IOException {
107                 BufferedReader  br = new BufferedReader(r);
108                 String          s;
109
110                 data = new ArrayList<Byte>();
111                 while ((s = br.readLine()) != null) {
112
113                         String[] tokens = s.split("\\s+");
114
115                         for (int i = 0; i < tokens.length; i++) {
116                                 if (tokens[i].length() > 0) {
117                                         try {
118                                                 data.add((byte) AltosLib.fromhex(tokens[i]));
119                                         } catch (NumberFormatException e) {
120                                                 throw new IOException(e.toString());
121                                         }
122                                 }
123                         }
124                 }
125                 return true;
126         }
127
128         private boolean read_old_config(BufferedReader r) throws IOException {
129                 AltosConfigData cfg = new AltosConfigData();
130                 for (;;) {
131                         boolean done = false;
132
133                         /* The data starts with an upper case F character followed by a space */
134                         r.mark(2);
135                         int     first = r.read();
136                         if (first == 'F') {
137                                 int second =  r.read();
138                                 if (second == ' ')
139                                         done = true;
140                         }
141                         r.reset();
142                         if (done)
143                                 break;
144
145                         String line = r.readLine();
146                         if (line == null)
147                                 return false;
148                         cfg.parse_line(line);
149                 }
150                 config = new AltosJson(cfg);
151                 return true;
152         }
153
154         private boolean read_old_data(BufferedReader r) throws IOException {
155                 String line;
156
157                 data = new ArrayList<Byte>();
158                 while ((line = r.readLine()) != null) {
159                         String[] tokens = line.split("\\s+");
160
161                         /* Make sure there's at least a type and time */
162                         if (tokens.length < 2)
163                                 break;
164
165                         /* packet type */
166                         if (tokens[0].length() != 1)
167                                 break;
168                         int start = data.size();
169
170                         if (config_data().log_format != AltosLib.AO_LOG_FORMAT_TINY) {
171                                 data.add((byte) tokens[0].codePointAt(0));
172
173                                 int time = AltosLib.fromhex(tokens[1]);
174
175                                 data.add((byte) 0);
176                                 data.add((byte) (time & 0xff));
177                                 data.add((byte) (time >> 8));
178                         }
179                         if (tokens.length == 4) {
180                                 /* Handle ancient log files */
181                                 if (config_data().log_format == AltosLib.AO_LOG_FORMAT_TINY) {
182                                         /*
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.
186                                          */
187                                         int a = AltosLib.fromhex(tokens[2]);
188                                         int b = AltosLib.fromhex(tokens[3]);
189                                         if (a != 0)
190                                                 b = 0x8000 | a;
191                                         data.add((byte) (b & 0xff));
192                                         data.add((byte) ((b >> 8)));
193                                 } else {
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)));
198                                         }
199                                         /* Re-compute the checksum byte */
200                                         data.set(start + 1, (byte) (256 - AltosConvert.checksum(data, start, data.size() - start)));
201                                 }
202                         } else {
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)));
207                         }
208                 }
209                 return true;
210         }
211
212         private void read(Reader r) throws IOException {
213                 BufferedReader  br = new BufferedReader(r);
214
215                 br.mark(1);
216                 int c = br.read();
217                 br.reset();
218
219                 if (c == '{') {
220                         if (!read_config(br))
221                                 throw new IOException("failed to read config");
222                         if (!read_data(br))
223                                 throw new IOException("failed to read data");
224                 } else {
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");
229                 }
230         }
231
232         /*
233          * Public APIs for I/O
234          */
235         public void write(Writer w) throws IOException {
236                 write_config(w);
237                 write_data(w);
238         }
239
240         public String toString() {
241                 try {
242                         Writer  w = new StringWriter();
243
244                         write(w);
245                         return w.toString();
246                 } catch (Exception e) {
247                         return null;
248                 }
249         }
250
251         public void print() throws IOException {
252                 System.out.printf("%s", toString());
253         }
254
255         /*
256          * Constructors
257          */
258         public AltosEepromNew(Reader r) throws IOException {
259                 read(r);
260         }
261
262         public AltosEepromNew(String s) throws IOException {
263                 read(new StringReader(s));
264         }
265
266         public AltosEepromNew(AltosJson config, ArrayList<Byte> data) {
267                 this.config = config;
268                 this.data = data;
269         }
270
271         public AltosEepromNew(AltosConfigData config_data, ArrayList<Byte> data) {
272                 this.config = new AltosJson(config_data);
273                 this.data = data;
274         }
275
276         public AltosEepromNew() {
277         }
278 }