-
-
Notifications
You must be signed in to change notification settings - Fork 398
/
Copy pathlist.go
153 lines (131 loc) · 4.28 KB
/
list.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
* This file is part of arduino-cli.
*
* Copyright 2018 ARDUINO SA (http://www.arduino.cc/)
*
* This software is released under the GNU General Public License version 3,
* which covers the main part of arduino-cli.
* The terms of this license can be found at:
* https://www.gnu.org/licenses/gpl-3.0.en.html
*
* You can be released from the requirements of the above licenses by purchasing
* a commercial license. Buying such a license is mandatory if you want to modify or
* otherwise use the software for commercial activities involving the Arduino
* software without disclosing the source code of your own applications. To purchase
* a commercial license, send an email to [email protected].
*/
package board
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strconv"
"sync"
"github.com/arduino/arduino-cli/cli/globals"
"github.com/arduino/arduino-cli/commands"
rpc "github.com/arduino/arduino-cli/rpc/commands"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
var (
// ErrNotFound is returned when the API returns 404
ErrNotFound = errors.New("board not found")
m sync.Mutex
vidPidURL = "https://builder.arduino.cc/v3/boards/byVidPid"
)
func apiByVidPid(vid, pid string) ([]*rpc.BoardListItem, error) {
// ensure vid and pid are valid before hitting the API
_, vidErr := strconv.ParseInt(vid, 0, 64)
_, pidErr := strconv.ParseInt(pid, 0, 64)
if vidErr != nil || pidErr != nil {
return nil, errors.Errorf("Invalid vid/pid value: '%s:%s'", vid, pid)
}
url := fmt.Sprintf("%s/%s/%s", vidPidURL, vid, pid)
retVal := []*rpc.BoardListItem{}
req, _ := http.NewRequest("GET", url, nil)
req.Header = globals.NewHTTPClientHeader()
req.Header.Set("Content-Type", "application/json")
if res, err := http.DefaultClient.Do(req); err == nil {
if res.StatusCode >= 400 {
if res.StatusCode == 404 {
return nil, ErrNotFound
}
return nil, errors.Errorf("the server responded with status %s", res.Status)
}
body, _ := ioutil.ReadAll(res.Body)
res.Body.Close()
var dat map[string]interface{}
err = json.Unmarshal(body, &dat)
if err != nil {
return nil, errors.Wrap(err, "error processing response from server")
}
name, nameFound := dat["name"].(string)
fqbn, fbqnFound := dat["fqbn"].(string)
if !nameFound || !fbqnFound {
return nil, errors.New("wrong format in server response")
}
retVal = append(retVal, &rpc.BoardListItem{
Name: name,
FQBN: fqbn,
})
} else {
return nil, errors.Wrap(err, "error querying Arduino Cloud Api")
}
return retVal, nil
}
// List FIXMEDOC
func List(instanceID int32) ([]*rpc.DetectedPort, error) {
m.Lock()
defer m.Unlock()
pm := commands.GetPackageManager(instanceID)
if pm == nil {
return nil, errors.New("invalid instance")
}
ports, err := commands.ListBoards(pm)
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
logrus.Debug("Querying installed cores for board identification...")
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 {
logrus.Debug("Querying builder API for board identification...")
items, err := apiByVidPid(
port.IdentificationPrefs.Get("vid"),
port.IdentificationPrefs.Get("pid"),
)
if err == ErrNotFound {
// the board couldn't be detected, print a warning
logrus.Debug("Board not recognized")
} else if err != nil {
// this is bad, bail out
return nil, errors.Wrap(err, "error getting board info from Arduino Cloud")
}
// add a DetectedPort entry in any case: the `Boards` field will
// be empty but the port will be shown anyways (useful for 3rd party
// boards)
b = items
}
// 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,
}
retVal = append(retVal, p)
}
return retVal, nil
}