Skip to content
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

Auto-select boards config with hardwareId if present #1885

Closed
wants to merge 13 commits into from
Closed
6 changes: 5 additions & 1 deletion arduino-ide-extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,11 @@
],
"arduino": {
"cli": {
"version": "0.29.0"
"version": {
"owner": "arduino",
"repo": "arduino-cli",
"commitish": "master"
davegarthsimpson marked this conversation as resolved.
Show resolved Hide resolved
}
},
"fwuploader": {
"version": "2.2.2"
Expand Down
63 changes: 42 additions & 21 deletions arduino-ide-extension/src/browser/boards/boards-service-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,15 +158,9 @@ export class BoardsServiceProvider
this.lastAvailablePortsOnUpload = undefined;
}

private portToAutoSelectCanBeDerived(): boolean {
return Boolean(
this.lastBoardsConfigOnUpload && this.lastAvailablePortsOnUpload
);
}

attemptPostUploadAutoSelect(): void {
setTimeout(() => {
if (this.portToAutoSelectCanBeDerived()) {
if (this.lastBoardsConfigOnUpload && this.lastAvailablePortsOnUpload) {
this.attemptAutoSelect({
ports: this._availablePorts,
boards: this._availableBoards,
Expand All @@ -185,12 +179,12 @@ export class BoardsServiceProvider
private deriveBoardConfigToAutoSelect(
newState: AttachedBoardsChangeEvent['newState']
): void {
if (!this.portToAutoSelectCanBeDerived()) {
if (!this.lastBoardsConfigOnUpload || !this.lastAvailablePortsOnUpload) {
this.boardConfigToAutoSelect = undefined;
return;
}

const oldPorts = this.lastAvailablePortsOnUpload!;
const oldPorts = this.lastAvailablePortsOnUpload;
const { ports: newPorts, boards: newBoards } = newState;

const appearedPorts =
Expand All @@ -205,20 +199,36 @@ export class BoardsServiceProvider
Port.sameAs(board.port, port)
);

const lastBoardsConfigOnUpload = this.lastBoardsConfigOnUpload!;
const lastBoardsConfigOnUpload = this.lastBoardsConfigOnUpload;

if (
boardOnAppearedPort &&
lastBoardsConfigOnUpload.selectedBoard &&
Board.sameAs(
if (boardOnAppearedPort && lastBoardsConfigOnUpload.selectedBoard) {
const boardIsSameHardware = Board.sameDistinctHardwareAs(
boardOnAppearedPort,
lastBoardsConfigOnUpload.selectedBoard
)
) {
);

const boardIsSameFqbn = Board.sameAs(
boardOnAppearedPort,
lastBoardsConfigOnUpload.selectedBoard
);

if (!boardIsSameHardware && !boardIsSameFqbn) return;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See here: #1913 (review)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


let boardToAutoSelect = boardOnAppearedPort;
if (boardIsSameHardware && !boardIsSameFqbn) {
const { name, fqbn } = lastBoardsConfigOnUpload.selectedBoard;

boardToAutoSelect = {
...boardToAutoSelect,
name,
fqbn,
};
}

this.clearBoardDiscoverySnapshot();

this.boardConfigToAutoSelect = {
selectedBoard: boardOnAppearedPort,
selectedBoard: boardToAutoSelect,
selectedPort: port,
};
return;
Expand Down Expand Up @@ -326,8 +336,10 @@ export class BoardsServiceProvider
// it is just a FQBN, so we need to find the `selected` board among the `AvailableBoards`
const selectedAvailableBoard = AvailableBoard.is(selectedBoard)
? selectedBoard
: this._availableBoards.find((availableBoard) =>
Board.sameAs(availableBoard, selectedBoard)
: this._availableBoards.find(
(availableBoard) =>
Board.sameDistinctHardwareAs(availableBoard, selectedBoard) ||
Board.sameAs(availableBoard, selectedBoard)
);
if (
selectedAvailableBoard &&
Expand All @@ -353,24 +365,33 @@ export class BoardsServiceProvider

protected tryReconnect(): boolean {
if (this.latestValidBoardsConfig && !this.canUploadTo(this.boardsConfig)) {
// ** Reconnect to a board unplugged from, and plugged back into the same port
for (const board of this.availableBoards.filter(
({ state }) => state !== AvailableBoard.State.incomplete
)) {
if (
this.latestValidBoardsConfig.selectedBoard.fqbn === board.fqbn &&
this.latestValidBoardsConfig.selectedBoard.name === board.name &&
(Board.sameDistinctHardwareAs(
this.latestValidBoardsConfig.selectedBoard,
board
) ||
(this.latestValidBoardsConfig.selectedBoard.fqbn === board.fqbn &&
this.latestValidBoardsConfig.selectedBoard.name ===
board.name)) &&
Port.sameAs(this.latestValidBoardsConfig.selectedPort, board.port)
) {
this.boardsConfig = this.latestValidBoardsConfig;
return true;
}
}
// **

// ** Reconnect to a board whose port changed due to an upload
if (!this.boardConfigToAutoSelect) return false;

this.boardsConfig = this.boardConfigToAutoSelect;
this.boardConfigToAutoSelect = undefined;
return true;
// **
}
return false;
}
Expand Down
17 changes: 17 additions & 0 deletions arduino-ide-extension/src/common/protocol/boards-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ export interface Port {
readonly protocol: string;
readonly protocolLabel: string;
readonly properties?: Record<string, string>;
readonly hardwareId?: string;
}
export namespace Port {
export type Properties = Record<string, string>;
Expand Down Expand Up @@ -553,6 +554,22 @@ export namespace Board {
return left.name === right.name && left.fqbn === right.fqbn;
}

export function sameDistinctHardwareAs(
davegarthsimpson marked this conversation as resolved.
Show resolved Hide resolved
left: Board,
right: string | Board
davegarthsimpson marked this conversation as resolved.
Show resolved Hide resolved
): boolean {
if (Board.is(right) && left.port && right.port) {
davegarthsimpson marked this conversation as resolved.
Show resolved Hide resolved
const { hardwareId: leftHardwareId } = left.port;
const { hardwareId: rightHardwareId } = right.port;

if (leftHardwareId && rightHardwareId) {
return leftHardwareId === rightHardwareId;
}
}

return false;
}

export function sameAs(left: Board, right: string | Board): boolean {
// How to associate a selected board with one of the available cores: https://typefox.slack.com/archives/CJJHJCJSJ/p1571142327059200
// 1. How to use the FQBN if any and infer the package ID from it: https://typefox.slack.com/archives/CJJHJCJSJ/p1571147549069100
Expand Down
4 changes: 2 additions & 2 deletions arduino-ide-extension/src/node/board-discovery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,14 +323,14 @@ export class BoardDiscovery
}

private fromRpcPort(rpcPort: RpcPort): Port {
const port = {
return {
address: rpcPort.getAddress(),
addressLabel: rpcPort.getLabel(),
protocol: rpcPort.getProtocol(),
protocolLabel: rpcPort.getProtocolLabel(),
properties: Port.Properties.create(rpcPort.getPropertiesMap().toObject()),
hardwareId: rpcPort.getHardwareId(),
};
return port;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -388,66 +388,6 @@ export namespace ConfigValue {
}
}

export class BoardAttachRequest extends jspb.Message {

hasInstance(): boolean;
clearInstance(): void;
getInstance(): cc_arduino_cli_commands_v1_common_pb.Instance | undefined;
setInstance(value?: cc_arduino_cli_commands_v1_common_pb.Instance): BoardAttachRequest;

getBoardUri(): string;
setBoardUri(value: string): BoardAttachRequest;

getSketchPath(): string;
setSketchPath(value: string): BoardAttachRequest;

getSearchTimeout(): string;
setSearchTimeout(value: string): BoardAttachRequest;


serializeBinary(): Uint8Array;
toObject(includeInstance?: boolean): BoardAttachRequest.AsObject;
static toObject(includeInstance: boolean, msg: BoardAttachRequest): BoardAttachRequest.AsObject;
static extensions: {[key: number]: jspb.ExtensionFieldInfo<jspb.Message>};
static extensionsBinary: {[key: number]: jspb.ExtensionFieldBinaryInfo<jspb.Message>};
static serializeBinaryToWriter(message: BoardAttachRequest, writer: jspb.BinaryWriter): void;
static deserializeBinary(bytes: Uint8Array): BoardAttachRequest;
static deserializeBinaryFromReader(message: BoardAttachRequest, reader: jspb.BinaryReader): BoardAttachRequest;
}

export namespace BoardAttachRequest {
export type AsObject = {
instance?: cc_arduino_cli_commands_v1_common_pb.Instance.AsObject,
boardUri: string,
sketchPath: string,
searchTimeout: string,
}
}

export class BoardAttachResponse extends jspb.Message {

hasTaskProgress(): boolean;
clearTaskProgress(): void;
getTaskProgress(): cc_arduino_cli_commands_v1_common_pb.TaskProgress | undefined;
setTaskProgress(value?: cc_arduino_cli_commands_v1_common_pb.TaskProgress): BoardAttachResponse;


serializeBinary(): Uint8Array;
toObject(includeInstance?: boolean): BoardAttachResponse.AsObject;
static toObject(includeInstance: boolean, msg: BoardAttachResponse): BoardAttachResponse.AsObject;
static extensions: {[key: number]: jspb.ExtensionFieldInfo<jspb.Message>};
static extensionsBinary: {[key: number]: jspb.ExtensionFieldBinaryInfo<jspb.Message>};
static serializeBinaryToWriter(message: BoardAttachResponse, writer: jspb.BinaryWriter): void;
static deserializeBinary(bytes: Uint8Array): BoardAttachResponse;
static deserializeBinaryFromReader(message: BoardAttachResponse, reader: jspb.BinaryReader): BoardAttachResponse;
}

export namespace BoardAttachResponse {
export type AsObject = {
taskProgress?: cc_arduino_cli_commands_v1_common_pb.TaskProgress.AsObject,
}
}

export class BoardListRequest extends jspb.Message {

hasInstance(): boolean;
Expand All @@ -458,6 +398,9 @@ export class BoardListRequest extends jspb.Message {
getTimeout(): number;
setTimeout(value: number): BoardListRequest;

getFqbn(): string;
setFqbn(value: string): BoardListRequest;


serializeBinary(): Uint8Array;
toObject(includeInstance?: boolean): BoardListRequest.AsObject;
Expand All @@ -473,6 +416,7 @@ export namespace BoardListRequest {
export type AsObject = {
instance?: cc_arduino_cli_commands_v1_common_pb.Instance.AsObject,
timeout: number,
fqbn: string,
}
}

Expand Down
Loading