Skip to content

Commit

Permalink
#161 Fix BluetoothGATT data serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
colemancda committed Nov 5, 2024
1 parent 59f8b88 commit 0aee5ce
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 35 deletions.
3 changes: 2 additions & 1 deletion Sources/BluetoothGATT/ATTFindInformationResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

import Foundation
import Bluetooth

/// Find Information Response
///
Expand Down Expand Up @@ -227,7 +228,7 @@ public extension ATTFindInformationResponse {
let uuid = UInt16(littleEndian: UInt16(bytes: (pairBytes[2], pairBytes[3])))
bit16Pairs.append(Attribute16Bit(handle: handle, uuid: uuid))
case .bit128:
let uuidBytes = pairBytes.subdataNoCopy(in: 2 ..< 18)
let uuidBytes = pairBytes.subdata(in: 2 ..< 18)
let uuid = UInt128(littleEndian: UInt128(data: uuidBytes)!)
bit128Pairs.append(Attribute128Bit(handle: handle, uuid: uuid))
}
Expand Down
34 changes: 5 additions & 29 deletions Sources/BluetoothGATT/GAPUUIDList.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
//

import Foundation
import Bluetooth

/// GAP UUID List
internal struct GAPUUIDList <Element: GAPUUIDElement> {

internal var uuids: [Element]

internal init(uuids: [Element]) {

self.uuids = uuids
}

Expand Down Expand Up @@ -96,7 +96,7 @@ extension GAPUUIDList: Collection {

internal protocol GAPUUIDElement: UnsafeDataConvertible {

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

init(littleEndian: Self)

Expand All @@ -107,7 +107,7 @@ internal protocol GAPUUIDElement: UnsafeDataConvertible {

extension UInt16: GAPUUIDElement {

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

guard data.count == MemoryLayout<UInt16>.size
else { return nil }
Expand All @@ -119,7 +119,7 @@ extension UInt16: GAPUUIDElement {

extension UInt32: GAPUUIDElement {

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

guard data.count == MemoryLayout<UInt32>.size
else { return nil }
Expand All @@ -131,28 +131,4 @@ extension UInt32: GAPUUIDElement {
}
}

extension UInt128: GAPUUIDElement {

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

guard data.count == MemoryLayout<UInt128>.size
else { return nil }

self.init(bytes: (data[0],
data[1],
data[2],
data[3],
data[4],
data[5],
data[6],
data[7],
data[8],
data[9],
data[10],
data[11],
data[12],
data[13],
data[14],
data[15]))
}
}
extension Bluetooth.UInt128: GAPUUIDElement { }
4 changes: 2 additions & 2 deletions Sources/BluetoothGATT/GATTClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ public actor GATTClient {
startHandle: start,
endHandle: end,
attributeType: attributeType.rawValue,
attributeValue: uuid.littleEndian.data
attributeValue: Data(uuid.littleEndian)
)
let response = try await send(request, response: ATTFindByTypeResponse.self)
try await findByTypeResponse(response, &operation)
Expand Down Expand Up @@ -766,7 +766,7 @@ public actor GATTClient {
startHandle: operation.start,
endHandle: operation.end,
attributeType: operation.type.rawValue,
attributeValue: serviceUUID.littleEndian.data
attributeValue: Data(serviceUUID.littleEndian)
)
let response = try await send(request, response: ATTFindByTypeResponse.self)
try await findByTypeResponse(response, &operation)
Expand Down
2 changes: 1 addition & 1 deletion Sources/BluetoothGATT/GATTDatabase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ internal extension GATTDatabase {
return Attribute(
handle: handle,
uuid: serviceUUID,
value: self.uuid.littleEndian.data,
value: Data(self.uuid.littleEndian),
permissions: [.read]
)
}
Expand Down
3 changes: 1 addition & 2 deletions Sources/BluetoothGATT/GATTExternalReportReference.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ public struct GATTExternalReportReference: GATTDescriptor {
}

public var data: Data {

return uuid.data
return Data(uuid)
}

public var descriptor: GATTAttribute.Descriptor {
Expand Down

0 comments on commit 0aee5ce

Please sign in to comment.