Skip to content

Commit

Permalink
#139 Add OptionSet conformance to `GATTCharacteristicExtendedProper…
Browse files Browse the repository at this point in the history
…ties`
  • Loading branch information
colemancda committed Nov 7, 2024
1 parent afce541 commit f2b96eb
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 31 deletions.
17 changes: 0 additions & 17 deletions Sources/BluetoothGATT/GATT.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,3 @@ internal enum GATTUUID: UInt16 {
return .bit16(rawValue)
}
}

// MARK: - Characteristic Extended Property

/// GATT Characteristic Extended Properties Bitfield values.
///
/// The Characteristic Extended Properties bit field describes additional
/// properties on how the Characteristic Value can be used, or how the characteristic
/// descriptors can be accessed.
@frozen
public enum GATTCharacteristicExtendedProperty: UInt8 {

/// If set, permits reliable writes of the Characteristic Value.
case reliableWrite = 0x01

///
case writableAuxiliaries = 0x02
}
62 changes: 48 additions & 14 deletions Sources/BluetoothGATT/GATTCharacteristicExtendedProperties.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@
// Copyright © 2018 PureSwift. All rights reserved.
//

import Foundation
import Bluetooth

// MARK: - Characteristic Extended Properties
/// GATT Characteristic Extended Properties Descriptor
///
/// The Characteristic Extended Properties descriptor defines additional Characteristic Properties.
Expand All @@ -17,8 +16,25 @@ import Foundation
/// The Characteristic Extended Properties descriptor is a bit field defining Reliable Write and Writeable Auxiliaries are enabled for the Characteristic.
/// This descriptor is readable without authentication and authorization being required.
@frozen
public struct GATTCharacteristicExtendedProperties: GATTDescriptor {
public struct GATTCharacteristicExtendedProperties: GATTDescriptor, OptionSet, Hashable, Sendable {

public static var uuid: BluetoothUUID { .characteristicExtendedProperties }

public var rawValue: UInt16

public init(rawValue: UInt16) {
self.rawValue = rawValue
}
}

// MARK: - ExpressibleByIntegerLiteral

extension GATTCharacteristicExtendedProperties: ExpressibleByIntegerLiteral {

public init(integerLiteral rawValue: RawValue) {
self.init(rawValue: rawValue)
}
}
public static let uuid: BluetoothUUID = .characteristicExtendedProperties

public static let length = 2
Expand Down Expand Up @@ -53,19 +69,37 @@ public struct GATTCharacteristicExtendedProperties: GATTDescriptor {
value: data,
permissions: [.read])
}

// MARK: - GATTCharacteristicExtendedProperties

extension GATTCharacteristicExtendedProperties: CustomStringConvertible, CustomDebugStringConvertible {

#if hasFeature(Embedded)
public var description: String {
"0x" + rawValue.toHexadecimal()
}
#else
@inline(never)
public var description: String {
let descriptions: [(GATTCharacteristicExtendedProperties, StaticString)] = [
(.reliableWrite, ".reliableWrite"),
(.writableAuxiliaries, ".writableAuxiliaries")
]
return buildDescription(descriptions)
}
#endif

/// A textual representation of the file permissions, suitable for debugging.
public var debugDescription: String { self.description }
}

// MARK: - Options

public extension GATTCharacteristicExtendedProperties {

/// GATT Characteristic Extended Properties Options
enum Property: UInt16, BitMaskOption {

/// Reliable Write enabled
case reliableWrite = 0b01

/// Writable Auxiliaries enabled
case writableAuxiliaries = 0b10

public static let allCases: [Property] = [.reliableWrite, .writableAuxiliaries]
}
/// Reliable Write enabled
static var reliableWrite: GATTCharacteristicExtendedProperties { 0b01 }

/// Writable Auxiliaries enabled
static var writableAuxiliaries: GATTCharacteristicExtendedProperties { 0b10 }
}

0 comments on commit f2b96eb

Please sign in to comment.