Skip to content

Commit

Permalink
Perform a deep-copy of upload ports where needed.
Browse files Browse the repository at this point in the history
Previously only the pointer was copied, thus making changes in
`actualPort` to be reflected also to `port`. This lead to some weird
result in the `updatedUploadPort` result:

{
  "stdout": "Verify 11344 bytes of flash with checksum.\nVerify successful\ndone in 0.010 seconds\nCPU reset.\n",
  "stderr": "",
  "updated_upload_port": {
    "address": "/dev/tty.usbmodem14101",     <------- this address...
    "label": "/dev/cu.usbmodem14101",        <------- ...is different from the label
    "protocol": "serial",
    "protocol_label": "Serial Port (USB)",
    "properties": {
      "pid": "0x804E",
      "serialNumber": "94A3397C5150435437202020FF150838",
      "vid": "0x2341"
    },
    "hardware_id": "94A3397C5150435437202020FF150838"
  }
}
  • Loading branch information
cmaglie committed Aug 11, 2023
1 parent 990d93a commit a750bdc
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
25 changes: 25 additions & 0 deletions arduino/discovery/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,38 @@ func (p *Port) ToRPC() *rpc.Port {
}
}

// PortFromRPCPort converts an *rpc.Port to a *Port
func PortFromRPCPort(o *rpc.Port) (p *Port) {
if o == nil {
return nil
}
return &Port{
Address: o.Address,
AddressLabel: o.Label,
Protocol: o.Protocol,
ProtocolLabel: o.ProtocolLabel,
HardwareID: o.HardwareId,
Properties: properties.NewFromHashmap(o.Properties),
}
}

func (p *Port) String() string {
if p == nil {
return "none"
}
return p.Address
}

// Clone creates a copy of this Port
func (p *Port) Clone() *Port {
if p == nil {
return nil
}
var res Port = *p
res.Properties = p.Properties.Clone()
return &res
}

// Event is a pluggable discovery event
type Event struct {
Type string
Expand Down
15 changes: 8 additions & 7 deletions commands/upload/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/arduino/arduino-cli/arduino"
"github.com/arduino/arduino-cli/arduino/cores"
"github.com/arduino/arduino-cli/arduino/cores/packagemanager"
"github.com/arduino/arduino-cli/arduino/discovery"
"github.com/arduino/arduino-cli/arduino/globals"
"github.com/arduino/arduino-cli/arduino/serialutils"
"github.com/arduino/arduino-cli/arduino/sketch"
Expand Down Expand Up @@ -212,10 +213,10 @@ func runProgramAction(pme *packagemanager.Explorer,
go f.DiscardCh(watch)
}()

port := userPort
port := discovery.PortFromRPCPort(userPort)
if port == nil || (port.Address == "" && port.Protocol == "") {
// For no-port uploads use "default" protocol
port = &rpc.Port{Protocol: "default"}
port = &discovery.Port{Protocol: "default"}
}
logrus.WithField("port", port).Tracef("Upload port")

Expand Down Expand Up @@ -421,7 +422,7 @@ func runProgramAction(pme *packagemanager.Explorer,

// If not using programmer perform some action required
// to set the board in bootloader mode
actualPort := port
actualPort := port.Clone()
if programmer == nil && !burnBootloader && (port.Protocol == "serial" || forcedSerialPortWait) {
// Perform reset via 1200bps touch if requested and wait for upload port also if requested.
touch := uploadProperties.GetBoolean("upload.use_1200bps_touch")
Expand Down Expand Up @@ -491,10 +492,10 @@ func runProgramAction(pme *packagemanager.Explorer,

// Get Port properties gathered using pluggable discovery
uploadProperties.Set("upload.port.address", port.Address)
uploadProperties.Set("upload.port.label", port.Label)
uploadProperties.Set("upload.port.label", port.AddressLabel)
uploadProperties.Set("upload.port.protocol", port.Protocol)
uploadProperties.Set("upload.port.protocolLabel", port.ProtocolLabel)
for prop, value := range actualPort.Properties {
for prop, value := range actualPort.Properties.AsMap() {
uploadProperties.Set(fmt.Sprintf("upload.port.properties.%s", prop), value)
}

Expand Down Expand Up @@ -527,7 +528,7 @@ func runProgramAction(pme *packagemanager.Explorer,
return userPort, nil
}

func detectUploadPort(uploadCtx context.Context, uploadPort *rpc.Port, watch <-chan *rpc.BoardListWatchResponse, result f.Future[*rpc.Port]) {
func detectUploadPort(uploadCtx context.Context, uploadPort *discovery.Port, watch <-chan *rpc.BoardListWatchResponse, result f.Future[*rpc.Port]) {
log := logrus.WithField("task", "port_detection")
log.Tracef("Detecting new board port after upload")

Expand All @@ -553,7 +554,7 @@ func detectUploadPort(uploadCtx context.Context, uploadPort *rpc.Port, watch <-c
}

// Pick the first port that is detected after the upload
desiredHwID := uploadPort.HardwareId
desiredHwID := uploadPort.HardwareID
timeout := time.After(5 * time.Second)
for {
select {
Expand Down

0 comments on commit a750bdc

Please sign in to comment.