Skip to content

Commit ec4787a

Browse files
committed
Fixed board identification in BoardPort
1 parent 5bc9665 commit ec4787a

File tree

1 file changed

+42
-15
lines changed

1 file changed

+42
-15
lines changed

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

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ public PreferencesMap getPrefs() {
8888
return prefs;
8989
}
9090

91+
public PreferencesMap getIdentificationPrefs() {
92+
return identificationPrefs;
93+
}
94+
9195
public void setLabel(String label) {
9296
this.label = label;
9397
}
@@ -117,7 +121,7 @@ public TargetBoard searchMatchingBoard() {
117121
for (TargetPackage targetPackage : BaseNoGui.packages.values()) {
118122
for (TargetPlatform targetPlatform : targetPackage.getPlatforms().values()) {
119123
for (TargetBoard board : targetPlatform.getBoards().values()) {
120-
if (matchesIdentificationPrefs(board)) {
124+
if (matchesBoard(board)) {
121125
setBoardName(board.getName());
122126
return board;
123127
}
@@ -126,21 +130,44 @@ public TargetBoard searchMatchingBoard() {
126130
}
127131
return null;
128132
}
129-
// Check whether a board matches all identificationPrefs fields
130-
private boolean matchesIdentificationPrefs(TargetBoard board) {
131-
for (String key : identificationPrefs.keySet()) {
132-
if (!matchesIdentificationPref(board, key)) return false;
133-
}
134-
return true;
135-
}
136-
// Check whether a board matches a single identificationPrefs field
137-
private boolean matchesIdentificationPref(TargetBoard board, String key) {
138-
String value = identificationPrefs.get(key);
139-
if (value == null) return false;
140-
for (String property : board.getPreferences().subTree(key).values()) {
141-
if (property.equalsIgnoreCase(value)) return true;
133+
134+
public boolean matchesBoard(TargetBoard board) {
135+
PreferencesMap identificationProps = getIdentificationPrefs();
136+
PreferencesMap boardProps = board.getPreferences();
137+
138+
// Identification properties are defined in boards.txt with a ".N" suffix
139+
// for example:
140+
//
141+
// uno.name=Arduino/Genuino Uno
142+
// uno.vid.0=0x2341
143+
// uno.pid.0=0x0043
144+
// uno.vid.1=0x2341
145+
// uno.pid.1=0x0001
146+
// uno.vid.2=0x2A03
147+
// uno.pid.2=0x0043
148+
// uno.vid.3=0x2341
149+
// uno.pid.3=0x0243
150+
//
151+
// so we must search starting from suffix ".0" and increasing until we
152+
// found a match or the board has no more identification properties defined
153+
154+
for (int suffix = 0;; suffix++) {
155+
boolean found = true;
156+
for (String prop : identificationProps.keySet()) {
157+
String value = identificationProps.get(prop);
158+
prop += "." + suffix;
159+
if (!boardProps.containsKey(prop)) {
160+
return false;
161+
}
162+
if (!value.equalsIgnoreCase(boardProps.get(prop))) {
163+
found = false;
164+
break;
165+
}
166+
}
167+
if (found) {
168+
return true;
169+
}
142170
}
143-
return false;
144171
}
145172

146173
}

0 commit comments

Comments
 (0)