@@ -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
202193func 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 }
0 commit comments