altosuilib: Show a more useful message when the flash target device is unknown
[fw/altos] / altosuilib / AltosFlashUI.java
index 0ed4e75df4616c90fff87de9cbe12ecb6b2adf61..6dc80272a6772089517ef07bf9da02b0551efd19 100644 (file)
@@ -3,7 +3,8 @@
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; version 2 of the License.
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful, but
  * WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -15,7 +16,7 @@
  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
  */
 
-package org.altusmetrum.altosuilib_3;
+package org.altusmetrum.altosuilib_13;
 
 import java.awt.*;
 import java.awt.event.*;
@@ -23,7 +24,7 @@ import javax.swing.*;
 import javax.swing.filechooser.FileNameExtensionFilter;
 import java.io.*;
 import java.util.concurrent.*;
-import org.altusmetrum.altoslib_5.*;
+import org.altusmetrum.altoslib_13.*;
 
 public class AltosFlashUI
        extends AltosUIDialog
@@ -54,33 +55,48 @@ public class AltosFlashUI
        // Flash controller
        AltosProgrammer programmer;
 
-       private static String[] pair_programmed = {
+       private static final String[] pair_programmed_files = {
                "teleballoon",
-               "telebt",
-               "teledongle",
-               "telefire",
+               "telebt-v1",
+               "teledongle-v0",
+               "telefire-v0",
                "telemetrum-v0",
                "telemetrum-v1",
-               "telemini",
+               "telemini-v1",
                "telenano",
                "teleshield",
                "teleterra"
        };
 
+       private static final String[] pair_programmed_devices = {
+               "TeleBalloon",
+               "TeleBT-v1",
+               "TeleDongle-v0",
+               "TeleFire-v0",
+               "TeleFire",
+               "TeleMetrum-v0",
+               "TeleMetrum-v1",
+               "TeleMini-v1",
+               "TeleNano",
+               "TeleShield",
+               "TeleTerra"
+       };
+
        private boolean is_pair_programmed() {
 
                if (file != null) {
                        String  name = file.getName();
-                       for (int i = 0; i < pair_programmed.length; i++) {
-                               if (name.startsWith(pair_programmed[i]))
+                       for (int i = 0; i < pair_programmed_files.length; i++) {
+                               if (name.startsWith(pair_programmed_files[i]))
                                        return true;
                        }
                }
                if (device != null) {
-                       if (!device.matchProduct(AltosLib.product_altusmetrum) &&
-                           (device.matchProduct(AltosLib.product_teledongle) ||
-                            device.matchProduct(AltosLib.product_telebt)))
-                               return true;
+                       String  name = device.toString();
+                       for (int i = 0; i < pair_programmed_devices.length; i++) {
+                               if (name.startsWith(pair_programmed_devices[i]))
+                                       return true;
+                       }
                }
                return false;
        }
@@ -213,6 +229,7 @@ public class AltosFlashUI
                new AltosHexfileFilter(AltosLib.product_teledongle, "teledongle", "TeleDongle Image"),
                new AltosHexfileFilter(AltosLib.product_telemega, "telemega", "TeleMega Image"),
                new AltosHexfileFilter(AltosLib.product_easymini, "easymini", "EasyMini Image"),
+               new AltosHexfileFilter(AltosLib.product_easymega, "easymega", "EasyMega Image"),
        };
 
        boolean select_source_file() {
@@ -260,8 +277,54 @@ public class AltosFlashUI
                return true;
        }
 
-       boolean update_rom_config_info(AltosRomconfig existing_config) {
+       boolean rom_config_matches (AltosRomconfig a, AltosRomconfig b) {
+               if (a == null || b == null)
+                       return (a == null && b == null);
+
+               if (!a.valid || !b.valid)
+                       return false;
+
+               if (a.usb_id != null && b.usb_id != null &&
+                   (a.usb_id.vid != b.usb_id.vid ||
+                    a.usb_id.pid != b.usb_id.pid))
+                       return false;
+
+               if (a.usb_product != null && b.usb_product != null &&
+                   !a.usb_product.equals(b.usb_product))
+                       return false;
+
+               return true;
+       }
+
+       boolean update_rom_config_info(AltosRomconfig existing_config, AltosRomconfig image_config) {
                AltosRomconfig  new_config;
+
+               if (!rom_config_matches(existing_config, image_config)) {
+                       int ret;
+                       if (existing_config == null || !existing_config.valid) {
+                               ret = JOptionPane.showConfirmDialog(this,
+                                                                   String.format("Cannot determine target device type\nImage is %04x:%04x %s\nFlash anyways?",
+                                                                                 image_config.usb_id.vid,
+                                                                                 image_config.usb_id.pid,
+                                                                                 image_config.usb_product),
+                                                                   "Unknown Target Device",
+                                                                   JOptionPane.YES_NO_OPTION);
+                       } else {
+                               ret = JOptionPane.showConfirmDialog(this,
+                                                                   String.format("Device is %04x:%04x %s\nImage is %04x:%04x %s\nFlash anyways?",
+                                                                                 existing_config.usb_id.vid,
+                                                                                 existing_config.usb_id.pid,
+                                                                                 existing_config.usb_product,
+                                                                                 image_config.usb_id.vid,
+                                                                                 image_config.usb_id.pid,
+                                                                                 image_config.usb_product),
+                                                                   "Image doesn't match Device",
+                                                                   JOptionPane.YES_NO_OPTION);
+                       }
+                       if (ret != JOptionPane.YES_OPTION)
+                               return false;
+               }
+
                new_config = AltosRomconfigUI.show(frame, existing_config);
                if (new_config == null)
                        return false;
@@ -319,13 +382,15 @@ public class AltosFlashUI
                                else
                                        programmer = new AltosSelfFlash(ui.file, link, this);
 
-                               final AltosRomconfig    current_config = programmer.romconfig();
+                               final AltosRomconfig    current_config = programmer.target_romconfig();
+
+                               final AltosRomconfig    image_config = programmer.image_romconfig();
 
                                final Semaphore await_rom_config = new Semaphore(0);
                                SwingUtilities.invokeLater(new Runnable() {
                                                public void run() {
                                                        ui.programmer = programmer;
-                                                       ui.update_rom_config_info(current_config);
+                                                       ui.update_rom_config_info(current_config, image_config);
                                                        await_rom_config.release();
                                                }
                                        });