8d383f1219498def39161c055dbe7c4e5b530073
[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;
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 megametrum 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_megametrum = 0x0023;
93         public final static int product_megadongle = 0x0024;
94         public final static int product_altusmetrum_min = 0x000a;
95         public final static int product_altusmetrum_max = 0x0024;
96
97         public final static int product_any = 0x10000;
98         public final static int product_basestation = 0x10000 + 1;
99         public final static int product_altimeter = 0x10000 + 2;
100
101         /* Bluetooth "identifier" (bluetooth sucks) */
102         public final static String bt_product_telebt = "TeleBT";
103
104         /* Telemetry modes */
105         public static final int ao_telemetry_off = 0;
106         public static final int ao_telemetry_min = 1;
107         public static final int ao_telemetry_standard = 1;
108         public static final int ao_telemetry_0_9 = 2;
109         public static final int ao_telemetry_0_8 = 3;
110         public static final int ao_telemetry_max = 3;
111
112         public static final String[] ao_telemetry_name = {
113                 "Off", "Standard Telemetry", "TeleMetrum v0.9", "TeleMetrum v0.8"
114         };
115
116         public static final String launch_sites_url = "http://www.altusmetrum.org/AltOS/launch-sites.txt";
117
118         public static final int ao_telemetry_standard_len = 32;
119         public static final int ao_telemetry_0_9_len = 95;
120         public static final int ao_telemetry_0_8_len = 94;
121
122         public static final int[] ao_telemetry_len = {
123                 0, 32, 95, 94
124         };
125
126         public static HashMap<String,Integer>   string_to_state = new HashMap<String,Integer>();
127
128         public static boolean map_initialized = false;
129
130         public static void initialize_map()
131         {
132                 string_to_state.put("startup", ao_flight_startup);
133                 string_to_state.put("idle", ao_flight_idle);
134                 string_to_state.put("pad", ao_flight_pad);
135                 string_to_state.put("boost", ao_flight_boost);
136                 string_to_state.put("fast", ao_flight_fast);
137                 string_to_state.put("coast", ao_flight_coast);
138                 string_to_state.put("drogue", ao_flight_drogue);
139                 string_to_state.put("apogee", ao_flight_coast);
140                 string_to_state.put("main", ao_flight_main);
141                 string_to_state.put("landed", ao_flight_landed);
142                 string_to_state.put("invalid", ao_flight_invalid);
143                 map_initialized = true;
144         }
145
146         public static int telemetry_len(int telemetry) {
147                 if (telemetry <= ao_telemetry_max)
148                         return ao_telemetry_len[telemetry];
149                 throw new IllegalArgumentException(String.format("Invalid telemetry %d",
150                                                                  telemetry));
151         }
152
153         public static String telemetry_name(int telemetry) {
154                 if (telemetry <= ao_telemetry_max)
155                         return ao_telemetry_name[telemetry];
156                 throw new IllegalArgumentException(String.format("Invalid telemetry %d",
157                                                                  telemetry));
158         }
159         
160         public static String[] state_to_string = {
161                 "startup",
162                 "idle",
163                 "pad",
164                 "boost",
165                 "fast",
166                 "coast",
167                 "drogue",
168                 "main",
169                 "landed",
170                 "invalid",
171         };
172
173         public static String[] state_to_string_capital = {
174                 "Startup",
175                 "Idle",
176                 "Pad",
177                 "Boost",
178                 "Fast",
179                 "Coast",
180                 "Drogue",
181                 "Main",
182                 "Landed",
183                 "Invalid",
184         };
185
186         public static int state(String state) {
187                 if (!map_initialized)
188                         initialize_map();
189                 if (string_to_state.containsKey(state))
190                         return string_to_state.get(state);
191                 return ao_flight_invalid;
192         }
193
194         public static String state_name(int state) {
195                 if (state < 0 || state_to_string.length <= state)
196                         return "invalid";
197                 return state_to_string[state];
198         }
199
200         public static final int AO_GPS_VALID = (1 << 4);
201         public static final int AO_GPS_RUNNING = (1 << 5);
202         public static final int AO_GPS_DATE_VALID = (1 << 6);
203         public static final int AO_GPS_NUM_SAT_SHIFT = 0;
204         public static final int AO_GPS_NUM_SAT_MASK = 0xf;
205
206         public static final int AO_LOG_FORMAT_UNKNOWN = 0;
207         public static final int AO_LOG_FORMAT_FULL = 1;
208         public static final int AO_LOG_FORMAT_TINY = 2;
209         public static final int AO_LOG_FORMAT_TELEMETRY = 3;
210         public static final int AO_LOG_FORMAT_TELESCIENCE = 4;
211         public static final int AO_LOG_FORMAT_MEGAMETRUM = 5;
212         public static final int AO_LOG_FORMAT_NONE = 127;
213
214         public static boolean isspace(int c) {
215                 switch (c) {
216                 case ' ':
217                 case '\t':
218                         return true;
219                 }
220                 return false;
221         }
222
223         public static boolean ishex(int c) {
224                 if ('0' <= c && c <= '9')
225                         return true;
226                 if ('a' <= c && c <= 'f')
227                         return true;
228                 if ('A' <= c && c <= 'F')
229                         return true;
230                 return false;
231         }
232
233         public static boolean ishex(String s) {
234                 for (int i = 0; i < s.length(); i++)
235                         if (!ishex(s.charAt(i)))
236                                 return false;
237                 return true;
238         }
239
240         public static int fromhex(int c) {
241                 if ('0' <= c && c <= '9')
242                         return c - '0';
243                 if ('a' <= c && c <= 'f')
244                         return c - 'a' + 10;
245                 if ('A' <= c && c <= 'F')
246                         return c - 'A' + 10;
247                 return -1;
248         }
249
250         public static int fromhex(String s) throws NumberFormatException {
251                 int c, v = 0;
252                 for (int i = 0; i < s.length(); i++) {
253                         c = s.charAt(i);
254                         if (!ishex(c)) {
255                                 if (i == 0)
256                                         throw new NumberFormatException(String.format("invalid hex \"%s\"", s));
257                                 return v;
258                         }
259                         v = v * 16 + fromhex(c);
260                 }
261                 return v;
262         }
263
264         public static boolean isdec(int c) {
265                 if ('0' <= c && c <= '9')
266                         return true;
267                 return false;
268         }
269
270         public static boolean isdec(String s) {
271                 for (int i = 0; i < s.length(); i++)
272                         if (!isdec(s.charAt(i)))
273                                 return false;
274                 return true;
275         }
276
277         public static int fromdec(int c) {
278                 if ('0' <= c && c <= '9')
279                         return c - '0';
280                 return -1;
281         }
282
283         public static int int8(int[] bytes, int i) {
284                 return (int) (byte) bytes[i];
285         }
286
287         public static int uint8(int[] bytes, int i) {
288                 return bytes[i];
289         }
290
291         public static int int16(int[] bytes, int i) {
292                 return (int) (short) (bytes[i] + (bytes[i+1] << 8));
293         }
294
295         public static int uint16(int[] bytes, int i) {
296                 return bytes[i] + (bytes[i+1] << 8);
297         }
298
299         public static int uint32(int[] bytes, int i) {
300                 return bytes[i] +
301                         (bytes[i+1] << 8) +
302                         (bytes[i+2] << 16) +
303                         (bytes[i+3] << 24);
304         }
305
306         public static int int32(int[] bytes, int i) {
307                 return (int) uint32(bytes, i);
308         }
309
310         public static final Charset     unicode_set = Charset.forName("UTF-8");
311
312         public static String string(int[] bytes, int s, int l) {
313                 if (s + l > bytes.length) {
314                         if (s > bytes.length) {
315                                 s = bytes.length;
316                                 l = 0;
317                         } else {
318                                 l = bytes.length - s;
319                         }
320                 }
321
322                 int i;
323                 for (i = l - 1; i >= 0; i--)
324                         if (bytes[s+i] != 0)
325                                 break;
326
327                 l = i + 1;
328                 byte[]  b = new byte[l];
329
330                 for (i = 0; i < l; i++)
331                         b[i] = (byte) bytes[s+i];
332                 String n = new String(b, unicode_set);
333                 return n;
334         }
335
336         public static int hexbyte(String s, int i) {
337                 int c0, c1;
338
339                 if (s.length() < i + 2)
340                         throw new NumberFormatException(String.format("invalid hex \"%s\"", s));
341                 c0 = s.charAt(i);
342                 if (!ishex(c0))
343                         throw new NumberFormatException(String.format("invalid hex \"%c\"", c0));
344                 c1 = s.charAt(i+1);
345                 if (!ishex(c1))
346                         throw new NumberFormatException(String.format("invalid hex \"%c\"", c1));
347                 return fromhex(c0) * 16 + fromhex(c1);
348         }
349
350         public static int[] hexbytes(String s) {
351                 int     n;
352                 int[]   r;
353                 int     i;
354
355                 if ((s.length() & 1) != 0)
356                         throw new NumberFormatException(String.format("invalid line \"%s\"", s));
357                 n = s.length() / 2;
358                 r = new int[n];
359                 for (i = 0; i < n; i++)
360                         r[i] = hexbyte(s, i * 2);
361                 return r;
362         }
363
364         public static int fromdec(String s) throws NumberFormatException {
365                 int c, v = 0;
366                 int sign = 1;
367                 for (int i = 0; i < s.length(); i++) {
368                         c = s.charAt(i);
369                         if (i == 0 && c == '-') {
370                                 sign = -1;
371                         } else if (!isdec(c)) {
372                                 if (i == 0)
373                                         throw new NumberFormatException(String.format("invalid number \"%s\"", s));
374                                 return v;
375                         } else
376                                 v = v * 10 + fromdec(c);
377                 }
378                 return v * sign;
379         }
380
381         public static String gets(FileInputStream s) throws IOException {
382                 int c;
383                 String  line = "";
384
385                 while ((c = s.read()) != -1) {
386                         if (c == '\r')
387                                 continue;
388                         if (c == '\n') {
389                                 return line;
390                         }
391                         line = line + (char) c;
392                 }
393                 return null;
394         }
395
396         public static String replace_extension(String input, String extension) {
397                 int dot = input.lastIndexOf(".");
398                 if (dot > 0)
399                         input = input.substring(0,dot);
400                 return input.concat(extension);
401         }
402 }