2 * Copyright © 2010 Keith Packard <keithp@keithp.com>
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.
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.
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.
19 package org.altusmetrum.altoslib_14;
22 import java.util.concurrent.*;
24 public class AltosRomconfig implements AltosUnitInfoListener {
26 public boolean radio_calibration_broken;
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;
35 static private long find_address(AltosHexfile hexfile, String name, int len) throws AltosNoSymbol {
36 AltosHexsym symbol = hexfile.lookup_symbol(name);
38 throw new AltosNoSymbol(name);
40 if (hexfile.address <= symbol.address && symbol.address + len <= hexfile.max_address) {
41 return symbol.address;
43 throw new AltosNoSymbol(name);
46 static private int find_offset(AltosHexfile hexfile, String name, int len) throws AltosNoSymbol {
47 return (int) (find_address(hexfile, name, len) - hexfile.address);
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;
57 v = v | ((((int) bytes[start]) & 0xff) << o);
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);
70 bytes[start] = (byte) (value & 0xff);
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());
81 for (int i = 0; i < value.length(); i++)
82 bytes[start + i] = (byte) value.charAt(i);
85 static final int AO_USB_DESC_STRING = 3;
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);
93 while (start < bytes.length && bytes[start] != 0) {
94 if (bytes[start + 1] == AO_USB_DESC_STRING) {
99 start += ((int) bytes[start]) & 0xff;
101 if (start >= bytes.length || bytes[start] == 0)
102 throw new AltosNoSymbol(name);
104 int len = ((((int) bytes[start]) & 0xff) - 2) / 2;
105 String fmt = String.format("%%0%dd", len);
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));
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;
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";
123 Semaphore unit_info_done;
125 public void notify_unit_info(AltosUnitInfo unit_info) {
126 unit_info_done.release();
129 private void fetch_radio_cal() {
130 unit_info_done = new Semaphore(0);
131 AltosUnitInfo info = new AltosUnitInfo(serial_number, this);
133 /* Block waiting for the rf calibration data */
134 radio_calibration_broken = true;
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;
142 } catch (InterruptedException ie) {
146 static final int adjust_rom[] = { 0, -4, 4 };
148 public AltosRomconfig(AltosHexfile hexfile) {
150 for (int adjust : adjust_rom) {
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);
159 serial_number = get_int(hexfile, ao_serial_number, 2, adjust);
161 radio_calibration = get_int(hexfile, ao_radio_cal, 4, adjust);
162 } catch (AltosNoSymbol missing) {
163 radio_calibration = 0;
168 /* XXX TeleBT v4.0 units originally shipped without RF calibration programmed. Go fetch
169 * the correct value from the web site
171 if (serial_number == 2584 ||
172 (3686 <= serial_number && serial_number <= 3938 && radio_calibration == 5695485))
181 System.out.printf("adjust %d version %x check %x fail\n", adjust, version, check);
183 } catch (ArrayIndexOutOfBoundsException aie) {
184 System.out.printf("adjust %d out of bounds\n", adjust);
188 usb_id = hexfile.find_usb_id();
189 usb_product = hexfile.find_usb_product();
191 } catch (AltosNoSymbol missing) {
196 private final static String[] fetch_names = {
197 ao_romconfig_version,
204 private static int fetch_len(String name) {
205 if (name.equals(ao_usb_descriptors))
210 private final static String[] required_names = {
211 ao_romconfig_version,
216 private static boolean name_required(String name) {
217 for (String required : required_names)
218 if (name.equals(required))
223 public static long fetch_base(AltosHexfile hexfile) throws AltosNoSymbol {
224 long base = 0xffffffffL;
225 for (String name : fetch_names) {
227 int len = fetch_len(name);
228 long addr = find_address(hexfile, name, len);
232 } catch (AltosNoSymbol ns) {
233 if (name_required(name))
240 public static long fetch_bounds(AltosHexfile hexfile) throws AltosNoSymbol {
242 for (String name : fetch_names) {
244 int len = fetch_len(name);
245 long addr = find_address(hexfile, name, len) + len;
248 } catch (AltosNoSymbol ns) {
249 if (name_required(name))
257 public void write (AltosHexfile hexfile) throws IOException {
259 throw new IOException("rom configuration invalid");
261 AltosRomconfig existing = new AltosRomconfig(hexfile);
263 throw new IOException("image does not contain existing rom config");
266 switch (existing.version) {
269 put_usb_serial(serial_number, hexfile, ao_usb_descriptors);
270 } catch (AltosNoSymbol missing) {
272 /* fall through ... */
274 put_int(serial_number, hexfile, ao_serial_number, 2);
276 put_int(radio_calibration, hexfile, ao_radio_cal, 4);
277 } catch (AltosNoSymbol missing) {
281 } catch (AltosNoSymbol missing) {
282 throw new IOException(missing.getMessage());
285 AltosRomconfig check = new AltosRomconfig(hexfile);
287 throw new IOException("writing new rom config failed\n");
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);
298 public AltosRomconfig(int in_serial_number, int in_radio_calibration) {
301 check = (~version & 0xffff);
302 serial_number = in_serial_number;
303 radio_calibration = in_radio_calibration;
306 public boolean valid() {
310 public AltosRomconfig() {