Skip to content

Upgrade arduino-cli #88

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 8 commits into from
Dec 20, 2021
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
24 changes: 19 additions & 5 deletions arduino/cli/commander.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ package cli

import (
"context"
"errors"
"fmt"
"os"
"path/filepath"
"time"

"github.com/arduino/arduino-cli/cli/instance"
"github.com/arduino/arduino-cli/commands/board"
Expand All @@ -45,9 +47,17 @@ func NewCommander() (arduino.Commander, error) {
// Initialize arduino-cli configuration
configuration.Settings = configuration.Init(configuration.FindConfigFileInArgsOrWorkingDirectory(os.Args))
// Create arduino-cli instance, needed to execute arduino-cli commands
inst, err := instance.CreateInstance()
inst, err := instance.Create()
if err != nil {
err = fmt.Errorf("%s: %w", "creating arduino-cli instance", err)
err = fmt.Errorf("creating arduino-cli instance: %w", err)
return nil, err
}
errs := instance.Init(inst)
if len(errs) > 0 {
err = errors.New("initializing arduino-cli instance: received errors: ")
for _, e := range errs {
err = fmt.Errorf("%w%v; ", err, e)
}
return nil, err
}

Expand All @@ -60,7 +70,11 @@ func NewCommander() (arduino.Commander, error) {
// BoardList executes the 'arduino-cli board list' command
// and returns its result.
func (c *commander) BoardList() ([]*rpc.DetectedPort, error) {
ports, err := board.List(c.GetId())
req := &rpc.BoardListRequest{
Instance: c.Instance,
Timeout: time.Second.Milliseconds(),
}
ports, err := board.List(req)
if err != nil {
err = fmt.Errorf("%s: %w", "detecting boards", err)
return nil, err
Expand All @@ -70,13 +84,13 @@ func (c *commander) BoardList() ([]*rpc.DetectedPort, error) {

// UploadBin executes the 'arduino-cli upload -i' command
// and returns its result.
func (c *commander) UploadBin(fqbn, bin, port string) error {
func (c *commander) UploadBin(fqbn, bin, address, protocol string) error {
req := &rpc.UploadRequest{
Instance: c.Instance,
Fqbn: fqbn,
SketchPath: filepath.Dir(bin),
ImportFile: bin,
Port: port,
Port: &rpc.Port{Address: address, Protocol: protocol},
Verbose: false,
}

Expand Down
2 changes: 1 addition & 1 deletion arduino/commander.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ import (
// the arduino-cli commands in a programmatic way.
type Commander interface {
BoardList() ([]*rpc.DetectedPort, error)
UploadBin(fqbn, bin, port string) error
UploadBin(fqbn, bin, address, protocol string) error
//Compile() error
}
39 changes: 19 additions & 20 deletions arduino/grpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,11 @@ func NewClient() (arduino.Commander, func() error, error) {
return nil, func() error { return nil }, err
}

serv := &service{}
serv := &service{
serviceClient: rpc.NewArduinoCoreServiceClient(conn),
settingsClient: settings.NewSettingsServiceClient(conn),
}
// Create an instance of the gRPC clients.
serv.serviceClient = rpc.NewArduinoCoreServiceClient(conn)
serv.settingsClient = settings.NewSettingsServiceClient(conn)
serv.instance, err = initInstance(serv.serviceClient)
if err != nil {
conn.Close()
Expand All @@ -72,13 +73,17 @@ func NewClient() (arduino.Commander, func() error, error) {
}

func initInstance(client rpc.ArduinoCoreServiceClient) (*rpc.Instance, error) {
initRespStream, err := client.Init(context.Background(), &rpc.InitRequest{})
createResp, err := client.Create(context.Background(), &rpc.CreateRequest{})
if err != nil {
err = fmt.Errorf("%s: %w", "Error creating server instance", err)
return nil, err
}
initRespStream, err := client.Init(context.Background(), &rpc.InitRequest{Instance: createResp.GetInstance()})
if err != nil {
err = fmt.Errorf("%s: %w", "Error initializing server instance", err)
return nil, err
}

var instance *rpc.Instance
// Loop and consume the server stream until all the setup procedures are done.
for {
initResp, err := initRespStream.Recv()
Expand All @@ -93,22 +98,16 @@ func initInstance(client rpc.ArduinoCoreServiceClient) (*rpc.Instance, error) {
return nil, err
}

// The server sent us a valid instance, let's print its ID.
if initResp.GetInstance() != nil {
instance = initResp.GetInstance()
//fmt.Printf("Got a new instance with ID: %v", instance.GetId())
}

// When a download is ongoing, log the progress
if initResp.GetDownloadProgress() != nil {
fmt.Printf("DOWNLOAD: %s", initResp.GetDownloadProgress())
}

// When an overall task is ongoing, log the progress
if initResp.GetTaskProgress() != nil {
fmt.Printf("TASK: %s", initResp.GetTaskProgress())
// When a download or task is ongoing, log the progress
if progress := initResp.GetInitProgress(); progress != nil {
if progress.DownloadProgress != nil {
fmt.Printf("DOWNLOAD: %s", progress.DownloadProgress)
}
if progress.TaskProgress != nil {
fmt.Printf("TASK: %s", progress.TaskProgress)
}
}
}

return instance, nil
return createResp.GetInstance(), nil
}
4 changes: 2 additions & 2 deletions arduino/grpc/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ func (c compileHandler) Compile() error {

// Upload executes the 'arduino-cli upload -i' command
// and returns its result.
func (c compileHandler) UploadBin(fqbn, bin, port string) error {
func (c compileHandler) UploadBin(fqbn, bin, address, protocol string) error {
stream, err := c.serviceClient.Upload(context.Background(),
&rpc.UploadRequest{
Instance: c.instance,
Fqbn: fqbn,
SketchPath: filepath.Dir(bin),
ImportFile: bin,
Port: port,
Port: &rpc.Port{Address: address, Protocol: protocol},
Verbose: true,
})

Expand Down
24 changes: 13 additions & 11 deletions command/device/board.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,11 @@ var (

// board contains details of a physical arduino board.
type board struct {
fqbn string
serial string
dType string
port string
fqbn string
serial string
dType string
address string
protocol string
}

// isCrypto checks if the board is a valid arduino board with a
Expand Down Expand Up @@ -75,13 +76,14 @@ func boardFromPorts(ports []*rpc.DetectedPort, params *CreateParams) *board {
if portFilter(port, params) {
continue
}
boardFound := boardFilter(port.Boards, params)
boardFound := boardFilter(port.MatchingBoards, params)
if boardFound != nil {
b := &board{
fqbn: boardFound.Fqbn,
serial: port.SerialNumber,
dType: strings.Split(boardFound.Fqbn, ":")[2],
port: port.Address,
fqbn: boardFound.Fqbn,
serial: port.Port.Properties["serialNumber"],
dType: strings.Split(boardFound.Fqbn, ":")[2],
address: port.Port.Address,
protocol: port.Port.Protocol,
}
return b
}
Expand All @@ -97,10 +99,10 @@ func boardFromPorts(ports []*rpc.DetectedPort, params *CreateParams) *board {
// true -> to skip the port.
// false -> to keep the port.
func portFilter(port *rpc.DetectedPort, params *CreateParams) bool {
if len(port.Boards) == 0 {
if len(port.MatchingBoards) == 0 {
return true
}
if params.Port != nil && *params.Port != port.Address {
if params.Port != nil && *params.Port != port.Port.Address {
return true
}
return false
Expand Down
32 changes: 16 additions & 16 deletions command/device/board_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,25 @@ import (
var (
portsNoBoards = []*rpc.DetectedPort{
{
Address: "ACM0",
Boards: []*rpc.BoardListItem{},
Port: &rpc.Port{Address: "ACM0"},
MatchingBoards: []*rpc.BoardListItem{},
},
{
Address: "ACM1",
Boards: []*rpc.BoardListItem{},
Port: &rpc.Port{Address: "ACM1"},
MatchingBoards: []*rpc.BoardListItem{},
},
}

portsTwoBoards = []*rpc.DetectedPort{
{
Address: "ACM0",
Boards: []*rpc.BoardListItem{
Port: &rpc.Port{Address: "ACM0"},
MatchingBoards: []*rpc.BoardListItem{
{Fqbn: "arduino:samd:nano_33_iot"},
},
},
{
Address: "ACM1",
Boards: []*rpc.BoardListItem{
Port: &rpc.Port{Address: "ACM1"},
MatchingBoards: []*rpc.BoardListItem{
{Fqbn: "arduino:avr:uno"},
},
},
Expand All @@ -68,14 +68,14 @@ func TestBoardFromPorts(t *testing.T) {
name: "port-filter",
filter: &CreateParams{FQBN: nil, Port: stringPointer("ACM1")},
ports: portsTwoBoards,
want: &board{fqbn: "arduino:avr:uno", port: "ACM1"},
want: &board{fqbn: "arduino:avr:uno", address: "ACM1"},
},

{
name: "fqbn-filter",
filter: &CreateParams{FQBN: stringPointer("arduino:avr:uno"), Port: nil},
ports: portsTwoBoards,
want: &board{fqbn: "arduino:avr:uno", port: "ACM1"},
want: &board{fqbn: "arduino:avr:uno", address: "ACM1"},
},

{
Expand All @@ -90,7 +90,7 @@ func TestBoardFromPorts(t *testing.T) {
filter: &CreateParams{FQBN: nil, Port: nil},
ports: portsTwoBoards,
// first board found is selected
want: &board{fqbn: "arduino:samd:nano_33_iot", port: "ACM0"},
want: &board{fqbn: "arduino:samd:nano_33_iot", address: "ACM0"},
},

{
Expand All @@ -104,7 +104,7 @@ func TestBoardFromPorts(t *testing.T) {
name: "both-filter-found",
filter: &CreateParams{FQBN: stringPointer("arduino:avr:uno"), Port: stringPointer("ACM1")},
ports: portsTwoBoards,
want: &board{fqbn: "arduino:avr:uno", port: "ACM1"},
want: &board{fqbn: "arduino:avr:uno", address: "ACM1"},
},

{
Expand All @@ -123,14 +123,14 @@ func TestBoardFromPorts(t *testing.T) {
return

} else if got != nil && tt.want == nil {
t.Errorf("Expected nil board, received not nil board with port %s and fqbn %s", got.port, got.fqbn)
t.Errorf("Expected nil board, received not nil board with port %s and fqbn %s", got.address, got.fqbn)

} else if got == nil && tt.want != nil {
t.Errorf("Expected not nil board with port %s and fqbn %s, received a nil board", tt.want.port, tt.want.fqbn)
t.Errorf("Expected not nil board with port %s and fqbn %s, received a nil board", tt.want.address, tt.want.fqbn)

} else if got.port != tt.want.port || got.fqbn != tt.want.fqbn {
} else if got.address != tt.want.address || got.fqbn != tt.want.fqbn {
t.Errorf("Expected board with port %s and fqbn %s, received board with port %s and fqbn %s",
tt.want.port, tt.want.fqbn, got.port, got.fqbn)
tt.want.address, tt.want.fqbn, got.address, got.fqbn)
}
})
}
Expand Down
2 changes: 1 addition & 1 deletion command/device/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func Create(params *CreateParams) (*DeviceInfo, error) {
"Try the 'create-lora' command instead if it's a LoRa device"+
" or 'create-generic' otherwise",
board.fqbn,
board.port,
board.address,
)
}

Expand Down
6 changes: 3 additions & 3 deletions command/device/createlora.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func CreateLora(params *CreateLoraParams) (*DeviceLoraInfo, error) {
" Try the 'create' command instead if it's a device with a supported crypto-chip"+
" or 'create-generic' otherwise",
board.fqbn,
board.port,
board.address,
)
}

Expand All @@ -96,13 +96,13 @@ func CreateLora(params *CreateLoraParams) (*DeviceLoraInfo, error) {
logrus.Infof("%s", "Uploading deveui sketch on the LoRa board")
errMsg := "Error while uploading the LoRa provisioning binary"
err = retry(deveuiUploadAttempts, deveuiUploadWait*time.Millisecond, errMsg, func() error {
return comm.UploadBin(board.fqbn, bin, board.port)
return comm.UploadBin(board.fqbn, bin, board.address, board.protocol)
})
if err != nil {
return nil, fmt.Errorf("failed to upload LoRa provisioning binary: %w", err)
}

eui, err := extractEUI(board.port)
eui, err := extractEUI(board.address)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions command/device/provision.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (p provision) run() error {
errMsg := "Error while uploading the provisioning sketch"
err = retry(5, time.Millisecond*1000, errMsg, func() error {
//serialutils.Reset(dev.port, true, nil)
return p.UploadBin(p.board.fqbn, bin, p.board.port)
return p.UploadBin(p.board.fqbn, bin, p.board.address, p.board.protocol)
})
if err != nil {
return err
Expand All @@ -98,7 +98,7 @@ func (p provision) run() error {
p.ser = serial.NewSerial()
errMsg = "Error while connecting to the board"
err = retry(5, time.Millisecond*1000, errMsg, func() error {
return p.ser.Connect(p.board.port)
return p.ser.Connect(p.board.address)
})
if err != nil {
return err
Expand Down
Loading