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