From b1a2de541771c6b45d46cfa6613c10f5e52ece68 Mon Sep 17 00:00:00 2001 From: Christian Kandeler Date: Wed, 3 Feb 2016 17:52:55 +0100 Subject: Bluetooth LE: Add support for Signed Write command in the central role. Task-number: QTBUG-41175 Change-Id: I62d74236faf9161681306d952e409e23e0cea24d Reviewed-by: Alex Blasche --- .../server/qlowenergycontroller-gattserver.cpp | 8 +++- .../test/tst_qlowenergycontroller-gattserver.cpp | 50 +++++++++++++++++++--- 2 files changed, 52 insertions(+), 6 deletions(-) (limited to 'tests') diff --git a/tests/auto/qlowenergycontroller-gattserver/server/qlowenergycontroller-gattserver.cpp b/tests/auto/qlowenergycontroller-gattserver/server/qlowenergycontroller-gattserver.cpp index b7c95816..6fa9b023 100644 --- a/tests/auto/qlowenergycontroller-gattserver/server/qlowenergycontroller-gattserver.cpp +++ b/tests/auto/qlowenergycontroller-gattserver/server/qlowenergycontroller-gattserver.cpp @@ -121,7 +121,7 @@ void addCustomService() serviceData.addCharacteristic(charData); charData.setUuid(QBluetoothUuid(quint16(0x5001))); - charData.setProperties(QLowEnergyCharacteristic::Read | QLowEnergyCharacteristic::WriteSigned); + charData.setProperties(QLowEnergyCharacteristic::Read); charData.setReadConstraints(QBluetooth::AttAuthorizationRequired); // To test read failure. serviceData.addCharacteristic(charData); charData.setValue("something"); @@ -138,6 +138,12 @@ void addCustomService() charData.setProperties(QLowEnergyCharacteristic::Read | QLowEnergyCharacteristic::Notify); serviceData.addCharacteristic(charData); + charData.setUuid(QBluetoothUuid(quint16(0x5004))); + charData.setProperties(QLowEnergyCharacteristic::Read | QLowEnergyCharacteristic::WriteSigned); + charData.setDescriptors(QList()); + charData.setValue("initial"); + serviceData.addCharacteristic(charData); + addService(serviceData); } diff --git a/tests/auto/qlowenergycontroller-gattserver/test/tst_qlowenergycontroller-gattserver.cpp b/tests/auto/qlowenergycontroller-gattserver/test/tst_qlowenergycontroller-gattserver.cpp index f487db00..ac2060e9 100644 --- a/tests/auto/qlowenergycontroller-gattserver/test/tst_qlowenergycontroller-gattserver.cpp +++ b/tests/auto/qlowenergycontroller-gattserver/test/tst_qlowenergycontroller-gattserver.cpp @@ -43,7 +43,7 @@ #include #ifdef Q_OS_LINUX -#include +#include #endif #include @@ -167,7 +167,7 @@ void TestQLowEnergyControllerGattServer::cmacVerifier() }; QFETCH(QByteArray, message); QFETCH(quint64, expectedMac); - const bool success = LeCmacVerifier().verify(message, csrk, expectedMac); + const bool success = LeCmacCalculator().verify(message, csrk, expectedMac); QVERIFY(success); #else // CONFIG_LINUX_CRYPTO_API QSKIP("CMAC verification test only applicable for developer builds on Linux " @@ -333,7 +333,7 @@ void TestQLowEnergyControllerGattServer::serverCommunication() QVERIFY(spy->wait(5000)); } QCOMPARE(customService->includedServices().count(), 0); - QCOMPARE(customService->characteristics().count(), 4); + QCOMPARE(customService->characteristics().count(), 5); QLowEnergyCharacteristic customChar = customService->characteristic(QBluetoothUuid(quint16(0x5000))); QVERIFY(customChar.isValid()); @@ -366,12 +366,43 @@ void TestQLowEnergyControllerGattServer::serverCommunication() = customChar4.descriptor(QBluetoothUuid::ClientCharacteristicConfiguration); QVERIFY(cc4ClientConfig.isValid()); + QLowEnergyCharacteristic customChar5 + = customService->characteristic(QBluetoothUuid(quint16(0x5004))); + QVERIFY(customChar5.isValid()); + QCOMPARE(customChar5.properties(), + QLowEnergyCharacteristic::Read | QLowEnergyCharacteristic::WriteSigned); + QCOMPARE(customChar5.descriptors().count(), 0); + QCOMPARE(customChar5.value(), QByteArray("initial")); + customService->writeCharacteristic(customChar, "whatever"); spy.reset(new QSignalSpy(customService.data(), static_cast(&QLowEnergyService::error))); QVERIFY(spy->wait(3000)); QCOMPARE(customService->error(), QLowEnergyService::CharacteristicWriteError); + const bool isBonded = QBluetoothLocalDevice().pairingStatus(m_serverAddress) + != QBluetoothLocalDevice::Unpaired; + + customService->writeCharacteristic(customChar5, "1", QLowEnergyService::WriteSigned); + if (isBonded) { + // Signed write is done twice to test the sign counter stuff. + // Note: We assume here that the link is not encrypted, as this information is not exported. + customService->readCharacteristic(customChar5); + spy.reset(new QSignalSpy(customService.data(), &QLowEnergyService::characteristicRead)); + QVERIFY(spy->wait(3000)); + QCOMPARE(customChar5.value(), QByteArray("1")); + customService->writeCharacteristic(customChar5, "2", QLowEnergyService::WriteSigned); + customService->readCharacteristic(customChar5); + spy.reset(new QSignalSpy(customService.data(), &QLowEnergyService::characteristicRead)); + QVERIFY(spy->wait(3000)); + QCOMPARE(customChar5.value(), QByteArray("2")); + } else { + spy.reset(new QSignalSpy(customService.data(), static_cast(&QLowEnergyService::error))); + QVERIFY(spy->wait(3000)); + QCOMPARE(customService->error(), QLowEnergyService::CharacteristicWriteError); + } + QByteArray indicateValue(2, 0); qToLittleEndian(2, reinterpret_cast(indicateValue.data())); customService->writeDescriptor(cc3ClientConfig, indicateValue); @@ -396,8 +427,6 @@ void TestQLowEnergyControllerGattServer::serverCommunication() spy.reset(new QSignalSpy(m_leController.data(), &QLowEnergyController::connectionUpdated)); QVERIFY(spy->wait(5000)); - const bool isBonded = QBluetoothLocalDevice().pairingStatus(m_serverAddress) - != QBluetoothLocalDevice::Unpaired; m_leController->disconnectFromDevice(); if (m_leController->state() != QLowEnergyController::UnconnectedState) { @@ -438,6 +467,17 @@ void TestQLowEnergyControllerGattServer::serverCommunication() if (isBonded) { QCOMPARE(cc3ClientConfig.value(), indicateValue); QCOMPARE(cc4ClientConfig.value(), notifyValue); + + // Do a third signed write to test sign counter persistence. + customChar5 = customService->characteristic(QBluetoothUuid(quint16(0x5004))); + QVERIFY(customChar5.isValid()); + QCOMPARE(customChar5.value(), QByteArray("2")); + customService->writeCharacteristic(customChar5, "3", QLowEnergyService::WriteSigned); + customService->readCharacteristic(customChar5); + spy.reset(new QSignalSpy(customService.data(), &QLowEnergyService::characteristicRead)); + QVERIFY(spy->wait(3000)); + QCOMPARE(customChar5.value(), QByteArray("3")); + } else { QCOMPARE(cc3ClientConfig.value(), QByteArray(2, 0)); QCOMPARE(cc4ClientConfig.value(), QByteArray(2, 0)); -- cgit v1.2.3