Skip to content

Commit 8863c21

Browse files
committed
Revert "Fixed POSIXError.errorno"
This reverts commit bf54a21.
1 parent bf54a21 commit 8863c21

File tree

7 files changed

+42
-36
lines changed

7 files changed

+42
-36
lines changed

Sources/BluetoothLinux/DeviceCommand.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,5 @@ internal func HCISendCommand <T: HCICommand> (_ deviceDescriptor: CInt,
4747

4848
// write to device descriptor socket
4949
guard write(deviceDescriptor, &data, data.count) >= 0 // should we check if all data was written?
50-
else { throw POSIXError.errorno }
50+
else { throw POSIXError.fromErrno! }
5151
}

Sources/BluetoothLinux/DevicePollEvent.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ internal func HCIPollEvent(_ deviceDescriptor: CInt,
4444
guard withUnsafeMutablePointer(to: &oldFilter, {
4545
let pointer = UnsafeMutableRawPointer($0)
4646
return getsockopt(deviceDescriptor, SOL_HCI, HCISocketOption.Filter.rawValue, pointer, &oldFilterLength) == 0
47-
}) else { throw POSIXError.errorno }
47+
}) else { throw POSIXError.fromErrno! }
4848

4949
var newFilter = HCIFilter()
5050
newFilter.clear()
@@ -56,15 +56,15 @@ internal func HCIPollEvent(_ deviceDescriptor: CInt,
5656
guard withUnsafeMutablePointer(to: &newFilter, {
5757
let pointer = UnsafeMutableRawPointer($0)
5858
return setsockopt(deviceDescriptor, SOL_HCI, HCISocketOption.Filter.rawValue, pointer, newFilterLength) == 0
59-
}) else { throw POSIXError.errorno }
59+
}) else { throw POSIXError.fromErrno! }
6060

6161
// restore old filter in case of error
6262
func restoreFilter(_ error: Error) -> Error {
6363

6464
guard withUnsafeMutablePointer(to: &oldFilter, {
6565
let pointer = UnsafeMutableRawPointer($0)
6666
return setsockopt(deviceDescriptor, SOL_HCI, HCISocketOption.Filter.rawValue, pointer, newFilterLength) == 0
67-
}) else { return BluetoothHostControllerError.couldNotRestoreFilter(error, POSIXError.errorno) }
67+
}) else { return BluetoothHostControllerError.couldNotRestoreFilter(error, POSIXError.fromErrno!) }
6868

6969
return error
7070
}
@@ -102,7 +102,7 @@ internal func HCIPollEvent(_ deviceDescriptor: CInt,
102102
} else {
103103

104104
// attempt to restore filter and throw
105-
throw restoreFilter(POSIXError.errorno)
105+
throw restoreFilter(POSIXError.fromErrno!)
106106
}
107107
}
108108

Sources/BluetoothLinux/DeviceRequest.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ internal func HCISendRequest <Command: HCICommand> (_ deviceDescriptor: CInt,
208208

209209
// get old filter
210210
guard getsockopt(deviceDescriptor, SOL_HCI, HCISocketOption.Filter.rawValue, oldFilterPointer, &filterLength) == 0
211-
else { throw POSIXError.errorno }
211+
else { throw POSIXError.fromErrno! }
212212

213213
// configure new filter
214214
newFilter.clear()
@@ -222,13 +222,13 @@ internal func HCISendRequest <Command: HCICommand> (_ deviceDescriptor: CInt,
222222

223223
// set new filter
224224
guard setsockopt(deviceDescriptor, SOL_HCI, HCISocketOption.Filter.rawValue, newFilterPointer, filterLength) == 0
225-
else { throw POSIXError.errorno }
225+
else { throw POSIXError.fromErrno! }
226226

227227
// restore old filter in case of error
228228
func restoreFilter(_ error: Error) -> Error {
229229

230230
guard setsockopt(deviceDescriptor, SOL_HCI, HCISocketOption.Filter.rawValue, oldFilterPointer, filterLength) == 0
231-
else { return BluetoothHostControllerError.couldNotRestoreFilter(error, POSIXError.errorno) }
231+
else { return BluetoothHostControllerError.couldNotRestoreFilter(error, POSIXError.fromErrno!) }
232232

233233
return error
234234
}
@@ -268,7 +268,7 @@ internal func HCISendRequest <Command: HCICommand> (_ deviceDescriptor: CInt,
268268
} else {
269269

270270
// attempt to restore filter and throw
271-
throw restoreFilter(POSIXError.errorno)
271+
throw restoreFilter(POSIXError.fromErrno!)
272272
}
273273
}
274274

@@ -298,7 +298,7 @@ internal func HCISendRequest <Command: HCICommand> (_ deviceDescriptor: CInt,
298298
} else {
299299

300300
// attempt to restore filter and throw
301-
throw restoreFilter(POSIXError.errorno)
301+
throw restoreFilter(POSIXError.fromErrno!)
302302
}
303303
}
304304

@@ -317,7 +317,7 @@ internal func HCISendRequest <Command: HCICommand> (_ deviceDescriptor: CInt,
317317
func done() throws {
318318

319319
guard setsockopt(deviceDescriptor, SOL_HCI, HCISocketOption.Filter.rawValue, oldFilterPointer, filterLength) == 0
320-
else { throw POSIXError.errorno }
320+
else { throw POSIXError.fromErrno! }
321321
}
322322

323323
switch eventHeader.event {

Sources/BluetoothLinux/HostController.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ internal func HCIOpenDevice(_ deviceIdentifier: UInt16) throws -> CInt {
117117
// Create HCI socket
118118
let hciSocket = socket(AF_BLUETOOTH, SOCK_RAW | SOCK_CLOEXEC, BluetoothProtocol.hci.rawValue)
119119

120-
guard hciSocket >= 0 else { throw POSIXError.errorno }
120+
guard hciSocket >= 0 else { throw POSIXError.fromErrno! }
121121

122122
// Bind socket to the HCI device
123123
var address = HCISocketAddress()
@@ -132,7 +132,7 @@ internal func HCIOpenDevice(_ deviceIdentifier: UInt16) throws -> CInt {
132132

133133
guard didBind else {
134134
close(hciSocket)
135-
throw POSIXError.errorno
135+
throw POSIXError.fromErrno!
136136
}
137137

138138
return hciSocket
@@ -143,7 +143,7 @@ internal func HCIRequestDeviceList <T> (_ response: (_ hciSocket: CInt, _ list:
143143
// open HCI socket
144144
let hciSocket = socket(AF_BLUETOOTH, SOCK_RAW | SOCK_CLOEXEC, BluetoothProtocol.hci.rawValue)
145145

146-
guard hciSocket >= 0 else { throw POSIXError.errorno }
146+
guard hciSocket >= 0 else { throw POSIXError.fromErrno! }
147147

148148
defer { close(hciSocket) }
149149

@@ -156,7 +156,7 @@ internal func HCIRequestDeviceList <T> (_ response: (_ hciSocket: CInt, _ list:
156156
IOControl(hciSocket, HCI.IOCTL.GetDeviceList, $0)
157157
}
158158

159-
guard ioctlValue >= 0 else { throw POSIXError.errorno }
159+
guard ioctlValue >= 0 else { throw POSIXError.fromErrno! }
160160

161161
return try response(hciSocket, &deviceList)
162162
}
@@ -212,7 +212,7 @@ internal func HCIGetRoute(_ address: BluetoothAddress? = nil) throws -> UInt16?
212212

213213
guard withUnsafeMutablePointer(to: &deviceInfo, {
214214
IOControl(CInt(dd), HCI.IOCTL.GetDeviceInfo, UnsafeMutableRawPointer($0)) }) == 0
215-
else { throw POSIXError.errorno }
215+
else { throw POSIXError.fromErrno! }
216216

217217
return deviceInfo.address == address
218218
}
@@ -224,7 +224,7 @@ internal func HCIDeviceInfo(_ deviceIdentifier: UInt16) throws -> HCIDeviceInfor
224224

225225
let hciSocket = socket(AF_BLUETOOTH, SOCK_RAW | SOCK_CLOEXEC, BluetoothProtocol.hci.rawValue)
226226

227-
guard hciSocket >= 0 else { throw POSIXError.errorno }
227+
guard hciSocket >= 0 else { throw POSIXError.fromErrno! }
228228

229229
defer { close(hciSocket) }
230230

@@ -233,7 +233,7 @@ internal func HCIDeviceInfo(_ deviceIdentifier: UInt16) throws -> HCIDeviceInfor
233233

234234
guard withUnsafeMutablePointer(to: &deviceInfo, {
235235
IOControl(hciSocket, HCI.IOCTL.GetDeviceInfo, UnsafeMutableRawPointer($0)) }) == 0
236-
else { throw POSIXError.errorno }
236+
else { throw POSIXError.fromErrno! }
237237

238238
return deviceInfo
239239
}

Sources/BluetoothLinux/L2CAP.swift

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public final class L2CAPSocket: L2CAPSocketProtocol {
114114
var optionLength = socklen_t(MemoryLayout<CInt>.size)
115115

116116
guard getsockopt(fileDescriptor, SOL_SOCKET, socketOption, &optionValue, &optionLength) == 0
117-
else { throw POSIXError.errorno }
117+
else { throw POSIXError.fromErrno! }
118118

119119
return optionValue
120120
}
@@ -141,7 +141,7 @@ public final class L2CAPSocket: L2CAPSocketProtocol {
141141

142142
// error creating socket
143143
guard internalSocket >= 0
144-
else { throw POSIXError.errorno }
144+
else { throw POSIXError.fromErrno! }
145145

146146
// set source address
147147
var localAddress = sockaddr_l2()
@@ -157,7 +157,7 @@ public final class L2CAPSocket: L2CAPSocketProtocol {
157157
$0.withMemoryRebound(to: sockaddr.self, capacity: 1, {
158158
bind(internalSocket, $0, socklen_t(MemoryLayout<sockaddr_l2>.size)) == 0
159159
})
160-
}) else { close(internalSocket); throw POSIXError.errorno }
160+
}) else { close(internalSocket); throw POSIXError.fromErrno! }
161161

162162
return (internalSocket, localAddress)
163163
}
@@ -200,7 +200,7 @@ public final class L2CAPSocket: L2CAPSocketProtocol {
200200
security.level = securityLevel.rawValue
201201

202202
guard setsockopt(internalSocket, SOL_BLUETOOTH, BT_SECURITY, &security, socklen_t(MemoryLayout<bt_security>.size)) == 0
203-
else { throw POSIXError.errorno }
203+
else { throw POSIXError.fromErrno! }
204204

205205
self.securityLevel = securityLevel
206206
}
@@ -210,7 +210,7 @@ public final class L2CAPSocket: L2CAPSocketProtocol {
210210

211211
// put socket into listening mode
212212
guard listen(internalSocket, Int32(queueLimit)) == 0
213-
else { throw POSIXError.errorno }
213+
else { throw POSIXError.fromErrno! }
214214
}
215215

216216
/// Blocks the caller until a new connection is recieved.
@@ -228,7 +228,7 @@ public final class L2CAPSocket: L2CAPSocketProtocol {
228228
})
229229

230230
// error accepting new connection
231-
guard client >= 0 else { throw POSIXError.errorno }
231+
guard client >= 0 else { throw POSIXError.fromErrno! }
232232

233233
let newSocket = L2CAPSocket(clientSocket: client,
234234
remoteAddress: remoteAddress,
@@ -257,7 +257,7 @@ public final class L2CAPSocket: L2CAPSocketProtocol {
257257
$0.withMemoryRebound(to: sockaddr.self, capacity: 1, {
258258
connect(internalSocket, $0, socklen_t(MemoryLayout<sockaddr_l2>.size)) == 0
259259
})
260-
}) else { throw POSIXError.errorno }
260+
}) else { throw POSIXError.fromErrno! }
261261

262262
// make socket non-blocking
263263
try setNonblocking()
@@ -275,7 +275,13 @@ public final class L2CAPSocket: L2CAPSocketProtocol {
275275

276276
let actualByteCount = read(internalSocket, &buffer, bufferSize)
277277

278-
guard actualByteCount >= 0 else { throw POSIXError.errorno }
278+
guard actualByteCount >= 0 else {
279+
if let error = POSIXError.fromErrno {
280+
throw error
281+
} else {
282+
return nil
283+
}
284+
}
279285

280286
let actualBytes = Array(buffer.prefix(actualByteCount))
281287

@@ -293,7 +299,7 @@ public final class L2CAPSocket: L2CAPSocketProtocol {
293299
let fdCount = select(internalSocket + 1, &readSockets, nil, nil, &time)
294300

295301
guard fdCount != -1
296-
else { throw POSIXError.errorno }
302+
else { throw POSIXError.fromErrno! }
297303

298304
return fdCount > 0
299305
}
@@ -303,12 +309,12 @@ public final class L2CAPSocket: L2CAPSocketProtocol {
303309
var flags = fcntl(internalSocket, F_GETFL, 0)
304310

305311
guard flags != -1
306-
else { throw POSIXError.errorno }
312+
else { throw POSIXError.fromErrno! }
307313

308314
flags = fcntl(internalSocket, F_SETFL, flags | O_NONBLOCK);
309315

310316
guard flags != -1
311-
else { throw POSIXError.errorno }
317+
else { throw POSIXError.fromErrno! }
312318
}
313319

314320
/// Write to the socket.
@@ -319,7 +325,7 @@ public final class L2CAPSocket: L2CAPSocketProtocol {
319325
let actualByteCount = write(internalSocket, &buffer, buffer.count)
320326

321327
guard actualByteCount >= 0
322-
else { throw POSIXError.errorno }
328+
else { throw POSIXError.fromErrno! }
323329

324330
guard actualByteCount == buffer.count
325331
else { throw L2CAPSocketError.sentLessBytes(actualByteCount) }
@@ -332,7 +338,7 @@ public final class L2CAPSocket: L2CAPSocketProtocol {
332338
var optionLength = socklen_t(MemoryLayout<Options>.size)
333339

334340
guard getsockopt(internalSocket, SOL_L2CAP, L2CAP_OPTIONS, &optionValue, &optionLength) == 0
335-
else { throw POSIXError.errorno }
341+
else { throw POSIXError.fromErrno! }
336342

337343
return optionValue
338344
}

Sources/BluetoothLinux/POSIXError.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ import Foundation
1111
internal extension POSIXError {
1212

1313
/// Creates error from C ```errno```.
14-
static var errorno: POSIXError {
14+
static var fromErrno: POSIXError? {
1515

1616
guard let code = POSIXError.Code(rawValue: POSIXError.Code.RawValue(errno))
17-
else { return POSIXError(.EIO) } // default error
17+
else { return nil }
1818

1919
return self.init(code)
2020
}

Sources/BluetoothLinux/Scan.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public extension HostController {
5959
defer { nameBuffer.deallocateCapacity(maxNameLength) }
6060

6161
guard hci_read_remote_name(internalSocket, &address, CInt(maxNameLength), nameBuffer, CInt(timeout)) == CInt(0)
62-
else { throw POSIXError.errorno }
62+
else { throw POSIXError.fromErrno! }
6363

6464
let name = String.fromCString(nameBuffer)
6565

@@ -113,7 +113,7 @@ internal func HCIInquiry(_ deviceIdentifier: UInt16,
113113

114114
let deviceDescriptor = socket(AF_BLUETOOTH, SOCK_RAW | SOCK_CLOEXEC, BluetoothProtocol.hci.rawValue)
115115

116-
guard deviceDescriptor >= 0 else { throw POSIXError.errorno }
116+
guard deviceDescriptor >= 0 else { throw POSIXError.fromErrno! }
117117

118118
defer { close(deviceDescriptor) }
119119

@@ -136,7 +136,7 @@ internal func HCIInquiry(_ deviceIdentifier: UInt16,
136136
}
137137

138138
guard IOControl(deviceDescriptor, HCI.IOCTL.Inquiry, UnsafeMutableRawPointer(buffer) ) >= 0
139-
else { throw POSIXError.errorno }
139+
else { throw POSIXError.fromErrno! }
140140

141141
let resultCount = buffer.withMemoryRebound(to: HCIInquiryRequest.self, capacity: 1) { (inquiryRequest) in
142142
Int(inquiryRequest.pointee.responseCount)

0 commit comments

Comments
 (0)