Skip to content

Commit 10d96fc

Browse files
silvanocerzaAlberto Iannaccone
authored and
Alberto Iannaccone
committed
Implement methods to get user fields for board/port combination
1 parent ccd44c2 commit 10d96fc

File tree

3 files changed

+103
-23
lines changed

3 files changed

+103
-23
lines changed

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

+62-23
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
BoardsPackage,
1313
AttachedBoardsChangeEvent,
1414
BoardWithPackage,
15+
BoardUserField,
1516
} from '../../common/protocol';
1617
import { BoardsConfig } from './boards-config';
1718
import { naturalCompare } from '../../common/utils';
@@ -68,7 +69,8 @@ export class BoardsServiceProvider implements FrontendApplicationContribution {
6869
* This event is also emitted when the board package for the currently selected board was uninstalled.
6970
*/
7071
readonly onBoardsConfigChanged = this.onBoardsConfigChangedEmitter.event;
71-
readonly onAvailableBoardsChanged = this.onAvailableBoardsChangedEmitter.event;
72+
readonly onAvailableBoardsChanged =
73+
this.onAvailableBoardsChangedEmitter.event;
7274
readonly onAvailablePortsChanged = this.onAvailablePortsChangedEmitter.event;
7375

7476
onStart(): void {
@@ -183,8 +185,8 @@ export class BoardsServiceProvider implements FrontendApplicationContribution {
183185
const selectedAvailableBoard = AvailableBoard.is(selectedBoard)
184186
? selectedBoard
185187
: this._availableBoards.find((availableBoard) =>
186-
Board.sameAs(availableBoard, selectedBoard)
187-
);
188+
Board.sameAs(availableBoard, selectedBoard)
189+
);
188190
if (
189191
selectedAvailableBoard &&
190192
selectedAvailableBoard.selected &&
@@ -274,6 +276,18 @@ export class BoardsServiceProvider implements FrontendApplicationContribution {
274276
return boards;
275277
}
276278

279+
async selectedBoardUserFields(): Promise<BoardUserField[]> {
280+
if (!this._boardsConfig.selectedBoard || !this._boardsConfig.selectedPort) {
281+
return [];
282+
}
283+
const fqbn = this._boardsConfig.selectedBoard.fqbn;
284+
if (!fqbn) {
285+
return [];
286+
}
287+
const protocol = this._boardsConfig.selectedPort.protocol;
288+
return await this.boardsService.getBoardUserFields({ fqbn, protocol });
289+
}
290+
277291
/**
278292
* `true` if the `config.selectedBoard` is defined; hence can compile against the board. Otherwise, `false`.
279293
*/
@@ -361,14 +375,14 @@ export class BoardsServiceProvider implements FrontendApplicationContribution {
361375
const timeoutTask =
362376
!!timeout && timeout > 0
363377
? new Promise<void>((_, reject) =>
364-
setTimeout(
365-
() => reject(new Error(`Timeout after ${timeout} ms.`)),
366-
timeout
378+
setTimeout(
379+
() => reject(new Error(`Timeout after ${timeout} ms.`)),
380+
timeout
381+
)
367382
)
368-
)
369383
: new Promise<void>(() => {
370-
/* never */
371-
});
384+
/* never */
385+
});
372386
const waitUntilTask = new Promise<void>((resolve) => {
373387
let candidate = find(what, this.availableBoards);
374388
if (candidate) {
@@ -406,7 +420,7 @@ export class BoardsServiceProvider implements FrontendApplicationContribution {
406420
const availableBoards: AvailableBoard[] = [];
407421
const attachedBoards = this._attachedBoards.filter(({ port }) => !!port);
408422
const availableBoardPorts = availablePorts.filter((port) => {
409-
if (port.protocol === "serial") {
423+
if (port.protocol === 'serial') {
410424
// We always show all serial ports, even if there
411425
// is no recognized board connected to it
412426
return true;
@@ -424,8 +438,12 @@ export class BoardsServiceProvider implements FrontendApplicationContribution {
424438
});
425439

426440
for (const boardPort of availableBoardPorts) {
427-
let board = attachedBoards.find(({ port }) => Port.sameAs(boardPort, port));
428-
const lastSelectedBoard = await this.getLastSelectedBoardOnPort(boardPort);
441+
const board = attachedBoards.find(({ port }) =>
442+
Port.sameAs(boardPort, port)
443+
);
444+
const lastSelectedBoard = await this.getLastSelectedBoardOnPort(
445+
boardPort
446+
);
429447

430448
let availableBoard = {} as AvailableBoard;
431449
if (board) {
@@ -454,11 +472,16 @@ export class BoardsServiceProvider implements FrontendApplicationContribution {
454472
availableBoards.push(availableBoard);
455473
}
456474

457-
if (boardsConfig.selectedBoard && !availableBoards.some(({ selected }) => selected)) {
475+
if (
476+
boardsConfig.selectedBoard &&
477+
!availableBoards.some(({ selected }) => selected)
478+
) {
458479
// If the selected board has the same port of an unknown board
459480
// that is already in availableBoards we might get a duplicate port.
460481
// So we remove the one already in the array and add the selected one.
461-
const found = availableBoards.findIndex(board => board.port?.address === boardsConfig.selectedPort?.address);
482+
const found = availableBoards.findIndex(
483+
(board) => board.port?.address === boardsConfig.selectedPort?.address
484+
);
462485
if (found >= 0) {
463486
availableBoards.splice(found, 1);
464487
}
@@ -475,15 +498,19 @@ export class BoardsServiceProvider implements FrontendApplicationContribution {
475498
let hasChanged = availableBoards.length !== currentAvailableBoards.length;
476499
for (let i = 0; !hasChanged && i < availableBoards.length; i++) {
477500
const [left, right] = [availableBoards[i], currentAvailableBoards[i]];
478-
hasChanged = !!AvailableBoard.compare(left, right) || left.selected !== right.selected;
501+
hasChanged =
502+
!!AvailableBoard.compare(left, right) ||
503+
left.selected !== right.selected;
479504
}
480505
if (hasChanged) {
481506
this._availableBoards = availableBoards;
482507
this.onAvailableBoardsChangedEmitter.fire(this._availableBoards);
483508
}
484509
}
485510

486-
protected async getLastSelectedBoardOnPort(port: Port): Promise<Board | undefined> {
511+
protected async getLastSelectedBoardOnPort(
512+
port: Port
513+
): Promise<Board | undefined> {
487514
const key = this.getLastSelectedBoardOnPortKey(port);
488515
return this.getData<Board>(key);
489516
}
@@ -504,8 +531,11 @@ export class BoardsServiceProvider implements FrontendApplicationContribution {
504531
]);
505532
}
506533

507-
protected getLastSelectedBoardOnPortKey(port: Port): string {
508-
return `last-selected-board-on-port:${Port.toString(port)}`;
534+
protected getLastSelectedBoardOnPortKey(port: Port | string): string {
535+
// 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+
}`;
509539
}
510540

511541
protected async loadState(): Promise<void> {
@@ -596,13 +626,22 @@ export namespace AvailableBoard {
596626
// 4. Network with recognized boards
597627
// 5. Other protocols with recognized boards
598628
export const compare = (left: AvailableBoard, right: AvailableBoard) => {
599-
if (left.port?.protocol === "serial" && right.port?.protocol !== "serial") {
629+
if (left.port?.protocol === 'serial' && right.port?.protocol !== 'serial') {
600630
return -1;
601-
} else if (left.port?.protocol !== "serial" && right.port?.protocol === "serial") {
631+
} else if (
632+
left.port?.protocol !== 'serial' &&
633+
right.port?.protocol === 'serial'
634+
) {
602635
return 1;
603-
} else if (left.port?.protocol === "network" && right.port?.protocol !== "network") {
636+
} else if (
637+
left.port?.protocol === 'network' &&
638+
right.port?.protocol !== 'network'
639+
) {
604640
return -1;
605-
} else if (left.port?.protocol !== "network" && right.port?.protocol === "network") {
641+
} else if (
642+
left.port?.protocol !== 'network' &&
643+
right.port?.protocol === 'network'
644+
) {
606645
return 1;
607646
} else if (left.port?.protocol === right.port?.protocol) {
608647
// We show all ports, including those that have guessed
@@ -614,5 +653,5 @@ export namespace AvailableBoard {
614653
}
615654
}
616655
return naturalCompare(left.port?.address!, right.port?.address!);
617-
}
656+
};
618657
}

arduino-ide-extension/src/common/protocol/boards-service.ts

+9
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ export interface BoardsService
143143
fqbn: string;
144144
}): Promise<BoardsPackage | undefined>;
145145
searchBoards({ query }: { query?: string }): Promise<BoardWithPackage[]>;
146+
getBoardUserFields(options: { fqbn: string, protocol: string }): Promise<BoardUserField[]>;
146147
}
147148

148149
export interface Port {
@@ -251,6 +252,14 @@ export interface Board {
251252
readonly port?: Port;
252253
}
253254

255+
export interface BoardUserField {
256+
readonly toolId: string;
257+
readonly name: string;
258+
readonly label: string;
259+
readonly secret: boolean;
260+
value: string;
261+
}
262+
254263
export interface BoardWithPackage extends Board {
255264
readonly packageName: string;
256265
readonly packageId: string;

arduino-ide-extension/src/node/boards-service-impl.ts

+32
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
NotificationServiceServer,
1717
AvailablePorts,
1818
BoardWithPackage,
19+
BoardUserField,
1920
} from '../common/protocol';
2021
import {
2122
PlatformInstallRequest,
@@ -36,6 +37,8 @@ import {
3637
import {
3738
ListProgrammersAvailableForUploadRequest,
3839
ListProgrammersAvailableForUploadResponse,
40+
SupportedUserFieldsRequest,
41+
SupportedUserFieldsResponse,
3942
} from './cli-protocol/cc/arduino/cli/commands/v1/upload_pb';
4043
import { InstallWithProgress } from './grpc-installable';
4144

@@ -244,6 +247,35 @@ export class BoardsServiceImpl
244247
return boards;
245248
}
246249

250+
async getBoardUserFields(options: { fqbn: string, protocol: string }): Promise<BoardUserField[]> {
251+
await this.coreClientProvider.initialized;
252+
const coreClient = await this.coreClient();
253+
const { client, instance } = coreClient;
254+
255+
const supportedUserFieldsReq = new SupportedUserFieldsRequest();
256+
supportedUserFieldsReq.setInstance(instance);
257+
supportedUserFieldsReq.setFqbn(options.fqbn);
258+
supportedUserFieldsReq.setProtocol(options.protocol);
259+
260+
const supportedUserFieldsResp = await new Promise<SupportedUserFieldsResponse>(
261+
(resolve, reject) => {
262+
client.supportedUserFields(supportedUserFieldsReq, (err, resp) => {
263+
(!!err ? reject : resolve)(!!err ? err : resp)
264+
})
265+
}
266+
);
267+
return supportedUserFieldsResp.getUserFieldsList().map(e => {
268+
return {
269+
toolId: e.getToolId(),
270+
name: e.getName(),
271+
label: e.getLabel(),
272+
secret: e.getSecret(),
273+
value: "",
274+
};
275+
});
276+
}
277+
278+
247279
async search(options: { query?: string }): Promise<BoardsPackage[]> {
248280
await this.coreClientProvider.initialized;
249281
const coreClient = await this.coreClient();

0 commit comments

Comments
 (0)