altos: Make sure pyro remains valid during delay
[fw/altos] / altosuilib / AltosFlashUI.java
1 /*
2  * Copyright © 2010 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.altosuilib_3;
19
20 import java.awt.*;
21 import java.awt.event.*;
22 import javax.swing.*;
23 import javax.swing.filechooser.FileNameExtensionFilter;
24 import java.io.*;
25 import java.util.concurrent.*;
26 import org.altusmetrum.altoslib_5.*;
27
28 public class AltosFlashUI
29         extends AltosUIDialog
30         implements ActionListener
31 {
32         Container       pane;
33         Box             box;
34         JLabel          serial_label;
35         JLabel          serial_value;
36         JLabel          file_label;
37         JLabel          file_value;
38         JProgressBar    pbar;
39         JButton         cancel;
40
41         AltosUIFrame    frame;
42
43         // Hex file with rom image
44         File            file;
45
46         // Debug connection
47         AltosDevice     device;
48
49         AltosLink       link;
50
51         // Desired Rom configuration
52         AltosRomconfig  rom_config;
53
54         // Flash controller
55         AltosProgrammer programmer;
56
57         private static String[] pair_programmed = {
58                 "teleballoon",
59                 "telebt",
60                 "teledongle",
61                 "telefire",
62                 "telemetrum-v0",
63                 "telemetrum-v1",
64                 "telemini",
65                 "telenano",
66                 "teleshield",
67                 "teleterra"
68         };
69
70         private boolean is_pair_programmed() {
71
72                 if (file != null) {
73                         String  name = file.getName();
74                         for (int i = 0; i < pair_programmed.length; i++) {
75                                 if (name.startsWith(pair_programmed[i]))
76                                         return true;
77                         }
78                 }
79                 if (device != null) {
80                         if (!device.matchProduct(AltosLib.product_altusmetrum) &&
81                             (device.matchProduct(AltosLib.product_teledongle) ||
82                              device.matchProduct(AltosLib.product_telebt)))
83                                 return true;
84                 }
85                 return false;
86         }
87
88         public void actionPerformed(ActionEvent e) {
89                 if (e.getSource() == cancel) {
90                         if (programmer != null)
91                                 programmer.abort();
92                         setVisible(false);
93                         dispose();
94                 } else {
95                         String  cmd = e.getActionCommand();
96                         if (e.getID() == -1) {
97                                 JOptionPane.showMessageDialog(frame,
98                                                               e.getActionCommand(),
99                                                               file.toString(),
100                                                               JOptionPane.ERROR_MESSAGE);
101                                 setVisible(false);
102                                 dispose();
103                         } else if (cmd.equals("done")) {
104                                 setVisible(false);
105                                 dispose();
106                         } else if (cmd.equals("start")) {
107                                 setVisible(true);
108                         } else {
109                                 pbar.setValue(e.getID());
110                                 pbar.setString(cmd);
111                         }
112                 }
113         }
114
115         public void build_dialog() {
116                 GridBagConstraints c;
117                 Insets il = new Insets(4,4,4,4);
118                 Insets ir = new Insets(4,4,4,4);
119
120                 pane = getContentPane();
121                 pane.setLayout(new GridBagLayout());
122
123                 c = new GridBagConstraints();
124                 c.gridx = 0; c.gridy = 0;
125                 c.fill = GridBagConstraints.NONE;
126                 c.anchor = GridBagConstraints.LINE_START;
127                 c.insets = il;
128                 serial_label = new JLabel("Serial:");
129                 pane.add(serial_label, c);
130
131                 c = new GridBagConstraints();
132                 c.gridx = 1; c.gridy = 0;
133                 c.fill = GridBagConstraints.HORIZONTAL;
134                 c.weightx = 1;
135                 c.anchor = GridBagConstraints.LINE_START;
136                 c.insets = ir;
137                 serial_value = new JLabel("");
138                 pane.add(serial_value, c);
139
140                 c = new GridBagConstraints();
141                 c.fill = GridBagConstraints.NONE;
142                 c.gridx = 0; c.gridy = 1;
143                 c.anchor = GridBagConstraints.LINE_START;
144                 c.insets = il;
145                 file_label = new JLabel("File:");
146                 pane.add(file_label, c);
147
148                 c = new GridBagConstraints();
149                 c.fill = GridBagConstraints.HORIZONTAL;
150                 c.weightx = 1;
151                 c.gridx = 1; c.gridy = 1;
152                 c.anchor = GridBagConstraints.LINE_START;
153                 c.insets = ir;
154                 file_value = new JLabel(file.toString());
155                 pane.add(file_value, c);
156
157                 pbar = new JProgressBar();
158                 pbar.setMinimum(0);
159                 pbar.setMaximum(100);
160                 pbar.setValue(0);
161                 pbar.setString("");
162                 pbar.setStringPainted(true);
163                 pbar.setPreferredSize(new Dimension(600, 20));
164                 c = new GridBagConstraints();
165                 c.fill = GridBagConstraints.HORIZONTAL;
166                 c.anchor = GridBagConstraints.CENTER;
167                 c.gridx = 0; c.gridy = 2;
168                 c.gridwidth = GridBagConstraints.REMAINDER;
169                 Insets ib = new Insets(4,4,4,4);
170                 c.insets = ib;
171                 pane.add(pbar, c);
172
173                 cancel = new JButton("Cancel");
174                 c = new GridBagConstraints();
175                 c.fill = GridBagConstraints.NONE;
176                 c.anchor = GridBagConstraints.CENTER;
177                 c.gridx = 0; c.gridy = 3;
178                 c.gridwidth = GridBagConstraints.REMAINDER;
179                 Insets ic = new Insets(4,4,4,4);
180                 c.insets = ic;
181                 pane.add(cancel, c);
182                 cancel.addActionListener(this);
183                 pack();
184                 setLocationRelativeTo(frame);
185         }
186
187         void set_serial(int serial_number) {
188                 serial_value.setText(String.format("%d", serial_number));
189         }
190
191         static class AltosHexfileFilter extends javax.swing.filechooser.FileFilter {
192                 int product;
193                 String head;
194                 String description;
195
196                 public AltosHexfileFilter(int product, String head, String description) {
197                         this.product = product;
198                         this.head = head;
199                         this.description = description;
200                 }
201
202                 public boolean accept(File file) {
203                         return !file.isFile() || (file.getName().startsWith(head) && file.getName().endsWith(".ihx"));
204                 }
205
206                 public String getDescription() {
207                         return description;
208                 }
209         }
210
211         static AltosHexfileFilter[] filters = {
212                 new AltosHexfileFilter(AltosLib.product_telemetrum, "telemetrum", "TeleMetrum Image"),
213                 new AltosHexfileFilter(AltosLib.product_teledongle, "teledongle", "TeleDongle Image"),
214                 new AltosHexfileFilter(AltosLib.product_telemega, "telemega", "TeleMega Image"),
215                 new AltosHexfileFilter(AltosLib.product_easymini, "easymini", "EasyMini Image"),
216         };
217
218         boolean select_source_file() {
219                 JFileChooser    hexfile_chooser = new JFileChooser();
220
221                 File firmwaredir = AltosUIPreferences.firmwaredir();
222                 if (firmwaredir != null)
223                         hexfile_chooser.setCurrentDirectory(firmwaredir);
224
225                 hexfile_chooser.setDialogTitle("Select Flash Image");
226
227                 for (int i = 0; i < filters.length; i++) {
228                         hexfile_chooser.addChoosableFileFilter(filters[i]);
229                 }
230                 javax.swing.filechooser.FileFilter ihx_filter = new FileNameExtensionFilter("Flash Image", "ihx");
231                 hexfile_chooser.addChoosableFileFilter(ihx_filter);
232                 hexfile_chooser.setFileFilter(ihx_filter);
233
234                 if (!is_pair_programmed() && !device.matchProduct(AltosLib.product_altusmetrum)) {
235                         for (int i = 0; i < filters.length; i++) {
236                                 if (device != null && device.matchProduct(filters[i].product))
237                                         hexfile_chooser.setFileFilter(filters[i]);
238                         }
239                 }
240
241                 int returnVal = hexfile_chooser.showOpenDialog(frame);
242
243                 if (returnVal != JFileChooser.APPROVE_OPTION)
244                         return false;
245                 file = hexfile_chooser.getSelectedFile();
246                 if (file == null)
247                         return false;
248                 AltosUIPreferences.set_firmwaredir(file.getParentFile());
249
250                 return true;
251         }
252
253         boolean select_device() {
254                 int     product = AltosLib.product_any;
255
256                 device = AltosDeviceUIDialog.show(frame, AltosLib.product_any);
257
258                 if (device == null)
259                         return false;
260                 return true;
261         }
262
263         boolean update_rom_config_info(AltosRomconfig existing_config) {
264                 AltosRomconfig  new_config;
265                 new_config = AltosRomconfigUI.show(frame, existing_config);
266                 if (new_config == null)
267                         return false;
268                 rom_config = new_config;
269                 set_serial(rom_config.serial_number);
270                 setVisible(true);
271                 return true;
272         }
273
274         void exception (Exception e) {
275                 if (e instanceof FileNotFoundException) {
276                         JOptionPane.showMessageDialog(frame,
277                                                       ((FileNotFoundException) e).getMessage(),
278                                                       "Cannot open file",
279                                                       JOptionPane.ERROR_MESSAGE);
280                 } else if (e instanceof AltosSerialInUseException) {
281                         JOptionPane.showMessageDialog(frame,
282                                                       String.format("Device \"%s\" already in use",
283                                                                     device.toShortString()),
284                                                       "Device in use",
285                                                       JOptionPane.ERROR_MESSAGE);
286                 } else if (e instanceof IOException) {
287                         JOptionPane.showMessageDialog(frame,
288                                                       e.getMessage(),
289                                                       file.toString(),
290                                                       JOptionPane.ERROR_MESSAGE);
291                 }
292         }
293
294         class flash_task implements Runnable, AltosFlashListener {
295                 AltosFlashUI    ui;
296                 Thread          t;
297                 AltosProgrammer programmer;
298
299                 public void position(String in_s, int in_percent) {
300                         final String s = in_s;
301                         final int percent = in_percent;
302                         Runnable r = new Runnable() {
303                                         public void run() {
304                                                 try {
305                                                         ui.actionPerformed(new ActionEvent(this,
306                                                                                            percent,
307                                                                                            s));
308                                                 } catch (Exception ex) {
309                                                 }
310                                         }
311                                 };
312                         SwingUtilities.invokeLater(r);
313                 }
314
315                 public void run () {
316                         try {
317                                 if (ui.is_pair_programmed())
318                                         programmer = new AltosFlash(ui.file, link, this);
319                                 else
320                                         programmer = new AltosSelfFlash(ui.file, link, this);
321
322                                 final AltosRomconfig    current_config = programmer.romconfig();
323
324                                 final Semaphore await_rom_config = new Semaphore(0);
325                                 SwingUtilities.invokeLater(new Runnable() {
326                                                 public void run() {
327                                                         ui.programmer = programmer;
328                                                         ui.update_rom_config_info(current_config);
329                                                         await_rom_config.release();
330                                                 }
331                                         });
332                                 await_rom_config.acquire();
333
334                                 if (ui.rom_config != null) {
335                                         programmer.set_romconfig(ui.rom_config);
336                                         programmer.flash();
337                                 }
338                         } catch (InterruptedException ee) {
339                                 final Exception e = ee;
340                                 SwingUtilities.invokeLater(new Runnable() {
341                                                 public void run() {
342                                                         ui.exception(e);
343                                                 }
344                                         });
345                         } catch (IOException ee) {
346                                 final Exception e = ee;
347                                 SwingUtilities.invokeLater(new Runnable() {
348                                                 public void run() {
349                                                         ui.exception(e);
350                                                 }
351                                         });
352                         } finally {
353                                 if (programmer != null)
354                                         programmer.close();
355                         }
356                 }
357
358                 public flash_task(AltosFlashUI in_ui) {
359                         ui = in_ui;
360                         t = new Thread(this);
361                         t.start();
362                 }
363         }
364
365         flash_task      flasher;
366
367         private boolean open_device() throws InterruptedException {
368                 try {
369                         link = new AltosSerial(device);
370                         if (is_pair_programmed())
371                                 return true;
372
373                         if (link == null)
374                                 throw new IOException(String.format("%s: open failed", device.toShortString()));
375
376                         while (!link.is_loader()) {
377                                 link.to_loader();
378
379                                 java.util.List<AltosDevice> devices = null;
380
381                                 for (int tries = 0; tries < 10; tries++) {
382                                         Thread.sleep(100);
383                                         devices = AltosUSBDevice.list(AltosLib.product_altusmetrum);
384                                         if (devices.size() != 0)
385                                                 break;
386                                 }
387
388                                 if (devices.size() == 1)
389                                         device = devices.get(0);
390                                 else {
391                                         device = AltosDeviceUIDialog.show(frame, AltosLib.product_altusmetrum);
392                                         if (device == null)
393                                                 return false;
394                                 }
395                                 link = new AltosSerial(device);
396                         }
397                         return true;
398                 } catch (AltosSerialInUseException ee) {
399                         exception(ee);
400                 } catch (FileNotFoundException fe) {
401                         exception(fe);
402                 } catch (IOException ie) {
403                         exception (ie);
404                 }
405                 return false;
406         }
407
408         /*
409          * Execute the steps for flashing
410          * a device. Note that this returns immediately;
411          * this dialog is not modal
412          */
413         void showDialog() {
414                 if (!select_device())
415                         return;
416                 if (!select_source_file())
417                         return;
418                 try {
419                         if (!open_device())
420                                 return;
421                 } catch (InterruptedException ie) {
422                         return;
423                 }
424                 build_dialog();
425                 flash_task      f = new flash_task(this);
426         }
427
428         public static void show(AltosUIFrame frame) {
429                 AltosFlashUI    ui = new AltosFlashUI(frame);
430                 ui.showDialog();
431         }
432
433         public AltosFlashUI(AltosUIFrame in_frame) {
434                 super(in_frame, "Program Altusmetrum Device", false);
435
436                 frame = in_frame;
437         }
438 }