Skip to content

Commit

Permalink
#161 Add DataConvertible conformance to GATTTimeTriggerSetting
Browse files Browse the repository at this point in the history
  • Loading branch information
colemancda committed Nov 11, 2024
1 parent 41c7290 commit 815994b
Showing 1 changed file with 44 additions and 38 deletions.
82 changes: 44 additions & 38 deletions Sources/BluetoothGATT/GATTTimeTriggerSetting.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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: DataContainer>(data: Data) {

guard data.count >= type(of: self).minimumLength
guard data.count >= Self.minimumLength
else { return nil }

guard let condition = GATTTimeTriggerSettingCondition(rawValue: data[0])
Expand Down Expand Up @@ -70,33 +64,45 @@ public enum GATTTimeTriggerSetting: GATTDescriptor {
}
}

public var data: Data {

public func append<Data>(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
}

0 comments on commit 815994b

Please sign in to comment.