8000 feature: Detect board port change after upload by cmaglie · Pull Request #2253 · arduino/arduino-cli · GitHub
[go: up one dir, main page]

Skip to content

feature: Detect board port change after upload #2253

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 18 commits into from
Aug 18, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Perform a deep-copy of upload ports where needed.
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
commit a750bdc69a7b835bdd82d61f392e666fa54e04ec
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