diff --git a/arduino-core/src/cc/arduino/packages/DiscoveryManager.java b/arduino-core/src/cc/arduino/packages/DiscoveryManager.java index b1ec50d85..f8b239401 100644 --- a/arduino-core/src/cc/arduino/packages/DiscoveryManager.java +++ b/arduino-core/src/cc/arduino/packages/DiscoveryManager.java @@ -29,13 +29,20 @@ package cc.arduino.packages; -import cc.arduino.packages.discoverers.NetworkDiscovery; -import cc.arduino.packages.discoverers.SerialDiscovery; +import static processing.app.I18n.format; +import static processing.app.I18n.tr; import java.util.ArrayList; import java.util.List; +import java.util.Map; -import static processing.app.I18n.tr; +import cc.arduino.packages.discoverers.PluggableDiscovery; +import cc.arduino.packages.discoverers.NetworkDiscovery; +import cc.arduino.packages.discoverers.SerialDiscovery; +import processing.app.debug.TargetPackage; +import processing.app.debug.TargetPlatform; +import processing.app.helpers.PreferencesMap; +import processing.app.helpers.StringReplacer; public class DiscoveryManager { @@ -43,17 +50,46 @@ public class DiscoveryManager { private final SerialDiscovery serialDiscoverer = new SerialDiscovery(); private final NetworkDiscovery networkDiscoverer = new NetworkDiscovery(); - public DiscoveryManager() { +// private final Map packages; + + public DiscoveryManager(Map packages) { +// this.packages = packages; + discoverers = new ArrayList<>(); discoverers.add(serialDiscoverer); discoverers.add(networkDiscoverer); + // Search for discoveries in installed packages + for (TargetPackage targetPackage : packages.values()) { + for (TargetPlatform platform: targetPackage.getPlatforms().values()) { + //System.out.println("installed: "+platform); + PreferencesMap prefs = platform.getPreferences().subTree("discovery"); + for (String discoveryName : prefs.firstLevelMap().keySet()) { + PreferencesMap discoveryPrefs = prefs.subTree(discoveryName); + + String pattern = discoveryPrefs.get("pattern"); + if (pattern == null) { + System.out.println(format(tr("No recipes defined for discovery '{0}'"),discoveryName)); + continue; + } + try { + System.out.println("found discovery: " + discoveryName + " -> " + pattern); + System.out.println("with preferencess -> " + discoveryPrefs); + String[] cmd = StringReplacer.formatAndSplit(pattern, discoveryPrefs); + discoverers.add(new PluggableDiscovery(discoveryName, cmd)); + } catch (Exception e) { + System.out.println(format(tr("Could not start discovery '{0}': {1}"), discoveryName, e.getMessage())); + } + } + } + } + // Start all discoverers for (Discovery d : discoverers) { try { new Thread(d).start(); } catch (Exception e) { - System.err.println(tr("Error starting discovery method: ") + d.getClass()); + System.err.println(tr("Error starting discovery method: ") + d.toString()); e.printStackTrace(); } } diff --git a/arduino-core/src/cc/arduino/packages/discoverers/PluggableDiscovery.java b/arduino-core/src/cc/arduino/packages/discoverers/PluggableDiscovery.java new file mode 100644 index 000000000..8bd435087 --- /dev/null +++ b/arduino-core/src/cc/arduino/packages/discoverers/PluggableDiscovery.java @@ -0,0 +1,99 @@ +/* + * This file is part of Arduino. + * + * Arduino 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; either version 2 of the License, or + * (at your option) any later version. + * + * 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + * As a special exception, you may use this file as part of a free software + * library without restriction. Specifically, if other files instantiate + * templates or use macros or inline functions from this file, or you compile + * this file and link it with other files to produce an executable, this + * file does not by itself cause the resulting executable to be covered by + * the GNU General Public License. This exception does not however + * invalidate any other reasons why the executable file might be covered by + * the GNU General Public License. + * + * Copyright 2018 Arduino SA (http://www.arduino.cc/) + */ + +package cc.arduino.packages.discoverers; + +import java.util.ArrayList; +import java.util.List; + +import cc.arduino.packages.BoardPort; +import cc.arduino.packages.Discovery; +import processing.app.legacy.PApplet; + +public class PluggableDiscovery implements Discovery { + + private String discoveryName; + + public PluggableDiscovery(String discoveryName, String[] cmd) { + this.discoveryName = discoveryName; + System.out.println("Starting: " + PApplet.join(cmd, " ")); + } + + @Override + public void run() { + // TODO this method is started as a new thread, it will constantly + // communicate with the discovery tool and keep track of the discovered + // port to be returned from listDiscoveredBoard() + try { + start(); + while (true) { // TODO: Find a better way to terminate discovery + System.out.println(discoveryName + ": looping..."); + Thread.sleep(500); + } + // stop(); + } catch (InterruptedException e) { + e.printStackTrace(); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @Override + public void start() throws Exception { + // TODO send a START_SYNC command to the discovery tool + // or fallback to START if not available + } + + @Override + public void stop() throws Exception { + // TODO send a STOP to the discovery + } + + @Override + public List listDiscoveredBoards() { + // TODO return the ports discovered so far + final List empty = new ArrayList<>(); + return empty; + } + + @Override + public List listDiscoveredBoards(boolean complete) { + // XXX: parameter "complete "is really needed? + // should be checked on all existing discoveries + + // TODO + final List empty = new ArrayList<>(); + return empty; + } + + @Override + public String toString() { + return discoveryName; + } +} diff --git a/arduino-core/src/processing/app/BaseNoGui.java b/arduino-core/src/processing/app/BaseNoGui.java index 7ab457dcf..0a5876971 100644 --- a/arduino-core/src/processing/app/BaseNoGui.java +++ b/arduino-core/src/processing/app/BaseNoGui.java @@ -223,7 +223,7 @@ public class BaseNoGui { public static DiscoveryManager getDiscoveryManager() { if (discoveryManager == null) { - discoveryManager = new DiscoveryManager(); + discoveryManager = new DiscoveryManager(packages); } return discoveryManager; } @@ -506,7 +506,7 @@ public class BaseNoGui { } if (discoveryManager == null) { - discoveryManager = new DiscoveryManager(); + discoveryManager = new DiscoveryManager(packages); } }