altoslib: Remove spurious debug printf in AltosCalData
[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_13;
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_teleterra = 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_altusmetrum_min = 0x000a;
136         public final static int product_altusmetrum_max = 0x002c;
137
138         public final static int product_any = 0x10000;
139         public final static int product_basestation = 0x10000 + 1;
140         public final static int product_altimeter = 0x10000 + 2;
141
142         private static class Product {
143                 final String    name;
144                 final int       product;
145
146                 Product (String name, int product) {
147                         this.name = name;
148                         this.product = product;
149                 }
150         }
151
152         private static Product[] products = {
153                 new Product("telemetrum", product_telemetrum),
154                 new Product("teleballoon", product_telemetrum),
155                 new Product("teledongle", product_teledongle),
156                 new Product("teleterra", product_teledongle),
157                 new Product("telebt", product_telebt),
158                 new Product("telelaunch", product_telelaunch),
159                 new Product("telelco", product_telelco),
160                 new Product("telescience", product_telescience),
161                 new Product("telepyro", product_telepyro),
162                 new Product("telemega", product_telemega),
163                 new Product("megadongle", product_megadongle),
164                 new Product("telegps", product_telegps),
165                 new Product("easymini", product_easymini),
166                 new Product("telemini", product_telemini),
167                 new Product("easymega", product_easymega)
168         };
169
170         public static int name_to_product(String name) {
171                 String low = name.toLowerCase();
172
173                 for (int i = 0; i < products.length; i++)
174                         if (low.startsWith(products[i].name))
175                                 return products[i].product;
176                 return product_any;
177         }
178
179         public static boolean has_9dof(int device_type) {
180                 return device_type == product_telemega || device_type == product_easymega;
181         }
182
183         public static boolean has_radio(int device_type) {
184                 return device_type != product_easymini && device_type != product_easymega;
185         }
186
187         public static boolean has_gps(int device_type) {
188                 return device_type == product_telemetrum ||
189                         device_type == product_telemega ||
190                         device_type == product_telegps;
191         }
192
193         /* Bluetooth "identifier" (bluetooth sucks) */
194         public final static String bt_product_telebt = "TeleBT";
195
196         /* "good" voltages */
197
198         public final static double ao_battery_good = 3.8;
199         public final static double ao_igniter_good = 3.5;
200
201         /* Telemetry modes */
202         public static final int ao_telemetry_off = 0;
203         public static final int ao_telemetry_min = 1;
204         public static final int ao_telemetry_standard = 1;
205         public static final int ao_telemetry_0_9 = 2;
206         public static final int ao_telemetry_0_8 = 3;
207         public static final int ao_telemetry_max = 3;
208
209         private static final String[] ao_telemetry_name = {
210                 "Off", "Standard Telemetry", "TeleMetrum v0.9", "TeleMetrum v0.8"
211         };
212
213         public static final int ao_telemetry_rate_38400 = 0;
214         public static final int ao_telemetry_rate_9600 = 1;
215         public static final int ao_telemetry_rate_2400 = 2;
216         public static final int ao_telemetry_rate_max = 2;
217
218         public static final Integer[] ao_telemetry_rate_values = {
219                 38400, 9600, 2400
220         };
221
222         public static final int ao_aprs_format_compressed = 0;
223         public static final int ao_aprs_format_uncompressed = 1;
224
225         public static final String[] ao_aprs_format_name = {
226                 "Compressed", "Uncompressed"
227         };
228
229         public static final String launch_sites_url = "https://maps.altusmetrum.org/launch-sites.txt";
230         public static final String launch_sites_env = "LAUNCH_SITES";
231 //      public static final String launch_sites_url = "file:///home/keithp/misc/text/altusmetrum/AltOS/launch-sites.txt";
232
233         public static final String unit_info_url = "https://altusmetrum.org/cgi-bin/unitinfo.cgi?sn=%d";
234         public static final String unit_info_env = "UNIT_INFO";
235
236         public static final int ao_telemetry_standard_len = 32;
237         public static final int ao_telemetry_0_9_len = 95;
238         public static final int ao_telemetry_0_8_len = 94;
239
240         private static final int[] ao_telemetry_len = {
241                 0, 32, 95, 94
242         };
243
244         private static HashMap<String,Integer>  string_to_state = new HashMap<String,Integer>();
245
246         private static boolean map_initialized = false;
247
248         public static void initialize_map()
249         {
250                 string_to_state.put("startup", ao_flight_startup);
251                 string_to_state.put("idle", ao_flight_idle);
252                 string_to_state.put("pad", ao_flight_pad);
253                 string_to_state.put("boost", ao_flight_boost);
254                 string_to_state.put("fast", ao_flight_fast);
255                 string_to_state.put("coast", ao_flight_coast);
256                 string_to_state.put("drogue", ao_flight_drogue);
257                 string_to_state.put("apogee", ao_flight_coast);
258                 string_to_state.put("main", ao_flight_main);
259                 string_to_state.put("landed", ao_flight_landed);
260                 string_to_state.put("invalid", ao_flight_invalid);
261                 string_to_state.put("stateless", ao_flight_stateless);
262                 map_initialized = true;
263         }
264
265         public static int telemetry_len(int telemetry) {
266                 if (telemetry <= ao_telemetry_max)
267                         return ao_telemetry_len[telemetry];
268                 throw new IllegalArgumentException(String.format("Invalid telemetry %d",
269                                                                  telemetry));
270         }
271
272         public static String telemetry_name(int telemetry) {
273                 if (telemetry <= ao_telemetry_max)
274                         return ao_telemetry_name[telemetry];
275                 throw new IllegalArgumentException(String.format("Invalid telemetry %d",
276                                                                  telemetry));
277         }
278
279         private static int[] split_version(String version) {
280                 String[] tokens = version.split("\\.");
281                 int[] ret = new int[tokens.length];
282                 for (int i = 0; i < tokens.length; i++)
283                         ret[i] = Integer.parseInt(tokens[i]);
284                 return ret;
285         }
286
287         public static int compare_version(String version_a, String version_b) {
288                 int[] a = split_version(version_a);
289                 int[] b = split_version(version_b);
290
291                 for (int i = 0; i < Math.min(a.length, b.length); i++) {
292                         if (a[i] < b[i])
293                                 return -1;
294                         if (a[i] > b[i])
295                                 return 1;
296                 }
297                 if (a.length < b.length)
298                         return -1;
299                 if (a.length > b.length)
300                         return 1;
301                 return 0;
302         }
303
304         private static String[] state_to_string = {
305                 "startup",
306                 "idle",
307                 "pad",
308                 "boost",
309                 "fast",
310                 "coast",
311                 "drogue",
312                 "main",
313                 "landed",
314                 "invalid",
315                 "stateless",
316         };
317
318         private static String[] state_to_string_capital = {
319                 "Startup",
320                 "Idle",
321                 "Pad",
322                 "Boost",
323                 "Fast",
324                 "Coast",
325                 "Drogue",
326                 "Main",
327                 "Landed",
328                 "Invalid",
329                 "Stateless",
330         };
331
332         public static int state(String state) {
333                 if (!map_initialized)
334                         initialize_map();
335                 if (string_to_state.containsKey(state))
336                         return string_to_state.get(state);
337                 return ao_flight_invalid;
338         }
339
340         public static String state_name(int state) {
341                 if (state < 0 || state_to_string.length <= state)
342                         return "invalid";
343                 return state_to_string[state];
344         }
345
346         public static String state_name_capital(int state) {
347                 if (state < 0 || state_to_string.length <= state)
348                         return "Invalid";
349                 return state_to_string_capital[state];
350         }
351
352         public static final int AO_GPS_VALID = (1 << 4);
353         public static final int AO_GPS_RUNNING = (1 << 5);
354         public static final int AO_GPS_DATE_VALID = (1 << 6);
355         public static final int AO_GPS_NUM_SAT_SHIFT = 0;
356         public static final int AO_GPS_NUM_SAT_MASK = 0xf;
357
358         public static final int AO_PAD_ORIENTATION_ANTENNA_UP = 0;
359         public static final int AO_PAD_ORIENTATION_ANTENNA_DOWN = 1;
360
361         public static final int AO_LOG_FORMAT_UNKNOWN = 0;
362         public static final int AO_LOG_FORMAT_FULL = 1;
363         public static final int AO_LOG_FORMAT_TINY = 2;
364         public static final int AO_LOG_FORMAT_TELEMETRY = 3;
365         public static final int AO_LOG_FORMAT_TELESCIENCE = 4;
366         public static final int AO_LOG_FORMAT_TELEMEGA_OLD = 5;
367         public static final int AO_LOG_FORMAT_EASYMINI1 = 6;
368         public static final int AO_LOG_FORMAT_TELEMETRUM = 7;
369         public static final int AO_LOG_FORMAT_TELEMINI2 = 8;
370         public static final int AO_LOG_FORMAT_TELEGPS = 9;
371         public static final int AO_LOG_FORMAT_TELEMEGA = 10;
372         public static final int AO_LOG_FORMAT_DETHERM = 11;
373         public static final int AO_LOG_FORMAT_TELEMINI3 = 12;
374         public static final int AO_LOG_FORMAT_TELEFIRETWO = 13;
375         public static final int AO_LOG_FORMAT_EASYMINI2 = 14;
376         public static final int AO_LOG_FORMAT_TELEMEGA_3 = 15;
377         public static final int AO_LOG_FORMAT_EASYMEGA_2 = 16;
378         public static final int AO_LOG_FORMAT_NONE = 127;
379
380         public static boolean isspace(int c) {
381                 switch (c) {
382                 case ' ':
383                 case '\t':
384                         return true;
385                 }
386                 return false;
387         }
388
389         public static final boolean ishex(int c) {
390                 if ('0' <= c && c <= '9')
391                         return true;
392                 if ('a' <= c && c <= 'f')
393                         return true;
394                 if ('A' <= c && c <= 'F')
395                         return true;
396                 return false;
397         }
398
399         public static final boolean ishex(String s) {
400                 for (int i = 0; i < s.length(); i++)
401                         if (!ishex(s.charAt(i)))
402                                 return false;
403                 return true;
404         }
405
406         public static int fromhex(int c) {
407                 if ('0' <= c && c <= '9')
408                         return c - '0';
409                 if ('a' <= c && c <= 'f')
410                         return c - 'a' + 10;
411                 if ('A' <= c && c <= 'F')
412                         return c - 'A' + 10;
413                 return -1;
414         }
415
416         public static int fromhex(String s) throws NumberFormatException {
417                 int c, v = 0;
418                 for (int i = 0; i < s.length(); i++) {
419                         c = s.charAt(i);
420                         if (!ishex(c)) {
421                                 if (i == 0)
422                                         throw new NumberFormatException(String.format("invalid hex \"%s\"", s));
423                                 return v;
424                         }
425                         v = v * 16 + fromhex(c);
426                 }
427                 return v;
428         }
429
430         public static boolean isdec(int c) {
431                 if ('0' <= c && c <= '9')
432                         return true;
433                 return false;
434         }
435
436         public static boolean isdec(String s) {
437                 for (int i = 0; i < s.length(); i++)
438                         if (!isdec(s.charAt(i)))
439                                 return false;
440                 return true;
441         }
442
443         public static int fromdec(int c) {
444                 if ('0' <= c && c <= '9')
445                         return c - '0';
446                 return -1;
447         }
448
449         public static int int8(int[] bytes, int i) {
450                 return (int) (byte) bytes[i];
451         }
452
453         public static int uint8(int[] bytes, int i) {
454                 return bytes[i];
455         }
456
457         public static int int16(int[] bytes, int i) {
458                 return (int) (short) (bytes[i] + (bytes[i+1] << 8));
459         }
460
461         public static int uint16(int[] bytes, int i) {
462                 return bytes[i] + (bytes[i+1] << 8);
463         }
464
465         public static int uint32(int[] bytes, int i) {
466                 return bytes[i] +
467                         (bytes[i+1] << 8) +
468                         (bytes[i+2] << 16) +
469                         (bytes[i+3] << 24);
470         }
471
472         public static int int32(int[] bytes, int i) {
473                 return (int) uint32(bytes, i);
474         }
475
476         public static final Charset     unicode_set = Charset.forName("UTF-8");
477
478         public static String string(int[] bytes, int s, int l) {
479                 if (s + l > bytes.length) {
480                         if (s > bytes.length) {
481                                 s = bytes.length;
482                                 l = 0;
483                         } else {
484                                 l = bytes.length - s;
485                         }
486                 }
487
488                 int i;
489                 for (i = l - 1; i >= 0; i--)
490                         if (bytes[s+i] != 0)
491                                 break;
492
493                 l = i + 1;
494                 byte[]  b = new byte[l];
495
496                 for (i = 0; i < l; i++)
497                         b[i] = (byte) bytes[s+i];
498                 String n = new String(b, unicode_set);
499                 return n;
500         }
501
502         public static int hexbyte(String s, int i) {
503                 int c0, c1;
504
505                 if (s.length() < i + 2)
506                         throw new NumberFormatException(String.format("invalid hex \"%s\"", s));
507                 c0 = s.charAt(i);
508                 if (!ishex(c0))
509                         throw new NumberFormatException(String.format("invalid hex \"%c\"", c0));
510                 c1 = s.charAt(i+1);
511                 if (!ishex(c1))
512                         throw new NumberFormatException(String.format("invalid hex \"%c\"", c1));
513                 return fromhex(c0) * 16 + fromhex(c1);
514         }
515
516         public static int[] hexbytes(String s) {
517                 int     n;
518                 int[]   r;
519                 int     i;
520
521                 if ((s.length() & 1) != 0)
522                         throw new NumberFormatException(String.format("invalid line \"%s\"", s));
523                 byte[] bytes = s.getBytes(unicode_set);
524                 n = bytes.length / 2;
525                 r = new int[n];
526                 for (i = 0; i < n; i++) {
527                         int h = fromhex(bytes[(i << 1)]);
528                         int l = fromhex(bytes[(i << 1) + 1]);
529                         if (h < 0 || l < 0)
530                                 throw new NumberFormatException(String.format("invalid hex \"%c%c\"",
531                                                                               bytes[(i<<1)], bytes[(i<<1) + 1]));
532                         r[i] = (h << 4) + l;
533                 }
534                 return r;
535         }
536
537         public static long fromdec(String s) throws NumberFormatException {
538                 int c;
539                 long v = 0;
540                 long sign = 1;
541                 for (int i = 0; i < s.length(); i++) {
542                         c = s.charAt(i);
543                         if (i == 0 && c == '-') {
544                                 sign = -1;
545                         } else if (!isdec(c)) {
546                                 if (i == 0)
547                                         throw new NumberFormatException(String.format("invalid number \"%s\"", s));
548                                 return v;
549                         } else
550                                 v = v * 10 + fromdec(c);
551                 }
552                 return v * sign;
553         }
554
555         public static String gets(FileInputStream s) throws IOException {
556                 int c;
557                 String  line = "";
558
559                 while ((c = s.read()) != -1) {
560                         if (c == '\r')
561                                 continue;
562                         if (c == '\n') {
563                                 return line;
564                         }
565                         line = line + (char) c;
566                 }
567                 return null;
568         }
569
570         public static String replace_extension(String input, String extension) {
571                 int dot = input.lastIndexOf(".");
572                 if (dot > 0)
573                         input = input.substring(0,dot);
574                 return input.concat(extension);
575         }
576
577         public static File replace_extension(File input, String extension) {
578                 return new File(replace_extension(input.getPath(), extension));
579         }
580
581         public static String product_name(int product_id) {
582                 switch (product_id) {
583                 case product_altusmetrum: return "AltusMetrum";
584                 case product_telemetrum: return "TeleMetrum";
585                 case product_teledongle: return "TeleDongle";
586                 case product_teleterra: return "TeleTerra";
587                 case product_telebt: return "TeleBT";
588                 case product_telelaunch: return "TeleLaunch";
589                 case product_telelco: return "TeleLco";
590                 case product_telescience: return "Telescience";
591                 case product_telepyro: return "TelePyro";
592                 case product_telemega: return "TeleMega";
593                 case product_megadongle: return "MegaDongle";
594                 case product_telegps: return "TeleGPS";
595                 case product_easymini: return "EasyMini";
596                 case product_telemini: return "TeleMini";
597                 default: return "unknown";
598                 }
599         }
600
601         public static String igniter_name(int i) {
602                 return String.format("Igniter %c", 'A' + i);
603         }
604
605         public static String igniter_short_name(int i) {
606                 return String.format("igniter_%c", 'a' + i);
607         }
608
609         public static AltosRecordSet record_set(File file) throws FileNotFoundException, IOException {
610                 FileInputStream in;
611                 in = new FileInputStream(file);
612                 if (file.getName().endsWith("telem")) {
613                         return new AltosTelemetryFile(in);
614                 } else if (file.getName().endsWith("eeprom")) {
615                         return new AltosEepromFile(in);
616                 } else {
617                         String  name = file.getName();
618                         int     dot = name.lastIndexOf('.');
619                         String  extension;
620
621                         if (dot == -1)
622                                 throw new IOException(String.format("%s (Missing extension)", file.toString()));
623                         else {
624                                 extension = name.substring(dot);
625                                 throw new IOException(String.format("%s (Invalid extension '%s')",
626                                                                     file.toString(),
627                                                                     extension));
628                         }
629                 }
630         }
631
632 }