Skip to content

Commit

Permalink
#161 Add DataConvertible conformance to ClassOfDevice
Browse files Browse the repository at this point in the history
  • Loading branch information
colemancda committed Nov 7, 2024
1 parent 107fc89 commit 0a33d01
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions Sources/Bluetooth/ClassOfDevice.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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: DataContainer>(data: Data) {

guard data.count == Self.length
else { return nil }

guard let formatType = FormatType(rawValue: (data[0] << 6) >> 6)
Expand Down Expand Up @@ -87,23 +86,23 @@ public extension ClassOfDevice {
}
}

var data: Data {
public func append<Data: DataContainer>(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 {

Expand Down

0 comments on commit 0a33d01

Please sign in to comment.