Skip to content

Commit

Permalink
#161 Add DataConvertible conformance to ATTFindInformationRequest
Browse files Browse the repository at this point in the history
  • Loading branch information
colemancda committed Nov 7, 2024
1 parent 0a3194b commit 1003a23
Showing 1 changed file with 20 additions and 29 deletions.
49 changes: 20 additions & 29 deletions Sources/BluetoothGATT/ATTFindInformationRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,62 +6,53 @@
// Copyright © 2018 PureSwift. All rights reserved.
//

import Foundation
import Bluetooth

/// Find Information Request
///
/// The *Find Information Request* is used to obtain the mapping of attribute handles with their associated types.
/// This allows a client to discover the list of attributes and their types on a server.
@frozen
public struct ATTFindInformationRequest: ATTProtocolDataUnit, Equatable {
public struct ATTFindInformationRequest: ATTProtocolDataUnit, Equatable, Hashable, Sendable {

public static var attributeOpcode: ATTOpcode { return .findInformationRequest }
public static var attributeOpcode: ATTOpcode { .findInformationRequest }

public var startHandle: UInt16

public var endHandle: UInt16

public init(startHandle: UInt16,
endHandle: UInt16) {

public init(
startHandle: UInt16,
endHandle: UInt16
) {
self.startHandle = startHandle
self.endHandle = endHandle
}
}

public extension ATTFindInformationRequest {
// MARK: - DataConvertible

extension ATTFindInformationRequest: DataConvertible {

internal static var length: Int { return 5 }
public static var length: Int { 5 }

init?(data: Data) {
public init?<Data: DataContainer>(data: Data) {

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

self.startHandle = UInt16(littleEndian: UInt16(bytes: (data[1], data[2])))
self.endHandle = UInt16(littleEndian: UInt16(bytes: (data[3], data[4])))
}

var data: Data {

return Data(self)
}
}

// MARK: - DataConvertible

extension ATTFindInformationRequest: DataConvertible {

var dataLength: Int {

return type(of: self).length
public func append<Data>(to data: inout Data) where Data : DataContainer {
data += Self.attributeOpcode.rawValue
data += self.startHandle.littleEndian
data += self.endHandle.littleEndian
}

static func += <T: DataContainer> (data: inout T, value: ATTFindInformationRequest) {

data += attributeOpcode.rawValue
data += value.startHandle.littleEndian
data += value.endHandle.littleEndian
public var dataLength: Int {
Self.length
}
}

0 comments on commit 1003a23

Please sign in to comment.