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/QList>
20#include <QtCore/QQueue>
21#include <QtBluetooth/qbluetooth.h>
22#include <QtBluetooth/qlowenergycharacteristic.h>
23#include <QtBluetooth/qlowenergyservicedata.h>
26
27#include <winrt/Windows.Foundation.h>
28#include <winrt/Windows.Devices.Bluetooth.h>
29#include <winrt/Windows.Devices.Bluetooth.GenericAttributeProfile.h>
30
31using namespace winrt::Windows::Devices::Bluetooth;
32using namespace winrt::Windows::Devices::Bluetooth::GenericAttributeProfile;
33
34namespace winrt
35{
38}
39
40#include <functional>
41
42QT_BEGIN_NAMESPACE
43
44class QLowEnergyServiceData;
45class QTimer;
46
48
49class QLowEnergyControllerPrivateWinRT final : public QLowEnergyControllerPrivate
50{
51 Q_OBJECT
52public:
53 QLowEnergyControllerPrivateWinRT();
55
56 void init() override;
57
58 void connectToDevice() override;
59 void disconnectFromDevice() override;
60
61 void discoverServices() override;
62 void discoverServiceDetails(const QBluetoothUuid &service,
63 QLowEnergyService::DiscoveryMode mode) override;
64
65 void startAdvertising(const QLowEnergyAdvertisingParameters &params,
66 const QLowEnergyAdvertisingData &advertisingData,
67 const QLowEnergyAdvertisingData &scanResponseData) override;
68 void stopAdvertising() override;
69
70 void requestConnectionUpdate(const QLowEnergyConnectionParameters &params) override;
71
72 // read data
73 void readCharacteristic(const QSharedPointer<QLowEnergyServicePrivate> service,
74 const QLowEnergyHandle charHandle) override;
75 void readDescriptor(const QSharedPointer<QLowEnergyServicePrivate> service,
76 const QLowEnergyHandle charHandle,
77 const QLowEnergyHandle descriptorHandle) override;
78
79 // write data
80 void writeCharacteristic(const QSharedPointer<QLowEnergyServicePrivate> service,
81 const QLowEnergyHandle charHandle,
82 const QByteArray &newValue, QLowEnergyService::WriteMode mode) override;
83 void writeDescriptor(const QSharedPointer<QLowEnergyServicePrivate> service,
84 const QLowEnergyHandle charHandle,
85 const QLowEnergyHandle descriptorHandle,
86 const QByteArray &newValue) override;
87
88 void addToGenericAttributeList(const QLowEnergyServiceData &service,
89 QLowEnergyHandle startHandle) override;
90
91 int mtu() const override;
92
93signals:
94 void characteristicChanged(quint16 charHandle, const QByteArray &data);
96
97private slots:
98 void handleCharacteristicChanged(quint16 charHandle, const QByteArray &data);
99 void handleServiceHandlerError(const QString &error);
100
101private:
102 void handleConnectionError(const char *logMessage);
103
104 BluetoothLEDevice mDevice = nullptr;
105 GattSession mGattSession = nullptr;
106 winrt::event_token mStatusChangedToken{ 0 };
107 winrt::event_token mMtuChangedToken{ 0 };
108 struct ValueChangedEntry {
109 ValueChangedEntry() {}
110 ValueChangedEntry(GattCharacteristic c,
111 winrt::event_token t)
112 : characteristic(c)
113 , token(t)
114 {
115 }
116
117 GattCharacteristic characteristic = nullptr;
118 winrt::event_token token = { 0 };
119 };
120 QList<ValueChangedEntry> mValueChangedTokens;
121
122 QMap<QBluetoothUuid, GattDeviceService> m_openedServices;
123 QSet<QBluetoothUuid> m_requestDetailsServiceUuids;
124
125 using NativeServiceCallback = std::function<void(GattDeviceService)>;
126 bool getNativeService(const QBluetoothUuid &serviceUuid, NativeServiceCallback callback);
127
128 using NativeCharacteristicCallback = std::function<void(GattCharacteristic)>;
129 bool getNativeCharacteristic(const QBluetoothUuid &serviceUuid,
130 const QBluetoothUuid &charUuid,
131 NativeCharacteristicCallback callback);
132
133 void registerForValueChanges(const QBluetoothUuid &serviceUuid, const QBluetoothUuid &charUuid);
134 void unregisterFromValueChanges();
135 void onValueChange(GattCharacteristic characteristic, GattValueChangedEventArgs const &args);
136 void onMtuChange(GattSession session, winrt::IInspectable args);
137 bool registerForMtuChanges();
138 void unregisterFromMtuChanges();
139
140 bool registerForStatusChanges();
141 void unregisterFromStatusChanges();
142 void onStatusChange(BluetoothLEDevice dev, winrt::IInspectable args);
143
144 void obtainIncludedServices(QSharedPointer<QLowEnergyServicePrivate> servicePointer,
145 GattDeviceService nativeService);
146 void onServiceDiscoveryFinished(winrt::Windows::Foundation::IAsyncOperation<GattDeviceServicesResult> const &op,
147 winrt::AsyncStatus status);
148
149 void readCharacteristicHelper(const QSharedPointer<QLowEnergyServicePrivate> service,
150 const QLowEnergyHandle charHandle,
151 GattCharacteristic characteristic);
152 void readDescriptorHelper(const QSharedPointer<QLowEnergyServicePrivate> service,
153 const QLowEnergyHandle charHandle,
154 const QLowEnergyHandle descriptorHandle,
155 GattCharacteristic characteristic);
156 void writeCharacteristicHelper(const QSharedPointer<QLowEnergyServicePrivate> service,
157 const QLowEnergyHandle charHandle, const QByteArray &newValue,
158 bool writeWithResponse,
159 GattCharacteristic characteristic);
160 void writeDescriptorHelper(const QSharedPointer<QLowEnergyServicePrivate> service,
161 const QLowEnergyHandle charHandle,
162 const QLowEnergyHandle descriptorHandle,
163 const QByteArray &newValue,
164 GattCharacteristic characteristic);
165 void discoverServiceDetailsHelper(const QBluetoothUuid &service,
166 QLowEnergyService::DiscoveryMode mode,
167 GattDeviceService deviceService);
168
169 void clearAllServices();
170 void closeAndRemoveService(const QBluetoothUuid &uuid);
171};
172
173QT_END_NAMESPACE
174
175#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
void registerQLowEnergyControllerMetaType()
#define SAFE(x)
#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)
#define LOG_HRESULT(hr)
constexpr T bitwise_or(T x, E y)
std::function< bool()> GlobalCondition
#define HR(x)
static T await(IAsyncOperation< T > asyncInfo, GlobalCondition canceled=never, int timeout=5000)
static QByteArray byteArrayFromBuffer(IBuffer buffer, bool isWCharString=false)
#define TRY(x)
#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)