From 4301d682e05c678c378864cb48da489203f785e2 Mon Sep 17 00:00:00 2001 From: Denis Shienkov Date: Thu, 1 Jun 2017 17:42:42 +0300 Subject: Pass a device address to correctly search its service system path We need to check on a device address from the returned system path and to return matched path. Otherwise always returns a first found system path for devices with same service UUID. Change-Id: Iccfae4d7b19451b4fa19fce9bbd1ab9b749cd5bb Reviewed-by: Alex Blasche --- src/bluetooth/qlowenergycontroller_win.cpp | 38 +++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 11 deletions(-) diff --git a/src/bluetooth/qlowenergycontroller_win.cpp b/src/bluetooth/qlowenergycontroller_win.cpp index 02b03297..e7967dc4 100644 --- a/src/bluetooth/qlowenergycontroller_win.cpp +++ b/src/bluetooth/qlowenergycontroller_win.cpp @@ -84,7 +84,17 @@ enum { UseNotifications = 0x1, UseIndications = 0x2 }; static bool gattFunctionsResolved = false; -static QString getServiceSystemPath(const QBluetoothUuid &serviceUuid, int *systemErrorCode) +static QBluetoothAddress getDeviceAddress(const QString &servicePath) +{ + const int firstbound = servicePath.indexOf(QStringLiteral("_")); + const int lastbound = servicePath.indexOf(QLatin1Char('#'), firstbound); + const QString hex = servicePath.mid(firstbound + 1, lastbound - firstbound - 1); + bool ok = false; + return QBluetoothAddress(hex.toULongLong(&ok, 16)); +} + +static QString getServiceSystemPath(const QBluetoothAddress &deviceAddress, + const QBluetoothUuid &serviceUuid, int *systemErrorCode) { const HDEVINFO deviceInfoSet = ::SetupDiGetClassDevs( reinterpret_cast(&serviceUuid), @@ -154,9 +164,15 @@ static QString getServiceSystemPath(const QBluetoothUuid &serviceUuid, int *syst break; } - foundSystemPath = QString::fromWCharArray(deviceInterfaceDetailData->DevicePath); - *systemErrorCode = NO_ERROR; - break; + // We need to check on required device address which contains in a + // system path. As it is not enough to use only service UUID for this. + const auto candidateSystemPath = QString::fromWCharArray(deviceInterfaceDetailData->DevicePath); + const auto candidateDeviceAddress = getDeviceAddress(candidateSystemPath); + if (candidateDeviceAddress == deviceAddress) { + foundSystemPath = candidateSystemPath; + *systemErrorCode = NO_ERROR; + break; + } } ::SetupDiDestroyDeviceInfoList(deviceInfoSet); @@ -193,11 +209,11 @@ static HANDLE openSystemDevice( return hDevice; } -static HANDLE openSystemService( +static HANDLE openSystemService(const QBluetoothAddress &deviceAddress, const QBluetoothUuid &service, QIODevice::OpenMode openMode, int *systemErrorCode) { const QString serviceSystemPath = getServiceSystemPath( - service, systemErrorCode); + deviceAddress, service, systemErrorCode); if (*systemErrorCode != NO_ERROR) return INVALID_HANDLE_VALUE; @@ -759,7 +775,7 @@ void QLowEnergyControllerPrivate::discoverServiceDetails( int systemErrorCode = NO_ERROR; const HANDLE hService = openSystemService( - service, QIODevice::ReadOnly, &systemErrorCode); + remoteDevice, service, QIODevice::ReadOnly, &systemErrorCode); if (systemErrorCode != NO_ERROR) { qCWarning(QT_BT_WINDOWS) << "Unable to open service" << service.toString() @@ -924,7 +940,7 @@ void QLowEnergyControllerPrivate::readCharacteristic( int systemErrorCode = NO_ERROR; const HANDLE hService = openSystemService( - service->uuid, QIODevice::ReadOnly, &systemErrorCode); + remoteDevice, service->uuid, QIODevice::ReadOnly, &systemErrorCode); if (systemErrorCode != NO_ERROR) { qCWarning(QT_BT_WINDOWS) << "Unable to open service" << service->uuid.toString() @@ -968,7 +984,7 @@ void QLowEnergyControllerPrivate::writeCharacteristic( int systemErrorCode = NO_ERROR; const HANDLE hService = openSystemService( - service->uuid, QIODevice::ReadWrite, &systemErrorCode); + remoteDevice, service->uuid, QIODevice::ReadWrite, &systemErrorCode); if (systemErrorCode != NO_ERROR) { qCWarning(QT_BT_WINDOWS) << "Unable to open service" << service->uuid.toString() @@ -1028,7 +1044,7 @@ void QLowEnergyControllerPrivate::readDescriptor( int systemErrorCode = NO_ERROR; const HANDLE hService = openSystemService( - service->uuid, QIODevice::ReadOnly, &systemErrorCode); + remoteDevice, service->uuid, QIODevice::ReadOnly, &systemErrorCode); if (systemErrorCode != NO_ERROR) { qCWarning(QT_BT_WINDOWS) << "Unable to open service" << service->uuid.toString() @@ -1083,7 +1099,7 @@ void QLowEnergyControllerPrivate::writeDescriptor( int systemErrorCode = NO_ERROR; const HANDLE hService = openSystemService( - service->uuid, QIODevice::ReadWrite, &systemErrorCode); + remoteDevice, service->uuid, QIODevice::ReadWrite, &systemErrorCode); if (systemErrorCode != NO_ERROR) { qCWarning(QT_BT_WINDOWS) << "Unable to open service" << service->uuid.toString() -- cgit v1.2.3