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 e6b0571342212f93d763a746ab1d17753b672640..434a02653a2eec993b55e794222f416e6a554404 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 b1323484d96339646bb4a3690746c02a7c7d4625..a2ba6935f0ee58ac895ef6ed477bffc99449566a 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 1872392d805c26136c22973938d54916d4c98dca..f87e334c26a79e7576b1d718fbbd33de64efe713 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 ebeb76f32b54375db7ed9b1be2469f3658f0f949..00811ce7e4702d89a69da7289bb060096844642d 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 0250cce741a6d1f3c64713418f5a953b12f04412..70a5ced5e8db0ffc551946e0740813fc4824cc2d 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 93ab178f579150b38201a3c231ac26fbce823fec..8ea9e233e330a71c53bb984edac349896a2e4161 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 b91776aa198ebb120c4b0a633c3a4d21ff1830cf..ffb39b8c2aae4e4e7b2035a9e780355114808bf8 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 f43d6befda46e57ec9ee26f0d105452308afe9b9..e4b94eeaed77efd12645dc208f57452f31fee2af 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;