diff --git a/Sources/BluetoothGAP/Extensions/UUID.swift b/Sources/BluetoothGAP/Extensions/UUID.swift index 9078ca12e..5dc4f68c6 100644 --- a/Sources/BluetoothGAP/Extensions/UUID.swift +++ b/Sources/BluetoothGAP/Extensions/UUID.swift @@ -14,28 +14,6 @@ internal extension UUID { static var length: Int { return 16 } } -internal extension UUID { // ByteValue - - typealias ByteValue = uuid_t - - static var bitWidth: Int { return 128 } - - @inline(__always) - init(bytes: uuid_t) { - - self.init(uuid: bytes) - } - - var bytes: uuid_t { - - @inline(__always) - get { return uuid } - - @inline(__always) - set { self = UUID(uuid: newValue) } - } -} - internal extension UUID { init?(data: Data) { diff --git a/Sources/BluetoothGAP/GAPCompleteListOf128BitServiceClassUUIDs.swift b/Sources/BluetoothGAP/GAPCompleteListOf128BitServiceClassUUIDs.swift index 7062f7663..a4cfb8fbc 100644 --- a/Sources/BluetoothGAP/GAPCompleteListOf128BitServiceClassUUIDs.swift +++ b/Sources/BluetoothGAP/GAPCompleteListOf128BitServiceClassUUIDs.swift @@ -58,10 +58,11 @@ extension GAPCompleteListOf128BitServiceClassUUIDs: ExpressibleByArrayLiteral { // MARK: - CustomStringConvertible +#if !hasFeature(Embedded) extension GAPCompleteListOf128BitServiceClassUUIDs: CustomStringConvertible { public var description: String { - return uuids.description } } +#endif diff --git a/Sources/BluetoothGAP/GAPCompleteListOf16BitServiceClassUUIDs.swift b/Sources/BluetoothGAP/GAPCompleteListOf16BitServiceClassUUIDs.swift index 0fc0e97b2..80eb0a5c0 100644 --- a/Sources/BluetoothGAP/GAPCompleteListOf16BitServiceClassUUIDs.swift +++ b/Sources/BluetoothGAP/GAPCompleteListOf16BitServiceClassUUIDs.swift @@ -58,10 +58,11 @@ extension GAPCompleteListOf16BitServiceClassUUIDs: ExpressibleByArrayLiteral { // MARK: - CustomStringConvertible +#if !hasFeature(Embedded) extension GAPCompleteListOf16BitServiceClassUUIDs: CustomStringConvertible { public var description: String { - return uuids.map { BluetoothUUID.bit16($0) }.description } } +#endif diff --git a/Sources/BluetoothGAP/GAPCompleteListOf32BitServiceClassUUIDs.swift b/Sources/BluetoothGAP/GAPCompleteListOf32BitServiceClassUUIDs.swift index deaebdcd4..9a0457aa9 100644 --- a/Sources/BluetoothGAP/GAPCompleteListOf32BitServiceClassUUIDs.swift +++ b/Sources/BluetoothGAP/GAPCompleteListOf32BitServiceClassUUIDs.swift @@ -58,10 +58,11 @@ extension GAPCompleteListOf32BitServiceClassUUIDs: ExpressibleByArrayLiteral { // MARK: - CustomStringConvertible +#if !hasFeature(Embedded) extension GAPCompleteListOf32BitServiceClassUUIDs: CustomStringConvertible { public var description: String { - return uuids.map { BluetoothUUID.bit32($0) }.description } } +#endif diff --git a/Sources/BluetoothGAP/GAPIncompleteListOf128BitServiceClassUUIDs.swift b/Sources/BluetoothGAP/GAPIncompleteListOf128BitServiceClassUUIDs.swift index 2c22175b1..081e37b70 100644 --- a/Sources/BluetoothGAP/GAPIncompleteListOf128BitServiceClassUUIDs.swift +++ b/Sources/BluetoothGAP/GAPIncompleteListOf128BitServiceClassUUIDs.swift @@ -58,10 +58,11 @@ extension GAPIncompleteListOf128BitServiceClassUUIDs: ExpressibleByArrayLiteral // MARK: - CustomStringConvertible +#if !hasFeature(Embedded) extension GAPIncompleteListOf128BitServiceClassUUIDs: CustomStringConvertible { public var description: String { - return uuids.description } } +#endif diff --git a/Sources/BluetoothGAP/GAPIncompleteListOf16BitServiceClassUUIDs.swift b/Sources/BluetoothGAP/GAPIncompleteListOf16BitServiceClassUUIDs.swift index 9a63c86bd..b10110bd0 100644 --- a/Sources/BluetoothGAP/GAPIncompleteListOf16BitServiceClassUUIDs.swift +++ b/Sources/BluetoothGAP/GAPIncompleteListOf16BitServiceClassUUIDs.swift @@ -58,10 +58,11 @@ extension GAPIncompleteListOf16BitServiceClassUUIDs: ExpressibleByArrayLiteral { // MARK: - CustomStringConvertible +#if !hasFeature(Embedded) extension GAPIncompleteListOf16BitServiceClassUUIDs: CustomStringConvertible { public var description: String { - return uuids.map { BluetoothUUID.bit16($0) }.description } } +#endif diff --git a/Sources/BluetoothGAP/GAPIncompleteListOf32BitServiceClassUUIDs.swift b/Sources/BluetoothGAP/GAPIncompleteListOf32BitServiceClassUUIDs.swift index 598559416..67bb56c80 100644 --- a/Sources/BluetoothGAP/GAPIncompleteListOf32BitServiceClassUUIDs.swift +++ b/Sources/BluetoothGAP/GAPIncompleteListOf32BitServiceClassUUIDs.swift @@ -58,10 +58,11 @@ extension GAPIncompleteListOf32BitServiceClassUUIDs: ExpressibleByArrayLiteral { // MARK: - CustomStringConvertible +#if !hasFeature(Embedded) extension GAPIncompleteListOf32BitServiceClassUUIDs: CustomStringConvertible { public var description: String { - return uuids.map { BluetoothUUID.bit32($0) }.description } } +#endif diff --git a/Sources/BluetoothGAP/GAPListOf128BitServiceSolicitationUUIDs.swift b/Sources/BluetoothGAP/GAPListOf128BitServiceSolicitationUUIDs.swift index 3d0904f5a..1c173f8d9 100644 --- a/Sources/BluetoothGAP/GAPListOf128BitServiceSolicitationUUIDs.swift +++ b/Sources/BluetoothGAP/GAPListOf128BitServiceSolicitationUUIDs.swift @@ -58,10 +58,11 @@ extension GAPListOf128BitServiceSolicitationUUIDs: ExpressibleByArrayLiteral { // MARK: - CustomStringConvertible +#if !hasFeature(Embedded) extension GAPListOf128BitServiceSolicitationUUIDs: CustomStringConvertible { public var description: String { - return uuids.description } } +#endif diff --git a/Sources/BluetoothGAP/GAPListOf16BitServiceSolicitationUUIDs.swift b/Sources/BluetoothGAP/GAPListOf16BitServiceSolicitationUUIDs.swift index 9e3593765..dfc607e5a 100644 --- a/Sources/BluetoothGAP/GAPListOf16BitServiceSolicitationUUIDs.swift +++ b/Sources/BluetoothGAP/GAPListOf16BitServiceSolicitationUUIDs.swift @@ -62,10 +62,11 @@ extension GAPListOf16BitServiceSolicitationUUIDs: ExpressibleByArrayLiteral { // MARK: - CustomStringConvertible +#if !hasFeature(Embedded) extension GAPListOf16BitServiceSolicitationUUIDs: CustomStringConvertible { public var description: String { - return uuids.map { BluetoothUUID.bit16($0) }.description } } +#endif diff --git a/Sources/BluetoothGAP/GAPListOf32BitServiceSolicitationUUIDs.swift b/Sources/BluetoothGAP/GAPListOf32BitServiceSolicitationUUIDs.swift index c6bc5ccf8..d285bdca3 100644 --- a/Sources/BluetoothGAP/GAPListOf32BitServiceSolicitationUUIDs.swift +++ b/Sources/BluetoothGAP/GAPListOf32BitServiceSolicitationUUIDs.swift @@ -58,10 +58,11 @@ extension GAPListOf32BitServiceSolicitationUUIDs: ExpressibleByArrayLiteral { // MARK: - CustomStringConvertible +#if !hasFeature(Embedded) extension GAPListOf32BitServiceSolicitationUUIDs: CustomStringConvertible { public var description: String { - return uuids.map { BluetoothUUID.bit32($0) }.description } } +#endif diff --git a/Sources/BluetoothGAP/GAPMeshBeacon.swift b/Sources/BluetoothGAP/GAPMeshBeacon.swift index 83156ea81..dd9c1bff4 100644 --- a/Sources/BluetoothGAP/GAPMeshBeacon.swift +++ b/Sources/BluetoothGAP/GAPMeshBeacon.swift @@ -63,20 +63,18 @@ public extension GAPMeshBeacon { switch self { case let .unprovisionedDevice(beacon): - data += beacon.data + data += beacon case let .secureNetwork(beacon): - data += beacon.data + data += beacon } } var dataLength: Int { - - // FIXME: Improve performance switch self { case let .unprovisionedDevice(beacon): - return beacon.data.count + return beacon.dataLength case let .secureNetwork(beacon): - return beacon.data.count + return beacon.dataLength } } } @@ -90,7 +88,7 @@ public protocol GAPMeshBeaconProtocol { init?(data: Data) - var data: Data { get } + } /** @@ -152,28 +150,6 @@ public struct GAPUnprovisionedDeviceBeacon: GAPMeshBeaconProtocol, Equatable { self.init(deviceUUID: deviceUUID, oobInformationFlags: oobInformationFlags, uriHash: uriHash) } - public var data: Data { - - let length = DataLength(uriHash: uriHash != nil) - - var data = Data(capacity: length.rawValue) - - data.append(type(of: self).beaconType.rawValue) - - data += UInt128(uuid: deviceUUID).littleEndian - - let flagsBytes = oobInformationFlags.rawValue.littleEndian.bytes - - data += [flagsBytes.0, flagsBytes.1] - - if let uriBytes = uriHash?.littleEndian.bytes { - - data += [uriBytes.0, uriBytes.1, uriBytes.2, uriBytes.3] - } - - return data - } - struct DataLength: RawRepresentable { static let minimum = DataLength(19) @@ -215,6 +191,26 @@ public struct GAPUnprovisionedDeviceBeacon: GAPMeshBeaconProtocol, Equatable { } } +extension GAPUnprovisionedDeviceBeacon: DataConvertible { + + public static func += (data: inout Data, value: Self) { + + data.append(Self.beaconType.rawValue) + data += UInt128(uuid: value.deviceUUID).littleEndian + + let flagsBytes = value.oobInformationFlags.rawValue.littleEndian.bytes + data += [flagsBytes.0, flagsBytes.1] + + if let uriBytes = value.uriHash?.littleEndian.bytes { + data += [uriBytes.0, uriBytes.1, uriBytes.2, uriBytes.3] + } + } + + public var dataLength: Int { + DataLength(uriHash: uriHash != nil).rawValue + } +} + @frozen public enum GAPBeaconType: UInt8 { @@ -297,7 +293,7 @@ public enum GAPSecureNetworkFlag: UInt8, BitMaskOption { @frozen public struct GAPSecureNetworkBeacon: GAPMeshBeaconProtocol, Equatable { - internal static let length = 22 + internal static var length: Int { 22 } public static let beaconType: GAPBeaconType = .secureNetwork @@ -330,36 +326,32 @@ public struct GAPSecureNetworkBeacon: GAPMeshBeaconProtocol, Equatable { else { return nil } let flags = BitMaskOptionSet(rawValue: data[1]) - let networkID = UInt64(littleEndian: UInt64(bytes: (data[2], data[3], data[4], data[5], data[6], data[7], data[8], data[9]))) - let ivIndex = UInt32(littleEndian: UInt32(bytes: (data[10], data[11], data[12], data[13]))) - let authenticationValue = UInt64(littleEndian: UInt64(bytes: (data[14], data[15], data[16], data[17], data[18], data[19], data[20], data[21]))) self.init(flags: flags, networkID: networkID, ivIndex: ivIndex, authenticationValue: authenticationValue) } +} + +extension GAPSecureNetworkBeacon: DataConvertible { - public var data: Data { - - var data = Data(capacity: type(of: self).length) - - data.append(type(of: self).beaconType.rawValue) - - data.append(flags.rawValue) - - let networkIDBytes = networkID.littleEndian.bytes + static func += (data: inout T, value: Self) { + + data.append(Self.beaconType.rawValue) + data.append(value.flags.rawValue) + let networkIDBytes = value.networkID.littleEndian.bytes data += [networkIDBytes.0, networkIDBytes.1, networkIDBytes.2, networkIDBytes.3, networkIDBytes.4, networkIDBytes.5, networkIDBytes.6, networkIDBytes.7] - let ivIndexBytes = ivIndex.littleEndian.bytes - + let ivIndexBytes = value.ivIndex.littleEndian.bytes data += [ivIndexBytes.0, ivIndexBytes.1, ivIndexBytes.2, ivIndexBytes.3] - let authValueBytes = authenticationValue.littleEndian.bytes - + let authValueBytes = value.authenticationValue.littleEndian.bytes data += [authValueBytes.0, authValueBytes.1, authValueBytes.2, authValueBytes.3, authValueBytes.4, authValueBytes.5, authValueBytes.6, authValueBytes.7] - - return data + } + + var dataLength: Int { + Self.length } } diff --git a/Sources/BluetoothGAP/GAPPublicTargetAddress.swift b/Sources/BluetoothGAP/GAPPublicTargetAddress.swift index f10bb130f..9611a0bd8 100644 --- a/Sources/BluetoothGAP/GAPPublicTargetAddress.swift +++ b/Sources/BluetoothGAP/GAPPublicTargetAddress.swift @@ -61,13 +61,14 @@ public extension GAPPublicTargetAddress { // MARK: - CustomStringConvertible +#if !hasFeature(Embedded) extension GAPPublicTargetAddress: CustomStringConvertible { public var description: String { - return addresses.description } } +#endif // MARK: - ExpressibleByArrayLiteral diff --git a/Sources/BluetoothGAP/GAPRandomTargetAddress.swift b/Sources/BluetoothGAP/GAPRandomTargetAddress.swift index 9a6947cae..4f8f65b31 100644 --- a/Sources/BluetoothGAP/GAPRandomTargetAddress.swift +++ b/Sources/BluetoothGAP/GAPRandomTargetAddress.swift @@ -18,16 +18,15 @@ import Bluetooth /// The format of each 6 octet address is the same as the Random Device Address defined in Vol. 6, Part B, Section 1.3. /// The Random Target Address value shall be the enumerated value as defined by Bluetooth Assigned Numbers. @frozen -public struct GAPRandomTargetAddress: GAPData, Equatable { +public struct GAPRandomTargetAddress: GAPData, Equatable, Hashable, Sendable { public typealias ByteValue = (UInt8, UInt8, UInt8) - public static let dataType: GAPDataType = .randomTargetAddress + public static var dataType: GAPDataType { .randomTargetAddress } public let addresses: [BluetoothAddress] public init(addresses: [BluetoothAddress]) { - self.addresses = addresses } } @@ -50,24 +49,24 @@ public extension GAPRandomTargetAddress { } var dataLength: Int { - return addresses.count * BluetoothAddress.length } func append(to data: inout Data) { - addresses.forEach { data += $0.littleEndian } } } // MARK: - CustomStringConvertible +#if !hasFeature(Embedded) extension GAPRandomTargetAddress: CustomStringConvertible { public var description: String { return addresses.description } } +#endif // MARK: - ExpressibleByArrayLiteral