Skip to content

Commit 7662cfa

Browse files
committed
discovery: first implementation (WIP)
1 parent 09b4da6 commit 7662cfa

File tree

1 file changed

+97
-0
lines changed

1 file changed

+97
-0
lines changed

arduino/discovery/discovery.go

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
//
2+
// This file is part of arduino-cli.
3+
//
4+
// Copyright 2018 ARDUINO SA (http://www.arduino.cc/)
5+
//
6+
// This software is released under the GNU General Public License version 3,
7+
// which covers the main part of arduino-cli.
8+
// The terms of this license can be found at:
9+
// https://www.gnu.org/licenses/gpl-3.0.en.html
10+
//
11+
// You can be released from the requirements of the above licenses by purchasing
12+
// a commercial license. Buying such a license is mandatory if you want to modify or
13+
// otherwise use the software for commercial activities involving the Arduino
14+
// software without disclosing the source code of your own applications. To purchase
15+
// a commercial license, send an email to license@arduino.cc.
16+
//
17+
18+
package discovery
19+
20+
import (
21+
"encoding/json"
22+
"fmt"
23+
"io"
24+
25+
properties "github.com/arduino/go-properties-orderedmap"
26+
27+
"github.com/arduino/arduino-cli/executils"
28+
)
29+
30+
// Discovery is an instance of a discovery tool
31+
type Discovery struct {
32+
in io.WriteCloser
33+
out io.ReadCloser
34+
outJSON *json.Decoder
35+
}
36+
37+
// BoardPort is a generic port descriptor
38+
type BoardPort struct {
39+
Address string `json:"address"`
40+
Label string `json:"label"`
41+
Prefs *properties.Map `json:"prefs"`
42+
IdentificationPrefs *properties.Map `json:"identificationPrefs"`
43+
Protocol string `json:"protocol"`
44+
ProtocolLabel string `json:"protocolLabel"`
45+
}
46+
47+
type eventJSON struct {
48+
EventType string `json:"eventType,required"`
49+
Ports []*BoardPort `json:"ports"`
50+
}
51+
52+
// NewFromCommandLine creates a new Discovery object
53+
func NewFromCommandLine(args ...string) (*Discovery, error) {
54+
cmd, err := executils.Command(args)
55+
if err != nil {
56+
return nil, fmt.Errorf("creating discovery process: %s", err)
57+
}
58+
disc := &Discovery{}
59+
if in, err := cmd.StdinPipe(); err == nil {
60+
disc.in = in
61+
} else {
62+
return nil, fmt.Errorf("creating stdin pipe for discovery: %s", err)
63+
}
64+
if out, err := cmd.StdoutPipe(); err == nil {
65+
disc.out = out
66+
disc.outJSON = json.NewDecoder(disc.out)
67+
} else {
68+
return nil, fmt.Errorf("creating stdout pipe for discovery: %s", err)
69+
}
70+
if err := cmd.Start(); err != nil {
71+
return nil, fmt.Errorf("starting discovery: %s", err)
72+
}
73+
return disc, nil
74+
}
75+
76+
// List retrieve the port list from this discovery
77+
func (d *Discovery) List() ([]*BoardPort, error) {
78+
if _, err := d.in.Write([]byte("LIST\n")); err != nil {
79+
return nil, fmt.Errorf("sending LIST command to discovery: %s", err)
80+
}
81+
var event eventJSON
82+
if err := d.outJSON.Decode(&event); err != nil {
83+
return nil, fmt.Errorf("decoding LIST command: %s", err)
84+
}
85+
return event.Ports, nil
86+
}
87+
88+
// Close stops the Discovery and free the resources
89+
func (d *Discovery) Close() error {
90+
if err := d.in.Close(); err != nil {
91+
return fmt.Errorf("closing stdin pipe: %s", err)
92+
}
93+
if err := d.out.Close(); err != nil {
94+
return fmt.Errorf("closing stdout pipe: %s", err)
95+
}
96+
return nil
97+
}

0 commit comments

Comments
 (0)