Skip to content

Use default FQBN options when missing #53

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 0 additions & 4 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,6 @@
branch = "master"
name = "github.com/arduino/board-discovery"

[[constraint]]
branch = "master"
name = "github.com/arduino/go-properties-map"

[[constraint]]
branch = "master"
name = "github.com/arduino/go-win32-utils"
Expand Down
49 changes: 30 additions & 19 deletions arduino/cores/board.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,22 @@ import (
"fmt"
"strings"

"github.com/arduino/go-properties-map"
"github.com/arduino/go-properties-orderedmap"
)

// Board represents a board loaded from an installed platform
type Board struct {
BoardID string
Properties properties.Map `json:"-"`
Properties *properties.Map `json:"-"`
PlatformRelease *PlatformRelease `json:"-"`
}

// HasUsbID returns true if the board match the usb vid and pid parameters
func (b *Board) HasUsbID(reqVid, reqPid string) bool {
vids := b.Properties.SubTree("vid")
pids := b.Properties.SubTree("pid")
for id, vid := range vids {
if pid, ok := pids[id]; ok {
for id, vid := range vids.AsMap() {
if pid, ok := pids.GetOk(id); ok {
if strings.EqualFold(vid, reqVid) && strings.EqualFold(pid, reqPid) {
return true
}
Expand All @@ -47,7 +47,7 @@ func (b *Board) HasUsbID(reqVid, reqPid string) bool {

// Name returns the board name as defined in boards.txt properties
func (b *Board) Name() string {
return b.Properties["name"]
return b.Properties.Get("name")
}

// FQBN return the Fully-Qualified-Board-Name for the default configuration of this board
Expand All @@ -62,29 +62,40 @@ func (b *Board) String() string {

// GetBuildProperties returns the build properties and the build
// platform for the Board with the configuration passed as parameter.
func (b *Board) GetBuildProperties(configs properties.Map) (properties.Map, error) {
func (b *Board) GetBuildProperties(userConfigs *properties.Map) (*properties.Map, error) {
// Clone user configs because they are destroyed during iteration
userConfigs = userConfigs.Clone()

// Start with board's base properties
buildProperties := b.Properties.Clone()

// Add all sub-configurations one by one
// Add all sub-configurations one by one (a config is: option=value)
menu := b.Properties.SubTree("menu")
for option, value := range configs {
if option == "" {
return nil, fmt.Errorf("invalid empty option found")
}

for _, option := range menu.FirstLevelKeys() {
optionMenu := menu.SubTree(option)
if len(optionMenu) == 0 {
return nil, fmt.Errorf("invalid option '%s'", option)
}
if _, ok := optionMenu[value]; !ok {
return nil, fmt.Errorf("invalid value '%s' for option '%s'", value, option)
userValue, haveUserValue := userConfigs.GetOk(option)
if haveUserValue {
userConfigs.Remove(option)
if !optionMenu.ContainsKey(userValue) {
return nil, fmt.Errorf("invalid value '%s' for option '%s'", userValue, option)
}
} else {
// apply default
userValue = optionMenu.FirstLevelKeys()[0]
}

optionsConf := optionMenu.SubTree(value)
optionsConf := optionMenu.SubTree(userValue)
buildProperties.Merge(optionsConf)
}

// Check for residual invalid options...
for _, invalidOption := range userConfigs.Keys() {
if invalidOption == "" {
return nil, fmt.Errorf("invalid empty option found")
}
return nil, fmt.Errorf("invalid option '%s'", invalidOption)
}

return buildProperties, nil
}

Expand All @@ -93,7 +104,7 @@ func (b *Board) GetBuildProperties(configs properties.Map) (properties.Map, erro
// the full FQBN is "arduino:avr:mega:cpu=atmega2560" the config part must be
// "cpu=atmega2560".
// FIXME: deprecated, use GetBuildProperties instead
func (b *Board) GeneratePropertiesForConfiguration(config string) (properties.Map, error) {
func (b *Board) GeneratePropertiesForConfiguration(config string) (*properties.Map, error) {
fqbn, err := ParseFQBN(b.String() + ":" + config)
if err != nil {
return nil, fmt.Errorf("parsing fqbn: %s", err)
Expand Down
32 changes: 16 additions & 16 deletions arduino/cores/board_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ package cores
import (
"testing"

properties "github.com/arduino/go-properties-map"
properties "github.com/arduino/go-properties-orderedmap"
"github.com/stretchr/testify/require"
)

var boardUno = &Board{
BoardID: "uno",
Properties: properties.Map{
Properties: properties.NewFromHashmap(map[string]string{
"name": "Arduino/Genuino Uno",
"vid.0": "0x2341",
"pid.0": "0x0043",
Expand All @@ -53,7 +53,7 @@ var boardUno = &Board{
"build.board": "AVR_UNO",
"build.core": "arduino",
"build.variant": "standard",
},
}),
PlatformRelease: &PlatformRelease{
Platform: &Platform{
Architecture: "avr",
Expand All @@ -66,7 +66,7 @@ var boardUno = &Board{

var boardMega = &Board{
BoardID: "mega",
Properties: properties.Map{
Properties: properties.NewFromHashmap(map[string]string{
"name": "Arduino/Genuino Mega or Mega 2560",
"vid.0": "0x2341",
"pid.0": "0x0010",
Expand Down Expand Up @@ -108,7 +108,7 @@ var boardMega = &Board{
"menu.cpu.atmega1280.bootloader.file": "atmega/ATmegaBOOT_168_atmega1280.hex",
"menu.cpu.atmega1280.build.mcu": "atmega1280",
"menu.cpu.atmega1280.build.board": "AVR_MEGA",
},
}),
PlatformRelease: &PlatformRelease{
Platform: &Platform{
Architecture: "avr",
Expand All @@ -121,7 +121,7 @@ var boardMega = &Board{

var boardWatterottTiny841 = &Board{
BoardID: "attiny841",
Properties: properties.Map{
Properties: properties.NewFromHashmap(map[string]string{
"name": "ATtiny841 (8 MHz)",
"menu.core.arduino": "Standard Arduino",
"menu.core.arduino.build.core": "arduino:arduino",
Expand All @@ -148,7 +148,7 @@ var boardWatterottTiny841 = &Board{
"build.mcu": "attiny841",
"build.f_cpu": "8000000L",
"build.board": "AVR_ATTINY841",
},
}),
PlatformRelease: &PlatformRelease{
Platform: &Platform{
Architecture: "avr",
Expand Down Expand Up @@ -188,7 +188,7 @@ func TestBoard(t *testing.T) {
}

func TestBoardOptions(t *testing.T) {
expConf2560 := properties.Map{
expConf2560 := properties.NewFromHashmap(map[string]string{
"bootloader.extended_fuses": "0xFD",
"bootloader.file": "stk500v2/stk500boot_v2_mega2560.hex",
"bootloader.high_fuses": "0xD8",
Expand Down Expand Up @@ -237,13 +237,13 @@ func TestBoardOptions(t *testing.T) {
"vid.3": "0x2A03",
"vid.4": "0x2341",
"vid.5": "0x2341",
}
})

conf2560, err := boardMega.GeneratePropertiesForConfiguration("cpu=atmega2560")
require.NoError(t, err, "generating cpu=atmega2560 configuration")
require.EqualValues(t, expConf2560, conf2560, "configuration for cpu=atmega2560")
require.EqualValues(t, expConf2560.AsMap(), conf2560.AsMap(), "configuration for cpu=atmega2560")

expConf1280 := properties.Map{
expConf1280 := properties.NewFromHashmap(map[string]string{
"bootloader.extended_fuses": "0xF5",
"bootloader.file": "atmega/ATmegaBOOT_168_atmega1280.hex",
"bootloader.high_fuses": "0xDA",
Expand Down Expand Up @@ -292,18 +292,18 @@ func TestBoardOptions(t *testing.T) {
"vid.3": "0x2A03",
"vid.4": "0x2341",
"vid.5": "0x2341",
}
})
conf1280, err := boardMega.GeneratePropertiesForConfiguration("cpu=atmega1280")
require.NoError(t, err, "generating cpu=atmega1280 configuration")
require.EqualValues(t, expConf1280, conf1280, "configuration for cpu=atmega1280")
require.EqualValues(t, expConf1280.AsMap(), conf1280.AsMap(), "configuration for cpu=atmega1280")

_, err = boardMega.GeneratePropertiesForConfiguration("cpu=atmegassss")
require.Error(t, err, "generating cpu=atmegassss configuration")

_, err = boardUno.GeneratePropertiesForConfiguration("cpu=atmega1280")
require.Error(t, err, "generating cpu=atmega1280 configuration")

expWatterott := properties.Map{
expWatterott := properties.NewFromHashmap(map[string]string{
"bootloader.extended_fuses": "0xFE",
"bootloader.file": "micronucleus-t841.hex",
"bootloader.high_fuses": "0xDD",
Expand Down Expand Up @@ -332,10 +332,10 @@ func TestBoardOptions(t *testing.T) {
"upload.use_1200bps_touch": "false",
"upload.wait_for_upload_port": "false",
"vid.0": "0x16D0",
}
})
confWatterott, err := boardWatterottTiny841.GeneratePropertiesForConfiguration("core=spencekonde,info=info")
require.NoError(t, err, "generating core=spencekonde,info=info configuration")
require.EqualValues(t, expWatterott, confWatterott, "generating core=spencekonde,info=info configuration")
require.EqualValues(t, expWatterott.AsMap(), confWatterott.AsMap(), "generating core=spencekonde,info=info configuration")

// data, err := json.MarshalIndent(prop, "", " ")
// require.NoError(t, err, "marshaling result")
Expand Down
26 changes: 13 additions & 13 deletions arduino/cores/cores.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"github.com/arduino/go-paths-helper"

"github.com/arduino/arduino-cli/arduino/resources"
"github.com/arduino/go-properties-map"
"github.com/arduino/go-properties-orderedmap"
"go.bug.st/relaxed-semver"
)

Expand All @@ -44,11 +44,11 @@ type PlatformRelease struct {
Dependencies ToolDependencies // The Dependency entries to load tools.
Platform *Platform `json:"-"`

Properties properties.Map `json:"-"`
Boards map[string]*Board `json:"-"`
Programmers map[string]properties.Map `json:"-"`
Menus map[string]string `json:"-"`
InstallDir *paths.Path `json:"-"`
Properties *properties.Map `json:"-"`
Boards map[string]*Board `json:"-"`
Programmers map[string]*properties.Map `json:"-"`
Menus *properties.Map `json:"-"`
InstallDir *paths.Path `json:"-"`
}

// BoardManifest contains information about a board. These metadata are usually
Expand Down Expand Up @@ -103,8 +103,8 @@ func (platform *Platform) GetOrCreateRelease(version *semver.Version) (*Platform
release := &PlatformRelease{
Version: version,
Boards: map[string]*Board{},
Properties: properties.Map{},
Programmers: map[string]properties.Map{},
Properties: properties.NewMap(),
Programmers: map[string]*properties.Map{},
Platform: platform,
}
platform.Releases[tag] = release
Expand Down Expand Up @@ -177,7 +177,7 @@ func (release *PlatformRelease) GetOrCreateBoard(boardID string) *Board {
}
board := &Board{
BoardID: boardID,
Properties: properties.Map{},
Properties: properties.NewMap(),
PlatformRelease: release,
}
release.Boards[boardID] = board
Expand All @@ -198,10 +198,10 @@ func (release *PlatformRelease) RequiresToolRelease(toolRelease *ToolRelease) bo
}

// RuntimeProperties returns the runtime properties for this PlatformRelease
func (release *PlatformRelease) RuntimeProperties() properties.Map {
return properties.Map{
"runtime.platform.path": release.InstallDir.String(),
}
func (release *PlatformRelease) RuntimeProperties() *properties.Map {
res := properties.NewMap()
res.Set("runtime.platform.path", release.InstallDir.String())
return res
}

// GetLibrariesDir returns the path to the core libraries or nil if not
Expand Down
17 changes: 7 additions & 10 deletions arduino/cores/fqbn.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,17 @@ package cores

import (
"fmt"
"sort"
"strings"

properties "github.com/arduino/go-properties-map"
properties "github.com/arduino/go-properties-orderedmap"
)

// FQBN represents a Board with a specific configuration
type FQBN struct {
Package string
PlatformArch string
BoardID string
Configs properties.Map
Configs *properties.Map
}

// ParseFQBN extract an FQBN object from the input string
Expand All @@ -45,12 +44,12 @@ func ParseFQBN(fqbnIn string) (*FQBN, error) {
Package: fqbnParts[0],
PlatformArch: fqbnParts[1],
BoardID: fqbnParts[2],
Configs: properties.NewMap(),
}
if fqbn.BoardID == "" {
return nil, fmt.Errorf("invalid fqbn: empty board identifier")
}
if len(fqbnParts) > 3 {
fqbn.Configs = properties.Map{}
for _, pair := range strings.Split(fqbnParts[3], ",") {
parts := strings.SplitN(pair, "=", 2)
if len(parts) != 2 {
Expand All @@ -61,20 +60,18 @@ func ParseFQBN(fqbnIn string) (*FQBN, error) {
if k == "" {
return nil, fmt.Errorf("invalid fqbn config: %s", pair)
}
fqbn.Configs[k] = v
fqbn.Configs.Set(k, v)
}
}
return fqbn, nil
}

func (fqbn *FQBN) String() string {
res := fmt.Sprintf("%s:%s:%s", fqbn.Package, fqbn.PlatformArch, fqbn.BoardID)
if fqbn.Configs != nil {
if fqbn.Configs.Size() > 0 {
sep := ":"
keys := fqbn.Configs.Keys()
sort.Strings(keys)
for _, k := range keys {
res += sep + k + "=" + fqbn.Configs[k]
for _, k := range fqbn.Configs.Keys() {
res += sep + k + "=" + fqbn.Configs.Get(k)
sep = ","
}
}
Expand Down
Loading