a9dac13aaa9a67623c8d28e38c58793f7b642e11
[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; 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_3;
19
20 import java.io.*;
21 import java.util.*;
22 import java.text.*;
23 import java.util.concurrent.*;
24
25 /*
26  * Temporary structure to hold the list of stored flights;
27  * each of these will be queried in turn to generate more
28  * complete information
29  */
30
31 class AltosEepromFlight {
32         int     flight;
33         int     start;
34         int     end;
35
36         public AltosEepromFlight(int in_flight, int in_start, int in_end) {
37                 flight = in_flight;
38                 start = in_start;
39                 end = in_end;
40         }
41 }
42
43 /*
44  * Construct a list of flights available in a connected device
45  */
46
47 public class AltosEepromList extends ArrayList<AltosEepromLog> {
48         public AltosConfigData  config_data;
49
50         public AltosEepromList (AltosLink link, boolean remote)
51                 throws IOException, InterruptedException, TimeoutException
52         {
53                 try {
54                         if (remote)
55                                 link.start_remote();
56                         config_data = new AltosConfigData (link);
57 //                      if (config_data.serial == 0)
58 //                              throw new IOException("no serial number found");
59
60                         ArrayList<AltosEepromFlight> flights = new ArrayList<AltosEepromFlight>();
61
62                         if (config_data.flight_log_max != 0 || config_data.log_format != 0) {
63
64                                 /* Devices with newer firmware will support the 'l'
65                                  * command which will list the region of storage
66                                  * occupied by each available flight
67                                  */
68                                 link.printf("l\n");
69                                 for (;;) {
70                                         String line = link.get_reply(5000);
71                                         if (line == null)
72                                                 throw new TimeoutException();
73                                         if (line.contains("done"))
74                                                 break;
75                                         if (line.contains("Syntax"))
76                                                 continue;
77                                         String[] tokens = line.split("\\s+");
78                                         if (tokens.length < 6)
79                                                 break;
80
81                                         int     flight = -1, start = -1, end = -1;
82                                         try {
83                                                 if (tokens[0].equals("flight"))
84                                                         flight = AltosParse.parse_int(tokens[1]);
85                                                 if (tokens[2].equals("start"))
86                                                         start = AltosParse.parse_hex(tokens[3]);
87                                                 if (tokens[4].equals("end"))
88                                                         end = AltosParse.parse_hex(tokens[5]);
89                                                 if (flight > 0 && start >= 0 && end > 0)
90                                                         flights.add(new AltosEepromFlight(flight, start, end));
91                                         } catch (ParseException pe) { System.out.printf("Parse error %s\n", pe.toString()); }
92                                 }
93                         } else {
94
95                                 /* Older devices will hold only a single
96                                  * flight. This also assumes that any older
97                                  * device will have a 1MB flash device
98                                  */
99                                 flights.add(new AltosEepromFlight(0, 0, 0xfff));
100                         }
101
102                         /* With the list of flights collected, collect more complete
103                          * information on them by reading the first block or two of
104                          * data. This will add GPS coordinates and a date. For older
105                          * firmware, this will also extract the flight number.
106                          */
107                         for (AltosEepromFlight flight : flights) {
108                                 add(new AltosEepromLog(config_data, link,
109                                                        flight.flight, flight.start, flight.end));
110                         }
111                 } finally {
112                         if (remote)
113                                 link.stop_remote();
114                         link.flush_output();
115                 }
116         }
117 }