|
| 1 | +// |
| 2 | +// ISOSocket.swift |
| 3 | +// BluetoothLinux |
| 4 | +// |
| 5 | + |
| 6 | +import Foundation |
| 7 | +import Bluetooth |
| 8 | +import SystemPackage |
| 9 | +import Socket |
| 10 | + |
| 11 | +/// Bluetooth ISO Socket |
| 12 | +/// |
| 13 | +/// Isochronous channels carry time-sensitive audio data between the host |
| 14 | +/// and Low Energy devices (LE Audio). Requires kernel 6.0 or later; on older |
| 15 | +/// kernels the ISO socket is gated behind an experimental feature toggled |
| 16 | +/// via the management interface. |
| 17 | +public struct ISOSocket: Sendable { |
| 18 | + |
| 19 | + // MARK: - Properties |
| 20 | + |
| 21 | + @usableFromInline |
| 22 | + internal let fileDescriptor: SocketDescriptor |
| 23 | + |
| 24 | + /// Socket address. |
| 25 | + public let address: ISOSocketAddress |
| 26 | + |
| 27 | + // MARK: - Initialization |
| 28 | + |
| 29 | + internal init( |
| 30 | + fileDescriptor: SocketDescriptor, |
| 31 | + address: ISOSocketAddress |
| 32 | + ) { |
| 33 | + self.fileDescriptor = fileDescriptor |
| 34 | + self.address = address |
| 35 | + } |
| 36 | + |
| 37 | + /// Create a new ISO socket bound to the specified address. |
| 38 | + public init(address: ISOSocketAddress) throws(Errno) { |
| 39 | + self.fileDescriptor = try .iso(address, [.closeOnExec, .nonBlocking]) |
| 40 | + self.address = address |
| 41 | + } |
| 42 | + |
| 43 | + /// Creates a client socket connected to the specified remote device. |
| 44 | + /// |
| 45 | + /// - Note: The quality of service must be configured before connecting |
| 46 | + /// when using non-default stream parameters. |
| 47 | + public static func client( |
| 48 | + address localAddress: ISOSocketAddress, |
| 49 | + destination destinationAddress: ISOSocketAddress, |
| 50 | + qualityOfService: BluetoothSocketOption.ISOQualityOfService? = nil |
| 51 | + ) throws(Errno) -> Self { |
| 52 | + let fileDescriptor = try SocketDescriptor.iso(localAddress, [.closeOnExec, .nonBlocking]) |
| 53 | + |
| 54 | + // configure stream parameters before connecting |
| 55 | + if let qualityOfService { |
| 56 | + do { |
| 57 | + try fileDescriptor.setSocketOption(qualityOfService) |
| 58 | + } catch { |
| 59 | + try? fileDescriptor.close() |
| 60 | + throw error |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + // Start async connect - for non-blocking sockets this returns EINPROGRESS |
| 65 | + do { |
| 66 | + try fileDescriptor.connect(to: destinationAddress) |
| 67 | + } catch Errno.nowInProgress { |
| 68 | + // Expected for non-blocking socket - connection is in progress |
| 69 | + // Wait for socket to become writable (indicates connect completed) |
| 70 | + let timeout: Int = 30_000 // 30 seconds in milliseconds |
| 71 | + let events = try fileDescriptor.poll(for: [.write, .error, .hangup], timeout: timeout) |
| 72 | + |
| 73 | + // Check for errors |
| 74 | + if events.contains(.error) || events.contains(.hangup) { |
| 75 | + try? fileDescriptor.close() |
| 76 | + throw Errno.connectionRefused |
| 77 | + } |
| 78 | + |
| 79 | + // Check if we timed out (no events returned) |
| 80 | + if !events.contains(.write) { |
| 81 | + try? fileDescriptor.close() |
| 82 | + throw Errno.timedOut |
| 83 | + } |
| 84 | + } catch { |
| 85 | + // Other errors during connect |
| 86 | + try? fileDescriptor.close() |
| 87 | + throw error |
| 88 | + } |
| 89 | + |
| 90 | + return Self.init(fileDescriptor: fileDescriptor, address: localAddress) |
| 91 | + } |
| 92 | + |
| 93 | + /// Creates a server socket listening on the specified address. |
| 94 | + public static func server( |
| 95 | + address: ISOSocketAddress, |
| 96 | + backlog: Int = Socket.maxBacklog |
| 97 | + ) throws(Errno) -> Self { |
| 98 | + let fileDescriptor = try SocketDescriptor.iso(address, [.closeOnExec, .nonBlocking]) |
| 99 | + try fileDescriptor.closeIfThrows { () throws(Errno) -> () in |
| 100 | + try fileDescriptor.listen(backlog: backlog) |
| 101 | + } |
| 102 | + return Self.init(fileDescriptor: fileDescriptor, address: address) |
| 103 | + } |
| 104 | + |
| 105 | + // MARK: - Methods |
| 106 | + |
| 107 | + /// Close socket. |
| 108 | + public func close() { |
| 109 | + try? fileDescriptor.close() |
| 110 | + } |
| 111 | + |
| 112 | + /// Attempt to accept an incoming connection. |
| 113 | + public func accept() throws(Errno) -> Self { |
| 114 | + let (fileDescriptor, address) = try self.fileDescriptor.accept(ISOSocketAddress.self) |
| 115 | + return Self.init( |
| 116 | + fileDescriptor: fileDescriptor, |
| 117 | + address: address |
| 118 | + ) |
| 119 | + } |
| 120 | + |
| 121 | + /// Write to the socket. |
| 122 | + public func send(_ data: Data) throws(Errno) -> Int { |
| 123 | + do { |
| 124 | + return try data.withUnsafeBytes { (bytes) throws(Errno) -> Int in |
| 125 | + try fileDescriptor.write(bytes) |
| 126 | + } |
| 127 | + } |
| 128 | + catch { |
| 129 | + throw error as! Errno // TODO: Foundation doesnt support typed error yet |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + /// Reads from the socket. |
| 134 | + public func receive(_ length: Int) throws(Errno) -> Data { |
| 135 | + do { |
| 136 | + var data = Data(count: length) |
| 137 | + let bytesRead = try data.withUnsafeMutableBytes { (bytes) throws(Errno) -> Int in |
| 138 | + try fileDescriptor.read(into: bytes) |
| 139 | + } |
| 140 | + if bytesRead < length { |
| 141 | + data = data.prefix(bytesRead) |
| 142 | + } |
| 143 | + return data |
| 144 | + } |
| 145 | + catch { |
| 146 | + throw error as! Errno // TODO: Foundation doesnt support typed error yet |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + // MARK: - Options |
| 151 | + |
| 152 | + /// The quality of service of the socket. |
| 153 | + public var qualityOfService: BluetoothSocketOption.ISOQualityOfService { |
| 154 | + get throws(Errno) { |
| 155 | + try fileDescriptor.getSocketOption(BluetoothSocketOption.ISOQualityOfService.self) |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + /// Set the quality of service of the socket. |
| 160 | + /// |
| 161 | + /// Must be configured before the connection is established. |
| 162 | + public func setQualityOfService(_ qualityOfService: BluetoothSocketOption.ISOQualityOfService) throws(Errno) { |
| 163 | + try fileDescriptor.setSocketOption(qualityOfService) |
| 164 | + } |
| 165 | +} |
0 commit comments