altoslib: Add TeleFireTwo eeprom support
[fw/altos] / altoslib / AltosEepromChunk.java
1 /*
2  * Copyright © 2011 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  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 package org.altusmetrum.altoslib_11;
20
21 import java.text.*;
22 import java.util.concurrent.*;
23
24 public class AltosEepromChunk {
25
26         public static final int chunk_size = 256;
27         public static final int per_line = 8;
28
29         public int              data[];
30         public int              address;
31         public ParseException   parse_exception = null;
32
33         int[] ParseHex(String line) {
34                 String[] tokens = line.split("\\s+");
35                 int[] array = new int[tokens.length];
36
37                 for (int i = 0; i < tokens.length; i++)
38                         try {
39                                 array[i] = Integer.parseInt(tokens[i], 16);
40                         } catch (NumberFormatException ne) {
41                                 return null;
42                         }
43                 return array;
44         }
45
46         public int data(int offset) {
47                 return data[offset];
48         }
49
50         public int data16(int offset) {
51                 return data[offset] | (data[offset + 1] << 8);
52         }
53
54         public int data32(int offset) {
55                 return data[offset] | (data[offset + 1] << 8) |
56                         (data[offset+2] << 16) | (data[offset+3] << 24);
57         }
58
59         public boolean erased(int start, int len) {
60                 for (int i = 0; i < len; i++)
61                         if (data[start+i] != 0xff)
62                                 return false;
63                 return true;
64         }
65
66         public AltosEeprom eeprom(int offset, int log_format, AltosState state) throws ParseException {
67                 AltosEeprom     eeprom = null;
68                 switch (log_format) {
69                 case AltosLib.AO_LOG_FORMAT_FULL:
70                         eeprom = new AltosEepromTM(this, offset);
71                         break;
72                 case AltosLib.AO_LOG_FORMAT_TINY:
73                         eeprom = new AltosEepromTMini(this, offset, state);
74                         break;
75                 case AltosLib.AO_LOG_FORMAT_TELEMETRY:
76                 case AltosLib.AO_LOG_FORMAT_TELESCIENCE:
77                         break;
78                 case AltosLib.AO_LOG_FORMAT_TELEMEGA:
79                 case AltosLib.AO_LOG_FORMAT_TELEMEGA_OLD:
80                         eeprom = new AltosEepromMega(this, offset, log_format);
81                         break;
82                 case AltosLib.AO_LOG_FORMAT_TELEMETRUM:
83                         eeprom = new AltosEepromMetrum2(this, offset);
84                         break;
85                 case AltosLib.AO_LOG_FORMAT_TELEMINI2:
86                 case AltosLib.AO_LOG_FORMAT_TELEMINI3:
87                 case AltosLib.AO_LOG_FORMAT_EASYMINI:
88                         eeprom = new AltosEepromMini(this, offset);
89                         break;
90                 case AltosLib.AO_LOG_FORMAT_TELEGPS:
91                         eeprom = new AltosEepromGPS(this, offset);
92                         break;
93                 case AltosLib.AO_LOG_FORMAT_TELEFIRETWO:
94                         eeprom = new AltosEepromFireTwo(this, offset);
95                         break;
96                 default:
97                         throw new ParseException("unknown eeprom format " + log_format, 0);
98                 }
99                 return eeprom;
100         }
101
102         public AltosEepromChunk(AltosLink link, int block, boolean flush)
103                 throws TimeoutException, InterruptedException {
104
105                 int     offset;
106
107                 data = new int[chunk_size];
108                 address = block * chunk_size;
109                 if (flush)
110                         link.flush_input();
111                 link.printf("e %x\n", block);
112
113                 for (offset = 0; offset < chunk_size; offset += per_line) {
114                         try {
115                                 String  line = link.get_reply(5000);
116
117                                 if (line == null)
118                                         throw new TimeoutException();
119
120                                 int[] values = ParseHex(line);
121
122                                 if (values == null || values.length != per_line + 1)
123                                         throw new ParseException(String.format("invalid line %s", line), 0);
124                                 if (values[0] != offset)
125                                         throw new ParseException(String.format("data address out of sync at 0x%x",
126                                                                                address + offset), 0);
127                                 for (int i = 0; i < per_line; i++)
128                                         data[offset + i] = values[1 + i];
129                         } catch (ParseException pe) {
130                                 for (int i = 0; i < per_line; i++)
131                                         data[offset + i] = 0xff;
132                                 if (parse_exception == null)
133                                         parse_exception = pe;
134                         }
135                 }
136         }
137 }