| 1 | // This file is part of dummy-monitor. |
| 2 | // |
| 3 | // SPDX-FileCopyrightText: Arduino s.r.l. and/or its affiliated companies |
| 4 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 5 | |
| 6 | package main |
| 7 | |
| 8 | import ( |
| 9 | "errors" |
| 10 | "fmt" |
| 11 | "io" |
| 12 | "os" |
| 13 | |
| 14 | monitor "github.com/arduino/pluggable-monitor-protocol-handler" |
| 15 | "github.com/arduino/pluggable-monitor-protocol-handler/dummy-monitor/args" |
| 16 | ) |
| 17 | |
| 18 | // dummyMonitor is an example implementation of a Monitor. |
| 19 | // It simulates a real implementation of a Monitor by generating |
| 20 | // connected ports deterministically, it can also be used for testing |
| 21 | // purposes. |
| 22 | type dummyMonitor struct { |
| 23 | startSyncCount int |
| 24 | closeChan chan<- bool |
| 25 | } |
| 26 | |
| 27 | var settings = &monitor.PortDescriptor{ |
| 28 | Protocol: "test", |
| 29 | ConfigurationParameter: map[string]*monitor.PortParameterDescriptor{ |
| 30 | "speed": { |
| 31 | Label: "Baudrate", |
| 32 | Type: "enum", |
| 33 | Values: []string{"9600", "19200", "38400", "57600", "115200"}, |
| 34 | Selected: "9600", |
| 35 | }, |
| 36 | "echo": { |
| 37 | Label: "echo", |
| 38 | Type: "enum", |
| 39 | Values: []string{"on", "off"}, |
| 40 | Selected: "on", |
| 41 | }, |
| 42 | }, |
| 43 | } |
| 44 | |
| 45 | var openedPort *dummyPort |
| 46 | |
| 47 | func main() { |
| 48 | args.Parse() |
| 49 | dummy := &dummyMonitor{} |
| 50 | server := monitor.NewServer(dummy) |
| 51 | if err := server.Run(os.Stdin, os.Stdout); err != nil { |
| 52 | os.Exit(1) |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | // Hello does nothing. |
| 57 | // In a real implementation it could setup background processes |
| 58 | // or other kind of resources necessary to discover Ports. |
| 59 | func (d *dummyMonitor) Hello(userAgent string, protocol int) error { |
| 60 | return nil |
| 61 | } |
| 62 | |
| 63 | func (d *dummyMonitor) Describe() (*monitor.PortDescriptor, error) { |
| 64 | return settings, nil |
| 65 | } |
| 66 | |
| 67 | func (d *dummyMonitor) Configure(parameterName string, value string) error { |
| 68 | if settings.ConfigurationParameter[parameterName] == nil { |
| 69 | return fmt.Errorf("could not find parameter named %s", parameterName) |
| 70 | } |
| 71 | values := settings.ConfigurationParameter[parameterName].Values |
| 72 | for _, i := range values { |
| 73 | if i == value { |
| 74 | settings.ConfigurationParameter[parameterName].Selected = value |
| 75 | return nil |
| 76 | } |
| 77 | } |
| 78 | return fmt.Errorf("invalid value for parameter %s: %s", parameterName, value) |
| 79 | } |
| 80 | |
| 81 | func (d *dummyMonitor) Open(boardPort string) (io.ReadWriter, error) { |
| 82 | if openedPort != nil { |
| 83 | return nil, fmt.Errorf("port already opened: %s", openedPort.portName) |
| 84 | } |
| 85 | openedPort = newDummyPort(boardPort) |
| 86 | return openedPort, nil |
| 87 | } |
| 88 | |
| 89 | func (d *dummyMonitor) Close() error { |
| 90 | if openedPort == nil { |
| 91 | return errors.New("port already closed") |
| 92 | } |
| 93 | openedPort.Close() |
| 94 | openedPort = nil |
| 95 | return nil |
| 96 | } |
| 97 | |
| 98 | // Quit does nothing. |
| 99 | // In a real implementation it can be used to tear down resources |
| 100 | // used to monitor Ports. |
| 101 | func (d *dummyMonitor) Quit() {} |