diff --git a/Sources/BluetoothGATT/GATTTimeTriggerSetting.swift b/Sources/BluetoothGATT/GATTTimeTriggerSetting.swift index a8ef46c9c..c59330074 100644 --- a/Sources/BluetoothGATT/GATTTimeTriggerSetting.swift +++ b/Sources/BluetoothGATT/GATTTimeTriggerSetting.swift @@ -6,39 +6,33 @@ // Copyright © 2018 PureSwift. All rights reserved. // -import Foundation +import Bluetooth -/// GATT Time Trigger Setting Condition -@frozen -public enum GATTTimeTriggerSettingCondition: UInt8 { - - case none = 0x00 - case indicateTimeInterval = 0x01 - case timeInterval = 0x02 - case count = 0x03 -} - -// MARK: - Time Trigger Setting /// GATT Time Trigger Setting Descriptor /// /// The value of the descriptor has two parts. /// Part one is a condition field and occupies one octet, and part two is the comparison value (trigger point) that the characteristic value is checked against. @frozen -public enum GATTTimeTriggerSetting: GATTDescriptor { - - public static let uuid: BluetoothUUID = .timeTriggerSetting - - public static let minimumLength = 2 +public enum GATTTimeTriggerSetting: GATTDescriptor, Hashable, Sendable { + public static var uuid: BluetoothUUID { .timeTriggerSetting } + case valueC1(UInt8) case valueC2(UInt8, UInt8, UInt8) case valueC3(UInt16) +} + +// MARK: - DataConvertible + +extension GATTTimeTriggerSetting: DataConvertible { - public init?(data: Data) { + internal static var minimumLength: Int { 2 } + + public init?(data: Data) { - guard data.count >= type(of: self).minimumLength + guard data.count >= Self.minimumLength else { return nil } guard let condition = GATTTimeTriggerSettingCondition(rawValue: data[0]) @@ -70,33 +64,45 @@ public enum GATTTimeTriggerSetting: GATTDescriptor { } } - public var data: Data { - + public func append(to data: inout Data) where Data : DataContainer { switch self { - case let .valueC1(byte): - let conditionByte = GATTTimeTriggerSettingCondition.none.rawValue - return Data([conditionByte, byte]) - + data += conditionByte + data += byte case let .valueC2(byte0, byte1, byte2): - let conditionByte = GATTTimeTriggerSettingCondition.timeInterval.rawValue - return Data([conditionByte, byte0, byte1, byte2]) - - case let .valueC3(byte): - + data += conditionByte + data += byte0 + data += byte1 + data += byte2 + case let .valueC3(value): let conditionByte = GATTTimeTriggerSettingCondition.count.rawValue - let bytes = byte.bytes - return Data([conditionByte, bytes.0, bytes.1]) - + data += conditionByte + data += value.littleEndian } } - public var descriptor: GATTAttribute.Descriptor { - - return GATTAttribute.Descriptor(uuid: type(of: self).uuid, - value: data, - permissions: [.read]) + public var dataLength: Int { + switch self { + case .valueC1: + return 2 + case .valueC2: + return 4 + case .valueC3: + return 3 + } } } + +// MARK: - Supporting Types + +/// GATT Time Trigger Setting Condition +@frozen +public enum GATTTimeTriggerSettingCondition: UInt8, CaseIterable, Sendable { + + case none = 0x00 + case indicateTimeInterval = 0x01 + case timeInterval = 0x02 + case count = 0x03 +}