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