diff --git a/Package.swift b/Package.swift index 247bf18..834f212 100644 --- a/Package.swift +++ b/Package.swift @@ -1,25 +1,20 @@ +// swift-tools-version:4.1 import PackageDescription -let package = Package( - name: "BluetoothLinux", - targets: [ - Target( - name: "BluetoothLinux", +_ = Package(name: "BluetoothLinux", + products: [ + .library( + name: "BluetoothLinux", + targets: ["BluetoothLinux"] + ) + ], dependencies: [ - .Target(name: "CSwiftBluetoothLinux") - ]), - Target( - name: "CSwiftBluetoothLinux"), - Target( - name: "CSwiftBluetoothLinuxTest"), - Target( - name: "BluetoothLinuxTests", - dependencies: [ - .Target(name: "BluetoothLinux"), - .Target(name: "CSwiftBluetoothLinuxTest") - ]) - ], - dependencies: [ - .Package(url: "https://github.com/PureSwift/Bluetooth.git", majorVersion: 2) - ] -) + .package(url: "https://github.com/PureSwift/Bluetooth.git", .branch("master")) + ], + targets: [ + .target(name: "BluetoothLinux", dependencies: ["Bluetooth", "CSwiftBluetoothLinux"]), + .target(name: "CSwiftBluetoothLinux"), + .target(name: "CSwiftBluetoothLinuxTest"), + .testTarget(name: "BluetoothLinuxTests", dependencies: ["BluetoothLinux", "CSwiftBluetoothLinuxTest"]) + ], + swiftLanguageVersions: [4]) diff --git a/Sources/BluetoothLinux/HostController.swift b/Sources/BluetoothLinux/HostController.swift index 00b5d6c..c1c0577 100644 --- a/Sources/BluetoothLinux/HostController.swift +++ b/Sources/BluetoothLinux/HostController.swift @@ -66,9 +66,9 @@ public extension HostController { return try HCIRequestDeviceList { (_, list) in - list.sorted(by: { $0.0.identifier < $0.1.identifier }) + list.sorted(by: { $0.identifier < $1.identifier }) .filter { HCITestBit(.up, $0.options) && $0.identifier >= 0 } - .flatMap { try? HostController(identifier: $0.identifier) } + .compactMap { try? HostController(identifier: $0.identifier) } } } diff --git a/Sources/BluetoothLinux/IOCTL.swift b/Sources/BluetoothLinux/IOCTL.swift index 4239a83..2b94a80 100644 --- a/Sources/BluetoothLinux/IOCTL.swift +++ b/Sources/BluetoothLinux/IOCTL.swift @@ -58,8 +58,12 @@ internal struct IOC { static func IOC(_ direction: CUnsignedInt, _ type: CInt, _ nr: CInt, _ size: CInt) -> CUnsignedLong { let dir = CInt(direction) - - return CUnsignedLong(bitPattern: CLong(((dir) << DIRSHIFT) | ((type) << TYPESHIFT) | ((nr) << NRSHIFT) | ((size) << SIZESHIFT))) + let dirValue = dir << DIRSHIFT + let typeValue = type << TYPESHIFT + let nrValue = nr << NRSHIFT + let sizeValue = size << SIZESHIFT + let value = CLong(dirValue | typeValue | nrValue | sizeValue) + return CUnsignedLong(bitPattern: value) } @inline(__always) diff --git a/Sources/BluetoothLinux/POSIXError.swift b/Sources/BluetoothLinux/POSIXError.swift new file mode 100755 index 0000000..f83e350 --- /dev/null +++ b/Sources/BluetoothLinux/POSIXError.swift @@ -0,0 +1,34 @@ +// +// POSIXError.swift +// SwiftFoundation +// +// Created by Alsey Coleman Miller on 7/22/15. +// Copyright © 2015 PureSwift. All rights reserved. +// + +import Foundation + +internal extension POSIXError { + + /// Creates error from C ```errno```. + static var fromErrno: POSIXError? { + + guard let code = POSIXError.Code(rawValue: POSIXError.Code.RawValue(errno)) + else { return nil } + + return self.init(code: code) + } + + /// Creates `POSIXError` from error code. + init(code: POSIXError.Code) { + + let nsError = NSError( + domain: NSPOSIXErrorDomain, + code: Int(code.rawValue), + userInfo: [ + NSLocalizedDescriptionKey: String(cString: strerror(CInt(code.rawValue)), encoding: .ascii)! + ]) + + self.init(_nsError: nsError) + } +} diff --git a/Sources/BluetoothLinux/Scan.swift b/Sources/BluetoothLinux/Scan.swift index 127c0e8..ce085de 100644 --- a/Sources/BluetoothLinux/Scan.swift +++ b/Sources/BluetoothLinux/Scan.swift @@ -123,7 +123,7 @@ internal func HCIInquiry(_ deviceIdentifier: UInt16, let buffer = UnsafeMutablePointer.allocate(capacity: bufferSize) - defer { buffer.deallocate(capacity: bufferSize) } + defer { buffer.deallocate() } let deviceClass = deviceClass ?? (0x33, 0x8b, 0x9e)