1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
|
// Copyright (C) 2017 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
#ifndef QLOWENERGYCONTROLLERPRIVATEWINRT_P_H
#define QLOWENERGYCONTROLLERPRIVATEWINRT_P_H
//
// W A R N I N G
// -------------
//
// This file is not part of the Qt API. It exists purely as an
// implementation detail. This header file may change from version to
// version without notice, or even be removed.
//
// We mean it.
//
#include <qglobal.h>
#include <QtCore/QList>
#include <QtCore/QQueue>
#include <QtBluetooth/qbluetooth.h>
#include <QtBluetooth/qlowenergycharacteristic.h>
#include <QtBluetooth/qlowenergyservicedata.h>
#include "qlowenergycontroller.h"
#include "qlowenergycontrollerbase_p.h"
#include <winrt/Windows.Foundation.h>
#include <winrt/Windows.Devices.Bluetooth.h>
#include <winrt/Windows.Devices.Bluetooth.GenericAttributeProfile.h>
using namespace winrt::Windows::Devices::Bluetooth;
using namespace winrt::Windows::Devices::Bluetooth::GenericAttributeProfile;
namespace winrt
{
using AsyncStatus = Windows::Foundation::AsyncStatus;
using IInspectable = Windows::Foundation::IInspectable;
}
#include <functional>
QT_BEGIN_NAMESPACE
class QLowEnergyServiceData;
class QTimer;
extern void registerQLowEnergyControllerMetaType();
class QLowEnergyControllerPrivateWinRT final : public QLowEnergyControllerPrivate
{
Q_OBJECT
public:
QLowEnergyControllerPrivateWinRT();
~QLowEnergyControllerPrivateWinRT() override;
void init() override;
void connectToDevice() override;
void disconnectFromDevice() override;
void discoverServices() override;
void discoverServiceDetails(const QBluetoothUuid &service,
QLowEnergyService::DiscoveryMode mode) override;
void startAdvertising(const QLowEnergyAdvertisingParameters ¶ms,
const QLowEnergyAdvertisingData &advertisingData,
const QLowEnergyAdvertisingData &scanResponseData) override;
void stopAdvertising() override;
void requestConnectionUpdate(const QLowEnergyConnectionParameters ¶ms) override;
// read data
void readCharacteristic(const QSharedPointer<QLowEnergyServicePrivate> service,
const QLowEnergyHandle charHandle) override;
void readDescriptor(const QSharedPointer<QLowEnergyServicePrivate> service,
const QLowEnergyHandle charHandle,
const QLowEnergyHandle descriptorHandle) override;
// write data
void writeCharacteristic(const QSharedPointer<QLowEnergyServicePrivate> service,
const QLowEnergyHandle charHandle,
const QByteArray &newValue, QLowEnergyService::WriteMode mode) override;
void writeDescriptor(const QSharedPointer<QLowEnergyServicePrivate> service,
const QLowEnergyHandle charHandle,
const QLowEnergyHandle descriptorHandle,
const QByteArray &newValue) override;
void addToGenericAttributeList(const QLowEnergyServiceData &service,
QLowEnergyHandle startHandle) override;
int mtu() const override;
signals:
void characteristicChanged(quint16 charHandle, const QByteArray &data);
void abortConnection();
private slots:
void handleCharacteristicChanged(quint16 charHandle, const QByteArray &data);
void handleServiceHandlerError(const QString &error);
private:
void handleConnectionError(const char *logMessage);
BluetoothLEDevice mDevice = nullptr;
GattSession mGattSession = nullptr;
winrt::event_token mStatusChangedToken{ 0 };
winrt::event_token mMtuChangedToken{ 0 };
struct ValueChangedEntry {
ValueChangedEntry() {}
ValueChangedEntry(GattCharacteristic c,
winrt::event_token t)
: characteristic(c)
, token(t)
{
}
GattCharacteristic characteristic = nullptr;
winrt::event_token token = { 0 };
};
QList<ValueChangedEntry> mValueChangedTokens;
QMap<QBluetoothUuid, GattDeviceService> m_openedServices;
QSet<QBluetoothUuid> m_requestDetailsServiceUuids;
using NativeServiceCallback = std::function<void(GattDeviceService)>;
bool getNativeService(const QBluetoothUuid &serviceUuid, NativeServiceCallback callback);
using NativeCharacteristicCallback = std::function<void(GattCharacteristic)>;
bool getNativeCharacteristic(const QBluetoothUuid &serviceUuid,
const QBluetoothUuid &charUuid,
NativeCharacteristicCallback callback);
void registerForValueChanges(const QBluetoothUuid &serviceUuid, const QBluetoothUuid &charUuid);
void unregisterFromValueChanges();
void onValueChange(GattCharacteristic characteristic, GattValueChangedEventArgs const &args);
void onMtuChange(GattSession session, winrt::IInspectable args);
bool registerForMtuChanges();
void unregisterFromMtuChanges();
bool registerForStatusChanges();
void unregisterFromStatusChanges();
void onStatusChange(BluetoothLEDevice dev, winrt::IInspectable args);
void obtainIncludedServices(QSharedPointer<QLowEnergyServicePrivate> servicePointer,
GattDeviceService nativeService);
void onServiceDiscoveryFinished(winrt::Windows::Foundation::IAsyncOperation<GattDeviceServicesResult> const &op,
winrt::AsyncStatus status);
void readCharacteristicHelper(const QSharedPointer<QLowEnergyServicePrivate> service,
const QLowEnergyHandle charHandle,
GattCharacteristic characteristic);
void readDescriptorHelper(const QSharedPointer<QLowEnergyServicePrivate> service,
const QLowEnergyHandle charHandle,
const QLowEnergyHandle descriptorHandle,
GattCharacteristic characteristic);
void writeCharacteristicHelper(const QSharedPointer<QLowEnergyServicePrivate> service,
const QLowEnergyHandle charHandle, const QByteArray &newValue,
bool writeWithResponse,
GattCharacteristic characteristic);
void writeDescriptorHelper(const QSharedPointer<QLowEnergyServicePrivate> service,
const QLowEnergyHandle charHandle,
const QLowEnergyHandle descriptorHandle,
const QByteArray &newValue,
GattCharacteristic characteristic);
void discoverServiceDetailsHelper(const QBluetoothUuid &service,
QLowEnergyService::DiscoveryMode mode,
GattDeviceService deviceService);
void clearAllServices();
void closeAndRemoveService(const QBluetoothUuid &uuid);
};
QT_END_NAMESPACE
#endif // QLOWENERGYCONTROLLERPRIVATEWINRT_P_H
|