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 AltosUsbId usb_id;
32 public String usb_product;
34 static private long find_address(AltosHexfile hexfile, String name, int len) throws AltosNoSymbol {
35 AltosHexsym symbol = hexfile.lookup_symbol(name);
37 throw new AltosNoSymbol(name);
39 if (hexfile.address <= symbol.address && symbol.address + len <= hexfile.max_address) {
40 return symbol.address;
42 throw new AltosNoSymbol(name);
45 static private int find_offset(AltosHexfile hexfile, String name, int len) throws AltosNoSymbol {
46 return (int) (find_address(hexfile, name, len) - hexfile.address);
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);
56 v = v | ((((int) bytes[start]) & 0xff) << o);
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);
69 bytes[start] = (byte) (value & 0xff);
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());
80 for (int i = 0; i < value.length(); i++)
81 bytes[start + i] = (byte) value.charAt(i);
84 static final int AO_USB_DESC_STRING = 3;
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);
92 while (start < bytes.length && bytes[start] != 0) {
93 if (bytes[start + 1] == AO_USB_DESC_STRING) {
98 start += ((int) bytes[start]) & 0xff;
100 if (start >= bytes.length || bytes[start] == 0)
101 throw new AltosNoSymbol(name);
103 int len = ((((int) bytes[start]) & 0xff) - 2) / 2;
104 String fmt = String.format("%%0%dd", len);
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));
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;
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";
122 Semaphore unit_info_done;
124 public void notify_unit_info(AltosUnitInfo unit_info) {
125 unit_info_done.release();
128 private void fetch_radio_cal() {
129 unit_info_done = new Semaphore(0);
130 AltosUnitInfo info = new AltosUnitInfo(serial_number, this);
132 /* Block waiting for the rf calibration data */
133 radio_calibration_broken = true;
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;
141 } catch (InterruptedException ie) {
145 public AltosRomconfig(AltosHexfile hexfile) {
147 version = get_int(hexfile, ao_romconfig_version, 2);
148 check = get_int(hexfile, ao_romconfig_check, 2);
149 if (check == (~version & 0xffff)) {
153 serial_number = get_int(hexfile, ao_serial_number, 2);
155 radio_calibration = get_int(hexfile, ao_radio_cal, 4);
156 } catch (AltosNoSymbol missing) {
157 radio_calibration = 0;
162 /* XXX TeleBT v4.0 units originally shipped without RF calibration programmed. Go fetch
163 * the correct value from the web site
165 if (serial_number == 2584 ||
166 (3686 <= serial_number && serial_number <= 3938 && radio_calibration == 5695485))
174 usb_id = hexfile.find_usb_id();
175 usb_product = hexfile.find_usb_product();
177 } catch (AltosNoSymbol missing) {
182 private final static String[] fetch_names = {
183 ao_romconfig_version,
190 private static int fetch_len(String name) {
191 if (name.equals(ao_usb_descriptors))
196 private final static String[] required_names = {
197 ao_romconfig_version,
202 private static boolean name_required(String name) {
203 for (String required : required_names)
204 if (name.equals(required))
209 public static long fetch_base(AltosHexfile hexfile) throws AltosNoSymbol {
210 long base = 0xffffffffL;
211 for (String name : fetch_names) {
213 int len = fetch_len(name);
214 long addr = find_address(hexfile, name, len);
218 } catch (AltosNoSymbol ns) {
219 if (name_required(name))
226 public static long fetch_bounds(AltosHexfile hexfile) throws AltosNoSymbol {
228 for (String name : fetch_names) {
230 int len = fetch_len(name);
231 long addr = find_address(hexfile, name, len) + len;
234 } catch (AltosNoSymbol ns) {
235 if (name_required(name))
243 public void write (AltosHexfile hexfile) throws IOException {
245 throw new IOException("rom configuration invalid");
247 AltosRomconfig existing = new AltosRomconfig(hexfile);
249 throw new IOException("image does not contain existing rom config");
252 switch (existing.version) {
255 put_usb_serial(serial_number, hexfile, ao_usb_descriptors);
256 } catch (AltosNoSymbol missing) {
258 /* fall through ... */
260 put_int(serial_number, hexfile, ao_serial_number, 2);
262 put_int(radio_calibration, hexfile, ao_radio_cal, 4);
263 } catch (AltosNoSymbol missing) {
267 } catch (AltosNoSymbol missing) {
268 throw new IOException(missing.getMessage());
271 AltosRomconfig check = new AltosRomconfig(hexfile);
273 throw new IOException("writing new rom config failed\n");
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);
284 public AltosRomconfig(int in_serial_number, int in_radio_calibration) {
287 check = (~version & 0xffff);
288 serial_number = in_serial_number;
289 radio_calibration = in_radio_calibration;
292 public boolean valid() {
296 public AltosRomconfig() {