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; version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
21 public class AltosRomconfig {
25 public int serial_number;
26 public int radio_calibration;
28 static int get_int(byte[] bytes, int start, int len) {
32 v = v | ((((int) bytes[start]) & 0xff) << o);
40 static void put_int(int value, byte[] bytes, int start, int len) {
42 bytes[start] = (byte) (value & 0xff);
49 static void put_string(String value, byte[] bytes, int start) {
50 for (int i = 0; i < value.length(); i++)
51 bytes[start + i] = (byte) value.charAt(i);
54 static final int AO_USB_DESC_STRING = 3;
56 static void put_usb_serial(int value, byte[] bytes, int start) {
57 int offset = start + 0xa;
60 while (offset < bytes.length && bytes[offset] != 0) {
61 if (bytes[offset + 1] == AO_USB_DESC_STRING) {
66 offset += ((int) bytes[offset]) & 0xff;
68 if (offset >= bytes.length || bytes[offset] == 0)
70 int len = ((((int) bytes[offset]) & 0xff) - 2) / 2;
71 String fmt = String.format("%%0%dd", len);
73 String s = String.format(fmt, value);
74 if (s.length() != len) {
75 System.out.printf("weird usb length issue %s isn't %d\n",
79 for (int i = 0; i < len; i++) {
80 bytes[offset + 2 + i*2] = (byte) s.charAt(i);
81 bytes[offset + 2 + i*2+1] = 0;
85 public AltosRomconfig(byte[] bytes, int offset) {
86 version = get_int(bytes, offset + 0, 2);
87 check = get_int(bytes, offset + 2, 2);
88 if (check == (~version & 0xffff)) {
92 serial_number = get_int(bytes, offset + 4, 2);
93 radio_calibration = get_int(bytes, offset + 6, 4);
100 public AltosRomconfig(AltosHexfile hexfile) {
101 this(hexfile.data, 0xa0 - hexfile.address);
104 public void write(byte[] bytes, int offset) throws IOException {
106 throw new IOException("rom configuration invalid");
108 if (offset < 0 || bytes.length < offset + 10)
109 throw new IOException("image cannot contain rom config");
111 AltosRomconfig existing = new AltosRomconfig(bytes, offset);
113 throw new IOException("image does not contain existing rom config");
115 switch (existing.version) {
117 put_usb_serial(serial_number, bytes, offset);
119 put_int(serial_number, bytes, offset + 4, 2);
120 put_int(radio_calibration, bytes, offset + 6, 4);
125 public void write (AltosHexfile hexfile) throws IOException {
126 write(hexfile.data, 0xa0 - hexfile.address);
127 AltosRomconfig check = new AltosRomconfig(hexfile);
129 throw new IOException("writing new rom config failed\n");
132 public AltosRomconfig(int in_serial_number, int in_radio_calibration) {
135 check = (~version & 0xffff);
136 serial_number = in_serial_number;
137 radio_calibration = in_radio_calibration;
140 public boolean valid() {
141 return valid && serial_number != 0;
144 public AltosRomconfig() {