Skip to content
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

Fix: Missing error codes added #446

Merged
merged 3 commits into from
Jul 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
22 changes: 19 additions & 3 deletions iOSDFULibrary/Classes/Implementation/DFUServiceDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@

import Foundation

internal enum DFURemoteError : Int {
case legacy = 0
case secure = 10
case secureExtended = 20
case buttonless = 90
case experimentalButtonless = 9000

func with(code: UInt8) -> DFUError {
return DFUError(rawValue: Int(code) + rawValue)!
}
}

@objc public enum DFUError : Int {
// Legacy DFU errors.
case remoteLegacyDFUSuccess = 1
Expand All @@ -52,6 +64,7 @@ import Foundation

// This error will no longer be reported.
case remoteSecureDFUExtendedError = 21 // 10 + 11

// Instead, one of the extended errors below will used.
case remoteExtendedErrorWrongCommandFormat = 22 // 20 + 0x02
case remoteExtendedErrorUnknownCommand = 23 // 20 + 0x03
Expand All @@ -74,9 +87,12 @@ import Foundation

// Buttonless DFU errors (received value + 90 as they overlap legacy
// and secure DFU errors).
case remoteButtonlessDFUSuccess = 91 // 90 + 1
case remoteButtonlessDFUOpCodeNotSupported = 92 // 90 + 2
case remoteButtonlessDFUOperationFailed = 94 // 90 + 4
case remoteButtonlessDFUSuccess = 91 // 90 + 1
case remoteButtonlessDFUOpCodeNotSupported = 92 // 90 + 2
case remoteButtonlessDFUOperationFailed = 94 // 90 + 4
case remoteButtonlessDFUInvalidAdvertisementName = 95 // 90 + 5
case remoteButtonlessDFUBusy = 96 // 90 + 6
case remoteButtonlessDFUNotBonded = 97 // 90 + 7

/// Providing the DFUFirmware is required.
case fileNotSpecified = 101
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,14 @@ internal enum DFUResultCode : UInt8 {
case crcError = 5
case operationFailed = 6

var code: UInt8 {
return rawValue
}

var error: DFUError {
return DFURemoteError.legacy.with(code: code)
}

var description: String {
switch self {
case .success: return "Success"
Expand All @@ -130,10 +138,6 @@ internal enum DFUResultCode : UInt8 {
case .operationFailed: return "Operation failed"
}
}

var code: UInt8 {
return rawValue
}
}

internal struct Response {
Expand Down Expand Up @@ -455,7 +459,7 @@ internal struct PacketReceiptNotification {
success?()
} else {
logger.e("Error \(response.status.code): \(response.status.description)")
report?(DFUError(rawValue: Int(response.status.rawValue))!, response.status.description)
report?(response.status.error, response.status.description)
}
} else {
logger.e("Unknown response received: 0x\(characteristicValue.hexString)")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ internal enum ButtonlessDFUResultCode : UInt8 {
/// The request was rejected because no bond was created.
case notBonded = 0x07

var code: UInt8 {
return rawValue
}

func error(ofType remoteError: DFURemoteError) -> DFUError {
return remoteError.with(code: code)
}

var description: String {
switch self {
case .success: return "Success"
Expand All @@ -69,17 +77,13 @@ internal enum ButtonlessDFUResultCode : UInt8 {
case .notBonded: return "Device not bonded"
}
}

var code: UInt8 {
return rawValue
}
}

internal enum ButtonlessDFURequest {
case enterBootloader
case set(name : String)
case set(name: String)

var data : Data {
var data: Data {
switch self {
case .enterBootloader:
return Data([ButtonlessDFUOpCode.enterBootloader.code])
Expand Down Expand Up @@ -142,6 +146,14 @@ internal class ButtonlessDFU : NSObject, CBPeripheralDelegate, DFUCharacteristic
|| characteristic.uuid.isEqual(uuidHelper.buttonlessWithoutBonds)
}

/**
Returns whether the characteristic is an instance of Experimental Buttonless
DFU Service from SDK 12.
*/
internal var isExperimental: Bool {
return characteristic.uuid.isEqual(uuidHelper.buttonlessExperimentalCharacteristic)
}

/**
Returns `true` for a buttonless DFU characteristic that may support setting
bootloader's name. This feature has been added in SDK 14.0 to Buttonless
Expand Down Expand Up @@ -306,11 +318,10 @@ internal class ButtonlessDFU : NSObject, CBPeripheralDelegate, DFUCharacteristic

guard dfuResponse.status == .success else {
logger.e("Error \(dfuResponse.status.code): \(dfuResponse.status.description)")
// The returned errod code is incremented by 90 or 9000 to match Buttonless DFU or
// Experimental Buttonless DFU remote codes.
// See DFUServiceDelegate.swift -> DFUError.
let offset = characteristic.uuid.isEqual(uuidHelper.buttonlessExperimentalCharacteristic) ? 9000 : 90
report?(DFUError(rawValue: Int(dfuResponse.status.code) + offset)!, dfuResponse.status.description)
let type = isExperimental ?
DFURemoteError.experimentalButtonless :
DFURemoteError.buttonless
report?(dfuResponse.status.error(ofType: type), dfuResponse.status.description)
return
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ internal enum SecureDFUExtendedErrorCode : UInt8 {
return rawValue
}

var error: DFUError {
return DFURemoteError.secureExtended.with(code: code)
}

var description: String {
switch self {
case .noError: return "No error"
Expand Down Expand Up @@ -155,6 +159,14 @@ internal enum SecureDFUResultCode : UInt8 {
case operationFailed = 0x0A
case extendedError = 0x0B

var code: UInt8 {
return rawValue
}

var error: DFUError {
return DFURemoteError.secure.with(code: code)
}

var description: String {
switch self {
case .invalidCode: return "Invalid code"
Expand All @@ -170,10 +182,6 @@ internal enum SecureDFUResultCode : UInt8 {
case .extendedError: return "Extended error"
}
}

var code: UInt8 {
return rawValue
}
}

internal typealias SecureDFUResponseCallback = (_ response : SecureDFUResponse) -> Void
Expand Down Expand Up @@ -576,12 +584,10 @@ internal class SecureDFUControlPoint : NSObject, CBPeripheralDelegate, DFUCharac
case .extendedError:
// An extended error was received.
logger.e("Error \(dfuResponse.error!.code): \(dfuResponse.error!.description)")
// The returned errod code is incremented by 20 to match Secure DFU remote codes.
report?(DFUError(rawValue: Int(dfuResponse.error!.code) + 20)!, dfuResponse.error!.description)
report?(dfuResponse.error!.error, dfuResponse.error!.description)
default:
logger.e("Error \(dfuResponse.status.code): \(dfuResponse.status.description)")
// The returned errod code is incremented by 10 to match Secure DFU remote codes.
report?(DFUError(rawValue: Int(dfuResponse.status.code) + 10)!, dfuResponse.status.description)
report?(dfuResponse.status.error, dfuResponse.status.description)
}
}

Expand Down