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 QLOWENERGYCONTROLLERPRIVATEDBUS_P_H
5#define QLOWENERGYCONTROLLERPRIVATEDBUS_P_H
6
7
8//
9// W A R N I N G
10// -------------
11//
12// This file is not part of the Qt API. It exists purely as an
13// implementation detail. This header file may change from version to
14// version without notice, or even be removed.
15//
16// We mean it.
17//
18
19#include "qlowenergycontroller.h"
20#include "qlowenergycontrollerbase_p.h"
21#include "qleadvertiser_bluezdbus_p.h"
22
23#include <QtDBus/QDBusObjectPath>
24
25namespace QtBluetoothPrivate {
26
27class OrgBluezAdapter1Interface;
28class OrgBluezBattery1Interface;
29class OrgBluezDevice1Interface;
30class OrgBluezGattCharacteristic1Interface;
31class OrgBluezGattDescriptor1Interface;
32class OrgBluezGattService1Interface;
33class OrgFreedesktopDBusObjectManagerInterface;
34class OrgFreedesktopDBusPropertiesInterface;
35
36} // namespace QtBluetoothPrivate
37
38QT_BEGIN_NAMESPACE
39
40class QtBluezPeripheralApplication;
41class QtBluezPeripheralConnectionManager;
42class QDBusPendingCallWatcher;
43
44class QLowEnergyControllerPrivateBluezDBus final : public QLowEnergyControllerPrivate
45{
46 Q_OBJECT
47public:
48 QLowEnergyControllerPrivateBluezDBus(const QString& adapterPathWithPeripheralSupport = {});
49 ~QLowEnergyControllerPrivateBluezDBus() override;
50
51 void init() override;
52 void connectToDevice() override;
53 void disconnectFromDevice() override;
54
55 void discoverServices() override;
56 void discoverServiceDetails(const QBluetoothUuid &service,
57 QLowEnergyService::DiscoveryMode mode) override;
58
59 void readCharacteristic(
60 const QSharedPointer<QLowEnergyServicePrivate> service,
61 const QLowEnergyHandle charHandle) override;
62 void readDescriptor(
63 const QSharedPointer<QLowEnergyServicePrivate> service,
64 const QLowEnergyHandle charHandle,
65 const QLowEnergyHandle descriptorHandle) override;
66
67 void writeCharacteristic(
68 const QSharedPointer<QLowEnergyServicePrivate> service,
69 const QLowEnergyHandle charHandle,
70 const QByteArray &newValue,
71 QLowEnergyService::WriteMode writeMode) override;
72 void writeDescriptor(
73 const QSharedPointer<QLowEnergyServicePrivate> service,
74 const QLowEnergyHandle charHandle,
75 const QLowEnergyHandle descriptorHandle,
76 const QByteArray &newValue) override;
77
78 void startAdvertising(
79 const QLowEnergyAdvertisingParameters &params,
80 const QLowEnergyAdvertisingData &advertisingData,
81 const QLowEnergyAdvertisingData &scanResponseData) override;
82 void stopAdvertising() override;
83
84 void requestConnectionUpdate(
85 const QLowEnergyConnectionParameters & params) override;
86 void addToGenericAttributeList(
87 const QLowEnergyServiceData &service,
88 QLowEnergyHandle startHandle) override;
89
90 int mtu() const override;
91
92private:
93 void connectToDeviceHelper();
94 void resetController();
95
96 void scheduleNextJob();
97
98private slots:
99 void devicePropertiesChanged(const QString &interface, const QVariantMap &changedProperties,
100 const QStringList &invalidatedProperties);
101 void characteristicPropertiesChanged(QLowEnergyHandle charHandle, const QString &interface,
102 const QVariantMap &changedProperties,
103 const QStringList &invalidatedProperties);
104 void interfacesRemoved(const QDBusObjectPath &objectPath, const QStringList &interfaces);
105
106 void onCharReadFinished(QDBusPendingCallWatcher *call);
107 void onDescReadFinished(QDBusPendingCallWatcher *call);
108 void onCharWriteFinished(QDBusPendingCallWatcher *call);
109 void onDescWriteFinished(QDBusPendingCallWatcher *call);
110private:
111
112 QtBluetoothPrivate::OrgBluezAdapter1Interface* adapter{};
113 QtBluetoothPrivate::OrgBluezDevice1Interface* device{};
114 QtBluetoothPrivate::OrgFreedesktopDBusObjectManagerInterface* managerBluez{};
115 QtBluetoothPrivate::OrgFreedesktopDBusPropertiesInterface* deviceMonitor{};
116 QString adapterPathWithPeripheralSupport;
117
118 int remoteMtu{-1};
119 QtBluezPeripheralApplication* peripheralApplication{};
120 QtBluezPeripheralConnectionManager* peripheralConnectionManager{};
121 QLeDBusAdvertiser *advertiser{};
122 void handleAdvertisingError();
123 void handlePeripheralApplicationError();
124 void handlePeripheralApplicationRegistered();
125 void handlePeripheralConnectivityChanged(bool connected);
126 void handlePeripheralCharacteristicValueUpdate(QLowEnergyHandle handle,
127 const QByteArray& value);
128 void handlePeripheralDescriptorValueUpdate(QLowEnergyHandle characteristicHandle,
129 QLowEnergyHandle descriptorHandle,
130 const QByteArray& value);
131 void handlePeripheralRemoteDeviceChanged(const QBluetoothAddress& address,
132 const QString& name,
133 quint16 mtu);
134 bool pendingConnect = false;
135 bool disconnectSignalRequired = false;
136
137 struct GattCharacteristic
138 {
139 QSharedPointer<QtBluetoothPrivate::OrgBluezGattCharacteristic1Interface> characteristic;
140 QSharedPointer<QtBluetoothPrivate::OrgFreedesktopDBusPropertiesInterface> charMonitor;
141 QList<QSharedPointer<QtBluetoothPrivate::OrgBluezGattDescriptor1Interface>> descriptors;
142 };
143
144 struct GattService
145 {
146 QString servicePath;
147 QList<GattCharacteristic> characteristics;
148
149 bool hasBatteryService = false;
150 QSharedPointer<QtBluetoothPrivate::OrgBluezBattery1Interface> batteryInterface;
151 };
152
153 QHash<QBluetoothUuid, GattService> dbusServices;
154 QLowEnergyHandle runningHandle = 1;
155
156 struct GattJob {
157 enum JobFlag {
158 Unset = 0x00,
159 CharRead = 0x01,
160 CharWrite = 0x02,
161 DescRead = 0x04,
162 DescWrite = 0x08,
163 ServiceDiscovery = 0x10,
164 LastServiceDiscovery = 0x20
165 };
166 Q_DECLARE_FLAGS(JobFlags, JobFlag)
167
168 JobFlags flags = GattJob::Unset;
169 QLowEnergyHandle handle;
170 QByteArray value;
171 QLowEnergyService::WriteMode writeMode = QLowEnergyService::WriteWithResponse;
172 QSharedPointer<QLowEnergyServicePrivate> service;
173 };
174
175 QList<GattJob> jobs;
176 bool jobPending = false;
177
178 void prepareNextJob();
179 void discoverBatteryServiceDetails(GattService &dbusData,
180 QSharedPointer<QLowEnergyServicePrivate> serviceData);
181 void executeClose(QLowEnergyController::Error newError);
182};
183
184QT_END_NAMESPACE
185
186#endif // QLOWENERGYCONTROLLERPRIVATEDBUS_P_H
187

source code of qtconnectivity/src/bluetooth/qlowenergycontroller_bluezdbus_p.h