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