Skip to content

Commit f81798b

Browse files
committed
Pluggable discovery: search in platform.txt (WIP)
1 parent 3ba8583 commit f81798b

File tree

3 files changed

+142
-7
lines changed

3 files changed

+142
-7
lines changed

arduino-core/src/cc/arduino/packages/DiscoveryManager.java

+41-5
Original file line numberDiff line numberDiff line change
@@ -29,31 +29,67 @@
2929

3030
package cc.arduino.packages;
3131

32-
import cc.arduino.packages.discoverers.NetworkDiscovery;
33-
import cc.arduino.packages.discoverers.SerialDiscovery;
32+
import static processing.app.I18n.format;
33+
import static processing.app.I18n.tr;
3434

3535
import java.util.ArrayList;
3636
import java.util.List;
37+
import java.util.Map;
3738

38-
import static processing.app.I18n.tr;
39+
import cc.arduino.packages.discoverers.PluggableDiscovery;
40+
import cc.arduino.packages.discoverers.NetworkDiscovery;
41+
import cc.arduino.packages.discoverers.SerialDiscovery;
42+
import processing.app.debug.TargetPackage;
43+
import processing.app.debug.TargetPlatform;
44+
import processing.app.helpers.PreferencesMap;
45+
import processing.app.helpers.StringReplacer;
3946

4047
public class DiscoveryManager {
4148

4249
private final List<Discovery> discoverers;
4350
private final SerialDiscovery serialDiscoverer = new SerialDiscovery();
4451
private final NetworkDiscovery networkDiscoverer = new NetworkDiscovery();
4552

46-
public DiscoveryManager() {
53+
// private final Map<String, TargetPackage> packages;
54+
55+
public DiscoveryManager(Map<String, TargetPackage> packages) {
56+
// this.packages = packages;
57+
4758
discoverers = new ArrayList<>();
4859
discoverers.add(serialDiscoverer);
4960
discoverers.add(networkDiscoverer);
5061

62+
// Search for discoveries in installed packages
63+
for (TargetPackage targetPackage : packages.values()) {
64+
for (TargetPlatform platform: targetPackage.getPlatforms().values()) {
65+
//System.out.println("installed: "+platform);
66+
PreferencesMap prefs = platform.getPreferences().subTree("discovery");
67+
for (String discoveryName : prefs.firstLevelMap().keySet()) {
68+
PreferencesMap discoveryPrefs = prefs.subTree(discoveryName);
69+
70+
String pattern = discoveryPrefs.get("pattern");
71+
if (pattern == null) {
72+
System.out.println(format(tr("No recipes defined for discovery '{0}'"),discoveryName));
73+
continue;
74+
}
75+
try {
76+
System.out.println("found discovery: " + discoveryName + " -> " + pattern);
77+
System.out.println("with preferencess -> " + discoveryPrefs);
78+
String[] cmd = StringReplacer.formatAndSplit(pattern, discoveryPrefs);
79+
discoverers.add(new PluggableDiscovery(discoveryName, cmd));
80+
} catch (Exception e) {
81+
System.out.println(format(tr("Could not start discovery '{0}': {1}"), discoveryName, e.getMessage()));
82+
}
83+
}
84+
}
85+
}
86+
5187
// Start all discoverers
5288
for (Discovery d : discoverers) {
5389
try {
5490
new Thread(d).start();
5591
} catch (Exception e) {
56-
System.err.println(tr("Error starting discovery method: ") + d.getClass());
92+
System.err.println(tr("Error starting discovery method: ") + d.toString());
5793
e.printStackTrace();
5894
}
5995
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
* This file is part of Arduino.
3+
*
4+
* Arduino 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; either version 2 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program; if not, write to the Free Software
16+
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17+
*
18+
* As a special exception, you may use this file as part of a free software
19+
* library without restriction. Specifically, if other files instantiate
20+
* templates or use macros or inline functions from this file, or you compile
21+
* this file and link it with other files to produce an executable, this
22+
* file does not by itself cause the resulting executable to be covered by
23+
* the GNU General Public License. This exception does not however
24+
* invalidate any other reasons why the executable file might be covered by
25+
* the GNU General Public License.
26+
*
27+
* Copyright 2018 Arduino SA (http://www.arduino.cc/)
28+
*/
29+
30+
package cc.arduino.packages.discoverers;
31+
32+
import java.util.ArrayList;
33+
import java.util.List;
34+
35+
import cc.arduino.packages.BoardPort;
36+
import cc.arduino.packages.Discovery;
37+
import processing.app.legacy.PApplet;
38+
39+
public class PluggableDiscovery implements Discovery {
40+
41+
private String discoveryName;
42+
43+
public PluggableDiscovery(String discoveryName, String[] cmd) {
44+
this.discoveryName = discoveryName;
45+
System.out.println("Starting: " + PApplet.join(cmd, " "));
46+
}
47+
48+
@Override
49+
public void run() {
50+
// TODO this method is started as a new thread, it will constantly
51+
// communicate with the discovery tool and keep track of the discovered
52+
// port to be returned from listDiscoveredBoard()
53+
try {
54+
start();
55+
while (true) { // TODO: Find a better way to terminate discovery
56+
System.out.println(discoveryName + ": looping...");
57+
Thread.sleep(500);
58+
}
59+
// stop();
60+
} catch (InterruptedException e) {
61+
e.printStackTrace();
62+
} catch (Exception e) {
63+
e.printStackTrace();
64+
}
65+
}
66+
67+
@Override
68+
public void start() throws Exception {
69+
// TODO send a START_SYNC command to the discovery tool
70+
// or fallback to START if not available
71+
}
72+
73+
@Override
74+
public void stop() throws Exception {
75+
// TODO send a STOP to the discovery
76+
}
77+
78+
@Override
79+
public List<BoardPort> listDiscoveredBoards() {
80+
// TODO return the ports discovered so far
81+
final List<BoardPort> empty = new ArrayList<>();
82+
return empty;
83+
}
84+
85+
@Override
86+
public List<BoardPort> listDiscoveredBoards(boolean complete) {
87+
// XXX: parameter "complete "is really needed?
88+
// should be checked on all existing discoveries
89+
90+
// TODO
91+
final List<BoardPort> empty = new ArrayList<>();
92+
return empty;
93+
}
94+
95+
@Override
96+
public String toString() {
97+
return discoveryName;
98+
}
99+
}

arduino-core/src/processing/app/BaseNoGui.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ static public File getDefaultSketchbookFolder() {
223223

224224
public static DiscoveryManager getDiscoveryManager() {
225225
if (discoveryManager == null) {
226-
discoveryManager = new DiscoveryManager();
226+
discoveryManager = new DiscoveryManager(packages);
227227
}
228228
return discoveryManager;
229229
}
@@ -506,7 +506,7 @@ static public void initPackages() throws Exception {
506506
}
507507

508508
if (discoveryManager == null) {
509-
discoveryManager = new DiscoveryManager();
509+
discoveryManager = new DiscoveryManager(packages);
510510
}
511511
}
512512

0 commit comments

Comments
 (0)