| 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 mainscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler/dummy-monitor`/ |
| 7 | |
| 8 | import ( |
| 9 | "errorsscip-go gomod github.com/golang/go/src go1.16 errors/" |
| 10 | "fmtscip-go gomod github.com/golang/go/src go1.16 fmt/" |
| 11 | "ioscip-go gomod github.com/golang/go/src go1.16 io/" |
| 12 | "osscip-go gomod github.com/golang/go/src go1.16 os/" |
| 13 | |
| 14 | monitorscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/ "github.com/arduino/pluggable-monitor-protocol-handlerscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/" |
| 15 | "github.com/arduino/pluggable-monitor-protocol-handler/dummy-monitor/argsscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `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 dummyMonitorscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler/dummy-monitor`/dummyMonitor# struct { |
| 23 | startSyncCountscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler/dummy-monitor`/dummyMonitor#startSyncCount. int |
| 24 | closeChanscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler/dummy-monitor`/dummyMonitor#closeChan. chan<- bool |
| 25 | } |
| 26 | |
| 27 | var settingsscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler/dummy-monitor`/settings. = &monitorscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/.PortDescriptorscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/PortDescriptor#{ |
| 28 | Protocolscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/PortDescriptor#Protocol.: "test", |
| 29 | ConfigurationParameterscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/PortDescriptor#ConfigurationParameter.: map[string]*monitorscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/.PortParameterDescriptorscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/PortParameterDescriptor#{ |
| 30 | "speed": { |
| 31 | Labelscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/PortParameterDescriptor#Label.: "Baudrate", |
| 32 | Typescip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/PortParameterDescriptor#Type.: "enum", |
| 33 | Valuesscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/PortParameterDescriptor#Values.: []string{"9600", "19200", "38400", "57600", "115200"}, |
| 34 | Selectedscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/PortParameterDescriptor#Selected.: "9600", |
| 35 | }, |
| 36 | "echo": { |
| 37 | Labelscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/PortParameterDescriptor#Label.: "echo", |
| 38 | Typescip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/PortParameterDescriptor#Type.: "enum", |
| 39 | Valuesscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/PortParameterDescriptor#Values.: []string{"on", "off"}, |
| 40 | Selectedscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/PortParameterDescriptor#Selected.: "on", |
| 41 | }, |
| 42 | }, |
| 43 | } |
| 44 | |
| 45 | var openedPortscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler/dummy-monitor`/openedPort. *dummyPortscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler/dummy-monitor`/dummyPort# |
| 46 | |
| 47 | func mainscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler/dummy-monitor`/main().() { |
| 48 | argsscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler/dummy-monitor/args`/.Parsescip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler/dummy-monitor/args`/Parse().() |
| 49 | dummy := &dummyMonitorscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler/dummy-monitor`/dummyMonitor#{} |
| 50 | server := monitorscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/.NewServerscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/NewServer().(dummy) |
| 51 | if err := server.Runscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/Server#Run().(osscip-go gomod github.com/golang/go/src go1.16 os/.Stdinscip-go gomod github.com/golang/go/src go1.16 os/Stdin., osscip-go gomod github.com/golang/go/src go1.16 os/.Stdoutscip-go gomod github.com/golang/go/src go1.16 os/Stdout.); err != nil { |
| 52 | osscip-go gomod github.com/golang/go/src go1.16 os/.Exitscip-go gomod github.com/golang/go/src go1.16 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 *dummyMonitorscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler/dummy-monitor`/dummyMonitor#) Helloscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler/dummy-monitor`/dummyMonitor#Hello().(userAgent string, protocol int) error { |
| 60 | return nil |
| 61 | } |
| 62 | |
| 63 | func (d *dummyMonitorscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler/dummy-monitor`/dummyMonitor#) Describescip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler/dummy-monitor`/dummyMonitor#Describe().() (*monitorscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/.PortDescriptorscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/PortDescriptor#, error) { |
| 64 | return settingsscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler/dummy-monitor`/settings., nil |
| 65 | } |
| 66 | |
| 67 | func (d *dummyMonitorscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler/dummy-monitor`/dummyMonitor#) Configurescip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler/dummy-monitor`/dummyMonitor#Configure().(parameterName string, value string) error { |
| 68 | if settingsscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler/dummy-monitor`/settings..ConfigurationParameterscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/PortDescriptor#ConfigurationParameter.[parameterName] == nil { |
| 69 | return fmtscip-go gomod github.com/golang/go/src go1.16 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.16 fmt/Errorf().("could not find parameter named %s", parameterName) |
| 70 | } |
| 71 | values := settingsscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler/dummy-monitor`/settings..ConfigurationParameterscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/PortDescriptor#ConfigurationParameter.[parameterName].Valuesscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/PortParameterDescriptor#Values. |
| 72 | for _, i := range values { |
| 73 | if i == value { |
| 74 | settingsscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler/dummy-monitor`/settings..ConfigurationParameterscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/PortDescriptor#ConfigurationParameter.[parameterName].Selectedscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/PortParameterDescriptor#Selected. = value |
| 75 | return nil |
| 76 | } |
| 77 | } |
| 78 | return fmtscip-go gomod github.com/golang/go/src go1.16 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.16 fmt/Errorf().("invalid value for parameter %s: %s", parameterName, value) |
| 79 | } |
| 80 | |
| 81 | func (d *dummyMonitorscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler/dummy-monitor`/dummyMonitor#) Openscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler/dummy-monitor`/dummyMonitor#Open().(boardPort string) (ioscip-go gomod github.com/golang/go/src go1.16 io/.ReadWriterscip-go gomod github.com/golang/go/src go1.16 io/ReadWriter#, error) { |
| 82 | if openedPortscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler/dummy-monitor`/openedPort. != nil { |
| 83 | return nil, fmtscip-go gomod github.com/golang/go/src go1.16 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.16 fmt/Errorf().("port already opened: %s", openedPortscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler/dummy-monitor`/openedPort..portNamescip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler/dummy-monitor`/dummyPort#portName.) |
| 84 | } |
| 85 | openedPortscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler/dummy-monitor`/openedPort. = newDummyPortscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler/dummy-monitor`/newDummyPort().(boardPort) |
| 86 | return openedPortscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler/dummy-monitor`/openedPort., nil |
| 87 | } |
| 88 | |
| 89 | func (d *dummyMonitorscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler/dummy-monitor`/dummyMonitor#) Closescip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler/dummy-monitor`/dummyMonitor#Close().() error { |
| 90 | if openedPortscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler/dummy-monitor`/openedPort. == nil { |
| 91 | return errorsscip-go gomod github.com/golang/go/src go1.16 errors/.Newscip-go gomod github.com/golang/go/src go1.16 errors/New().("port already closed") |
| 92 | } |
| 93 | openedPortscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler/dummy-monitor`/openedPort..Closescip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler/dummy-monitor`/dummyPort#Close().() |
| 94 | openedPortscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler/dummy-monitor`/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 *dummyMonitorscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler/dummy-monitor`/dummyMonitor#) Quitscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler/dummy-monitor`/dummyMonitor#Quit().() {} |