Skip to content

Commit

Permalink
Refactor code structure to be similar to other Tooling Team projects (#…
Browse files Browse the repository at this point in the history
…32)

* Add cobra dependency

* Refactored code structure similarly to Arduino CLI

* Update license header to all code files

* Update README.md
  • Loading branch information
silvanocerza committed May 19, 2021
1 parent 1a1d8e1 commit 59fbc28
Show file tree
Hide file tree
Showing 20 changed files with 470 additions and 243 deletions.
48 changes: 24 additions & 24 deletions README.md
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
@@ -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
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
)

0 comments on commit 59fbc28

Please sign in to comment.