-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwrapped.go
More file actions
198 lines (174 loc) · 4.87 KB
/
Copy pathwrapped.go
File metadata and controls
198 lines (174 loc) · 4.87 KB
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
package folie
// This file contains !commands.
import (
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"sort"
"strconv"
"strings"
)
// The functions in this file are only called in the context of interactive console input and they
// all print directly to stdout. This is OK in this context, but may have to be changed in the
// future if we want to support a remote interactive console. Not clear there is a need for that.
// specialCommand recognises and handles certain commands in a different way.
func (sw *Switchboard) specialCommand(line string) bool {
cmd := strings.SplitN(line, " ", 2)
switch cmd[0] {
case "!":
fmt.Println("[enter '!h' for help]")
case "!c", "!cd":
fmt.Println(line)
wrappedCd(cmd)
case "!h", "!help":
fmt.Println(line)
showHelp()
case "!l", "!ls":
fmt.Println(line)
wrappedLs(cmd)
case "!r", "!reset":
fmt.Println(line)
sw.wrappedReset()
case "!s", "!send":
fmt.Println(line)
sw.wrappedSend(cmd)
case "!u", "!upload":
fmt.Println(line)
sw.wrappedUpload(cmd)
default:
return false
}
return true
}
const helpMsg = `
Special commands, these can also be abbreviated as "!r", etc:
!reset reset the board, same as ctrl-c
!send <file> send text file to the serial port, expand "include" lines
!upload show the list of built-in firmware images
!upload <n> upload built-in image <n> using STM32 boot protocol
!upload <file> upload specified firmware image (bin or hex format)
!upload <url> fetch firmware image from given URL, then upload it
Utility commands:
!cd <dir> change directory (or list current one if not specified)
!ls <dir> list contents of the specified (or current) directory
!help this message
To quit, hit ctrl-d. For command history, use up-/down-arrow.
`
func showHelp() {
fmt.Print(helpMsg[1:])
}
func wrappedCd(argv []string) {
if len(argv) > 1 {
if err := os.Chdir(argv[1]); err != nil {
fmt.Println(err)
return
}
}
if dir, err := os.Getwd(); err == nil {
fmt.Println(dir)
} else {
fmt.Println(err)
}
}
func wrappedLs(argv []string) {
dir := "."
if len(argv) > 1 {
dir = argv[1]
}
var names []string
files, _ := ioutil.ReadDir(dir)
for _, f := range files {
n := f.Name()
if !strings.HasPrefix(n, ".") {
if f.IsDir() {
n = n + "/"
}
names = append(names, n)
}
}
fmt.Println(strings.Join(names, " "))
}
func (sw *Switchboard) wrappedReset() {
if ok := sw.MicroOutput.Reset(false); !ok {
// Couldn't perform the reset, probably error on serial/telnet.
fmt.Println("[use CTRL-D to exit]")
}
}
func (sw *Switchboard) wrappedSend(argv []string) {
if len(argv) == 1 {
fmt.Printf("Usage: %s <filename>\n", argv[0])
return
}
if !includeFile(sw.MicroOutput, sw.MicroInput, argv[1], 0) { // FIXME: need tx channel
fmt.Println("Send failed.")
}
}
var crcTable = []uint16{
0x0000, 0xCC01, 0xD801, 0x1400, 0xF001, 0x3C00, 0x2800, 0xE401,
0xA001, 0x6C00, 0x7800, 0xB401, 0x5000, 0x9C01, 0x8801, 0x4400,
}
func crc16(data []byte) uint16 {
var crc uint16 = 0xFFFF
for _, b := range data {
crc = (crc >> 4) ^ crcTable[crc&0x0F] ^ crcTable[b&0x0F]
crc = (crc >> 4) ^ crcTable[crc&0x0F] ^ crcTable[(b>>4)&0x0F]
}
return crc
}
func (sw *Switchboard) wrappedUpload(argv []string) {
names := sw.AssetNames
sort.Strings(names)
if len(argv) == 1 {
fmt.Println("These firmware images are built-in:")
for i, name := range names {
data, _ := sw.Asset(name)
fmt.Printf("%3d: %-16s %5db crc:%04X\n",
i+1, name, len(data), crc16(data))
}
fmt.Println("Use '!u <n>' to upload a specific one.")
return
}
// try built-in images first, indicated by entering a valid number
var data []byte
if n, err := strconv.Atoi(argv[1]); err == nil && 0 < n && n <= len(names) {
data, _ = sw.Asset(names[n-1])
} else if u, err := url.Parse(argv[1]); err == nil && u.Scheme != "" {
fmt.Print("Fetching... ")
res, err := http.Get(argv[1])
if err == nil {
data, err = ioutil.ReadAll(res.Body)
res.Body.Close()
}
if err != nil {
fmt.Println(err)
return
}
if res.StatusCode < 200 || res.StatusCode >= 300 {
fmt.Printf("%s: %s\n", res.Status, string(data))
}
fmt.Printf("got it, crc:%04X\n", crc16(data))
} else { // else try opening the arg as file
f, err := os.Open(argv[1])
if err == nil {
data, err = ioutil.ReadAll(f)
f.Close()
}
if err != nil {
fmt.Println(err)
return
}
}
if fl, ok := sw.MicroOutput.(MicroFlasher); ok {
// The MicroOutput implements a special flashing method. Call it!
// This is primarily the case for a remote SSH connection: it sends the bytes
// to the remote end to play the flashing game there.
fl.Flash(data)
} else {
// We get to perform the flashing algorithm here...
defer sw.MicroOutput.Reset(false) // reset with BOOT0 low to restart normally
u := &Uploader{Tx: sw.MicroOutput, Rx: sw.MicroInput, Stdout: &consoleWriter{sw}}
u.Upload(data)
}
}