Skip to content

Refactor code structure to be similar to other Tooling Team projects #32

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 24 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,47 +13,47 @@ https://github.com/arduino/FirmwareUploader/releases/latest
Extract the zip file and run (for example, NINA -> WiFi1010)

```
./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
./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
```

To flash a MKR1000:

```
./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
./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
```

To update a MKRNB1500:

```
./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
./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
```

### Command line options

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

```
Usage of ./FirmwareUploader:
-address value
address (host:port) to fetch and flash root certificate for, multiple values allowed
-certs string
root certificate directory
-firmware string
firmware file to flash
-flasher string
firmware upload binary (precompiled for the right target)
-get_available_for string
Ask for available firmwares matching a given board
-model string
module model (winc, nina or sara)
-port string
serial port to use for flashing
-programmer string
path of programmer in use (avrdude/bossac)
-read
read all firmware and output to stdout
-restore_binary string
firmware upload binary (precompiled for the right target)
FirmwareUploader (FirmwareUploader).

Usage:
FirmwareUploader [flags]

Examples:
./FirmwareUploader <command> [flags...]

Flags:
--address strings address (host:port) to fetch and flash root certificate for, multiple values allowed
--certs string root certificate directory
--firmware string firmware file to flash
--flasher string firmware upload binary (precompiled for the right target)
--get_available_for string Ask for available firmwares matching a given board
-h, --help help for FirmwareUploader
--model string module model (winc, nina or sara)
--port string serial port to use for flashing
--programmer string path of programmer in use (avrdude/bossac)
--read read all firmware and output to stdout
--restore_binary string binary to restore after the firmware upload (precompiled for the right target)
--retries int Number of retries in case of upload failure (default 9)
```

## How to build the tools from source file
Expand Down
119 changes: 119 additions & 0 deletions cli/cli.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
FirmwareUploader
Copyright (c) 2021 Arduino LLC. All right reserved.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

package cli

import (
"encoding/json"
"fmt"
"log"
"os"
"strings"
"time"

"github.com/arduino/FirmwareUploader/modules/nina"
"github.com/arduino/FirmwareUploader/modules/sara"
"github.com/arduino/FirmwareUploader/modules/winc"
"github.com/arduino/FirmwareUploader/utils"
"github.com/arduino/FirmwareUploader/utils/context"
"github.com/arduino/go-paths-helper"
"github.com/spf13/cobra"
)

var ctx = &context.Context{}

func NewCommand() *cobra.Command {
// FirmwareUploader is the root command
firmwareUploaderCli := &cobra.Command{
Use: "FirmwareUploader",
Short: "FirmwareUploader.",
Long: "FirmwareUploader (FirmwareUploader).",
Example: " " + os.Args[0] + " <command> [flags...]",
Args: cobra.NoArgs,
Run: run,
}

firmwareUploaderCli.Flags().StringVar(&ctx.PortName, "port", "", "serial port to use for flashing")
firmwareUploaderCli.Flags().StringVar(&ctx.RootCertDir, "certs", "", "root certificate directory")
firmwareUploaderCli.Flags().StringSliceVar(&ctx.Addresses, "address", []string{}, "address (host:port) to fetch and flash root certificate for, multiple values allowed")
firmwareUploaderCli.Flags().StringVar(&ctx.FirmwareFile, "firmware", "", "firmware file to flash")
firmwareUploaderCli.Flags().BoolVar(&ctx.ReadAll, "read", false, "read all firmware and output to stdout")
firmwareUploaderCli.Flags().StringVar(&ctx.FWUploaderBinary, "flasher", "", "firmware upload binary (precompiled for the right target)")
firmwareUploaderCli.Flags().StringVar(&ctx.BinaryToRestore, "restore_binary", "", "binary to restore after the firmware upload (precompiled for the right target)")
firmwareUploaderCli.Flags().StringVar(&ctx.ProgrammerPath, "programmer", "", "path of programmer in use (avrdude/bossac)")
firmwareUploaderCli.Flags().StringVar(&ctx.Model, "model", "", "module model (winc, nina or sara)")
firmwareUploaderCli.Flags().StringVar(&ctx.BoardName, "get_available_for", "", "Ask for available firmwares matching a given board")
firmwareUploaderCli.Flags().IntVar(&ctx.Retries, "retries", 9, "Number of retries in case of upload failure")

return firmwareUploaderCli
}

func run(cmd *cobra.Command, args []string) {
if ctx.BoardName != "" {
el, _ := json.Marshal(utils.GetCompatibleWith(ctx.BoardName, ""))
fmt.Println(string(el))
os.Exit(0)
}

if ctx.PortName == "" {
log.Fatal("Please specify a serial port")
}

if ctx.BinaryToRestore != "" {
// sanity check for BinaryToRestore
f := paths.New(ctx.BinaryToRestore)
info, err := f.Stat()
if err != nil {
log.Fatalf("Error opening restore_binary: %s", err)
}
if info.IsDir() {
log.Fatalf("Error opening restore_binary: is a directory...")
}
if info.Size() == 0 {
log.Println("WARNING: restore_binary is empty! Will not restore binary after upload.")
ctx.BinaryToRestore = ""
}
}

retry := 0
for {
var err error
if ctx.Model == "nina" || strings.Contains(ctx.FirmwareFile, "NINA") || strings.Contains(ctx.FWUploaderBinary, "NINA") {
err = nina.Run(ctx)
} else if ctx.Model == "winc" || strings.Contains(ctx.FirmwareFile, "WINC") || strings.Contains(ctx.FWUploaderBinary, "WINC") {
err = winc.Run(ctx)
} else {
err = sara.Run(ctx)
}
if err == nil {
log.Println("Operation completed: success! :-)")
break
}
log.Println("Error: " + err.Error())

if retry >= ctx.Retries {
log.Fatal("Operation failed. :-(")
}

retry++
log.Println("Waiting 1 second before retrying...")
time.Sleep(time.Second)
log.Printf("Retrying upload (%d of %d)", retry, ctx.Retries)
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ replace go.bug.st/serial => github.com/cmaglie/go-serial v0.0.0-20200923162623-b
require (
github.com/arduino/arduino-cli v0.0.0-20210422154105-5aa424818026
github.com/arduino/go-paths-helper v1.4.0
github.com/imjasonmiller/godice v0.1.2 // indirect
github.com/pkg/errors v0.9.1
github.com/spf13/cobra v1.1.3
github.com/stretchr/testify v1.6.1
go.bug.st/serial v1.1.2
)
Loading