From dbfa95684caea6610b879f913eae329d9deef771 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Wed, 30 Oct 2019 10:39:47 +0100 Subject: [PATCH 1/2] Board identification: always check vid.1/pid.1 Even if vid.0/pid.0 is missing. Fix #456 --- arduino/cores/packagemanager/identify.go | 2 +- .../packagemanager/package_manager_test.go | 21 +++++++++++++++++++ .../custom_hardware/test/avr/boards.txt | 17 +++++++++++++++ 3 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 arduino/cores/packagemanager/testdata/custom_hardware/test/avr/boards.txt diff --git a/arduino/cores/packagemanager/identify.go b/arduino/cores/packagemanager/identify.go index 926fa4dd1bf..548d9f257e8 100644 --- a/arduino/cores/packagemanager/identify.go +++ b/arduino/cores/packagemanager/identify.go @@ -55,7 +55,7 @@ func (pm *PackageManager) IdentifyBoard(idProps *properties.Map) []*cores.Board if found { foundBoards = append(foundBoards, board) } - if !again { + if !again && id > 0 { // Always check id 0 and 1 (https://github.com/arduino/arduino-cli/issues/456) break } id++ diff --git a/arduino/cores/packagemanager/package_manager_test.go b/arduino/cores/packagemanager/package_manager_test.go index 37bf940117f..94ffdf5c012 100644 --- a/arduino/cores/packagemanager/package_manager_test.go +++ b/arduino/cores/packagemanager/package_manager_test.go @@ -18,6 +18,7 @@ package packagemanager_test import ( + "fmt" "net/url" "testing" @@ -295,3 +296,23 @@ func TestFindToolsRequiredForBoard(t *testing.T) { } require.Equal(t, bossac18.InstallDir.String(), uploadProperties.Get("runtime.tools.bossac.path")) } + +func TestIdentifyBoard(t *testing.T) { + pm := packagemanager.NewPackageManager(customHardware, customHardware, customHardware, customHardware) + pm.LoadHardwareFromDirectory(customHardware) + + identify := func(vid, pid string) []*cores.Board { + return pm.IdentifyBoard(properties.NewFromHashmap(map[string]string{ + "vid": vid, "pid": pid, + })) + } + require.Equal(t, "[arduino:avr:uno]", fmt.Sprintf("%v", identify("0x2341", "0x0001"))) + + // Check indexed vid/pid format (vid.0/pid.0) + require.Equal(t, "[test:avr:a]", fmt.Sprintf("%v", identify("0x9999", "0x0001"))) + require.Equal(t, "[test:avr:b]", fmt.Sprintf("%v", identify("0x9999", "0x0002"))) + require.Equal(t, "[test:avr:c]", fmt.Sprintf("%v", identify("0x9999", "0x0003"))) + require.Equal(t, "[test:avr:c]", fmt.Sprintf("%v", identify("0x9999", "0x0004"))) + // https://github.com/arduino/arduino-cli/issues/456 + require.Equal(t, "[test:avr:d]", fmt.Sprintf("%v", identify("0x9999", "0x0005"))) +} diff --git a/arduino/cores/packagemanager/testdata/custom_hardware/test/avr/boards.txt b/arduino/cores/packagemanager/testdata/custom_hardware/test/avr/boards.txt new file mode 100644 index 00000000000..76a3f32baf2 --- /dev/null +++ b/arduino/cores/packagemanager/testdata/custom_hardware/test/avr/boards.txt @@ -0,0 +1,17 @@ +a.name=Board A +a.vid=0x9999 +a.pid=0x0001 + +b.name=Board B +b.vid.0=0x9999 +b.pid.0=0x0002 + +c.name=Board C +c.vid.0=0x9999 +c.pid.0=0x0003 +c.vid.1=0x9999 +c.pid.1=0x0004 + +d.name=Board D +d.vid.1=0x9999 +d.pid.1=0x0005 From 6cdc384a372eba10fcb376cb2a3bda3a47ca63e3 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Mon, 4 Nov 2019 16:34:23 +0100 Subject: [PATCH 2/2] Renamed 'again'->'present' and 'found'->'matched' for clarity --- arduino/cores/packagemanager/identify.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/arduino/cores/packagemanager/identify.go b/arduino/cores/packagemanager/identify.go index 548d9f257e8..16e9bed3b89 100644 --- a/arduino/cores/packagemanager/identify.go +++ b/arduino/cores/packagemanager/identify.go @@ -30,7 +30,7 @@ func (pm *PackageManager) IdentifyBoard(idProps *properties.Map) []*cores.Board return []*cores.Board{} } - checkSuffix := func(props *properties.Map, s string) (checked bool, found bool) { + checkSuffix := func(props *properties.Map, s string) (present bool, matched bool) { for k, v1 := range idProps.AsMap() { v2, ok := props.GetOk(k + s) if !ok { @@ -45,17 +45,17 @@ func (pm *PackageManager) IdentifyBoard(idProps *properties.Map) []*cores.Board foundBoards := []*cores.Board{} for _, board := range pm.InstalledBoards() { - if _, found := checkSuffix(board.Properties, ""); found { + if _, matched := checkSuffix(board.Properties, ""); matched { foundBoards = append(foundBoards, board) continue } id := 0 for { - again, found := checkSuffix(board.Properties, fmt.Sprintf(".%d", id)) - if found { + present, matched := checkSuffix(board.Properties, fmt.Sprintf(".%d", id)) + if matched { foundBoards = append(foundBoards, board) } - if !again && id > 0 { // Always check id 0 and 1 (https://github.com/arduino/arduino-cli/issues/456) + if !present && id > 0 { // Always check id 0 and 1 (https://github.com/arduino/arduino-cli/issues/456) break } id++