Skip to content

Commit

Permalink
Improved tracking algorithm for upload-port reconnection
Browse files Browse the repository at this point in the history
The new algorithm takes into account the case where a single board may
expose multiple ports, in this case the selection will increase priority
to ports that:

  1. have the same HW id as the user specified port for upload
  2. have the same protocol as the user specified port for upload
  3. have the same address as the user specified port for upload
  • Loading branch information
cmaglie committed Aug 16, 2023
1 parent e12a068 commit 862bbe2
Showing 1 changed file with 29 additions and 9 deletions.
38 changes: 29 additions & 9 deletions commands/upload/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,6 @@ func detectUploadPort(
}

// Pick the first port that is detected after the upload
desiredHwID := uploadPort.HardwareID
timeout := time.After(5 * time.Second)
if !waitForUploadPort {
timeout = time.After(time.Second)
Expand All @@ -582,20 +581,41 @@ func detectUploadPort(
log.WithField("event", ev).Trace("Ignored non-add event")
continue
}

portPriority := func(port *discovery.Port) int {
if port == nil {
return 0
}
prio := 0
if port.HardwareID == uploadPort.HardwareID {
prio += 1000
}
if port.Protocol == uploadPort.Protocol {
prio += 100
}
if port.Address == uploadPort.Address {
prio += 10
}
return prio
}
evPortPriority := portPriority(ev.Port)
candidatePriority := portPriority(candidate)
if evPortPriority <= candidatePriority {
log.WithField("event", ev).Tracef("New upload port candidate is worse than the current one (prio=%d)", evPortPriority)
continue
}
log.WithField("event", ev).Tracef("Found new upload port candidate (prio=%d)", evPortPriority)
candidate = ev.Port
log.WithField("event", ev).Trace("New upload port candidate")

// If the current candidate port does not have the desired HW-ID do
// not return it immediately.
if desiredHwID != "" && candidate.HardwareID != desiredHwID {
log.Trace("New candidate port did not match desired HW ID, keep watching...")
// If the current candidate have the desired HW-ID return it quickly.
if candidate.HardwareID == ev.Port.HardwareID {
timeout = time.After(time.Second)
log.Trace("New candidate port match the desired HW ID, timeout reduced to 1 second.")
continue
}

log.Trace("Found new upload port!")
return
case <-timeout:
log.Trace("Timeout waiting for candidate port")
log.WithField("selected_port", candidate).Trace("Timeout waiting for candidate port")
return
}
}
Expand Down

0 comments on commit 862bbe2

Please sign in to comment.