Skip to content

Commit

Permalink
#161 Improve DataContainer support
Browse files Browse the repository at this point in the history
  • Loading branch information
colemancda committed Nov 11, 2024
1 parent 87e43b8 commit 41c7290
Show file tree
Hide file tree
Showing 73 changed files with 283 additions and 296 deletions.
2 changes: 2 additions & 0 deletions Sources/BluetoothGATT/ATTConnection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,8 @@ public enum ATTConnectionError<SocketError: Swift.Error, Data: DataContainer>: E
case socket(SocketError)
}

internal typealias ATTResponse<Success: ATTProtocolDataUnit> = Result<Success, ATTErrorResponse>

internal extension ATTConnection {

struct SendOperation {
Expand Down
2 changes: 2 additions & 0 deletions Sources/BluetoothGATT/ATTErrorResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
// Copyright © 2018 PureSwift. All rights reserved.
//

import Bluetooth

/// The Error Response is used to state that a given request cannot be performed,
/// and to provide the reason.
///
Expand Down
5 changes: 3 additions & 2 deletions Sources/BluetoothGATT/GATTAerobicHeartRateLowerLimit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

import Foundation
import Bluetooth

/**
Aerobic Heart Rate Lower Limit
Expand All @@ -31,9 +32,9 @@ public struct GATTAerobicHeartRateLowerLimit: GATTCharacteristic {
self.beats = beats
}

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

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

let beats = BeatsPerMinute(rawValue: data[0])
Expand Down
5 changes: 3 additions & 2 deletions Sources/BluetoothGATT/GATTAerobicHeartRateUpperLimit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

import Foundation
import Bluetooth

/**
Aerobic Heart Rate Upper Limit
Expand All @@ -31,9 +32,9 @@ public struct GATTAerobicHeartRateUpperLimit: GATTCharacteristic {
self.beats = beats
}

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

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

let beats = BeatsPerMinute(rawValue: data[0])
Expand Down
24 changes: 6 additions & 18 deletions Sources/BluetoothGATT/GATTAerobicThreshold.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

import Foundation
import Bluetooth

/**
Aerobic Threshold
Expand All @@ -16,7 +17,7 @@ import Foundation
- SeeAlso: [Aerobic Threshold](https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.characteristic.aerobic_threshold.xml)
*/
@frozen
public struct GATTAerobicThreshold: GATTCharacteristic {
public struct GATTAerobicThreshold: GATTCharacteristic, Equatable, Hashable, Sendable {

public typealias BeatsPerMinute = GATTBeatsPerMinute.Byte

Expand All @@ -27,40 +28,27 @@ public struct GATTAerobicThreshold: GATTCharacteristic {
public var beats: BeatsPerMinute

public init(beats: BeatsPerMinute) {

self.beats = beats
}

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

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

let beats = BeatsPerMinute(rawValue: data[0])

self.init(beats: beats)
}

public var data: Data {

return Data([beats.rawValue])
}

}

extension GATTAerobicThreshold: Equatable {

public static func == (lhs: GATTAerobicThreshold,
rhs: GATTAerobicThreshold) -> Bool {

return lhs.beats == rhs.beats
public func append<Data>(to data: inout Data) where Data : DataContainer {
data.append(beats.rawValue)
}
}

extension GATTAerobicThreshold: CustomStringConvertible {

public var description: String {

return beats.description
}
}
5 changes: 3 additions & 2 deletions Sources/BluetoothGATT/GATTAge.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

import Foundation
import Bluetooth

/**
Age
Expand All @@ -29,9 +30,9 @@ public struct GATTAge: GATTCharacteristic, Equatable {
self.year = year
}

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

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

let year = Year(rawValue: data[0])
Expand Down
5 changes: 3 additions & 2 deletions Sources/BluetoothGATT/GATTAlertCategory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

import Foundation
import Bluetooth

/**
Categories of alerts/messages.
Expand Down Expand Up @@ -57,9 +58,9 @@ public enum GATTAlertCategory: UInt8, GATTCharacteristic {
/// Instant Message: Alert for incoming instant messages
case instantMessage

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

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

self.init(rawValue: data[0])
Expand Down
5 changes: 3 additions & 2 deletions Sources/BluetoothGATT/GATTAlertCategoryBitMask.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

import Foundation
import Bluetooth

/**
Alert Category ID Bit Mask
Expand All @@ -28,7 +29,7 @@ public struct GATTAlertCategoryBitMask: GATTCharacteristic, Equatable, Hashable
/// This field shows the category of the new alert.
public var categories: BitMaskOptionSet<Category>

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

guard let bitmask = Category.RawValue(bitmaskArray: data)
else { return nil }
Expand All @@ -38,7 +39,7 @@ public struct GATTAlertCategoryBitMask: GATTCharacteristic, Equatable, Hashable

public var data: Data {

return categories.rawValue.bitmaskArray
return Data(categories.rawValue.bitmaskArray)
}
}

Expand Down
5 changes: 3 additions & 2 deletions Sources/BluetoothGATT/GATTAlertLevel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

import Foundation
import Bluetooth

/**
Alert Level
Expand Down Expand Up @@ -43,9 +44,9 @@ public enum GATTAlertLevel: UInt8, GATTCharacteristic {
/// High alert.
case high = 0x02

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

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

self.init(rawValue: data[0])
Expand Down
9 changes: 5 additions & 4 deletions Sources/BluetoothGATT/GATTAlertNotificationControlPoint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

import Foundation
import Bluetooth

/**
Alert Notification Control Point
Expand Down Expand Up @@ -47,9 +48,9 @@ public struct GATTAlertNotificationControlPoint: GATTCharacteristic {
self.category = category
}

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

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

guard let command = Command(rawValue: data[0])
Expand All @@ -66,9 +67,9 @@ public struct GATTAlertNotificationControlPoint: GATTCharacteristic {
return Data([command.rawValue, category.rawValue])
}

public var characteristic: GATTAttribute.Characteristic {
public var characteristic: GATTAttribute<Data>.Characteristic {

return GATTAttribute.Characteristic(uuid: type(of: self).uuid,
return GATTAttribute.Characteristic(uuid: Self.uuid,
value: data,
permissions: [.read],
properties: [.notify],
Expand Down
5 changes: 3 additions & 2 deletions Sources/BluetoothGATT/GATTAlertStatus.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

import Foundation
import Bluetooth

/**
Alert Status
Expand All @@ -33,9 +34,9 @@ public struct GATTAlertStatus: GATTCharacteristic {
self.states = states
}

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

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

self.states = BitMaskOptionSet<State>(rawValue: data[0])
Expand Down
5 changes: 3 additions & 2 deletions Sources/BluetoothGATT/GATTAltitude.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

import Foundation
import Bluetooth

/**
Altitude
Expand All @@ -29,9 +30,9 @@ public struct GATTAltitude: RawRepresentable, GATTCharacteristic, Equatable, Has
self.rawValue = rawValue
}

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

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

self.init(rawValue: UInt16(littleEndian: UInt16(bytes: (data[0], data[1]))))
Expand Down
5 changes: 3 additions & 2 deletions Sources/BluetoothGATT/GATTAnaerobicHeartRateLowerLimit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

import Foundation
import Bluetooth

/**
Anaerobic Heart Rate Lower Limit
Expand All @@ -31,9 +32,9 @@ public struct GATTAnaerobicHeartRateLowerLimit: GATTCharacteristic, Equatable, H
self.beats = beats
}

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

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

let beats = BeatsPerMinute(rawValue: data[0])
Expand Down
5 changes: 3 additions & 2 deletions Sources/BluetoothGATT/GATTAnaerobicHeartRateUpperLimit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

import Foundation
import Bluetooth

/**
Anaerobic Heart Rate Upper Limit
Expand All @@ -31,9 +32,9 @@ public struct GATTAnaerobicHeartRateUpperLimit: GATTCharacteristic, Equatable, H
self.beats = beats
}

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

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

let beats = BeatsPerMinute(rawValue: data[0])
Expand Down
5 changes: 3 additions & 2 deletions Sources/BluetoothGATT/GATTAnalog.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

import Foundation
import Bluetooth

/**
Analog
Expand All @@ -29,9 +30,9 @@ public struct GATTAnalog: GATTCharacteristic, Equatable, Hashable {
self.analog = analog
}

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

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

self.init(analog: UInt16(littleEndian: UInt16(bytes: (data[0], data[1]))))
Expand Down
5 changes: 3 additions & 2 deletions Sources/BluetoothGATT/GATTAnalogOutput.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

import Foundation
import Bluetooth

/**
Analog Output
Expand All @@ -29,9 +30,9 @@ public struct GATTAnalogOutput: GATTCharacteristic, Equatable, Hashable {
self.output = output
}

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

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

self.init(output: UInt16(littleEndian: UInt16(bytes: (data[0], data[1]))))
Expand Down
5 changes: 3 additions & 2 deletions Sources/BluetoothGATT/GATTBarometricPressureTrend.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

import Foundation
import Bluetooth

/**
Barometric Pressure Trend
Expand Down Expand Up @@ -52,9 +53,9 @@ public enum GATTBarometricPressureTrend: UInt8, GATTCharacteristic, BluetoothUni
// Steady
case steady = 0x09

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

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

self.init(rawValue: data[0])
Expand Down
5 changes: 3 additions & 2 deletions Sources/BluetoothGATT/GATTBatteryLevel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

import Foundation
import Bluetooth

/// Battery Level
///
Expand All @@ -32,9 +33,9 @@ public struct GATTBatteryLevel: GATTCharacteristic, Equatable, Hashable {

public extension GATTBatteryLevel {

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

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

guard let level = Percentage(rawValue: data[0])
Expand Down
Loading

0 comments on commit 41c7290

Please sign in to comment.