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