From 48e221cca00c9f925ff57588dd782842705f7a23 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Sat, 20 Oct 2018 17:29:36 -0700 Subject: [PATCH] altoslib: When flashing hardware, pull USB data from device if needed 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 --- altoslib/AltosFlash.java | 6 +++- altoslib/AltosHexsym.java | 6 +++- altoslib/AltosProgrammer.java | 2 +- altoslib/AltosRomconfig.java | 10 +++++- altoslib/AltosSelfFlash.java | 6 +++- altosuilib/AltosDeviceUIDialog.java | 51 +++++++++++++++++++++-------- altosuilib/AltosFlashUI.java | 44 +++++++++++-------------- altosuilib/AltosUSBDevice.java | 9 +++++ 8 files changed, 92 insertions(+), 42 deletions(-) diff --git a/altoslib/AltosFlash.java b/altoslib/AltosFlash.java index e6b05713..434a0265 100644 --- a/altoslib/AltosFlash.java +++ b/altoslib/AltosFlash.java @@ -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; } diff --git a/altoslib/AltosHexsym.java b/altoslib/AltosHexsym.java index b1323484..a2ba6935 100644 --- a/altoslib/AltosHexsym.java +++ b/altoslib/AltosHexsym.java @@ -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 +} diff --git a/altoslib/AltosProgrammer.java b/altoslib/AltosProgrammer.java index 1872392d..f87e334c 100644 --- a/altoslib/AltosProgrammer.java +++ b/altoslib/AltosProgrammer.java @@ -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(); diff --git a/altoslib/AltosRomconfig.java b/altoslib/AltosRomconfig.java index ebeb76f3..00811ce7 100644 --- a/altoslib/AltosRomconfig.java +++ b/altoslib/AltosRomconfig.java @@ -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() { diff --git a/altoslib/AltosSelfFlash.java b/altoslib/AltosSelfFlash.java index 0250cce7..70a5ced5 100644 --- a/altoslib/AltosSelfFlash.java +++ b/altoslib/AltosSelfFlash.java @@ -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; } diff --git a/altosuilib/AltosDeviceUIDialog.java b/altosuilib/AltosDeviceUIDialog.java index 93ab178f..8ea9e233 100644 --- a/altosuilib/AltosDeviceUIDialog.java +++ b/altosuilib/AltosDeviceUIDialog.java @@ -24,27 +24,39 @@ import java.awt.event.*; public class AltosDeviceUIDialog extends AltosDeviceDialog { + boolean include_bluetooth; + public AltosDevice[] devices() { java.util.List usb_devices = AltosUSBDevice.list(product); int num_devices = usb_devices.size(); - java.util.List bt_devices = AltosBTKnown.bt_known().list(product); - num_devices += bt_devices.size(); + + java.util.List 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); + } } diff --git a/altosuilib/AltosFlashUI.java b/altosuilib/AltosFlashUI.java index b91776aa..ffb39b8c 100644 --- a/altosuilib/AltosFlashUI.java +++ b/altosuilib/AltosFlashUI.java @@ -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; } } diff --git a/altosuilib/AltosUSBDevice.java b/altosuilib/AltosUSBDevice.java index f43d6bef..e4b94eea 100644 --- a/altosuilib/AltosUSBDevice.java +++ b/altosuilib/AltosUSBDevice.java @@ -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; -- 2.30.2