Skip to content

Commit

Permalink
#161 Add DataConvertible conformance to ATTReadByGroupTypeRequest
Browse files Browse the repository at this point in the history
  • Loading branch information
colemancda committed Nov 7, 2024
1 parent 8a9a63b commit 382c19e
Showing 1 changed file with 28 additions and 36 deletions.
64 changes: 28 additions & 36 deletions Sources/BluetoothGATT/ATTReadByGroupTypeRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,14 @@
// Copyright © 2018 PureSwift. All rights reserved.
//

import Foundation

/// Read by Group Type Request
///
/// The *Read By Group Type Request* is used to obtain the values of attributes where the attribute type is known,
/// the type of a grouping attribute as defined by a higher layer specification, but the handle is not known.
@frozen
public struct ATTReadByGroupTypeRequest: ATTProtocolDataUnit, Equatable {
public struct ATTReadByGroupTypeRequest: ATTProtocolDataUnit, Equatable, Hashable, Sendable {

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

/// First requested handle number.
public var startHandle: UInt16
Expand All @@ -28,24 +26,26 @@ public struct ATTReadByGroupTypeRequest: ATTProtocolDataUnit, Equatable {
/// 2 or 16 octet UUID
public var type: BluetoothUUID

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

assert(type.length != .bit32)

public init(
startHandle: UInt16,
endHandle: UInt16,
type: BluetoothUUID
) {
assert(type.dataLength != 4, "Cannot use 32-bit UUID")
self.startHandle = startHandle
self.endHandle = endHandle
self.type = type
}
}

public extension ATTReadByGroupTypeRequest {
// MARK: - DataConvertible

extension ATTReadByGroupTypeRequest: DataConvertible {

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

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

self.startHandle = UInt16(littleEndian: UInt16(bytes: (data[1], data[2])))
Expand All @@ -56,37 +56,25 @@ public extension ATTReadByGroupTypeRequest {
let value = UInt16(littleEndian: UInt16(bytes: (data[5], data[6])))
self.type = .bit16(value)
case .uuid128:
self.type = BluetoothUUID(littleEndian: BluetoothUUID(data: data.subdataNoCopy(in: 5 ..< 21))!)
self.type = BluetoothUUID(littleEndian: BluetoothUUID(data: data.subdata(in: 5 ..< 21))!)
}
}

var data: Data {

return Data(self)
}
}

// MARK: - DataConvertible

extension ATTReadByGroupTypeRequest: DataConvertible {

var dataLength: Int {

public var dataLength: Int {
return length.rawValue
}

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

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

// MARK: - Supporting Types

private extension ATTReadByGroupTypeRequest {
fileprivate extension ATTReadByGroupTypeRequest {

enum Length: Int {

Expand All @@ -97,9 +85,13 @@ private extension ATTReadByGroupTypeRequest {
var length: Length {

switch type {
case .bit16: return .uuid16
case .bit128: return .uuid128
case .bit32: fatalError()
case .bit16:
return .uuid16
case .bit128:
return .uuid128
case .bit32:
assertionFailure("")
return .uuid128
}
}
}

0 comments on commit 382c19e

Please sign in to comment.