forked from arduino/arduino-cli
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdiscovery.go
152 lines (136 loc) · 4.26 KB
/
discovery.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
//
// This file is part of arduino-cli.
//
// Copyright 2018 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 discovery
import (
"encoding/json"
"fmt"
"io"
"os/exec"
"time"
"github.com/arduino/arduino-cli/arduino/cores/packagemanager"
properties "github.com/arduino/go-properties-orderedmap"
"github.com/arduino/arduino-cli/executils"
)
// Discovery is an instance of a discovery tool
type Discovery struct {
in io.WriteCloser
out io.ReadCloser
outJSON *json.Decoder
cmd *exec.Cmd
Timeout time.Duration
}
// BoardPort is a generic port descriptor
type BoardPort struct {
Address string `json:"address"`
Label string `json:"label"`
Prefs *properties.Map `json:"prefs"`
IdentificationPrefs *properties.Map `json:"identificationPrefs"`
Protocol string `json:"protocol"`
ProtocolLabel string `json:"protocolLabel"`
}
type eventJSON struct {
EventType string `json:"eventType,required"`
Ports []*BoardPort `json:"ports"`
}
// NewFromCommandLine creates a new Discovery object
func NewFromCommandLine(args ...string) (*Discovery, error) {
cmd, err := executils.Command(args)
if err != nil {
return nil, fmt.Errorf("creating discovery process: %s", err)
}
disc := &Discovery{Timeout: time.Second}
disc.cmd = cmd
return disc, nil
}
// Start starts the specified discovery
func (d *Discovery) Start() error {
if in, err := d.cmd.StdinPipe(); err == nil {
d.in = in
} else {
return fmt.Errorf("creating stdin pipe for discovery: %s", err)
}
if out, err := d.cmd.StdoutPipe(); err == nil {
d.out = out
d.outJSON = json.NewDecoder(d.out)
} else {
return fmt.Errorf("creating stdout pipe for discovery: %s", err)
}
if err := d.cmd.Start(); err != nil {
return fmt.Errorf("starting discovery process: %s", err)
}
return nil
}
// List retrieve the port list from this discovery
func (d *Discovery) List() ([]*BoardPort, error) {
if _, err := d.in.Write([]byte("LIST\n")); err != nil {
return nil, fmt.Errorf("sending LIST command to discovery: %s", err)
}
var event eventJSON
done := make(chan bool)
timeout := false
go func() {
select {
case <-done:
case <-time.After(d.Timeout):
timeout = true
d.Close()
}
}()
if err := d.outJSON.Decode(&event); err != nil {
if timeout {
return nil, fmt.Errorf("decoding LIST command: timeout")
}
return nil, fmt.Errorf("decoding LIST command: %s", err)
}
done <- true
return event.Ports, nil
}
// Close stops the Discovery and free the resources
func (d *Discovery) Close() error {
// TODO: Send QUIT for safe close or terminate process after a small timeout
if err := d.in.Close(); err != nil {
return fmt.Errorf("closing stdin pipe: %s", err)
}
if err := d.out.Close(); err != nil {
return fmt.Errorf("closing stdout pipe: %s", err)
}
if d.cmd != nil {
d.cmd.Process.Kill()
}
return nil
}
// ExtractDiscoveriesFromPlatforms returns all Discovery from all the installed platforms.
func ExtractDiscoveriesFromPlatforms(pm *packagemanager.PackageManager) map[string]*Discovery {
res := map[string]*Discovery{}
for _, platformRelease := range pm.InstalledPlatformReleases() {
discoveries := platformRelease.Properties.SubTree("discovery").FirstLevelOf()
for name, props := range discoveries {
if pattern, has := props.GetOk("pattern"); has {
props.Merge(platformRelease.Properties)
cmdLine := props.ExpandPropsInString(pattern)
if cmdArgs, err := properties.SplitQuotedString(cmdLine, `"`, false); err != nil {
// TODO
} else if disc, err := NewFromCommandLine(cmdArgs...); err != nil {
// TODO
} else {
res[name] = disc
}
}
}
}
return res
}