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