From 0a33d01c827d92c31d3e7a0cff0f5c5f609fcd01 Mon Sep 17 00:00:00 2001 From: Alsey Coleman Miller Date: Wed, 6 Nov 2024 21:52:51 -0500 Subject: [PATCH] #161 Add `DataConvertible` conformance to `ClassOfDevice` --- Sources/Bluetooth/ClassOfDevice.swift | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/Sources/Bluetooth/ClassOfDevice.swift b/Sources/Bluetooth/ClassOfDevice.swift index 108722ef0..4217f913b 100644 --- a/Sources/Bluetooth/ClassOfDevice.swift +++ b/Sources/Bluetooth/ClassOfDevice.swift @@ -30,12 +30,11 @@ public struct ClassOfDevice: Equatable, Sendable { } } -#if canImport(Foundation) -public extension ClassOfDevice { +extension ClassOfDevice: DataConvertible { - init?(data: Data) { - - guard data.count == type(of: self).length + public init?(data: Data) { + + guard data.count == Self.length else { return nil } guard let formatType = FormatType(rawValue: (data[0] << 6) >> 6) @@ -87,23 +86,23 @@ public extension ClassOfDevice { } } - var data: Data { + public func append(to data: inout Data) { // combine Format Type with Major Device Class let firstByte = formatType.rawValue | (majorDeviceClass.minorClassValue << 2) - // get first 3 bits of the Mejor Service Class let majorServiceClass3Bits = (majorServiceClass.rawValue.bytes.0 << 5) /// e.g. 11100000 - // combine part of the Major Device Class of part with the Major Service Class let secondByte = majorDeviceClass.type.rawValue | majorServiceClass3Bits - let thirdByte = (majorServiceClass.rawValue.bytes.1 << 5) | (majorServiceClass.rawValue.bytes.0 >> 3) - return Data([firstByte, secondByte, thirdByte]) + data += [firstByte, secondByte, thirdByte] + } + + public var dataLength: Int { + Self.length } } -#endif extension ClassOfDevice {