Skip to content

Commit 131e160

Browse files
committed
Updated BluetoothProtocol
1 parent 2dd9e72 commit 131e160

File tree

7 files changed

+66
-24
lines changed

7 files changed

+66
-24
lines changed

Sources/BluetoothLinux/BluetoothProtocol.swift

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,54 @@
66
// Copyright © 2016 PureSwift. All rights reserved.
77
//
88

9-
/// BTPROTO_*
10-
public enum BluetoothProtocol: CInt {
9+
//import System
10+
11+
/// Bluetooth Socket Protocol
12+
public enum BluetoothSocketProtocol: Int32, Codable {
1113

14+
/// Bluetooth L2CAP (Logical link control and adaptation protocol)
1215
case l2cap = 0
16+
17+
/// Bluetooth HCI protocol (Host Controller Interface)
1318
case hci = 1
19+
20+
/// Bluetooth SCO protocol (Synchronous Connection Oriented Link)
1421
case sco = 2
22+
23+
/// Bluetooth RFCOMM protocol (Radio frequency communication)
1524
case rfcomm = 3
25+
26+
/// Bluetooth BNEP (network encapsulation protocol)
1627
case bnep = 4
28+
29+
/// CAPI Message Transport Protocol
1730
case cmtp = 5
31+
32+
/// HIDP (Human Interface Device Protocol) is a transport layer for HID reports.
1833
case hidp = 6
34+
35+
/// Audio/video data transport protocol
1936
case avdtp = 7
2037
}
38+
39+
/*
40+
extension BluetoothSocketProtocol: SocketProtocol {
41+
42+
@_alwaysEmitIntoClient
43+
public static var family: SocketAddressFamily { .bluetooth }
44+
45+
@_alwaysEmitIntoClient
46+
public var type: SocketType {
47+
switch self {
48+
case .l2cap: return .sequencedPacket
49+
case .hci: return .raw
50+
case .sco: return .sequencedPacket
51+
case .rfcomm: return .stream
52+
case .bnep: return .raw
53+
case .cmtp: return .raw
54+
case .hidp: return .raw
55+
case .avdtp: return .raw
56+
}
57+
}
58+
}
59+
*/

Sources/BluetoothLinux/Darwin.swift

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66
// Copyright © 2016 PureSwift. All rights reserved.
77
//
88

9-
#if os(macOS) || os(iOS)
10-
11-
internal func stub() -> Never {
12-
13-
fatalError("Method not implemented. This code only runs on Linux.")
14-
}
15-
9+
#if canImport(Darwin)
10+
internal func stub() -> Never {
11+
fatalError("Method not implemented. This code only runs on Linux.")
12+
}
13+
#endif
14+
15+
#if !os(Linux)
16+
#warning("This module will only run on Linux")
1617
#endif

Sources/BluetoothLinux/DevicePollEvent.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ internal func HCIPollEvent(_ deviceDescriptor: CInt,
5252
newFilter.setEvent(event)
5353

5454
// set new filter
55-
var newFilterLength = socklen_t(MemoryLayout<HCIFilter>.size)
55+
let newFilterLength = socklen_t(MemoryLayout<HCIFilter>.size)
5656
guard withUnsafeMutablePointer(to: &newFilter, {
5757
let pointer = UnsafeMutableRawPointer($0)
5858
return setsockopt(deviceDescriptor, SOL_HCI, HCISocketOption.filter.rawValue, pointer, newFilterLength) == 0

Sources/BluetoothLinux/HostController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ internal extension HostController {
107107
}
108108

109109
init() throws {
110-
let fileDescriptor = socket(AF_BLUETOOTH, SOCK_RAW | SOCK_CLOEXEC, BluetoothProtocol.hci.rawValue)
110+
let fileDescriptor = socket(AF_BLUETOOTH, SOCK_RAW | SOCK_CLOEXEC, BluetoothSocketProtocol.hci.rawValue)
111111
guard fileDescriptor >= 0 else { throw POSIXError.fromErrno() }
112112
self.fileDescriptor = fileDescriptor
113113
}

Sources/BluetoothLinux/L2CAP.swift

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,9 @@ public final class L2CAPSocket: L2CAPSocketProtocol {
117117
return optionValue
118118
}
119119

120-
//. socket domain and protocol
120+
// socket domain and protocol
121121
guard try value(for: SO_DOMAIN) == AF_BLUETOOTH,
122-
try value(for: SO_PROTOCOL) == BluetoothProtocol.l2cap.rawValue
122+
try value(for: SO_PROTOCOL) == BluetoothSocketProtocol.l2cap.rawValue
123123
else { return false }
124124

125125
return true
@@ -135,7 +135,7 @@ public final class L2CAPSocket: L2CAPSocketProtocol {
135135
// open socket
136136
let internalSocket = socket(AF_BLUETOOTH,
137137
SOCK_SEQPACKET,
138-
BluetoothProtocol.l2cap.rawValue)
138+
BluetoothSocketProtocol.l2cap.rawValue)
139139

140140
// error creating socket
141141
guard internalSocket >= 0
@@ -164,18 +164,15 @@ public final class L2CAPSocket: L2CAPSocketProtocol {
164164

165165
/// Bluetooth address
166166
public var address: BluetoothAddress {
167-
168167
return BluetoothAddress(littleEndian: internalAddress.l2_bdaddr)
169168
}
170169

171170
public var addressType: AddressType {
172-
173171
return AddressType(rawValue: internalAddress.l2_bdaddr_type)!
174172
}
175173

176174
/// Protocol/Service Multiplexer (PSM)
177175
public var protocolServiceMultiplexer: UInt16 {
178-
179176
return UInt16(littleEndian: internalAddress.l2_psm)
180177
}
181178

@@ -260,7 +257,7 @@ public final class L2CAPSocket: L2CAPSocketProtocol {
260257
// make socket non-blocking
261258
try setNonblocking()
262259
}
263-
260+
264261
/// Reads from the socket.
265262
public func recieve(_ bufferSize: Int = 1024) throws -> Data? {
266263

@@ -287,6 +284,16 @@ public final class L2CAPSocket: L2CAPSocketProtocol {
287284
return Data(actualBytes)
288285
}
289286

287+
/// Blocks until data is ready.
288+
public func waitForEvents(timeout: TimeInterval) {
289+
var pollData = pollfd(
290+
fd: internalSocket,
291+
events: Int16(POLLIN) & Int16(POLLOUT) & Int16(POLLPRI) & Int16(POLLERR) & Int16(POLLHUP) & Int16(POLLNVAL),
292+
revents: 0
293+
)
294+
poll(&pollData, 1, 0)
295+
}
296+
290297
private func canRead() throws -> Bool {
291298

292299
var readSockets = FileDescriptorSet()

Sources/BluetoothLinux/Scan.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ internal func HCIInquiry(_ deviceIdentifier: UInt16,
109109

110110
typealias InquiryResult = HostController.InquiryResult
111111

112-
let deviceDescriptor = socket(AF_BLUETOOTH, SOCK_RAW | SOCK_CLOEXEC, BluetoothProtocol.hci.rawValue)
112+
let deviceDescriptor = socket(AF_BLUETOOTH, SOCK_RAW | SOCK_CLOEXEC, BluetoothSocketProtocol.hci.rawValue)
113113

114114
guard deviceDescriptor >= 0 else { throw POSIXError.fromErrno() }
115115

Tests/BluetoothLinuxTests/BluetoothLinuxTests.swift

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,6 @@ final class BluetoothLinuxTests: XCTestCase {
4444
XCTAssertEqual(error._nsError.domain, NSPOSIXErrorDomain)
4545
XCTAssertEqual(error._nsError.code, Int(errorCode.rawValue))
4646

47-
#if Xcode
48-
print("Description:", error.description)
49-
print("Debug Information:", error.debugInformation ?? "")
50-
#endif
51-
5247
do { throw error } // deal with protocol and not concrete type
5348
catch {
5449
XCTAssert("\(error)".contains(string))

0 commit comments

Comments
 (0)