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.AltosDeviceName;
23 import altosui.AltosDeviceNameLinux;
24 import altosui.AltosDevice;
26 public class AltosDeviceLinux extends AltosDevice {
28 String load_string(File file) {
30 FileInputStream in = new FileInputStream(file);
34 while ((c = in.read()) != -1) {
37 result = result + (char) c;
40 } catch (IOException ee) {
43 } catch (FileNotFoundException ee) {
47 String load_string(File dir, String name) {
48 return load_string(new File(dir, name));
51 int load_hex(File file) {
53 return Integer.parseInt(load_string(file).trim(), 16);
54 } catch (NumberFormatException ee) {
59 int load_hex(File dir, String name) {
60 return load_hex(new File(dir, name));
63 int load_dec(File file) {
65 return Integer.parseInt(load_string(file).trim());
66 } catch (NumberFormatException ee) {
71 int load_dec(File dir, String name) {
72 return load_dec(new File(dir, name));
75 String usb_tty(File sys_dir) {
76 String base = sys_dir.getName();
77 int num_configs = load_hex(sys_dir, "bNumConfigurations");
78 int num_inters = load_hex(sys_dir, "bNumInterfaces");
79 for (int config = 1; config <= num_configs; config++) {
80 for (int inter = 0; inter < num_inters; inter++) {
81 String endpoint_base = String.format("%s:%d.%d",
83 File endpoint_full = new File(sys_dir, endpoint_base);
87 /* Check for tty:ttyACMx style names */
88 class tty_colon_filter implements FilenameFilter {
89 public boolean accept(File dir, String name) {
90 return name.startsWith("tty:");
93 namelist = endpoint_full.listFiles(new tty_colon_filter());
94 if (namelist != null && namelist.length > 0)
95 return new File ("/dev", namelist[0].getName().substring(4)).getPath();
97 /* Check for tty/ttyACMx style names */
98 class tty_filter implements FilenameFilter {
99 public boolean accept(File dir, String name) {
100 return name.startsWith("tty");
103 File tty_dir = new File(endpoint_full, "tty");
104 namelist = tty_dir.listFiles(new tty_filter());
105 if (namelist != null && namelist.length > 0)
106 return new File ("/dev", namelist[0].getName()).getPath();
112 public AltosDeviceLinux (File sys) {
114 manufacturer = load_string(sys, "manufacturer");
115 product = load_string(sys, "product");
116 serial = load_dec(sys, "serial");
117 idProduct = load_hex(sys, "idProduct");
118 idVendor = load_hex(sys, "idVendor");
122 public String toString() {
123 return manufacturer + " " + product + " " + serial + " " + idProduct + " " + idVendor + " " + tty;
125 static public AltosDeviceLinux[] list() {
126 LinkedList<AltosDeviceLinux> devices = new LinkedList<AltosDeviceLinux>();
128 class dev_filter implements FilenameFilter{
129 public boolean accept(File dir, String name) {
130 for (int i = 0; i < name.length(); i++) {
131 char c = name.charAt(i);
132 if (Character.isDigit(c))
136 if (c == '.' && i != 1)
144 File usb_devices = new File("/sys/bus/usb/devices");
145 File[] devs = usb_devices.listFiles(new dev_filter());
147 for (int e = 0; e < devs.length; e++) {
148 AltosDeviceLinux dev = new AltosDeviceLinux(devs[e]);
149 if (dev.idVendor == 0xfffe && dev.tty != null) {
154 AltosDeviceLinux[] foo = new AltosDeviceLinux[devices.size()];
155 for (int e = 0; e < devices.size(); e++) {
156 foo[e] = devices.get(e);
157 System.out.println("Device " + foo[e]);
162 static public AltosDeviceLinux[] list(String model) {
163 AltosDeviceLinux[] devices = list();
164 LinkedList<AltosDeviceLinux> subset = new LinkedList<AltosDeviceLinux>();
165 for (int i = 0; i < devices.length; i++) {
166 if (devices[i].product.startsWith(model))
167 subset.add(devices[i]);
169 return (AltosDeviceLinux[]) subset.toArray();