-
-
Notifications
You must be signed in to change notification settings - Fork 17
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
Changes from 11 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
e541576
refactor DownloadLoaderSketch to be more generic
umbynos 5a98dbc
make OpenSerial public
umbynos a733f34
now the program is able to parse the new `module_firmware_index.json`
umbynos b501a51
refactor business logic to prepare for ´get-version´ command
umbynos 5cc7c5f
add baudrate to OpenSerial (it makes more sense), usefull for new cmd
umbynos 91f8c09
add readTimeout parameter to OpenSerial
umbynos e5ae5db
add some debug printing
umbynos 67a376e
finally add the implementation of the new ´ǵet-version´ command
umbynos f7d48e2
add/fix some comments
umbynos 1961ca0
add docs
umbynos 726eb85
handle the communication problem between a board and a module
umbynos dfbcf15
Apply suggestions from code review
umbynos 79e3d27
print version string only when using `--format text` (default behavior)
umbynos 40f1ad4
remove the `\r` from the version output
umbynos 00140b2
remove line ending before parsing
umbynos File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 out and the err 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 | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.