altoslib: Fix gyro headings in CSV files
[fw/altos] / altoslib / AltosEepromList.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; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 package org.altusmetrum.altoslib_13;
20
21 import java.io.*;
22 import java.util.*;
23 import java.text.*;
24 import java.util.concurrent.*;
25
26 /*
27  * Temporary structure to hold the list of stored flights;
28  * each of these will be queried in turn to generate more
29  * complete information
30  */
31
32 class AltosEepromFlight {
33         int     flight;
34         int     start;
35         int     end;
36
37         public AltosEepromFlight(int in_flight, int in_start, int in_end) {
38                 flight = in_flight;
39                 start = in_start;
40                 end = in_end;
41         }
42 }
43
44 /*
45  * Construct a list of flights available in a connected device
46  */
47
48 public class AltosEepromList extends ArrayList<AltosEepromLog> {
49         public AltosConfigData  config_data;
50
51         public AltosEepromList (AltosLink link, boolean remote)
52                 throws IOException, InterruptedException, TimeoutException
53         {
54                 try {
55                         if (remote)
56                                 link.start_remote();
57                         config_data = new AltosConfigData (link);
58 //                      if (config_data.serial == 0)
59 //                              throw new IOException("no serial number found");
60
61                         ArrayList<AltosEepromFlight> flights = new ArrayList<AltosEepromFlight>();
62
63                         if (config_data.flight_log_max != 0 || config_data.log_format != 0) {
64
65                                 /* Devices with newer firmware will support the 'l'
66                                  * command which will list the region of storage
67                                  * occupied by each available flight
68                                  */
69                                 link.printf("l\n");
70                                 for (;;) {
71                                         String line = link.get_reply(5000);
72                                         if (line == null)
73                                                 throw new TimeoutException();
74                                         if (line.contains("done"))
75                                                 break;
76                                         if (line.contains("Syntax"))
77                                                 continue;
78                                         String[] tokens = line.split("\\s+");
79                                         if (tokens.length < 6)
80                                                 break;
81
82                                         int     flight = -1, start = -1, end = -1;
83                                         try {
84                                                 if (tokens[0].equals("flight"))
85                                                         flight = AltosParse.parse_int(tokens[1]);
86                                                 if (tokens[2].equals("start"))
87                                                         start = AltosParse.parse_hex(tokens[3]);
88                                                 if (tokens[4].equals("end"))
89                                                         end = AltosParse.parse_hex(tokens[5]);
90                                                 if (flight != 0 && start >= 0 && end > 0)
91                                                         flights.add(new AltosEepromFlight(flight, start, end));
92                                         } catch (ParseException pe) { System.out.printf("Parse error %s\n", pe.toString()); }
93                                 }
94                         } else {
95
96                                 /* Older devices will hold only a single
97                                  * flight. This also assumes that any older
98                                  * device will have a 1MB flash device
99                                  */
100                                 flights.add(new AltosEepromFlight(0, 0, 0xfff));
101                         }
102
103                         /* With the list of flights collected, collect more complete
104                          * information on them by reading the first block or two of
105                          * data. This will add GPS coordinates and a date. For older
106                          * firmware, this will also extract the flight number.
107                          */
108                         for (AltosEepromFlight flight : flights) {
109                                 add(new AltosEepromLog(config_data, link,
110                                                        flight.flight, flight.start, flight.end));
111                         }
112                 } finally {
113                         if (remote)
114                                 link.stop_remote();
115                         link.flush_output();
116                 }
117         }
118 }