Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
query the backend to get the fqbn
  • Loading branch information
masci committed Aug 8, 2019
commit 173d76493b2f85ae09c2fb629c43152ea102ca06
2 changes: 1 addition & 1 deletion arduino/cores/packagemanager/identify.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
properties "github.com/arduino/go-properties-orderedmap"
)

// IdentifyBoard returns a list of baords matching the provided identification properties.
// IdentifyBoard returns a list of boards matching the provided identification properties.
func (pm *PackageManager) IdentifyBoard(idProps *properties.Map) []*cores.Board {
if idProps.Size() == 0 {
return []*cores.Board{}
Expand Down
11 changes: 5 additions & 6 deletions cli/board/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,18 @@ func runListCommand(cmd *cobra.Command, args []string) {
time.Sleep(timeout)
}

resp, err := board.List(instance.CreateInstance().GetId())
ports, err := board.List(instance.CreateInstance().GetId())
if err != nil {
formatter.PrintError(err, "Error detecting boards")
os.Exit(errorcodes.ErrNetwork)
}

if output.JSONOrElse(resp) {
outputListResp(resp)
if output.JSONOrElse(ports) {
outputListResp(ports)
}
}

func outputListResp(resp *rpc.BoardListResp) {
ports := resp.GetPorts()
func outputListResp(ports []*rpc.DetectedPort) {
if len(ports) == 0 {
formatter.Print("No boards found.")
return
Expand All @@ -84,7 +83,7 @@ func outputListResp(resp *rpc.BoardListResp) {
})
table := output.NewTable()
table.SetHeader("Port", "Type", "Board Name", "FQBN")
for _, port := range resp.GetPorts() {
for _, port := range ports {
address := port.GetProtocol() + "://" + port.GetAddress()
if port.GetProtocol() == "serial" {
address = port.GetAddress()
Expand Down
41 changes: 36 additions & 5 deletions commands/board/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,18 @@
package board

import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"

"github.com/arduino/arduino-cli/commands"
rpc "github.com/arduino/arduino-cli/rpc/commands"
"github.com/pkg/errors"
)

// List FIXMEDOC
func List(instanceID int32) (*rpc.BoardListResp, error) {
func List(instanceID int32) ([]*rpc.DetectedPort, error) {
pm := commands.GetPackageManager(instanceID)
if pm == nil {
return nil, errors.New("invalid instance")
Expand All @@ -40,29 +45,55 @@ func List(instanceID int32) (*rpc.BoardListResp, error) {
}
defer serialDiscovery.Close()

resp := &rpc.BoardListResp{Ports: []*rpc.DetectedPort{}}

ports, err := serialDiscovery.List()
if err != nil {
return nil, errors.Wrap(err, "error getting port list from serial-discovery")
}

retVal := []*rpc.DetectedPort{}
for _, port := range ports {
b := []*rpc.BoardListItem{}

// first query installed cores through the Package Manager
for _, board := range pm.IdentifyBoard(port.IdentificationPrefs) {
b = append(b, &rpc.BoardListItem{
Name: board.Name(),
FQBN: board.FQBN(),
})
}

// if installed cores didn't recognize the board, try querying
// the builder API
if len(b) == 0 {
url := fmt.Sprintf("https://builder.arduino.cc/v3/boards/byVidPid/%s/%s",
port.IdentificationPrefs.Get("vid"),
port.IdentificationPrefs.Get("pid"))
req, _ := http.NewRequest("GET", url, nil)
if res, err := http.DefaultClient.Do(req); err == nil {
body, _ := ioutil.ReadAll(res.Body)
res.Body.Close()

var dat map[string]interface{}

if err := json.Unmarshal(body, &dat); err == nil {
b = append(b, &rpc.BoardListItem{
Name: dat["name"].(string),
FQBN: dat["fqbn"].(string),
})
}
}
}

// boards slice can be empty at this point if neither the cores nor the
// API managed to recognize the connected board
p := &rpc.DetectedPort{
Address: port.Address,
Protocol: port.Protocol,
ProtocolLabel: port.ProtocolLabel,
Boards: b,
}
resp.Ports = append(resp.Ports, p)
retVal = append(retVal, p)
}

return resp, nil
return retVal, nil
}
9 changes: 8 additions & 1 deletion commands/daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,14 @@ func (s *ArduinoCoreServerImpl) BoardDetails(ctx context.Context, req *rpc.Board

// BoardList FIXMEDOC
func (s *ArduinoCoreServerImpl) BoardList(ctx context.Context, req *rpc.BoardListReq) (*rpc.BoardListResp, error) {
return board.List(req.GetInstance().GetId())
ports, err := board.List(req.GetInstance().GetId())
if err != nil {
return nil, err
}

return &rpc.BoardListResp{
Ports: ports,
}, nil
}

// BoardListAll FIXMEDOC
Expand Down