Skip to content

Commit 8fb4b95

Browse files
committed
Change gRPC upload functions to use new Port message
1 parent dbdc1ef commit 8fb4b95

23 files changed

+362
-256
lines changed

cli/burnbootloader/burnbootloader.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"context"
2020
"os"
2121

22+
"github.com/arduino/arduino-cli/cli/arguments"
2223
"github.com/arduino/arduino-cli/cli/errorcodes"
2324
"github.com/arduino/arduino-cli/cli/feedback"
2425
"github.com/arduino/arduino-cli/cli/instance"
@@ -29,7 +30,7 @@ import (
2930

3031
var (
3132
fqbn string
32-
port string
33+
port arguments.Port
3334
verbose bool
3435
verify bool
3536
programmer string
@@ -48,7 +49,7 @@ func NewCommand() *cobra.Command {
4849
}
4950

5051
burnBootloaderCommand.Flags().StringVarP(&fqbn, "fqbn", "b", "", "Fully Qualified Board Name, e.g.: arduino:avr:uno")
51-
burnBootloaderCommand.Flags().StringVarP(&port, "port", "p", "", "Upload port, e.g.: COM10 or /dev/ttyACM0")
52+
port.AddToCommand(burnBootloaderCommand)
5253
burnBootloaderCommand.Flags().BoolVarP(&verify, "verify", "t", false, "Verify uploaded binary after the upload.")
5354
burnBootloaderCommand.Flags().BoolVarP(&verbose, "verbose", "v", false, "Turns on verbose mode.")
5455
burnBootloaderCommand.Flags().StringVarP(&programmer, "programmer", "P", "", "Use the specified programmer to upload.")
@@ -61,10 +62,17 @@ func NewCommand() *cobra.Command {
6162
func run(command *cobra.Command, args []string) {
6263
instance := instance.CreateAndInit()
6364

65+
// We don't need a Sketch to upload a board's bootloader
66+
discoveryPort, err := port.GetPort(instance, nil)
67+
if err != nil {
68+
feedback.Errorf("Error during Upload: %v", err)
69+
os.Exit(errorcodes.ErrGeneric)
70+
}
71+
6472
if _, err := upload.BurnBootloader(context.Background(), &rpc.BurnBootloaderRequest{
6573
Instance: instance,
6674
Fqbn: fqbn,
67-
Port: port,
75+
Port: discoveryPort.ToRPC(),
6876
Verbose: verbose,
6977
Verify: verify,
7078
Programmer: programmer,

cli/compile/compile.go

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"encoding/json"
2222
"os"
2323

24+
"github.com/arduino/arduino-cli/arduino/discovery"
2425
"github.com/arduino/arduino-cli/arduino/sketch"
2526
"github.com/arduino/arduino-cli/cli/arguments"
2627
"github.com/arduino/arduino-cli/cli/feedback"
@@ -37,25 +38,25 @@ import (
3738
)
3839

3940
var (
40-
fqbn string // Fully Qualified Board Name, e.g.: arduino:avr:uno.
41-
showProperties bool // Show all build preferences used instead of compiling.
42-
preprocess bool // Print preprocessed code to stdout.
43-
buildCachePath string // Builds of 'core.a' are saved into this path to be cached and reused.
44-
buildPath string // Path where to save compiled files.
45-
buildProperties []string // List of custom build properties separated by commas. Or can be used multiple times for multiple properties.
46-
warnings string // Used to tell gcc which warning level to use.
47-
verbose bool // Turns on verbose mode.
48-
quiet bool // Suppresses almost every output.
49-
vidPid string // VID/PID specific build properties.
50-
uploadAfterCompile bool // Upload the binary after the compilation.
51-
port string // Upload port, e.g.: COM10 or /dev/ttyACM0.
52-
verify bool // Upload, verify uploaded binary after the upload.
53-
exportDir string // The compiled binary is written to this file
54-
optimizeForDebug bool // Optimize compile output for debug, not for release
55-
programmer string // Use the specified programmer to upload
56-
clean bool // Cleanup the build folder and do not use any cached build
57-
compilationDatabaseOnly bool // Only create compilation database without actually compiling
58-
sourceOverrides string // Path to a .json file that contains a set of replacements of the sketch source code.
41+
fqbn string // Fully Qualified Board Name, e.g.: arduino:avr:uno.
42+
showProperties bool // Show all build preferences used instead of compiling.
43+
preprocess bool // Print preprocessed code to stdout.
44+
buildCachePath string // Builds of 'core.a' are saved into this path to be cached and reused.
45+
buildPath string // Path where to save compiled files.
46+
buildProperties []string // List of custom build properties separated by commas. Or can be used multiple times for multiple properties.
47+
warnings string // Used to tell gcc which warning level to use.
48+
verbose bool // Turns on verbose mode.
49+
quiet bool // Suppresses almost every output.
50+
vidPid string // VID/PID specific build properties.
51+
uploadAfterCompile bool // Upload the binary after the compilation.
52+
port arguments.Port // Upload port, e.g.: COM10 or /dev/ttyACM0.
53+
verify bool // Upload, verify uploaded binary after the upload.
54+
exportDir string // The compiled binary is written to this file
55+
optimizeForDebug bool // Optimize compile output for debug, not for release
56+
programmer string // Use the specified programmer to upload
57+
clean bool // Cleanup the build folder and do not use any cached build
58+
compilationDatabaseOnly bool // Only create compilation database without actually compiling
59+
sourceOverrides string // Path to a .json file that contains a set of replacements of the sketch source code.
5960
// library and libraries sound similar but they're actually different.
6061
// library expects a path to the root folder of one single library.
6162
// libraries expects a path to a directory containing multiple libraries, similarly to the <directories.user>/libraries path.
@@ -94,7 +95,7 @@ func NewCommand() *cobra.Command {
9495
command.Flags().BoolVarP(&verbose, "verbose", "v", false, "Optional, turns on verbose mode.")
9596
command.Flags().BoolVar(&quiet, "quiet", false, "Optional, suppresses almost every output.")
9697
command.Flags().BoolVarP(&uploadAfterCompile, "upload", "u", false, "Upload the binary after the compilation.")
97-
command.Flags().StringVarP(&port, "port", "p", "", "Upload port, e.g.: COM10 or /dev/ttyACM0")
98+
port.AddToCommand(command)
9899
command.Flags().BoolVarP(&verify, "verify", "t", false, "Verify uploaded binary after the upload.")
99100
command.Flags().StringVar(&vidPid, "vid-pid", "", "When specified, VID/PID specific build properties are used, if board supports them.")
100101
command.Flags().StringSliceVar(&library, "library", []string{},
@@ -186,11 +187,23 @@ func run(cmd *cobra.Command, args []string) {
186187
}
187188

188189
if err == nil && uploadAfterCompile {
190+
191+
var sk *sketch.Sketch
192+
sk, err = sketch.New(sketchPath)
193+
if err != nil {
194+
195+
}
196+
var discoveryPort *discovery.Port
197+
discoveryPort, err = port.GetPort(inst, sk)
198+
if err != nil {
199+
200+
}
201+
189202
uploadRequest := &rpc.UploadRequest{
190203
Instance: inst,
191204
Fqbn: fqbn,
192205
SketchPath: sketchPath.String(),
193-
Port: port,
206+
Port: discoveryPort.ToRPC(),
194207
Verbose: verbose,
195208
Verify: verify,
196209
ImportDir: buildPath,

cli/upload/upload.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import (
3131

3232
var (
3333
fqbn string
34-
port string
34+
port arguments.Port
3535
verbose bool
3636
verify bool
3737
importDir string
@@ -53,7 +53,7 @@ func NewCommand() *cobra.Command {
5353
}
5454

5555
uploadCommand.Flags().StringVarP(&fqbn, "fqbn", "b", "", "Fully Qualified Board Name, e.g.: arduino:avr:uno")
56-
uploadCommand.Flags().StringVarP(&port, "port", "p", "", "Upload port, e.g.: COM10 or /dev/ttyACM0")
56+
port.AddToCommand(uploadCommand)
5757
uploadCommand.Flags().StringVarP(&importDir, "input-dir", "", "", "Directory containing binaries to upload.")
5858
uploadCommand.Flags().StringVarP(&importFile, "input-file", "i", "", "Binary file to upload.")
5959
uploadCommand.Flags().BoolVarP(&verify, "verify", "t", false, "Verify uploaded binary after the upload.")
@@ -88,11 +88,22 @@ func run(command *cobra.Command, args []string) {
8888
}
8989
}
9090

91+
sk, err := sketch.New(sketchPath)
92+
if err != nil {
93+
feedback.Errorf("Error during Upload: %v", err)
94+
os.Exit(errorcodes.ErrGeneric)
95+
}
96+
discoveryPort, err := port.GetPort(instance, sk)
97+
if err != nil {
98+
feedback.Errorf("Error during Upload: %v", err)
99+
os.Exit(errorcodes.ErrGeneric)
100+
}
101+
91102
if _, err := upload.Upload(context.Background(), &rpc.UploadRequest{
92103
Instance: instance,
93104
Fqbn: fqbn,
94105
SketchPath: sketchPath.String(),
95-
Port: port,
106+
Port: discoveryPort.ToRPC(),
96107
Verbose: verbose,
97108
Verify: verify,
98109
ImportFile: importFile,

commands/upload/upload.go

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919
"context"
2020
"fmt"
2121
"io"
22-
"net/url"
2322
"path/filepath"
2423
"strings"
2524

@@ -37,6 +36,22 @@ import (
3736
"github.com/sirupsen/logrus"
3837
)
3938

39+
// getToolId returns the ID of the tool that supports the action and protocol combination by searching in props.
40+
// Returns error if tool cannot be found.
41+
func getToolId(props *properties.Map, action, protocol string) (string, error) {
42+
toolProperty := fmt.Sprintf("%s.tool.%s", action, protocol)
43+
defaultToolProperty := fmt.Sprintf("%s.tool.default", action)
44+
45+
if t, ok := props.GetOk(toolProperty); ok {
46+
return t, nil
47+
} else if t, ok := props.GetOk(defaultToolProperty); ok {
48+
// Fallback for platform that don't support the specified protocol for specified action:
49+
// https://github.com/arduino/tooling-rfcs/blob/main/RFCs/0002-pluggable-discovery.md#support-for-default-fallback-allows-upload-without-a-port
50+
return t, nil
51+
}
52+
return "", fmt.Errorf("cannot find tool: undefined '%s' property", toolProperty)
53+
}
54+
4055
// Upload FIXMEDOC
4156
func Upload(ctx context.Context, req *rpc.UploadRequest, outStream io.Writer, errStream io.Writer) (*rpc.UploadResponse, error) {
4257
logrus.Tracef("Upload %s on %s started", req.GetSketchPath(), req.GetFqbn())
@@ -95,7 +110,7 @@ func UsingProgrammer(ctx context.Context, req *rpc.UploadUsingProgrammerRequest,
95110

96111
func runProgramAction(pm *packagemanager.PackageManager,
97112
sk *sketch.Sketch,
98-
importFile, importDir, fqbnIn, port string,
113+
importFile, importDir, fqbnIn string, port *rpc.Port,
99114
programmerID string,
100115
verbose, verify, burnBootloader bool,
101116
outStream, errStream io.Writer,
@@ -105,16 +120,6 @@ func runProgramAction(pm *packagemanager.PackageManager,
105120
return fmt.Errorf("no programmer specified for burning bootloader")
106121
}
107122

108-
// FIXME: make a specification on how a port is specified via command line
109-
if port == "" && sk != nil && sk.Metadata != nil {
110-
deviceURI, err := url.Parse(sk.Metadata.CPU.Port)
111-
if err != nil {
112-
return fmt.Errorf("invalid Device URL format: %s", err)
113-
}
114-
if deviceURI.Scheme == "serial" {
115-
port = deviceURI.Host + deviceURI.Path
116-
}
117-
}
118123
logrus.WithField("port", port).Tracef("Upload port")
119124

120125
if fqbnIn == "" && sk != nil && sk.Metadata != nil {
@@ -154,28 +159,23 @@ func runProgramAction(pm *packagemanager.PackageManager,
154159
}
155160

156161
// Determine upload tool
157-
var uploadToolID string
158-
{
159-
toolProperty := "upload.tool"
160-
if burnBootloader {
161-
toolProperty = "bootloader.tool"
162-
} else if programmer != nil {
163-
toolProperty = "program.tool"
164-
}
165-
166-
// create a temporary configuration only for the selection of upload tool
167-
props := properties.NewMap()
168-
props.Merge(boardPlatform.Properties)
169-
props.Merge(boardPlatform.RuntimeProperties())
170-
props.Merge(boardProperties)
171-
if programmer != nil {
172-
props.Merge(programmer.Properties)
173-
}
174-
if t, ok := props.GetOk(toolProperty); ok {
175-
uploadToolID = t
176-
} else {
177-
return fmt.Errorf("cannot get programmer tool: undefined '%s' property", toolProperty)
178-
}
162+
// create a temporary configuration only for the selection of upload tool
163+
props := properties.NewMap()
164+
props.Merge(boardPlatform.Properties)
165+
props.Merge(boardPlatform.RuntimeProperties())
166+
props.Merge(boardProperties)
167+
if programmer != nil {
168+
props.Merge(programmer.Properties)
169+
}
170+
action := "upload"
171+
if burnBootloader {
172+
action = "bootloader"
173+
} else if programmer != nil {
174+
action = "program"
175+
}
176+
uploadToolID, err := getToolId(props, action, port.Protocol)
177+
if err != nil {
178+
return err
179179
}
180180

181181
var uploadToolPlatform *cores.PlatformRelease
@@ -299,7 +299,7 @@ func runProgramAction(pm *packagemanager.PackageManager,
299299
wait := false
300300
portToTouch := ""
301301
if touch {
302-
portToTouch = port
302+
portToTouch = port.Address
303303
// Waits for upload port only if a 1200bps touch is done
304304
wait = uploadProperties.GetBoolean("upload.wait_for_upload_port")
305305
}
@@ -349,18 +349,18 @@ func runProgramAction(pm *packagemanager.PackageManager,
349349
outStream.Write([]byte(fmt.Sprintln()))
350350
} else {
351351
if newPort != "" {
352-
actualPort = newPort
352+
actualPort.Address = newPort
353353
}
354354
}
355355
}
356356

357-
if actualPort != "" {
357+
if actualPort.Address != "" {
358358
// Set serial port property
359-
uploadProperties.Set("serial.port", actualPort)
360-
if strings.HasPrefix(actualPort, "/dev/") {
361-
uploadProperties.Set("serial.port.file", actualPort[5:])
362-
} else {
363-
uploadProperties.Set("serial.port.file", actualPort)
359+
uploadProperties.Set("serial.port", actualPort.Address)
360+
if actualPort.Protocol == "serial" {
361+
// This must be done only for serial ports
362+
portFile := strings.TrimPrefix(actualPort.Address, "/dev/")
363+
uploadProperties.Set("serial.port.file", portFile)
364364
}
365365
}
366366

0 commit comments

Comments
 (0)