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