summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorIvan Solovev <ivan.solovev@qt.io>2021-09-13 17:01:15 +0200
committerQt Cherry-pick Bot <cherrypick_bot@qt-project.org>2021-09-17 08:06:07 +0000
commitcb2d2de00c3dca59645dbdb8fc4d4872e1b96c6d (patch)
tree5095c47d78a996c89f0c839211f3c6195958aa7b /src
parent80c16eaffcd45148f2dbd4615f0bee868a91eb2e (diff)
Cleanup QLowEnergyController WinRT
This patch introduces some minor cleanups: * Fix used error code in case the remote device address is invalid. * Introduce a helper function to handle connection errors. This allows to avoid code duplications. * Simplify CHECK_FOR_DEVICE_CONNECTION_ERROR_IMPL macro to use the new helper function. * Minor formatting and indentation cleanups. Change-Id: I3c679bef56f719c2e97dc9399c3bae33032f3861 Reviewed-by: Juha Vuolle <juha.vuolle@insta.fi> Reviewed-by: Alex Blasche <alexander.blasche@qt.io> (cherry picked from commit bcaae09e94373d45ac62d283b404f3b9c02cde30) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
Diffstat (limited to 'src')
-rw-r--r--src/bluetooth/qlowenergycontroller_winrt.cpp59
-rw-r--r--src/bluetooth/qlowenergycontroller_winrt_p.h1
2 files changed, 23 insertions, 37 deletions
diff --git a/src/bluetooth/qlowenergycontroller_winrt.cpp b/src/bluetooth/qlowenergycontroller_winrt.cpp
index b7038210..8ad71785 100644
--- a/src/bluetooth/qlowenergycontroller_winrt.cpp
+++ b/src/bluetooth/qlowenergycontroller_winrt.cpp
@@ -93,10 +93,7 @@ typedef IGattReadClientCharacteristicConfigurationDescriptorResult IClientCharCo
#define CHECK_FOR_DEVICE_CONNECTION_ERROR_IMPL(this, hr, msg, ret) \
if (FAILED(hr)) { \
- qCWarning(QT_BT_WINDOWS) << msg; \
- this->unregisterFromStatusChanges(); \
- this->setError(QLowEnergyController::ConnectionError); \
- this->setState(QLowEnergyController::UnconnectedState); \
+ this->handleConnectionError(msg); \
ret; \
}
@@ -483,7 +480,7 @@ void QLowEnergyControllerPrivateWinRT::connectToDevice()
QWinRTFunctions::ProcessMainThreadEvents, 5000);
if (FAILED(hr) || !mDevice) {
qCWarning(QT_BT_WINDOWS) << "Could not find LE device";
- setError(QLowEnergyController::InvalidBluetoothAdapterError);
+ setError(QLowEnergyController::UnknownRemoteDeviceError);
setState(QLowEnergyController::UnconnectedState);
return;
}
@@ -1632,25 +1629,19 @@ void QLowEnergyControllerPrivateWinRT::connectToPairedDevice()
GattCommunicationStatus commStatus;
hr = deviceServicesResult->get_Status(&commStatus);
if (FAILED(hr) || commStatus != GattCommunicationStatus_Success) {
- qCWarning(QT_BT_WINDOWS()) << "Service operation failed";
- setError(QLowEnergyController::ConnectionError);
- setState(QLowEnergyController::UnconnectedState);
- unregisterFromStatusChanges();
+ handleConnectionError("Service operation failed");
return;
}
ComPtr<IVectorView <GattDeviceService *>> deviceServices;
hr = deviceServicesResult->get_Services(&deviceServices);
CHECK_FOR_DEVICE_CONNECTION_ERROR(hr, "Could not obtain list of services", return)
- uint serviceCount;
+ uint serviceCount;
hr = deviceServices->get_Size(&serviceCount);
CHECK_FOR_DEVICE_CONNECTION_ERROR(hr, "Could not obtain service count", return)
if (serviceCount == 0) {
- qCWarning(QT_BT_WINDOWS()) << "Found devices without services";
- setError(QLowEnergyController::ConnectionError);
- setState(QLowEnergyController::UnconnectedState);
- unregisterFromStatusChanges();
+ handleConnectionError("Found devices without services");
return;
}
@@ -1679,13 +1670,10 @@ void QLowEnergyControllerPrivateWinRT::connectToPairedDevice()
ComPtr<IVectorView<GattCharacteristic *>> characteristics;
hr = characteristicsResult->get_Characteristics(&characteristics);
if (hr == E_ACCESSDENIED) {
- // Everything will work as expected up until this point if the manifest capabilties
- // for bluetooth LE are not set.
- qCWarning(QT_BT_WINDOWS) << "Could not obtain characteristic list. Please check your "
- "manifest capabilities";
- setState(QLowEnergyController::UnconnectedState);
- setError(QLowEnergyController::ConnectionError);
- unregisterFromStatusChanges();
+ // Everything will work as expected up until this point if the
+ // manifest capabilties for bluetooth LE are not set.
+ handleConnectionError("Could not obtain characteristic list. "
+ "Please check your manifest capabilities");
return;
}
CHECK_FOR_DEVICE_CONNECTION_ERROR(hr, "Could not obtain characteristic list", return);
@@ -1730,12 +1718,8 @@ void QLowEnergyControllerPrivateWinRT::connectToPairedDevice()
}
}
}
- if (deadline.hasExpired()) {
- qCWarning(QT_BT_WINDOWS) << "Connect to device failed due to timeout!";
- setError(QLowEnergyController::ConnectionError);
- setState(QLowEnergyController::UnconnectedState);
- unregisterFromStatusChanges();
- }
+ if (deadline.hasExpired())
+ handleConnectionError("Connect to device failed due to timeout!");
}
void QLowEnergyControllerPrivateWinRT::connectToUnpairedDevice()
@@ -1764,21 +1748,22 @@ void QLowEnergyControllerPrivateWinRT::connectToUnpairedDevice()
continue;
if (FAILED(hr) || commStatus != GattCommunicationStatus_Success) {
- qCWarning(QT_BT_WINDOWS()) << "Service operation failed";
- setError(QLowEnergyController::ConnectionError);
- setState(QLowEnergyController::UnconnectedState);
- unregisterFromStatusChanges();
+ handleConnectionError("Service operation failed");
return;
}
break;
}
- if (deadline.hasExpired()) {
- qCWarning(QT_BT_WINDOWS) << "Connect to device failed due to timeout!";
- setError(QLowEnergyController::ConnectionError);
- setState(QLowEnergyController::UnconnectedState);
- unregisterFromStatusChanges();
- }
+ if (deadline.hasExpired())
+ handleConnectionError("Connect to device failed due to timeout!");
+}
+
+void QLowEnergyControllerPrivateWinRT::handleConnectionError(const char *logMessage)
+{
+ qCWarning(QT_BT_WINDOWS) << logMessage;
+ setError(QLowEnergyController::ConnectionError);
+ setState(QLowEnergyController::UnconnectedState);
+ unregisterFromStatusChanges();
}
QT_END_NAMESPACE
diff --git a/src/bluetooth/qlowenergycontroller_winrt_p.h b/src/bluetooth/qlowenergycontroller_winrt_p.h
index 7e6c8823..04d56f26 100644
--- a/src/bluetooth/qlowenergycontroller_winrt_p.h
+++ b/src/bluetooth/qlowenergycontroller_winrt_p.h
@@ -140,6 +140,7 @@ private slots:
private:
void connectToPairedDevice();
void connectToUnpairedDevice();
+ void handleConnectionError(const char *logMessage);
bool mAbortPending = false;
Microsoft::WRL::ComPtr<ABI::Windows::Devices::Bluetooth::IBluetoothLEDevice> mDevice;