Skip to content

Commit d154120

Browse files
facchinmcmaglie
authored andcommitted
Fix bogus port disconnection during serial event
Fixes #9785 and probably many others This commit strongly simplyfies the serial list code Pluggable discovery introduced a bug since BoardPort.toString() started reporting only the name of the port, not the complete name_vid_pid needed to match liblistserial output. Adding .toCompleteString() almost solves the bogus disconnection part alone, but resolveDeviceByVendorIdProductId() uses "0x" prefixes VID/PID, breaking it again. In addition, all the logic used to match a board with its bootloader (to obtain a serial number on 32u4 boards) has been completely removed since it is currently useless (and unused).
1 parent a0cd3ea commit d154120

File tree

3 files changed

+24
-32
lines changed

3 files changed

+24
-32
lines changed

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

+4
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,10 @@ public String toString() {
122122
return this.address;
123123
}
124124

125+
public String toCompleteString() {
126+
return this.address + "_" + this.getPrefs().get("vid") + "_" + this.getPrefs().get("pid");
127+
}
128+
125129
// Search for the board which matches identificationPrefs.
126130
// If found, boardName is set to the name from boards.txt
127131
// and the board is returned. If not found, null is returned.

arduino-core/src/cc/arduino/packages/discoverers/serial/SerialDiscovery.java

+17-30
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ public class SerialDiscovery implements Discovery, Runnable {
4646
private final List<String> oldPorts = new ArrayList<>();
4747
public boolean uploadInProgress = false;
4848
public boolean pausePolling = false;
49-
private BoardPort oldUploadBoardPort = null;
5049
private final BoardCloudResolver boardCloudResolver = new BoardCloudResolver();
5150

5251

@@ -56,7 +55,7 @@ public List<BoardPort> listDiscoveredBoards() {
5655
}
5756

5857
@Override
59-
public List<BoardPort> listDiscoveredBoards(boolean complete) {
58+
public synchronized List<BoardPort> listDiscoveredBoards(boolean complete) {
6059
if (complete) {
6160
return new ArrayList<>(serialBoardPorts);
6261
}
@@ -69,7 +68,7 @@ public List<BoardPort> listDiscoveredBoards(boolean complete) {
6968
return onlineBoardPorts;
7069
}
7170

72-
public void setSerialBoardPorts(List<BoardPort> newSerialBoardPorts) {
71+
public synchronized void setSerialBoardPorts(List<BoardPort> newSerialBoardPorts) {
7372
serialBoardPorts.clear();
7473
serialBoardPorts.addAll(newSerialBoardPorts);
7574
}
@@ -116,27 +115,17 @@ public synchronized void forceRefresh() {
116115
return;
117116
}
118117

119-
// if (updating) {}
120-
// a port will disappear, another will appear
121-
// use this information to "merge" the boards
122-
// updating must be signaled by SerialUpload class
123-
124118
oldPorts.clear();
125119
oldPorts.addAll(ports);
126120

121+
// set unreachable ports offline
127122
for (BoardPort board : boardPorts) {
128-
if (ports.contains(board.toString())) {
129-
if (board.isOnline()) {
130-
ports.remove(ports.indexOf(board.toString()));
131-
}
132-
} else {
133-
if (uploadInProgress && board.isOnline()) {
134-
oldUploadBoardPort = board;
135-
}
123+
if (!ports.contains(board.toCompleteString())) {
136124
board.setOnlineStatus(false);
137125
}
138126
}
139127

128+
// add information for newly added ports
140129
for (String newPort : ports) {
141130

142131
String[] parts = newPort.split("_");
@@ -161,35 +150,35 @@ public synchronized void forceRefresh() {
161150

162151
BoardPort boardPort = null;
163152
int i = 0;
164-
// create new board or update existing
153+
154+
// create new board if in ports but not in boardPorts
165155
for (BoardPort board : boardPorts) {
166-
if (board.toString().equals(newPort)) {
156+
if (board.toCompleteString().equalsIgnoreCase(newPort)) {
167157
boardPort = boardPorts.get(i);
158+
boardPorts.get(i).setOnlineStatus(true);
168159
break;
169160
}
170161
i++;
171162
}
172-
if (boardPort == null) {
173-
boardPort = new BoardPort();
174-
boardPorts.add(boardPort);
163+
164+
if (boardPort != null) {
165+
continue;
175166
}
167+
168+
boardPort = new BoardPort();
169+
boardPorts.add(boardPort);
176170
boardPort.setAddress(port);
177171
boardPort.setProtocol("serial");
178172
boardPort.setOnlineStatus(true);
179173

180-
String label = port;
174+
boardPort.setLabel(port);
181175

182176
if (boardData != null) {
183177
boardPort.getPrefs().put("vid", boardData.get("vid").toString());
184178
boardPort.getPrefs().put("pid", boardData.get("pid").toString());
185179

186180
String iserial = boardData.get("iserial").toString();
187-
if (iserial.length() >= 10) {
188-
boardPort.getPrefs().put("iserial", iserial);
189-
}
190-
if (uploadInProgress && oldUploadBoardPort!=null) {
191-
oldUploadBoardPort.getPrefs().put("iserial", iserial);
192-
}
181+
boardPort.getPrefs().put("iserial", iserial);
193182

194183
TargetBoard board = (TargetBoard) boardData.get("board");
195184
if (board != null) {
@@ -208,8 +197,6 @@ public synchronized void forceRefresh() {
208197
boardPort.getPrefs().put("iserial", "");
209198
}
210199
}
211-
212-
boardPort.setLabel(label);
213200
}
214201
setSerialBoardPorts(boardPorts);
215202
}

arduino-core/src/processing/app/Platform.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,9 @@ public synchronized Map<String, Object> resolveDeviceByVendorIdProductId(String
202202
}
203203
Map<String, Object> boardData = new HashMap<>();
204204
boardData.put("board", board);
205-
boardData.put("vid", vids.get(i));
206-
boardData.put("pid", pids.get(i));
205+
// remove 0x from VID / PID to keep them as reported by liblistserial
206+
boardData.put("vid", vids.get(i).replaceAll("0x", ""));
207+
boardData.put("pid", pids.get(i).replaceAll("0x", ""));
207208
String extrafields = vid_pid_iSerial.substring(vidPid.length() + 1);
208209
String[] parts = extrafields.split("_");
209210
boardData.put("iserial", parts[0]);

0 commit comments

Comments
 (0)