Skip to content

Commit

Permalink
fixed HCI commands
Browse files Browse the repository at this point in the history
  • Loading branch information
colemancda committed Apr 30, 2016
1 parent fea45d3 commit 290800c
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 25 deletions.
28 changes: 6 additions & 22 deletions Sources/BluetoothLinux/DeviceCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,33 +41,17 @@ internal func HCISendCommand(_ deviceDescriptor: CInt, opcode: (commandField: UI
let packetType = HCIPacketType.Command.rawValue

var header = HCICommandHeader()
header.opcode = HCICommandOpcodePack(opcode.commandField, opcode.groupField).littleEndian
header.parameterLength = UInt8(parameterData.count)

/// data sent to host controller interface...

// build iovec
var ioVectors = [iovec](repeating: iovec(), count: 2)
header.opcode = HCICommandOpcodePack(opcode.commandField, opcode.groupField).littleEndian

ioVectors[0] = iovec(byteValue: [packetType])
ioVectors[1] = iovec(byteValue: header.byteValue)
header.parameterLength = UInt8(parameterData.count)

defer { ioVectors[0].iov_base.deallocateCapacity(ioVectors[0].iov_len) }
defer { ioVectors[1].iov_base.deallocateCapacity(ioVectors[1].iov_len) }

if parameterData.isEmpty == false {

ioVectors.append(iovec(byteValue: parameterData))

defer { ioVectors[2].iov_base.deallocateCapacity(ioVectors[2].iov_len) }
}
/// data sent to host controller interface
var data = [packetType] + header.byteValue + parameterData

// write to device descriptor socket
while writev(deviceDescriptor, &ioVectors, CInt(ioVectors.count)) < 0 {

guard (errno == EAGAIN || errno == EINTR)
else { throw POSIXError.fromErrorNumber! }
}
guard write(deviceDescriptor, &data, data.count) >= 0 // should we check if all data was written?
else { throw POSIXError.fromErrorNumber! }
}

@inline(__always)
Expand Down
4 changes: 2 additions & 2 deletions Sources/BluetoothLinux/DeviceRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public extension Adapter {

let opcode = (CP.command.rawValue, CP.command.dynamicType.opcodeGroupField.rawValue)

let data = try HCISendRequest(internalSocket, opcode: opcode, eventParameterLength: 1, timeout: timeout)
let data = try HCISendRequest(internalSocket, opcode: opcode, commandParameterData: commandParameter.byteValue, eventParameterLength: 1, timeout: timeout)

guard let statusByte = data.first
else { fatalError("Missing status byte!") }
Expand Down Expand Up @@ -143,7 +143,7 @@ internal func HCISendRequest(_ deviceDescriptor: CInt, opcode: (commandField: UI
// get old filter
guard getsockopt(deviceDescriptor, SOL_HCI, HCISocketOption.Filter.rawValue, oldFilterPointer, &filterLength) == 0
else { throw POSIXError.fromErrorNumber! }

// configure new filter
newFilter.clear()
newFilter.typeMask = 16
Expand Down
2 changes: 1 addition & 1 deletion Sources/BluetoothLinux/iBeacon.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ private func SetBeaconData(UUID: SwiftFoundation.UUID, mayor: UInt16, minor: UIn
parameter.data.5 = 0x4c // Apple, Inc
parameter.data.6 = 0x00 // Apple, Inc
parameter.data.7 = 0x02 // iBeacon
parameter.data.8 = 0x15 // length: 16 byte UUID, 2 bytes major & minor, 1 byte RSSI
parameter.data.8 = 0x15 // length: 21 = 16 byte UUID + 2 bytes major + 2 bytes minor + 1 byte RSSI

// set UUID bytes

Expand Down

0 comments on commit 290800c

Please sign in to comment.