diff --git a/Sources/BluetoothGATT/GATT.swift b/Sources/BluetoothGATT/GATT.swift index eab54e97c..090415568 100644 --- a/Sources/BluetoothGATT/GATT.swift +++ b/Sources/BluetoothGATT/GATT.swift @@ -6,7 +6,6 @@ // Copyright © 2016 PureSwift. All rights reserved. // -import Foundation @_exported import Bluetooth /** @@ -34,44 +33,6 @@ internal enum GATTUUID: UInt16 { } } -// MARK: - Characteristic Property - -/// GATT Characteristic Properties Bitfield valuess -@frozen -public enum GATTCharacteristicProperty: UInt8, BitMaskOption { - - case broadcast = 0x01 - case read = 0x02 - case writeWithoutResponse = 0x04 - case write = 0x08 - case notify = 0x10 - case indicate = 0x20 - - /// Characteristic supports write with signature - case signedWrite = 0x40 // BT_GATT_CHRC_PROP_AUTH - - case extendedProperties = 0x80 -} - -// MARK: CustomStringConvertible - -extension GATTCharacteristicProperty: CustomStringConvertible { - - public var description: String { - - switch self { - case .broadcast: return "Broadcast" - case .read: return "Read" - case .write: return "Write" - case .writeWithoutResponse: return "Write without Response" - case .notify: return "Notify" - case .indicate: return "Indicate" - case .signedWrite: return "Signed Write" - case .extendedProperties: return "Extended Properties" - } - } -} - // MARK: - Characteristic Extended Property /// GATT Characteristic Extended Properties Bitfield values. diff --git a/Sources/BluetoothGATT/GATTCharacteristicProperty.swift b/Sources/BluetoothGATT/GATTCharacteristicProperty.swift new file mode 100644 index 000000000..a89a0f444 --- /dev/null +++ b/Sources/BluetoothGATT/GATTCharacteristicProperty.swift @@ -0,0 +1,70 @@ +// +// GATTCharacteristicProperty.swift +// Bluetooth +// +// Created by Alsey Coleman Miller on 11/7/24. +// + +/// GATT Characteristic Properties Bitfield valuess +public struct GATTCharacteristicProperty: OptionSet, Hashable, Sendable { + + public var rawValue: UInt8 + + public init(rawValue: UInt8) { + self.rawValue = rawValue + } +} + +// MARK: - ExpressibleByIntegerLiteral + +extension GATTCharacteristicProperty: ExpressibleByIntegerLiteral { + + public init(integerLiteral value: UInt8) { + self.rawValue = value + } +} + +// MARK: CustomStringConvertible + +extension GATTCharacteristicProperty: CustomStringConvertible, CustomDebugStringConvertible { + + #if hasFeature(Embedded) + public var description: String { + rawValue.description + } + #else + @inline(never) + public var description: String { + let descriptions: [(GATTCharacteristicProperty, StaticString)] = [ + (.broadcast, ".broadcast"), + (.read, ".read"), + (.write, ".write"), + (.notify, ".notify"), + (.indicate, ".indicate"), + (.signedWrite, ".signedWrite"), + (.extendedProperties, ".extendedProperties") + ] + return buildDescription(descriptions) + } + #endif + + /// A textual representation of the file permissions, suitable for debugging. + public var debugDescription: String { self.description } +} + +// MARK: - Options + +public extension GATTCharacteristicProperty { + + static var broadcast: GATTCharacteristicProperty { 0x01 } + static var read: GATTCharacteristicProperty { 0x02 } + static var writeWithoutResponse: GATTCharacteristicProperty { 0x04 } + static var write: GATTCharacteristicProperty { 0x08 } + static var notify: GATTCharacteristicProperty { 0x10 } + static var indicate: GATTCharacteristicProperty { 0x20 } + + /// Characteristic supports write with signature + static var signedWrite: GATTCharacteristicProperty { 0x40 } // BT_GATT_CHRC_PROP_AUTH + + static var extendedProperties: GATTCharacteristicProperty { 0x80 } +}