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