Skip to content

Commit 70d8c00

Browse files
committed
Implementation of Monitor business logic
1 parent 73b8aa0 commit 70d8c00

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

Diff for: commands/monitor/monitor.go

+75
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,87 @@
1616
package monitor
1717

1818
import (
19+
"context"
20+
"io"
21+
1922
"github.com/arduino/arduino-cli/arduino/cores"
2023
"github.com/arduino/arduino-cli/arduino/cores/packagemanager"
24+
pluggableMonitor "github.com/arduino/arduino-cli/arduino/monitor"
2125
"github.com/arduino/arduino-cli/commands"
2226
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
2327
)
2428

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+
25100
func findMonitorForProtocolAndBoard(pm *packagemanager.PackageManager, port *rpc.Port, fqbn string) (*cores.MonitorDependency, error) {
26101
if port == nil {
27102
return nil, &commands.MissingPortError{}

0 commit comments

Comments
 (0)