Skip to content

Commit

Permalink
#139 Add OptionSet conformance to GATTCharacteristicProperty
Browse files Browse the repository at this point in the history
  • Loading branch information
colemancda committed Nov 7, 2024
1 parent 80efe2f commit adf7efb
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 39 deletions.
39 changes: 0 additions & 39 deletions Sources/BluetoothGATT/GATT.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
// Copyright © 2016 PureSwift. All rights reserved.
//

import Foundation
@_exported import Bluetooth

/**
Expand Down Expand Up @@ -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.
Expand Down
70 changes: 70 additions & 0 deletions Sources/BluetoothGATT/GATTCharacteristicProperty.swift
Original file line number Diff line number Diff line change
@@ -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 }
}

0 comments on commit adf7efb

Please sign in to comment.