Skip to content

Commit 14a424b

Browse files
committed
Added board config identification subroutines
1 parent baecc78 commit 14a424b

File tree

3 files changed

+125
-1
lines changed

3 files changed

+125
-1
lines changed

arduino/cores/board.go

+38-1
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ func (b *Board) GetIdentificationProperties() []*properties.Map {
165165
}
166166

167167
// IsBoardMatchingIDProperties returns true if the board match the given
168-
// identification properties
168+
// upload port identification properties
169169
func (b *Board) IsBoardMatchingIDProperties(query *properties.Map) bool {
170170
// check checks if the given set of properties p match the "query"
171171
check := func(p *properties.Map) bool {
@@ -191,3 +191,40 @@ func (b *Board) IsBoardMatchingIDProperties(query *properties.Map) bool {
191191
func GetMonitorSettings(protocol string, boardProperties *properties.Map) *properties.Map {
192192
return boardProperties.SubTree("monitor_port." + protocol)
193193
}
194+
195+
// IdentifyBoardConfiguration returns the configuration of the board that can be
196+
// deduced from the given upload port identification properties
197+
func (b *Board) IdentifyBoardConfiguration(query *properties.Map) *properties.Map {
198+
// check checks if the given set of properties p match the "query"
199+
check := func(p *properties.Map) bool {
200+
for k, v := range p.AsMap() {
201+
if !strings.EqualFold(query.Get(k), v) {
202+
return false
203+
}
204+
}
205+
return true
206+
}
207+
checkAll := func(allP []*properties.Map) bool {
208+
for _, p := range allP {
209+
if check(p) {
210+
return true
211+
}
212+
}
213+
return false
214+
}
215+
216+
res := properties.NewMap()
217+
for _, option := range b.GetConfigOptions().Keys() {
218+
values := b.GetConfigOptionValues(option).Keys()
219+
220+
for _, value := range values {
221+
config := option + "=" + value
222+
configProps := b.configOptionProperties[config]
223+
224+
if checkAll(configProps.ExtractSubIndexSets("upload_port")) {
225+
res.Set(option, value)
226+
}
227+
}
228+
}
229+
return res
230+
}

arduino/cores/board_test.go

+78
Original file line numberDiff line numberDiff line change
@@ -554,3 +554,81 @@ func TestBoardMatching(t *testing.T) {
554554
"lemons": "XXX",
555555
})))
556556
}
557+
558+
func TestBoardConfigMatching(t *testing.T) {
559+
brd01 := &Board{
560+
Properties: properties.NewFromHashmap(map[string]string{
561+
"upload_port.pid": "0x0010",
562+
"upload_port.vid": "0x2341",
563+
"menu.cpu.atmega1280": "ATmega1280",
564+
"menu.cpu.atmega1280.upload_port.cpu": "atmega1280",
565+
"menu.cpu.atmega1280.build_cpu": "atmega1280",
566+
"menu.cpu.atmega2560": "ATmega2560",
567+
"menu.cpu.atmega2560.upload_port.cpu": "atmega2560",
568+
"menu.cpu.atmega2560.build_cpu": "atmega2560",
569+
"menu.mem.1k": "1KB",
570+
"menu.mem.1k.upload_port.mem": "1",
571+
"menu.mem.1k.build_mem": "1024",
572+
"menu.mem.2k": "2KB",
573+
"menu.mem.2k.upload_port.1.mem": "2",
574+
"menu.mem.2k.upload_port.2.ab": "ef",
575+
"menu.mem.2k.upload_port.2.cd": "gh",
576+
"menu.mem.2k.build_mem": "2048",
577+
}),
578+
PlatformRelease: &PlatformRelease{
579+
Platform: &Platform{
580+
Architecture: "avr",
581+
Package: &Package{
582+
Name: "arduino",
583+
},
584+
},
585+
Menus: properties.NewFromHashmap(map[string]string{
586+
"cpu": "Processor",
587+
"mem": "Memory",
588+
}),
589+
},
590+
}
591+
592+
type m map[string]string
593+
type Test struct {
594+
testName string
595+
identificationProps map[string]string
596+
configOutput map[string]string
597+
}
598+
599+
tests := []Test{
600+
{"Simple",
601+
m{"pid": "0x0010", "vid": "0x2341"},
602+
m{}},
603+
{"WithConfig1",
604+
m{"pid": "0x0010", "vid": "0x2341", "cpu": "atmega2560"},
605+
m{"cpu": "atmega2560"}},
606+
{"WithConfig2",
607+
m{"pid": "0x0010", "vid": "0x2341", "cpu": "atmega1280"},
608+
m{"cpu": "atmega1280"}},
609+
{"WithDoubleConfig1",
610+
m{"pid": "0x0010", "vid": "0x2341", "cpu": "atmega1280", "mem": "1"},
611+
m{"cpu": "atmega1280", "mem": "1k"}},
612+
{"WithDoubleConfig2",
613+
m{"pid": "0x0010", "vid": "0x2341", "cpu": "atmega1280", "ab": "ef"},
614+
m{"cpu": "atmega1280"}},
615+
{"WithDoubleConfig3",
616+
m{"pid": "0x0010", "vid": "0x2341", "cpu": "atmega1280", "ab": "ef", "cd": "gh"},
617+
m{"cpu": "atmega1280", "mem": "2k"}},
618+
{"WithIncompleteIdentificationProps",
619+
m{"cpu": "atmega1280"},
620+
nil},
621+
}
622+
for _, test := range tests {
623+
t.Run(test.testName, func(t *testing.T) {
624+
identificationProps := properties.NewFromHashmap(test.identificationProps)
625+
if test.configOutput != nil {
626+
require.True(t, brd01.IsBoardMatchingIDProperties(identificationProps))
627+
config := brd01.IdentifyBoardConfiguration(identificationProps)
628+
require.EqualValues(t, test.configOutput, config.AsMap())
629+
} else {
630+
require.False(t, brd01.IsBoardMatchingIDProperties(identificationProps))
631+
}
632+
})
633+
}
634+
}

arduino/cores/packagemanager/identify.go

+9
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,12 @@ func (pme *Explorer) IdentifyBoard(idProps *properties.Map) []*cores.Board {
3535

3636
return foundBoards
3737
}
38+
39+
// IdentifyBoardConfiguration returns a set of identification properties for configuration options
40+
// of a board.
41+
func (pm *PackageManager) IdentifyBoardConfiguration(idProps *properties.Map, board *cores.Board) *properties.Map {
42+
if idProps.Size() == 0 {
43+
return properties.NewMap()
44+
}
45+
return board.IdentifyBoardConfiguration(idProps)
46+
}

0 commit comments

Comments
 (0)