e5196c50039a69434d36bd661f612ad50bb3ae08
[fw/altos] / altosui / AltosEepromRecord.java
1 /*
2  * Copyright © 2010 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 altosui;
19
20 import java.awt.*;
21 import java.awt.event.*;
22 import javax.swing.*;
23 import javax.swing.filechooser.FileNameExtensionFilter;
24 import javax.swing.table.*;
25 import java.io.*;
26 import java.util.*;
27 import java.text.*;
28 import java.util.prefs.*;
29 import java.util.concurrent.*;
30
31 import libaltosJNI.*;
32
33 public class AltosEepromRecord {
34         public int      cmd;
35         public int      tick;
36         public int      a;
37         public int      b;
38         public String   data;
39         public boolean  tick_valid;
40
41         int[] ParseHex(String line) {
42                 String[] tokens = line.split("\\s+");
43                 int[] array = new int[tokens.length];
44
45                 for (int i = 0; i < tokens.length; i++)
46                         try {
47                                 array[i] = Integer.parseInt(tokens[i], 16);
48                         } catch (NumberFormatException ne) {
49                                 return null;
50                         }
51                 return array;
52         }
53
54         int checksum(int[] line) {
55                 int     csum = 0x5a;
56                 for (int i = 1; i < line.length; i++)
57                         csum += line[i];
58                 return csum & 0xff;
59         }
60
61         public AltosEepromRecord (AltosSerial serial_line, int addr)
62                 throws TimeoutException, ParseException, InterruptedException {
63                 String  line = serial_line.get_reply(5000);
64                 if (line == null)
65                         throw new TimeoutException();
66                 int[] values = ParseHex(line);
67
68                 if (values == null || values.length < 9) {
69                         System.out.printf("invalid line %s", line);
70                         throw new ParseException(String.format("inalid line %s", line), 0);
71                 }
72                 if (values[0] != (addr & 0xff))
73                         throw new ParseException(String.format("data address out of sync at 0x%x",
74                                                                addr), 0);
75                 int i;
76                 for (i = 1; i < values.length; i++)
77                         if (values[i] != 0xff)
78                                 break;
79                 cmd = values[1];
80                 tick_valid = true;
81                 if (i != values.length) {
82                         if (checksum(values) != 0)
83                                 throw new ParseException(String.format("invalid checksum at 0x%x in line %s", addr, line), 0);
84                 } else {
85                         cmd = Altos.AO_LOG_INVALID;
86                         tick_valid = false;
87                 }
88
89                 tick = values[3] + (values[4] << 8);
90                 a = values[5] + (values[6] << 8);
91                 b = values[7] + (values[8] << 8);
92                 data = null;
93         }
94
95         public AltosEepromRecord (String line) {
96                 tick_valid = false;
97                 tick = 0;
98                 a = 0;
99                 b = 0;
100                 data = null;
101                 if (line == null) {
102                         cmd = Altos.AO_LOG_INVALID;
103                         data = "";
104                 } else {
105                         try {
106                                 String[] tokens = line.split("\\s+");
107
108                                 if (tokens[0].length() == 1) {
109                                         if (tokens.length != 4) {
110                                                 cmd = Altos.AO_LOG_INVALID;
111                                                 data = line;
112                                         } else {
113                                                 cmd = tokens[0].codePointAt(0);
114                                                 tick = Integer.parseInt(tokens[1],16);
115                                                 tick_valid = true;
116                                                 a = Integer.parseInt(tokens[2],16);
117                                                 b = Integer.parseInt(tokens[3],16);
118                                         }
119                                 } else if (tokens[0].equals("Config") && tokens[1].equals("version:")) {
120                                         cmd = Altos.AO_LOG_CONFIG_VERSION;
121                                         data = tokens[2];
122                                 } else if (tokens[0].equals("Main") && tokens[1].equals("deploy:")) {
123                                         cmd = Altos.AO_LOG_MAIN_DEPLOY;
124                                         a = Integer.parseInt(tokens[2]);
125                                 } else if (tokens[0].equals("Apogee") && tokens[1].equals("delay:")) {
126                                         cmd = Altos.AO_LOG_APOGEE_DELAY;
127                                         a = Integer.parseInt(tokens[2]);
128                                 } else if (tokens[0].equals("Radio") && tokens[1].equals("channel:")) {
129                                         cmd = Altos.AO_LOG_RADIO_CHANNEL;
130                                         a = Integer.parseInt(tokens[2]);
131                                 } else if (tokens[0].equals("Callsign:")) {
132                                         cmd = Altos.AO_LOG_CALLSIGN;
133                                         data = tokens[1].replaceAll("\"","");
134                                 } else if (tokens[0].equals("Accel") && tokens[1].equals("cal")) {
135                                         cmd = Altos.AO_LOG_ACCEL_CAL;
136                                         a = Integer.parseInt(tokens[3]);
137                                         b = Integer.parseInt(tokens[5]);
138                                 } else if (tokens[0].equals("Radio") && tokens[1].equals("cal:")) {
139                                         cmd = Altos.AO_LOG_RADIO_CAL;
140                                         a = Integer.parseInt(tokens[2]);
141                                 } else if (tokens[0].equals("manufacturer")) {
142                                         cmd = Altos.AO_LOG_MANUFACTURER;
143                                         data = tokens[1];
144                                 } else if (tokens[0].equals("product")) {
145                                         cmd = Altos.AO_LOG_PRODUCT;
146                                         data = tokens[1];
147                                 } else if (tokens[0].equals("serial-number")) {
148                                         cmd = Altos.AO_LOG_SERIAL_NUMBER;
149                                         a = Integer.parseInt(tokens[1]);
150                                 } else if (tokens[0].equals("software-version")) {
151                                         cmd = Altos.AO_LOG_SOFTWARE_VERSION;
152                                         data = tokens[1];
153                                 } else {
154                                         cmd = Altos.AO_LOG_INVALID;
155                                         data = line;
156                                 }
157                         } catch (NumberFormatException ne) {
158                                 cmd = Altos.AO_LOG_INVALID;
159                                 data = line;
160                         }
161                 }
162         }
163
164         public AltosEepromRecord(int in_cmd, int in_tick, int in_a, int in_b) {
165                 tick_valid = true;
166                 cmd = in_cmd;
167                 tick = in_tick;
168                 a = in_a;
169                 b = in_b;
170         }
171 }