altosui: Complete split out of separate java library
[fw/altos] / altosui / 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 altosui;
19
20 import java.io.*;
21 import java.util.*;
22 import java.text.*;
23 import java.util.concurrent.*;
24 import org.altusmetrum.AltosLib.*;
25
26 public class AltosEepromChunk {
27
28         static final int        chunk_size = 256;
29         static final int        per_line = 8;
30
31         public int              data[];
32         public int              address;
33         public ParseException   parse_exception = null;
34
35         int[] ParseHex(String line) {
36                 String[] tokens = line.split("\\s+");
37                 int[] array = new int[tokens.length];
38
39                 for (int i = 0; i < tokens.length; i++)
40                         try {
41                                 array[i] = Integer.parseInt(tokens[i], 16);
42                         } catch (NumberFormatException ne) {
43                                 return null;
44                         }
45                 return array;
46         }
47
48         int data(int offset) {
49                 return data[offset];
50         }
51
52         int data16(int offset) {
53                 return data[offset] | (data[offset + 1] << 8);
54         }
55
56         int data32(int offset) {
57                 return data[offset] | (data[offset + 1] << 8) |
58                         (data[offset+2] << 16) | (data[offset+3] << 24);
59         }
60
61         boolean erased(int start, int len) {
62                 for (int i = 0; i < len; i++)
63                         if (data[start+i] != 0xff)
64                                 return false;
65                 return true;
66         }
67
68         public AltosEepromChunk(AltosSerial serial_line, int block, boolean flush)
69                 throws TimeoutException, InterruptedException {
70
71                 int     offset;
72
73                 data = new int[chunk_size];
74                 address = block * chunk_size;
75                 if (flush)
76                         serial_line.flush_input();
77                 serial_line.printf("e %x\n", block);
78
79                 for (offset = 0; offset < chunk_size; offset += per_line) {
80                         try {
81                                 String  line = serial_line.get_reply(5000);
82
83                                 if (line == null)
84                                         throw new TimeoutException();
85
86                                 int[] values = ParseHex(line);
87
88                                 if (values == null || values.length != per_line + 1)
89                                         throw new ParseException(String.format("invalid line %s", line), 0);
90                                 if (values[0] != offset)
91                                         throw new ParseException(String.format("data address out of sync at 0x%x",
92                                                                                address + offset), 0);
93                                 for (int i = 0; i < per_line; i++)
94                                         data[offset + i] = values[1 + i];
95                         } catch (ParseException pe) {
96                                 for (int i = 0; i < per_line; i++)
97                                         data[offset + i] = 0xff;
98                                 if (parse_exception == null)
99                                         parse_exception = pe;
100                         }
101                 }
102         }
103 }