Skip to content

Commit

Permalink
Initial start of the User Data Service
Browse files Browse the repository at this point in the history
  • Loading branch information
Supereg committed Jun 10, 2024
1 parent a6f0b1e commit 394331f
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 0 deletions.
9 changes: 9 additions & 0 deletions Sources/BluetoothServices/Characteristics/Time/Date.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//
// 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 Foundation
44 changes: 44 additions & 0 deletions Sources/BluetoothServices/Characteristics/UserData/Gender.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//
// 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 Gender {
public static let male = Gender(rawValue: 0x00)
public static let female = Gender(rawValue: 0x01)
public static let unspecified = Gender(rawValue: 0x02)


public let rawValue: UInt8

public init(rawValue: UInt8) {
self.rawValue = rawValue
}
}


extension Gender: RawRepresentable {}


extension Gender: Hashable, Sendable {}


extension Gender: 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,57 @@
//
// 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
import SpeziBluetooth


public struct UserControlPoint {
public struct OpCode {
public static let reserved = OpCode(rawValue: 0x00)
public static let registerNewUser = OpCode(rawValue: 0x01)
public static let consent = OpCode(rawValue: 0x02)
public static let deleteUserData = OpCode(rawValue: 0x03)
public static let listAllUsers = OpCode(rawValue: 0x04) // TODO: optional
public static let deleteUser = OpCode(rawValue: 0x05) // TODO: optional
public static let responseCode = OpCode(rawValue: 0x20)

public let rawValue: UInt8

public init(rawValue: UInt8) {
self.rawValue = rawValue
}
}


public let opcode: OpCode
public let parameter: Any // TODO: what?
}


extension UserControlPoint.OpCode: RawRepresentable {}


extension UserControlPoint.OpCode: Hashable, Sendable {}


extension UserControlPoint.OpCode: 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)
}
}


extension UserControlPoint: ControlPointCharacteristic {}
46 changes: 46 additions & 0 deletions Sources/BluetoothServices/Services/UserDataService.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//
// 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 class CoreBluetooth.CBUUID
import SpeziBluetooth


// TODO: non-final for custom instantiations?
public final class UserDataService: BluetoothService {
public static let id = CBUUID(string: "181C")

// TODO: UDS characteristics?

@Characteristic(id: "2A85")
public var dateOfBirth: Date? // TODO: date overload?
@Characteristic(id: "2A8C")
public var gender: Gender?
@Characteristic(id: "2A8E")
public var height: UInt16?
// TODO: Represented values: M = 1, d = -2, b = 0 => 0.01 unit!
// TODO: depends on the characteristic presentation format descriptor!

// TODO: end of Omron characteristics

/// Count of changes made to the set of related characteristics.
///
/// Use this count to determine the need to synchronize the data set
/// with the peripheral.
@Characteristic(id: "2A99", notify: true)
public var databaseChangeIncrement: UInt32? // TODO: doc, read write, and C1: notify; write requires security permissions?

/// Index of the current user.
///
/// A value of `UInt8/max` (`0xFF`) indicates an unknown user.
@Characteristic(id: "2A9A")
public var userIndex: UInt8? // TODO: read only!

// TODO: user control point 2A9F

// TODO: does not support registered user characteristic??? [2B37]
}

0 comments on commit 394331f

Please sign in to comment.