altoslib: Make sure AltosFlightSeries is filled in before use
[fw/altos] / altoslib / AltosLib.java
1 /*
2  * Copyright © 2010 Keith Packard <keithp@keithp.com>
3  *
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.
8  *
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.
13  *
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.
17  */
18
19 package org.altusmetrum.altoslib_11;
20
21 import java.util.*;
22 import java.io.*;
23 import java.nio.charset.Charset;
24
25 public class AltosLib {
26         /* EEProm command letters */
27         public static final int AO_LOG_FLIGHT = 'F';
28         public static final int AO_LOG_SENSOR = 'A';
29         public static final int AO_LOG_TEMP_VOLT = 'T';
30         public static final int AO_LOG_DEPLOY = 'D';
31         public static final int AO_LOG_STATE = 'S';
32         public static final int AO_LOG_GPS_POS = 'P';
33         public static final int AO_LOG_GPS_TIME = 'G';
34         public static final int AO_LOG_GPS_LAT = 'N';
35         public static final int AO_LOG_GPS_LON = 'W';
36         public static final int AO_LOG_GPS_ALT = 'H';
37         public static final int AO_LOG_GPS_SAT = 'V';
38         public static final int AO_LOG_GPS_DATE = 'Y';
39         public static final int AO_LOG_PRESSURE = 'P';
40
41         /* Added for header fields in eeprom files */
42         public static final int AO_LOG_CONFIG_VERSION = 1000;
43         public static final int AO_LOG_MAIN_DEPLOY = 1001;
44         public static final int AO_LOG_APOGEE_DELAY = 1002;
45         public static final int AO_LOG_RADIO_CHANNEL = 1003;
46         public static final int AO_LOG_CALLSIGN = 1004;
47         public static final int AO_LOG_ACCEL_CAL = 1005;
48         public static final int AO_LOG_RADIO_CAL = 1006;
49         public static final int AO_LOG_MAX_FLIGHT_LOG = 1007;
50         public static final int AO_LOG_MANUFACTURER = 2000;
51         public static final int AO_LOG_PRODUCT = 2001;
52         public static final int AO_LOG_SERIAL_NUMBER = 2002;
53         public static final int AO_LOG_LOG_FORMAT = 2003;
54
55         public static final int AO_LOG_FREQUENCY = 2004;
56         public static final int AO_LOG_APOGEE_LOCKOUT = 2005;
57         public static final int AO_LOG_RADIO_RATE = 2006;
58         public static final int AO_LOG_IGNITE_MODE = 2007;
59         public static final int AO_LOG_PAD_ORIENTATION = 2008;
60         public static final int AO_LOG_RADIO_ENABLE = 2009;
61         public static final int AO_LOG_AES_KEY = 2010;
62         public static final int AO_LOG_APRS = 2011;
63         public static final int AO_LOG_BEEP_SETTING = 2012;
64         public static final int AO_LOG_TRACKER_SETTING = 2013;
65         public static final int AO_LOG_PYRO_TIME = 2014;
66         public static final int AO_LOG_APRS_ID = 2015;
67         public static final int AO_LOG_ALTITUDE_32 = 2016;
68
69         /* Added for header fields in telemega files */
70         public static final int AO_LOG_BARO_RESERVED = 3000;
71         public static final int AO_LOG_BARO_SENS = 3001;
72         public static final int AO_LOG_BARO_OFF = 3002;
73         public static final int AO_LOG_BARO_TCS = 3004;
74         public static final int AO_LOG_BARO_TCO = 3005;
75         public static final int AO_LOG_BARO_TREF = 3006;
76         public static final int AO_LOG_BARO_TEMPSENS = 3007;
77         public static final int AO_LOG_BARO_CRC = 3008;
78         public static final int AO_LOG_IMU_CAL = 3009;
79
80         public static final int AO_LOG_SOFTWARE_VERSION = 9999;
81
82         public final static int MISSING = 0x7fffffff;
83
84         /* Added to flag invalid records */
85         public static final int AO_LOG_INVALID = -1;
86
87         /* Flight state numbers and names */
88         public static final int ao_flight_startup = 0;
89         public static final int ao_flight_idle = 1;
90         public static final int ao_flight_pad = 2;
91         public static final int ao_flight_boost = 3;
92         public static final int ao_flight_fast = 4;
93         public static final int ao_flight_coast = 5;
94         public static final int ao_flight_drogue = 6;
95         public static final int ao_flight_main = 7;
96         public static final int ao_flight_landed = 8;
97         public static final int ao_flight_invalid = 9;
98         public static final int ao_flight_stateless = 10;
99
100         /* USB product IDs */
101         public final static int vendor_altusmetrum = 0xfffe;
102
103         public final static int product_altusmetrum = 0x000a;
104         public final static int product_telemetrum = 0x000b;
105         public final static int product_teledongle = 0x000c;
106         public final static int product_teleterra = 0x000d;
107         public final static int product_telebt = 0x000e;
108         public final static int product_telelaunch = 0x000f;
109         public final static int product_telelco = 0x0010;
110         public final static int product_telescience = 0x0011;
111         public final static int product_telepyro =0x0012;
112         public final static int product_telemega = 0x0023;
113         public final static int product_megadongle = 0x0024;
114         public final static int product_telegps = 0x0025;
115         public final static int product_easymini = 0x0026;
116         public final static int product_telemini = 0x0027;
117         public final static int product_easymega = 0x0028;
118         public final static int product_usbtrng = 0x0029;
119         public final static int product_usbrelay = 0x002a;
120         public final static int product_mpusb = 0x002b;
121         public final static int product_altusmetrum_min = 0x000a;
122         public final static int product_altusmetrum_max = 0x002c;
123
124         public final static int product_any = 0x10000;
125         public final static int product_basestation = 0x10000 + 1;
126         public final static int product_altimeter = 0x10000 + 2;
127
128         private static class Product {
129                 final String    name;
130                 final int       product;
131
132                 Product (String name, int product) {
133                         this.name = name;
134                         this.product = product;
135                 }
136         }
137
138         private static Product[] products = {
139                 new Product("telemetrum", product_telemetrum),
140                 new Product("teleballoon", product_telemetrum),
141                 new Product("teledongle", product_teledongle),
142                 new Product("teleterra", product_teledongle),
143                 new Product("telebt", product_telebt),
144                 new Product("telelaunch", product_telelaunch),
145                 new Product("telelco", product_telelco),
146                 new Product("telescience", product_telescience),
147                 new Product("telepyro", product_telepyro),
148                 new Product("telemega", product_telemega),
149                 new Product("megadongle", product_megadongle),
150                 new Product("telegps", product_telegps),
151                 new Product("easymini", product_easymini),
152                 new Product("telemini", product_telemini),
153                 new Product("easymega", product_easymega)
154         };
155
156         public static int name_to_product(String name) {
157                 String low = name.toLowerCase();
158
159                 for (int i = 0; i < products.length; i++)
160                         if (low.startsWith(products[i].name))
161                                 return products[i].product;
162                 return product_any;
163         }
164
165         public static boolean has_9dof(int device_type) {
166                 return device_type == product_telemega || device_type == product_easymega;
167         }
168
169         public static boolean has_gps(int device_type) {
170                 return device_type == product_telemetrum ||
171                         device_type == product_telemega ||
172                         device_type == product_telegps;
173         }
174
175         /* Bluetooth "identifier" (bluetooth sucks) */
176         public final static String bt_product_telebt = "TeleBT";
177
178         /* "good" voltages */
179
180         public final static double ao_battery_good = 3.8;
181         public final static double ao_igniter_good = 3.5;
182
183         /* Telemetry modes */
184         public static final int ao_telemetry_off = 0;
185         public static final int ao_telemetry_min = 1;
186         public static final int ao_telemetry_standard = 1;
187         public static final int ao_telemetry_0_9 = 2;
188         public static final int ao_telemetry_0_8 = 3;
189         public static final int ao_telemetry_max = 3;
190
191         private static final String[] ao_telemetry_name = {
192                 "Off", "Standard Telemetry", "TeleMetrum v0.9", "TeleMetrum v0.8"
193         };
194
195         public static final int ao_telemetry_rate_38400 = 0;
196         public static final int ao_telemetry_rate_9600 = 1;
197         public static final int ao_telemetry_rate_2400 = 2;
198         public static final int ao_telemetry_rate_max = 2;
199
200         public static final Integer[] ao_telemetry_rate_values = {
201                 38400, 9600, 2400
202         };
203
204         public static final int ao_aprs_format_compressed = 0;
205         public static final int ao_aprs_format_uncompressed = 1;
206
207         public static final String[] ao_aprs_format_name = {
208                 "Compressed", "Uncompressed"
209         };
210
211         public static final String launch_sites_url = "http://www.altusmetrum.org/AltOS/launch-sites.txt";
212         public static final String launch_sites_env = "LAUNCH_SITES";
213 //      public static final String launch_sites_url = "file:///home/keithp/misc/text/altusmetrum/AltOS/launch-sites.txt";
214
215         public static final int ao_telemetry_standard_len = 32;
216         public static final int ao_telemetry_0_9_len = 95;
217         public static final int ao_telemetry_0_8_len = 94;
218
219         private static final int[] ao_telemetry_len = {
220                 0, 32, 95, 94
221         };
222
223         private static HashMap<String,Integer>  string_to_state = new HashMap<String,Integer>();
224
225         private static boolean map_initialized = false;
226
227         public static void initialize_map()
228         {
229                 string_to_state.put("startup", ao_flight_startup);
230                 string_to_state.put("idle", ao_flight_idle);
231                 string_to_state.put("pad", ao_flight_pad);
232                 string_to_state.put("boost", ao_flight_boost);
233                 string_to_state.put("fast", ao_flight_fast);
234                 string_to_state.put("coast", ao_flight_coast);
235                 string_to_state.put("drogue", ao_flight_drogue);
236                 string_to_state.put("apogee", ao_flight_coast);
237                 string_to_state.put("main", ao_flight_main);
238                 string_to_state.put("landed", ao_flight_landed);
239                 string_to_state.put("invalid", ao_flight_invalid);
240                 string_to_state.put("stateless", ao_flight_stateless);
241                 map_initialized = true;
242         }
243
244         public static int telemetry_len(int telemetry) {
245                 if (telemetry <= ao_telemetry_max)
246                         return ao_telemetry_len[telemetry];
247                 throw new IllegalArgumentException(String.format("Invalid telemetry %d",
248                                                                  telemetry));
249         }
250
251         public static String telemetry_name(int telemetry) {
252                 if (telemetry <= ao_telemetry_max)
253                         return ao_telemetry_name[telemetry];
254                 throw new IllegalArgumentException(String.format("Invalid telemetry %d",
255                                                                  telemetry));
256         }
257
258         private static int[] split_version(String version) {
259                 String[] tokens = version.split("\\.");
260                 int[] ret = new int[tokens.length];
261                 for (int i = 0; i < tokens.length; i++)
262                         ret[i] = Integer.parseInt(tokens[i]);
263                 return ret;
264         }
265
266         public static int compare_version(String version_a, String version_b) {
267                 int[] a = split_version(version_a);
268                 int[] b = split_version(version_b);
269
270                 for (int i = 0; i < Math.min(a.length, b.length); i++) {
271                         if (a[i] < b[i])
272                                 return -1;
273                         if (a[i] > b[i])
274                                 return 1;
275                 }
276                 if (a.length < b.length)
277                         return -1;
278                 if (a.length > b.length)
279                         return 1;
280                 return 0;
281         }
282
283         private static String[] state_to_string = {
284                 "startup",
285                 "idle",
286                 "pad",
287                 "boost",
288                 "fast",
289                 "coast",
290                 "drogue",
291                 "main",
292                 "landed",
293                 "invalid",
294                 "stateless",
295         };
296
297         private static String[] state_to_string_capital = {
298                 "Startup",
299                 "Idle",
300                 "Pad",
301                 "Boost",
302                 "Fast",
303                 "Coast",
304                 "Drogue",
305                 "Main",
306                 "Landed",
307                 "Invalid",
308                 "Stateless",
309         };
310
311         public static int state(String state) {
312                 if (!map_initialized)
313                         initialize_map();
314                 if (string_to_state.containsKey(state))
315                         return string_to_state.get(state);
316                 return ao_flight_invalid;
317         }
318
319         public static String state_name(int state) {
320                 if (state < 0 || state_to_string.length <= state)
321                         return "invalid";
322                 return state_to_string[state];
323         }
324
325         public static String state_name_capital(int state) {
326                 if (state < 0 || state_to_string.length <= state)
327                         return "Invalid";
328                 return state_to_string_capital[state];
329         }
330
331         public static final int AO_GPS_VALID = (1 << 4);
332         public static final int AO_GPS_RUNNING = (1 << 5);
333         public static final int AO_GPS_DATE_VALID = (1 << 6);
334         public static final int AO_GPS_NUM_SAT_SHIFT = 0;
335         public static final int AO_GPS_NUM_SAT_MASK = 0xf;
336
337         public static final int AO_LOG_FORMAT_UNKNOWN = 0;
338         public static final int AO_LOG_FORMAT_FULL = 1;
339         public static final int AO_LOG_FORMAT_TINY = 2;
340         public static final int AO_LOG_FORMAT_TELEMETRY = 3;
341         public static final int AO_LOG_FORMAT_TELESCIENCE = 4;
342         public static final int AO_LOG_FORMAT_TELEMEGA_OLD = 5;
343         public static final int AO_LOG_FORMAT_EASYMINI = 6;
344         public static final int AO_LOG_FORMAT_TELEMETRUM = 7;
345         public static final int AO_LOG_FORMAT_TELEMINI2 = 8;
346         public static final int AO_LOG_FORMAT_TELEGPS = 9;
347         public static final int AO_LOG_FORMAT_TELEMEGA = 10;
348         public static final int AO_LOG_FORMAT_DETHERM = 11;
349         public static final int AO_LOG_FORMAT_TELEMINI3 = 12;
350         public static final int AO_LOG_FORMAT_TELEFIRETWO = 13;
351         public static final int AO_LOG_FORMAT_NONE = 127;
352
353         public static boolean isspace(int c) {
354                 switch (c) {
355                 case ' ':
356                 case '\t':
357                         return true;
358                 }
359                 return false;
360         }
361
362         public static final boolean ishex(int c) {
363                 if ('0' <= c && c <= '9')
364                         return true;
365                 if ('a' <= c && c <= 'f')
366                         return true;
367                 if ('A' <= c && c <= 'F')
368                         return true;
369                 return false;
370         }
371
372         public static final boolean ishex(String s) {
373                 for (int i = 0; i < s.length(); i++)
374                         if (!ishex(s.charAt(i)))
375                                 return false;
376                 return true;
377         }
378
379         public static int fromhex(int c) {
380                 if ('0' <= c && c <= '9')
381                         return c - '0';
382                 if ('a' <= c && c <= 'f')
383                         return c - 'a' + 10;
384                 if ('A' <= c && c <= 'F')
385                         return c - 'A' + 10;
386                 return -1;
387         }
388
389         public static int fromhex(String s) throws NumberFormatException {
390                 int c, v = 0;
391                 for (int i = 0; i < s.length(); i++) {
392                         c = s.charAt(i);
393                         if (!ishex(c)) {
394                                 if (i == 0)
395                                         throw new NumberFormatException(String.format("invalid hex \"%s\"", s));
396                                 return v;
397                         }
398                         v = v * 16 + fromhex(c);
399                 }
400                 return v;
401         }
402
403         public static boolean isdec(int c) {
404                 if ('0' <= c && c <= '9')
405                         return true;
406                 return false;
407         }
408
409         public static boolean isdec(String s) {
410                 for (int i = 0; i < s.length(); i++)
411                         if (!isdec(s.charAt(i)))
412                                 return false;
413                 return true;
414         }
415
416         public static int fromdec(int c) {
417                 if ('0' <= c && c <= '9')
418                         return c - '0';
419                 return -1;
420         }
421
422         public static int int8(int[] bytes, int i) {
423                 return (int) (byte) bytes[i];
424         }
425
426         public static int uint8(int[] bytes, int i) {
427                 return bytes[i];
428         }
429
430         public static int int16(int[] bytes, int i) {
431                 return (int) (short) (bytes[i] + (bytes[i+1] << 8));
432         }
433
434         public static int uint16(int[] bytes, int i) {
435                 return bytes[i] + (bytes[i+1] << 8);
436         }
437
438         public static int uint32(int[] bytes, int i) {
439                 return bytes[i] +
440                         (bytes[i+1] << 8) +
441                         (bytes[i+2] << 16) +
442                         (bytes[i+3] << 24);
443         }
444
445         public static int int32(int[] bytes, int i) {
446                 return (int) uint32(bytes, i);
447         }
448
449         public static final Charset     unicode_set = Charset.forName("UTF-8");
450
451         public static String string(int[] bytes, int s, int l) {
452                 if (s + l > bytes.length) {
453                         if (s > bytes.length) {
454                                 s = bytes.length;
455                                 l = 0;
456                         } else {
457                                 l = bytes.length - s;
458                         }
459                 }
460
461                 int i;
462                 for (i = l - 1; i >= 0; i--)
463                         if (bytes[s+i] != 0)
464                                 break;
465
466                 l = i + 1;
467                 byte[]  b = new byte[l];
468
469                 for (i = 0; i < l; i++)
470                         b[i] = (byte) bytes[s+i];
471                 String n = new String(b, unicode_set);
472                 return n;
473         }
474
475         public static int hexbyte(String s, int i) {
476                 int c0, c1;
477
478                 if (s.length() < i + 2)
479                         throw new NumberFormatException(String.format("invalid hex \"%s\"", s));
480                 c0 = s.charAt(i);
481                 if (!ishex(c0))
482                         throw new NumberFormatException(String.format("invalid hex \"%c\"", c0));
483                 c1 = s.charAt(i+1);
484                 if (!ishex(c1))
485                         throw new NumberFormatException(String.format("invalid hex \"%c\"", c1));
486                 return fromhex(c0) * 16 + fromhex(c1);
487         }
488
489         public static int[] hexbytes(String s) {
490                 int     n;
491                 int[]   r;
492                 int     i;
493
494                 if ((s.length() & 1) != 0)
495                         throw new NumberFormatException(String.format("invalid line \"%s\"", s));
496                 byte[] bytes = s.getBytes(unicode_set);
497                 n = bytes.length / 2;
498                 r = new int[n];
499                 for (i = 0; i < n; i++) {
500                         int h = fromhex(bytes[(i << 1)]);
501                         int l = fromhex(bytes[(i << 1) + 1]);
502                         if (h < 0 || l < 0)
503                                 throw new NumberFormatException(String.format("invalid hex \"%c%c\"",
504                                                                               bytes[(i<<1)], bytes[(i<<1) + 1]));
505                         r[i] = (h << 4) + l;
506                 }
507                 return r;
508         }
509
510         public static long fromdec(String s) throws NumberFormatException {
511                 int c;
512                 long v = 0;
513                 long sign = 1;
514                 for (int i = 0; i < s.length(); i++) {
515                         c = s.charAt(i);
516                         if (i == 0 && c == '-') {
517                                 sign = -1;
518                         } else if (!isdec(c)) {
519                                 if (i == 0)
520                                         throw new NumberFormatException(String.format("invalid number \"%s\"", s));
521                                 return v;
522                         } else
523                                 v = v * 10 + fromdec(c);
524                 }
525                 return v * sign;
526         }
527
528         public static String gets(FileInputStream s) throws IOException {
529                 int c;
530                 String  line = "";
531
532                 while ((c = s.read()) != -1) {
533                         if (c == '\r')
534                                 continue;
535                         if (c == '\n') {
536                                 return line;
537                         }
538                         line = line + (char) c;
539                 }
540                 return null;
541         }
542
543         public static String replace_extension(String input, String extension) {
544                 int dot = input.lastIndexOf(".");
545                 if (dot > 0)
546                         input = input.substring(0,dot);
547                 return input.concat(extension);
548         }
549
550         public static File replace_extension(File input, String extension) {
551                 return new File(replace_extension(input.getPath(), extension));
552         }
553
554         public static String product_name(int product_id) {
555                 switch (product_id) {
556                 case product_altusmetrum: return "AltusMetrum";
557                 case product_telemetrum: return "TeleMetrum";
558                 case product_teledongle: return "TeleDongle";
559                 case product_teleterra: return "TeleTerra";
560                 case product_telebt: return "TeleBT";
561                 case product_telelaunch: return "TeleLaunch";
562                 case product_telelco: return "TeleLco";
563                 case product_telescience: return "Telescience";
564                 case product_telepyro: return "TelePyro";
565                 case product_telemega: return "TeleMega";
566                 case product_megadongle: return "MegaDongle";
567                 case product_telegps: return "TeleGPS";
568                 case product_easymini: return "EasyMini";
569                 case product_telemini: return "TeleMini";
570                 default: return "unknown";
571                 }
572         }
573
574         public static String igniter_name(int i) {
575                 return String.format("Ignitor %c", 'A' + i);
576         }
577 }