Skip to content

Commit d011848

Browse files
committed
Cache board identification properties
1 parent 83d71c8 commit d011848

File tree

1 file changed

+38
-21
lines changed

1 file changed

+38
-21
lines changed

arduino/cores/board.go

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@ import (
2424

2525
// Board represents a board loaded from an installed platform
2626
type Board struct {
27-
BoardID string
28-
Properties *properties.Map `json:"-"`
29-
PlatformRelease *PlatformRelease `json:"-"`
27+
BoardID string
28+
Properties *properties.Map `json:"-"`
29+
PlatformRelease *PlatformRelease `json:"-"`
30+
identificationProperties []*properties.Map
3031
}
3132

3233
// HasUsbID returns true if the board match the usb vid and pid parameters
@@ -140,22 +141,18 @@ func (b *Board) GeneratePropertiesForConfiguration(config string) (*properties.M
140141
return b.GetBuildProperties(fqbn.Configs)
141142
}
142143

143-
// IsBoardMatchingIDProperties returns true if the board match the given
144-
// identification properties
145-
func (b *Board) IsBoardMatchingIDProperties(query *properties.Map) bool {
146-
portIDPropsSet := b.Properties.SubTree("upload_port")
147-
if portIDPropsSet.Size() == 0 {
148-
return false
144+
// GetIdentificationProperties calculate and returns a list of properties sets
145+
// containing the properties required to identify the board. The returned sets
146+
// must not be changed by the caller.
147+
func (b *Board) GetIdentificationProperties() []*properties.Map {
148+
if b.identificationProperties != nil {
149+
return b.identificationProperties
149150
}
151+
b.identificationProperties = []*properties.Map{}
150152

151-
// check checks if the given set of properties p match the "query"
152-
check := func(p *properties.Map) bool {
153-
for k, v := range p.AsMap() {
154-
if !strings.EqualFold(query.Get(k), v) {
155-
return false
156-
}
157-
}
158-
return true
153+
portIDPropsSet := b.Properties.SubTree("upload_port")
154+
if portIDPropsSet.Size() == 0 {
155+
return b.identificationProperties
159156
}
160157

161158
// First check the identification properties with sub index "upload_port.N.xxx"
@@ -166,9 +163,7 @@ func (b *Board) IsBoardMatchingIDProperties(query *properties.Map) bool {
166163
idx++
167164
if idProps.Size() > 0 {
168165
haveIndexedProperties = true
169-
if check(idProps) {
170-
return true
171-
}
166+
b.identificationProperties = append(b.identificationProperties, idProps)
172167
} else if idx > 1 {
173168
// Always check sub-id 0 and 1 (https://github.com/arduino/arduino-cli/issues/456)
174169
break
@@ -177,8 +172,30 @@ func (b *Board) IsBoardMatchingIDProperties(query *properties.Map) bool {
177172

178173
// if there are no subindexed then check the whole "upload_port.xxx"
179174
if !haveIndexedProperties {
180-
return check(portIDPropsSet)
175+
b.identificationProperties = append(b.identificationProperties, portIDPropsSet)
181176
}
182177

178+
return b.identificationProperties
179+
}
180+
181+
// IsBoardMatchingIDProperties returns true if the board match the given
182+
// identification properties
183+
func (b *Board) IsBoardMatchingIDProperties(query *properties.Map) bool {
184+
// check checks if the given set of properties p match the "query"
185+
check := func(p *properties.Map) bool {
186+
for k, v := range p.AsMap() {
187+
if !strings.EqualFold(query.Get(k), v) {
188+
return false
189+
}
190+
}
191+
return true
192+
}
193+
194+
// First check the identification properties with sub index "upload_port.N.xxx"
195+
for _, idProps := range b.GetIdentificationProperties() {
196+
if check(idProps) {
197+
return true
198+
}
199+
}
183200
return false
184201
}

0 commit comments

Comments
 (0)