31646c7e4543a923b29fc22f780b9b1d4cd04b93
[fw/altos] / altoslib / AltosEeprom.java
1 /*
2  * Copyright © 2013 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; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 package org.altusmetrum.altoslib_1;
19
20 import java.io.*;
21 import java.util.*;
22 import java.text.*;
23
24 public abstract class AltosEeprom implements AltosStateUpdate {
25         public int      cmd;
26         public int      tick;
27         public int      data8[];
28         public boolean  valid;
29
30         public final static int header_length = 4;
31
32         public abstract void update_state(AltosState state);
33
34         public void write(PrintStream out) {
35                 out.printf("%c %04x", cmd, tick);
36                 if (data8 != null) {
37                         for (int i = 0; i < data8.length; i++)
38                                 out.printf (" %02x", data8[i]);
39                 }
40                 out.printf ("\n");
41         }
42
43         void parse_chunk(AltosEepromChunk chunk, int start, int record_length) throws ParseException {
44                 cmd = chunk.data(start);
45
46                 int data_length = record_length - header_length;
47
48                 valid = !chunk.erased(start, record_length);
49                 if (valid) {
50                         if (AltosConvert.checksum(chunk.data, start, record_length) != 0)
51                                 throw new ParseException(String.format("invalid checksum at 0x%x",
52                                                                        chunk.address + start), 0);
53                 } else {
54                         cmd = AltosLib.AO_LOG_INVALID;
55                 }
56
57                 tick = chunk.data16(start+2);
58
59                 data8 = new int[data_length];
60                 for (int i = 0; i < data_length; i++)
61                         data8[i] = chunk.data(start + header_length + i);
62         }
63
64         void parse_string(String line, int record_length) {
65                 valid = false;
66                 tick = 0;
67                 cmd = AltosLib.AO_LOG_INVALID;
68
69                 int data_length = record_length - header_length;
70
71                 if (line == null)
72                         return;
73                 try {
74                         String[] tokens = line.split("\\s+");
75
76                         if (tokens[0].length() == 1) {
77                                 if (tokens.length == 2 + data_length) {
78                                         cmd = tokens[0].codePointAt(0);
79                                         tick = Integer.parseInt(tokens[1],16);
80                                         valid = true;
81                                         data8 = new int[data_length];
82                                         for (int i = 0; i < data_length; i++)
83                                                 data8[i] = Integer.parseInt(tokens[2 + i],16);
84                                 }
85                         }
86                 } catch (NumberFormatException ne) {
87                 }
88         }
89 }