Skip to content

Commit b676a2d

Browse files
committed
Correctly implemented 'board list' timeout option
1 parent 85d0ad6 commit b676a2d

19 files changed

+182
-156
lines changed

cli/board/list.go

+7-12
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,16 @@ func initListCommand() *cobra.Command {
4141
Run: runListCommand,
4242
}
4343

44-
listCommand.Flags().StringVar(&listFlags.timeout, "timeout", "0s",
45-
"The connected devices search timeout, raise it if your board doesn't show up (e.g. to 10s).")
44+
listCommand.Flags().DurationVar(&listFlags.timeout, "timeout", 0,
45+
"The connected devices search timeout, raise it if your board doesn't show up e.g.: 10s")
4646
listCommand.Flags().BoolVarP(&listFlags.watch, "watch", "w", false,
4747
"Command keeps running and prints list of connected boards whenever there is a change.")
4848

4949
return listCommand
5050
}
5151

5252
var listFlags struct {
53-
timeout string // Expressed in a parsable duration, is the timeout for the list and attach commands.
53+
timeout time.Duration
5454
watch bool
5555
}
5656

@@ -62,20 +62,15 @@ func runListCommand(cmd *cobra.Command, args []string) {
6262
os.Exit(0)
6363
}
6464

65-
if timeout, err := time.ParseDuration(listFlags.timeout); err != nil {
66-
feedback.Errorf("Invalid timeout: %v", err)
67-
os.Exit(errorcodes.ErrBadArgument)
68-
} else {
69-
time.Sleep(timeout)
70-
}
71-
7265
inst := instance.CreateAndInit()
73-
ports, err := board.List(inst.GetId())
66+
ports, err := board.List(&rpc.BoardListRequest{
67+
Instance: inst,
68+
Timeout: listFlags.timeout.Milliseconds(),
69+
})
7470
if err != nil {
7571
feedback.Errorf("Error detecting boards: %v", err)
7672
os.Exit(errorcodes.ErrNetwork)
7773
}
78-
7974
feedback.PrintResult(result{ports})
8075
}
8176

commands/board/list.go

+13-11
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import (
2323
"regexp"
2424
"sort"
2525
"strings"
26-
"sync"
26+
"time"
2727

2828
"github.com/arduino/arduino-cli/arduino/cores/packagemanager"
2929
"github.com/arduino/arduino-cli/arduino/discovery"
@@ -38,7 +38,6 @@ import (
3838
var (
3939
// ErrNotFound is returned when the API returns 404
4040
ErrNotFound = errors.New("board not found")
41-
m sync.Mutex
4241
vidPidURL = "https://builder.arduino.cc/v3/boards/byVidPid"
4342
validVidPid = regexp.MustCompile(`0[xX][a-fA-F\d]{4}`)
4443
)
@@ -173,10 +172,7 @@ func identify(pm *packagemanager.PackageManager, port *discovery.Port) ([]*rpc.B
173172
}
174173

175174
// List FIXMEDOC
176-
func List(instanceID int32) (r []*rpc.DetectedPort, e error) {
177-
m.Lock()
178-
defer m.Unlock()
179-
175+
func List(req *rpc.BoardListRequest) (r []*rpc.DetectedPort, e error) {
180176
tags := map[string]string{}
181177
// Use defer func() to evaluate tags map when function returns
182178
// and set success flag inspecting the error named return parameter
@@ -188,21 +184,27 @@ func List(instanceID int32) (r []*rpc.DetectedPort, e error) {
188184
stats.Incr("compile", stats.M(tags)...)
189185
}()
190186

191-
pm := commands.GetPackageManager(instanceID)
187+
pm := commands.GetPackageManager(req.GetInstance().Id)
192188
if pm == nil {
193189
return nil, errors.New("invalid instance")
194190
}
195191

196-
if err := pm.DiscoveryManager().RunAll(); err != nil {
192+
dm := pm.DiscoveryManager()
193+
if err := dm.RunAll(); err != nil {
197194
return nil, err
198195
}
199-
if err := pm.DiscoveryManager().StartAll(); err != nil {
196+
if err := dm.StartAll(); err != nil {
200197
return nil, err
201198
}
202-
ports := pm.DiscoveryManager().List()
199+
defer func() {
200+
if err := dm.StopAll(); err != nil {
201+
logrus.Error(err)
202+
}
203+
}()
204+
time.Sleep(time.Duration(req.GetTimeout()) * time.Millisecond)
203205

204206
retVal := []*rpc.DetectedPort{}
205-
for _, port := range ports {
207+
for _, port := range pm.DiscoveryManager().List() {
206208
boards, err := identify(pm, port)
207209
if err != nil {
208210
return nil, err

commands/daemon/daemon.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func (s *ArduinoCoreServerImpl) BoardDetails(ctx context.Context, req *rpc.Board
4545

4646
// BoardList FIXMEDOC
4747
func (s *ArduinoCoreServerImpl) BoardList(ctx context.Context, req *rpc.BoardListRequest) (*rpc.BoardListResponse, error) {
48-
ports, err := board.List(req.GetInstance().GetId())
48+
ports, err := board.List(req)
4949
if err != nil {
5050
return nil, err
5151
}

rpc/cc/arduino/cli/commands/v1/board.pb.go

+98-87
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rpc/cc/arduino/cli/commands/v1/board.proto

+2
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,8 @@ message BoardAttachResponse {
174174
message BoardListRequest {
175175
// Arduino Core Service instance from the `Init` response.
176176
Instance instance = 1;
177+
// Search for boards for the given time (in milliseconds)
178+
int64 timeout = 2;
177179
}
178180

179181
message BoardListResponse {

rpc/cc/arduino/cli/commands/v1/commands.pb.go

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rpc/cc/arduino/cli/commands/v1/commands_grpc.pb.go

+28-24
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rpc/cc/arduino/cli/commands/v1/common.pb.go

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rpc/cc/arduino/cli/commands/v1/compile.pb.go

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rpc/cc/arduino/cli/commands/v1/core.pb.go

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rpc/cc/arduino/cli/commands/v1/lib.pb.go

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rpc/cc/arduino/cli/commands/v1/port.pb.go

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)