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.
18 package org.altusmetrum.altoslib_11;
22 import java.nio.charset.Charset;
24 public class AltosLib {
25 /* EEProm command letters */
26 public static final int AO_LOG_FLIGHT = 'F';
27 public static final int AO_LOG_SENSOR = 'A';
28 public static final int AO_LOG_TEMP_VOLT = 'T';
29 public static final int AO_LOG_DEPLOY = 'D';
30 public static final int AO_LOG_STATE = 'S';
31 public static final int AO_LOG_GPS_POS = 'P';
32 public static final int AO_LOG_GPS_TIME = 'G';
33 public static final int AO_LOG_GPS_LAT = 'N';
34 public static final int AO_LOG_GPS_LON = 'W';
35 public static final int AO_LOG_GPS_ALT = 'H';
36 public static final int AO_LOG_GPS_SAT = 'V';
37 public static final int AO_LOG_GPS_DATE = 'Y';
38 public static final int AO_LOG_PRESSURE = 'P';
40 /* Added for header fields in eeprom files */
41 public static final int AO_LOG_CONFIG_VERSION = 1000;
42 public static final int AO_LOG_MAIN_DEPLOY = 1001;
43 public static final int AO_LOG_APOGEE_DELAY = 1002;
44 public static final int AO_LOG_RADIO_CHANNEL = 1003;
45 public static final int AO_LOG_CALLSIGN = 1004;
46 public static final int AO_LOG_ACCEL_CAL = 1005;
47 public static final int AO_LOG_RADIO_CAL = 1006;
48 public static final int AO_LOG_MAX_FLIGHT_LOG = 1007;
49 public static final int AO_LOG_MANUFACTURER = 2000;
50 public static final int AO_LOG_PRODUCT = 2001;
51 public static final int AO_LOG_SERIAL_NUMBER = 2002;
52 public static final int AO_LOG_LOG_FORMAT = 2003;
54 public static final int AO_LOG_FREQUENCY = 2004;
55 public static final int AO_LOG_APOGEE_LOCKOUT = 2005;
56 public static final int AO_LOG_RADIO_RATE = 2006;
57 public static final int AO_LOG_IGNITE_MODE = 2007;
58 public static final int AO_LOG_PAD_ORIENTATION = 2008;
59 public static final int AO_LOG_RADIO_ENABLE = 2009;
60 public static final int AO_LOG_AES_KEY = 2010;
61 public static final int AO_LOG_APRS = 2011;
62 public static final int AO_LOG_BEEP_SETTING = 2012;
63 public static final int AO_LOG_TRACKER_SETTING = 2013;
64 public static final int AO_LOG_PYRO_TIME = 2014;
65 public static final int AO_LOG_APRS_ID = 2015;
66 public static final int AO_LOG_ALTITUDE_32 = 2016;
68 /* Added for header fields in telemega files */
69 public static final int AO_LOG_BARO_RESERVED = 3000;
70 public static final int AO_LOG_BARO_SENS = 3001;
71 public static final int AO_LOG_BARO_OFF = 3002;
72 public static final int AO_LOG_BARO_TCS = 3004;
73 public static final int AO_LOG_BARO_TCO = 3005;
74 public static final int AO_LOG_BARO_TREF = 3006;
75 public static final int AO_LOG_BARO_TEMPSENS = 3007;
76 public static final int AO_LOG_BARO_CRC = 3008;
77 public static final int AO_LOG_IMU_CAL = 3009;
79 public static final int AO_LOG_SOFTWARE_VERSION = 9999;
81 public final static int MISSING = 0x7fffffff;
83 /* Added to flag invalid records */
84 public static final int AO_LOG_INVALID = -1;
86 /* Flight state numbers and names */
87 public static final int ao_flight_startup = 0;
88 public static final int ao_flight_idle = 1;
89 public static final int ao_flight_pad = 2;
90 public static final int ao_flight_boost = 3;
91 public static final int ao_flight_fast = 4;
92 public static final int ao_flight_coast = 5;
93 public static final int ao_flight_drogue = 6;
94 public static final int ao_flight_main = 7;
95 public static final int ao_flight_landed = 8;
96 public static final int ao_flight_invalid = 9;
97 public static final int ao_flight_stateless = 10;
100 public final static int vendor_altusmetrum = 0xfffe;
102 public final static int product_altusmetrum = 0x000a;
103 public final static int product_telemetrum = 0x000b;
104 public final static int product_teledongle = 0x000c;
105 public final static int product_teleterra = 0x000d;
106 public final static int product_telebt = 0x000e;
107 public final static int product_telelaunch = 0x000f;
108 public final static int product_telelco = 0x0010;
109 public final static int product_telescience = 0x0011;
110 public final static int product_telepyro =0x0012;
111 public final static int product_telemega = 0x0023;
112 public final static int product_megadongle = 0x0024;
113 public final static int product_telegps = 0x0025;
114 public final static int product_easymini = 0x0026;
115 public final static int product_telemini = 0x0027;
116 public final static int product_easymega = 0x0028;
117 public final static int product_usbtrng = 0x0029;
118 public final static int product_usbrelay = 0x002a;
119 public final static int product_mpusb = 0x002b;
120 public final static int product_altusmetrum_min = 0x000a;
121 public final static int product_altusmetrum_max = 0x002c;
123 public final static int product_any = 0x10000;
124 public final static int product_basestation = 0x10000 + 1;
125 public final static int product_altimeter = 0x10000 + 2;
127 private static class Product {
131 Product (String name, int product) {
133 this.product = product;
137 private static Product[] products = {
138 new Product("telemetrum", product_telemetrum),
139 new Product("teleballoon", product_telemetrum),
140 new Product("teledongle", product_teledongle),
141 new Product("teleterra", product_teledongle),
142 new Product("telebt", product_telebt),
143 new Product("telelaunch", product_telelaunch),
144 new Product("telelco", product_telelco),
145 new Product("telescience", product_telescience),
146 new Product("telepyro", product_telepyro),
147 new Product("telemega", product_telemega),
148 new Product("megadongle", product_megadongle),
149 new Product("telegps", product_telegps),
150 new Product("easymini", product_easymini),
151 new Product("telemini", product_telemini),
152 new Product("easymega", product_easymega)
155 public static int name_to_product(String name) {
156 String low = name.toLowerCase();
158 for (int i = 0; i < products.length; i++)
159 if (low.startsWith(products[i].name))
160 return products[i].product;
164 /* Bluetooth "identifier" (bluetooth sucks) */
165 public final static String bt_product_telebt = "TeleBT";
167 /* "good" voltages */
169 public final static double ao_battery_good = 3.8;
170 public final static double ao_igniter_good = 3.5;
172 /* Telemetry modes */
173 public static final int ao_telemetry_off = 0;
174 public static final int ao_telemetry_min = 1;
175 public static final int ao_telemetry_standard = 1;
176 public static final int ao_telemetry_0_9 = 2;
177 public static final int ao_telemetry_0_8 = 3;
178 public static final int ao_telemetry_max = 3;
180 private static final String[] ao_telemetry_name = {
181 "Off", "Standard Telemetry", "TeleMetrum v0.9", "TeleMetrum v0.8"
184 public static final int ao_telemetry_rate_38400 = 0;
185 public static final int ao_telemetry_rate_9600 = 1;
186 public static final int ao_telemetry_rate_2400 = 2;
187 public static final int ao_telemetry_rate_max = 2;
189 public static final Integer[] ao_telemetry_rate_values = {
193 public static final int ao_aprs_format_compressed = 0;
194 public static final int ao_aprs_format_uncompressed = 1;
196 public static final String[] ao_aprs_format_name = {
197 "Compressed", "Uncompressed"
200 public static final String launch_sites_url = "http://www.altusmetrum.org/AltOS/launch-sites.txt";
201 public static final String launch_sites_env = "LAUNCH_SITES";
202 // public static final String launch_sites_url = "file:///home/keithp/misc/text/altusmetrum/AltOS/launch-sites.txt";
204 public static final int ao_telemetry_standard_len = 32;
205 public static final int ao_telemetry_0_9_len = 95;
206 public static final int ao_telemetry_0_8_len = 94;
208 private static final int[] ao_telemetry_len = {
212 private static HashMap<String,Integer> string_to_state = new HashMap<String,Integer>();
214 private static boolean map_initialized = false;
216 public static void initialize_map()
218 string_to_state.put("startup", ao_flight_startup);
219 string_to_state.put("idle", ao_flight_idle);
220 string_to_state.put("pad", ao_flight_pad);
221 string_to_state.put("boost", ao_flight_boost);
222 string_to_state.put("fast", ao_flight_fast);
223 string_to_state.put("coast", ao_flight_coast);
224 string_to_state.put("drogue", ao_flight_drogue);
225 string_to_state.put("apogee", ao_flight_coast);
226 string_to_state.put("main", ao_flight_main);
227 string_to_state.put("landed", ao_flight_landed);
228 string_to_state.put("invalid", ao_flight_invalid);
229 string_to_state.put("stateless", ao_flight_stateless);
230 map_initialized = true;
233 public static int telemetry_len(int telemetry) {
234 if (telemetry <= ao_telemetry_max)
235 return ao_telemetry_len[telemetry];
236 throw new IllegalArgumentException(String.format("Invalid telemetry %d",
240 public static String telemetry_name(int telemetry) {
241 if (telemetry <= ao_telemetry_max)
242 return ao_telemetry_name[telemetry];
243 throw new IllegalArgumentException(String.format("Invalid telemetry %d",
247 private static int[] split_version(String version) {
248 String[] tokens = version.split("\\.");
249 int[] ret = new int[tokens.length];
250 for (int i = 0; i < tokens.length; i++)
251 ret[i] = Integer.parseInt(tokens[i]);
255 public static int compare_version(String version_a, String version_b) {
256 int[] a = split_version(version_a);
257 int[] b = split_version(version_b);
259 for (int i = 0; i < Math.min(a.length, b.length); i++) {
265 if (a.length < b.length)
267 if (a.length > b.length)
272 private static String[] state_to_string = {
286 private static String[] state_to_string_capital = {
300 public static int state(String state) {
301 if (!map_initialized)
303 if (string_to_state.containsKey(state))
304 return string_to_state.get(state);
305 return ao_flight_invalid;
308 public static String state_name(int state) {
309 if (state < 0 || state_to_string.length <= state)
311 return state_to_string[state];
314 public static String state_name_capital(int state) {
315 if (state < 0 || state_to_string.length <= state)
317 return state_to_string_capital[state];
320 public static final int AO_GPS_VALID = (1 << 4);
321 public static final int AO_GPS_RUNNING = (1 << 5);
322 public static final int AO_GPS_DATE_VALID = (1 << 6);
323 public static final int AO_GPS_NUM_SAT_SHIFT = 0;
324 public static final int AO_GPS_NUM_SAT_MASK = 0xf;
326 public static final int AO_LOG_FORMAT_UNKNOWN = 0;
327 public static final int AO_LOG_FORMAT_FULL = 1;
328 public static final int AO_LOG_FORMAT_TINY = 2;
329 public static final int AO_LOG_FORMAT_TELEMETRY = 3;
330 public static final int AO_LOG_FORMAT_TELESCIENCE = 4;
331 public static final int AO_LOG_FORMAT_TELEMEGA_OLD = 5;
332 public static final int AO_LOG_FORMAT_EASYMINI = 6;
333 public static final int AO_LOG_FORMAT_TELEMETRUM = 7;
334 public static final int AO_LOG_FORMAT_TELEMINI = 8;
335 public static final int AO_LOG_FORMAT_TELEGPS = 9;
336 public static final int AO_LOG_FORMAT_TELEMEGA = 10;
337 public static final int AO_LOG_FORMAT_NONE = 127;
339 public static boolean isspace(int c) {
348 public static final boolean ishex(int c) {
349 if ('0' <= c && c <= '9')
351 if ('a' <= c && c <= 'f')
353 if ('A' <= c && c <= 'F')
358 public static final boolean ishex(String s) {
359 for (int i = 0; i < s.length(); i++)
360 if (!ishex(s.charAt(i)))
365 public static int fromhex(int c) {
366 if ('0' <= c && c <= '9')
368 if ('a' <= c && c <= 'f')
370 if ('A' <= c && c <= 'F')
375 public static int fromhex(String s) throws NumberFormatException {
377 for (int i = 0; i < s.length(); i++) {
381 throw new NumberFormatException(String.format("invalid hex \"%s\"", s));
384 v = v * 16 + fromhex(c);
389 public static boolean isdec(int c) {
390 if ('0' <= c && c <= '9')
395 public static boolean isdec(String s) {
396 for (int i = 0; i < s.length(); i++)
397 if (!isdec(s.charAt(i)))
402 public static int fromdec(int c) {
403 if ('0' <= c && c <= '9')
408 public static int int8(int[] bytes, int i) {
409 return (int) (byte) bytes[i];
412 public static int uint8(int[] bytes, int i) {
416 public static int int16(int[] bytes, int i) {
417 return (int) (short) (bytes[i] + (bytes[i+1] << 8));
420 public static int uint16(int[] bytes, int i) {
421 return bytes[i] + (bytes[i+1] << 8);
424 public static int uint32(int[] bytes, int i) {
431 public static int int32(int[] bytes, int i) {
432 return (int) uint32(bytes, i);
435 public static final Charset unicode_set = Charset.forName("UTF-8");
437 public static String string(int[] bytes, int s, int l) {
438 if (s + l > bytes.length) {
439 if (s > bytes.length) {
443 l = bytes.length - s;
448 for (i = l - 1; i >= 0; i--)
453 byte[] b = new byte[l];
455 for (i = 0; i < l; i++)
456 b[i] = (byte) bytes[s+i];
457 String n = new String(b, unicode_set);
461 public static int hexbyte(String s, int i) {
464 if (s.length() < i + 2)
465 throw new NumberFormatException(String.format("invalid hex \"%s\"", s));
468 throw new NumberFormatException(String.format("invalid hex \"%c\"", c0));
471 throw new NumberFormatException(String.format("invalid hex \"%c\"", c1));
472 return fromhex(c0) * 16 + fromhex(c1);
475 public static int[] hexbytes(String s) {
480 if ((s.length() & 1) != 0)
481 throw new NumberFormatException(String.format("invalid line \"%s\"", s));
482 byte[] bytes = s.getBytes(unicode_set);
483 n = bytes.length / 2;
485 for (i = 0; i < n; i++) {
486 int h = fromhex(bytes[(i << 1)]);
487 int l = fromhex(bytes[(i << 1) + 1]);
489 throw new NumberFormatException(String.format("invalid hex \"%c%c\"",
490 bytes[(i<<1)], bytes[(i<<1) + 1]));
496 public static long fromdec(String s) throws NumberFormatException {
500 for (int i = 0; i < s.length(); i++) {
502 if (i == 0 && c == '-') {
504 } else if (!isdec(c)) {
506 throw new NumberFormatException(String.format("invalid number \"%s\"", s));
509 v = v * 10 + fromdec(c);
514 public static String gets(FileInputStream s) throws IOException {
518 while ((c = s.read()) != -1) {
524 line = line + (char) c;
529 public static String replace_extension(String input, String extension) {
530 int dot = input.lastIndexOf(".");
532 input = input.substring(0,dot);
533 return input.concat(extension);
536 public static File replace_extension(File input, String extension) {
537 return new File(replace_extension(input.getPath(), extension));
540 public static String product_name(int product_id) {
541 switch (product_id) {
542 case product_altusmetrum: return "AltusMetrum";
543 case product_telemetrum: return "TeleMetrum";
544 case product_teledongle: return "TeleDongle";
545 case product_teleterra: return "TeleTerra";
546 case product_telebt: return "TeleBT";
547 case product_telelaunch: return "TeleLaunch";
548 case product_telelco: return "TeleLco";
549 case product_telescience: return "Telescience";
550 case product_telepyro: return "TelePyro";
551 case product_telemega: return "TeleMega";
552 case product_megadongle: return "MegaDongle";
553 case product_telegps: return "TeleGPS";
554 case product_easymini: return "EasyMini";
555 case product_telemini: return "TeleMini";
556 default: return "unknown";
560 public static String ignitor_name(int i) {
561 return String.format("Ignitor %c", 'A' + i);