From d7143d6859b6f4c9ab4a23824829834f7e017575 Mon Sep 17 00:00:00 2001 From: PaulStoffregen Date: Tue, 2 Oct 2018 17:08:43 -0700 Subject: [PATCH] Add BoardPort identificationPrefs and searchMatchingBoard --- .../src/cc/arduino/packages/BoardPort.java | 42 +++++++++++++++++++ .../discoverers/PluggableDiscovery.java | 16 ++++++- 2 files changed, 56 insertions(+), 2 deletions(-) diff --git a/arduino-core/src/cc/arduino/packages/BoardPort.java b/arduino-core/src/cc/arduino/packages/BoardPort.java index 95476f478..c71ac8aaa 100644 --- a/arduino-core/src/cc/arduino/packages/BoardPort.java +++ b/arduino-core/src/cc/arduino/packages/BoardPort.java @@ -29,6 +29,10 @@ package cc.arduino.packages; +import processing.app.BaseNoGui; +import processing.app.debug.TargetBoard; +import processing.app.debug.TargetPackage; +import processing.app.debug.TargetPlatform; import processing.app.helpers.PreferencesMap; public class BoardPort { @@ -37,15 +41,18 @@ public class BoardPort { private String protocol; // how to communicate, used for Ports menu sections private String boardName; private String label; // friendly name shown in Ports menu + private final PreferencesMap identificationPrefs; // data to match with boards.txt private final PreferencesMap prefs; // "vendorId", "productId", "serialNumber" private boolean online; // used by SerialBoardsLister (during upload??) public BoardPort() { this.prefs = new PreferencesMap(); + this.identificationPrefs = new PreferencesMap(); } public BoardPort(BoardPort bp) { prefs = new PreferencesMap(bp.prefs); + identificationPrefs = new PreferencesMap(bp.identificationPrefs); address = bp.address; protocol = bp.protocol; boardName = bp.boardName; @@ -133,4 +140,39 @@ public class BoardPort { public String toString() { return this.address+"_"+getVID()+"_"+getPID(); } + + // Search for the board which matches identificationPrefs. + // If found, boardName is set to the name from boards.txt + // and the board is returned. If not found, null is returned. + public TargetBoard searchMatchingBoard() { + if (identificationPrefs.isEmpty()) return null; + for (TargetPackage targetPackage : BaseNoGui.packages.values()) { + for (TargetPlatform targetPlatform : targetPackage.getPlatforms().values()) { + for (TargetBoard board : targetPlatform.getBoards().values()) { + if (matchesIdentificationPrefs(board)) { + setBoardName(board.getName()); + return board; + } + } + } + } + return null; + } + // Check whether a board matches all identificationPrefs fields + private boolean matchesIdentificationPrefs(TargetBoard board) { + for (String key : identificationPrefs.keySet()) { + if (!matchesIdentificationPref(board, key)) return false; + } + return true; + } + // Check whether a board matches a single identificationPrefs field + private boolean matchesIdentificationPref(TargetBoard board, String key) { + String value = identificationPrefs.get(key); + if (value == null) return false; + for (String property : board.getPreferences().subTree(key).values()) { + if (property.equalsIgnoreCase(value)) return true; + } + return false; + } + } diff --git a/arduino-core/src/cc/arduino/packages/discoverers/PluggableDiscovery.java b/arduino-core/src/cc/arduino/packages/discoverers/PluggableDiscovery.java index 1c66716ae..899a3c7e2 100644 --- a/arduino-core/src/cc/arduino/packages/discoverers/PluggableDiscovery.java +++ b/arduino-core/src/cc/arduino/packages/discoverers/PluggableDiscovery.java @@ -43,6 +43,8 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.core.JsonFactory; import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.annotation.PropertyAccessor; +import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility; public class PluggableDiscovery implements Discovery { @@ -69,6 +71,8 @@ public class PluggableDiscovery implements Discovery { JsonFactory factory = new JsonFactory(); JsonParser parser = factory.createParser(input); ObjectMapper mapper = new ObjectMapper(); + mapper.setVisibility(PropertyAccessor.ALL, Visibility.NONE); + mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); while (program != null && program.isAlive()) { @@ -82,6 +86,9 @@ public class PluggableDiscovery implements Discovery { startPolling(); } } else { + if (event.equals("add")) { + msg.searchMatchingBoard(); + } update(msg); } } @@ -175,8 +182,13 @@ public class PluggableDiscovery implements Discovery { } if (port.getEventType().equals("add")) { if (port.getLabel() == null) { - // if no label, use address - port.setLabel(address); + // if no label, use address & name, or just address if no name + String name = port.getBoardName(); + if (name == null) { + port.setLabel(address); + } else { + port.setLabel(address + " (" + name + ")"); + } } if (port.getProtocol() == null) { // if no protocol, assume serial