1616package commands
1717
1818import (
19- "encoding/json"
2019 "fmt"
2120 "sync"
22- "time"
2321
2422 "github.com/arduino/arduino-cli/arduino/cores"
2523 "github.com/arduino/arduino-cli/arduino/cores/packagemanager"
2624 "github.com/arduino/arduino-cli/arduino/discovery"
2725 "github.com/arduino/arduino-cli/arduino/resources"
28- "github.com/arduino/arduino-cli/executils"
29- "github.com/arduino/go-properties-orderedmap"
30- "github.com/pkg/errors"
3126 semver "go.bug.st/relaxed-semver"
3227)
3328
@@ -107,24 +102,10 @@ var (
107102 }
108103)
109104
110- // BoardPort is a generic port descriptor
111- type BoardPort struct {
112- Address string `json:"address"`
113- Label string `json:"label"`
114- Properties * properties.Map `json:"properties"`
115- Protocol string `json:"protocol"`
116- ProtocolLabel string `json:"protocolLabel"`
117- }
118-
119- type eventJSON struct {
120- EventType string `json:"eventType,required"`
121- Ports []* BoardPort `json:"ports"`
122- }
123-
124105var listBoardMutex sync.Mutex
125106
126107// ListBoards foo
127- func ListBoards (pm * packagemanager.PackageManager ) ([]* BoardPort , error ) {
108+ func ListBoards (pm * packagemanager.PackageManager ) ([]* discovery. Port , error ) {
128109 // ensure the connection to the discoverer is unique to avoid messing up
129110 // the messages exchanged
130111 listBoardMutex .Lock ()
@@ -141,67 +122,26 @@ func ListBoards(pm *packagemanager.PackageManager) ([]*BoardPort, error) {
141122 return nil , fmt .Errorf ("missing serial-discovery tool" )
142123 }
143124
144- // build the command to be executed
145- cmd , err := executils .NewProcessFromPath (t .InstallDir .Join ("serial-discovery" ))
146- if err != nil {
147- return nil , errors .Wrap (err , "creating discovery process" )
148- }
149-
150- // attach in/out pipes to the process
151- in , err := cmd .StdinPipe ()
152- if err != nil {
153- return nil , fmt .Errorf ("creating stdin pipe for discovery: %s" , err )
154- }
155-
156- out , err := cmd .StdoutPipe ()
125+ disc , err := discovery .New ("serial-discovery" , t .InstallDir .Join (t .Tool .Name ).String ())
157126 if err != nil {
158- return nil , fmt . Errorf ( "creating stdout pipe for discovery: %s" , err )
127+ return nil , err
159128 }
160- outJSON := json . NewDecoder ( out )
129+ defer disc . Quit ( )
161130
162- // start the process
163- if err := cmd .Start (); err != nil {
164- return nil , fmt .Errorf ("starting discovery process: %s" , err )
131+ if err = disc .Hello (); err != nil {
132+ return nil , fmt .Errorf ("starting discovery: %v" , err )
165133 }
166134
167- // send the LIST command
168- if _ , err := in .Write ([]byte ("LIST\n " )); err != nil {
169- return nil , fmt .Errorf ("sending LIST command to discovery: %s" , err )
135+ if err = disc .Start (); err != nil {
136+ return nil , fmt .Errorf ("starting discovery: %v" , err )
170137 }
171138
172- // read the response from the pipe
173- decodeResult := make (chan error )
174- var event eventJSON
175- go func () {
176- decodeResult <- outJSON .Decode (& event )
177- }()
178-
179- var finalError error
180- var retVal []* BoardPort
181-
182- // wait for the response
183- select {
184- case err := <- decodeResult :
185- if err == nil {
186- retVal = event .Ports
187- } else {
188- finalError = err
189- }
190- case <- time .After (10 * time .Second ):
191- finalError = fmt .Errorf ("decoding LIST command: timeout" )
139+ res , err := disc .List ()
140+ if err != nil {
141+ return nil , fmt .Errorf ("getting port list from discovery: %v" , err )
192142 }
193143
194- // tell the process to quit
195- in .Write ([]byte ("QUIT\n " ))
196- in .Close ()
197- out .Close ()
198- // kill the process if it takes too long to quit
199- time .AfterFunc (time .Second , func () {
200- cmd .Kill ()
201- })
202- cmd .Wait ()
203-
204- return retVal , finalError
144+ return res , nil
205145}
206146
207147// WatchListBoards returns a channel that receives events from the bundled discovery tool
0 commit comments