forked from arduino/arduino-cli
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathport.go
165 lines (150 loc) · 5.86 KB
/
port.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
153
154
155
156
157
158
159
160
161
162
163
164
165
// This file is part of arduino-cli.
//
// Copyright 2020 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 arguments
import (
"fmt"
"time"
"github.com/arduino/arduino-cli/arduino"
"github.com/arduino/arduino-cli/arduino/discovery"
"github.com/arduino/arduino-cli/commands"
"github.com/arduino/arduino-cli/commands/board"
"github.com/arduino/arduino-cli/internal/cli/feedback"
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
// Port contains the port arguments result.
// This is useful so all flags used by commands that need
// this information are consistent with each other.
type Port struct {
address string
protocol string
timeout DiscoveryTimeout
}
// AddToCommand adds the flags used to set port and protocol to the specified Command
func (p *Port) AddToCommand(cmd *cobra.Command) {
cmd.Flags().StringVarP(&p.address, "port", "p", "", tr("Upload port address, e.g.: COM3 or /dev/ttyACM2"))
cmd.RegisterFlagCompletionFunc("port", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return GetConnectedBoards(), cobra.ShellCompDirectiveDefault
})
cmd.Flags().StringVarP(&p.protocol, "protocol", "l", "", tr("Upload port protocol, e.g: serial"))
cmd.RegisterFlagCompletionFunc("protocol", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return GetInstalledProtocols(), cobra.ShellCompDirectiveDefault
})
p.timeout.AddToCommand(cmd)
}
// GetPortAddressAndProtocol returns only the port address and the port protocol
// without any other port metadata obtained from the discoveries.
// This method allows will bypass the discoveries if:
// - a nil instance is passed: in this case the plain port and protocol arguments are returned (even if empty)
// - a protocol is specified: in this case the discoveries are not needed to autodetect the protocol.
func (p *Port) GetPortAddressAndProtocol(instance *rpc.Instance, defaultAddress, defaultProtocol string) (string, string, error) {
if p.protocol != "" || instance == nil {
return p.address, p.protocol, nil
}
port, err := p.GetPort(instance, defaultAddress, defaultProtocol)
if err != nil {
return "", "", err
}
return port.Address, port.Protocol, nil
}
// GetPort returns the Port obtained by parsing command line arguments.
// The extra metadata for the ports is obtained using the pluggable discoveries.
func (p *Port) GetPort(instance *rpc.Instance, defaultAddress, defaultProtocol string) (*discovery.Port, error) {
// TODO: REMOVE discovery from here (use board.List instead)
address := p.address
protocol := p.protocol
if address == "" && (defaultAddress != "" || defaultProtocol != "") {
address, protocol = defaultAddress, defaultProtocol
}
if address == "" {
// If no address is provided we assume the user is trying to upload
// to a board that supports a tool that automatically detects
// the attached board without specifying explictly a port.
// Tools that work this way must be specified using the property
// "BOARD_ID.upload.tool.default" in the platform's boards.txt.
return &discovery.Port{
Protocol: "default",
}, nil
}
logrus.WithField("port", address).Tracef("Upload port")
// FIXME: We must not access PackageManager directly here but use one of the commands.* functions
pme, release := commands.GetPackageManagerExplorer(&rpc.BoardListAllRequest{Instance: instance})
if pme == nil {
return nil, &arduino.InvalidInstanceError{}
}
defer release()
dm := pme.DiscoveryManager()
watcher, err := dm.Watch()
if err != nil {
return nil, err
}
defer watcher.Close()
deadline := time.After(p.timeout.Get())
for {
select {
case portEvent := <-watcher.Feed():
if portEvent.Type != "add" {
continue
}
port := portEvent.Port
if (protocol == "" || protocol == port.Protocol) && address == port.Address {
return port, nil
}
case <-deadline:
// No matching port found
if protocol == "" {
return &discovery.Port{
Address: address,
Protocol: "serial",
}, nil
}
return nil, fmt.Errorf(tr("port not found: %[1]s %[2]s"), address, protocol)
}
}
}
// GetSearchTimeout returns the timeout
func (p *Port) GetSearchTimeout() time.Duration {
return p.timeout.Get()
}
// DetectFQBN tries to identify the board connected to the port and returns the
// discovered Port object together with the FQBN. If the port does not match
// exactly 1 board,
func (p *Port) DetectFQBN(inst *rpc.Instance) (string, *rpc.Port) {
detectedPorts, _, err := board.List(&rpc.BoardListRequest{
Instance: inst,
Timeout: p.timeout.Get().Milliseconds(),
})
if err != nil {
feedback.Fatal(tr("Error during FQBN detection: %v", err), feedback.ErrGeneric)
}
for _, detectedPort := range detectedPorts {
port := detectedPort.GetPort()
if p.address != port.GetAddress() {
continue
}
if p.protocol != "" && p.protocol != port.GetProtocol() {
continue
}
if len(detectedPort.MatchingBoards) > 1 {
feedback.FatalError(&arduino.MultipleBoardsDetectedError{Port: port}, feedback.ErrBadArgument)
}
if len(detectedPort.MatchingBoards) == 0 {
feedback.FatalError(&arduino.NoBoardsDetectedError{Port: port}, feedback.ErrBadArgument)
}
return detectedPort.MatchingBoards[0].Fqbn, port
}
return "", nil
}