From 22800dc094797e1e0ad99124198809d0360f7556 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Tue, 17 Aug 2010 18:22:28 -0700 Subject: [PATCH 1/1] altosui: Select devices by USB vendor/product ID. Because Win7 doesn't expose the product name, we're swtiching to using the USB idProduct/idVendor values. This patch adds support for selecting devices by those new IDs. Signed-off-by: Keith Packard --- ao-tools/altosui/AltosDevice.java | 72 +++++++++++++++++++++-- ao-tools/altosui/AltosDeviceDialog.java | 8 +-- ao-tools/altosui/AltosEepromDownload.java | 7 ++- ao-tools/altosui/AltosUI.java | 2 +- 4 files changed, 75 insertions(+), 14 deletions(-) diff --git a/ao-tools/altosui/AltosDevice.java b/ao-tools/altosui/AltosDevice.java index f488174c..3daf0742 100644 --- a/ao-tools/altosui/AltosDevice.java +++ b/ao-tools/altosui/AltosDevice.java @@ -22,16 +22,76 @@ import libaltosJNI.*; public class AltosDevice extends altos_device { + static boolean initialized = false; + static { + try { + System.loadLibrary("altos"); + libaltos.altos_init(); + initialized = true; + } catch (UnsatisfiedLinkError e) { + System.err.println("Native library failed to load.\n" + e); + } + } + public final static int TeleMetrum = libaltosConstants.USB_PRODUCT_TELEMETRUM; + public final static int TeleDongle = libaltosConstants.USB_PRODUCT_TELEDONGLE; + public final static int TeleTerra = libaltosConstants.USB_PRODUCT_TELETERRA; + public final static int Any = 0x10000; + public final static int BaseStation = 0x10000 + 1; + public String toString() { + String name = getName(); + if (name == null) + name = "Altus Metrum"; return String.format("%-20.20s %4d %s", - getProduct(), getSerial(), getPath()); + getName(), getSerial(), getPath()); } - static { - System.loadLibrary("altos"); - libaltos.altos_init(); + public boolean isAltusMetrum() { + if (getVendor() != libaltosConstants.USB_VENDOR_ALTUSMETRUM) + return false; + if (getProduct() < libaltosConstants.USB_PRODUCT_ALTUSMETRUM_MIN) + return false; + if (getProduct() > libaltosConstants.USB_PRODUCT_ALTUSMETRUM_MAX) + return false; + return true; } - static AltosDevice[] list(String product) { + + public boolean matchProduct(int want_product) { + + if (want_product == Any) + return true; + + if (want_product == BaseStation) + return matchProduct(TeleDongle) || matchProduct(TeleTerra); + + if (!isAltusMetrum()) + return false; + + int have_product = getProduct(); + + if (want_product == have_product) + return true; + + if (have_product != libaltosConstants.USB_PRODUCT_ALTUSMETRUM) + return false; + + String name = getName(); + + if (name == null) + return false; + if (want_product == libaltosConstants.USB_PRODUCT_TELEMETRUM) + return name.startsWith("TeleMetrum"); + if (want_product == libaltosConstants.USB_PRODUCT_TELEDONGLE) + return name.startsWith("TeleDongle"); + if (want_product == libaltosConstants.USB_PRODUCT_TELETERRA) + return name.startsWith("TeleTerra"); + return false; + } + + static AltosDevice[] list(int product) { + if (!initialized) + return null; + SWIGTYPE_p_altos_list list = libaltos.altos_list_start(); ArrayList device_list = new ArrayList(); @@ -42,7 +102,7 @@ public class AltosDevice extends altos_device { AltosDevice device = new AltosDevice(); if (libaltos.altos_list_next(list, device) == 0) break; - if (product == null || device.getProduct().startsWith(product)) + if (device.matchProduct(product)) device_list.add(device); } libaltos.altos_list_finish(list); diff --git a/ao-tools/altosui/AltosDeviceDialog.java b/ao-tools/altosui/AltosDeviceDialog.java index c60bd7c3..3df4c6eb 100644 --- a/ao-tools/altosui/AltosDeviceDialog.java +++ b/ao-tools/altosui/AltosDeviceDialog.java @@ -31,16 +31,16 @@ import altosui.AltosDevice; public class AltosDeviceDialog extends JDialog implements ActionListener { private static AltosDeviceDialog dialog; - private static altos_device value = null; + private static AltosDevice value = null; private JList list; - public static altos_device show (Component frameComp, String product) { + public static AltosDevice show (Component frameComp, int product) { Frame frame = JOptionPane.getFrameForComponent(frameComp); AltosDevice[] devices; devices = AltosDevice.list(product); - if (devices != null & devices.length > 0) { + if (devices != null && devices.length > 0) { value = null; dialog = new AltosDeviceDialog(frame, frameComp, devices, @@ -153,7 +153,7 @@ public class AltosDeviceDialog extends JDialog implements ActionListener { //Handle clicks on the Set and Cancel buttons. public void actionPerformed(ActionEvent e) { if ("select".equals(e.getActionCommand())) - AltosDeviceDialog.value = (altos_device)(list.getSelectedValue()); + AltosDeviceDialog.value = (AltosDevice)(list.getSelectedValue()); AltosDeviceDialog.dialog.setVisible(false); } diff --git a/ao-tools/altosui/AltosEepromDownload.java b/ao-tools/altosui/AltosEepromDownload.java index f2fcd09e..c2a8d25e 100644 --- a/ao-tools/altosui/AltosEepromDownload.java +++ b/ao-tools/altosui/AltosEepromDownload.java @@ -84,7 +84,7 @@ public class AltosEepromDownload implements Runnable { } JFrame frame; - altos_device device; + AltosDevice device; AltosSerial serial_line; boolean remote; Thread eeprom_thread; @@ -251,7 +251,7 @@ public class AltosEepromDownload implements Runnable { public AltosEepromDownload(JFrame given_frame) { frame = given_frame; - device = AltosDeviceDialog.show(frame, null); + device = AltosDeviceDialog.show(frame, AltosDevice.Any); serial_line = new AltosSerial(); remote = false; @@ -259,7 +259,8 @@ public class AltosEepromDownload implements Runnable { if (device != null) { try { serial_line.open(device); - if (!device.getProduct().startsWith("TeleMetrum")) + String name = device.getName(); + if (!device.matchProduct(AltosDevice.TeleMetrum)) remote = true; eeprom_thread = new Thread(this); eeprom_thread.start(); diff --git a/ao-tools/altosui/AltosUI.java b/ao-tools/altosui/AltosUI.java index 7d5ac93a..b96e16a6 100644 --- a/ao-tools/altosui/AltosUI.java +++ b/ao-tools/altosui/AltosUI.java @@ -413,7 +413,7 @@ public class AltosUI extends JFrame { } private void ConnectToDevice() { - altos_device device = AltosDeviceDialog.show(AltosUI.this, "TeleDongle"); + AltosDevice device = AltosDeviceDialog.show(AltosUI.this, AltosDevice.BaseStation); if (device != null) { try { -- 2.30.2