Skip to content

Commit 862bbe2

Browse files
committed
Improved tracking algorithm for upload-port reconnection
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
1 parent e12a068 commit 862bbe2

File tree

1 file changed

+29
-9
lines changed

1 file changed

+29
-9
lines changed

Diff for: commands/upload/upload.go

+29-9
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,6 @@ func detectUploadPort(
556556
}
557557

558558
// Pick the first port that is detected after the upload
559-
desiredHwID := uploadPort.HardwareID
560559
timeout := time.After(5 * time.Second)
561560
if !waitForUploadPort {
562561
timeout = time.After(time.Second)
@@ -582,20 +581,41 @@ func detectUploadPort(
582581
log.WithField("event", ev).Trace("Ignored non-add event")
583582
continue
584583
}
584+
585+
portPriority := func(port *discovery.Port) int {
586+
if port == nil {
587+
return 0
588+
}
589+
prio := 0
590+
if port.HardwareID == uploadPort.HardwareID {
591+
prio += 1000
592+
}
593+
if port.Protocol == uploadPort.Protocol {
594+
prio += 100
595+
}
596+
if port.Address == uploadPort.Address {
597+
prio += 10
598+
}
599+
return prio
600+
}
601+
evPortPriority := portPriority(ev.Port)
602+
candidatePriority := portPriority(candidate)
603+
if evPortPriority <= candidatePriority {
604+
log.WithField("event", ev).Tracef("New upload port candidate is worse than the current one (prio=%d)", evPortPriority)
605+
continue
606+
}
607+
log.WithField("event", ev).Tracef("Found new upload port candidate (prio=%d)", evPortPriority)
585608
candidate = ev.Port
586-
log.WithField("event", ev).Trace("New upload port candidate")
587609

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

595-
log.Trace("Found new upload port!")
596-
return
597617
case <-timeout:
598-
log.Trace("Timeout waiting for candidate port")
618+
log.WithField("selected_port", candidate).Trace("Timeout waiting for candidate port")
599619
return
600620
}
601621
}

0 commit comments

Comments
 (0)