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