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