16
16
package commands
17
17
18
18
import (
19
- "encoding/json"
20
19
"fmt"
21
20
"sync"
22
- "time"
23
21
24
22
"github.com/arduino/arduino-cli/arduino/cores"
25
23
"github.com/arduino/arduino-cli/arduino/cores/packagemanager"
26
24
"github.com/arduino/arduino-cli/arduino/discovery"
27
25
"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"
31
26
semver "go.bug.st/relaxed-semver"
32
27
)
33
28
@@ -107,24 +102,10 @@ var (
107
102
}
108
103
)
109
104
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
-
124
105
var listBoardMutex sync.Mutex
125
106
126
107
// ListBoards foo
127
- func ListBoards (pm * packagemanager.PackageManager ) ([]* BoardPort , error ) {
108
+ func ListBoards (pm * packagemanager.PackageManager ) ([]* discovery. Port , error ) {
128
109
// ensure the connection to the discoverer is unique to avoid messing up
129
110
// the messages exchanged
130
111
listBoardMutex .Lock ()
@@ -141,67 +122,26 @@ func ListBoards(pm *packagemanager.PackageManager) ([]*BoardPort, error) {
141
122
return nil , fmt .Errorf ("missing serial-discovery tool" )
142
123
}
143
124
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 ())
157
126
if err != nil {
158
- return nil , fmt . Errorf ( "creating stdout pipe for discovery: %s" , err )
127
+ return nil , err
159
128
}
160
- outJSON := json . NewDecoder ( out )
129
+ defer disc . Quit ( )
161
130
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 )
165
133
}
166
134
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 )
170
137
}
171
138
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 )
192
142
}
193
143
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
205
145
}
206
146
207
147
// WatchListBoards returns a channel that receives events from the bundled discovery tool
0 commit comments