altos/altosui: Report log format in the version command
[fw/altos] / altosui / Altos.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 altosui;
19
20 import java.awt.*;
21 import java.util.*;
22 import java.text.*;
23 import java.nio.charset.Charset;
24
25 import libaltosJNI.*;
26
27 public class Altos {
28         /* EEProm command letters */
29         static final int AO_LOG_FLIGHT = 'F';
30         static final int AO_LOG_SENSOR = 'A';
31         static final int AO_LOG_TEMP_VOLT = 'T';
32         static final int AO_LOG_DEPLOY = 'D';
33         static final int AO_LOG_STATE = 'S';
34         static final int AO_LOG_GPS_TIME = 'G';
35         static final int AO_LOG_GPS_LAT = 'N';
36         static final int AO_LOG_GPS_LON = 'W';
37         static final int AO_LOG_GPS_ALT = 'H';
38         static final int AO_LOG_GPS_SAT = 'V';
39         static final int AO_LOG_GPS_DATE = 'Y';
40         static final int AO_LOG_PRESSURE = 'P';
41
42         /* Added for header fields in eeprom files */
43         static final int AO_LOG_CONFIG_VERSION = 1000;
44         static final int AO_LOG_MAIN_DEPLOY = 1001;
45         static final int AO_LOG_APOGEE_DELAY = 1002;
46         static final int AO_LOG_RADIO_CHANNEL = 1003;
47         static final int AO_LOG_CALLSIGN = 1004;
48         static final int AO_LOG_ACCEL_CAL = 1005;
49         static final int AO_LOG_RADIO_CAL = 1006;
50         static final int AO_LOG_MAX_FLIGHT_LOG = 1007;
51         static final int AO_LOG_MANUFACTURER = 2000;
52         static final int AO_LOG_PRODUCT = 2001;
53         static final int AO_LOG_SERIAL_NUMBER = 2002;
54         static final int AO_LOG_LOG_FORMAT = 2003;
55         static final int AO_LOG_SOFTWARE_VERSION = 9999;
56
57         /* Added to flag invalid records */
58         static final int AO_LOG_INVALID = -1;
59
60         /* Flight state numbers and names */
61         static final int ao_flight_startup = 0;
62         static final int ao_flight_idle = 1;
63         static final int ao_flight_pad = 2;
64         static final int ao_flight_boost = 3;
65         static final int ao_flight_fast = 4;
66         static final int ao_flight_coast = 5;
67         static final int ao_flight_drogue = 6;
68         static final int ao_flight_main = 7;
69         static final int ao_flight_landed = 8;
70         static final int ao_flight_invalid = 9;
71
72         /* Telemetry modes */
73         static final int ao_telemetry_off = 0;
74         static final int ao_telemetry_min = 1;
75         static final int ao_telemetry_standard = 1;
76         static final int ao_telemetry_0_9 = 2;
77         static final int ao_telemetry_0_8 = 3;
78         static final int ao_telemetry_max = 3;
79
80         static final String[] ao_telemetry_name = {
81                 "Off", "Standard Telemetry", "TeleMetrum v0.9", "TeleMetrum v0.8"
82         };
83
84         static final String launch_sites_url = "http://www.altusmetrum.org/AltOS/launch-sites.txt";
85
86         static final int ao_telemetry_standard_len = 32;
87         static final int ao_telemetry_0_9_len = 95;
88         static final int ao_telemetry_0_8_len = 94;
89
90         static final int[] ao_telemetry_len = {
91                 0, 32, 95, 94
92         };
93
94         static HashMap<String,Integer>  string_to_state = new HashMap<String,Integer>();
95
96         static boolean map_initialized = false;
97
98         static final int tab_elt_pad = 5;
99
100         static final Font label_font = new Font("Dialog", Font.PLAIN, 22);
101         static final Font value_font = new Font("Monospaced", Font.PLAIN, 22);
102         static final Font status_font = new Font("SansSerif", Font.BOLD, 24);
103
104         static final int text_width = 20;
105
106         static void initialize_map()
107         {
108                 string_to_state.put("startup", ao_flight_startup);
109                 string_to_state.put("idle", ao_flight_idle);
110                 string_to_state.put("pad", ao_flight_pad);
111                 string_to_state.put("boost", ao_flight_boost);
112                 string_to_state.put("fast", ao_flight_fast);
113                 string_to_state.put("coast", ao_flight_coast);
114                 string_to_state.put("drogue", ao_flight_drogue);
115                 string_to_state.put("main", ao_flight_main);
116                 string_to_state.put("landed", ao_flight_landed);
117                 string_to_state.put("invalid", ao_flight_invalid);
118                 map_initialized = true;
119         }
120
121         static int telemetry_len(int telemetry) {
122                 if (telemetry <= ao_telemetry_max)
123                         return ao_telemetry_len[telemetry];
124                 throw new IllegalArgumentException(String.format("Invalid telemetry %d",
125                                                                  telemetry));
126         }
127
128         static String telemetry_name(int telemetry) {
129                 if (telemetry <= ao_telemetry_max)
130                         return ao_telemetry_name[telemetry];
131                 throw new IllegalArgumentException(String.format("Invalid telemetry %d",
132                                                                  telemetry));
133         }
134         
135         static String[] state_to_string = {
136                 "startup",
137                 "idle",
138                 "pad",
139                 "boost",
140                 "fast",
141                 "coast",
142                 "drogue",
143                 "main",
144                 "landed",
145                 "invalid",
146         };
147
148         static String[] state_to_string_capital = {
149                 "Startup",
150                 "Idle",
151                 "Pad",
152                 "Boost",
153                 "Fast",
154                 "Coast",
155                 "Drogue",
156                 "Main",
157                 "Landed",
158                 "Invalid",
159         };
160
161         static public int state(String state) {
162                 if (!map_initialized)
163                         initialize_map();
164                 if (string_to_state.containsKey(state))
165                         return string_to_state.get(state);
166                 return ao_flight_invalid;
167         }
168
169         static public String state_name(int state) {
170                 if (state < 0 || state_to_string.length <= state)
171                         return "invalid";
172                 return state_to_string[state];
173         }
174
175         static final int AO_GPS_VALID = (1 << 4);
176         static final int AO_GPS_RUNNING = (1 << 5);
177         static final int AO_GPS_DATE_VALID = (1 << 6);
178         static final int AO_GPS_NUM_SAT_SHIFT = 0;
179         static final int AO_GPS_NUM_SAT_MASK = 0xf;
180
181         static final int AO_LOG_FORMAT_UNKNOWN = 0;
182         static final int AO_LOG_FORMAT_FULL = 1;
183         static final int AO_LOG_FORMAT_TINY = 2;
184         static final int AO_LOG_FORMAT_TELEMETRY = 3;
185         static final int AO_LOG_FORMAT_TELESCIENCE = 4;
186         static final int AO_LOG_FORMAT_NONE = 127;
187
188         static boolean isspace(int c) {
189                 switch (c) {
190                 case ' ':
191                 case '\t':
192                         return true;
193                 }
194                 return false;
195         }
196
197         static boolean ishex(int c) {
198                 if ('0' <= c && c <= '9')
199                         return true;
200                 if ('a' <= c && c <= 'f')
201                         return true;
202                 if ('A' <= c && c <= 'F')
203                         return true;
204                 return false;
205         }
206
207         static boolean ishex(String s) {
208                 for (int i = 0; i < s.length(); i++)
209                         if (!ishex(s.charAt(i)))
210                                 return false;
211                 return true;
212         }
213
214         static int fromhex(int c) {
215                 if ('0' <= c && c <= '9')
216                         return c - '0';
217                 if ('a' <= c && c <= 'f')
218                         return c - 'a' + 10;
219                 if ('A' <= c && c <= 'F')
220                         return c - 'A' + 10;
221                 return -1;
222         }
223
224         static int fromhex(String s) throws NumberFormatException {
225                 int c, v = 0;
226                 for (int i = 0; i < s.length(); i++) {
227                         c = s.charAt(i);
228                         if (!ishex(c)) {
229                                 if (i == 0)
230                                         throw new NumberFormatException(String.format("invalid hex \"%s\"", s));
231                                 return v;
232                         }
233                         v = v * 16 + fromhex(c);
234                 }
235                 return v;
236         }
237
238         static boolean isdec(int c) {
239                 if ('0' <= c && c <= '9')
240                         return true;
241                 return false;
242         }
243
244         static boolean isdec(String s) {
245                 for (int i = 0; i < s.length(); i++)
246                         if (!isdec(s.charAt(i)))
247                                 return false;
248                 return true;
249         }
250
251         static int fromdec(int c) {
252                 if ('0' <= c && c <= '9')
253                         return c - '0';
254                 return -1;
255         }
256
257         static int int8(int[] bytes, int i) {
258                 return (int) (byte) bytes[i];
259         }
260
261         static int uint8(int[] bytes, int i) {
262                 return bytes[i];
263         }
264
265         static int int16(int[] bytes, int i) {
266                 return (int) (short) (bytes[i] + (bytes[i+1] << 8));
267         }
268
269         static int uint16(int[] bytes, int i) {
270                 return bytes[i] + (bytes[i+1] << 8);
271         }
272
273         static int uint32(int[] bytes, int i) {
274                 return bytes[i] +
275                         (bytes[i+1] << 8) +
276                         (bytes[i+2] << 16) +
277                         (bytes[i+3] << 24);
278         }
279
280         static final Charset    unicode_set = Charset.forName("UTF-8");
281
282         static String string(int[] bytes, int s, int l) {
283                 if (s + l > bytes.length) {
284                         if (s > bytes.length) {
285                                 s = bytes.length;
286                                 l = 0;
287                         } else {
288                                 l = bytes.length - s;
289                         }
290                 }
291
292                 int i;
293                 for (i = l - 1; i >= 0; i--)
294                         if (bytes[s+i] != 0)
295                                 break;
296
297                 l = i + 1;
298                 byte[]  b = new byte[l];
299
300                 for (i = 0; i < l; i++)
301                         b[i] = (byte) bytes[s+i];
302                 String n = new String(b, unicode_set);
303                 return n;
304         }
305
306         static int hexbyte(String s, int i) {
307                 int c0, c1;
308
309                 if (s.length() < i + 2)
310                         throw new NumberFormatException(String.format("invalid hex \"%s\"", s));
311                 c0 = s.charAt(i);
312                 if (!Altos.ishex(c0))
313                         throw new NumberFormatException(String.format("invalid hex \"%c\"", c0));
314                 c1 = s.charAt(i+1);
315                 if (!Altos.ishex(c1))
316                         throw new NumberFormatException(String.format("invalid hex \"%c\"", c1));
317                 return Altos.fromhex(c0) * 16 + Altos.fromhex(c1);
318         }
319
320         static int[] hexbytes(String s) {
321                 int     n;
322                 int[]   r;
323                 int     i;
324
325                 if ((s.length() & 1) != 0)
326                         throw new NumberFormatException(String.format("invalid line \"%s\"", s));
327                 n = s.length() / 2;
328                 r = new int[n];
329                 for (i = 0; i < n; i++)
330                         r[i] = Altos.hexbyte(s, i * 2);
331                 return r;
332         }
333
334         static int fromdec(String s) throws NumberFormatException {
335                 int c, v = 0;
336                 int sign = 1;
337                 for (int i = 0; i < s.length(); i++) {
338                         c = s.charAt(i);
339                         if (i == 0 && c == '-') {
340                                 sign = -1;
341                         } else if (!isdec(c)) {
342                                 if (i == 0)
343                                         throw new NumberFormatException(String.format("invalid number \"%s\"", s));
344                                 return v;
345                         } else
346                                 v = v * 10 + fromdec(c);
347                 }
348                 return v * sign;
349         }
350
351         static String replace_extension(String input, String extension) {
352                 int dot = input.lastIndexOf(".");
353                 if (dot > 0)
354                         input = input.substring(0,dot);
355                 return input.concat(extension);
356         }
357
358         static public boolean initialized = false;
359         static public boolean loaded_library = false;
360
361         public static boolean load_library() {
362                 if (!initialized) {
363                         try {
364                                 System.loadLibrary("altos");
365                                 libaltos.altos_init();
366                                 loaded_library = true;
367                         } catch (UnsatisfiedLinkError e) {
368                                 loaded_library = false;
369                         }
370                         initialized = true;
371                 }
372                 return loaded_library;
373         }
374
375         static int usb_vendor_altusmetrum() {
376                 load_library();
377                 return 0xfffe;
378         }
379
380         static int usb_product_altusmetrum() {
381                 load_library();
382                 return 0x000a;
383         }
384
385         static int usb_product_altusmetrum_min() {
386                 load_library();
387                 return 0x000a;
388         }
389
390         static int usb_product_altusmetrum_max() {
391                 load_library();
392                 return 0x0013;
393         }
394
395         static int usb_product_telemetrum() {
396                 load_library();
397                 return 0x000b;
398         }
399
400         static int usb_product_teledongle() {
401                 load_library();
402                 return 0x000c;
403         }
404
405         static int usb_product_teleterra() {
406                 load_library();
407                 return 0x000d;
408         }
409
410         static int usb_product_telebt() {
411                 load_library();
412                 return 0x000e;
413         }
414
415         static int usb_product_telelaunch() {
416                 load_library();
417                 return 0x000f;
418         }
419
420         static int usb_product_telelco() {
421                 load_library();
422                 return 0x0010;
423         }
424
425         static int usb_product_telescience() {
426                 load_library();
427                 return 0x0011;
428         }
429
430         static int usb_product_telepyro() {
431                 load_library();
432                 return 0x0012;
433         }
434
435         public final static int vendor_altusmetrum = usb_vendor_altusmetrum();
436         public final static int product_altusmetrum = usb_product_altusmetrum();
437         public final static int product_telemetrum = usb_product_telemetrum();
438         public final static int product_teledongle = usb_product_teledongle();
439         public final static int product_teleterra = usb_product_teleterra();
440         public final static int product_telebt = usb_product_telebt();
441         public final static int product_telelaunch = usb_product_telelaunch();
442         public final static int product_tele10 = usb_product_telelco();
443         public final static int product_telescience = usb_product_telescience();
444         public final static int product_telepyro = usb_product_telepyro();
445         public final static int product_altusmetrum_min = usb_product_altusmetrum_min();
446         public final static int product_altusmetrum_max = usb_product_altusmetrum_max();
447
448         public final static int product_any = 0x10000;
449         public final static int product_basestation = 0x10000 + 1;
450
451         static String bt_product_telebt() {
452                 load_library();
453                 return "TeleBT";
454         }
455
456         public final static String bt_product_telebt = bt_product_telebt();
457
458 //      public static AltosBTKnown bt_known = new AltosBTKnown();
459 }