Skip to content

Commit

Permalink
#154 Fix Embedded Swift support for GATTDatabase
Browse files Browse the repository at this point in the history
  • Loading branch information
colemancda committed Nov 18, 2024
1 parent 0c17cdc commit d453bd7
Showing 1 changed file with 34 additions and 26 deletions.
60 changes: 34 additions & 26 deletions Sources/BluetoothGATT/GATTDatabase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -178,14 +178,18 @@ public struct GATTDatabase<Data: DataContainer>: Equatable, Hashable, Sendable {
public mutating func remove(service handle: UInt16) {

guard let serviceIndex = attributeGroups.firstIndex(where: { $0.serviceAttribute.handle == handle })
else { fatalError("Service with handle \(handle) doesnt exist") }
else { fatalError("Invalid service handle") }

attributeGroups.remove(at: serviceIndex)
}

/// Write the value to attribute specified by the handle.
public mutating func write(_ value: Data, forAttribute handle: UInt16) {
self[handle: handle].value = value
var attribute = self[handle: handle]
attribute.value = value
guard self.setAttribute(attribute, for: handle) else {
fatalError("Invalid attribute index")
}
}

/// The handle of the service at the specified index.
Expand All @@ -209,35 +213,16 @@ public struct GATTDatabase<Data: DataContainer>: Equatable, Hashable, Sendable {
return attribute
}
}
fatalError("Invalid attribute index \(index)")
fatalError("Invalid attribute index")
}

/// The attribute with the specified handle.
public private(set) subscript(handle handle: UInt16) -> GATTDatabase.Attribute {

public subscript(handle handle: UInt16) -> GATTDatabase.Attribute {
get {
for group in attributeGroups {
for attribute in group.attributes {
guard attribute.handle != handle
else { return attribute }
}
}

fatalError("Invalid handle \(handle)")
}

mutating set {

for (groupIndex, group) in attributeGroups.enumerated() {
for (attributeIndex, attribute) in group.attributes.enumerated() {
guard attribute.handle != handle else {
attributeGroups[groupIndex].attributes[attributeIndex] = newValue
return
}
}
guard let attribute = attribute(for: handle) else {
fatalError("Invalid handle")
}

fatalError("Invalid handle \(handle)")
return attribute
}
}

Expand All @@ -248,6 +233,29 @@ public struct GATTDatabase<Data: DataContainer>: Equatable, Hashable, Sendable {
lastHandle += 1
return lastHandle
}

private func attribute(for handle: UInt16) -> Self.Attribute? {
for group in attributeGroups {
for attribute in group.attributes {
guard attribute.handle != handle
else { return attribute }
}
}
return nil
}

@discardableResult
private mutating func setAttribute(_ newValue: Self.Attribute, for handle: UInt16) -> Bool {
for (groupIndex, group) in attributeGroups.enumerated() {
for (attributeIndex, attribute) in group.attributes.enumerated() {
guard attribute.handle != handle else {
attributeGroups[groupIndex].attributes[attributeIndex] = newValue
return true
}
}
}
return false
}
}

// MARK: - ExpressibleByArrayLiteral
Expand Down

0 comments on commit d453bd7

Please sign in to comment.