|
16 | 16 | package monitor
|
17 | 17 |
|
18 | 18 | import (
|
| 19 | + "context" |
| 20 | + "io" |
| 21 | + |
19 | 22 | "github.com/arduino/arduino-cli/arduino/cores"
|
20 | 23 | "github.com/arduino/arduino-cli/arduino/cores/packagemanager"
|
| 24 | + pluggableMonitor "github.com/arduino/arduino-cli/arduino/monitor" |
21 | 25 | "github.com/arduino/arduino-cli/commands"
|
22 | 26 | rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
|
23 | 27 | )
|
24 | 28 |
|
| 29 | +// PortProxy is an io.ReadWriteCloser that maps into the monitor port of the board |
| 30 | +type PortProxy struct { |
| 31 | + rw io.ReadWriter |
| 32 | + changeSettingsCB func(setting, value string) error |
| 33 | + closeCB func() error |
| 34 | +} |
| 35 | + |
| 36 | +func (p *PortProxy) Read(buff []byte) (int, error) { |
| 37 | + return p.rw.Read(buff) |
| 38 | +} |
| 39 | + |
| 40 | +func (p *PortProxy) Write(buff []byte) (int, error) { |
| 41 | + return p.rw.Write(buff) |
| 42 | +} |
| 43 | + |
| 44 | +// Config sets the port configuration setting to the specified value |
| 45 | +func (p *PortProxy) Config(setting, value string) error { |
| 46 | + return p.changeSettingsCB(setting, value) |
| 47 | +} |
| 48 | + |
| 49 | +// Close the port |
| 50 | +func (p *PortProxy) Close() error { |
| 51 | + return p.closeCB() |
| 52 | +} |
| 53 | + |
| 54 | +// Monitor opens a communication port. It returns a PortProxy to communicate with the port and a PortDescriptor |
| 55 | +// that describes the available configuration settings. |
| 56 | +func Monitor(ctx context.Context, req *rpc.MonitorRequest) (*PortProxy, *pluggableMonitor.PortDescriptor, error) { |
| 57 | + pm := commands.GetPackageManager(req.GetInstance().GetId()) |
| 58 | + if pm == nil { |
| 59 | + return nil, nil, &commands.InvalidInstanceError{} |
| 60 | + } |
| 61 | + |
| 62 | + monitorRef, err := findMonitorForProtocolAndBoard(pm, req.GetPort(), req.GetFqbn()) |
| 63 | + if err != nil { |
| 64 | + return nil, nil, err |
| 65 | + } |
| 66 | + |
| 67 | + tool := pm.FindMonitorDependency(monitorRef) |
| 68 | + if tool == nil { |
| 69 | + return nil, nil, &commands.MonitorNotFoundError{Monitor: monitorRef.String()} |
| 70 | + } |
| 71 | + |
| 72 | + m := pluggableMonitor.New(monitorRef.Name, tool.InstallDir.Join(monitorRef.Name).String()) |
| 73 | + |
| 74 | + if err := m.Run(); err != nil { |
| 75 | + return nil, nil, &commands.FailedMonitorError{Cause: err} |
| 76 | + } |
| 77 | + |
| 78 | + descriptor, err := m.Describe() |
| 79 | + if err != nil { |
| 80 | + return nil, nil, &commands.FailedMonitorError{Cause: err} |
| 81 | + } |
| 82 | + |
| 83 | + monIO, err := m.Open(req.GetPort()) |
| 84 | + if err != nil { |
| 85 | + return nil, nil, &commands.FailedMonitorError{Cause: err} |
| 86 | + } |
| 87 | + |
| 88 | + return &PortProxy{ |
| 89 | + rw: monIO, |
| 90 | + changeSettingsCB: func(setting, value string) error { |
| 91 | + return m.Configure(setting, value) |
| 92 | + }, |
| 93 | + closeCB: func() error { |
| 94 | + m.Close() |
| 95 | + return m.Quit() |
| 96 | + }, |
| 97 | + }, descriptor, nil |
| 98 | +} |
| 99 | + |
25 | 100 | func findMonitorForProtocolAndBoard(pm *packagemanager.PackageManager, port *rpc.Port, fqbn string) (*cores.MonitorDependency, error) {
|
26 | 101 | if port == nil {
|
27 | 102 | return nil, &commands.MissingPortError{}
|
|
0 commit comments