Skip to content

Commit

Permalink
Merge pull request #37 from PureSwift/feature/hciTests
Browse files Browse the repository at this point in the history
Added HCI Tests and methods
  • Loading branch information
colemancda authored May 11, 2018
2 parents 9e4f17e + abf3c43 commit 100388f
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 3 deletions.
4 changes: 3 additions & 1 deletion Sources/LowEnergyCommandParameter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -736,7 +736,7 @@ public extension LowEnergyCommand {
/// * the initiator filter policy uses the White List and a create connection command is outstanding.
///
/// Address is ignored when Address Type is set to 0xFF.
public struct RemoveDeviceToWhiteListParameter: HCICommandParameter { // HCI_LE_Remove_Device_From_White_List
public struct RemoveDeviceFromWhiteListParameter: HCICommandParameter { // HCI_LE_Remove_Device_From_White_List

public static let command = LowEnergyCommand.removeDeviceFromWhiteList //0x0012

Expand Down Expand Up @@ -3516,6 +3516,8 @@ public extension LowEnergyCommand {
}

/// LE Read Supported States
///
/// The LE_Read_Supported_States command reads the states and state combinations that the link layer supports.
public struct ReadSupportedStatesReturnParameter: HCICommandReturnParameter {

public static let command = LowEnergyCommand.readSupportedStates //0x001C
Expand Down
10 changes: 10 additions & 0 deletions Sources/LowEnergyFeaturesCommands.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ public extension BluetoothHostControllerInterface {
return returValue.features
}

/// LE Read Supported States
///
/// The LE_Read_Supported_States command reads the states and state combinations that the link layer supports.
func readSupportedStates(timeout: HCICommandTimeout = .default) throws -> LowEnergyStateSet {

let returValue = try deviceRequest(LowEnergyCommand.ReadSupportedStatesReturnParameter.self, timeout: timeout)

return returValue.state
}

/// LE Read Remote Features Command
///
/// The command requests, from the remote device identified by the connection handle,
Expand Down
2 changes: 1 addition & 1 deletion Sources/LowEnergyWhiteList.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@ public extension BluetoothHostControllerInterface {
/// Used to remove a single device from the White List stored in the Controller.
func lowEnergyRemoveDeviceFromWhiteList(_ whiteListDevice: LowEnergyWhiteListDevice, timeout: HCICommandTimeout = .default) throws {

try deviceRequest(LowEnergyCommand.RemoveDeviceToWhiteListParameter(device: whiteListDevice), timeout: timeout)
try deviceRequest(LowEnergyCommand.RemoveDeviceFromWhiteListParameter(device: whiteListDevice), timeout: timeout)
}
}
102 changes: 101 additions & 1 deletion Tests/BluetoothTests/HCITests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,109 @@ final class HCITests: XCTestCase {
("testLEStartEncryption", testLEStartEncryption),
("testEncryptionChangeEvent", testEncryptionChangeEvent),
("testLowEnergyEncrypt", testLowEnergyEncrypt),
("testSetLERandomAddress", testSetLERandomAddress)
("testSetLERandomAddress", testSetLERandomAddress),
("testReadLocalSupportedFeatures", testReadLocalSupportedFeatures),
("testReadBufferSize", testReadBufferSize),
("testSetAdvertiseEnableParameter", testSetAdvertiseEnableParameter)
]

func testSetAdvertiseEnableParameter(){

let hostController = TestHostController()

/**
SEND [2006] Opcode: 0x2006 (OGF: 0x08 OCF: 0x06) 06 20 0f 14 00 1e 00 01 01 00 77 d8 47 a3 39 54 01 00
Parameter Length: 15 (0x0F)
Advertising Interval Min: 0x0014 (12.5ms)
Advertising Interval Max: 0x001E (18.75ms)
Advertising Type: 0x01 - Connectable directed advertising (ADV_DIRECT_IND)
Own Address Type: Random
Direct Address Type: Public
Direct Address: (null)
Advertising Channel Map: 0x01
Advertising Filter Policy: 0x00 - Allow Scan Request from Any, Allow Connect Request from Any
*/
hostController.queue.append(
.command(LowEnergyCommand.setAdvertisingParameters.opcode,
[0x06, 0x20, 0x0f, 0x14, 0x00, 0x1E, 0x00, 0x01, 0x01, 0x00, 0x77, 0xD8, 0x47, 0xA3, 0x39, 0x54, 0x01, 0x00])
)

/// 0e 04 01 06 20 12
hostController.queue.append(.event([0x0E, 0x04, 0x01, 0x06, 0x20, 0x12]))
}

func testReadBufferSize(){
typealias ReadBufferSize = LowEnergyCommand.ReadBufferSizeReturnParameter

let hostController = TestHostController()

/**
SEND [1001] Read Buffer Size 02 20 00
[2002] Opcode: 0x2002 (OGF: 0x08 OCF: 0x02)
*/
hostController.queue.append(
.command(LowEnergyCommand.readBufferSize.opcode,
[0x02, 0x20, 0x00])
)

hostController.queue.append(.event([0x0E, 0x07, 0x01, 0x02, 0x20, 0x00, 0xFB, 0x00, 0x0F]))

/**
Command Complete [2002] - LE Read Buffer Size - Num LE Data Packets: 0x000F 0e 07 01 02 20 00 fb 00 0f
Parameter Length: 7 (0x07)
Status: 0x00 - Success
Num HCI Command Packets: 0x01
Opcode: 0x2002 (OGF: 0x08 OCF: 0x02) - [Low Energy] LE Read Buffer Size
HC LE Data Packet Length: 0x00FB
HC Total Num LE Data Packets: 0x000F
*/
var readBufferSizeReturn: ReadBufferSize!
XCTAssertNoThrow(readBufferSizeReturn = try hostController.readBufferSize())
XCTAssert(hostController.queue.isEmpty)

XCTAssertEqual(readBufferSizeReturn.dataPacketLength, 0x00FB)
XCTAssertEqual(readBufferSizeReturn.dataPacket, 0x000F)
}

func testReadLocalSupportedFeatures() {
typealias ReadLocalSupportedFeatures = LowEnergyCommand.ReadLocalSupportedFeaturesReturnParameter

let hostController = TestHostController()

/**
SEND [1001] Read Local Supported Features 03 20 00
[2003] Opcode: 0x2003 (OGF: 0x08 OCF: 0x03)
*/
hostController.queue.append(
.command(LowEnergyCommand.readLocalSupportedFeatures.opcode,
[0x03, 0x20, 0x00])
)

hostController.queue.append(.event([0x0E, 0x0C, 0x01, 0x03, 0x20, 0x00, 0x3F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]))

/**
Command Complete [2003] - LE Read Local Supported Features 0e 0c 01 03 20 00 3f 00 00 00 00 00 00 00
Parameter Length: 12 (0x0C)
Status: 0x00 - Success
Num HCI Command Packets: 0x01
Opcode: 0x2003 (OGF: 0x08 OCF: 0x03) - [Low Energy] LE Read Local Supported Features
LE Features: 0X000000000000003F
LE Encryption
Connection Parameters Request Procedure
Extended Reject Indication
Slave-initiated Features Exchange
LE Ping
LE Data Packet Length Extension
*/

var lowEnergyFeatureSet: LowEnergyFeatureSet!
XCTAssertNoThrow(lowEnergyFeatureSet = try hostController.readLocalSupportedFeatures())
XCTAssert(hostController.queue.isEmpty)

XCTAssertEqual(lowEnergyFeatureSet.rawValue, 0x000000000000003F)
}

func testName() {

/// HCI command
Expand Down

0 comments on commit 100388f

Please sign in to comment.