/
wmc.go
270 lines (236 loc) · 5.91 KB
/
wmc.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
package main
import (
"bufio"
"fmt"
"log"
"os"
"path/filepath"
"strings"
"time"
)
import (
"github.com/spf13/cobra"
"github.com/spf13/viper"
serial "github.com/tarm/serial"
)
var Version = ""
var Verbose bool
// Functions
func cleanString(dirty string) string {
clean := strings.Replace(dirty, "\"", "\\\"", -1)
return clean
}
func readSerial(p serial.Port) string {
if Verbose {
log.Printf("Reading from serial '%v'\n", p)
}
buf := make([]byte, 1024)
r, _ := p.Read(buf)
return strings.TrimSpace(string(buf[:r]))
}
func writeSerial(p serial.Port, s string) (err error) {
if Verbose {
log.Printf("Writing '%s' to serial '%v'\n", p, s)
}
_, err = p.Write([]byte(s + "\n"))
if err != nil {
log.Fatal(err)
}
return err
}
func sendCommand(p serial.Port, cmd string) (out string, err error) {
if Verbose {
log.Printf("Sending command '%s' to '%v'\n", p, cmd)
}
var res string
err = writeSerial(p, cmd)
// drop the first line, it's always an
// echo of the command being sent
_ = readSerial(p)
// read until we get a '>'
for !strings.Contains(res, ">") {
res = readSerial(p)
if !strings.Contains(res, ">") {
out = out + res + "\n"
}
}
return out, err
}
func uploadFile(p serial.Port, filename string) (err error) {
if Verbose {
log.Printf("Uploading file: %s\n", filename)
}
fmt.Printf("Uploading file: %s\n", filename)
_, destFileName := filepath.Split(filename)
if Verbose {
log.Printf("Destination name: %s\n", destFileName)
}
fmt.Printf("Destination name: %s\n", destFileName)
file, err := os.Open(filename)
if err != nil {
log.Fatal(err)
}
defer file.Close()
fmt.Print("Writing file\n")
_, _ = sendCommand(p, fmt.Sprintf("file.open('%s', 'w+')", destFileName))
scanner := bufio.NewScanner(file)
for scanner.Scan() {
cmd := fmt.Sprintf("file.writeline(\"%s\")", cleanString(scanner.Text()))
//fmt.Printf(cmd)
_, _ = sendCommand(p, cmd)
fmt.Printf(".")
}
fmt.Printf("\n")
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
_, _ = sendCommand(p, "file.close()")
return err
}
func flushSerial(p serial.Port) {
if Verbose {
log.Printf("Flushing port '%v'\n", p)
}
res := "flush"
// read until we get no response
for res != "" {
res = readSerial(p)
}
}
func openSerial() *serial.Port {
//c := &serial.Config{Name: "/dev/ttyUSB0", Baud: 115200}
if Verbose {
log.Printf("Opening serial port '%s', Baud: 115200, Timeout: 1 second}", viper.GetString("serial"))
}
c := &serial.Config{Name: viper.GetString("serial"), Baud: 115200, ReadTimeout: time.Second * 1}
s, err := serial.OpenPort(c)
if err != nil {
log.Fatal(err)
}
flushSerial(*s)
return s
}
// Commands
var cmdWmcVersion = &cobra.Command{
Use: "ver",
Short: "Get the current version",
Run: func(cmd *cobra.Command, args []string) {
s := openSerial()
res, err := sendCommand(*s, "print(mcu.ver())")
if err != nil {
log.Fatal(err)
}
fmt.Printf("WifiMCU Firmware: %s", res)
},
}
var cmdList = &cobra.Command{
Use: "ls",
Short: "List all files on device",
Run: func(cmd *cobra.Command, args []string) {
s := openSerial()
res, err := sendCommand(*s, "for k,_ in pairs(file.list()) do print(k) end")
//res, err := sendCommand(*s, "file.slist()")
if err != nil {
log.Fatal(err)
}
fmt.Printf("File list...\n")
fmt.Printf("%s\n", res)
},
}
var cmdPut = &cobra.Command{
Use: "put [filename]",
Short: "Send a file to the device",
Run: func(cmd *cobra.Command, args []string) {
p := openSerial()
err := uploadFile(*p, args[0])
if err != nil {
log.Fatal(err)
}
},
}
var cmdRm = &cobra.Command{
Use: "rm",
Short: "Remove a file",
Run: func(cmd *cobra.Command, args []string) {
fmt.Printf("removing %s\n", args[0])
s := openSerial()
cmdString := fmt.Sprintf("file.remove(\"%s\")", args[0])
_, err := sendCommand(*s, cmdString)
if err != nil {
log.Fatal(err)
}
},
}
var cmdConfig = &cobra.Command{
Use: "config",
Short: "Display current config",
Run: func(cmd *cobra.Command, args []string) {
fmt.Printf("WMC_SERIAL: %s\n", viper.Get("serial"))
fmt.Printf("WMC_DEBUG: %s\n", viper.Get("debug"))
},
}
var cmdRead = &cobra.Command{
Use: "read",
Short: "read a file",
Run: func(cmd *cobra.Command, args []string) {
s := openSerial()
_, _ = sendCommand(*s, "file.open(\""+args[0]+"\")")
_, _ = sendCommand(*s, "data=file.read()")
_, _ = sendCommand(*s, "file.close()")
res, _ := sendCommand(*s, "print(data)")
fmt.Printf("%s\n", res)
},
}
var cmdCommand = &cobra.Command{
Use: "cmd",
Short: "Run an arbitrary command, needs to be in double-quotes",
Run: func(cmd *cobra.Command, args []string) {
s := openSerial()
res, err := sendCommand(*s, args[0])
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s\n", res)
},
}
var cmdRun = &cobra.Command{
Use: "run",
Short: "Run the specified script on the WifiMCU",
Run: func(cmd *cobra.Command, args []string) {
s := openSerial()
res, err := sendCommand(*s, "dofile('"+args[0]+"')")
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s\n", res)
},
}
var cmdReboot = &cobra.Command{
Use: "reboot",
Short: "Reboot the connected module",
Run: func(cmd *cobra.Command, args []string) {
s := openSerial()
res, err := sendCommand(*s, "mcu.reboot()")
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s\n", res)
},
}
func main() {
// setup our settings
viper.SetEnvPrefix("wmc")
viper.BindEnv("serial")
viper.SetDefault("serial", "/dev/ttyUSB0")
var rootCmd = &cobra.Command{
Use: "wmc",
Short: "wmc is a cross-platform command line utility for the WifiMCU Platform",
Long: `wmc: ` + Version + `
wmc is a cross-platform command line utility for the WifiMCU Platform
GPL Licensed source code, downloads and issue tracker at
https://github.com/zpeters/wmc`,
}
rootCmd.AddCommand(cmdWmcVersion, cmdList, cmdPut, cmdRm, cmdConfig, cmdCommand, cmdRead, cmdRun, cmdReboot)
rootCmd.PersistentFlags().BoolVarP(&Verbose, "verbose", "v", false, "verbose output")
rootCmd.Execute()
}