2 * Copyright © 2010 Keith Packard <keithp@keithp.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
22 import altosui.AltosDevice;
24 public class AltosDeviceLinux extends AltosDevice {
26 String load_string(File file) {
28 FileInputStream in = new FileInputStream(file);
32 while ((c = in.read()) != -1) {
35 result = result + (char) c;
38 } catch (IOException ee) {
41 } catch (FileNotFoundException ee) {
45 String load_string(File dir, String name) {
46 return load_string(new File(dir, name));
49 int load_hex(File file) {
51 return Integer.parseInt(load_string(file).trim(), 16);
52 } catch (NumberFormatException ee) {
57 int load_hex(File dir, String name) {
58 return load_hex(new File(dir, name));
61 int load_dec(File file) {
63 return Integer.parseInt(load_string(file).trim());
64 } catch (NumberFormatException ee) {
69 int load_dec(File dir, String name) {
70 return load_dec(new File(dir, name));
73 String usb_tty(File sys_dir) {
74 String base = sys_dir.getName();
75 int num_configs = load_hex(sys_dir, "bNumConfigurations");
76 int num_inters = load_hex(sys_dir, "bNumInterfaces");
77 for (int config = 1; config <= num_configs; config++) {
78 for (int inter = 0; inter < num_inters; inter++) {
79 String endpoint_base = String.format("%s:%d.%d",
81 File endpoint_full = new File(sys_dir, endpoint_base);
85 /* Check for tty:ttyACMx style names */
86 class tty_colon_filter implements FilenameFilter {
87 public boolean accept(File dir, String name) {
88 return name.startsWith("tty:");
91 namelist = endpoint_full.listFiles(new tty_colon_filter());
92 if (namelist != null && namelist.length > 0)
93 return new File ("/dev", namelist[0].getName().substring(4)).getPath();
95 /* Check for tty/ttyACMx style names */
96 class tty_filter implements FilenameFilter {
97 public boolean accept(File dir, String name) {
98 return name.startsWith("tty");
101 File tty_dir = new File(endpoint_full, "tty");
102 namelist = tty_dir.listFiles(new tty_filter());
103 if (namelist != null && namelist.length > 0)
104 return new File ("/dev", namelist[0].getName()).getPath();
110 public AltosDeviceLinux (File sys) {
112 manufacturer = load_string(sys, "manufacturer");
113 product = load_string(sys, "product");
114 serial = load_dec(sys, "serial");
115 idProduct = load_hex(sys, "idProduct");
116 idVendor = load_hex(sys, "idVendor");
120 public String toString() {
121 return String.format("%-20s %6d %-15s", product, serial, tty == null ? "" : tty);
123 static public AltosDeviceLinux[] list() {
124 LinkedList<AltosDeviceLinux> devices = new LinkedList<AltosDeviceLinux>();
126 class dev_filter implements FilenameFilter{
127 public boolean accept(File dir, String name) {
128 for (int i = 0; i < name.length(); i++) {
129 char c = name.charAt(i);
130 if (Character.isDigit(c))
134 if (c == '.' && i != 1)
142 File usb_devices = new File("/sys/bus/usb/devices");
143 File[] devs = usb_devices.listFiles(new dev_filter());
145 for (int e = 0; e < devs.length; e++) {
146 AltosDeviceLinux dev = new AltosDeviceLinux(devs[e]);
147 if (dev.idVendor == 0xfffe && dev.tty != null) {
152 AltosDeviceLinux[] foo = new AltosDeviceLinux[devices.size()];
153 for (int e = 0; e < devices.size(); e++)
154 foo[e] = devices.get(e);
158 static public AltosDeviceLinux[] list(String model) {
159 AltosDeviceLinux[] devices = list();
161 LinkedList<AltosDeviceLinux> subset = new LinkedList<AltosDeviceLinux>();
162 for (int i = 0; i < devices.length; i++) {
163 if (devices[i].product.startsWith(model))
164 subset.add(devices[i]);
166 devices = new AltosDeviceLinux[subset.size()];
167 for (int e = 0; e < subset.size(); e++)
168 devices[e] = subset.get(e);