diff options
author | Denis Shienkov <denis.shienkov@gmail.com> | 2014-11-19 20:01:15 +0300 |
---|---|---|
committer | Timur Pocheptsov <Timur.Pocheptsov@digia.com> | 2014-12-01 15:17:22 +0100 |
commit | 11095f3504515f0ec2a754f64f97719340247505 (patch) | |
tree | 74c64183fbb5cd6457bd956da574585c35b8ab9e /src/bluetooth/qbluetoothdevicediscoveryagent_win.cpp | |
parent | a3c295297a4ee1365619cc49f504a9f35b4f7785 (diff) |
Refactor of code for "classic" part on Windows
The previous code is based on the assumption where we can use each
of local radio separatelly to discovery of remote devices. Windows
API allows to use a handle of separately local bluetooth adapter to
operate with its power, to start/stop detection of remote devices
through it and so on. But in this API there is no opportunity to
enumerate services and attributes of the given remote device via
the concrete local adapter.
Therefore now to discovery of remote devices are used all local
bluetooth adapters in system. Also, the power and the host modes
management now are belongs to all local adapters at once.
Tested on Windows 8 with the USB CSR8510 A10 adapter
Change-Id: I949b112158a575f5b563a78163c1e3990c952ada
Reviewed-by: Alex Blasche <alexander.blasche@theqtcompany.com>
Diffstat (limited to 'src/bluetooth/qbluetoothdevicediscoveryagent_win.cpp')
-rw-r--r-- | src/bluetooth/qbluetoothdevicediscoveryagent_win.cpp | 209 |
1 files changed, 100 insertions, 109 deletions
diff --git a/src/bluetooth/qbluetoothdevicediscoveryagent_win.cpp b/src/bluetooth/qbluetoothdevicediscoveryagent_win.cpp index bdfb0237..53e4d5aa 100644 --- a/src/bluetooth/qbluetoothdevicediscoveryagent_win.cpp +++ b/src/bluetooth/qbluetoothdevicediscoveryagent_win.cpp @@ -45,45 +45,30 @@ #include "qbluetoothaddress.h" #include "qbluetoothuuid.h" -#define QT_DEVICEDISCOVERY_DEBUG - QT_BEGIN_NAMESPACE -struct NativeFindResult -{ - NativeFindResult(); - - BLUETOOTH_DEVICE_INFO deviceInfo; - HBLUETOOTH_DEVICE_FIND findHandle; - DWORD errorCode; -}; - -NativeFindResult::NativeFindResult() - : findHandle(NULL) - , errorCode(ERROR_SUCCESS) -{ - ::ZeroMemory(&deviceInfo, sizeof(deviceInfo)); - deviceInfo.dwSize = sizeof(deviceInfo); -} +Q_DECLARE_LOGGING_CATEGORY(QT_BT_WINDOWS) QBluetoothDeviceDiscoveryAgentPrivate::QBluetoothDeviceDiscoveryAgentPrivate( const QBluetoothAddress &deviceAdapter, QBluetoothDeviceDiscoveryAgent *parent) - : QBluetoothLocalDevicePrivateData(deviceAdapter) - , inquiryType(QBluetoothDeviceDiscoveryAgent::GeneralUnlimitedInquiry) + : inquiryType(QBluetoothDeviceDiscoveryAgent::GeneralUnlimitedInquiry) , lastError(QBluetoothDeviceDiscoveryAgent::NoError) - , findWatcher(0) + , classicDiscoveryWatcher(0) , pendingCancel(false) , pendingStart(false) + , isClassicActive(false) + , isClassicValid(false) , q_ptr(parent) { + initialize(deviceAdapter); } QBluetoothDeviceDiscoveryAgentPrivate::~QBluetoothDeviceDiscoveryAgentPrivate() { - if (isRunning()) { + if (isClassicActive) { stop(); - findWatcher->waitForFinished(); + classicDiscoveryWatcher->waitForFinished(); } } @@ -94,12 +79,16 @@ bool QBluetoothDeviceDiscoveryAgentPrivate::isActive() const if (pendingCancel) return false; - return isRunning(); + return isClassicActive; } void QBluetoothDeviceDiscoveryAgentPrivate::start() { - Q_Q(QBluetoothDeviceDiscoveryAgent); + if (!isClassicValid) { + setError(ERROR_INVALID_HANDLE, + QBluetoothDeviceDiscoveryAgent::tr("Passed address is not a local device.")); + return; + } if (pendingCancel == true) { pendingStart = true; @@ -108,40 +97,27 @@ void QBluetoothDeviceDiscoveryAgentPrivate::start() discoveredDevices.clear(); - if (isValid() == false) { - lastError = QBluetoothDeviceDiscoveryAgent::InvalidBluetoothAdapterError; - errorString = qt_error_string(ERROR_INVALID_HANDLE); - emit q->error(lastError); - return; - } - - if (!findWatcher) { - findWatcher = new QFutureWatcher<QVariant>(q); - QObject::connect(findWatcher, SIGNAL(finished()), q, SLOT(_q_handleFindResult())); - } - - const QFuture<QVariant> future = QtConcurrent::run(findFirstDevice, deviceHandle); - findWatcher->setFuture(future); + startDiscoveryForFirstClassicDevice(); } void QBluetoothDeviceDiscoveryAgentPrivate::stop() { - if (!isRunning()) + if (!isClassicActive) return; pendingCancel = true; pendingStart = false; } -void QBluetoothDeviceDiscoveryAgentPrivate::_q_handleFindResult() +void QBluetoothDeviceDiscoveryAgentPrivate::classicDeviceDiscovered() { Q_Q(QBluetoothDeviceDiscoveryAgent); - const QVariant result = findWatcher->result(); - const NativeFindResult nativeFindResult = result.value<NativeFindResult>(); + const WinClassicBluetooth::RemoteDeviceDiscoveryResult result = + classicDiscoveryWatcher->result(); - if (nativeFindResult.errorCode == ERROR_SUCCESS - || nativeFindResult.errorCode == ERROR_NO_MORE_ITEMS) { + if (result.error == ERROR_SUCCESS + || result.error == ERROR_NO_MORE_ITEMS) { if (pendingCancel && !pendingStart) { emit q->canceled(); @@ -151,101 +127,116 @@ void QBluetoothDeviceDiscoveryAgentPrivate::_q_handleFindResult() pendingStart = false; start(); } else { - if (nativeFindResult.errorCode == ERROR_NO_MORE_ITEMS) { - emit q->finished(); - } else { - processDiscoveredDevices(nativeFindResult.deviceInfo); - const QFuture<QVariant> future = - QtConcurrent::run(findNextDevice, nativeFindResult.findHandle); - findWatcher->setFuture(future); + if (result.error != ERROR_NO_MORE_ITEMS) { + acceptDiscoveredClassicDevice(result.device); + startDiscoveryForNextClassicDevice(result.hSearch); return; } } } else { - handleErrors(nativeFindResult.errorCode); + setError(result.error); pendingCancel = false; pendingStart = false; } - findClose(nativeFindResult.findHandle); + completeClassicDiscovery(result.hSearch); } -void QBluetoothDeviceDiscoveryAgentPrivate::processDiscoveredDevices( - const BLUETOOTH_DEVICE_INFO &info) +void QBluetoothDeviceDiscoveryAgentPrivate::initialize( + const QBluetoothAddress &deviceAdapter) { - Q_Q(QBluetoothDeviceDiscoveryAgent); + isClassicValid = isClassicAdapterValid(deviceAdapter); +} - QBluetoothDeviceInfo deviceInfo( - QBluetoothAddress(info.Address.ullLong), - QString::fromWCharArray(info.szName), - info.ulClassofDevice); +bool QBluetoothDeviceDiscoveryAgentPrivate::isClassicAdapterValid( + const QBluetoothAddress &deviceAdapter) +{ + const WinClassicBluetooth::LocalRadiosDiscoveryResult result = + WinClassicBluetooth::enumerateLocalRadios(); - if (info.fRemembered) - deviceInfo.setCached(true); + if (result.error != NO_ERROR + && result.error != ERROR_NO_MORE_ITEMS) { + qCWarning(QT_BT_WINDOWS) << "Occurred error during search of classic local radios"; + return false; + } else if (result.radios.isEmpty()) { + qCWarning(QT_BT_WINDOWS) << "No any classic local radio found"; + return false; + } - discoveredDevices.append(deviceInfo); - emit q->deviceDiscovered(deviceInfo); + foreach (const BLUETOOTH_RADIO_INFO &radio, result.radios) { + if (deviceAdapter == QBluetoothAddress() + || deviceAdapter == QBluetoothAddress(radio.address.ullLong)) { + return true; + } + } + + qCWarning(QT_BT_WINDOWS) << "No matching for classic local radio:" << deviceAdapter; + return false; } -void QBluetoothDeviceDiscoveryAgentPrivate::handleErrors(DWORD errorCode) +void QBluetoothDeviceDiscoveryAgentPrivate::startDiscoveryForFirstClassicDevice() { - Q_Q(QBluetoothDeviceDiscoveryAgent); + isClassicActive = true; - lastError = (errorCode == ERROR_INVALID_HANDLE) ? - QBluetoothDeviceDiscoveryAgent::InvalidBluetoothAdapterError - : QBluetoothDeviceDiscoveryAgent::InputOutputError; - errorString = qt_error_string(errorCode); - emit q->error(lastError); + if (!classicDiscoveryWatcher) { + classicDiscoveryWatcher = new QFutureWatcher< + WinClassicBluetooth::RemoteDeviceDiscoveryResult>(this); + QObject::connect(classicDiscoveryWatcher, SIGNAL(finished()), + this, SLOT(classicDeviceDiscovered())); + } + + const QFuture<WinClassicBluetooth::RemoteDeviceDiscoveryResult> future = + QtConcurrent::run(WinClassicBluetooth::startDiscoveryOfFirstRemoteDevice); + classicDiscoveryWatcher->setFuture(future); } -bool QBluetoothDeviceDiscoveryAgentPrivate::isRunning() const +void QBluetoothDeviceDiscoveryAgentPrivate::startDiscoveryForNextClassicDevice( + HBLUETOOTH_DEVICE_FIND hSearch) { - return findWatcher && findWatcher->isRunning(); + Q_ASSERT(classicDiscoveryWatcher); + + const QFuture<WinClassicBluetooth::RemoteDeviceDiscoveryResult> future = + QtConcurrent::run(WinClassicBluetooth::startDiscoveryOfNextRemoteDevice, hSearch); + classicDiscoveryWatcher->setFuture(future); } -QVariant QBluetoothDeviceDiscoveryAgentPrivate::findFirstDevice(HANDLE radioHandle) +void QBluetoothDeviceDiscoveryAgentPrivate::completeClassicDiscovery( + HBLUETOOTH_DEVICE_FIND hSearch) { - BLUETOOTH_DEVICE_SEARCH_PARAMS searchParams; - ::ZeroMemory(&searchParams, sizeof(searchParams)); - searchParams.dwSize = sizeof(searchParams); - searchParams.cTimeoutMultiplier = 10; // 12.8 sec - searchParams.fIssueInquiry = TRUE; - searchParams.fReturnAuthenticated = TRUE; - searchParams.fReturnConnected = TRUE; - searchParams.fReturnRemembered = TRUE; - searchParams.fReturnUnknown = TRUE; - searchParams.hRadio = radioHandle; - - NativeFindResult nativeFindResult; - nativeFindResult.findHandle = ::BluetoothFindFirstDevice( - &searchParams, &nativeFindResult.deviceInfo); - if (!nativeFindResult.findHandle) - nativeFindResult.errorCode = ::GetLastError(); - - QVariant result; - result.setValue(nativeFindResult); - return result; + Q_Q(QBluetoothDeviceDiscoveryAgent); + + WinClassicBluetooth::cancelRemoteDevicesDiscovery(hSearch); + isClassicActive = false; + emit q->finished(); } -QVariant QBluetoothDeviceDiscoveryAgentPrivate::findNextDevice(HBLUETOOTH_DEVICE_FIND findHandle) +void QBluetoothDeviceDiscoveryAgentPrivate::acceptDiscoveredClassicDevice( + const BLUETOOTH_DEVICE_INFO &device) { - NativeFindResult nativeFindResult; - nativeFindResult.findHandle = findHandle; - if (!::BluetoothFindNextDevice(findHandle, &nativeFindResult.deviceInfo)) - nativeFindResult.errorCode = ::GetLastError(); - - QVariant result; - result.setValue(nativeFindResult); - return result; + Q_Q(QBluetoothDeviceDiscoveryAgent); + + QBluetoothDeviceInfo deviceInfo( + QBluetoothAddress(device.Address.ullLong), + QString::fromWCharArray(device.szName), + device.ulClassofDevice); + + if (device.fRemembered) + deviceInfo.setCached(true); + + discoveredDevices.append(deviceInfo); + emit q->deviceDiscovered(deviceInfo); } -void QBluetoothDeviceDiscoveryAgentPrivate::findClose(HBLUETOOTH_DEVICE_FIND findHandle) +void QBluetoothDeviceDiscoveryAgentPrivate::setError(DWORD error, const QString &str) { - if (findHandle) - ::BluetoothFindDeviceClose(findHandle); + Q_Q(QBluetoothDeviceDiscoveryAgent); + + lastError = (error == ERROR_INVALID_HANDLE) ? + QBluetoothDeviceDiscoveryAgent::InvalidBluetoothAdapterError + : QBluetoothDeviceDiscoveryAgent::InputOutputError; + errorString = str.isEmpty() ? qt_error_string(error) : str; + emit q->error(lastError); } QT_END_NAMESPACE - -Q_DECLARE_METATYPE(QT_PREPEND_NAMESPACE(NativeFindResult)) |