Skip to content

Commit f8394fb

Browse 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

File tree

5 files changed

+26
-54
lines changed

5 files changed

+26
-54
lines changed

Diff for: arduino/discovery/discovery.go

+6
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

Diff for: commands/upload/burnbootloader.go

-1
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

Diff for: commands/upload/upload.go

+20-30
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
}

Diff for: commands/upload/upload_test.go

-1
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

Diff for: rpc/cc/arduino/cli/commands/v1/port.go

-22
This file was deleted.

0 commit comments

Comments
 (0)