-
-
Notifications
You must be signed in to change notification settings - Fork 150
/
Copy pathupload.go
200 lines (166 loc) · 5.45 KB
/
upload.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
// Copyright 2022 Arduino SA
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package upload
import (
"bufio"
"os/exec"
"path/filepath"
"regexp"
"runtime"
"strings"
"github.com/arduino/arduino-cli/arduino/serialutils"
"github.com/arduino/arduino-create-agent/utilities"
shellwords "github.com/mattn/go-shellwords"
"github.com/pkg/errors"
"go.bug.st/serial/enumerator"
)
// Busy tells wether the programmer is doing something
var Busy = false
// Extra contains some options used during the upload
type Extra struct {
Use1200bpsTouch bool `json:"use_1200bps_touch"`
WaitForUploadPort bool `json:"wait_for_upload_port"`
Network bool `json:"network"`
}
// PartiallyResolve replaces some symbols in the commandline with the appropriate values
// it can return an error when looking a variable in the Locater
func PartiallyResolve(board, file, platformPath, commandline string, extra Extra, t Locater) (string, error) {
commandline = strings.Replace(commandline, "{build.path}", filepath.ToSlash(filepath.Dir(file)), -1)
commandline = strings.Replace(commandline, "{build.project_name}", strings.TrimSuffix(filepath.Base(file), filepath.Ext(filepath.Base(file))), -1)
commandline = strings.Replace(commandline, "{runtime.platform.path}", filepath.ToSlash(platformPath), -1)
commandline = strings.Replace(commandline, "{fqbn}", board, -1)
// search for runtime variables and replace with values from Locater
var runtimeRe = regexp.MustCompile("\\{(.*?)\\}")
runtimeVars := runtimeRe.FindAllString(commandline, -1)
for _, element := range runtimeVars {
location, err := t.GetLocation(element)
if err != nil {
return "", errors.Wrapf(err, "get location of %s", element)
}
if location != "" {
commandline = strings.Replace(commandline, element, location, 1)
}
}
return commandline, nil
}
func fixupPort(port, commandline string) string {
commandline = strings.Replace(commandline, "{serial.port}", port, -1)
commandline = strings.Replace(commandline, "{serial.port.file}", filepath.Base(port), -1)
ports, err := enumerator.GetDetailedPortsList()
if err == nil {
for _, p := range ports {
if p.Name == port {
commandline = strings.Replace(commandline, "{serial.port.iserial}", p.SerialNumber, -1)
}
}
}
return commandline
}
// Serial performs a serial upload
func Serial(port, commandline string, extra Extra, l Logger) error {
Busy = true
defer func() { Busy = false }()
// some boards needs to be resetted
if extra.Use1200bpsTouch {
var err error
port, err = reset(port, extra.WaitForUploadPort, l)
if err != nil {
return errors.Wrapf(err, "Reset before upload")
}
}
commandline = fixupPort(port, commandline)
z, err := shellwords.Parse(commandline)
if err != nil {
return errors.Wrapf(err, "Parse commandline")
}
return program(z[0], z[1:], l)
}
var cmds = map[*exec.Cmd]bool{}
// Kill stops any upload process as soon as possible
func Kill() {
for cmd := range cmds {
if cmd.Process.Pid > 0 {
cmd.Process.Kill()
}
}
}
// reset wraps arduino-cli's serialutils
// it opens the port at 1200bps. It returns the new port name (which could change
// sometimes) and an error (usually because the port listing failed)
func reset(port string, wait bool, l Logger) (string, error) {
info(l, "Restarting in bootloader mode")
newPort, err := serialutils.Reset(port, wait, nil, false) // TODO use callbacks to print as the cli does
if err != nil {
info(l, err)
return "", err
}
if newPort != "" {
port = newPort
}
return port, nil
}
// program spawns the given binary with the given args, logging the sdtout and stderr
// through the Logger
func program(binary string, args []string, l Logger) error {
// remove quotes form binary command and args
binary = strings.Replace(binary, "\"", "", -1)
for i := range args {
args[i] = strings.Replace(args[i], "\"", "", -1)
}
// find extension
extension := ""
if runtime.GOOS == "windows" {
extension = ".exe"
}
cmd := exec.Command(binary, args...)
// Add the command to the map of running commands
cmds[cmd] = true
defer func() {
delete(cmds, cmd)
}()
utilities.TellCommandNotToSpawnShell(cmd)
stdout, err := cmd.StdoutPipe()
if err != nil {
return errors.Wrapf(err, "Retrieve output")
}
stderr, err := cmd.StderrPipe()
if err != nil {
return errors.Wrapf(err, "Retrieve output")
}
info(l, "Flashing with command:"+binary+extension+" "+strings.Join(args, " "))
err = cmd.Start()
if err != nil {
return errors.Wrapf(err, "Start command")
}
stdoutCopy := bufio.NewScanner(stdout)
stderrCopy := bufio.NewScanner(stderr)
stdoutCopy.Split(bufio.ScanLines)
stderrCopy.Split(bufio.ScanLines)
go func() {
for stdoutCopy.Scan() {
info(l, stdoutCopy.Text())
}
}()
go func() {
for stderrCopy.Scan() {
info(l, stderrCopy.Text())
}
}()
err = cmd.Wait()
if err != nil {
return errors.Wrapf(err, "Executing command")
}
return nil
}