altoslib, altosui: Restructured state management now does TM eeprom files
[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_1;
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_TIME = 'G';
32         public static final int AO_LOG_GPS_LAT = 'N';
33         public static final int AO_LOG_GPS_LON = 'W';
34         public static final int AO_LOG_GPS_ALT = 'H';
35         public static final int AO_LOG_GPS_SAT = 'V';
36         public static final int AO_LOG_GPS_DATE = 'Y';
37         public static final int AO_LOG_PRESSURE = 'P';
38
39         /* Added for header fields in eeprom files */
40         public static final int AO_LOG_CONFIG_VERSION = 1000;
41         public static final int AO_LOG_MAIN_DEPLOY = 1001;
42         public static final int AO_LOG_APOGEE_DELAY = 1002;
43         public static final int AO_LOG_RADIO_CHANNEL = 1003;
44         public static final int AO_LOG_CALLSIGN = 1004;
45         public static final int AO_LOG_ACCEL_CAL = 1005;
46         public static final int AO_LOG_RADIO_CAL = 1006;
47         public static final int AO_LOG_MAX_FLIGHT_LOG = 1007;
48         public static final int AO_LOG_MANUFACTURER = 2000;
49         public static final int AO_LOG_PRODUCT = 2001;
50         public static final int AO_LOG_SERIAL_NUMBER = 2002;
51         public static final int AO_LOG_LOG_FORMAT = 2003;
52
53         /* Added for header fields in telemega files */
54         public static final int AO_LOG_BARO_RESERVED = 3000;
55         public static final int AO_LOG_BARO_SENS = 3001;
56         public static final int AO_LOG_BARO_OFF = 3002;
57         public static final int AO_LOG_BARO_TCS = 3004;
58         public static final int AO_LOG_BARO_TCO = 3005;
59         public static final int AO_LOG_BARO_TREF = 3006;
60         public static final int AO_LOG_BARO_TEMPSENS = 3007;
61         public static final int AO_LOG_BARO_CRC = 3008;
62
63         public static final int AO_LOG_SOFTWARE_VERSION = 9999;
64
65         /* Added to flag invalid records */
66         public static final int AO_LOG_INVALID = -1;
67
68         /* Flight state numbers and names */
69         public static final int ao_flight_startup = 0;
70         public static final int ao_flight_idle = 1;
71         public static final int ao_flight_pad = 2;
72         public static final int ao_flight_boost = 3;
73         public static final int ao_flight_fast = 4;
74         public static final int ao_flight_coast = 5;
75         public static final int ao_flight_drogue = 6;
76         public static final int ao_flight_main = 7;
77         public static final int ao_flight_landed = 8;
78         public static final int ao_flight_invalid = 9;
79
80         /* USB product IDs */
81         public final static int vendor_altusmetrum = 0xfffe;
82
83         public final static int product_altusmetrum = 0x000a;
84         public final static int product_telemetrum = 0x000b;
85         public final static int product_teledongle = 0x000c;
86         public final static int product_teleterra = 0x000d;
87         public final static int product_telebt = 0x000e;
88         public final static int product_telelaunch = 0x000f;
89         public final static int product_telelco = 0x0010;
90         public final static int product_telescience = 0x0011;
91         public final static int product_telepyro =0x0012;
92         public final static int product_telemega = 0x0023;
93         public final static int product_megadongle = 0x0024;
94         public final static int product_telegps = 0x0025;
95         public final static int product_easymini = 0x0026;
96         public final static int product_telemini = 0x0027;
97         public final static int product_altusmetrum_min = 0x000a;
98         public final static int product_altusmetrum_max = 0x002c;
99
100         public final static int product_any = 0x10000;
101         public final static int product_basestation = 0x10000 + 1;
102         public final static int product_altimeter = 0x10000 + 2;
103
104         /* Bluetooth "identifier" (bluetooth sucks) */
105         public final static String bt_product_telebt = "TeleBT";
106
107         /* Telemetry modes */
108         public static final int ao_telemetry_off = 0;
109         public static final int ao_telemetry_min = 1;
110         public static final int ao_telemetry_standard = 1;
111         public static final int ao_telemetry_0_9 = 2;
112         public static final int ao_telemetry_0_8 = 3;
113         public static final int ao_telemetry_max = 3;
114
115         private static final String[] ao_telemetry_name = {
116                 "Off", "Standard Telemetry", "TeleMetrum v0.9", "TeleMetrum v0.8"
117         };
118
119         public static final String launch_sites_url = "http://www.altusmetrum.org/AltOS/launch-sites.txt";
120
121         public static final int ao_telemetry_standard_len = 32;
122         public static final int ao_telemetry_0_9_len = 95;
123         public static final int ao_telemetry_0_8_len = 94;
124
125         private static final int[] ao_telemetry_len = {
126                 0, 32, 95, 94
127         };
128
129         private static HashMap<String,Integer>  string_to_state = new HashMap<String,Integer>();
130
131         private static boolean map_initialized = false;
132
133         public static void initialize_map()
134         {
135                 string_to_state.put("startup", ao_flight_startup);
136                 string_to_state.put("idle", ao_flight_idle);
137                 string_to_state.put("pad", ao_flight_pad);
138                 string_to_state.put("boost", ao_flight_boost);
139                 string_to_state.put("fast", ao_flight_fast);
140                 string_to_state.put("coast", ao_flight_coast);
141                 string_to_state.put("drogue", ao_flight_drogue);
142                 string_to_state.put("apogee", ao_flight_coast);
143                 string_to_state.put("main", ao_flight_main);
144                 string_to_state.put("landed", ao_flight_landed);
145                 string_to_state.put("invalid", ao_flight_invalid);
146                 map_initialized = true;
147         }
148
149         public static int telemetry_len(int telemetry) {
150                 if (telemetry <= ao_telemetry_max)
151                         return ao_telemetry_len[telemetry];
152                 throw new IllegalArgumentException(String.format("Invalid telemetry %d",
153                                                                  telemetry));
154         }
155
156         public static String telemetry_name(int telemetry) {
157                 if (telemetry <= ao_telemetry_max)
158                         return ao_telemetry_name[telemetry];
159                 throw new IllegalArgumentException(String.format("Invalid telemetry %d",
160                                                                  telemetry));
161         }
162         
163         private static String[] state_to_string = {
164                 "startup",
165                 "idle",
166                 "pad",
167                 "boost",
168                 "fast",
169                 "coast",
170                 "drogue",
171                 "main",
172                 "landed",
173                 "invalid",
174         };
175
176         private static String[] state_to_string_capital = {
177                 "Startup",
178                 "Idle",
179                 "Pad",
180                 "Boost",
181                 "Fast",
182                 "Coast",
183                 "Drogue",
184                 "Main",
185                 "Landed",
186                 "Invalid",
187         };
188
189         public static int state(String state) {
190                 if (!map_initialized)
191                         initialize_map();
192                 if (string_to_state.containsKey(state))
193                         return string_to_state.get(state);
194                 return ao_flight_invalid;
195         }
196
197         public static String state_name(int state) {
198                 if (state < 0 || state_to_string.length <= state)
199                         return "invalid";
200                 return state_to_string[state];
201         }
202
203         public static String state_name_capital(int state) {
204                 if (state < 0 || state_to_string.length <= state)
205                         return "Invalid";
206                 return state_to_string_capital[state];
207         }
208
209         public static final int AO_GPS_VALID = (1 << 4);
210         public static final int AO_GPS_RUNNING = (1 << 5);
211         public static final int AO_GPS_DATE_VALID = (1 << 6);
212         public static final int AO_GPS_NUM_SAT_SHIFT = 0;
213         public static final int AO_GPS_NUM_SAT_MASK = 0xf;
214
215         public static final int AO_LOG_FORMAT_UNKNOWN = 0;
216         public static final int AO_LOG_FORMAT_FULL = 1;
217         public static final int AO_LOG_FORMAT_TINY = 2;
218         public static final int AO_LOG_FORMAT_TELEMETRY = 3;
219         public static final int AO_LOG_FORMAT_TELESCIENCE = 4;
220         public static final int AO_LOG_FORMAT_TELEMEGA = 5;
221         public static final int AO_LOG_FORMAT_EASYMINI = 6;
222         public static final int AO_LOG_FORMAT_TELEMETRUM = 7;
223         public static final int AO_LOG_FORMAT_TELEMINI = 8;
224         public static final int AO_LOG_FORMAT_NONE = 127;
225
226         public static boolean isspace(int c) {
227                 switch (c) {
228                 case ' ':
229                 case '\t':
230                         return true;
231                 }
232                 return false;
233         }
234
235         public static boolean ishex(int c) {
236                 if ('0' <= c && c <= '9')
237                         return true;
238                 if ('a' <= c && c <= 'f')
239                         return true;
240                 if ('A' <= c && c <= 'F')
241                         return true;
242                 return false;
243         }
244
245         public static boolean ishex(String s) {
246                 for (int i = 0; i < s.length(); i++)
247                         if (!ishex(s.charAt(i)))
248                                 return false;
249                 return true;
250         }
251
252         public static int fromhex(int c) {
253                 if ('0' <= c && c <= '9')
254                         return c - '0';
255                 if ('a' <= c && c <= 'f')
256                         return c - 'a' + 10;
257                 if ('A' <= c && c <= 'F')
258                         return c - 'A' + 10;
259                 return -1;
260         }
261
262         public static int fromhex(String s) throws NumberFormatException {
263                 int c, v = 0;
264                 for (int i = 0; i < s.length(); i++) {
265                         c = s.charAt(i);
266                         if (!ishex(c)) {
267                                 if (i == 0)
268                                         throw new NumberFormatException(String.format("invalid hex \"%s\"", s));
269                                 return v;
270                         }
271                         v = v * 16 + fromhex(c);
272                 }
273                 return v;
274         }
275
276         public static boolean isdec(int c) {
277                 if ('0' <= c && c <= '9')
278                         return true;
279                 return false;
280         }
281
282         public static boolean isdec(String s) {
283                 for (int i = 0; i < s.length(); i++)
284                         if (!isdec(s.charAt(i)))
285                                 return false;
286                 return true;
287         }
288
289         public static int fromdec(int c) {
290                 if ('0' <= c && c <= '9')
291                         return c - '0';
292                 return -1;
293         }
294
295         public static int int8(int[] bytes, int i) {
296                 return (int) (byte) bytes[i];
297         }
298
299         public static int uint8(int[] bytes, int i) {
300                 return bytes[i];
301         }
302
303         public static int int16(int[] bytes, int i) {
304                 return (int) (short) (bytes[i] + (bytes[i+1] << 8));
305         }
306
307         public static int uint16(int[] bytes, int i) {
308                 return bytes[i] + (bytes[i+1] << 8);
309         }
310
311         public static int uint32(int[] bytes, int i) {
312                 return bytes[i] +
313                         (bytes[i+1] << 8) +
314                         (bytes[i+2] << 16) +
315                         (bytes[i+3] << 24);
316         }
317
318         public static int int32(int[] bytes, int i) {
319                 return (int) uint32(bytes, i);
320         }
321
322         public static final Charset     unicode_set = Charset.forName("UTF-8");
323
324         public static String string(int[] bytes, int s, int l) {
325                 if (s + l > bytes.length) {
326                         if (s > bytes.length) {
327                                 s = bytes.length;
328                                 l = 0;
329                         } else {
330                                 l = bytes.length - s;
331                         }
332                 }
333
334                 int i;
335                 for (i = l - 1; i >= 0; i--)
336                         if (bytes[s+i] != 0)
337                                 break;
338
339                 l = i + 1;
340                 byte[]  b = new byte[l];
341
342                 for (i = 0; i < l; i++)
343                         b[i] = (byte) bytes[s+i];
344                 String n = new String(b, unicode_set);
345                 return n;
346         }
347
348         public static int hexbyte(String s, int i) {
349                 int c0, c1;
350
351                 if (s.length() < i + 2)
352                         throw new NumberFormatException(String.format("invalid hex \"%s\"", s));
353                 c0 = s.charAt(i);
354                 if (!ishex(c0))
355                         throw new NumberFormatException(String.format("invalid hex \"%c\"", c0));
356                 c1 = s.charAt(i+1);
357                 if (!ishex(c1))
358                         throw new NumberFormatException(String.format("invalid hex \"%c\"", c1));
359                 return fromhex(c0) * 16 + fromhex(c1);
360         }
361
362         public static int[] hexbytes(String s) {
363                 int     n;
364                 int[]   r;
365                 int     i;
366
367                 if ((s.length() & 1) != 0)
368                         throw new NumberFormatException(String.format("invalid line \"%s\"", s));
369                 n = s.length() / 2;
370                 r = new int[n];
371                 for (i = 0; i < n; i++)
372                         r[i] = hexbyte(s, i * 2);
373                 return r;
374         }
375
376         public static int fromdec(String s) throws NumberFormatException {
377                 int c, v = 0;
378                 int sign = 1;
379                 for (int i = 0; i < s.length(); i++) {
380                         c = s.charAt(i);
381                         if (i == 0 && c == '-') {
382                                 sign = -1;
383                         } else if (!isdec(c)) {
384                                 if (i == 0)
385                                         throw new NumberFormatException(String.format("invalid number \"%s\"", s));
386                                 return v;
387                         } else
388                                 v = v * 10 + fromdec(c);
389                 }
390                 return v * sign;
391         }
392
393         public static String gets(FileInputStream s) throws IOException {
394                 int c;
395                 String  line = "";
396
397                 while ((c = s.read()) != -1) {
398                         if (c == '\r')
399                                 continue;
400                         if (c == '\n') {
401                                 return line;
402                         }
403                         line = line + (char) c;
404                 }
405                 return null;
406         }
407
408         public static String replace_extension(String input, String extension) {
409                 int dot = input.lastIndexOf(".");
410                 if (dot > 0)
411                         input = input.substring(0,dot);
412                 return input.concat(extension);
413         }
414
415         public static File replace_extension(File input, String extension) {
416                 return new File(replace_extension(input.getPath(), extension));
417         }
418 }