Skip to content

Commit

Permalink
Added board config identification subroutines
Browse files Browse the repository at this point in the history
  • Loading branch information
cmaglie committed Oct 14, 2022
1 parent baecc78 commit 14a424b
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 1 deletion.
39 changes: 38 additions & 1 deletion arduino/cores/board.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ func (b *Board) GetIdentificationProperties() []*properties.Map {
}

// IsBoardMatchingIDProperties returns true if the board match the given
// identification properties
// upload port identification properties
func (b *Board) IsBoardMatchingIDProperties(query *properties.Map) bool {
// check checks if the given set of properties p match the "query"
check := func(p *properties.Map) bool {
Expand All @@ -191,3 +191,40 @@ func (b *Board) IsBoardMatchingIDProperties(query *properties.Map) bool {
func GetMonitorSettings(protocol string, boardProperties *properties.Map) *properties.Map {
return boardProperties.SubTree("monitor_port." + protocol)
}

// IdentifyBoardConfiguration returns the configuration of the board that can be
// deduced from the given upload port identification properties
func (b *Board) IdentifyBoardConfiguration(query *properties.Map) *properties.Map {
// check checks if the given set of properties p match the "query"
check := func(p *properties.Map) bool {
for k, v := range p.AsMap() {
if !strings.EqualFold(query.Get(k), v) {
return false
}
}
return true
}
checkAll := func(allP []*properties.Map) bool {
for _, p := range allP {
if check(p) {
return true
}
}
return false
}

res := properties.NewMap()
for _, option := range b.GetConfigOptions().Keys() {
values := b.GetConfigOptionValues(option).Keys()

for _, value := range values {
config := option + "=" + value
configProps := b.configOptionProperties[config]

if checkAll(configProps.ExtractSubIndexSets("upload_port")) {
res.Set(option, value)
}
}
}
return res
}
78 changes: 78 additions & 0 deletions arduino/cores/board_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -554,3 +554,81 @@ func TestBoardMatching(t *testing.T) {
"lemons": "XXX",
})))
}

func TestBoardConfigMatching(t *testing.T) {
brd01 := &Board{
Properties: properties.NewFromHashmap(map[string]string{
"upload_port.pid": "0x0010",
"upload_port.vid": "0x2341",
"menu.cpu.atmega1280": "ATmega1280",
"menu.cpu.atmega1280.upload_port.cpu": "atmega1280",
"menu.cpu.atmega1280.build_cpu": "atmega1280",
"menu.cpu.atmega2560": "ATmega2560",
"menu.cpu.atmega2560.upload_port.cpu": "atmega2560",
"menu.cpu.atmega2560.build_cpu": "atmega2560",
"menu.mem.1k": "1KB",
"menu.mem.1k.upload_port.mem": "1",
"menu.mem.1k.build_mem": "1024",
"menu.mem.2k": "2KB",
"menu.mem.2k.upload_port.1.mem": "2",
"menu.mem.2k.upload_port.2.ab": "ef",
"menu.mem.2k.upload_port.2.cd": "gh",
"menu.mem.2k.build_mem": "2048",
}),
PlatformRelease: &PlatformRelease{
Platform: &Platform{
Architecture: "avr",
Package: &Package{
Name: "arduino",
},
},
Menus: properties.NewFromHashmap(map[string]string{
"cpu": "Processor",
"mem": "Memory",
}),
},
}

type m map[string]string
type Test struct {
testName string
identificationProps map[string]string
configOutput map[string]string
}

tests := []Test{
{"Simple",
m{"pid": "0x0010", "vid": "0x2341"},
m{}},
{"WithConfig1",
m{"pid": "0x0010", "vid": "0x2341", "cpu": "atmega2560"},
m{"cpu": "atmega2560"}},
{"WithConfig2",
m{"pid": "0x0010", "vid": "0x2341", "cpu": "atmega1280"},
m{"cpu": "atmega1280"}},
{"WithDoubleConfig1",
m{"pid": "0x0010", "vid": "0x2341", "cpu": "atmega1280", "mem": "1"},
m{"cpu": "atmega1280", "mem": "1k"}},
{"WithDoubleConfig2",
m{"pid": "0x0010", "vid": "0x2341", "cpu": "atmega1280", "ab": "ef"},
m{"cpu": "atmega1280"}},
{"WithDoubleConfig3",
m{"pid": "0x0010", "vid": "0x2341", "cpu": "atmega1280", "ab": "ef", "cd": "gh"},
m{"cpu": "atmega1280", "mem": "2k"}},
{"WithIncompleteIdentificationProps",
m{"cpu": "atmega1280"},
nil},
}
for _, test := range tests {
t.Run(test.testName, func(t *testing.T) {
identificationProps := properties.NewFromHashmap(test.identificationProps)
if test.configOutput != nil {
require.True(t, brd01.IsBoardMatchingIDProperties(identificationProps))
config := brd01.IdentifyBoardConfiguration(identificationProps)
require.EqualValues(t, test.configOutput, config.AsMap())
} else {
require.False(t, brd01.IsBoardMatchingIDProperties(identificationProps))
}
})
}
}
9 changes: 9 additions & 0 deletions arduino/cores/packagemanager/identify.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,12 @@ func (pme *Explorer) IdentifyBoard(idProps *properties.Map) []*cores.Board {

return foundBoards
}

// IdentifyBoardConfiguration returns a set of identification properties for configuration options
// of a board.
func (pm *PackageManager) IdentifyBoardConfiguration(idProps *properties.Map, board *cores.Board) *properties.Map {
if idProps.Size() == 0 {
return properties.NewMap()
}
return board.IdentifyBoardConfiguration(idProps)
}

0 comments on commit 14a424b

Please sign in to comment.