diff --git a/arduino/discovery/discovery.go b/arduino/discovery/discovery.go deleted file mode 100644 index 4150a90c426..00000000000 --- a/arduino/discovery/discovery.go +++ /dev/null @@ -1,171 +0,0 @@ -// -// 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 license@arduino.cc. -// - -package discovery - -import ( - "encoding/json" - "fmt" - "io" - "os/exec" - "strings" - "sync" - "time" - - "github.com/arduino/arduino-cli/arduino/cores" - "github.com/arduino/arduino-cli/arduino/cores/packagemanager" - "github.com/arduino/arduino-cli/executils" - properties "github.com/arduino/go-properties-orderedmap" -) - -// Discovery is an instance of a discovery tool -type Discovery struct { - sync.Mutex - ID string - in io.WriteCloser - out io.ReadCloser - outJSON *json.Decoder - cmd *exec.Cmd -} - -// 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) - } - return &Discovery{ - ID: strings.Join(args, " "), - cmd: cmd, - }, 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) { - // ensure the connection to the discoverer is unique to avoid messing up - // the messages exchanged - d.Lock() - defer d.Unlock() - - 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(2000 * time.Millisecond): - 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 { - _, _ = d.in.Write([]byte("QUIT\n")) - _ = d.in.Close() - _ = d.out.Close() - timer := time.AfterFunc(time.Second, func() { - _ = d.cmd.Process.Kill() - }) - err := d.cmd.Wait() - _ = timer.Stop() - return err -} - -// ExtractDiscoveriesFromPlatforms returns all Discovery from all the installed platforms. -func ExtractDiscoveriesFromPlatforms(pm *packagemanager.PackageManager) []*Discovery { - res := []*Discovery{} - taken := map[string]bool{} - for _, platformRelease := range pm.InstalledPlatformReleases() { - for _, disc := range ExtractDiscoveriesFromPlatform(platformRelease) { - if taken[disc.ID] { - continue - } - taken[disc.ID] = true - res = append(res, disc) - } - } - return res -} - -// ExtractDiscoveriesFromPlatform returns all Discovery from the specified platform. -func ExtractDiscoveriesFromPlatform(platformRelease *cores.PlatformRelease) []*Discovery { - discoveries := platformRelease.Properties.SubTree("discovery").FirstLevelOf() - - res := []*Discovery{} - for _, 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 = append(res, disc) - } - } - } - - return res -} diff --git a/cli/board/list.go b/cli/board/list.go index 79da263ac62..fdaf4f3fdb0 100644 --- a/cli/board/list.go +++ b/cli/board/list.go @@ -18,7 +18,6 @@ package board import ( - "context" "fmt" "os" "sort" @@ -61,7 +60,7 @@ func runListCommand(cmd *cobra.Command, args []string) { time.Sleep(timeout) } - resp, err := board.List(context.Background(), &rpc.BoardListReq{Instance: instance.CreateInstance()}) + resp, err := board.List(instance.CreateInstance().GetId()) if err != nil { formatter.PrintError(err, "Error detecting boards") os.Exit(errorcodes.ErrNetwork) diff --git a/commands/board/attach.go b/commands/board/attach.go index fecdba8a8e0..220eddc1bde 100644 --- a/commands/board/attach.go +++ b/commands/board/attach.go @@ -37,7 +37,7 @@ import ( // Attach FIXMEDOC func Attach(ctx context.Context, req *rpc.BoardAttachReq, taskCB commands.TaskProgressCB) (*rpc.BoardAttachResp, error) { - pm := commands.GetPackageManager(req) + pm := commands.GetPackageManager(req.GetInstance().GetId()) if pm == nil { return nil, errors.New("invalid instance") } diff --git a/commands/board/details.go b/commands/board/details.go index 4edf1a8cb72..228c44e8d64 100644 --- a/commands/board/details.go +++ b/commands/board/details.go @@ -29,7 +29,7 @@ import ( // Details FIXMEDOC func Details(ctx context.Context, req *rpc.BoardDetailsReq) (*rpc.BoardDetailsResp, error) { - pm := commands.GetPackageManager(req) + pm := commands.GetPackageManager(req.GetInstance().GetId()) if pm == nil { return nil, errors.New("invalid instance") } diff --git a/commands/board/list.go b/commands/board/list.go index 3254a38933b..0f58a6e3852 100644 --- a/commands/board/list.go +++ b/commands/board/list.go @@ -18,44 +18,50 @@ package board import ( - "context" - "errors" - "fmt" - "github.com/arduino/arduino-cli/commands" rpc "github.com/arduino/arduino-cli/rpc/commands" + "github.com/pkg/errors" ) // List FIXMEDOC -func List(ctx context.Context, req *rpc.BoardListReq) (*rpc.BoardListResp, error) { - pm := commands.GetPackageManager(req) +func List(instanceID int32) (*rpc.BoardListResp, error) { + pm := commands.GetPackageManager(instanceID) if pm == nil { return nil, errors.New("invalid instance") } + serialDiscovery, err := commands.NewBuiltinSerialDiscovery(pm) + if err != nil { + return nil, errors.Wrap(err, "unable to instance serial-discovery") + } + + if err := serialDiscovery.Start(); err != nil { + return nil, errors.Wrap(err, "unable to start serial-discovery") + } + defer serialDiscovery.Close() + resp := &rpc.BoardListResp{Ports: []*rpc.DetectedPort{}} - for _, disc := range commands.GetDiscoveries(req) { - ports, err := disc.List() - if err != nil { - fmt.Printf("Error getting port list from discovery %s: %s\n", disc.ID, err) - continue + + ports, err := serialDiscovery.List() + if err != nil { + return nil, errors.Wrap(err, "error getting port list from serial-discovery") + } + + for _, port := range ports { + b := []*rpc.BoardListItem{} + for _, board := range pm.IdentifyBoard(port.IdentificationPrefs) { + b = append(b, &rpc.BoardListItem{ + Name: board.Name(), + FQBN: board.FQBN(), + }) } - for _, port := range ports { - b := []*rpc.BoardListItem{} - for _, board := range pm.IdentifyBoard(port.IdentificationPrefs) { - b = append(b, &rpc.BoardListItem{ - Name: board.Name(), - FQBN: board.FQBN(), - }) - } - p := &rpc.DetectedPort{ - Address: port.Address, - Protocol: port.Protocol, - ProtocolLabel: port.ProtocolLabel, - Boards: b, - } - resp.Ports = append(resp.Ports, p) + p := &rpc.DetectedPort{ + Address: port.Address, + Protocol: port.Protocol, + ProtocolLabel: port.ProtocolLabel, + Boards: b, } + resp.Ports = append(resp.Ports, p) } return resp, nil diff --git a/commands/board/listall.go b/commands/board/listall.go index 6367984c7ec..cfa767f543a 100644 --- a/commands/board/listall.go +++ b/commands/board/listall.go @@ -28,7 +28,7 @@ import ( // ListAll FIXMEDOC func ListAll(ctx context.Context, req *rpc.BoardListAllReq) (*rpc.BoardListAllResp, error) { - pm := commands.GetPackageManager(req) + pm := commands.GetPackageManager(req.GetInstance().GetId()) if pm == nil { return nil, errors.New("invalid instance") } diff --git a/commands/bundled_tools_serial_discovery.go b/commands/bundled_tools_serial_discovery.go index c481bc08a3d..25724064f66 100644 --- a/commands/bundled_tools_serial_discovery.go +++ b/commands/bundled_tools_serial_discovery.go @@ -18,27 +18,31 @@ package commands import ( + "encoding/json" "fmt" + "io" + "os/exec" + "strings" + "sync" + "time" "github.com/arduino/arduino-cli/arduino/cores" "github.com/arduino/arduino-cli/arduino/cores/packagemanager" - "github.com/arduino/arduino-cli/arduino/discovery" "github.com/arduino/arduino-cli/arduino/resources" + "github.com/arduino/arduino-cli/executils" + "github.com/arduino/go-properties-orderedmap" + "github.com/pkg/errors" semver "go.bug.st/relaxed-semver" ) -var serialDiscoveryVersion = semver.ParseRelaxed("0.5.0") - -func loadBuiltinSerialDiscoveryMetadata(pm *packagemanager.PackageManager) { - builtinPackage := pm.Packages.GetOrCreatePackage("builtin") - ctagsTool := builtinPackage.GetOrCreateTool("serial-discovery") - ctagsRel := ctagsTool.GetOrCreateRelease(serialDiscoveryVersion) - ctagsRel.Flavors = []*cores.Flavor{ +var ( + sdVersion = semver.ParseRelaxed("1.0.0") + flavors = []*cores.Flavor{ { OS: "i686-pc-linux-gnu", Resource: &resources.DownloadResource{ - ArchiveFileName: "serial-discovery-linux32-v1.0.0.tar.bz2", - URL: "https://downloads.arduino.cc/tools/serial-discovery-linux32-v1.0.0.tar.bz2", + ArchiveFileName: fmt.Sprintf("serial-discovery-linux32-v%s.tar.bz2", sdVersion), + URL: fmt.Sprintf("https://downloads.arduino.cc/tools/serial-discovery-linux32-v%s.tar.bz2", sdVersion), Size: 1469113, Checksum: "SHA-256:35d96977844ad8d5ca9363e1ae5794450e5f7cf3d29ce7fdfe656b59e7fff725", CachePath: "tools", @@ -47,8 +51,8 @@ func loadBuiltinSerialDiscoveryMetadata(pm *packagemanager.PackageManager) { { OS: "x86_64-pc-linux-gnu", Resource: &resources.DownloadResource{ - ArchiveFileName: "serial-discovery-linux64-v1.0.0.tar.bz2", - URL: "https://downloads.arduino.cc/tools/serial-discovery-linux64-v1.0.0.tar.bz2", + ArchiveFileName: fmt.Sprintf("serial-discovery-linux64-v%s.tar.bz2", sdVersion), + URL: fmt.Sprintf("https://downloads.arduino.cc/tools/serial-discovery-linux64-v%s.tar.bz2", sdVersion), Size: 1503971, Checksum: "SHA-256:1a870d4d823ea6ebec403f63b10a1dbc9c623a6efea5cfa9141fa20045b731e2", CachePath: "tools", @@ -57,8 +61,8 @@ func loadBuiltinSerialDiscoveryMetadata(pm *packagemanager.PackageManager) { { OS: "i686-mingw32", Resource: &resources.DownloadResource{ - ArchiveFileName: "serial-discovery-windows-v1.0.0.zip", - URL: "https://downloads.arduino.cc/tools/serial-discovery-windows-v1.0.0.zip", + ArchiveFileName: fmt.Sprintf("serial-discovery-windows-v%s.zip", sdVersion), + URL: fmt.Sprintf("https://downloads.arduino.cc/tools/serial-discovery-windows-v%s.zip", sdVersion), Size: 1512379, Checksum: "SHA-256:b956128ab27a3a883c938d17cad640ba396876472f2ed25d8e661f12f5d0f584", CachePath: "tools", @@ -67,8 +71,8 @@ func loadBuiltinSerialDiscoveryMetadata(pm *packagemanager.PackageManager) { { OS: "x86_64-apple-darwin", Resource: &resources.DownloadResource{ - ArchiveFileName: "serial-discovery-macosx-v1.0.0.tar.bz2", - URL: "https://downloads.arduino.cc/tools/serial-discovery-macosx-v1.0.0.tar.bz2", + ArchiveFileName: fmt.Sprintf("serial-discovery-macosx-v%s.tar.bz2", sdVersion), + URL: fmt.Sprintf("https://downloads.arduino.cc/tools/serial-discovery-macosx-v%s.tar.bz2", sdVersion), Size: 746132, Checksum: "SHA-256:fcff1b972b70a73cd738facc6d99174d8323293b60c12149c8f6f3084fb2170e", CachePath: "tools", @@ -77,8 +81,8 @@ func loadBuiltinSerialDiscoveryMetadata(pm *packagemanager.PackageManager) { { OS: "arm-linux-gnueabihf", Resource: &resources.DownloadResource{ - ArchiveFileName: "serial-discovery-linuxarm-v1.0.0.tar.bz2", - URL: "https://downloads.arduino.cc/tools/serial-discovery-linuxarm-v1.0.0.tar.bz2", + ArchiveFileName: fmt.Sprintf("serial-discovery-linuxarm-v%s.tar.bz2", sdVersion), + URL: fmt.Sprintf("https://downloads.arduino.cc/tools/serial-discovery-linuxarm-v%s.tar.bz2", sdVersion), Size: 1395174, Checksum: "SHA-256:f196765caa62d38475208c27b3b516e61427d5d3a8ddc6e863acb4e4a3984701", CachePath: "tools", @@ -87,28 +91,141 @@ func loadBuiltinSerialDiscoveryMetadata(pm *packagemanager.PackageManager) { { OS: "arm64-linux-gnueabihf", Resource: &resources.DownloadResource{ - ArchiveFileName: "serial-discovery-linuxarm64-v1.0.0.tar.bz2", - URL: "https://downloads.arduino.cc/tools/serial-discovery-linuxarm64-v1.0.0.tar.bz2", + ArchiveFileName: fmt.Sprintf("serial-discovery-linuxarm64-v%s.tar.bz2", sdVersion), + URL: fmt.Sprintf("https://downloads.arduino.cc/tools/serial-discovery-linuxarm64-v%s.tar.bz2", sdVersion), Size: 1402706, Checksum: "SHA-256:c87010ed670254c06ac7abbc4daf7446e4e17f1945a75fc2602dd5930835dd25", CachePath: "tools", }, }, } +) + +// SerialDiscovery is an instance of a discovery tool +type SerialDiscovery struct { + sync.Mutex + ID string + in io.WriteCloser + out io.ReadCloser + outJSON *json.Decoder + cmd *exec.Cmd } -func getBuiltinSerialDiscoveryTool(pm *packagemanager.PackageManager) (*cores.ToolRelease, error) { - loadBuiltinSerialDiscoveryMetadata(pm) - return pm.Package("builtin").Tool("serial-discovery").Release(serialDiscoveryVersion).Get() +// 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"` } -func newBuiltinSerialDiscovery(pm *packagemanager.PackageManager) (*discovery.Discovery, error) { +// NewBuiltinSerialDiscovery returns a wrapper to control the serial-discovery program +func NewBuiltinSerialDiscovery(pm *packagemanager.PackageManager) (*SerialDiscovery, error) { t, err := getBuiltinSerialDiscoveryTool(pm) if err != nil { return nil, err } + if !t.IsInstalled() { return nil, fmt.Errorf("missing serial-discovery tool") } - return discovery.NewFromCommandLine(t.InstallDir.Join("serial-discovery").String()) + + cmdArgs := []string{ + t.InstallDir.Join("serial-discovery").String(), + } + + cmd, err := executils.Command(cmdArgs) + if err != nil { + return nil, errors.Wrap(err, "creating discovery process") + } + + return &SerialDiscovery{ + ID: strings.Join(cmdArgs, " "), + cmd: cmd, + }, nil +} + +// Start starts the specified discovery +func (d *SerialDiscovery) 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 *SerialDiscovery) List() ([]*BoardPort, error) { + // ensure the connection to the discoverer is unique to avoid messing up + // the messages exchanged + d.Lock() + defer d.Unlock() + + if d.cmd.Process == nil { + return nil, fmt.Errorf("discovery hasn't started") + } + + 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(2000 * time.Millisecond): + 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 *SerialDiscovery) Close() error { + _, _ = d.in.Write([]byte("QUIT\n")) + _ = d.in.Close() + _ = d.out.Close() + timer := time.AfterFunc(time.Second, func() { + _ = d.cmd.Process.Kill() + }) + err := d.cmd.Wait() + _ = timer.Stop() + return err +} + +func getBuiltinSerialDiscoveryTool(pm *packagemanager.PackageManager) (*cores.ToolRelease, error) { + builtinPackage := pm.Packages.GetOrCreatePackage("builtin") + ctagsTool := builtinPackage.GetOrCreateTool("serial-discovery") + ctagsRel := ctagsTool.GetOrCreateRelease(sdVersion) + ctagsRel.Flavors = flavors + return pm.Package("builtin").Tool("serial-discovery").Release(sdVersion).Get() } diff --git a/commands/compile/compile.go b/commands/compile/compile.go index 5f7d819d8ce..4c2815aac39 100644 --- a/commands/compile/compile.go +++ b/commands/compile/compile.go @@ -42,7 +42,7 @@ import ( // Compile FIXMEDOC func Compile(ctx context.Context, req *rpc.CompileReq, outStream, errStream io.Writer, config *configs.Configuration, debug bool) (*rpc.CompileResp, error) { - pm := commands.GetPackageManager(req) + pm := commands.GetPackageManager(req.GetInstance().GetId()) if pm == nil { return nil, errors.New("invalid instance") } diff --git a/commands/core/download.go b/commands/core/download.go index 705e4a78e2a..30f3c6acb4f 100644 --- a/commands/core/download.go +++ b/commands/core/download.go @@ -32,7 +32,7 @@ import ( // PlatformDownload FIXMEDOC func PlatformDownload(ctx context.Context, req *rpc.PlatformDownloadReq, downloadCB commands.DownloadProgressCB, downloaderHeaders http.Header) (*rpc.PlatformDownloadResp, error) { - pm := commands.GetPackageManager(req) + pm := commands.GetPackageManager(req.GetInstance().GetId()) if pm == nil { return nil, errors.New("invalid instance") } diff --git a/commands/core/install.go b/commands/core/install.go index ed00d652254..5a364f91b26 100644 --- a/commands/core/install.go +++ b/commands/core/install.go @@ -33,7 +33,7 @@ import ( func PlatformInstall(ctx context.Context, req *rpc.PlatformInstallReq, downloadCB commands.DownloadProgressCB, taskCB commands.TaskProgressCB, downloaderHeaders http.Header) (*rpc.PlatformInstallResp, error) { - pm := commands.GetPackageManager(req) + pm := commands.GetPackageManager(req.GetInstance().GetId()) if pm == nil { return nil, errors.New("invalid instance") } diff --git a/commands/core/search.go b/commands/core/search.go index 2622d3c39a0..961773840a4 100644 --- a/commands/core/search.go +++ b/commands/core/search.go @@ -30,7 +30,7 @@ import ( // PlatformSearch FIXMEDOC func PlatformSearch(ctx context.Context, req *rpc.PlatformSearchReq) (*rpc.PlatformSearchResp, error) { - pm := commands.GetPackageManager(req) + pm := commands.GetPackageManager(req.GetInstance().GetId()) if pm == nil { return nil, errors.New("invalid instance") } diff --git a/commands/core/uninstall.go b/commands/core/uninstall.go index db79e8a17d3..524e21e6aeb 100644 --- a/commands/core/uninstall.go +++ b/commands/core/uninstall.go @@ -30,7 +30,7 @@ import ( // PlatformUninstall FIXMEDOC func PlatformUninstall(ctx context.Context, req *rpc.PlatformUninstallReq, taskCB commands.TaskProgressCB) (*rpc.PlatformUninstallResp, error) { - pm := commands.GetPackageManager(req) + pm := commands.GetPackageManager(req.GetInstance().GetId()) if pm == nil { return nil, errors.New("invalid instance") } diff --git a/commands/core/upgrade.go b/commands/core/upgrade.go index 5dde73907b3..a5fd4948c64 100644 --- a/commands/core/upgrade.go +++ b/commands/core/upgrade.go @@ -38,7 +38,7 @@ var ( func PlatformUpgrade(ctx context.Context, req *rpc.PlatformUpgradeReq, downloadCB commands.DownloadProgressCB, taskCB commands.TaskProgressCB, downloaderHeaders http.Header) (*rpc.PlatformUpgradeResp, error) { - pm := commands.GetPackageManager(req) + pm := commands.GetPackageManager(req.GetInstance().GetId()) if pm == nil { return nil, errors.New("invalid instance") } diff --git a/commands/daemon/daemon.go b/commands/daemon/daemon.go index cf3eab2adc2..aa5feafc5f0 100644 --- a/commands/daemon/daemon.go +++ b/commands/daemon/daemon.go @@ -49,7 +49,7 @@ func (s *ArduinoCoreServerImpl) BoardDetails(ctx context.Context, req *rpc.Board // BoardList FIXMEDOC func (s *ArduinoCoreServerImpl) BoardList(ctx context.Context, req *rpc.BoardListReq) (*rpc.BoardListResp, error) { - return board.List(ctx, req) + return board.List(req.GetInstance().GetId()) } // BoardListAll FIXMEDOC diff --git a/commands/discoveries.go b/commands/discoveries.go deleted file mode 100644 index 0e5f9706a63..00000000000 --- a/commands/discoveries.go +++ /dev/null @@ -1,70 +0,0 @@ -// -// 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 license@arduino.cc. -// - -package commands - -import ( - "sync" - - "github.com/arduino/arduino-cli/arduino/discovery" -) - -type sharedDiscovery struct { - discovery *discovery.Discovery - referenceCount int -} - -// this map contains all the running pluggable-discoveries instances -var sharedDiscoveries = map[string]*sharedDiscovery{} -var sharedDiscoveriesMutex sync.Mutex - -// StartSharedDiscovery starts a discovery or returns the instance of an already -// started shared discovery. -func StartSharedDiscovery(disc *discovery.Discovery) (*discovery.Discovery, error) { - sharedDiscoveriesMutex.Lock() - defer sharedDiscoveriesMutex.Unlock() - - instance, started := sharedDiscoveries[disc.ID] - if started { - instance.referenceCount++ - return instance.discovery, nil - } - sharedDiscoveries[disc.ID] = &sharedDiscovery{ - discovery: disc, - referenceCount: 1, - } - err := disc.Start() - return disc, err -} - -// StopSharedDiscovery will dispose an instance of a shared discovery if it is -// no more needed. -func StopSharedDiscovery(disc *discovery.Discovery) error { - sharedDiscoveriesMutex.Lock() - defer sharedDiscoveriesMutex.Unlock() - - instance, started := sharedDiscoveries[disc.ID] - if started { - instance.referenceCount-- - - if instance.referenceCount == 0 { - delete(sharedDiscoveries, disc.ID) - return instance.discovery.Close() - } - } - return nil -} diff --git a/commands/instances.go b/commands/instances.go index b937d717e6d..3f6ef908e60 100644 --- a/commands/instances.go +++ b/commands/instances.go @@ -29,7 +29,6 @@ import ( "github.com/arduino/arduino-cli/arduino/cores" "github.com/arduino/arduino-cli/arduino/cores/packageindex" "github.com/arduino/arduino-cli/arduino/cores/packagemanager" - "github.com/arduino/arduino-cli/arduino/discovery" "github.com/arduino/arduino-cli/arduino/libraries" "github.com/arduino/arduino-cli/arduino/libraries/librariesmanager" "github.com/arduino/arduino-cli/configs" @@ -52,7 +51,6 @@ type CoreInstance struct { PackageManager *packagemanager.PackageManager lm *librariesmanager.LibrariesManager getLibOnly bool - discoveries []*discovery.Discovery } // InstanceContainer FIXMEDOC @@ -66,9 +64,10 @@ func GetInstance(id int32) *CoreInstance { return instances[id] } -// GetPackageManager FIXMEDOC -func GetPackageManager(req InstanceContainer) *packagemanager.PackageManager { - i, ok := instances[req.GetInstance().GetId()] +// GetPackageManager returns a PackageManager for the given ID, or nil if +// ID doesn't exist +func GetPackageManager(id int32) *packagemanager.PackageManager { + i, ok := instances[id] if !ok { return nil } @@ -84,15 +83,6 @@ func GetLibraryManager(instanceID int32) *librariesmanager.LibrariesManager { return i.lm } -// GetDiscoveries FIXMEDOC -func GetDiscoveries(req InstanceContainer) []*discovery.Discovery { - i, ok := instances[req.GetInstance().GetId()] - if !ok { - return nil - } - return i.discoveries -} - func (instance *CoreInstance) installToolIfMissing(tool *cores.ToolRelease, downloadCB DownloadProgressCB, taskCB TaskProgressCB, downloaderHeaders http.Header) (bool, error) { if tool.IsInstalled() { @@ -133,32 +123,6 @@ func (instance *CoreInstance) checkForBuiltinTools(downloadCB DownloadProgressCB return nil } -func (instance *CoreInstance) startDiscoveries() error { - serialDiscovery, err := newBuiltinSerialDiscovery(instance.PackageManager) - if err != nil { - return fmt.Errorf("starting serial discovery: %s", err) - } - - discoveriesToStop := instance.discoveries - discoveriesToStart := append( - discovery.ExtractDiscoveriesFromPlatforms(instance.PackageManager), - serialDiscovery, - ) - - instance.discoveries = []*discovery.Discovery{} - for _, disc := range discoveriesToStart { - sharedDisc, err := StartSharedDiscovery(disc) - if err != nil { - return fmt.Errorf("starting discovery: %s", err) - } - instance.discoveries = append(instance.discoveries, sharedDisc) - } - for _, disc := range discoveriesToStop { - StopSharedDiscovery(disc) - } - return nil -} - // Init FIXMEDOC func Init(ctx context.Context, req *rpc.InitReq, downloadCB DownloadProgressCB, taskCB TaskProgressCB, downloaderHeaders http.Header) (*rpc.InitResp, error) { inConfig := req.GetConfiguration() @@ -201,11 +165,6 @@ func Init(ctx context.Context, req *rpc.InitReq, downloadCB DownloadProgressCB, return nil, err } - if err := instance.startDiscoveries(); err != nil { - // TODO: handle discovery errors - fmt.Println(err) - } - return &rpc.InitResp{ Instance: &rpc.Instance{Id: handle}, PlatformsIndexErrors: reqPltIndex, @@ -220,10 +179,6 @@ func Destroy(ctx context.Context, req *rpc.DestroyReq) (*rpc.DestroyResp, error) return nil, fmt.Errorf("invalid handle") } - for _, disc := range GetDiscoveries(req) { - StopSharedDiscovery(disc) - } - delete(instances, id) return &rpc.DestroyResp{}, nil } @@ -313,8 +268,6 @@ func Rescan(instanceID int32) (*rpc.RescanResp, error) { coreInstance.PackageManager = pm coreInstance.lm = lm - coreInstance.startDiscoveries() - return &rpc.RescanResp{ PlatformsIndexErrors: reqPltIndex, LibrariesIndexError: reqLibIndex, diff --git a/commands/upload/upload.go b/commands/upload/upload.go index 3dcb1fef4c0..446d6036670 100644 --- a/commands/upload/upload.go +++ b/commands/upload/upload.go @@ -71,7 +71,7 @@ func Upload(ctx context.Context, req *rpc.UploadReq, outStream io.Writer, errStr return nil, fmt.Errorf("incorrect FQBN: %s", err) } - pm := commands.GetPackageManager(req) + pm := commands.GetPackageManager(req.GetInstance().GetId()) // Find target board and board properties _, _, board, boardProperties, _, err := pm.ResolveFQBN(fqbn)