1
0
mirror of https://github.com/arduino/Arduino.git synced 2025-01-21 10:52:14 +01:00

Fix bogus port disconnection during serial event

Fixes https://github.com/arduino/Arduino/issues/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).
This commit is contained in:
Martino Facchin 2020-03-10 17:11:09 +01:00 committed by Cristian Maglie
parent a0cd3eade8
commit d154120f0e
3 changed files with 24 additions and 32 deletions

View File

@ -122,6 +122,10 @@ public class BoardPort {
return this.address; return this.address;
} }
public String toCompleteString() {
return this.address + "_" + this.getPrefs().get("vid") + "_" + this.getPrefs().get("pid");
}
// Search for the board which matches identificationPrefs. // Search for the board which matches identificationPrefs.
// If found, boardName is set to the name from boards.txt // If found, boardName is set to the name from boards.txt
// and the board is returned. If not found, null is returned. // and the board is returned. If not found, null is returned.

View File

@ -46,7 +46,6 @@ public class SerialDiscovery implements Discovery, Runnable {
private final List<String> oldPorts = new ArrayList<>(); private final List<String> oldPorts = new ArrayList<>();
public boolean uploadInProgress = false; public boolean uploadInProgress = false;
public boolean pausePolling = false; public boolean pausePolling = false;
private BoardPort oldUploadBoardPort = null;
private final BoardCloudResolver boardCloudResolver = new BoardCloudResolver(); private final BoardCloudResolver boardCloudResolver = new BoardCloudResolver();
@ -56,7 +55,7 @@ public class SerialDiscovery implements Discovery, Runnable {
} }
@Override @Override
public List<BoardPort> listDiscoveredBoards(boolean complete) { public synchronized List<BoardPort> listDiscoveredBoards(boolean complete) {
if (complete) { if (complete) {
return new ArrayList<>(serialBoardPorts); return new ArrayList<>(serialBoardPorts);
} }
@ -69,7 +68,7 @@ public class SerialDiscovery implements Discovery, Runnable {
return onlineBoardPorts; return onlineBoardPorts;
} }
public void setSerialBoardPorts(List<BoardPort> newSerialBoardPorts) { public synchronized void setSerialBoardPorts(List<BoardPort> newSerialBoardPorts) {
serialBoardPorts.clear(); serialBoardPorts.clear();
serialBoardPorts.addAll(newSerialBoardPorts); serialBoardPorts.addAll(newSerialBoardPorts);
} }
@ -116,27 +115,17 @@ public class SerialDiscovery implements Discovery, Runnable {
return; return;
} }
// if (updating) {}
// a port will disappear, another will appear
// use this information to "merge" the boards
// updating must be signaled by SerialUpload class
oldPorts.clear(); oldPorts.clear();
oldPorts.addAll(ports); oldPorts.addAll(ports);
// set unreachable ports offline
for (BoardPort board : boardPorts) { for (BoardPort board : boardPorts) {
if (ports.contains(board.toString())) { if (!ports.contains(board.toCompleteString())) {
if (board.isOnline()) {
ports.remove(ports.indexOf(board.toString()));
}
} else {
if (uploadInProgress && board.isOnline()) {
oldUploadBoardPort = board;
}
board.setOnlineStatus(false); board.setOnlineStatus(false);
} }
} }
// add information for newly added ports
for (String newPort : ports) { for (String newPort : ports) {
String[] parts = newPort.split("_"); String[] parts = newPort.split("_");
@ -161,35 +150,35 @@ public class SerialDiscovery implements Discovery, Runnable {
BoardPort boardPort = null; BoardPort boardPort = null;
int i = 0; int i = 0;
// create new board or update existing
// create new board if in ports but not in boardPorts
for (BoardPort board : boardPorts) { for (BoardPort board : boardPorts) {
if (board.toString().equals(newPort)) { if (board.toCompleteString().equalsIgnoreCase(newPort)) {
boardPort = boardPorts.get(i); boardPort = boardPorts.get(i);
boardPorts.get(i).setOnlineStatus(true);
break; break;
} }
i++; i++;
} }
if (boardPort == null) {
if (boardPort != null) {
continue;
}
boardPort = new BoardPort(); boardPort = new BoardPort();
boardPorts.add(boardPort); boardPorts.add(boardPort);
}
boardPort.setAddress(port); boardPort.setAddress(port);
boardPort.setProtocol("serial"); boardPort.setProtocol("serial");
boardPort.setOnlineStatus(true); boardPort.setOnlineStatus(true);
String label = port; boardPort.setLabel(port);
if (boardData != null) { if (boardData != null) {
boardPort.getPrefs().put("vid", boardData.get("vid").toString()); boardPort.getPrefs().put("vid", boardData.get("vid").toString());
boardPort.getPrefs().put("pid", boardData.get("pid").toString()); boardPort.getPrefs().put("pid", boardData.get("pid").toString());
String iserial = boardData.get("iserial").toString(); String iserial = boardData.get("iserial").toString();
if (iserial.length() >= 10) {
boardPort.getPrefs().put("iserial", iserial); boardPort.getPrefs().put("iserial", iserial);
}
if (uploadInProgress && oldUploadBoardPort!=null) {
oldUploadBoardPort.getPrefs().put("iserial", iserial);
}
TargetBoard board = (TargetBoard) boardData.get("board"); TargetBoard board = (TargetBoard) boardData.get("board");
if (board != null) { if (board != null) {
@ -208,8 +197,6 @@ public class SerialDiscovery implements Discovery, Runnable {
boardPort.getPrefs().put("iserial", ""); boardPort.getPrefs().put("iserial", "");
} }
} }
boardPort.setLabel(label);
} }
setSerialBoardPorts(boardPorts); setSerialBoardPorts(boardPorts);
} }

View File

@ -202,8 +202,9 @@ public class Platform {
} }
Map<String, Object> boardData = new HashMap<>(); Map<String, Object> boardData = new HashMap<>();
boardData.put("board", board); boardData.put("board", board);
boardData.put("vid", vids.get(i)); // remove 0x from VID / PID to keep them as reported by liblistserial
boardData.put("pid", pids.get(i)); boardData.put("vid", vids.get(i).replaceAll("0x", ""));
boardData.put("pid", pids.get(i).replaceAll("0x", ""));
String extrafields = vid_pid_iSerial.substring(vidPid.length() + 1); String extrafields = vid_pid_iSerial.substring(vidPid.length() + 1);
String[] parts = extrafields.split("_"); String[] parts = extrafields.split("_");
boardData.put("iserial", parts[0]); boardData.put("iserial", parts[0]);