altosui/telegps: Expose configurable APRS SSID
[fw/altos] / altoslib / AltosConfigData.java
1 /*
2  * Copyright © 2011 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.text.*;
22 import java.util.concurrent.*;
23
24 public class AltosConfigData implements Iterable<String> {
25
26         /* Version information */
27         public String   manufacturer;
28         public String   product;
29         public int      serial;
30         public int      flight;
31         public int      log_format;
32         public int      log_space;
33         public String   version;
34
35         /* Strings returned */
36         public LinkedList<String>       lines;
37
38         /* Config information */
39         /* HAS_FLIGHT*/
40         public int      main_deploy;
41         public int      apogee_delay;
42         public int      apogee_lockout;
43
44         /* HAS_RADIO */
45         public int      radio_frequency;
46         public String   callsign;
47         public int      radio_enable;
48         public int      radio_calibration;
49         /* Old HAS_RADIO values */
50         public int      radio_channel;
51         public int      radio_setting;
52
53         /* HAS_ACCEL */
54         public int      accel_cal_plus, accel_cal_minus;
55         public int      pad_orientation;
56
57         /* HAS_LOG */
58         public int      flight_log_max;
59
60         /* HAS_IGNITE */
61         public int      ignite_mode;
62
63         /* HAS_AES */
64         public String   aes_key;
65
66         /* AO_PYRO_NUM */
67         public AltosPyro[]      pyros;
68         public int              npyro;
69         public int              pyro;
70         public double           pyro_firing_time;
71
72         /* HAS_APRS */
73         public int              aprs_interval;
74         public int              aprs_ssid;
75
76         /* HAS_BEEP */
77         public int              beep;
78
79         /* Storage info replies */
80         public int      storage_size;
81         public int      storage_erase_unit;
82
83         /* Log listing replies */
84         public int      stored_flight;
85
86         /* HAS_TRACKER */
87         public int      tracker_motion;
88         public int      tracker_interval;
89
90         public static String get_string(String line, String label) throws  ParseException {
91                 if (line.startsWith(label)) {
92                         String  quoted = line.substring(label.length()).trim();
93
94                         if (quoted.startsWith("\""))
95                                 quoted = quoted.substring(1);
96                         if (quoted.endsWith("\""))
97                                 quoted = quoted.substring(0,quoted.length()-1);
98                         return quoted;
99                 }
100                 throw new ParseException("mismatch", 0);
101         }
102
103         public static int get_int(String line, String label) throws NumberFormatException, ParseException {
104                 if (line.startsWith(label)) {
105                         String tail = line.substring(label.length()).trim();
106                         String[] tokens = tail.split("\\s+");
107                         if (tokens.length > 0)
108                                 return  Integer.parseInt(tokens[0]);
109                 }
110                 throw new ParseException("mismatch", 0);
111         }
112
113         public static int[] get_values(String line, String label) throws NumberFormatException, ParseException {
114                 if (line.startsWith(label)) {
115                         String tail = line.substring(label.length()).trim();
116                         String[] tokens = tail.split("\\s+");
117                         if (tokens.length > 1) {
118                                 int[]   values = new int[2];
119                                 values[0] = Integer.parseInt(tokens[0]);
120                                 values[1] = Integer.parseInt(tokens[1]);
121                                 return values;
122                         }
123                 }
124                 throw new ParseException("mismatch", 0);
125         }
126
127         public Iterator<String> iterator() {
128                 return lines.iterator();
129         }
130
131         public int log_space() {
132                 if (log_space > 0)
133                         return log_space;
134
135                 if (storage_size > 0) {
136                         int     space = storage_size;
137
138                         if (storage_erase_unit > 0 && use_flash_for_config())
139                                 space -= storage_erase_unit;
140
141                         if (space > 0)
142                                 return space;
143                 }
144                 return 0;
145         }
146
147         public int log_available() {
148                 switch (log_format) {
149                 case AltosLib.AO_LOG_FORMAT_TINY:
150                         if (stored_flight == 0)
151                                 return 1;
152                         return 0;
153                 case AltosLib.AO_LOG_FORMAT_TELEMETRY:
154                 case AltosLib.AO_LOG_FORMAT_TELESCIENCE:
155                         return 1;
156                 default:
157                         if (flight_log_max <= 0)
158                                 return 1;
159                         int     log_max = flight_log_max * 1024;
160                         int     log_space = log_space();
161                         int     log_used;
162
163                         if (stored_flight <= 0)
164                                 log_used = 0;
165                         else
166                                 log_used = stored_flight * log_max;
167                         int     log_avail;
168
169                         if (log_used >= log_space)
170                                 log_avail = 0;
171                         else
172                                 log_avail = (log_space - log_used) / log_max;
173
174                         return log_avail;
175                 }
176         }
177
178         public boolean has_monitor_battery() {
179                 if (product.startsWith("TeleBT"))
180                         return true;
181                 return false;
182         }
183
184         int[] parse_version(String v) {
185                 String[] parts = v.split("\\.");
186                 int r[] = new int[parts.length];
187
188                 for (int i = 0; i < parts.length; i++) {
189                         try {
190                                 r[i] = AltosLib.fromdec(parts[i]);
191                         } catch (NumberFormatException n) {
192                                 r[i] = 0;
193                         }
194                 }
195
196                 return r;
197         }
198
199         public int compare_version(String other) {
200                 int[]   me = parse_version(version);
201                 int[]   them = parse_version(other);
202
203                 int     l = Math.min(me.length, them.length);
204
205                 for (int i = 0; i < l; i++) {
206                         int     d = me[i] - them[i];
207                         if (d != 0)
208                                 return d;
209                 }
210                 if (me.length > l)
211                         return 1;
212                 if (them.length > l)
213                         return -1;
214                 return 0;
215         }
216
217         public void reset() {
218                 lines = new LinkedList<String>();
219
220                 manufacturer = "unknown";
221                 product = "unknown";
222                 serial = 0;
223                 flight = 0;
224                 log_format = AltosLib.AO_LOG_FORMAT_UNKNOWN;
225                 log_space = -1;
226                 version = "unknown";
227
228                 main_deploy = -1;
229                 apogee_delay = -1;
230                 apogee_lockout = -1;
231
232                 radio_frequency = -1;
233                 callsign = null;
234                 radio_enable = -1;
235                 radio_calibration = -1;
236                 radio_channel = -1;
237                 radio_setting = -1;
238
239                 accel_cal_plus = -1;
240                 accel_cal_minus = -1;
241                 pad_orientation = -1;
242
243                 flight_log_max = -1;
244                 ignite_mode = -1;
245
246                 aes_key = "";
247
248                 pyro = 0;
249                 npyro = 0;
250                 pyros = null;
251                 pyro_firing_time = -1;
252
253                 aprs_interval = -1;
254                 aprs_ssid = -1;
255
256                 beep = -1;
257
258                 tracker_motion = -1;
259                 tracker_interval = -1;
260
261                 storage_size = -1;
262                 storage_erase_unit = -1;
263                 stored_flight = 0;
264         }
265
266         public void parse_line(String line) {
267                 lines.add(line);
268                 /* Version replies */
269                 try { manufacturer = get_string(line, "manufacturer"); } catch (Exception e) {}
270                 try { product = get_string(line, "product"); } catch (Exception e) {}
271                 try { serial = get_int(line, "serial-number"); } catch (Exception e) {}
272                 try { flight = get_int(line, "current-flight"); } catch (Exception e) {}
273                 try { log_format = get_int(line, "log-format"); } catch (Exception e) {}
274                 try { log_space = get_int(line, "log-space"); } catch (Exception e) {}
275                 try { version = get_string(line, "software-version"); } catch (Exception e) {}
276
277                 /* Version also contains MS5607 info, which we ignore here */
278
279                 /* Config show replies */
280
281                 /* HAS_FLIGHT */
282                 try { main_deploy = get_int(line, "Main deploy:"); } catch (Exception e) {}
283                 try { apogee_delay = get_int(line, "Apogee delay:"); } catch (Exception e) {}
284                 try { apogee_lockout = get_int(line, "Apogee lockout:"); } catch (Exception e) {}
285
286                 /* HAS_RADIO */
287                 try {
288                         radio_frequency = get_int(line, "Frequency:");
289                         if (radio_frequency < 0)
290                                 radio_frequency = 434550;
291                 } catch (Exception e) {}
292                 try { callsign = get_string(line, "Callsign:"); } catch (Exception e) {}
293                 try { radio_enable = get_int(line, "Radio enable:"); } catch (Exception e) {}
294                 try { radio_calibration = get_int(line, "Radio cal:"); } catch (Exception e) {}
295
296                 /* Old HAS_RADIO values */
297                 try { radio_channel = get_int(line, "Radio channel:"); } catch (Exception e) {}
298                 try { radio_setting = get_int(line, "Radio setting:"); } catch (Exception e) {}
299
300                 /* HAS_ACCEL */
301                 try {
302                         if (line.startsWith("Accel cal")) {
303                                 String[] bits = line.split("\\s+");
304                                 if (bits.length >= 6) {
305                                         accel_cal_plus = Integer.parseInt(bits[3]);
306                                         accel_cal_minus = Integer.parseInt(bits[5]);
307                                 }
308                         }
309                 } catch (Exception e) {}
310                 try { pad_orientation = get_int(line, "Pad orientation:"); } catch (Exception e) {}
311
312                 /* HAS_LOG */
313                 try { flight_log_max = get_int(line, "Max flight log:"); } catch (Exception e) {}
314
315                 /* HAS_IGNITE */
316                 try { ignite_mode = get_int(line, "Ignite mode:"); } catch (Exception e) {}
317
318                 /* HAS_AES */
319                 try { aes_key = get_string(line, "AES key:"); } catch (Exception e) {}
320
321                 /* AO_PYRO_NUM */
322                 try {
323                         npyro = get_int(line, "Pyro-count:");
324                         pyros = new AltosPyro[npyro];
325                         pyro = 0;
326                 } catch (Exception e) {}
327                 if (npyro > 0) {
328                         try {
329                                 AltosPyro p = new AltosPyro(pyro, line);
330                                 if (pyro < npyro)
331                                         pyros[pyro++] = p;
332                         } catch (Exception e) {}
333                 }
334                 try { pyro_firing_time = get_int(line, "Pyro time:") / 100.0; } catch (Exception e) {}
335
336                 /* HAS_APRS */
337                 try { aprs_interval = get_int(line, "APRS interval:"); } catch (Exception e) {}
338                 try { aprs_ssid = get_int(line, "APRS SSID:"); } catch (Exception e) {}
339
340                 /* HAS_BEEP */
341                 try { beep = get_int(line, "Beeper setting:"); } catch (Exception e) {}
342
343                 /* HAS_TRACKER */
344                 try {
345                         int[] values = get_values(line, "Tracker setting:");
346                         tracker_motion = values[0];
347                         tracker_interval = values[1];
348                 } catch (Exception e) {}
349
350                 /* Storage info replies */
351                 try { storage_size = get_int(line, "Storage size:"); } catch (Exception e) {}
352                 try { storage_erase_unit = get_int(line, "Storage erase unit:"); } catch (Exception e) {}
353
354                 /* Log listing replies */
355                 try { get_int(line, "flight"); stored_flight++; }  catch (Exception e) {}
356         }
357
358         public AltosConfigData() {
359                 reset();
360         }
361
362         private void read_link(AltosLink link, String finished) throws InterruptedException, TimeoutException {
363                 for (;;) {
364                         String line = link.get_reply();
365                         if (line == null)
366                                 throw new TimeoutException();
367                         if (line.contains("Syntax error"))
368                                 continue;
369                         this.parse_line(line);
370
371                         /* signals the end of the version info */
372                         if (line.startsWith(finished))
373                                 break;
374                 }
375         }
376
377         public boolean has_frequency() {
378                 return radio_frequency >= 0 || radio_setting >= 0 || radio_channel >= 0;
379         }
380
381         public void set_frequency(double freq) {
382                 int     frequency = radio_frequency;
383                 int     setting = radio_setting;
384
385                 if (frequency > 0) {
386                         radio_frequency = (int) Math.floor (freq * 1000 + 0.5);
387                         radio_channel = -1;
388                 } else if (setting > 0) {
389                         radio_setting =AltosConvert.radio_frequency_to_setting(freq,
390                                                                                     radio_calibration);
391                         radio_channel = -1;
392                 } else {
393                         radio_channel = AltosConvert.radio_frequency_to_channel(freq);
394                 }
395         }
396
397         public double frequency() {
398                 int     channel = radio_channel;
399                 int     setting = radio_setting;
400
401                 if (radio_frequency < 0 && channel < 0 && setting < 0)
402                         return -1;
403
404                 if (channel < 0)
405                         channel = 0;
406                 if (setting < 0)
407                         setting = 0;
408
409                 return AltosConvert.radio_to_frequency(radio_frequency,
410                                                        setting,
411                                                        radio_calibration,
412                                                        channel);
413         }
414
415         boolean use_flash_for_config() {
416                 if (product.startsWith("TeleMega"))
417                         return false;
418                 if (product.startsWith("TeleMetrum-v2"))
419                         return false;
420                 return true;
421         }
422
423
424         public void get_values(AltosConfigValues source) throws AltosConfigDataException {
425
426                 /* HAS_FLIGHT */
427                 if (main_deploy >= 0)
428                         main_deploy = source.main_deploy();
429                 if (apogee_delay >= 0)
430                         apogee_delay = source.apogee_delay();
431                 if (apogee_lockout >= 0)
432                         apogee_lockout = source.apogee_lockout();
433
434                 /* HAS_RADIO */
435                 if (has_frequency())
436                         set_frequency(source.radio_frequency());
437                 if (radio_enable >= 0)
438                         radio_enable = source.radio_enable();
439                 if (callsign != null)
440                         callsign = source.callsign();
441                 if (radio_calibration >= 0)
442                         radio_calibration = source.radio_calibration();
443
444                 /* HAS_ACCEL */
445                 if (pad_orientation >= 0)
446                         pad_orientation = source.pad_orientation();
447
448                 /* HAS_LOG */
449                 if (flight_log_max >= 0)
450                         flight_log_max = source.flight_log_max();
451
452                 /* HAS_IGNITE */
453                 if (ignite_mode >= 0)
454                         ignite_mode = source.ignite_mode();
455
456                 /* AO_PYRO_NUM */
457                 if (npyro > 0)
458                         pyros = source.pyros();
459                 if (pyro_firing_time >= 0)
460                         pyro_firing_time = source.pyro_firing_time();
461
462                 /* HAS_APRS */
463                 if (aprs_interval >= 0)
464                         aprs_interval = source.aprs_interval();
465                 if (aprs_ssid >= 0)
466                         aprs_ssid = source.aprs_ssid();
467
468                 /* HAS_BEEP */
469                 if (beep >= 0)
470                         beep = source.beep();
471                 /* HAS_TRACKER */
472                 if (tracker_motion >= 0)
473                         tracker_motion = source.tracker_motion();
474                 if (tracker_interval >= 0)
475                         tracker_interval = source.tracker_interval();
476         }
477
478         public void set_values(AltosConfigValues dest) {
479                 dest.set_serial(serial);
480                 dest.set_product(product);
481                 dest.set_version(version);
482                 dest.set_main_deploy(main_deploy);
483                 dest.set_apogee_delay(apogee_delay);
484                 dest.set_apogee_lockout(apogee_lockout);
485                 dest.set_radio_calibration(radio_calibration);
486                 dest.set_radio_frequency(frequency());
487                 boolean max_enabled = true;
488
489                 if (log_space() == 0)
490                         max_enabled = false;
491
492                 switch (log_format) {
493                 case AltosLib.AO_LOG_FORMAT_TINY:
494                         max_enabled = false;
495                         break;
496                 default:
497                         if (stored_flight > 0)
498                                 max_enabled = false;
499                         break;
500                 }
501
502                 dest.set_flight_log_max_enabled(max_enabled);
503                 dest.set_radio_enable(radio_enable);
504                 dest.set_flight_log_max_limit(log_space() / 1024);
505                 dest.set_flight_log_max(flight_log_max);
506                 dest.set_ignite_mode(ignite_mode);
507                 dest.set_pad_orientation(pad_orientation);
508                 dest.set_callsign(callsign);
509                 if (npyro > 0)
510                         dest.set_pyros(pyros);
511                 else
512                         dest.set_pyros(null);
513                 dest.set_pyro_firing_time(pyro_firing_time);
514                 dest.set_aprs_interval(aprs_interval);
515                 dest.set_aprs_ssid(aprs_ssid);
516                 dest.set_beep(beep);
517                 dest.set_tracker_motion(tracker_motion);
518                 dest.set_tracker_interval(tracker_interval);
519         }
520
521         public void save(AltosLink link, boolean remote) throws InterruptedException, TimeoutException {
522
523                 /* HAS_FLIGHT */
524                 if (main_deploy >= 0)
525                         link.printf("c m %d\n", main_deploy);
526                 if (apogee_delay >= 0)
527                         link.printf("c d %d\n", apogee_delay);
528                 if (apogee_lockout >= 0)
529                         link.printf("c L %d\n", apogee_lockout);
530
531                 /* Don't mess with radio calibration when remote */
532                 if (radio_calibration > 0 && !remote)
533                         link.printf("c f %d\n", radio_calibration);
534
535                 /* HAS_RADIO */
536                 if (has_frequency()) {
537                         boolean has_frequency = radio_frequency >= 0;
538                         boolean has_setting = radio_setting > 0;
539                         double frequency = frequency();
540                         link.set_radio_frequency(frequency,
541                                                         has_frequency,
542                                                         has_setting,
543                                                         radio_calibration);
544                         /* When remote, reset the dongle frequency at the same time */
545                         if (remote) {
546                                 link.stop_remote();
547                                 link.set_radio_frequency(frequency);
548                                 link.start_remote();
549                         }
550                 }
551
552                 if (callsign != null)
553                         link.printf("c c %s\n", callsign);
554                 if (radio_enable >= 0)
555                         link.printf("c e %d\n", radio_enable);
556
557                 /* HAS_ACCEL */
558                 /* UI doesn't support accel cal */
559                 if (pad_orientation >= 0)
560                         link.printf("c o %d\n", pad_orientation);
561
562                 /* HAS_LOG */
563                 if (flight_log_max != 0)
564                         link.printf("c l %d\n", flight_log_max);
565
566                 /* HAS_IGNITE */
567                 if (ignite_mode >= 0)
568                         link.printf("c i %d\n", ignite_mode);
569
570                 /* HAS_AES */
571                 /* UI doesn't support AES key config */
572
573                 /* AO_PYRO_NUM */
574                 if (npyro > 0) {
575                         for (int p = 0; p < pyros.length; p++) {
576                                 link.printf("c P %s\n",
577                                                    pyros[p].toString());
578                         }
579                 }
580                 if (pyro_firing_time >= 0)
581                         link.printf("c I %d\n", (int) (pyro_firing_time * 100.0 + 0.5));
582
583                 /* HAS_APRS */
584                 if (aprs_interval >= 0)
585                         link.printf("c A %d\n", aprs_interval);
586                 if (aprs_ssid >= 0)
587                         link.printf("c S %d\n", aprs_ssid);
588
589                 /* HAS_BEEP */
590                 if (beep >= 0)
591                         link.printf("c b %d\n", beep);
592
593                 /* HAS_TRACKER */
594                 if (tracker_motion >= 0 && tracker_interval >= 0)
595                         link.printf("c t %d %d\n", tracker_motion, tracker_interval);
596
597                 link.printf("c w\n");
598                 link.flush_output();
599         }
600
601         public AltosConfigData(AltosLink link) throws InterruptedException, TimeoutException {
602                 reset();
603                 link.printf("c s\nf\nv\n");
604                 read_link(link, "software-version");
605                 switch (log_format) {
606                 case AltosLib.AO_LOG_FORMAT_UNKNOWN:
607                 case AltosLib.AO_LOG_FORMAT_NONE:
608                         break;
609                 default:
610                         link.printf("l\n");
611                         read_link(link, "done");
612                         break;
613                 }
614         }
615
616 }