081b3be1fa971d96f530b485c51b318a7db4cfcc
[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 int data8(int i) {
31                 return data8[i];
32         }
33
34         public int data16(int i) {
35                 return ((data8[i] | (data8[i+1] << 8)) << 16) >> 16;
36         }
37
38         public int data24(int i) {
39                 return data8[i] | (data8[i+1] << 8) | (data8[i+2] << 16);
40         }
41
42         public int data32(int i) {
43                 return data8[i] | (data8[i+1] << 8) | (data8[i+2] << 16) | (data8[i+3] << 24);
44         }
45
46         public final static int header_length = 4;
47
48         public abstract int record_length();
49
50         public abstract void update_state(AltosState state);
51
52         public void write(PrintStream out) {
53                 out.printf("%c %04x", cmd, tick);
54                 if (data8 != null) {
55                         for (int i = 0; i < data8.length; i++)
56                                 out.printf (" %02x", data8[i]);
57                 }
58                 out.printf ("\n");
59         }
60
61         public String string() {
62                 String  s;
63
64                 s = String.format("%c %04x", cmd, tick);
65                 if (data8 != null) {
66                         for (int i = 0; i < data8.length; i++) {
67                                 String  d = String.format(" %02x", data8[i]);
68                                 s = s.concat(d);
69                         }
70                 }
71                 s = s.concat("\n");
72                 return s;
73         }
74
75         void parse_chunk(AltosEepromChunk chunk, int start) throws ParseException {
76                 cmd = chunk.data(start);
77
78                 int data_length = record_length() - header_length;
79
80                 valid = !chunk.erased(start, record_length());
81                 if (valid) {
82                         if (AltosConvert.checksum(chunk.data, start, record_length()) != 0)
83                                 throw new ParseException(String.format("invalid checksum at 0x%x",
84                                                                        chunk.address + start), 0);
85                 } else {
86                         cmd = AltosLib.AO_LOG_INVALID;
87                 }
88
89                 tick = chunk.data16(start+2);
90
91                 data8 = new int[data_length];
92                 for (int i = 0; i < data_length; i++)
93                         data8[i] = chunk.data(start + header_length + i);
94         }
95
96         void parse_string(String line) {
97                 valid = false;
98                 tick = 0;
99                 cmd = AltosLib.AO_LOG_INVALID;
100
101                 int data_length = record_length() - header_length;
102
103                 if (line == null)
104                         return;
105                 try {
106                         String[] tokens = line.split("\\s+");
107
108                         if (tokens[0].length() == 1) {
109                                 if (tokens.length == 2 + data_length) {
110                                         cmd = tokens[0].codePointAt(0);
111                                         tick = Integer.parseInt(tokens[1],16);
112                                         valid = true;
113                                         data8 = new int[data_length];
114
115                                         for (int i = 0; i < data_length; i++)
116                                                 data8[i] = Integer.parseInt(tokens[2 + i],16);
117                                 }
118                         }
119                 } catch (NumberFormatException ne) {
120                 }
121         }
122 }