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