@@ -31,7 +31,6 @@ import (
31
31
"github.com/arduino/arduino-cli/arduino/serialutils"
32
32
"github.com/arduino/arduino-cli/arduino/sketch"
33
33
"github.com/arduino/arduino-cli/commands"
34
- "github.com/arduino/arduino-cli/commands/board"
35
34
"github.com/arduino/arduino-cli/executils"
36
35
"github.com/arduino/arduino-cli/i18n"
37
36
f "github.com/arduino/arduino-cli/internal/algorithms"
@@ -144,17 +143,9 @@ func Upload(ctx context.Context, req *rpc.UploadRequest, outStream io.Writer, er
144
143
}
145
144
defer pmeRelease ()
146
145
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
-
154
146
updatedPort , err := runProgramAction (
155
147
pme ,
156
148
sk ,
157
- watch ,
158
149
req .GetImportFile (),
159
150
req .GetImportDir (),
160
151
req .GetFqbn (),
@@ -201,18 +192,12 @@ func UsingProgrammer(ctx context.Context, req *rpc.UploadUsingProgrammerRequest,
201
192
202
193
func runProgramAction (pme * packagemanager.Explorer ,
203
194
sk * sketch.Sketch ,
204
- watch <- chan * rpc.BoardListWatchResponse ,
205
195
importFile , importDir , fqbnIn string , userPort * rpc.Port ,
206
196
programmerID string ,
207
197
verbose , verify , burnBootloader bool ,
208
198
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 ) {
216
201
port := discovery .PortFromRPCPort (userPort )
217
202
if port == nil || (port .Address == "" && port .Protocol == "" ) {
218
203
// For no-port uploads use "default" protocol
@@ -398,12 +383,17 @@ func runProgramAction(pme *packagemanager.Explorer,
398
383
399
384
// By default do not return any new port but if there is an
400
385
// 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 )
404
395
} else {
405
396
updatedUploadPort .Send (nil )
406
- go f .DiscardCh (watch )
407
397
}
408
398
409
399
// 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,
526
516
527
517
updatedPort := updatedUploadPort .Await ()
528
518
if updatedPort == nil {
529
- updatedPort = userPort
519
+ return userPort , nil
530
520
}
531
- return userPort , nil
521
+ return updatedPort . ToRPC () , nil
532
522
}
533
523
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 ]) {
535
525
log := logrus .WithField ("task" , "port_detection" )
536
526
log .Tracef ("Detecting new board port after upload" )
537
527
538
- var candidate * rpc .Port
528
+ var candidate * discovery .Port
539
529
defer func () {
540
530
result .Send (candidate )
541
531
}()
@@ -566,23 +556,23 @@ func detectUploadPort(uploadCtx context.Context, uploadPort *discovery.Port, wat
566
556
log .Error ("Upload port detection failed, watcher closed" )
567
557
return
568
558
}
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 ) {
571
561
log .WithField ("event" , ev ).Trace ("Candidate port is no more available" )
572
562
candidate = nil
573
563
continue
574
564
}
575
565
}
576
- if ev .EventType != "add" {
566
+ if ev .Type != "add" {
577
567
log .WithField ("event" , ev ).Trace ("Ignored non-add event" )
578
568
continue
579
569
}
580
- candidate = ev .Port . GetPort ()
570
+ candidate = ev .Port
581
571
log .WithField ("event" , ev ).Trace ("New upload port candidate" )
582
572
583
573
// If the current candidate port does not have the desired HW-ID do
584
574
// not return it immediately.
585
- if desiredHwID != "" && candidate .GetHardwareId () != desiredHwID {
575
+ if desiredHwID != "" && candidate .HardwareID != desiredHwID {
586
576
log .Trace ("New candidate port did not match desired HW ID, keep watching..." )
587
577
continue
588
578
}
0 commit comments