-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathgetversion.go
163 lines (144 loc) · 5.32 KB
/
getversion.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
arduino-fwuploader
Copyright (c) 2021 Arduino LLC. All right reserved.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program 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 Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package firmware
import (
"fmt"
"io"
"log"
"os"
"strings"
"time"
"github.com/arduino/arduino-fwuploader/cli/common"
"github.com/arduino/arduino-fwuploader/cli/feedback"
"github.com/arduino/arduino-fwuploader/cli/globals"
"github.com/arduino/arduino-fwuploader/flasher"
"github.com/arduino/arduino-fwuploader/indexes/download"
"github.com/arduino/arduino-fwuploader/indexes/firmwareindex"
"github.com/arduino/arduino-fwuploader/plugin"
"github.com/arduino/go-paths-helper"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
semver "go.bug.st/relaxed-semver"
)
// NewGetVersionCommand creates a new `get-version` command
func NewGetVersionCommand() *cobra.Command {
command := &cobra.Command{
Use: "get-version",
Short: "Gets the version of the firmware the board is using.",
Long: "Flashes a sketch to a board to obtain the firmware version used by the board",
Example: "" +
" " + os.Args[0] + " firmware get-version --fqbn arduino:samd:mkr1000 --address COM10\n" +
" " + os.Args[0] + " firmware get-version -b arduino:samd:mkr1000 -a COM10\n",
Args: cobra.NoArgs,
Run: runGetVersion,
}
commonFlags.AddToCommand(command)
return command
}
func runGetVersion(cmd *cobra.Command, args []string) {
// at the end cleanup the fwuploader temp dir
defer globals.FwUploaderPath.RemoveAll()
common.CheckFlags(commonFlags.Fqbn, commonFlags.Address)
packageIndex, firmwareIndex := common.InitIndexes()
board := common.GetBoard(firmwareIndex, commonFlags.Fqbn)
uploadToolDir := common.DownloadRequiredToolsForBoard(packageIndex, board)
var result *flasher.FlashResult
if !board.IsPlugin() {
result = getVersion(board, uploadToolDir)
} else {
uploader, err := plugin.NewFWUploaderPlugin(uploadToolDir)
if err != nil {
feedback.Fatal(fmt.Sprintf("Could not open uploader plugin: %s", err), feedback.ErrGeneric)
}
result = getVersionWithPlugin(uploader)
}
if feedback.GetFormat() == feedback.Text {
fmt.Printf("Firmware version installed: %s", result.Version)
} else {
// Print the results
feedback.PrintResult(result)
}
}
func getVersionWithPlugin(uploader *plugin.FwUploader) *flasher.FlashResult {
var stdout, stderr io.Writer
if feedback.GetFormat() == feedback.Text {
stdout = os.Stdout
stderr = os.Stderr
}
res, err := uploader.GetFirmwareVersion(commonFlags.Address, commonFlags.Fqbn, stdout, stderr)
if err != nil {
feedback.Fatal(fmt.Sprintf("Couldn't get firmware version: %s", err), feedback.ErrGeneric)
}
return &flasher.FlashResult{
Programmer: (&flasher.ExecOutput{
Stdout: string(res.Stdout),
Stderr: string(res.Stderr),
}),
Version: res.FirmwareVersion.String(),
}
}
func getVersion(board *firmwareindex.IndexBoard, uploadToolDir *paths.Path) *flasher.FlashResult {
versionSketchPath, err := download.DownloadSketch(board.VersionSketch)
if err != nil {
feedback.Fatal(fmt.Sprintf("Error downloading loader sketch from %s: %s", board.LoaderSketch.URL, err), feedback.ErrGeneric)
}
logrus.Debugf("version sketch downloaded in %s", versionSketchPath.String())
versionSketch := strings.ReplaceAll(versionSketchPath.String(), versionSketchPath.Ext(), "")
programmerOut, programmerErr, err := common.FlashSketch(board, versionSketch, uploadToolDir, commonFlags.Address)
if err != nil {
feedback.FatalError(err, feedback.ErrGeneric)
}
// Wait a bit after flashing the sketch for the board to become available again.
logrus.Debug("sleeping for 3 sec")
time.Sleep(3 * time.Second)
// 9600 is the baudrate used in the CheckVersion sketch
port, err := flasher.OpenSerial(commonFlags.Address, 9600, 2)
if err != nil {
feedback.FatalError(err, feedback.ErrGeneric)
}
buff := make([]byte, 200)
serialResult := make([]byte, 0)
for {
n, err := port.Read(buff)
if err != nil {
log.Fatal(err)
break
}
serialResult = append(serialResult, buff[:n]...)
if n == 0 { // exit when done reading from serial
break
}
logrus.Info(string(buff[:n]))
}
lines := strings.Split(string(serialResult), "\n")
for _, line := range lines {
if strings.HasPrefix(line, "Firmware version installed: ") {
version := strings.TrimSpace(strings.Replace(line, "Firmware version installed: ", "", 1))
semver := semver.ParseRelaxed(version)
return &flasher.FlashResult{
Programmer: (&flasher.ExecOutput{
Stdout: programmerOut.String(),
Stderr: programmerErr.String(),
}),
Version: semver.String(),
}
}
if strings.HasPrefix(line, "Communication with WiFi module failed!") {
feedback.Fatal("communication with WiFi module failed", feedback.ErrGeneric)
}
}
feedback.Fatal("could not find the version string to parse", feedback.ErrGeneric)
return nil
}