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