[go: up one dir, main page]

File: AltosEeprom.java

package info (click to toggle)
altos 1.9-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 67,272 kB
  • sloc: ansic: 90,607; java: 39,048; makefile: 6,591; sh: 3,008; xml: 1,972; pascal: 1,597
file content (292 lines) | stat: -rw-r--r-- 7,097 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*
 * Copyright © 2017 Keith Packard <keithp@keithp.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 */

package org.altusmetrum.altoslib_13;

import java.util.*;
import java.io.*;

public class AltosEeprom {

	private AltosJson	config;
	ArrayList<Byte>		data;
	private AltosConfigData	config_data;
	int			errors = 0;

	/*
	 * Public accessor APIs
	 */
	public int data8(int offset) {
		return ((int) data.get(offset)) & 0xff;
	}

	public int data16(int offset) {
		return data8(offset) | (data8(offset+1) << 8);
	}

	public int data24(int offset) {
		return (data8(offset) |
			(data8(offset+1) << 8) |
			(data8(offset+2) << 16));
	}

	public int data32(int offset) {
		return (data8(offset) |
			(data8(offset+1) << 8) |
			(data8(offset+2) << 16) |
			(data8(offset+3) << 24));
	}

	public int size() {
		return data.size();
	}

	public AltosConfigData config_data() {
		if (config_data == null) {
			config_data = (AltosConfigData) config.make(AltosConfigData.class);
			if (config_data == null)
				config_data = new AltosConfigData();

			if (config_data.log_format == AltosLib.AO_LOG_FORMAT_UNKNOWN) {
				config_data.log_format = AltosLib.AO_LOG_FORMAT_FULL;
				if (config_data.product != null) {
					if (config_data.product.startsWith("TeleMetrum"))
						config_data.log_format = AltosLib.AO_LOG_FORMAT_FULL;
					else if (config_data.product.startsWith("TeleMini"))
						config_data.log_format = AltosLib.AO_LOG_FORMAT_TINY;
				}
			}
		}
		return config_data;
	}

	private void write_config(Writer w) throws IOException {
		config.write(w, 0, true);
		w.append('\n');
	}

	/*
	 * Private I/O APIs
	 */
	private void write_data(Writer w) throws IOException {
		PrintWriter pw = new PrintWriter(w);

		for (int i = 0; i < data.size(); i++) {
			if (i > 0) {
				if ((i & 0x1f) == 0)
					pw.printf("\n");
				else
					pw.printf(" ");
			}
			pw.printf("%02x", data.get(i));
		}
		w.append('\n');
	}

	private boolean read_config(InputStream stream) throws IOException {
		config = AltosJson.fromInputStream(stream);
		if (config == null)
			return false;
		return true;
	}

	private String read_line(InputStream stream) throws IOException {
		StringBuffer	buffer = null;
		int		c;

		for (;;) {
			c = stream.read();
			if (c == -1 && buffer == null)
				return null;
			if (buffer == null)
				buffer = new StringBuffer();
			if (c == -1 || c == '\n')
				return buffer.toString();
			buffer.append((char) c);
		}
	}

	private boolean read_data(InputStream stream) throws IOException {
		String			s;

		data = new ArrayList<Byte>();
		while ((s = read_line(stream)) != null) {

			String[] tokens = s.split("\\s+");

			for (int i = 0; i < tokens.length; i++) {
				if (tokens[i].length() > 0) {
					try {
						data.add((byte) AltosLib.fromhex(tokens[i]));
					} catch (NumberFormatException e) {
						throw new IOException(e.toString());
					}
				}
			}
		}
		return true;
	}

	private boolean read_old_config(InputStream stream) throws IOException {
		AltosConfigData	cfg = new AltosConfigData();
		for (;;) {
			boolean	done = false;

			/* The data starts with an upper case F character followed by a space */
			stream.mark(2);
			int	first = stream.read();
			if (first == 'F') {
				int second =  stream.read();
				if (second == ' ')
					done = true;
			}
			stream.reset();
			if (done)
				break;

			String line = read_line(stream);
			if (line == null)
				return false;
			cfg.parse_line(line);
		}
		config = new AltosJson(cfg);
		return true;
	}

	private boolean read_old_data(InputStream stream) throws IOException {
		String line;

		data = new ArrayList<Byte>();
		while ((line = read_line(stream)) != null) {
			String[] tokens = line.split("\\s+");

			/* Make sure there's at least a type and time */
			if (tokens.length < 2)
				break;

			/* packet type */
			if (tokens[0].length() != 1)
				break;
			int start = data.size();

			if (config_data().log_format != AltosLib.AO_LOG_FORMAT_TINY) {
				byte cmd = (byte) tokens[0].codePointAt(0);
				data.add(cmd);

				int time = AltosLib.fromhex(tokens[1]);

				data.add((byte) 0);
				data.add((byte) (time & 0xff));
				data.add((byte) (time >> 8));
			}
			if (tokens.length == 4) {
				/* Handle ancient log files */
				if (config_data().log_format == AltosLib.AO_LOG_FORMAT_TINY) {
					/*
					 * Ancient TeleMini log files stored "extra" data to pretend
					 * that it was a TeleMetrum device. Throw that away and
					 * just save the actual log data.
					 */
					int a = AltosLib.fromhex(tokens[2]);
					int b = AltosLib.fromhex(tokens[3]);
					if (a != 0)
						b = 0x8000 | a;
					data.add((byte) (b & 0xff));
					data.add((byte) ((b >> 8)));
				} else {
					for (int i = 2; i < tokens.length; i++) {
						int v = AltosLib.fromhex(tokens[i]);
						data.add((byte) (v & 0xff));
						data.add((byte) ((v >> 8)));
					}
					/* Re-compute the checksum byte */
					data.set(start + 1, (byte) (256 - AltosConvert.checksum(data, start, data.size() - start)));
				}
			} else {
				for (int i = 2; i < tokens.length; i++)
					data.add((byte) AltosLib.fromhex(tokens[i]));
				/* Re-compute the checksum byte */
				data.set(start + 1, (byte) (256 - AltosConvert.checksum(data, start, data.size() - start)));
			}
		}
		return true;
	}

	private void read(InputStream stream) throws IOException {
		BufferedInputStream	bis = new BufferedInputStream(stream);

		bis.mark(1);
		int c = bis.read();
		bis.reset();

		if (c == '{') {
			if (!read_config(bis))
				throw new IOException("failed to read config");
			if (!read_data(bis))
				throw new IOException("failed to read data");
		} else {
			if (!read_old_config(bis))
				throw new IOException("failed to read old config");
			if (!read_old_data(bis))
				throw new IOException("failed to read old data");
		}
	}

	/*
	 * Public APIs for I/O
	 */
	public void write(Writer w) throws IOException {
		write_config(w);
		write_data(w);
	}

	public String toString() {
		try {
			Writer	w = new StringWriter();

			write(w);
			return w.toString();
		} catch (Exception e) {
			return null;
		}
	}

	public void print() throws IOException {
		System.out.printf("%s", toString());
	}

	/*
	 * Constructors
	 */
	public AltosEeprom(InputStream stream) throws IOException {
		read(stream);
	}

	public AltosEeprom(String s) throws IOException {
		read(new AltosStringInputStream(s));
	}

	public AltosEeprom(AltosJson config, ArrayList<Byte> data) {
		this.config = config;
		this.data = data;
	}

	public AltosEeprom(AltosConfigData config_data, ArrayList<Byte> data) {
		this.config = new AltosJson(config_data);
		this.data = data;
	}

	public AltosEeprom() {
	}
}