Skip to content
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

Get default port address and protocol from sketch profile using monitor -s <sketchPath> #2329

Merged
merged 7 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions internal/cli/arguments/port.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,8 @@ func (p *Port) DetectFQBN(inst *rpc.Instance) (string, *rpc.Port) {
}
return "", nil
}

// IsPortFlagSet returns true if the port address is provided
func (p *Port) IsPortFlagSet() bool {
return p.address != ""
}
8 changes: 5 additions & 3 deletions internal/cli/arguments/sketch.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
// InitSketchPath returns an instance of paths.Path pointing to sketchPath.
// If sketchPath is an empty string returns the current working directory.
// In both cases it warns the user if he's using deprecated files
func InitSketchPath(path string) (sketchPath *paths.Path) {
func InitSketchPath(path string, printWarnings bool) (sketchPath *paths.Path) {
if path != "" {
sketchPath = paths.New(path)
} else {
Expand All @@ -36,8 +36,10 @@ func InitSketchPath(path string) (sketchPath *paths.Path) {
logrus.Infof("Reading sketch from dir: %s", wd)
sketchPath = wd
}
if msg := sk.WarnDeprecatedFiles(sketchPath); msg != "" {
feedback.Warning(msg)
if printWarnings {
if msg := sk.WarnDeprecatedFiles(sketchPath); msg != "" {
feedback.Warning(msg)
}
}
return sketchPath
}
2 changes: 1 addition & 1 deletion internal/cli/board/attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func initAttachCommand() *cobra.Command {
}

func runAttachCommand(path string, port *arguments.Port, fqbn string) {
sketchPath := arguments.InitSketchPath(path)
sketchPath := arguments.InitSketchPath(path, true)

portAddress, portProtocol, _ := port.GetPortAddressAndProtocol(nil, "", "")
newDefaults, err := sketch.SetSketchDefaults(context.Background(), &rpc.SetSketchDefaultsRequest{
Expand Down
2 changes: 1 addition & 1 deletion internal/cli/compile/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ func runCompileCommand(cmd *cobra.Command, args []string) {
path = args[0]
}

sketchPath := arguments.InitSketchPath(path)
sketchPath := arguments.InitSketchPath(path, true)

sk, err := sketch.LoadSketch(context.Background(), &rpc.LoadSketchRequest{SketchPath: sketchPath.String()})
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion internal/cli/debug/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func runDebugCommand(command *cobra.Command, args []string) {
path = args[0]
}

sketchPath := arguments.InitSketchPath(path)
sketchPath := arguments.InitSketchPath(path, true)
sk, err := sketch.LoadSketch(context.Background(), &rpc.LoadSketchRequest{SketchPath: sketchPath.String()})
if err != nil {
feedback.FatalError(err, feedback.ErrGeneric)
Expand Down
92 changes: 74 additions & 18 deletions internal/cli/monitor/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"time"

"github.com/arduino/arduino-cli/commands/monitor"
sk "github.com/arduino/arduino-cli/commands/sketch"
"github.com/arduino/arduino-cli/configuration"
"github.com/arduino/arduino-cli/i18n"
"github.com/arduino/arduino-cli/internal/cli/arguments"
Expand All @@ -45,13 +46,14 @@ var tr = i18n.Tr
// NewCommand created a new `monitor` command
func NewCommand() *cobra.Command {
var (
raw bool
portArgs arguments.Port
describe bool
configs []string
quiet bool
timestamp bool
fqbn arguments.Fqbn
portArgs arguments.Port
fqbnArg arguments.Fqbn
profileArg arguments.Profile
raw bool
describe bool
configs []string
quiet bool
timestamp bool
)
monitorCommand := &cobra.Command{
Use: "monitor",
Expand All @@ -61,38 +63,92 @@ func NewCommand() *cobra.Command {
" " + os.Args[0] + " monitor -p /dev/ttyACM0\n" +
" " + os.Args[0] + " monitor -p /dev/ttyACM0 --describe",
Run: func(cmd *cobra.Command, args []string) {
runMonitorCmd(&portArgs, &fqbn, configs, describe, timestamp, quiet, raw)
sketchPath := ""
if len(args) > 0 {
sketchPath = args[0]
}
runMonitorCmd(&portArgs, &fqbnArg, &profileArg, sketchPath, configs, describe, timestamp, quiet, raw)
},
}
portArgs.AddToCommand(monitorCommand)
profileArg.AddToCommand(monitorCommand)
monitorCommand.Flags().BoolVar(&raw, "raw", false, tr("Set terminal in raw mode (unbuffered)."))
monitorCommand.Flags().BoolVar(&describe, "describe", false, tr("Show all the settings of the communication port."))
monitorCommand.Flags().StringSliceVarP(&configs, "config", "c", []string{}, tr("Configure communication port settings. The format is <ID>=<value>[,<ID>=<value>]..."))
monitorCommand.Flags().BoolVarP(&quiet, "quiet", "q", false, tr("Run in silent mode, show only monitor input and output."))
monitorCommand.Flags().BoolVar(&timestamp, "timestamp", false, tr("Timestamp each incoming line."))
fqbn.AddToCommand(monitorCommand)
monitorCommand.MarkFlagRequired("port")
fqbnArg.AddToCommand(monitorCommand)
return monitorCommand
}

func runMonitorCmd(portArgs *arguments.Port, fqbn *arguments.Fqbn, configs []string, describe, timestamp, quiet, raw bool) {
instance := instance.CreateAndInit()
func runMonitorCmd(
portArgs *arguments.Port, fqbnArg *arguments.Fqbn, profileArg *arguments.Profile, sketchPathArg string,
configs []string, describe, timestamp, quiet, raw bool,
) {
logrus.Info("Executing `arduino-cli monitor`")

if !configuration.HasConsole {
quiet = true
}

// TODO: Should use sketch default_port/protocol?
portAddress, portProtocol, err := portArgs.GetPortAddressAndProtocol(instance, "", "")
var (
inst *rpc.Instance
profile *rpc.Profile
fqbn string
defaultPort, defaultProtocol string
)

// Flags takes maximum precedence over sketch.yaml
// If {--port --fqbn --profile} are set we ignore the profile.
// If both {--port --profile} are set we read the fqbn in the following order: profile -> default_fqbn -> discovery
// If only --port is set we read the fqbn in the following order: default_fqbn -> discovery
// If only --fqbn is set we read the port in the following order: default_port
sketchPath := arguments.InitSketchPath(sketchPathArg, false)
sketch, err := sk.LoadSketch(context.Background(), &rpc.LoadSketchRequest{SketchPath: sketchPath.String()})
if err != nil && !portArgs.IsPortFlagSet() {
feedback.Fatal(
tr("Error getting default port from `sketch.yaml`. Check if you're in the correct sketch folder or provide the --port flag: %s", err),
feedback.ErrGeneric,
)
}
if sketch != nil {
defaultPort, defaultProtocol = sketch.GetDefaultPort(), sketch.GetDefaultProtocol()
}
if fqbnArg.String() == "" {
if profileArg.Get() == "" {
inst, profile = instance.CreateAndInitWithProfile(sketch.GetDefaultProfile().GetName(), sketchPath)
} else {
inst, profile = instance.CreateAndInitWithProfile(profileArg.Get(), sketchPath)
}
}
if inst == nil {
inst = instance.CreateAndInit()
}
// Priority on how to retrieve the fqbn
// 1. from flag
// 2. from profile
// 3. from default_fqbn specified in the sketch.yaml
// 4. try to detect from the port
switch {
case fqbnArg.String() != "":
fqbn = fqbnArg.String()
case profile.GetFqbn() != "":
fqbn = profile.GetFqbn()
case sketch.GetDefaultFqbn() != "":
fqbn = sketch.GetDefaultFqbn()
default:
fqbn, _ = portArgs.DetectFQBN(inst)
}

portAddress, portProtocol, err := portArgs.GetPortAddressAndProtocol(inst, defaultPort, defaultProtocol)
if err != nil {
feedback.FatalError(err, feedback.ErrGeneric)
}

enumerateResp, err := monitor.EnumerateMonitorPortSettings(context.Background(), &rpc.EnumerateMonitorPortSettingsRequest{
Instance: instance,
Instance: inst,
PortProtocol: portProtocol,
Fqbn: fqbn.String(),
Fqbn: fqbn,
})
if err != nil {
feedback.Fatal(tr("Error getting port settings details: %s", err), feedback.ErrGeneric)
Expand Down Expand Up @@ -144,9 +200,9 @@ func runMonitorCmd(portArgs *arguments.Port, fqbn *arguments.Fqbn, configs []str
}
}
portProxy, _, err := monitor.Monitor(context.Background(), &rpc.MonitorRequest{
Instance: instance,
Instance: inst,
Port: &rpc.Port{Address: portAddress, Protocol: portProtocol},
Fqbn: fqbn.String(),
Fqbn: fqbn,
PortConfiguration: configuration,
})
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion internal/cli/upload/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func runUploadCommand(args []string, uploadFieldsArgs map[string]string) {
if len(args) > 0 {
path = args[0]
}
sketchPath := arguments.InitSketchPath(path)
sketchPath := arguments.InitSketchPath(path, true)

if msg := sk.WarnDeprecatedFiles(sketchPath); importDir == "" && importFile == "" && msg != "" {
feedback.Warning(msg)
Expand Down
Loading
Loading