Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qlowenergycontroller_winrt_p.h
Go to the documentation of this file.
1// Copyright (C) 2017 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QLOWENERGYCONTROLLERPRIVATEWINRT_P_H
5#define QLOWENERGYCONTROLLERPRIVATEWINRT_P_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists purely as an
12// implementation detail. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include <qglobal.h>
19#include <QtCore/qhash.h>
20#include <QtCore/QList>
21#include <QtCore/QQueue>
22#include <QtBluetooth/qbluetooth.h>
23#include <QtBluetooth/qlowenergycharacteristic.h>
24#include <QtBluetooth/qlowenergyservicedata.h>
27
28#include <winrt/Windows.Foundation.h>
29#include <winrt/Windows.Devices.Bluetooth.h>
30#include <winrt/Windows.Devices.Bluetooth.GenericAttributeProfile.h>
31
32using namespace winrt::Windows::Devices::Bluetooth;
33using namespace winrt::Windows::Devices::Bluetooth::GenericAttributeProfile;
34
35namespace winrt
36{
39}
40
41#include <functional>
42
43QT_BEGIN_NAMESPACE
44
45class QLowEnergyServiceData;
46class QTimer;
47
49
50class QLowEnergyControllerPrivateWinRT final : public QLowEnergyControllerPrivate
51{
52 Q_OBJECT
53public:
54 QLowEnergyControllerPrivateWinRT();
56
57 void init() override;
58
59 void connectToDevice() override;
60 void disconnectFromDevice() override;
61
62 void discoverServices() override;
63 void discoverServiceDetails(const QBluetoothUuid &service,
64 QLowEnergyService::DiscoveryMode mode) override;
65
66 void startAdvertising(const QLowEnergyAdvertisingParameters &params,
67 const QLowEnergyAdvertisingData &advertisingData,
68 const QLowEnergyAdvertisingData &scanResponseData) override;
69 void stopAdvertising() override;
70
71 void requestConnectionUpdate(const QLowEnergyConnectionParameters &params) override;
72
73 // read data
74 void readCharacteristic(const QSharedPointer<QLowEnergyServicePrivate> service,
75 const QLowEnergyHandle charHandle) override;
76 void readDescriptor(const QSharedPointer<QLowEnergyServicePrivate> service,
77 const QLowEnergyHandle charHandle,
78 const QLowEnergyHandle descriptorHandle) override;
79
80 // write data
81 void writeCharacteristic(const QSharedPointer<QLowEnergyServicePrivate> service,
82 const QLowEnergyHandle charHandle,
83 const QByteArray &newValue, QLowEnergyService::WriteMode mode) override;
84 void writeDescriptor(const QSharedPointer<QLowEnergyServicePrivate> service,
85 const QLowEnergyHandle charHandle,
86 const QLowEnergyHandle descriptorHandle,
87 const QByteArray &newValue) override;
88
89 void addToGenericAttributeList(const QLowEnergyServiceData &service,
90 QLowEnergyHandle startHandle) override;
91
92 int mtu() const override;
93
94signals:
95 void characteristicChanged(quint16 charHandle, const QByteArray &data);
97
98private slots:
99 void handleCharacteristicChanged(quint16 charHandle, const QByteArray &data);
100 void handleServiceHandlerError(const QString &error);
101
102private:
103 void handleConnectionError(const char *logMessage);
104
105 BluetoothLEDevice mDevice = nullptr;
106 GattSession mGattSession = nullptr;
107 winrt::event_token mStatusChangedToken{ 0 };
108 winrt::event_token mMtuChangedToken{ 0 };
109 struct ValueChangedEntry {
110 ValueChangedEntry() {}
111 ValueChangedEntry(GattCharacteristic c,
112 winrt::event_token t)
113 : characteristic(c)
114 , token(t)
115 {
116 }
117
118 GattCharacteristic characteristic = nullptr;
119 winrt::event_token token = { 0 };
120 };
121 QList<ValueChangedEntry> mValueChangedTokens;
122
123 QMap<QBluetoothUuid, GattDeviceService> m_openedServices;
124 QSet<QBluetoothUuid> m_requestDetailsServiceUuids;
125
126 using NativeCharacteristicKey = std::pair<QBluetoothUuid/* service UUID */,
127 QBluetoothUuid/* characteristic UUID */>;
128 QHash<NativeCharacteristicKey, GattCharacteristic> m_openedCharacteristics;
129
130 // Note: the callback can be called with a null GattDeviceService.
131 // This means that the service discovery has failed, and can be used
132 // to run some cleanup logic.
133 using NativeServiceCallback = std::function<void(GattDeviceService)>;
134 bool getNativeService(const QBluetoothUuid &serviceUuid, NativeServiceCallback callback);
135
136 // Note: the callback is only called with a valid characteristic. If the
137 // service or characteristic lookup fails, all queued callbacks for that
138 // characteristic are dropped without being called and do not report any
139 // errors.
140 using NativeCharacteristicCallback = std::function<void(GattCharacteristic)>;
141 bool getNativeCharacteristic(const QBluetoothUuid &serviceUuid,
142 const QBluetoothUuid &charUuid,
143 NativeCharacteristicCallback callback);
144
145 // The generation counter is needed to prevent the cases when a stale
146 // lookup clears a new operations queue. That's possible if a device
147 // was disconnected, and then quickly connected again (even though very
148 // unlikely in practice).
149 struct PendingCharOperations
150 {
151 quint64 generation = 0;
152 QList<NativeCharacteristicCallback> callbacks;
153 };
154 QHash<NativeCharacteristicKey, PendingCharOperations> m_pendingCharOperations;
155 quint64 m_charOperationsGeneration = 0;
156
157 void registerForValueChanges(const QBluetoothUuid &serviceUuid, const QBluetoothUuid &charUuid);
158 void unregisterFromValueChanges();
159 void onValueChange(GattCharacteristic characteristic, GattValueChangedEventArgs const &args);
160 void onMtuChange(GattSession session, winrt::IInspectable args);
161 bool registerForMtuChanges();
162 void unregisterFromMtuChanges();
163
164 bool registerForStatusChanges();
165 void unregisterFromStatusChanges();
166 void onStatusChange(BluetoothLEDevice dev, winrt::IInspectable args);
167
168 void obtainIncludedServices(QSharedPointer<QLowEnergyServicePrivate> servicePointer,
169 GattDeviceService nativeService);
170 void onServiceDiscoveryFinished(winrt::Windows::Foundation::IAsyncOperation<GattDeviceServicesResult> const &op,
171 winrt::AsyncStatus status);
172
173 void readCharacteristicHelper(const QSharedPointer<QLowEnergyServicePrivate> service,
174 const QLowEnergyHandle charHandle,
175 GattCharacteristic characteristic);
176 void readDescriptorHelper(const QSharedPointer<QLowEnergyServicePrivate> service,
177 const QLowEnergyHandle charHandle,
178 const QLowEnergyHandle descriptorHandle,
179 GattCharacteristic characteristic);
180 void writeCharacteristicHelper(const QSharedPointer<QLowEnergyServicePrivate> service,
181 const QLowEnergyHandle charHandle, const QByteArray &newValue,
182 bool writeWithResponse,
183 GattCharacteristic characteristic);
184 void writeDescriptorHelper(const QSharedPointer<QLowEnergyServicePrivate> service,
185 const QLowEnergyHandle charHandle,
186 const QLowEnergyHandle descriptorHandle,
187 const QByteArray &newValue,
188 GattCharacteristic characteristic);
189 void discoverServiceDetailsHelper(const QBluetoothUuid &service,
190 QLowEnergyService::DiscoveryMode mode,
191 GattDeviceService deviceService);
192
193 void clearAllServices();
194 void closeAndRemoveService(const QBluetoothUuid &uuid);
195
196 void handleCharacteristic(const NativeCharacteristicKey &key,
197 quint64 generation,
198 GattDeviceService service,
199 GattCharacteristic characteristic);
200 void removeCachedCharacteristics(const QBluetoothUuid &serviceUuid);
201};
202
203QT_END_NAMESPACE
204
205#endif // QLOWENERGYCONTROLLERPRIVATEWINRT_P_H
void discoverServiceDetails(const QBluetoothUuid &service, QLowEnergyService::DiscoveryMode mode) override
void addToGenericAttributeList(const QLowEnergyServiceData &service, QLowEnergyHandle startHandle) override
void readDescriptor(const QSharedPointer< QLowEnergyServicePrivate > service, const QLowEnergyHandle charHandle, const QLowEnergyHandle descriptorHandle) override
void writeDescriptor(const QSharedPointer< QLowEnergyServicePrivate > service, const QLowEnergyHandle charHandle, const QLowEnergyHandle descriptorHandle, const QByteArray &newValue) override
void startAdvertising(const QLowEnergyAdvertisingParameters &params, const QLowEnergyAdvertisingData &advertisingData, const QLowEnergyAdvertisingData &scanResponseData) override
void readCharacteristic(const QSharedPointer< QLowEnergyServicePrivate > service, const QLowEnergyHandle charHandle) override
void writeCharacteristic(const QSharedPointer< QLowEnergyServicePrivate > service, const QLowEnergyHandle charHandle, const QByteArray &newValue, QLowEnergyService::WriteMode mode) override
void requestConnectionUpdate(const QLowEnergyConnectionParameters &params) override
void setController(QLowEnergyControllerPrivate *control)
void errorOccurred(const QString &error)
QHash< QLowEnergyHandle, QLowEnergyServicePrivate::CharData > mCharacteristicList
void errorOccured(const QString &error)
QLowEnergyService::DiscoveryMode mMode
Windows::Foundation::AsyncStatus AsyncStatus
#define SAFE(x)
#define HR(x)
#define TRY(x)
void registerQLowEnergyControllerMetaType()
#define RETURN_FALSE(msg)
#define DEC_CHAR_COUNT_AND_CONTINUE(msg)
constexpr T & bitwise_or_equal(T &a, const E &b)
#define ENUM_BITWISE_OPS(E)
QT_BEGIN_NAMESPACE typedef GattReadClientCharacteristicConfigurationDescriptorResult ClientCharConfigDescriptorResult
static T await_forever(IAsyncOperation< T > asyncInfo, GlobalCondition canceled=never)
static QByteArray byteArrayFromGattResult(GattReadResult gattResult, bool isWCharString=false)
constexpr std::underlying_type_t< E > bitwise_or(E x, E y)
constexpr T bitwise_or(T x, E y)
std::function< bool()> GlobalCondition
static T await(IAsyncOperation< T > asyncInfo, GlobalCondition canceled=never, int timeout=5000)
static QByteArray byteArrayFromBuffer(IBuffer buffer, bool isWCharString=false)
#define RETURN_SERVICE_ERROR(msg, service, error)
constexpr std::underlying_type_t< E > bitwise_and(E x, E y)
static constexpr bool never()
constexpr T bitwise_and(T x, E y)
constexpr int timeout_infinity
#define WARN_AND_CONTINUE(msg)
#define RETURN_MSG(msg)