ffb39b8c2aae4e4e7b2035a9e780355114808bf8
[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; 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_13;
20
21 import java.awt.*;
22 import java.awt.event.*;
23 import javax.swing.*;
24 import javax.swing.filechooser.FileNameExtensionFilter;
25 import java.io.*;
26 import java.util.concurrent.*;
27 import org.altusmetrum.altoslib_13.*;
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         AltosUIFrame    frame;
43
44         // Hex file with rom image
45         File            file;
46
47         // Debug connection
48         AltosUSBDevice  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 final String[] pair_programmed_files = {
59                 "teleballoon",
60                 "telebt-v1",
61                 "teledongle-v0",
62                 "telefire-v0",
63                 "telemetrum-v0",
64                 "telemetrum-v1",
65                 "telemini-v1",
66                 "telenano",
67                 "teleshield",
68                 "teleterra"
69         };
70
71         private static final String[] pair_programmed_devices = {
72                 "TeleBalloon",
73                 "TeleBT-v1",
74                 "TeleDongle-v0",
75                 "TeleFire-v0",
76                 "TeleFire",
77                 "TeleMetrum-v0",
78                 "TeleMetrum-v1",
79                 "TeleMini-v1",
80                 "TeleNano",
81                 "TeleShield",
82                 "TeleTerra"
83         };
84
85         private boolean is_pair_programmed() {
86
87                 if (file != null) {
88                         String  name = file.getName();
89                         for (int i = 0; i < pair_programmed_files.length; i++) {
90                                 if (name.startsWith(pair_programmed_files[i]))
91                                         return true;
92                         }
93                 }
94                 if (device != null) {
95                         String  name = device.toString();
96                         for (int i = 0; i < pair_programmed_devices.length; i++) {
97                                 if (name.startsWith(pair_programmed_devices[i]))
98                                         return true;
99                         }
100                 }
101                 return false;
102         }
103
104         public void actionPerformed(ActionEvent e) {
105                 if (e.getSource() == cancel) {
106                         if (programmer != null)
107                                 programmer.abort();
108                         setVisible(false);
109                         dispose();
110                 } else {
111                         String  cmd = e.getActionCommand();
112                         if (e.getID() == -1) {
113                                 JOptionPane.showMessageDialog(frame,
114                                                               e.getActionCommand(),
115                                                               file.toString(),
116                                                               JOptionPane.ERROR_MESSAGE);
117                                 setVisible(false);
118                                 dispose();
119                         } else if (cmd.equals(AltosFlashListener.flash_done)) {
120                                 setVisible(false);
121                                 dispose();
122                         } else if (cmd.equals(AltosFlashListener.flash_start)) {
123                                 setVisible(true);
124                         } else {
125                                 pbar.setValue(e.getID());
126                                 pbar.setString(cmd);
127                         }
128                 }
129         }
130
131         public void build_dialog() {
132                 GridBagConstraints c;
133                 Insets il = new Insets(4,4,4,4);
134                 Insets ir = new Insets(4,4,4,4);
135
136                 pane = getContentPane();
137                 pane.setLayout(new GridBagLayout());
138
139                 c = new GridBagConstraints();
140                 c.gridx = 0; c.gridy = 0;
141                 c.fill = GridBagConstraints.NONE;
142                 c.anchor = GridBagConstraints.LINE_START;
143                 c.insets = il;
144                 serial_label = new JLabel("Serial:");
145                 pane.add(serial_label, c);
146
147                 c = new GridBagConstraints();
148                 c.gridx = 1; c.gridy = 0;
149                 c.fill = GridBagConstraints.HORIZONTAL;
150                 c.weightx = 1;
151                 c.anchor = GridBagConstraints.LINE_START;
152                 c.insets = ir;
153                 serial_value = new JLabel("");
154                 pane.add(serial_value, c);
155
156                 c = new GridBagConstraints();
157                 c.fill = GridBagConstraints.NONE;
158                 c.gridx = 0; c.gridy = 1;
159                 c.anchor = GridBagConstraints.LINE_START;
160                 c.insets = il;
161                 file_label = new JLabel("File:");
162                 pane.add(file_label, c);
163
164                 c = new GridBagConstraints();
165                 c.fill = GridBagConstraints.HORIZONTAL;
166                 c.weightx = 1;
167                 c.gridx = 1; c.gridy = 1;
168                 c.anchor = GridBagConstraints.LINE_START;
169                 c.insets = ir;
170                 file_value = new JLabel(file.toString());
171                 pane.add(file_value, c);
172
173                 pbar = new JProgressBar();
174                 pbar.setMinimum(0);
175                 pbar.setMaximum(100);
176                 pbar.setValue(0);
177                 pbar.setString("");
178                 pbar.setStringPainted(true);
179                 pbar.setPreferredSize(new Dimension(600, 20));
180                 c = new GridBagConstraints();
181                 c.fill = GridBagConstraints.HORIZONTAL;
182                 c.anchor = GridBagConstraints.CENTER;
183                 c.gridx = 0; c.gridy = 2;
184                 c.gridwidth = GridBagConstraints.REMAINDER;
185                 Insets ib = new Insets(4,4,4,4);
186                 c.insets = ib;
187                 pane.add(pbar, c);
188
189                 cancel = new JButton("Cancel");
190                 c = new GridBagConstraints();
191                 c.fill = GridBagConstraints.NONE;
192                 c.anchor = GridBagConstraints.CENTER;
193                 c.gridx = 0; c.gridy = 3;
194                 c.gridwidth = GridBagConstraints.REMAINDER;
195                 Insets ic = new Insets(4,4,4,4);
196                 c.insets = ic;
197                 pane.add(cancel, c);
198                 cancel.addActionListener(this);
199                 pack();
200                 setLocationRelativeTo(frame);
201         }
202
203         void set_serial(int serial_number) {
204                 serial_value.setText(String.format("%d", serial_number));
205         }
206
207         static class AltosHexfileFilter extends javax.swing.filechooser.FileFilter {
208                 String head;
209                 String description;
210
211                 public AltosHexfileFilter(String usb_product) {
212                         int l;
213
214                         /* Trim off any trailing variants (1.0a vs 1.0) */
215                         for (l = usb_product.length(); l > 0; l--) {
216                                 char c = usb_product.charAt(l-1);
217                                 if (c < 'a' || 'z' < c)
218                                         break;
219                         }
220                         head = usb_product.substring(0, l).toLowerCase();
221                         description = String.format("%s Image File", usb_product);
222                 }
223
224                 public boolean accept(File file) {
225                         return !file.isFile() || (file.getName().startsWith(head) && file.getName().endsWith(".ihx"));
226                 }
227
228                 public String getDescription() {
229                         return description;
230                 }
231         }
232
233         boolean select_source_file() {
234                 JFileChooser    hexfile_chooser = new JFileChooser();
235
236                 File firmwaredir = AltosUIPreferences.firmwaredir();
237                 if (firmwaredir != null)
238                         hexfile_chooser.setCurrentDirectory(firmwaredir);
239
240                 hexfile_chooser.setDialogTitle("Select Flash Image");
241
242                 javax.swing.filechooser.FileFilter ihx_filter = new FileNameExtensionFilter("Flash Image", "ihx");
243                 hexfile_chooser.addChoosableFileFilter(ihx_filter);
244                 hexfile_chooser.setFileFilter(ihx_filter);
245
246                 if (!is_pair_programmed() && !device.matchProduct(AltosLib.product_altusmetrum)) {
247                         AltosHexfileFilter filter = new AltosHexfileFilter(device.usb_product());
248                         hexfile_chooser.addChoosableFileFilter(filter);
249                         hexfile_chooser.setFileFilter(filter);
250                 }
251
252                 int returnVal = hexfile_chooser.showOpenDialog(frame);
253
254                 if (returnVal != JFileChooser.APPROVE_OPTION)
255                         return false;
256                 file = hexfile_chooser.getSelectedFile();
257                 if (file == null)
258                         return false;
259                 AltosUIPreferences.set_firmwaredir(file.getParentFile());
260
261                 return true;
262         }
263
264         boolean select_device() {
265                 int     product = AltosLib.product_any;
266
267                 device = AltosDeviceUIDialog.show_usb(frame, AltosLib.product_any);
268
269                 if (device == null)
270                         return false;
271                 return true;
272         }
273
274         boolean rom_config_matches (AltosRomconfig a, AltosRomconfig b) {
275                 if (a == null || b == null)
276                         return (a == null && b == null);
277
278                 if (!a.valid || !b.valid)
279                         return false;
280
281                 if (a.usb_id != null && b.usb_id != null &&
282                     (a.usb_id.vid != b.usb_id.vid ||
283                      a.usb_id.pid != b.usb_id.pid))
284                         return false;
285
286                 if (a.usb_product != null && b.usb_product != null &&
287                     !a.usb_product.equals(b.usb_product))
288                         return false;
289
290                 return true;
291         }
292
293         boolean update_rom_config_info(AltosRomconfig existing_config, AltosRomconfig image_config) {
294                 AltosRomconfig  new_config;
295
296                 if (!rom_config_matches(existing_config, image_config)) {
297                         int ret;
298                         if (existing_config == null || !existing_config.valid) {
299                                 ret = JOptionPane.showConfirmDialog(this,
300                                                                     String.format("Cannot determine target device type\nImage is %04x:%04x %s\nFlash anyways?",
301                                                                                   image_config.usb_id.vid,
302                                                                                   image_config.usb_id.pid,
303                                                                                   image_config.usb_product),
304                                                                     "Unknown Target Device",
305                                                                     JOptionPane.YES_NO_OPTION);
306                         } else {
307                                 ret = JOptionPane.showConfirmDialog(this,
308                                                                     String.format("Device is %04x:%04x %s\nImage is %04x:%04x %s\nFlash anyways?",
309                                                                                   existing_config.usb_id.vid,
310                                                                                   existing_config.usb_id.pid,
311                                                                                   existing_config.usb_product,
312                                                                                   image_config.usb_id.vid,
313                                                                                   image_config.usb_id.pid,
314                                                                                   image_config.usb_product),
315                                                                     "Image doesn't match Device",
316                                                                     JOptionPane.YES_NO_OPTION);
317                         }
318                         if (ret != JOptionPane.YES_OPTION)
319                                 return false;
320                 }
321
322                 if (existing_config.radio_calibration_broken) {
323                         int ret = JOptionPane.showConfirmDialog(this,
324                                                                 String.format("Radio calibration value %d may be incorrect\nFlash anyways?",
325                                                                               existing_config.radio_calibration),
326                                                                 "Radio Calibration Invalid",
327                                                                 JOptionPane.YES_NO_OPTION);
328                         if (ret != JOptionPane.YES_OPTION)
329                                 return false;
330                 }
331
332
333                 new_config = AltosRomconfigUI.show(frame, existing_config);
334                 if (new_config == null)
335                         return false;
336                 rom_config = new_config;
337                 set_serial(rom_config.serial_number);
338                 setVisible(true);
339                 return true;
340         }
341
342         void exception (Exception e) {
343                 if (e instanceof FileNotFoundException) {
344                         JOptionPane.showMessageDialog(frame,
345                                                       ((FileNotFoundException) e).getMessage(),
346                                                       "Cannot open file",
347                                                       JOptionPane.ERROR_MESSAGE);
348                 } else if (e instanceof AltosSerialInUseException) {
349                         JOptionPane.showMessageDialog(frame,
350                                                       String.format("Device \"%s\" already in use",
351                                                                     device.toShortString()),
352                                                       "Device in use",
353                                                       JOptionPane.ERROR_MESSAGE);
354                 } else {
355                         JOptionPane.showMessageDialog(frame,
356                                                       e.getMessage(),
357                                                       file.toString(),
358                                                       JOptionPane.ERROR_MESSAGE);
359                 }
360         }
361
362         class flash_task implements Runnable, AltosFlashListener {
363                 AltosFlashUI    ui;
364                 Thread          t;
365                 AltosProgrammer programmer;
366
367                 public void position(String in_s, int in_percent) {
368                         final String s = in_s;
369                         final int percent = in_percent;
370                         Runnable r = new Runnable() {
371                                         public void run() {
372                                                 try {
373                                                         ui.actionPerformed(new ActionEvent(this,
374                                                                                            percent,
375                                                                                            s));
376                                                 } catch (Exception ex) {
377                                                 }
378                                         }
379                                 };
380                         SwingUtilities.invokeLater(r);
381                 }
382
383                 public void run () {
384                         try {
385                                 if (ui.is_pair_programmed())
386                                         programmer = new AltosFlash(ui.file, link, this);
387                                 else
388                                         programmer = new AltosSelfFlash(ui.file, link, this);
389
390                                 final AltosRomconfig    current_config = programmer.target_romconfig(device.usb_id(), device.usb_product());
391
392                                 final AltosRomconfig    image_config = programmer.image_romconfig();
393
394                                 System.out.printf("product %s current %s image %s\n", device.usb_product(), current_config, image_config);
395
396                                 final Semaphore await_rom_config = new Semaphore(0);
397                                 SwingUtilities.invokeLater(new Runnable() {
398                                                 public void run() {
399                                                         ui.programmer = programmer;
400                                                         ui.update_rom_config_info(current_config, image_config);
401                                                         await_rom_config.release();
402                                                 }
403                                         });
404                                 await_rom_config.acquire();
405
406                                 if (ui.rom_config != null) {
407                                         programmer.set_romconfig(ui.rom_config);
408                                         programmer.flash();
409                                 }
410                         } catch (InterruptedException ee) {
411                                 final Exception e = ee;
412                                 SwingUtilities.invokeLater(new Runnable() {
413                                                 public void run() {
414                                                         ui.exception(e);
415                                                 }
416                                         });
417                         } catch (IOException ee) {
418                                 final Exception e = ee;
419                                 SwingUtilities.invokeLater(new Runnable() {
420                                                 public void run() {
421                                                         ui.exception(e);
422                                                 }
423                                         });
424                         } finally {
425                                 if (programmer != null)
426                                         programmer.close();
427                         }
428                 }
429
430                 public flash_task(AltosFlashUI in_ui) {
431                         ui = in_ui;
432                         t = new Thread(this);
433                         t.start();
434                 }
435         }
436
437         flash_task      flasher;
438
439
440         class open_task implements Runnable {
441                 AltosDevice     device;
442                 Thread          t;
443                 open_dialog     dialog;
444
445                 public void do_exception(final Exception e) {
446                         SwingUtilities.invokeLater(
447                                 new Runnable() {
448                                         public void run() {
449                                                 try { dialog.open_exception(e); } catch (Exception ex) { }
450                                         }
451                                 });
452                 }
453
454                 public void do_success(final AltosLink link) {
455                         SwingUtilities.invokeLater(
456                                 new Runnable() {
457                                         public void run() {
458                                                 try { dialog.open_success(link); } catch (Exception ex) { }
459                                         }
460                                 });
461                 }
462
463                 public void do_failure() {
464                         SwingUtilities.invokeLater(
465                                 new Runnable() {
466                                         public void run() {
467                                                 try { dialog.open_failure(); } catch (Exception ex) { }
468                                         }
469                                 });
470                 }
471
472                 public void do_cancel() {
473                         SwingUtilities.invokeLater(
474                                 new Runnable() {
475                                         public void run() {
476                                                 try { dialog.open_cancel(); } catch (Exception ex) { }
477                                         }
478                                 });
479                 }
480
481                 public void run () {
482                         try {
483                                 AltosLink link = null;
484
485                                 for (;;) {
486                                         System.out.printf("Attempting to open %s\n", device.toShortString());
487
488                                         link = new AltosSerial(device);
489
490                                         if (link == null)
491                                                 throw new IOException(String.format("%s: open failed",
492                                                                                     device.toShortString()));
493
494                                         /* See if the link is usable already */
495                                         if (is_pair_programmed() || link.is_loader()) {
496                                                 System.out.printf("Device ready for use\n");
497                                                 do_success(link);
498                                                 return;
499                                         }
500
501                                         java.util.List<AltosDevice> prev_devices =
502                                                 AltosUSBDevice.list(AltosLib.product_altusmetrum);
503
504                                         /* Nope, switch to loader and
505                                          * wait for it to re-appear
506                                          */
507
508                                         System.out.printf("Switch to loader\n");
509
510                                         link.to_loader();
511
512                                         /* This is a bit fragile, but
513                                          * I'm not sure what else to
514                                          * do other than ask the user.
515                                          *
516                                          * Search for a device which
517                                          * wasn't there before we
518                                          * asked the target to switch
519                                          * to loader mode
520                                          */
521
522                                         device = null;
523                                         for (;;) {
524                                                 Thread.sleep(100);
525                                                 java.util.List<AltosDevice> devices =
526                                                         AltosUSBDevice.list(AltosLib.product_altusmetrum);
527
528                                                 for (AltosDevice d : devices) {
529                                                         boolean matched = false;
530                                                         System.out.printf("\tfound device %s\n", d.toShortString());
531                                                         for (AltosDevice p : prev_devices)
532                                                                 if (d.equals(p)) {
533                                                                         matched = true;
534                                                                         break;
535                                                                 }
536                                                         if (!matched) {
537                                                                 System.out.printf("Identified new device %s\n", d.toShortString());
538                                                                 device = (AltosUSBDevice) d;
539                                                                 break;
540                                                         }
541                                                 }
542                                                 if (device != null)
543                                                         break;
544                                         }
545                                 }
546                         } catch (AltosSerialInUseException ee) {
547                                 do_exception(ee);
548                         } catch (FileNotFoundException fe) {
549                                 do_exception(fe);
550                         } catch (IOException ie) {
551                                 do_exception (ie);
552                         } catch (InterruptedException ie) {
553                         }
554                 }
555
556                 public void cancel() {
557                         t.interrupt();
558                 }
559
560                 public open_task(AltosDevice device, open_dialog dialog) {
561                         this.device = device;
562                         this.dialog = dialog;
563                         t = new Thread(this);
564                         t.start();
565                 }
566         }
567
568         class open_dialog
569                 extends AltosUIDialog
570                 implements ActionListener
571         {
572                 AltosUIFrame owner;
573
574                 private JLabel  opening_label;
575                 private JButton cancel_button;
576
577                 boolean done = false;
578
579                 AltosLink link = null;
580
581                 open_task open = null;
582
583                 public void open_exception(Exception e) {
584                         System.out.printf("open_exception\n");
585                         setVisible(false);
586                         exception(e);
587                         done = true;
588                 }
589
590                 public void open_success(AltosLink link) {
591                         System.out.printf("open_success\n");
592                         setVisible(false);
593                         this.link = link;
594                         done = true;
595                 }
596
597                 public void open_failure() {
598                         System.out.printf("open_failure\n");
599                         setVisible(false);
600                         done = true;
601                 }
602
603                 public void open_cancel() {
604                         System.out.printf("open_cancel\n");
605                         setVisible(false);
606                         done = true;
607                 }
608
609                 public AltosLink do_open(open_task open) throws InterruptedException {
610                         this.open = open;
611                         setVisible(true);
612                         return link;
613                 }
614
615                 public void actionPerformed(ActionEvent e) {
616                         String cmd = e.getActionCommand();
617
618                         if (cmd.equals("cancel"))
619                                 if (open != null)
620                                         open.cancel();
621                         done = true;
622                         setVisible(false);
623                 }
624
625                 public open_dialog(AltosUIFrame in_owner) {
626                         super(in_owner, "Open Flash Target Device", true);
627                         owner = in_owner;
628
629                         Container               pane = getContentPane();
630                         GridBagConstraints      c = new GridBagConstraints();
631                         Insets                  i = new Insets(4,4,4,4);
632
633
634                         pane.setLayout(new GridBagLayout());
635
636                         opening_label = new JLabel("Opening Device");
637                         c.fill = GridBagConstraints.HORIZONTAL;
638                         c.anchor = GridBagConstraints.LINE_START;
639                         c.insets = i;
640                         c.weightx = 0;
641                         c.weighty = 0;
642
643                         c.gridx = 0;
644                         c.gridy = 0;
645
646                         pane.add(opening_label, c);
647
648                         cancel_button = new JButton("Cancel");
649                         cancel_button.addActionListener(this);
650                         cancel_button.setActionCommand("cancel");
651
652                         c.gridy = 1;
653                         pane.add(cancel_button, c);
654                         pack();
655                         setLocationRelativeTo(owner);
656                 }
657         }
658
659         private boolean open_device() throws InterruptedException {
660
661                 open_dialog     dialog = new open_dialog(frame);
662
663                 open_task       open = new open_task(device, dialog);
664
665                 link = dialog.do_open(open);
666
667                 return link != null;
668         }
669
670         /*
671          * Execute the steps for flashing
672          * a device. Note that this returns immediately;
673          * this dialog is not modal
674          */
675         void showDialog() {
676                 if (!select_device())
677                         return;
678                 if (!select_source_file())
679                         return;
680                 try {
681                         if (!open_device())
682                                 return;
683                 } catch (InterruptedException ie) {
684                         return;
685                 }
686                 build_dialog();
687                 flash_task      f = new flash_task(this);
688         }
689
690         public static void show(AltosUIFrame frame) {
691                 AltosFlashUI    ui = new AltosFlashUI(frame);
692                 ui.showDialog();
693         }
694
695         public AltosFlashUI(AltosUIFrame in_frame) {
696                 super(in_frame, "Program Altusmetrum Device", false);
697
698                 frame = in_frame;
699         }
700 }