Skip to content

Commit

Permalink
#139 Add OptionSet conformance to `GATTServerCharacteristicConfigur…
Browse files Browse the repository at this point in the history
…ation`
  • Loading branch information
colemancda committed Nov 8, 2024
1 parent 02c7197 commit 521d22d
Showing 1 changed file with 39 additions and 35 deletions.
74 changes: 39 additions & 35 deletions Sources/BluetoothGATT/GATTServerCharacteristicConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// Copyright © 2018 PureSwift. All rights reserved.
//

import Foundation
import Bluetooth

// MARK: - Server Characteristic Configuration
/// GATT Server Characteristic Configuration Descriptor
Expand All @@ -18,50 +18,54 @@ import Foundation
/// There is a single instantiation of this descriptor for all clients.
/// Authentication and authorization may be required by the server to write this descriptor.
@frozen
public struct GATTServerCharacteristicConfiguration: GATTDescriptor {
public struct GATTServerCharacteristicConfiguration: GATTDescriptor, OptionSet, Hashable, Sendable {

public static let uuid: BluetoothUUID = .serverCharacteristicConfiguration
public static var uuid: BluetoothUUID { .serverCharacteristicConfiguration }

public static let length = 1
public var rawValue: UInt8

public var serverConfiguration: BitMaskOptionSet<ServerConfiguration>

public init(serverConfiguration: BitMaskOptionSet<ServerConfiguration> = []) {

self.serverConfiguration = serverConfiguration
public init(rawValue: UInt8) {
self.rawValue = rawValue
}
}

// MARK: - ExpressibleByIntegerLiteral

extension GATTServerCharacteristicConfiguration: ExpressibleByIntegerLiteral {

public init?(data: Data) {

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

let rawValue = data[0]

self.serverConfiguration = BitMaskOptionSet<ServerConfiguration>(rawValue: rawValue)
public init(integerLiteral rawValue: RawValue) {
self.init(rawValue: rawValue)
}
}



// MARK: - CustomStringConvertible

extension GATTServerCharacteristicConfiguration: CustomStringConvertible, CustomDebugStringConvertible {

public var data: Data {
return Data([serverConfiguration.rawValue])
#if hasFeature(Embedded)
public var description: String {
"0x" + rawValue.toHexadecimal()
}

public var descriptor: GATTAttribute.Descriptor {

return GATTAttribute.Descriptor(uuid: type(of: self).uuid,
value: data,
permissions: [.read, .write])
#else
@inline(never)
public var description: String {
let descriptions: [(GATTServerCharacteristicConfiguration, StaticString)] = [
(.broadcasts, ".broadcasts")
]
return buildDescription(descriptions)
}
#endif

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

extension GATTServerCharacteristicConfiguration {
// MARK: - Options

public extension GATTServerCharacteristicConfiguration {

/// GATT Server Characteristics Configuration Options
public enum ServerConfiguration: UInt8, BitMaskOption {

/// Broadcasts enabled
case broadcasts = 0b01

public static let allCases: [ServerConfiguration] = [.broadcasts]
}
/// Broadcasts enabled
static var broadcasts: GATTServerCharacteristicConfiguration { 0b01 }
}

0 comments on commit 521d22d

Please sign in to comment.