Skip to content

Commit 59fbc28

Browse files
authored
Refactor code structure to be similar to other Tooling Team projects (#32)
* Add cobra dependency * Refactored code structure similarly to Arduino CLI * Update license header to all code files * Update README.md
1 parent 1a1d8e1 commit 59fbc28

File tree

20 files changed

+470
-243
lines changed

20 files changed

+470
-243
lines changed

Diff for: README.md

+24-24
Original file line numberDiff line numberDiff line change
@@ -13,47 +13,47 @@ https://github.com/arduino/FirmwareUploader/releases/latest
1313
Extract the zip file and run (for example, NINA -> WiFi1010)
1414

1515
```
16-
./FirmwareUploader -flasher firmwares/NINA/FirmwareUpdater.mkrwifi1010.ino.bin -firmware firmwares/NINA/1.2.1/NINA_W102.bin -port /dev/ttyACM0 -address arduino.cc:443 -restore_binary /tmp/arduino_build_619137/WiFiSSLClient.ino.bin -programmer {runtime.tools.bossac}/bossac
16+
./FirmwareUploader --flasher firmwares/NINA/FirmwareUpdater.mkrwifi1010.ino.bin --firmware firmwares/NINA/1.2.1/NINA_W102.bin --port /dev/ttyACM0 --address arduino.cc:443 --restore_binary /tmp/arduino_build_619137/WiFiSSLClient.ino.bin --programmer {runtime.tools.bossac}/bossac
1717
```
1818

1919
To flash a MKR1000:
2020

2121
```
22-
./FirmwareUploader -flasher firmwares/WINC1500/FirmwareUpdater.mkr1000.ino.bin -firmware firmwares/WINC1500/19.5.4/m2m_aio_3a0.bin -port /dev/ttyACM0 -address arduino.cc:443 -restore_binary /tmp/arduino_build_619137/WiFiSSLClient.ino.bin -programmer {runtime.tools.bossac}/bossac
22+
./FirmwareUploader --flasher firmwares/WINC1500/FirmwareUpdater.mkr1000.ino.bin --firmware firmwares/WINC1500/19.5.4/m2m_aio_3a0.bin --port /dev/ttyACM0 --address arduino.cc:443 --restore_binary /tmp/arduino_build_619137/WiFiSSLClient.ino.bin --programmer {runtime.tools.bossac}/bossac
2323
```
2424

2525
To update a MKRNB1500:
2626

2727
```
28-
./FirmwareUploader -flasher firmwares/SARA/SerialSARAPassthrough.ino.bin -firmware firmwares/SARA/5.6A2.00-to-5.6A2.01.pkg -port /dev/ttyACM0 -restore_binary firmwares/SARA/SerialSARAPassthrough.ino.bin -programmer {runtime.tools.bossac}/bossac
28+
./FirmwareUploader --flasher firmwares/SARA/SerialSARAPassthrough.ino.bin --firmware firmwares/SARA/5.6A2.00-to-5.6A2.01.pkg --port /dev/ttyACM0 --restore_binary firmwares/SARA/SerialSARAPassthrough.ino.bin --programmer {runtime.tools.bossac}/bossac
2929
```
3030

3131
### Command line options
3232

3333
The full list of command line options can be obtained with the `-h` option: `./FirmwareUploader -h`
3434

3535
```
36-
Usage of ./FirmwareUploader:
37-
-address value
38-
address (host:port) to fetch and flash root certificate for, multiple values allowed
39-
-certs string
40-
root certificate directory
41-
-firmware string
42-
firmware file to flash
43-
-flasher string
44-
firmware upload binary (precompiled for the right target)
45-
-get_available_for string
46-
Ask for available firmwares matching a given board
47-
-model string
48-
module model (winc, nina or sara)
49-
-port string
50-
serial port to use for flashing
51-
-programmer string
52-
path of programmer in use (avrdude/bossac)
53-
-read
54-
read all firmware and output to stdout
55-
-restore_binary string
56-
firmware upload binary (precompiled for the right target)
36+
FirmwareUploader (FirmwareUploader).
37+
38+
Usage:
39+
FirmwareUploader [flags]
40+
41+
Examples:
42+
./FirmwareUploader <command> [flags...]
43+
44+
Flags:
45+
--address strings address (host:port) to fetch and flash root certificate for, multiple values allowed
46+
--certs string root certificate directory
47+
--firmware string firmware file to flash
48+
--flasher string firmware upload binary (precompiled for the right target)
49+
--get_available_for string Ask for available firmwares matching a given board
50+
-h, --help help for FirmwareUploader
51+
--model string module model (winc, nina or sara)
52+
--port string serial port to use for flashing
53+
--programmer string path of programmer in use (avrdude/bossac)
54+
--read read all firmware and output to stdout
55+
--restore_binary string binary to restore after the firmware upload (precompiled for the right target)
56+
--retries int Number of retries in case of upload failure (default 9)
5757
```
5858

5959
## How to build the tools from source file

Diff for: cli/cli.go

+119
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/*
2+
FirmwareUploader
3+
Copyright (c) 2021 Arduino LLC. All right reserved.
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
package cli
21+
22+
import (
23+
"encoding/json"
24+
"fmt"
25+
"log"
26+
"os"
27+
"strings"
28+
"time"
29+
30+
"github.com/arduino/FirmwareUploader/modules/nina"
31+
"github.com/arduino/FirmwareUploader/modules/sara"
32+
"github.com/arduino/FirmwareUploader/modules/winc"
33+
"github.com/arduino/FirmwareUploader/utils"
34+
"github.com/arduino/FirmwareUploader/utils/context"
35+
"github.com/arduino/go-paths-helper"
36+
"github.com/spf13/cobra"
37+
)
38+
39+
var ctx = &context.Context{}
40+
41+
func NewCommand() *cobra.Command {
42+
// FirmwareUploader is the root command
43+
firmwareUploaderCli := &cobra.Command{
44+
Use: "FirmwareUploader",
45+
Short: "FirmwareUploader.",
46+
Long: "FirmwareUploader (FirmwareUploader).",
47+
Example: " " + os.Args[0] + " <command> [flags...]",
48+
Args: cobra.NoArgs,
49+
Run: run,
50+
}
51+
52+
firmwareUploaderCli.Flags().StringVar(&ctx.PortName, "port", "", "serial port to use for flashing")
53+
firmwareUploaderCli.Flags().StringVar(&ctx.RootCertDir, "certs", "", "root certificate directory")
54+
firmwareUploaderCli.Flags().StringSliceVar(&ctx.Addresses, "address", []string{}, "address (host:port) to fetch and flash root certificate for, multiple values allowed")
55+
firmwareUploaderCli.Flags().StringVar(&ctx.FirmwareFile, "firmware", "", "firmware file to flash")
56+
firmwareUploaderCli.Flags().BoolVar(&ctx.ReadAll, "read", false, "read all firmware and output to stdout")
57+
firmwareUploaderCli.Flags().StringVar(&ctx.FWUploaderBinary, "flasher", "", "firmware upload binary (precompiled for the right target)")
58+
firmwareUploaderCli.Flags().StringVar(&ctx.BinaryToRestore, "restore_binary", "", "binary to restore after the firmware upload (precompiled for the right target)")
59+
firmwareUploaderCli.Flags().StringVar(&ctx.ProgrammerPath, "programmer", "", "path of programmer in use (avrdude/bossac)")
60+
firmwareUploaderCli.Flags().StringVar(&ctx.Model, "model", "", "module model (winc, nina or sara)")
61+
firmwareUploaderCli.Flags().StringVar(&ctx.BoardName, "get_available_for", "", "Ask for available firmwares matching a given board")
62+
firmwareUploaderCli.Flags().IntVar(&ctx.Retries, "retries", 9, "Number of retries in case of upload failure")
63+
64+
return firmwareUploaderCli
65+
}
66+
67+
func run(cmd *cobra.Command, args []string) {
68+
if ctx.BoardName != "" {
69+
el, _ := json.Marshal(utils.GetCompatibleWith(ctx.BoardName, ""))
70+
fmt.Println(string(el))
71+
os.Exit(0)
72+
}
73+
74+
if ctx.PortName == "" {
75+
log.Fatal("Please specify a serial port")
76+
}
77+
78+
if ctx.BinaryToRestore != "" {
79+
// sanity check for BinaryToRestore
80+
f := paths.New(ctx.BinaryToRestore)
81+
info, err := f.Stat()
82+
if err != nil {
83+
log.Fatalf("Error opening restore_binary: %s", err)
84+
}
85+
if info.IsDir() {
86+
log.Fatalf("Error opening restore_binary: is a directory...")
87+
}
88+
if info.Size() == 0 {
89+
log.Println("WARNING: restore_binary is empty! Will not restore binary after upload.")
90+
ctx.BinaryToRestore = ""
91+
}
92+
}
93+
94+
retry := 0
95+
for {
96+
var err error
97+
if ctx.Model == "nina" || strings.Contains(ctx.FirmwareFile, "NINA") || strings.Contains(ctx.FWUploaderBinary, "NINA") {
98+
err = nina.Run(ctx)
99+
} else if ctx.Model == "winc" || strings.Contains(ctx.FirmwareFile, "WINC") || strings.Contains(ctx.FWUploaderBinary, "WINC") {
100+
err = winc.Run(ctx)
101+
} else {
102+
err = sara.Run(ctx)
103+
}
104+
if err == nil {
105+
log.Println("Operation completed: success! :-)")
106+
break
107+
}
108+
log.Println("Error: " + err.Error())
109+
110+
if retry >= ctx.Retries {
111+
log.Fatal("Operation failed. :-(")
112+
}
113+
114+
retry++
115+
log.Println("Waiting 1 second before retrying...")
116+
time.Sleep(time.Second)
117+
log.Printf("Retrying upload (%d of %d)", retry, ctx.Retries)
118+
}
119+
}

Diff for: go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ replace go.bug.st/serial => github.com/cmaglie/go-serial v0.0.0-20200923162623-b
88
require (
99
github.com/arduino/arduino-cli v0.0.0-20210422154105-5aa424818026
1010
github.com/arduino/go-paths-helper v1.4.0
11-
github.com/imjasonmiller/godice v0.1.2 // indirect
1211
github.com/pkg/errors v0.9.1
12+
github.com/spf13/cobra v1.1.3
1313
github.com/stretchr/testify v1.6.1
1414
go.bug.st/serial v1.1.2
1515
)

0 commit comments

Comments
 (0)