first cut at turnon scripts for EasyTimer v2
[fw/altos] / altoslib / AltosRomconfig.java
index ebeb76f32b54375db7ed9b1be2469f3658f0f949..670946582386a3f5412e6881a006bab840aeca46 100644 (file)
@@ -16,7 +16,7 @@
  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
  */
 
-package org.altusmetrum.altoslib_13;
+package org.altusmetrum.altoslib_14;
 
 import java.io.*;
 import java.util.concurrent.*;
@@ -28,6 +28,7 @@ public class AltosRomconfig implements AltosUnitInfoListener {
        public int      check;
        public int      serial_number;
        public int      radio_calibration;
+       public int      address_offset;
        public AltosUsbId       usb_id;
        public String           usb_product;
 
@@ -46,9 +47,9 @@ public class AltosRomconfig implements AltosUnitInfoListener {
                return (int) (find_address(hexfile, name, len) - hexfile.address);
        }
 
-       static int get_int(AltosHexfile hexfile, String name, int len) throws AltosNoSymbol {
+       static private int get_int(AltosHexfile hexfile, String name, int len, int adjust) throws AltosNoSymbol {
                byte[] bytes = hexfile.data;
-               int start = (int) find_offset(hexfile, name, len);
+               int start = (int) find_offset(hexfile, name, len) + adjust;
 
                int     v = 0;
                int     o = 0;
@@ -142,33 +143,46 @@ public class AltosRomconfig implements AltosUnitInfoListener {
                }
        }
 
+       static final int adjust_rom[] = { 0, -4, 4 };
+
        public AltosRomconfig(AltosHexfile hexfile) {
                try {
-                       version = get_int(hexfile, ao_romconfig_version, 2);
-                       check = get_int(hexfile, ao_romconfig_check, 2);
-                       if (check == (~version & 0xffff)) {
-                               switch (version) {
-                               case 2:
-                               case 1:
-                                       serial_number = get_int(hexfile, ao_serial_number, 2);
-                                       try {
-                                               radio_calibration = get_int(hexfile, ao_radio_cal, 4);
-                                       } catch (AltosNoSymbol missing) {
-                                               radio_calibration = 0;
-                                       }
-
-                                       valid = true;
-
-                                       /* XXX TeleBT v4.0 units originally shipped without RF calibration programmed. Go fetch
-                                        * the correct value from the web site
-                                        */
-                                       if (serial_number == 2584 ||
-                                           (3686 <= serial_number && serial_number <= 3938 && radio_calibration == 5695485))
-                                       {
-                                               fetch_radio_cal();
+                       for (int adjust : adjust_rom) {
+                               try {
+                                       version = get_int(hexfile, ao_romconfig_version, 2, adjust);
+                                       check = get_int(hexfile, ao_romconfig_check, 2, adjust);
+                                       if (check == (~version & 0xffff)) {
+                                               System.out.printf("adjust %d version %x check %x success\n", adjust, version, check);
+                                               switch (version) {
+                                               case 2:
+                                               case 1:
+                                                       serial_number = get_int(hexfile, ao_serial_number, 2, adjust);
+                                                       try {
+                                                               radio_calibration = get_int(hexfile, ao_radio_cal, 4, adjust);
+                                                       } catch (AltosNoSymbol missing) {
+                                                               radio_calibration = 0;
+                                                       }
+
+                                                       valid = true;
+
+                                                       /* XXX TeleBT v4.0 units originally shipped without RF calibration programmed. Go fetch
+                                                        * the correct value from the web site
+                                                        */
+                                                       if (serial_number == 2584 ||
+                                                           (3686 <= serial_number && serial_number <= 3938 && radio_calibration == 5695485))
+                                                       {
+                                                               fetch_radio_cal();
+                                                       }
+
+                                                       break;
+                                               }
+                                               break;
+                                       } else {
+                                               System.out.printf("adjust %d version %x check %x fail\n", adjust, version, check);
                                        }
-
-                                       break;
+                               } catch (ArrayIndexOutOfBoundsException aie) {
+                                       System.out.printf("adjust %d out of bounds\n", adjust);
+                                       continue;
                                }
                        }
                        usb_id = hexfile.find_usb_id();
@@ -273,6 +287,14 @@ public class AltosRomconfig implements AltosUnitInfoListener {
                        throw new IOException("writing new rom config failed\n");
        }
 
+       public String toString() {
+               return String.format("valid %b version %d serial %d radio %d usb %04x:%04x %s",
+                                    valid, version, serial_number, radio_calibration,
+                                    usb_id == null ? 0 : usb_id.vid,
+                                    usb_id == null ? 0 : usb_id.pid,
+                                    usb_product == null ? "unknown" : usb_product);
+       }
+
        public AltosRomconfig(int in_serial_number, int in_radio_calibration) {
                valid = true;
                version = 1;
@@ -282,7 +304,7 @@ public class AltosRomconfig implements AltosUnitInfoListener {
        }
 
        public boolean valid() {
-               return valid && serial_number != 0;
+               return valid;
        }
 
        public AltosRomconfig() {