ErrorResponse.swift 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. //
  2. // ErrorResponse.swift
  3. // OmniKit
  4. //
  5. // Created by Pete Schwamb on 2/25/18.
  6. // Copyright © 2018 Pete Schwamb. All rights reserved.
  7. //
  8. import Foundation
  9. fileprivate let errorResponseCode_badNonce: UInt8 = 0x14
  10. public enum ErrorResponseType {
  11. case badNonce(nonceResyncKey: UInt16)
  12. case nonretryableError(code: UInt8, faultEventCode: FaultEventCode, podProgress: PodProgressStatus)
  13. }
  14. // 06 14 WWWW, WWWW is the encoded nonce resync key
  15. // 06 EE FF0P, EE != 0x14, FF = fault code (if any), 0P = pod progress status (1..15)
  16. public struct ErrorResponse : MessageBlock {
  17. public let blockType: MessageBlockType = .errorResponse
  18. public let errorResponseType: ErrorResponseType
  19. public let data: Data
  20. public init(encodedData: Data) throws {
  21. let errorCode = encodedData[2]
  22. switch (errorCode) {
  23. case errorResponseCode_badNonce:
  24. // For this error code only the 2 next bytes are the encoded nonce resync key.
  25. let nonceResyncKey: UInt16 = encodedData[3...].toBigEndian(UInt16.self)
  26. errorResponseType = .badNonce(nonceResyncKey: nonceResyncKey)
  27. break
  28. default:
  29. // All other error codes are some non-retryable command error. In this case,
  30. // the next 2 bytes are any saved fault code (typically 0) and the pod progress value.
  31. let faultEventCode = FaultEventCode(rawValue: encodedData[3])
  32. guard let podProgress = PodProgressStatus(rawValue: encodedData[4]) else {
  33. throw MessageError.unknownValue(value: encodedData[4], typeDescription: "ErrorResponse PodProgressStatus")
  34. }
  35. errorResponseType = .nonretryableError(code: errorCode, faultEventCode: faultEventCode, podProgress: podProgress)
  36. break
  37. }
  38. self.data = encodedData
  39. }
  40. }