dd2a4baea86563e24ad5a305e8f649dd84ba1d5c
[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_14;
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         public static boolean is_gps_cmd(int cmd) {
42                 switch (cmd) {
43                 case AltosLib.AO_LOG_GPS_POS:
44                 case AltosLib.AO_LOG_GPS_TIME:
45                 case AltosLib.AO_LOG_GPS_LAT:
46                 case AltosLib.AO_LOG_GPS_LON:
47                 case AltosLib.AO_LOG_GPS_ALT:
48                 case AltosLib.AO_LOG_GPS_SAT:
49                 case AltosLib.AO_LOG_GPS_DATE:
50                         return true;
51                 }
52                 return false;
53         }
54
55         /* Added for header fields in eeprom files */
56         public static final int AO_LOG_CONFIG_VERSION = 1000;
57         public static final int AO_LOG_MAIN_DEPLOY = 1001;
58         public static final int AO_LOG_APOGEE_DELAY = 1002;
59         public static final int AO_LOG_RADIO_CHANNEL = 1003;
60         public static final int AO_LOG_CALLSIGN = 1004;
61         public static final int AO_LOG_ACCEL_CAL = 1005;
62         public static final int AO_LOG_RADIO_CAL = 1006;
63         public static final int AO_LOG_MAX_FLIGHT_LOG = 1007;
64         public static final int AO_LOG_MANUFACTURER = 2000;
65         public static final int AO_LOG_PRODUCT = 2001;
66         public static final int AO_LOG_SERIAL_NUMBER = 2002;
67         public static final int AO_LOG_LOG_FORMAT = 2003;
68
69         public static final int AO_LOG_FREQUENCY = 2004;
70         public static final int AO_LOG_APOGEE_LOCKOUT = 2005;
71         public static final int AO_LOG_RADIO_RATE = 2006;
72         public static final int AO_LOG_IGNITE_MODE = 2007;
73         public static final int AO_LOG_PAD_ORIENTATION = 2008;
74         public static final int AO_LOG_RADIO_ENABLE = 2009;
75         public static final int AO_LOG_AES_KEY = 2010;
76         public static final int AO_LOG_APRS = 2011;
77         public static final int AO_LOG_BEEP_SETTING = 2012;
78         public static final int AO_LOG_TRACKER_SETTING = 2013;
79         public static final int AO_LOG_PYRO_TIME = 2014;
80         public static final int AO_LOG_APRS_ID = 2015;
81         public static final int AO_LOG_ALTITUDE_32 = 2016;
82
83         /* Added for header fields in telemega files */
84         public static final int AO_LOG_BARO_RESERVED = 3000;
85         public static final int AO_LOG_BARO_SENS = 3001;
86         public static final int AO_LOG_BARO_OFF = 3002;
87         public static final int AO_LOG_BARO_TCS = 3004;
88         public static final int AO_LOG_BARO_TCO = 3005;
89         public static final int AO_LOG_BARO_TREF = 3006;
90         public static final int AO_LOG_BARO_TEMPSENS = 3007;
91         public static final int AO_LOG_BARO_CRC = 3008;
92         public static final int AO_LOG_IMU_CAL = 3009;
93
94         public static final int AO_LOG_SOFTWARE_VERSION = 9999;
95
96         public final static int MISSING = 0x7fffffff;
97
98         /* Added to flag invalid records */
99         public static final int AO_LOG_INVALID = -1;
100
101         /* Flight state numbers and names */
102         public static final int ao_flight_startup = 0;
103         public static final int ao_flight_idle = 1;
104         public static final int ao_flight_pad = 2;
105         public static final int ao_flight_boost = 3;
106         public static final int ao_flight_fast = 4;
107         public static final int ao_flight_coast = 5;
108         public static final int ao_flight_drogue = 6;
109         public static final int ao_flight_main = 7;
110         public static final int ao_flight_landed = 8;
111         public static final int ao_flight_invalid = 9;
112         public static final int ao_flight_stateless = 10;
113
114         /* USB product IDs */
115         public final static int vendor_altusmetrum = 0xfffe;
116
117         public final static int product_altusmetrum = 0x000a;
118         public final static int product_telemetrum = 0x000b;
119         public final static int product_teledongle = 0x000c;
120         public final static int product_easytimer = 0x000d;
121         public final static int product_telebt = 0x000e;
122         public final static int product_telelaunch = 0x000f;
123         public final static int product_telelco = 0x0010;
124         public final static int product_telescience = 0x0011;
125         public final static int product_telepyro =0x0012;
126         public final static int product_telemega = 0x0023;
127         public final static int product_megadongle = 0x0024;
128         public final static int product_telegps = 0x0025;
129         public final static int product_easymini = 0x0026;
130         public final static int product_telemini = 0x0027;
131         public final static int product_easymega = 0x0028;
132         public final static int product_usbtrng = 0x0029;
133         public final static int product_usbrelay = 0x002a;
134         public final static int product_mpusb = 0x002b;
135         public final static int product_easymotor = 0x002c;
136         public final static int product_altusmetrum_min = 0x000a;
137         public final static int product_altusmetrum_max = 0x002c;
138
139         public final static int product_any = 0x10000;
140         public final static int product_basestation = 0x10000 + 1;
141         public final static int product_altimeter = 0x10000 + 2;
142
143         public final static int gps_builtin = 0;
144         public final static int gps_mosaic = 1;
145
146         public final static String[] gps_receiver_names = {
147                 "Builtin", "Mosaic-X5"
148         };
149
150         private static class Product {
151                 final String    name;
152                 final int       product;
153
154                 Product (String name, int product) {
155                         this.name = name;
156                         this.product = product;
157                 }
158         }
159
160         private static Product[] products = {
161                 new Product("telemetrum", product_telemetrum),
162                 new Product("teleballoon", product_telemetrum),
163                 new Product("teledongle", product_teledongle),
164                 new Product("easytimer", product_easytimer),
165                 new Product("telebt", product_telebt),
166                 new Product("telelaunch", product_telelaunch),
167                 new Product("telelco", product_telelco),
168                 new Product("telescience", product_telescience),
169                 new Product("telepyro", product_telepyro),
170                 new Product("telemega", product_telemega),
171                 new Product("megadongle", product_megadongle),
172                 new Product("telegps", product_telegps),
173                 new Product("easymini", product_easymini),
174                 new Product("telemini", product_telemini),
175                 new Product("easymega", product_easymega),
176                 new Product("easymotor", product_easymotor)
177         };
178
179         public static int name_to_product(String name) {
180                 String low = name.toLowerCase();
181
182                 for (int i = 0; i < products.length; i++)
183                         if (low.startsWith(products[i].name))
184                                 return products[i].product;
185                 return product_any;
186         }
187
188         public static boolean has_9dof(int device_type) {
189                 return device_type == product_telemega || device_type == product_easymega;
190         }
191
192         public static boolean has_radio(int device_type) {
193                 return device_type != product_easymini && device_type != product_easymega;
194         }
195
196         public static boolean has_gps(int device_type) {
197                 return device_type == product_telemetrum ||
198                         device_type == product_telemega ||
199                         device_type == product_telegps;
200         }
201
202         /* Bluetooth "identifier" (bluetooth sucks) */
203         public final static String bt_product_telebt = "TeleBT";
204
205         /* "good" voltages */
206
207         public final static double ao_battery_good = 3.8;
208         public final static double ao_igniter_good = 3.5;
209
210         /* Telemetry modes */
211         public static final int ao_telemetry_off = 0;
212         public static final int ao_telemetry_min = 1;
213         public static final int ao_telemetry_standard = 1;
214         public static final int ao_telemetry_0_9 = 2;
215         public static final int ao_telemetry_0_8 = 3;
216         public static final int ao_telemetry_max = 3;
217
218         private static final String[] ao_telemetry_name = {
219                 "Off", "Standard Telemetry", "TeleMetrum v0.9", "TeleMetrum v0.8"
220         };
221
222         public static final int ao_telemetry_rate_38400 = 0;
223         public static final int ao_telemetry_rate_9600 = 1;
224         public static final int ao_telemetry_rate_2400 = 2;
225         public static final int ao_telemetry_rate_max = 2;
226
227         public static final Integer[] ao_telemetry_rate_values = {
228                 38400, 9600, 2400
229         };
230
231         public static final int ao_aprs_format_compressed = 0;
232         public static final int ao_aprs_format_uncompressed = 1;
233
234         public static final String[] ao_aprs_format_name = {
235                 "Compressed", "Uncompressed"
236         };
237
238         public static final String[] ignite_mode_values = {
239                 "Dual Deploy",
240                 "Redundant Apogee",
241                 "Redundant Main",
242                 "Separation & Apogee",
243         };
244
245         public static final String[]    pad_orientation_values_radio = {
246                 "Antenna Up",
247                 "Antenna Down",
248         };
249
250         public static final String[]    pad_orientation_values_no_radio = {
251                 "Beeper Up",
252                 "Beeper Down",
253         };
254
255         public static String[] pad_orientation_values(boolean radio) {
256                 if (radio)
257                         return pad_orientation_values_radio;
258                 else
259                         return pad_orientation_values_no_radio;
260         }
261
262         public static final String launch_sites_url = "https://maps.altusmetrum.org/launch-sites.txt";
263         public static final String launch_sites_env = "LAUNCH_SITES";
264 //      public static final String launch_sites_url = "file:///home/keithp/misc/text/altusmetrum/AltOS/launch-sites.txt";
265
266         public static final String unit_info_url = "https://altusmetrum.org/cgi-bin/unitinfo.cgi?sn=%d";
267         public static final String unit_info_env = "UNIT_INFO";
268
269         public static final int ao_telemetry_standard_len = 32;
270         public static final int ao_telemetry_0_9_len = 95;
271         public static final int ao_telemetry_0_8_len = 94;
272
273         private static final int[] ao_telemetry_len = {
274                 0, 32, 95, 94
275         };
276
277         private static HashMap<String,Integer>  string_to_state = new HashMap<String,Integer>();
278
279         private static boolean map_initialized = false;
280
281         public static void initialize_map()
282         {
283                 string_to_state.put("startup", ao_flight_startup);
284                 string_to_state.put("idle", ao_flight_idle);
285                 string_to_state.put("pad", ao_flight_pad);
286                 string_to_state.put("boost", ao_flight_boost);
287                 string_to_state.put("fast", ao_flight_fast);
288                 string_to_state.put("coast", ao_flight_coast);
289                 string_to_state.put("drogue", ao_flight_drogue);
290                 string_to_state.put("apogee", ao_flight_coast);
291                 string_to_state.put("main", ao_flight_main);
292                 string_to_state.put("landed", ao_flight_landed);
293                 string_to_state.put("invalid", ao_flight_invalid);
294                 string_to_state.put("stateless", ao_flight_stateless);
295                 map_initialized = true;
296         }
297
298         public static int telemetry_len(int telemetry) {
299                 if (telemetry <= ao_telemetry_max)
300                         return ao_telemetry_len[telemetry];
301                 throw new IllegalArgumentException(String.format("Invalid telemetry %d",
302                                                                  telemetry));
303         }
304
305         public static String telemetry_name(int telemetry) {
306                 if (telemetry <= ao_telemetry_max)
307                         return ao_telemetry_name[telemetry];
308                 throw new IllegalArgumentException(String.format("Invalid telemetry %d",
309                                                                  telemetry));
310         }
311
312         private static int[] split_version(String version) {
313                 String[] tokens = version.split("\\.");
314                 int[] ret = new int[tokens.length];
315                 for (int i = 0; i < tokens.length; i++)
316                         ret[i] = Integer.parseInt(tokens[i]);
317                 return ret;
318         }
319
320         public static int compare_version(String version_a, String version_b) {
321                 int[] a = split_version(version_a);
322                 int[] b = split_version(version_b);
323
324                 for (int i = 0; i < Math.min(a.length, b.length); i++) {
325                         if (a[i] < b[i])
326                                 return -1;
327                         if (a[i] > b[i])
328                                 return 1;
329                 }
330                 if (a.length < b.length)
331                         return -1;
332                 if (a.length > b.length)
333                         return 1;
334                 return 0;
335         }
336
337         private static String[] state_to_string = {
338                 "startup",
339                 "idle",
340                 "pad",
341                 "boost",
342                 "fast",
343                 "coast",
344                 "drogue",
345                 "main",
346                 "landed",
347                 "invalid",
348                 "stateless",
349         };
350
351         private static String[] state_to_string_capital = {
352                 "Startup",
353                 "Idle",
354                 "Pad",
355                 "Boost",
356                 "Fast",
357                 "Coast",
358                 "Drogue",
359                 "Main",
360                 "Landed",
361                 "Invalid",
362                 "Stateless",
363         };
364
365         public static int state(String state) {
366                 if (!map_initialized)
367                         initialize_map();
368                 if (string_to_state.containsKey(state))
369                         return string_to_state.get(state);
370                 return ao_flight_invalid;
371         }
372
373         public static String state_name(int state) {
374                 if (state < 0 || state_to_string.length <= state)
375                         return "invalid";
376                 return state_to_string[state];
377         }
378
379         public static String state_name_capital(int state) {
380                 if (state < 0 || state_to_string.length <= state)
381                         return "Invalid";
382                 return state_to_string_capital[state];
383         }
384
385         public static final int AO_GPS_VALID = (1 << 4);
386         public static final int AO_GPS_RUNNING = (1 << 5);
387         public static final int AO_GPS_DATE_VALID = (1 << 6);
388         public static final int AO_GPS_NUM_SAT_SHIFT = 0;
389         public static final int AO_GPS_NUM_SAT_MASK = 0xf;
390
391         public static final int AO_PAD_ORIENTATION_ANTENNA_UP = 0;
392         public static final int AO_PAD_ORIENTATION_ANTENNA_DOWN = 1;
393         public static final int AO_PAD_ORIENTATION_WORDS_UPRIGHT = 2;
394         public static final int AO_PAD_ORIENTATION_WORDS_UPSIDEDOWN = 3;
395         public static final int AO_PAD_ORIENTATION_BIG_PARTS_UP = 4;
396         public static final int AO_PAD_ORIENTATION_BIG_PARTS_DOWN = 5;
397
398         public static final int AO_LOG_FORMAT_UNKNOWN = 0;
399         public static final int AO_LOG_FORMAT_FULL = 1;
400         public static final int AO_LOG_FORMAT_TINY = 2;
401         public static final int AO_LOG_FORMAT_TELEMETRY = 3;
402         public static final int AO_LOG_FORMAT_TELESCIENCE = 4;
403         public static final int AO_LOG_FORMAT_TELEMEGA_OLD = 5;
404         public static final int AO_LOG_FORMAT_EASYMINI1 = 6;
405         public static final int AO_LOG_FORMAT_TELEMETRUM = 7;
406         public static final int AO_LOG_FORMAT_TELEMINI2 = 8;
407         public static final int AO_LOG_FORMAT_TELEGPS = 9;
408         public static final int AO_LOG_FORMAT_TELEMEGA = 10;
409         public static final int AO_LOG_FORMAT_DETHERM = 11;
410         public static final int AO_LOG_FORMAT_TELEMINI3 = 12;
411         public static final int AO_LOG_FORMAT_TELEFIRETWO = 13;
412         public static final int AO_LOG_FORMAT_EASYMINI2 = 14;
413         public static final int AO_LOG_FORMAT_TELEMEGA_3 = 15;
414         public static final int AO_LOG_FORMAT_EASYMEGA_2 = 16;
415         public static final int AO_LOG_FORMAT_TELESTATIC = 17;
416         public static final int AO_LOG_FORMAT_MICROPEAK2 = 18;
417         public static final int AO_LOG_FORMAT_TELEMEGA_4 = 19;
418         public static final int AO_LOG_FORMAT_EASYMOTOR = 20;
419         public static final int AO_LOG_FORMAT_TELEMEGA_5 = 21;
420         public static final int AO_LOG_FORMAT_TELEMEGA_6 = 22;
421         public static final int AO_LOG_FORMAT_EASYTIMER_2 = 23;
422         public static final int AO_LOG_FORMAT_EASYMEGA_3 = 24;
423         public static final int AO_LOG_FORMAT_NONE = 127;
424
425         public static final int model_mpu6000 = 0;
426         public static final int model_mpu9250 = 1;
427         public static final int model_adxl375 = 2;
428         public static final int model_bmx160 = 3;
429         public static final int model_hmc5883 = 4;
430         public static final int model_mmc5983 = 5;
431         public static final int model_bmi088 = 6;
432
433         public static boolean isspace(int c) {
434                 switch (c) {
435                 case ' ':
436                 case '\t':
437                         return true;
438                 }
439                 return false;
440         }
441
442         public static final boolean ishex(int c) {
443                 if ('0' <= c && c <= '9')
444                         return true;
445                 if ('a' <= c && c <= 'f')
446                         return true;
447                 if ('A' <= c && c <= 'F')
448                         return true;
449                 return false;
450         }
451
452         public static final boolean ishex(String s) {
453                 for (int i = 0; i < s.length(); i++)
454                         if (!ishex(s.charAt(i)))
455                                 return false;
456                 return true;
457         }
458
459         public static int fromhex(int c) {
460                 if ('0' <= c && c <= '9')
461                         return c - '0';
462                 if ('a' <= c && c <= 'f')
463                         return c - 'a' + 10;
464                 if ('A' <= c && c <= 'F')
465                         return c - 'A' + 10;
466                 return -1;
467         }
468
469         public static int fromhex(String s) throws NumberFormatException {
470                 int c, v = 0;
471                 for (int i = 0; i < s.length(); i++) {
472                         c = s.charAt(i);
473                         if (!ishex(c)) {
474                                 if (i == 0)
475                                         throw new NumberFormatException(String.format("invalid hex \"%s\"", s));
476                                 return v;
477                         }
478                         v = v * 16 + fromhex(c);
479                 }
480                 return v;
481         }
482
483         public static boolean isdec(int c) {
484                 if ('0' <= c && c <= '9')
485                         return true;
486                 return false;
487         }
488
489         public static boolean isdec(String s) {
490                 for (int i = 0; i < s.length(); i++)
491                         if (!isdec(s.charAt(i)))
492                                 return false;
493                 return true;
494         }
495
496         public static int fromdec(int c) {
497                 if ('0' <= c && c <= '9')
498                         return c - '0';
499                 return -1;
500         }
501
502         public static int int8(int[] bytes, int i) {
503                 return (int) (byte) bytes[i];
504         }
505
506         public static int uint8(int[] bytes, int i) {
507                 return bytes[i];
508         }
509
510         public static int int16(int[] bytes, int i) {
511                 return (int) (short) (bytes[i] + (bytes[i+1] << 8));
512         }
513
514         public static int uint16(int[] bytes, int i) {
515                 return bytes[i] + (bytes[i+1] << 8);
516         }
517
518         public static int uint32(int[] bytes, int i) {
519                 return bytes[i] +
520                         (bytes[i+1] << 8) +
521                         (bytes[i+2] << 16) +
522                         (bytes[i+3] << 24);
523         }
524
525         public static int int32(int[] bytes, int i) {
526                 return (int) uint32(bytes, i);
527         }
528
529         public static final Charset     unicode_set = Charset.forName("UTF-8");
530
531         public static String string(int[] bytes, int s, int l) {
532                 if (s + l > bytes.length) {
533                         if (s > bytes.length) {
534                                 s = bytes.length;
535                                 l = 0;
536                         } else {
537                                 l = bytes.length - s;
538                         }
539                 }
540
541                 int i;
542                 for (i = l - 1; i >= 0; i--)
543                         if (bytes[s+i] != 0)
544                                 break;
545
546                 l = i + 1;
547                 byte[]  b = new byte[l];
548
549                 for (i = 0; i < l; i++)
550                         b[i] = (byte) bytes[s+i];
551                 String n = new String(b, unicode_set);
552                 return n;
553         }
554
555         public static int hexbyte(String s, int i) {
556                 int c0, c1;
557
558                 if (s.length() < i + 2)
559                         throw new NumberFormatException(String.format("invalid hex \"%s\"", s));
560                 c0 = s.charAt(i);
561                 if (!ishex(c0))
562                         throw new NumberFormatException(String.format("invalid hex \"%c\"", c0));
563                 c1 = s.charAt(i+1);
564                 if (!ishex(c1))
565                         throw new NumberFormatException(String.format("invalid hex \"%c\"", c1));
566                 return fromhex(c0) * 16 + fromhex(c1);
567         }
568
569         public static int[] hexbytes(String s) {
570                 int     n;
571                 int[]   r;
572                 int     i;
573
574                 if ((s.length() & 1) != 0)
575                         throw new NumberFormatException(String.format("invalid line \"%s\"", s));
576                 byte[] bytes = s.getBytes(unicode_set);
577                 n = bytes.length / 2;
578                 r = new int[n];
579                 for (i = 0; i < n; i++) {
580                         int h = fromhex(bytes[(i << 1)]);
581                         int l = fromhex(bytes[(i << 1) + 1]);
582                         if (h < 0 || l < 0)
583                                 throw new NumberFormatException(String.format("invalid hex \"%c%c\"",
584                                                                               bytes[(i<<1)], bytes[(i<<1) + 1]));
585                         r[i] = (h << 4) + l;
586                 }
587                 return r;
588         }
589
590         public static long fromdec(String s) throws NumberFormatException {
591                 int c;
592                 long v = 0;
593                 long sign = 1;
594                 for (int i = 0; i < s.length(); i++) {
595                         c = s.charAt(i);
596                         if (i == 0 && c == '-') {
597                                 sign = -1;
598                         } else if (!isdec(c)) {
599                                 if (i == 0)
600                                         throw new NumberFormatException(String.format("invalid number \"%s\"", s));
601                                 return v;
602                         } else
603                                 v = v * 10 + fromdec(c);
604                 }
605                 return v * sign;
606         }
607
608         public static String gets(FileInputStream s) throws IOException {
609                 int c;
610                 String  line = "";
611
612                 while ((c = s.read()) != -1) {
613                         if (c == '\r')
614                                 continue;
615                         if (c == '\n') {
616                                 return line;
617                         }
618                         line = line + (char) c;
619                 }
620                 return null;
621         }
622
623         public static String replace_extension(String input, String extension) {
624                 int dot = input.lastIndexOf(".");
625                 if (dot > 0)
626                         input = input.substring(0,dot);
627                 return input.concat(extension);
628         }
629
630         public static File replace_extension(File input, String extension) {
631                 return new File(replace_extension(input.getPath(), extension));
632         }
633
634         public static String product_name(int product_id) {
635                 switch (product_id) {
636                 case product_altusmetrum: return "AltusMetrum";
637                 case product_telemetrum: return "TeleMetrum";
638                 case product_teledongle: return "TeleDongle";
639                 case product_easytimer: return "EasyTimer";
640                 case product_telebt: return "TeleBT";
641                 case product_telelaunch: return "TeleLaunch";
642                 case product_telelco: return "TeleLco";
643                 case product_telescience: return "Telescience";
644                 case product_telepyro: return "TelePyro";
645                 case product_telemega: return "TeleMega";
646                 case product_megadongle: return "MegaDongle";
647                 case product_telegps: return "TeleGPS";
648                 case product_easymini: return "EasyMini";
649                 case product_telemini: return "TeleMini";
650                 case product_easymega: return "EasyMega";
651                 case product_easymotor: return "EasyMotor";
652                 default: return "unknown";
653                 }
654         }
655
656         public static int product_id_from_log_format(int log_format) {
657                 switch (log_format){
658                 case AO_LOG_FORMAT_UNKNOWN:
659                         return product_altusmetrum;
660                 case AO_LOG_FORMAT_FULL:
661                         return product_telemetrum;
662                 case AO_LOG_FORMAT_TINY:
663                         return product_telemini;
664                 case AO_LOG_FORMAT_TELEMETRY:
665                         return product_altusmetrum;
666                 case AO_LOG_FORMAT_TELESCIENCE:
667                         return product_telescience;
668                 case AO_LOG_FORMAT_TELEMEGA_OLD:
669                         return product_telemega;
670                 case AO_LOG_FORMAT_EASYMINI1:
671                         return product_easymini;
672                 case AO_LOG_FORMAT_TELEMETRUM:
673                         return product_telemetrum;
674                 case AO_LOG_FORMAT_TELEMINI2:
675                         return product_telemini;
676                 case AO_LOG_FORMAT_TELEGPS:
677                         return product_telegps;
678                 case AO_LOG_FORMAT_TELEMEGA:
679                         return product_telemega;
680                 case AO_LOG_FORMAT_DETHERM:
681                         return product_altusmetrum;
682                 case AO_LOG_FORMAT_TELEMINI3:
683                         return product_telemini;
684                 case AO_LOG_FORMAT_TELEFIRETWO:
685                         return product_altusmetrum;
686                 case AO_LOG_FORMAT_EASYMINI2:
687                         return product_easymini;
688                 case AO_LOG_FORMAT_TELEMEGA_3:
689                         return product_telemega;
690                 case AO_LOG_FORMAT_EASYMEGA_2:
691                 case AO_LOG_FORMAT_EASYMEGA_3:
692                         return product_easymega;
693                 case AO_LOG_FORMAT_TELESTATIC:
694                         return product_altusmetrum;
695                 case AO_LOG_FORMAT_MICROPEAK2:
696                         return product_altusmetrum;
697                 case AO_LOG_FORMAT_TELEMEGA_4:
698                         return product_telemega;
699                 case AO_LOG_FORMAT_EASYMOTOR:
700                         return product_easymotor;
701                 case AO_LOG_FORMAT_TELEMEGA_5:
702                         return product_telemega;
703                 case AO_LOG_FORMAT_TELEMEGA_6:
704                         return product_telemega;
705                 case AO_LOG_FORMAT_NONE:
706                         return product_altusmetrum;
707                 default:
708                         return product_altusmetrum;
709                 }
710         }
711
712         public static String igniter_name(int i) {
713                 return String.format("Igniter %c", 'A' + i);
714         }
715
716         public static String igniter_short_name(int i) {
717                 return String.format("igniter_%c", 'a' + i);
718         }
719
720         public static AltosRecordSet record_set(File file) throws FileNotFoundException, IOException {
721                 FileInputStream in;
722                 in = new FileInputStream(file);
723                 if (file.getName().endsWith("telem")) {
724                         return new AltosTelemetryFile(in);
725                 } else if (file.getName().endsWith("eeprom")) {
726                         return new AltosEepromFile(in);
727                 } else {
728                         String  name = file.getName();
729                         int     dot = name.lastIndexOf('.');
730                         String  extension;
731
732                         if (dot == -1)
733                                 throw new IOException(String.format("%s (Missing extension)", file.toString()));
734                         else {
735                                 extension = name.substring(dot);
736                                 throw new IOException(String.format("%s (Invalid extension '%s')",
737                                                                     file.toString(),
738                                                                     extension));
739                         }
740                 }
741         }
742
743 }