Skip to content

Commit 67a376e

Browse files
committed
finally add the implementation of the new ´ǵet-version´ command
1 parent e5ae5db commit 67a376e

File tree

3 files changed

+135
-1
lines changed

3 files changed

+135
-1
lines changed

cli/firmware/firmware.go

+1
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,6 @@ func NewCommand() *cobra.Command {
3535

3636
firmwareCmd.AddCommand(NewFlashCommand())
3737
firmwareCmd.AddCommand(newListCommand())
38+
firmwareCmd.AddCommand(NewGetVersionCommand())
3839
return firmwareCmd
3940
}

cli/firmware/getversion.go

+132
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
/*
2+
arduino-fwuploader
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 firmware
21+
22+
import (
23+
"fmt"
24+
"log"
25+
"os"
26+
"strings"
27+
"time"
28+
29+
"github.com/arduino/arduino-cli/cli/errorcodes"
30+
"github.com/arduino/arduino-cli/cli/feedback"
31+
"github.com/arduino/arduino-fwuploader/cli/common"
32+
"github.com/arduino/arduino-fwuploader/flasher"
33+
"github.com/arduino/arduino-fwuploader/indexes/download"
34+
"github.com/arduino/arduino-fwuploader/indexes/firmwareindex"
35+
"github.com/sirupsen/logrus"
36+
"github.com/spf13/cobra"
37+
semver "go.bug.st/relaxed-semver"
38+
)
39+
40+
// NewCommand created a new `version` command
41+
func NewGetVersionCommand() *cobra.Command {
42+
43+
command := &cobra.Command{
44+
Use: "get-version",
45+
Short: "Gets the version of the firmware the board is using.",
46+
Long: "Flashes a sketch to a board to obtain the firmware version used by the board",
47+
Example: "" +
48+
" " + os.Args[0] + " firmware get-version --fqbn arduino:samd:mkr1000 --address COM10\n" +
49+
" " + os.Args[0] + " firmware get-version -b arduino:samd:mkr1000 -a COM10\n",
50+
Args: cobra.NoArgs,
51+
Run: runGetVersion,
52+
}
53+
commonFlags.AddToCommand(command)
54+
return command
55+
}
56+
57+
func runGetVersion(cmd *cobra.Command, args []string) {
58+
59+
packageIndex, firmwareIndex := common.InitIndexes()
60+
common.CheckFlags(commonFlags.Fqbn, commonFlags.Address)
61+
board := common.GetBoard(firmwareIndex, commonFlags.Fqbn)
62+
uploadToolDir := common.GetUploadToolDir(packageIndex, board)
63+
64+
versionSketchPath, err := download.DownloadSketch(board.VersionSketch)
65+
if err != nil {
66+
feedback.Errorf("Error downloading loader sketch from %s: %s", board.LoaderSketch.URL, err)
67+
os.Exit(errorcodes.ErrGeneric)
68+
}
69+
logrus.Debugf("version sketch downloaded in %s", versionSketchPath.String())
70+
71+
versionSketch := strings.ReplaceAll(versionSketchPath.String(), versionSketchPath.Ext(), "")
72+
73+
programmerOut, programmerErr, err := common.FlashSketch(board, versionSketch, uploadToolDir, commonFlags.Address)
74+
if err != nil {
75+
feedback.Error(err)
76+
os.Exit(errorcodes.ErrGeneric)
77+
}
78+
79+
// Wait a bit after flashing the sketch for the board to become available again.
80+
logrus.Debug("sleeping for 3 sec")
81+
time.Sleep(3 * time.Second)
82+
83+
currentVersion, err := getVersion(board)
84+
if err != nil {
85+
feedback.Error(err)
86+
os.Exit(1)
87+
}
88+
feedback.Printf("Firmware version installed: %s", currentVersion)
89+
90+
// Print the results
91+
feedback.PrintResult(&flasher.FlashResult{
92+
Programmer: (&flasher.ExecOutput{
93+
Stdout: programmerOut.String(),
94+
Stderr: programmerErr.String(),
95+
}),
96+
Version: currentVersion,
97+
})
98+
}
99+
100+
func getVersion(board *firmwareindex.IndexBoard) (fwVersion string, err error) {
101+
102+
// 9600 is the baudrate used in the CheckVersion sketch
103+
port, err := flasher.OpenSerial(commonFlags.Address, 9600, 2)
104+
if err != nil {
105+
feedback.Error(err)
106+
os.Exit(errorcodes.ErrGeneric)
107+
}
108+
109+
buff := make([]byte, 200)
110+
serialResult := make([]byte, 0)
111+
for {
112+
n, err := port.Read(buff)
113+
if err != nil {
114+
log.Fatal(err)
115+
break
116+
}
117+
serialResult = append(serialResult, buff[:n]...)
118+
if n == 0 { // exit when done reading from serial
119+
break
120+
}
121+
logrus.Info(string(buff[:n]))
122+
}
123+
lines := strings.Split(string(serialResult), "\n")
124+
for _, line := range lines {
125+
if strings.HasPrefix(line, "Firmware version installed: ") {
126+
version := strings.Replace(line, "Firmware version installed: ", "", 1)
127+
semver := semver.ParseRelaxed(version)
128+
return semver.String(), nil
129+
}
130+
}
131+
return "", fmt.Errorf("could not find the version string to parse")
132+
}

flasher/flasher.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ func OpenSerial(portAddress string, baudRate int, readTimeout int) (serial.Port,
8080

8181
type FlashResult struct {
8282
Programmer *ExecOutput `json:"programmer"`
83-
Flasher *ExecOutput `json:"flasher"`
83+
Flasher *ExecOutput `json:"flasher,omitempty"`
84+
Version string `json:"version,omitempty"`
8485
}
8586

8687
type ExecOutput struct {

0 commit comments

Comments
 (0)