Skip to content

Commit

Permalink
#154 Fix BluetoothGAP Embedded Swift support
Browse files Browse the repository at this point in the history
  • Loading branch information
colemancda committed Nov 5, 2024
1 parent 550b1fb commit b096687
Show file tree
Hide file tree
Showing 13 changed files with 64 additions and 85 deletions.
22 changes: 0 additions & 22 deletions Sources/BluetoothGAP/Extensions/UUID.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,6 @@ internal extension UUID {
static var length: Int { return 16 }
}

internal extension UUID { // ByteValue

typealias ByteValue = uuid_t

static var bitWidth: Int { return 128 }

@inline(__always)
init(bytes: uuid_t) {

self.init(uuid: bytes)
}

var bytes: uuid_t {

@inline(__always)
get { return uuid }

@inline(__always)
set { self = UUID(uuid: newValue) }
}
}

internal extension UUID {

init?(data: Data) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,11 @@ extension GAPCompleteListOf128BitServiceClassUUIDs: ExpressibleByArrayLiteral {

// MARK: - CustomStringConvertible

#if !hasFeature(Embedded)
extension GAPCompleteListOf128BitServiceClassUUIDs: CustomStringConvertible {

public var description: String {

return uuids.description
}
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,11 @@ extension GAPCompleteListOf16BitServiceClassUUIDs: ExpressibleByArrayLiteral {

// MARK: - CustomStringConvertible

#if !hasFeature(Embedded)
extension GAPCompleteListOf16BitServiceClassUUIDs: CustomStringConvertible {

public var description: String {

return uuids.map { BluetoothUUID.bit16($0) }.description
}
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,11 @@ extension GAPCompleteListOf32BitServiceClassUUIDs: ExpressibleByArrayLiteral {

// MARK: - CustomStringConvertible

#if !hasFeature(Embedded)
extension GAPCompleteListOf32BitServiceClassUUIDs: CustomStringConvertible {

public var description: String {

return uuids.map { BluetoothUUID.bit32($0) }.description
}
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,11 @@ extension GAPIncompleteListOf128BitServiceClassUUIDs: ExpressibleByArrayLiteral

// MARK: - CustomStringConvertible

#if !hasFeature(Embedded)
extension GAPIncompleteListOf128BitServiceClassUUIDs: CustomStringConvertible {

public var description: String {

return uuids.description
}
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,11 @@ extension GAPIncompleteListOf16BitServiceClassUUIDs: ExpressibleByArrayLiteral {

// MARK: - CustomStringConvertible

#if !hasFeature(Embedded)
extension GAPIncompleteListOf16BitServiceClassUUIDs: CustomStringConvertible {

public var description: String {

return uuids.map { BluetoothUUID.bit16($0) }.description
}
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,11 @@ extension GAPIncompleteListOf32BitServiceClassUUIDs: ExpressibleByArrayLiteral {

// MARK: - CustomStringConvertible

#if !hasFeature(Embedded)
extension GAPIncompleteListOf32BitServiceClassUUIDs: CustomStringConvertible {

public var description: String {

return uuids.map { BluetoothUUID.bit32($0) }.description
}
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,11 @@ extension GAPListOf128BitServiceSolicitationUUIDs: ExpressibleByArrayLiteral {

// MARK: - CustomStringConvertible

#if !hasFeature(Embedded)
extension GAPListOf128BitServiceSolicitationUUIDs: CustomStringConvertible {

public var description: String {

return uuids.description
}
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,11 @@ extension GAPListOf16BitServiceSolicitationUUIDs: ExpressibleByArrayLiteral {

// MARK: - CustomStringConvertible

#if !hasFeature(Embedded)
extension GAPListOf16BitServiceSolicitationUUIDs: CustomStringConvertible {

public var description: String {

return uuids.map { BluetoothUUID.bit16($0) }.description
}
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,11 @@ extension GAPListOf32BitServiceSolicitationUUIDs: ExpressibleByArrayLiteral {

// MARK: - CustomStringConvertible

#if !hasFeature(Embedded)
extension GAPListOf32BitServiceSolicitationUUIDs: CustomStringConvertible {

public var description: String {

return uuids.map { BluetoothUUID.bit32($0) }.description
}
}
#endif
88 changes: 40 additions & 48 deletions Sources/BluetoothGAP/GAPMeshBeacon.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,18 @@ public extension GAPMeshBeacon {

switch self {
case let .unprovisionedDevice(beacon):
data += beacon.data
data += beacon
case let .secureNetwork(beacon):
data += beacon.data
data += beacon
}
}

var dataLength: Int {

// FIXME: Improve performance
switch self {
case let .unprovisionedDevice(beacon):
return beacon.data.count
return beacon.dataLength
case let .secureNetwork(beacon):
return beacon.data.count
return beacon.dataLength
}
}
}
Expand All @@ -90,7 +88,7 @@ public protocol GAPMeshBeaconProtocol {

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

var data: Data { get }

}

/**
Expand Down Expand Up @@ -152,28 +150,6 @@ public struct GAPUnprovisionedDeviceBeacon: GAPMeshBeaconProtocol, Equatable {
self.init(deviceUUID: deviceUUID, oobInformationFlags: oobInformationFlags, uriHash: uriHash)
}

public var data: Data {

let length = DataLength(uriHash: uriHash != nil)

var data = Data(capacity: length.rawValue)

data.append(type(of: self).beaconType.rawValue)

data += UInt128(uuid: deviceUUID).littleEndian

let flagsBytes = oobInformationFlags.rawValue.littleEndian.bytes

data += [flagsBytes.0, flagsBytes.1]

if let uriBytes = uriHash?.littleEndian.bytes {

data += [uriBytes.0, uriBytes.1, uriBytes.2, uriBytes.3]
}

return data
}

struct DataLength: RawRepresentable {

static let minimum = DataLength(19)
Expand Down Expand Up @@ -215,6 +191,26 @@ public struct GAPUnprovisionedDeviceBeacon: GAPMeshBeaconProtocol, Equatable {
}
}

extension GAPUnprovisionedDeviceBeacon: DataConvertible {

public static func += <Data: DataContainer> (data: inout Data, value: Self) {

data.append(Self.beaconType.rawValue)
data += UInt128(uuid: value.deviceUUID).littleEndian

let flagsBytes = value.oobInformationFlags.rawValue.littleEndian.bytes
data += [flagsBytes.0, flagsBytes.1]

if let uriBytes = value.uriHash?.littleEndian.bytes {
data += [uriBytes.0, uriBytes.1, uriBytes.2, uriBytes.3]
}
}

public var dataLength: Int {
DataLength(uriHash: uriHash != nil).rawValue
}
}

@frozen
public enum GAPBeaconType: UInt8 {

Expand Down Expand Up @@ -297,7 +293,7 @@ public enum GAPSecureNetworkFlag: UInt8, BitMaskOption {
@frozen
public struct GAPSecureNetworkBeacon: GAPMeshBeaconProtocol, Equatable {

internal static let length = 22
internal static var length: Int { 22 }

public static let beaconType: GAPBeaconType = .secureNetwork

Expand Down Expand Up @@ -330,36 +326,32 @@ public struct GAPSecureNetworkBeacon: GAPMeshBeaconProtocol, Equatable {
else { return nil }

let flags = BitMaskOptionSet<GAPSecureNetworkFlag>(rawValue: data[1])

let networkID = UInt64(littleEndian: UInt64(bytes: (data[2], data[3], data[4], data[5], data[6], data[7], data[8], data[9])))

let ivIndex = UInt32(littleEndian: UInt32(bytes: (data[10], data[11], data[12], data[13])))

let authenticationValue = UInt64(littleEndian: UInt64(bytes: (data[14], data[15], data[16], data[17], data[18], data[19], data[20], data[21])))

self.init(flags: flags, networkID: networkID, ivIndex: ivIndex, authenticationValue: authenticationValue)
}
}

extension GAPSecureNetworkBeacon: DataConvertible {

public var data: Data {

var data = Data(capacity: type(of: self).length)

data.append(type(of: self).beaconType.rawValue)

data.append(flags.rawValue)

let networkIDBytes = networkID.littleEndian.bytes
static func += <T: DataContainer> (data: inout T, value: Self) {

data.append(Self.beaconType.rawValue)
data.append(value.flags.rawValue)

let networkIDBytes = value.networkID.littleEndian.bytes
data += [networkIDBytes.0, networkIDBytes.1, networkIDBytes.2, networkIDBytes.3, networkIDBytes.4, networkIDBytes.5, networkIDBytes.6, networkIDBytes.7]

let ivIndexBytes = ivIndex.littleEndian.bytes

let ivIndexBytes = value.ivIndex.littleEndian.bytes
data += [ivIndexBytes.0, ivIndexBytes.1, ivIndexBytes.2, ivIndexBytes.3]

let authValueBytes = authenticationValue.littleEndian.bytes

let authValueBytes = value.authenticationValue.littleEndian.bytes
data += [authValueBytes.0, authValueBytes.1, authValueBytes.2, authValueBytes.3, authValueBytes.4, authValueBytes.5, authValueBytes.6, authValueBytes.7]

return data
}

var dataLength: Int {
Self.length
}
}
3 changes: 2 additions & 1 deletion Sources/BluetoothGAP/GAPPublicTargetAddress.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,14 @@ public extension GAPPublicTargetAddress {

// MARK: - CustomStringConvertible

#if !hasFeature(Embedded)
extension GAPPublicTargetAddress: CustomStringConvertible {

public var description: String {

return addresses.description
}
}
#endif

// MARK: - ExpressibleByArrayLiteral

Expand Down
9 changes: 4 additions & 5 deletions Sources/BluetoothGAP/GAPRandomTargetAddress.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,15 @@ import Bluetooth
/// The format of each 6 octet address is the same as the Random Device Address defined in Vol. 6, Part B, Section 1.3.
/// The Random Target Address value shall be the enumerated value as defined by Bluetooth Assigned Numbers.
@frozen
public struct GAPRandomTargetAddress: GAPData, Equatable {
public struct GAPRandomTargetAddress: GAPData, Equatable, Hashable, Sendable {

public typealias ByteValue = (UInt8, UInt8, UInt8)

public static let dataType: GAPDataType = .randomTargetAddress
public static var dataType: GAPDataType { .randomTargetAddress }

public let addresses: [BluetoothAddress]

public init(addresses: [BluetoothAddress]) {

self.addresses = addresses
}
}
Expand All @@ -50,24 +49,24 @@ public extension GAPRandomTargetAddress {
}

var dataLength: Int {

return addresses.count * BluetoothAddress.length
}

func append<Data: DataContainer>(to data: inout Data) {

addresses.forEach { data += $0.littleEndian }
}
}

// MARK: - CustomStringConvertible

#if !hasFeature(Embedded)
extension GAPRandomTargetAddress: CustomStringConvertible {

public var description: String {
return addresses.description
}
}
#endif

// MARK: - ExpressibleByArrayLiteral

Expand Down

0 comments on commit b096687

Please sign in to comment.