|
| 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 | + "os/exec" |
| 25 | + "time" |
| 26 | + |
| 27 | + "github.com/arduino/arduino-cli/arduino/cores/packagemanager" |
| 28 | + |
| 29 | + properties "github.com/arduino/go-properties-orderedmap" |
| 30 | + |
| 31 | + "github.com/arduino/arduino-cli/executils" |
| 32 | +) |
| 33 | + |
| 34 | +// Discovery is an instance of a discovery tool |
| 35 | +type Discovery struct { |
| 36 | + in io.WriteCloser |
| 37 | + out io.ReadCloser |
| 38 | + outJSON *json.Decoder |
| 39 | + cmd *exec.Cmd |
| 40 | + Timeout time.Duration |
| 41 | +} |
| 42 | + |
| 43 | +// BoardPort is a generic port descriptor |
| 44 | +type BoardPort struct { |
| 45 | + Address string `json:"address"` |
| 46 | + Label string `json:"label"` |
| 47 | + Prefs *properties.Map `json:"prefs"` |
| 48 | + IdentificationPrefs *properties.Map `json:"identificationPrefs"` |
| 49 | + Protocol string `json:"protocol"` |
| 50 | + ProtocolLabel string `json:"protocolLabel"` |
| 51 | +} |
| 52 | + |
| 53 | +type eventJSON struct { |
| 54 | + EventType string `json:"eventType,required"` |
| 55 | + Ports []*BoardPort `json:"ports"` |
| 56 | +} |
| 57 | + |
| 58 | +// NewFromCommandLine creates a new Discovery object |
| 59 | +func NewFromCommandLine(args ...string) (*Discovery, error) { |
| 60 | + cmd, err := executils.Command(args) |
| 61 | + if err != nil { |
| 62 | + return nil, fmt.Errorf("creating discovery process: %s", err) |
| 63 | + } |
| 64 | + disc := &Discovery{Timeout: time.Second} |
| 65 | + disc.cmd = cmd |
| 66 | + return disc, nil |
| 67 | +} |
| 68 | + |
| 69 | +// Start starts the specified discovery |
| 70 | +func (d *Discovery) Start() error { |
| 71 | + if in, err := d.cmd.StdinPipe(); err == nil { |
| 72 | + d.in = in |
| 73 | + } else { |
| 74 | + return fmt.Errorf("creating stdin pipe for discovery: %s", err) |
| 75 | + } |
| 76 | + if out, err := d.cmd.StdoutPipe(); err == nil { |
| 77 | + d.out = out |
| 78 | + d.outJSON = json.NewDecoder(d.out) |
| 79 | + } else { |
| 80 | + return fmt.Errorf("creating stdout pipe for discovery: %s", err) |
| 81 | + } |
| 82 | + if err := d.cmd.Start(); err != nil { |
| 83 | + return fmt.Errorf("starting discovery process: %s", err) |
| 84 | + } |
| 85 | + return nil |
| 86 | +} |
| 87 | + |
| 88 | +// List retrieve the port list from this discovery |
| 89 | +func (d *Discovery) List() ([]*BoardPort, error) { |
| 90 | + if _, err := d.in.Write([]byte("LIST\n")); err != nil { |
| 91 | + return nil, fmt.Errorf("sending LIST command to discovery: %s", err) |
| 92 | + } |
| 93 | + var event eventJSON |
| 94 | + done := make(chan bool) |
| 95 | + timeout := false |
| 96 | + go func() { |
| 97 | + select { |
| 98 | + case <-done: |
| 99 | + case <-time.After(d.Timeout): |
| 100 | + timeout = true |
| 101 | + d.Close() |
| 102 | + } |
| 103 | + }() |
| 104 | + if err := d.outJSON.Decode(&event); err != nil { |
| 105 | + if timeout { |
| 106 | + return nil, fmt.Errorf("decoding LIST command: timeout") |
| 107 | + } |
| 108 | + return nil, fmt.Errorf("decoding LIST command: %s", err) |
| 109 | + } |
| 110 | + done <- true |
| 111 | + return event.Ports, nil |
| 112 | +} |
| 113 | + |
| 114 | +// Close stops the Discovery and free the resources |
| 115 | +func (d *Discovery) Close() error { |
| 116 | + // TODO: Send QUIT for safe close or terminate process after a small timeout |
| 117 | + if err := d.in.Close(); err != nil { |
| 118 | + return fmt.Errorf("closing stdin pipe: %s", err) |
| 119 | + } |
| 120 | + if err := d.out.Close(); err != nil { |
| 121 | + return fmt.Errorf("closing stdout pipe: %s", err) |
| 122 | + } |
| 123 | + if d.cmd != nil { |
| 124 | + d.cmd.Process.Kill() |
| 125 | + } |
| 126 | + return nil |
| 127 | +} |
| 128 | + |
| 129 | +// ExtractDiscoveriesFromPlatforms returns all Discovery from all the installed platforms. |
| 130 | +func ExtractDiscoveriesFromPlatforms(pm *packagemanager.PackageManager) map[string]*Discovery { |
| 131 | + res := map[string]*Discovery{} |
| 132 | + |
| 133 | + for _, platformRelease := range pm.InstalledPlatformReleases() { |
| 134 | + discoveries := platformRelease.Properties.SubTree("discovery").FirstLevelOf() |
| 135 | + |
| 136 | + for name, props := range discoveries { |
| 137 | + if pattern, has := props.GetOk("pattern"); has { |
| 138 | + props.Merge(platformRelease.Properties) |
| 139 | + cmdLine := props.ExpandPropsInString(pattern) |
| 140 | + if cmdArgs, err := properties.SplitQuotedString(cmdLine, `"`, false); err != nil { |
| 141 | + // TODO |
| 142 | + } else if disc, err := NewFromCommandLine(cmdArgs...); err != nil { |
| 143 | + // TODO |
| 144 | + } else { |
| 145 | + res[name] = disc |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + return res |
| 152 | +} |
0 commit comments