Skip to content

Commit 724dd42

Browse files
committed
Convert to flags
1 parent 56e7938 commit 724dd42

File tree

1 file changed

+145
-55
lines changed

1 file changed

+145
-55
lines changed

main.go

Lines changed: 145 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -2,78 +2,76 @@ package main
22

33
import (
44
"bufio"
5+
"flag"
56
"fmt"
7+
"github.com/kardianos/osext"
68
"github.com/mattn/go-shellwords"
79
"io"
810
"io/ioutil"
911
"os"
1012
"os/exec"
1113
"path/filepath"
1214
"runtime"
15+
"strconv"
1316
"strings"
1417
"time"
1518
)
1619

17-
var verbose bool
20+
var (
21+
verbose = flag.Bool("v", false, "Show verbose logging")
22+
quiet = flag.Bool("q", true, "Show quiet logging")
23+
force = flag.Bool("f", false, "Force firmware update")
24+
dfu_path = flag.String("dfu", "", "Location of dfu-util binaries")
25+
bin_file_name = flag.String("bin", "", "Location of sketch binary")
26+
com_port = flag.String("port", "", "Upload serial port")
27+
ble_compliance_string = flag.String("ble_fw_str", "", "BLE FW ID string")
28+
ble_compliance_offset = flag.Int("ble_fw_pos", 0, "BLE FW ID offset")
29+
rtos_compliance_string = flag.String("rots_fw_str", "", "RTOS FW ID string")
30+
rtos_compliance_offset = flag.Int("rtos_fw_pos", 0, "RTOS FW ID offset")
31+
)
32+
33+
const Version = "1.8.0"
34+
35+
const dfu_flags = "-d,8087:0ABA"
36+
const rtos_firmware = "quark.bin"
37+
const ble_firmware = "ble_core.bin"
1838

1939
func PrintlnVerbose(a ...interface{}) {
20-
if verbose {
40+
if *verbose {
2141
fmt.Println(a...)
2242
}
2343
}
2444

25-
func main_load(args []string) {
45+
func main_load() {
2646

27-
// ARG 1: Path to binaries
28-
// ARG 2: BIN File to download
29-
// ARG 3: TTY port to use.
30-
// ARG 4: quiet/verbose
31-
// path may contain \ need to change all to /
32-
33-
if len(args) < 4 {
34-
fmt.Println("Not enough arguments")
47+
if *dfu_path == "" {
48+
fmt.Println("Need to specify dfu-util location")
3549
os.Exit(1)
3650
}
3751

38-
bin_path := args[0]
39-
dfu := bin_path + "/dfu-util"
40-
dfu = filepath.ToSlash(dfu)
41-
dfu_flags := "-d,8087:0ABA"
42-
43-
bin_file_name := args[1]
44-
45-
com_port := args[2]
46-
verbosity := args[3]
47-
48-
ble_compliance_string := ""
49-
ble_compliance_offset := ""
50-
if len(args) >= 5 {
51-
// Called by post 1.0.6 platform.txt
52-
ble_compliance_string = args[4]
53-
ble_compliance_offset = args[5]
52+
if *bin_file_name == "" && *force == false {
53+
fmt.Println("Need to specify a binary location or force FW update")
54+
os.Exit(1)
5455
}
5556

56-
if verbosity == "quiet" {
57-
verbose = false
58-
} else {
59-
verbose = true
60-
}
57+
dfu := *dfu_path + "/dfu-util"
58+
dfu = filepath.ToSlash(dfu)
6159

62-
PrintlnVerbose("Args to shell:", args)
63-
PrintlnVerbose("Serial Port: " + com_port)
64-
PrintlnVerbose("BIN FILE " + bin_file_name)
60+
PrintlnVerbose("Serial Port: " + *com_port)
61+
PrintlnVerbose("BIN FILE " + *bin_file_name)
6562

6663
counter := 0
6764
board_found := false
6865

6966
if runtime.GOOS == "darwin" {
7067
library_path := os.Getenv("DYLD_LIBRARY_PATH")
71-
if !strings.Contains(library_path, bin_path) {
72-
os.Setenv("DYLD_LIBRARY_PATH", bin_path+":"+library_path)
68+
if !strings.Contains(library_path, *dfu_path) {
69+
os.Setenv("DYLD_LIBRARY_PATH", *dfu_path+":"+library_path)
7370
}
7471
}
7572

7673
dfu_search_command := []string{dfu, dfu_flags, "-l"}
74+
var err error
7775

7876
for counter < 100 && board_found == false {
7977
if counter%10 == 0 {
@@ -98,11 +96,14 @@ func main_load(args []string) {
9896
}
9997

10098
if board_found == false {
101-
fmt.Println("ERROR: Timed out waiting for Arduino 101 on " + com_port)
99+
fmt.Println("ERROR: Timed out waiting for Arduino 101 on " + *com_port)
102100
os.Exit(1)
103101
}
104102

105-
if ble_compliance_string != "" {
103+
needUpdateRTOS := false
104+
needUpdateBLE := false
105+
106+
if *ble_compliance_string != "" {
106107

107108
// obtain a temporary filename
108109
tmpfile, _ := ioutil.TempFile(os.TempDir(), "dfu")
@@ -121,7 +122,7 @@ func main_load(args []string) {
121122
os.Remove(tmpfile.Name())
122123

123124
// download a piece of BLE firmware
124-
dfu_ble_dump_command := []string{dfu, dfu_flags, "-U", tmpfile.Name(), "--alt", "8", "-K", ble_compliance_offset}
125+
dfu_ble_dump_command := []string{dfu, dfu_flags, "-U", tmpfile.Name(), "--alt", "8", "-K", strconv.Itoa(*ble_compliance_offset)}
125126

126127
err, _, _ = launchCommandAndWaitForOutput(dfu_ble_dump_command, "", false)
127128
if err != nil {
@@ -130,29 +131,115 @@ func main_load(args []string) {
130131
}
131132

132133
// check for BLE library compliance
133-
PrintlnVerbose("Verifying BLE version:", ble_compliance_string)
134-
found := searchBLEversionInDFU(tmpfile.Name(), ble_compliance_string)
134+
PrintlnVerbose("Verifying BLE version:", *ble_compliance_string)
135+
found := searchVersionInDFU(tmpfile.Name(), *ble_compliance_string)
136+
137+
// remove the temporary file
138+
os.Remove(tmpfile.Name())
139+
140+
if !found {
141+
needUpdateBLE = true
142+
} else {
143+
PrintlnVerbose("BLE version: verified")
144+
}
145+
}
146+
147+
if *rtos_compliance_string != "" {
148+
149+
// obtain a temporary filename
150+
tmpfile, _ := ioutil.TempFile(os.TempDir(), "dfu")
151+
tmpfile.Close()
152+
os.Remove(tmpfile.Name())
153+
154+
// reset DFU interface counter
155+
dfu_reset_command := []string{dfu, dfu_flags, "-U", tmpfile.Name(), "--alt", "2", "-K", "1"}
156+
157+
err, _, _ := launchCommandAndWaitForOutput(dfu_reset_command, "", false)
158+
if err != nil {
159+
fmt.Println(err)
160+
os.Exit(1)
161+
}
162+
163+
os.Remove(tmpfile.Name())
164+
165+
// download a piece of RTOS firmware
166+
dfu_rtos_dump_command := []string{dfu, dfu_flags, "-U", tmpfile.Name(), "--alt", "2", "-K", strconv.Itoa(*rtos_compliance_offset)}
167+
168+
err, _, _ = launchCommandAndWaitForOutput(dfu_rtos_dump_command, "", false)
169+
if err != nil {
170+
fmt.Println(err)
171+
os.Exit(1)
172+
}
173+
174+
// check for BLE library compliance
175+
PrintlnVerbose("Verifying RTOS version:", *rtos_compliance_string)
176+
found := searchVersionInDFU(tmpfile.Name(), *rtos_compliance_string)
135177

136178
// remove the temporary file
137179
os.Remove(tmpfile.Name())
138180

139181
if !found {
140-
fmt.Println("!! BLE firmware version is not in sync with CurieBLE library !!")
141-
fmt.Println("* Set Programmer to \"Arduino/Genuino 101 Firmware Updater\"")
142-
fmt.Println("* Update it using \"Burn Bootloader\" menu")
182+
needUpdateRTOS = true
183+
} else {
184+
PrintlnVerbose("RTOS version: verified")
185+
}
186+
}
187+
188+
executablePath, _ := osext.ExecutableFolder()
189+
firmwarePath := executablePath + "/firmwares/"
190+
191+
// Save verbose flag
192+
verbose_user := *verbose
193+
194+
if needUpdateBLE || *force == true {
195+
196+
*verbose = true
197+
// flash current BLE firmware to partition 8
198+
dfu_ble_flash_command := []string{dfu, dfu_flags, "-D", firmwarePath + ble_firmware, "--alt", "8"}
199+
200+
fmt.Println("ATTENTION: BLE firmware is being flashed")
201+
fmt.Println("DO NOT DISCONNECT THE BOARD")
202+
203+
err, _, _ = launchCommandAndWaitForOutput(dfu_ble_flash_command, "", true)
204+
if err != nil {
205+
fmt.Println(err)
206+
os.Exit(1)
207+
}
208+
}
209+
210+
if needUpdateRTOS || *force == true {
211+
212+
*verbose = true
213+
// flash current RTOS firmware to partition 2
214+
dfu_rtos_flash_command := []string{dfu, dfu_flags, "-D", firmwarePath + rtos_firmware, "--alt", "2"}
215+
216+
fmt.Println("ATTENTION: RTOS firmware is being flashed")
217+
fmt.Println("DO NOT DISCONNECT THE BOARD")
218+
219+
err, _, _ = launchCommandAndWaitForOutput(dfu_rtos_flash_command, "", true)
220+
if err != nil {
221+
fmt.Println(err)
143222
os.Exit(1)
144223
}
145-
PrintlnVerbose("BLE version: verified")
146224
}
147225

148-
dfu_download := []string{dfu, dfu_flags, "-D", bin_file_name, "-v", "--alt", "7", "-R"}
149-
err, _, _ := launchCommandAndWaitForOutput(dfu_download, "", true)
226+
// Restore verbose flag
227+
*verbose = verbose_user
228+
229+
// Finally flash the sketch
230+
231+
if *bin_file_name == "" {
232+
os.Exit(0)
233+
}
234+
235+
dfu_download := []string{dfu, dfu_flags, "-D", *bin_file_name, "-v", "--alt", "7", "-R"}
236+
err, _, _ = launchCommandAndWaitForOutput(dfu_download, "", true)
150237

151238
if err == nil {
152239
fmt.Println("SUCCESS: Sketch will execute in about 5 seconds.")
153240
os.Exit(0)
154241
} else {
155-
fmt.Println("ERROR: Upload failed on " + com_port)
242+
fmt.Println("ERROR: Upload failed on " + *com_port)
156243
os.Exit(1)
157244
}
158245
}
@@ -164,7 +251,7 @@ func main_debug(args []string) {
164251
os.Exit(1)
165252
}
166253

167-
verbose = true
254+
*verbose = true
168255

169256
type Command struct {
170257
command string
@@ -205,24 +292,27 @@ func main_debug(args []string) {
205292
}
206293

207294
func main() {
208-
name := os.Args[0]
209-
args := os.Args[1:]
295+
name := filepath.Base(os.Args[0])
296+
297+
flag.Parse()
298+
299+
PrintlnVerbose(name + " " + Version + " - compiled with " + runtime.Version())
210300

211301
if strings.Contains(name, "load") {
212302
fmt.Println("Starting download script...")
213-
main_load(args)
303+
main_load()
214304
}
215305

216306
if strings.Contains(name, "debug") {
217307
fmt.Println("Starting debug script...")
218-
main_debug(args)
308+
main_debug(os.Args[1:])
219309
}
220310

221311
fmt.Println("Wrong executable name")
222312
os.Exit(1)
223313
}
224314

225-
func searchBLEversionInDFU(file string, string_to_search string) bool {
315+
func searchVersionInDFU(file string, string_to_search string) bool {
226316
read, _ := ioutil.ReadFile(file)
227317
return strings.Contains(string(read), string_to_search)
228318
}

0 commit comments

Comments
 (0)