altoslib: When flashing hardware, pull USB data from device if needed
authorKeith Packard <keithp@keithp.com>
Sun, 21 Oct 2018 00:29:36 +0000 (17:29 -0700)
committerKeith Packard <keithp@keithp.com>
Sun, 21 Oct 2018 00:31:57 +0000 (17:31 -0700)
If we fail to extract USB vid/pid and product values from the device
ROM, use the data discovered over USB when the device was originally
discovered.

Also, use the USB product to select appropriate .ihx files instead of
only using the USB vid/pid. This will help people avoid using the
wrong file when reflashing devices.

Signed-off-by: Keith Packard <keithp@keithp.com>
altoslib/AltosFlash.java
altoslib/AltosHexsym.java
altoslib/AltosProgrammer.java
altoslib/AltosRomconfig.java
altoslib/AltosSelfFlash.java
altosuilib/AltosDeviceUIDialog.java
altosuilib/AltosFlashUI.java
altosuilib/AltosUSBDevice.java

index e6b0571..434a026 100644 (file)
@@ -331,9 +331,13 @@ public class AltosFlash extends AltosProgrammer {
                rom_config = romconfig;
        }
 
-       public AltosRomconfig target_romconfig() throws InterruptedException {
+       public AltosRomconfig target_romconfig(AltosUsbId usb_id, String usb_product) throws InterruptedException {
                if (!check_rom_config())
                        return null;
+               if (rom_config.usb_id == null)
+                       rom_config.usb_id = usb_id;
+               if (rom_config.usb_product == null)
+                       rom_config.usb_product = usb_product;
                return rom_config;
        }
 
index b132348..a2ba693 100644 (file)
@@ -24,8 +24,12 @@ public class AltosHexsym {
 
        final static long invalid_addr = 0xffffffff;
 
+       public String toString() {
+               return String.format("%s:0x%x", name, address);
+       }
+
        public AltosHexsym(String name, long address) {
                this.name = name;
                this.address = address;
        }
-}
\ No newline at end of file
+}
index 1872392..f87e334 100644 (file)
@@ -28,7 +28,7 @@ public abstract class AltosProgrammer {
 
        abstract public void abort();
 
-       abstract public AltosRomconfig target_romconfig() throws InterruptedException;
+       abstract public AltosRomconfig target_romconfig(AltosUsbId usb_id, String usb_product) throws InterruptedException;
 
        abstract public AltosRomconfig image_romconfig();
 
index ebeb76f..00811ce 100644 (file)
@@ -273,6 +273,14 @@ public class AltosRomconfig implements AltosUnitInfoListener {
                        throw new IOException("writing new rom config failed\n");
        }
 
+       public String toString() {
+               return String.format("valid %b version %d serial %d radio %d usb %04x:%04x %s",
+                                    valid, version, serial_number, radio_calibration,
+                                    usb_id == null ? 0 : usb_id.vid,
+                                    usb_id == null ? 0 : usb_id.pid,
+                                    usb_product == null ? "unknown" : usb_product);
+       }
+
        public AltosRomconfig(int in_serial_number, int in_radio_calibration) {
                valid = true;
                version = 1;
@@ -282,7 +290,7 @@ public class AltosRomconfig implements AltosUnitInfoListener {
        }
 
        public boolean valid() {
-               return valid && serial_number != 0;
+               return valid;
        }
 
        public AltosRomconfig() {
index 0250cce..70a5ced 100644 (file)
@@ -184,9 +184,13 @@ public class AltosSelfFlash extends AltosProgrammer {
                rom_config = romconfig;
        }
 
-       public AltosRomconfig target_romconfig() throws InterruptedException {
+       public AltosRomconfig target_romconfig(AltosUsbId usb_id, String usb_product) throws InterruptedException {
                if (!check_rom_config())
                        return null;
+               if (rom_config.usb_id == null)
+                       rom_config.usb_id = usb_id;
+               if (rom_config.usb_product == null)
+                       rom_config.usb_product = usb_product;
                return rom_config;
        }
 
index 93ab178..8ea9e23 100644 (file)
@@ -24,27 +24,39 @@ import java.awt.event.*;
 
 public class AltosDeviceUIDialog extends AltosDeviceDialog {
 
+       boolean include_bluetooth;
+
        public AltosDevice[] devices() {
                java.util.List<AltosDevice>     usb_devices = AltosUSBDevice.list(product);
                int                             num_devices = usb_devices.size();
-               java.util.List<AltosDevice>     bt_devices = AltosBTKnown.bt_known().list(product);
-               num_devices += bt_devices.size();
+
+               java.util.List<AltosDevice>     bt_devices = null;
+
+               if (include_bluetooth) {
+                       bt_devices = AltosBTKnown.bt_known().list(product);
+                       num_devices += bt_devices.size();
+               }
+
                AltosDevice[]                   devices = new AltosDevice[num_devices];
 
                for (int i = 0; i < usb_devices.size(); i++)
                        devices[i] = usb_devices.get(i);
-               int off = usb_devices.size();
-               for (int j = 0; j < bt_devices.size(); j++)
-                       devices[off + j] = bt_devices.get(j);
+               if (include_bluetooth) {
+                       int off = usb_devices.size();
+                       for (int j = 0; j < bt_devices.size(); j++)
+                               devices[off + j] = bt_devices.get(j);
+               }
                return devices;
        }
 
        public void add_bluetooth() {
-               JButton manage_bluetooth_button = new JButton("Manage Bluetooth");
-               manage_bluetooth_button.setActionCommand("manage");
-               manage_bluetooth_button.addActionListener(this);
-               buttonPane.add(manage_bluetooth_button);
-               buttonPane.add(Box.createRigidArea(new Dimension(10, 0)));
+               if (include_bluetooth) {
+                       JButton manage_bluetooth_button = new JButton("Manage Bluetooth");
+                       manage_bluetooth_button.setActionCommand("manage");
+                       manage_bluetooth_button.addActionListener(this);
+                       buttonPane.add(manage_bluetooth_button);
+                       buttonPane.add(Box.createRigidArea(new Dimension(10, 0)));
+               }
        }
 
        public void actionPerformed(ActionEvent e) {
@@ -55,16 +67,29 @@ public class AltosDeviceUIDialog extends AltosDeviceDialog {
                }
        }
 
-       public AltosDeviceUIDialog (Frame in_frame, Component location, int in_product) {
+       public AltosDeviceUIDialog (Frame in_frame, Component location, int in_product, boolean include_bluetooth) {
                super(in_frame, location, in_product);
+               this.include_bluetooth = include_bluetooth;
        }
 
-       public static AltosDevice show (Component frameComp, int product) {
+       public AltosDeviceUIDialog (Frame in_frame, Component location, int in_product) {
+               this(in_frame, location, in_product, true);
+       }
+
+       public static AltosDevice show (Component frameComp, int product, boolean include_bluetooth) {
                Frame                   frame = JOptionPane.getFrameForComponent(frameComp);
                AltosDeviceUIDialog     dialog;
 
-               dialog = new AltosDeviceUIDialog(frame, frameComp, product);
+               dialog = new AltosDeviceUIDialog(frame, frameComp, product, include_bluetooth);
                dialog.setVisible(true);
                return dialog.getValue();
        }
+
+       public static AltosDevice show (Component frameComp, int product) {
+               return show(frameComp, product, true);
+       }
+
+       public static AltosUSBDevice show_usb (Component frameComp, int product) {
+               return (AltosUSBDevice) show(frameComp, product, false);
+       }
 }
index b91776a..ffb39b8 100644 (file)
@@ -45,7 +45,7 @@ public class AltosFlashUI
        File            file;
 
        // Debug connection
-       AltosDevice     device;
+       AltosUSBDevice  device;
 
        AltosLink       link;
 
@@ -205,14 +205,20 @@ public class AltosFlashUI
        }
 
        static class AltosHexfileFilter extends javax.swing.filechooser.FileFilter {
-               int product;
                String head;
                String description;
 
-               public AltosHexfileFilter(int product, String head, String description) {
-                       this.product = product;
-                       this.head = head;
-                       this.description = description;
+               public AltosHexfileFilter(String usb_product) {
+                       int l;
+
+                       /* Trim off any trailing variants (1.0a vs 1.0) */
+                       for (l = usb_product.length(); l > 0; l--) {
+                               char c = usb_product.charAt(l-1);
+                               if (c < 'a' || 'z' < c)
+                                       break;
+                       }
+                       head = usb_product.substring(0, l).toLowerCase();
+                       description = String.format("%s Image File", usb_product);
                }
 
                public boolean accept(File file) {
@@ -224,14 +230,6 @@ public class AltosFlashUI
                }
        }
 
-       static AltosHexfileFilter[] filters = {
-               new AltosHexfileFilter(AltosLib.product_telemetrum, "telemetrum", "TeleMetrum Image"),
-               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() {
                JFileChooser    hexfile_chooser = new JFileChooser();
 
@@ -241,18 +239,14 @@ public class AltosFlashUI
 
                hexfile_chooser.setDialogTitle("Select Flash Image");
 
-               for (int i = 0; i < filters.length; i++) {
-                       hexfile_chooser.addChoosableFileFilter(filters[i]);
-               }
                javax.swing.filechooser.FileFilter ihx_filter = new FileNameExtensionFilter("Flash Image", "ihx");
                hexfile_chooser.addChoosableFileFilter(ihx_filter);
                hexfile_chooser.setFileFilter(ihx_filter);
 
                if (!is_pair_programmed() && !device.matchProduct(AltosLib.product_altusmetrum)) {
-                       for (int i = 0; i < filters.length; i++) {
-                               if (device != null && device.matchProduct(filters[i].product))
-                                       hexfile_chooser.setFileFilter(filters[i]);
-                       }
+                       AltosHexfileFilter filter = new AltosHexfileFilter(device.usb_product());
+                       hexfile_chooser.addChoosableFileFilter(filter);
+                       hexfile_chooser.setFileFilter(filter);
                }
 
                int returnVal = hexfile_chooser.showOpenDialog(frame);
@@ -270,7 +264,7 @@ public class AltosFlashUI
        boolean select_device() {
                int     product = AltosLib.product_any;
 
-               device = AltosDeviceUIDialog.show(frame, AltosLib.product_any);
+               device = AltosDeviceUIDialog.show_usb(frame, AltosLib.product_any);
 
                if (device == null)
                        return false;
@@ -393,10 +387,12 @@ public class AltosFlashUI
                                else
                                        programmer = new AltosSelfFlash(ui.file, link, this);
 
-                               final AltosRomconfig    current_config = programmer.target_romconfig();
+                               final AltosRomconfig    current_config = programmer.target_romconfig(device.usb_id(), device.usb_product());
 
                                final AltosRomconfig    image_config = programmer.image_romconfig();
 
+                               System.out.printf("product %s current %s image %s\n", device.usb_product(), current_config, image_config);
+
                                final Semaphore await_rom_config = new Semaphore(0);
                                SwingUtilities.invokeLater(new Runnable() {
                                                public void run() {
@@ -539,7 +535,7 @@ public class AltosFlashUI
                                                                }
                                                        if (!matched) {
                                                                System.out.printf("Identified new device %s\n", d.toShortString());
-                                                               device = d;
+                                                               device = (AltosUSBDevice) d;
                                                                break;
                                                        }
                                                }
index f43d6be..e4b94ee 100644 (file)
@@ -20,6 +20,7 @@ package org.altusmetrum.altosuilib_13;
 
 import java.util.*;
 import libaltosJNI.*;
+import org.altusmetrum.altoslib_13.*;
 
 public class AltosUSBDevice  extends altos_device implements AltosDevice {
 
@@ -98,6 +99,14 @@ public class AltosUSBDevice  extends altos_device implements AltosDevice {
                return getVendor() ^ getProduct() ^ getSerial() ^ getPath().hashCode();
        }
 
+       public AltosUsbId usb_id() {
+               return new AltosUsbId(getVendor(), getProduct());
+       }
+
+       public String usb_product() {
+               return getName();
+       }
+
        public boolean equals(Object o) {
                if (o == null)
                        return false;