Skip to content

add get-version command #116

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 15 commits into from
Sep 30, 2021
Merged
Show file tree
Hide file tree
Changes from 14 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
19 changes: 19 additions & 0 deletions cli/arguments/arguments.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package arguments

import (
"github.com/spf13/cobra"
)

// Flags contains various common flags.
// This is useful so all flags used by commands that need
// this information are consistent with each other.
type Flags struct {
Address string
Fqbn string
}

// AddToCommand adds the flags used to set address and fqbn to the specified Command
func (f *Flags) AddToCommand(cmd *cobra.Command) {
cmd.Flags().StringVarP(&f.Fqbn, "fqbn", "b", "", "Fully Qualified Board Name, e.g.: arduino:samd:mkr1000, arduino:mbed_nano:nanorp2040connect")
cmd.Flags().StringVarP(&f.Address, "address", "a", "", "Upload port, e.g.: COM10, /dev/ttyACM0")
}
110 changes: 22 additions & 88 deletions cli/certificates/flash.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,31 +23,27 @@ import (
"bytes"
"fmt"
"os"
"path/filepath"
"strings"
"time"

"github.com/arduino/arduino-cli/arduino/serialutils"
"github.com/arduino/arduino-cli/cli/errorcodes"
"github.com/arduino/arduino-cli/cli/feedback"
"github.com/arduino/arduino-fwuploader/cli/arguments"
"github.com/arduino/arduino-fwuploader/cli/common"
"github.com/arduino/arduino-fwuploader/flasher"
"github.com/arduino/arduino-fwuploader/indexes"
"github.com/arduino/arduino-fwuploader/indexes/download"
programmer "github.com/arduino/arduino-fwuploader/programmers"
"github.com/arduino/go-paths-helper"
"github.com/arduino/go-properties-orderedmap"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

var (
fqbn string
address string
commonFlags arguments.Flags
certificateURLs []string
certificatePaths []string
)

// NewCommand created a new `version` command
// NewFlashCommand creates a new `flash` command
func NewFlashCommand() *cobra.Command {
command := &cobra.Command{
Use: "flash",
Expand All @@ -58,121 +54,59 @@ func NewFlashCommand() *cobra.Command {
" " + os.Args[0] + " certificates flash -b arduino:samd:mkr1000 -a COM10 -u arduino.cc:443 -u google.cc:443\n" +
" " + os.Args[0] + " certificates flash -b arduino:samd:mkr1000 -a COM10 -f /home/me/VeriSign.cer -f /home/me/Digicert.cer\n",
Args: cobra.NoArgs,
Run: run,
Run: runFlash,
}

command.Flags().StringVarP(&fqbn, "fqbn", "b", "", "Fully Qualified Board Name, e.g.: arduino:samd:mkr1000, arduino:mbed_nano:nanorp2040connect")
command.Flags().StringVarP(&address, "address", "a", "", "Upload port, e.g.: COM10, /dev/ttyACM0")
commonFlags.AddToCommand(command)
command.Flags().StringSliceVarP(&certificateURLs, "url", "u", []string{}, "List of urls to download root certificates, e.g.: arduino.cc:443")
command.Flags().StringSliceVarP(&certificatePaths, "file", "f", []string{}, "List of paths to certificate file, e.g.: /home/me/Digicert.cer")
return command
}

func run(cmd *cobra.Command, args []string) {
packageIndex, err := indexes.GetPackageIndex()
if err != nil {
feedback.Errorf("Can't load package index: %s", err)
os.Exit(errorcodes.ErrGeneric)
}
func runFlash(cmd *cobra.Command, args []string) {

firmwareIndex, err := indexes.GetFirmwareIndex()
if err != nil {
feedback.Errorf("Can't load firmware index: %s", err)
os.Exit(errorcodes.ErrGeneric)
}

if fqbn == "" {
feedback.Errorf("Error during certificates flashing: missing board fqbn")
os.Exit(errorcodes.ErrBadArgument)
}

if address == "" {
feedback.Errorf("Error during certificates flashing: missing board address")
os.Exit(errorcodes.ErrBadArgument)
}
packageIndex, firmwareIndex := common.InitIndexes()
common.CheckFlags(commonFlags.Fqbn, commonFlags.Address)
board := common.GetBoard(firmwareIndex, commonFlags.Fqbn)
uploadToolDir := common.GetUploadToolDir(packageIndex, board)

if len(certificateURLs) == 0 && len(certificatePaths) == 0 {
feedback.Errorf("Error during certificates flashing: no certificates provided")
os.Exit(errorcodes.ErrBadArgument)
}

board := firmwareIndex.GetBoard(fqbn)
if board == nil {
feedback.Errorf("Can't find board with %s fqbn", fqbn)
os.Exit(errorcodes.ErrBadArgument)
}

toolRelease := indexes.GetToolRelease(packageIndex, board.Uploader)
if toolRelease == nil {
feedback.Errorf("Error getting upload tool %s for board %s", board.Uploader, board.Fqbn)
os.Exit(errorcodes.ErrGeneric)
}
uploadToolDir, err := download.DownloadTool(toolRelease)
if err != nil {
feedback.Errorf("Error downloading tool %s: %s", board.Uploader, err)
os.Exit(errorcodes.ErrGeneric)
}

loaderSketchPath, err := download.DownloadLoaderSketch(board.LoaderSketch)
loaderSketchPath, err := download.DownloadSketch(board.LoaderSketch)
if err != nil {
feedback.Errorf("Error downloading loader sketch from %s: %s", board.LoaderSketch.URL, err)
os.Exit(errorcodes.ErrGeneric)
}
logrus.Debugf("loader sketch downloaded in %s", loaderSketchPath.String())

loaderSketch := strings.ReplaceAll(loaderSketchPath.String(), loaderSketchPath.Ext(), "")

// Check if board needs a 1200bps touch for upload
bootloaderPort := address
if board.UploadTouch {
logrus.Info("Putting board into bootloader mode")
newUploadPort, err := serialutils.Reset(address, board.UploadWait, nil)
if err != nil {
feedback.Errorf("Error during certificates flashing: missing board address")
os.Exit(errorcodes.ErrGeneric)
}
if newUploadPort != "" {
logrus.Infof("Found port to upload Loader: %s", newUploadPort)
bootloaderPort = newUploadPort
}
}

uploaderCommand := board.GetUploaderCommand()
uploaderCommand = strings.ReplaceAll(uploaderCommand, "{tool_dir}", filepath.FromSlash(uploadToolDir.String()))
uploaderCommand = strings.ReplaceAll(uploaderCommand, "{serial.port.file}", bootloaderPort)
uploaderCommand = strings.ReplaceAll(uploaderCommand, "{loader.sketch}", loaderSketch)

commandLine, err := properties.SplitQuotedString(uploaderCommand, "\"", false)
programmerOut, programmerErr, err := common.FlashSketch(board, loaderSketch, uploadToolDir, commonFlags.Address)
if err != nil {
feedback.Errorf(`Error splitting command line "%s": %s`, uploaderCommand, err)
os.Exit(errorcodes.ErrGeneric)
}

// Flash loader Sketch
programmerOut := new(bytes.Buffer)
programmerErr := new(bytes.Buffer)
if feedback.GetFormat() == feedback.JSON {
err = programmer.Flash(commandLine, programmerOut, programmerErr)
} else {
err = programmer.Flash(commandLine, os.Stdout, os.Stderr)
}
if err != nil {
feedback.Errorf("Error during certificates flashing: %s", err)
feedback.Error(err)
os.Exit(errorcodes.ErrGeneric)
}

// Wait a bit after flashing the loader sketch for the board to become
// available again.
logrus.Debug("sleeping for 3 sec")
time.Sleep(3 * time.Second)

// Get flasher depending on which module to use
var f flasher.Flasher
moduleName := board.Module

// This matches the baudrate used in the FirmwareUpdater.ino sketch
// https://github.com/arduino-libraries/WiFiNINA/blob/master/examples/Tools/FirmwareUpdater/FirmwareUpdater.ino
const baudRate = 1000000
switch moduleName {
case "NINA":
// we use address and not bootloaderPort because the board should not be in bootloader mode
f, err = flasher.NewNinaFlasher(address)
f, err = flasher.NewNinaFlasher(commonFlags.Address, baudRate, 30)
case "WINC1500":
f, err = flasher.NewWincFlasher(address)
f, err = flasher.NewWincFlasher(commonFlags.Address, baudRate, 30)
default:
err = fmt.Errorf("unknown module: %s", moduleName)
}
Expand Down
134 changes: 134 additions & 0 deletions cli/common/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
package common

import (
"bytes"
"fmt"
"os"
"path/filepath"
"strings"

"github.com/arduino/arduino-cli/arduino/cores/packageindex"
"github.com/arduino/arduino-cli/arduino/serialutils"
"github.com/arduino/arduino-cli/cli/errorcodes"
"github.com/arduino/arduino-cli/cli/feedback"
"github.com/arduino/arduino-fwuploader/indexes"
"github.com/arduino/arduino-fwuploader/indexes/download"
"github.com/arduino/arduino-fwuploader/indexes/firmwareindex"
programmer "github.com/arduino/arduino-fwuploader/programmers"
"github.com/arduino/go-paths-helper"
"github.com/arduino/go-properties-orderedmap"
"github.com/sirupsen/logrus"
)

// InitIndexes does exactly what the name implies
func InitIndexes() (*packageindex.Index, *firmwareindex.Index) {
packageIndex, err := indexes.GetPackageIndex()
if err != nil {
feedback.Errorf("Can't load package index: %s", err)
os.Exit(errorcodes.ErrGeneric)
}

firmwareIndex, err := indexes.GetFirmwareIndex()
if err != nil {
feedback.Errorf("Can't load firmware index: %s", err)
os.Exit(errorcodes.ErrGeneric)
}
return packageIndex, firmwareIndex
}

// CheckFlags runs a basic check, errors if the flags are not defined
func CheckFlags(fqbn, address string) {
if fqbn == "" {
feedback.Errorf("Error during firmware flashing: missing board fqbn")
os.Exit(errorcodes.ErrBadArgument)
}

if address == "" {
feedback.Errorf("Error during firmware flashing: missing board address")
os.Exit(errorcodes.ErrBadArgument)
}
logrus.Debugf("fqbn: %s, address: %s", fqbn, address)
}

// GetBoard is an helper function useful to get the IndexBoard,
// the struct that contains all the infos to make all the operations possible
func GetBoard(firmwareIndex *firmwareindex.Index, fqbn string) *firmwareindex.IndexBoard {
board := firmwareIndex.GetBoard(fqbn)
if board == nil {
feedback.Errorf("Can't find board with %s fqbn", fqbn)
os.Exit(errorcodes.ErrBadArgument)
}
logrus.Debugf("got board: %s", board.Fqbn)
return board
}

// GetUploadToolDir is an helper function that downloads the correct tool to flash a board,
// it returns the path of the downloaded tool
func GetUploadToolDir(packageIndex *packageindex.Index, board *firmwareindex.IndexBoard) *paths.Path {
toolRelease := indexes.GetToolRelease(packageIndex, board.Uploader)
if toolRelease == nil {
feedback.Errorf("Error getting upload tool %s for board %s", board.Uploader, board.Fqbn)
os.Exit(errorcodes.ErrGeneric)
}
uploadToolDir, err := download.DownloadTool(toolRelease)
if err != nil {
feedback.Errorf("Error downloading tool %s: %s", board.Uploader, err)
os.Exit(errorcodes.ErrGeneric)
}
logrus.Debugf("upload tool downloaded in %s", uploadToolDir.String())
return uploadToolDir
}

// FlashSketch is the business logic that handles the flashing procedure,
// it returns using a buffer the stdout and the stderr of the programmer
func FlashSketch(board *firmwareindex.IndexBoard, sketch string, uploadToolDir *paths.Path, address string) (programmerOut, programmerErr *bytes.Buffer, err error) {
bootloaderPort, err := GetNewAddress(board, address)
if err != nil {
return nil, nil, err
}

uploaderCommand := board.GetUploaderCommand()
uploaderCommand = strings.ReplaceAll(uploaderCommand, "{tool_dir}", filepath.FromSlash(uploadToolDir.String()))
uploaderCommand = strings.ReplaceAll(uploaderCommand, "{serial.port.file}", bootloaderPort)
uploaderCommand = strings.ReplaceAll(uploaderCommand, "{loader.sketch}", sketch) // we leave that name here because it's only a template,

logrus.Debugf("uploading with command: %s", uploaderCommand)
commandLine, err := properties.SplitQuotedString(uploaderCommand, "\"", false)
if err != nil {
feedback.Errorf(`Error splitting command line "%s": %s`, uploaderCommand, err)
os.Exit(errorcodes.ErrGeneric)
}

// Flash the actual sketch
programmerOut = new(bytes.Buffer)
programmerErr = new(bytes.Buffer)
if feedback.GetFormat() == feedback.JSON {
err = programmer.Flash(commandLine, programmerOut, programmerErr)
} else {
err = programmer.Flash(commandLine, os.Stdout, os.Stderr)
}
if err != nil {
return nil, nil, fmt.Errorf("error during sketch flashing: %s", err)
}
return programmerOut, programmerErr, err
}

// GetNewAddress is a function used to reset a board and put it in bootloader mode
// it could happen that the board is assigned to a different serial port, after the reset,
// this fuction handles also this possibility
func GetNewAddress(board *firmwareindex.IndexBoard, oldAddress string) (string, error) {
// Check if board needs a 1200bps touch for upload
bootloaderPort := oldAddress
if board.UploadTouch {
logrus.Info("Putting board into bootloader mode")
newUploadPort, err := serialutils.Reset(oldAddress, board.UploadWait, nil)
if err != nil {
return "", fmt.Errorf("error during sketch flashing: missing board address. %s", err)
}
if newUploadPort != "" {
logrus.Infof("Found port to upload: %s", newUploadPort)
bootloaderPort = newUploadPort
}
}
return bootloaderPort, nil
}
1 change: 1 addition & 0 deletions cli/firmware/firmware.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,6 @@ func NewCommand() *cobra.Command {

firmwareCmd.AddCommand(NewFlashCommand())
firmwareCmd.AddCommand(newListCommand())
firmwareCmd.AddCommand(NewGetVersionCommand())
return firmwareCmd
}
Loading