Skip to content

Solve ports conflicts with same address and different protocol #713

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

Merged
merged 2 commits into from
Jan 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import * as React from 'react';
import { remote } from 'electron';
import {
BoardsService,
Port,
SketchesService,
ExecutableService,
Sketch,
Expand Down Expand Up @@ -216,7 +215,7 @@ export class ArduinoFrontendContribution
? nls.localize(
'arduino/common/selectedOn',
'on {0}',
Port.toString(selectedPort)
selectedPort.address
)
: nls.localize('arduino/common/notConnected', '[not connected]'),
className: 'arduino-selected-port',
Expand Down
16 changes: 8 additions & 8 deletions arduino-ide-extension/src/browser/boards/boards-config.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ export class BoardsConfig extends React.Component<
this.queryPorts(Promise.resolve(ports)).then(({ knownPorts }) => {
let { selectedPort } = this.state;
// If the currently selected port is not available anymore, unset the selected port.
if (removedPorts.some((port) => Port.equals(port, selectedPort))) {
if (removedPorts.some((port) => Port.sameAs(port, selectedPort))) {
selectedPort = undefined;
}
this.setState({ knownPorts, selectedPort }, () =>
Expand Down Expand Up @@ -213,11 +213,11 @@ export class BoardsConfig extends React.Component<
} else if (left.protocol === right.protocol) {
// We show ports, including those that have guessed
// or unrecognized boards, so we must sort those too.
const leftBoard = this.availableBoards.find((board) =>
Port.sameAs(board.port, left)
const leftBoard = this.availableBoards.find(
(board) => board.port === left
);
const rightBoard = this.availableBoards.find((board) =>
Port.sameAs(board.port, right)
const rightBoard = this.availableBoards.find(
(board) => board.port === right
);
if (leftBoard && !rightBoard) {
return -1;
Expand Down Expand Up @@ -348,10 +348,10 @@ export class BoardsConfig extends React.Component<
<div className="ports list">
{ports.map((port) => (
<Item<Port>
key={Port.toString(port)}
key={`${port.id}`}
item={port}
label={Port.toString(port)}
selected={Port.equals(this.state.selectedPort, port)}
selected={Port.sameAs(this.state.selectedPort, port)}
onClick={this.selectPort}
/>
))}
Expand Down Expand Up @@ -410,7 +410,7 @@ export namespace BoardsConfig {
return options.default;
}
const { name } = selectedBoard;
return `${name}${port ? ' at ' + Port.toString(port) : ''}`;
return `${name}${port ? ` at ${port.address}` : ''}`;
}

export function setConfig(
Expand Down
29 changes: 14 additions & 15 deletions arduino-ide-extension/src/browser/boards/boards-service-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,8 @@ export class BoardsServiceProvider implements FrontendApplicationContribution {
const selectedAvailableBoard = AvailableBoard.is(selectedBoard)
? selectedBoard
: this._availableBoards.find((availableBoard) =>
Board.sameAs(availableBoard, selectedBoard)
);
Board.sameAs(availableBoard, selectedBoard)
);
if (
selectedAvailableBoard &&
selectedAvailableBoard.selected &&
Expand Down Expand Up @@ -244,7 +244,7 @@ export class BoardsServiceProvider implements FrontendApplicationContribution {
}

set boardsConfig(config: BoardsConfig.Config) {
this.doSetBoardsConfig(config);
this.setBoardsConfig(config);
this.saveState().finally(() =>
this.reconcileAvailableBoards().finally(() =>
this.onBoardsConfigChangedEmitter.fire(this._boardsConfig)
Expand All @@ -256,7 +256,7 @@ export class BoardsServiceProvider implements FrontendApplicationContribution {
return this._boardsConfig;
}

protected doSetBoardsConfig(config: BoardsConfig.Config): void {
protected setBoardsConfig(config: BoardsConfig.Config): void {
this.logger.info('Board config changed: ', JSON.stringify(config));
this._boardsConfig = config;
this.latestBoardsConfig = this._boardsConfig;
Expand Down Expand Up @@ -370,19 +370,19 @@ export class BoardsServiceProvider implements FrontendApplicationContribution {
const find = (needle: Board & { port: Port }, haystack: AvailableBoard[]) =>
haystack.find(
(board) =>
Board.equals(needle, board) && Port.equals(needle.port, board.port)
Board.equals(needle, board) && Port.sameAs(needle.port, board.port)
);
const timeoutTask =
!!timeout && timeout > 0
? new Promise<void>((_, reject) =>
setTimeout(
() => reject(new Error(`Timeout after ${timeout} ms.`)),
timeout
)
setTimeout(
() => reject(new Error(`Timeout after ${timeout} ms.`)),
timeout
)
)
: new Promise<void>(() => {
/* never */
});
/* never */
});
const waitUntilTask = new Promise<void>((resolve) => {
let candidate = find(what, this.availableBoards);
if (candidate) {
Expand All @@ -409,7 +409,7 @@ export class BoardsServiceProvider implements FrontendApplicationContribution {
Port.sameAs(port, this.boardsConfig.selectedPort)
)
) {
this.doSetBoardsConfig({
this.setBoardsConfig({
selectedBoard: this.boardsConfig.selectedBoard,
selectedPort: undefined,
});
Expand Down Expand Up @@ -533,9 +533,8 @@ export class BoardsServiceProvider implements FrontendApplicationContribution {

protected getLastSelectedBoardOnPortKey(port: Port | string): string {
// TODO: we lose the port's `protocol` info (`serial`, `network`, etc.) here if the `port` is a `string`.
return `last-selected-board-on-port:${
typeof port === 'string' ? port : Port.toString(port)
}`;
return `last-selected-board-on-port:${typeof port === 'string' ? port : port.address
}`;
}

protected async loadState(): Promise<void> {
Expand Down
28 changes: 13 additions & 15 deletions arduino-ide-extension/src/browser/contributions/board-selection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,10 +204,9 @@ PID: ${PID}`;

const packageLabel =
packageName +
`${
manuallyInstalled
? nls.localize('arduino/board/inSketchbook', ' (in Sketchbook)')
: ''
`${manuallyInstalled
? nls.localize('arduino/board/inSketchbook', ' (in Sketchbook)')
: ''
}`;
// Platform submenu
const platformMenuPath = [...boardsPackagesGroup, packageId];
Expand Down Expand Up @@ -255,8 +254,8 @@ PID: ${PID}`;
protocolOrder: number,
ports: AvailablePorts
) => {
const addresses = Object.keys(ports);
if (!addresses.length) {
const portIDs = Object.keys(ports);
if (!portIDs.length) {
return;
}

Expand All @@ -279,27 +278,26 @@ PID: ${PID}`;

// First we show addresses with recognized boards connected,
// then all the rest.
const sortedAddresses = Object.keys(ports);
sortedAddresses.sort((left: string, right: string): number => {
const sortedIDs = Object.keys(ports).sort((left: string, right: string): number => {
const [, leftBoards] = ports[left];
const [, rightBoards] = ports[right];
return rightBoards.length - leftBoards.length;
});

for (let i = 0; i < sortedAddresses.length; i++) {
const address = sortedAddresses[i];
const [port, boards] = ports[address];
let label = `${address}`;
for (let i = 0; i < sortedIDs.length; i++) {
const portID = sortedIDs[i];
const [port, boards] = ports[portID];
let label = `${port.address}`;
if (boards.length) {
const boardsList = boards.map((board) => board.name).join(', ');
label = `${label} (${boardsList})`;
}
const id = `arduino-select-port--${address}`;
const id = `arduino-select-port--${portID}`;
const command = { id };
const handler = {
execute: () => {
if (
!Port.equals(
!Port.sameAs(
port,
this.boardsServiceProvider.boardsConfig.selectedPort
)
Expand All @@ -312,7 +310,7 @@ PID: ${PID}`;
}
},
isToggled: () =>
Port.equals(
Port.sameAs(
port,
this.boardsServiceProvider.boardsConfig.selectedPort
),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as React from 'react';
import { Key, KeyCode } from '@theia/core/lib/browser/keys';
import { Board, Port } from '../../../common/protocol/boards-service';
import { Board } from '../../../common/protocol/boards-service';
import { isOSX } from '@theia/core/lib/common/os';
import { DisposableCollection, nls } from '@theia/core/lib/common';
import { SerialConnectionManager } from '../serial-connection-manager';
Expand Down Expand Up @@ -87,7 +87,7 @@ export class SerialMonitorSendInput extends React.Component<
useFqbn: false,
})
: 'unknown',
port ? Port.toString(port) : 'unknown'
port ? port.address : 'unknown'
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
} from '../../common/protocol/serial-service';
import { BoardsServiceProvider } from '../boards/boards-service-provider';
import {
Port,
Board,
BoardsService,
} from '../../common/protocol/boards-service';
Expand Down Expand Up @@ -217,7 +216,7 @@ export class SerialConnectionManager {
nls.localize(
'arduino/serial/connectionBusy',
'Connection failed. Serial port is busy: {0}',
Port.toString(port)
port.address
),
options
);
Expand All @@ -232,7 +231,7 @@ export class SerialConnectionManager {
Board.toString(board, {
useFqbn: false,
}),
Port.toString(port)
port.address
),
options
);
Expand All @@ -244,7 +243,7 @@ export class SerialConnectionManager {
'arduino/serial/unexpectedError',
'Unexpected error. Reconnecting {0} on port {1}.',
Board.toString(board),
Port.toString(port)
port.address
),
options
);
Expand All @@ -262,7 +261,7 @@ export class SerialConnectionManager {
Board.toString(board, {
useFqbn: false,
}),
Port.toString(port)
port.address
)
);
this.serialErrors.length = 0;
Expand All @@ -280,7 +279,7 @@ export class SerialConnectionManager {
Board.toString(board, {
useFqbn: false,
}),
Port.toString(port),
port.address,
attempts.toString()
)
);
Expand Down Expand Up @@ -351,7 +350,7 @@ export namespace Serial {
export function toString(config: Partial<SerialConfig>): string {
if (!isSerialConfig(config)) return '';
const { board, port } = config;
return `${Board.toString(board)} ${Port.toString(port)}`;
return `${Board.toString(board)} ${port.address}`;
}
}
}
Expand Down
Loading