Add Linux device discovery
authorKeith Packard <keithp@keithp.com>
Tue, 6 Apr 2010 05:21:46 +0000 (22:21 -0700)
committerKeith Packard <keithp@keithp.com>
Tue, 6 Apr 2010 05:21:46 +0000 (22:21 -0700)
AltosDeviceLinux.java scans /proc to locate suitable devices. This
will be hooked up to the UI shortly.

ao-tools/altosui/AltosDevice.java [new file with mode: 0644]
ao-tools/altosui/AltosDeviceLinux.java [new file with mode: 0644]
ao-tools/altosui/AltosUI.java
ao-tools/altosui/Makefile

diff --git a/ao-tools/altosui/AltosDevice.java b/ao-tools/altosui/AltosDevice.java
new file mode 100644 (file)
index 0000000..66800c5
--- /dev/null
@@ -0,0 +1,30 @@
+/*
+ * Copyright © 2010 Keith Packard <keithp@keithp.com>
+ *
+ * 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.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ */
+
+package altosui;
+import java.lang.*;
+import java.util.*;
+
+public class AltosDevice {
+       String  tty;    /* suitable to be passed to AltosSerial.connect */
+       String  manufacturer;
+       String  product;
+       int     serial;
+       int     idProduct;
+       int     idVendor;
+
+}
\ No newline at end of file
diff --git a/ao-tools/altosui/AltosDeviceLinux.java b/ao-tools/altosui/AltosDeviceLinux.java
new file mode 100644 (file)
index 0000000..b777462
--- /dev/null
@@ -0,0 +1,171 @@
+/*
+ * Copyright © 2010 Keith Packard <keithp@keithp.com>
+ *
+ * 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.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ */
+
+package altosui;
+import java.lang.*;
+import java.io.*;
+import java.util.*;
+import altosui.AltosDeviceName;
+import altosui.AltosDeviceNameLinux;
+import altosui.AltosDevice;
+
+public class AltosDeviceLinux extends AltosDevice {
+
+       String load_string(File file) {
+               try {
+                       FileInputStream in = new FileInputStream(file);
+                       String result = "";
+                       int c;
+                       try {
+                               while ((c = in.read()) != -1) {
+                                       if (c == '\n')
+                                               break;
+                                       result = result + (char) c;
+                               }
+                               return result;
+                       } catch (IOException ee) {
+                               return "";
+                       }
+               } catch (FileNotFoundException ee) {
+                       return "";
+               }
+       }
+       String load_string(File dir, String name) {
+               return load_string(new File(dir, name));
+       }
+
+       int load_hex(File file) {
+               try {
+                       return Integer.parseInt(load_string(file).trim(), 16);
+               } catch (NumberFormatException ee) {
+                       return -1;
+               }
+       }
+
+       int load_hex(File dir, String name) {
+               return load_hex(new File(dir, name));
+       }
+
+       int load_dec(File file) {
+               try {
+                       return Integer.parseInt(load_string(file).trim());
+               } catch (NumberFormatException ee) {
+                       return -1;
+               }
+       }
+
+       int load_dec(File dir, String name) {
+               return load_dec(new File(dir, name));
+       }
+
+       String usb_tty(File sys_dir) {
+               String base = sys_dir.getName();
+               int num_configs = load_hex(sys_dir, "bNumConfigurations");
+               int num_inters = load_hex(sys_dir, "bNumInterfaces");
+               for (int config = 1; config <= num_configs; config++) {
+                       for (int inter = 0; inter < num_inters; inter++) {
+                               String endpoint_base = String.format("%s:%d.%d",
+                                                                    base, config, inter);
+                               File endpoint_full = new File(sys_dir, endpoint_base);
+
+                               File[] namelist;
+
+                               /* Check for tty:ttyACMx style names */
+                               class tty_colon_filter implements FilenameFilter {
+                                       public boolean accept(File dir, String name) {
+                                               return name.startsWith("tty:");
+                                       }
+                               }
+                               namelist = endpoint_full.listFiles(new tty_colon_filter());
+                               if (namelist != null && namelist.length > 0)
+                                       return new File ("/dev", namelist[0].getName().substring(4)).getPath();
+
+                               /* Check for tty/ttyACMx style names */
+                               class tty_filter implements FilenameFilter {
+                                       public boolean accept(File dir, String name) {
+                                               return name.startsWith("tty");
+                                       }
+                               }
+                               File tty_dir = new File(endpoint_full, "tty");
+                               namelist = tty_dir.listFiles(new tty_filter());
+                               if (namelist != null && namelist.length > 0)
+                                       return new File ("/dev", namelist[0].getName()).getPath();
+                       }
+               }
+               return null;
+       }
+
+       public AltosDeviceLinux (File sys) {
+               sys = sys;
+               manufacturer = load_string(sys, "manufacturer");
+               product = load_string(sys, "product");
+               serial = load_dec(sys, "serial");
+               idProduct = load_hex(sys, "idProduct");
+               idVendor = load_hex(sys, "idVendor");
+               tty = usb_tty(sys);
+       }
+
+       public String toString() {
+               return manufacturer + " " + product + " " + serial + " " + idProduct + " " + idVendor + " " + tty;
+       }
+       static public AltosDeviceLinux[] list() {
+               LinkedList<AltosDeviceLinux> devices = new LinkedList<AltosDeviceLinux>();
+
+               class dev_filter implements FilenameFilter{
+                       public boolean accept(File dir, String name) {
+                               for (int i = 0; i < name.length(); i++) {
+                                       char c = name.charAt(i);
+                                       if (Character.isDigit(c))
+                                               continue;
+                                       if (c == '-')
+                                               continue;
+                                       if (c == '.' && i != 1)
+                                               continue;
+                                       return false;
+                               }
+                               return true;
+                       }
+               }
+
+               File usb_devices = new File("/sys/bus/usb/devices");
+               File[] devs = usb_devices.listFiles(new dev_filter());
+               if (devs != null) {
+                       for (int e = 0; e < devs.length; e++) {
+                               AltosDeviceLinux        dev = new AltosDeviceLinux(devs[e]);
+                               if (dev.idVendor == 0xfffe && dev.tty != null) {
+                                       devices.add(dev);
+                               }
+                       }
+               }
+               AltosDeviceLinux[] foo = new AltosDeviceLinux[devices.size()];
+               for (int e = 0; e < devices.size(); e++) {
+                       foo[e] = devices.get(e);
+                       System.out.println("Device " + foo[e]);
+               }
+               return foo;
+       }
+
+       static public AltosDeviceLinux[] list(String model) {
+               AltosDeviceLinux[] devices = list();
+               LinkedList<AltosDeviceLinux> subset = new LinkedList<AltosDeviceLinux>();
+               for (int i = 0; i < devices.length; i++) {
+                       if (devices[i].product.startsWith(model))
+                               subset.add(devices[i]);
+               }
+               return (AltosDeviceLinux[]) subset.toArray();
+       }
+}
index a51ca1eb37f2b38a32ede234a4b224b2584dec0d..54016ac5d8df112c17378d5ca8d4aa3d7a6ab129 100644 (file)
@@ -682,6 +682,10 @@ public class AltosUI extends JFrame {
 
        }
        public static void main(final String[] args) {
+               AltosDevice[] devices = AltosDeviceLinux.list();
+               for (int i = 0; i < devices.length; i++)
+                       System.out.printf("Model: %s Serial: %d Tty: %s\n",
+                                         devices[i].product, devices[i].serial, devices[i].tty);
                AltosUI altosui = new AltosUI();
                altosui.setVisible(true);
        }
index 74b29f7f5acbab47f2c383caad6fbe15d033a548..fcb259ebe9d0648cc5a2a598fa7fc9bf7ca17e64 100644 (file)
@@ -12,9 +12,7 @@ CLASSFILES=\
        AltosTelemetry.class \
        AltosUI.class \
        AltosDevice.class \
-       AltosDeviceName.class \
        AltosDeviceLinux.class \
-       AltosDeviceNameLinux.class
 
 JAVAFLAGS=-Xlint:unchecked