MessageBlock.swift 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. //
  2. // MessageBlock.swift
  3. // OmniKit
  4. //
  5. // Created by Pete Schwamb on 10/14/17.
  6. // Copyright © 2017 Pete Schwamb. All rights reserved.
  7. //
  8. import Foundation
  9. public enum MessageBlockError: Error {
  10. case notEnoughData
  11. case unknownBlockType(rawVal: UInt8)
  12. case parseError
  13. }
  14. // See https://github.com/openaps/openomni/wiki/Message-Types
  15. public enum MessageBlockType: UInt8 {
  16. case versionResponse = 0x01
  17. case podInfoResponse = 0x02
  18. case setupPod = 0x03
  19. case errorResponse = 0x06
  20. case assignAddress = 0x07
  21. case faultConfig = 0x08
  22. case getStatus = 0x0e
  23. case acknowledgeAlert = 0x11
  24. case basalScheduleExtra = 0x13
  25. case tempBasalExtra = 0x16
  26. case bolusExtra = 0x17
  27. case configureAlerts = 0x19
  28. case setInsulinSchedule = 0x1a
  29. case deactivatePod = 0x1c
  30. case statusResponse = 0x1d
  31. case beepConfig = 0x1e
  32. case cancelDelivery = 0x1f
  33. public var blockType: MessageBlock.Type {
  34. switch self {
  35. case .versionResponse:
  36. return VersionResponse.self
  37. case .acknowledgeAlert:
  38. return AcknowledgeAlertCommand.self
  39. case .podInfoResponse:
  40. return PodInfoResponse.self
  41. case .setupPod:
  42. return SetupPodCommand.self
  43. case .errorResponse:
  44. return ErrorResponse.self
  45. case .assignAddress:
  46. return AssignAddressCommand.self
  47. case .getStatus:
  48. return GetStatusCommand.self
  49. case .basalScheduleExtra:
  50. return BasalScheduleExtraCommand.self
  51. case .bolusExtra:
  52. return BolusExtraCommand.self
  53. case .configureAlerts:
  54. return ConfigureAlertsCommand.self
  55. case .setInsulinSchedule:
  56. return SetInsulinScheduleCommand.self
  57. case .deactivatePod:
  58. return DeactivatePodCommand.self
  59. case .statusResponse:
  60. return StatusResponse.self
  61. case .tempBasalExtra:
  62. return TempBasalExtraCommand.self
  63. case .beepConfig:
  64. return BeepConfigCommand.self
  65. case .cancelDelivery:
  66. return CancelDeliveryCommand.self
  67. case .faultConfig:
  68. return FaultConfigCommand.self
  69. }
  70. }
  71. }
  72. public protocol MessageBlock {
  73. init(encodedData: Data) throws
  74. var blockType: MessageBlockType { get }
  75. var data: Data { get }
  76. }
  77. public protocol NonceResyncableMessageBlock : MessageBlock {
  78. var nonce: UInt32 { get set }
  79. }