Clean up reflashing section, include section on self-flash recovery
[fw/altos] / altosui / AltosEepromDelete.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.event.*;
21 import javax.swing.*;
22 import java.io.*;
23 import java.util.concurrent.*;
24 import org.altusmetrum.altoslib_2.*;
25
26 public class AltosEepromDelete implements Runnable {
27         AltosEepromList         flights;
28         Thread                  eeprom_thread;
29         AltosSerial             serial_line;
30         boolean                 remote;
31         JFrame                  frame;
32         ActionListener          listener;
33         boolean                 success;
34
35         private void DeleteLog (AltosEepromLog log)
36                 throws IOException, InterruptedException, TimeoutException {
37
38                 if (flights.config_data.flight_log_max != 0 || flights.config_data.log_format != 0) {
39
40                         /* Devices with newer firmware can erase the
41                          * flash blocks containing each flight
42                          */
43                         serial_line.flush_input();
44                         serial_line.printf("d %d\n", log.flight);
45                         for (;;) {
46                                 /* It can take a while to erase the flash... */
47                                 String line = serial_line.get_reply(20000);
48                                 if (line == null)
49                                         throw new TimeoutException();
50                                 if (line.equals("Erased"))
51                                         break;
52                                 if (line.startsWith("No such"))
53                                         throw new IOException(line);
54                         }
55                 }
56         }
57
58         private void show_error_internal(String message, String title) {
59                 JOptionPane.showMessageDialog(frame,
60                                               message,
61                                               title,
62                                               JOptionPane.ERROR_MESSAGE);
63         }
64
65         private void show_error(String in_message, String in_title) {
66                 final String message = in_message;
67                 final String title = in_title;
68                 Runnable r = new Runnable() {
69                                 public void run() {
70                                         try {
71                                                 show_error_internal(message, title);
72                                         } catch (Exception ex) {
73                                         }
74                                 }
75                         };
76                 SwingUtilities.invokeLater(r);
77         }
78
79         public void run () {
80                 success = false;
81                 try {
82                         if (remote)
83                                 serial_line.start_remote();
84
85                         for (AltosEepromLog log : flights) {
86                                 if (log.selected) {
87                                         DeleteLog(log);
88                                 }
89                         }
90                         success = true;
91                 } catch (IOException ee) {
92                         show_error (ee.getLocalizedMessage(),
93                                     serial_line.device.toShortString());
94                 } catch (InterruptedException ie) {
95                 } catch (TimeoutException te) {
96                         show_error (String.format("Connection to \"%s\" failed",
97                                                   serial_line.device.toShortString()),
98                                     "Connection Failed");
99                 } finally {
100                         try {
101                                 if (remote)
102                                         serial_line.stop_remote();
103                         } catch (InterruptedException ie) {
104                         } finally {
105                                 serial_line.flush_output();
106                                 serial_line.close();
107                         }
108                 }
109                 if (listener != null) {
110                         Runnable r = new Runnable() {
111                                         public void run() {
112                                                 try {
113                                                         listener.actionPerformed(new ActionEvent(this,
114                                                                                                  success ? 1 : 0,
115                                                                                                  "delete"));
116                                                 } catch (Exception ex) {
117                                                 }
118                                         }
119                                 };
120                         SwingUtilities.invokeLater(r);
121                 }
122         }
123
124         public void start() {
125                 eeprom_thread = new Thread(this);
126                 eeprom_thread.start();
127         }
128
129         public void addActionListener(ActionListener l) {
130                 listener = l;
131         }
132
133         public AltosEepromDelete(JFrame given_frame,
134                                  AltosSerial given_serial_line,
135                                  boolean given_remote,
136                                  AltosEepromList given_flights) {
137                 frame = given_frame;
138                 serial_line = given_serial_line;
139                 remote = given_remote;
140                 flights = given_flights;
141                 success = false;
142         }
143 }