forked from arduino/arduino-cli
- Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbundled_tools_serial_discovery.go
241 lines (214 loc) · 8.25 KB
/
bundled_tools_serial_discovery.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
// This file is part of arduino-cli.
//
// Copyright 2020 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 license@arduino.cc.
package commands
import (
"encoding/json"
"fmt"
"sync"
"time"
"github.com/arduino/arduino-cli/arduino/cores"
"github.com/arduino/arduino-cli/arduino/cores/packagemanager"
"github.com/arduino/arduino-cli/arduino/discovery"
"github.com/arduino/arduino-cli/arduino/resources"
"github.com/arduino/arduino-cli/executils"
"github.com/arduino/go-properties-orderedmap"
"github.com/pkg/errors"
semver "go.bug.st/relaxed-semver"
)
var (
serialDiscoveryVersion=semver.ParseRelaxed("1.2.1")
serialDiscoveryFlavors= []*cores.Flavor{
{
OS: "i686-pc-linux-gnu",
Resource: &resources.DownloadResource{
ArchiveFileName: fmt.Sprintf("serial-discovery_v%s_Linux_32bit.tar.bz2", serialDiscoveryVersion),
URL: fmt.Sprintf("https://downloads.arduino.cc/discovery/serial-discovery/serial-discovery_v%s_Linux_32bit.tar.gz", serialDiscoveryVersion),
Size: 1623562,
Checksum: "SHA-256:624996c2483cd66dc318e9559b9e25754180514a794acd390b4c0de58742d335",
CachePath: "tools",
},
},
{
OS: "x86_64-pc-linux-gnu",
Resource: &resources.DownloadResource{
ArchiveFileName: fmt.Sprintf("serial-discovery_v%s_Linux_64bit.tar.bz2", serialDiscoveryVersion),
URL: fmt.Sprintf("https://downloads.arduino.cc/discovery/serial-discovery/serial-discovery_v%s_Linux_64bit.tar.gz", serialDiscoveryVersion),
Size: 1679556,
Checksum: "SHA-256:fca394dde79838cdd4cbeaa4a249237c95869c7d39ec5c778ecdc76d227679ef",
CachePath: "tools",
},
},
{
OS: "i686-mingw32",
Resource: &resources.DownloadResource{
ArchiveFileName: fmt.Sprintf("serial-discovery_v%s_Windows_32bit.zip", serialDiscoveryVersion),
URL: fmt.Sprintf("https://downloads.arduino.cc/discovery/serial-discovery/serial-discovery_v%s_Windows_32bit.zip", serialDiscoveryVersion),
Size: 1735085,
Checksum: "SHA-256:c2d8e92e790862ee3374810121a588c9e8c6e6ff8100112912c05312e04e7570",
CachePath: "tools",
},
},
{
OS: "x86_64-mingw32",
Resource: &resources.DownloadResource{
ArchiveFileName: fmt.Sprintf("serial-discovery_v%s_Windows_64bit.zip", serialDiscoveryVersion),
URL: fmt.Sprintf("https://downloads.arduino.cc/discovery/serial-discovery/serial-discovery_v%s_Windows_64bit.zip", serialDiscoveryVersion),
Size: 1700849,
Checksum: "SHA-256:4da007d89bb5134e7c44a70d23b026470ee0466912db16254a2a6f431cb7d9a4",
CachePath: "tools",
},
},
{
OS: "x86_64-apple-darwin",
Resource: &resources.DownloadResource{
ArchiveFileName: fmt.Sprintf("serial-discovery_v%s_macOS_64bit.tar.bz2", serialDiscoveryVersion),
URL: fmt.Sprintf("https://downloads.arduino.cc/discovery/serial-discovery/serial-discovery_v%s_macOS_64bit.tar.gz", serialDiscoveryVersion),
Size: 872913,
Checksum: "SHA-256:c3dfb2d20b1fe839ddfba3567377200d335a93eff19b0a0f553db76d5a7e2dbd",
CachePath: "tools",
},
},
{
OS: "arm-linux-gnueabihf",
Resource: &resources.DownloadResource{
ArchiveFileName: fmt.Sprintf("serial-discovery_v%s_Linux_ARMv6.tar.bz2", serialDiscoveryVersion),
URL: fmt.Sprintf("https://downloads.arduino.cc/discovery/serial-discovery/serial-discovery_v%s_Linux_ARMv6.tar.gz", serialDiscoveryVersion),
Size: 1560586,
Checksum: "SHA-256:dd1687748c59ba94631f63a1f8ebee7ec21f5f40c01f5b0510f02c72c71d2d66",
CachePath: "tools",
},
},
{
OS: "arm64-linux-gnueabihf",
Resource: &resources.DownloadResource{
ArchiveFileName: fmt.Sprintf("serial-discovery_v%s_Linux_ARM64.tar.bz2", serialDiscoveryVersion),
URL: fmt.Sprintf("https://downloads.arduino.cc/discovery/serial-discovery/serial-discovery_v%s_Linux_ARM64.tar.gz", serialDiscoveryVersion),
Size: 1573778,
Checksum: "SHA-256:3f44b5932dcfc2f01a72536d19080e45976d87c0652fcfbe75a87451327faa1f",
CachePath: "tools",
},
},
}
)
// BoardPort is a generic port descriptor
typeBoardPortstruct {
Addressstring`json:"address"`
Labelstring`json:"label"`
Prefs*properties.Map`json:"prefs"`
IdentificationPrefs*properties.Map`json:"identificationPrefs"`
Protocolstring`json:"protocol"`
ProtocolLabelstring`json:"protocolLabel"`
}
typeeventJSONstruct {
EventTypestring`json:"eventType,required"`
Ports []*BoardPort`json:"ports"`
}
varlistBoardMutex sync.Mutex
// ListBoards foo
funcListBoards(pm*packagemanager.PackageManager) ([]*BoardPort, error) {
// ensure the connection to the discoverer is unique to avoid messing up
// the messages exchanged
listBoardMutex.Lock()
deferlistBoardMutex.Unlock()
// get the bundled tool
t, err:=getBuiltinSerialDiscoveryTool(pm)
iferr!=nil {
returnnil, err
}
// determine if it's installed
if!t.IsInstalled() {
returnnil, fmt.Errorf("missing serial-discovery tool")
}
// build the command to be executed
cmd, err:=executils.NewProcessFromPath(t.InstallDir.Join("serial-discovery"))
iferr!=nil {
returnnil, errors.Wrap(err, "creating discovery process")
}
// attach in/out pipes to the process
in, err:=cmd.StdinPipe()
iferr!=nil {
returnnil, fmt.Errorf("creating stdin pipe for discovery: %s", err)
}
out, err:=cmd.StdoutPipe()
iferr!=nil {
returnnil, fmt.Errorf("creating stdout pipe for discovery: %s", err)
}
outJSON:=json.NewDecoder(out)
// start the process
iferr:=cmd.Start(); err!=nil {
returnnil, fmt.Errorf("starting discovery process: %s", err)
}
// send the LIST command
if_, err:=in.Write([]byte("LIST\n")); err!=nil {
returnnil, fmt.Errorf("sending LIST command to discovery: %s", err)
}
// read the response from the pipe
decodeResult:=make(chanerror)
vareventeventJSON
gofunc() {
decodeResult<-outJSON.Decode(&event)
}()
varfinalErrorerror
varretVal []*BoardPort
// wait for the response
select {
caseerr:=<-decodeResult:
iferr==nil {
retVal=event.Ports
} else {
finalError=err
}
case<-time.After(10*time.Second):
finalError=fmt.Errorf("decoding LIST command: timeout")
}
// tell the process to quit
in.Write([]byte("QUIT\n"))
in.Close()
out.Close()
// kill the process if it takes too long to quit
time.AfterFunc(time.Second, func() {
cmd.Kill()
})
cmd.Wait()
returnretVal, finalError
}
// WatchListBoards returns a channel that receives events from the bundled discovery tool
funcWatchListBoards(pm*packagemanager.PackageManager) (<-chan*discovery.Event, error) {
t, err:=getBuiltinSerialDiscoveryTool(pm)
iferr!=nil {
returnnil, err
}
if!t.IsInstalled() {
returnnil, fmt.Errorf("missing serial-discovery tool")
}
disc, err:=discovery.New("serial-discovery", t.InstallDir.Join(t.Tool.Name).String())
iferr!=nil {
returnnil, err
}
iferr=disc.Start(); err!=nil {
returnnil, fmt.Errorf("starting discovery: %v", err)
}
iferr=disc.StartSync(); err!=nil {
returnnil, fmt.Errorf("starting sync: %v", err)
}
returndisc.EventChannel(10), nil
}
funcgetBuiltinSerialDiscoveryTool(pm*packagemanager.PackageManager) (*cores.ToolRelease, error) {
builtinPackage:=pm.Packages.GetOrCreatePackage("builtin")
serialDiscoveryTool:=builtinPackage.GetOrCreateTool("serial-discovery")
serialDiscoveryToolRel:=serialDiscoveryTool.GetOrCreateRelease(serialDiscoveryVersion)
serialDiscoveryToolRel.Flavors=serialDiscoveryFlavors
returnpm.Package("builtin").Tool("serial-discovery").Release(serialDiscoveryVersion).Get()
}