Merge remote-tracking branch 'origin/master'
[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; 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_2;
19
20 import java.text.*;
21 import java.util.concurrent.*;
22
23 public class AltosEepromChunk {
24
25         public static final int chunk_size = 256;
26         public static final int per_line = 8;
27
28         public int              data[];
29         public int              address;
30         public ParseException   parse_exception = null;
31
32         int[] ParseHex(String line) {
33                 String[] tokens = line.split("\\s+");
34                 int[] array = new int[tokens.length];
35
36                 for (int i = 0; i < tokens.length; i++)
37                         try {
38                                 array[i] = Integer.parseInt(tokens[i], 16);
39                         } catch (NumberFormatException ne) {
40                                 return null;
41                         }
42                 return array;
43         }
44
45         public int data(int offset) {
46                 return data[offset];
47         }
48
49         public int data16(int offset) {
50                 return data[offset] | (data[offset + 1] << 8);
51         }
52
53         public int data32(int offset) {
54                 return data[offset] | (data[offset + 1] << 8) |
55                         (data[offset+2] << 16) | (data[offset+3] << 24);
56         }
57
58         public boolean erased(int start, int len) {
59                 for (int i = 0; i < len; i++)
60                         if (data[start+i] != 0xff)
61                                 return false;
62                 return true;
63         }
64
65         public AltosEeprom eeprom(int offset, int log_format, AltosState state) {
66                 AltosEeprom     eeprom = null;
67                 try {
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 AltosEepromTm(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                                 eeprom = new AltosEepromMega(this, offset);
80                                 break;
81                         case AltosLib.AO_LOG_FORMAT_TELEMETRUM:
82                                 eeprom = new AltosEepromMetrum2(this, offset);
83                                 break;
84                         case AltosLib.AO_LOG_FORMAT_TELEMINI:
85                         case AltosLib.AO_LOG_FORMAT_EASYMINI:
86                                 eeprom = new AltosEepromMini(this, offset);
87                                 break;
88                         }
89                 } catch (ParseException e) {
90                 }
91                 return eeprom;
92         }
93
94         public AltosEepromChunk(AltosLink link, int block, boolean flush)
95                 throws TimeoutException, InterruptedException {
96
97                 int     offset;
98
99                 data = new int[chunk_size];
100                 address = block * chunk_size;
101                 if (flush)
102                         link.flush_input();
103                 link.printf("e %x\n", block);
104
105                 for (offset = 0; offset < chunk_size; offset += per_line) {
106                         try {
107                                 String  line = link.get_reply(5000);
108
109                                 if (line == null)
110                                         throw new TimeoutException();
111
112                                 int[] values = ParseHex(line);
113
114                                 if (values == null || values.length != per_line + 1)
115                                         throw new ParseException(String.format("invalid line %s", line), 0);
116                                 if (values[0] != offset)
117                                         throw new ParseException(String.format("data address out of sync at 0x%x",
118                                                                                address + offset), 0);
119                                 for (int i = 0; i < per_line; i++)
120                                         data[offset + i] = values[1 + i];
121                         } catch (ParseException pe) {
122                                 for (int i = 0; i < per_line; i++)
123                                         data[offset + i] = 0xff;
124                                 if (parse_exception == null)
125                                         parse_exception = pe;
126                         }
127                 }
128         }
129 }