Skip to content

Commit af33dce

Browse files
committed
Solve ports conflicts with same address and different protocol
1 parent b3b2279 commit af33dce

12 files changed

+124
-121
lines changed

arduino-ide-extension/src/browser/arduino-frontend-contribution.tsx

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import * as React from 'react';
33
import { remote } from 'electron';
44
import {
55
BoardsService,
6-
Port,
76
SketchesService,
87
ExecutableService,
98
Sketch,
@@ -216,7 +215,7 @@ export class ArduinoFrontendContribution
216215
? nls.localize(
217216
'arduino/common/selectedOn',
218217
'on {0}',
219-
Port.toString(selectedPort)
218+
selectedPort.address
220219
)
221220
: nls.localize('arduino/common/notConnected', '[not connected]'),
222221
className: 'arduino-selected-port',

arduino-ide-extension/src/browser/boards/boards-config.tsx

+8-8
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ export class BoardsConfig extends React.Component<
167167
this.queryPorts(Promise.resolve(ports)).then(({ knownPorts }) => {
168168
let { selectedPort } = this.state;
169169
// If the currently selected port is not available anymore, unset the selected port.
170-
if (removedPorts.some((port) => Port.equals(port, selectedPort))) {
170+
if (removedPorts.some((port) => Port.sameAs(port, selectedPort))) {
171171
selectedPort = undefined;
172172
}
173173
this.setState({ knownPorts, selectedPort }, () =>
@@ -213,11 +213,11 @@ export class BoardsConfig extends React.Component<
213213
} else if (left.protocol === right.protocol) {
214214
// We show ports, including those that have guessed
215215
// or unrecognized boards, so we must sort those too.
216-
const leftBoard = this.availableBoards.find((board) =>
217-
Port.sameAs(board.port, left)
216+
const leftBoard = this.availableBoards.find(
217+
(board) => board.port === left
218218
);
219-
const rightBoard = this.availableBoards.find((board) =>
220-
Port.sameAs(board.port, right)
219+
const rightBoard = this.availableBoards.find(
220+
(board) => board.port === right
221221
);
222222
if (leftBoard && !rightBoard) {
223223
return -1;
@@ -348,10 +348,10 @@ export class BoardsConfig extends React.Component<
348348
<div className="ports list">
349349
{ports.map((port) => (
350350
<Item<Port>
351-
key={Port.toString(port)}
351+
key={`${port.id}`}
352352
item={port}
353353
label={Port.toString(port)}
354-
selected={Port.equals(this.state.selectedPort, port)}
354+
selected={Port.sameAs(this.state.selectedPort, port)}
355355
onClick={this.selectPort}
356356
/>
357357
))}
@@ -410,7 +410,7 @@ export namespace BoardsConfig {
410410
return options.default;
411411
}
412412
const { name } = selectedBoard;
413-
return `${name}${port ? ' at ' + Port.toString(port) : ''}`;
413+
return `${name}${port ? ` at ${port.address}` : ''}`;
414414
}
415415

416416
export function setConfig(

arduino-ide-extension/src/browser/boards/boards-service-provider.ts

+14-15
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,8 @@ export class BoardsServiceProvider implements FrontendApplicationContribution {
185185
const selectedAvailableBoard = AvailableBoard.is(selectedBoard)
186186
? selectedBoard
187187
: this._availableBoards.find((availableBoard) =>
188-
Board.sameAs(availableBoard, selectedBoard)
189-
);
188+
Board.sameAs(availableBoard, selectedBoard)
189+
);
190190
if (
191191
selectedAvailableBoard &&
192192
selectedAvailableBoard.selected &&
@@ -244,7 +244,7 @@ export class BoardsServiceProvider implements FrontendApplicationContribution {
244244
}
245245

246246
set boardsConfig(config: BoardsConfig.Config) {
247-
this.doSetBoardsConfig(config);
247+
this.setBoardsConfig(config);
248248
this.saveState().finally(() =>
249249
this.reconcileAvailableBoards().finally(() =>
250250
this.onBoardsConfigChangedEmitter.fire(this._boardsConfig)
@@ -256,7 +256,7 @@ export class BoardsServiceProvider implements FrontendApplicationContribution {
256256
return this._boardsConfig;
257257
}
258258

259-
protected doSetBoardsConfig(config: BoardsConfig.Config): void {
259+
protected setBoardsConfig(config: BoardsConfig.Config): void {
260260
this.logger.info('Board config changed: ', JSON.stringify(config));
261261
this._boardsConfig = config;
262262
this.latestBoardsConfig = this._boardsConfig;
@@ -370,19 +370,19 @@ export class BoardsServiceProvider implements FrontendApplicationContribution {
370370
const find = (needle: Board & { port: Port }, haystack: AvailableBoard[]) =>
371371
haystack.find(
372372
(board) =>
373-
Board.equals(needle, board) && Port.equals(needle.port, board.port)
373+
Board.equals(needle, board) && Port.sameAs(needle.port, board.port)
374374
);
375375
const timeoutTask =
376376
!!timeout && timeout > 0
377377
? new Promise<void>((_, reject) =>
378-
setTimeout(
379-
() => reject(new Error(`Timeout after ${timeout} ms.`)),
380-
timeout
381-
)
378+
setTimeout(
379+
() => reject(new Error(`Timeout after ${timeout} ms.`)),
380+
timeout
382381
)
382+
)
383383
: new Promise<void>(() => {
384-
/* never */
385-
});
384+
/* never */
385+
});
386386
const waitUntilTask = new Promise<void>((resolve) => {
387387
let candidate = find(what, this.availableBoards);
388388
if (candidate) {
@@ -409,7 +409,7 @@ export class BoardsServiceProvider implements FrontendApplicationContribution {
409409
Port.sameAs(port, this.boardsConfig.selectedPort)
410410
)
411411
) {
412-
this.doSetBoardsConfig({
412+
this.setBoardsConfig({
413413
selectedBoard: this.boardsConfig.selectedBoard,
414414
selectedPort: undefined,
415415
});
@@ -533,9 +533,8 @@ export class BoardsServiceProvider implements FrontendApplicationContribution {
533533

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

541540
protected async loadState(): Promise<void> {

arduino-ide-extension/src/browser/contributions/board-selection.ts

+14-15
Original file line numberDiff line numberDiff line change
@@ -204,10 +204,9 @@ PID: ${PID}`;
204204

205205
const packageLabel =
206206
packageName +
207-
`${
208-
manuallyInstalled
209-
? nls.localize('arduino/board/inSketchbook', ' (in Sketchbook)')
210-
: ''
207+
`${manuallyInstalled
208+
? nls.localize('arduino/board/inSketchbook', ' (in Sketchbook)')
209+
: ''
211210
}`;
212211
// Platform submenu
213212
const platformMenuPath = [...boardsPackagesGroup, packageId];
@@ -255,8 +254,8 @@ PID: ${PID}`;
255254
protocolOrder: number,
256255
ports: AvailablePorts
257256
) => {
258-
const addresses = Object.keys(ports);
259-
if (!addresses.length) {
257+
const portIDs = Object.keys(ports);
258+
if (!portIDs.length) {
260259
return;
261260
}
262261

@@ -279,27 +278,27 @@ PID: ${PID}`;
279278

280279
// First we show addresses with recognized boards connected,
281280
// then all the rest.
282-
const sortedAddresses = Object.keys(ports);
283-
sortedAddresses.sort((left: string, right: string): number => {
281+
const sortedIDs = Object.keys(ports);
282+
sortedIDs.sort((left: string, right: string): number => {
284283
const [, leftBoards] = ports[left];
285284
const [, rightBoards] = ports[right];
286285
return rightBoards.length - leftBoards.length;
287286
});
288287

289-
for (let i = 0; i < sortedAddresses.length; i++) {
290-
const address = sortedAddresses[i];
291-
const [port, boards] = ports[address];
292-
let label = `${address}`;
288+
for (let i = 0; i < sortedIDs.length; i++) {
289+
const portID = sortedIDs[i];
290+
const [port, boards] = ports[portID];
291+
let label = `${port.address}`;
293292
if (boards.length) {
294293
const boardsList = boards.map((board) => board.name).join(', ');
295294
label = `${label} (${boardsList})`;
296295
}
297-
const id = `arduino-select-port--${address}`;
296+
const id = `arduino-select-port--${portID}`;
298297
const command = { id };
299298
const handler = {
300299
execute: () => {
301300
if (
302-
!Port.equals(
301+
!Port.sameAs(
303302
port,
304303
this.boardsServiceProvider.boardsConfig.selectedPort
305304
)
@@ -312,7 +311,7 @@ PID: ${PID}`;
312311
}
313312
},
314313
isToggled: () =>
315-
Port.equals(
314+
Port.sameAs(
316315
port,
317316
this.boardsServiceProvider.boardsConfig.selectedPort
318317
),

arduino-ide-extension/src/browser/serial/monitor/serial-monitor-send-input.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as React from 'react';
22
import { Key, KeyCode } from '@theia/core/lib/browser/keys';
3-
import { Board, Port } from '../../../common/protocol/boards-service';
3+
import { Board } from '../../../common/protocol/boards-service';
44
import { isOSX } from '@theia/core/lib/common/os';
55
import { DisposableCollection, nls } from '@theia/core/lib/common';
66
import { SerialConnectionManager } from '../serial-connection-manager';
@@ -87,7 +87,7 @@ export class SerialMonitorSendInput extends React.Component<
8787
useFqbn: false,
8888
})
8989
: 'unknown',
90-
port ? Port.toString(port) : 'unknown'
90+
port ? port.address : 'unknown'
9191
);
9292
}
9393

arduino-ide-extension/src/browser/serial/serial-connection-manager.ts

+6-7
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import {
1010
} from '../../common/protocol/serial-service';
1111
import { BoardsServiceProvider } from '../boards/boards-service-provider';
1212
import {
13-
Port,
1413
Board,
1514
BoardsService,
1615
} from '../../common/protocol/boards-service';
@@ -217,7 +216,7 @@ export class SerialConnectionManager {
217216
nls.localize(
218217
'arduino/serial/connectionBusy',
219218
'Connection failed. Serial port is busy: {0}',
220-
Port.toString(port)
219+
port.address
221220
),
222221
options
223222
);
@@ -232,7 +231,7 @@ export class SerialConnectionManager {
232231
Board.toString(board, {
233232
useFqbn: false,
234233
}),
235-
Port.toString(port)
234+
port.address
236235
),
237236
options
238237
);
@@ -244,7 +243,7 @@ export class SerialConnectionManager {
244243
'arduino/serial/unexpectedError',
245244
'Unexpected error. Reconnecting {0} on port {1}.',
246245
Board.toString(board),
247-
Port.toString(port)
246+
port.address
248247
),
249248
options
250249
);
@@ -262,7 +261,7 @@ export class SerialConnectionManager {
262261
Board.toString(board, {
263262
useFqbn: false,
264263
}),
265-
Port.toString(port)
264+
port.address
266265
)
267266
);
268267
this.serialErrors.length = 0;
@@ -280,7 +279,7 @@ export class SerialConnectionManager {
280279
Board.toString(board, {
281280
useFqbn: false,
282281
}),
283-
Port.toString(port),
282+
port.address,
284283
attempts.toString()
285284
)
286285
);
@@ -351,7 +350,7 @@ export namespace Serial {
351350
export function toString(config: Partial<SerialConfig>): string {
352351
if (!isSerialConfig(config)) return '';
353352
const { board, port } = config;
354-
return `${Board.toString(board)} ${Port.toString(port)}`;
353+
return `${Board.toString(board)} ${port.address}`;
355354
}
356355
}
357356
}

0 commit comments

Comments
 (0)