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