-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
159 lines (143 loc) · 6.54 KB
/
main.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
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"time"
)
const version string = "1.0"
const defaultConnectBaudrate uint = 115200
const defaultTransferBaudrate uint = 921600
type CliCommand struct {
Name string
Description string
FlagSet *flag.FlagSet
Callback func(*log.Logger) error
}
var (
versionFlagSet = flag.NewFlagSet("version", flag.ExitOnError)
versionJson = versionFlagSet.Bool("json", false, "Display version info in JSON format")
help = flag.Bool("help", false, "Show a help page")
infoFlagSet = flag.NewFlagSet("info", flag.ExitOnError)
infoPort = infoFlagSet.String("serial.port", "", "Serial port device file")
infoConnectBaudrate = infoFlagSet.Uint("serial.baudrate.connect", defaultConnectBaudrate, "Serial signalling rate during connect phase")
infoTransferBaudrate = infoFlagSet.Uint("serial.baudrate.transfer", defaultTransferBaudrate, "Serial signalling rate during data transfer")
infoTimeout = infoFlagSet.Duration("serial.connect.timeout", 500*time.Millisecond, "Timeout to wait for chip response upon connecting")
infoRetries = infoFlagSet.Uint("serial.connect.retries", 5, "How often to retry connecting")
infoJson = infoFlagSet.Bool("json", false, "Display chip info in JSON format")
flashReadFlagSet = flag.NewFlagSet("readFlash", flag.ExitOnError)
flashReadPort = flashReadFlagSet.String("serial.port", "", "Serial port device file")
flashReadConnectBaudrate = flashReadFlagSet.Uint("serial.baudrate.connect", defaultConnectBaudrate, "Serial signalling rate during connect phase")
flashReadTransferBaudrate = flashReadFlagSet.Uint("serial.baudrate.transfer", defaultTransferBaudrate, "Serial signalling rate during data transfer")
flashReadTimeout = flashReadFlagSet.Duration("serial.connect.timeout", 500*time.Millisecond, "Timeout to wait for chip response upon connecting")
flashReadRetries = flashReadFlagSet.Uint("serial.connect.retries", 5, "How often to retry connecting")
flashReadOffset = flashReadFlagSet.Uint("flash.offset", 0, "Offset")
flashReadSize = flashReadFlagSet.Uint("flash.size", 0, "Bytes to read")
flashReadFile = flashReadFlagSet.String("flash.file", "", "File to read flash contents into")
flashReadPartitionName = flashReadFlagSet.String("flash.partition.name", "", "Partition to read")
flashWriteFlagSet = flag.NewFlagSet("writeFlash", flag.ExitOnError)
flashWritePort = flashWriteFlagSet.String("serial.port", "", "Serial port device file")
flashWriteConnectBaudrate = flashWriteFlagSet.Uint("serial.baudrate.connect", defaultConnectBaudrate, "Serial signalling rate during connect phase")
flashWriteTransferBaudrate = flashWriteFlagSet.Uint("serial.baudrate.transfer", defaultTransferBaudrate, "Serial signalling rate during data transfer")
flashWriteTimeout = flashWriteFlagSet.Duration("serial.connect.timeout", 500*time.Millisecond, "Timeout to wait for chip response upon connecting")
flashWriteRetries = flashWriteFlagSet.Uint("serial.connect.retries", 5, "How often to retry connecting")
flashWriteOffset = flashWriteFlagSet.Uint("flash.offset", 0, "Offset")
flashWriteFile = flashWriteFlagSet.String("flash.file", "", "File with data to flash")
flashWritePartitionName = flashWriteFlagSet.String("flash.partition.name", "", "Partition to write")
flashWriteCompress = flashWriteFlagSet.Bool("flash.compress", true, "Use compression for transfer")
cliCommands = []*CliCommand{
&CliCommand{
Name: "version",
Description: "Show version info and exit",
FlagSet: versionFlagSet,
Callback: func(logger *log.Logger) error {
versionFlagSet.Parse(os.Args[2:])
return versionCommand(*versionJson)
},
},
&CliCommand{
Name: "info",
Description: "Retrieve various information from chip",
FlagSet: infoFlagSet,
Callback: func(logger *log.Logger) error {
infoFlagSet.Parse(os.Args[2:])
esp32, err := connectEsp32(*infoPort, uint32(*infoConnectBaudrate), uint32(*infoTransferBaudrate), *infoRetries, logger)
if err != nil {
return err
}
return infoCommand(*infoJson, esp32)
},
},
&CliCommand{
Name: "flashRead",
Description: "Read flash contents",
FlagSet: flashReadFlagSet,
Callback: func(logger *log.Logger) error {
flashReadFlagSet.Parse(os.Args[2:])
esp32, err := connectEsp32(*flashReadPort, uint32(*flashReadConnectBaudrate), uint32(*flashReadTransferBaudrate), *flashReadRetries, logger)
if err != nil {
return err
}
bytes, err := esp32.ReadFlash(uint32(*flashReadOffset), uint32(*flashReadSize))
if err != nil {
return err
}
os.Stdout.Write(bytes)
return nil
},
},
&CliCommand{
Name: "flashWrite",
Description: "Write flash contents",
FlagSet: flashWriteFlagSet,
Callback: func(logger *log.Logger) error {
flashWriteFlagSet.Parse(os.Args[2:])
contents, err := ioutil.ReadFile(*flashWriteFile)
if err != nil {
return err
}
esp32, err := connectEsp32(*flashWritePort, uint32(*flashWriteConnectBaudrate), uint32(*flashWriteTransferBaudrate), *flashWriteRetries, logger)
if err != nil {
return err
}
err = esp32.WriteFlash(uint32(*flashWriteOffset), contents, *flashWriteCompress)
if err != nil {
panic(err)
}
logger.Print("Done")
return nil
},
},
}
port = flag.String("port", "", "Serial port device")
baudrate = flag.Uint("baudrate", 115200, "Serial port baud rate used when flashing/reading")
connectTimeout = flag.Duration("timeout.connect", 500*time.Millisecond, "Timeout to wait for chip response upon connecting")
responseTimeout = flag.Duration("timeout.response", 10*time.Millisecond, "Timeout to wait for chip to respond")
server = flag.NewFlagSet("server", flag.ExitOnError)
listenAddress = server.String("listen-address", ":8080", "Port and andress to listen on")
)
func main() {
flag.Parse()
if len(os.Args) < 2 || *help {
printHelp()
}
logger := log.New(os.Stderr, bold("[LOG]: "), log.Ltime|log.Lshortfile)
for _, command := range cliCommands {
if command.Name == os.Args[1] {
err := command.Callback(logger)
if err != nil {
logger.Printf("Failed to run %s: %s", command.Name, err.Error())
}
os.Exit(1)
}
}
}
func printHelp() {
fmt.Println("Please choose one of the following subcommands")
for _, command := range cliCommands {
fmt.Printf(" * \033[1m%s\033[0m: %s\n", command.Name, command.Description)
}
os.Exit(1)
}