Skip to content

Commit

Permalink
Updated BluetoothProtocol
Browse files Browse the repository at this point in the history
  • Loading branch information
colemancda committed Oct 10, 2021
1 parent 2dd9e72 commit 131e160
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 24 deletions.
43 changes: 41 additions & 2 deletions Sources/BluetoothLinux/BluetoothProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,54 @@
// Copyright © 2016 PureSwift. All rights reserved.
//

/// BTPROTO_*
public enum BluetoothProtocol: CInt {
//import System

/// Bluetooth Socket Protocol
public enum BluetoothSocketProtocol: Int32, Codable {

/// Bluetooth L2CAP (Logical link control and adaptation protocol)
case l2cap = 0

/// Bluetooth HCI protocol (Host Controller Interface)
case hci = 1

/// Bluetooth SCO protocol (Synchronous Connection Oriented Link)
case sco = 2

/// Bluetooth RFCOMM protocol (Radio frequency communication)
case rfcomm = 3

/// Bluetooth BNEP (network encapsulation protocol)
case bnep = 4

/// CAPI Message Transport Protocol
case cmtp = 5

/// HIDP (Human Interface Device Protocol) is a transport layer for HID reports.
case hidp = 6

/// Audio/video data transport protocol
case avdtp = 7
}

/*
extension BluetoothSocketProtocol: SocketProtocol {
@_alwaysEmitIntoClient
public static var family: SocketAddressFamily { .bluetooth }
@_alwaysEmitIntoClient
public var type: SocketType {
switch self {
case .l2cap: return .sequencedPacket
case .hci: return .raw
case .sco: return .sequencedPacket
case .rfcomm: return .stream
case .bnep: return .raw
case .cmtp: return .raw
case .hidp: return .raw
case .avdtp: return .raw
}
}
}
*/
15 changes: 8 additions & 7 deletions Sources/BluetoothLinux/Darwin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
// Copyright © 2016 PureSwift. All rights reserved.
//

#if os(macOS) || os(iOS)

internal func stub() -> Never {

fatalError("Method not implemented. This code only runs on Linux.")
}

#if canImport(Darwin)
internal func stub() -> Never {
fatalError("Method not implemented. This code only runs on Linux.")
}
#endif

#if !os(Linux)
#warning("This module will only run on Linux")
#endif
2 changes: 1 addition & 1 deletion Sources/BluetoothLinux/DevicePollEvent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ internal func HCIPollEvent(_ deviceDescriptor: CInt,
newFilter.setEvent(event)

// set new filter
var newFilterLength = socklen_t(MemoryLayout<HCIFilter>.size)
let newFilterLength = socklen_t(MemoryLayout<HCIFilter>.size)
guard withUnsafeMutablePointer(to: &newFilter, {
let pointer = UnsafeMutableRawPointer($0)
return setsockopt(deviceDescriptor, SOL_HCI, HCISocketOption.filter.rawValue, pointer, newFilterLength) == 0
Expand Down
2 changes: 1 addition & 1 deletion Sources/BluetoothLinux/HostController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ internal extension HostController {
}

init() throws {
let fileDescriptor = socket(AF_BLUETOOTH, SOCK_RAW | SOCK_CLOEXEC, BluetoothProtocol.hci.rawValue)
let fileDescriptor = socket(AF_BLUETOOTH, SOCK_RAW | SOCK_CLOEXEC, BluetoothSocketProtocol.hci.rawValue)
guard fileDescriptor >= 0 else { throw POSIXError.fromErrno() }
self.fileDescriptor = fileDescriptor
}
Expand Down
21 changes: 14 additions & 7 deletions Sources/BluetoothLinux/L2CAP.swift
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,9 @@ public final class L2CAPSocket: L2CAPSocketProtocol {
return optionValue
}

//. socket domain and protocol
// socket domain and protocol
guard try value(for: SO_DOMAIN) == AF_BLUETOOTH,
try value(for: SO_PROTOCOL) == BluetoothProtocol.l2cap.rawValue
try value(for: SO_PROTOCOL) == BluetoothSocketProtocol.l2cap.rawValue
else { return false }

return true
Expand All @@ -135,7 +135,7 @@ public final class L2CAPSocket: L2CAPSocketProtocol {
// open socket
let internalSocket = socket(AF_BLUETOOTH,
SOCK_SEQPACKET,
BluetoothProtocol.l2cap.rawValue)
BluetoothSocketProtocol.l2cap.rawValue)

// error creating socket
guard internalSocket >= 0
Expand Down Expand Up @@ -164,18 +164,15 @@ public final class L2CAPSocket: L2CAPSocketProtocol {

/// Bluetooth address
public var address: BluetoothAddress {

return BluetoothAddress(littleEndian: internalAddress.l2_bdaddr)
}

public var addressType: AddressType {

return AddressType(rawValue: internalAddress.l2_bdaddr_type)!
}

/// Protocol/Service Multiplexer (PSM)
public var protocolServiceMultiplexer: UInt16 {

return UInt16(littleEndian: internalAddress.l2_psm)
}

Expand Down Expand Up @@ -260,7 +257,7 @@ public final class L2CAPSocket: L2CAPSocketProtocol {
// make socket non-blocking
try setNonblocking()
}

/// Reads from the socket.
public func recieve(_ bufferSize: Int = 1024) throws -> Data? {

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

/// Blocks until data is ready.
public func waitForEvents(timeout: TimeInterval) {
var pollData = pollfd(
fd: internalSocket,
events: Int16(POLLIN) & Int16(POLLOUT) & Int16(POLLPRI) & Int16(POLLERR) & Int16(POLLHUP) & Int16(POLLNVAL),
revents: 0
)
poll(&pollData, 1, 0)
}

private func canRead() throws -> Bool {

var readSockets = FileDescriptorSet()
Expand Down
2 changes: 1 addition & 1 deletion Sources/BluetoothLinux/Scan.swift
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ internal func HCIInquiry(_ deviceIdentifier: UInt16,

typealias InquiryResult = HostController.InquiryResult

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

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

Expand Down
5 changes: 0 additions & 5 deletions Tests/BluetoothLinuxTests/BluetoothLinuxTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,6 @@ final class BluetoothLinuxTests: XCTestCase {
XCTAssertEqual(error._nsError.domain, NSPOSIXErrorDomain)
XCTAssertEqual(error._nsError.code, Int(errorCode.rawValue))

#if Xcode
print("Description:", error.description)
print("Debug Information:", error.debugInformation ?? "")
#endif

do { throw error } // deal with protocol and not concrete type
catch {
XCTAssert("\(error)".contains(string))
Expand Down

0 comments on commit 131e160

Please sign in to comment.