Skip to content

Commit

Permalink
Fixed #109 Added 128 bit UUID to 16 / 32 bit UUID conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
colemancda committed Sep 8, 2018
1 parent fec0a38 commit 6b71b20
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 5 deletions.
56 changes: 52 additions & 4 deletions Sources/BluetoothUUID.swift
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,58 @@ public extension BluetoothUUID {
}
}

internal extension UUID {

@inline(__always)
internal func bluetoothPrefix() -> (UInt8, UInt8, UInt8, UInt8)? {

// big endian
let baseUUID = BluetoothUUID.baseUUID.bytes

guard bytes.4 == baseUUID.4,
bytes.5 == baseUUID.5,
bytes.6 == baseUUID.6,
bytes.7 == baseUUID.7,
bytes.8 == baseUUID.8,
bytes.9 == baseUUID.9,
bytes.10 == baseUUID.10,
bytes.11 == baseUUID.11,
bytes.12 == baseUUID.12,
bytes.13 == baseUUID.13,
bytes.14 == baseUUID.14,
bytes.15 == baseUUID.15
else { return nil }

return (bytes.0, bytes.1, bytes.2, bytes.3)
}
}

public extension UInt16 {

/// Attempt to extract Bluetooth 16-bit UUID from standard 128-bit UUID.
init?(bluetooth uuid: Foundation.UUID) {

guard let prefixBytes = uuid.bluetoothPrefix(),
prefixBytes.0 == 0,
prefixBytes.1 == 0
else { return nil }

self.init(bigEndian: UInt16(bytes: (prefixBytes.2, prefixBytes.3)))
}
}

public extension UInt32 {

/// Attempt to extract Bluetooth 32-bit UUID from standard 128-bit UUID.
init?(bluetooth uuid: Foundation.UUID) {

guard let prefixBytes = uuid.bluetoothPrefix()
else { return nil }

self.init(bigEndian: UInt32(bytes: (prefixBytes.0, prefixBytes.1, prefixBytes.2, prefixBytes.3)))
}
}

// MARK: - NSUUID Conversion

public extension BluetoothUUID {
Expand Down Expand Up @@ -320,16 +372,12 @@ public extension Foundation.UUID {

// CBUUID is always big endian
self.init(bigEndian: uuid)

assert(self.rawValue == coreBluetooth.uuidString)
}

public func toCoreBluetooth() -> CBUUID {

let coreBluetooth = CBUUID(data: self.bigEndian.data)

assert(self.rawValue == coreBluetooth.uuidString)

return coreBluetooth
}
}
Expand Down
34 changes: 33 additions & 1 deletion Tests/BluetoothTests/BluetoothUUIDTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ final class BluetoothUUIDTests: XCTestCase {
("testPrimaryServiceUUID", testPrimaryServiceUUID),
("test128BitUUID", test128BitUUID),
("testDefinedUUID", testDefinedUUID),
("test32BitUUID", test32BitUUID)
("test32BitUUID", test32BitUUID),
("test16BitBaseUUID", test16BitBaseUUID),
("test32BitBaseUUID", test32BitBaseUUID)
]

func testMalformed() {
Expand Down Expand Up @@ -198,6 +200,36 @@ final class BluetoothUUIDTests: XCTestCase {
XCTAssertEqual(BluetoothUUID.bit16(1000).rawValue, "03E8")
}

func test16BitBaseUUID() {

let uuids: [UInt16: UUID] = [
0x1800: UUID(rawValue: "00001800-0000-1000-8000-00805F9B34FB")!,
0x1801: UUID(rawValue: "00001801-0000-1000-8000-00805F9B34FB")!,
0xFE59: UUID(rawValue: "0000FE59-0000-1000-8000-00805F9B34FB")!
]

uuids.forEach {
XCTAssertEqual($0.key, UInt16(bluetooth: $0.value))
XCTAssertEqual(UInt128(.bit16($0.key)), UInt128(uuid: $0.value))
}
}

func test32BitBaseUUID() {

let uuids: [UInt32: UUID] = [
0x00001800: UUID(rawValue: "00001800-0000-1000-8000-00805F9B34FB")!,
0x00001801: UUID(rawValue: "00001801-0000-1000-8000-00805F9B34FB")!,
0x0000FE59: UUID(rawValue: "0000FE59-0000-1000-8000-00805F9B34FB")!,
0x00000000: UUID(rawValue: "00000000-0000-1000-8000-00805F9B34FB")!,
0x12345678: UUID(rawValue: "12345678-0000-1000-8000-00805F9B34FB")!
]

uuids.forEach {
XCTAssertEqual($0.key, UInt32(bluetooth: $0.value))
XCTAssertEqual(UInt128(.bit32($0.key)), UInt128(uuid: $0.value))
}
}

func testPerformanceStringParse() {

let uuids = randomUUIDs.map { $0.uuidString }
Expand Down

0 comments on commit 6b71b20

Please sign in to comment.