b777462780f0cf089d2b35eeda3375cae26ad3a5
[fw/altos] / ao-tools / altosui / AltosDeviceLinux.java
1 /*
2  * Copyright © 2010 Keith Packard <keithp@keithp.com>
3  *
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.
7  *
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.
12  *
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.
16  */
17
18 package altosui;
19 import java.lang.*;
20 import java.io.*;
21 import java.util.*;
22 import altosui.AltosDeviceName;
23 import altosui.AltosDeviceNameLinux;
24 import altosui.AltosDevice;
25
26 public class AltosDeviceLinux extends AltosDevice {
27
28         String load_string(File file) {
29                 try {
30                         FileInputStream in = new FileInputStream(file);
31                         String result = "";
32                         int c;
33                         try {
34                                 while ((c = in.read()) != -1) {
35                                         if (c == '\n')
36                                                 break;
37                                         result = result + (char) c;
38                                 }
39                                 return result;
40                         } catch (IOException ee) {
41                                 return "";
42                         }
43                 } catch (FileNotFoundException ee) {
44                         return "";
45                 }
46         }
47         String load_string(File dir, String name) {
48                 return load_string(new File(dir, name));
49         }
50
51         int load_hex(File file) {
52                 try {
53                         return Integer.parseInt(load_string(file).trim(), 16);
54                 } catch (NumberFormatException ee) {
55                         return -1;
56                 }
57         }
58
59         int load_hex(File dir, String name) {
60                 return load_hex(new File(dir, name));
61         }
62
63         int load_dec(File file) {
64                 try {
65                         return Integer.parseInt(load_string(file).trim());
66                 } catch (NumberFormatException ee) {
67                         return -1;
68                 }
69         }
70
71         int load_dec(File dir, String name) {
72                 return load_dec(new File(dir, name));
73         }
74
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",
82                                                                      base, config, inter);
83                                 File endpoint_full = new File(sys_dir, endpoint_base);
84
85                                 File[] namelist;
86
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:");
91                                         }
92                                 }
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();
96
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");
101                                         }
102                                 }
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();
107                         }
108                 }
109                 return null;
110         }
111
112         public AltosDeviceLinux (File sys) {
113                 sys = 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");
119                 tty = usb_tty(sys);
120         }
121
122         public String toString() {
123                 return manufacturer + " " + product + " " + serial + " " + idProduct + " " + idVendor + " " + tty;
124         }
125         static public AltosDeviceLinux[] list() {
126                 LinkedList<AltosDeviceLinux> devices = new LinkedList<AltosDeviceLinux>();
127
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))
133                                                 continue;
134                                         if (c == '-')
135                                                 continue;
136                                         if (c == '.' && i != 1)
137                                                 continue;
138                                         return false;
139                                 }
140                                 return true;
141                         }
142                 }
143
144                 File usb_devices = new File("/sys/bus/usb/devices");
145                 File[] devs = usb_devices.listFiles(new dev_filter());
146                 if (devs != null) {
147                         for (int e = 0; e < devs.length; e++) {
148                                 AltosDeviceLinux        dev = new AltosDeviceLinux(devs[e]);
149                                 if (dev.idVendor == 0xfffe && dev.tty != null) {
150                                         devices.add(dev);
151                                 }
152                         }
153                 }
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]);
158                 }
159                 return foo;
160         }
161
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]);
168                 }
169                 return (AltosDeviceLinux[]) subset.toArray();
170         }
171 }