|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2024 Apple Inc. and the Swift project authors. |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// |
| 10 | +//===----------------------------------------------------------------------===// |
| 11 | + |
| 12 | +/// Bluetooth address. |
| 13 | +public struct BluetoothAddress: Sendable { |
| 14 | + |
| 15 | + // MARK: - Properties |
| 16 | + |
| 17 | + /// Underlying address bytes (host endianess). |
| 18 | + public var bytes: ByteValue |
| 19 | + |
| 20 | + // MARK: - Initialization |
| 21 | + |
| 22 | + /// Initialize with the specifed bytes (in host endianess). |
| 23 | + public init(bytes: ByteValue = (0, 0, 0, 0, 0, 0)) { |
| 24 | + self.bytes = bytes |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +public extension BluetoothAddress { |
| 29 | + |
| 30 | + /// The minimum representable value in this type. |
| 31 | + static var min: BluetoothAddress { return BluetoothAddress(bytes: (.min, .min, .min, .min, .min, .min)) } |
| 32 | + |
| 33 | + /// The maximum representable value in this type. |
| 34 | + static var max: BluetoothAddress { return BluetoothAddress(bytes: (.max, .max, .max, .max, .max, .max)) } |
| 35 | + |
| 36 | + /// A zero address. |
| 37 | + static var zero: BluetoothAddress { return .min } |
| 38 | +} |
| 39 | + |
| 40 | +// MARK: - ByteValue |
| 41 | + |
| 42 | +extension BluetoothAddress { |
| 43 | + |
| 44 | + /// Raw Bluetooth Address 6 byte (48 bit) value. |
| 45 | + public typealias ByteValue = (UInt8, UInt8, UInt8, UInt8, UInt8, UInt8) |
| 46 | + |
| 47 | + public static var bitWidth: Int { return 48 } |
| 48 | + |
| 49 | + public static var length: Int { return 6 } |
| 50 | +} |
| 51 | + |
| 52 | +// MARK: - Equatable |
| 53 | + |
| 54 | +extension BluetoothAddress: Equatable { |
| 55 | + |
| 56 | + public static func == (lhs: BluetoothAddress, rhs: BluetoothAddress) -> Bool { |
| 57 | + return lhs.bytes.0 == rhs.bytes.0 |
| 58 | + && lhs.bytes.1 == rhs.bytes.1 |
| 59 | + && lhs.bytes.2 == rhs.bytes.2 |
| 60 | + && lhs.bytes.3 == rhs.bytes.3 |
| 61 | + && lhs.bytes.4 == rhs.bytes.4 |
| 62 | + && lhs.bytes.5 == rhs.bytes.5 |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +// MARK: - Hashable |
| 67 | + |
| 68 | +extension BluetoothAddress: Hashable { |
| 69 | + |
| 70 | + public func hash(into hasher: inout Hasher) { |
| 71 | + withUnsafeBytes(of: bytes) { hasher.combine(bytes: $0) } |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +// MARK: - Byte Swap |
| 76 | + |
| 77 | +extension BluetoothAddress: ByteSwap { |
| 78 | + |
| 79 | + /// A representation of this address with the byte order swapped. |
| 80 | + public var byteSwapped: BluetoothAddress { |
| 81 | + return BluetoothAddress(bytes: (bytes.5, bytes.4, bytes.3, bytes.2, bytes.1, bytes.0)) |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +// MARK: - RawRepresentable |
| 86 | + |
| 87 | +extension BluetoothAddress: RawRepresentable { |
| 88 | + |
| 89 | + /// Initialize a Bluetooth Address from its big endian string representation (e.g. `00:1A:7D:DA:71:13`). |
| 90 | + public init?(rawValue: String) { |
| 91 | + self.init(rawValue) |
| 92 | + } |
| 93 | + |
| 94 | + /// Initialize a Bluetooth Address from its big endian string representation (e.g. `00:1A:7D:DA:71:13`). |
| 95 | + internal init?<S: StringProtocol>(_ rawValue: S) { |
| 96 | + |
| 97 | + // verify string length |
| 98 | + let characters = rawValue.utf8 |
| 99 | + guard characters.count == 17, |
| 100 | + let separator = ":".utf8.first |
| 101 | + else { return nil } |
| 102 | + |
| 103 | + var bytes: ByteValue = (0, 0, 0, 0, 0, 0) |
| 104 | + |
| 105 | + let components = characters.split(whereSeparator: { $0 == separator }) |
| 106 | + |
| 107 | + guard components.count == 6 |
| 108 | + else { return nil } |
| 109 | + |
| 110 | + for (index, subsequence) in components.enumerated() { |
| 111 | + |
| 112 | + guard subsequence.count == 2, |
| 113 | + let byte = UInt8(hexadecimal: subsequence) |
| 114 | + else { return nil } |
| 115 | + |
| 116 | + withUnsafeMutablePointer(to: &bytes) { |
| 117 | + $0.withMemoryRebound(to: UInt8.self, capacity: 6) { |
| 118 | + $0.advanced(by: index).pointee = byte |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + self.init(bigEndian: BluetoothAddress(bytes: bytes)) |
| 124 | + } |
| 125 | + |
| 126 | + /// Convert a Bluetooth Address to its big endian string representation (e.g. `00:1A:7D:DA:71:13`). |
| 127 | + public var rawValue: String { |
| 128 | + let bytes = self.bigEndian.bytes |
| 129 | + return bytes.0.toHexadecimal() |
| 130 | + + ":" + bytes.1.toHexadecimal() |
| 131 | + + ":" + bytes.2.toHexadecimal() |
| 132 | + + ":" + bytes.3.toHexadecimal() |
| 133 | + + ":" + bytes.4.toHexadecimal() |
| 134 | + + ":" + bytes.5.toHexadecimal() |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +// MARK: - CustomStringConvertible |
| 139 | + |
| 140 | +extension BluetoothAddress: CustomStringConvertible { |
| 141 | + |
| 142 | + public var description: String { rawValue } |
| 143 | +} |
| 144 | + |
| 145 | +// MARK: - Data |
| 146 | + |
| 147 | +public extension BluetoothAddress { |
| 148 | + |
| 149 | + init?<Data: DataContainer>(data: Data) { |
| 150 | + guard data.count == type(of: self).length |
| 151 | + else { return nil } |
| 152 | + self.bytes = (data[0], data[1], data[2], data[3], data[4], data[5]) |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +// MARK: - Codable |
| 157 | + |
| 158 | +#if !hasFeature(Embedded) |
| 159 | +extension BluetoothAddress: Codable { } |
| 160 | +#endif |
0 commit comments