1/****************************************************************************
2**
3** Copyright (C) 2017 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtBluetooth module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#ifndef QLOWENERGYCONTROLLERPRIVATEDBUS_P_H
41#define QLOWENERGYCONTROLLERPRIVATEDBUS_P_H
42
43
44//
45// W A R N I N G
46// -------------
47//
48// This file is not part of the Qt API. It exists purely as an
49// implementation detail. This header file may change from version to
50// version without notice, or even be removed.
51//
52// We mean it.
53//
54
55#include "qlowenergycontroller.h"
56#include "qlowenergycontrollerbase_p.h"
57
58#include <QtDBus/QDBusObjectPath>
59
60class OrgBluezAdapter1Interface;
61class OrgBluezBattery1Interface;
62class OrgBluezDevice1Interface;
63class OrgBluezGattCharacteristic1Interface;
64class OrgBluezGattDescriptor1Interface;
65class OrgBluezGattService1Interface;
66class OrgFreedesktopDBusObjectManagerInterface;
67class OrgFreedesktopDBusPropertiesInterface;
68
69QT_BEGIN_NAMESPACE
70
71class QDBusPendingCallWatcher;
72
73class QLowEnergyControllerPrivateBluezDBus final : public QLowEnergyControllerPrivate
74{
75 Q_OBJECT
76public:
77 QLowEnergyControllerPrivateBluezDBus();
78 ~QLowEnergyControllerPrivateBluezDBus() override;
79
80 void init() override;
81 void connectToDevice() override;
82 void disconnectFromDevice() override;
83
84 void discoverServices() override;
85 void discoverServiceDetails(const QBluetoothUuid &/*service*/) override;
86
87 void readCharacteristic(
88 const QSharedPointer<QLowEnergyServicePrivate> /*service*/,
89 const QLowEnergyHandle /*charHandle*/) override;
90 void readDescriptor(
91 const QSharedPointer<QLowEnergyServicePrivate> /*service*/,
92 const QLowEnergyHandle /*charHandle*/,
93 const QLowEnergyHandle /*descriptorHandle*/) override;
94
95 void writeCharacteristic(
96 const QSharedPointer<QLowEnergyServicePrivate> /*service*/,
97 const QLowEnergyHandle /*charHandle*/,
98 const QByteArray &/*newValue*/,
99 QLowEnergyService::WriteMode /*writeMode*/) override;
100 void writeDescriptor(
101 const QSharedPointer<QLowEnergyServicePrivate> /*service*/,
102 const QLowEnergyHandle /*charHandle*/,
103 const QLowEnergyHandle /*descriptorHandle*/,
104 const QByteArray &/*newValue*/) override;
105
106 void startAdvertising(
107 const QLowEnergyAdvertisingParameters &/* params */,
108 const QLowEnergyAdvertisingData &/* advertisingData */,
109 const QLowEnergyAdvertisingData &/* scanResponseData */) override;
110 void stopAdvertising() override;
111
112 void requestConnectionUpdate(
113 const QLowEnergyConnectionParameters & /* params */) override;
114 void addToGenericAttributeList(
115 const QLowEnergyServiceData &/* service */,
116 QLowEnergyHandle /* startHandle */) override;
117
118 QLowEnergyService *addServiceHelper(const QLowEnergyServiceData &service) override;
119
120
121private:
122 void connectToDeviceHelper();
123 void resetController();
124
125 void scheduleNextJob();
126
127private slots:
128 void devicePropertiesChanged(const QString &interface, const QVariantMap &changedProperties,
129 const QStringList &/*invalidatedProperties*/);
130 void characteristicPropertiesChanged(QLowEnergyHandle charHandle, const QString &interface,
131 const QVariantMap &changedProperties,
132 const QStringList &invalidatedProperties);
133 void interfacesRemoved(const QDBusObjectPath &objectPath, const QStringList &interfaces);
134
135 void onCharReadFinished(QDBusPendingCallWatcher *call);
136 void onDescReadFinished(QDBusPendingCallWatcher *call);
137 void onCharWriteFinished(QDBusPendingCallWatcher *call);
138 void onDescWriteFinished(QDBusPendingCallWatcher *call);
139private:
140 OrgBluezAdapter1Interface* adapter{};
141 OrgBluezDevice1Interface* device{};
142 OrgFreedesktopDBusObjectManagerInterface* managerBluez{};
143 OrgFreedesktopDBusPropertiesInterface* deviceMonitor{};
144
145 bool pendingConnect = false;
146 bool disconnectSignalRequired = false;
147
148 struct GattCharacteristic
149 {
150 QSharedPointer<OrgBluezGattCharacteristic1Interface> characteristic;
151 QSharedPointer<OrgFreedesktopDBusPropertiesInterface> charMonitor;
152 QVector<QSharedPointer<OrgBluezGattDescriptor1Interface>> descriptors;
153 };
154
155 struct GattService
156 {
157 QString servicePath;
158 QVector<GattCharacteristic> characteristics;
159
160 bool hasBatteryService = false;
161 QSharedPointer<OrgBluezBattery1Interface> batteryInterface;
162 };
163
164 QHash<QBluetoothUuid, GattService> dbusServices;
165 QLowEnergyHandle runningHandle = 1;
166
167 struct GattJob {
168 enum JobFlag {
169 Unset = 0x00,
170 CharRead = 0x01,
171 CharWrite = 0x02,
172 DescRead = 0x04,
173 DescWrite = 0x08,
174 ServiceDiscovery = 0x10,
175 LastServiceDiscovery = 0x20
176 };
177 Q_DECLARE_FLAGS(JobFlags, JobFlag)
178
179 JobFlags flags = GattJob::Unset;
180 QLowEnergyHandle handle;
181 QByteArray value;
182 QLowEnergyService::WriteMode writeMode = QLowEnergyService::WriteWithResponse;
183 QSharedPointer<QLowEnergyServicePrivate> service;
184 };
185
186 QVector<GattJob> jobs;
187 bool jobPending = false;
188
189 void prepareNextJob();
190 void discoverBatteryServiceDetails(GattService &dbusData,
191 QSharedPointer<QLowEnergyServicePrivate> serviceData);
192 void executeClose(QLowEnergyController::Error newError);
193};
194
195QT_END_NAMESPACE
196
197#endif // QLOWENERGYCONTROLLERPRIVATEDBUS_P_H
198