5220f3a0b26957c3ca96f07692209e1a5893a349
[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.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;
67                                 }
68                         }
69                 }
70                 return config_data;
71         }
72
73         private void write_config(Writer w) throws IOException {
74                 config.write(w, 0, true);
75                 w.append('\n');
76         }
77
78         /*
79          * Private I/O APIs
80          */
81         private void write_data(Writer w) throws IOException {
82                 PrintWriter pw = new PrintWriter(w);
83
84                 for (int i = 0; i < data.size(); i++) {
85                         if (i > 0) {
86                                 if ((i & 0x1f) == 0)
87                                         pw.printf("\n");
88                                 else
89                                         pw.printf(" ");
90                         }
91                         pw.printf("%02x", data.get(i));
92                 }
93                 w.append('\n');
94         }
95
96         private boolean read_config(Reader r) throws IOException {
97                 config = AltosJson.fromReader(r);
98                 if (config == null)
99                         return false;
100                 return true;
101         }
102
103         private boolean read_data(Reader r) throws IOException {
104                 BufferedReader  br = new BufferedReader(r);
105                 String          s;
106
107                 data = new ArrayList<Byte>();
108                 while ((s = br.readLine()) != null) {
109
110                         String[] tokens = s.split("\\s+");
111
112                         for (int i = 0; i < tokens.length; i++) {
113                                 if (tokens[i].length() > 0) {
114                                         try {
115                                                 data.add((byte) AltosLib.fromhex(tokens[i]));
116                                         } catch (NumberFormatException e) {
117                                                 throw new IOException(e.toString());
118                                         }
119                                 }
120                         }
121                 }
122                 return true;
123         }
124
125         private boolean read_old_config(BufferedReader r) throws IOException {
126                 AltosConfigData cfg = new AltosConfigData();
127                 for (;;) {
128                         boolean done = false;
129
130                         /* The data starts with an upper case F character followed by a space */
131                         r.mark(2);
132                         int     first = r.read();
133                         if (first == 'F') {
134                                 int second =  r.read();
135                                 if (second == ' ')
136                                         done = true;
137                         }
138                         r.reset();
139                         if (done)
140                                 break;
141
142                         String line = r.readLine();
143                         if (line == null)
144                                 return false;
145                         cfg.parse_line(line);
146                 }
147                 config = new AltosJson(cfg);
148                 return true;
149         }
150
151         private boolean read_old_data(BufferedReader r) throws IOException {
152                 String line;
153
154                 data = new ArrayList<Byte>();
155                 while ((line = r.readLine()) != null) {
156                         String[] tokens = line.split("\\s+");
157
158                         /* Make sure there's at least a type and time */
159                         if (tokens.length < 2)
160                                 break;
161
162                         /* packet type */
163                         if (tokens[0].length() != 1)
164                                 break;
165                         int start = data.size();
166
167                         if (config_data().log_format != AltosLib.AO_LOG_FORMAT_TINY) {
168                                 data.add((byte) tokens[0].codePointAt(0));
169
170                                 int time = AltosLib.fromhex(tokens[1]);
171
172                                 data.add((byte) 0);
173                                 data.add((byte) (time & 0xff));
174                                 data.add((byte) (time >> 8));
175                         }
176                         if (tokens.length == 4) {
177                                 /* Handle ancient log files */
178                                 if (config_data().log_format == AltosLib.AO_LOG_FORMAT_TINY) {
179                                         /*
180                                          * Ancient TeleMini log files stored "extra" data to pretend
181                                          * that it was a TeleMetrum device. Throw that away and
182                                          * just save the actual log data.
183                                          */
184                                         int a = AltosLib.fromhex(tokens[2]);
185                                         int b = AltosLib.fromhex(tokens[3]);
186                                         if (a != 0)
187                                                 b = 0x8000 | a;
188                                         data.add((byte) (b & 0xff));
189                                         data.add((byte) ((b >> 8)));
190                                 } else {
191                                         for (int i = 2; i < tokens.length; i++) {
192                                                 int v = AltosLib.fromhex(tokens[i]);
193                                                 data.add((byte) (v & 0xff));
194                                                 data.add((byte) ((v >> 8)));
195                                         }
196                                         /* Re-compute the checksum byte */
197                                         data.set(start + 1, (byte) (256 - AltosConvert.checksum(data, start, data.size() - start)));
198                                 }
199                         } else {
200                                 for (int i = 2; i < tokens.length; i++)
201                                         data.add((byte) AltosLib.fromhex(tokens[i]));
202                                 /* Re-compute the checksum byte */
203                                 data.set(start + 1, (byte) (256 - AltosConvert.checksum(data, start, data.size() - start)));
204                         }
205                 }
206                 return true;
207         }
208
209         private void read(Reader r) throws IOException {
210                 BufferedReader  br = new BufferedReader(r);
211
212                 br.mark(1);
213                 int c = br.read();
214                 br.reset();
215
216                 if (c == '{') {
217                         if (!read_config(br))
218                                 throw new IOException("failed to read config");
219                         if (!read_data(br))
220                                 throw new IOException("failed to read data");
221                 } else {
222                         if (!read_old_config(br))
223                                 throw new IOException("failed to read old config");
224                         if (!read_old_data(br))
225                                 throw new IOException("failed to read old data");
226                 }
227         }
228
229         /*
230          * Public APIs for I/O
231          */
232         public void write(Writer w) throws IOException {
233                 write_config(w);
234                 write_data(w);
235         }
236
237         public String toString() {
238                 try {
239                         Writer  w = new StringWriter();
240
241                         write(w);
242                         return w.toString();
243                 } catch (Exception e) {
244                         return null;
245                 }
246         }
247
248         public void print() throws IOException {
249                 System.out.printf("%s", toString());
250         }
251
252         /*
253          * Constructors
254          */
255         public AltosEepromNew(Reader r) throws IOException {
256                 read(r);
257         }
258
259         public AltosEepromNew(String s) throws IOException {
260                 read(new StringReader(s));
261         }
262
263         public AltosEepromNew(AltosJson config, ArrayList<Byte> data) {
264                 this.config = config;
265                 this.data = data;
266         }
267
268         public AltosEepromNew(AltosConfigData config_data, ArrayList<Byte> data) {
269                 this.config = new AltosJson(config_data);
270                 this.data = data;
271         }
272
273         public AltosEepromNew() {
274         }
275 }