diff --git a/Sources/BluetoothGATT/GATTClientCharacteristicConfiguration.swift b/Sources/BluetoothGATT/GATTClientCharacteristicConfiguration.swift index 0157f2c34..5130ab496 100644 --- a/Sources/BluetoothGATT/GATTClientCharacteristicConfiguration.swift +++ b/Sources/BluetoothGATT/GATTClientCharacteristicConfiguration.swift @@ -6,7 +6,7 @@ // Copyright © 2018 PureSwift. All rights reserved. // -import Foundation +import Bluetooth /// GATT Client Characteristic Configuration Descriptor /// @@ -20,57 +20,57 @@ import Foundation /// The default value for the Client Characteristic Configuration descriptor is `0x00`. /// Upon connection of non-binded clients, this descriptor is set to the default value. @frozen -public struct GATTClientCharacteristicConfiguration: GATTDescriptor { +public struct GATTClientCharacteristicConfiguration: GATTDescriptor, OptionSet, Hashable, Sendable { - public static let uuid: BluetoothUUID = .clientCharacteristicConfiguration + public static var uuid: BluetoothUUID { .clientCharacteristicConfiguration } - public static let length = 2 + public var rawValue: UInt16 - public var configuration: BitMaskOptionSet - - public init(configuration: BitMaskOptionSet = []) { - - self.configuration = configuration + public init(rawValue: UInt16) { + self.rawValue = rawValue } +} + +// MARK: - ExpressibleByIntegerLiteral + +extension GATTClientCharacteristicConfiguration: ExpressibleByIntegerLiteral { - public init?(data: Data) { - - guard data.count == type(of: self).length - else { return nil } - - let rawValue = UInt16(littleEndian: UInt16(bytes: (data[0], data[1]))) - - self.configuration = BitMaskOptionSet(rawValue: rawValue) + public init(integerLiteral rawValue: RawValue) { + self.init(rawValue: rawValue) } +} + + +// MARK: - Options + +public extension GATTClientCharacteristicConfiguration { - public var data: Data { - - let bytes = configuration.rawValue.littleEndian.bytes - - return Data([bytes.0, bytes.1]) - } + /// Notifications enabled + static var notify: GATTClientCharacteristicConfiguration { 0b01 } - public var descriptor: GATTAttribute.Descriptor { - - return GATTAttribute.Descriptor(uuid: type(of: self).uuid, - value: data, - permissions: [.read, .write]) - } + /// Indications enabled + static var indicate: GATTClientCharacteristicConfiguration { 0b10 } } -// MARK: - Supporting Types +// MARK: - CustomStringConvertible -public extension GATTClientCharacteristicConfiguration { +extension GATTClientCharacteristicConfiguration: CustomStringConvertible, CustomDebugStringConvertible { - /// GATT Client Characteristic Configuration Options - enum Configuration: UInt16, BitMaskOption { - - /// Notifications enabled - case notify = 0b01 - - /// Indications enabled - case indicate = 0b10 - - public static let allCases: [Configuration] = [.notify, .indicate] + #if hasFeature(Embedded) + public var description: String { + "0x" + rawValue.toHexadecimal() + } + #else + @inline(never) + public var description: String { + let descriptions: [(GATTClientCharacteristicConfiguration, StaticString)] = [ + (.notify, ".notify"), + (.indicate, ".indicate") + ] + return buildDescription(descriptions) } + #endif + + /// A textual representation of the file permissions, suitable for debugging. + public var debugDescription: String { self.description } }