Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit f8394fb

Browse filesBrowse files
committed
Further simplified board watcher
We must acesss the gRPC API only until we cross the `command` package border. Once we are inside the `command` package we should use the internal API only.
1 parent af873e0 commit f8394fb
Copy full SHA for f8394fb

File tree

5 files changed

+26
-54
lines changed
Filter options

5 files changed

+26
-54
lines changed

‎arduino/discovery/discovery.go

Copy file name to clipboardExpand all lines: arduino/discovery/discovery.go
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,12 @@ type Port struct {
9797

9898
var tr = i18n.Tr
9999

100+
// Equals return true if the given port has the same address and protocol
101+
// of the current port.
102+
func (p *Port) Equals(o *Port) bool {
103+
return p.Address == o.Address && p.Protocol == o.Protocol
104+
}
105+
100106
// ToRPC converts Port into rpc.Port
101107
func (p *Port) ToRPC() *rpc.Port {
102108
props := p.Properties

‎commands/upload/burnbootloader.go

Copy file name to clipboardExpand all lines: commands/upload/burnbootloader.go
-1Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ func BurnBootloader(ctx context.Context, req *rpc.BurnBootloaderRequest, outStre
4141

4242
_, err := runProgramAction(
4343
pme,
44-
nil, // watcher
4544
nil, // sketch
4645
"", // importFile
4746
"", // importDir

‎commands/upload/upload.go

Copy file name to clipboardExpand all lines: commands/upload/upload.go
+20-30Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ import (
3131
"github.com/arduino/arduino-cli/arduino/serialutils"
3232
"github.com/arduino/arduino-cli/arduino/sketch"
3333
"github.com/arduino/arduino-cli/commands"
34-
"github.com/arduino/arduino-cli/commands/board"
3534
"github.com/arduino/arduino-cli/executils"
3635
"github.com/arduino/arduino-cli/i18n"
3736
f "github.com/arduino/arduino-cli/internal/algorithms"
@@ -144,17 +143,9 @@ func Upload(ctx context.Context, req *rpc.UploadRequest, outStream io.Writer, er
144143
}
145144
defer pmeRelease()
146145

147-
watch, watchRelease, err := board.Watch(&rpc.BoardListWatchRequest{Instance: req.GetInstance()})
148-
if err != nil {
149-
logrus.WithError(err).Error("Error watching board ports")
150-
} else {
151-
defer watchRelease()
152-
}
153-
154146
updatedPort, err := runProgramAction(
155147
pme,
156148
sk,
157-
watch,
158149
req.GetImportFile(),
159150
req.GetImportDir(),
160151
req.GetFqbn(),
@@ -201,18 +192,12 @@ func UsingProgrammer(ctx context.Context, req *rpc.UploadUsingProgrammerRequest,
201192

202193
func runProgramAction(pme *packagemanager.Explorer,
203194
sk *sketch.Sketch,
204-
watch <-chan *rpc.BoardListWatchResponse,
205195
importFile, importDir, fqbnIn string, userPort *rpc.Port,
206196
programmerID string,
207197
verbose, verify, burnBootloader bool,
208198
outStream, errStream io.Writer,
209-
dryRun bool, userFields map[string]string) (*rpc.Port, error) {
210-
211-
// Ensure watcher events consumption in case of exit on error
212-
defer func() {
213-
go f.DiscardCh(watch)
214-
}()
215-
199+
dryRun bool, userFields map[string]string,
200+
) (*rpc.Port, error) {
216201
port := discovery.PortFromRPCPort(userPort)
217202
if port == nil || (port.Address == "" && port.Protocol == "") {
218203
// For no-port uploads use "default" protocol
@@ -398,12 +383,17 @@ func runProgramAction(pme *packagemanager.Explorer,
398383

399384
// By default do not return any new port but if there is an
400385
// expected port change then run the detector.
401-
updatedUploadPort := f.NewFuture[*rpc.Port]()
402-
if uploadProperties.GetBoolean("upload.wait_for_upload_port") && watch != nil {
403-
go detectUploadPort(uploadCtx, port, watch, updatedUploadPort)
386+
updatedUploadPort := f.NewFuture[*discovery.Port]()
387+
if uploadProperties.GetBoolean("upload.wait_for_upload_port") {
388+
watcher, err := pme.DiscoveryManager().Watch()
389+
if err != nil {
390+
return nil, err
391+
}
392+
defer watcher.Close()
393+
394+
go detectUploadPort(uploadCtx, port, watcher.Feed(), updatedUploadPort)
404395
} else {
405396
updatedUploadPort.Send(nil)
406-
go f.DiscardCh(watch)
407397
}
408398

409399
// Force port wait to make easier to unbrick boards like the Arduino Leonardo, or similar with native USB,
@@ -526,16 +516,16 @@ func runProgramAction(pme *packagemanager.Explorer,
526516

527517
updatedPort := updatedUploadPort.Await()
528518
if updatedPort == nil {
529-
updatedPort = userPort
519+
return userPort, nil
530520
}
531-
return userPort, nil
521+
return updatedPort.ToRPC(), nil
532522
}
533523

534-
func detectUploadPort(uploadCtx context.Context, uploadPort *discovery.Port, watch <-chan *rpc.BoardListWatchResponse, result f.Future[*rpc.Port]) {
524+
func detectUploadPort(uploadCtx context.Context, uploadPort *discovery.Port, watch <-chan *discovery.Event, result f.Future[*discovery.Port]) {
535525
log := logrus.WithField("task", "port_detection")
536526
log.Tracef("Detecting new board port after upload")
537527

538-
var candidate *rpc.Port
528+
var candidate *discovery.Port
539529
defer func() {
540530
result.Send(candidate)
541531
}()
@@ -566,23 +556,23 @@ func detectUploadPort(uploadCtx context.Context, uploadPort *discovery.Port, wat
566556
log.Error("Upload port detection failed, watcher closed")
567557
return
568558
}
569-
if ev.EventType == "remove" && candidate != nil {
570-
if candidate.Equals(ev.Port.GetPort()) {
559+
if ev.Type == "remove" && candidate != nil {
560+
if candidate.Equals(ev.Port) {
571561
log.WithField("event", ev).Trace("Candidate port is no more available")
572562
candidate = nil
573563
continue
574564
}
575565
}
576-
if ev.EventType != "add" {
566+
if ev.Type != "add" {
577567
log.WithField("event", ev).Trace("Ignored non-add event")
578568
continue
579569
}
580-
candidate = ev.Port.GetPort()
570+
candidate = ev.Port
581571
log.WithField("event", ev).Trace("New upload port candidate")
582572

583573
// If the current candidate port does not have the desired HW-ID do
584574
// not return it immediately.
585-
if desiredHwID != "" && candidate.GetHardwareId() != desiredHwID {
575+
if desiredHwID != "" && candidate.HardwareID != desiredHwID {
586576
log.Trace("New candidate port did not match desired HW ID, keep watching...")
587577
continue
588578
}

‎commands/upload/upload_test.go

Copy file name to clipboardExpand all lines: commands/upload/upload_test.go
-1Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,6 @@ func TestUploadPropertiesComposition(t *testing.T) {
187187
_, err := runProgramAction(
188188
pme,
189189
nil, // sketch
190-
nil, // board watcher
191190
"", // importFile
192191
test.importDir.String(), // importDir
193192
test.fqbn, // FQBN

‎rpc/cc/arduino/cli/commands/v1/port.go

Copy file name to clipboardExpand all lines: rpc/cc/arduino/cli/commands/v1/port.go
-22Lines changed: 0 additions & 22 deletions
This file was deleted.

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.