-
Notifications
You must be signed in to change notification settings - Fork 0
/
TeensyDiscovery.java
333 lines (306 loc) · 7.68 KB
/
TeensyDiscovery.java
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
package cc.arduino.packages.discoverers;
import cc.arduino.packages.BoardPort;
import cc.arduino.packages.Discovery;
import processing.app.BaseNoGui;
import java.util.LinkedList;
import java.util.List;
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParser;
public class TeensyDiscovery implements Discovery {
private final List<BoardPort> portlist;
private Process program=null;
private JsonParser parser;
private ObjectMapper mapper;
public TeensyDiscovery() {
portlist = new LinkedList<>();
}
@Override
public void run() {
String cmdline = BaseNoGui.getHardwarePath() + File.separator
+ "tools" + File.separator + "teensy_ports";
try {
program = Runtime.getRuntime().exec(new String[] {cmdline, "-J"});
} catch (Exception e1) {
program = null;
}
if (program == null) {
print("unable to run teensy_ports -J");
return;
}
InputStream input = program.getInputStream();
print("teensy_ports -J started");
try {
// https://sohlich.github.io/post/jackson/
// https://www.baeldung.com/jackson-object-mapper-tutorial
JsonFactory factory = new JsonFactory();
parser = factory.createJsonParser(input);
mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
while (true) {
BoardPort port = mapper.readValue(parser, BoardPort.class);
if (port != null) {
incoming(port);
}
}
} catch (Exception e) {
print("ended with exception");
}
print("end");
}
private synchronized void incoming(BoardPort port) {
String address = port.getAddress();
if (address == null) {
return; // address is required
}
for (BoardPort bp : portlist) {
if (address.equals(bp.getAddress())) {
// if address already on the list, discard old info
portlist.remove(bp);
}
}
if (port.isOnline()) {
if (port.getLabel() == null) {
// if no label, use address
port.setLabel(address);
}
if (port.getProtocol() == null) {
// if no protocol, assume serial
port.setProtocol("serial");
}
portlist.add(new BoardPort(port));
}
}
@Override
public void start() {
print("start");
run();
}
@Override
public void stop() {
print("stop");
if (program != null) {
program.destroy();
program = null;
}
}
@Override
public List<BoardPort> listDiscoveredBoards() {
return listDiscoveredBoards(false);
}
@Override
public synchronized List<BoardPort> listDiscoveredBoards(boolean complete) {
print("listDiscoveredBoards");
List<BoardPort> portlistCopy = new LinkedList<>();
for (BoardPort bp : portlist) {
portlistCopy.add(new BoardPort(bp));
}
return portlistCopy;
}
void print(String str) {
System.err.println("TeensyDiscovery: " + str);
}
}
/*
public class TeensyDiscovery implements Discovery {
Socket sock=null;
InputStream input;
OutputStream output;
static Process program=null;
@Override
public void run() {
start();
}
@Override
public void start() {
print("start");
if (!connect()) {
if (run_program()) {
connect();
}
}
}
@Override
public void stop() {
print("stop");
close();
}
@Override
public List<BoardPort> listDiscoveredBoards() {
return listDiscoveredBoards(false);
}
@Override
public List<BoardPort> listDiscoveredBoards(boolean complete) {
print("listDiscoveredBoards");
List<BoardPort> ports = new LinkedList<>();
for (int attempt=0; attempt < 5; attempt++) {
if (sock != null && !sock.isConnected()) {
print("sock not connected, setting null");
sock = null;
}
if (sock == null) {
print("sock try reconnect");
if (!connect()) {
print("sock try run program & reconnect");
if (!run_program()) {
print("Unable to run teensy_ports");
break;
}
for (int n=1; n < 100; n++) {
if (connect()) {
print("reconnect on try #" + n);
break;
}
try {
Thread.sleep(5);
} catch (Exception e) {
}
}
if (sock == null) {
print("still can't connect");
break;
}
}
}
try {
String listcmd = "list\n";
output.write(listcmd.getBytes());
} catch (IOException e1) {
print("output.write exception");
close();
continue;
}
byte[] b = new byte[65536];
int count = 0;
int loopcount = 0;
while (true) {
try {
count = input.read(b, count, b.length - count);
} catch (IOException e2) {
print("input.read I/O exception");
}
if (count == -1) {
print("input.read -1 (end of input)");
// TODO: is a brief delay needed?
}
//print("got " + count + " bytes");
if (count > 0 && b[count-1] == 0) {
print("last bytes is null term, good :)");
break;
}
if (++loopcount > 6) {
// too many tries, give up :-(
print("input give up");
close();
break;
}
}
if (sock == null) continue;
String data = new String(b, 0, count-1);
String[] lines = data.split("\n");
//print("found " + lines.length + " lines");
BoardPort board;
for (int i=0; i < lines.length; i++) {
String[] fields = lines[i].split(" ", 2);
//print("found " + fields.length + " fields");
if (fields.length == 2) {
board = new BoardPort();
board.setProtocol("Teensy"); // Ports menu category
board.setAddress(fields[0]); // used to open port
String label = fields[1];
if (label.startsWith("\\\\?\\")) {
label = label.substring(4);
}
int teensyindex = label.indexOf("(Teensy");
if (teensyindex > 23) {
label = label.substring(0, 23) + "... " +
label.substring(teensyindex);
}
board.setLabel(label); // name shown in Ports menu
ports.add(board);
}
}
return ports;
}
return ports;
}
void print(String str) {
//System.err.println("TeensyDiscovery: " + str);
}
boolean connect() {
String name = "teensy ports";
int namelen = name.length();
byte[] buf = new byte[namelen];
byte[] namebuf = name.getBytes();
InetAddress local;
int[] addrlist = {28542,4985,18925};
try {
byte[] loop = new byte[] {127, 0, 0, 1};
local = InetAddress.getByAddress("localhost", loop);
} catch (Exception e1) {
sock = null;
return false;
}
for (int i=0; i<addrlist.length; i++) {
try {
sock = new Socket();
InetSocketAddress addr = new InetSocketAddress(local, addrlist[i]);
sock.connect(addr, 50);
input = sock.getInputStream();
output = sock.getOutputStream();
sock.setSoTimeout(50);
} catch (Exception e2) {
sock = null;
return false;
}
// check for welcome message
try {
int wait = 0;
while (input.available() < namelen) {
if (++wait > 5) throw new Exception();
try {
Thread.sleep(10);
} catch (Exception e3) { }
}
input.read(buf, 0, namelen);
for (int n=0; n<namelen; n++) {
if (buf[n] != namebuf[n]) throw new Exception();
}
} catch (Exception e) {
// mistakenly connected to some other program!
close();
continue;
}
print("connected to teensy_ports");
return true;
}
sock = null;
return false;
}
void close() {
try {
sock.close();
} catch (Exception e4) { }
sock = null;
}
synchronized boolean run_program() {
String cmdline = BaseNoGui.getHardwarePath() + File.separator
+ "tools" + File.separator + "teensy_ports";
try {
program = Runtime.getRuntime().exec(new String[] {cmdline});
} catch (Exception e1) {
program = null;
return false;
}
return true;
}
}
*/