altoslib: Deal with new Mega log format that has 32-bit gyro cal
[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; version 2 of the License.
7  *
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.
12  *
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.
16  */
17
18 package org.altusmetrum.altoslib_5;
19
20 import java.util.*;
21 import java.io.*;
22 import java.nio.charset.Charset;
23
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';
39
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;
53
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;
67
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
78         public static final int AO_LOG_SOFTWARE_VERSION = 9999;
79
80         public final static int MISSING = 0x7fffffff;
81
82         /* Added to flag invalid records */
83         public static final int AO_LOG_INVALID = -1;
84
85         /* Flight state numbers and names */
86         public static final int ao_flight_startup = 0;
87         public static final int ao_flight_idle = 1;
88         public static final int ao_flight_pad = 2;
89         public static final int ao_flight_boost = 3;
90         public static final int ao_flight_fast = 4;
91         public static final int ao_flight_coast = 5;
92         public static final int ao_flight_drogue = 6;
93         public static final int ao_flight_main = 7;
94         public static final int ao_flight_landed = 8;
95         public static final int ao_flight_invalid = 9;
96         public static final int ao_flight_stateless = 10;
97
98         /* USB product IDs */
99         public final static int vendor_altusmetrum = 0xfffe;
100
101         public final static int product_altusmetrum = 0x000a;
102         public final static int product_telemetrum = 0x000b;
103         public final static int product_teledongle = 0x000c;
104         public final static int product_teleterra = 0x000d;
105         public final static int product_telebt = 0x000e;
106         public final static int product_telelaunch = 0x000f;
107         public final static int product_telelco = 0x0010;
108         public final static int product_telescience = 0x0011;
109         public final static int product_telepyro =0x0012;
110         public final static int product_telemega = 0x0023;
111         public final static int product_megadongle = 0x0024;
112         public final static int product_telegps = 0x0025;
113         public final static int product_easymini = 0x0026;
114         public final static int product_telemini = 0x0027;
115         public final static int product_easymega = 0x0028;
116         public final static int product_altusmetrum_min = 0x000a;
117         public final static int product_altusmetrum_max = 0x002c;
118
119         public final static int product_any = 0x10000;
120         public final static int product_basestation = 0x10000 + 1;
121         public final static int product_altimeter = 0x10000 + 2;
122
123         private static class Product {
124                 final String    name;
125                 final int       product;
126
127                 Product (String name, int product) {
128                         this.name = name;
129                         this.product = product;
130                 }
131         }
132
133         private static Product[] products = {
134                 new Product("telemetrum", product_telemetrum),
135                 new Product("teleballoon", product_telemetrum),
136                 new Product("teledongle", product_teledongle),
137                 new Product("teleterra", product_teledongle),
138                 new Product("telebt", product_telebt),
139                 new Product("telelaunch", product_telelaunch),
140                 new Product("telelco", product_telelco),
141                 new Product("telescience", product_telescience),
142                 new Product("telepyro", product_telepyro),
143                 new Product("telemega", product_telemega),
144                 new Product("megadongle", product_megadongle),
145                 new Product("telegps", product_telegps),
146                 new Product("easymini", product_easymini),
147                 new Product("telemini", product_telemini),
148                 new Product("easymega", product_easymega)
149         };
150
151         public static int name_to_product(String name) {
152                 String low = name.toLowerCase();
153
154                 for (int i = 0; i < products.length; i++)
155                         if (low.startsWith(products[i].name))
156                                 return products[i].product;
157                 return product_any;
158         }
159
160         /* Bluetooth "identifier" (bluetooth sucks) */
161         public final static String bt_product_telebt = "TeleBT";
162
163         /* "good" voltages */
164
165         public final static double ao_battery_good = 3.8;
166         public final static double ao_igniter_good = 3.5;
167
168         /* Telemetry modes */
169         public static final int ao_telemetry_off = 0;
170         public static final int ao_telemetry_min = 1;
171         public static final int ao_telemetry_standard = 1;
172         public static final int ao_telemetry_0_9 = 2;
173         public static final int ao_telemetry_0_8 = 3;
174         public static final int ao_telemetry_max = 3;
175
176         private static final String[] ao_telemetry_name = {
177                 "Off", "Standard Telemetry", "TeleMetrum v0.9", "TeleMetrum v0.8"
178         };
179
180         public static final int ao_telemetry_rate_38400 = 0;
181         public static final int ao_telemetry_rate_9600 = 1;
182         public static final int ao_telemetry_rate_2400 = 2;
183         public static final int ao_telemetry_rate_max = 2;
184
185         public static final Integer[] ao_telemetry_rate_values = {
186                 38400, 9600, 2400
187         };
188
189         public static final String launch_sites_url = "http://www.altusmetrum.org/AltOS/launch-sites.txt";
190
191         public static final int ao_telemetry_standard_len = 32;
192         public static final int ao_telemetry_0_9_len = 95;
193         public static final int ao_telemetry_0_8_len = 94;
194
195         private static final int[] ao_telemetry_len = {
196                 0, 32, 95, 94
197         };
198
199         private static HashMap<String,Integer>  string_to_state = new HashMap<String,Integer>();
200
201         private static boolean map_initialized = false;
202
203         public static void initialize_map()
204         {
205                 string_to_state.put("startup", ao_flight_startup);
206                 string_to_state.put("idle", ao_flight_idle);
207                 string_to_state.put("pad", ao_flight_pad);
208                 string_to_state.put("boost", ao_flight_boost);
209                 string_to_state.put("fast", ao_flight_fast);
210                 string_to_state.put("coast", ao_flight_coast);
211                 string_to_state.put("drogue", ao_flight_drogue);
212                 string_to_state.put("apogee", ao_flight_coast);
213                 string_to_state.put("main", ao_flight_main);
214                 string_to_state.put("landed", ao_flight_landed);
215                 string_to_state.put("invalid", ao_flight_invalid);
216                 string_to_state.put("stateless", ao_flight_stateless);
217                 map_initialized = true;
218         }
219
220         public static int telemetry_len(int telemetry) {
221                 if (telemetry <= ao_telemetry_max)
222                         return ao_telemetry_len[telemetry];
223                 throw new IllegalArgumentException(String.format("Invalid telemetry %d",
224                                                                  telemetry));
225         }
226
227         public static String telemetry_name(int telemetry) {
228                 if (telemetry <= ao_telemetry_max)
229                         return ao_telemetry_name[telemetry];
230                 throw new IllegalArgumentException(String.format("Invalid telemetry %d",
231                                                                  telemetry));
232         }
233
234         private static int[] split_version(String version) {
235                 String[] tokens = version.split("\\.");
236                 int[] ret = new int[tokens.length];
237                 for (int i = 0; i < tokens.length; i++)
238                         ret[i] = Integer.parseInt(tokens[i]);
239                 return ret;
240         }
241
242         public static int compare_version(String version_a, String version_b) {
243                 int[] a = split_version(version_a);
244                 int[] b = split_version(version_b);
245
246                 for (int i = 0; i < Math.min(a.length, b.length); i++) {
247                         if (a[i] < b[i])
248                                 return -1;
249                         if (a[i] > b[i])
250                                 return 1;
251                 }
252                 if (a.length < b.length)
253                         return -1;
254                 if (a.length > b.length)
255                         return 1;
256                 return 0;
257         }
258
259         private static String[] state_to_string = {
260                 "startup",
261                 "idle",
262                 "pad",
263                 "boost",
264                 "fast",
265                 "coast",
266                 "drogue",
267                 "main",
268                 "landed",
269                 "invalid",
270                 "stateless",
271         };
272
273         private static String[] state_to_string_capital = {
274                 "Startup",
275                 "Idle",
276                 "Pad",
277                 "Boost",
278                 "Fast",
279                 "Coast",
280                 "Drogue",
281                 "Main",
282                 "Landed",
283                 "Invalid",
284                 "Stateless",
285         };
286
287         public static int state(String state) {
288                 if (!map_initialized)
289                         initialize_map();
290                 if (string_to_state.containsKey(state))
291                         return string_to_state.get(state);
292                 return ao_flight_invalid;
293         }
294
295         public static String state_name(int state) {
296                 if (state < 0 || state_to_string.length <= state)
297                         return "invalid";
298                 return state_to_string[state];
299         }
300
301         public static String state_name_capital(int state) {
302                 if (state < 0 || state_to_string.length <= state)
303                         return "Invalid";
304                 return state_to_string_capital[state];
305         }
306
307         public static final int AO_GPS_VALID = (1 << 4);
308         public static final int AO_GPS_RUNNING = (1 << 5);
309         public static final int AO_GPS_DATE_VALID = (1 << 6);
310         public static final int AO_GPS_NUM_SAT_SHIFT = 0;
311         public static final int AO_GPS_NUM_SAT_MASK = 0xf;
312
313         public static final int AO_LOG_FORMAT_UNKNOWN = 0;
314         public static final int AO_LOG_FORMAT_FULL = 1;
315         public static final int AO_LOG_FORMAT_TINY = 2;
316         public static final int AO_LOG_FORMAT_TELEMETRY = 3;
317         public static final int AO_LOG_FORMAT_TELESCIENCE = 4;
318         public static final int AO_LOG_FORMAT_TELEMEGA_OLD = 5;
319         public static final int AO_LOG_FORMAT_EASYMINI = 6;
320         public static final int AO_LOG_FORMAT_TELEMETRUM = 7;
321         public static final int AO_LOG_FORMAT_TELEMINI = 8;
322         public static final int AO_LOG_FORMAT_TELEGPS = 9;
323         public static final int AO_LOG_FORMAT_TELEMEGA = 10;
324         public static final int AO_LOG_FORMAT_NONE = 127;
325
326         public static boolean isspace(int c) {
327                 switch (c) {
328                 case ' ':
329                 case '\t':
330                         return true;
331                 }
332                 return false;
333         }
334
335         public static final boolean ishex(int c) {
336                 if ('0' <= c && c <= '9')
337                         return true;
338                 if ('a' <= c && c <= 'f')
339                         return true;
340                 if ('A' <= c && c <= 'F')
341                         return true;
342                 return false;
343         }
344
345         public static final boolean ishex(String s) {
346                 for (int i = 0; i < s.length(); i++)
347                         if (!ishex(s.charAt(i)))
348                                 return false;
349                 return true;
350         }
351
352         public static int fromhex(int c) {
353                 if ('0' <= c && c <= '9')
354                         return c - '0';
355                 if ('a' <= c && c <= 'f')
356                         return c - 'a' + 10;
357                 if ('A' <= c && c <= 'F')
358                         return c - 'A' + 10;
359                 return -1;
360         }
361
362         public static int fromhex(String s) throws NumberFormatException {
363                 int c, v = 0;
364                 for (int i = 0; i < s.length(); i++) {
365                         c = s.charAt(i);
366                         if (!ishex(c)) {
367                                 if (i == 0)
368                                         throw new NumberFormatException(String.format("invalid hex \"%s\"", s));
369                                 return v;
370                         }
371                         v = v * 16 + fromhex(c);
372                 }
373                 return v;
374         }
375
376         public static boolean isdec(int c) {
377                 if ('0' <= c && c <= '9')
378                         return true;
379                 return false;
380         }
381
382         public static boolean isdec(String s) {
383                 for (int i = 0; i < s.length(); i++)
384                         if (!isdec(s.charAt(i)))
385                                 return false;
386                 return true;
387         }
388
389         public static int fromdec(int c) {
390                 if ('0' <= c && c <= '9')
391                         return c - '0';
392                 return -1;
393         }
394
395         public static int int8(int[] bytes, int i) {
396                 return (int) (byte) bytes[i];
397         }
398
399         public static int uint8(int[] bytes, int i) {
400                 return bytes[i];
401         }
402
403         public static int int16(int[] bytes, int i) {
404                 return (int) (short) (bytes[i] + (bytes[i+1] << 8));
405         }
406
407         public static int uint16(int[] bytes, int i) {
408                 return bytes[i] + (bytes[i+1] << 8);
409         }
410
411         public static int uint32(int[] bytes, int i) {
412                 return bytes[i] +
413                         (bytes[i+1] << 8) +
414                         (bytes[i+2] << 16) +
415                         (bytes[i+3] << 24);
416         }
417
418         public static int int32(int[] bytes, int i) {
419                 return (int) uint32(bytes, i);
420         }
421
422         public static final Charset     unicode_set = Charset.forName("UTF-8");
423
424         public static String string(int[] bytes, int s, int l) {
425                 if (s + l > bytes.length) {
426                         if (s > bytes.length) {
427                                 s = bytes.length;
428                                 l = 0;
429                         } else {
430                                 l = bytes.length - s;
431                         }
432                 }
433
434                 int i;
435                 for (i = l - 1; i >= 0; i--)
436                         if (bytes[s+i] != 0)
437                                 break;
438
439                 l = i + 1;
440                 byte[]  b = new byte[l];
441
442                 for (i = 0; i < l; i++)
443                         b[i] = (byte) bytes[s+i];
444                 String n = new String(b, unicode_set);
445                 return n;
446         }
447
448         public static int hexbyte(String s, int i) {
449                 int c0, c1;
450
451                 if (s.length() < i + 2)
452                         throw new NumberFormatException(String.format("invalid hex \"%s\"", s));
453                 c0 = s.charAt(i);
454                 if (!ishex(c0))
455                         throw new NumberFormatException(String.format("invalid hex \"%c\"", c0));
456                 c1 = s.charAt(i+1);
457                 if (!ishex(c1))
458                         throw new NumberFormatException(String.format("invalid hex \"%c\"", c1));
459                 return fromhex(c0) * 16 + fromhex(c1);
460         }
461
462         public static int[] hexbytes(String s) {
463                 int     n;
464                 int[]   r;
465                 int     i;
466
467                 if ((s.length() & 1) != 0)
468                         throw new NumberFormatException(String.format("invalid line \"%s\"", s));
469                 byte[] bytes = s.getBytes(unicode_set);
470                 n = bytes.length / 2;
471                 r = new int[n];
472                 for (i = 0; i < n; i++) {
473                         int h = fromhex(bytes[(i << 1)]);
474                         int l = fromhex(bytes[(i << 1) + 1]);
475                         if (h < 0 || l < 0)
476                                 throw new NumberFormatException(String.format("invalid hex \"%c%c\"",
477                                                                               bytes[(i<<1)], bytes[(i<<1) + 1]));
478                         r[i] = (h << 4) + l;
479                 }
480                 return r;
481         }
482
483         public static int fromdec(String s) throws NumberFormatException {
484                 int c, v = 0;
485                 int sign = 1;
486                 for (int i = 0; i < s.length(); i++) {
487                         c = s.charAt(i);
488                         if (i == 0 && c == '-') {
489                                 sign = -1;
490                         } else if (!isdec(c)) {
491                                 if (i == 0)
492                                         throw new NumberFormatException(String.format("invalid number \"%s\"", s));
493                                 return v;
494                         } else
495                                 v = v * 10 + fromdec(c);
496                 }
497                 return v * sign;
498         }
499
500         public static String gets(FileInputStream s) throws IOException {
501                 int c;
502                 String  line = "";
503
504                 while ((c = s.read()) != -1) {
505                         if (c == '\r')
506                                 continue;
507                         if (c == '\n') {
508                                 return line;
509                         }
510                         line = line + (char) c;
511                 }
512                 return null;
513         }
514
515         public static String replace_extension(String input, String extension) {
516                 int dot = input.lastIndexOf(".");
517                 if (dot > 0)
518                         input = input.substring(0,dot);
519                 return input.concat(extension);
520         }
521
522         public static File replace_extension(File input, String extension) {
523                 return new File(replace_extension(input.getPath(), extension));
524         }
525
526         public static String product_name(int product_id) {
527                 switch (product_id) {
528                 case product_altusmetrum: return "AltusMetrum";
529                 case product_telemetrum: return "TeleMetrum";
530                 case product_teledongle: return "TeleDongle";
531                 case product_teleterra: return "TeleTerra";
532                 case product_telebt: return "TeleBT";
533                 case product_telelaunch: return "TeleLaunch";
534                 case product_telelco: return "TeleLco";
535                 case product_telescience: return "Telescience";
536                 case product_telepyro: return "TelePyro";
537                 case product_telemega: return "TeleMega";
538                 case product_megadongle: return "MegaDongle";
539                 case product_telegps: return "TeleGPS";
540                 case product_easymini: return "EasyMini";
541                 case product_telemini: return "TeleMini";
542                 default: return "unknown";
543                 }
544         }
545
546         public static String ignitor_name(int i) {
547                 return String.format("Ignitor %c", 'A' + i);
548         }
549 }