|
| 1 | +// This file is part of arduino-cli. |
| 2 | +// |
| 3 | +// Copyright 2020 ARDUINO SA (http://www.arduino.cc/) |
| 4 | +// |
| 5 | +// This software is released under the GNU General Public License version 3, |
| 6 | +// which covers the main part of arduino-cli. |
| 7 | +// The terms of this license can be found at: |
| 8 | +// https://www.gnu.org/licenses/gpl-3.0.en.html |
| 9 | +// |
| 10 | +// You can be released from the requirements of the above licenses by purchasing |
| 11 | +// a commercial license. Buying such a license is mandatory if you want to |
| 12 | +// modify or otherwise use the software for commercial activities involving the |
| 13 | +// Arduino software without disclosing the source code of your own applications. |
| 14 | +// To purchase a commercial license, send an email to [email protected]. |
| 15 | + |
| 16 | +package monitor |
| 17 | + |
| 18 | +import ( |
| 19 | + "context" |
| 20 | + "errors" |
| 21 | + "fmt" |
| 22 | + "io" |
| 23 | + "os" |
| 24 | + "sort" |
| 25 | + "strings" |
| 26 | + |
| 27 | + "github.com/arduino/arduino-cli/cli/arguments" |
| 28 | + "github.com/arduino/arduino-cli/cli/errorcodes" |
| 29 | + "github.com/arduino/arduino-cli/cli/feedback" |
| 30 | + "github.com/arduino/arduino-cli/cli/instance" |
| 31 | + "github.com/arduino/arduino-cli/commands/monitor" |
| 32 | + "github.com/arduino/arduino-cli/configuration" |
| 33 | + "github.com/arduino/arduino-cli/i18n" |
| 34 | + rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1" |
| 35 | + "github.com/arduino/arduino-cli/table" |
| 36 | + "github.com/fatih/color" |
| 37 | + "github.com/spf13/cobra" |
| 38 | +) |
| 39 | + |
| 40 | +var tr = i18n.Tr |
| 41 | + |
| 42 | +var portArgs arguments.Port |
| 43 | +var describe bool |
| 44 | +var configs []string |
| 45 | +var quiet bool |
| 46 | +var fqbn string |
| 47 | + |
| 48 | +// NewCommand created a new `monitor` command |
| 49 | +func NewCommand() *cobra.Command { |
| 50 | + cmd := &cobra.Command{ |
| 51 | + Use: "monitor", |
| 52 | + Short: tr("Open a communication port with a board."), |
| 53 | + Long: tr("Open a communication port with a board."), |
| 54 | + Example: "" + |
| 55 | + " " + os.Args[0] + " monitor -p /dev/ttyACM0\n" + |
| 56 | + " " + os.Args[0] + " monitor -p /dev/ttyACM0 --describe", |
| 57 | + Run: runMonitorCmd, |
| 58 | + } |
| 59 | + portArgs.AddToCommand(cmd) |
| 60 | + cmd.Flags().BoolVar(&describe, "describe", false, tr("Show all the settings of the communication port.")) |
| 61 | + cmd.Flags().StringSliceVarP(&configs, "config", "c", []string{}, tr("Configuration of the port.")) |
| 62 | + cmd.Flags().BoolVarP(&quiet, "quiet", "q", false, tr("Run in silent mode, show only monitor input and output.")) |
| 63 | + cmd.Flags().StringVarP(&fqbn, "fqbn", "b", "", tr("Fully Qualified Board Name, e.g.: arduino:avr:uno")) |
| 64 | + cmd.MarkFlagRequired("port") |
| 65 | + return cmd |
| 66 | +} |
| 67 | + |
| 68 | +func runMonitorCmd(cmd *cobra.Command, args []string) { |
| 69 | + instance := instance.CreateAndInit() |
| 70 | + |
| 71 | + if !configuration.HasConsole { |
| 72 | + quiet = true |
| 73 | + } |
| 74 | + |
| 75 | + portAddress, portProtocol, err := portArgs.GetPortAddressAndProtocol(instance, nil) |
| 76 | + if err != nil { |
| 77 | + feedback.Error(err) |
| 78 | + os.Exit(errorcodes.ErrGeneric) |
| 79 | + } |
| 80 | + |
| 81 | + enumerateResp, err := monitor.EnumerateMonitorPortSettings(context.Background(), &rpc.EnumerateMonitorPortSettingsRequest{ |
| 82 | + Instance: instance, |
| 83 | + PortProtocol: portProtocol, |
| 84 | + Fqbn: fqbn, |
| 85 | + }) |
| 86 | + if err != nil { |
| 87 | + feedback.Error(tr("Error getting port settings details: %s", err)) |
| 88 | + os.Exit(errorcodes.ErrGeneric) |
| 89 | + } |
| 90 | + if describe { |
| 91 | + feedback.PrintResult(&detailsResult{Settings: enumerateResp.Settings}) |
| 92 | + return |
| 93 | + } |
| 94 | + |
| 95 | + tty, err := newStdInOutTerminal() |
| 96 | + if err != nil { |
| 97 | + feedback.Error(err) |
| 98 | + os.Exit(errorcodes.ErrGeneric) |
| 99 | + } |
| 100 | + defer tty.Close() |
| 101 | + |
| 102 | + configuration := &rpc.MonitorPortConfiguration{} |
| 103 | + if len(configs) > 0 { |
| 104 | + for _, config := range configs { |
| 105 | + split := strings.SplitN(config, "=", 2) |
| 106 | + k := "" |
| 107 | + v := config |
| 108 | + if len(split) == 2 { |
| 109 | + k = split[0] |
| 110 | + v = split[1] |
| 111 | + } |
| 112 | + |
| 113 | + var setting *rpc.MonitorPortSettingDescriptor |
| 114 | + for _, s := range enumerateResp.GetSettings() { |
| 115 | + if k == "" { |
| 116 | + if contains(s.EnumValues, v) { |
| 117 | + setting = s |
| 118 | + break |
| 119 | + } |
| 120 | + } else { |
| 121 | + if strings.EqualFold(s.SettingId, k) { |
| 122 | + if !contains(s.EnumValues, v) { |
| 123 | + feedback.Error(tr("invalid port configuration value for %s: %s", k, v)) |
| 124 | + os.Exit(errorcodes.ErrBadArgument) |
| 125 | + } |
| 126 | + setting = s |
| 127 | + break |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + if setting == nil { |
| 132 | + feedback.Error(tr("invalid port configuration: %s", config)) |
| 133 | + os.Exit(errorcodes.ErrBadArgument) |
| 134 | + } |
| 135 | + configuration.Settings = append(configuration.Settings, &rpc.MonitorPortSetting{ |
| 136 | + SettingId: setting.SettingId, |
| 137 | + Value: v, |
| 138 | + }) |
| 139 | + if !quiet { |
| 140 | + feedback.Print(tr("Monitor port settings:")) |
| 141 | + feedback.Print(fmt.Sprintf("%s=%s", setting.SettingId, v)) |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | + portProxy, _, err := monitor.Monitor(context.Background(), &rpc.MonitorRequest{ |
| 146 | + Instance: instance, |
| 147 | + Port: &rpc.Port{Address: portAddress, Protocol: portProtocol}, |
| 148 | + Fqbn: fqbn, |
| 149 | + PortConfiguration: configuration, |
| 150 | + }) |
| 151 | + if err != nil { |
| 152 | + feedback.Error(err) |
| 153 | + os.Exit(errorcodes.ErrGeneric) |
| 154 | + } |
| 155 | + defer portProxy.Close() |
| 156 | + |
| 157 | + ctx, cancel := context.WithCancel(context.Background()) |
| 158 | + go func() { |
| 159 | + _, err := io.Copy(tty, portProxy) |
| 160 | + if err != nil && !errors.Is(err, io.EOF) { |
| 161 | + feedback.Error(tr("Port closed:"), err) |
| 162 | + } |
| 163 | + cancel() |
| 164 | + }() |
| 165 | + go func() { |
| 166 | + _, err := io.Copy(portProxy, tty) |
| 167 | + if err != nil && !errors.Is(err, io.EOF) { |
| 168 | + feedback.Error(tr("Port closed:"), err) |
| 169 | + } |
| 170 | + cancel() |
| 171 | + }() |
| 172 | + |
| 173 | + if !quiet { |
| 174 | + feedback.Print(tr("Connected to %s! Press CTRL-C to exit.", portAddress)) |
| 175 | + } |
| 176 | + |
| 177 | + // Wait for port closed |
| 178 | + <-ctx.Done() |
| 179 | +} |
| 180 | + |
| 181 | +type detailsResult struct { |
| 182 | + Settings []*rpc.MonitorPortSettingDescriptor `json:"settings"` |
| 183 | +} |
| 184 | + |
| 185 | +func (r *detailsResult) Data() interface{} { |
| 186 | + return r |
| 187 | +} |
| 188 | + |
| 189 | +func (r *detailsResult) String() string { |
| 190 | + t := table.New() |
| 191 | + green := color.New(color.FgGreen) |
| 192 | + t.SetHeader(tr("ID"), tr("Setting"), tr("Default"), tr("Values")) |
| 193 | + sort.Slice(r.Settings, func(i, j int) bool { |
| 194 | + return r.Settings[i].Label < r.Settings[j].Label |
| 195 | + }) |
| 196 | + for _, setting := range r.Settings { |
| 197 | + values := strings.Join(setting.EnumValues, ", ") |
| 198 | + t.AddRow(setting.SettingId, setting.Label, table.NewCell(setting.Value, green), values) |
| 199 | + } |
| 200 | + return t.Render() |
| 201 | +} |
| 202 | + |
| 203 | +func contains(s []string, searchterm string) bool { |
| 204 | + for _, item := range s { |
| 205 | + if strings.EqualFold(item, searchterm) { |
| 206 | + return true |
| 207 | + } |
| 208 | + } |
| 209 | + return false |
| 210 | +} |
0 commit comments