Skip to content

Commit

Permalink
Merge pull request #174 from NordicSemiconductor/reconnect-change
Browse files Browse the repository at this point in the history
Fix: Ignore FirmwareUpgradeManagerConfiguration's 'estimatedSwapTime'…
  • Loading branch information
dinesharjani authored Oct 31, 2023
2 parents 9024125 + 22e8ee5 commit b4124fa
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
31 changes: 22 additions & 9 deletions Source/Managers/DFU/FirmwareUpgradeManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -822,8 +822,8 @@ public class FirmwareUpgradeManager : FirmwareUpgradeController, ConnectionObser
guard state == .disconnected else {
return
}
self.log(msg: "Device has disconnected", atLevel: .info)
self.log(msg: "Reconnecting...", atLevel: .verbose)

self.log(msg: "Device Has Disconnected", atLevel: .verbose)
let timeSinceReset: TimeInterval
if let resetResponseTime = resetResponseTime {
let now = Date()
Expand All @@ -833,12 +833,20 @@ public class FirmwareUpgradeManager : FirmwareUpgradeController, ConnectionObser
timeSinceReset = 0
}
let remainingTime = configuration.estimatedSwapTime - timeSinceReset
if remainingTime > 0 {
DispatchQueue.main.asyncAfter(deadline: .now() + remainingTime) { [weak self] in
self?.reconnect()
}
} else {

// If DirectXIP, regardless of variant, there's no swap time. So we try to reconnect
// immediately.
let waitForReconnectRequired = !configuration.bootloaderMode.isDirectXIP
&& remainingTime > .leastNonzeroMagnitude
guard waitForReconnectRequired else {
reconnect()
return
}

self.log(msg: "Waiting \(Int(configuration.estimatedSwapTime)) (Swap Time) Seconds Before Attempting to Reconnect...", atLevel: .info)
DispatchQueue.main.asyncAfter(deadline: .now() + remainingTime) { [weak self] in
self?.log(msg: "Reconnecting...", atLevel: .info)
self?.reconnect()
}
}

Expand Down Expand Up @@ -898,8 +906,13 @@ private extension FirmwareUpgradeManager {

public struct FirmwareUpgradeConfiguration: Codable {

/// Estimated time required for swapping images, in seconds.
/// If the mode is set to `.testAndConfirm`, the manager will try to reconnect after this time. 0 by default.
/**
Estimated time required for swapping images, in seconds.
If the mode is set to `.testAndConfirm`, the manager will try to reconnect after this time. 0 by default.
Note: This setting is ignored if `bootloaderMode` is in any DirectXIP variant, since there's no swap whatsoever when DirectXIP is involved. Hence, why we can upload the same Image (though different hash) to either slot.
*/
public var estimatedSwapTime: TimeInterval
/// If enabled, after successful upload but before test/confirm/reset phase, an Erase App Settings Command will be sent and awaited before proceeding.
public var eraseAppSettings: Bool
Expand Down
9 changes: 9 additions & 0 deletions Source/McuMgrResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,15 @@ public final class BootloaderInfoResponse: McuMgrResponse {
case directXIPWithRevert = 5
case RAMLoader = 6

/**
Intended for use cases where it's not important to know what kind of DirectXIP
variant this Bootloader Mode might represent, but instead, whether it's
DirectXIP or not.
*/
public var isDirectXIP: Bool {
return self == .directXIPNoRevert || self == .directXIPWithRevert
}

public var debugDescription: String {
switch self {
case .unknown:
Expand Down

0 comments on commit b4124fa

Please sign in to comment.