-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
// | ||
// This source file is part of the Stanford Spezi open-source project | ||
// | ||
// SPDX-FileCopyrightText: 2024 Stanford University and the project authors (see CONTRIBUTORS.md) | ||
// | ||
// SPDX-License-Identifier: MIT | ||
// | ||
|
||
import ByteCoding | ||
import NIOCore | ||
|
||
|
||
// TODO: CharacteristicAccessor extensions? | ||
|
||
|
||
public enum UserControlPointGenericParameter { | ||
case registerNewUser(consentCode: UInt16) | ||
case consent(userIndex: UInt8, consentCode: UInt16) | ||
case deleteUserData | ||
case listAllUsers | ||
case deleterUser(userIndex: UInt8) | ||
case response( | ||
requestOpCode: UserControlPointOpCode, | ||
response: UserControlPointResponse | ||
) | ||
} | ||
|
||
|
||
extension UserControlPointGenericParameter: Hashable, Sendable {} | ||
|
||
|
||
extension UserControlPointGenericParameter: UserControlPointParameter { | ||
public var opCode: UserControlPointOpCode { | ||
switch self { | ||
case .registerNewUser: | ||
return .registerNewUser | ||
case .consent: | ||
return .consent | ||
case .deleteUserData: | ||
return .deleteUserData | ||
case .listAllUsers: | ||
return .listAllUsers | ||
case .deleterUser: | ||
return .deleteUser | ||
case .response: | ||
return .response | ||
} | ||
} | ||
|
||
public init?(from byteBuffer: inout ByteBuffer, preferredEndianness endianness: Endianness, opCode: UserControlPointOpCode) { | ||
switch opCode { | ||
case .registerNewUser: | ||
guard let consentCode = UInt16(from: &byteBuffer, preferredEndianness: endianness) else { | ||
return nil | ||
Check warning on line 54 in Sources/BluetoothServices/Characteristics/UserData/UserControlPointGenericParameter.swift Codecov / codecov/patchSources/BluetoothServices/Characteristics/UserData/UserControlPointGenericParameter.swift#L54
|
||
} | ||
self = .registerNewUser(consentCode: consentCode) | ||
case .consent: | ||
guard let userIndex = UInt8(from: &byteBuffer, preferredEndianness: endianness), | ||
let consentCode = UInt16(from: &byteBuffer, preferredEndianness: endianness) else { | ||
return nil | ||
Check warning on line 60 in Sources/BluetoothServices/Characteristics/UserData/UserControlPointGenericParameter.swift Codecov / codecov/patchSources/BluetoothServices/Characteristics/UserData/UserControlPointGenericParameter.swift#L60
|
||
} | ||
self = .consent(userIndex: userIndex, consentCode: consentCode) | ||
case .deleteUserData: | ||
self = .deleteUserData | ||
case .listAllUsers: | ||
self = .listAllUsers | ||
case .deleteUser: | ||
guard let userIndex = UInt8(from: &byteBuffer, preferredEndianness: endianness) else { | ||
return nil | ||
Check warning on line 69 in Sources/BluetoothServices/Characteristics/UserData/UserControlPointGenericParameter.swift Codecov / codecov/patchSources/BluetoothServices/Characteristics/UserData/UserControlPointGenericParameter.swift#L69
|
||
} | ||
self = .deleterUser(userIndex: userIndex) | ||
case .response: | ||
guard let requestOpCode = UserControlPointOpCode(from: &byteBuffer, preferredEndianness: endianness), | ||
let response = UserControlPointResponse(from: &byteBuffer, preferredEndianness: endianness, requestOpCode: requestOpCode) else { | ||
return nil | ||
Check warning on line 75 in Sources/BluetoothServices/Characteristics/UserData/UserControlPointGenericParameter.swift Codecov / codecov/patchSources/BluetoothServices/Characteristics/UserData/UserControlPointGenericParameter.swift#L75
|
||
} | ||
self = .response(requestOpCode: requestOpCode, response: response) | ||
default: | ||
return nil | ||
Check warning on line 79 in Sources/BluetoothServices/Characteristics/UserData/UserControlPointGenericParameter.swift Codecov / codecov/patchSources/BluetoothServices/Characteristics/UserData/UserControlPointGenericParameter.swift#L79
|
||
} | ||
} | ||
|
||
public func encode(to byteBuffer: inout ByteBuffer, preferredEndianness endianness: Endianness) { | ||
switch self { | ||
case let .registerNewUser(consentCode): | ||
consentCode.encode(to: &byteBuffer, preferredEndianness: endianness) | ||
case let .consent(userIndex, consentCode): | ||
userIndex.encode(to: &byteBuffer, preferredEndianness: endianness) | ||
consentCode.encode(to: &byteBuffer, preferredEndianness: endianness) | ||
case .deleteUserData: | ||
break | ||
case .listAllUsers: | ||
break | ||
case let .deleterUser(userIndex): | ||
userIndex.encode(to: &byteBuffer, preferredEndianness: endianness) | ||
case let .response(requestOpCode, response): | ||
requestOpCode.encode(to: &byteBuffer, preferredEndianness: endianness) | ||
response.encode(to: &byteBuffer, preferredEndianness: endianness) | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// | ||
// This source file is part of the Stanford Spezi open-source project | ||
// | ||
// SPDX-FileCopyrightText: 2024 Stanford University and the project authors (see CONTRIBUTORS.md) | ||
// | ||
// SPDX-License-Identifier: MIT | ||
// | ||
|
||
import ByteCoding | ||
import NIOCore | ||
|
||
|
||
public struct UserControlPointOpCode { | ||
public static let reserved = UserControlPointOpCode(rawValue: 0x00) | ||
public static let registerNewUser = UserControlPointOpCode(rawValue: 0x01) | ||
public static let consent = UserControlPointOpCode(rawValue: 0x02) | ||
public static let deleteUserData = UserControlPointOpCode(rawValue: 0x03) | ||
public static let listAllUsers = UserControlPointOpCode(rawValue: 0x04) // TODO: optional | ||
public static let deleteUser = UserControlPointOpCode(rawValue: 0x05) // TODO: optional | ||
public static let response = UserControlPointOpCode(rawValue: 0x20) | ||
|
||
public let rawValue: UInt8 | ||
|
||
public init(rawValue: UInt8) { | ||
self.rawValue = rawValue | ||
} | ||
} | ||
|
||
|
||
extension UserControlPointOpCode: RawRepresentable {} | ||
|
||
|
||
extension UserControlPointOpCode: Hashable, Sendable {} | ||
|
||
|
||
extension UserControlPointOpCode: ByteCodable { | ||
public init?(from byteBuffer: inout ByteBuffer, preferredEndianness endianness: Endianness) { | ||
guard let rawValue = UInt8(from: &byteBuffer, preferredEndianness: endianness) else { | ||
return nil | ||
} | ||
self.init(rawValue: rawValue) | ||
} | ||
|
||
public func encode(to byteBuffer: inout ByteBuffer, preferredEndianness endianness: Endianness) { | ||
rawValue.encode(to: &byteBuffer, preferredEndianness: endianness) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// | ||
// This source file is part of the Stanford Spezi open-source project | ||
// | ||
// SPDX-FileCopyrightText: 2024 Stanford University and the project authors (see CONTRIBUTORS.md) | ||
// | ||
// SPDX-License-Identifier: MIT | ||
// | ||
|
||
import ByteCoding | ||
import NIOCore | ||
|
||
|
||
public protocol UserControlPointParameter: ByteEncodable, Hashable, Sendable { | ||
var opCode: UserControlPointOpCode { get } | ||
|
||
|
||
init?(from byteBuffer: inout ByteBuffer, preferredEndianness endianness: Endianness, opCode: UserControlPointOpCode) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// | ||
// This source file is part of the Stanford Spezi open-source project | ||
// | ||
// SPDX-FileCopyrightText: 2024 Stanford University and the project authors (see CONTRIBUTORS.md) | ||
// | ||
// SPDX-License-Identifier: MIT | ||
// | ||
|
||
import ByteCoding | ||
import NIOCore | ||
|
||
|
||
public enum UserControlPointResponse { | ||
case success(UserControlPointResponseParameter? = nil) | ||
case opCodeNotSupported | ||
case invalidParameter | ||
case operationFailed | ||
case userNotAuthorized | ||
|
||
|
||
public var responseValue: UserControlPointResponseValue { | ||
switch self { | ||
case .success: | ||
return .success | ||
case .opCodeNotSupported: | ||
return .opCodeNotSupported | ||
case .invalidParameter: | ||
return .invalidParameter | ||
case .operationFailed: | ||
return .operationFailed | ||
case .userNotAuthorized: | ||
return .userNotAuthorized | ||
} | ||
} | ||
} | ||
|
||
|
||
extension UserControlPointResponse: Hashable, Sendable {} | ||
|
||
|
||
extension UserControlPointResponse: ByteEncodable { | ||
public init?(from byteBuffer: inout ByteBuffer, preferredEndianness endianness: Endianness, requestOpCode: UserControlPointOpCode) { | ||
guard let responseValue = UserControlPointResponseValue(from: &byteBuffer, preferredEndianness: endianness) else { | ||
return nil | ||
} | ||
|
||
switch responseValue { | ||
case .success: | ||
let parameter: UserControlPointResponseParameter? | ||
switch requestOpCode { | ||
case .registerNewUser, .deleteUser: | ||
guard let userIndex = UInt8(from: &byteBuffer, preferredEndianness: endianness) else { | ||
return nil | ||
} | ||
parameter = .userIndex(userIndex) | ||
case .listAllUsers: | ||
guard let numberOfUsers = UInt8(from: &byteBuffer, preferredEndianness: endianness) else { | ||
return nil | ||
} | ||
parameter = .numberOfUsers(numberOfUsers) | ||
default: | ||
parameter = nil | ||
} | ||
|
||
self = .success(parameter) | ||
case .opCodeNotSupported: | ||
self = .opCodeNotSupported | ||
case .invalidParameter: | ||
self = .invalidParameter | ||
case .operationFailed: | ||
self = .operationFailed | ||
case .userNotAuthorized: | ||
self = .userNotAuthorized | ||
default: | ||
return nil | ||
} | ||
} | ||
|
||
public func encode(to byteBuffer: inout ByteBuffer, preferredEndianness endianness: Endianness) { | ||
responseValue.encode(to: &byteBuffer, preferredEndianness: endianness) | ||
switch self { | ||
case let .success(parameter): | ||
if let parameter { | ||
parameter.rawValue.encode(to: &byteBuffer, preferredEndianness: endianness) | ||
} | ||
default: | ||
break | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// | ||
// This source file is part of the Stanford Spezi open-source project | ||
// | ||
// SPDX-FileCopyrightText: 2024 Stanford University and the project authors (see CONTRIBUTORS.md) | ||
// | ||
// SPDX-License-Identifier: MIT | ||
// | ||
|
||
import ByteCoding | ||
import NIOCore | ||
|
||
|
||
public enum UserControlPointResponseParameter { | ||
case userIndex(UInt8) | ||
case numberOfUsers(UInt8) | ||
|
||
|
||
public var rawValue: UInt8 { | ||
switch self { | ||
case let .userIndex(value): | ||
return value | ||
case let .numberOfUsers(value): | ||
return value | ||
} | ||
} | ||
} | ||
|
||
|
||
extension UserControlPointResponseParameter: Hashable, Sendable {} |