Skip to content

Pluggable discovery: search in platform.txt (WIP) #8038

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 28 commits into from
Mar 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
f81798b
Pluggable discovery: search in platform.txt (WIP)
cmaglie Sep 10, 2018
f5bf6e5
Add BoardPort copy constructor
PaulStoffregen Sep 29, 2018
5ba56ab
Initial PluggableDiscovery using BoardPort for JSON
PaulStoffregen Sep 29, 2018
b606657
PluggableDiscovery check for START_SYNC not supported
PaulStoffregen Sep 29, 2018
e029acc
Add PluggableDiscoveryMessage for BoardPort change metadata
PaulStoffregen Oct 1, 2018
05092bf
Move BoardPort fixed fields into prefs
PaulStoffregen Oct 1, 2018
d7143d6
Add BoardPort identificationPrefs and searchMatchingBoard
PaulStoffregen Oct 3, 2018
8d6fa72
Removing fixed fields in BoardPort
cmaglie Oct 4, 2018
3ccb2d9
Merged SerialDiscovery and SerialBoardLister
cmaglie Oct 4, 2018
80fb9a0
Optimized forceRefresh() method by removing redundant boolean paramater
cmaglie Oct 4, 2018
5bc9665
Slightly optimized method by removing redundant boolean flag
cmaglie Oct 4, 2018
ec4787a
Fixed board identification in BoardPort
cmaglie Oct 4, 2018
9ba172b
Show BoardName.boardName field in 'Ports' menu
cmaglie Nov 23, 2018
cfd3cf2
Use correctly the setBoardName() method in NetworkDiscovery
cmaglie Nov 23, 2018
c03a8bc
Minor fix in indentation and style
cmaglie Nov 23, 2018
349af4b
Added BoardPort.protocolLabel and simplified port menu rendering
cmaglie Nov 29, 2018
7186213
Slightly changed pluggable discovery json parsing
cmaglie Nov 30, 2018
8e9f0cf
PluggableDiscovery: added a 'port' field in json messages
cmaglie Nov 30, 2018
4c188c9
PluggableDiscovery: Factored out method to umarshal BoardPort from JSON
cmaglie Nov 30, 2018
4ae740a
PluggableDiscovery: BoardPort.label sanity check in the correct place
cmaglie Nov 30, 2018
7bc086a
PluggableDiscovery: correct synchronization on 'portList' access
cmaglie Nov 30, 2018
6c50007
Editor: renamed status bar field serialport -> port
cmaglie Dec 7, 2018
4fffcd6
Editor: use TargetBoard.getName() to get board name
cmaglie Dec 7, 2018
651dcd5
Removed unused field
cmaglie Dec 7, 2018
e1caaf1
Perform port selection after initializing packages
cmaglie Jan 23, 2019
feb863d
PluggableDiscovery: allow patterns to contain runtime variables
facchinm Dec 13, 2018
be1a840
Add TargetBoard.getFQBN helper
facchinm Mar 7, 2019
d4bbf71
Match wildcard property "." with board fqbn/name
facchinm Mar 7, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app/src/processing/app/Base.java
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,8 @@ public Base(String[] args) throws Exception {
splash.splashText(tr("Initializing packages..."));
BaseNoGui.initPackages();

parser.getUploadPort().ifPresent(BaseNoGui::selectSerialPort);

splash.splashText(tr("Preparing boards..."));

if (!isCommandLine()) {
Expand Down
83 changes: 50 additions & 33 deletions app/src/processing/app/Editor.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.LinkedList;
Expand Down Expand Up @@ -94,6 +93,7 @@
import cc.arduino.view.findreplace.FindReplace;
import jssc.SerialPortException;
import processing.app.debug.RunnerException;
import processing.app.debug.TargetBoard;
import processing.app.forms.PasswordAuthorizationDialog;
import processing.app.helpers.DocumentTextChangeListener;
import processing.app.helpers.Keys;
Expand Down Expand Up @@ -147,9 +147,6 @@ public boolean test(SketchController controller) {
}
}

private final static List<String> BOARD_PROTOCOLS_ORDER = Arrays.asList("serial", "network");
private final static List<String> BOARD_PROTOCOLS_ORDER_TRANSLATIONS = Arrays.asList(tr("Serial ports"), tr("Network ports"));

final Base base;

// otherwise, if the window is resized with the message label
Expand Down Expand Up @@ -1041,22 +1038,30 @@ class BoardPortJCheckBoxMenuItem extends JCheckBoxMenuItem {
private BoardPort port;

public BoardPortJCheckBoxMenuItem(BoardPort port) {
super(port.getLabel());
super();
this.port = port;
setText(toString());
addActionListener(e -> {
selectSerialPort(port.getAddress());
base.onBoardOrPortChange();
});
this.port = port;
}

@Override
public String toString() {
// This is required for serialPrompt()
return port.getLabel();
String label = port.getLabel();
if (port.getBoardName() != null && !port.getBoardName().isEmpty()) {
label += " (" + port.getBoardName() + ")";
}
return label;
}
}

private void populatePortMenu() {
final List<String> PROTOCOLS_ORDER = Arrays.asList("serial", "network");
final List<String> PROTOCOLS_LABELS = Arrays.asList(tr("Serial ports"), tr("Network ports"));

portMenu.removeAll();

String selectedPort = PreferencesData.get("serial.port");
Expand All @@ -1065,31 +1070,43 @@ private void populatePortMenu() {

ports = platform.filterPorts(ports, PreferencesData.getBoolean("serial.ports.showall"));

Collections.sort(ports, new Comparator<BoardPort>() {
@Override
public int compare(BoardPort o1, BoardPort o2) {
return (BOARD_PROTOCOLS_ORDER.indexOf(o1.getProtocol()) - BOARD_PROTOCOLS_ORDER.indexOf(o2.getProtocol())) * 10 +
o1.getAddress().compareTo(o2.getAddress());
}
ports.stream() //
.filter(port -> port.getProtocolLabel() == null || port.getProtocolLabel().isEmpty())
.forEach(port -> {
int labelIdx = PROTOCOLS_ORDER.indexOf(port.getProtocol());
if (labelIdx != -1) {
port.setProtocolLabel(PROTOCOLS_LABELS.get(labelIdx));
} else {
port.setProtocolLabel(port.getProtocol());
}
});

Collections.sort(ports, (port1, port2) -> {
String pr1 = port1.getProtocol();
String pr2 = port2.getProtocol();
int prIdx1 = PROTOCOLS_ORDER.contains(pr1) ? PROTOCOLS_ORDER.indexOf(pr1) : 999;
int prIdx2 = PROTOCOLS_ORDER.contains(pr2) ? PROTOCOLS_ORDER.indexOf(pr2) : 999;
int r = prIdx1 - prIdx2;
if (r != 0)
return r;
r = port1.getProtocolLabel().compareTo(port2.getProtocolLabel());
if (r != 0)
return r;
return port1.getAddress().compareTo(port2.getAddress());
});

String lastProtocol = null;
String lastProtocolTranslated;
String lastProtocol = "";
String lastProtocolLabel = "";
for (BoardPort port : ports) {
if (lastProtocol == null || !port.getProtocol().equals(lastProtocol)) {
if (lastProtocol != null) {
if (!port.getProtocol().equals(lastProtocol) || !port.getProtocolLabel().equals(lastProtocolLabel)) {
if (!lastProtocol.isEmpty()) {
portMenu.addSeparator();
}
lastProtocol = port.getProtocol();

if (BOARD_PROTOCOLS_ORDER.indexOf(port.getProtocol()) != -1) {
lastProtocolTranslated = BOARD_PROTOCOLS_ORDER_TRANSLATIONS.get(BOARD_PROTOCOLS_ORDER.indexOf(port.getProtocol()));
} else {
lastProtocolTranslated = port.getProtocol();
}
JMenuItem lastProtocolMenuItem = new JMenuItem(tr(lastProtocolTranslated));
lastProtocolMenuItem.setEnabled(false);
portMenu.add(lastProtocolMenuItem);
lastProtocolLabel = port.getProtocolLabel();
JMenuItem item = new JMenuItem(tr(lastProtocolLabel));
item.setEnabled(false);
portMenu.add(item);
}
String address = port.getAddress();

Expand Down Expand Up @@ -2392,9 +2409,9 @@ private void handleBoardInfo() {
for (BoardPort port : ports) {
if (port.getAddress().equals(selectedPort)) {
label = port.getBoardName();
vid = port.getVID();
pid = port.getPID();
iserial = port.getISerial();
vid = port.getPrefs().get("vid");
pid = port.getPrefs().get("pid");
iserial = port.getPrefs().get("iserial");
protocol = port.getProtocol();
found = true;
break;
Expand Down Expand Up @@ -2564,12 +2581,12 @@ private void statusEmpty() {
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

protected void onBoardOrPortChange() {
Map<String, String> boardPreferences = BaseNoGui.getBoardPreferences();
if (boardPreferences != null)
lineStatus.setBoardName(boardPreferences.get("name"));
TargetBoard board = BaseNoGui.getTargetBoard();
if (board != null)
lineStatus.setBoardName(board.getName());
else
lineStatus.setBoardName("-");
lineStatus.setSerialPort(PreferencesData.get("serial.port"));
lineStatus.setPort(PreferencesData.get("serial.port"));
lineStatus.repaint();
}

Expand Down
19 changes: 7 additions & 12 deletions app/src/processing/app/EditorLineStatus.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ public class EditorLineStatus extends JComponent {

String text = "";
String name = "";
String serialport = "";
String serialnumber = "";
String port = "";

public EditorLineStatus() {
background = Theme.getColor("linestatus.bgcolor");
Expand Down Expand Up @@ -92,13 +91,13 @@ public void set(int newStart, int newStop) {

public void paintComponent(Graphics graphics) {
Graphics2D g = Theme.setupGraphics2D(graphics);
if (name.isEmpty() && serialport.isEmpty()) {
if (name.isEmpty() && port.isEmpty()) {
PreferencesMap boardPreferences = BaseNoGui.getBoardPreferences();
if (boardPreferences != null)
setBoardName(boardPreferences.get("name"));
else
setBoardName("-");
setSerialPort(PreferencesData.get("serial.port"));
setPort(PreferencesData.get("serial.port"));
}
g.setColor(background);
Dimension size = getSize();
Expand All @@ -112,8 +111,8 @@ public void paintComponent(Graphics graphics) {
g.setColor(messageForeground);

String statusText;
if (serialport != null && !serialport.isEmpty()) {
statusText = I18n.format(tr("{0} on {1}"), name, serialport);
if (port != null && !port.isEmpty()) {
statusText = I18n.format(tr("{0} on {1}"), name, port);
} else {
statusText = name;
}
Expand All @@ -132,12 +131,8 @@ public void setBoardName(String name) {
this.name = name;
}

public void setSerialPort(String serialport) {
this.serialport = serialport;
}

public void setSerialNumber(String serialnumber) {
this.serialnumber = serialnumber;
public void setPort(String port) {
this.port = port;
}

public Dimension getPreferredSize() {
Expand Down
132 changes: 103 additions & 29 deletions arduino-core/src/cc/arduino/packages/BoardPort.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,36 @@

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 {

private String address;
private String protocol;
private String address; // unique name for this port, used by Preferences
private String protocol; // how to communicate, used for Ports menu sections
private String protocolLabel; // protocol extended name to display on GUI
private String boardName;
private String vid;
private String pid;
private String iserial;
private String label;
private final PreferencesMap prefs;
private boolean online;
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;
label = bp.label;
online = bp.online;
}

public String getAddress() {
Expand All @@ -63,6 +77,14 @@ public void setProtocol(String protocol) {
this.protocol = protocol;
}

public String getProtocolLabel() {
return protocolLabel;
}

public void setProtocolLabel(String protocolLabel) {
this.protocolLabel = protocolLabel;
}

public String getBoardName() {
return boardName;
}
Expand All @@ -75,6 +97,10 @@ public PreferencesMap getPrefs() {
return prefs;
}

public PreferencesMap getIdentificationPrefs() {
return identificationPrefs;
}

public void setLabel(String label) {
this.label = label;
}
Expand All @@ -91,28 +117,76 @@ public boolean isOnline() {
return online;
}

public void setVIDPID(String vid, String pid) {
this.vid = vid;
this.pid = pid;
}

public String getVID() {
return vid;
}

public String getPID() {
return pid;
}

public void setISerial(String iserial) {
this.iserial = iserial;
}
public String getISerial() {
return iserial;
}

@Override
public String toString() {
return this.address+"_"+this.vid+"_"+this.pid;
return this.address;
}

// 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 == null || identificationPrefs.isEmpty()) return null;
for (TargetPackage targetPackage : BaseNoGui.packages.values()) {
for (TargetPlatform targetPlatform : targetPackage.getPlatforms().values()) {
for (TargetBoard board : targetPlatform.getBoards().values()) {
if (matchesBoard(board)) {
setBoardName(board.getName());
return board;
}
}
}
}
return null;
}

public boolean matchesBoard(TargetBoard board) {
PreferencesMap identificationProps = getIdentificationPrefs();
PreferencesMap boardProps = board.getPreferences();

String wildMatcher = identificationProps.get(".");
if (wildMatcher != null) {
if (wildMatcher.equals(board.getId())) {
return true;
}
if (wildMatcher.equals(board.getFQBN())) {
return true;
}
}

// Identification properties are defined in boards.txt with a ".N" suffix
// for example:
//
// uno.name=Arduino/Genuino Uno
// uno.vid.0=0x2341
// uno.pid.0=0x0043
// uno.vid.1=0x2341
// uno.pid.1=0x0001
// uno.vid.2=0x2A03
// uno.pid.2=0x0043
// uno.vid.3=0x2341
// uno.pid.3=0x0243
//
// so we must search starting from suffix ".0" and increasing until we
// found a match or the board has no more identification properties defined

for (int suffix = 0;; suffix++) {
boolean found = true;
for (String prop : identificationProps.keySet()) {
String value = identificationProps.get(prop);
prop += "." + suffix;
if (!boardProps.containsKey(prop)) {
return false;
}
if (!value.equalsIgnoreCase(boardProps.get(prop))) {
found = false;
break;
}
}
if (found) {
return true;
}
}
}

}
Loading