1// Copyright (C) 2022 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 BLUEZ_PERIPHERAL_OBJECTS_P_H
5#define BLUEZ_PERIPHERAL_OBJECTS_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 "bluez5_helper_p.h"
19
20#include <QtBluetooth/qbluetooth.h>
21#include <QtBluetooth/QBluetoothUuid>
22#include <QtBluetooth/QLowEnergyDescriptorData>
23#include <QtBluetooth/QLowEnergyCharacteristicData>
24#include <QtBluetooth/QLowEnergyServiceData>
25
26namespace QtBluetoothPrivate {
27
28class OrgFreedesktopDBusPropertiesAdaptor;
29class OrgBluezGattCharacteristic1Adaptor;
30class OrgBluezGattDescriptor1Adaptor;
31class OrgBluezGattService1Adaptor;
32
33} // namespace QtBluetoothPrivate
34
35QT_BEGIN_NAMESPACE
36
37// The QtBluezPeripheralGattObject is the base class for services, characteristics, and descriptors
38class QtBluezPeripheralGattObject : public QObject
39{
40 Q_OBJECT
41
42public:
43 QtBluezPeripheralGattObject(const QString& objectPath, const QString& uuid,
44 QLowEnergyHandle handle, QObject* parent = nullptr);
45 virtual ~QtBluezPeripheralGattObject();
46
47 // List of properties exposed by this object, used when Bluez inquiries details
48 virtual InterfaceList properties() const = 0;
49
50 bool registerObject();
51 void unregisterObject();
52
53public:
54 // DBus object path
55 QString objectPath;
56 // UUID of the gatt object
57 QString uuid;
58 // QtBluetooth internal handle and reference to the application
59 // to read and write values towards the Qt API
60 QLowEnergyHandle handle;
61 // Bluez DBus Gatt objects need to provide this
62 QtBluetoothPrivate::OrgFreedesktopDBusPropertiesAdaptor* propertiesAdaptor{};
63
64signals:
65 void remoteDeviceAccessEvent(const QString& remoteDeviceObjectPath, quint16 mtu);
66
67protected:
68 void accessEvent(const QVariantMap& options);
69
70private:
71 bool m_registered = false;
72};
73
74class QtBluezPeripheralDescriptor : public QtBluezPeripheralGattObject
75{
76 Q_OBJECT
77
78public:
79 QtBluezPeripheralDescriptor(const QLowEnergyDescriptorData& descriptorData,
80 const QString& characteristicPath, quint16 ordinal,
81 QLowEnergyHandle handle, QLowEnergyHandle characteristicHandle,
82 QObject* parent);
83
84 InterfaceList properties() const final;
85
86 // org.bluez.GattDescriptor1
87 // This function is invoked when remote device reads the value. Sets error if any
88 Q_INVOKABLE QByteArray ReadValue(const QVariantMap &options, QString &error);
89
90 // org.bluez.GattDescriptor1
91 // This function is invoked when remote device writes a value. Returns Bluez DBus error if any
92 Q_INVOKABLE QString WriteValue(const QByteArray &value, const QVariantMap &options);
93
94 // Call this function when value has been updated locally (server/user application side)
95 bool localValueUpdate(const QByteArray& value);
96
97signals:
98 void valueUpdatedByRemote(QLowEnergyHandle characteristicHandle,
99 QLowEnergyHandle descriptorHandle, const QByteArray& value);
100
101private:
102 void initializeFlags(const QLowEnergyDescriptorData& data);
103
104 QtBluetoothPrivate::OrgBluezGattDescriptor1Adaptor* m_adaptor{};
105 QString m_characteristicPath;
106 QByteArray m_value;
107 QStringList m_flags;
108 QLowEnergyHandle m_characteristicHandle;
109};
110
111
112class QtBluezPeripheralCharacteristic : public QtBluezPeripheralGattObject
113{
114 Q_OBJECT
115
116public:
117 QtBluezPeripheralCharacteristic(const QLowEnergyCharacteristicData& characteristicData,
118 const QString& servicePath, quint16 ordinal,
119 QLowEnergyHandle handle, QObject* parent);
120
121 InterfaceList properties() const final;
122
123 // org.bluez.GattCharacteristic1
124 // This function is invoked when remote device reads the value. Sets error if any
125 Q_INVOKABLE QByteArray ReadValue(const QVariantMap &options, QString& error);
126
127 // org.bluez.GattCharacteristic1
128 // This function is invoked when remote device writes a value. Returns Bluez DBus error if any
129 Q_INVOKABLE QString WriteValue(const QByteArray &value, const QVariantMap &options);
130
131 // org.bluez.GattCharacteristic1
132 // These are called when remote client enables or disables NTF/IND
133 Q_INVOKABLE void StartNotify();
134 Q_INVOKABLE void StopNotify();
135
136 // Call this function when value has been updated locally (server/user application side)
137 bool localValueUpdate(const QByteArray& value);
138
139signals:
140 void valueUpdatedByRemote(QLowEnergyHandle handle, const QByteArray& value);
141
142private:
143 void initializeValue(const QByteArray& value);
144 void initializeFlags(const QLowEnergyCharacteristicData& data);
145
146 QtBluetoothPrivate::OrgBluezGattCharacteristic1Adaptor* m_adaptor{};
147 QString m_servicePath;
148 bool m_notifying{false};
149 QByteArray m_value;
150 QStringList m_flags;
151 int m_minimumValueLength;
152 int m_maximumValueLength;
153};
154
155class QtBluezPeripheralService : public QtBluezPeripheralGattObject
156{
157 Q_OBJECT
158public:
159 QtBluezPeripheralService(const QLowEnergyServiceData &serviceData,
160 const QString& applicationPath, quint16 ordinal,
161 QLowEnergyHandle handle, QObject* parent);
162
163 InterfaceList properties() const final;
164 void addIncludedService(const QString& objectPath);
165
166private:
167 const bool m_isPrimary;
168 QtBluetoothPrivate::OrgBluezGattService1Adaptor* m_adaptor{};
169 QList<QDBusObjectPath> m_includedServices;
170};
171
172
173QT_END_NAMESPACE
174
175#endif
176

source code of qtconnectivity/src/bluetooth/bluez/bluezperipheralobjects_p.h