diff --git a/Library/Classes/Implementation/DFUServiceDelegate.swift b/Library/Classes/Implementation/DFUServiceDelegate.swift index 3132987e..544ba0da 100644 --- a/Library/Classes/Implementation/DFUServiceDelegate.swift +++ b/Library/Classes/Implementation/DFUServiceDelegate.swift @@ -244,6 +244,8 @@ internal enum DFURemoteError : Int { @objc public enum DFUState : Int { /// Service is connecting to the DFU target. case connecting + /// Service is connected to the DFU target. + case connected /// DFU Service is initializing DFU operation. case starting /// DFU Service is switching the device to DFU mode. @@ -254,6 +256,8 @@ internal enum DFURemoteError : Int { case validating /// The iDevice is disconnecting or waiting for disconnection. case disconnecting + /// The iDevice is disconnected. + case disconnected /// DFU operation is completed and successful. case completed /// DFU operation was aborted. @@ -265,11 +269,13 @@ extension DFUState : CustomStringConvertible { public var description: String { switch self { case .connecting: return "Connecting" + case .connected: return "Connected" case .starting: return "Starting" case .enablingDfuMode: return "Enabling DFU Mode" case .uploading: return "Uploading" case .validating: return "Validating" // This state occurs only in Legacy DFU. case .disconnecting: return "Disconnecting" + case .disconnected: return "Disconnected" case .completed: return "Completed" case .aborted: return "Aborted" } diff --git a/Library/Classes/Implementation/GenericDFU/DFUExecutor.swift b/Library/Classes/Implementation/GenericDFU/DFUExecutor.swift index 3c3be04f..0fc42537 100644 --- a/Library/Classes/Implementation/GenericDFU/DFUExecutor.swift +++ b/Library/Classes/Implementation/GenericDFU/DFUExecutor.swift @@ -71,8 +71,18 @@ extension BaseDFUExecutor { } // MARK: - BasePeripheralDelegate API - + + func peripheralDidConnect() { + delegate { + $0.dfuStateDidChange(to: .connected) + } + } + func peripheralDidFailToConnect() { + delegate { + $0.dfuStateDidChange(to: .disconnected) + } + delegate { $0.dfuError(.failedToConnect, didOccurWithMessage: "Device failed to connect") } @@ -90,6 +100,11 @@ extension BaseDFUExecutor { peripheral.reconnect() return } + + delegate { + $0.dfuStateDidChange(to: .disconnected) + } + // Check if there was an error that needs to be reported now. delegate { $0.dfuError(error.error, didOccurWithMessage: error.message) @@ -99,6 +114,10 @@ extension BaseDFUExecutor { } func peripheralDidDisconnect(withError error: Error) { + delegate { + $0.dfuStateDidChange(to: .disconnected) + } + delegate { $0.dfuError(.deviceDisconnected, didOccurWithMessage: "\(error.localizedDescription) (code: \((error as NSError).code))") @@ -111,11 +130,16 @@ extension BaseDFUExecutor { delegate { $0.dfuStateDidChange(to: .aborted) } + // Release the cyclic reference. peripheral.destroy() } func error(_ error: DFUError, didOccurWithMessage message: String) { + delegate { + $0.dfuStateDidChange(to: .disconnected) + } + if error == .bluetoothDisabled { delegate { $0.dfuError(.bluetoothDisabled, didOccurWithMessage: message) diff --git a/Library/Classes/Implementation/GenericDFU/DFUPeripheral.swift b/Library/Classes/Implementation/GenericDFU/DFUPeripheral.swift index b5edabfd..9a3a0db2 100644 --- a/Library/Classes/Implementation/GenericDFU/DFUPeripheral.swift +++ b/Library/Classes/Implementation/GenericDFU/DFUPeripheral.swift @@ -159,22 +159,25 @@ internal class BaseDFUPeripheral