Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 173d764

Browse files
committedAug 8, 2019
query the backend to get the fqbn
1 parent 7e547b0 commit 173d764

File tree

4 files changed

+50
-13
lines changed

4 files changed

+50
-13
lines changed
 

‎arduino/cores/packagemanager/identify.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424
properties "github.com/arduino/go-properties-orderedmap"
2525
)
2626

27-
// IdentifyBoard returns a list of baords matching the provided identification properties.
27+
// IdentifyBoard returns a list of boards matching the provided identification properties.
2828
func (pm *PackageManager) IdentifyBoard(idProps *properties.Map) []*cores.Board {
2929
if idProps.Size() == 0 {
3030
return []*cores.Board{}

‎cli/board/list.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,19 +60,18 @@ func runListCommand(cmd *cobra.Command, args []string) {
6060
time.Sleep(timeout)
6161
}
6262

63-
resp, err := board.List(instance.CreateInstance().GetId())
63+
ports, err := board.List(instance.CreateInstance().GetId())
6464
if err != nil {
6565
formatter.PrintError(err, "Error detecting boards")
6666
os.Exit(errorcodes.ErrNetwork)
6767
}
6868

69-
if output.JSONOrElse(resp) {
70-
outputListResp(resp)
69+
if output.JSONOrElse(ports) {
70+
outputListResp(ports)
7171
}
7272
}
7373

74-
func outputListResp(resp *rpc.BoardListResp) {
75-
ports := resp.GetPorts()
74+
func outputListResp(ports []*rpc.DetectedPort) {
7675
if len(ports) == 0 {
7776
formatter.Print("No boards found.")
7877
return
@@ -84,7 +83,7 @@ func outputListResp(resp *rpc.BoardListResp) {
8483
})
8584
table := output.NewTable()
8685
table.SetHeader("Port", "Type", "Board Name", "FQBN")
87-
for _, port := range resp.GetPorts() {
86+
for _, port := range ports {
8887
address := port.GetProtocol() + "://" + port.GetAddress()
8988
if port.GetProtocol() == "serial" {
9089
address = port.GetAddress()

‎commands/board/list.go

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,18 @@
1818
package board
1919

2020
import (
21+
"encoding/json"
22+
"fmt"
23+
"io/ioutil"
24+
"net/http"
25+
2126
"github.com/arduino/arduino-cli/commands"
2227
rpc "github.com/arduino/arduino-cli/rpc/commands"
2328
"github.com/pkg/errors"
2429
)
2530

2631
// List FIXMEDOC
27-
func List(instanceID int32) (*rpc.BoardListResp, error) {
32+
func List(instanceID int32) ([]*rpc.DetectedPort, error) {
2833
pm := commands.GetPackageManager(instanceID)
2934
if pm == nil {
3035
return nil, errors.New("invalid instance")
@@ -40,29 +45,55 @@ func List(instanceID int32) (*rpc.BoardListResp, error) {
4045
}
4146
defer serialDiscovery.Close()
4247

43-
resp := &rpc.BoardListResp{Ports: []*rpc.DetectedPort{}}
44-
4548
ports, err := serialDiscovery.List()
4649
if err != nil {
4750
return nil, errors.Wrap(err, "error getting port list from serial-discovery")
4851
}
4952

53+
retVal := []*rpc.DetectedPort{}
5054
for _, port := range ports {
5155
b := []*rpc.BoardListItem{}
56+
57+
// first query installed cores through the Package Manager
5258
for _, board := range pm.IdentifyBoard(port.IdentificationPrefs) {
5359
b = append(b, &rpc.BoardListItem{
5460
Name: board.Name(),
5561
FQBN: board.FQBN(),
5662
})
5763
}
64+
65+
// if installed cores didn't recognize the board, try querying
66+
// the builder API
67+
if len(b) == 0 {
68+
url := fmt.Sprintf("https://builder.arduino.cc/v3/boards/byVidPid/%s/%s",
69+
port.IdentificationPrefs.Get("vid"),
70+
port.IdentificationPrefs.Get("pid"))
71+
req, _ := http.NewRequest("GET", url, nil)
72+
if res, err := http.DefaultClient.Do(req); err == nil {
73+
body, _ := ioutil.ReadAll(res.Body)
74+
res.Body.Close()
75+
76+
var dat map[string]interface{}
77+
78+
if err := json.Unmarshal(body, &dat); err == nil {
79+
b = append(b, &rpc.BoardListItem{
80+
Name: dat["name"].(string),
81+
FQBN: dat["fqbn"].(string),
82+
})
83+
}
84+
}
85+
}
86+
87+
// boards slice can be empty at this point if neither the cores nor the
88+
// API managed to recognize the connected board
5889
p := &rpc.DetectedPort{
5990
Address: port.Address,
6091
Protocol: port.Protocol,
6192
ProtocolLabel: port.ProtocolLabel,
6293
Boards: b,
6394
}
64-
resp.Ports = append(resp.Ports, p)
95+
retVal = append(retVal, p)
6596
}
6697

67-
return resp, nil
98+
return retVal, nil
6899
}

‎commands/daemon/daemon.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,14 @@ func (s *ArduinoCoreServerImpl) BoardDetails(ctx context.Context, req *rpc.Board
4949

5050
// BoardList FIXMEDOC
5151
func (s *ArduinoCoreServerImpl) BoardList(ctx context.Context, req *rpc.BoardListReq) (*rpc.BoardListResp, error) {
52-
return board.List(req.GetInstance().GetId())
52+
ports, err := board.List(req.GetInstance().GetId())
53+
if err != nil {
54+
return nil, err
55+
}
56+
57+
return &rpc.BoardListResp{
58+
Ports: ports,
59+
}, nil
5360
}
5461

5562
// BoardListAll FIXMEDOC

0 commit comments

Comments
 (0)
Please sign in to comment.