diff --git a/Sources/BluetoothLinux/DeviceCommand.swift b/Sources/BluetoothLinux/DeviceCommand.swift index 1c61aa5..809f4c2 100644 --- a/Sources/BluetoothLinux/DeviceCommand.swift +++ b/Sources/BluetoothLinux/DeviceCommand.swift @@ -12,15 +12,15 @@ import Darwin.C #endif +import SwiftFoundation + public extension Adapter { - @inline(__always) func deviceCommand(command: T) throws { try HCISendCommand(internalSocket, opcode: (command.rawValue, T.opcodeGroupField.rawValue)) } - @inline(__always) func deviceCommand(commandParameter: T) throws { let command = T.command diff --git a/Sources/BluetoothLinux/HCI.swift b/Sources/BluetoothLinux/HCI.swift index aa7aa1e..4bac6aa 100644 --- a/Sources/BluetoothLinux/HCI.swift +++ b/Sources/BluetoothLinux/HCI.swift @@ -6,6 +6,12 @@ // Copyright © 2016 PureSwift. All rights reserved. // +#if os(Linux) + import Glibc +#elseif os(OSX) || os(iOS) + import Darwin.C +#endif + import SwiftFoundation /// Bluetooth HCI @@ -349,7 +355,7 @@ internal struct HCIFilter { internal struct Bits { - static let Type = CInt(31) + static let FilterType = CInt(31) static let Event = CInt(63) static let OpcodeGroupField = CInt(63) static let OpcodeCommandField = CInt(127) @@ -376,7 +382,7 @@ internal struct HCIFilter { @inline(__always) mutating func setPacketType(type: HCIPacketType) { - let bit = type == .Event ? 0 : CInt(type.rawValue) & HCIFilter.Bits.Type + let bit = type == .Event ? 0 : CInt(type.rawValue) & HCIFilter.Bits.FilterType HCISetBit(bit, &typeMask) } @@ -393,7 +399,7 @@ internal struct HCIFilter { // HCI Bit functions @inline(__always) -internal func HCISetBit(bit: CInt, inout _ destination: UInt32) { +internal func HCISetBit(bit: CInt, _ destination: inout UInt32) { let unsignedBit = UInt32(bit) diff --git a/Sources/BluetoothLinux/LowEnergyCommandParameter.swift b/Sources/BluetoothLinux/LowEnergyCommandParameter.swift index eeb058d..931ed9f 100644 --- a/Sources/BluetoothLinux/LowEnergyCommandParameter.swift +++ b/Sources/BluetoothLinux/LowEnergyCommandParameter.swift @@ -6,270 +6,13 @@ // Copyright © 2016 PureSwift. All rights reserved. // -/* + #if os(Linux) import Glibc #elseif os(OSX) || os(iOS) import Darwin.C #endif -import typealias SwiftFoundation.Byte - -#if os(OSX) - public struct le_set_event_mask_cp { - public var mask: (UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8) - public init() { stub() } - } -#endif - -extension le_set_event_mask_cp: HCICommandParameter { - public static var dataLength: CInt { return 8 } -} - -#if os(OSX) - public struct le_read_buffer_size_rp { - public var status: UInt8 - public var pkt_len: UInt16 - public var max_pkt: UInt8 - public init() { stub() } - } -#endif - -extension le_read_buffer_size_rp: HCICommandParameter { - public static var dataLength: CInt { return 4 } -} - -#if os(OSX) - public struct le_read_local_supported_features_rp { - public var status: UInt8 - public var features: (UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8) - public init() { stub() } - } -#endif - -extension le_read_local_supported_features_rp: HCICommandParameter { - public static var dataLength: CInt { return 9 } -} - -#if os(OSX) - public struct le_set_random_address_cp { - public var bdaddr: Address - public init() { stub() } - } -#endif - -extension le_set_random_address_cp: HCICommandParameter { - public static var dataLength: CInt { return 6 } -} - -#if os(OSX) - public struct le_set_advertising_parameters_cp { - public var min_interval: UInt16 - public var max_interval: UInt16 - public var advtype: UInt8 - public var own_bdaddr_type: UInt8 - public var direct_bdaddr_type: UInt8 - public var direct_bdaddr: Address - public var chan_map: UInt8 - public var filter: UInt8 - public init() { stub() } - } -#endif - -extension le_set_advertising_parameters_cp: HCICommandParameter { - public static var dataLength: CInt { return 15 } -} - -#if os(OSX) - public struct le_read_advertising_channel_tx_power_rp { - public var status: UInt8 - public var level: Int8 - public init() { stub() } - } -#endif - -extension le_read_advertising_channel_tx_power_rp: HCICommandParameter { - public static var dataLength: CInt { return 2 } -} - -#if os(OSX) - public struct le_set_advertising_data_cp { - public var length: UInt8 - public var data: LowEnergyAdvertisingData - public init() { stub() } - } -#endif - -extension le_set_advertising_data_cp: HCICommandParameter { - public static var dataLength: CInt { return 32 } -} - -#if os(OSX) - public struct le_set_scan_response_data_cp { - public var length: UInt8 - public var data: LowEnergyAdvertisingData - public init() { stub() } - } -#endif - -extension le_set_scan_response_data_cp: HCICommandParameter { - public static var dataLength: CInt { return 32 } -} - -#if os(OSX) - public struct le_set_advertise_enable_cp { - public var enable: UInt8 - public init() { stub() } - } -#endif - -extension le_set_advertise_enable_cp: HCICommandParameter { - public static var dataLength: CInt { return 1 } -} - -#if os(OSX) - public struct le_set_scan_parameters_cp { - public var type: UInt8 - public var interval: UInt16 - public var window: UInt16 - public var own_bdaddr_type: UInt8 - public var filter: UInt8 - public init() { stub() } - } -#endif - -extension le_set_scan_parameters_cp: HCICommandParameter { - public static var dataLength: CInt { return 7 } -} - -#if os(OSX) - public struct le_set_scan_enable_cp { - public var enable: UInt8 - public var filter_dup: UInt8 - public init() { stub() } - } -#endif - -extension le_set_scan_enable_cp: HCICommandParameter { - public static var dataLength: CInt { return 2 } -} - -#if os(OSX) - public struct le_create_connection_cp { - public var interval: UInt16 - public var window: UInt16 - public var initiator_filter: UInt8 - public var peer_bdaddr_type: UInt8 - public var peer_bdaddr: bdaddr_t - public var own_bdaddr_type: UInt8 - public var min_interval: UInt16 - public var max_interval: UInt16 - public var latency: UInt16 - public var supervision_timeout: UInt16 - public var min_ce_length: UInt16 - public var max_ce_length: UInt16 - public init() { stub() } - } -#endif - -extension le_create_connection_cp: HCICommandParameter { - public static var dataLength: CInt { return 25 } -} - -#if os(OSX) - public struct le_read_white_list_size_rp { - public var bdaddr_type: UInt8 - public var bdaddr: bdaddr_t - public init() { stub() } - } -#endif - -extension le_read_white_list_size_rp: HCICommandParameter { - public static var dataLength: CInt { return 7 } -} - -#if os(OSX) - public struct le_set_host_channel_classification_cp { - public var map: (UInt8, UInt8, UInt8, UInt8, UInt8) - public init() { stub() } - } -#endif - -extension le_set_host_channel_classification_cp: HCICommandParameter { - public static var dataLength: CInt { return 5 } -} - -#if os(OSX) - public struct le_read_channel_map_cp { - public var handle: UInt16 - public init() { stub() } - } -#endif - -extension le_read_channel_map_cp: HCICommandParameter { - public static var dataLength: CInt { return 2 } -} - -#if os(OSX) - public struct le_read_channel_map_rp { - public var status: UInt8 - public var handle: UInt16 - public var map: (UInt8, UInt8, UInt8, UInt8, UInt8) - public init() { stub() } - } -#endif - -extension le_read_channel_map_rp: HCICommandParameter { - public static var dataLength: CInt { return 8 } -} - -#if os(OSX) - public struct le_read_remote_used_features_cp { - public var handle: UInt16 - public init() { stub() } - } -#endif - -extension le_read_remote_used_features_cp: HCICommandParameter { - public static var dataLength: CInt { return 2 } -} - -#if os(OSX) - public struct le_encrypt_cp { - public var key: (UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8) - public var plaintext: (UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8) - public init() { stub() } - } -#endif - -extension le_encrypt_cp: HCICommandParameter { - public static var dataLength: CInt { return 32 } -} - -#if os(OSX) - public struct le_encrypt_rp { - public var status: UInt8 - public var data: (UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8) - public init() { stub() } - } -#endif - -extension le_encrypt_rp: HCICommandParameter { - public static var dataLength: CInt { return 17 } -} - -#if os(OSX) - public struct le_rand_rp { - public var status: UInt8 - public var random: UInt64 - public init() { stub() } - } -#endif - -extension le_rand_rp: HCICommandParameter { - public static var dataLength: CInt { return 9 } -} -*/ diff --git a/Sources/BluetoothLinux/LowEnergyEventParameter.swift b/Sources/BluetoothLinux/LowEnergyEventParameter.swift index 14f929b..2f8e538 100644 --- a/Sources/BluetoothLinux/LowEnergyEventParameter.swift +++ b/Sources/BluetoothLinux/LowEnergyEventParameter.swift @@ -27,7 +27,7 @@ public extension LowEnergyEvent { public init?(byteValue: [UInt8]) { - + fatalError("Not implemented") } } } \ No newline at end of file diff --git a/Sources/BluetoothLinux/Scan.swift b/Sources/BluetoothLinux/Scan.swift index 4a0e7ff..6c37171 100644 --- a/Sources/BluetoothLinux/Scan.swift +++ b/Sources/BluetoothLinux/Scan.swift @@ -8,6 +8,7 @@ #if os(Linux) import Glibc + import CSwiftBluetoothLinux #elseif os(OSX) || os(iOS) import Darwin.C #endif diff --git a/Sources/BluetoothLinux/iBeacon.swift b/Sources/BluetoothLinux/iBeacon.swift index ea1b228..4f28c17 100644 --- a/Sources/BluetoothLinux/iBeacon.swift +++ b/Sources/BluetoothLinux/iBeacon.swift @@ -16,98 +16,5 @@ import SwiftFoundation /// 31 Byte String public typealias LowEnergyAdvertisingData = (Byte, Byte, Byte, Byte, Byte, Byte, Byte, Byte, Byte, Byte, Byte, Byte, Byte, Byte, Byte, Byte, Byte, Byte, Byte, Byte, Byte, Byte, Byte, Byte, Byte, Byte, Byte, Byte, Byte, Byte, Byte) -/* -public extension Adapter { - - /// Enable iBeacon functionality. - func enableBeacon(UUID: SwiftFoundation.UUID = UUID(), mayor: UInt16, minor: UInt16, RSSI: Byte, interval: Int = 100, commandTimeout: Int = 1000) throws { - - assert(interval <= Int(UInt16.max), "Interval can only be 2 bytes long") - - // set advertising parameters - var advertisingParameters = le_set_advertising_parameters_cp() - memset(&advertisingParameters, 0, sizeof(le_set_advertising_parameters_cp)) - - advertisingParameters.max_interval = UInt16(interval).littleEndian - advertisingParameters.min_interval = UInt16(interval).littleEndian - //advertisingParameters.advtype = 3 // advertising non-connectable - advertisingParameters.chan_map = 7 // // all three advertising channels - - try self.deviceRequest(LowEnergyCommand.SetAdvertisingParameters, parameter: advertisingParameters, timeout: commandTimeout) - - // start advertising - var enableAdvertise = le_set_advertise_enable_cp() - memset(&enableAdvertise, 0, sizeof(le_set_advertise_enable_cp.self)) - - enableAdvertise.enable = 0x01 // true - - try self.deviceRequest(LowEnergyCommand.SetAdvertiseEnable, parameter: enableAdvertise, timeout: commandTimeout) - - // set iBeacon data - var advertisingDataCommand = le_set_advertising_data_cp() - memset(&advertisingDataCommand, 0, sizeof(le_set_advertising_data_cp)) - - let beaconData = GenerateBeaconData(UUID, mayor: mayor, minor: minor, RSSI: RSSI) - advertisingDataCommand.length = beaconData.length - advertisingDataCommand.data = beaconData.data - - try self.deviceRequest(LowEnergyCommand.SetAdvertisingData, parameter: advertisingDataCommand, timeout: commandTimeout) - } - - func setAdvertising(enabled: Bool) throws { - - // stop advertising - var enableAdvertise = le_set_advertise_enable_cp() - enableAdvertise.enable = 0x00 // false - - try self.deviceRequest(LowEnergyCommand.SetAdvertiseEnable, parameter: enableAdvertise) - } -} -// MARK: - Private - -private func GenerateBeaconData(UUID: SwiftFoundation.UUID, mayor: UInt16, minor: UInt16, RSSI: Byte) -> (data: LowEnergyAdvertisingData, length: Byte) { - - let length = 30 - - var data: [UInt8] = [UInt8](count: length, repeatedValue: 0) - - data[0] = 0x02 // length of flags - data[1] = 0x01 // flags type - data[2] = 0x1a // Flags: 000011010 - data[3] = 0x1a // length - data[4] = 0xff // vendor specific - data[5] = 0x4c // Apple, Inc - data[6] = 0x00 // Apple, Inc - data[7] = 0x02 // iBeacon - data[8] = 0x15 // length: 16 byte UUID, 2 bytes major&minor, 1 byte RSSI - - var uuidBytes = UUID.byteValue - - memcpy(&data + 9, &uuidBytes, 16) // UUID - - let littleMayor = mayor.littleEndian - - data[25] = Byte(truncatingBitPattern: UInt16(littleMayor >> 8) & UInt16(0x00ff)) - data[26] = Byte(truncatingBitPattern: UInt16(littleMayor & 0x00ff)) - - let littleMinor = minor.littleEndian - - data[25] = Byte(truncatingBitPattern: UInt16(littleMinor >> 8) & UInt16(0x00ff)) - data[26] = Byte(truncatingBitPattern: UInt16(littleMinor & 0x00ff)) - - data[29] = RSSI - - let byteTuple: LowEnergyAdvertisingData = withUnsafePointer(&data) { (pointer) in - - let convertedPointer = UnsafePointer(pointer) - - return convertedPointer.memory - } - - return (byteTuple, Byte(length)) -} - - -*/ diff --git a/Sources/iBeaconTest/main.swift b/Sources/iBeaconTest/main.swift index 65ab565..5880b4b 100644 --- a/Sources/iBeaconTest/main.swift +++ b/Sources/iBeaconTest/main.swift @@ -31,5 +31,5 @@ print("Found Bluetooth adapter with device ID: \(adapter.identifier)") print("Address: \(adapter.address!)") /// Perform Test -iBeaconTest(adapter, timeout: 10) +//iBeaconTest(adapter, timeout: 10)