1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
|
/*
* 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.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 String.format("%-20s %6d %-15s", product, serial, tty == null ? "" : 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);
return foo;
}
static public AltosDeviceLinux[] list(String model) {
AltosDeviceLinux[] devices = list();
if (model != null) {
LinkedList<AltosDeviceLinux> subset = new LinkedList<AltosDeviceLinux>();
for (int i = 0; i < devices.length; i++) {
if (devices[i].product.startsWith(model))
subset.add(devices[i]);
}
devices = new AltosDeviceLinux[subset.size()];
for (int e = 0; e < subset.size(); e++)
devices[e] = subset.get(e);
}
return devices;
}
}
|