altos/test: Adjust CRC error rate after FEC fix
[fw/altos] / altoslib / AltosRomconfig.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; 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_14;
20
21 import java.io.*;
22 import java.util.concurrent.*;
23
24 public class AltosRomconfig implements AltosUnitInfoListener {
25         public boolean  valid;
26         public boolean  radio_calibration_broken;
27         public int      version;
28         public int      check;
29         public int      serial_number;
30         public int      radio_calibration;
31         public int      address_offset;
32         public AltosUsbId       usb_id;
33         public String           usb_product;
34
35         static private long find_address(AltosHexfile hexfile, String name, int len) throws AltosNoSymbol {
36                 AltosHexsym symbol = hexfile.lookup_symbol(name);
37                 if (symbol == null) {
38                         throw new AltosNoSymbol(name);
39                 }
40                 if (hexfile.address <= symbol.address && symbol.address + len <= hexfile.max_address) {
41                         return symbol.address;
42                 }
43                 throw new AltosNoSymbol(name);
44         }
45
46         static private int find_offset(AltosHexfile hexfile, String name, int len) throws AltosNoSymbol {
47                 return (int) (find_address(hexfile, name, len) - hexfile.address);
48         }
49
50         static private int get_int(AltosHexfile hexfile, String name, int len, int adjust) throws AltosNoSymbol {
51                 byte[] bytes = hexfile.data;
52                 int start = (int) find_offset(hexfile, name, len) + adjust;
53
54                 int     v = 0;
55                 int     o = 0;
56                 while (len > 0) {
57                         v = v | ((((int) bytes[start]) & 0xff) << o);
58                         start++;
59                         len--;
60                         o += 8;
61                 }
62                 return v;
63         }
64
65         static void put_int(int value, AltosHexfile hexfile, String name, int len) throws AltosNoSymbol, IOException {
66                 byte[] bytes = hexfile.data;
67                 int start = find_offset(hexfile, name, len);
68
69                 while (len > 0) {
70                         bytes[start] = (byte) (value & 0xff);
71                         start++;
72                         len--;
73                         value >>= 8;
74                 }
75         }
76
77         static void put_string(String value, AltosHexfile hexfile, String name) throws AltosNoSymbol {
78                 byte[] bytes = hexfile.data;
79                 int start = find_offset(hexfile, name, value.length());
80
81                 for (int i = 0; i < value.length(); i++)
82                         bytes[start + i] = (byte) value.charAt(i);
83         }
84
85         static final int AO_USB_DESC_STRING     = 3;
86
87         static void put_usb_serial(int value, AltosHexfile hexfile, String name) throws AltosNoSymbol {
88                 byte[] bytes = hexfile.data;
89                 int start = find_offset(hexfile, name, 2);
90
91                 int string_num = 0;
92
93                 while (start < bytes.length && bytes[start] != 0) {
94                         if (bytes[start + 1] == AO_USB_DESC_STRING) {
95                                 ++string_num;
96                                 if (string_num == 4)
97                                         break;
98                         }
99                         start += ((int) bytes[start]) & 0xff;
100                 }
101                 if (start >= bytes.length || bytes[start] == 0)
102                         throw new AltosNoSymbol(name);
103
104                 int len = ((((int) bytes[start]) & 0xff) - 2) / 2;
105                 String fmt = String.format("%%0%dd", len);
106
107                 String s = String.format(fmt, value);
108                 if (s.length() != len)
109                         throw new AltosNoSymbol(String.format("weird usb length issue %s isn't %d\n", s, len));
110
111                 for (int i = 0; i < len; i++) {
112                         bytes[start + 2 + i*2] = (byte) s.charAt(i);
113                         bytes[start + 2 + i*2+1] = 0;
114                 }
115         }
116
117         final static String ao_romconfig_version = "ao_romconfig_version";
118         final static String ao_romconfig_check = "ao_romconfig_check";
119         final static String ao_serial_number = "ao_serial_number";
120         final static String ao_radio_cal = "ao_radio_cal";
121         final static String ao_usb_descriptors = "ao_usb_descriptors";
122
123         Semaphore       unit_info_done;
124
125         public void notify_unit_info(AltosUnitInfo unit_info) {
126                 unit_info_done.release();
127         }
128
129         private void fetch_radio_cal() {
130                 unit_info_done = new Semaphore(0);
131                 AltosUnitInfo   info = new AltosUnitInfo(serial_number, this);
132
133                 /* Block waiting for the rf calibration data */
134                 radio_calibration_broken = true;
135                 try {
136                         unit_info_done.acquire();
137                         int new_cal = info.rfcal();
138                         if (new_cal != AltosLib.MISSING) {
139                                 radio_calibration = new_cal;
140                                 radio_calibration_broken = false;
141                         }
142                 } catch (InterruptedException ie) {
143                 }
144         }
145
146         static final int adjust_rom[] = { 0, -4, 4 };
147
148         public AltosRomconfig(AltosHexfile hexfile) {
149                 try {
150                         for (int adjust : adjust_rom) {
151                                 try {
152                                         version = get_int(hexfile, ao_romconfig_version, 2, adjust);
153                                         check = get_int(hexfile, ao_romconfig_check, 2, adjust);
154                                         if (check == (~version & 0xffff)) {
155                                                 System.out.printf("adjust %d version %x check %x success\n", adjust, version, check);
156                                                 switch (version) {
157                                                 case 2:
158                                                 case 1:
159                                                         serial_number = get_int(hexfile, ao_serial_number, 2, adjust);
160                                                         try {
161                                                                 radio_calibration = get_int(hexfile, ao_radio_cal, 4, adjust);
162                                                         } catch (AltosNoSymbol missing) {
163                                                                 radio_calibration = 0;
164                                                         }
165
166                                                         valid = true;
167
168                                                         /* XXX TeleBT v4.0 units originally shipped without RF calibration programmed. Go fetch
169                                                          * the correct value from the web site
170                                                          */
171                                                         if (serial_number == 2584 ||
172                                                             (3686 <= serial_number && serial_number <= 3938 && radio_calibration == 5695485))
173                                                         {
174                                                                 fetch_radio_cal();
175                                                         }
176
177                                                         break;
178                                                 }
179                                                 break;
180                                         } else {
181                                                 System.out.printf("adjust %d version %x check %x fail\n", adjust, version, check);
182                                         }
183                                 } catch (ArrayIndexOutOfBoundsException aie) {
184                                         System.out.printf("adjust %d out of bounds\n", adjust);
185                                         continue;
186                                 }
187                         }
188                         usb_id = hexfile.find_usb_id();
189                         usb_product = hexfile.find_usb_product();
190
191                 } catch (AltosNoSymbol missing) {
192                         valid = false;
193                 }
194         }
195
196         private final static String[] fetch_names = {
197                 ao_romconfig_version,
198                 ao_romconfig_check,
199                 ao_serial_number,
200                 ao_radio_cal,
201                 ao_usb_descriptors,
202         };
203
204         private static int fetch_len(String name) {
205                 if (name.equals(ao_usb_descriptors))
206                         return 256;
207                 return 2;
208         }
209
210         private final static String[] required_names = {
211                 ao_romconfig_version,
212                 ao_romconfig_check,
213                 ao_serial_number
214         };
215
216         private static boolean name_required(String name) {
217                 for (String required : required_names)
218                         if (name.equals(required))
219                                 return true;
220                 return false;
221         }
222
223         public static long fetch_base(AltosHexfile hexfile) throws AltosNoSymbol {
224                 long    base = 0xffffffffL;
225                 for (String name : fetch_names) {
226                         try {
227                                 int     len = fetch_len(name);
228                                 long    addr = find_address(hexfile, name, len);
229
230                                 if (addr < base)
231                                         base = addr;
232                         } catch (AltosNoSymbol ns) {
233                                 if (name_required(name))
234                                         throw (ns);
235                         }
236                 }
237                 return base;
238         }
239
240         public static long fetch_bounds(AltosHexfile hexfile) throws AltosNoSymbol {
241                 long    bounds = 0;
242                 for (String name : fetch_names) {
243                         try {
244                                 int     len = fetch_len(name);
245                                 long    addr = find_address(hexfile, name, len) + len;
246                                 if (addr > bounds)
247                                         bounds = addr;
248                         } catch (AltosNoSymbol ns) {
249                                 if (name_required(name))
250                                         throw (ns);
251                         }
252                 }
253
254                 return bounds;
255         }
256
257         public void write (AltosHexfile hexfile) throws IOException {
258                 if (!valid)
259                         throw new IOException("rom configuration invalid");
260
261                 AltosRomconfig existing = new AltosRomconfig(hexfile);
262                 if (!existing.valid)
263                         throw new IOException("image does not contain existing rom config");
264
265                 try {
266                         switch (existing.version) {
267                         case 2:
268                                 try {
269                                         put_usb_serial(serial_number, hexfile, ao_usb_descriptors);
270                                 } catch (AltosNoSymbol missing) {
271                                 }
272                                 /* fall through ... */
273                         case 1:
274                                 put_int(serial_number, hexfile, ao_serial_number, 2);
275                                 try {
276                                         put_int(radio_calibration, hexfile, ao_radio_cal, 4);
277                                 } catch (AltosNoSymbol missing) {
278                                 }
279                                 break;
280                         }
281                 } catch (AltosNoSymbol missing) {
282                         throw new IOException(missing.getMessage());
283                 }
284
285                 AltosRomconfig check = new AltosRomconfig(hexfile);
286                 if (!check.valid)
287                         throw new IOException("writing new rom config failed\n");
288         }
289
290         public String toString() {
291                 return String.format("valid %b version %d serial %d radio %d usb %04x:%04x %s",
292                                      valid, version, serial_number, radio_calibration,
293                                      usb_id == null ? 0 : usb_id.vid,
294                                      usb_id == null ? 0 : usb_id.pid,
295                                      usb_product == null ? "unknown" : usb_product);
296         }
297
298         public AltosRomconfig(int in_serial_number, int in_radio_calibration) {
299                 valid = true;
300                 version = 1;
301                 check = (~version & 0xffff);
302                 serial_number = in_serial_number;
303                 radio_calibration = in_radio_calibration;
304         }
305
306         public boolean valid() {
307                 return valid;
308         }
309
310         public AltosRomconfig() {
311                 valid = false;
312         }
313 }