15
15
// a commercial license, send an email to license@arduino.cc.
16
16
//
17
17
18
+ // discovery is a library for handling the Arduino Pluggable-Discovery protocol
19
+ // (https://github.com/arduino/tooling-rfcs/blob/main/RFCs/0002-pluggable-discovery.md#pluggable-discovery-api-via-stdinstdout)
20
+ //
21
+ // The library implements the state machine and the parsing logic to communicate with a pluggable-discovery client.
22
+ // All the commands issued by the client are conveniently translated into function calls, in particular
23
+ // the Discovery interface are the only functions that must be implemented to get a fully working pluggable discovery
24
+ // using this library.
25
+ //
26
+ // A usage example is provided in the dummy-discovery package.
18
27
package discovery
19
28
20
29
import (
@@ -30,6 +39,7 @@ import (
30
39
"github.com/arduino/go-properties-orderedmap"
31
40
)
32
41
42
+ // Port is a descriptor for a board port
33
43
type Port struct {
34
44
Address string `json:"address"`
35
45
AddressLabel string `json:"label,omitempty"`
@@ -38,16 +48,39 @@ type Port struct {
38
48
Properties * properties.Map `json:"properties,omitempty"`
39
49
}
40
50
41
- type EventCallback func (event string , port * Port )
42
-
51
+ // Discovery is an interface that represents the business logic that
52
+ // a pluggable discovery must implement. The communication protocol
53
+ // is completely hidden and it's handled by a DiscoveryServer.
43
54
type Discovery interface {
55
+ // Hello is called once at startup to provide the userAgent string
56
+ // and the protocolVersion negotiated with the client.
44
57
Hello (userAgent string , protocolVersion int ) error
58
+
59
+ // Start is called to start the discovery internal subroutines.
45
60
Start () error
46
- Stop () error
47
- List () ([]* Port , error )
61
+
62
+ // List returns the list of the currently available ports. It works
63
+ // only after a Start.
64
+ List () (portList []* Port , err error )
65
+
66
+ // StartSync is called to put the discovery in event mode. When the
67
+ // function returns the discovery must send port events ("add" or "remove")
68
+ // using the eventCB function.
48
69
StartSync (eventCB EventCallback ) (chan <- bool , error )
70
+
71
+ // Stop stops the discovery internal subroutines. If the discovery is
72
+ // in event mode it must stop sending events through the eventCB previously
73
+ // set.
74
+ Stop () error
49
75
}
50
76
77
+ // EventCallback is a callback function to call to transmit port
78
+ // metadata when the discovery is in "sync" mode and a new event
79
+ // is detected.
80
+ type EventCallback func (event string , port * Port )
81
+
82
+ // A DiscoveryServer is a pluggable discovery protocol handler,
83
+ // it must be created using the NewDiscoveryServer function.
51
84
type DiscoveryServer struct {
52
85
impl Discovery
53
86
out io.Writer
@@ -60,12 +93,20 @@ type DiscoveryServer struct {
60
93
syncCloseChan chan <- bool
61
94
}
62
95
96
+ // NewDiscoveryServer creates a new discovery server backed by the
97
+ // provided pluggable discovery implementation. To start the server
98
+ // use the Run method.
63
99
func NewDiscoveryServer (impl Discovery ) * DiscoveryServer {
64
100
return & DiscoveryServer {
65
101
impl : impl ,
66
102
}
67
103
}
68
104
105
+ // Run starts the protocol handling loop on the given input and
106
+ // output stream, usually `os.Stdin` and `os.Stdout` are used.
107
+ // The function blocks until the `QUIT` command is received or
108
+ // the input stream is closed. In case of IO error the error is
109
+ // returned.
69
110
func (d * DiscoveryServer ) Run (in io.Reader , out io.Writer ) error {
70
111
d .out = out
71
112
reader := bufio .NewReader (in )
0 commit comments