0da3df71c98740f282f785894669dc1f24786cbe
[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                                 byte cmd = (byte) tokens[0].codePointAt(0);
169                                 data.add(cmd);
170
171                                 int time = AltosLib.fromhex(tokens[1]);
172
173                                 data.add((byte) 0);
174                                 data.add((byte) (time & 0xff));
175                                 data.add((byte) (time >> 8));
176                         }
177                         if (tokens.length == 4) {
178                                 /* Handle ancient log files */
179                                 if (config_data().log_format == AltosLib.AO_LOG_FORMAT_TINY) {
180                                         /*
181                                          * Ancient TeleMini log files stored "extra" data to pretend
182                                          * that it was a TeleMetrum device. Throw that away and
183                                          * just save the actual log data.
184                                          */
185                                         int a = AltosLib.fromhex(tokens[2]);
186                                         int b = AltosLib.fromhex(tokens[3]);
187                                         if (a != 0)
188                                                 b = 0x8000 | a;
189                                         data.add((byte) (b & 0xff));
190                                         data.add((byte) ((b >> 8)));
191                                 } else {
192                                         for (int i = 2; i < tokens.length; i++) {
193                                                 int v = AltosLib.fromhex(tokens[i]);
194                                                 data.add((byte) (v & 0xff));
195                                                 data.add((byte) ((v >> 8)));
196                                         }
197                                         /* Re-compute the checksum byte */
198                                         data.set(start + 1, (byte) (256 - AltosConvert.checksum(data, start, data.size() - start)));
199                                 }
200                         } else {
201                                 for (int i = 2; i < tokens.length; i++)
202                                         data.add((byte) AltosLib.fromhex(tokens[i]));
203                                 /* Re-compute the checksum byte */
204                                 data.set(start + 1, (byte) (256 - AltosConvert.checksum(data, start, data.size() - start)));
205                         }
206                 }
207                 return true;
208         }
209
210         private void read(Reader r) throws IOException {
211                 BufferedReader  br = new BufferedReader(r);
212
213                 br.mark(1);
214                 int c = br.read();
215                 br.reset();
216
217                 if (c == '{') {
218                         if (!read_config(br))
219                                 throw new IOException("failed to read config");
220                         if (!read_data(br))
221                                 throw new IOException("failed to read data");
222                 } else {
223                         if (!read_old_config(br))
224                                 throw new IOException("failed to read old config");
225                         if (!read_old_data(br))
226                                 throw new IOException("failed to read old data");
227                 }
228         }
229
230         /*
231          * Public APIs for I/O
232          */
233         public void write(Writer w) throws IOException {
234                 write_config(w);
235                 write_data(w);
236         }
237
238         public String toString() {
239                 try {
240                         Writer  w = new StringWriter();
241
242                         write(w);
243                         return w.toString();
244                 } catch (Exception e) {
245                         return null;
246                 }
247         }
248
249         public void print() throws IOException {
250                 System.out.printf("%s", toString());
251         }
252
253         /*
254          * Constructors
255          */
256         public AltosEepromNew(Reader r) throws IOException {
257                 read(r);
258         }
259
260         public AltosEepromNew(String s) throws IOException {
261                 read(new StringReader(s));
262         }
263
264         public AltosEepromNew(AltosJson config, ArrayList<Byte> data) {
265                 this.config = config;
266                 this.data = data;
267         }
268
269         public AltosEepromNew(AltosConfigData config_data, ArrayList<Byte> data) {
270                 this.config = new AltosJson(config_data);
271                 this.data = data;
272         }
273
274         public AltosEepromNew() {
275         }
276 }