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