forked from arduino/Arduino
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathBoardPort.java
192 lines (165 loc) · 5.61 KB
/
BoardPort.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
* 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 2013 Arduino LLC (http://www.arduino.cc/)
*/
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; // 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 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() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getProtocol() {
return protocol;
}
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;
}
public void setBoardName(String boardName) {
this.boardName = boardName;
}
public PreferencesMap getPrefs() {
return prefs;
}
public PreferencesMap getIdentificationPrefs() {
return identificationPrefs;
}
public void setLabel(String label) {
this.label = label;
}
public String getLabel() {
return label;
}
public void setOnlineStatus(boolean online) {
this.online = online;
}
public boolean isOnline() {
return online;
}
@Override
public String toString() {
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;
}
}
}
}