-
Notifications
You must be signed in to change notification settings - Fork 0
/
flasher.go
318 lines (261 loc) · 6.63 KB
/
flasher.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
package main
import (
"bufio"
"flag"
"fmt"
"log"
"os"
"path/filepath"
"regexp"
"strings"
"time"
"go.bug.st/serial.v1"
tooling "github.com/conservify/tooling"
)
type configuration struct {
Board string
Port string
Binary string
Tools string
SkipTouch bool
Touch bool
Verbose bool
Verify bool
UploadQuietly bool
Tail bool
TailAppend string
TailInactivity int
TailReopen bool
TailTriggerFail string
TailTriggerPass string
TailTriggerStop string
FlashOffset int
}
func openSerial(config *configuration) (serial.Port, error) {
mode := &serial.Mode{
BaudRate: 115200,
}
port, err := serial.Open(config.Port, mode)
if err != nil {
return nil, err
}
return port, nil
}
type EchoStatus struct {
Data bool
Exited bool
}
func openFile(config *configuration) *os.File {
if config.TailAppend == "" {
return nil
}
log.Printf("Logging to %s...", config.TailAppend)
file, err := os.OpenFile(config.TailAppend, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0666)
if err != nil {
log.Fatalf("Unable to open %s: %v", config.TailAppend, err)
}
return file
}
type StopTriggers struct {
Stop *regexp.Regexp
Pass *regexp.Regexp
Fail *regexp.Regexp
}
func NewStopTriggers(config *configuration) (st *StopTriggers) {
var stop *regexp.Regexp
var pass *regexp.Regexp
var fail *regexp.Regexp
if config.TailTriggerStop != "" {
stop = regexp.MustCompile(config.TailTriggerStop)
log.Printf("Stopping on '%s'", config.TailTriggerStop)
}
if config.TailTriggerPass != "" {
pass = regexp.MustCompile(config.TailTriggerPass)
log.Printf("Passing on '%s'", config.TailTriggerPass)
}
if config.TailTriggerFail != "" {
fail = regexp.MustCompile(config.TailTriggerFail)
log.Printf("Failing on '%s'", config.TailTriggerFail)
}
return &StopTriggers{
Stop: stop,
Pass: pass,
Fail: fail,
}
}
func (st *StopTriggers) Apply(line string) (exitCode int) {
if st.Pass != nil {
if st.Pass.MatchString(line) {
return 0
}
}
if st.Fail != nil {
if st.Fail.MatchString(line) {
return 2
}
}
if st.Stop != nil {
if st.Stop.MatchString(line) {
return 0
}
}
return -1
}
func echoSerial(config *configuration, port serial.Port, c chan *EchoStatus) {
defer port.Close()
var w *bufio.Writer
file := openFile(config)
if file != nil {
w = bufio.NewWriter(file)
defer file.Close()
defer w.Flush()
}
buff := make([]byte, 256)
triggers := NewStopTriggers(config)
for {
n, err := port.Read(buff)
if err != nil {
log.Printf("Error reading: %v", err)
break
}
if n == 0 {
break
}
c <- &EchoStatus{
Data: true,
}
// This is probably controversial:
sanitized := strings.Replace(string(buff[:n]), "\r", "", -1)
if w != nil {
fmt.Printf("%v", sanitized)
w.WriteString(sanitized)
w.Flush()
} else {
fmt.Printf("%v", sanitized)
}
exitCode := triggers.Apply(sanitized)
if exitCode >= 0 {
os.Exit(exitCode)
}
}
c <- &EchoStatus{
Exited: true,
}
}
func main() {
config := configuration{}
flag.StringVar(&config.Tools, "tools", "", "path to the tools directory")
flag.StringVar(&config.Board, "board", "adafruit_feather_m0", "board to upload to")
flag.BoolVar(&config.SkipTouch, "skip-touch", false, "skip the touch")
flag.BoolVar(&config.Touch, "touch", false, "touch")
flag.BoolVar(&config.Verbose, "verbose", false, "verbose")
flag.BoolVar(&config.Verify, "verify", false, "verify")
flag.StringVar(&config.Port, "port", "", "port to upload to")
flag.StringVar(&config.Binary, "binary", "", "path to the binary (required)")
flag.IntVar(&config.FlashOffset, "flash-offset", 0, "flash offset to flash program")
flag.BoolVar(&config.UploadQuietly, "upload-quietly", false, "hide upload progress")
flag.BoolVar(&config.Tail, "tail", false, "show serial")
flag.StringVar(&config.TailAppend, "append", "", "append tail to file")
flag.IntVar(&config.TailInactivity, "tail-inactivity", 0, "inactive time until quitting tail")
flag.BoolVar(&config.TailReopen, "tail-reopen", true, "tail again after inactivity or file loss")
flag.StringVar(&config.TailTriggerStop, "tail-trigger-stop", "", "tail trigger stop")
flag.StringVar(&config.TailTriggerPass, "tail-trigger-pass", "", "tail trigger pass")
flag.StringVar(&config.TailTriggerFail, "tail-trigger-fail", "", "tail trigger fail")
flag.Parse()
pd := tooling.NewPortDiscoveror()
if config.Binary != "" {
if config.FlashOffset == 0 {
flag.Usage()
os.Exit(2)
}
if _, err := os.Stat(config.Binary); os.IsNotExist(err) {
log.Fatalf("Error: No such binary '%s'", config.Binary)
}
ae := tooling.NewArduinoEnvironment()
err := ae.Locate(config.Tools)
if err != nil {
log.Fatalf("Error: %v", err)
}
portPath, err := filepath.EvalSymlinks(config.Port)
if err != nil {
log.Fatalf("Unable to evaluate symlinks %s (%v)", config.Port, err)
}
tooling.Upload(&tooling.UploadOptions{
Arduino: ae,
SkipTouch: config.SkipTouch,
Board: config.Board,
Port: portPath,
Binary: config.Binary,
FlashOffset: config.FlashOffset,
Verbose: config.Verbose,
Verify: config.Verify,
Quietly: config.UploadQuietly,
})
}
if config.Port != "" && config.Touch {
tooling.Touch(config.Port)
}
if config.Tail {
if config.Touch {
if config.Port != "" {
portPath, err := filepath.EvalSymlinks(config.Port)
if err != nil {
log.Fatalf("Unable to evaluate symlinks %s (%v)", config.Port, err)
}
tooling.Touch(portPath)
}
}
if config.Touch || config.Binary != "" { // Did we upload?
time.Sleep(500 * time.Millisecond)
}
for {
if _, err := os.Stat(config.Port); os.IsNotExist(err) {
log.Printf("Port '%s' disappeared, scanning...", config.Port)
config.Port = pd.Discover()
if config.Port == "" {
log.Fatalf("Error: Unable to find port to tail.")
}
}
ch := make(chan *EchoStatus)
port, err := openSerial(&config)
if err != nil {
if config.TailReopen {
time.Sleep(500 * time.Millisecond)
continue
} else {
log.Fatalf("Error: Unable to open port: %v", err)
}
}
go echoSerial(&config, port, ch)
go func() {
for {
time.Sleep(1 * time.Second)
ch <- &EchoStatus{}
}
}()
previous := time.Now()
for {
status := <-ch
if status.Data {
previous = time.Now()
}
if status.Exited {
port.Close()
break
}
if config.TailInactivity > 0 {
ago := time.Duration(-config.TailInactivity) * time.Second
to := time.Now().Add(ago)
if previous.Before(to) {
port.Close()
log.Printf("Tail inactive!")
break
}
}
}
if !config.TailReopen {
break
}
}
}
}