diff options
author | Ivan Solovev <ivan.solovev@qt.io> | 2022-06-22 18:57:41 +0200 |
---|---|---|
committer | Qt Cherry-pick Bot <cherrypick_bot@qt-project.org> | 2022-07-11 12:18:50 +0000 |
commit | f7453b9ac69fd177ead408c22eb580a21797bef3 (patch) | |
tree | 58dde6f22a6c281985a1f3db37cf4bc7c41dbbf6 /src/bluetooth/qbluetoothlocaldevice_winrt.cpp | |
parent | 23804f42fb201c0495d28b67095482012b672c48 (diff) |
Windows Bluetooth: move DeviceWatcher wrapper into a separate header
The DeviceWatcher wrapper seems to be useful not only in
QBluetoothLocalDevice implementation, but also for device discovery.
This patch moves it to a separate header, so that it can be reused.
The class' API is also refactored to suit for more general usecases.
This patch also applies the changes to QBluetoothLocalDevice.
As a drive-by: clean-up some includes and namespaces in
QBluetoothLocalDevice implementation.
Task-number: QTBUG-103263
Change-Id: I470c6eab4810065c03d5905032f4288fa9d6de8e
Reviewed-by: Juha Vuolle <juha.vuolle@insta.fi>
Reviewed-by: Oliver Wolff <oliver.wolff@qt.io>
(cherry picked from commit 761a059d5a5ef97e97039f3a34b3a7f92944f1f0)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
Diffstat (limited to 'src/bluetooth/qbluetoothlocaldevice_winrt.cpp')
-rw-r--r-- | src/bluetooth/qbluetoothlocaldevice_winrt.cpp | 90 |
1 files changed, 28 insertions, 62 deletions
diff --git a/src/bluetooth/qbluetoothlocaldevice_winrt.cpp b/src/bluetooth/qbluetoothlocaldevice_winrt.cpp index d97ba507..06fb5340 100644 --- a/src/bluetooth/qbluetoothlocaldevice_winrt.cpp +++ b/src/bluetooth/qbluetoothlocaldevice_winrt.cpp @@ -1,11 +1,12 @@ -// Copyright (C) 2019 The Qt Company Ltd. +// Copyright (C) 2022 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only -#include <qbluetoothutils_winrt_p.h> +#include <private/qbluetoothutils_winrt_p.h> #include "qbluetoothlocaldevice.h" #include "qbluetoothaddress.h" +#include "qbluetoothdevicewatcher_winrt_p.h" #include "qbluetoothlocaldevice_p.h" #include "qbluetoothutils_winrt_p.h" @@ -14,20 +15,18 @@ #include <winrt/Windows.Devices.Enumeration.h> #include <winrt/Windows.Devices.Bluetooth.h> #include <winrt/Windows.Devices.Radios.h> -#include <wrl.h> -#include <QtCore/private/qfunctions_winrt_p.h> +#include <QtCore/QCoreApplication> +#include <QtCore/QElapsedTimer> +#include <QtCore/QLoggingCategory> +#include <QtCore/QMutex> #include <QtCore/QPointer> #include <QtCore/QTimer> -#include <QtCore/QMutex> -#include <QtCore/QLoggingCategory> using namespace winrt::Windows::Foundation; using namespace winrt::Windows::Foundation::Collections; using namespace winrt::Windows::Devices::Enumeration; using namespace winrt::Windows::Devices::Bluetooth; -using namespace Microsoft::WRL; -using namespace Microsoft::WRL::Wrappers; using namespace winrt::Windows::Devices::Radios; QT_BEGIN_NAMESPACE @@ -75,66 +74,35 @@ static RadioState windowsStateFromMode(QBluetoothLocalDevice::HostMode mode) class AdapterManager; -class WatcherWrapper : public QObject, public std::enable_shared_from_this<WatcherWrapper> +class WatcherWrapper { - Q_OBJECT public: - WatcherWrapper(winrt::hstring selector) : mWatcher(DeviceInformation::CreateWatcher(selector)) + // we do not really care about unique ids here + WatcherWrapper(winrt::hstring selector) : + mWatcher(std::make_shared<QBluetoothDeviceWatcherWinRT>(0, selector)) { } - ~WatcherWrapper() - { - if (mWatcher && mInitialized) { - mWatcher.Stop(); - unsubscribeFromEvents(); - } - } template<typename AddedSlot, typename RemovedSlot> - bool init(AdapterManager *manager, AddedSlot onAdded, RemovedSlot onRemoved) + void init(AdapterManager *manager, AddedSlot onAdded, RemovedSlot onRemoved) { if (!mWatcher) { - qWarning() << "Windows failed to create an instance of DeviceWatcher. " - << "QBluetoothLocalDevice might fail to provide some information."; - return false; + qWarning("QBluetoothLocalDevice: failed to create device watcher!"); + return; } - subscribeToEvents(); - connect(this, &WatcherWrapper::deviceAdded, manager, onAdded, Qt::QueuedConnection); - connect(this, &WatcherWrapper::deviceRemoved, manager, onRemoved, Qt::QueuedConnection); - mInitialized = true; - mWatcher.Start(); - return true; - } - -signals: - void deviceAdded(winrt::hstring id); - void deviceRemoved(winrt::hstring id); + QObject::connect(mWatcher.get(), &QBluetoothDeviceWatcherWinRT::deviceAdded, + manager, onAdded, Qt::QueuedConnection); + QObject::connect(mWatcher.get(), &QBluetoothDeviceWatcherWinRT::deviceRemoved, + manager, onRemoved, Qt::QueuedConnection); -private: - void subscribeToEvents() - { - // The callbacks are triggered from separate threads. So we capture - // thisPtr to make sure that the object is valid. - auto thisPtr = shared_from_this(); - mAddedToken = mWatcher.Added([thisPtr](DeviceWatcher, const DeviceInformation &info) { - emit thisPtr->deviceAdded(info.Id()); - }); - mRemovedToken = - mWatcher.Removed([thisPtr](DeviceWatcher, const DeviceInformationUpdate &upd) { - emit thisPtr->deviceRemoved(upd.Id()); - }); - } - void unsubscribeFromEvents() - { - mWatcher.Added(mAddedToken); - mWatcher.Removed(mRemovedToken); + // This will also print a warning on failure. + if (mWatcher->init()) + mWatcher->start(); } - bool mInitialized = false; - DeviceWatcher mWatcher = nullptr; - winrt::event_token mAddedToken; - winrt::event_token mRemovedToken; +private: + std::shared_ptr<QBluetoothDeviceWatcherWinRT> mWatcher = nullptr; }; /* @@ -193,9 +161,9 @@ private: Q_SLOT void onDeviceAdded(winrt::hstring id); Q_SLOT void onDeviceRemoved(winrt::hstring id); - std::shared_ptr<WatcherWrapper> mAdapterWatcher = nullptr; - std::shared_ptr<WatcherWrapper> mLeDevicesWatcher = nullptr; - std::shared_ptr<WatcherWrapper> mClassicDevicesWatcher = nullptr; + std::unique_ptr<WatcherWrapper> mAdapterWatcher = nullptr; + std::unique_ptr<WatcherWrapper> mLeDevicesWatcher = nullptr; + std::unique_ptr<WatcherWrapper> mClassicDevicesWatcher = nullptr; QMutex mMutex; // Key for this map is BluetoothAdapter Id, *not* Radio Id. QMap<winrt::hstring, RadioInfo> mRadios; @@ -204,10 +172,8 @@ private: AdapterManager::AdapterManager() : QObject() { - qRegisterMetaType<winrt::hstring>("winrt::hstring"); - const auto adapterSelector = BluetoothAdapter::GetDeviceSelector(); - mAdapterWatcher = std::make_shared<WatcherWrapper>(adapterSelector); + mAdapterWatcher = std::make_unique<WatcherWrapper>(adapterSelector); mAdapterWatcher->init(this, &AdapterManager::onAdapterAdded, &AdapterManager::onAdapterRemoved); // Once created, device watchers will also populate the initial list of @@ -215,7 +181,7 @@ AdapterManager::AdapterManager() : QObject() const auto leSelector = BluetoothLEDevice::GetDeviceSelectorFromConnectionStatus( BluetoothConnectionStatus::Connected); - mLeDevicesWatcher = std::make_shared<WatcherWrapper>(leSelector); + mLeDevicesWatcher = std::make_unique<WatcherWrapper>(leSelector); mLeDevicesWatcher->init(this, &AdapterManager::onDeviceAdded, &AdapterManager::onDeviceRemoved); const auto classicSelector = BluetoothDevice::GetDeviceSelectorFromConnectionStatus( |