Skip to content

Commit 4b6156e

Browse files
committed
Refactor port in address and protocol
1 parent b52d716 commit 4b6156e

File tree

8 files changed

+30
-27
lines changed

8 files changed

+30
-27
lines changed

arduino/cli/commander.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,13 @@ func (c *commander) BoardList() ([]*rpc.DetectedPort, error) {
7878

7979
// UploadBin executes the 'arduino-cli upload -i' command
8080
// and returns its result.
81-
func (c *commander) UploadBin(fqbn, bin, port string) error {
81+
func (c *commander) UploadBin(fqbn, bin, address, protocol string) error {
8282
req := &rpc.UploadRequest{
8383
Instance: c.Instance,
8484
Fqbn: fqbn,
8585
SketchPath: filepath.Dir(bin),
8686
ImportFile: bin,
87-
Port: &rpc.Port{Address: port},
87+
Port: &rpc.Port{Address: address, Protocol: protocol},
8888
Verbose: false,
8989
}
9090

arduino/commander.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,6 @@ import (
2525
// the arduino-cli commands in a programmatic way.
2626
type Commander interface {
2727
BoardList() ([]*rpc.DetectedPort, error)
28-
UploadBin(fqbn, bin, port string) error
28+
UploadBin(fqbn, bin, address, protocol string) error
2929
//Compile() error
3030
}

arduino/grpc/compile.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,14 @@ func (c compileHandler) Compile() error {
3838

3939
// Upload executes the 'arduino-cli upload -i' command
4040
// and returns its result.
41-
func (c compileHandler) UploadBin(fqbn, bin, port string) error {
41+
func (c compileHandler) UploadBin(fqbn, bin, address, protocol string) error {
4242
stream, err := c.serviceClient.Upload(context.Background(),
4343
&rpc.UploadRequest{
4444
Instance: c.instance,
4545
Fqbn: fqbn,
4646
SketchPath: filepath.Dir(bin),
4747
ImportFile: bin,
48-
Port: &rpc.Port{Address: port},
48+
Port: &rpc.Port{Address: address, Protocol: protocol},
4949
Verbose: true,
5050
})
5151

command/device/board.go

+10-8
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,11 @@ var (
4141

4242
// board contains details of a physical arduino board.
4343
type board struct {
44-
fqbn string
45-
serial string
46-
dType string
47-
port string
44+
fqbn string
45+
serial string
46+
dType string
47+
address string
48+
protocol string
4849
}
4950

5051
// isCrypto checks if the board is a valid arduino board with a
@@ -78,10 +79,11 @@ func boardFromPorts(ports []*rpc.DetectedPort, params *CreateParams) *board {
7879
boardFound := boardFilter(port.MatchingBoards, params)
7980
if boardFound != nil {
8081
b := &board{
81-
fqbn: boardFound.Fqbn,
82-
serial: port.Port.Properties["serialNumber"],
83-
dType: strings.Split(boardFound.Fqbn, ":")[2],
84-
port: port.Port.Address,
82+
fqbn: boardFound.Fqbn,
83+
serial: port.Port.Properties["serialNumber"],
84+
dType: strings.Split(boardFound.Fqbn, ":")[2],
85+
address: port.Port.Address,
86+
protocol: port.Port.Protocol,
8587
}
8688
return b
8789
}

command/device/board_test.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,14 @@ func TestBoardFromPorts(t *testing.T) {
6868
name: "port-filter",
6969
filter: &CreateParams{FQBN: nil, Port: stringPointer("ACM1")},
7070
ports: portsTwoBoards,
71-
want: &board{fqbn: "arduino:avr:uno", port: "ACM1"},
71+
want: &board{fqbn: "arduino:avr:uno", address: "ACM1"},
7272
},
7373

7474
{
7575
name: "fqbn-filter",
7676
filter: &CreateParams{FQBN: stringPointer("arduino:avr:uno"), Port: nil},
7777
ports: portsTwoBoards,
78-
want: &board{fqbn: "arduino:avr:uno", port: "ACM1"},
78+
want: &board{fqbn: "arduino:avr:uno", address: "ACM1"},
7979
},
8080

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

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

110110
{
@@ -123,14 +123,14 @@ func TestBoardFromPorts(t *testing.T) {
123123
return
124124

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

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

131-
} else if got.port != tt.want.port || got.fqbn != tt.want.fqbn {
131+
} else if got.address != tt.want.address || got.fqbn != tt.want.fqbn {
132132
t.Errorf("Expected board with port %s and fqbn %s, received board with port %s and fqbn %s",
133-
tt.want.port, tt.want.fqbn, got.port, got.fqbn)
133+
tt.want.address, tt.want.fqbn, got.address, got.fqbn)
134134
}
135135
})
136136
}

command/device/create.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func Create(params *CreateParams) (*DeviceInfo, error) {
5959
"Try the 'create-lora' command instead if it's a LoRa device"+
6060
" or 'create-generic' otherwise",
6161
board.fqbn,
62-
board.port,
62+
board.address,
6363
)
6464
}
6565

command/device/createlora.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func CreateLora(params *CreateLoraParams) (*DeviceLoraInfo, error) {
8484
" Try the 'create' command instead if it's a device with a supported crypto-chip"+
8585
" or 'create-generic' otherwise",
8686
board.fqbn,
87-
board.port,
87+
board.address,
8888
)
8989
}
9090

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

105-
eui, err := extractEUI(board.port)
105+
eui, err := extractEUI(board.address)
106106
if err != nil {
107107
return nil, err
108108
}

command/device/provision.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ func (p provision) run() error {
8686
errMsg := "Error while uploading the provisioning sketch"
8787
err = retry(5, time.Millisecond*1000, errMsg, func() error {
8888
//serialutils.Reset(dev.port, true, nil)
89-
return p.UploadBin(p.board.fqbn, bin, p.board.port)
89+
fmt.Println(p.board.fqbn, bin, p.board.address)
90+
return p.UploadBin(p.board.fqbn, bin, p.board.address, p.board.protocol)
9091
})
9192
if err != nil {
9293
return err
@@ -98,7 +99,7 @@ func (p provision) run() error {
9899
p.ser = serial.NewSerial()
99100
errMsg = "Error while connecting to the board"
100101
err = retry(5, time.Millisecond*1000, errMsg, func() error {
101-
return p.ser.Connect(p.board.port)
102+
return p.ser.Connect(p.board.address)
102103
})
103104
if err != nil {
104105
return err

0 commit comments

Comments
 (0)