Skip to content

Commit 093b044

Browse files
committed
discovery: Added method to identify boards from BoardPort identification properties
1 parent e95a6da commit 093b044

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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 packagemanager
19+
20+
import (
21+
"fmt"
22+
23+
"github.com/arduino/arduino-cli/arduino/cores"
24+
properties "github.com/arduino/go-properties-orderedmap"
25+
)
26+
27+
// IdentifyBoard returns a list of baords matching the provided identification properties.
28+
func (pm *PackageManager) IdentifyBoard(idProps *properties.Map) []*cores.Board {
29+
if idProps.Size() == 0 {
30+
return []*cores.Board{}
31+
}
32+
33+
checkSuffix := func(props *properties.Map, s string) (checked bool, found bool) {
34+
for k, v1 := range idProps.AsMap() {
35+
v2, ok := props.GetOk(k + s)
36+
if !ok {
37+
return false, false
38+
}
39+
if v1 != v2 {
40+
return true, false
41+
}
42+
}
43+
return false, true
44+
}
45+
46+
foundBoards := []*cores.Board{}
47+
for _, board := range pm.InstalledBoards() {
48+
if _, found := checkSuffix(board.Properties, ""); found {
49+
foundBoards = append(foundBoards, board)
50+
continue
51+
}
52+
id := 0
53+
for {
54+
again, found := checkSuffix(board.Properties, fmt.Sprintf(".%d", id))
55+
if found {
56+
foundBoards = append(foundBoards, board)
57+
}
58+
if !again {
59+
break
60+
}
61+
id++
62+
}
63+
}
64+
return foundBoards
65+
}

0 commit comments

Comments
 (0)