forked from arduino/arduino-cli
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
100 lines (85 loc) · 2.44 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
//
// This file is part arduino-cli.
//
// Copyright 2023 ARDUINO SA (http://www.arduino.cc/)
//
// This software is released under the GNU General Public License version 3,
// which covers the main part of arduino-cli.
// The terms of this license can be found at:
// https://www.gnu.org/licenses/gpl-3.0.en.html
//
// You can be released from the requirements of the above licenses by purchasing
// a commercial license. Buying such a license is mandatory if you want to modify or
// otherwise use the software for commercial activities involving the Arduino
// software without disclosing the source code of your own applications. To purchase
// a commercial license, send an email to [email protected].
//
package main
import (
"errors"
"os"
"time"
"github.com/arduino/go-properties-orderedmap"
discovery "github.com/arduino/pluggable-discovery-protocol-handler/v2"
)
type mockSerialDiscovery struct {
startSyncCount int
closeChan chan<- bool
}
func main() {
dummy := &mockSerialDiscovery{}
server := discovery.NewServer(dummy)
if err := server.Run(os.Stdin, os.Stdout); err != nil {
os.Exit(1)
}
}
// Hello does nothing here...
func (d *mockSerialDiscovery) Hello(userAgent string, protocol int) error {
return nil
}
// Quit does nothing here...
func (d *mockSerialDiscovery) Quit() {}
// Stop terminates the discovery loop
func (d *mockSerialDiscovery) Stop() error {
if d.closeChan != nil {
d.closeChan <- true
close(d.closeChan)
d.closeChan = nil
}
return nil
}
// StartSync starts the goroutine that generates fake Ports.
func (d *mockSerialDiscovery) StartSync(eventCB discovery.EventCallback, errorCB discovery.ErrorCallback) error {
// Every 5 starts produce an error
d.startSyncCount++
if d.startSyncCount%5 == 0 {
return errors.New("could not start_sync every 5 times")
}
c := make(chan bool)
d.closeChan = c
// Start asynchronous event emitter
go func() {
var closeChan <-chan bool = c
// Output initial port state
eventCB("add", &discovery.Port{
Address: "/dev/ttyCIAO",
AddressLabel: "Mocked Serial port",
Protocol: "serial",
ProtocolLabel: "Serial",
HardwareID: "123456",
Properties: properties.NewFromHashmap(map[string]string{
"vid": "0x2341",
"pid": "0x0041",
"serial": "123456",
}),
})
select {
case <-closeChan:
return
case <-time.After(5 * time.Second):
errorCB("unrecoverable error, cannot send more events")
}
<-closeChan
}()
return nil
}