Bump java lib versions in preparation for 1.9.2
[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                 "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 != null && 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                                 boolean new_device = false;
485
486                                 for (;;) {
487                                         System.out.printf("Attempting to open %s\n", device.toShortString());
488
489                                         for (int i = 0; i < 20; i++) {
490                                                 link = new AltosSerial(device);
491
492                                                 if (link != null)
493                                                         break;
494
495                                                 if (!new_device)
496                                                         break;
497
498                                                 System.out.printf("Waiting for device to become ready\n");
499
500                                                 Thread.sleep(1000);
501                                         }
502                                         if (link == null)
503                                                 throw new IOException(String.format("%s: open failed",
504                                                                                     device.toShortString()));
505
506                                         /* See if the link is usable already */
507                                         if (is_pair_programmed() || link.is_loader()) {
508                                                 System.out.printf("Device ready for use\n");
509                                                 do_success(link);
510                                                 return;
511                                         }
512
513                                         java.util.List<AltosDevice> prev_devices =
514                                                 AltosUSBDevice.list(AltosLib.product_altusmetrum);
515
516                                         /* Nope, switch to loader and
517                                          * wait for it to re-appear
518                                          */
519
520                                         System.out.printf("Switch to loader\n");
521
522                                         link.to_loader();
523
524                                         /* This is a bit fragile, but
525                                          * I'm not sure what else to
526                                          * do other than ask the user.
527                                          *
528                                          * Search for a device which
529                                          * wasn't there before we
530                                          * asked the target to switch
531                                          * to loader mode
532                                          */
533
534                                         device = null;
535                                         for (;;) {
536                                                 Thread.sleep(100);
537                                                 java.util.List<AltosDevice> devices =
538                                                         AltosUSBDevice.list(AltosLib.product_altusmetrum);
539
540                                                 for (AltosDevice d : devices) {
541                                                         boolean matched = false;
542                                                         System.out.printf("\tfound device %s\n", d.toShortString());
543                                                         for (AltosDevice p : prev_devices)
544                                                                 if (d.equals(p)) {
545                                                                         matched = true;
546                                                                         break;
547                                                                 }
548                                                         if (!matched) {
549                                                                 System.out.printf("Identified new device %s\n", d.toShortString());
550                                                                 device = (AltosUSBDevice) d;
551                                                                 new_device = true;
552                                                                 break;
553                                                         }
554                                                 }
555                                                 if (device != null)
556                                                         break;
557                                         }
558                                 }
559                         } catch (AltosSerialInUseException ee) {
560                                 do_exception(ee);
561                         } catch (FileNotFoundException fe) {
562                                 do_exception(fe);
563                         } catch (IOException ie) {
564                                 do_exception (ie);
565                         } catch (InterruptedException ie) {
566                         }
567                 }
568
569                 public void cancel() {
570                         t.interrupt();
571                 }
572
573                 public open_task(AltosDevice device, open_dialog dialog) {
574                         this.device = device;
575                         this.dialog = dialog;
576                         t = new Thread(this);
577                         t.start();
578                 }
579         }
580
581         class open_dialog
582                 extends AltosUIDialog
583                 implements ActionListener
584         {
585                 AltosUIFrame owner;
586
587                 private JLabel  opening_label;
588                 private JButton cancel_button;
589
590                 boolean done = false;
591
592                 AltosLink link = null;
593
594                 open_task open = null;
595
596                 public void open_exception(Exception e) {
597                         System.out.printf("open_exception\n");
598                         setVisible(false);
599                         exception(e);
600                         done = true;
601                 }
602
603                 public void open_success(AltosLink link) {
604                         System.out.printf("open_success\n");
605                         setVisible(false);
606                         this.link = link;
607                         done = true;
608                 }
609
610                 public void open_failure() {
611                         System.out.printf("open_failure\n");
612                         setVisible(false);
613                         done = true;
614                 }
615
616                 public void open_cancel() {
617                         System.out.printf("open_cancel\n");
618                         setVisible(false);
619                         done = true;
620                 }
621
622                 public AltosLink do_open(open_task open) throws InterruptedException {
623                         this.open = open;
624                         setVisible(true);
625                         return link;
626                 }
627
628                 public void actionPerformed(ActionEvent e) {
629                         String cmd = e.getActionCommand();
630
631                         if (cmd.equals("cancel"))
632                                 if (open != null)
633                                         open.cancel();
634                         done = true;
635                         setVisible(false);
636                 }
637
638                 public open_dialog(AltosUIFrame in_owner) {
639                         super(in_owner, "Open Flash Target Device", true);
640                         owner = in_owner;
641
642                         Container               pane = getContentPane();
643                         GridBagConstraints      c = new GridBagConstraints();
644                         Insets                  i = new Insets(4,4,4,4);
645
646
647                         pane.setLayout(new GridBagLayout());
648
649                         opening_label = new JLabel("Opening Device");
650                         c.fill = GridBagConstraints.HORIZONTAL;
651                         c.anchor = GridBagConstraints.LINE_START;
652                         c.insets = i;
653                         c.weightx = 0;
654                         c.weighty = 0;
655
656                         c.gridx = 0;
657                         c.gridy = 0;
658
659                         pane.add(opening_label, c);
660
661                         cancel_button = new JButton("Cancel");
662                         cancel_button.addActionListener(this);
663                         cancel_button.setActionCommand("cancel");
664
665                         c.gridy = 1;
666                         pane.add(cancel_button, c);
667                         pack();
668                         setLocationRelativeTo(owner);
669                 }
670         }
671
672         private boolean open_device() throws InterruptedException {
673
674                 open_dialog     dialog = new open_dialog(frame);
675
676                 open_task       open = new open_task(device, dialog);
677
678                 link = dialog.do_open(open);
679
680                 return link != null;
681         }
682
683         /*
684          * Execute the steps for flashing
685          * a device. Note that this returns immediately;
686          * this dialog is not modal
687          */
688         void showDialog() {
689                 if (!select_device())
690                         return;
691                 if (!select_source_file())
692                         return;
693                 try {
694                         if (!open_device())
695                                 return;
696                 } catch (InterruptedException ie) {
697                         return;
698                 }
699                 build_dialog();
700                 flash_task      f = new flash_task(this);
701         }
702
703         public static void show(AltosUIFrame frame) {
704                 AltosFlashUI    ui = new AltosFlashUI(frame);
705                 ui.showDialog();
706         }
707
708         public AltosFlashUI(AltosUIFrame in_frame) {
709                 super(in_frame, "Program Altusmetrum Device", false);
710
711                 frame = in_frame;
712         }
713 }